Add dwarf2_per_objfile parameter to recursively_compute_inclusions
[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
ab432490
SM
915 cutu_reader (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
c0ab21c2 917 struct abbrev_table *abbrev_table,
6751ebae 918 int use_existing_cu,
c0ab21c2
TT
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 922 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
c0ab21c2
TT
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
930 bool dummy_p = false;
931
6751ebae
TT
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
c0ab21c2 936private:
9e021579
SM
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
6751ebae 939 int use_existing_cu);
c0ab21c2
TT
940
941 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949};
dee91e82 950
c906108c 951/* When we construct a partial symbol table entry we only
0963b4bd 952 need this much information. */
6f06d47b 953struct partial_die_info : public allocate_on_obstack
c906108c 954 {
6f06d47b
YQ
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
52356b79
YQ
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
48fbe735
YQ
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
72bf9492 972 /* Offset of this DIE. */
6f06d47b 973 const sect_offset sect_off;
72bf9492
DJ
974
975 /* DWARF-2 tag for this DIE. */
6f06d47b 976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 977
72bf9492 978 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
979 const unsigned int has_children : 1;
980
72bf9492
DJ
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
481860b3 986 unsigned int may_be_inlined : 1;
72bf9492 987
0c1b455e
TT
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
72bf9492
DJ
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
fa4028e9
JB
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
ff908ebf
AW
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
98bfdba5
PA
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
52356b79 1004 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1005 unsigned int fixup_called : 1;
1006
36586728
TT
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
72bf9492 1013 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1014 sometimes a default name for unnamed DIEs. */
6f06d47b 1015 const char *name = nullptr;
72bf9492 1016
abc72ce4 1017 /* The linkage name, if present. */
6f06d47b 1018 const char *linkage_name = nullptr;
abc72ce4 1019
72bf9492
DJ
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
6f06d47b 1023 const char *scope = nullptr;
72bf9492 1024
95554aad
TT
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1032 sect_offset sect_off;
6f06d47b 1033 } d {};
72bf9492
DJ
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
72bf9492 1038
93311388 1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1040 DW_AT_sibling, if any. */
48fbe735
YQ
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1043 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
6f06d47b 1048 sect_offset spec_offset {};
72bf9492
DJ
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
6f06d47b
YQ
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
c906108c
SS
1085 };
1086
c906108c
SS
1087/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090static int bits_per_byte = 8;
1091
9c6a1327
TT
1092struct variant_part_builder;
1093
1094/* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
2ddeaf8a
TT
1096
1097struct variant_field
1098{
9c6a1327
TT
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
2ddeaf8a
TT
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
9c6a1327
TT
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114};
1115
1116/* This represents a DW_TAG_variant_part. */
1117
1118struct variant_part_builder
1119{
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
2ddeaf8a
TT
1128};
1129
52059ffd
TT
1130struct nextfield
1131{
be2daae6
TT
1132 int accessibility = 0;
1133 int virtuality = 0;
9c6a1327
TT
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
be2daae6 1138 struct field field {};
52059ffd
TT
1139};
1140
1141struct fnfieldlist
1142{
be2daae6
TT
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
52059ffd
TT
1145};
1146
c906108c
SS
1147/* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150struct field_info
c5aa993b 1151 {
0963b4bd 1152 /* List of data member and baseclasses fields. */
be2daae6
TT
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
c906108c 1155
85102364 1156 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1157 int non_public_fields = 0;
c906108c 1158
c5aa993b
JM
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
be2daae6 1162 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1166 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
be2daae6 1170 std::vector<struct decl_field> nested_types_list;
317f7127 1171
9c6a1327
TT
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
317f7127
TT
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
c5aa993b 1184 };
c906108c 1185
ae038cb0
DJ
1186/* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1191static int dwarf_max_cache_age = 5;
920d2a44 1192static void
b4f54984
DE
1193show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
920d2a44 1195{
3e43a32a 1196 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1197 "DWARF compilation units is %s.\n"),
920d2a44
AC
1198 value);
1199}
4390d890 1200\f
c906108c
SS
1201/* local function prototypes */
1202
918dd910
JK
1203static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
891813be 1206static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
0018ea6f 1209
f1902523
JK
1210static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
3e225074 1212 struct die_info *type_unit_die);
f1902523 1213
ed2dc618
SM
1214static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1216
72bf9492
DJ
1217static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1219 int, struct dwarf2_cu *);
c906108c 1220
72bf9492
DJ
1221static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
63d06c5c 1223
72bf9492
DJ
1224static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1226 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1227
5d7cb8df 1228static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1229 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1230 struct dwarf2_cu *cu);
1231
72bf9492
DJ
1232static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
91c24f0a 1234
bc30ff58
JB
1235static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1237 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1238
d521ce57 1239static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1240
dee91e82 1241static struct partial_die_info *load_partial_dies
d521ce57 1242 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1243
fb816e8b
TV
1244/* A pair of partial_die_info and compilation unit. */
1245struct cu_partial_die_info
1246{
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
122cf0f2
AB
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
405feb71 1255 { /* Nothing. */ }
122cf0f2
AB
1256
1257private:
1258 cu_partial_die_info () = delete;
fb816e8b
TV
1259};
1260
122cf0f2
AB
1261static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
72bf9492 1263
d521ce57
TT
1264static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1266 const gdb_byte *, bool *need_reprocess);
1267
1268static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1272
ed2dc618
SM
1273static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1276
ed2dc618
SM
1277static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1280
ed2dc618 1281static const char *read_indirect_string_at_offset
4f44ae6c 1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1283
d521ce57
TT
1284static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
3019eac3
DE
1286 unsigned int *);
1287
18a8505e
AT
1288static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
3019eac3 1293
e142c38c 1294static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1295
e142c38c
DJ
1296static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
c906108c 1298
7d45c7c3
KB
1299static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
a084a2a6
AT
1302static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
05cf31d1
JB
1304static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
e142c38c 1307static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1308
e142c38c 1309static struct die_info *die_specification (struct die_info *die,
f2f0e013 1310 struct dwarf2_cu **);
63d06c5c 1311
9c541725 1312static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1313 struct dwarf2_cu *cu);
debd256d 1314
f3f5162e 1315static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1316 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1317 CORE_ADDR, int decode_mapping);
c906108c 1318
804d2729
TT
1319static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
c906108c 1321
a14ed312 1322static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1323 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1324
ff39bb5e 1325static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1326 struct dwarf2_cu *);
c906108c 1327
ff39bb5e 1328static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
12df843f 1332 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1333 const gdb_byte **bytes,
98bfdba5 1334 struct dwarf2_locexpr_baton **baton);
2df3850c 1335
e7c27a73 1336static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1337
b4ba55a1
JB
1338static int need_gnat_info (struct dwarf2_cu *);
1339
3e43a32a
MS
1340static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
b4ba55a1
JB
1342
1343static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
e7c27a73
DJ
1346static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
c906108c 1348
ff39bb5e 1349static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1350 struct dwarf2_cu *);
c906108c 1351
f792889a 1352static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1353
673bfd45
DE
1354static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
0d5cff50 1356static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1357
6e70227d 1358static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
63d06c5c 1361
e7c27a73 1362static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1363
348e048f
DE
1364static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
e7c27a73 1366static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1367
e7c27a73 1368static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1369
96408a79
SA
1370static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
71a3c369
TT
1372static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
ff013f42 1374static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1375 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1376
41144253 1377/* Return the .debug_loclists section to use for cu. */
1378static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
3a2b436a 1380/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1381 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1382enum pc_bounds_kind
1383{
e385593e 1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1385 PC_BOUNDS_NOT_PRESENT,
1386
e385593e
JK
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
3a2b436a
JK
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396};
1397
1398static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
891813be 1401 dwarf2_psymtab *);
c906108c 1402
fae299cd
DC
1403static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
801e3a5b
JB
1407static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
a14ed312 1410static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1411 struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1414 struct type *, struct dwarf2_cu *);
c906108c 1415
a14ed312 1416static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1417 struct die_info *, struct type *,
e7c27a73 1418 struct dwarf2_cu *);
c906108c 1419
a14ed312 1420static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1421 struct type *,
1422 struct dwarf2_cu *);
c906108c 1423
134d01f1 1424static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1425
e7c27a73 1426static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1427
e7c27a73 1428static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1429
5d7cb8df
JK
1430static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
804d2729 1432static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1433
27aa8d6a
SW
1434static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
74921315
KS
1436static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
f55ee35c
JK
1438static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
38d518c9 1441static const char *namespace_name (struct die_info *die,
e142c38c 1442 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1443
134d01f1 1444static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
7d79de9a
TT
1446static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
c906108c 1448
6e70227d 1449static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1450 struct dwarf2_cu *);
1451
bf6af496 1452static struct die_info *read_die_and_siblings_1
d521ce57 1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1454 struct die_info *);
639d11d3 1455
dee91e82 1456static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
639d11d3
DC
1459 struct die_info *parent);
1460
d521ce57
TT
1461static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
3e225074 1463 int);
3019eac3 1464
d521ce57 1465static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1466 struct die_info **, const gdb_byte *);
93311388 1467
e7c27a73 1468static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1469
15d034d0 1470static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1471 struct objfile *);
71c25dea 1472
15d034d0 1473static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1474
15d034d0 1475static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
ca69b9e6
DE
1479static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
e142c38c 1482static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1483 struct dwarf2_cu **);
9219021c 1484
d97bc12b
DE
1485static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487static void dump_die_for_error (struct die_info *);
1488
1489static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
c906108c 1491
d97bc12b 1492/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1493
51545339 1494static void store_in_ref_table (struct die_info *,
10b3939b 1495 struct dwarf2_cu *);
c906108c 1496
348e048f 1497static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
10b3939b 1501static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1502 const struct attribute *,
f2f0e013 1503 struct dwarf2_cu **);
c906108c 1504
348e048f 1505static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1506 const struct attribute *,
348e048f
DE
1507 struct dwarf2_cu **);
1508
ac9ec31b
DE
1509static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1513 const struct attribute *,
ac9ec31b
DE
1514 struct dwarf2_cu *);
1515
ab432490
SM
1516static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
348e048f 1518
ab432490
SM
1519static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
348e048f 1521
63e43d3a
PMR
1522static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1524 struct dynamic_prop *prop, struct type *type);
63e43d3a 1525
c906108c
SS
1526/* memory allocation interface */
1527
7b5a2f43 1528static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1529
b60c80d6 1530static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1531
43f3e411 1532static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1533
8cf6f0b1
TT
1534static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
ff39bb5e 1536 const struct attribute *attr);
8cf6f0b1 1537
ff39bb5e 1538static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1539 struct symbol *sym,
f1e6e072
TT
1540 struct dwarf2_cu *cu,
1541 int is_block);
4c2df51b 1542
d521ce57
TT
1543static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
4bb7a0a7 1546
72bf9492
DJ
1547static hashval_t partial_die_hash (const void *item);
1548
1549static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
ae038cb0 1551static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1554
9816fde3 1555static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
93311388 1558
ed2dc618 1559static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1560
dee91e82 1561static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1562
f792889a
DJ
1563static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1c379e20 1565
ed2dc618 1566static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1567
ed2dc618 1568static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1569
ab432490
SM
1570static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
10b3939b 1574
95554aad
TT
1575static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1576 enum language);
10b3939b 1577
f4dc4d17
DE
1578static void process_full_type_unit (struct dwarf2_per_cu_data *,
1579 enum language);
1580
10b3939b
DJ
1581static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
ae038cb0
DJ
1584static void dwarf2_mark (struct dwarf2_cu *);
1585
1586static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
b64f50a1 1588static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1589 struct dwarf2_per_cu_data *);
673bfd45 1590
f792889a 1591static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1592
95554aad
TT
1593static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1594 enum language pretend_language);
1595
ed2dc618 1596static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1597
b303c6f6
AB
1598/* Class, the destructor of which frees all allocated queue entries. This
1599 will only have work to do if an error was thrown while processing the
1600 dwarf. If no error was thrown then the queue entries should have all
1601 been processed, and freed, as we went along. */
1602
1603class dwarf2_queue_guard
1604{
1605public:
39856def
TT
1606 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1607 : m_per_objfile (per_objfile)
1608 {
1609 }
b303c6f6
AB
1610
1611 /* Free any entries remaining on the queue. There should only be
1612 entries left if we hit an error while processing the dwarf. */
1613 ~dwarf2_queue_guard ()
1614 {
39856def
TT
1615 /* Ensure that no memory is allocated by the queue. */
1616 std::queue<dwarf2_queue_item> empty;
5989a64e 1617 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1618 }
b303c6f6 1619
39856def 1620 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1621
39856def
TT
1622private:
1623 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1624};
1625
39856def
TT
1626dwarf2_queue_item::~dwarf2_queue_item ()
1627{
1628 /* Anything still marked queued is likely to be in an
1629 inconsistent state, so discard it. */
1630 if (per_cu->queued)
1631 {
1632 if (per_cu->cu != NULL)
1633 free_one_cached_comp_unit (per_cu);
1634 per_cu->queued = 0;
1635 }
1636}
1637
d721ba37
PA
1638/* The return type of find_file_and_directory. Note, the enclosed
1639 string pointers are only valid while this object is valid. */
1640
1641struct file_and_directory
1642{
1643 /* The filename. This is never NULL. */
1644 const char *name;
1645
1646 /* The compilation directory. NULL if not known. If we needed to
1647 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1648 points directly to the DW_AT_comp_dir string attribute owned by
1649 the obstack that owns the DIE. */
1650 const char *comp_dir;
1651
1652 /* If we needed to build a new string for comp_dir, this is what
1653 owns the storage. */
1654 std::string comp_dir_storage;
1655};
1656
1657static file_and_directory find_file_and_directory (struct die_info *die,
1658 struct dwarf2_cu *cu);
9291a0cd 1659
298e9637 1660static htab_up allocate_signatured_type_table ();
1fd400ff 1661
298e9637 1662static htab_up allocate_dwo_unit_table ();
3019eac3 1663
57d63ce2 1664static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1665 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1666 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1667 ULONGEST signature, int is_debug_types);
a2ce51a0 1668
ed2dc618
SM
1669static struct dwp_file *get_dwp_file
1670 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1671
3019eac3 1672static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1673 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1674
1675static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1676 (struct signatured_type *, const char *, const char *);
3019eac3 1677
89e63ee4
DE
1678static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1679
263db9a1
TT
1680/* A unique pointer to a dwo_file. */
1681
51ac9db5 1682typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1683
ed2dc618 1684static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1685
1b80a9fa 1686static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1687
1688static void free_line_header_voidp (void *arg);
4390d890
DE
1689\f
1690/* Various complaints about symbol reading that don't abort the process. */
1691
4390d890
DE
1692static void
1693dwarf2_debug_line_missing_file_complaint (void)
1694{
b98664d3 1695 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1696}
1697
1698static void
1699dwarf2_debug_line_missing_end_sequence_complaint (void)
1700{
b98664d3 1701 complaint (_(".debug_line section has line "
4390d890
DE
1702 "program sequence without an end"));
1703}
1704
1705static void
1706dwarf2_complex_location_expr_complaint (void)
1707{
b98664d3 1708 complaint (_("location expression too complex"));
4390d890
DE
1709}
1710
1711static void
1712dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1713 int arg3)
1714{
b98664d3 1715 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1716 arg1, arg2, arg3);
1717}
1718
4390d890
DE
1719static void
1720dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1721{
b98664d3 1722 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1723 arg1, arg2);
1724}
527f3840
JK
1725
1726/* Hash function for line_header_hash. */
1727
1728static hashval_t
1729line_header_hash (const struct line_header *ofs)
1730{
9c541725 1731 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1732}
1733
1734/* Hash function for htab_create_alloc_ex for line_header_hash. */
1735
1736static hashval_t
1737line_header_hash_voidp (const void *item)
1738{
9a3c8263 1739 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1740
1741 return line_header_hash (ofs);
1742}
1743
1744/* Equality function for line_header_hash. */
1745
1746static int
1747line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1748{
9a3c8263
SM
1749 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1750 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1751
9c541725 1752 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1753 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1754}
1755
4390d890 1756\f
9291a0cd 1757
330cdd98
PA
1758/* See declaration. */
1759
5989a64e
SM
1760dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1761 bool can_copy_)
c3699833
SM
1762 : obfd (obfd),
1763 can_copy (can_copy_)
330cdd98
PA
1764{
1765 if (names == NULL)
1766 names = &dwarf2_elf_names;
1767
330cdd98
PA
1768 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1769 locate_sections (obfd, sec, *names);
1770}
1771
5989a64e 1772dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1773{
1774 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1775 free_cached_comp_units ();
1776
b76e467d 1777 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1778 per_cu->imported_symtabs_free ();
fc8e7e75 1779
b2bdb8cf 1780 for (signatured_type *sig_type : all_type_units)
ae640021 1781 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1782
5989a64e 1783 /* Everything else should be on this->obstack. */
330cdd98
PA
1784}
1785
1786/* See declaration. */
1787
1788void
5989a64e 1789dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1790{
1791 dwarf2_per_cu_data *per_cu = read_in_chain;
1792 dwarf2_per_cu_data **last_chain = &read_in_chain;
1793 while (per_cu != NULL)
1794 {
1795 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1796
fcd3b13d 1797 delete per_cu->cu;
330cdd98
PA
1798 *last_chain = next_cu;
1799 per_cu = next_cu;
1800 }
1801}
1802
11ed8cad
TT
1803/* A helper class that calls free_cached_comp_units on
1804 destruction. */
1805
1806class free_cached_comp_units
1807{
1808public:
1809
1810 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1811 : m_per_objfile (per_objfile)
1812 {
1813 }
1814
1815 ~free_cached_comp_units ()
1816 {
5989a64e 1817 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1818 }
1819
1820 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1821
1822private:
1823
1824 dwarf2_per_objfile *m_per_objfile;
1825};
1826
af758d11
SM
1827/* See read.h. */
1828
1829bool
1830dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1831{
1832 gdb_assert (per_cu->index < this->m_symtabs.size ());
1833
1834 return this->m_symtabs[per_cu->index] != nullptr;
1835}
1836
1837/* See read.h. */
1838
1839compunit_symtab *
1840dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1841{
1842 gdb_assert (per_cu->index < this->m_symtabs.size ());
1843
1844 return this->m_symtabs[per_cu->index];
1845}
1846
1847/* See read.h. */
1848
1849void
1850dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1851 compunit_symtab *symtab)
1852{
1853 gdb_assert (per_cu->index < this->m_symtabs.size ());
1854 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1855
1856 this->m_symtabs[per_cu->index] = symtab;
1857}
1858
c906108c 1859/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1860 information and return true if we have enough to do something.
1861 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1862 ELF names are used. CAN_COPY is true for formats where symbol
1863 interposition is possible and so symbol values must follow copy
1864 relocation rules. */
c906108c
SS
1865
1866int
251d32d9 1867dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1868 const struct dwarf2_debug_sections *names,
1869 bool can_copy)
c906108c 1870{
97cbe998
SDJ
1871 if (objfile->flags & OBJF_READNEVER)
1872 return 0;
1873
ed2dc618
SM
1874 struct dwarf2_per_objfile *dwarf2_per_objfile
1875 = get_dwarf2_per_objfile (objfile);
1876
1877 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1878 {
1879 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1880 sharing yet). */
1881 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1882
1883 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1884 }
5bfd760d 1885
5989a64e
SM
1886 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1887 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1888 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1889 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1890}
1891
251d32d9
TG
1892/* When loading sections, we look either for uncompressed section or for
1893 compressed section names. */
233a11ab
CS
1894
1895static int
251d32d9
TG
1896section_is_p (const char *section_name,
1897 const struct dwarf2_section_names *names)
233a11ab 1898{
251d32d9
TG
1899 if (names->normal != NULL
1900 && strcmp (section_name, names->normal) == 0)
1901 return 1;
1902 if (names->compressed != NULL
1903 && strcmp (section_name, names->compressed) == 0)
1904 return 1;
1905 return 0;
233a11ab
CS
1906}
1907
330cdd98 1908/* See declaration. */
c906108c 1909
330cdd98 1910void
5989a64e
SM
1911dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1912 const dwarf2_debug_sections &names)
c906108c 1913{
fd361982 1914 flagword aflag = bfd_section_flags (sectp);
251d32d9 1915
dc7650b8
JK
1916 if ((aflag & SEC_HAS_CONTENTS) == 0)
1917 {
1918 }
950b7495
KS
1919 else if (elf_section_data (sectp)->this_hdr.sh_size
1920 > bfd_get_file_size (abfd))
1921 {
1922 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1923 warning (_("Discarding section %s which has a section size (%s"
1924 ") larger than the file size [in module %s]"),
1925 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1926 bfd_get_filename (abfd));
1927 }
330cdd98 1928 else if (section_is_p (sectp->name, &names.info))
c906108c 1929 {
330cdd98 1930 this->info.s.section = sectp;
fd361982 1931 this->info.size = bfd_section_size (sectp);
c906108c 1932 }
330cdd98 1933 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1934 {
330cdd98 1935 this->abbrev.s.section = sectp;
fd361982 1936 this->abbrev.size = bfd_section_size (sectp);
c906108c 1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.line))
c906108c 1939 {
330cdd98 1940 this->line.s.section = sectp;
fd361982 1941 this->line.size = bfd_section_size (sectp);
c906108c 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.loc))
c906108c 1944 {
330cdd98 1945 this->loc.s.section = sectp;
fd361982 1946 this->loc.size = bfd_section_size (sectp);
c906108c 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.loclists))
43988095 1949 {
330cdd98 1950 this->loclists.s.section = sectp;
fd361982 1951 this->loclists.size = bfd_section_size (sectp);
43988095 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1954 {
330cdd98 1955 this->macinfo.s.section = sectp;
fd361982 1956 this->macinfo.size = bfd_section_size (sectp);
c906108c 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1959 {
330cdd98 1960 this->macro.s.section = sectp;
fd361982 1961 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1962 }
330cdd98 1963 else if (section_is_p (sectp->name, &names.str))
c906108c 1964 {
330cdd98 1965 this->str.s.section = sectp;
fd361982 1966 this->str.size = bfd_section_size (sectp);
c906108c 1967 }
18a8505e
AT
1968 else if (section_is_p (sectp->name, &names.str_offsets))
1969 {
1970 this->str_offsets.s.section = sectp;
1971 this->str_offsets.size = bfd_section_size (sectp);
1972 }
330cdd98 1973 else if (section_is_p (sectp->name, &names.line_str))
43988095 1974 {
330cdd98 1975 this->line_str.s.section = sectp;
fd361982 1976 this->line_str.size = bfd_section_size (sectp);
43988095 1977 }
330cdd98 1978 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1979 {
330cdd98 1980 this->addr.s.section = sectp;
fd361982 1981 this->addr.size = bfd_section_size (sectp);
3019eac3 1982 }
330cdd98 1983 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1984 {
330cdd98 1985 this->frame.s.section = sectp;
fd361982 1986 this->frame.size = bfd_section_size (sectp);
b6af0555 1987 }
330cdd98 1988 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1989 {
330cdd98 1990 this->eh_frame.s.section = sectp;
fd361982 1991 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1992 }
330cdd98 1993 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1994 {
330cdd98 1995 this->ranges.s.section = sectp;
fd361982 1996 this->ranges.size = bfd_section_size (sectp);
af34e669 1997 }
330cdd98 1998 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1999 {
330cdd98 2000 this->rnglists.s.section = sectp;
fd361982 2001 this->rnglists.size = bfd_section_size (sectp);
43988095 2002 }
330cdd98 2003 else if (section_is_p (sectp->name, &names.types))
348e048f 2004 {
8b70b953
TT
2005 struct dwarf2_section_info type_section;
2006
2007 memset (&type_section, 0, sizeof (type_section));
049412e3 2008 type_section.s.section = sectp;
fd361982 2009 type_section.size = bfd_section_size (sectp);
8b70b953 2010
fd5866f6 2011 this->types.push_back (type_section);
348e048f 2012 }
330cdd98 2013 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2014 {
330cdd98 2015 this->gdb_index.s.section = sectp;
fd361982 2016 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2017 }
927aa2e7
JK
2018 else if (section_is_p (sectp->name, &names.debug_names))
2019 {
2020 this->debug_names.s.section = sectp;
fd361982 2021 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2022 }
2023 else if (section_is_p (sectp->name, &names.debug_aranges))
2024 {
2025 this->debug_aranges.s.section = sectp;
fd361982 2026 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2027 }
dce234bc 2028
fd361982
AM
2029 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2030 && bfd_section_vma (sectp) == 0)
330cdd98 2031 this->has_section_at_zero = true;
c906108c
SS
2032}
2033
dce234bc 2034/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2035 SECTION_NAME. */
af34e669 2036
dce234bc 2037void
3017a003
TG
2038dwarf2_get_section_info (struct objfile *objfile,
2039 enum dwarf2_section_enum sect,
d521ce57 2040 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2041 bfd_size_type *sizep)
2042{
5bfd760d 2043 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2044 struct dwarf2_section_info *info;
a3b2a86b
TT
2045
2046 /* We may see an objfile without any DWARF, in which case we just
2047 return nothing. */
2048 if (data == NULL)
2049 {
2050 *sectp = NULL;
2051 *bufp = NULL;
2052 *sizep = 0;
2053 return;
2054 }
3017a003
TG
2055 switch (sect)
2056 {
2057 case DWARF2_DEBUG_FRAME:
5989a64e 2058 info = &data->per_bfd->frame;
3017a003
TG
2059 break;
2060 case DWARF2_EH_FRAME:
5989a64e 2061 info = &data->per_bfd->eh_frame;
3017a003
TG
2062 break;
2063 default:
2064 gdb_assert_not_reached ("unexpected section");
2065 }
dce234bc 2066
96b79293 2067 info->read (objfile);
dce234bc 2068
96b79293 2069 *sectp = info->get_bfd_section ();
dce234bc
PP
2070 *bufp = info->buffer;
2071 *sizep = info->size;
2072}
2073
36586728
TT
2074/* A helper function to find the sections for a .dwz file. */
2075
2076static void
2077locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2078{
9a3c8263 2079 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2080
2081 /* Note that we only support the standard ELF names, because .dwz
2082 is ELF-only (at the time of writing). */
2083 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2084 {
049412e3 2085 dwz_file->abbrev.s.section = sectp;
fd361982 2086 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2087 }
2088 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2089 {
049412e3 2090 dwz_file->info.s.section = sectp;
fd361982 2091 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2092 }
2093 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2094 {
049412e3 2095 dwz_file->str.s.section = sectp;
fd361982 2096 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2097 }
2098 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2099 {
049412e3 2100 dwz_file->line.s.section = sectp;
fd361982 2101 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2102 }
2103 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2104 {
049412e3 2105 dwz_file->macro.s.section = sectp;
fd361982 2106 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2107 }
2ec9a5e0
TT
2108 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2109 {
049412e3 2110 dwz_file->gdb_index.s.section = sectp;
fd361982 2111 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2112 }
927aa2e7
JK
2113 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2114 {
2115 dwz_file->debug_names.s.section = sectp;
fd361982 2116 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2117 }
36586728
TT
2118}
2119
c4973306 2120/* See dwarf2read.h. */
36586728 2121
c4973306 2122struct dwz_file *
c3699833 2123dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2124{
36586728 2125 const char *filename;
acd13123 2126 bfd_size_type buildid_len_arg;
dc294be5
TT
2127 size_t buildid_len;
2128 bfd_byte *buildid;
36586728 2129
c3699833
SM
2130 if (per_bfd->dwz_file != NULL)
2131 return per_bfd->dwz_file.get ();
36586728 2132
4db1a1dc 2133 bfd_set_error (bfd_error_no_error);
791afaa2 2134 gdb::unique_xmalloc_ptr<char> data
c3699833 2135 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2136 &buildid_len_arg, &buildid));
4db1a1dc
TT
2137 if (data == NULL)
2138 {
2139 if (bfd_get_error () == bfd_error_no_error)
2140 return NULL;
2141 error (_("could not read '.gnu_debugaltlink' section: %s"),
2142 bfd_errmsg (bfd_get_error ()));
2143 }
791afaa2
TT
2144
2145 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2146
acd13123
TT
2147 buildid_len = (size_t) buildid_len_arg;
2148
791afaa2 2149 filename = data.get ();
d721ba37
PA
2150
2151 std::string abs_storage;
36586728
TT
2152 if (!IS_ABSOLUTE_PATH (filename))
2153 {
14278e1f 2154 gdb::unique_xmalloc_ptr<char> abs
c3699833 2155 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2156
14278e1f 2157 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2158 filename = abs_storage.c_str ();
36586728
TT
2159 }
2160
dc294be5
TT
2161 /* First try the file name given in the section. If that doesn't
2162 work, try to use the build-id instead. */
ad80db5b 2163 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2164 if (dwz_bfd != NULL)
36586728 2165 {
192b62ce 2166 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2167 dwz_bfd.reset (nullptr);
36586728
TT
2168 }
2169
dc294be5
TT
2170 if (dwz_bfd == NULL)
2171 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2172
0d79cdc4
AM
2173 if (dwz_bfd == nullptr)
2174 {
2175 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2176 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2177
2178 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2179 buildid_len,
2180 origname,
2181 &alt_filename));
2182
2183 if (fd.get () >= 0)
2184 {
2185 /* File successfully retrieved from server. */
ad80db5b 2186 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2187
2188 if (dwz_bfd == nullptr)
2189 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2190 alt_filename.get ());
2191 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2192 dwz_bfd.reset (nullptr);
2193 }
2194 }
2195
dc294be5
TT
2196 if (dwz_bfd == NULL)
2197 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2198 bfd_get_filename (per_bfd->obfd));
dc294be5 2199
7ff8cb8c
TT
2200 std::unique_ptr<struct dwz_file> result
2201 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2202
7ff8cb8c
TT
2203 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2204 result.get ());
36586728 2205
c3699833
SM
2206 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2207 per_bfd->dwz_file = std::move (result);
2208 return per_bfd->dwz_file.get ();
36586728 2209}
9291a0cd 2210\f
7b9f3c50
DE
2211/* DWARF quick_symbols_functions support. */
2212
2213/* TUs can share .debug_line entries, and there can be a lot more TUs than
2214 unique line tables, so we maintain a separate table of all .debug_line
2215 derived entries to support the sharing.
2216 All the quick functions need is the list of file names. We discard the
2217 line_header when we're done and don't need to record it here. */
2218struct quick_file_names
2219{
094b34ac
DE
2220 /* The data used to construct the hash key. */
2221 struct stmt_list_hash hash;
7b9f3c50
DE
2222
2223 /* The number of entries in file_names, real_names. */
2224 unsigned int num_file_names;
2225
2226 /* The file names from the line table, after being run through
2227 file_full_name. */
2228 const char **file_names;
2229
2230 /* The file names from the line table after being run through
2231 gdb_realpath. These are computed lazily. */
2232 const char **real_names;
2233};
2234
2235/* When using the index (and thus not using psymtabs), each CU has an
2236 object of this type. This is used to hold information needed by
2237 the various "quick" methods. */
2238struct dwarf2_per_cu_quick_data
2239{
2240 /* The file table. This can be NULL if there was no file table
2241 or it's currently not read in.
5989a64e 2242 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2243 struct quick_file_names *file_names;
2244
7b9f3c50
DE
2245 /* A temporary mark bit used when iterating over all CUs in
2246 expand_symtabs_matching. */
2247 unsigned int mark : 1;
2248
2249 /* True if we've tried to read the file table and found there isn't one.
2250 There will be no point in trying to read it again next time. */
2251 unsigned int no_file_data : 1;
2252};
2253
094b34ac
DE
2254/* Utility hash function for a stmt_list_hash. */
2255
2256static hashval_t
2257hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2258{
2259 hashval_t v = 0;
2260
2261 if (stmt_list_hash->dwo_unit != NULL)
2262 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2263 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2264 return v;
2265}
2266
2267/* Utility equality function for a stmt_list_hash. */
2268
2269static int
2270eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2271 const struct stmt_list_hash *rhs)
2272{
2273 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2274 return 0;
2275 if (lhs->dwo_unit != NULL
2276 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2277 return 0;
2278
9c541725 2279 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2280}
2281
7b9f3c50
DE
2282/* Hash function for a quick_file_names. */
2283
2284static hashval_t
2285hash_file_name_entry (const void *e)
2286{
9a3c8263
SM
2287 const struct quick_file_names *file_data
2288 = (const struct quick_file_names *) e;
7b9f3c50 2289
094b34ac 2290 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2291}
2292
2293/* Equality function for a quick_file_names. */
2294
2295static int
2296eq_file_name_entry (const void *a, const void *b)
2297{
9a3c8263
SM
2298 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2299 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2300
094b34ac 2301 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2302}
2303
2304/* Delete function for a quick_file_names. */
2305
2306static void
2307delete_file_name_entry (void *e)
2308{
9a3c8263 2309 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2310 int i;
2311
2312 for (i = 0; i < file_data->num_file_names; ++i)
2313 {
2314 xfree ((void*) file_data->file_names[i]);
2315 if (file_data->real_names)
2316 xfree ((void*) file_data->real_names[i]);
2317 }
2318
45940949
TT
2319 /* The space for the struct itself lives on the obstack, so we don't
2320 free it here. */
7b9f3c50
DE
2321}
2322
2323/* Create a quick_file_names hash table. */
2324
5895093f 2325static htab_up
7b9f3c50
DE
2326create_quick_file_names_table (unsigned int nr_initial_entries)
2327{
5895093f
TT
2328 return htab_up (htab_create_alloc (nr_initial_entries,
2329 hash_file_name_entry, eq_file_name_entry,
2330 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2331}
9291a0cd 2332
ab432490
SM
2333/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2334 function is unrelated to symtabs, symtab would have to be created afterwards.
2335 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2336
2337static void
ab432490
SM
2338load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2339 bool skip_partial)
918dd910 2340{
3019eac3 2341 if (per_cu->is_debug_types)
ab432490 2342 load_full_type_unit (per_cu, per_objfile);
918dd910 2343 else
ab432490 2344 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2345
cc12ce38
DE
2346 if (per_cu->cu == NULL)
2347 return; /* Dummy CU. */
2dc860c0
DE
2348
2349 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2350}
2351
97a1449a 2352/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2353
9291a0cd 2354static void
97a1449a
SM
2355dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2356 dwarf2_per_objfile *dwarf2_per_objfile,
2357 bool skip_partial)
9291a0cd 2358{
f4dc4d17
DE
2359 /* Skip type_unit_groups, reading the type units they contain
2360 is handled elsewhere. */
197400e8 2361 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2362 return;
2363
b303c6f6
AB
2364 /* The destructor of dwarf2_queue_guard frees any entries left on
2365 the queue. After this point we're guaranteed to leave this function
2366 with the dwarf queue empty. */
39856def 2367 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2368
af758d11 2369 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad
TT
2370 {
2371 queue_comp_unit (per_cu, language_minimal);
ab432490 2372 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2373
2374 /* If we just loaded a CU from a DWO, and we're working with an index
2375 that may badly handle TUs, load all the TUs in that DWO as well.
2376 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2377 if (!per_cu->is_debug_types
cc12ce38 2378 && per_cu->cu != NULL
89e63ee4 2379 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2380 && dwarf2_per_objfile->per_bfd->index_table != NULL
2381 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2382 /* DWP files aren't supported yet. */
ed2dc618 2383 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2384 queue_and_load_all_dwo_tus (per_cu);
95554aad 2385 }
9291a0cd 2386
ed2dc618 2387 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2388
2389 /* Age the cache, releasing compilation units that have not
2390 been used recently. */
ed2dc618 2391 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2392}
2393
97a1449a
SM
2394/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2395 the per-objfile for which this symtab is instantiated.
2396
2397 Returns the resulting symbol table. */
2fdf6df6 2398
43f3e411 2399static struct compunit_symtab *
97a1449a
SM
2400dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2401 dwarf2_per_objfile *dwarf2_per_objfile,
2402 bool skip_partial)
9291a0cd 2403{
5989a64e 2404 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2405
2406 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2407 {
11ed8cad 2408 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2409 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2410 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2411 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2412 }
f194fefb 2413
af758d11 2414 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2415}
2416
ff4c9fec 2417/* See declaration. */
f4dc4d17 2418
ff4c9fec 2419dwarf2_per_cu_data *
5989a64e 2420dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2421{
b76e467d 2422 if (index >= this->all_comp_units.size ())
ff4c9fec 2423 {
b76e467d 2424 index -= this->all_comp_units.size ();
b2bdb8cf 2425 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2426 return &this->all_type_units[index]->per_cu;
2427 }
f4dc4d17 2428
ff4c9fec
SM
2429 return this->all_comp_units[index];
2430}
f4dc4d17 2431
ff4c9fec 2432/* See declaration. */
2fdf6df6 2433
ff4c9fec 2434dwarf2_per_cu_data *
5989a64e 2435dwarf2_per_bfd::get_cu (int index)
1fd400ff 2436{
b76e467d 2437 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2438
ff4c9fec 2439 return this->all_comp_units[index];
f4dc4d17
DE
2440}
2441
ff4c9fec 2442/* See declaration. */
f4dc4d17 2443
ff4c9fec 2444signatured_type *
5989a64e 2445dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2446{
b2bdb8cf 2447 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2448
ff4c9fec 2449 return this->all_type_units[index];
1fd400ff
TT
2450}
2451
d3473f0c
TT
2452/* See read.h. */
2453
2454dwarf2_per_cu_data *
5989a64e 2455dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2456{
2457 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2458 result->per_bfd = this;
d3473f0c
TT
2459 result->index = m_num_psymtabs++;
2460 return result;
2461}
2462
2463/* See read.h. */
2464
2465signatured_type *
5989a64e 2466dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2467{
2468 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2469 result->per_cu.per_bfd = this;
d3473f0c
TT
2470 result->per_cu.index = m_num_psymtabs++;
2471 return result;
2472}
2473
45940949
TT
2474/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2475 obstack, and constructed with the specified field values. */
4b514bc8
JK
2476
2477static dwarf2_per_cu_data *
ed2dc618 2478create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2479 struct dwarf2_section_info *section,
2480 int is_dwz,
2481 sect_offset sect_off, ULONGEST length)
2482{
5989a64e 2483 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2484 the_cu->sect_off = sect_off;
2485 the_cu->length = length;
e3b94546 2486 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8 2487 the_cu->section = section;
5989a64e 2488 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2489 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2490 the_cu->is_dwz = is_dwz;
2491 return the_cu;
2492}
2493
2ec9a5e0
TT
2494/* A helper for create_cus_from_index that handles a given list of
2495 CUs. */
2fdf6df6 2496
74a0d9f6 2497static void
12359b5e 2498create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2499 const gdb_byte *cu_list, offset_type n_elements,
2500 struct dwarf2_section_info *section,
b76e467d 2501 int is_dwz)
9291a0cd 2502{
12359b5e 2503 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2504 {
74a0d9f6 2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2506
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2509 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2510 cu_list += 2 * 8;
2511
b76e467d 2512 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2513 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2514 sect_off, length);
5989a64e 2515 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2516 }
9291a0cd
TT
2517}
2518
2ec9a5e0 2519/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2520 the CU objects for this objfile. */
2ec9a5e0 2521
74a0d9f6 2522static void
12359b5e 2523create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2524 const gdb_byte *cu_list, offset_type cu_list_elements,
2525 const gdb_byte *dwz_list, offset_type dwz_elements)
2526{
5989a64e
SM
2527 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2528 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2529 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2530
12359b5e 2531 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2532 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2533
2534 if (dwz_elements == 0)
74a0d9f6 2535 return;
2ec9a5e0 2536
c3699833 2537 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2538 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2539 &dwz->info, 1);
2ec9a5e0
TT
2540}
2541
1fd400ff 2542/* Create the signatured type hash table from the index. */
673bfd45 2543
74a0d9f6 2544static void
12359b5e
SM
2545create_signatured_type_table_from_index
2546 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2547 struct dwarf2_section_info *section,
2548 const gdb_byte *bytes,
2549 offset_type elements)
1fd400ff 2550{
5989a64e
SM
2551 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2552 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2553
298e9637 2554 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2555
12359b5e 2556 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2557 {
52dc124a 2558 struct signatured_type *sig_type;
9c541725 2559 ULONGEST signature;
1fd400ff 2560 void **slot;
9c541725 2561 cu_offset type_offset_in_tu;
1fd400ff 2562
74a0d9f6 2563 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2564 sect_offset sect_off
2565 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2566 type_offset_in_tu
2567 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2568 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2569 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2570 bytes += 3 * 8;
2571
5989a64e 2572 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2573 sig_type->signature = signature;
9c541725 2574 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2575 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2576 sig_type->per_cu.section = section;
9c541725 2577 sig_type->per_cu.sect_off = sect_off;
e3b94546 2578 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2579 sig_type->per_cu.v.quick
5989a64e 2580 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2581 struct dwarf2_per_cu_quick_data);
2582
b0b6a987 2583 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2584 *slot = sig_type;
1fd400ff 2585
5989a64e 2586 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2587 }
2588
5989a64e 2589 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2590}
2591
927aa2e7
JK
2592/* Create the signatured type hash table from .debug_names. */
2593
2594static void
2595create_signatured_type_table_from_debug_names
ed2dc618 2596 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2597 const mapped_debug_names &map,
2598 struct dwarf2_section_info *section,
2599 struct dwarf2_section_info *abbrev_section)
2600{
ed2dc618
SM
2601 struct objfile *objfile = dwarf2_per_objfile->objfile;
2602
96b79293
TT
2603 section->read (objfile);
2604 abbrev_section->read (objfile);
927aa2e7 2605
5989a64e
SM
2606 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2607 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2608
298e9637 2609 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2610
2611 for (uint32_t i = 0; i < map.tu_count; ++i)
2612 {
2613 struct signatured_type *sig_type;
927aa2e7 2614 void **slot;
927aa2e7
JK
2615
2616 sect_offset sect_off
2617 = (sect_offset) (extract_unsigned_integer
2618 (map.tu_table_reordered + i * map.offset_size,
2619 map.offset_size,
2620 map.dwarf5_byte_order));
2621
2622 comp_unit_head cu_header;
ed2dc618
SM
2623 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2624 abbrev_section,
927aa2e7
JK
2625 section->buffer + to_underlying (sect_off),
2626 rcuh_kind::TYPE);
2627
5989a64e 2628 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2629 sig_type->signature = cu_header.signature;
2630 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2631 sig_type->per_cu.is_debug_types = 1;
2632 sig_type->per_cu.section = section;
2633 sig_type->per_cu.sect_off = sect_off;
e3b94546 2634 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7 2635 sig_type->per_cu.v.quick
5989a64e 2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2637 struct dwarf2_per_cu_quick_data);
2638
b0b6a987 2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2640 *slot = sig_type;
2641
5989a64e 2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2643 }
2644
5989a64e 2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2646}
2647
9291a0cd
TT
2648/* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2650
9291a0cd 2651static void
ed2dc618
SM
2652create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
9291a0cd 2654{
ed2dc618 2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2656 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2657 const gdb_byte *iter, *end;
9291a0cd 2658 struct addrmap *mutable_map;
9291a0cd
TT
2659 CORE_ADDR baseaddr;
2660
8268c778
PA
2661 auto_obstack temp_obstack;
2662
9291a0cd
TT
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
f00a2de2
PA
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
9291a0cd 2667
b3b3bada 2668 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
f652bce2 2679
24a55014 2680 if (lo > hi)
f652bce2 2681 {
b98664d3 2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2683 hex_string (lo), hex_string (hi));
24a55014 2684 continue;
f652bce2 2685 }
24a55014 2686
5989a64e 2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2688 {
b98664d3 2689 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2690 (unsigned) cu_index);
24a55014 2691 continue;
f652bce2 2692 }
24a55014 2693
79748972
TT
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2696 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2698 }
2699
d320c2b5 2700 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2702}
2703
927aa2e7
JK
2704/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707static void
ed2dc618 2708create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2709 struct dwarf2_section_info *section)
2710{
ed2dc618 2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2712 bfd *abfd = objfile->obfd;
08feed99 2713 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
5989a64e 2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2724 {
927aa2e7
JK
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2732 return;
2733 }
2734 }
2735
96b79293 2736 section->read (objfile);
927aa2e7
JK
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
47e3f474 2756 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
47e3f474
TV
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
927aa2e7
JK
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
47e3f474 2771 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2772 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
47e3f474 2785 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
47e3f474
TV
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
927aa2e7
JK
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
47e3f474 2798 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2799 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
47e3f474 2808 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
47e3f474
TV
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
927aa2e7
JK
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
47e3f474 2825 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2826 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
927aa2e7
JK
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
47e3f474 2836 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
47e3f474
TV
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
927aa2e7
JK
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
5989a64e 2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
79748972
TT
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
927aa2e7
JK
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
d320c2b5 2865 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2867}
2868
9291a0cd
TT
2869/* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2fdf6df6 2873
109483d9 2874static bool
9291a0cd
TT
2875find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877{
0cf03b49 2878 offset_type hash;
9291a0cd 2879 offset_type slot, step;
559a7a62 2880 int (*cmp) (const char *, const char *);
9291a0cd 2881
791afaa2 2882 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2883 if (current_language->la_language == language_cplus
45280282
IB
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
0cf03b49
JK
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
a8719064 2889
72998fb3 2890 if (strchr (name, '(') != NULL)
0cf03b49 2891 {
109483d9 2892 without_params = cp_remove_params (name);
0cf03b49 2893
72998fb3 2894 if (without_params != NULL)
791afaa2 2895 name = without_params.get ();
0cf03b49
JK
2896 }
2897 }
2898
559a7a62 2899 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2900 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
f00a2de2
PA
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2910
2911 for (;;)
2912 {
9291a0cd 2913 const char *str;
f00a2de2
PA
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2917 return false;
9291a0cd 2918
f00a2de2 2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2920 if (!cmp (name, str))
9291a0cd
TT
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2923 + MAYBE_SWAP (bucket.vec));
109483d9 2924 return true;
9291a0cd
TT
2925 }
2926
f00a2de2 2927 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2928 }
2929}
2930
4485a1c1
SM
2931/* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2933 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
4485a1c1 2940 Returns true if all went well, false otherwise. */
2fdf6df6 2941
d33bc52e 2942static bool
3810f182 2943read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951{
2952 const gdb_byte *addr = &buffer[0];
82430852 2953
9291a0cd 2954 /* Version check. */
4485a1c1 2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2957 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2958 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2959 indices. */
831adc1f 2960 if (version < 4)
481860b3
GB
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2966 filename);
481860b3
GB
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2ec9a5e0 2979 if (version < 6 && !deprecated_ok)
481860b3
GB
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
e615022a
DE
2984 warning (_("\
2985Skipping deprecated .gdb_index section in %s.\n\
2986Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987to use the section anyway."),
2ec9a5e0 2988 filename);
481860b3
GB
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
796a7ff8 2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3000
481860b3 3001 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3002 longer backward compatible. */
796a7ff8 3003 if (version > 8)
594e8718 3004 return 0;
9291a0cd 3005
559a7a62 3006 map->version = version;
9291a0cd 3007
4485a1c1 3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3009
4485a1c1 3010 int i = 0;
2ec9a5e0
TT
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
1fd400ff
TT
3014 ++i;
3015
2ec9a5e0
TT
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
987d643c 3020 ++i;
1fd400ff 3021
f00a2de2
PA
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3026 ++i;
3027
f00a2de2
PA
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3034
f00a2de2 3035 ++i;
f9d83a0b 3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3037
2ec9a5e0
TT
3038 return 1;
3039}
3040
4485a1c1
SM
3041/* Callback types for dwarf2_read_gdb_index. */
3042
3043typedef gdb::function_view
5989a64e 3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3045 get_gdb_index_contents_ftype;
3046typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
927aa2e7 3050/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053static int
4485a1c1
SM
3054dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3058{
2ec9a5e0
TT
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3061 struct dwz_file *dwz;
12359b5e 3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3063
4485a1c1 3064 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3063847f 3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
2ec9a5e0
TT
3076 return 0;
3077
0fefef59 3078 /* Don't use the index if it's empty. */
3063847f 3079 if (map->symbol_table.empty ())
0fefef59
DE
3080 return 0;
3081
2ec9a5e0
TT
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
c3699833 3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3085 if (dwz != NULL)
2ec9a5e0 3086 {
2ec9a5e0
TT
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
4485a1c1
SM
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3810f182 3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3098 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
2ec9a5e0
TT
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3104 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3105 return 0;
3106 }
3107 }
3108
12359b5e
SM
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
1fd400ff 3111
8b70b953
TT
3112 if (types_list_elements)
3113 {
8b70b953
TT
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
5989a64e 3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3117 return 0;
3118
5989a64e 3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3120
12359b5e
SM
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
8b70b953 3123 }
9291a0cd 3124
3063847f 3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3126
5989a64e
SM
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3131
3132 return 1;
3133}
3134
dee91e82 3135/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3136
dee91e82
DE
3137static void
3138dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3139 const gdb_byte *info_ptr,
3e225074 3140 struct die_info *comp_unit_die)
9291a0cd 3141{
dee91e82 3142 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3145 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3146 struct attribute *attr;
7b9f3c50
DE
3147 void **slot;
3148 struct quick_file_names *qfn;
9291a0cd 3149
0186c6a7
DE
3150 gdb_assert (! this_cu->is_debug_types);
3151
07261596
TT
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
0186c6a7 3160 lh_cu = this_cu;
7b9f3c50 3161 slot = NULL;
dee91e82 3162
fff8551c 3163 line_header_up lh;
9c541725 3164 sect_offset line_offset {};
fff8551c 3165
dee91e82 3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3167 if (attr != nullptr)
9291a0cd 3168 {
7b9f3c50
DE
3169 struct quick_file_names find_entry;
3170
9c541725 3171 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
094b34ac 3175 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3176 find_entry.hash.line_sect_off = line_offset;
5989a64e 3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
9a3c8263 3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3182 return;
7b9f3c50
DE
3183 }
3184
3019eac3 3185 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3186 }
3187 if (lh == NULL)
3188 {
094b34ac 3189 lh_cu->v.quick->no_file_data = 1;
dee91e82 3190 return;
9291a0cd
TT
3191 }
3192
5989a64e 3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3194 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3195 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
9291a0cd 3198
d721ba37 3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3200
aa391654
TT
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
7ba99d21 3205 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3206 qfn->file_names =
5989a64e 3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3208 qfn->num_file_names);
aa391654
TT
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3211 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
7b9f3c50 3214 qfn->real_names = NULL;
9291a0cd 3215
094b34ac 3216 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3217}
3218
3219/* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222static struct quick_file_names *
ab432490
SM
3223dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
dee91e82 3225{
0186c6a7
DE
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
197400e8 3229 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3230
dee91e82
DE
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
ab432490 3237 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3238 if (!reader.dummy_p)
3e225074 3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
9291a0cd
TT
3244}
3245
3246/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3247 real path for a given file name from the line table. */
2fdf6df6 3248
9291a0cd 3249static const char *
45940949 3250dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3251 struct quick_file_names *qfn, int index)
9291a0cd 3252{
7b9f3c50 3253 if (qfn->real_names == NULL)
5989a64e 3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3255 qfn->num_file_names, const char *);
9291a0cd 3256
7b9f3c50 3257 if (qfn->real_names[index] == NULL)
14278e1f 3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3259
7b9f3c50 3260 return qfn->real_names[index];
9291a0cd
TT
3261}
3262
3263static struct symtab *
3264dw2_find_last_source_symtab (struct objfile *objfile)
3265{
ed2dc618
SM
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
5989a64e 3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3271
43f3e411
DE
3272 if (cust == NULL)
3273 return NULL;
ed2dc618 3274
43f3e411 3275 return compunit_primary_filetab (cust);
9291a0cd
TT
3276}
3277
7b9f3c50
DE
3278/* Traversal function for dw2_forget_cached_source_info. */
3279
3280static int
3281dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3282{
7b9f3c50 3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3284
7b9f3c50 3285 if (file_data->real_names)
9291a0cd 3286 {
7b9f3c50 3287 int i;
9291a0cd 3288
7b9f3c50 3289 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3290 {
7b9f3c50
DE
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
9291a0cd
TT
3293 }
3294 }
7b9f3c50
DE
3295
3296 return 1;
3297}
3298
3299static void
3300dw2_forget_cached_source_info (struct objfile *objfile)
3301{
ed2dc618
SM
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3304
5989a64e 3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3306 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3307}
3308
f8eba3c6
TT
3309/* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312static int
3313dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3315 const char *name, const char *real_path,
14bc53a8 3316 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3317{
43f3e411 3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3319
3320 /* Don't visit already-expanded CUs. */
af758d11
SM
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
97a1449a 3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3328
14bc53a8
PA
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
f8eba3c6
TT
3331}
3332
3333/* Implementation of the map_symtabs_matching_filename method. */
3334
14bc53a8
PA
3335static bool
3336dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3339{
c011a4f4 3340 const char *name_basename = lbasename (name);
ed2dc618
SM
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3343
848e3e78
DE
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
f4dc4d17 3346
5989a64e 3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3348 {
3d7bb9d9 3349 /* We only need to look at symtabs not already expanded. */
af758d11 3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3351 continue;
3352
ab432490
SM
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3355 if (file_data == NULL)
9291a0cd
TT
3356 continue;
3357
b76e467d 3358 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3359 {
7b9f3c50 3360 const char *this_name = file_data->file_names[j];
da235a7c 3361 const char *this_real_name;
9291a0cd 3362
af529f8f 3363 if (compare_filenames_for_search (this_name, name))
9291a0cd 3364 {
f5b95b50 3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3366 callback))
3367 return true;
288e77a7 3368 continue;
4aac40c8 3369 }
9291a0cd 3370
c011a4f4
DE
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
45940949
TT
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
da235a7c 3379 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3380 {
da235a7c 3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3382 callback))
3383 return true;
288e77a7 3384 continue;
da235a7c 3385 }
9291a0cd 3386
da235a7c
JK
3387 if (real_path != NULL)
3388 {
af529f8f
JK
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3391 if (this_real_name != NULL
af529f8f 3392 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3393 {
f5b95b50 3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3395 callback))
3396 return true;
288e77a7 3397 continue;
9291a0cd
TT
3398 }
3399 }
3400 }
3401 }
3402
14bc53a8 3403 return false;
9291a0cd
TT
3404}
3405
da51c347
DE
3406/* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408struct dw2_symtab_iterator
9291a0cd 3409{
ed2dc618
SM
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3414 gdb::optional<block_enum> block_index;
da51c347
DE
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
8943b874
DE
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
da51c347 3429};
9291a0cd 3430
2b79f376 3431/* Initialize the index symtab iterator ITER. */
2fdf6df6 3432
9291a0cd 3433static void
da51c347 3434dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3436 gdb::optional<block_enum> block_index,
da51c347
DE
3437 domain_enum domain,
3438 const char *name)
3439{
ed2dc618 3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
8943b874 3444 iter->global_seen = 0;
da51c347 3445
5989a64e 3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456}
3457
3458/* Return the next matching CU or NULL if there are no more. */
3459
3460static struct dwarf2_per_cu_data *
3461dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462{
ed2dc618
SM
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
da51c347
DE
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
5989a64e 3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3190f0c6 3480 /* Don't crash on bad data. */
5989a64e
SM
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3483 {
b98664d3 3484 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3487 continue;
3488 }
3489
5989a64e 3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3491
da51c347 3492 /* Skip if already read in. */
af758d11 3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3494 continue;
3495
8943b874
DE
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
2b79f376
SM
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
8943b874
DE
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
da51c347
DE
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
59c35742
AB
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
da51c347
DE
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550}
3551
43f3e411 3552static struct compunit_symtab *
c7f839cb 3553dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3554 const char *name, domain_enum domain)
9291a0cd 3555{
43f3e411 3556 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
9291a0cd 3559
b5ec771e
PA
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
ed2dc618
SM
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
da51c347 3564
2b79f376 3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3566
ed2dc618
SM
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3574
ed2dc618
SM
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
b2e2f908 3578
ed2dc618
SM
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
da51c347 3582
ed2dc618
SM
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
da51c347 3589
ed2dc618 3590 /* Keep looking through other CUs. */
9291a0cd 3591 }
9291a0cd 3592
da51c347 3593 return stab_best;
9291a0cd
TT
3594}
3595
3596static void
3597dw2_print_stats (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3603 int count = 0;
9291a0cd 3604
ed2dc618 3605 for (int i = 0; i < total; ++i)
9291a0cd 3606 {
5989a64e 3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3608
af758d11 3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3610 ++count;
3611 }
e4a48d9d 3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614}
3615
779bd270
DE
3616/* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
9291a0cd
TT
3621static void
3622dw2_dump (struct objfile *objfile)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
5989a64e 3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3628 printf_filtered (".gdb_index:");
5989a64e 3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3630 {
3631 printf_filtered (" version %d\n",
5989a64e 3632 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
9291a0cd
TT
3637}
3638
9291a0cd
TT
3639static void
3640dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642{
ed2dc618
SM
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
da51c347 3645
ed2dc618
SM
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
da51c347 3648
2b79f376 3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3650
ed2dc618 3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3653
9291a0cd
TT
3654}
3655
3656static void
3657dw2_expand_all_symtabs (struct objfile *objfile)
3658{
ed2dc618
SM
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3663
ed2dc618 3664 for (int i = 0; i < total_units; ++i)
9291a0cd 3665 {
5989a64e 3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3667
58f0c718
TT
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3674 }
3675}
3676
3677static void
652a8996
JK
3678dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
9291a0cd 3680{
ed2dc618
SM
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
5989a64e 3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3690 {
3d7bb9d9 3691 /* We only need to look at symtabs not already expanded. */
af758d11 3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3693 continue;
3694
ab432490
SM
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3697 if (file_data == NULL)
9291a0cd
TT
3698 continue;
3699
b76e467d 3700 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3701 {
652a8996
JK
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3705 {
97a1449a 3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3707 break;
3708 }
3709 }
3710 }
3711}
3712
9a0bacfb
TV
3713static void
3714dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
3720
3721static void
3722dw2_expand_symtabs_matching_one
97a1449a
SM
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
9291a0cd 3728static void
199b4314
TT
3729dw2_map_matching_symbols
3730 (struct objfile *objfile,
b054970d 3731 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3734 symbol_compare_ftype *ordered_compare)
9291a0cd 3735{
1aa98955
TV
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
9a0bacfb
TV
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
5989a64e 3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3746 using GNU index. */
5989a64e 3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3748
9a0bacfb
TV
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
9a0bacfb
TV
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
1aa98955
TV
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
9291a0cd
TT
3788}
3789
e1ef7d7a
PA
3790/* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795static std::string
3796make_sort_after_prefix_name (const char *search_name)
3797{
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861}
3862
5c58de74 3863/* See declaration. */
61d96d7e 3864
5c58de74
PA
3865std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
44ed8f3e 3867mapped_index_base::find_name_components_bounds
3b00ef10 3868 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3869{
5c58de74
PA
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3872
3b00ef10 3873 const char *lang_name
e0802d59 3874 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3875
3f563c84
PA
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
5c58de74 3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
5c58de74 3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
5c58de74
PA
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3f563c84
PA
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3b00ef10 3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3903 return begin;
3904 else
3b00ef10 3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
5c58de74 3911 if (lookup_name_without_params.completion_mode ())
3f563c84 3912 {
e1ef7d7a
PA
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3b00ef10 3924 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3925 if (after.empty ())
3f563c84 3926 return end;
e6b2f5ef
PA
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3f563c84
PA
3929 }
3930 else
3b00ef10 3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3932 } ();
3933
5c58de74
PA
3934 return {lower, upper};
3935}
3936
3937/* See declaration. */
3938
3939void
44ed8f3e 3940mapped_index_base::build_name_components ()
5c58de74
PA
3941{
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3b00ef10 3951 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3954 {
44ed8f3e 3955 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3b00ef10
TT
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
5c58de74 3977 {
3b00ef10
TT
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
5c58de74 3987 }
3b00ef10 3988
5c58de74
PA
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008}
4009
4010/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
5c58de74 4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4015 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4016
4017static void
4018dw2_expand_symtabs_matching_symbol
44ed8f3e 4019 (mapped_index_base &index,
5c58de74
PA
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
3b00ef10 4023 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4024{
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
3f563c84
PA
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
3f563c84 4040
3b00ef10
TT
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
ecc6c606 4044 const char *name;
3b00ef10
TT
4045
4046 bool operator== (const name_and_matcher &other) const
3f563c84 4047 {
ecc6c606 4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4063
3b00ef10
TT
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
9291a0cd 4075 continue;
3b00ef10
TT
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
9291a0cd 4092
3b00ef10
TT
4093 matches.push_back (bounds.first->idx);
4094 }
3f563c84
PA
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
3b00ef10
TT
4105 if (!match_callback (idx))
4106 break;
3f563c84
PA
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114}
4115
c62446b1
PA
4116#if GDB_SELF_TEST
4117
4118namespace selftests { namespace dw2_expand_symtabs_matching {
4119
a3c5fafd
PA
4120/* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124class mock_mapped_index : public mapped_index_base
c62446b1
PA
4125{
4126public:
a3c5fafd
PA
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
c62446b1
PA
4129 {}
4130
a3c5fafd 4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4132
a3c5fafd 4133 /* Return the number of names in the symbol table. */
632e107b 4134 size_t symbol_name_count () const override
c62446b1 4135 {
a3c5fafd 4136 return m_symbol_table.size ();
c62446b1
PA
4137 }
4138
a3c5fafd 4139 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4140 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4141 {
4142 return m_symbol_table[idx];
4143 }
c62446b1 4144
a3c5fafd
PA
4145private:
4146 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4147};
4148
4149/* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152static const char *
4153string_or_null (const char *str)
4154{
4155 return str != NULL ? str : "<null>";
4156}
4157
4158/* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165static bool
4166check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171{
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
a3c5fafd 4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
a3c5fafd 4195 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
3b00ef10 4201 return true;
c62446b1
PA
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210}
4211
4212/* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
a20714ff
PA
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
c62446b1 4225
e1ef7d7a
PA
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
c62446b1
PA
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248#define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254};
4255
a3c5fafd
PA
4256/* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
5c58de74
PA
4259
4260static bool
a3c5fafd 4261check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264{
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
3b00ef10
TT
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
5c58de74
PA
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284}
4285
4286/* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
c62446b1 4289static void
5c58de74
PA
4290test_mapped_index_find_name_component_bounds ()
4291{
4292 mock_mapped_index mock_index (test_symbols);
4293
a3c5fafd 4294 mock_index.build_name_components ();
5c58de74
PA
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
5c58de74
PA
4302 };
4303
a3c5fafd 4304 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
a3c5fafd 4315 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
a3c5fafd 4321 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4322 "\377\377", expected_syms2));
4323 }
4324}
4325
4326/* Test dw2_expand_symtabs_matching_symbol. */
4327
4328static void
4329test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4330{
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340#define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
e1ef7d7a
PA
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
c62446b1
PA
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
a20714ff
PA
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
c62446b1
PA
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
a20714ff
PA
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
c62446b1
PA
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
a20714ff
PA
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
c62446b1
PA
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
a20714ff
PA
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
c62446b1
PA
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
a20714ff
PA
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
c62446b1
PA
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
a20714ff
PA
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
c62446b1
PA
4526 SELF_CHECK (!any_mismatch);
4527
4528#undef EXPECT
4529#undef CHECK_MATCH
4530}
4531
5c58de74
PA
4532static void
4533run_test ()
4534{
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537}
4538
c62446b1
PA
4539}} // namespace selftests::dw2_expand_symtabs_matching
4540
4541#endif /* GDB_SELF_TEST */
4542
4b514bc8
JK
4543/* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548static void
4549dw2_expand_symtabs_matching_one
97a1449a
SM
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554{
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
af758d11 4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4558
97a1449a
SM
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4561 gdb_assert (symtab != nullptr);
4b514bc8 4562
af758d11
SM
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4b514bc8
JK
4565 }
4566}
4567
3f563c84
PA
4568/* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572static void
4573dw2_expand_marked_cus
97a1449a 4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578{
3f563c84
PA
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
5989a64e 4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4582
61920122 4583 vec = (offset_type *) (index.constant_pool
f00a2de2 4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
61920122
PA
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
9291a0cd 4604 {
61920122
PA
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
3190f0c6 4610
61920122
PA
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
8943b874 4615 {
61920122
PA
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4622 continue;
61920122
PA
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
59c35742
AB
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
61920122
PA
4632 default:
4633 break;
8943b874 4634 }
61920122 4635 }
8943b874 4636
61920122 4637 /* Don't crash on bad data. */
5989a64e
SM
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4640 {
b98664d3 4641 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4644 continue;
4645 }
4646
5989a64e 4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4649 expansion_notify);
61920122
PA
4650 }
4651}
4652
4b514bc8
JK
4653/* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
61920122 4657static void
4b514bc8 4658dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4661{
4b514bc8 4662 if (file_matcher == NULL)
61920122
PA
4663 return;
4664
4b514bc8
JK
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
61920122 4671
4b514bc8
JK
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
61920122 4674
5989a64e 4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4676 {
927aa2e7
JK
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
af758d11 4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4683 continue;
4684
ab432490
SM
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
b76e467d 4698 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
45940949
TT
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
927aa2e7
JK
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
b76e467d
SM
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
927aa2e7
JK
4728 *slot = file_data;
4729 }
4730}
4731
4732static void
4733dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4736 const lookup_name_info *lookup_name,
927aa2e7
JK
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740{
ed2dc618
SM
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4743
4744 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4745 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4746 return;
4747
ed2dc618 4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4749
c1a66c06
TV
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
5989a64e 4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4753 {
4754 QUIT;
4755
97a1449a
SM
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
c1a66c06
TV
4758 }
4759 return;
4760 }
4761
5989a64e 4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4763
c1a66c06 4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
ed2dc618 4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4769 expansion_notify, kind);
3b00ef10 4770 return true;
927aa2e7
JK
4771 });
4772}
4773
4774/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777static struct compunit_symtab *
4778recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780{
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800}
4801
4802static struct compunit_symtab *
4803dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808{
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
d320c2b5 4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4813 return NULL;
4814
b3b3bada 4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4818 if (!data)
4819 return NULL;
4820
af758d11
SM
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4824 paddress (objfile->arch (), pc));
927aa2e7 4825
97a1449a
SM
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
927aa2e7
JK
4829 gdb_assert (result != NULL);
4830 return result;
4831}
4832
4833static void
4834dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836{
ed2dc618
SM
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
927aa2e7 4839
5989a64e 4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4841 {
5989a64e 4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
5989a64e 4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4853 {
af758d11 4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
5989a64e 4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4865 {
927aa2e7 4866 /* We only need to look at symtabs not already expanded. */
af758d11 4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4868 continue;
4869
ab432490
SM
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4872 if (file_data == NULL)
4873 continue;
4874
b76e467d 4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
5989a64e 4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4887 }
4888 }
4889 }
4890
5989a64e 4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899}
4900
4901static int
4902dw2_has_symbols (struct objfile *objfile)
4903{
4904 return 1;
4905}
4906
4907const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908{
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
d3214198 4914 NULL,
927aa2e7
JK
4915 dw2_print_stats,
4916 dw2_dump,
927aa2e7
JK
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925};
4926
4927/* DWARF-5 debug_names reader. */
4928
4929/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932/* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938static bool
4939read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943{
96b79293 4944 if (section->empty ())
927aa2e7
JK
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
96b79293 4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4950 return false;
4951
96b79293 4952 section->read (objfile);
927aa2e7 4953
08feed99 4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4955
4956 const gdb_byte *addr = section->buffer;
4957
96b79293 4958 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
927aa2e7
JK
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
927aa2e7
JK
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114}
5115
5116/* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119static void
ed2dc618 5120create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
b76e467d 5123 bool is_dwz)
927aa2e7 5124{
3ee6bb11
TV
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5989a64e 5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5142 }
5143 }
5144
927aa2e7
JK
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5162 dwarf2_per_cu_data *per_cu
ed2dc618 5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5164 sect_off_prev, length);
5989a64e 5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169}
5170
5171/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5172 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5173
5174static void
ed2dc618 5175create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178{
5989a64e
SM
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5181
ed2dc618 5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5183 dwarf2_per_objfile->per_bfd->info,
b76e467d 5184 false /* is_dwz */);
927aa2e7
JK
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
c3699833 5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5191 true /* is_dwz */);
927aa2e7
JK
5192}
5193
5194/* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197static bool
ed2dc618 5198dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5199{
22ca247e
TT
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5206 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5207 *map))
927aa2e7
JK
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
22ca247e 5211 if (map->name_count == 0)
927aa2e7
JK
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
c3699833 5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
00f93c44 5220 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5224 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5225 return false;
5226 }
5227 }
5228
22ca247e 5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5230
22ca247e 5231 if (map->tu_count != 0)
927aa2e7
JK
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5989a64e 5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5236 return false;
5237
5989a64e 5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5239
5240 create_signatured_type_table_from_debug_names
5989a64e 5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5242 }
5243
ed2dc618 5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5246
5989a64e
SM
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5251
5252 return true;
5253}
5254
927aa2e7
JK
5255/* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258class dw2_debug_names_iterator
5259{
5260public:
927aa2e7 5261 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
927aa2e7 5264 const char *name)
2b79f376 5265 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
3b00ef10
TT
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
927aa2e7
JK
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
2b79f376
SM
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306};
5307
5308const char *
5309mapped_debug_names::namei_to_name (uint32_t namei) const
5310{
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
4f44ae6c
TT
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
927aa2e7
JK
5318}
5319
5320/* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324const gdb_byte *
5325dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327{
5328 int (*cmp) (const char *, const char *);
5329
54ee4252 5330 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
54ee4252 5340 without_params = cp_remove_params (name);
927aa2e7 5341 if (without_params != NULL)
54ee4252 5342 name = without_params.get ();
927aa2e7
JK
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
b98664d3 5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5360 "[in module %s]"),
5361 namei, map.name_count,
ed2dc618 5362 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379#if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
b98664d3 5382 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387#endif
5388
5389 if (cmp (namei_string, name) == 0)
5390 {
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397 }
5398
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403}
5404
5405const gdb_byte *
5406dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408{
5409 if (namei >= map.name_count)
5410 {
b98664d3 5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5412 "[in module %s]"),
5413 namei, map.name_count,
ed2dc618 5414 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423}
5424
5425/* See dw2_debug_names_iterator. */
5426
5427dwarf2_per_cu_data *
5428dw2_debug_names_iterator::next ()
5429{
5430 if (m_addr == NULL)
5431 return NULL;
5432
ed2dc618
SM
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
b98664d3 5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5449 "[in module %s]"),
ed2dc618 5450 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
23c13d42 5458 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
6dc55ce9 5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
927aa2e7 5487 default:
b98664d3 5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5489 dwarf_form_name (attr.form),
ed2dc618 5490 objfile_name (objfile));
927aa2e7
JK
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5989a64e 5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5498 {
b98664d3 5499 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5989a64e 5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5506 break;
8af5c486
JK
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5989a64e 5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5510 {
b98664d3 5511 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5989a64e 5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5518 break;
6dc55ce9 5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5989a64e 5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5524 break;
927aa2e7
JK
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
23c13d42 5528 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
23c13d42 5533 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
af758d11 5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5540 goto again;
5541
5542 /* Check static vs global. */
23c13d42 5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5544 {
2b79f376 5545 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5548 if (want_static != symbol_is_static)
2b79f376 5549 goto again;
927aa2e7
JK
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
59c35742
AB
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
927aa2e7
JK
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
4b514bc8 5605 {
927aa2e7
JK
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
4b514bc8 5608 {
927aa2e7
JK
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
4b514bc8 5613 }
927aa2e7
JK
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
4b514bc8 5617 {
927aa2e7
JK
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
4b514bc8 5622 }
927aa2e7
JK
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
59c35742
AB
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
927aa2e7
JK
5642 default:
5643 break;
4b514bc8 5644 }
927aa2e7
JK
5645
5646 return per_cu;
4b514bc8 5647}
61920122 5648
927aa2e7 5649static struct compunit_symtab *
c7f839cb 5650dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5651 const char *name, domain_enum domain)
4b514bc8 5652{
ed2dc618
SM
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
61920122 5655
5989a64e 5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5657 if (!mapp)
61920122 5658 {
927aa2e7
JK
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
9291a0cd 5663
2b79f376 5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5665
927aa2e7
JK
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5675
927aa2e7
JK
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
9703b513 5679
927aa2e7
JK
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
a3ec0bb1 5683
927aa2e7 5684 if (sym != NULL
987012b8 5685 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5686 return stab;
5687 if (with_opaque != NULL
987012b8 5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5689 stab_best = stab;
9703b513 5690
927aa2e7 5691 /* Keep looking through other CUs. */
9703b513
TT
5692 }
5693
927aa2e7 5694 return stab_best;
9703b513
TT
5695}
5696
927aa2e7
JK
5697/* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
9291a0cd 5700
927aa2e7
JK
5701static void
5702dw2_debug_names_dump (struct objfile *objfile)
5703{
ed2dc618
SM
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5989a64e 5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5708 printf_filtered (".debug_names:");
5989a64e 5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
9291a0cd
TT
5714}
5715
9291a0cd 5716static void
927aa2e7
JK
5717dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
9291a0cd 5719{
ed2dc618
SM
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5722
5989a64e
SM
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5725 {
5989a64e 5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5727
2b79f376 5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5729
927aa2e7
JK
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
97a1449a 5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5733 }
5734}
24c79950 5735
3b00ef10
TT
5736static void
5737dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743{
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5749 return;
5750
5989a64e 5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
3b00ef10
TT
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5989a64e 5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5781 {
af758d11
SM
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
3b00ef10
TT
5784 {
5785 const struct block *block
af758d11 5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792}
5793
927aa2e7
JK
5794static void
5795dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5798 const lookup_name_info *lookup_name,
927aa2e7
JK
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802{
ed2dc618
SM
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
9291a0cd 5805
927aa2e7 5806 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5808 return;
9291a0cd 5809
ed2dc618 5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5811
c1a66c06
TV
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5989a64e 5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5815 {
5816 QUIT;
5817
97a1449a
SM
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
c1a66c06
TV
5820 }
5821 return;
5822 }
5823
5989a64e 5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5825
c1a66c06 5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
927aa2e7 5829 {
927aa2e7
JK
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5833
927aa2e7
JK
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
3b00ef10 5838 return true;
44ed8f3e 5839 });
9291a0cd
TT
5840}
5841
927aa2e7 5842const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5843{
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
f8eba3c6 5847 dw2_map_symtabs_matching_filename,
927aa2e7 5848 dw2_debug_names_lookup_symbol,
d3214198 5849 NULL,
9291a0cd 5850 dw2_print_stats,
927aa2e7 5851 dw2_debug_names_dump,
927aa2e7 5852 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5853 dw2_expand_all_symtabs,
652a8996 5854 dw2_expand_symtabs_with_fullname,
3b00ef10 5855 dw2_debug_names_map_matching_symbols,
927aa2e7 5856 dw2_debug_names_expand_symtabs_matching,
43f3e411 5857 dw2_find_pc_sect_compunit_symtab,
71a3c369 5858 NULL,
9291a0cd
TT
5859 dw2_map_symbol_filenames
5860};
5861
4485a1c1 5862/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5863 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5864
5865template <typename T>
5866static gdb::array_view<const gdb_byte>
5867get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868{
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
96b79293 5871 if (section->empty ())
4485a1c1
SM
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
96b79293 5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5877 return {};
5878
96b79293 5879 section->read (obj);
4485a1c1 5880
8bebfcda
PA
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5888}
5889
87d6a7aa
SM
5890/* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893static gdb::array_view<const gdb_byte>
5989a64e 5894get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5895{
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5901 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5902}
5903
5904/* Same as the above, but for DWZ. */
5905
5906static gdb::array_view<const gdb_byte>
5907get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908{
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914}
5915
3c0aa29a 5916/* See symfile.h. */
9291a0cd 5917
3c0aa29a
PA
5918bool
5919dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5920{
ed2dc618
SM
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
9291a0cd
TT
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5989a64e 5930 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5989a64e 5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5934 = create_quick_file_names_table
5989a64e 5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5936 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5937
5989a64e
SM
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5940 {
5989a64e 5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5942
5989a64e 5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5944 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
3c0aa29a
PA
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
9291a0cd
TT
5952 }
5953
ed2dc618 5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5957 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5958 return true;
5959 }
927aa2e7 5960
4485a1c1 5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5963 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5966 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5967 return true;
5968 }
9291a0cd 5969
87d6a7aa
SM
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5977 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
3c0aa29a 5982 return false;
9291a0cd
TT
5983}
5984
5985\f
5986
dce234bc
PP
5987/* Build a partial symbol table. */
5988
5989void
f29dff0a 5990dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5991{
ed2dc618
SM
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
c9bf0622 5994
6eee24ce 5995 init_psymbol_list (objfile, 1024);
c906108c 5996
a70b8144 5997 try
c9bf0622
TT
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
906768f9 6002 psymtab_discarder psymtabs (objfile);
ed2dc618 6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6004 psymtabs.keep ();
87d6a7aa 6005
af758d11
SM
6006 dwarf2_per_objfile->resize_symtabs ();
6007
87d6a7aa
SM
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6010 }
230d2906 6011 catch (const gdb_exception_error &except)
492d29ea
PA
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
c906108c 6015}
c906108c 6016
3b80fe9b
DE
6017/* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023static void
6024dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025{
6026 struct attribute *attr;
6027
2b24b6e4 6028 cu->base_address.reset ();
3b80fe9b
DE
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6031 if (attr != nullptr)
2b24b6e4 6032 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6036 if (attr != nullptr)
2b24b6e4 6037 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6038 }
6039}
6040
36586728
TT
6041/* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044static struct dwarf2_section_info *
6045get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046{
6047 struct dwarf2_section_info *abbrev;
c3699833 6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6049
6050 if (this_cu->is_dwz)
c3699833 6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6052 else
c3699833 6053 abbrev = &per_bfd->abbrev;
36586728
TT
6054
6055 return abbrev;
6056}
6057
f4dc4d17
DE
6058/* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060static sect_offset
ed2dc618
SM
6061read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
9c541725 6063 sect_offset sect_off)
f4dc4d17 6064{
96b79293 6065 bfd *abfd = section->get_bfd_owner ();
d521ce57 6066 const gdb_byte *info_ptr;
ac298888 6067 unsigned int initial_length_size, offset_size;
43988095 6068 uint16_t version;
f4dc4d17 6069
96b79293 6070 section->read (dwarf2_per_objfile->objfile);
9c541725 6071 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6072 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6073 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
24aa364d 6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6085}
6086
b83470bf
TT
6087/* A partial symtab that is used only for include files. */
6088struct dwarf2_include_psymtab : public partial_symtab
6089{
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
194d088f
TV
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
b83470bf
TT
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
194d088f
TV
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
b83470bf
TT
6117 }
6118
5717c425 6119 bool readin_p (struct objfile *objfile) const override
b83470bf 6120 {
5717c425 6121 return includer ()->readin_p (objfile);
b83470bf
TT
6122 }
6123
5717c425 6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6125 {
6126 return nullptr;
6127 }
6128
6129private:
194d088f
TV
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
b83470bf
TT
6137};
6138
aaa75496
JB
6139/* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142static void
891813be 6143dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6144 struct objfile *objfile)
6145{
b83470bf 6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6147
fbd9ab74 6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6149 subpst->dirname = pst->dirname;
fbd9ab74 6150
a9342b62 6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
aaa75496
JB
6154}
6155
6156/* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
d85a05f0 6158 partial symtab for each of these included files. */
aaa75496
JB
6159
6160static void
6161dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6162 struct die_info *die,
891813be 6163 dwarf2_psymtab *pst)
aaa75496 6164{
fff8551c 6165 line_header_up lh;
d85a05f0 6166 struct attribute *attr;
aaa75496 6167
d85a05f0 6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6169 if (attr != nullptr)
9c541725 6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
79748972
TT
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
4ae976d1 6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6179 pst->raw_text_low (), 1);
aaa75496
JB
6180}
6181
348e048f 6182static hashval_t
52dc124a 6183hash_signatured_type (const void *item)
348e048f 6184{
9a3c8263
SM
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
9a619af0 6187
348e048f 6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6189 return sig_type->signature;
348e048f
DE
6190}
6191
6192static int
52dc124a 6193eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6194{
9a3c8263
SM
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6197
348e048f
DE
6198 return lhs->signature == rhs->signature;
6199}
6200
1fd400ff
TT
6201/* Allocate a hash table for signatured types. */
6202
b0b6a987 6203static htab_up
298e9637 6204allocate_signatured_type_table ()
1fd400ff 6205{
b0b6a987
TT
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
1fd400ff
TT
6210}
6211
d467dd73 6212/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6213
6214static int
d467dd73 6215add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6216{
9a3c8263 6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
1fd400ff 6220
b2bdb8cf 6221 all_type_units->push_back (sigt);
1fd400ff
TT
6222
6223 return 1;
6224}
6225
78d4d2c5 6226/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
c88ee1f0 6229
78d4d2c5 6230static void
ed2dc618
SM
6231create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
b0b6a987 6233 dwarf2_section_info *section, htab_up &types_htab,
43988095 6234 rcuh_kind section_kind)
348e048f 6235{
3019eac3 6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6237 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
348e048f 6240
4bdcc0c1
DE
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
5989a64e 6243 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6244
b4f54984 6245 if (dwarf_read_debug)
43988095 6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
09406207 6249
96b79293 6250 section->read (objfile);
78d4d2c5 6251 info_ptr = section->buffer;
348e048f 6252
78d4d2c5
JK
6253 if (info_ptr == NULL)
6254 return;
348e048f 6255
78d4d2c5
JK
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
96b79293 6258 abfd = section->get_bfd_owner ();
348e048f 6259
c0ab21c2
TT
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
3019eac3 6262
78d4d2c5
JK
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
78d4d2c5
JK
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
8b70b953 6272
9c541725 6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6274
a49dd8dd
JK
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
9c541725 6277 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6278
78d4d2c5
JK
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
348e048f 6281
ed2dc618 6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6283 abbrev_section, ptr, section_kind);
348e048f 6284
4057dfde 6285 length = header.get_length ();
6caca83c 6286
78d4d2c5
JK
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
43988095
JK
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
dee91e82 6295
78d4d2c5
JK
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
298e9637 6299 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6300 else
298e9637 6301 types_htab = allocate_signatured_type_table ();
78d4d2c5 6302 }
8b70b953 6303
78d4d2c5
JK
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
5989a64e 6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
43988095 6310 dwo_tu->signature = header.signature;
9c541725 6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6312 dwo_tu->section = section;
9c541725 6313 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
5989a64e 6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6322 sig_type->signature = header.signature;
9c541725 6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6324 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6325 sig_type->per_cu.is_debug_types = 1;
6326 sig_type->per_cu.section = section;
9c541725 6327 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6328 sig_type->per_cu.length = length;
6329 }
6330
b0b6a987 6331 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6332 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6333 INSERT);
6334 gdb_assert (slot != NULL);
6335 if (*slot != NULL)
6336 {
9c541725 6337 sect_offset dup_sect_off;
0349ea22 6338
3019eac3
DE
6339 if (dwo_file)
6340 {
78d4d2c5
JK
6341 const struct dwo_unit *dup_tu
6342 = (const struct dwo_unit *) *slot;
6343
9c541725 6344 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6345 }
6346 else
6347 {
78d4d2c5
JK
6348 const struct signatured_type *dup_tu
6349 = (const struct signatured_type *) *slot;
6350
9c541725 6351 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6352 }
8b70b953 6353
b98664d3 6354 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6355 " the entry at offset %s, signature %s"),
6356 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6357 hex_string (header.signature));
78d4d2c5
JK
6358 }
6359 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6360
78d4d2c5 6361 if (dwarf_read_debug > 1)
9d8780f0
SM
6362 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6363 sect_offset_str (sect_off),
43988095 6364 hex_string (header.signature));
3019eac3 6365
78d4d2c5
JK
6366 info_ptr += length;
6367 }
6368}
3019eac3 6369
78d4d2c5
JK
6370/* Create the hash table of all entries in the .debug_types
6371 (or .debug_types.dwo) section(s).
6372 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6373 otherwise it is NULL.
b3c8eb43 6374
78d4d2c5 6375 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6376
78d4d2c5 6377 Note: This function processes DWO files only, not DWP files. */
348e048f 6378
78d4d2c5 6379static void
ed2dc618
SM
6380create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6381 struct dwo_file *dwo_file,
fd5866f6 6382 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6383 htab_up &types_htab)
78d4d2c5 6384{
fd5866f6
SM
6385 for (dwarf2_section_info &section : type_sections)
6386 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6387 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6388}
6389
6390/* Create the hash table of all entries in the .debug_types section,
6391 and initialize all_type_units.
6392 The result is zero if there is an error (e.g. missing .debug_types section),
6393 otherwise non-zero. */
6394
6395static int
ed2dc618 6396create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6397{
b0b6a987 6398 htab_up types_htab;
3019eac3 6399
ed2dc618 6400 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6401 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6402 rcuh_kind::COMPILE);
ed2dc618 6403 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6404 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6405 if (types_htab == NULL)
6406 {
5989a64e 6407 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6408 return 0;
6409 }
6410
5989a64e 6411 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6412
5989a64e
SM
6413 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6414 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6415 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6416
5989a64e 6417 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6418 add_signatured_type_cu_to_table,
5989a64e 6419 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6420
348e048f
DE
6421 return 1;
6422}
6423
5989a64e 6424/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6425 If SLOT is non-NULL, it is the entry to use in the hash table.
6426 Otherwise we find one. */
6427
6428static struct signatured_type *
ed2dc618
SM
6429add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6430 void **slot)
6aa5f3a6 6431{
5989a64e
SM
6432 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6433 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6434 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6435
5989a64e 6436 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6437
af758d11
SM
6438 dwarf2_per_objfile->resize_symtabs ();
6439
5989a64e 6440 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6441 sig_type->signature = sig;
6442 sig_type->per_cu.is_debug_types = 1;
5989a64e 6443 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6444 {
6445 sig_type->per_cu.v.quick =
5989a64e 6446 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6447 struct dwarf2_per_cu_quick_data);
6448 }
6449
6450 if (slot == NULL)
6451 {
5989a64e 6452 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6453 sig_type, INSERT);
6454 }
6455 gdb_assert (*slot == NULL);
6456 *slot = sig_type;
6457 /* The rest of sig_type must be filled in by the caller. */
6458 return sig_type;
6459}
6460
a2ce51a0
DE
6461/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6462 Fill in SIG_ENTRY with DWO_ENTRY. */
6463
6464static void
ed2dc618 6465fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6466 struct signatured_type *sig_entry,
6467 struct dwo_unit *dwo_entry)
6468{
1859c670
SM
6469 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6470
7ee85ab1 6471 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6472 gdb_assert (! sig_entry->per_cu.queued);
6473 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6474 if (per_bfd->using_index)
6aa5f3a6
DE
6475 {
6476 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6477 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6478 }
6479 else
6480 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6481 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6482 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6483 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6484 gdb_assert (sig_entry->dwo_unit == NULL);
6485
6486 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6487 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6488 sig_entry->per_cu.length = dwo_entry->length;
6489 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6490 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 6491 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6492 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6493 sig_entry->dwo_unit = dwo_entry;
6494}
6495
6496/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6497 If we haven't read the TU yet, create the signatured_type data structure
6498 for a TU to be read in directly from a DWO file, bypassing the stub.
6499 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6500 using .gdb_index, then when reading a CU we want to stay in the DWO file
6501 containing that CU. Otherwise we could end up reading several other DWO
6502 files (due to comdat folding) to process the transitive closure of all the
6503 mentioned TUs, and that can be slow. The current DWO file will have every
6504 type signature that it needs.
a2ce51a0
DE
6505 We only do this for .gdb_index because in the psymtab case we already have
6506 to read all the DWOs to build the type unit groups. */
6507
6508static struct signatured_type *
6509lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6510{
5e22e966 6511 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6512 struct dwo_file *dwo_file;
6513 struct dwo_unit find_dwo_entry, *dwo_entry;
6514 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6515 void **slot;
a2ce51a0 6516
5989a64e 6517 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6518
6aa5f3a6
DE
6519 /* If TU skeletons have been removed then we may not have read in any
6520 TUs yet. */
5989a64e
SM
6521 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6522 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6523
6524 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6525 Use the global signatured_types array to do our own comdat-folding
6526 of types. If this is the first time we're reading this TU, and
6527 the TU has an entry in .gdb_index, replace the recorded data from
6528 .gdb_index with this TU. */
a2ce51a0 6529
a2ce51a0 6530 find_sig_entry.signature = sig;
5989a64e 6531 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6532 &find_sig_entry, INSERT);
9a3c8263 6533 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6534
6535 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6536 read. Don't reassign the global entry to point to this DWO if that's
6537 the case. Also note that if the TU is already being read, it may not
6538 have come from a DWO, the program may be a mix of Fission-compiled
6539 code and non-Fission-compiled code. */
6540
6541 /* Have we already tried to read this TU?
6542 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6543 needn't exist in the global table yet). */
6544 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6545 return sig_entry;
6546
6aa5f3a6
DE
6547 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6548 dwo_unit of the TU itself. */
6549 dwo_file = cu->dwo_unit->dwo_file;
6550
a2ce51a0
DE
6551 /* Ok, this is the first time we're reading this TU. */
6552 if (dwo_file->tus == NULL)
6553 return NULL;
6554 find_dwo_entry.signature = sig;
b0b6a987
TT
6555 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6556 &find_dwo_entry);
a2ce51a0
DE
6557 if (dwo_entry == NULL)
6558 return NULL;
6559
6aa5f3a6
DE
6560 /* If the global table doesn't have an entry for this TU, add one. */
6561 if (sig_entry == NULL)
ed2dc618 6562 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6563
ed2dc618 6564 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6565 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6566 return sig_entry;
6567}
6568
a2ce51a0
DE
6569/* Subroutine of lookup_signatured_type.
6570 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6571 then try the DWP file. If the TU stub (skeleton) has been removed then
6572 it won't be in .gdb_index. */
a2ce51a0
DE
6573
6574static struct signatured_type *
6575lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6576{
5e22e966 6577 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6578 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6579 struct dwo_unit *dwo_entry;
6580 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6581 void **slot;
a2ce51a0 6582
5989a64e 6583 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6584 gdb_assert (dwp_file != NULL);
6585
6aa5f3a6
DE
6586 /* If TU skeletons have been removed then we may not have read in any
6587 TUs yet. */
5989a64e
SM
6588 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6589 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6590
6aa5f3a6 6591 find_sig_entry.signature = sig;
5989a64e 6592 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6593 &find_sig_entry, INSERT);
9a3c8263 6594 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6595
6596 /* Have we already tried to read this TU?
6597 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6598 needn't exist in the global table yet). */
6599 if (sig_entry != NULL)
6600 return sig_entry;
6601
a2ce51a0
DE
6602 if (dwp_file->tus == NULL)
6603 return NULL;
ed2dc618 6604 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6605 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6606 if (dwo_entry == NULL)
6607 return NULL;
6608
ed2dc618
SM
6609 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6610 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6611
a2ce51a0
DE
6612 return sig_entry;
6613}
6614
380bca97 6615/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6616 Returns NULL if signature SIG is not present in the table.
6617 It is up to the caller to complain about this. */
348e048f
DE
6618
6619static struct signatured_type *
a2ce51a0 6620lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6621{
5e22e966 6622 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6623
a2ce51a0 6624 if (cu->dwo_unit
5989a64e 6625 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6626 {
6627 /* We're in a DWO/DWP file, and we're using .gdb_index.
6628 These cases require special processing. */
ed2dc618 6629 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6630 return lookup_dwo_signatured_type (cu, sig);
6631 else
6632 return lookup_dwp_signatured_type (cu, sig);
6633 }
6634 else
6635 {
6636 struct signatured_type find_entry, *entry;
348e048f 6637
5989a64e 6638 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6639 return NULL;
6640 find_entry.signature = sig;
9a3c8263 6641 entry = ((struct signatured_type *)
5989a64e 6642 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6643 &find_entry));
a2ce51a0
DE
6644 return entry;
6645 }
348e048f 6646}
18a8505e 6647
42e7ad6c 6648/* Low level DIE reading support. */
348e048f 6649
d85a05f0
DJ
6650/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6651
6652static void
6653init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6654 struct dwarf2_cu *cu,
3019eac3 6655 struct dwarf2_section_info *section,
685af9cd
TT
6656 struct dwo_file *dwo_file,
6657 struct abbrev_table *abbrev_table)
d85a05f0 6658{
fceca515 6659 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6660 reader->abfd = section->get_bfd_owner ();
d85a05f0 6661 reader->cu = cu;
3019eac3 6662 reader->dwo_file = dwo_file;
dee91e82
DE
6663 reader->die_section = section;
6664 reader->buffer = section->buffer;
f664829e 6665 reader->buffer_end = section->buffer + section->size;
685af9cd 6666 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6667}
6668
c0ab21c2 6669/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6670 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6671 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6672 already.
6673
6674 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6675 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6676 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6677 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6678 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6679 STUB_COMP_DIR may be non-NULL.
3e225074 6680 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6681 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6682 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6683 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6684 kept around for at least as long as *RESULT_READER.
6685
b0c7bfa9
DE
6686 The result is non-zero if a valid (non-dummy) DIE was found. */
6687
6688static int
6689read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6690 struct dwo_unit *dwo_unit,
b0c7bfa9 6691 struct die_info *stub_comp_unit_die,
a2ce51a0 6692 const char *stub_comp_dir,
b0c7bfa9 6693 struct die_reader_specs *result_reader,
d521ce57 6694 const gdb_byte **result_info_ptr,
b0c7bfa9 6695 struct die_info **result_comp_unit_die,
685af9cd 6696 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6697{
ed2dc618 6698 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6699 struct objfile *objfile = dwarf2_per_objfile->objfile;
6700 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6701 bfd *abfd;
d521ce57 6702 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6703 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6704 int i,num_extra_attrs;
6705 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6706 struct die_info *comp_unit_die;
6707
b0aeadb3
DE
6708 /* At most one of these may be provided. */
6709 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6710
b0c7bfa9
DE
6711 /* These attributes aren't processed until later:
6712 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6713 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6714 referenced later. However, these attributes are found in the stub
6715 which we won't have later. In order to not impose this complication
6716 on the rest of the code, we read them here and copy them to the
6717 DWO CU/TU die. */
b0c7bfa9
DE
6718
6719 stmt_list = NULL;
6720 low_pc = NULL;
6721 high_pc = NULL;
6722 ranges = NULL;
6723 comp_dir = NULL;
6724
6725 if (stub_comp_unit_die != NULL)
6726 {
6727 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6728 DWO file. */
6729 if (! this_cu->is_debug_types)
6730 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6731 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6732 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6733 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6734 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6735
a39fdb41 6736 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6737
18a8505e
AT
6738 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6739 here (if needed). We need the value before we can process
6740 DW_AT_ranges. */
a39fdb41 6741 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6742 }
a2ce51a0
DE
6743 else if (stub_comp_dir != NULL)
6744 {
6745 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6746 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6747 comp_dir->name = DW_AT_comp_dir;
6748 comp_dir->form = DW_FORM_string;
6749 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6750 DW_STRING (comp_dir) = stub_comp_dir;
6751 }
b0c7bfa9
DE
6752
6753 /* Set up for reading the DWO CU/TU. */
6754 cu->dwo_unit = dwo_unit;
685af9cd 6755 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6756 section->read (objfile);
6757 abfd = section->get_bfd_owner ();
9c541725
PA
6758 begin_info_ptr = info_ptr = (section->buffer
6759 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6760 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6761
6762 if (this_cu->is_debug_types)
6763 {
b0c7bfa9
DE
6764 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6765
ed2dc618
SM
6766 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6767 &cu->header, section,
b0c7bfa9 6768 dwo_abbrev_section,
43988095 6769 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6770 /* This is not an assert because it can be caused by bad debug info. */
43988095 6771 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6772 {
6773 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6774 " TU at offset %s [in module %s]"),
a2ce51a0 6775 hex_string (sig_type->signature),
43988095 6776 hex_string (cu->header.signature),
9d8780f0 6777 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6778 bfd_get_filename (abfd));
6779 }
9c541725 6780 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6781 /* For DWOs coming from DWP files, we don't know the CU length
6782 nor the type's offset in the TU until now. */
4057dfde 6783 dwo_unit->length = cu->header.get_length ();
9c541725 6784 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6785
6786 /* Establish the type offset that can be used to lookup the type.
6787 For DWO files, we don't know it until now. */
9c541725
PA
6788 sig_type->type_offset_in_section
6789 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6790 }
6791 else
6792 {
ed2dc618
SM
6793 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6794 &cu->header, section,
b0c7bfa9 6795 dwo_abbrev_section,
43988095 6796 info_ptr, rcuh_kind::COMPILE);
9c541725 6797 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6798 /* For DWOs coming from DWP files, we don't know the CU length
6799 until now. */
4057dfde 6800 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6801 }
6802
685af9cd 6803 *result_dwo_abbrev_table
86de1d91
TT
6804 = abbrev_table::read (objfile, dwo_abbrev_section,
6805 cu->header.abbrev_sect_off);
685af9cd
TT
6806 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6807 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6808
6809 /* Read in the die, but leave space to copy over the attributes
6810 from the stub. This has the benefit of simplifying the rest of
6811 the code - all the work to maintain the illusion of a single
6812 DW_TAG_{compile,type}_unit DIE is done here. */
6813 num_extra_attrs = ((stmt_list != NULL)
6814 + (low_pc != NULL)
6815 + (high_pc != NULL)
6816 + (ranges != NULL)
6817 + (comp_dir != NULL));
6818 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6819 num_extra_attrs);
b0c7bfa9
DE
6820
6821 /* Copy over the attributes from the stub to the DIE we just read in. */
6822 comp_unit_die = *result_comp_unit_die;
6823 i = comp_unit_die->num_attrs;
6824 if (stmt_list != NULL)
6825 comp_unit_die->attrs[i++] = *stmt_list;
6826 if (low_pc != NULL)
6827 comp_unit_die->attrs[i++] = *low_pc;
6828 if (high_pc != NULL)
6829 comp_unit_die->attrs[i++] = *high_pc;
6830 if (ranges != NULL)
6831 comp_unit_die->attrs[i++] = *ranges;
6832 if (comp_dir != NULL)
6833 comp_unit_die->attrs[i++] = *comp_dir;
6834 comp_unit_die->num_attrs += num_extra_attrs;
6835
b4f54984 6836 if (dwarf_die_debug)
bf6af496
DE
6837 {
6838 fprintf_unfiltered (gdb_stdlog,
6839 "Read die from %s@0x%x of %s:\n",
96b79293 6840 section->get_name (),
bf6af496
DE
6841 (unsigned) (begin_info_ptr - section->buffer),
6842 bfd_get_filename (abfd));
b4f54984 6843 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6844 }
6845
b0c7bfa9
DE
6846 /* Skip dummy compilation units. */
6847 if (info_ptr >= begin_info_ptr + dwo_unit->length
6848 || peek_abbrev_code (abfd, info_ptr) == 0)
6849 return 0;
6850
6851 *result_info_ptr = info_ptr;
6852 return 1;
6853}
6854
a084a2a6
AT
6855/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6856 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6857 signature is part of the header. */
6858static gdb::optional<ULONGEST>
6859lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6860{
6861 if (cu->header.version >= 5)
6862 return cu->header.signature;
6863 struct attribute *attr;
6864 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6865 if (attr == nullptr)
6866 return gdb::optional<ULONGEST> ();
6867 return DW_UNSND (attr);
6868}
6869
c0ab21c2 6870/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6871 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6872 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6873
6874static struct dwo_unit *
6875lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6876 struct die_info *comp_unit_die,
6877 const char *dwo_name)
b0c7bfa9
DE
6878{
6879 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6880 struct dwo_unit *dwo_unit;
c0ab21c2 6881 const char *comp_dir;
b0c7bfa9 6882
a2ce51a0
DE
6883 gdb_assert (cu != NULL);
6884
b0c7bfa9 6885 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6886 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6887 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6888
6889 if (this_cu->is_debug_types)
6890 {
6891 struct signatured_type *sig_type;
6892
6893 /* Since this_cu is the first member of struct signatured_type,
6894 we can go from a pointer to one to a pointer to the other. */
6895 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6896 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6897 }
6898 else
6899 {
a084a2a6
AT
6900 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6901 if (!signature.has_value ())
b0c7bfa9
DE
6902 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6903 " [in module %s]"),
313bad1b 6904 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
b0c7bfa9 6905 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6906 *signature);
b0c7bfa9
DE
6907 }
6908
b0c7bfa9
DE
6909 return dwo_unit;
6910}
6911
c0ab21c2 6912/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6913 See it for a description of the parameters.
fcd3b13d 6914 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6915
c0ab21c2 6916void
9e021579
SM
6917cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6918 dwarf2_per_objfile *per_objfile,
6751ebae 6919 int use_existing_cu)
a2ce51a0 6920{
a2ce51a0 6921 struct signatured_type *sig_type;
a2ce51a0
DE
6922
6923 /* Verify we can do the following downcast, and that we have the
6924 data we need. */
6925 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6926 sig_type = (struct signatured_type *) this_cu;
6927 gdb_assert (sig_type->dwo_unit != NULL);
6928
6aa5f3a6
DE
6929 if (use_existing_cu && this_cu->cu != NULL)
6930 {
6931 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6932 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6933 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6934 }
6935 else
6936 {
6937 /* If !use_existing_cu, this_cu->cu must be NULL. */
6938 gdb_assert (this_cu->cu == NULL);
9e021579 6939 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6940 }
6941
6942 /* A future optimization, if needed, would be to use an existing
6943 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6944 could share abbrev tables. */
a2ce51a0
DE
6945
6946 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6947 NULL /* stub_comp_unit_die */,
6948 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6949 this, &info_ptr,
3e225074 6950 &comp_unit_die,
c0ab21c2 6951 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6952 {
6953 /* Dummy die. */
c0ab21c2 6954 dummy_p = true;
a2ce51a0 6955 }
a2ce51a0
DE
6956}
6957
fd820528 6958/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6959 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6960
f4dc4d17
DE
6961 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6962 Otherwise the table specified in the comp unit header is read in and used.
6963 This is an optimization for when we already have the abbrev table.
6964
dee91e82 6965 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6966 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6967
ab432490
SM
6968cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6969 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6970 struct abbrev_table *abbrev_table,
6751ebae 6971 int use_existing_cu,
c0ab21c2
TT
6972 bool skip_partial)
6973 : die_reader_specs {},
6751ebae 6974 m_this_cu (this_cu)
c906108c 6975{
dee91e82 6976 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6977 struct dwarf2_section_info *section = this_cu->section;
96b79293 6978 bfd *abfd = section->get_bfd_owner ();
dee91e82 6979 struct dwarf2_cu *cu;
c0ab21c2 6980 const gdb_byte *begin_info_ptr;
dee91e82 6981 struct signatured_type *sig_type = NULL;
4bdcc0c1 6982 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6983 /* Non-zero if CU currently points to a DWO file and we need to
6984 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6985 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6986 int rereading_dwo_cu = 0;
c906108c 6987
b4f54984 6988 if (dwarf_die_debug)
9d8780f0 6989 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6990 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6991 sect_offset_str (this_cu->sect_off));
09406207 6992
a2ce51a0
DE
6993 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6994 file (instead of going through the stub), short-circuit all of this. */
6995 if (this_cu->reading_dwo_directly)
6996 {
6997 /* Narrow down the scope of possibilities to have to understand. */
6998 gdb_assert (this_cu->is_debug_types);
6999 gdb_assert (abbrev_table == NULL);
9e021579 7000 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
7001 return;
7002 }
7003
dee91e82 7004 /* This is cheap if the section is already read in. */
96b79293 7005 section->read (objfile);
dee91e82 7006
9c541725 7007 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7008
7009 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7010
7011 if (use_existing_cu && this_cu->cu != NULL)
7012 {
7013 cu = this_cu->cu;
42e7ad6c
DE
7014 /* If this CU is from a DWO file we need to start over, we need to
7015 refetch the attributes from the skeleton CU.
7016 This could be optimized by retrieving those attributes from when we
7017 were here the first time: the previous comp_unit_die was stored in
7018 comp_unit_obstack. But there's no data yet that we need this
7019 optimization. */
7020 if (cu->dwo_unit != NULL)
7021 rereading_dwo_cu = 1;
dee91e82
DE
7022 }
7023 else
7024 {
7025 /* If !use_existing_cu, this_cu->cu must be NULL. */
7026 gdb_assert (this_cu->cu == NULL);
9e021579 7027 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7028 cu = m_new_cu.get ();
42e7ad6c 7029 }
dee91e82 7030
b0c7bfa9 7031 /* Get the header. */
9c541725 7032 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7033 {
7034 /* We already have the header, there's no need to read it in again. */
9c541725 7035 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7036 }
7037 else
7038 {
3019eac3 7039 if (this_cu->is_debug_types)
dee91e82 7040 {
ed2dc618
SM
7041 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7042 &cu->header, section,
4bdcc0c1 7043 abbrev_section, info_ptr,
43988095 7044 rcuh_kind::TYPE);
dee91e82 7045
42e7ad6c
DE
7046 /* Since per_cu is the first member of struct signatured_type,
7047 we can go from a pointer to one to a pointer to the other. */
7048 sig_type = (struct signatured_type *) this_cu;
43988095 7049 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7050 gdb_assert (sig_type->type_offset_in_tu
7051 == cu->header.type_cu_offset_in_tu);
7052 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7053
42e7ad6c
DE
7054 /* LENGTH has not been set yet for type units if we're
7055 using .gdb_index. */
4057dfde 7056 this_cu->length = cu->header.get_length ();
3019eac3
DE
7057
7058 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7059 sig_type->type_offset_in_section =
7060 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7061
7062 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7063 }
7064 else
7065 {
ed2dc618
SM
7066 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7067 &cu->header, section,
4bdcc0c1 7068 abbrev_section,
43988095
JK
7069 info_ptr,
7070 rcuh_kind::COMPILE);
dee91e82 7071
9c541725 7072 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7073 if (this_cu->length == 0)
7074 this_cu->length = cu->header.get_length ();
7075 else
7076 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7077 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7078 }
7079 }
10b3939b 7080
6caca83c 7081 /* Skip dummy compilation units. */
dee91e82 7082 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7083 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7084 {
7085 dummy_p = true;
7086 return;
7087 }
6caca83c 7088
433df2d4
DE
7089 /* If we don't have them yet, read the abbrevs for this compilation unit.
7090 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7091 done. */
f4dc4d17 7092 if (abbrev_table != NULL)
685af9cd
TT
7093 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7094 else
f4dc4d17 7095 {
c0ab21c2 7096 m_abbrev_table_holder
86de1d91
TT
7097 = abbrev_table::read (objfile, abbrev_section,
7098 cu->header.abbrev_sect_off);
c0ab21c2 7099 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7100 }
af703f96 7101
dee91e82 7102 /* Read the top level CU/TU die. */
c0ab21c2 7103 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7104 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7105
58f0c718 7106 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7107 {
7108 dummy_p = true;
7109 return;
7110 }
58f0c718 7111
b0c7bfa9 7112 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7113 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7114 table from the DWO file and pass the ownership over to us. It will be
7115 referenced from READER, so we must make sure to free it after we're done
7116 with READER.
7117
b0c7bfa9
DE
7118 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7119 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7120 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7121 if (dwo_name != nullptr)
3019eac3 7122 {
3019eac3 7123 struct dwo_unit *dwo_unit;
b0c7bfa9 7124 struct die_info *dwo_comp_unit_die;
3019eac3 7125
3e225074 7126 if (comp_unit_die->has_children)
6a506a2d 7127 {
b98664d3 7128 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7129 " has children (offset %s) [in module %s]"),
7130 sect_offset_str (this_cu->sect_off),
7131 bfd_get_filename (abfd));
6a506a2d 7132 }
c0ab21c2 7133 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 7134 if (dwo_unit != NULL)
3019eac3 7135 {
6a506a2d 7136 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7137 comp_unit_die, NULL,
c0ab21c2 7138 this, &info_ptr,
3e225074 7139 &dwo_comp_unit_die,
c0ab21c2 7140 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7141 {
7142 /* Dummy die. */
c0ab21c2 7143 dummy_p = true;
6a506a2d
DE
7144 return;
7145 }
7146 comp_unit_die = dwo_comp_unit_die;
7147 }
7148 else
7149 {
7150 /* Yikes, we couldn't find the rest of the DIE, we only have
7151 the stub. A complaint has already been logged. There's
7152 not much more we can do except pass on the stub DIE to
7153 die_reader_func. We don't want to throw an error on bad
7154 debug info. */
3019eac3
DE
7155 }
7156 }
c0ab21c2 7157}
3019eac3 7158
6751ebae
TT
7159void
7160cutu_reader::keep ()
c0ab21c2 7161{
b0c7bfa9 7162 /* Done, clean up. */
6751ebae
TT
7163 gdb_assert (!dummy_p);
7164 if (m_new_cu != NULL)
348e048f 7165 {
e3beb21d
SM
7166 /* We know that m_this_cu->cu is set, since we are in the process of
7167 parsing the CU. */
7168 gdb_assert (m_this_cu->cu != nullptr);
7169 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7170
fcd3b13d 7171 /* Link this CU into read_in_chain. */
5989a64e
SM
7172 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7173 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7174 /* The chain owns it now. */
c0ab21c2 7175 m_new_cu.release ();
348e048f 7176 }
dee91e82
DE
7177}
7178
18a8505e
AT
7179/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7180 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7181 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7182
7183 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7184 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7185
7186 We fill in THIS_CU->length.
7187
dee91e82 7188 THIS_CU->cu is always freed when done.
3019eac3 7189 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7190 to care whether it refers to the "main" CU or the DWO CU.
7191
7192 When parent_cu is passed, it is used to provide a default value for
7193 str_offsets_base and addr_base from the parent. */
dee91e82 7194
ab432490
SM
7195cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7196 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7197 struct dwarf2_cu *parent_cu,
7198 struct dwo_file *dwo_file)
7199 : die_reader_specs {},
7200 m_this_cu (this_cu)
dee91e82
DE
7201{
7202 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7203 struct dwarf2_section_info *section = this_cu->section;
96b79293 7204 bfd *abfd = section->get_bfd_owner ();
33e80786 7205 struct dwarf2_section_info *abbrev_section;
d521ce57 7206 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7207
b4f54984 7208 if (dwarf_die_debug)
9d8780f0 7209 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7210 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7211 sect_offset_str (this_cu->sect_off));
09406207 7212
dee91e82
DE
7213 gdb_assert (this_cu->cu == NULL);
7214
33e80786
DE
7215 abbrev_section = (dwo_file != NULL
7216 ? &dwo_file->sections.abbrev
7217 : get_abbrev_section_for_cu (this_cu));
7218
dee91e82 7219 /* This is cheap if the section is already read in. */
96b79293 7220 section->read (objfile);
dee91e82 7221
9e021579 7222 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7223
9c541725 7224 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7225 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7226 &m_new_cu->header, section,
4bdcc0c1 7227 abbrev_section, info_ptr,
43988095
JK
7228 (this_cu->is_debug_types
7229 ? rcuh_kind::TYPE
7230 : rcuh_kind::COMPILE));
dee91e82 7231
18a8505e
AT
7232 if (parent_cu != nullptr)
7233 {
c0ab21c2
TT
7234 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7235 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7236 }
4057dfde 7237 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7238
7239 /* Skip dummy compilation units. */
7240 if (info_ptr >= begin_info_ptr + this_cu->length
7241 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7242 {
7243 dummy_p = true;
7244 return;
7245 }
72bf9492 7246
c0ab21c2 7247 m_abbrev_table_holder
86de1d91
TT
7248 = abbrev_table::read (objfile, abbrev_section,
7249 m_new_cu->header.abbrev_sect_off);
dee91e82 7250
c0ab21c2
TT
7251 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7252 m_abbrev_table_holder.get ());
3e225074 7253 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7254}
7255
0018ea6f
DE
7256\f
7257/* Type Unit Groups.
dee91e82 7258
0018ea6f
DE
7259 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7260 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7261 so that all types coming from the same compilation (.o file) are grouped
7262 together. A future step could be to put the types in the same symtab as
7263 the CU the types ultimately came from. */
ff013f42 7264
f4dc4d17
DE
7265static hashval_t
7266hash_type_unit_group (const void *item)
7267{
9a3c8263
SM
7268 const struct type_unit_group *tu_group
7269 = (const struct type_unit_group *) item;
f4dc4d17 7270
094b34ac 7271 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7272}
348e048f
DE
7273
7274static int
f4dc4d17 7275eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7276{
9a3c8263
SM
7277 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7278 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7279
094b34ac 7280 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7281}
348e048f 7282
f4dc4d17
DE
7283/* Allocate a hash table for type unit groups. */
7284
eaa5fa8b 7285static htab_up
298e9637 7286allocate_type_unit_groups_table ()
f4dc4d17 7287{
eaa5fa8b
TT
7288 return htab_up (htab_create_alloc (3,
7289 hash_type_unit_group,
7290 eq_type_unit_group,
7291 NULL, xcalloc, xfree));
f4dc4d17 7292}
dee91e82 7293
f4dc4d17
DE
7294/* Type units that don't have DW_AT_stmt_list are grouped into their own
7295 partial symtabs. We combine several TUs per psymtab to not let the size
7296 of any one psymtab grow too big. */
7297#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7298#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7299
094b34ac 7300/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7301 Create the type_unit_group object used to hold one or more TUs. */
7302
7303static struct type_unit_group *
094b34ac 7304create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7305{
5e22e966 7306 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7307 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7308 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7309 struct type_unit_group *tu_group;
f4dc4d17 7310
5989a64e 7311 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7312 struct type_unit_group);
094b34ac 7313 per_cu = &tu_group->per_cu;
518817b3 7314 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 7315 per_cu->per_bfd = per_bfd;
f4dc4d17 7316
1859c670 7317 if (per_bfd->using_index)
094b34ac 7318 {
1859c670 7319 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7320 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7321 }
7322 else
7323 {
9c541725 7324 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7325 dwarf2_psymtab *pst;
528e1572 7326 std::string name;
094b34ac
DE
7327
7328 /* Give the symtab a useful name for debug purposes. */
7329 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7330 name = string_printf ("<type_units_%d>",
7331 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7332 else
528e1572 7333 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7334
7aa104c4 7335 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7336 pst->anonymous = true;
094b34ac 7337 }
f4dc4d17 7338
094b34ac 7339 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7340 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7341
7342 return tu_group;
7343}
7344
094b34ac
DE
7345/* Look up the type_unit_group for type unit CU, and create it if necessary.
7346 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7347
7348static struct type_unit_group *
ff39bb5e 7349get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7350{
5e22e966 7351 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7352 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7353 struct type_unit_group *tu_group;
7354 void **slot;
7355 unsigned int line_offset;
7356 struct type_unit_group type_unit_group_for_lookup;
7357
5989a64e
SM
7358 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7359 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7360
7361 /* Do we need to create a new group, or can we use an existing one? */
7362
7363 if (stmt_list)
7364 {
7365 line_offset = DW_UNSND (stmt_list);
7366 ++tu_stats->nr_symtab_sharers;
7367 }
7368 else
7369 {
7370 /* Ugh, no stmt_list. Rare, but we have to handle it.
7371 We can do various things here like create one group per TU or
7372 spread them over multiple groups to split up the expansion work.
7373 To avoid worst case scenarios (too many groups or too large groups)
7374 we, umm, group them in bunches. */
7375 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7376 | (tu_stats->nr_stmt_less_type_units
7377 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7378 ++tu_stats->nr_stmt_less_type_units;
7379 }
7380
094b34ac 7381 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7382 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7383 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7384 &type_unit_group_for_lookup, INSERT);
7385 if (*slot != NULL)
7386 {
9a3c8263 7387 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7388 gdb_assert (tu_group != NULL);
7389 }
7390 else
7391 {
9c541725 7392 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7393 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7394 *slot = tu_group;
7395 ++tu_stats->nr_symtabs;
7396 }
7397
7398 return tu_group;
7399}
0018ea6f
DE
7400\f
7401/* Partial symbol tables. */
7402
7403/* Create a psymtab named NAME and assign it to PER_CU.
7404
7405 The caller must fill in the following details:
7406 dirname, textlow, texthigh. */
7407
891813be 7408static dwarf2_psymtab *
7aa104c4
SM
7409create_partial_symtab (dwarf2_per_cu_data *per_cu,
7410 dwarf2_per_objfile *per_objfile,
7411 const char *name)
0018ea6f 7412{
7aa104c4 7413 struct objfile *objfile = per_objfile->objfile;
891813be 7414 dwarf2_psymtab *pst;
0018ea6f 7415
9f4e76a4 7416 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7417
6d94535f 7418 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7419
7420 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7421 per_cu->v.psymtab = pst;
7422
7423 return pst;
7424}
7425
c0ab21c2 7426/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7427
7428static void
7429process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7430 const gdb_byte *info_ptr,
0018ea6f 7431 struct die_info *comp_unit_die,
c0ab21c2 7432 enum language pretend_language)
0018ea6f
DE
7433{
7434 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7435 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7436 struct objfile *objfile = per_objfile->objfile;
08feed99 7437 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7438 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7439 CORE_ADDR baseaddr;
7440 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7441 dwarf2_psymtab *pst;
3a2b436a 7442 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7443 const char *filename;
0018ea6f 7444
0018ea6f
DE
7445 gdb_assert (! per_cu->is_debug_types);
7446
c0ab21c2 7447 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7448
0018ea6f 7449 /* Allocate a new partial symbol table structure. */
2e927613
TV
7450 gdb::unique_xmalloc_ptr<char> debug_filename;
7451 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7452 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7453 if (filename == NULL)
0018ea6f 7454 filename = "";
2e927613
TV
7455 else if (strcmp (filename, artificial) == 0)
7456 {
7457 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7458 sect_offset_str (per_cu->sect_off),
7459 (char *) NULL));
2e927613
TV
7460 filename = debug_filename.get ();
7461 }
0018ea6f 7462
7aa104c4 7463 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7464
7465 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7466 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7467
b3b3bada 7468 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7469
7470 dwarf2_find_base_address (comp_unit_die, cu);
7471
7472 /* Possibly set the default values of LOWPC and HIGHPC from
7473 `DW_AT_ranges'. */
3a2b436a
JK
7474 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7475 &best_highpc, cu, pst);
7476 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7477 {
7478 CORE_ADDR low
7479 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7480 - baseaddr);
7481 CORE_ADDR high
7482 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7483 - baseaddr - 1);
7484 /* Store the contiguous range if it is not empty; it can be
7485 empty for CUs with no code. */
d320c2b5
TT
7486 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7487 low, high, pst);
79748972 7488 }
0018ea6f
DE
7489
7490 /* Check if comp unit has_children.
7491 If so, read the rest of the partial symbols from this comp unit.
7492 If not, there's no more debug_info for this comp unit. */
3e225074 7493 if (comp_unit_die->has_children)
0018ea6f
DE
7494 {
7495 struct partial_die_info *first_die;
7496 CORE_ADDR lowpc, highpc;
7497
7498 lowpc = ((CORE_ADDR) -1);
7499 highpc = ((CORE_ADDR) 0);
7500
7501 first_die = load_partial_dies (reader, info_ptr, 1);
7502
7503 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7504 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7505
7506 /* If we didn't find a lowpc, set it to highpc to avoid
7507 complaints from `maint check'. */
7508 if (lowpc == ((CORE_ADDR) -1))
7509 lowpc = highpc;
7510
7511 /* If the compilation unit didn't have an explicit address range,
7512 then use the information extracted from its child dies. */
e385593e 7513 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7514 {
7515 best_lowpc = lowpc;
7516 best_highpc = highpc;
7517 }
7518 }
4ae976d1 7519 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7520 best_lowpc + baseaddr)
7521 - baseaddr);
4ae976d1 7522 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7523 best_highpc + baseaddr)
7524 - baseaddr);
0018ea6f 7525
8763cede 7526 end_psymtab_common (objfile, pst);
0018ea6f 7527
ae640021 7528 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7529 {
7530 int i;
ae640021 7531 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7532
7533 /* Fill in 'dependencies' here; we fill in 'users' in a
7534 post-pass. */
7535 pst->number_of_dependencies = len;
a9342b62
TT
7536 pst->dependencies
7537 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7538 for (i = 0; i < len; ++i)
7539 {
7540 pst->dependencies[i]
7541 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7542 }
0018ea6f 7543
ae640021 7544 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7545 }
7546
7547 /* Get the list of files included in the current compilation unit,
7548 and build a psymtab for each of them. */
7549 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7550
b4f54984 7551 if (dwarf_read_debug)
b926417a
TT
7552 fprintf_unfiltered (gdb_stdlog,
7553 "Psymtab for %s unit @%s: %s - %s"
7554 ", %d global, %d static syms\n",
7555 per_cu->is_debug_types ? "type" : "comp",
7556 sect_offset_str (per_cu->sect_off),
7557 paddress (gdbarch, pst->text_low (objfile)),
7558 paddress (gdbarch, pst->text_high (objfile)),
7559 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7560}
7561
7562/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7563 Process compilation unit THIS_CU for a psymtab. */
7564
7565static void
ab432490
SM
7566process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7567 dwarf2_per_objfile *per_objfile,
135f5437 7568 bool want_partial_unit,
b93601f3 7569 enum language pretend_language)
0018ea6f
DE
7570{
7571 /* If this compilation unit was already read in, free the
7572 cached copy in order to read it in again. This is
7573 necessary because we skipped some symbols when we first
7574 read in the compilation unit (see load_partial_dies).
7575 This problem could be avoided, but the benefit is unclear. */
7576 if (this_cu->cu != NULL)
7577 free_one_cached_comp_unit (this_cu);
7578
ab432490 7579 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7580
58990295
TV
7581 switch (reader.comp_unit_die->tag)
7582 {
7583 case DW_TAG_compile_unit:
7584 this_cu->unit_type = DW_UT_compile;
7585 break;
7586 case DW_TAG_partial_unit:
7587 this_cu->unit_type = DW_UT_partial;
7588 break;
7589 default:
7590 abort ();
7591 }
7592
c0ab21c2 7593 if (reader.dummy_p)
f1902523 7594 {
c0ab21c2 7595 /* Nothing. */
f1902523 7596 }
c0ab21c2 7597 else if (this_cu->is_debug_types)
3e225074
TT
7598 build_type_psymtabs_reader (&reader, reader.info_ptr,
7599 reader.comp_unit_die);
135f5437
TT
7600 else if (want_partial_unit
7601 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7602 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7603 reader.comp_unit_die,
c0ab21c2 7604 pretend_language);
0018ea6f 7605
58990295
TV
7606 this_cu->lang = this_cu->cu->language;
7607
0018ea6f 7608 /* Age out any secondary CUs. */
ed2dc618 7609 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7610}
f4dc4d17
DE
7611
7612/* Reader function for build_type_psymtabs. */
7613
7614static void
7615build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7616 const gdb_byte *info_ptr,
3e225074 7617 struct die_info *type_unit_die)
f4dc4d17 7618{
5e22e966 7619 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7620 struct objfile *objfile = dwarf2_per_objfile->objfile;
7621 struct dwarf2_cu *cu = reader->cu;
7622 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7623 struct signatured_type *sig_type;
f4dc4d17
DE
7624 struct type_unit_group *tu_group;
7625 struct attribute *attr;
7626 struct partial_die_info *first_die;
7627 CORE_ADDR lowpc, highpc;
891813be 7628 dwarf2_psymtab *pst;
f4dc4d17 7629
0186c6a7
DE
7630 gdb_assert (per_cu->is_debug_types);
7631 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7632
3e225074 7633 if (! type_unit_die->has_children)
f4dc4d17
DE
7634 return;
7635
052c8bb8 7636 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7637 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7638
df07e2c7 7639 if (tu_group->tus == nullptr)
a8b3b8e9 7640 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7641 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7642
7643 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7644 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7645 pst->anonymous = true;
f4dc4d17
DE
7646
7647 first_die = load_partial_dies (reader, info_ptr, 1);
7648
7649 lowpc = (CORE_ADDR) -1;
7650 highpc = (CORE_ADDR) 0;
7651 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7652
8763cede 7653 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7654}
7655
73051182
DE
7656/* Struct used to sort TUs by their abbreviation table offset. */
7657
7658struct tu_abbrev_offset
7659{
b2bdb8cf
SM
7660 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7661 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7662 {}
7663
7664 signatured_type *sig_type;
73051182
DE
7665 sect_offset abbrev_offset;
7666};
7667
484cf504 7668/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7669
484cf504
TT
7670static bool
7671sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7672 const struct tu_abbrev_offset &b)
73051182 7673{
484cf504 7674 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7675}
7676
7677/* Efficiently read all the type units.
7678 This does the bulk of the work for build_type_psymtabs.
7679
7680 The efficiency is because we sort TUs by the abbrev table they use and
7681 only read each abbrev table once. In one program there are 200K TUs
7682 sharing 8K abbrev tables.
7683
7684 The main purpose of this function is to support building the
5989a64e 7685 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7686 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7687 can collapse the search space by grouping them by stmt_list.
7688 The savings can be significant, in the same program from above the 200K TUs
7689 share 8K stmt_list tables.
7690
7691 FUNC is expected to call get_type_unit_group, which will create the
7692 struct type_unit_group if necessary and add it to
5989a64e 7693 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7694
7695static void
ed2dc618 7696build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7697{
5989a64e 7698 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7699 abbrev_table_up abbrev_table;
73051182 7700 sect_offset abbrev_offset;
73051182
DE
7701
7702 /* It's up to the caller to not call us multiple times. */
5989a64e 7703 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7704
5989a64e 7705 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7706 return;
7707
7708 /* TUs typically share abbrev tables, and there can be way more TUs than
7709 abbrev tables. Sort by abbrev table to reduce the number of times we
7710 read each abbrev table in.
7711 Alternatives are to punt or to maintain a cache of abbrev tables.
7712 This is simpler and efficient enough for now.
7713
7714 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7715 symtab to use). Typically TUs with the same abbrev offset have the same
7716 stmt_list value too so in practice this should work well.
7717
7718 The basic algorithm here is:
7719
7720 sort TUs by abbrev table
7721 for each TU with same abbrev table:
7722 read abbrev table if first user
7723 read TU top level DIE
7724 [IWBN if DWO skeletons had DW_AT_stmt_list]
7725 call FUNC */
7726
b4f54984 7727 if (dwarf_read_debug)
73051182
DE
7728 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7729
7730 /* Sort in a separate table to maintain the order of all_type_units
7731 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7732 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7733 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7734
5989a64e 7735 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7736 sorted_by_abbrev.emplace_back
7737 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7738 sig_type->per_cu.section,
7739 sig_type->per_cu.sect_off));
73051182 7740
484cf504
TT
7741 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7742 sort_tu_by_abbrev_offset);
73051182 7743
9c541725 7744 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7745
b2bdb8cf 7746 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7747 {
73051182
DE
7748 /* Switch to the next abbrev table if necessary. */
7749 if (abbrev_table == NULL
b2bdb8cf 7750 || tu.abbrev_offset != abbrev_offset)
73051182 7751 {
b2bdb8cf 7752 abbrev_offset = tu.abbrev_offset;
73051182 7753 abbrev_table =
86de1d91 7754 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7755 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7756 abbrev_offset);
73051182
DE
7757 ++tu_stats->nr_uniq_abbrev_tables;
7758 }
7759
ab432490
SM
7760 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7761 abbrev_table.get (), 0, false);
c0ab21c2
TT
7762 if (!reader.dummy_p)
7763 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7764 reader.comp_unit_die);
73051182 7765 }
6aa5f3a6 7766}
73051182 7767
6aa5f3a6
DE
7768/* Print collected type unit statistics. */
7769
7770static void
ed2dc618 7771print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7772{
5989a64e 7773 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7774
7775 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7776 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7777 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7778 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7779 tu_stats->nr_uniq_abbrev_tables);
7780 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7781 tu_stats->nr_symtabs);
7782 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7783 tu_stats->nr_symtab_sharers);
7784 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7785 tu_stats->nr_stmt_less_type_units);
7786 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7787 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7788}
7789
f4dc4d17
DE
7790/* Traversal function for build_type_psymtabs. */
7791
7792static int
7793build_type_psymtab_dependencies (void **slot, void *info)
7794{
ed2dc618
SM
7795 struct dwarf2_per_objfile *dwarf2_per_objfile
7796 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7797 struct objfile *objfile = dwarf2_per_objfile->objfile;
7798 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7799 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7800 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7801 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7802 int i;
7803
7804 gdb_assert (len > 0);
197400e8 7805 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7806
7807 pst->number_of_dependencies = len;
a9342b62 7808 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7809 for (i = 0; i < len; ++i)
f4dc4d17 7810 {
df07e2c7 7811 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7812 gdb_assert (iter->per_cu.is_debug_types);
7813 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7814 iter->type_unit_group = tu_group;
f4dc4d17
DE
7815 }
7816
df07e2c7
AB
7817 delete tu_group->tus;
7818 tu_group->tus = nullptr;
348e048f
DE
7819
7820 return 1;
7821}
7822
7823/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7824 Build partial symbol tables for the .debug_types comp-units. */
7825
7826static void
ed2dc618 7827build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7828{
ed2dc618 7829 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7830 return;
7831
ed2dc618 7832 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7833}
f4dc4d17 7834
6aa5f3a6
DE
7835/* Traversal function for process_skeletonless_type_unit.
7836 Read a TU in a DWO file and build partial symbols for it. */
7837
7838static int
7839process_skeletonless_type_unit (void **slot, void *info)
7840{
7841 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7842 struct dwarf2_per_objfile *dwarf2_per_objfile
7843 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7844 struct signatured_type find_entry, *entry;
7845
7846 /* If this TU doesn't exist in the global table, add it and read it in. */
7847
5989a64e
SM
7848 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7849 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7850
7851 find_entry.signature = dwo_unit->signature;
5989a64e 7852 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7853 &find_entry, INSERT);
6aa5f3a6
DE
7854 /* If we've already seen this type there's nothing to do. What's happening
7855 is we're doing our own version of comdat-folding here. */
7856 if (*slot != NULL)
7857 return 1;
7858
7859 /* This does the job that create_all_type_units would have done for
7860 this TU. */
ed2dc618
SM
7861 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7862 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7863 *slot = entry;
7864
7865 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7866 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7867 if (!reader.dummy_p)
7868 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7869 reader.comp_unit_die);
6aa5f3a6
DE
7870
7871 return 1;
7872}
7873
7874/* Traversal function for process_skeletonless_type_units. */
7875
7876static int
7877process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7878{
7879 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7880
7881 if (dwo_file->tus != NULL)
b0b6a987
TT
7882 htab_traverse_noresize (dwo_file->tus.get (),
7883 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7884
7885 return 1;
7886}
7887
7888/* Scan all TUs of DWO files, verifying we've processed them.
7889 This is needed in case a TU was emitted without its skeleton.
7890 Note: This can't be done until we know what all the DWO files are. */
7891
7892static void
ed2dc618 7893process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7894{
7895 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7896 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7897 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7898 {
5989a64e 7899 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7900 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7901 dwarf2_per_objfile);
6aa5f3a6 7902 }
348e048f
DE
7903}
7904
ed2dc618 7905/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7906
7907static void
ed2dc618 7908set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7909{
5989a64e 7910 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7911 {
891813be 7912 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7913
36586728
TT
7914 if (pst == NULL)
7915 continue;
7916
b76e467d 7917 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7918 {
7919 /* Set the 'user' field only if it is not already set. */
7920 if (pst->dependencies[j]->user == NULL)
7921 pst->dependencies[j]->user = pst;
7922 }
7923 }
7924}
7925
93311388
DE
7926/* Build the partial symbol table by doing a quick pass through the
7927 .debug_info and .debug_abbrev sections. */
72bf9492 7928
93311388 7929static void
ed2dc618 7930dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7931{
ed2dc618 7932 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7933
b4f54984 7934 if (dwarf_read_debug)
45cfd468
DE
7935 {
7936 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7937 objfile_name (objfile));
45cfd468
DE
7938 }
7939
76935768 7940 scoped_restore restore_reading_psyms
5989a64e 7941 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7942 true);
98bfdba5 7943
5989a64e 7944 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7945
93311388
DE
7946 /* Any cached compilation units will be linked by the per-objfile
7947 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7948 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7949
ed2dc618 7950 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7951
ed2dc618 7952 create_all_comp_units (dwarf2_per_objfile);
c906108c 7953
60606b2c
TT
7954 /* Create a temporary address map on a temporary obstack. We later
7955 copy this to the final obstack. */
8268c778 7956 auto_obstack temp_obstack;
791afaa2
TT
7957
7958 scoped_restore save_psymtabs_addrmap
d320c2b5 7959 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7960 addrmap_create_mutable (&temp_obstack));
72bf9492 7961
5989a64e 7962 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7963 {
7964 if (per_cu->v.psymtab != NULL)
7965 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7966 continue;
ab432490
SM
7967 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7968 language_minimal);
3d5afab3 7969 }
ff013f42 7970
6aa5f3a6 7971 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7972 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7973
7974 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7975 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7976 {
5989a64e 7977 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7978 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7979 }
7980
b4f54984 7981 if (dwarf_read_debug)
ed2dc618 7982 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7983
ed2dc618 7984 set_partial_user (dwarf2_per_objfile);
95554aad 7985
d320c2b5
TT
7986 objfile->partial_symtabs->psymtabs_addrmap
7987 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7988 objfile->partial_symtabs->obstack ());
791afaa2
TT
7989 /* At this point we want to keep the address map. */
7990 save_psymtabs_addrmap.release ();
ff013f42 7991
b4f54984 7992 if (dwarf_read_debug)
45cfd468 7993 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7994 objfile_name (objfile));
ae038cb0
DJ
7995}
7996
dee91e82
DE
7997/* Load the partial DIEs for a secondary CU into memory.
7998 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7999
dee91e82 8000static void
ab432490
SM
8001load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8002 dwarf2_per_objfile *per_objfile)
dee91e82 8003{
ab432490 8004 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
8005
8006 if (!reader.dummy_p)
8007 {
8008 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8009 language_minimal);
8010
8011 /* Check if comp unit has_children.
8012 If so, read the rest of the partial symbols from this comp unit.
8013 If not, there's no more debug_info for this comp unit. */
3e225074 8014 if (reader.comp_unit_die->has_children)
c0ab21c2 8015 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8016
8017 reader.keep ();
c0ab21c2 8018 }
ae038cb0
DJ
8019}
8020
ae038cb0 8021static void
ed2dc618 8022read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8023 struct dwarf2_section_info *section,
f1902523 8024 struct dwarf2_section_info *abbrev_section,
b76e467d 8025 unsigned int is_dwz)
ae038cb0 8026{
d521ce57 8027 const gdb_byte *info_ptr;
ed2dc618 8028 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8029
b4f54984 8030 if (dwarf_read_debug)
bf6af496 8031 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8032 section->get_name (),
8033 section->get_file_name ());
bf6af496 8034
96b79293 8035 section->read (objfile);
ae038cb0 8036
36586728 8037 info_ptr = section->buffer;
6e70227d 8038
36586728 8039 while (info_ptr < section->buffer + section->size)
ae038cb0 8040 {
ae038cb0 8041 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8042
9c541725 8043 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8044
f1902523 8045 comp_unit_head cu_header;
ed2dc618
SM
8046 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8047 abbrev_section, info_ptr,
8048 rcuh_kind::COMPILE);
ae038cb0
DJ
8049
8050 /* Save the compilation unit for later lookup. */
f1902523 8051 if (cu_header.unit_type != DW_UT_type)
5989a64e 8052 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8053 else
8054 {
5989a64e 8055 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8056 sig_type->signature = cu_header.signature;
8057 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8058 this_cu = &sig_type->per_cu;
8059 }
8060 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8061 this_cu->sect_off = sect_off;
f1902523 8062 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8063 this_cu->is_dwz = is_dwz;
e3b94546 8064 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8065 this_cu->section = section;
ae038cb0 8066
5989a64e 8067 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8068
8069 info_ptr = info_ptr + this_cu->length;
8070 }
36586728
TT
8071}
8072
8073/* Create a list of all compilation units in OBJFILE.
8074 This is only done for -readnow and building partial symtabs. */
8075
8076static void
ed2dc618 8077create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8078{
5989a64e
SM
8079 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8080 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8081 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8082
c3699833 8083 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8084 if (dwz != NULL)
ed2dc618 8085 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8086 1);
c906108c
SS
8087}
8088
5734ee8b 8089/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8090 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8091 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8092 DW_AT_ranges). See the comments of add_partial_subprogram on how
8093 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8094
72bf9492
DJ
8095static void
8096scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8097 CORE_ADDR *highpc, int set_addrmap,
8098 struct dwarf2_cu *cu)
c906108c 8099{
72bf9492 8100 struct partial_die_info *pdi;
c906108c 8101
91c24f0a
DC
8102 /* Now, march along the PDI's, descending into ones which have
8103 interesting children but skipping the children of the other ones,
8104 until we reach the end of the compilation unit. */
c906108c 8105
72bf9492 8106 pdi = first_die;
91c24f0a 8107
72bf9492
DJ
8108 while (pdi != NULL)
8109 {
52356b79 8110 pdi->fixup (cu);
c906108c 8111
f55ee35c 8112 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8113 children, so we need to look at them. Ditto for anonymous
8114 enums. */
933c6fe4 8115
72bf9492 8116 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8117 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8118 || pdi->tag == DW_TAG_imported_unit
8119 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8120 {
72bf9492 8121 switch (pdi->tag)
c906108c
SS
8122 {
8123 case DW_TAG_subprogram:
b1dc1806 8124 case DW_TAG_inlined_subroutine:
cdc07690 8125 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8126 break;
72929c62 8127 case DW_TAG_constant:
c906108c
SS
8128 case DW_TAG_variable:
8129 case DW_TAG_typedef:
91c24f0a 8130 case DW_TAG_union_type:
317d2668
TV
8131 if (!pdi->is_declaration
8132 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8133 {
72bf9492 8134 add_partial_symbol (pdi, cu);
63d06c5c
DC
8135 }
8136 break;
c906108c 8137 case DW_TAG_class_type:
680b30c7 8138 case DW_TAG_interface_type:
c906108c 8139 case DW_TAG_structure_type:
72bf9492 8140 if (!pdi->is_declaration)
c906108c 8141 {
72bf9492 8142 add_partial_symbol (pdi, cu);
c906108c 8143 }
b7fee5a3
KS
8144 if ((cu->language == language_rust
8145 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8146 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8147 set_addrmap, cu);
c906108c 8148 break;
91c24f0a 8149 case DW_TAG_enumeration_type:
72bf9492
DJ
8150 if (!pdi->is_declaration)
8151 add_partial_enumeration (pdi, cu);
c906108c
SS
8152 break;
8153 case DW_TAG_base_type:
a02abb62 8154 case DW_TAG_subrange_type:
c906108c 8155 /* File scope base type definitions are added to the partial
c5aa993b 8156 symbol table. */
72bf9492 8157 add_partial_symbol (pdi, cu);
c906108c 8158 break;
d9fa45fe 8159 case DW_TAG_namespace:
cdc07690 8160 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8161 break;
5d7cb8df 8162 case DW_TAG_module:
59c35742
AB
8163 if (!pdi->is_declaration)
8164 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8165 break;
95554aad
TT
8166 case DW_TAG_imported_unit:
8167 {
8168 struct dwarf2_per_cu_data *per_cu;
8169
f4dc4d17
DE
8170 /* For now we don't handle imported units in type units. */
8171 if (cu->per_cu->is_debug_types)
8172 {
8173 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8174 " supported in type units [in module %s]"),
5e22e966 8175 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8176 }
8177
e3b94546 8178 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8179 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8180
8181 /* Go read the partial unit, if needed. */
8182 if (per_cu->v.psymtab == NULL)
ab432490
SM
8183 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8184 cu->language);
95554aad 8185
ae640021 8186 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8187 }
8188 break;
74921315
KS
8189 case DW_TAG_imported_declaration:
8190 add_partial_symbol (pdi, cu);
8191 break;
c906108c
SS
8192 default:
8193 break;
8194 }
8195 }
8196
72bf9492
DJ
8197 /* If the die has a sibling, skip to the sibling. */
8198
8199 pdi = pdi->die_sibling;
8200 }
8201}
8202
8203/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8204
72bf9492 8205 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8206 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8207 Enumerators are an exception; they use the scope of their parent
8208 enumeration type, i.e. the name of the enumeration type is not
8209 prepended to the enumerator.
91c24f0a 8210
72bf9492
DJ
8211 There are two complexities. One is DW_AT_specification; in this
8212 case "parent" means the parent of the target of the specification,
8213 instead of the direct parent of the DIE. The other is compilers
8214 which do not emit DW_TAG_namespace; in this case we try to guess
8215 the fully qualified name of structure types from their members'
8216 linkage names. This must be done using the DIE's children rather
8217 than the children of any DW_AT_specification target. We only need
8218 to do this for structures at the top level, i.e. if the target of
8219 any DW_AT_specification (if any; otherwise the DIE itself) does not
8220 have a parent. */
8221
8222/* Compute the scope prefix associated with PDI's parent, in
8223 compilation unit CU. The result will be allocated on CU's
8224 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8225 field. NULL is returned if no prefix is necessary. */
15d034d0 8226static const char *
72bf9492
DJ
8227partial_die_parent_scope (struct partial_die_info *pdi,
8228 struct dwarf2_cu *cu)
8229{
15d034d0 8230 const char *grandparent_scope;
72bf9492 8231 struct partial_die_info *parent, *real_pdi;
91c24f0a 8232
72bf9492
DJ
8233 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8234 then this means the parent of the specification DIE. */
8235
8236 real_pdi = pdi;
72bf9492 8237 while (real_pdi->has_specification)
fb816e8b 8238 {
122cf0f2
AB
8239 auto res = find_partial_die (real_pdi->spec_offset,
8240 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8241 real_pdi = res.pdi;
8242 cu = res.cu;
8243 }
72bf9492
DJ
8244
8245 parent = real_pdi->die_parent;
8246 if (parent == NULL)
8247 return NULL;
8248
8249 if (parent->scope_set)
8250 return parent->scope;
8251
52356b79 8252 parent->fixup (cu);
72bf9492 8253
10b3939b 8254 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8255
acebe513
UW
8256 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8257 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8258 Work around this problem here. */
8259 if (cu->language == language_cplus
6e70227d 8260 && parent->tag == DW_TAG_namespace
acebe513
UW
8261 && strcmp (parent->name, "::") == 0
8262 && grandparent_scope == NULL)
8263 {
8264 parent->scope = NULL;
8265 parent->scope_set = 1;
8266 return NULL;
8267 }
8268
0a4b0913 8269 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8270 if (pdi->tag == DW_TAG_enumerator)
8271 /* Enumerators should not get the name of the enumeration as a prefix. */
8272 parent->scope = grandparent_scope;
8273 else if (parent->tag == DW_TAG_namespace
f55ee35c 8274 || parent->tag == DW_TAG_module
72bf9492
DJ
8275 || parent->tag == DW_TAG_structure_type
8276 || parent->tag == DW_TAG_class_type
680b30c7 8277 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8278 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8279 || parent->tag == DW_TAG_enumeration_type
8280 || (cu->language == language_fortran
8281 && parent->tag == DW_TAG_subprogram
8282 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8283 {
8284 if (grandparent_scope == NULL)
8285 parent->scope = parent->name;
8286 else
3e43a32a
MS
8287 parent->scope = typename_concat (&cu->comp_unit_obstack,
8288 grandparent_scope,
f55ee35c 8289 parent->name, 0, cu);
72bf9492 8290 }
72bf9492
DJ
8291 else
8292 {
8293 /* FIXME drow/2004-04-01: What should we be doing with
8294 function-local names? For partial symbols, we should probably be
8295 ignoring them. */
fa9c3fa0
TT
8296 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8297 dwarf_tag_name (parent->tag),
8298 sect_offset_str (pdi->sect_off));
72bf9492 8299 parent->scope = grandparent_scope;
c906108c
SS
8300 }
8301
72bf9492
DJ
8302 parent->scope_set = 1;
8303 return parent->scope;
8304}
8305
8306/* Return the fully scoped name associated with PDI, from compilation unit
8307 CU. The result will be allocated with malloc. */
4568ecf9 8308
43816ebc 8309static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8310partial_die_full_name (struct partial_die_info *pdi,
8311 struct dwarf2_cu *cu)
8312{
15d034d0 8313 const char *parent_scope;
72bf9492 8314
98bfdba5
PA
8315 /* If this is a template instantiation, we can not work out the
8316 template arguments from partial DIEs. So, unfortunately, we have
8317 to go through the full DIEs. At least any work we do building
8318 types here will be reused if full symbols are loaded later. */
8319 if (pdi->has_template_arguments)
8320 {
52356b79 8321 pdi->fixup (cu);
98bfdba5
PA
8322
8323 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8324 {
8325 struct die_info *die;
8326 struct attribute attr;
8327 struct dwarf2_cu *ref_cu = cu;
8328
b64f50a1 8329 /* DW_FORM_ref_addr is using section offset. */
b4069958 8330 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8331 attr.form = DW_FORM_ref_addr;
9c541725 8332 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8333 die = follow_die_ref (NULL, &attr, &ref_cu);
8334
43816ebc 8335 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8336 }
8337 }
8338
72bf9492
DJ
8339 parent_scope = partial_die_parent_scope (pdi, cu);
8340 if (parent_scope == NULL)
8341 return NULL;
8342 else
43816ebc
TT
8343 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8344 pdi->name, 0, cu));
c906108c
SS
8345}
8346
8347static void
72bf9492 8348add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8349{
5e22e966 8350 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8351 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8352 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8353 CORE_ADDR addr = 0;
15d034d0 8354 const char *actual_name = NULL;
e142c38c
DJ
8355 CORE_ADDR baseaddr;
8356
b3b3bada 8357 baseaddr = objfile->text_section_offset ();
c906108c 8358
43816ebc
TT
8359 gdb::unique_xmalloc_ptr<char> built_actual_name
8360 = partial_die_full_name (pdi, cu);
15d034d0 8361 if (built_actual_name != NULL)
43816ebc 8362 actual_name = built_actual_name.get ();
63d06c5c 8363
72bf9492
DJ
8364 if (actual_name == NULL)
8365 actual_name = pdi->name;
8366
76e288d1
TT
8367 partial_symbol psymbol;
8368 memset (&psymbol, 0, sizeof (psymbol));
8369 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8370 psymbol.ginfo.section = -1;
8371
8372 /* The code below indicates that the psymbol should be installed by
8373 setting this. */
8374 gdb::optional<psymbol_placement> where;
8375
c906108c
SS
8376 switch (pdi->tag)
8377 {
b1dc1806 8378 case DW_TAG_inlined_subroutine:
c906108c 8379 case DW_TAG_subprogram:
79748972
TT
8380 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8381 - baseaddr);
0a4b0913
AB
8382 if (pdi->is_external
8383 || cu->language == language_ada
8384 || (cu->language == language_fortran
8385 && pdi->die_parent != NULL
8386 && pdi->die_parent->tag == DW_TAG_subprogram))
8387 {
8388 /* Normally, only "external" DIEs are part of the global scope.
8389 But in Ada and Fortran, we want to be able to access nested
8390 procedures globally. So all Ada and Fortran subprograms are
8391 stored in the global scope. */
76e288d1 8392 where = psymbol_placement::GLOBAL;
c906108c
SS
8393 }
8394 else
76e288d1
TT
8395 where = psymbol_placement::STATIC;
8396
8397 psymbol.domain = VAR_DOMAIN;
8398 psymbol.aclass = LOC_BLOCK;
8399 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8400 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8401
8402 if (pdi->main_subprogram && actual_name != NULL)
8403 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8404 break;
72929c62 8405 case DW_TAG_constant:
76e288d1
TT
8406 psymbol.domain = VAR_DOMAIN;
8407 psymbol.aclass = LOC_STATIC;
8408 where = (pdi->is_external
8409 ? psymbol_placement::GLOBAL
8410 : psymbol_placement::STATIC);
72929c62 8411 break;
c906108c 8412 case DW_TAG_variable:
95554aad
TT
8413 if (pdi->d.locdesc)
8414 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8415
95554aad 8416 if (pdi->d.locdesc
caac4577 8417 && addr == 0
5989a64e 8418 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8419 {
8420 /* A global or static variable may also have been stripped
8421 out by the linker if unused, in which case its address
8422 will be nullified; do not add such variables into partial
8423 symbol table then. */
8424 }
8425 else if (pdi->is_external)
c906108c
SS
8426 {
8427 /* Global Variable.
8428 Don't enter into the minimal symbol tables as there is
8429 a minimal symbol table entry from the ELF symbols already.
8430 Enter into partial symbol table if it has a location
8431 descriptor or a type.
8432 If the location descriptor is missing, new_symbol will create
8433 a LOC_UNRESOLVED symbol, the address of the variable will then
8434 be determined from the minimal symbol table whenever the variable
8435 is referenced.
8436 The address for the partial symbol table entry is not
8437 used by GDB, but it comes in handy for debugging partial symbol
8438 table building. */
8439
95554aad 8440 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8441 {
8442 psymbol.domain = VAR_DOMAIN;
8443 psymbol.aclass = LOC_STATIC;
8444 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8445 psymbol.ginfo.value.address = addr;
8446 where = psymbol_placement::GLOBAL;
8447 }
c906108c
SS
8448 }
8449 else
8450 {
ff908ebf
AW
8451 int has_loc = pdi->d.locdesc != NULL;
8452
8453 /* Static Variable. Skip symbols whose value we cannot know (those
8454 without location descriptors or constant values). */
8455 if (!has_loc && !pdi->has_const_value)
43816ebc 8456 return;
ff908ebf 8457
76e288d1
TT
8458 psymbol.domain = VAR_DOMAIN;
8459 psymbol.aclass = LOC_STATIC;
8460 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8461 if (has_loc)
8462 psymbol.ginfo.value.address = addr;
8463 where = psymbol_placement::STATIC;
c906108c
SS
8464 }
8465 break;
8466 case DW_TAG_typedef:
8467 case DW_TAG_base_type:
a02abb62 8468 case DW_TAG_subrange_type:
76e288d1
TT
8469 psymbol.domain = VAR_DOMAIN;
8470 psymbol.aclass = LOC_TYPEDEF;
8471 where = psymbol_placement::STATIC;
c906108c 8472 break;
74921315 8473 case DW_TAG_imported_declaration:
72bf9492 8474 case DW_TAG_namespace:
76e288d1
TT
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_TYPEDEF;
8477 where = psymbol_placement::GLOBAL;
72bf9492 8478 break;
530e8392 8479 case DW_TAG_module:
a5fd13a9
BH
8480 /* With Fortran 77 there might be a "BLOCK DATA" module
8481 available without any name. If so, we skip the module as it
8482 doesn't bring any value. */
8483 if (actual_name != nullptr)
76e288d1
TT
8484 {
8485 psymbol.domain = MODULE_DOMAIN;
8486 psymbol.aclass = LOC_TYPEDEF;
8487 where = psymbol_placement::GLOBAL;
8488 }
530e8392 8489 break;
c906108c 8490 case DW_TAG_class_type:
680b30c7 8491 case DW_TAG_interface_type:
c906108c
SS
8492 case DW_TAG_structure_type:
8493 case DW_TAG_union_type:
8494 case DW_TAG_enumeration_type:
fa4028e9
JB
8495 /* Skip external references. The DWARF standard says in the section
8496 about "Structure, Union, and Class Type Entries": "An incomplete
8497 structure, union or class type is represented by a structure,
8498 union or class entry that does not have a byte size attribute
8499 and that has a DW_AT_declaration attribute." */
8500 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8501 return;
fa4028e9 8502
63d06c5c
DC
8503 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8504 static vs. global. */
76e288d1
TT
8505 psymbol.domain = STRUCT_DOMAIN;
8506 psymbol.aclass = LOC_TYPEDEF;
8507 where = (cu->language == language_cplus
8508 ? psymbol_placement::GLOBAL
8509 : psymbol_placement::STATIC);
c906108c
SS
8510 break;
8511 case DW_TAG_enumerator:
76e288d1
TT
8512 psymbol.domain = VAR_DOMAIN;
8513 psymbol.aclass = LOC_CONST;
8514 where = (cu->language == language_cplus
8515 ? psymbol_placement::GLOBAL
8516 : psymbol_placement::STATIC);
c906108c
SS
8517 break;
8518 default:
8519 break;
8520 }
76e288d1
TT
8521
8522 if (where.has_value ())
8523 {
f049a313
TT
8524 if (built_actual_name != nullptr)
8525 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8526 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8527 psymbol.ginfo.set_linkage_name (actual_name);
8528 else
8529 {
8530 psymbol.ginfo.set_demangled_name (actual_name,
8531 &objfile->objfile_obstack);
8532 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8533 }
76e288d1
TT
8534 add_psymbol_to_list (psymbol, *where, objfile);
8535 }
c906108c
SS
8536}
8537
5c4e30ca
DC
8538/* Read a partial die corresponding to a namespace; also, add a symbol
8539 corresponding to that namespace to the symbol table. NAMESPACE is
8540 the name of the enclosing namespace. */
91c24f0a 8541
72bf9492
DJ
8542static void
8543add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8544 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8545 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8546{
72bf9492 8547 /* Add a symbol for the namespace. */
e7c27a73 8548
72bf9492 8549 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8550
8551 /* Now scan partial symbols in that namespace. */
8552
91c24f0a 8553 if (pdi->has_children)
cdc07690 8554 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8555}
8556
5d7cb8df
JK
8557/* Read a partial die corresponding to a Fortran module. */
8558
8559static void
8560add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8561 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8562{
530e8392
KB
8563 /* Add a symbol for the namespace. */
8564
8565 add_partial_symbol (pdi, cu);
8566
f55ee35c 8567 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8568
8569 if (pdi->has_children)
cdc07690 8570 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8571}
8572
b1dc1806
XR
8573/* Read a partial die corresponding to a subprogram or an inlined
8574 subprogram and create a partial symbol for that subprogram.
8575 When the CU language allows it, this routine also defines a partial
8576 symbol for each nested subprogram that this subprogram contains.
8577 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8578 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8579
cdc07690
YQ
8580 PDI may also be a lexical block, in which case we simply search
8581 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8582 Again, this is only performed when the CU language allows this
8583 type of definitions. */
8584
8585static void
8586add_partial_subprogram (struct partial_die_info *pdi,
8587 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8588 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8589{
b1dc1806 8590 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8591 {
8592 if (pdi->has_pc_info)
8593 {
8594 if (pdi->lowpc < *lowpc)
8595 *lowpc = pdi->lowpc;
8596 if (pdi->highpc > *highpc)
8597 *highpc = pdi->highpc;
cdc07690 8598 if (set_addrmap)
5734ee8b 8599 {
5e22e966 8600 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8601 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8602 CORE_ADDR baseaddr;
b926417a
TT
8603 CORE_ADDR this_highpc;
8604 CORE_ADDR this_lowpc;
5734ee8b 8605
b3b3bada 8606 baseaddr = objfile->text_section_offset ();
b926417a
TT
8607 this_lowpc
8608 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8609 pdi->lowpc + baseaddr)
8610 - baseaddr);
8611 this_highpc
8612 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8613 pdi->highpc + baseaddr)
8614 - baseaddr);
d320c2b5 8615 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8616 this_lowpc, this_highpc - 1,
9291a0cd 8617 cu->per_cu->v.psymtab);
5734ee8b 8618 }
481860b3
GB
8619 }
8620
8621 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8622 {
bc30ff58 8623 if (!pdi->is_declaration)
e8d05480
JB
8624 /* Ignore subprogram DIEs that do not have a name, they are
8625 illegal. Do not emit a complaint at this point, we will
8626 do so when we convert this psymtab into a symtab. */
8627 if (pdi->name)
8628 add_partial_symbol (pdi, cu);
bc30ff58
JB
8629 }
8630 }
6e70227d 8631
bc30ff58
JB
8632 if (! pdi->has_children)
8633 return;
8634
0a4b0913 8635 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8636 {
8637 pdi = pdi->die_child;
8638 while (pdi != NULL)
8639 {
52356b79 8640 pdi->fixup (cu);
bc30ff58 8641 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8642 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8643 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8644 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8645 pdi = pdi->die_sibling;
8646 }
8647 }
8648}
8649
91c24f0a
DC
8650/* Read a partial die corresponding to an enumeration type. */
8651
72bf9492
DJ
8652static void
8653add_partial_enumeration (struct partial_die_info *enum_pdi,
8654 struct dwarf2_cu *cu)
91c24f0a 8655{
72bf9492 8656 struct partial_die_info *pdi;
91c24f0a
DC
8657
8658 if (enum_pdi->name != NULL)
72bf9492
DJ
8659 add_partial_symbol (enum_pdi, cu);
8660
8661 pdi = enum_pdi->die_child;
8662 while (pdi)
91c24f0a 8663 {
72bf9492 8664 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8665 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8666 else
72bf9492
DJ
8667 add_partial_symbol (pdi, cu);
8668 pdi = pdi->die_sibling;
91c24f0a 8669 }
91c24f0a
DC
8670}
8671
6caca83c
CC
8672/* Return the initial uleb128 in the die at INFO_PTR. */
8673
8674static unsigned int
d521ce57 8675peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8676{
8677 unsigned int bytes_read;
8678
8679 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8680}
8681
685af9cd
TT
8682/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8683 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8684
4bb7a0a7
DJ
8685 Return the corresponding abbrev, or NULL if the number is zero (indicating
8686 an empty DIE). In either case *BYTES_READ will be set to the length of
8687 the initial number. */
8688
8689static struct abbrev_info *
685af9cd
TT
8690peek_die_abbrev (const die_reader_specs &reader,
8691 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8692{
685af9cd 8693 dwarf2_cu *cu = reader.cu;
5e22e966 8694 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8695 unsigned int abbrev_number
8696 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8697
8698 if (abbrev_number == 0)
8699 return NULL;
8700
685af9cd 8701 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8702 if (!abbrev)
8703 {
422b9917 8704 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8705 " at offset %s [in module %s]"),
422b9917 8706 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8707 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8708 }
8709
8710 return abbrev;
8711}
8712
93311388
DE
8713/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8714 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8715 DIE. Any children of the skipped DIEs will also be skipped. */
8716
d521ce57
TT
8717static const gdb_byte *
8718skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8719{
4bb7a0a7
DJ
8720 while (1)
8721 {
685af9cd
TT
8722 unsigned int bytes_read;
8723 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8724
4bb7a0a7
DJ
8725 if (abbrev == NULL)
8726 return info_ptr + bytes_read;
8727 else
dee91e82 8728 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8729 }
8730}
8731
93311388
DE
8732/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8733 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8734 abbrev corresponding to that skipped uleb128 should be passed in
8735 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8736 children. */
8737
d521ce57
TT
8738static const gdb_byte *
8739skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8740 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8741{
8742 unsigned int bytes_read;
8743 struct attribute attr;
dee91e82
DE
8744 bfd *abfd = reader->abfd;
8745 struct dwarf2_cu *cu = reader->cu;
d521ce57 8746 const gdb_byte *buffer = reader->buffer;
f664829e 8747 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8748 unsigned int form, i;
8749
8750 for (i = 0; i < abbrev->num_attrs; i++)
8751 {
8752 /* The only abbrev we care about is DW_AT_sibling. */
8753 if (abbrev->attrs[i].name == DW_AT_sibling)
8754 {
18a8505e
AT
8755 bool ignored;
8756 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8757 &ignored);
4bb7a0a7 8758 if (attr.form == DW_FORM_ref_addr)
b98664d3 8759 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8760 else
b9502d3f 8761 {
0826b30a 8762 sect_offset off = attr.get_ref_die_offset ();
9c541725 8763 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8764
8765 if (sibling_ptr < info_ptr)
b98664d3 8766 complaint (_("DW_AT_sibling points backwards"));
22869d73 8767 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8768 reader->die_section->overflow_complaint ();
b9502d3f
WN
8769 else
8770 return sibling_ptr;
8771 }
4bb7a0a7
DJ
8772 }
8773
8774 /* If it isn't DW_AT_sibling, skip this attribute. */
8775 form = abbrev->attrs[i].form;
8776 skip_attribute:
8777 switch (form)
8778 {
4bb7a0a7 8779 case DW_FORM_ref_addr:
ae411497
TT
8780 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8781 and later it is offset sized. */
8782 if (cu->header.version == 2)
8783 info_ptr += cu->header.addr_size;
8784 else
8785 info_ptr += cu->header.offset_size;
8786 break;
36586728
TT
8787 case DW_FORM_GNU_ref_alt:
8788 info_ptr += cu->header.offset_size;
8789 break;
ae411497 8790 case DW_FORM_addr:
4bb7a0a7
DJ
8791 info_ptr += cu->header.addr_size;
8792 break;
8793 case DW_FORM_data1:
8794 case DW_FORM_ref1:
8795 case DW_FORM_flag:
8fe0f950 8796 case DW_FORM_strx1:
4bb7a0a7
DJ
8797 info_ptr += 1;
8798 break;
2dc7f7b3 8799 case DW_FORM_flag_present:
43988095 8800 case DW_FORM_implicit_const:
2dc7f7b3 8801 break;
4bb7a0a7
DJ
8802 case DW_FORM_data2:
8803 case DW_FORM_ref2:
8fe0f950 8804 case DW_FORM_strx2:
4bb7a0a7
DJ
8805 info_ptr += 2;
8806 break;
8fe0f950
AT
8807 case DW_FORM_strx3:
8808 info_ptr += 3;
8809 break;
4bb7a0a7
DJ
8810 case DW_FORM_data4:
8811 case DW_FORM_ref4:
8fe0f950 8812 case DW_FORM_strx4:
4bb7a0a7
DJ
8813 info_ptr += 4;
8814 break;
8815 case DW_FORM_data8:
8816 case DW_FORM_ref8:
55f1336d 8817 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8818 info_ptr += 8;
8819 break;
0224619f
JK
8820 case DW_FORM_data16:
8821 info_ptr += 16;
8822 break;
4bb7a0a7 8823 case DW_FORM_string:
9b1c24c8 8824 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8825 info_ptr += bytes_read;
8826 break;
2dc7f7b3 8827 case DW_FORM_sec_offset:
4bb7a0a7 8828 case DW_FORM_strp:
36586728 8829 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8830 info_ptr += cu->header.offset_size;
8831 break;
2dc7f7b3 8832 case DW_FORM_exprloc:
4bb7a0a7
DJ
8833 case DW_FORM_block:
8834 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8835 info_ptr += bytes_read;
8836 break;
8837 case DW_FORM_block1:
8838 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8839 break;
8840 case DW_FORM_block2:
8841 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8842 break;
8843 case DW_FORM_block4:
8844 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8845 break;
336d760d 8846 case DW_FORM_addrx:
cf532bd1 8847 case DW_FORM_strx:
4bb7a0a7
DJ
8848 case DW_FORM_sdata:
8849 case DW_FORM_udata:
8850 case DW_FORM_ref_udata:
3019eac3
DE
8851 case DW_FORM_GNU_addr_index:
8852 case DW_FORM_GNU_str_index:
18a8505e 8853 case DW_FORM_rnglistx:
41144253 8854 case DW_FORM_loclistx:
d521ce57 8855 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8856 break;
8857 case DW_FORM_indirect:
8858 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 /* We need to continue parsing from here, so just go back to
8861 the top. */
8862 goto skip_attribute;
8863
8864 default:
3e43a32a
MS
8865 error (_("Dwarf Error: Cannot handle %s "
8866 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8867 dwarf_form_name (form),
8868 bfd_get_filename (abfd));
8869 }
8870 }
8871
8872 if (abbrev->has_children)
dee91e82 8873 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8874 else
8875 return info_ptr;
8876}
8877
93311388 8878/* Locate ORIG_PDI's sibling.
dee91e82 8879 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8880
d521ce57 8881static const gdb_byte *
dee91e82
DE
8882locate_pdi_sibling (const struct die_reader_specs *reader,
8883 struct partial_die_info *orig_pdi,
d521ce57 8884 const gdb_byte *info_ptr)
91c24f0a
DC
8885{
8886 /* Do we know the sibling already? */
72bf9492 8887
91c24f0a
DC
8888 if (orig_pdi->sibling)
8889 return orig_pdi->sibling;
8890
8891 /* Are there any children to deal with? */
8892
8893 if (!orig_pdi->has_children)
8894 return info_ptr;
8895
4bb7a0a7 8896 /* Skip the children the long way. */
91c24f0a 8897
dee91e82 8898 return skip_children (reader, info_ptr);
91c24f0a
DC
8899}
8900
257e7a09 8901/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8902 not NULL. */
c906108c 8903
891813be
TT
8904void
8905dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8906{
ed2dc618
SM
8907 struct dwarf2_per_objfile *dwarf2_per_objfile
8908 = get_dwarf2_per_objfile (objfile);
8909
af758d11
SM
8910 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8911
077cbab2
TT
8912 /* If this psymtab is constructed from a debug-only objfile, the
8913 has_section_at_zero flag will not necessarily be correct. We
8914 can get the correct value for this flag by looking at the data
8915 associated with the (presumably stripped) associated objfile. */
8916 if (objfile->separate_debug_objfile_backlink)
c906108c 8917 {
077cbab2
TT
8918 struct dwarf2_per_objfile *dpo_backlink
8919 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8920
5989a64e
SM
8921 dwarf2_per_objfile->per_bfd->has_section_at_zero
8922 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8923 }
98bfdba5 8924
8566b89b 8925 expand_psymtab (objfile);
95554aad 8926
ed2dc618 8927 process_cu_includes (dwarf2_per_objfile);
c906108c 8928}
9cdd5dbd
DE
8929\f
8930/* Reading in full CUs. */
c906108c 8931
10b3939b
DJ
8932/* Add PER_CU to the queue. */
8933
8934static void
95554aad
TT
8935queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8936 enum language pretend_language)
10b3939b 8937{
10b3939b 8938 per_cu->queued = 1;
1859c670 8939 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8940}
8941
89e63ee4
DE
8942/* If PER_CU is not yet queued, add it to the queue.
8943 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8944 dependency.
0907af0c 8945 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8946 meaning either PER_CU is already queued or it is already loaded.
8947
8948 N.B. There is an invariant here that if a CU is queued then it is loaded.
8949 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8950
8951static int
89e63ee4 8952maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8953 struct dwarf2_per_cu_data *per_cu,
8954 enum language pretend_language)
8955{
8956 /* We may arrive here during partial symbol reading, if we need full
8957 DIEs to process an unusual case (e.g. template arguments). Do
8958 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8959 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8960 {
8961 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8962 return 1;
8963 return 0;
8964 }
8965
8966 /* Mark the dependence relation so that we don't flush PER_CU
8967 too early. */
89e63ee4
DE
8968 if (dependent_cu != NULL)
8969 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8970
8971 /* If it's already on the queue, we have nothing to do. */
8972 if (per_cu->queued)
8973 return 0;
8974
8975 /* If the compilation unit is already loaded, just mark it as
8976 used. */
8977 if (per_cu->cu != NULL)
8978 {
8979 per_cu->cu->last_used = 0;
8980 return 0;
8981 }
8982
8983 /* Add it to the queue. */
8984 queue_comp_unit (per_cu, pretend_language);
8985
8986 return 1;
8987}
8988
10b3939b
DJ
8989/* Process the queue. */
8990
8991static void
ed2dc618 8992process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8993{
b4f54984 8994 if (dwarf_read_debug)
45cfd468
DE
8995 {
8996 fprintf_unfiltered (gdb_stdlog,
8997 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8998 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8999 }
9000
03dd20cc
DJ
9001 /* The queue starts out with one item, but following a DIE reference
9002 may load a new CU, adding it to the end of the queue. */
5989a64e 9003 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 9004 {
5989a64e 9005 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 9006
af758d11 9007 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 9008 /* Skip dummy CUs. */
39856def 9009 && item.per_cu->cu != NULL)
f4dc4d17 9010 {
39856def 9011 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9012 unsigned int debug_print_threshold;
247f5c4f 9013 char buf[100];
f4dc4d17 9014
247f5c4f 9015 if (per_cu->is_debug_types)
f4dc4d17 9016 {
247f5c4f
DE
9017 struct signatured_type *sig_type =
9018 (struct signatured_type *) per_cu;
9019
9d8780f0 9020 sprintf (buf, "TU %s at offset %s",
73be47f5 9021 hex_string (sig_type->signature),
9d8780f0 9022 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9023 /* There can be 100s of TUs.
9024 Only print them in verbose mode. */
9025 debug_print_threshold = 2;
f4dc4d17 9026 }
247f5c4f 9027 else
73be47f5 9028 {
9d8780f0
SM
9029 sprintf (buf, "CU at offset %s",
9030 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9031 debug_print_threshold = 1;
9032 }
247f5c4f 9033
b4f54984 9034 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9035 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9036
9037 if (per_cu->is_debug_types)
39856def 9038 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 9039 else
39856def 9040 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 9041
b4f54984 9042 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9043 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9044 }
10b3939b 9045
39856def 9046 item.per_cu->queued = 0;
5989a64e 9047 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9048 }
9049
b4f54984 9050 if (dwarf_read_debug)
45cfd468
DE
9051 {
9052 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9053 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9054 }
10b3939b
DJ
9055}
9056
10b3939b
DJ
9057/* Read in full symbols for PST, and anything it depends on. */
9058
8566b89b
TT
9059void
9060dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9061{
af758d11 9062 gdb_assert (!readin_p (objfile));
95554aad 9063
48993951 9064 expand_dependencies (objfile);
aaa75496 9065
97a1449a
SM
9066 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9067 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9068 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9069}
9070
af758d11
SM
9071/* See psympriv.h. */
9072
9073bool
9074dwarf2_psymtab::readin_p (struct objfile *objfile) const
9075{
9076 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9077 return per_objfile->symtab_set_p (per_cu_data);
9078}
9079
9080/* See psympriv.h. */
9081
9082compunit_symtab *
9083dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9084{
9085 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9086 return per_objfile->get_symtab (per_cu_data);
9087}
9088
dee91e82
DE
9089/* Trivial hash function for die_info: the hash value of a DIE
9090 is its offset in .debug_info for this objfile. */
10b3939b 9091
dee91e82
DE
9092static hashval_t
9093die_hash (const void *item)
10b3939b 9094{
9a3c8263 9095 const struct die_info *die = (const struct die_info *) item;
6502dd73 9096
9c541725 9097 return to_underlying (die->sect_off);
dee91e82 9098}
63d06c5c 9099
dee91e82
DE
9100/* Trivial comparison function for die_info structures: two DIEs
9101 are equal if they have the same offset. */
98bfdba5 9102
dee91e82
DE
9103static int
9104die_eq (const void *item_lhs, const void *item_rhs)
9105{
9a3c8263
SM
9106 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9107 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9108
9c541725 9109 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9110}
c906108c 9111
c0ab21c2 9112/* Load the DIEs associated with PER_CU into memory. */
c906108c 9113
dee91e82 9114static void
ab432490
SM
9115load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9116 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9117 bool skip_partial,
9118 enum language pretend_language)
dee91e82 9119{
c0ab21c2
TT
9120 gdb_assert (! this_cu->is_debug_types);
9121
ab432490 9122 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9123 if (reader.dummy_p)
9124 return;
9125
9126 struct dwarf2_cu *cu = reader.cu;
9127 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9128
dee91e82
DE
9129 gdb_assert (cu->die_hash == NULL);
9130 cu->die_hash =
9131 htab_create_alloc_ex (cu->header.length / 12,
9132 die_hash,
9133 die_eq,
9134 NULL,
9135 &cu->comp_unit_obstack,
9136 hashtab_obstack_allocate,
9137 dummy_obstack_deallocate);
e142c38c 9138
3e225074 9139 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9140 reader.comp_unit_die->child
9141 = read_die_and_siblings (&reader, reader.info_ptr,
9142 &info_ptr, reader.comp_unit_die);
9143 cu->dies = reader.comp_unit_die;
dee91e82 9144 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9145
9146 /* We try not to read any attributes in this function, because not
9cdd5dbd 9147 all CUs needed for references have been loaded yet, and symbol
10b3939b 9148 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9149 or we won't be able to build types correctly.
9150 Similarly, if we do not read the producer, we can not apply
9151 producer-specific interpretation. */
c0ab21c2 9152 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9153
9154 reader.keep ();
10b3939b
DJ
9155}
9156
3da10d80
KS
9157/* Add a DIE to the delayed physname list. */
9158
9159static void
9160add_to_method_list (struct type *type, int fnfield_index, int index,
9161 const char *name, struct die_info *die,
9162 struct dwarf2_cu *cu)
9163{
9164 struct delayed_method_info mi;
9165 mi.type = type;
9166 mi.fnfield_index = fnfield_index;
9167 mi.index = index;
9168 mi.name = name;
9169 mi.die = die;
c89b44cd 9170 cu->method_list.push_back (mi);
3da10d80
KS
9171}
9172
3693fdb3
PA
9173/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9174 "const" / "volatile". If so, decrements LEN by the length of the
9175 modifier and return true. Otherwise return false. */
9176
9177template<size_t N>
9178static bool
9179check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9180{
9181 size_t mod_len = sizeof (mod) - 1;
9182 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9183 {
9184 len -= mod_len;
9185 return true;
9186 }
9187 return false;
9188}
9189
3da10d80
KS
9190/* Compute the physnames of any methods on the CU's method list.
9191
9192 The computation of method physnames is delayed in order to avoid the
9193 (bad) condition that one of the method's formal parameters is of an as yet
9194 incomplete type. */
9195
9196static void
9197compute_delayed_physnames (struct dwarf2_cu *cu)
9198{
3693fdb3 9199 /* Only C++ delays computing physnames. */
c89b44cd 9200 if (cu->method_list.empty ())
3693fdb3
PA
9201 return;
9202 gdb_assert (cu->language == language_cplus);
9203
52941706 9204 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9205 {
1d06ead6 9206 const char *physname;
3da10d80 9207 struct fn_fieldlist *fn_flp
c89b44cd
TT
9208 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9209 physname = dwarf2_physname (mi.name, mi.die, cu);
9210 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9211 = physname ? physname : "";
3693fdb3
PA
9212
9213 /* Since there's no tag to indicate whether a method is a
9214 const/volatile overload, extract that information out of the
9215 demangled name. */
9216 if (physname != NULL)
9217 {
9218 size_t len = strlen (physname);
9219
9220 while (1)
9221 {
9222 if (physname[len] == ')') /* shortcut */
9223 break;
9224 else if (check_modifier (physname, len, " const"))
c89b44cd 9225 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9226 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9227 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9228 else
9229 break;
9230 }
9231 }
3da10d80 9232 }
c89b44cd
TT
9233
9234 /* The list is no longer needed. */
9235 cu->method_list.clear ();
3da10d80
KS
9236}
9237
a766d390
DE
9238/* Go objects should be embedded in a DW_TAG_module DIE,
9239 and it's not clear if/how imported objects will appear.
9240 To keep Go support simple until that's worked out,
9241 go back through what we've read and create something usable.
9242 We could do this while processing each DIE, and feels kinda cleaner,
9243 but that way is more invasive.
9244 This is to, for example, allow the user to type "p var" or "b main"
9245 without having to specify the package name, and allow lookups
9246 of module.object to work in contexts that use the expression
9247 parser. */
9248
9249static void
9250fixup_go_packaging (struct dwarf2_cu *cu)
9251{
421d1616 9252 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9253 struct pending *list;
9254 int i;
9255
c24bdb02 9256 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9257 list != NULL;
9258 list = list->next)
a766d390
DE
9259 {
9260 for (i = 0; i < list->nsyms; ++i)
9261 {
9262 struct symbol *sym = list->symbol[i];
9263
c1b5c1eb 9264 if (sym->language () == language_go
a766d390
DE
9265 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9266 {
421d1616
TT
9267 gdb::unique_xmalloc_ptr<char> this_package_name
9268 (go_symbol_package_name (sym));
a766d390
DE
9269
9270 if (this_package_name == NULL)
9271 continue;
9272 if (package_name == NULL)
421d1616 9273 package_name = std::move (this_package_name);
a766d390
DE
9274 else
9275 {
5e22e966 9276 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9277 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9278 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9279 (symbol_symtab (sym) != NULL
9280 ? symtab_to_filename_for_display
9281 (symbol_symtab (sym))
e3b94546 9282 : objfile_name (objfile)),
421d1616 9283 this_package_name.get (), package_name.get ());
a766d390
DE
9284 }
9285 }
9286 }
9287 }
9288
9289 if (package_name != NULL)
9290 {
5e22e966 9291 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9292 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9293 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9294 saved_package_name);
a766d390
DE
9295 struct symbol *sym;
9296
8c14c3a3 9297 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9298 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9299 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9300 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9301 e.g., "main" finds the "main" module and not C's main(). */
9302 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9303 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9304 SYMBOL_TYPE (sym) = type;
9305
c24bdb02 9306 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9307 }
9308}
9309
c9317f21
TT
9310/* Allocate a fully-qualified name consisting of the two parts on the
9311 obstack. */
9312
9313static const char *
9314rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9315{
9316 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9317}
9318
9c6a1327
TT
9319/* A helper that allocates a variant part to attach to a Rust enum
9320 type. OBSTACK is where the results should be allocated. TYPE is
9321 the type we're processing. DISCRIMINANT_INDEX is the index of the
9322 discriminant. It must be the index of one of the fields of TYPE.
9323 DEFAULT_INDEX is the index of the default field; or -1 if there is
9324 no default. RANGES is indexed by "effective" field number (the
9325 field index, but omitting the discriminant and default fields) and
9326 must hold the discriminant values used by the variants. Note that
9327 RANGES must have a lifetime at least as long as OBSTACK -- either
9328 already allocated on it, or static. */
c9317f21 9329
9c6a1327
TT
9330static void
9331alloc_rust_variant (struct obstack *obstack, struct type *type,
9332 int discriminant_index, int default_index,
9333 gdb::array_view<discriminant_range> ranges)
9334{
9335 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9336 must be handled by the caller. */
9337 gdb_assert (discriminant_index >= 0
1f704f76 9338 && discriminant_index < type->num_fields ());
c9317f21 9339 gdb_assert (default_index == -1
1f704f76 9340 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9341
9c6a1327 9342 /* We have one variant for each non-discriminant field. */
1f704f76 9343 int n_variants = type->num_fields () - 1;
c9317f21 9344
9c6a1327
TT
9345 variant *variants = new (obstack) variant[n_variants];
9346 int var_idx = 0;
9347 int range_idx = 0;
1f704f76 9348 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9349 {
9350 if (i == discriminant_index)
9351 continue;
c9317f21 9352
9c6a1327
TT
9353 variants[var_idx].first_field = i;
9354 variants[var_idx].last_field = i + 1;
9355
9356 /* The default field does not need a range, but other fields do.
9357 We skipped the discriminant above. */
9358 if (i != default_index)
9359 {
9360 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9361 ++range_idx;
9362 }
c9317f21 9363
9c6a1327
TT
9364 ++var_idx;
9365 }
9366
9367 gdb_assert (range_idx == ranges.size ());
9368 gdb_assert (var_idx == n_variants);
9369
9370 variant_part *part = new (obstack) variant_part;
9371 part->discriminant_index = discriminant_index;
9372 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9373 discriminant_index));
9374 part->variants = gdb::array_view<variant> (variants, n_variants);
9375
9376 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9377 gdb::array_view<variant_part> *prop_value
9378 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9379
9c6a1327
TT
9380 struct dynamic_prop prop;
9381 prop.kind = PROP_VARIANT_PARTS;
9382 prop.data.variant_parts = prop_value;
9383
5c54719c 9384 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9385}
9386
9387/* Some versions of rustc emitted enums in an unusual way.
9388
9389 Ordinary enums were emitted as unions. The first element of each
9390 structure in the union was named "RUST$ENUM$DISR". This element
9391 held the discriminant.
9392
9393 These versions of Rust also implemented the "non-zero"
9394 optimization. When the enum had two values, and one is empty and
9395 the other holds a pointer that cannot be zero, the pointer is used
9396 as the discriminant, with a zero value meaning the empty variant.
9397 Here, the union's first member is of the form
9398 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9399 where the fieldnos are the indices of the fields that should be
9400 traversed in order to find the field (which may be several fields deep)
9401 and the variantname is the name of the variant of the case when the
9402 field is zero.
9403
9404 This function recognizes whether TYPE is of one of these forms,
9405 and, if so, smashes it to be a variant type. */
9406
9407static void
9408quirk_rust_enum (struct type *type, struct objfile *objfile)
9409{
78134374 9410 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9411
9412 /* We don't need to deal with empty enums. */
1f704f76 9413 if (type->num_fields () == 0)
c9317f21
TT
9414 return;
9415
9416#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9417 if (type->num_fields () == 1
c9317f21
TT
9418 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9419 {
9420 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9421
9422 /* Decode the field name to find the offset of the
9423 discriminant. */
9424 ULONGEST bit_offset = 0;
9425 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9426 while (name[0] >= '0' && name[0] <= '9')
9427 {
9428 char *tail;
9429 unsigned long index = strtoul (name, &tail, 10);
9430 name = tail;
9431 if (*name != '$'
1f704f76 9432 || index >= field_type->num_fields ()
c9317f21
TT
9433 || (TYPE_FIELD_LOC_KIND (field_type, index)
9434 != FIELD_LOC_KIND_BITPOS))
9435 {
b98664d3 9436 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9437 "[in module %s]"),
9438 TYPE_FIELD_NAME (type, 0),
9439 objfile_name (objfile));
9440 return;
9441 }
9442 ++name;
9443
9444 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9445 field_type = TYPE_FIELD_TYPE (field_type, index);
9446 }
9447
9c6a1327
TT
9448 /* Smash this type to be a structure type. We have to do this
9449 because the type has already been recorded. */
67607e24 9450 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9451 type->set_num_fields (3);
9c6a1327 9452 /* Save the field we care about. */
ceacbf6e 9453 struct field saved_field = type->field (0);
3cabb6b0
SM
9454 type->set_fields
9455 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9456
9c6a1327
TT
9457 /* Put the discriminant at index 0. */
9458 TYPE_FIELD_TYPE (type, 0) = field_type;
9459 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9460 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9461 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9462
9463 /* The order of fields doesn't really matter, so put the real
9464 field at index 1 and the data-less field at index 2. */
ceacbf6e 9465 type->field (1) = saved_field;
9c6a1327 9466 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9467 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9468 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9469 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9470 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9471
9472 const char *dataless_name
7d93a1e0 9473 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9474 name);
9475 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9476 dataless_name);
9c6a1327 9477 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9478 /* NAME points into the original discriminant name, which
9479 already has the correct lifetime. */
9c6a1327 9480 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9481 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9482
9c6a1327
TT
9483 /* Indicate that this is a variant type. */
9484 static discriminant_range ranges[1] = { { 0, 0 } };
9485 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9486 }
77c2dba3
TT
9487 /* A union with a single anonymous field is probably an old-style
9488 univariant enum. */
1f704f76 9489 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9490 {
c9317f21
TT
9491 /* Smash this type to be a structure type. We have to do this
9492 because the type has already been recorded. */
67607e24 9493 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9494
9c6a1327 9495 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9496 const char *variant_name
7d93a1e0 9497 = rust_last_path_segment (field_type->name ());
9c6a1327 9498 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9499 field_type->set_name
9500 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9501 type->name (), variant_name));
c9317f21
TT
9502 }
9503 else
9504 {
9505 struct type *disr_type = nullptr;
1f704f76 9506 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9507 {
9508 disr_type = TYPE_FIELD_TYPE (type, i);
9509
78134374 9510 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9511 {
9512 /* All fields of a true enum will be structs. */
9513 return;
9514 }
1f704f76 9515 else if (disr_type->num_fields () == 0)
c9317f21
TT
9516 {
9517 /* Could be data-less variant, so keep going. */
a037790e 9518 disr_type = nullptr;
c9317f21
TT
9519 }
9520 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9521 "RUST$ENUM$DISR") != 0)
9522 {
9523 /* Not a Rust enum. */
9524 return;
9525 }
9526 else
9527 {
9528 /* Found one. */
9529 break;
9530 }
9531 }
9532
9533 /* If we got here without a discriminant, then it's probably
9534 just a union. */
9535 if (disr_type == nullptr)
9536 return;
9537
9538 /* Smash this type to be a structure type. We have to do this
9539 because the type has already been recorded. */
67607e24 9540 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9541
9c6a1327 9542 /* Make space for the discriminant field. */
ceacbf6e 9543 struct field *disr_field = &disr_type->field (0);
9c6a1327 9544 field *new_fields
1f704f76 9545 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9546 * sizeof (struct field)));
80fc5e77 9547 memcpy (new_fields + 1, type->fields (),
1f704f76 9548 type->num_fields () * sizeof (struct field));
3cabb6b0 9549 type->set_fields (new_fields);
1f704f76 9550 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9551
9552 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9553 type->field (0) = *disr_field;
9c6a1327
TT
9554 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9555 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9556
9557 /* We need a way to find the correct discriminant given a
9558 variant name. For convenience we build a map here. */
9559 struct type *enum_type = FIELD_TYPE (*disr_field);
9560 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9561 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9562 {
9563 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9564 {
9565 const char *name
9566 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9567 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9568 }
9569 }
9570
1f704f76 9571 int n_fields = type->num_fields ();
9c6a1327
TT
9572 /* We don't need a range entry for the discriminant, but we do
9573 need one for every other field, as there is no default
9574 variant. */
9575 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9576 discriminant_range,
9577 n_fields - 1);
c9317f21
TT
9578 /* Skip the discriminant here. */
9579 for (int i = 1; i < n_fields; ++i)
9580 {
9581 /* Find the final word in the name of this variant's type.
9582 That name can be used to look up the correct
9583 discriminant. */
9584 const char *variant_name
7d93a1e0 9585 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9586
9587 auto iter = discriminant_map.find (variant_name);
9588 if (iter != discriminant_map.end ())
9c6a1327
TT
9589 {
9590 ranges[i].low = iter->second;
9591 ranges[i].high = iter->second;
9592 }
c9317f21 9593
bedda9ac 9594 /* Remove the discriminant field, if it exists. */
9c6a1327 9595 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9596 if (sub_type->num_fields () > 0)
bedda9ac 9597 {
5e33d5f4 9598 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9599 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9600 }
9c6a1327 9601 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9602 sub_type->set_name
9603 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9604 type->name (), variant_name));
c9317f21 9605 }
9c6a1327
TT
9606
9607 /* Indicate that this is a variant type. */
9608 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9609 gdb::array_view<discriminant_range> (ranges,
9610 n_fields - 1));
c9317f21
TT
9611 }
9612}
9613
9614/* Rewrite some Rust unions to be structures with variants parts. */
9615
9616static void
9617rust_union_quirks (struct dwarf2_cu *cu)
9618{
9619 gdb_assert (cu->language == language_rust);
52941706 9620 for (type *type_ : cu->rust_unions)
5e22e966 9621 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9622 /* We don't need this any more. */
9623 cu->rust_unions.clear ();
c9317f21
TT
9624}
9625
95554aad
TT
9626/* A helper function for computing the list of all symbol tables
9627 included by PER_CU. */
9628
9629static void
4c39bc03 9630recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9631 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9632 dwarf2_per_cu_data *per_cu,
9633 dwarf2_per_objfile *per_objfile,
43f3e411 9634 struct compunit_symtab *immediate_parent)
95554aad 9635{
af758d11 9636 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9637 if (*slot != NULL)
9638 {
9639 /* This inclusion and its children have been processed. */
9640 return;
9641 }
9642
9643 *slot = per_cu;
af758d11 9644
95554aad 9645 /* Only add a CU if it has a symbol table. */
43182c09 9646 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9647 if (cust != NULL)
ec94af83
DE
9648 {
9649 /* If this is a type unit only add its symbol table if we haven't
9650 seen it yet (type unit per_cu's can share symtabs). */
9651 if (per_cu->is_debug_types)
9652 {
43f3e411 9653 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9654 if (*slot == NULL)
9655 {
43f3e411 9656 *slot = cust;
4c39bc03 9657 result->push_back (cust);
43f3e411
DE
9658 if (cust->user == NULL)
9659 cust->user = immediate_parent;
ec94af83
DE
9660 }
9661 }
9662 else
f9125b6c 9663 {
4c39bc03 9664 result->push_back (cust);
43f3e411
DE
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
f9125b6c 9667 }
ec94af83 9668 }
95554aad 9669
ae640021
AB
9670 if (!per_cu->imported_symtabs_empty ())
9671 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9672 {
9673 recursively_compute_inclusions (result, all_children,
43182c09
SM
9674 all_type_symtabs, ptr, per_objfile,
9675 cust);
ae640021 9676 }
95554aad
TT
9677}
9678
43f3e411 9679/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9680 PER_CU. */
9681
9682static void
43182c09
SM
9683compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9684 dwarf2_per_objfile *per_objfile)
95554aad 9685{
f4dc4d17
DE
9686 gdb_assert (! per_cu->is_debug_types);
9687
ae640021 9688 if (!per_cu->imported_symtabs_empty ())
95554aad 9689 {
ae640021 9690 int len;
4c39bc03 9691 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9692 htab_t all_children, all_type_symtabs;
43182c09 9693 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9694
9695 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9696 if (cust == NULL)
95554aad
TT
9697 return;
9698
9699 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9700 NULL, xcalloc, xfree);
ec94af83
DE
9701 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9702 NULL, xcalloc, xfree);
95554aad 9703
ae640021 9704 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9705 {
9706 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9707 all_type_symtabs, ptr, per_objfile,
9708 cust);
ec94af83 9709 }
95554aad 9710
ec94af83 9711 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9712 len = result_symtabs.size ();
43f3e411 9713 cust->includes
ed2dc618 9714 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9715 struct compunit_symtab *, len + 1);
4c39bc03
TT
9716 memcpy (cust->includes, result_symtabs.data (),
9717 len * sizeof (compunit_symtab *));
43f3e411 9718 cust->includes[len] = NULL;
95554aad 9719
95554aad 9720 htab_delete (all_children);
ec94af83 9721 htab_delete (all_type_symtabs);
95554aad
TT
9722 }
9723}
9724
9725/* Compute the 'includes' field for the symtabs of all the CUs we just
9726 read. */
9727
9728static void
ed2dc618 9729process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9730{
5989a64e 9731 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9732 {
9733 if (! iter->is_debug_types)
43182c09 9734 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9735 }
95554aad 9736
5989a64e 9737 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9738}
9739
9cdd5dbd 9740/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9741 already been loaded into memory. */
9742
9743static void
95554aad
TT
9744process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9745 enum language pretend_language)
10b3939b 9746{
10b3939b 9747 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9748 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9749 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9750 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9751 CORE_ADDR lowpc, highpc;
43f3e411 9752 struct compunit_symtab *cust;
10b3939b 9753 CORE_ADDR baseaddr;
4359dff1 9754 struct block *static_block;
3e29f34a 9755 CORE_ADDR addr;
10b3939b 9756
b3b3bada 9757 baseaddr = objfile->text_section_offset ();
10b3939b 9758
c89b44cd
TT
9759 /* Clear the list here in case something was left over. */
9760 cu->method_list.clear ();
10b3939b 9761
95554aad
TT
9762 cu->language = pretend_language;
9763 cu->language_defn = language_def (cu->language);
9764
c906108c 9765 /* Do line number decoding in read_file_scope () */
10b3939b 9766 process_die (cu->dies, cu);
c906108c 9767
a766d390
DE
9768 /* For now fudge the Go package. */
9769 if (cu->language == language_go)
9770 fixup_go_packaging (cu);
9771
5f48f8f3 9772 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9773 should be complete, and it should now be safe to compute all of the
9774 physnames. */
9775 compute_delayed_physnames (cu);
3da10d80 9776
c9317f21
TT
9777 if (cu->language == language_rust)
9778 rust_union_quirks (cu);
9779
fae299cd
DC
9780 /* Some compilers don't define a DW_AT_high_pc attribute for the
9781 compilation unit. If the DW_AT_high_pc is missing, synthesize
9782 it, by scanning the DIE's below the compilation unit. */
10b3939b 9783 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9784
3e29f34a 9785 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9786 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9787
9788 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9789 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9790 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9791 addrmap to help ensure it has an accurate map of pc values belonging to
9792 this comp unit. */
9793 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9794
c24bdb02 9795 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9796 SECT_OFF_TEXT (objfile),
9797 0);
c906108c 9798
43f3e411 9799 if (cust != NULL)
c906108c 9800 {
df15bd07 9801 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9802
8be455d7
JK
9803 /* Set symtab language to language from DW_AT_language. If the
9804 compilation is from a C file generated by language preprocessors, do
9805 not set the language if it was already deduced by start_subfile. */
43f3e411 9806 if (!(cu->language == language_c
40e3ad0e 9807 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9808 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9809
9810 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9811 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9812 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9813 there were bugs in prologue debug info, fixed later in GCC-4.5
9814 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9815
9816 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9817 needed, it would be wrong due to missing DW_AT_producer there.
9818
9819 Still one can confuse GDB by using non-standard GCC compilation
9820 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9821 */
ab260dad 9822 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9823 cust->locations_valid = 1;
e0d00bc7
JK
9824
9825 if (gcc_4_minor >= 5)
43f3e411 9826 cust->epilogue_unwind_valid = 1;
96408a79 9827
43f3e411 9828 cust->call_site_htab = cu->call_site_htab;
c906108c 9829 }
9291a0cd 9830
af758d11 9831 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9832
95554aad 9833 /* Push it for inclusion processing later. */
5989a64e 9834 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9835
9836 /* Not needed any more. */
c24bdb02 9837 cu->reset_builder ();
f4dc4d17 9838}
45cfd468 9839
f4dc4d17
DE
9840/* Generate full symbol information for type unit PER_CU, whose DIEs have
9841 already been loaded into memory. */
9842
9843static void
9844process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9845 enum language pretend_language)
9846{
9847 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9848 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9849 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9850 struct compunit_symtab *cust;
0186c6a7
DE
9851 struct signatured_type *sig_type;
9852
9853 gdb_assert (per_cu->is_debug_types);
9854 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9855
c89b44cd
TT
9856 /* Clear the list here in case something was left over. */
9857 cu->method_list.clear ();
f4dc4d17 9858
f4dc4d17
DE
9859 cu->language = pretend_language;
9860 cu->language_defn = language_def (cu->language);
9861
9862 /* The symbol tables are set up in read_type_unit_scope. */
9863 process_die (cu->dies, cu);
9864
9865 /* For now fudge the Go package. */
9866 if (cu->language == language_go)
9867 fixup_go_packaging (cu);
9868
5f48f8f3 9869 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9870 should be complete, and it should now be safe to compute all of the
9871 physnames. */
9872 compute_delayed_physnames (cu);
f4dc4d17 9873
c9317f21
TT
9874 if (cu->language == language_rust)
9875 rust_union_quirks (cu);
9876
f4dc4d17
DE
9877 /* TUs share symbol tables.
9878 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9879 of it with end_expandable_symtab. Otherwise, complete the addition of
9880 this TU's symbols to the existing symtab. */
43f3e411 9881 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9882 {
c24bdb02
KS
9883 buildsym_compunit *builder = cu->get_builder ();
9884 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9885 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9886
43f3e411 9887 if (cust != NULL)
f4dc4d17
DE
9888 {
9889 /* Set symtab language to language from DW_AT_language. If the
9890 compilation is from a C file generated by language preprocessors,
9891 do not set the language if it was already deduced by
9892 start_subfile. */
43f3e411
DE
9893 if (!(cu->language == language_c
9894 && COMPUNIT_FILETABS (cust)->language != language_c))
9895 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9896 }
9897 }
9898 else
9899 {
c24bdb02 9900 cu->get_builder ()->augment_type_symtab ();
43f3e411 9901 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9902 }
9903
af758d11 9904 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9905
9906 /* Not needed any more. */
c24bdb02 9907 cu->reset_builder ();
c906108c
SS
9908}
9909
95554aad
TT
9910/* Process an imported unit DIE. */
9911
9912static void
9913process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9914{
9915 struct attribute *attr;
9916
f4dc4d17
DE
9917 /* For now we don't handle imported units in type units. */
9918 if (cu->per_cu->is_debug_types)
9919 {
9920 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9921 " supported in type units [in module %s]"),
5e22e966 9922 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9923 }
9924
95554aad
TT
9925 attr = dwarf2_attr (die, DW_AT_import, cu);
9926 if (attr != NULL)
9927 {
0826b30a 9928 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9929 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9930 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9931 dwarf2_per_cu_data *per_cu
ab432490 9932 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9933
58990295
TV
9934 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9935 into another compilation unit, at root level. Regard this as a hint,
9936 and ignore it. */
9937 if (die->parent && die->parent->parent == NULL
9938 && per_cu->unit_type == DW_UT_compile
9939 && per_cu->lang == language_cplus)
9940 return;
9941
69d751e3 9942 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9943 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 9944 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9945
ae640021 9946 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9947 }
9948}
9949
4c8aa72d
PA
9950/* RAII object that represents a process_die scope: i.e.,
9951 starts/finishes processing a DIE. */
9952class process_die_scope
adde2bff 9953{
4c8aa72d
PA
9954public:
9955 process_die_scope (die_info *die, dwarf2_cu *cu)
9956 : m_die (die), m_cu (cu)
9957 {
9958 /* We should only be processing DIEs not already in process. */
9959 gdb_assert (!m_die->in_process);
9960 m_die->in_process = true;
9961 }
8c3cb9fa 9962
4c8aa72d
PA
9963 ~process_die_scope ()
9964 {
9965 m_die->in_process = false;
9966
9967 /* If we're done processing the DIE for the CU that owns the line
9968 header, we don't need the line header anymore. */
9969 if (m_cu->line_header_die_owner == m_die)
9970 {
9971 delete m_cu->line_header;
9972 m_cu->line_header = NULL;
9973 m_cu->line_header_die_owner = NULL;
9974 }
9975 }
9976
9977private:
9978 die_info *m_die;
9979 dwarf2_cu *m_cu;
9980};
adde2bff 9981
c906108c
SS
9982/* Process a die and its children. */
9983
9984static void
e7c27a73 9985process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9986{
4c8aa72d 9987 process_die_scope scope (die, cu);
adde2bff 9988
c906108c
SS
9989 switch (die->tag)
9990 {
9991 case DW_TAG_padding:
9992 break;
9993 case DW_TAG_compile_unit:
95554aad 9994 case DW_TAG_partial_unit:
e7c27a73 9995 read_file_scope (die, cu);
c906108c 9996 break;
348e048f
DE
9997 case DW_TAG_type_unit:
9998 read_type_unit_scope (die, cu);
9999 break;
c906108c 10000 case DW_TAG_subprogram:
0a4b0913
AB
10001 /* Nested subprograms in Fortran get a prefix. */
10002 if (cu->language == language_fortran
10003 && die->parent != NULL
10004 && die->parent->tag == DW_TAG_subprogram)
10005 cu->processing_has_namespace_info = true;
10006 /* Fall through. */
c906108c 10007 case DW_TAG_inlined_subroutine:
edb3359d 10008 read_func_scope (die, cu);
c906108c
SS
10009 break;
10010 case DW_TAG_lexical_block:
14898363
L
10011 case DW_TAG_try_block:
10012 case DW_TAG_catch_block:
e7c27a73 10013 read_lexical_block_scope (die, cu);
c906108c 10014 break;
216f72a1 10015 case DW_TAG_call_site:
96408a79
SA
10016 case DW_TAG_GNU_call_site:
10017 read_call_site_scope (die, cu);
10018 break;
c906108c 10019 case DW_TAG_class_type:
680b30c7 10020 case DW_TAG_interface_type:
c906108c
SS
10021 case DW_TAG_structure_type:
10022 case DW_TAG_union_type:
134d01f1 10023 process_structure_scope (die, cu);
c906108c
SS
10024 break;
10025 case DW_TAG_enumeration_type:
134d01f1 10026 process_enumeration_scope (die, cu);
c906108c 10027 break;
134d01f1 10028
f792889a
DJ
10029 /* These dies have a type, but processing them does not create
10030 a symbol or recurse to process the children. Therefore we can
10031 read them on-demand through read_type_die. */
c906108c 10032 case DW_TAG_subroutine_type:
72019c9c 10033 case DW_TAG_set_type:
c906108c 10034 case DW_TAG_array_type:
c906108c 10035 case DW_TAG_pointer_type:
c906108c 10036 case DW_TAG_ptr_to_member_type:
c906108c 10037 case DW_TAG_reference_type:
4297a3f0 10038 case DW_TAG_rvalue_reference_type:
c906108c 10039 case DW_TAG_string_type:
c906108c 10040 break;
134d01f1 10041
c906108c 10042 case DW_TAG_base_type:
a02abb62 10043 case DW_TAG_subrange_type:
cb249c71 10044 case DW_TAG_typedef:
134d01f1
DJ
10045 /* Add a typedef symbol for the type definition, if it has a
10046 DW_AT_name. */
f792889a 10047 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10048 break;
c906108c 10049 case DW_TAG_common_block:
e7c27a73 10050 read_common_block (die, cu);
c906108c
SS
10051 break;
10052 case DW_TAG_common_inclusion:
10053 break;
d9fa45fe 10054 case DW_TAG_namespace:
9068261f 10055 cu->processing_has_namespace_info = true;
e7c27a73 10056 read_namespace (die, cu);
d9fa45fe 10057 break;
5d7cb8df 10058 case DW_TAG_module:
9068261f 10059 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10060 read_module (die, cu);
10061 break;
d9fa45fe 10062 case DW_TAG_imported_declaration:
9068261f 10063 cu->processing_has_namespace_info = true;
74921315
KS
10064 if (read_namespace_alias (die, cu))
10065 break;
86a73007
TT
10066 /* The declaration is not a global namespace alias. */
10067 /* Fall through. */
d9fa45fe 10068 case DW_TAG_imported_module:
9068261f 10069 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10070 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10071 || cu->language != language_fortran))
b98664d3 10072 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10073 dwarf_tag_name (die->tag));
10074 read_import_statement (die, cu);
d9fa45fe 10075 break;
95554aad
TT
10076
10077 case DW_TAG_imported_unit:
10078 process_imported_unit_die (die, cu);
10079 break;
10080
71a3c369
TT
10081 case DW_TAG_variable:
10082 read_variable (die, cu);
10083 break;
10084
c906108c 10085 default:
e7c27a73 10086 new_symbol (die, NULL, cu);
c906108c
SS
10087 break;
10088 }
10089}
ca69b9e6
DE
10090\f
10091/* DWARF name computation. */
c906108c 10092
94af9270
KS
10093/* A helper function for dwarf2_compute_name which determines whether DIE
10094 needs to have the name of the scope prepended to the name listed in the
10095 die. */
10096
10097static int
10098die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10099{
1c809c68
TT
10100 struct attribute *attr;
10101
94af9270
KS
10102 switch (die->tag)
10103 {
10104 case DW_TAG_namespace:
10105 case DW_TAG_typedef:
10106 case DW_TAG_class_type:
10107 case DW_TAG_interface_type:
10108 case DW_TAG_structure_type:
10109 case DW_TAG_union_type:
10110 case DW_TAG_enumeration_type:
10111 case DW_TAG_enumerator:
10112 case DW_TAG_subprogram:
08a76f8a 10113 case DW_TAG_inlined_subroutine:
94af9270 10114 case DW_TAG_member:
74921315 10115 case DW_TAG_imported_declaration:
94af9270
KS
10116 return 1;
10117
10118 case DW_TAG_variable:
c2b0a229 10119 case DW_TAG_constant:
94af9270
KS
10120 /* We only need to prefix "globally" visible variables. These include
10121 any variable marked with DW_AT_external or any variable that
10122 lives in a namespace. [Variables in anonymous namespaces
10123 require prefixing, but they are not DW_AT_external.] */
10124
10125 if (dwarf2_attr (die, DW_AT_specification, cu))
10126 {
10127 struct dwarf2_cu *spec_cu = cu;
9a619af0 10128
94af9270
KS
10129 return die_needs_namespace (die_specification (die, &spec_cu),
10130 spec_cu);
10131 }
10132
1c809c68 10133 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10134 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10135 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10136 return 0;
10137 /* A variable in a lexical block of some kind does not need a
10138 namespace, even though in C++ such variables may be external
10139 and have a mangled name. */
10140 if (die->parent->tag == DW_TAG_lexical_block
10141 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10142 || die->parent->tag == DW_TAG_catch_block
10143 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10144 return 0;
10145 return 1;
94af9270
KS
10146
10147 default:
10148 return 0;
10149 }
10150}
10151
73b9be8b
KS
10152/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10153 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10154 defined for the given DIE. */
10155
10156static struct attribute *
10157dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10158{
10159 struct attribute *attr;
10160
10161 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10162 if (attr == NULL)
10163 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10164
10165 return attr;
10166}
10167
10168/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10169 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10170 defined for the given DIE. */
10171
10172static const char *
10173dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10174{
10175 const char *linkage_name;
10176
10177 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10178 if (linkage_name == NULL)
10179 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10180
787de330
TT
10181 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10182 See https://github.com/rust-lang/rust/issues/32925. */
10183 if (cu->language == language_rust && linkage_name != NULL
10184 && strchr (linkage_name, '{') != NULL)
10185 linkage_name = NULL;
10186
73b9be8b
KS
10187 return linkage_name;
10188}
10189
94af9270 10190/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10191 compute the physname for the object, which include a method's:
9c37b5ae 10192 - formal parameters (C++),
a766d390 10193 - receiver type (Go),
a766d390
DE
10194
10195 The term "physname" is a bit confusing.
10196 For C++, for example, it is the demangled name.
10197 For Go, for example, it's the mangled name.
94af9270 10198
af6b7be1
JB
10199 For Ada, return the DIE's linkage name rather than the fully qualified
10200 name. PHYSNAME is ignored..
10201
5989a64e 10202 The result is allocated on the objfile->per_bfd's obstack and
45940949 10203 canonicalized. */
94af9270
KS
10204
10205static const char *
15d034d0
TT
10206dwarf2_compute_name (const char *name,
10207 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10208 int physname)
10209{
5e22e966 10210 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10211
94af9270
KS
10212 if (name == NULL)
10213 name = dwarf2_name (die, cu);
10214
2ee7123e
DE
10215 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10216 but otherwise compute it by typename_concat inside GDB.
10217 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10218 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10219 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10220 will set the demangled name to the result of dwarf2_full_name, and it is
10221 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10222 if (cu->language == language_ada
10223 || (cu->language == language_fortran && physname))
10224 {
10225 /* For Ada unit, we prefer the linkage name over the name, as
10226 the former contains the exported name, which the user expects
10227 to be able to reference. Ideally, we want the user to be able
10228 to reference this entity using either natural or linkage name,
10229 but we haven't started looking at this enhancement yet. */
73b9be8b 10230 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10231
2ee7123e
DE
10232 if (linkage_name != NULL)
10233 return linkage_name;
f55ee35c
JK
10234 }
10235
94af9270
KS
10236 /* These are the only languages we know how to qualify names in. */
10237 if (name != NULL
9c37b5ae 10238 && (cu->language == language_cplus
c44af4eb
TT
10239 || cu->language == language_fortran || cu->language == language_d
10240 || cu->language == language_rust))
94af9270
KS
10241 {
10242 if (die_needs_namespace (die, cu))
10243 {
0d5cff50 10244 const char *prefix;
34a68019 10245 const char *canonical_name = NULL;
94af9270 10246
d7e74731
PA
10247 string_file buf;
10248
94af9270 10249 prefix = determine_prefix (die, cu);
94af9270
KS
10250 if (*prefix != '\0')
10251 {
43816ebc
TT
10252 gdb::unique_xmalloc_ptr<char> prefixed_name
10253 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10254
43816ebc 10255 buf.puts (prefixed_name.get ());
94af9270
KS
10256 }
10257 else
d7e74731 10258 buf.puts (name);
94af9270 10259
98bfdba5
PA
10260 /* Template parameters may be specified in the DIE's DW_AT_name, or
10261 as children with DW_TAG_template_type_param or
10262 DW_TAG_value_type_param. If the latter, add them to the name
10263 here. If the name already has template parameters, then
10264 skip this step; some versions of GCC emit both, and
10265 it is more efficient to use the pre-computed name.
10266
10267 Something to keep in mind about this process: it is very
10268 unlikely, or in some cases downright impossible, to produce
10269 something that will match the mangled name of a function.
10270 If the definition of the function has the same debug info,
10271 we should be able to match up with it anyway. But fallbacks
10272 using the minimal symbol, for instance to find a method
10273 implemented in a stripped copy of libstdc++, will not work.
10274 If we do not have debug info for the definition, we will have to
10275 match them up some other way.
10276
10277 When we do name matching there is a related problem with function
10278 templates; two instantiated function templates are allowed to
10279 differ only by their return types, which we do not add here. */
10280
10281 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10282 {
10283 struct attribute *attr;
10284 struct die_info *child;
10285 int first = 1;
10286
10287 die->building_fullname = 1;
10288
10289 for (child = die->child; child != NULL; child = child->sibling)
10290 {
10291 struct type *type;
12df843f 10292 LONGEST value;
d521ce57 10293 const gdb_byte *bytes;
98bfdba5
PA
10294 struct dwarf2_locexpr_baton *baton;
10295 struct value *v;
10296
10297 if (child->tag != DW_TAG_template_type_param
10298 && child->tag != DW_TAG_template_value_param)
10299 continue;
10300
10301 if (first)
10302 {
d7e74731 10303 buf.puts ("<");
98bfdba5
PA
10304 first = 0;
10305 }
10306 else
d7e74731 10307 buf.puts (", ");
98bfdba5
PA
10308
10309 attr = dwarf2_attr (child, DW_AT_type, cu);
10310 if (attr == NULL)
10311 {
b98664d3 10312 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10313 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10314 continue;
10315 }
10316 type = die_type (child, cu);
10317
10318 if (child->tag == DW_TAG_template_type_param)
10319 {
c1ec8cea
TT
10320 c_print_type (type, "", &buf, -1, 0, cu->language,
10321 &type_print_raw_options);
98bfdba5
PA
10322 continue;
10323 }
10324
10325 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10326 if (attr == NULL)
10327 {
b98664d3 10328 complaint (_("template parameter missing "
3e43a32a 10329 "DW_AT_const_value"));
d7e74731 10330 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10331 continue;
10332 }
10333
10334 dwarf2_const_value_attr (attr, type, name,
10335 &cu->comp_unit_obstack, cu,
10336 &value, &bytes, &baton);
10337
10338 if (TYPE_NOSIGN (type))
10339 /* GDB prints characters as NUMBER 'CHAR'. If that's
10340 changed, this can use value_print instead. */
d7e74731 10341 c_printchar (value, type, &buf);
98bfdba5
PA
10342 else
10343 {
10344 struct value_print_options opts;
10345
10346 if (baton != NULL)
10347 v = dwarf2_evaluate_loc_desc (type, NULL,
10348 baton->data,
10349 baton->size,
10350 baton->per_cu);
10351 else if (bytes != NULL)
10352 {
10353 v = allocate_value (type);
10354 memcpy (value_contents_writeable (v), bytes,
10355 TYPE_LENGTH (type));
10356 }
10357 else
10358 v = value_from_longest (type, value);
10359
3e43a32a
MS
10360 /* Specify decimal so that we do not depend on
10361 the radix. */
98bfdba5
PA
10362 get_formatted_print_options (&opts, 'd');
10363 opts.raw = 1;
d7e74731 10364 value_print (v, &buf, &opts);
98bfdba5 10365 release_value (v);
98bfdba5
PA
10366 }
10367 }
10368
10369 die->building_fullname = 0;
10370
10371 if (!first)
10372 {
10373 /* Close the argument list, with a space if necessary
10374 (nested templates). */
d7e74731
PA
10375 if (!buf.empty () && buf.string ().back () == '>')
10376 buf.puts (" >");
98bfdba5 10377 else
d7e74731 10378 buf.puts (">");
98bfdba5
PA
10379 }
10380 }
10381
9c37b5ae 10382 /* For C++ methods, append formal parameter type
94af9270 10383 information, if PHYSNAME. */
6e70227d 10384
94af9270 10385 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10386 && cu->language == language_cplus)
94af9270
KS
10387 {
10388 struct type *type = read_type_die (die, cu);
10389
d7e74731 10390 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10391 &type_print_raw_options);
94af9270 10392
9c37b5ae 10393 if (cu->language == language_cplus)
94af9270 10394 {
60430eff
DJ
10395 /* Assume that an artificial first parameter is
10396 "this", but do not crash if it is not. RealView
10397 marks unnamed (and thus unused) parameters as
10398 artificial; there is no way to differentiate
10399 the two cases. */
1f704f76 10400 if (type->num_fields () > 0
94af9270 10401 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10402 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10403 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10404 0))))
d7e74731 10405 buf.puts (" const");
94af9270
KS
10406 }
10407 }
10408
d7e74731 10409 const std::string &intermediate_name = buf.string ();
94af9270
KS
10410
10411 if (cu->language == language_cplus)
34a68019 10412 canonical_name
322a8516 10413 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10414 objfile);
34a68019
TT
10415
10416 /* If we only computed INTERMEDIATE_NAME, or if
10417 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10418 intern it. */
322a8516 10419 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10420 name = objfile->intern (intermediate_name);
34a68019
TT
10421 else
10422 name = canonical_name;
94af9270
KS
10423 }
10424 }
10425
10426 return name;
10427}
10428
0114d602
DJ
10429/* Return the fully qualified name of DIE, based on its DW_AT_name.
10430 If scope qualifiers are appropriate they will be added. The result
34a68019 10431 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10432 not have a name. NAME may either be from a previous call to
10433 dwarf2_name or NULL.
10434
9c37b5ae 10435 The output string will be canonicalized (if C++). */
0114d602
DJ
10436
10437static const char *
15d034d0 10438dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10439{
94af9270
KS
10440 return dwarf2_compute_name (name, die, cu, 0);
10441}
0114d602 10442
94af9270
KS
10443/* Construct a physname for the given DIE in CU. NAME may either be
10444 from a previous call to dwarf2_name or NULL. The result will be
10445 allocated on the objfile_objstack or NULL if the DIE does not have a
10446 name.
0114d602 10447
9c37b5ae 10448 The output string will be canonicalized (if C++). */
0114d602 10449
94af9270 10450static const char *
15d034d0 10451dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10452{
5e22e966 10453 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10454 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10455 int need_copy = 1;
10456
10457 /* In this case dwarf2_compute_name is just a shortcut not building anything
10458 on its own. */
10459 if (!die_needs_namespace (die, cu))
10460 return dwarf2_compute_name (name, die, cu, 1);
10461
906bb4c5
TT
10462 if (cu->language != language_rust)
10463 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10464
10465 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10466 has computed. */
791afaa2 10467 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10468 if (mangled != NULL)
900e11f9 10469 {
900e11f9 10470
59cc4834
JB
10471 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10472 {
10473 /* Do nothing (do not demangle the symbol name). */
10474 }
10475 else if (cu->language == language_go)
a766d390 10476 {
5e2db402
TT
10477 /* This is a lie, but we already lie to the caller new_symbol.
10478 new_symbol assumes we return the mangled name.
a766d390 10479 This just undoes that lie until things are cleaned up. */
a766d390
DE
10480 }
10481 else
10482 {
0eb876f5
JB
10483 /* Use DMGL_RET_DROP for C++ template functions to suppress
10484 their return type. It is easier for GDB users to search
10485 for such functions as `name(params)' than `long name(params)'.
10486 In such case the minimal symbol names do not match the full
10487 symbol names but for template functions there is never a need
10488 to look up their definition from their declaration so
10489 the only disadvantage remains the minimal symbol variant
10490 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10491 demangled.reset (gdb_demangle (mangled,
10492 (DMGL_PARAMS | DMGL_ANSI
10493 | DMGL_RET_DROP)));
a766d390 10494 }
900e11f9 10495 if (demangled)
791afaa2 10496 canon = demangled.get ();
900e11f9
JK
10497 else
10498 {
10499 canon = mangled;
10500 need_copy = 0;
10501 }
10502 }
10503
10504 if (canon == NULL || check_physname)
10505 {
10506 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10507
10508 if (canon != NULL && strcmp (physname, canon) != 0)
10509 {
10510 /* It may not mean a bug in GDB. The compiler could also
10511 compute DW_AT_linkage_name incorrectly. But in such case
10512 GDB would need to be bug-to-bug compatible. */
10513
b98664d3 10514 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10515 "(from linkage <%s>) - DIE at %s [in module %s]"),
10516 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10517 objfile_name (objfile));
900e11f9
JK
10518
10519 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10520 is available here - over computed PHYSNAME. It is safer
10521 against both buggy GDB and buggy compilers. */
10522
10523 retval = canon;
10524 }
10525 else
10526 {
10527 retval = physname;
10528 need_copy = 0;
10529 }
10530 }
10531 else
10532 retval = canon;
10533
10534 if (need_copy)
be1e3d3e 10535 retval = objfile->intern (retval);
900e11f9 10536
900e11f9 10537 return retval;
0114d602
DJ
10538}
10539
74921315
KS
10540/* Inspect DIE in CU for a namespace alias. If one exists, record
10541 a new symbol for it.
10542
10543 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10544
10545static int
10546read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10547{
10548 struct attribute *attr;
10549
10550 /* If the die does not have a name, this is not a namespace
10551 alias. */
10552 attr = dwarf2_attr (die, DW_AT_name, cu);
10553 if (attr != NULL)
10554 {
10555 int num;
10556 struct die_info *d = die;
10557 struct dwarf2_cu *imported_cu = cu;
10558
10559 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10560 keep inspecting DIEs until we hit the underlying import. */
10561#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10562 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10563 {
10564 attr = dwarf2_attr (d, DW_AT_import, cu);
10565 if (attr == NULL)
10566 break;
10567
10568 d = follow_die_ref (d, attr, &imported_cu);
10569 if (d->tag != DW_TAG_imported_declaration)
10570 break;
10571 }
10572
10573 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10574 {
b98664d3 10575 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10576 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10577 return 0;
10578 }
10579
10580 if (attr != NULL)
10581 {
10582 struct type *type;
0826b30a 10583 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10584
9c541725 10585 type = get_die_type_at_offset (sect_off, cu->per_cu);
78134374 10586 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10587 {
10588 /* This declaration is a global namespace alias. Add
10589 a symbol for it whose type is the aliased namespace. */
10590 new_symbol (die, type, cu);
10591 return 1;
10592 }
10593 }
10594 }
10595
10596 return 0;
10597}
10598
22cee43f 10599/* Return the using directives repository (global or local?) to use in the
804d2729 10600 current context for CU.
22cee43f
PMR
10601
10602 For Ada, imported declarations can materialize renamings, which *may* be
10603 global. However it is impossible (for now?) in DWARF to distinguish
10604 "external" imported declarations and "static" ones. As all imported
10605 declarations seem to be static in all other languages, make them all CU-wide
10606 global only in Ada. */
10607
10608static struct using_direct **
804d2729 10609using_directives (struct dwarf2_cu *cu)
22cee43f 10610{
c24bdb02
KS
10611 if (cu->language == language_ada
10612 && cu->get_builder ()->outermost_context_p ())
10613 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10614 else
c24bdb02 10615 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10616}
10617
27aa8d6a
SW
10618/* Read the import statement specified by the given die and record it. */
10619
10620static void
10621read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10622{
5e22e966 10623 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10624 struct attribute *import_attr;
32019081 10625 struct die_info *imported_die, *child_die;
de4affc9 10626 struct dwarf2_cu *imported_cu;
27aa8d6a 10627 const char *imported_name;
794684b6 10628 const char *imported_name_prefix;
13387711
SW
10629 const char *canonical_name;
10630 const char *import_alias;
10631 const char *imported_declaration = NULL;
794684b6 10632 const char *import_prefix;
eb1e02fd 10633 std::vector<const char *> excludes;
13387711 10634
27aa8d6a
SW
10635 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10636 if (import_attr == NULL)
10637 {
b98664d3 10638 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10639 dwarf_tag_name (die->tag));
10640 return;
10641 }
10642
de4affc9
CC
10643 imported_cu = cu;
10644 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10645 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10646 if (imported_name == NULL)
10647 {
10648 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10649
10650 The import in the following code:
10651 namespace A
10652 {
10653 typedef int B;
10654 }
10655
10656 int main ()
10657 {
10658 using A::B;
10659 B b;
10660 return b;
10661 }
10662
10663 ...
10664 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10665 <52> DW_AT_decl_file : 1
10666 <53> DW_AT_decl_line : 6
10667 <54> DW_AT_import : <0x75>
10668 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10669 <59> DW_AT_name : B
10670 <5b> DW_AT_decl_file : 1
10671 <5c> DW_AT_decl_line : 2
10672 <5d> DW_AT_type : <0x6e>
10673 ...
10674 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10675 <76> DW_AT_byte_size : 4
10676 <77> DW_AT_encoding : 5 (signed)
10677
10678 imports the wrong die ( 0x75 instead of 0x58 ).
10679 This case will be ignored until the gcc bug is fixed. */
10680 return;
10681 }
10682
82856980
SW
10683 /* Figure out the local name after import. */
10684 import_alias = dwarf2_name (die, cu);
27aa8d6a 10685
794684b6
SW
10686 /* Figure out where the statement is being imported to. */
10687 import_prefix = determine_prefix (die, cu);
10688
10689 /* Figure out what the scope of the imported die is and prepend it
10690 to the name of the imported die. */
de4affc9 10691 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10692
f55ee35c
JK
10693 if (imported_die->tag != DW_TAG_namespace
10694 && imported_die->tag != DW_TAG_module)
794684b6 10695 {
13387711
SW
10696 imported_declaration = imported_name;
10697 canonical_name = imported_name_prefix;
794684b6 10698 }
13387711 10699 else if (strlen (imported_name_prefix) > 0)
12aaed36 10700 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10701 imported_name_prefix,
10702 (cu->language == language_d ? "." : "::"),
10703 imported_name, (char *) NULL);
13387711
SW
10704 else
10705 canonical_name = imported_name;
794684b6 10706
32019081
JK
10707 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10708 for (child_die = die->child; child_die && child_die->tag;
436c571c 10709 child_die = child_die->sibling)
32019081
JK
10710 {
10711 /* DWARF-4: A Fortran use statement with a “rename list” may be
10712 represented by an imported module entry with an import attribute
10713 referring to the module and owned entries corresponding to those
10714 entities that are renamed as part of being imported. */
10715
10716 if (child_die->tag != DW_TAG_imported_declaration)
10717 {
b98664d3 10718 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10719 "- DIE at %s [in module %s]"),
10720 sect_offset_str (child_die->sect_off),
10721 objfile_name (objfile));
32019081
JK
10722 continue;
10723 }
10724
10725 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10726 if (import_attr == NULL)
10727 {
b98664d3 10728 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10729 dwarf_tag_name (child_die->tag));
10730 continue;
10731 }
10732
10733 imported_cu = cu;
10734 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10735 &imported_cu);
10736 imported_name = dwarf2_name (imported_die, imported_cu);
10737 if (imported_name == NULL)
10738 {
b98664d3 10739 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10740 "imported name - DIE at %s [in module %s]"),
10741 sect_offset_str (child_die->sect_off),
10742 objfile_name (objfile));
32019081
JK
10743 continue;
10744 }
10745
eb1e02fd 10746 excludes.push_back (imported_name);
32019081
JK
10747
10748 process_die (child_die, cu);
10749 }
10750
804d2729 10751 add_using_directive (using_directives (cu),
22cee43f
PMR
10752 import_prefix,
10753 canonical_name,
10754 import_alias,
10755 imported_declaration,
10756 excludes,
10757 0,
10758 &objfile->objfile_obstack);
27aa8d6a
SW
10759}
10760
5230b05a
WT
10761/* ICC<14 does not output the required DW_AT_declaration on incomplete
10762 types, but gives them a size of zero. Starting with version 14,
10763 ICC is compatible with GCC. */
10764
9068261f 10765static bool
5230b05a
WT
10766producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10767{
10768 if (!cu->checked_producer)
10769 check_producer (cu);
10770
10771 return cu->producer_is_icc_lt_14;
10772}
10773
eb77c9df
AB
10774/* ICC generates a DW_AT_type for C void functions. This was observed on
10775 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10776 which says that void functions should not have a DW_AT_type. */
10777
10778static bool
10779producer_is_icc (struct dwarf2_cu *cu)
10780{
10781 if (!cu->checked_producer)
10782 check_producer (cu);
10783
10784 return cu->producer_is_icc;
10785}
10786
1b80a9fa
JK
10787/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10788 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10789 this, it was first present in GCC release 4.3.0. */
10790
9068261f 10791static bool
1b80a9fa
JK
10792producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10793{
10794 if (!cu->checked_producer)
10795 check_producer (cu);
10796
10797 return cu->producer_is_gcc_lt_4_3;
10798}
10799
d721ba37
PA
10800static file_and_directory
10801find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10802{
d721ba37
PA
10803 file_and_directory res;
10804
9291a0cd
TT
10805 /* Find the filename. Do not use dwarf2_name here, since the filename
10806 is not a source language identifier. */
d721ba37
PA
10807 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10808 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10809
d721ba37
PA
10810 if (res.comp_dir == NULL
10811 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10812 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10813 {
d721ba37
PA
10814 res.comp_dir_storage = ldirname (res.name);
10815 if (!res.comp_dir_storage.empty ())
10816 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10817 }
d721ba37 10818 if (res.comp_dir != NULL)
9291a0cd
TT
10819 {
10820 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10821 directory, get rid of it. */
d721ba37 10822 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10823
d721ba37
PA
10824 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10825 res.comp_dir = cp + 1;
9291a0cd
TT
10826 }
10827
d721ba37
PA
10828 if (res.name == NULL)
10829 res.name = "<unknown>";
10830
10831 return res;
9291a0cd
TT
10832}
10833
f4dc4d17
DE
10834/* Handle DW_AT_stmt_list for a compilation unit.
10835 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10836 COMP_DIR is the compilation directory. LOWPC is passed to
10837 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10838
10839static void
10840handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10841 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10842{
5e22e966 10843 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10844 struct attribute *attr;
527f3840
JK
10845 struct line_header line_header_local;
10846 hashval_t line_header_local_hash;
527f3840
JK
10847 void **slot;
10848 int decode_mapping;
2ab95328 10849
f4dc4d17
DE
10850 gdb_assert (! cu->per_cu->is_debug_types);
10851
2ab95328 10852 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10853 if (attr == NULL)
10854 return;
10855
9c541725 10856 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10857
10858 /* The line header hash table is only created if needed (it exists to
10859 prevent redundant reading of the line table for partial_units).
10860 If we're given a partial_unit, we'll need it. If we're given a
10861 compile_unit, then use the line header hash table if it's already
10862 created, but don't create one just yet. */
10863
5989a64e 10864 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10865 && die->tag == DW_TAG_partial_unit)
2ab95328 10866 {
5989a64e 10867 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10868 .reset (htab_create_alloc (127, line_header_hash_voidp,
10869 line_header_eq_voidp,
10870 free_line_header_voidp,
10871 xcalloc, xfree));
527f3840 10872 }
2ab95328 10873
9c541725 10874 line_header_local.sect_off = line_offset;
527f3840
JK
10875 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10876 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10877 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10878 {
5989a64e 10879 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10880 &line_header_local,
10881 line_header_local_hash, NO_INSERT);
10882
10883 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10884 is not present in *SLOT (since if there is something in *SLOT then
10885 it will be for a partial_unit). */
10886 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10887 {
527f3840 10888 gdb_assert (*slot != NULL);
9a3c8263 10889 cu->line_header = (struct line_header *) *slot;
527f3840 10890 return;
dee91e82 10891 }
2ab95328 10892 }
527f3840
JK
10893
10894 /* dwarf_decode_line_header does not yet provide sufficient information.
10895 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10896 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10897 if (lh == NULL)
527f3840 10898 return;
4c8aa72d
PA
10899
10900 cu->line_header = lh.release ();
10901 cu->line_header_die_owner = die;
527f3840 10902
5989a64e 10903 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10904 slot = NULL;
10905 else
10906 {
5989a64e 10907 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10908 &line_header_local,
10909 line_header_local_hash, INSERT);
10910 gdb_assert (slot != NULL);
10911 }
10912 if (slot != NULL && *slot == NULL)
10913 {
10914 /* This newly decoded line number information unit will be owned
10915 by line_header_hash hash table. */
10916 *slot = cu->line_header;
4c8aa72d 10917 cu->line_header_die_owner = NULL;
527f3840
JK
10918 }
10919 else
10920 {
10921 /* We cannot free any current entry in (*slot) as that struct line_header
10922 may be already used by multiple CUs. Create only temporary decoded
10923 line_header for this CU - it may happen at most once for each line
10924 number information unit. And if we're not using line_header_hash
10925 then this is what we want as well. */
10926 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10927 }
10928 decode_mapping = (die->tag != DW_TAG_partial_unit);
10929 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10930 decode_mapping);
fff8551c 10931
2ab95328
TT
10932}
10933
95554aad 10934/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10935
c906108c 10936static void
e7c27a73 10937read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10938{
5e22e966 10939 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10940 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10941 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10942 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10943 CORE_ADDR highpc = ((CORE_ADDR) 0);
10944 struct attribute *attr;
c906108c 10945 struct die_info *child_die;
e142c38c 10946 CORE_ADDR baseaddr;
6e70227d 10947
380618d6 10948 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10949 baseaddr = objfile->text_section_offset ();
c906108c 10950
fae299cd 10951 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10952
10953 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10954 from finish_block. */
2acceee2 10955 if (lowpc == ((CORE_ADDR) -1))
c906108c 10956 lowpc = highpc;
3e29f34a 10957 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10958
d721ba37 10959 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10960
f4b8a18d
KW
10961 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10962 standardised yet. As a workaround for the language detection we fall
10963 back to the DW_AT_producer string. */
10964 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10965 cu->language = language_opencl;
10966
3019eac3
DE
10967 /* Similar hack for Go. */
10968 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10969 set_cu_language (DW_LANG_Go, cu);
10970
c24bdb02 10971 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10972
10973 /* Decode line number information if present. We do this before
10974 processing child DIEs, so that the line header table is available
10975 for DW_AT_decl_file. */
d721ba37 10976 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10977
10978 /* Process all dies in compilation unit. */
10979 if (die->child != NULL)
10980 {
10981 child_die = die->child;
10982 while (child_die && child_die->tag)
10983 {
10984 process_die (child_die, cu);
436c571c 10985 child_die = child_die->sibling;
3019eac3
DE
10986 }
10987 }
10988
10989 /* Decode macro information, if present. Dwarf 2 macro information
10990 refers to information in the line number info statement program
10991 header, so we can only read it if we've read the header
10992 successfully. */
0af92d60
JK
10993 attr = dwarf2_attr (die, DW_AT_macros, cu);
10994 if (attr == NULL)
10995 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10996 if (attr && cu->line_header)
10997 {
10998 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10999 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11000
43f3e411 11001 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11002 }
11003 else
11004 {
11005 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11006 if (attr && cu->line_header)
11007 {
11008 unsigned int macro_offset = DW_UNSND (attr);
11009
43f3e411 11010 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11011 }
11012 }
3019eac3
DE
11013}
11014
c24bdb02
KS
11015void
11016dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11017{
f4dc4d17
DE
11018 struct type_unit_group *tu_group;
11019 int first_time;
3019eac3 11020 struct attribute *attr;
9c541725 11021 unsigned int i;
0186c6a7 11022 struct signatured_type *sig_type;
3019eac3 11023
f4dc4d17 11024 gdb_assert (per_cu->is_debug_types);
0186c6a7 11025 sig_type = (struct signatured_type *) per_cu;
3019eac3 11026
c24bdb02 11027 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11028
f4dc4d17 11029 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11030 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11031 if (sig_type->type_unit_group == NULL)
c24bdb02 11032 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11033 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11034
11035 /* If we've already processed this stmt_list there's no real need to
11036 do it again, we could fake it and just recreate the part we need
11037 (file name,index -> symtab mapping). If data shows this optimization
11038 is useful we can do it then. */
43f3e411 11039 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11040
11041 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11042 debug info. */
fff8551c 11043 line_header_up lh;
f4dc4d17 11044 if (attr != NULL)
3019eac3 11045 {
9c541725 11046 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11047 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11048 }
11049 if (lh == NULL)
11050 {
11051 if (first_time)
c24bdb02 11052 start_symtab ("", NULL, 0);
f4dc4d17
DE
11053 else
11054 {
11055 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11056 gdb_assert (m_builder == nullptr);
804d2729 11057 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11058 m_builder.reset (new struct buildsym_compunit
11059 (COMPUNIT_OBJFILE (cust), "",
11060 COMPUNIT_DIRNAME (cust),
11061 compunit_language (cust),
11062 0, cust));
770479f2 11063 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11064 }
f4dc4d17 11065 return;
3019eac3
DE
11066 }
11067
c24bdb02
KS
11068 line_header = lh.release ();
11069 line_header_die_owner = die;
3019eac3 11070
f4dc4d17
DE
11071 if (first_time)
11072 {
c24bdb02 11073 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11074
1fd60fc0
DE
11075 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11076 still initializing it, and our caller (a few levels up)
11077 process_full_type_unit still needs to know if this is the first
11078 time. */
11079
4ac93832
TT
11080 tu_group->symtabs
11081 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11082 struct symtab *, line_header->file_names_size ());
3019eac3 11083
7ba99d21
AT
11084 auto &file_names = line_header->file_names ();
11085 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11086 {
7ba99d21 11087 file_entry &fe = file_names[i];
c24bdb02
KS
11088 dwarf2_start_subfile (this, fe.name,
11089 fe.include_dir (line_header));
11090 buildsym_compunit *b = get_builder ();
11091 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11092 {
4c8aa72d
PA
11093 /* NOTE: start_subfile will recognize when it's been
11094 passed a file it has already seen. So we can't
11095 assume there's a simple mapping from
11096 cu->line_header->file_names to subfiles, plus
11097 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11098 b->get_current_subfile ()->symtab
11099 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11100 }
11101
c24bdb02 11102 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11103 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11104 }
11105 }
11106 else
3019eac3 11107 {
c24bdb02 11108 gdb_assert (m_builder == nullptr);
804d2729 11109 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11110 m_builder.reset (new struct buildsym_compunit
11111 (COMPUNIT_OBJFILE (cust), "",
11112 COMPUNIT_DIRNAME (cust),
11113 compunit_language (cust),
11114 0, cust));
770479f2 11115 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11116
7ba99d21
AT
11117 auto &file_names = line_header->file_names ();
11118 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11119 {
7ba99d21 11120 file_entry &fe = file_names[i];
4c8aa72d 11121 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11122 }
3019eac3
DE
11123 }
11124
f4dc4d17
DE
11125 /* The main symtab is allocated last. Type units don't have DW_AT_name
11126 so they don't have a "real" (so to speak) symtab anyway.
11127 There is later code that will assign the main symtab to all symbols
11128 that don't have one. We need to handle the case of a symbol with a
11129 missing symtab (DW_AT_decl_file) anyway. */
11130}
3019eac3 11131
f4dc4d17
DE
11132/* Process DW_TAG_type_unit.
11133 For TUs we want to skip the first top level sibling if it's not the
11134 actual type being defined by this TU. In this case the first top
11135 level sibling is there to provide context only. */
3019eac3 11136
f4dc4d17
DE
11137static void
11138read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11139{
11140 struct die_info *child_die;
3019eac3 11141
f4dc4d17
DE
11142 prepare_one_comp_unit (cu, die, language_minimal);
11143
11144 /* Initialize (or reinitialize) the machinery for building symtabs.
11145 We do this before processing child DIEs, so that the line header table
11146 is available for DW_AT_decl_file. */
c24bdb02 11147 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11148
11149 if (die->child != NULL)
11150 {
11151 child_die = die->child;
11152 while (child_die && child_die->tag)
11153 {
11154 process_die (child_die, cu);
436c571c 11155 child_die = child_die->sibling;
f4dc4d17
DE
11156 }
11157 }
3019eac3
DE
11158}
11159\f
80626a55
DE
11160/* DWO/DWP files.
11161
11162 http://gcc.gnu.org/wiki/DebugFission
11163 http://gcc.gnu.org/wiki/DebugFissionDWP
11164
11165 To simplify handling of both DWO files ("object" files with the DWARF info)
11166 and DWP files (a file with the DWOs packaged up into one file), we treat
11167 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11168
11169static hashval_t
11170hash_dwo_file (const void *item)
11171{
9a3c8263 11172 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11173 hashval_t hash;
3019eac3 11174
a2ce51a0
DE
11175 hash = htab_hash_string (dwo_file->dwo_name);
11176 if (dwo_file->comp_dir != NULL)
11177 hash += htab_hash_string (dwo_file->comp_dir);
11178 return hash;
3019eac3
DE
11179}
11180
11181static int
11182eq_dwo_file (const void *item_lhs, const void *item_rhs)
11183{
9a3c8263
SM
11184 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11185 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11186
a2ce51a0
DE
11187 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11188 return 0;
11189 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11190 return lhs->comp_dir == rhs->comp_dir;
11191 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11192}
11193
11194/* Allocate a hash table for DWO files. */
11195
51ac9db5 11196static htab_up
298e9637 11197allocate_dwo_file_hash_table ()
3019eac3 11198{
51ac9db5
SM
11199 auto delete_dwo_file = [] (void *item)
11200 {
11201 struct dwo_file *dwo_file = (struct dwo_file *) item;
11202
11203 delete dwo_file;
11204 };
11205
bc68fb19
TT
11206 return htab_up (htab_create_alloc (41,
11207 hash_dwo_file,
11208 eq_dwo_file,
11209 delete_dwo_file,
11210 xcalloc, xfree));
3019eac3
DE
11211}
11212
80626a55
DE
11213/* Lookup DWO file DWO_NAME. */
11214
11215static void **
ed2dc618
SM
11216lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11217 const char *dwo_name,
11218 const char *comp_dir)
80626a55
DE
11219{
11220 struct dwo_file find_entry;
11221 void **slot;
11222
5989a64e
SM
11223 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11224 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11225
0ac5b59e
DE
11226 find_entry.dwo_name = dwo_name;
11227 find_entry.comp_dir = comp_dir;
5989a64e 11228 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11229 INSERT);
80626a55
DE
11230
11231 return slot;
11232}
11233
3019eac3
DE
11234static hashval_t
11235hash_dwo_unit (const void *item)
11236{
9a3c8263 11237 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11238
11239 /* This drops the top 32 bits of the id, but is ok for a hash. */
11240 return dwo_unit->signature;
11241}
11242
11243static int
11244eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11245{
9a3c8263
SM
11246 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11247 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11248
11249 /* The signature is assumed to be unique within the DWO file.
11250 So while object file CU dwo_id's always have the value zero,
11251 that's OK, assuming each object file DWO file has only one CU,
11252 and that's the rule for now. */
11253 return lhs->signature == rhs->signature;
11254}
11255
11256/* Allocate a hash table for DWO CUs,TUs.
11257 There is one of these tables for each of CUs,TUs for each DWO file. */
11258
b0b6a987 11259static htab_up
298e9637 11260allocate_dwo_unit_table ()
3019eac3
DE
11261{
11262 /* Start out with a pretty small number.
11263 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11264 return htab_up (htab_create_alloc (3,
11265 hash_dwo_unit,
11266 eq_dwo_unit,
11267 NULL, xcalloc, xfree));
3019eac3
DE
11268}
11269
19c3d4c9 11270/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11271
11272static void
19c3d4c9
DE
11273create_dwo_cu_reader (const struct die_reader_specs *reader,
11274 const gdb_byte *info_ptr,
11275 struct die_info *comp_unit_die,
c0ab21c2
TT
11276 struct dwo_file *dwo_file,
11277 struct dwo_unit *dwo_unit)
3019eac3
DE
11278{
11279 struct dwarf2_cu *cu = reader->cu;
9c541725 11280 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11281 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11282
a084a2a6
AT
11283 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11284 if (!signature.has_value ())
3019eac3 11285 {
b98664d3 11286 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11287 " its dwo_id [in module %s]"),
9d8780f0 11288 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11289 return;
11290 }
11291
3019eac3 11292 dwo_unit->dwo_file = dwo_file;
a084a2a6 11293 dwo_unit->signature = *signature;
8a0459fd 11294 dwo_unit->section = section;
9c541725 11295 dwo_unit->sect_off = sect_off;
3019eac3
DE
11296 dwo_unit->length = cu->per_cu->length;
11297
b4f54984 11298 if (dwarf_read_debug)
9d8780f0
SM
11299 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11300 sect_offset_str (sect_off),
9c541725 11301 hex_string (dwo_unit->signature));
3019eac3
DE
11302}
11303
33c5cd75 11304/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11305 Note: This function processes DWO files only, not DWP files. */
3019eac3 11306
33c5cd75 11307static void
ed2dc618 11308create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11309 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11310 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11311{
11312 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11313 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11314 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11315
96b79293 11316 section.read (objfile);
33c5cd75 11317 info_ptr = section.buffer;
3019eac3
DE
11318
11319 if (info_ptr == NULL)
33c5cd75 11320 return;
3019eac3 11321
b4f54984 11322 if (dwarf_read_debug)
19c3d4c9
DE
11323 {
11324 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11325 section.get_name (),
11326 section.get_file_name ());
19c3d4c9 11327 }
3019eac3 11328
33c5cd75 11329 end_ptr = info_ptr + section.size;
3019eac3
DE
11330 while (info_ptr < end_ptr)
11331 {
11332 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11333 struct dwo_unit read_unit {};
33c5cd75
DB
11334 struct dwo_unit *dwo_unit;
11335 void **slot;
11336 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11337
11338 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11339 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
1859c670 11340 per_cu.per_bfd = per_bfd;
3019eac3 11341 per_cu.is_debug_types = 0;
33c5cd75
DB
11342 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11343 per_cu.section = &section;
11344
ab432490 11345 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11346 if (!reader.dummy_p)
11347 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11348 &dwo_file, &read_unit);
33c5cd75
DB
11349 info_ptr += per_cu.length;
11350
11351 // If the unit could not be parsed, skip it.
c0ab21c2 11352 if (read_unit.dwo_file == NULL)
33c5cd75 11353 continue;
3019eac3 11354
33c5cd75 11355 if (cus_htab == NULL)
298e9637 11356 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11357
1859c670 11358 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11359 struct dwo_unit);
c0ab21c2 11360 *dwo_unit = read_unit;
b0b6a987 11361 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11362 gdb_assert (slot != NULL);
11363 if (*slot != NULL)
19c3d4c9 11364 {
33c5cd75
DB
11365 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11366 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11367
b98664d3 11368 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11369 " the entry at offset %s, signature %s"),
11370 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11371 hex_string (dwo_unit->signature));
19c3d4c9 11372 }
33c5cd75 11373 *slot = (void *)dwo_unit;
3019eac3 11374 }
3019eac3
DE
11375}
11376
80626a55
DE
11377/* DWP file .debug_{cu,tu}_index section format:
11378 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11379
d2415c6c
DE
11380 DWP Version 1:
11381
80626a55
DE
11382 Both index sections have the same format, and serve to map a 64-bit
11383 signature to a set of section numbers. Each section begins with a header,
11384 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11385 indexes, and a pool of 32-bit section numbers. The index sections will be
11386 aligned at 8-byte boundaries in the file.
11387
d2415c6c
DE
11388 The index section header consists of:
11389
11390 V, 32 bit version number
11391 -, 32 bits unused
11392 N, 32 bit number of compilation units or type units in the index
11393 M, 32 bit number of slots in the hash table
80626a55 11394
d2415c6c 11395 Numbers are recorded using the byte order of the application binary.
80626a55 11396
d2415c6c
DE
11397 The hash table begins at offset 16 in the section, and consists of an array
11398 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11399 order of the application binary). Unused slots in the hash table are 0.
11400 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11401
d2415c6c
DE
11402 The parallel table begins immediately after the hash table
11403 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11404 array of 32-bit indexes (using the byte order of the application binary),
11405 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11406 table contains a 32-bit index into the pool of section numbers. For unused
11407 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11408
73869dc2
DE
11409 The pool of section numbers begins immediately following the hash table
11410 (at offset 16 + 12 * M from the beginning of the section). The pool of
11411 section numbers consists of an array of 32-bit words (using the byte order
11412 of the application binary). Each item in the array is indexed starting
11413 from 0. The hash table entry provides the index of the first section
11414 number in the set. Additional section numbers in the set follow, and the
11415 set is terminated by a 0 entry (section number 0 is not used in ELF).
11416
11417 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11418 section must be the first entry in the set, and the .debug_abbrev.dwo must
11419 be the second entry. Other members of the set may follow in any order.
11420
11421 ---
11422
11423 DWP Version 2:
11424
11425 DWP Version 2 combines all the .debug_info, etc. sections into one,
11426 and the entries in the index tables are now offsets into these sections.
11427 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11428 section.
11429
11430 Index Section Contents:
11431 Header
11432 Hash Table of Signatures dwp_hash_table.hash_table
11433 Parallel Table of Indices dwp_hash_table.unit_table
11434 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11435 Table of Section Sizes dwp_hash_table.v2.sizes
11436
11437 The index section header consists of:
11438
11439 V, 32 bit version number
11440 L, 32 bit number of columns in the table of section offsets
11441 N, 32 bit number of compilation units or type units in the index
11442 M, 32 bit number of slots in the hash table
11443
11444 Numbers are recorded using the byte order of the application binary.
11445
11446 The hash table has the same format as version 1.
11447 The parallel table of indices has the same format as version 1,
11448 except that the entries are origin-1 indices into the table of sections
11449 offsets and the table of section sizes.
11450
11451 The table of offsets begins immediately following the parallel table
11452 (at offset 16 + 12 * M from the beginning of the section). The table is
11453 a two-dimensional array of 32-bit words (using the byte order of the
11454 application binary), with L columns and N+1 rows, in row-major order.
11455 Each row in the array is indexed starting from 0. The first row provides
11456 a key to the remaining rows: each column in this row provides an identifier
11457 for a debug section, and the offsets in the same column of subsequent rows
11458 refer to that section. The section identifiers are:
11459
11460 DW_SECT_INFO 1 .debug_info.dwo
11461 DW_SECT_TYPES 2 .debug_types.dwo
11462 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11463 DW_SECT_LINE 4 .debug_line.dwo
11464 DW_SECT_LOC 5 .debug_loc.dwo
11465 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11466 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11467 DW_SECT_MACRO 8 .debug_macro.dwo
11468
11469 The offsets provided by the CU and TU index sections are the base offsets
11470 for the contributions made by each CU or TU to the corresponding section
11471 in the package file. Each CU and TU header contains an abbrev_offset
11472 field, used to find the abbreviations table for that CU or TU within the
11473 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11474 be interpreted as relative to the base offset given in the index section.
11475 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11476 should be interpreted as relative to the base offset for .debug_line.dwo,
11477 and offsets into other debug sections obtained from DWARF attributes should
11478 also be interpreted as relative to the corresponding base offset.
11479
11480 The table of sizes begins immediately following the table of offsets.
11481 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11482 with L columns and N rows, in row-major order. Each row in the array is
11483 indexed starting from 1 (row 0 is shared by the two tables).
11484
11485 ---
11486
11487 Hash table lookup is handled the same in version 1 and 2:
11488
11489 We assume that N and M will not exceed 2^32 - 1.
11490 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11491
d2415c6c
DE
11492 Given a 64-bit compilation unit signature or a type signature S, an entry
11493 in the hash table is located as follows:
80626a55 11494
d2415c6c
DE
11495 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11496 the low-order k bits all set to 1.
80626a55 11497
d2415c6c 11498 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11499
d2415c6c
DE
11500 3) If the hash table entry at index H matches the signature, use that
11501 entry. If the hash table entry at index H is unused (all zeroes),
11502 terminate the search: the signature is not present in the table.
80626a55 11503
d2415c6c 11504 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11505
d2415c6c 11506 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11507 to stop at an unused slot or find the match. */
80626a55
DE
11508
11509/* Create a hash table to map DWO IDs to their CU/TU entry in
11510 .debug_{info,types}.dwo in DWP_FILE.
11511 Returns NULL if there isn't one.
11512 Note: This function processes DWP files only, not DWO files. */
11513
11514static struct dwp_hash_table *
ed2dc618
SM
11515create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11516 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11517{
11518 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11519 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11520 const gdb_byte *index_ptr, *index_end;
80626a55 11521 struct dwarf2_section_info *index;
73869dc2 11522 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11523 struct dwp_hash_table *htab;
11524
11525 if (is_debug_types)
11526 index = &dwp_file->sections.tu_index;
11527 else
11528 index = &dwp_file->sections.cu_index;
11529
96b79293 11530 if (index->empty ())
80626a55 11531 return NULL;
96b79293 11532 index->read (objfile);
80626a55
DE
11533
11534 index_ptr = index->buffer;
11535 index_end = index_ptr + index->size;
11536
11537 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11538 index_ptr += 4;
11539 if (version == 2)
11540 nr_columns = read_4_bytes (dbfd, index_ptr);
11541 else
11542 nr_columns = 0;
11543 index_ptr += 4;
80626a55
DE
11544 nr_units = read_4_bytes (dbfd, index_ptr);
11545 index_ptr += 4;
11546 nr_slots = read_4_bytes (dbfd, index_ptr);
11547 index_ptr += 4;
11548
73869dc2 11549 if (version != 1 && version != 2)
80626a55 11550 {
21aa081e 11551 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11552 " [in module %s]"),
21aa081e 11553 pulongest (version), dwp_file->name);
80626a55
DE
11554 }
11555 if (nr_slots != (nr_slots & -nr_slots))
11556 {
21aa081e 11557 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11558 " is not power of 2 [in module %s]"),
21aa081e 11559 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11560 }
11561
5989a64e 11562 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11563 htab->version = version;
11564 htab->nr_columns = nr_columns;
80626a55
DE
11565 htab->nr_units = nr_units;
11566 htab->nr_slots = nr_slots;
11567 htab->hash_table = index_ptr;
11568 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11569
11570 /* Exit early if the table is empty. */
11571 if (nr_slots == 0 || nr_units == 0
11572 || (version == 2 && nr_columns == 0))
11573 {
11574 /* All must be zero. */
11575 if (nr_slots != 0 || nr_units != 0
11576 || (version == 2 && nr_columns != 0))
11577 {
b98664d3 11578 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11579 " all zero [in modules %s]"),
11580 dwp_file->name);
11581 }
11582 return htab;
11583 }
11584
11585 if (version == 1)
11586 {
11587 htab->section_pool.v1.indices =
11588 htab->unit_table + sizeof (uint32_t) * nr_slots;
11589 /* It's harder to decide whether the section is too small in v1.
11590 V1 is deprecated anyway so we punt. */
11591 }
11592 else
11593 {
11594 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11595 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11596 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11597 /* Reverse map for error checking. */
11598 int ids_seen[DW_SECT_MAX + 1];
11599 int i;
11600
11601 if (nr_columns < 2)
11602 {
11603 error (_("Dwarf Error: bad DWP hash table, too few columns"
11604 " in section table [in module %s]"),
11605 dwp_file->name);
11606 }
11607 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11608 {
11609 error (_("Dwarf Error: bad DWP hash table, too many columns"
11610 " in section table [in module %s]"),
11611 dwp_file->name);
11612 }
04fd5eed
GB
11613 memset (ids, 255, sizeof_ids);
11614 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11615 for (i = 0; i < nr_columns; ++i)
11616 {
11617 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11618
11619 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11620 {
11621 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11622 " in section table [in module %s]"),
11623 id, dwp_file->name);
11624 }
11625 if (ids_seen[id] != -1)
11626 {
11627 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11628 " id %d in section table [in module %s]"),
11629 id, dwp_file->name);
11630 }
11631 ids_seen[id] = i;
11632 ids[i] = id;
11633 }
11634 /* Must have exactly one info or types section. */
11635 if (((ids_seen[DW_SECT_INFO] != -1)
11636 + (ids_seen[DW_SECT_TYPES] != -1))
11637 != 1)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11640 " DWO info/types section [in module %s]"),
11641 dwp_file->name);
11642 }
11643 /* Must have an abbrev section. */
11644 if (ids_seen[DW_SECT_ABBREV] == -1)
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11647 " section [in module %s]"),
11648 dwp_file->name);
11649 }
11650 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11651 htab->section_pool.v2.sizes =
11652 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11653 * nr_units * nr_columns);
11654 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11655 * nr_units * nr_columns))
11656 > index_end)
11657 {
11658 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11659 " [in module %s]"),
11660 dwp_file->name);
11661 }
11662 }
80626a55
DE
11663
11664 return htab;
11665}
11666
11667/* Update SECTIONS with the data from SECTP.
11668
11669 This function is like the other "locate" section routines that are
11670 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11671 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11672
11673 The result is non-zero for success, or zero if an error was found. */
11674
11675static int
73869dc2
DE
11676locate_v1_virtual_dwo_sections (asection *sectp,
11677 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11678{
11679 const struct dwop_section_names *names = &dwop_section_names;
11680
11681 if (section_is_p (sectp->name, &names->abbrev_dwo))
11682 {
11683 /* There can be only one. */
049412e3 11684 if (sections->abbrev.s.section != NULL)
80626a55 11685 return 0;
049412e3 11686 sections->abbrev.s.section = sectp;
fd361982 11687 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11688 }
11689 else if (section_is_p (sectp->name, &names->info_dwo)
11690 || section_is_p (sectp->name, &names->types_dwo))
11691 {
11692 /* There can be only one. */
049412e3 11693 if (sections->info_or_types.s.section != NULL)
80626a55 11694 return 0;
049412e3 11695 sections->info_or_types.s.section = sectp;
fd361982 11696 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11697 }
11698 else if (section_is_p (sectp->name, &names->line_dwo))
11699 {
11700 /* There can be only one. */
049412e3 11701 if (sections->line.s.section != NULL)
80626a55 11702 return 0;
049412e3 11703 sections->line.s.section = sectp;
fd361982 11704 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11705 }
11706 else if (section_is_p (sectp->name, &names->loc_dwo))
11707 {
11708 /* There can be only one. */
049412e3 11709 if (sections->loc.s.section != NULL)
80626a55 11710 return 0;
049412e3 11711 sections->loc.s.section = sectp;
fd361982 11712 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11713 }
11714 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11715 {
11716 /* There can be only one. */
049412e3 11717 if (sections->macinfo.s.section != NULL)
80626a55 11718 return 0;
049412e3 11719 sections->macinfo.s.section = sectp;
fd361982 11720 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11721 }
11722 else if (section_is_p (sectp->name, &names->macro_dwo))
11723 {
11724 /* There can be only one. */
049412e3 11725 if (sections->macro.s.section != NULL)
80626a55 11726 return 0;
049412e3 11727 sections->macro.s.section = sectp;
fd361982 11728 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11729 }
11730 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11731 {
11732 /* There can be only one. */
049412e3 11733 if (sections->str_offsets.s.section != NULL)
80626a55 11734 return 0;
049412e3 11735 sections->str_offsets.s.section = sectp;
fd361982 11736 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11737 }
11738 else
11739 {
11740 /* No other kind of section is valid. */
11741 return 0;
11742 }
11743
11744 return 1;
11745}
11746
73869dc2
DE
11747/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11748 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11749 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11750 This is for DWP version 1 files. */
80626a55
DE
11751
11752static struct dwo_unit *
ed2dc618
SM
11753create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11754 struct dwp_file *dwp_file,
73869dc2
DE
11755 uint32_t unit_index,
11756 const char *comp_dir,
11757 ULONGEST signature, int is_debug_types)
80626a55 11758{
73869dc2
DE
11759 const struct dwp_hash_table *dwp_htab =
11760 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11761 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11762 const char *kind = is_debug_types ? "TU" : "CU";
11763 struct dwo_file *dwo_file;
11764 struct dwo_unit *dwo_unit;
73869dc2 11765 struct virtual_v1_dwo_sections sections;
80626a55 11766 void **dwo_file_slot;
80626a55
DE
11767 int i;
11768
73869dc2
DE
11769 gdb_assert (dwp_file->version == 1);
11770
b4f54984 11771 if (dwarf_read_debug)
80626a55 11772 {
73869dc2 11773 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11774 kind,
73869dc2 11775 pulongest (unit_index), hex_string (signature),
80626a55
DE
11776 dwp_file->name);
11777 }
11778
19ac8c2e 11779 /* Fetch the sections of this DWO unit.
80626a55
DE
11780 Put a limit on the number of sections we look for so that bad data
11781 doesn't cause us to loop forever. */
11782
73869dc2 11783#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11784 (1 /* .debug_info or .debug_types */ \
11785 + 1 /* .debug_abbrev */ \
11786 + 1 /* .debug_line */ \
11787 + 1 /* .debug_loc */ \
11788 + 1 /* .debug_str_offsets */ \
19ac8c2e 11789 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11790 + 1 /* trailing zero */)
11791
11792 memset (&sections, 0, sizeof (sections));
80626a55 11793
73869dc2 11794 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11795 {
11796 asection *sectp;
11797 uint32_t section_nr =
11798 read_4_bytes (dbfd,
73869dc2
DE
11799 dwp_htab->section_pool.v1.indices
11800 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11801
11802 if (section_nr == 0)
11803 break;
11804 if (section_nr >= dwp_file->num_sections)
11805 {
11806 error (_("Dwarf Error: bad DWP hash table, section number too large"
11807 " [in module %s]"),
11808 dwp_file->name);
11809 }
11810
11811 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11812 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11815 " [in module %s]"),
11816 dwp_file->name);
11817 }
11818 }
11819
11820 if (i < 2
96b79293
TT
11821 || sections.info_or_types.empty ()
11822 || sections.abbrev.empty ())
80626a55
DE
11823 {
11824 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11825 " [in module %s]"),
11826 dwp_file->name);
11827 }
73869dc2 11828 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11831 " [in module %s]"),
11832 dwp_file->name);
11833 }
11834
11835 /* It's easier for the rest of the code if we fake a struct dwo_file and
11836 have dwo_unit "live" in that. At least for now.
11837
11838 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11839 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11840 file, we can combine them back into a virtual DWO file to save space
11841 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11842 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11843
791afaa2
TT
11844 std::string virtual_dwo_name =
11845 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11846 sections.abbrev.get_id (),
11847 sections.line.get_id (),
11848 sections.loc.get_id (),
11849 sections.str_offsets.get_id ());
80626a55 11850 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11851 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11852 virtual_dwo_name.c_str (),
11853 comp_dir);
80626a55
DE
11854 /* Create one if necessary. */
11855 if (*dwo_file_slot == NULL)
11856 {
b4f54984 11857 if (dwarf_read_debug)
80626a55
DE
11858 {
11859 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11860 virtual_dwo_name.c_str ());
80626a55 11861 }
51ac9db5 11862 dwo_file = new struct dwo_file;
45940949 11863 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11864 dwo_file->comp_dir = comp_dir;
80626a55
DE
11865 dwo_file->sections.abbrev = sections.abbrev;
11866 dwo_file->sections.line = sections.line;
11867 dwo_file->sections.loc = sections.loc;
11868 dwo_file->sections.macinfo = sections.macinfo;
11869 dwo_file->sections.macro = sections.macro;
11870 dwo_file->sections.str_offsets = sections.str_offsets;
11871 /* The "str" section is global to the entire DWP file. */
11872 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11873 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11874 there's no need to record it in dwo_file.
11875 Also, we can't simply record type sections in dwo_file because
11876 we record a pointer into the vector in dwo_unit. As we collect more
11877 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11878 for it, invalidating all copies of pointers into the previous
11879 contents. */
80626a55
DE
11880 *dwo_file_slot = dwo_file;
11881 }
11882 else
11883 {
b4f54984 11884 if (dwarf_read_debug)
80626a55
DE
11885 {
11886 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11887 virtual_dwo_name.c_str ());
80626a55 11888 }
9a3c8263 11889 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11890 }
80626a55 11891
5989a64e 11892 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11893 dwo_unit->dwo_file = dwo_file;
11894 dwo_unit->signature = signature;
8d749320 11895 dwo_unit->section =
5989a64e 11896 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11897 *dwo_unit->section = sections.info_or_types;
57d63ce2 11898 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11899
11900 return dwo_unit;
11901}
11902
73869dc2
DE
11903/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11904 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11905 piece within that section used by a TU/CU, return a virtual section
11906 of just that piece. */
11907
11908static struct dwarf2_section_info
ed2dc618
SM
11909create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11910 struct dwarf2_section_info *section,
73869dc2
DE
11911 bfd_size_type offset, bfd_size_type size)
11912{
11913 struct dwarf2_section_info result;
11914 asection *sectp;
11915
11916 gdb_assert (section != NULL);
11917 gdb_assert (!section->is_virtual);
11918
11919 memset (&result, 0, sizeof (result));
11920 result.s.containing_section = section;
dc4ccb6f 11921 result.is_virtual = true;
73869dc2
DE
11922
11923 if (size == 0)
11924 return result;
11925
96b79293 11926 sectp = section->get_bfd_section ();
73869dc2
DE
11927
11928 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11929 bounds of the real section. This is a pretty-rare event, so just
11930 flag an error (easier) instead of a warning and trying to cope. */
11931 if (sectp == NULL
fd361982 11932 || offset + size > bfd_section_size (sectp))
73869dc2 11933 {
73869dc2
DE
11934 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11935 " in section %s [in module %s]"),
fd361982 11936 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11937 objfile_name (dwarf2_per_objfile->objfile));
11938 }
11939
11940 result.virtual_offset = offset;
11941 result.size = size;
11942 return result;
11943}
11944
11945/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11946 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11947 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11948 This is for DWP version 2 files. */
11949
11950static struct dwo_unit *
ed2dc618
SM
11951create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11952 struct dwp_file *dwp_file,
73869dc2
DE
11953 uint32_t unit_index,
11954 const char *comp_dir,
11955 ULONGEST signature, int is_debug_types)
11956{
73869dc2
DE
11957 const struct dwp_hash_table *dwp_htab =
11958 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11959 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11960 const char *kind = is_debug_types ? "TU" : "CU";
11961 struct dwo_file *dwo_file;
11962 struct dwo_unit *dwo_unit;
11963 struct virtual_v2_dwo_sections sections;
11964 void **dwo_file_slot;
73869dc2
DE
11965 int i;
11966
11967 gdb_assert (dwp_file->version == 2);
11968
b4f54984 11969 if (dwarf_read_debug)
73869dc2
DE
11970 {
11971 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11972 kind,
11973 pulongest (unit_index), hex_string (signature),
11974 dwp_file->name);
11975 }
11976
11977 /* Fetch the section offsets of this DWO unit. */
11978
11979 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11980
11981 for (i = 0; i < dwp_htab->nr_columns; ++i)
11982 {
11983 uint32_t offset = read_4_bytes (dbfd,
11984 dwp_htab->section_pool.v2.offsets
11985 + (((unit_index - 1) * dwp_htab->nr_columns
11986 + i)
11987 * sizeof (uint32_t)));
11988 uint32_t size = read_4_bytes (dbfd,
11989 dwp_htab->section_pool.v2.sizes
11990 + (((unit_index - 1) * dwp_htab->nr_columns
11991 + i)
11992 * sizeof (uint32_t)));
11993
11994 switch (dwp_htab->section_pool.v2.section_ids[i])
11995 {
11996 case DW_SECT_INFO:
11997 case DW_SECT_TYPES:
11998 sections.info_or_types_offset = offset;
11999 sections.info_or_types_size = size;
12000 break;
12001 case DW_SECT_ABBREV:
12002 sections.abbrev_offset = offset;
12003 sections.abbrev_size = size;
12004 break;
12005 case DW_SECT_LINE:
12006 sections.line_offset = offset;
12007 sections.line_size = size;
12008 break;
12009 case DW_SECT_LOC:
12010 sections.loc_offset = offset;
12011 sections.loc_size = size;
12012 break;
12013 case DW_SECT_STR_OFFSETS:
12014 sections.str_offsets_offset = offset;
12015 sections.str_offsets_size = size;
12016 break;
12017 case DW_SECT_MACINFO:
12018 sections.macinfo_offset = offset;
12019 sections.macinfo_size = size;
12020 break;
12021 case DW_SECT_MACRO:
12022 sections.macro_offset = offset;
12023 sections.macro_size = size;
12024 break;
12025 }
12026 }
12027
12028 /* It's easier for the rest of the code if we fake a struct dwo_file and
12029 have dwo_unit "live" in that. At least for now.
12030
12031 The DWP file can be made up of a random collection of CUs and TUs.
12032 However, for each CU + set of TUs that came from the same original DWO
12033 file, we can combine them back into a virtual DWO file to save space
12034 (fewer struct dwo_file objects to allocate). Remember that for really
12035 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12036
791afaa2
TT
12037 std::string virtual_dwo_name =
12038 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12039 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12040 (long) (sections.line_size ? sections.line_offset : 0),
12041 (long) (sections.loc_size ? sections.loc_offset : 0),
12042 (long) (sections.str_offsets_size
12043 ? sections.str_offsets_offset : 0));
73869dc2 12044 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12045 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12046 virtual_dwo_name.c_str (),
12047 comp_dir);
73869dc2
DE
12048 /* Create one if necessary. */
12049 if (*dwo_file_slot == NULL)
12050 {
b4f54984 12051 if (dwarf_read_debug)
73869dc2
DE
12052 {
12053 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12054 virtual_dwo_name.c_str ());
73869dc2 12055 }
51ac9db5 12056 dwo_file = new struct dwo_file;
45940949 12057 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12058 dwo_file->comp_dir = comp_dir;
12059 dwo_file->sections.abbrev =
ed2dc618 12060 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12061 sections.abbrev_offset, sections.abbrev_size);
12062 dwo_file->sections.line =
ed2dc618 12063 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12064 sections.line_offset, sections.line_size);
12065 dwo_file->sections.loc =
ed2dc618 12066 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12067 sections.loc_offset, sections.loc_size);
12068 dwo_file->sections.macinfo =
ed2dc618 12069 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12070 sections.macinfo_offset, sections.macinfo_size);
12071 dwo_file->sections.macro =
ed2dc618 12072 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12073 sections.macro_offset, sections.macro_size);
12074 dwo_file->sections.str_offsets =
ed2dc618
SM
12075 create_dwp_v2_section (dwarf2_per_objfile,
12076 &dwp_file->sections.str_offsets,
73869dc2
DE
12077 sections.str_offsets_offset,
12078 sections.str_offsets_size);
12079 /* The "str" section is global to the entire DWP file. */
12080 dwo_file->sections.str = dwp_file->sections.str;
12081 /* The info or types section is assigned below to dwo_unit,
12082 there's no need to record it in dwo_file.
12083 Also, we can't simply record type sections in dwo_file because
12084 we record a pointer into the vector in dwo_unit. As we collect more
12085 types we'll grow the vector and eventually have to reallocate space
12086 for it, invalidating all copies of pointers into the previous
12087 contents. */
12088 *dwo_file_slot = dwo_file;
12089 }
12090 else
12091 {
b4f54984 12092 if (dwarf_read_debug)
73869dc2
DE
12093 {
12094 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12095 virtual_dwo_name.c_str ());
73869dc2 12096 }
9a3c8263 12097 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12098 }
73869dc2 12099
5989a64e 12100 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12101 dwo_unit->dwo_file = dwo_file;
12102 dwo_unit->signature = signature;
8d749320 12103 dwo_unit->section =
5989a64e 12104 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12105 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12106 is_debug_types
73869dc2
DE
12107 ? &dwp_file->sections.types
12108 : &dwp_file->sections.info,
12109 sections.info_or_types_offset,
12110 sections.info_or_types_size);
12111 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12112
12113 return dwo_unit;
12114}
12115
57d63ce2
DE
12116/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12117 Returns NULL if the signature isn't found. */
80626a55
DE
12118
12119static struct dwo_unit *
ed2dc618
SM
12120lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12121 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12122 ULONGEST signature, int is_debug_types)
80626a55 12123{
57d63ce2
DE
12124 const struct dwp_hash_table *dwp_htab =
12125 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12126 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12127 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12128 uint32_t hash = signature & mask;
12129 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12130 unsigned int i;
12131 void **slot;
870f88f7 12132 struct dwo_unit find_dwo_cu;
80626a55
DE
12133
12134 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12135 find_dwo_cu.signature = signature;
19ac8c2e 12136 slot = htab_find_slot (is_debug_types
48b490f2
TT
12137 ? dwp_file->loaded_tus.get ()
12138 : dwp_file->loaded_cus.get (),
19ac8c2e 12139 &find_dwo_cu, INSERT);
80626a55
DE
12140
12141 if (*slot != NULL)
9a3c8263 12142 return (struct dwo_unit *) *slot;
80626a55
DE
12143
12144 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12145 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12146 {
12147 ULONGEST signature_in_table;
12148
12149 signature_in_table =
57d63ce2 12150 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12151 if (signature_in_table == signature)
12152 {
57d63ce2
DE
12153 uint32_t unit_index =
12154 read_4_bytes (dbfd,
12155 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12156
73869dc2
DE
12157 if (dwp_file->version == 1)
12158 {
ed2dc618
SM
12159 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12160 dwp_file, unit_index,
73869dc2
DE
12161 comp_dir, signature,
12162 is_debug_types);
12163 }
12164 else
12165 {
ed2dc618
SM
12166 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12167 dwp_file, unit_index,
73869dc2
DE
12168 comp_dir, signature,
12169 is_debug_types);
12170 }
9a3c8263 12171 return (struct dwo_unit *) *slot;
80626a55
DE
12172 }
12173 if (signature_in_table == 0)
12174 return NULL;
12175 hash = (hash + hash2) & mask;
12176 }
12177
12178 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12179 " [in module %s]"),
12180 dwp_file->name);
12181}
12182
ab5088bf 12183/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12184 Open the file specified by FILE_NAME and hand it off to BFD for
12185 preliminary analysis. Return a newly initialized bfd *, which
12186 includes a canonicalized copy of FILE_NAME.
80626a55 12187 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12188 SEARCH_CWD is true if the current directory is to be searched.
12189 It will be searched before debug-file-directory.
13aaf454
DE
12190 If successful, the file is added to the bfd include table of the
12191 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12192 If unable to find/open the file, return NULL.
3019eac3
DE
12193 NOTE: This function is derived from symfile_bfd_open. */
12194
192b62ce 12195static gdb_bfd_ref_ptr
ed2dc618
SM
12196try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12197 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12198{
24b9144d 12199 int desc;
9c02c129
DE
12200 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12201 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12202 to debug_file_directory. */
e0cc99a6 12203 const char *search_path;
9c02c129
DE
12204 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12205
e0cc99a6 12206 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12207 if (search_cwd)
12208 {
12209 if (*debug_file_directory != '\0')
e0cc99a6
TT
12210 {
12211 search_path_holder.reset (concat (".", dirname_separator_string,
12212 debug_file_directory,
12213 (char *) NULL));
12214 search_path = search_path_holder.get ();
12215 }
6ac97d4c 12216 else
e0cc99a6 12217 search_path = ".";
6ac97d4c 12218 }
9c02c129 12219 else
e0cc99a6 12220 search_path = debug_file_directory;
3019eac3 12221
24b9144d 12222 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12223 if (is_dwp)
12224 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12225
12226 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12227 desc = openp (search_path, flags, file_name,
3019eac3
DE
12228 O_RDONLY | O_BINARY, &absolute_name);
12229 if (desc < 0)
12230 return NULL;
12231
e0cc99a6
TT
12232 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12233 gnutarget, desc));
9c02c129
DE
12234 if (sym_bfd == NULL)
12235 return NULL;
192b62ce 12236 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12237
192b62ce
TT
12238 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12239 return NULL;
3019eac3 12240
13aaf454
DE
12241 /* Success. Record the bfd as having been included by the objfile's bfd.
12242 This is important because things like demangled_names_hash lives in the
12243 objfile's per_bfd space and may have references to things like symbol
12244 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12245 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12246
3019eac3
DE
12247 return sym_bfd;
12248}
12249
ab5088bf 12250/* Try to open DWO file FILE_NAME.
3019eac3
DE
12251 COMP_DIR is the DW_AT_comp_dir attribute.
12252 The result is the bfd handle of the file.
12253 If there is a problem finding or opening the file, return NULL.
12254 Upon success, the canonicalized path of the file is stored in the bfd,
12255 same as symfile_bfd_open. */
12256
192b62ce 12257static gdb_bfd_ref_ptr
ed2dc618
SM
12258open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12259 const char *file_name, const char *comp_dir)
3019eac3 12260{
80626a55 12261 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12262 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12263 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12264
12265 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12266
12267 if (comp_dir != NULL)
12268 {
43816ebc
TT
12269 gdb::unique_xmalloc_ptr<char> path_to_try
12270 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12271
12272 /* NOTE: If comp_dir is a relative path, this will also try the
12273 search path, which seems useful. */
ed2dc618 12274 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12275 path_to_try.get (),
ed2dc618 12276 0 /*is_dwp*/,
192b62ce 12277 1 /*search_cwd*/));
3019eac3
DE
12278 if (abfd != NULL)
12279 return abfd;
12280 }
12281
12282 /* That didn't work, try debug-file-directory, which, despite its name,
12283 is a list of paths. */
12284
12285 if (*debug_file_directory == '\0')
12286 return NULL;
12287
ed2dc618
SM
12288 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12289 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12290}
12291
80626a55
DE
12292/* This function is mapped across the sections and remembers the offset and
12293 size of each of the DWO debugging sections we are interested in. */
12294
12295static void
12296dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12297{
9a3c8263 12298 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12299 const struct dwop_section_names *names = &dwop_section_names;
12300
12301 if (section_is_p (sectp->name, &names->abbrev_dwo))
12302 {
049412e3 12303 dwo_sections->abbrev.s.section = sectp;
fd361982 12304 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12305 }
12306 else if (section_is_p (sectp->name, &names->info_dwo))
12307 {
049412e3 12308 dwo_sections->info.s.section = sectp;
fd361982 12309 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12310 }
12311 else if (section_is_p (sectp->name, &names->line_dwo))
12312 {
049412e3 12313 dwo_sections->line.s.section = sectp;
fd361982 12314 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12315 }
12316 else if (section_is_p (sectp->name, &names->loc_dwo))
12317 {
049412e3 12318 dwo_sections->loc.s.section = sectp;
fd361982 12319 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12320 }
41144253 12321 else if (section_is_p (sectp->name, &names->loclists_dwo))
12322 {
12323 dwo_sections->loclists.s.section = sectp;
12324 dwo_sections->loclists.size = bfd_section_size (sectp);
12325 }
80626a55
DE
12326 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12327 {
049412e3 12328 dwo_sections->macinfo.s.section = sectp;
fd361982 12329 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12330 }
12331 else if (section_is_p (sectp->name, &names->macro_dwo))
12332 {
049412e3 12333 dwo_sections->macro.s.section = sectp;
fd361982 12334 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12335 }
12336 else if (section_is_p (sectp->name, &names->str_dwo))
12337 {
049412e3 12338 dwo_sections->str.s.section = sectp;
fd361982 12339 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12340 }
12341 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12342 {
049412e3 12343 dwo_sections->str_offsets.s.section = sectp;
fd361982 12344 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12345 }
12346 else if (section_is_p (sectp->name, &names->types_dwo))
12347 {
12348 struct dwarf2_section_info type_section;
12349
12350 memset (&type_section, 0, sizeof (type_section));
049412e3 12351 type_section.s.section = sectp;
fd361982 12352 type_section.size = bfd_section_size (sectp);
fd5866f6 12353 dwo_sections->types.push_back (type_section);
80626a55
DE
12354 }
12355}
12356
ab5088bf 12357/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12358 by PER_CU. This is for the non-DWP case.
80626a55 12359 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12360
12361static struct dwo_file *
0ac5b59e
DE
12362open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12363 const char *dwo_name, const char *comp_dir)
3019eac3 12364{
ed2dc618 12365 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12366
fb1eb2f9 12367 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12368 if (dbfd == NULL)
12369 {
b4f54984 12370 if (dwarf_read_debug)
80626a55
DE
12371 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12372 return NULL;
12373 }
263db9a1 12374
51ac9db5 12375 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12376 dwo_file->dwo_name = dwo_name;
12377 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12378 dwo_file->dbfd = std::move (dbfd);
3019eac3 12379
fb1eb2f9 12380 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12381 &dwo_file->sections);
3019eac3 12382
18a8505e
AT
12383 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12384 dwo_file->sections.info, dwo_file->cus);
3019eac3 12385
263db9a1 12386 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12387 dwo_file->sections.types, dwo_file->tus);
3019eac3 12388
b4f54984 12389 if (dwarf_read_debug)
80626a55
DE
12390 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12391
263db9a1 12392 return dwo_file.release ();
3019eac3
DE
12393}
12394
80626a55 12395/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12396 size of each of the DWP debugging sections common to version 1 and 2 that
12397 we are interested in. */
3019eac3 12398
80626a55 12399static void
73869dc2
DE
12400dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12401 void *dwp_file_ptr)
3019eac3 12402{
9a3c8263 12403 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12404 const struct dwop_section_names *names = &dwop_section_names;
12405 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12406
80626a55 12407 /* Record the ELF section number for later lookup: this is what the
73869dc2 12408 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12409 gdb_assert (elf_section_nr < dwp_file->num_sections);
12410 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12411
80626a55
DE
12412 /* Look for specific sections that we need. */
12413 if (section_is_p (sectp->name, &names->str_dwo))
12414 {
049412e3 12415 dwp_file->sections.str.s.section = sectp;
fd361982 12416 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12417 }
12418 else if (section_is_p (sectp->name, &names->cu_index))
12419 {
049412e3 12420 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12421 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12422 }
12423 else if (section_is_p (sectp->name, &names->tu_index))
12424 {
049412e3 12425 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12426 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12427 }
12428}
3019eac3 12429
73869dc2
DE
12430/* This function is mapped across the sections and remembers the offset and
12431 size of each of the DWP version 2 debugging sections that we are interested
12432 in. This is split into a separate function because we don't know if we
12433 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12434
12435static void
12436dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12437{
9a3c8263 12438 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12439 const struct dwop_section_names *names = &dwop_section_names;
12440 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12441
12442 /* Record the ELF section number for later lookup: this is what the
12443 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12444 gdb_assert (elf_section_nr < dwp_file->num_sections);
12445 dwp_file->elf_sections[elf_section_nr] = sectp;
12446
12447 /* Look for specific sections that we need. */
12448 if (section_is_p (sectp->name, &names->abbrev_dwo))
12449 {
049412e3 12450 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12451 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12452 }
12453 else if (section_is_p (sectp->name, &names->info_dwo))
12454 {
049412e3 12455 dwp_file->sections.info.s.section = sectp;
fd361982 12456 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12457 }
12458 else if (section_is_p (sectp->name, &names->line_dwo))
12459 {
049412e3 12460 dwp_file->sections.line.s.section = sectp;
fd361982 12461 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12462 }
12463 else if (section_is_p (sectp->name, &names->loc_dwo))
12464 {
049412e3 12465 dwp_file->sections.loc.s.section = sectp;
fd361982 12466 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12467 }
12468 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12469 {
049412e3 12470 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12471 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12472 }
12473 else if (section_is_p (sectp->name, &names->macro_dwo))
12474 {
049412e3 12475 dwp_file->sections.macro.s.section = sectp;
fd361982 12476 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12477 }
12478 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12479 {
049412e3 12480 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12481 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12482 }
12483 else if (section_is_p (sectp->name, &names->types_dwo))
12484 {
049412e3 12485 dwp_file->sections.types.s.section = sectp;
fd361982 12486 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12487 }
12488}
12489
80626a55 12490/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12491
80626a55
DE
12492static hashval_t
12493hash_dwp_loaded_cutus (const void *item)
12494{
9a3c8263 12495 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12496
80626a55
DE
12497 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12498 return dwo_unit->signature;
3019eac3
DE
12499}
12500
80626a55 12501/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12502
80626a55
DE
12503static int
12504eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12505{
9a3c8263
SM
12506 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12507 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12508
80626a55
DE
12509 return dua->signature == dub->signature;
12510}
3019eac3 12511
80626a55 12512/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12513
48b490f2 12514static htab_up
298e9637 12515allocate_dwp_loaded_cutus_table ()
80626a55 12516{
48b490f2
TT
12517 return htab_up (htab_create_alloc (3,
12518 hash_dwp_loaded_cutus,
12519 eq_dwp_loaded_cutus,
12520 NULL, xcalloc, xfree));
80626a55 12521}
3019eac3 12522
ab5088bf
DE
12523/* Try to open DWP file FILE_NAME.
12524 The result is the bfd handle of the file.
12525 If there is a problem finding or opening the file, return NULL.
12526 Upon success, the canonicalized path of the file is stored in the bfd,
12527 same as symfile_bfd_open. */
12528
192b62ce 12529static gdb_bfd_ref_ptr
ed2dc618
SM
12530open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12531 const char *file_name)
ab5088bf 12532{
ed2dc618
SM
12533 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12534 1 /*is_dwp*/,
192b62ce 12535 1 /*search_cwd*/));
6ac97d4c
DE
12536 if (abfd != NULL)
12537 return abfd;
12538
12539 /* Work around upstream bug 15652.
12540 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12541 [Whether that's a "bug" is debatable, but it is getting in our way.]
12542 We have no real idea where the dwp file is, because gdb's realpath-ing
12543 of the executable's path may have discarded the needed info.
12544 [IWBN if the dwp file name was recorded in the executable, akin to
12545 .gnu_debuglink, but that doesn't exist yet.]
12546 Strip the directory from FILE_NAME and search again. */
12547 if (*debug_file_directory != '\0')
12548 {
12549 /* Don't implicitly search the current directory here.
12550 If the user wants to search "." to handle this case,
12551 it must be added to debug-file-directory. */
ed2dc618
SM
12552 return try_open_dwop_file (dwarf2_per_objfile,
12553 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12554 0 /*search_cwd*/);
12555 }
12556
12557 return NULL;
ab5088bf
DE
12558}
12559
80626a55
DE
12560/* Initialize the use of the DWP file for the current objfile.
12561 By convention the name of the DWP file is ${objfile}.dwp.
12562 The result is NULL if it can't be found. */
a766d390 12563
400174b1 12564static std::unique_ptr<struct dwp_file>
ed2dc618 12565open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12566{
12567 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12568
82bf32bc
JK
12569 /* Try to find first .dwp for the binary file before any symbolic links
12570 resolving. */
6c447423
DE
12571
12572 /* If the objfile is a debug file, find the name of the real binary
12573 file and get the name of dwp file from there. */
d721ba37 12574 std::string dwp_name;
6c447423
DE
12575 if (objfile->separate_debug_objfile_backlink != NULL)
12576 {
12577 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12578 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12579
d721ba37 12580 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12581 }
12582 else
d721ba37
PA
12583 dwp_name = objfile->original_name;
12584
12585 dwp_name += ".dwp";
80626a55 12586
ed2dc618 12587 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12588 if (dbfd == NULL
12589 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12590 {
12591 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12592 dwp_name = objfile_name (objfile);
12593 dwp_name += ".dwp";
ed2dc618 12594 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12595 }
12596
80626a55
DE
12597 if (dbfd == NULL)
12598 {
b4f54984 12599 if (dwarf_read_debug)
d721ba37 12600 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12601 return std::unique_ptr<dwp_file> ();
3019eac3 12602 }
400174b1
TT
12603
12604 const char *name = bfd_get_filename (dbfd.get ());
12605 std::unique_ptr<struct dwp_file> dwp_file
12606 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12607
0a0f4c01 12608 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12609 dwp_file->elf_sections =
5989a64e 12610 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12611 dwp_file->num_sections, asection *);
12612
400174b1
TT
12613 bfd_map_over_sections (dwp_file->dbfd.get (),
12614 dwarf2_locate_common_dwp_sections,
12615 dwp_file.get ());
80626a55 12616
400174b1
TT
12617 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12618 0);
80626a55 12619
400174b1
TT
12620 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12621 1);
80626a55 12622
73869dc2 12623 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12624 if (dwp_file->cus && dwp_file->tus
12625 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12626 {
12627 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12628 pretty bizarre. We use pulongest here because that's the established
4d65956b 12629 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12630 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12631 " TU version %s [in DWP file %s]"),
12632 pulongest (dwp_file->cus->version),
d721ba37 12633 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12634 }
08302ed2
DE
12635
12636 if (dwp_file->cus)
12637 dwp_file->version = dwp_file->cus->version;
12638 else if (dwp_file->tus)
12639 dwp_file->version = dwp_file->tus->version;
12640 else
12641 dwp_file->version = 2;
73869dc2
DE
12642
12643 if (dwp_file->version == 2)
400174b1
TT
12644 bfd_map_over_sections (dwp_file->dbfd.get (),
12645 dwarf2_locate_v2_dwp_sections,
12646 dwp_file.get ());
73869dc2 12647
298e9637
SM
12648 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12649 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12650
b4f54984 12651 if (dwarf_read_debug)
80626a55
DE
12652 {
12653 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12654 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12655 " %s CUs, %s TUs\n",
12656 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12657 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12658 }
12659
12660 return dwp_file;
3019eac3 12661}
c906108c 12662
ab5088bf
DE
12663/* Wrapper around open_and_init_dwp_file, only open it once. */
12664
12665static struct dwp_file *
ed2dc618 12666get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12667{
5989a64e 12668 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12669 {
5989a64e 12670 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12671 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12672 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12673 }
5989a64e 12674 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12675}
12676
80626a55
DE
12677/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12678 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12679 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12680 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12681 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12682
12683 This is called, for example, when wanting to read a variable with a
12684 complex location. Therefore we don't want to do file i/o for every call.
12685 Therefore we don't want to look for a DWO file on every call.
12686 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12687 then we check if we've already seen DWO_NAME, and only THEN do we check
12688 for a DWO file.
12689
1c658ad5 12690 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12691 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12692
3019eac3 12693static struct dwo_unit *
80626a55
DE
12694lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12695 const char *dwo_name, const char *comp_dir,
12696 ULONGEST signature, int is_debug_types)
3019eac3 12697{
ed2dc618 12698 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12699 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12700 const char *kind = is_debug_types ? "TU" : "CU";
12701 void **dwo_file_slot;
3019eac3 12702 struct dwo_file *dwo_file;
80626a55 12703 struct dwp_file *dwp_file;
cb1df416 12704
6a506a2d
DE
12705 /* First see if there's a DWP file.
12706 If we have a DWP file but didn't find the DWO inside it, don't
12707 look for the original DWO file. It makes gdb behave differently
12708 depending on whether one is debugging in the build tree. */
cf2c3c16 12709
ed2dc618 12710 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12711 if (dwp_file != NULL)
cf2c3c16 12712 {
80626a55
DE
12713 const struct dwp_hash_table *dwp_htab =
12714 is_debug_types ? dwp_file->tus : dwp_file->cus;
12715
12716 if (dwp_htab != NULL)
12717 {
12718 struct dwo_unit *dwo_cutu =
ed2dc618 12719 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12720 signature, is_debug_types);
80626a55
DE
12721
12722 if (dwo_cutu != NULL)
12723 {
b4f54984 12724 if (dwarf_read_debug)
80626a55
DE
12725 {
12726 fprintf_unfiltered (gdb_stdlog,
12727 "Virtual DWO %s %s found: @%s\n",
12728 kind, hex_string (signature),
12729 host_address_to_string (dwo_cutu));
12730 }
12731 return dwo_cutu;
12732 }
12733 }
12734 }
6a506a2d 12735 else
80626a55 12736 {
6a506a2d 12737 /* No DWP file, look for the DWO file. */
80626a55 12738
ed2dc618
SM
12739 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12740 dwo_name, comp_dir);
6a506a2d 12741 if (*dwo_file_slot == NULL)
80626a55 12742 {
6a506a2d
DE
12743 /* Read in the file and build a table of the CUs/TUs it contains. */
12744 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12745 }
6a506a2d 12746 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12747 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12748
6a506a2d 12749 if (dwo_file != NULL)
19c3d4c9 12750 {
6a506a2d
DE
12751 struct dwo_unit *dwo_cutu = NULL;
12752
12753 if (is_debug_types && dwo_file->tus)
12754 {
12755 struct dwo_unit find_dwo_cutu;
12756
12757 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12758 find_dwo_cutu.signature = signature;
9a3c8263 12759 dwo_cutu
b0b6a987
TT
12760 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12761 &find_dwo_cutu);
6a506a2d 12762 }
33c5cd75 12763 else if (!is_debug_types && dwo_file->cus)
80626a55 12764 {
33c5cd75
DB
12765 struct dwo_unit find_dwo_cutu;
12766
12767 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12768 find_dwo_cutu.signature = signature;
b0b6a987 12769 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12770 &find_dwo_cutu);
6a506a2d
DE
12771 }
12772
12773 if (dwo_cutu != NULL)
12774 {
b4f54984 12775 if (dwarf_read_debug)
6a506a2d
DE
12776 {
12777 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12778 kind, dwo_name, hex_string (signature),
12779 host_address_to_string (dwo_cutu));
12780 }
12781 return dwo_cutu;
80626a55
DE
12782 }
12783 }
2e276125 12784 }
9cdd5dbd 12785
80626a55
DE
12786 /* We didn't find it. This could mean a dwo_id mismatch, or
12787 someone deleted the DWO/DWP file, or the search path isn't set up
12788 correctly to find the file. */
12789
b4f54984 12790 if (dwarf_read_debug)
80626a55
DE
12791 {
12792 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12793 kind, dwo_name, hex_string (signature));
12794 }
3019eac3 12795
6656a72d
DE
12796 /* This is a warning and not a complaint because it can be caused by
12797 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12798 {
12799 /* Print the name of the DWP file if we looked there, helps the user
12800 better diagnose the problem. */
791afaa2 12801 std::string dwp_text;
43942612
DE
12802
12803 if (dwp_file != NULL)
791afaa2
TT
12804 dwp_text = string_printf (" [in DWP file %s]",
12805 lbasename (dwp_file->name));
43942612 12806
9d8780f0 12807 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12808 " [in module %s]"),
12809 kind, dwo_name, hex_string (signature),
791afaa2 12810 dwp_text.c_str (),
43942612 12811 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12812 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12813 }
3019eac3 12814 return NULL;
5fb290d7
DJ
12815}
12816
80626a55
DE
12817/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12818 See lookup_dwo_cutu_unit for details. */
12819
12820static struct dwo_unit *
12821lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12822 const char *dwo_name, const char *comp_dir,
12823 ULONGEST signature)
12824{
12825 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12826}
12827
12828/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12829 See lookup_dwo_cutu_unit for details. */
12830
12831static struct dwo_unit *
12832lookup_dwo_type_unit (struct signatured_type *this_tu,
12833 const char *dwo_name, const char *comp_dir)
12834{
12835 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12836}
12837
89e63ee4
DE
12838/* Traversal function for queue_and_load_all_dwo_tus. */
12839
12840static int
12841queue_and_load_dwo_tu (void **slot, void *info)
12842{
12843 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12844 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12845 ULONGEST signature = dwo_unit->signature;
d460f660 12846 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12847
12848 if (sig_type != NULL)
12849 {
12850 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12851
12852 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12853 a real dependency of PER_CU on SIG_TYPE. That is detected later
12854 while processing PER_CU. */
d460f660
SM
12855 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12856 load_full_type_unit (sig_cu, cu->per_objfile);
12857 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12858 }
12859
12860 return 1;
12861}
12862
12863/* Queue all TUs contained in the DWO of PER_CU to be read in.
12864 The DWO may have the only definition of the type, though it may not be
12865 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12866 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12867
12868static void
12869queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12870{
12871 struct dwo_unit *dwo_unit;
12872 struct dwo_file *dwo_file;
12873
12874 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12875 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12876 gdb_assert (per_cu->cu != NULL);
12877
12878 dwo_unit = per_cu->cu->dwo_unit;
12879 gdb_assert (dwo_unit != NULL);
12880
12881 dwo_file = dwo_unit->dwo_file;
12882 if (dwo_file->tus != NULL)
b0b6a987 12883 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12884 per_cu->cu);
89e63ee4
DE
12885}
12886
3019eac3 12887/* Read in various DIEs. */
348e048f 12888
d389af10 12889/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12890 Inherit only the children of the DW_AT_abstract_origin DIE not being
12891 already referenced by DW_AT_abstract_origin from the children of the
12892 current DIE. */
d389af10
JK
12893
12894static void
12895inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12896{
12897 struct die_info *child_die;
791afaa2 12898 sect_offset *offsetp;
d389af10
JK
12899 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12900 struct die_info *origin_die;
12901 /* Iterator of the ORIGIN_DIE children. */
12902 struct die_info *origin_child_die;
d389af10 12903 struct attribute *attr;
cd02d79d
PA
12904 struct dwarf2_cu *origin_cu;
12905 struct pending **origin_previous_list_in_scope;
d389af10
JK
12906
12907 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12908 if (!attr)
12909 return;
12910
cd02d79d
PA
12911 /* Note that following die references may follow to a die in a
12912 different cu. */
12913
12914 origin_cu = cu;
12915 origin_die = follow_die_ref (die, attr, &origin_cu);
12916
12917 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12918 symbols in. */
12919 origin_previous_list_in_scope = origin_cu->list_in_scope;
12920 origin_cu->list_in_scope = cu->list_in_scope;
12921
edb3359d
DJ
12922 if (die->tag != origin_die->tag
12923 && !(die->tag == DW_TAG_inlined_subroutine
12924 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12925 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12926 sect_offset_str (die->sect_off),
12927 sect_offset_str (origin_die->sect_off));
d389af10 12928
791afaa2 12929 std::vector<sect_offset> offsets;
d389af10 12930
3ea89b92
PMR
12931 for (child_die = die->child;
12932 child_die && child_die->tag;
436c571c 12933 child_die = child_die->sibling)
3ea89b92
PMR
12934 {
12935 struct die_info *child_origin_die;
12936 struct dwarf2_cu *child_origin_cu;
12937
12938 /* We are trying to process concrete instance entries:
216f72a1 12939 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12940 it's not relevant to our analysis here. i.e. detecting DIEs that are
12941 present in the abstract instance but not referenced in the concrete
12942 one. */
216f72a1
JK
12943 if (child_die->tag == DW_TAG_call_site
12944 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12945 continue;
12946
c38f313d
DJ
12947 /* For each CHILD_DIE, find the corresponding child of
12948 ORIGIN_DIE. If there is more than one layer of
12949 DW_AT_abstract_origin, follow them all; there shouldn't be,
12950 but GCC versions at least through 4.4 generate this (GCC PR
12951 40573). */
3ea89b92
PMR
12952 child_origin_die = child_die;
12953 child_origin_cu = cu;
c38f313d
DJ
12954 while (1)
12955 {
cd02d79d
PA
12956 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12957 child_origin_cu);
c38f313d
DJ
12958 if (attr == NULL)
12959 break;
cd02d79d
PA
12960 child_origin_die = follow_die_ref (child_origin_die, attr,
12961 &child_origin_cu);
c38f313d
DJ
12962 }
12963
d389af10
JK
12964 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12965 counterpart may exist. */
c38f313d 12966 if (child_origin_die != child_die)
d389af10 12967 {
edb3359d
DJ
12968 if (child_die->tag != child_origin_die->tag
12969 && !(child_die->tag == DW_TAG_inlined_subroutine
12970 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12971 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12972 "different tags"),
9d8780f0
SM
12973 sect_offset_str (child_die->sect_off),
12974 sect_offset_str (child_origin_die->sect_off));
c38f313d 12975 if (child_origin_die->parent != origin_die)
b98664d3 12976 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12977 "different parents"),
9d8780f0
SM
12978 sect_offset_str (child_die->sect_off),
12979 sect_offset_str (child_origin_die->sect_off));
c38f313d 12980 else
791afaa2 12981 offsets.push_back (child_origin_die->sect_off);
d389af10 12982 }
d389af10 12983 }
791afaa2
TT
12984 std::sort (offsets.begin (), offsets.end ());
12985 sect_offset *offsets_end = offsets.data () + offsets.size ();
12986 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12987 if (offsetp[-1] == *offsetp)
b98664d3 12988 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12989 "to DIE %s as their abstract origin"),
12990 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12991
791afaa2 12992 offsetp = offsets.data ();
d389af10
JK
12993 origin_child_die = origin_die->child;
12994 while (origin_child_die && origin_child_die->tag)
12995 {
12996 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12997 while (offsetp < offsets_end
9c541725 12998 && *offsetp < origin_child_die->sect_off)
d389af10 12999 offsetp++;
b64f50a1 13000 if (offsetp >= offsets_end
9c541725 13001 || *offsetp > origin_child_die->sect_off)
d389af10 13002 {
adde2bff
DE
13003 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13004 Check whether we're already processing ORIGIN_CHILD_DIE.
13005 This can happen with mutually referenced abstract_origins.
13006 PR 16581. */
13007 if (!origin_child_die->in_process)
13008 process_die (origin_child_die, origin_cu);
d389af10 13009 }
436c571c 13010 origin_child_die = origin_child_die->sibling;
d389af10 13011 }
cd02d79d 13012 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13013
13014 if (cu != origin_cu)
13015 compute_delayed_physnames (origin_cu);
d389af10
JK
13016}
13017
c906108c 13018static void
e7c27a73 13019read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13020{
5e22e966 13021 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13022 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13023 struct context_stack *newobj;
c906108c
SS
13024 CORE_ADDR lowpc;
13025 CORE_ADDR highpc;
13026 struct die_info *child_die;
edb3359d 13027 struct attribute *attr, *call_line, *call_file;
15d034d0 13028 const char *name;
e142c38c 13029 CORE_ADDR baseaddr;
801e3a5b 13030 struct block *block;
edb3359d 13031 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13032 std::vector<struct symbol *> template_args;
34eaf542 13033 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13034
13035 if (inlined_func)
13036 {
13037 /* If we do not have call site information, we can't show the
13038 caller of this inlined function. That's too confusing, so
13039 only use the scope for local variables. */
13040 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13041 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13042 if (call_line == NULL || call_file == NULL)
13043 {
13044 read_lexical_block_scope (die, cu);
13045 return;
13046 }
13047 }
c906108c 13048
b3b3bada 13049 baseaddr = objfile->text_section_offset ();
e142c38c 13050
94af9270 13051 name = dwarf2_name (die, cu);
c906108c 13052
e8d05480
JB
13053 /* Ignore functions with missing or empty names. These are actually
13054 illegal according to the DWARF standard. */
13055 if (name == NULL)
13056 {
b98664d3 13057 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13058 sect_offset_str (die->sect_off));
e8d05480
JB
13059 return;
13060 }
13061
13062 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13063 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13064 <= PC_BOUNDS_INVALID)
e8d05480 13065 {
ae4d0c03
PM
13066 attr = dwarf2_attr (die, DW_AT_external, cu);
13067 if (!attr || !DW_UNSND (attr))
b98664d3 13068 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13069 "for subprogram DIE at %s"),
13070 sect_offset_str (die->sect_off));
e8d05480
JB
13071 return;
13072 }
c906108c 13073
3e29f34a
MR
13074 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13075 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13076
34eaf542
TT
13077 /* If we have any template arguments, then we must allocate a
13078 different sort of symbol. */
436c571c 13079 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13080 {
13081 if (child_die->tag == DW_TAG_template_type_param
13082 || child_die->tag == DW_TAG_template_value_param)
13083 {
8c14c3a3 13084 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13085 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13086 break;
13087 }
13088 }
13089
c24bdb02 13090 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13091 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13092 (struct symbol *) templ_func);
4c2df51b 13093
81873cc8 13094 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13095 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13096 cu->language);
13097
4cecd739
DJ
13098 /* If there is a location expression for DW_AT_frame_base, record
13099 it. */
e142c38c 13100 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13101 if (attr != nullptr)
fe978cb0 13102 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13103
63e43d3a
PMR
13104 /* If there is a location for the static link, record it. */
13105 newobj->static_link = NULL;
13106 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13107 if (attr != nullptr)
63e43d3a 13108 {
224c3ddb
SM
13109 newobj->static_link
13110 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13111 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 13112 cu->per_cu->addr_type ());
63e43d3a
PMR
13113 }
13114
c24bdb02 13115 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13116
639d11d3 13117 if (die->child != NULL)
c906108c 13118 {
639d11d3 13119 child_die = die->child;
c906108c
SS
13120 while (child_die && child_die->tag)
13121 {
34eaf542
TT
13122 if (child_die->tag == DW_TAG_template_type_param
13123 || child_die->tag == DW_TAG_template_value_param)
13124 {
13125 struct symbol *arg = new_symbol (child_die, NULL, cu);
13126
f1078f66 13127 if (arg != NULL)
2f4732b0 13128 template_args.push_back (arg);
34eaf542
TT
13129 }
13130 else
13131 process_die (child_die, cu);
436c571c 13132 child_die = child_die->sibling;
c906108c
SS
13133 }
13134 }
13135
d389af10
JK
13136 inherit_abstract_dies (die, cu);
13137
4a811a97
UW
13138 /* If we have a DW_AT_specification, we might need to import using
13139 directives from the context of the specification DIE. See the
13140 comment in determine_prefix. */
13141 if (cu->language == language_cplus
13142 && dwarf2_attr (die, DW_AT_specification, cu))
13143 {
13144 struct dwarf2_cu *spec_cu = cu;
13145 struct die_info *spec_die = die_specification (die, &spec_cu);
13146
13147 while (spec_die)
13148 {
13149 child_die = spec_die->child;
13150 while (child_die && child_die->tag)
13151 {
13152 if (child_die->tag == DW_TAG_imported_module)
13153 process_die (child_die, spec_cu);
436c571c 13154 child_die = child_die->sibling;
4a811a97
UW
13155 }
13156
13157 /* In some cases, GCC generates specification DIEs that
13158 themselves contain DW_AT_specification attributes. */
13159 spec_die = die_specification (spec_die, &spec_cu);
13160 }
13161 }
13162
c24bdb02 13163 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13164 /* Make a block for the local symbols within. */
c24bdb02 13165 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13166 cstk.static_link, lowpc, highpc);
801e3a5b 13167
df8a16a1 13168 /* For C++, set the block's scope. */
45280282
IB
13169 if ((cu->language == language_cplus
13170 || cu->language == language_fortran
c44af4eb
TT
13171 || cu->language == language_d
13172 || cu->language == language_rust)
4d4ec4e5 13173 && cu->processing_has_namespace_info)
195a3f6c
TT
13174 block_set_scope (block, determine_prefix (die, cu),
13175 &objfile->objfile_obstack);
df8a16a1 13176
801e3a5b
JB
13177 /* If we have address ranges, record them. */
13178 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13179
a60f3166 13180 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13181
34eaf542 13182 /* Attach template arguments to function. */
2f4732b0 13183 if (!template_args.empty ())
34eaf542
TT
13184 {
13185 gdb_assert (templ_func != NULL);
13186
2f4732b0 13187 templ_func->n_template_arguments = template_args.size ();
34eaf542 13188 templ_func->template_arguments
8d749320
SM
13189 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13190 templ_func->n_template_arguments);
34eaf542 13191 memcpy (templ_func->template_arguments,
2f4732b0 13192 template_args.data (),
34eaf542 13193 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13194
13195 /* Make sure that the symtab is set on the new symbols. Even
13196 though they don't appear in this symtab directly, other parts
13197 of gdb assume that symbols do, and this is reasonably
13198 true. */
8634679f 13199 for (symbol *sym : template_args)
3e1d3d8c 13200 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13201 }
13202
208d8187
JB
13203 /* In C++, we can have functions nested inside functions (e.g., when
13204 a function declares a class that has methods). This means that
13205 when we finish processing a function scope, we may need to go
13206 back to building a containing block's symbol lists. */
c24bdb02
KS
13207 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13208 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13209
921e78cf
JB
13210 /* If we've finished processing a top-level function, subsequent
13211 symbols go in the file symbol list. */
c24bdb02
KS
13212 if (cu->get_builder ()->outermost_context_p ())
13213 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13214}
13215
13216/* Process all the DIES contained within a lexical block scope. Start
13217 a new scope, process the dies, and then close the scope. */
13218
13219static void
e7c27a73 13220read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13221{
5e22e966 13222 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13223 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13224 CORE_ADDR lowpc, highpc;
13225 struct die_info *child_die;
e142c38c
DJ
13226 CORE_ADDR baseaddr;
13227
b3b3bada 13228 baseaddr = objfile->text_section_offset ();
c906108c
SS
13229
13230 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13231 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13232 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13233 be nasty. Might be easier to properly extend generic blocks to
af34e669 13234 describe ranges. */
e385593e
JK
13235 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13236 {
13237 case PC_BOUNDS_NOT_PRESENT:
13238 /* DW_TAG_lexical_block has no attributes, process its children as if
13239 there was no wrapping by that DW_TAG_lexical_block.
13240 GCC does no longer produces such DWARF since GCC r224161. */
13241 for (child_die = die->child;
13242 child_die != NULL && child_die->tag;
436c571c 13243 child_die = child_die->sibling)
4f7bc5ed
TT
13244 {
13245 /* We might already be processing this DIE. This can happen
13246 in an unusual circumstance -- where a subroutine A
13247 appears lexically in another subroutine B, but A actually
13248 inlines B. The recursion is broken here, rather than in
13249 inherit_abstract_dies, because it seems better to simply
13250 drop concrete children here. */
13251 if (!child_die->in_process)
13252 process_die (child_die, cu);
13253 }
e385593e
JK
13254 return;
13255 case PC_BOUNDS_INVALID:
13256 return;
13257 }
3e29f34a
MR
13258 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13259 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13260
c24bdb02 13261 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13262 if (die->child != NULL)
c906108c 13263 {
639d11d3 13264 child_die = die->child;
c906108c
SS
13265 while (child_die && child_die->tag)
13266 {
e7c27a73 13267 process_die (child_die, cu);
436c571c 13268 child_die = child_die->sibling;
c906108c
SS
13269 }
13270 }
3ea89b92 13271 inherit_abstract_dies (die, cu);
c24bdb02 13272 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13273
c24bdb02
KS
13274 if (*cu->get_builder ()->get_local_symbols () != NULL
13275 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13276 {
801e3a5b 13277 struct block *block
c24bdb02 13278 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13279 cstk.start_addr, highpc);
801e3a5b
JB
13280
13281 /* Note that recording ranges after traversing children, as we
13282 do here, means that recording a parent's ranges entails
13283 walking across all its children's ranges as they appear in
13284 the address map, which is quadratic behavior.
13285
13286 It would be nicer to record the parent's ranges before
13287 traversing its children, simply overriding whatever you find
13288 there. But since we don't even decide whether to create a
13289 block until after we've traversed its children, that's hard
13290 to do. */
13291 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13292 }
c24bdb02
KS
13293 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13294 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13295}
13296
216f72a1 13297/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13298
13299static void
13300read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13301{
5e22e966 13302 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13303 struct objfile *objfile = per_objfile->objfile;
08feed99 13304 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13305 CORE_ADDR pc, baseaddr;
13306 struct attribute *attr;
13307 struct call_site *call_site, call_site_local;
13308 void **slot;
13309 int nparams;
13310 struct die_info *child_die;
13311
b3b3bada 13312 baseaddr = objfile->text_section_offset ();
96408a79 13313
216f72a1
JK
13314 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13315 if (attr == NULL)
13316 {
13317 /* This was a pre-DWARF-5 GNU extension alias
13318 for DW_AT_call_return_pc. */
13319 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13320 }
96408a79
SA
13321 if (!attr)
13322 {
b98664d3 13323 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13324 "DIE %s [in module %s]"),
13325 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13326 return;
13327 }
cd6c91b4 13328 pc = attr->value_as_address () + baseaddr;
3e29f34a 13329 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13330
13331 if (cu->call_site_htab == NULL)
13332 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13333 NULL, &objfile->objfile_obstack,
13334 hashtab_obstack_allocate, NULL);
13335 call_site_local.pc = pc;
13336 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13337 if (*slot != NULL)
13338 {
b98664d3 13339 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13340 "DIE %s [in module %s]"),
13341 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13342 objfile_name (objfile));
96408a79
SA
13343 return;
13344 }
13345
13346 /* Count parameters at the caller. */
13347
13348 nparams = 0;
13349 for (child_die = die->child; child_die && child_die->tag;
436c571c 13350 child_die = child_die->sibling)
96408a79 13351 {
216f72a1
JK
13352 if (child_die->tag != DW_TAG_call_site_parameter
13353 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13354 {
b98664d3 13355 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13356 "DW_TAG_call_site child DIE %s [in module %s]"),
13357 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13358 objfile_name (objfile));
96408a79
SA
13359 continue;
13360 }
13361
13362 nparams++;
13363 }
13364
224c3ddb
SM
13365 call_site
13366 = ((struct call_site *)
13367 obstack_alloc (&objfile->objfile_obstack,
13368 sizeof (*call_site)
13369 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13370 *slot = call_site;
13371 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13372 call_site->pc = pc;
13373
216f72a1
JK
13374 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13375 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13376 {
13377 struct die_info *func_die;
13378
13379 /* Skip also over DW_TAG_inlined_subroutine. */
13380 for (func_die = die->parent;
13381 func_die && func_die->tag != DW_TAG_subprogram
13382 && func_die->tag != DW_TAG_subroutine_type;
13383 func_die = func_die->parent);
13384
216f72a1
JK
13385 /* DW_AT_call_all_calls is a superset
13386 of DW_AT_call_all_tail_calls. */
96408a79 13387 if (func_die
216f72a1 13388 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13389 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13390 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13391 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13392 {
13393 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13394 not complete. But keep CALL_SITE for look ups via call_site_htab,
13395 both the initial caller containing the real return address PC and
13396 the final callee containing the current PC of a chain of tail
13397 calls do not need to have the tail call list complete. But any
13398 function candidate for a virtual tail call frame searched via
13399 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13400 determined unambiguously. */
13401 }
13402 else
13403 {
13404 struct type *func_type = NULL;
13405
13406 if (func_die)
13407 func_type = get_die_type (func_die, cu);
13408 if (func_type != NULL)
13409 {
78134374 13410 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13411
13412 /* Enlist this call site to the function. */
13413 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13414 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13415 }
13416 else
b98664d3 13417 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13418 "DIE %s [in module %s]"),
13419 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13420 }
13421 }
13422
216f72a1
JK
13423 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13424 if (attr == NULL)
13425 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13426 if (attr == NULL)
13427 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13428 if (attr == NULL)
216f72a1
JK
13429 {
13430 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13431 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13432 }
96408a79 13433 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13434 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13435 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13436 else if (attr->form_is_block ())
96408a79
SA
13437 {
13438 struct dwarf2_locexpr_baton *dlbaton;
13439
8d749320 13440 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13441 dlbaton->data = DW_BLOCK (attr)->data;
13442 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13443 dlbaton->per_objfile = per_objfile;
96408a79
SA
13444 dlbaton->per_cu = cu->per_cu;
13445
13446 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13447 }
cd6c91b4 13448 else if (attr->form_is_ref ())
96408a79 13449 {
96408a79
SA
13450 struct dwarf2_cu *target_cu = cu;
13451 struct die_info *target_die;
13452
ac9ec31b 13453 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13454 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13455 if (die_is_declaration (target_die, target_cu))
13456 {
7d45c7c3 13457 const char *target_physname;
9112db09
JK
13458
13459 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13460 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13461 if (target_physname == NULL)
9112db09 13462 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13463 if (target_physname == NULL)
b98664d3 13464 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13465 "physname, for referencing DIE %s [in module %s]"),
13466 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13467 else
7d455152 13468 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13469 }
13470 else
13471 {
13472 CORE_ADDR lowpc;
13473
13474 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13475 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13476 <= PC_BOUNDS_INVALID)
b98664d3 13477 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13478 "low pc, for referencing DIE %s [in module %s]"),
13479 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13480 else
3e29f34a
MR
13481 {
13482 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13483 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13484 }
96408a79
SA
13485 }
13486 }
13487 else
b98664d3 13488 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13489 "block nor reference, for DIE %s [in module %s]"),
13490 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13491
13492 call_site->per_cu = cu->per_cu;
13493
13494 for (child_die = die->child;
13495 child_die && child_die->tag;
436c571c 13496 child_die = child_die->sibling)
96408a79 13497 {
96408a79 13498 struct call_site_parameter *parameter;
1788b2d3 13499 struct attribute *loc, *origin;
96408a79 13500
216f72a1
JK
13501 if (child_die->tag != DW_TAG_call_site_parameter
13502 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13503 {
13504 /* Already printed the complaint above. */
13505 continue;
13506 }
13507
13508 gdb_assert (call_site->parameter_count < nparams);
13509 parameter = &call_site->parameter[call_site->parameter_count];
13510
1788b2d3
JK
13511 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13512 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13513 register is contained in DW_AT_call_value. */
96408a79 13514
24c5c679 13515 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13516 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13517 if (origin == NULL)
13518 {
13519 /* This was a pre-DWARF-5 GNU extension alias
13520 for DW_AT_call_parameter. */
13521 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13522 }
cd6c91b4 13523 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13524 {
1788b2d3 13525 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13526
0826b30a 13527 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13528 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13529 {
13530 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13531 binding can be done only inside one CU. Such referenced DIE
13532 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13533 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13534 "DW_TAG_call_site child DIE %s [in module %s]"),
13535 sect_offset_str (child_die->sect_off),
9c541725 13536 objfile_name (objfile));
d76b7dbc
JK
13537 continue;
13538 }
9c541725
PA
13539 parameter->u.param_cu_off
13540 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13541 }
4fc6c0d5 13542 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13543 {
b98664d3 13544 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13545 "DW_TAG_call_site child DIE %s [in module %s]"),
13546 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13547 continue;
13548 }
24c5c679 13549 else
96408a79 13550 {
24c5c679
JK
13551 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13552 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13553 if (parameter->u.dwarf_reg != -1)
13554 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13555 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13556 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13557 &parameter->u.fb_offset))
13558 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13559 else
13560 {
b98664d3 13561 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13562 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13563 "DW_TAG_call_site child DIE %s "
24c5c679 13564 "[in module %s]"),
9d8780f0 13565 sect_offset_str (child_die->sect_off),
9c541725 13566 objfile_name (objfile));
24c5c679
JK
13567 continue;
13568 }
96408a79
SA
13569 }
13570
216f72a1
JK
13571 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13572 if (attr == NULL)
13573 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13574 if (attr == NULL || !attr->form_is_block ())
96408a79 13575 {
b98664d3 13576 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13577 "DW_TAG_call_site child DIE %s [in module %s]"),
13578 sect_offset_str (child_die->sect_off),
9c541725 13579 objfile_name (objfile));
96408a79
SA
13580 continue;
13581 }
13582 parameter->value = DW_BLOCK (attr)->data;
13583 parameter->value_size = DW_BLOCK (attr)->size;
13584
13585 /* Parameters are not pre-cleared by memset above. */
13586 parameter->data_value = NULL;
13587 parameter->data_value_size = 0;
13588 call_site->parameter_count++;
13589
216f72a1
JK
13590 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13591 if (attr == NULL)
13592 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13593 if (attr != nullptr)
96408a79 13594 {
4fc6c0d5 13595 if (!attr->form_is_block ())
b98664d3 13596 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13597 "DW_TAG_call_site child DIE %s [in module %s]"),
13598 sect_offset_str (child_die->sect_off),
9c541725 13599 objfile_name (objfile));
96408a79
SA
13600 else
13601 {
13602 parameter->data_value = DW_BLOCK (attr)->data;
13603 parameter->data_value_size = DW_BLOCK (attr)->size;
13604 }
13605 }
13606 }
13607}
13608
71a3c369
TT
13609/* Helper function for read_variable. If DIE represents a virtual
13610 table, then return the type of the concrete object that is
13611 associated with the virtual table. Otherwise, return NULL. */
13612
13613static struct type *
13614rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13615{
13616 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13617 if (attr == NULL)
13618 return NULL;
13619
13620 /* Find the type DIE. */
13621 struct die_info *type_die = NULL;
13622 struct dwarf2_cu *type_cu = cu;
13623
cd6c91b4 13624 if (attr->form_is_ref ())
71a3c369
TT
13625 type_die = follow_die_ref (die, attr, &type_cu);
13626 if (type_die == NULL)
13627 return NULL;
13628
13629 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13630 return NULL;
13631 return die_containing_type (type_die, type_cu);
13632}
13633
13634/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13635
13636static void
13637read_variable (struct die_info *die, struct dwarf2_cu *cu)
13638{
13639 struct rust_vtable_symbol *storage = NULL;
13640
13641 if (cu->language == language_rust)
13642 {
13643 struct type *containing_type = rust_containing_type (die, cu);
13644
13645 if (containing_type != NULL)
13646 {
5e22e966 13647 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13648
8c14c3a3 13649 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13650 storage->concrete_type = containing_type;
cf724bc9 13651 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13652 }
13653 }
13654
e4a62c65
TV
13655 struct symbol *res = new_symbol (die, NULL, cu, storage);
13656 struct attribute *abstract_origin
13657 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13658 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13659 if (res == NULL && loc && abstract_origin)
13660 {
13661 /* We have a variable without a name, but with a location and an abstract
13662 origin. This may be a concrete instance of an abstract variable
13663 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13664 later. */
13665 struct dwarf2_cu *origin_cu = cu;
13666 struct die_info *origin_die
13667 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13668 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13669 per_objfile->per_bfd->abstract_to_concrete
13670 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13671 }
71a3c369
TT
13672}
13673
43988095
JK
13674/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13675 reading .debug_rnglists.
13676 Callback's type should be:
13677 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13678 Return true if the attributes are present and valid, otherwise,
13679 return false. */
13680
13681template <typename Callback>
13682static bool
13683dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13684 Callback &&callback)
13685{
5e22e966 13686 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13687 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13688 bfd *obfd = objfile->obfd;
43988095 13689 /* Base address selection entry. */
2b24b6e4 13690 gdb::optional<CORE_ADDR> base;
43988095 13691 const gdb_byte *buffer;
43988095
JK
13692 CORE_ADDR baseaddr;
13693 bool overflow = false;
13694
43988095
JK
13695 base = cu->base_address;
13696
5989a64e
SM
13697 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13698 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13699 {
b98664d3 13700 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13701 offset);
13702 return false;
13703 }
5989a64e 13704 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13705
b3b3bada 13706 baseaddr = objfile->text_section_offset ();
43988095
JK
13707
13708 while (1)
13709 {
7814882a
JK
13710 /* Initialize it due to a false compiler warning. */
13711 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13712 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13713 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13714 unsigned int bytes_read;
13715
13716 if (buffer == buf_end)
13717 {
13718 overflow = true;
13719 break;
13720 }
13721 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13722 switch (rlet)
13723 {
13724 case DW_RLE_end_of_list:
13725 break;
13726 case DW_RLE_base_address:
13727 if (buffer + cu->header.addr_size > buf_end)
13728 {
13729 overflow = true;
13730 break;
13731 }
c8a7a66f 13732 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13733 buffer += bytes_read;
13734 break;
13735 case DW_RLE_start_length:
13736 if (buffer + cu->header.addr_size > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
c8a7a66f
TT
13741 range_beginning = cu->header.read_address (obfd, buffer,
13742 &bytes_read);
43988095
JK
13743 buffer += bytes_read;
13744 range_end = (range_beginning
13745 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13746 buffer += bytes_read;
13747 if (buffer > buf_end)
13748 {
13749 overflow = true;
13750 break;
13751 }
13752 break;
13753 case DW_RLE_offset_pair:
13754 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13755 buffer += bytes_read;
13756 if (buffer > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 if (buffer > buf_end)
13764 {
13765 overflow = true;
13766 break;
13767 }
13768 break;
13769 case DW_RLE_start_end:
13770 if (buffer + 2 * cu->header.addr_size > buf_end)
13771 {
13772 overflow = true;
13773 break;
13774 }
c8a7a66f
TT
13775 range_beginning = cu->header.read_address (obfd, buffer,
13776 &bytes_read);
43988095 13777 buffer += bytes_read;
c8a7a66f 13778 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13779 buffer += bytes_read;
13780 break;
13781 default:
b98664d3 13782 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13783 return false;
13784 }
13785 if (rlet == DW_RLE_end_of_list || overflow)
13786 break;
13787 if (rlet == DW_RLE_base_address)
13788 continue;
13789
2b24b6e4 13790 if (!base.has_value ())
43988095
JK
13791 {
13792 /* We have no valid base address for the ranges
13793 data. */
b98664d3 13794 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13795 return false;
13796 }
13797
13798 if (range_beginning > range_end)
13799 {
13800 /* Inverted range entries are invalid. */
b98664d3 13801 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13802 return false;
13803 }
13804
13805 /* Empty range entries have no effect. */
13806 if (range_beginning == range_end)
13807 continue;
13808
2b24b6e4
TT
13809 range_beginning += *base;
13810 range_end += *base;
43988095
JK
13811
13812 /* A not-uncommon case of bad debug info.
13813 Don't pollute the addrmap with bad data. */
13814 if (range_beginning + baseaddr == 0
5989a64e 13815 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13816 {
b98664d3 13817 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13818 " [in module %s]"), objfile_name (objfile));
13819 continue;
13820 }
13821
13822 callback (range_beginning, range_end);
13823 }
13824
13825 if (overflow)
13826 {
b98664d3 13827 complaint (_("Offset %d is not terminated "
43988095
JK
13828 "for DW_AT_ranges attribute"),
13829 offset);
13830 return false;
13831 }
13832
13833 return true;
13834}
13835
13836/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13837 Callback's type should be:
13838 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13839 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13840
43988095 13841template <typename Callback>
43039443 13842static int
5f46c5a5 13843dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13844 Callback &&callback)
43039443 13845{
5e22e966
SM
13846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13847 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13848 struct comp_unit_head *cu_header = &cu->header;
13849 bfd *obfd = objfile->obfd;
13850 unsigned int addr_size = cu_header->addr_size;
13851 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13852 /* Base address selection entry. */
2b24b6e4 13853 gdb::optional<CORE_ADDR> base;
43039443 13854 unsigned int dummy;
d521ce57 13855 const gdb_byte *buffer;
ff013f42 13856 CORE_ADDR baseaddr;
43039443 13857
43988095
JK
13858 if (cu_header->version >= 5)
13859 return dwarf2_rnglists_process (offset, cu, callback);
13860
d00adf39 13861 base = cu->base_address;
43039443 13862
5e22e966
SM
13863 per_objfile->per_bfd->ranges.read (objfile);
13864 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13865 {
b98664d3 13866 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13867 offset);
13868 return 0;
13869 }
5e22e966 13870 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13871
b3b3bada 13872 baseaddr = objfile->text_section_offset ();
ff013f42 13873
43039443
JK
13874 while (1)
13875 {
13876 CORE_ADDR range_beginning, range_end;
13877
c8a7a66f 13878 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13879 buffer += addr_size;
c8a7a66f 13880 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13881 buffer += addr_size;
13882 offset += 2 * addr_size;
13883
13884 /* An end of list marker is a pair of zero addresses. */
13885 if (range_beginning == 0 && range_end == 0)
13886 /* Found the end of list entry. */
13887 break;
13888
13889 /* Each base address selection entry is a pair of 2 values.
13890 The first is the largest possible address, the second is
13891 the base address. Check for a base address here. */
13892 if ((range_beginning & mask) == mask)
13893 {
28d2bfb9
AB
13894 /* If we found the largest possible address, then we already
13895 have the base address in range_end. */
13896 base = range_end;
43039443
JK
13897 continue;
13898 }
13899
2b24b6e4 13900 if (!base.has_value ())
43039443
JK
13901 {
13902 /* We have no valid base address for the ranges
13903 data. */
b98664d3 13904 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13905 return 0;
13906 }
13907
9277c30c
UW
13908 if (range_beginning > range_end)
13909 {
13910 /* Inverted range entries are invalid. */
b98664d3 13911 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13912 return 0;
13913 }
13914
13915 /* Empty range entries have no effect. */
13916 if (range_beginning == range_end)
13917 continue;
13918
2b24b6e4
TT
13919 range_beginning += *base;
13920 range_end += *base;
43039443 13921
01093045
DE
13922 /* A not-uncommon case of bad debug info.
13923 Don't pollute the addrmap with bad data. */
13924 if (range_beginning + baseaddr == 0
5e22e966 13925 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13926 {
b98664d3 13927 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13928 " [in module %s]"), objfile_name (objfile));
01093045
DE
13929 continue;
13930 }
13931
5f46c5a5
JK
13932 callback (range_beginning, range_end);
13933 }
13934
13935 return 1;
13936}
13937
13938/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13939 Return 1 if the attributes are present and valid, otherwise, return 0.
13940 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13941
13942static int
13943dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13944 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13945 dwarf2_psymtab *ranges_pst)
5f46c5a5 13946{
5e22e966 13947 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13948 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13949 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13950 int low_set = 0;
13951 CORE_ADDR low = 0;
13952 CORE_ADDR high = 0;
13953 int retval;
13954
13955 retval = dwarf2_ranges_process (offset, cu,
13956 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13957 {
9277c30c 13958 if (ranges_pst != NULL)
3e29f34a
MR
13959 {
13960 CORE_ADDR lowpc;
13961 CORE_ADDR highpc;
13962
79748972
TT
13963 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13964 range_beginning + baseaddr)
13965 - baseaddr);
13966 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13967 range_end + baseaddr)
13968 - baseaddr);
d320c2b5
TT
13969 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13970 lowpc, highpc - 1, ranges_pst);
3e29f34a 13971 }
ff013f42 13972
43039443
JK
13973 /* FIXME: This is recording everything as a low-high
13974 segment of consecutive addresses. We should have a
13975 data structure for discontiguous block ranges
13976 instead. */
13977 if (! low_set)
13978 {
13979 low = range_beginning;
13980 high = range_end;
13981 low_set = 1;
13982 }
13983 else
13984 {
13985 if (range_beginning < low)
13986 low = range_beginning;
13987 if (range_end > high)
13988 high = range_end;
13989 }
5f46c5a5
JK
13990 });
13991 if (!retval)
13992 return 0;
43039443
JK
13993
13994 if (! low_set)
13995 /* If the first entry is an end-of-list marker, the range
13996 describes an empty scope, i.e. no instructions. */
13997 return 0;
13998
13999 if (low_return)
14000 *low_return = low;
14001 if (high_return)
14002 *high_return = high;
14003 return 1;
14004}
14005
3a2b436a
JK
14006/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14007 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14008 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14009
3a2b436a 14010static enum pc_bounds_kind
af34e669 14011dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14012 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14013 dwarf2_psymtab *pst)
c906108c 14014{
5e22e966 14015 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14016 struct attribute *attr;
91da1414 14017 struct attribute *attr_high;
af34e669
DJ
14018 CORE_ADDR low = 0;
14019 CORE_ADDR high = 0;
e385593e 14020 enum pc_bounds_kind ret;
c906108c 14021
91da1414
MW
14022 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14023 if (attr_high)
af34e669 14024 {
e142c38c 14025 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14026 if (attr != nullptr)
91da1414 14027 {
cd6c91b4
TT
14028 low = attr->value_as_address ();
14029 high = attr_high->value_as_address ();
14030 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14031 high += low;
91da1414 14032 }
af34e669
DJ
14033 else
14034 /* Found high w/o low attribute. */
e385593e 14035 return PC_BOUNDS_INVALID;
af34e669
DJ
14036
14037 /* Found consecutive range of addresses. */
3a2b436a 14038 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14039 }
c906108c 14040 else
af34e669 14041 {
e142c38c 14042 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14043 if (attr != NULL)
14044 {
18a8505e 14045 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14046 We take advantage of the fact that DW_AT_ranges does not appear
14047 in DW_TAG_compile_unit of DWO files. */
14048 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14049 unsigned int ranges_offset = (DW_UNSND (attr)
14050 + (need_ranges_base
14051 ? cu->ranges_base
14052 : 0));
2e3cf129 14053
af34e669 14054 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14055 .debug_ranges section. */
2e3cf129 14056 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14057 return PC_BOUNDS_INVALID;
43039443 14058 /* Found discontinuous range of addresses. */
3a2b436a 14059 ret = PC_BOUNDS_RANGES;
af34e669 14060 }
e385593e
JK
14061 else
14062 return PC_BOUNDS_NOT_PRESENT;
af34e669 14063 }
c906108c 14064
48fbe735 14065 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14066 if (high <= low)
e385593e 14067 return PC_BOUNDS_INVALID;
c906108c
SS
14068
14069 /* When using the GNU linker, .gnu.linkonce. sections are used to
14070 eliminate duplicate copies of functions and vtables and such.
14071 The linker will arbitrarily choose one and discard the others.
14072 The AT_*_pc values for such functions refer to local labels in
14073 these sections. If the section from that file was discarded, the
14074 labels are not in the output, so the relocs get a value of 0.
14075 If this is a discarded function, mark the pc bounds as invalid,
14076 so that GDB will ignore it. */
5989a64e 14077 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14078 return PC_BOUNDS_INVALID;
c906108c
SS
14079
14080 *lowpc = low;
96408a79
SA
14081 if (highpc)
14082 *highpc = high;
af34e669 14083 return ret;
c906108c
SS
14084}
14085
b084d499
JB
14086/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14087 its low and high PC addresses. Do nothing if these addresses could not
14088 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14089 and HIGHPC to the high address if greater than HIGHPC. */
14090
14091static void
14092dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14093 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14094 struct dwarf2_cu *cu)
14095{
14096 CORE_ADDR low, high;
14097 struct die_info *child = die->child;
14098
e385593e 14099 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14100 {
325fac50
PA
14101 *lowpc = std::min (*lowpc, low);
14102 *highpc = std::max (*highpc, high);
b084d499
JB
14103 }
14104
14105 /* If the language does not allow nested subprograms (either inside
14106 subprograms or lexical blocks), we're done. */
14107 if (cu->language != language_ada)
14108 return;
6e70227d 14109
b084d499
JB
14110 /* Check all the children of the given DIE. If it contains nested
14111 subprograms, then check their pc bounds. Likewise, we need to
14112 check lexical blocks as well, as they may also contain subprogram
14113 definitions. */
14114 while (child && child->tag)
14115 {
14116 if (child->tag == DW_TAG_subprogram
14117 || child->tag == DW_TAG_lexical_block)
14118 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14119 child = child->sibling;
b084d499
JB
14120 }
14121}
14122
fae299cd
DC
14123/* Get the low and high pc's represented by the scope DIE, and store
14124 them in *LOWPC and *HIGHPC. If the correct values can't be
14125 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14126
14127static void
14128get_scope_pc_bounds (struct die_info *die,
14129 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14130 struct dwarf2_cu *cu)
14131{
14132 CORE_ADDR best_low = (CORE_ADDR) -1;
14133 CORE_ADDR best_high = (CORE_ADDR) 0;
14134 CORE_ADDR current_low, current_high;
14135
3a2b436a 14136 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14137 >= PC_BOUNDS_RANGES)
fae299cd
DC
14138 {
14139 best_low = current_low;
14140 best_high = current_high;
14141 }
14142 else
14143 {
14144 struct die_info *child = die->child;
14145
14146 while (child && child->tag)
14147 {
14148 switch (child->tag) {
14149 case DW_TAG_subprogram:
b084d499 14150 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14151 break;
14152 case DW_TAG_namespace:
f55ee35c 14153 case DW_TAG_module:
fae299cd
DC
14154 /* FIXME: carlton/2004-01-16: Should we do this for
14155 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14156 that current GCC's always emit the DIEs corresponding
14157 to definitions of methods of classes as children of a
14158 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14159 the DIEs giving the declarations, which could be
14160 anywhere). But I don't see any reason why the
14161 standards says that they have to be there. */
14162 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14163
14164 if (current_low != ((CORE_ADDR) -1))
14165 {
325fac50
PA
14166 best_low = std::min (best_low, current_low);
14167 best_high = std::max (best_high, current_high);
fae299cd
DC
14168 }
14169 break;
14170 default:
0963b4bd 14171 /* Ignore. */
fae299cd
DC
14172 break;
14173 }
14174
436c571c 14175 child = child->sibling;
fae299cd
DC
14176 }
14177 }
14178
14179 *lowpc = best_low;
14180 *highpc = best_high;
14181}
14182
801e3a5b
JB
14183/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14184 in DIE. */
380bca97 14185
801e3a5b
JB
14186static void
14187dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14188 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14189{
5e22e966 14190 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14191 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14192 struct attribute *attr;
91da1414 14193 struct attribute *attr_high;
801e3a5b 14194
91da1414
MW
14195 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14196 if (attr_high)
801e3a5b 14197 {
801e3a5b 14198 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14199 if (attr != nullptr)
801e3a5b 14200 {
cd6c91b4
TT
14201 CORE_ADDR low = attr->value_as_address ();
14202 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14203
cd6c91b4 14204 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14205 high += low;
9a619af0 14206
3e29f34a
MR
14207 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14208 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14209 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14210 }
14211 }
14212
14213 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14214 if (attr != nullptr)
801e3a5b 14215 {
18a8505e 14216 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14217 We take advantage of the fact that DW_AT_ranges does not appear
14218 in DW_TAG_compile_unit of DWO files. */
14219 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14220
14221 /* The value of the DW_AT_ranges attribute is the offset of the
14222 address range list in the .debug_ranges section. */
ab435259
DE
14223 unsigned long offset = (DW_UNSND (attr)
14224 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14225
2d5f09ec 14226 std::vector<blockrange> blockvec;
5f46c5a5
JK
14227 dwarf2_ranges_process (offset, cu,
14228 [&] (CORE_ADDR start, CORE_ADDR end)
14229 {
58fdfd2c
JK
14230 start += baseaddr;
14231 end += baseaddr;
5f46c5a5
JK
14232 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14233 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14234 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14235 blockvec.emplace_back (start, end);
5f46c5a5 14236 });
2d5f09ec
KB
14237
14238 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14239 }
14240}
14241
685b1105
JK
14242/* Check whether the producer field indicates either of GCC < 4.6, or the
14243 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14244
685b1105
JK
14245static void
14246check_producer (struct dwarf2_cu *cu)
60d5a603 14247{
38360086 14248 int major, minor;
60d5a603
JK
14249
14250 if (cu->producer == NULL)
14251 {
14252 /* For unknown compilers expect their behavior is DWARF version
14253 compliant.
14254
14255 GCC started to support .debug_types sections by -gdwarf-4 since
14256 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14257 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14258 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14259 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14260 }
b1ffba5a 14261 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14262 {
38360086
MW
14263 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14264 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14265 }
5230b05a 14266 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14267 {
14268 cu->producer_is_icc = true;
14269 cu->producer_is_icc_lt_14 = major < 14;
14270 }
c258c396
JD
14271 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14272 cu->producer_is_codewarrior = true;
685b1105
JK
14273 else
14274 {
14275 /* For other non-GCC compilers, expect their behavior is DWARF version
14276 compliant. */
60d5a603
JK
14277 }
14278
9068261f 14279 cu->checked_producer = true;
685b1105 14280}
ba919b58 14281
685b1105
JK
14282/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14283 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14284 during 4.6.0 experimental. */
14285
9068261f 14286static bool
685b1105
JK
14287producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14288{
14289 if (!cu->checked_producer)
14290 check_producer (cu);
14291
14292 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14293}
14294
c258c396
JD
14295
14296/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14297 with incorrect is_stmt attributes. */
14298
14299static bool
14300producer_is_codewarrior (struct dwarf2_cu *cu)
14301{
14302 if (!cu->checked_producer)
14303 check_producer (cu);
14304
14305 return cu->producer_is_codewarrior;
14306}
14307
405feb71 14308/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14309 DW_AT_accessibility. */
14310
14311static enum dwarf_access_attribute
14312dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14313{
14314 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14315 {
14316 /* The default DWARF 2 accessibility for members is public, the default
14317 accessibility for inheritance is private. */
14318
14319 if (die->tag != DW_TAG_inheritance)
14320 return DW_ACCESS_public;
14321 else
14322 return DW_ACCESS_private;
14323 }
14324 else
14325 {
14326 /* DWARF 3+ defines the default accessibility a different way. The same
14327 rules apply now for DW_TAG_inheritance as for the members and it only
14328 depends on the container kind. */
14329
14330 if (die->parent->tag == DW_TAG_class_type)
14331 return DW_ACCESS_private;
14332 else
14333 return DW_ACCESS_public;
14334 }
14335}
14336
74ac6d43
TT
14337/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14338 offset. If the attribute was not found return 0, otherwise return
14339 1. If it was found but could not properly be handled, set *OFFSET
14340 to 0. */
14341
14342static int
14343handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14344 LONGEST *offset)
14345{
14346 struct attribute *attr;
14347
14348 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14349 if (attr != NULL)
14350 {
14351 *offset = 0;
14352
14353 /* Note that we do not check for a section offset first here.
14354 This is because DW_AT_data_member_location is new in DWARF 4,
14355 so if we see it, we can assume that a constant form is really
14356 a constant and not a section offset. */
cd6c91b4 14357 if (attr->form_is_constant ())
0826b30a 14358 *offset = attr->constant_value (0);
cd6c91b4 14359 else if (attr->form_is_section_offset ())
74ac6d43 14360 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14361 else if (attr->form_is_block ())
74ac6d43
TT
14362 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14363 else
14364 dwarf2_complex_location_expr_complaint ();
14365
14366 return 1;
14367 }
14368
14369 return 0;
14370}
14371
7d79de9a
TT
14372/* Look for DW_AT_data_member_location and store the results in FIELD. */
14373
14374static void
14375handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14376 struct field *field)
14377{
14378 struct attribute *attr;
14379
14380 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14381 if (attr != NULL)
14382 {
14383 if (attr->form_is_constant ())
14384 {
14385 LONGEST offset = attr->constant_value (0);
14386 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14387 }
14388 else if (attr->form_is_section_offset ())
14389 dwarf2_complex_location_expr_complaint ();
14390 else if (attr->form_is_block ())
14391 {
14392 bool handled;
14393 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14394 if (handled)
14395 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14396 else
14397 {
5e22e966
SM
14398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14399 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14400 struct dwarf2_locexpr_baton *dlbaton
14401 = XOBNEW (&objfile->objfile_obstack,
14402 struct dwarf2_locexpr_baton);
14403 dlbaton->data = DW_BLOCK (attr)->data;
14404 dlbaton->size = DW_BLOCK (attr)->size;
14405 /* When using this baton, we want to compute the address
14406 of the field, not the value. This is why
14407 is_reference is set to false here. */
14408 dlbaton->is_reference = false;
5e22e966 14409 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14410 dlbaton->per_cu = cu->per_cu;
14411
14412 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14413 }
14414 }
14415 else
14416 dwarf2_complex_location_expr_complaint ();
14417 }
14418}
14419
c906108c
SS
14420/* Add an aggregate field to the field list. */
14421
14422static void
107d2387 14423dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14424 struct dwarf2_cu *cu)
6e70227d 14425{
5e22e966 14426 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14427 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14428 struct nextfield *new_field;
14429 struct attribute *attr;
14430 struct field *fp;
15d034d0 14431 const char *fieldname = "";
c906108c 14432
7d0ccb61
DJ
14433 if (die->tag == DW_TAG_inheritance)
14434 {
be2daae6
TT
14435 fip->baseclasses.emplace_back ();
14436 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14437 }
14438 else
14439 {
be2daae6
TT
14440 fip->fields.emplace_back ();
14441 new_field = &fip->fields.back ();
7d0ccb61 14442 }
be2daae6 14443
9c6a1327
TT
14444 new_field->offset = die->sect_off;
14445
e142c38c 14446 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14447 if (attr != nullptr)
c906108c 14448 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14449 else
14450 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14451 if (new_field->accessibility != DW_ACCESS_public)
14452 fip->non_public_fields = 1;
60d5a603 14453
e142c38c 14454 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14455 if (attr != nullptr)
c906108c 14456 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14457 else
14458 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14459
14460 fp = &new_field->field;
a9a9bd0f 14461
e142c38c 14462 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14463 {
a9a9bd0f 14464 /* Data member other than a C++ static data member. */
6e70227d 14465
c906108c 14466 /* Get type of field. */
e7c27a73 14467 fp->type = die_type (die, cu);
c906108c 14468
d6a843b5 14469 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14470
c906108c 14471 /* Get bit size of field (zero if none). */
e142c38c 14472 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14473 if (attr != nullptr)
c906108c
SS
14474 {
14475 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14476 }
14477 else
14478 {
14479 FIELD_BITSIZE (*fp) = 0;
14480 }
14481
14482 /* Get bit offset of field. */
7d79de9a 14483 handle_data_member_location (die, cu, fp);
e142c38c 14484 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14485 if (attr != nullptr)
c906108c 14486 {
d5a22e77 14487 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14488 {
14489 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14490 additional bit offset from the MSB of the containing
14491 anonymous object to the MSB of the field. We don't
14492 have to do anything special since we don't need to
14493 know the size of the anonymous object. */
f41f5e61 14494 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14495 }
14496 else
14497 {
14498 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14499 MSB of the anonymous object, subtract off the number of
14500 bits from the MSB of the field to the MSB of the
14501 object, and then subtract off the number of bits of
14502 the field itself. The result is the bit offset of
14503 the LSB of the field. */
c906108c
SS
14504 int anonymous_size;
14505 int bit_offset = DW_UNSND (attr);
14506
e142c38c 14507 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14508 if (attr != nullptr)
c906108c
SS
14509 {
14510 /* The size of the anonymous object containing
14511 the bit field is explicit, so use the
14512 indicated size (in bytes). */
14513 anonymous_size = DW_UNSND (attr);
14514 }
14515 else
14516 {
14517 /* The size of the anonymous object containing
14518 the bit field must be inferred from the type
14519 attribute of the data member containing the
14520 bit field. */
14521 anonymous_size = TYPE_LENGTH (fp->type);
14522 }
f41f5e61
PA
14523 SET_FIELD_BITPOS (*fp,
14524 (FIELD_BITPOS (*fp)
14525 + anonymous_size * bits_per_byte
14526 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14527 }
14528 }
da5b30da
AA
14529 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14530 if (attr != NULL)
14531 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14532 + attr->constant_value (0)));
c906108c
SS
14533
14534 /* Get name of field. */
39cbfefa
DJ
14535 fieldname = dwarf2_name (die, cu);
14536 if (fieldname == NULL)
14537 fieldname = "";
d8151005
DJ
14538
14539 /* The name is already allocated along with this objfile, so we don't
14540 need to duplicate it for the type. */
14541 fp->name = fieldname;
c906108c
SS
14542
14543 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14544 pointer or virtual base class pointer) to private. */
e142c38c 14545 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14546 {
d48cc9dd 14547 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14548 new_field->accessibility = DW_ACCESS_private;
14549 fip->non_public_fields = 1;
14550 }
14551 }
a9a9bd0f 14552 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14553 {
a9a9bd0f
DC
14554 /* C++ static member. */
14555
14556 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14557 is a declaration, but all versions of G++ as of this writing
14558 (so through at least 3.2.1) incorrectly generate
14559 DW_TAG_variable tags. */
6e70227d 14560
ff355380 14561 const char *physname;
c906108c 14562
a9a9bd0f 14563 /* Get name of field. */
39cbfefa
DJ
14564 fieldname = dwarf2_name (die, cu);
14565 if (fieldname == NULL)
c906108c
SS
14566 return;
14567
254e6b9e 14568 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14569 if (attr
14570 /* Only create a symbol if this is an external value.
14571 new_symbol checks this and puts the value in the global symbol
14572 table, which we want. If it is not external, new_symbol
14573 will try to put the value in cu->list_in_scope which is wrong. */
14574 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14575 {
14576 /* A static const member, not much different than an enum as far as
14577 we're concerned, except that we can support more types. */
14578 new_symbol (die, NULL, cu);
14579 }
14580
2df3850c 14581 /* Get physical name. */
ff355380 14582 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14583
d8151005
DJ
14584 /* The name is already allocated along with this objfile, so we don't
14585 need to duplicate it for the type. */
14586 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14587 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14588 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14589 }
14590 else if (die->tag == DW_TAG_inheritance)
14591 {
74ac6d43 14592 /* C++ base class field. */
7d79de9a 14593 handle_data_member_location (die, cu, fp);
c906108c 14594 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14595 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14596 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14597 }
2ddeaf8a
TT
14598 else
14599 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14600}
14601
883fd55a
KS
14602/* Can the type given by DIE define another type? */
14603
14604static bool
14605type_can_define_types (const struct die_info *die)
14606{
14607 switch (die->tag)
14608 {
14609 case DW_TAG_typedef:
14610 case DW_TAG_class_type:
14611 case DW_TAG_structure_type:
14612 case DW_TAG_union_type:
14613 case DW_TAG_enumeration_type:
14614 return true;
14615
14616 default:
14617 return false;
14618 }
14619}
14620
14621/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14622
14623static void
883fd55a
KS
14624dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14625 struct dwarf2_cu *cu)
6e70227d 14626{
be2daae6
TT
14627 struct decl_field fp;
14628 memset (&fp, 0, sizeof (fp));
98751a41 14629
883fd55a 14630 gdb_assert (type_can_define_types (die));
98751a41 14631
883fd55a 14632 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14633 fp.name = dwarf2_name (die, cu);
14634 fp.type = read_type_die (die, cu);
98751a41 14635
c191a687
KS
14636 /* Save accessibility. */
14637 enum dwarf_access_attribute accessibility;
14638 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14639 if (attr != NULL)
14640 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14641 else
14642 accessibility = dwarf2_default_access_attribute (die, cu);
14643 switch (accessibility)
14644 {
14645 case DW_ACCESS_public:
14646 /* The assumed value if neither private nor protected. */
14647 break;
14648 case DW_ACCESS_private:
be2daae6 14649 fp.is_private = 1;
c191a687
KS
14650 break;
14651 case DW_ACCESS_protected:
be2daae6 14652 fp.is_protected = 1;
c191a687
KS
14653 break;
14654 default:
b98664d3 14655 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14656 }
14657
883fd55a 14658 if (die->tag == DW_TAG_typedef)
be2daae6 14659 fip->typedef_field_list.push_back (fp);
883fd55a 14660 else
be2daae6 14661 fip->nested_types_list.push_back (fp);
98751a41
JK
14662}
14663
9c6a1327
TT
14664/* A convenience typedef that's used when finding the discriminant
14665 field for a variant part. */
1b95cdb7
SM
14666typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14667 offset_map_type;
9c6a1327
TT
14668
14669/* Compute the discriminant range for a given variant. OBSTACK is
14670 where the results will be stored. VARIANT is the variant to
14671 process. IS_UNSIGNED indicates whether the discriminant is signed
14672 or unsigned. */
14673
14674static const gdb::array_view<discriminant_range>
14675convert_variant_range (struct obstack *obstack, const variant_field &variant,
14676 bool is_unsigned)
14677{
14678 std::vector<discriminant_range> ranges;
14679
14680 if (variant.default_branch)
14681 return {};
14682
14683 if (variant.discr_list_data == nullptr)
14684 {
14685 discriminant_range r
14686 = {variant.discriminant_value, variant.discriminant_value};
14687 ranges.push_back (r);
14688 }
14689 else
14690 {
14691 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14692 variant.discr_list_data->size);
14693 while (!data.empty ())
14694 {
14695 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14696 {
14697 complaint (_("invalid discriminant marker: %d"), data[0]);
14698 break;
14699 }
14700 bool is_range = data[0] == DW_DSC_range;
14701 data = data.slice (1);
14702
14703 ULONGEST low, high;
14704 unsigned int bytes_read;
14705
14706 if (data.empty ())
14707 {
14708 complaint (_("DW_AT_discr_list missing low value"));
14709 break;
14710 }
14711 if (is_unsigned)
14712 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14713 else
14714 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14715 &bytes_read);
14716 data = data.slice (bytes_read);
14717
14718 if (is_range)
14719 {
14720 if (data.empty ())
14721 {
14722 complaint (_("DW_AT_discr_list missing high value"));
14723 break;
14724 }
14725 if (is_unsigned)
14726 high = read_unsigned_leb128 (nullptr, data.data (),
14727 &bytes_read);
14728 else
14729 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14730 &bytes_read);
14731 data = data.slice (bytes_read);
14732 }
14733 else
14734 high = low;
14735
14736 ranges.push_back ({ low, high });
14737 }
14738 }
14739
14740 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14741 ranges.size ());
14742 std::copy (ranges.begin (), ranges.end (), result);
14743 return gdb::array_view<discriminant_range> (result, ranges.size ());
14744}
14745
14746static const gdb::array_view<variant_part> create_variant_parts
14747 (struct obstack *obstack,
14748 const offset_map_type &offset_map,
14749 struct field_info *fi,
14750 const std::vector<variant_part_builder> &variant_parts);
14751
14752/* Fill in a "struct variant" for a given variant field. RESULT is
14753 the variant to fill in. OBSTACK is where any needed allocations
14754 will be done. OFFSET_MAP holds the mapping from section offsets to
14755 fields for the type. FI describes the fields of the type we're
14756 processing. FIELD is the variant field we're converting. */
14757
14758static void
14759create_one_variant (variant &result, struct obstack *obstack,
14760 const offset_map_type &offset_map,
14761 struct field_info *fi, const variant_field &field)
14762{
14763 result.discriminants = convert_variant_range (obstack, field, false);
14764 result.first_field = field.first_field + fi->baseclasses.size ();
14765 result.last_field = field.last_field + fi->baseclasses.size ();
14766 result.parts = create_variant_parts (obstack, offset_map, fi,
14767 field.variant_parts);
14768}
14769
14770/* Fill in a "struct variant_part" for a given variant part. RESULT
14771 is the variant part to fill in. OBSTACK is where any needed
14772 allocations will be done. OFFSET_MAP holds the mapping from
14773 section offsets to fields for the type. FI describes the fields of
14774 the type we're processing. BUILDER is the variant part to be
14775 converted. */
14776
14777static void
14778create_one_variant_part (variant_part &result,
14779 struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const variant_part_builder &builder)
14783{
14784 auto iter = offset_map.find (builder.discriminant_offset);
14785 if (iter == offset_map.end ())
14786 {
14787 result.discriminant_index = -1;
14788 /* Doesn't matter. */
14789 result.is_unsigned = false;
14790 }
14791 else
14792 {
14793 result.discriminant_index = iter->second;
14794 result.is_unsigned
14795 = TYPE_UNSIGNED (FIELD_TYPE
14796 (fi->fields[result.discriminant_index].field));
14797 }
14798
14799 size_t n = builder.variants.size ();
14800 variant *output = new (obstack) variant[n];
14801 for (size_t i = 0; i < n; ++i)
14802 create_one_variant (output[i], obstack, offset_map, fi,
14803 builder.variants[i]);
14804
14805 result.variants = gdb::array_view<variant> (output, n);
14806}
14807
14808/* Create a vector of variant parts that can be attached to a type.
14809 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14810 holds the mapping from section offsets to fields for the type. FI
14811 describes the fields of the type we're processing. VARIANT_PARTS
14812 is the vector to convert. */
14813
14814static const gdb::array_view<variant_part>
14815create_variant_parts (struct obstack *obstack,
14816 const offset_map_type &offset_map,
14817 struct field_info *fi,
14818 const std::vector<variant_part_builder> &variant_parts)
14819{
14820 if (variant_parts.empty ())
14821 return {};
14822
14823 size_t n = variant_parts.size ();
14824 variant_part *result = new (obstack) variant_part[n];
14825 for (size_t i = 0; i < n; ++i)
14826 create_one_variant_part (result[i], obstack, offset_map, fi,
14827 variant_parts[i]);
14828
14829 return gdb::array_view<variant_part> (result, n);
14830}
14831
14832/* Compute the variant part vector for FIP, attaching it to TYPE when
14833 done. */
14834
14835static void
14836add_variant_property (struct field_info *fip, struct type *type,
14837 struct dwarf2_cu *cu)
14838{
14839 /* Map section offsets of fields to their field index. Note the
14840 field index here does not take the number of baseclasses into
14841 account. */
14842 offset_map_type offset_map;
14843 for (int i = 0; i < fip->fields.size (); ++i)
14844 offset_map[fip->fields[i].offset] = i;
14845
5e22e966 14846 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14847 gdb::array_view<variant_part> parts
14848 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14849 fip->variant_parts);
14850
14851 struct dynamic_prop prop;
14852 prop.kind = PROP_VARIANT_PARTS;
14853 prop.data.variant_parts
14854 = ((gdb::array_view<variant_part> *)
14855 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14856
5c54719c 14857 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14858}
14859
c906108c
SS
14860/* Create the vector of fields, and attach it to the type. */
14861
14862static void
fba45db2 14863dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14864 struct dwarf2_cu *cu)
c906108c 14865{
317f7127 14866 int nfields = fip->nfields ();
c906108c
SS
14867
14868 /* Record the field count, allocate space for the array of fields,
14869 and create blank accessibility bitfields if necessary. */
5e33d5f4 14870 type->set_num_fields (nfields);
3cabb6b0
SM
14871 type->set_fields
14872 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14873
b4ba55a1 14874 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14875 {
14876 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14877
14878 TYPE_FIELD_PRIVATE_BITS (type) =
14879 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14880 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14881
14882 TYPE_FIELD_PROTECTED_BITS (type) =
14883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14884 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14885
774b6a14
TT
14886 TYPE_FIELD_IGNORE_BITS (type) =
14887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14888 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14889 }
14890
14891 /* If the type has baseclasses, allocate and clear a bit vector for
14892 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14893 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14894 {
be2daae6 14895 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14896 unsigned char *pointer;
c906108c
SS
14897
14898 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14899 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14900 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14901 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14902 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14903 }
14904
9c6a1327
TT
14905 if (!fip->variant_parts.empty ())
14906 add_variant_property (fip, type, cu);
2ddeaf8a 14907
be2daae6
TT
14908 /* Copy the saved-up fields into the field vector. */
14909 for (int i = 0; i < nfields; ++i)
c906108c 14910 {
be2daae6
TT
14911 struct nextfield &field
14912 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14913 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14914
ceacbf6e 14915 type->field (i) = field.field;
be2daae6 14916 switch (field.accessibility)
c906108c 14917 {
c5aa993b 14918 case DW_ACCESS_private:
b4ba55a1 14919 if (cu->language != language_ada)
be2daae6 14920 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14921 break;
c906108c 14922
c5aa993b 14923 case DW_ACCESS_protected:
b4ba55a1 14924 if (cu->language != language_ada)
be2daae6 14925 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14926 break;
c906108c 14927
c5aa993b
JM
14928 case DW_ACCESS_public:
14929 break;
c906108c 14930
c5aa993b
JM
14931 default:
14932 /* Unknown accessibility. Complain and treat it as public. */
14933 {
b98664d3 14934 complaint (_("unsupported accessibility %d"),
be2daae6 14935 field.accessibility);
c5aa993b
JM
14936 }
14937 break;
c906108c 14938 }
be2daae6 14939 if (i < fip->baseclasses.size ())
c906108c 14940 {
be2daae6 14941 switch (field.virtuality)
c906108c 14942 {
c5aa993b
JM
14943 case DW_VIRTUALITY_virtual:
14944 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14945 if (cu->language == language_ada)
a73c6dcd 14946 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14947 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14948 break;
c906108c
SS
14949 }
14950 }
c906108c
SS
14951 }
14952}
14953
7d27a96d
TT
14954/* Return true if this member function is a constructor, false
14955 otherwise. */
14956
14957static int
14958dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14959{
14960 const char *fieldname;
fe978cb0 14961 const char *type_name;
7d27a96d
TT
14962 int len;
14963
14964 if (die->parent == NULL)
14965 return 0;
14966
14967 if (die->parent->tag != DW_TAG_structure_type
14968 && die->parent->tag != DW_TAG_union_type
14969 && die->parent->tag != DW_TAG_class_type)
14970 return 0;
14971
14972 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14973 type_name = dwarf2_name (die->parent, cu);
14974 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14975 return 0;
14976
14977 len = strlen (fieldname);
fe978cb0
PA
14978 return (strncmp (fieldname, type_name, len) == 0
14979 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14980}
14981
e35000a7
TBA
14982/* Check if the given VALUE is a recognized enum
14983 dwarf_defaulted_attribute constant according to DWARF5 spec,
14984 Table 7.24. */
14985
14986static bool
14987is_valid_DW_AT_defaulted (ULONGEST value)
14988{
14989 switch (value)
14990 {
14991 case DW_DEFAULTED_no:
14992 case DW_DEFAULTED_in_class:
14993 case DW_DEFAULTED_out_of_class:
14994 return true;
14995 }
14996
3142e908 14997 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14998 return false;
14999}
15000
c906108c
SS
15001/* Add a member function to the proper fieldlist. */
15002
15003static void
107d2387 15004dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15005 struct type *type, struct dwarf2_cu *cu)
c906108c 15006{
5e22e966 15007 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15008 struct attribute *attr;
c906108c 15009 int i;
be2daae6 15010 struct fnfieldlist *flp = nullptr;
c906108c 15011 struct fn_field *fnp;
15d034d0 15012 const char *fieldname;
f792889a 15013 struct type *this_type;
60d5a603 15014 enum dwarf_access_attribute accessibility;
c906108c 15015
b4ba55a1 15016 if (cu->language == language_ada)
a73c6dcd 15017 error (_("unexpected member function in Ada type"));
b4ba55a1 15018
2df3850c 15019 /* Get name of member function. */
39cbfefa
DJ
15020 fieldname = dwarf2_name (die, cu);
15021 if (fieldname == NULL)
2df3850c 15022 return;
c906108c 15023
c906108c 15024 /* Look up member function name in fieldlist. */
be2daae6 15025 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15026 {
27bfe10e 15027 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15028 {
15029 flp = &fip->fnfieldlists[i];
15030 break;
15031 }
c906108c
SS
15032 }
15033
be2daae6
TT
15034 /* Create a new fnfieldlist if necessary. */
15035 if (flp == nullptr)
c906108c 15036 {
be2daae6
TT
15037 fip->fnfieldlists.emplace_back ();
15038 flp = &fip->fnfieldlists.back ();
c906108c 15039 flp->name = fieldname;
be2daae6 15040 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15041 }
15042
be2daae6
TT
15043 /* Create a new member function field and add it to the vector of
15044 fnfieldlists. */
15045 flp->fnfields.emplace_back ();
15046 fnp = &flp->fnfields.back ();
3da10d80
KS
15047
15048 /* Delay processing of the physname until later. */
9c37b5ae 15049 if (cu->language == language_cplus)
be2daae6
TT
15050 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15051 die, cu);
3da10d80
KS
15052 else
15053 {
1d06ead6 15054 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15055 fnp->physname = physname ? physname : "";
15056 }
15057
c906108c 15058 fnp->type = alloc_type (objfile);
f792889a 15059 this_type = read_type_die (die, cu);
78134374 15060 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15061 {
1f704f76 15062 int nparams = this_type->num_fields ();
c906108c 15063
f792889a 15064 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15065 of the method itself (TYPE_CODE_METHOD). */
15066 smash_to_method_type (fnp->type, type,
f792889a 15067 TYPE_TARGET_TYPE (this_type),
80fc5e77 15068 this_type->fields (),
1f704f76 15069 this_type->num_fields (),
f792889a 15070 TYPE_VARARGS (this_type));
c906108c
SS
15071
15072 /* Handle static member functions.
c5aa993b 15073 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15074 member functions. G++ helps GDB by marking the first
15075 parameter for non-static member functions (which is the this
15076 pointer) as artificial. We obtain this information from
15077 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15078 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15079 fnp->voffset = VOFFSET_STATIC;
15080 }
15081 else
b98664d3 15082 complaint (_("member function type missing for '%s'"),
3da10d80 15083 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15084
15085 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15086 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15087 fnp->fcontext = die_containing_type (die, cu);
c906108c 15088
3e43a32a
MS
15089 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15090 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15091
15092 /* Get accessibility. */
e142c38c 15093 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15094 if (attr != nullptr)
aead7601 15095 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15096 else
15097 accessibility = dwarf2_default_access_attribute (die, cu);
15098 switch (accessibility)
c906108c 15099 {
60d5a603
JK
15100 case DW_ACCESS_private:
15101 fnp->is_private = 1;
15102 break;
15103 case DW_ACCESS_protected:
15104 fnp->is_protected = 1;
15105 break;
c906108c
SS
15106 }
15107
b02dede2 15108 /* Check for artificial methods. */
e142c38c 15109 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15110 if (attr && DW_UNSND (attr) != 0)
15111 fnp->is_artificial = 1;
15112
e35000a7
TBA
15113 /* Check for defaulted methods. */
15114 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15115 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15116 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15117
15118 /* Check for deleted methods. */
15119 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15120 if (attr != nullptr && DW_UNSND (attr) != 0)
15121 fnp->is_deleted = 1;
15122
7d27a96d
TT
15123 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15124
0d564a31 15125 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15126 function. For older versions of GCC, this is an offset in the
15127 appropriate virtual table, as specified by DW_AT_containing_type.
15128 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15129 to the object address. */
15130
e142c38c 15131 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15132 if (attr != nullptr)
8e19ed76 15133 {
4fc6c0d5 15134 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15135 {
aec5aa8b
TT
15136 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15137 {
15138 /* Old-style GCC. */
15139 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15140 }
15141 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15142 || (DW_BLOCK (attr)->size > 1
15143 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15144 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15145 {
aec5aa8b
TT
15146 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15147 if ((fnp->voffset % cu->header.addr_size) != 0)
15148 dwarf2_complex_location_expr_complaint ();
15149 else
15150 fnp->voffset /= cu->header.addr_size;
15151 fnp->voffset += 2;
15152 }
15153 else
15154 dwarf2_complex_location_expr_complaint ();
15155
15156 if (!fnp->fcontext)
7e993ebf
KS
15157 {
15158 /* If there is no `this' field and no DW_AT_containing_type,
15159 we cannot actually find a base class context for the
15160 vtable! */
1f704f76 15161 if (this_type->num_fields () == 0
7e993ebf
KS
15162 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15163 {
b98664d3 15164 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15165 "function \"%s\" (offset %s)"),
15166 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15167 }
15168 else
15169 {
15170 fnp->fcontext
15171 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15172 }
15173 }
aec5aa8b 15174 }
cd6c91b4 15175 else if (attr->form_is_section_offset ())
8e19ed76 15176 {
4d3c2250 15177 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15178 }
15179 else
15180 {
4d3c2250
KB
15181 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15182 fieldname);
8e19ed76 15183 }
0d564a31 15184 }
d48cc9dd
DJ
15185 else
15186 {
15187 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15188 if (attr && DW_UNSND (attr))
15189 {
15190 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15191 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15192 "but the vtable offset is not specified"),
9d8780f0 15193 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15194 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15195 TYPE_CPLUS_DYNAMIC (type) = 1;
15196 }
15197 }
c906108c
SS
15198}
15199
15200/* Create the vector of member function fields, and attach it to the type. */
15201
15202static void
fba45db2 15203dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15204 struct dwarf2_cu *cu)
c906108c 15205{
b4ba55a1 15206 if (cu->language == language_ada)
a73c6dcd 15207 error (_("unexpected member functions in Ada type"));
b4ba55a1 15208
c906108c
SS
15209 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15210 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15211 TYPE_ALLOC (type,
15212 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15213
be2daae6 15214 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15215 {
be2daae6 15216 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15217 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15218
be2daae6
TT
15219 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15220 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15221 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15222 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15223
15224 for (int k = 0; k < nf.fnfields.size (); ++k)
15225 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15226 }
15227
be2daae6 15228 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15229}
15230
1168df01
JB
15231/* Returns non-zero if NAME is the name of a vtable member in CU's
15232 language, zero otherwise. */
15233static int
15234is_vtable_name (const char *name, struct dwarf2_cu *cu)
15235{
15236 static const char vptr[] = "_vptr";
15237
9c37b5ae
TT
15238 /* Look for the C++ form of the vtable. */
15239 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15240 return 1;
15241
15242 return 0;
15243}
15244
c0dd20ea 15245/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15246 functions, with the ABI-specified layout. If TYPE describes
15247 such a structure, smash it into a member function type.
61049d3b
DJ
15248
15249 GCC shouldn't do this; it should just output pointer to member DIEs.
15250 This is GCC PR debug/28767. */
c0dd20ea 15251
0b92b5bb
TT
15252static void
15253quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15254{
09e2d7c7 15255 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15256
15257 /* Check for a structure with no name and two children. */
1f704f76 15258 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15259 return;
c0dd20ea
DJ
15260
15261 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15262 if (TYPE_FIELD_NAME (type, 0) == NULL
15263 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15264 || TYPE_FIELD_NAME (type, 1) == NULL
15265 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15266 return;
c0dd20ea
DJ
15267
15268 /* Find the type of the method. */
0b92b5bb 15269 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15270 if (pfn_type == NULL
78134374
SM
15271 || pfn_type->code () != TYPE_CODE_PTR
15272 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15273 return;
c0dd20ea
DJ
15274
15275 /* Look for the "this" argument. */
15276 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15277 if (pfn_type->num_fields () == 0
0b92b5bb 15278 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15279 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15280 return;
c0dd20ea 15281
09e2d7c7 15282 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15283 new_type = alloc_type (objfile);
09e2d7c7 15284 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15285 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15286 TYPE_VARARGS (pfn_type));
0b92b5bb 15287 smash_to_methodptr_type (type, new_type);
c0dd20ea 15288}
1168df01 15289
2b4424c3
TT
15290/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15291 appropriate error checking and issuing complaints if there is a
15292 problem. */
15293
15294static ULONGEST
15295get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15296{
15297 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15298
15299 if (attr == nullptr)
15300 return 0;
15301
cd6c91b4 15302 if (!attr->form_is_constant ())
2b4424c3 15303 {
b98664d3 15304 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15305 " - DIE at %s [in module %s]"),
15306 sect_offset_str (die->sect_off),
5e22e966 15307 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15308 return 0;
15309 }
15310
15311 ULONGEST align;
15312 if (attr->form == DW_FORM_sdata)
15313 {
15314 LONGEST val = DW_SND (attr);
15315 if (val < 0)
15316 {
b98664d3 15317 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15318 " - DIE at %s [in module %s]"),
15319 sect_offset_str (die->sect_off),
5e22e966 15320 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15321 return 0;
15322 }
15323 align = val;
15324 }
15325 else
15326 align = DW_UNSND (attr);
15327
15328 if (align == 0)
15329 {
b98664d3 15330 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15331 " - DIE at %s [in module %s]"),
15332 sect_offset_str (die->sect_off),
5e22e966 15333 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15334 return 0;
15335 }
15336 if ((align & (align - 1)) != 0)
15337 {
b98664d3 15338 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15339 " - DIE at %s [in module %s]"),
15340 sect_offset_str (die->sect_off),
5e22e966 15341 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15342 return 0;
15343 }
15344
15345 return align;
15346}
15347
15348/* If the DIE has a DW_AT_alignment attribute, use its value to set
15349 the alignment for TYPE. */
15350
15351static void
15352maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15353 struct type *type)
15354{
15355 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15356 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15357 " - DIE at %s [in module %s]"),
15358 sect_offset_str (die->sect_off),
5e22e966 15359 objfile_name (cu->per_objfile->objfile));
2b4424c3 15360}
685b1105 15361
e35000a7
TBA
15362/* Check if the given VALUE is a valid enum dwarf_calling_convention
15363 constant for a type, according to DWARF5 spec, Table 5.5. */
15364
15365static bool
15366is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15367{
15368 switch (value)
15369 {
15370 case DW_CC_normal:
15371 case DW_CC_pass_by_reference:
15372 case DW_CC_pass_by_value:
15373 return true;
15374
15375 default:
15376 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15377 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15378 return false;
15379 }
15380}
15381
d0922fcf
TBA
15382/* Check if the given VALUE is a valid enum dwarf_calling_convention
15383 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15384 also according to GNU-specific values (see include/dwarf2.h). */
15385
15386static bool
15387is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15388{
15389 switch (value)
15390 {
15391 case DW_CC_normal:
15392 case DW_CC_program:
15393 case DW_CC_nocall:
15394 return true;
15395
15396 case DW_CC_GNU_renesas_sh:
15397 case DW_CC_GNU_borland_fastcall_i386:
15398 case DW_CC_GDB_IBM_OpenCL:
15399 return true;
15400
15401 default:
15402 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15403 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15404 return false;
15405 }
15406}
15407
c906108c 15408/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15409 (definition) to create a type for the structure or union. Fill in
15410 the type's name and general properties; the members will not be
83655187
DE
15411 processed until process_structure_scope. A symbol table entry for
15412 the type will also not be done until process_structure_scope (assuming
15413 the type has a name).
c906108c 15414
c767944b
DJ
15415 NOTE: we need to call these functions regardless of whether or not the
15416 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15417 structure or union. This gets the type entered into our set of
83655187 15418 user defined types. */
c906108c 15419
f792889a 15420static struct type *
134d01f1 15421read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15422{
5e22e966 15423 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15424 struct type *type;
15425 struct attribute *attr;
15d034d0 15426 const char *name;
c906108c 15427
348e048f
DE
15428 /* If the definition of this type lives in .debug_types, read that type.
15429 Don't follow DW_AT_specification though, that will take us back up
15430 the chain and we want to go down. */
052c8bb8 15431 attr = die->attr (DW_AT_signature);
435d3d88 15432 if (attr != nullptr)
348e048f 15433 {
ac9ec31b 15434 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15435
ac9ec31b 15436 /* The type's CU may not be the same as CU.
02142a6c 15437 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15438 return set_die_type (die, type, cu);
15439 }
15440
c0dd20ea 15441 type = alloc_type (objfile);
c906108c 15442 INIT_CPLUS_SPECIFIC (type);
93311388 15443
39cbfefa
DJ
15444 name = dwarf2_name (die, cu);
15445 if (name != NULL)
c906108c 15446 {
987504bb 15447 if (cu->language == language_cplus
c44af4eb
TT
15448 || cu->language == language_d
15449 || cu->language == language_rust)
63d06c5c 15450 {
15d034d0 15451 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15452
15453 /* dwarf2_full_name might have already finished building the DIE's
15454 type. If so, there is no need to continue. */
15455 if (get_die_type (die, cu) != NULL)
15456 return get_die_type (die, cu);
15457
d0e39ea2 15458 type->set_name (full_name);
63d06c5c
DC
15459 }
15460 else
15461 {
d8151005
DJ
15462 /* The name is already allocated along with this objfile, so
15463 we don't need to duplicate it for the type. */
d0e39ea2 15464 type->set_name (name);
63d06c5c 15465 }
c906108c
SS
15466 }
15467
15468 if (die->tag == DW_TAG_structure_type)
15469 {
67607e24 15470 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15471 }
15472 else if (die->tag == DW_TAG_union_type)
15473 {
67607e24 15474 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15475 }
15476 else
15477 {
67607e24 15478 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15479 }
15480
0cc2414c
TT
15481 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15482 TYPE_DECLARED_CLASS (type) = 1;
15483
e35000a7
TBA
15484 /* Store the calling convention in the type if it's available in
15485 the die. Otherwise the calling convention remains set to
15486 the default value DW_CC_normal. */
15487 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15488 if (attr != nullptr
15489 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15490 {
15491 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15492 TYPE_CPLUS_CALLING_CONVENTION (type)
15493 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15494 }
15495
e142c38c 15496 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15497 if (attr != nullptr)
c906108c 15498 {
cd6c91b4 15499 if (attr->form_is_constant ())
155bfbd3
JB
15500 TYPE_LENGTH (type) = DW_UNSND (attr);
15501 else
15502 {
f8e89861
TT
15503 struct dynamic_prop prop;
15504 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15505 cu->per_cu->addr_type ()))
5c54719c 15506 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15507 TYPE_LENGTH (type) = 0;
15508 }
c906108c
SS
15509 }
15510 else
15511 {
15512 TYPE_LENGTH (type) = 0;
15513 }
15514
2b4424c3
TT
15515 maybe_set_alignment (cu, die, type);
15516
5230b05a 15517 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15518 {
5230b05a
WT
15519 /* ICC<14 does not output the required DW_AT_declaration on
15520 incomplete types, but gives them a size of zero. */
422b1cb0 15521 TYPE_STUB (type) = 1;
685b1105
JK
15522 }
15523 else
15524 TYPE_STUB_SUPPORTED (type) = 1;
15525
dc718098 15526 if (die_is_declaration (die, cu))
876cecd0 15527 TYPE_STUB (type) = 1;
a6c727b2
DJ
15528 else if (attr == NULL && die->child == NULL
15529 && producer_is_realview (cu->producer))
15530 /* RealView does not output the required DW_AT_declaration
15531 on incomplete types. */
15532 TYPE_STUB (type) = 1;
dc718098 15533
c906108c
SS
15534 /* We need to add the type field to the die immediately so we don't
15535 infinitely recurse when dealing with pointers to the structure
0963b4bd 15536 type within the structure itself. */
1c379e20 15537 set_die_type (die, type, cu);
c906108c 15538
7e314c57
JK
15539 /* set_die_type should be already done. */
15540 set_descriptive_type (type, die, cu);
15541
c767944b
DJ
15542 return type;
15543}
15544
9c6a1327
TT
15545static void handle_struct_member_die
15546 (struct die_info *child_die,
15547 struct type *type,
15548 struct field_info *fi,
15549 std::vector<struct symbol *> *template_args,
15550 struct dwarf2_cu *cu);
15551
15552/* A helper for handle_struct_member_die that handles
15553 DW_TAG_variant_part. */
15554
15555static void
15556handle_variant_part (struct die_info *die, struct type *type,
15557 struct field_info *fi,
15558 std::vector<struct symbol *> *template_args,
15559 struct dwarf2_cu *cu)
15560{
15561 variant_part_builder *new_part;
15562 if (fi->current_variant_part == nullptr)
15563 {
15564 fi->variant_parts.emplace_back ();
15565 new_part = &fi->variant_parts.back ();
15566 }
15567 else if (!fi->current_variant_part->processing_variant)
15568 {
15569 complaint (_("nested DW_TAG_variant_part seen "
15570 "- DIE at %s [in module %s]"),
15571 sect_offset_str (die->sect_off),
5e22e966 15572 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15573 return;
15574 }
15575 else
15576 {
15577 variant_field &current = fi->current_variant_part->variants.back ();
15578 current.variant_parts.emplace_back ();
15579 new_part = &current.variant_parts.back ();
15580 }
15581
15582 /* When we recurse, we want callees to add to this new variant
15583 part. */
15584 scoped_restore save_current_variant_part
15585 = make_scoped_restore (&fi->current_variant_part, new_part);
15586
15587 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15588 if (discr == NULL)
15589 {
15590 /* It's a univariant form, an extension we support. */
15591 }
15592 else if (discr->form_is_ref ())
15593 {
15594 struct dwarf2_cu *target_cu = cu;
15595 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15596
15597 new_part->discriminant_offset = target_die->sect_off;
15598 }
15599 else
15600 {
15601 complaint (_("DW_AT_discr does not have DIE reference form"
15602 " - DIE at %s [in module %s]"),
15603 sect_offset_str (die->sect_off),
5e22e966 15604 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15605 }
15606
15607 for (die_info *child_die = die->child;
15608 child_die != NULL;
15609 child_die = child_die->sibling)
15610 handle_struct_member_die (child_die, type, fi, template_args, cu);
15611}
15612
15613/* A helper for handle_struct_member_die that handles
15614 DW_TAG_variant. */
15615
15616static void
15617handle_variant (struct die_info *die, struct type *type,
15618 struct field_info *fi,
15619 std::vector<struct symbol *> *template_args,
15620 struct dwarf2_cu *cu)
15621{
15622 if (fi->current_variant_part == nullptr)
15623 {
15624 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15625 "- DIE at %s [in module %s]"),
15626 sect_offset_str (die->sect_off),
5e22e966 15627 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15628 return;
15629 }
15630 if (fi->current_variant_part->processing_variant)
15631 {
15632 complaint (_("nested DW_TAG_variant seen "
15633 "- DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
5e22e966 15635 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15636 return;
15637 }
15638
15639 scoped_restore save_processing_variant
15640 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15641 true);
15642
15643 fi->current_variant_part->variants.emplace_back ();
15644 variant_field &variant = fi->current_variant_part->variants.back ();
15645 variant.first_field = fi->fields.size ();
15646
15647 /* In a variant we want to get the discriminant and also add a
15648 field for our sole member child. */
15649 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15650 if (discr == nullptr)
15651 {
15652 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15653 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15654 variant.default_branch = true;
15655 else
15656 variant.discr_list_data = DW_BLOCK (discr);
15657 }
15658 else
15659 variant.discriminant_value = DW_UNSND (discr);
15660
15661 for (die_info *variant_child = die->child;
15662 variant_child != NULL;
15663 variant_child = variant_child->sibling)
15664 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15665
15666 variant.last_field = fi->fields.size ();
15667}
15668
2ddeaf8a
TT
15669/* A helper for process_structure_scope that handles a single member
15670 DIE. */
15671
15672static void
15673handle_struct_member_die (struct die_info *child_die, struct type *type,
15674 struct field_info *fi,
15675 std::vector<struct symbol *> *template_args,
15676 struct dwarf2_cu *cu)
15677{
15678 if (child_die->tag == DW_TAG_member
9c6a1327 15679 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15680 {
15681 /* NOTE: carlton/2002-11-05: A C++ static data member
15682 should be a DW_TAG_member that is a declaration, but
15683 all versions of G++ as of this writing (so through at
15684 least 3.2.1) incorrectly generate DW_TAG_variable
15685 tags for them instead. */
15686 dwarf2_add_field (fi, child_die, cu);
15687 }
15688 else if (child_die->tag == DW_TAG_subprogram)
15689 {
15690 /* Rust doesn't have member functions in the C++ sense.
15691 However, it does emit ordinary functions as children
15692 of a struct DIE. */
15693 if (cu->language == language_rust)
15694 read_func_scope (child_die, cu);
15695 else
15696 {
15697 /* C++ member function. */
15698 dwarf2_add_member_fn (fi, child_die, type, cu);
15699 }
15700 }
15701 else if (child_die->tag == DW_TAG_inheritance)
15702 {
15703 /* C++ base class field. */
15704 dwarf2_add_field (fi, child_die, cu);
15705 }
15706 else if (type_can_define_types (child_die))
15707 dwarf2_add_type_defn (fi, child_die, cu);
15708 else if (child_die->tag == DW_TAG_template_type_param
15709 || child_die->tag == DW_TAG_template_value_param)
15710 {
15711 struct symbol *arg = new_symbol (child_die, NULL, cu);
15712
15713 if (arg != NULL)
15714 template_args->push_back (arg);
15715 }
9c6a1327
TT
15716 else if (child_die->tag == DW_TAG_variant_part)
15717 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15718 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15719 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15720}
15721
c767944b
DJ
15722/* Finish creating a structure or union type, including filling in
15723 its members and creating a symbol for it. */
15724
15725static void
15726process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15727{
5e22e966 15728 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15729 struct die_info *child_die;
c767944b
DJ
15730 struct type *type;
15731
15732 type = get_die_type (die, cu);
15733 if (type == NULL)
15734 type = read_structure_type (die, cu);
15735
3e1d3d8c 15736 bool has_template_parameters = false;
e142c38c 15737 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15738 {
15739 struct field_info fi;
2f4732b0 15740 std::vector<struct symbol *> template_args;
c906108c 15741
639d11d3 15742 child_die = die->child;
c906108c
SS
15743
15744 while (child_die && child_die->tag)
15745 {
2ddeaf8a 15746 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15747 child_die = child_die->sibling;
c906108c
SS
15748 }
15749
34eaf542 15750 /* Attach template arguments to type. */
2f4732b0 15751 if (!template_args.empty ())
34eaf542 15752 {
3e1d3d8c 15753 has_template_parameters = true;
34eaf542 15754 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15755 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15756 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15757 = XOBNEWVEC (&objfile->objfile_obstack,
15758 struct symbol *,
15759 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15760 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15761 template_args.data (),
34eaf542
TT
15762 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15763 * sizeof (struct symbol *)));
34eaf542
TT
15764 }
15765
c906108c 15766 /* Attach fields and member functions to the type. */
317f7127 15767 if (fi.nfields () > 0)
e7c27a73 15768 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15769 if (!fi.fnfieldlists.empty ())
c906108c 15770 {
e7c27a73 15771 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15772
c5aa993b 15773 /* Get the type which refers to the base class (possibly this
c906108c 15774 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15775 class from the DW_AT_containing_type attribute. This use of
15776 DW_AT_containing_type is a GNU extension. */
c906108c 15777
e142c38c 15778 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15779 {
e7c27a73 15780 struct type *t = die_containing_type (die, cu);
c906108c 15781
ae6ae975 15782 set_type_vptr_basetype (type, t);
c906108c
SS
15783 if (type == t)
15784 {
c906108c
SS
15785 int i;
15786
15787 /* Our own class provides vtbl ptr. */
1f704f76 15788 for (i = t->num_fields () - 1;
c906108c
SS
15789 i >= TYPE_N_BASECLASSES (t);
15790 --i)
15791 {
0d5cff50 15792 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15793
1168df01 15794 if (is_vtable_name (fieldname, cu))
c906108c 15795 {
ae6ae975 15796 set_type_vptr_fieldno (type, i);
c906108c
SS
15797 break;
15798 }
15799 }
15800
15801 /* Complain if virtual function table field not found. */
15802 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15803 complaint (_("virtual function table pointer "
3e43a32a 15804 "not found when defining class '%s'"),
7d93a1e0 15805 type->name () ? type->name () : "");
c906108c
SS
15806 }
15807 else
15808 {
ae6ae975 15809 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15810 }
15811 }
f6235d4c 15812 else if (cu->producer
61012eef 15813 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15814 {
15815 /* The IBM XLC compiler does not provide direct indication
15816 of the containing type, but the vtable pointer is
15817 always named __vfp. */
15818
15819 int i;
15820
1f704f76 15821 for (i = type->num_fields () - 1;
f6235d4c
EZ
15822 i >= TYPE_N_BASECLASSES (type);
15823 --i)
15824 {
15825 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15826 {
ae6ae975
DE
15827 set_type_vptr_fieldno (type, i);
15828 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15829 break;
15830 }
15831 }
15832 }
c906108c 15833 }
98751a41
JK
15834
15835 /* Copy fi.typedef_field_list linked list elements content into the
15836 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15837 if (!fi.typedef_field_list.empty ())
98751a41 15838 {
be2daae6 15839 int count = fi.typedef_field_list.size ();
98751a41 15840
a0d7a4ff 15841 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15842 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15843 = ((struct decl_field *)
be2daae6
TT
15844 TYPE_ALLOC (type,
15845 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15846 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15847
be2daae6
TT
15848 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15849 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15850 }
c767944b 15851
883fd55a
KS
15852 /* Copy fi.nested_types_list linked list elements content into the
15853 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15854 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15855 {
be2daae6 15856 int count = fi.nested_types_list.size ();
883fd55a
KS
15857
15858 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15859 TYPE_NESTED_TYPES_ARRAY (type)
15860 = ((struct decl_field *)
be2daae6
TT
15861 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15862 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15863
be2daae6
TT
15864 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15865 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15866 }
c906108c 15867 }
63d06c5c 15868
bb5ed363 15869 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15870 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15871 cu->rust_unions.push_back (type);
0b92b5bb 15872
90aeadfc
DC
15873 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15874 snapshots) has been known to create a die giving a declaration
15875 for a class that has, as a child, a die giving a definition for a
15876 nested class. So we have to process our children even if the
15877 current die is a declaration. Normally, of course, a declaration
15878 won't have any children at all. */
134d01f1 15879
ca040673
DE
15880 child_die = die->child;
15881
90aeadfc
DC
15882 while (child_die != NULL && child_die->tag)
15883 {
15884 if (child_die->tag == DW_TAG_member
15885 || child_die->tag == DW_TAG_variable
34eaf542
TT
15886 || child_die->tag == DW_TAG_inheritance
15887 || child_die->tag == DW_TAG_template_value_param
15888 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15889 {
90aeadfc 15890 /* Do nothing. */
134d01f1 15891 }
90aeadfc
DC
15892 else
15893 process_die (child_die, cu);
134d01f1 15894
436c571c 15895 child_die = child_die->sibling;
134d01f1
DJ
15896 }
15897
fa4028e9
JB
15898 /* Do not consider external references. According to the DWARF standard,
15899 these DIEs are identified by the fact that they have no byte_size
15900 attribute, and a declaration attribute. */
15901 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15902 || !die_is_declaration (die, cu)
15903 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15904 {
15905 struct symbol *sym = new_symbol (die, type, cu);
15906
15907 if (has_template_parameters)
15908 {
a776957c
TT
15909 struct symtab *symtab;
15910 if (sym != nullptr)
15911 symtab = symbol_symtab (sym);
15912 else if (cu->line_header != nullptr)
15913 {
15914 /* Any related symtab will do. */
15915 symtab
7ba99d21 15916 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15917 }
15918 else
15919 {
15920 symtab = nullptr;
15921 complaint (_("could not find suitable "
15922 "symtab for template parameter"
15923 " - DIE at %s [in module %s]"),
15924 sect_offset_str (die->sect_off),
15925 objfile_name (objfile));
15926 }
15927
15928 if (symtab != nullptr)
15929 {
15930 /* Make sure that the symtab is set on the new symbols.
15931 Even though they don't appear in this symtab directly,
15932 other parts of gdb assume that symbols do, and this is
15933 reasonably true. */
15934 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15935 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15936 }
3e1d3d8c
TT
15937 }
15938 }
134d01f1
DJ
15939}
15940
ed6acedd
TT
15941/* Assuming DIE is an enumeration type, and TYPE is its associated
15942 type, update TYPE using some information only available in DIE's
15943 children. In particular, the fields are computed. */
55426c9d
JB
15944
15945static void
15946update_enumeration_type_from_children (struct die_info *die,
15947 struct type *type,
15948 struct dwarf2_cu *cu)
15949{
60f7655a 15950 struct die_info *child_die;
55426c9d
JB
15951 int unsigned_enum = 1;
15952 int flag_enum = 1;
55426c9d 15953
8268c778 15954 auto_obstack obstack;
ed6acedd 15955 std::vector<struct field> fields;
55426c9d 15956
60f7655a
DE
15957 for (child_die = die->child;
15958 child_die != NULL && child_die->tag;
436c571c 15959 child_die = child_die->sibling)
55426c9d
JB
15960 {
15961 struct attribute *attr;
15962 LONGEST value;
15963 const gdb_byte *bytes;
15964 struct dwarf2_locexpr_baton *baton;
15965 const char *name;
60f7655a 15966
55426c9d
JB
15967 if (child_die->tag != DW_TAG_enumerator)
15968 continue;
15969
15970 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15971 if (attr == NULL)
15972 continue;
15973
15974 name = dwarf2_name (child_die, cu);
15975 if (name == NULL)
15976 name = "<anonymous enumerator>";
15977
15978 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15979 &value, &bytes, &baton);
15980 if (value < 0)
15981 {
15982 unsigned_enum = 0;
15983 flag_enum = 0;
15984 }
55426c9d 15985 else
edd45eb0
SM
15986 {
15987 if (count_one_bits_ll (value) >= 2)
15988 flag_enum = 0;
edd45eb0 15989 }
55426c9d 15990
ed6acedd
TT
15991 fields.emplace_back ();
15992 struct field &field = fields.back ();
15993 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15994 SET_FIELD_ENUMVAL (field, value);
15995 }
15996
15997 if (!fields.empty ())
15998 {
5e33d5f4 15999 type->set_num_fields (fields.size ());
3cabb6b0
SM
16000 type->set_fields
16001 ((struct field *)
16002 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16003 memcpy (type->fields (), fields.data (),
ed6acedd 16004 sizeof (struct field) * fields.size ());
55426c9d
JB
16005 }
16006
16007 if (unsigned_enum)
16008 TYPE_UNSIGNED (type) = 1;
16009 if (flag_enum)
16010 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16011}
16012
134d01f1
DJ
16013/* Given a DW_AT_enumeration_type die, set its type. We do not
16014 complete the type's fields yet, or create any symbols. */
c906108c 16015
f792889a 16016static struct type *
134d01f1 16017read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16018{
5e22e966 16019 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16020 struct type *type;
c906108c 16021 struct attribute *attr;
0114d602 16022 const char *name;
134d01f1 16023
348e048f
DE
16024 /* If the definition of this type lives in .debug_types, read that type.
16025 Don't follow DW_AT_specification though, that will take us back up
16026 the chain and we want to go down. */
052c8bb8 16027 attr = die->attr (DW_AT_signature);
435d3d88 16028 if (attr != nullptr)
348e048f 16029 {
ac9ec31b 16030 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16031
ac9ec31b 16032 /* The type's CU may not be the same as CU.
02142a6c 16033 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16034 return set_die_type (die, type, cu);
16035 }
16036
c906108c
SS
16037 type = alloc_type (objfile);
16038
67607e24 16039 type->set_code (TYPE_CODE_ENUM);
94af9270 16040 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16041 if (name != NULL)
d0e39ea2 16042 type->set_name (name);
c906108c 16043
0626fc76
TT
16044 attr = dwarf2_attr (die, DW_AT_type, cu);
16045 if (attr != NULL)
16046 {
16047 struct type *underlying_type = die_type (die, cu);
16048
16049 TYPE_TARGET_TYPE (type) = underlying_type;
16050 }
16051
e142c38c 16052 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16053 if (attr != nullptr)
c906108c
SS
16054 {
16055 TYPE_LENGTH (type) = DW_UNSND (attr);
16056 }
16057 else
16058 {
16059 TYPE_LENGTH (type) = 0;
16060 }
16061
2b4424c3
TT
16062 maybe_set_alignment (cu, die, type);
16063
137033e9
JB
16064 /* The enumeration DIE can be incomplete. In Ada, any type can be
16065 declared as private in the package spec, and then defined only
16066 inside the package body. Such types are known as Taft Amendment
16067 Types. When another package uses such a type, an incomplete DIE
16068 may be generated by the compiler. */
02eb380e 16069 if (die_is_declaration (die, cu))
876cecd0 16070 TYPE_STUB (type) = 1;
02eb380e 16071
0626fc76
TT
16072 /* If this type has an underlying type that is not a stub, then we
16073 may use its attributes. We always use the "unsigned" attribute
16074 in this situation, because ordinarily we guess whether the type
16075 is unsigned -- but the guess can be wrong and the underlying type
16076 can tell us the reality. However, we defer to a local size
16077 attribute if one exists, because this lets the compiler override
16078 the underlying type if needed. */
16079 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16080 {
9e7c9a03
HD
16081 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16082 underlying_type = check_typedef (underlying_type);
16083 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16084 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16085 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16086 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16087 && TYPE_RAW_ALIGN (underlying_type) != 0)
16088 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16089 }
16090
3d567982
TT
16091 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16092
ed6acedd
TT
16093 set_die_type (die, type, cu);
16094
16095 /* Finish the creation of this type by using the enum's children.
16096 Note that, as usual, this must come after set_die_type to avoid
16097 infinite recursion when trying to compute the names of the
16098 enumerators. */
16099 update_enumeration_type_from_children (die, type, cu);
16100
16101 return type;
134d01f1
DJ
16102}
16103
16104/* Given a pointer to a die which begins an enumeration, process all
16105 the dies that define the members of the enumeration, and create the
16106 symbol for the enumeration type.
16107
16108 NOTE: We reverse the order of the element list. */
16109
16110static void
16111process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16112{
f792889a 16113 struct type *this_type;
134d01f1 16114
f792889a
DJ
16115 this_type = get_die_type (die, cu);
16116 if (this_type == NULL)
16117 this_type = read_enumeration_type (die, cu);
9dc481d3 16118
639d11d3 16119 if (die->child != NULL)
c906108c 16120 {
9dc481d3 16121 struct die_info *child_die;
15d034d0 16122 const char *name;
9dc481d3 16123
639d11d3 16124 child_die = die->child;
c906108c
SS
16125 while (child_die && child_die->tag)
16126 {
16127 if (child_die->tag != DW_TAG_enumerator)
16128 {
e7c27a73 16129 process_die (child_die, cu);
c906108c
SS
16130 }
16131 else
16132 {
39cbfefa
DJ
16133 name = dwarf2_name (child_die, cu);
16134 if (name)
ed6acedd 16135 new_symbol (child_die, this_type, cu);
c906108c
SS
16136 }
16137
436c571c 16138 child_die = child_die->sibling;
c906108c 16139 }
c906108c 16140 }
134d01f1 16141
6c83ed52
TT
16142 /* If we are reading an enum from a .debug_types unit, and the enum
16143 is a declaration, and the enum is not the signatured type in the
16144 unit, then we do not want to add a symbol for it. Adding a
16145 symbol would in some cases obscure the true definition of the
16146 enum, giving users an incomplete type when the definition is
16147 actually available. Note that we do not want to do this for all
16148 enums which are just declarations, because C++0x allows forward
16149 enum declarations. */
3019eac3 16150 if (cu->per_cu->is_debug_types
6c83ed52
TT
16151 && die_is_declaration (die, cu))
16152 {
52dc124a 16153 struct signatured_type *sig_type;
6c83ed52 16154
c0f78cd4 16155 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16156 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16157 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16158 return;
16159 }
16160
f792889a 16161 new_symbol (die, this_type, cu);
c906108c
SS
16162}
16163
16164/* Extract all information from a DW_TAG_array_type DIE and put it in
16165 the DIE's type field. For now, this only handles one dimensional
16166 arrays. */
16167
f792889a 16168static struct type *
e7c27a73 16169read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16170{
5e22e966 16171 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16172 struct die_info *child_die;
7e314c57 16173 struct type *type;
c906108c 16174 struct type *element_type, *range_type, *index_type;
c906108c 16175 struct attribute *attr;
15d034d0 16176 const char *name;
a405673c 16177 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16178 unsigned int bit_stride = 0;
c906108c 16179
e7c27a73 16180 element_type = die_type (die, cu);
c906108c 16181
7e314c57
JK
16182 /* The die_type call above may have already set the type for this DIE. */
16183 type = get_die_type (die, cu);
16184 if (type)
16185 return type;
16186
dc53a7ad
JB
16187 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16188 if (attr != NULL)
a405673c
JB
16189 {
16190 int stride_ok;
09ba997f 16191 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16192
16193 byte_stride_prop
16194 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16195 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16196 prop_type);
a405673c
JB
16197 if (!stride_ok)
16198 {
b98664d3 16199 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16200 " - DIE at %s [in module %s]"),
16201 sect_offset_str (die->sect_off),
5e22e966 16202 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16203 /* Ignore this attribute. We will likely not be able to print
16204 arrays of this type correctly, but there is little we can do
16205 to help if we cannot read the attribute's value. */
16206 byte_stride_prop = NULL;
16207 }
16208 }
dc53a7ad
JB
16209
16210 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16211 if (attr != NULL)
16212 bit_stride = DW_UNSND (attr);
16213
c906108c
SS
16214 /* Irix 6.2 native cc creates array types without children for
16215 arrays with unspecified length. */
639d11d3 16216 if (die->child == NULL)
c906108c 16217 {
46bf5051 16218 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16219 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16220 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16221 byte_stride_prop, bit_stride);
f792889a 16222 return set_die_type (die, type, cu);
c906108c
SS
16223 }
16224
791afaa2 16225 std::vector<struct type *> range_types;
639d11d3 16226 child_die = die->child;
c906108c
SS
16227 while (child_die && child_die->tag)
16228 {
16229 if (child_die->tag == DW_TAG_subrange_type)
16230 {
f792889a 16231 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16232
f792889a 16233 if (child_type != NULL)
a02abb62 16234 {
0963b4bd
MS
16235 /* The range type was succesfully read. Save it for the
16236 array type creation. */
791afaa2 16237 range_types.push_back (child_type);
a02abb62 16238 }
c906108c 16239 }
436c571c 16240 child_die = child_die->sibling;
c906108c
SS
16241 }
16242
16243 /* Dwarf2 dimensions are output from left to right, create the
16244 necessary array types in backwards order. */
7ca2d3a3 16245
c906108c 16246 type = element_type;
7ca2d3a3
DL
16247
16248 if (read_array_order (die, cu) == DW_ORD_col_major)
16249 {
16250 int i = 0;
9a619af0 16251
791afaa2 16252 while (i < range_types.size ())
dc53a7ad 16253 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16254 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16255 }
16256 else
16257 {
791afaa2 16258 size_t ndim = range_types.size ();
7ca2d3a3 16259 while (ndim-- > 0)
dc53a7ad 16260 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16261 byte_stride_prop, bit_stride);
7ca2d3a3 16262 }
c906108c 16263
f5f8a009
EZ
16264 /* Understand Dwarf2 support for vector types (like they occur on
16265 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16266 array type. This is not part of the Dwarf2/3 standard yet, but a
16267 custom vendor extension. The main difference between a regular
16268 array and the vector variant is that vectors are passed by value
16269 to functions. */
e142c38c 16270 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16271 if (attr != nullptr)
ea37ba09 16272 make_vector_type (type);
f5f8a009 16273
dbc98a8b
KW
16274 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16275 implementation may choose to implement triple vectors using this
16276 attribute. */
16277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16278 if (attr != nullptr)
dbc98a8b
KW
16279 {
16280 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16281 TYPE_LENGTH (type) = DW_UNSND (attr);
16282 else
b98664d3 16283 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16284 "than the total size of elements"));
dbc98a8b
KW
16285 }
16286
39cbfefa
DJ
16287 name = dwarf2_name (die, cu);
16288 if (name)
d0e39ea2 16289 type->set_name (name);
6e70227d 16290
2b4424c3
TT
16291 maybe_set_alignment (cu, die, type);
16292
0963b4bd 16293 /* Install the type in the die. */
7e314c57
JK
16294 set_die_type (die, type, cu);
16295
16296 /* set_die_type should be already done. */
b4ba55a1
JB
16297 set_descriptive_type (type, die, cu);
16298
7e314c57 16299 return type;
c906108c
SS
16300}
16301
7ca2d3a3 16302static enum dwarf_array_dim_ordering
6e70227d 16303read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16304{
16305 struct attribute *attr;
16306
16307 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16308
435d3d88 16309 if (attr != nullptr)
aead7601 16310 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16311
0963b4bd
MS
16312 /* GNU F77 is a special case, as at 08/2004 array type info is the
16313 opposite order to the dwarf2 specification, but data is still
16314 laid out as per normal fortran.
7ca2d3a3 16315
0963b4bd
MS
16316 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16317 version checking. */
7ca2d3a3 16318
905e0470
PM
16319 if (cu->language == language_fortran
16320 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16321 {
16322 return DW_ORD_row_major;
16323 }
16324
6e70227d 16325 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16326 {
16327 case array_column_major:
16328 return DW_ORD_col_major;
16329 case array_row_major:
16330 default:
16331 return DW_ORD_row_major;
16332 };
16333}
16334
72019c9c 16335/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16336 the DIE's type field. */
72019c9c 16337
f792889a 16338static struct type *
72019c9c
GM
16339read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16340{
7e314c57
JK
16341 struct type *domain_type, *set_type;
16342 struct attribute *attr;
f792889a 16343
7e314c57
JK
16344 domain_type = die_type (die, cu);
16345
16346 /* The die_type call above may have already set the type for this DIE. */
16347 set_type = get_die_type (die, cu);
16348 if (set_type)
16349 return set_type;
16350
16351 set_type = create_set_type (NULL, domain_type);
16352
16353 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16354 if (attr != nullptr)
d09039dd 16355 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16356
2b4424c3
TT
16357 maybe_set_alignment (cu, die, set_type);
16358
f792889a 16359 return set_die_type (die, set_type, cu);
72019c9c 16360}
7ca2d3a3 16361
0971de02
TT
16362/* A helper for read_common_block that creates a locexpr baton.
16363 SYM is the symbol which we are marking as computed.
16364 COMMON_DIE is the DIE for the common block.
16365 COMMON_LOC is the location expression attribute for the common
16366 block itself.
16367 MEMBER_LOC is the location expression attribute for the particular
16368 member of the common block that we are processing.
16369 CU is the CU from which the above come. */
16370
16371static void
16372mark_common_block_symbol_computed (struct symbol *sym,
16373 struct die_info *common_die,
16374 struct attribute *common_loc,
16375 struct attribute *member_loc,
16376 struct dwarf2_cu *cu)
16377{
5e22e966 16378 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16379 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16380 struct dwarf2_locexpr_baton *baton;
16381 gdb_byte *ptr;
16382 unsigned int cu_off;
08feed99 16383 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16384 LONGEST offset = 0;
16385
16386 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16387 gdb_assert (common_loc->form_is_block ());
16388 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16389 || member_loc->form_is_constant ());
0971de02 16390
8d749320 16391 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16392 baton->per_objfile = per_objfile;
0971de02
TT
16393 baton->per_cu = cu->per_cu;
16394 gdb_assert (baton->per_cu);
16395
16396 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16397
cd6c91b4 16398 if (member_loc->form_is_constant ())
0971de02 16399 {
0826b30a 16400 offset = member_loc->constant_value (0);
0971de02
TT
16401 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16402 }
16403 else
16404 baton->size += DW_BLOCK (member_loc)->size;
16405
224c3ddb 16406 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16407 baton->data = ptr;
16408
16409 *ptr++ = DW_OP_call4;
9c541725 16410 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16411 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16412 ptr += 4;
16413
cd6c91b4 16414 if (member_loc->form_is_constant ())
0971de02
TT
16415 {
16416 *ptr++ = DW_OP_addr;
16417 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16418 ptr += cu->header.addr_size;
16419 }
16420 else
16421 {
16422 /* We have to copy the data here, because DW_OP_call4 will only
16423 use a DW_AT_location attribute. */
16424 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16425 ptr += DW_BLOCK (member_loc)->size;
16426 }
16427
16428 *ptr++ = DW_OP_plus;
16429 gdb_assert (ptr - baton->data == baton->size);
16430
0971de02 16431 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16432 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16433}
16434
4357ac6c
TT
16435/* Create appropriate locally-scoped variables for all the
16436 DW_TAG_common_block entries. Also create a struct common_block
16437 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16438 is used to separate the common blocks name namespace from regular
4357ac6c 16439 variable names. */
c906108c
SS
16440
16441static void
e7c27a73 16442read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16443{
0971de02
TT
16444 struct attribute *attr;
16445
16446 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16447 if (attr != nullptr)
0971de02
TT
16448 {
16449 /* Support the .debug_loc offsets. */
4fc6c0d5 16450 if (attr->form_is_block ())
0971de02
TT
16451 {
16452 /* Ok. */
16453 }
cd6c91b4 16454 else if (attr->form_is_section_offset ())
0971de02
TT
16455 {
16456 dwarf2_complex_location_expr_complaint ();
16457 attr = NULL;
16458 }
16459 else
16460 {
16461 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16462 "common block member");
16463 attr = NULL;
16464 }
16465 }
16466
639d11d3 16467 if (die->child != NULL)
c906108c 16468 {
5e22e966 16469 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16470 struct die_info *child_die;
16471 size_t n_entries = 0, size;
16472 struct common_block *common_block;
16473 struct symbol *sym;
74ac6d43 16474
4357ac6c
TT
16475 for (child_die = die->child;
16476 child_die && child_die->tag;
436c571c 16477 child_die = child_die->sibling)
4357ac6c
TT
16478 ++n_entries;
16479
16480 size = (sizeof (struct common_block)
16481 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16482 common_block
16483 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16484 size);
4357ac6c
TT
16485 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16486 common_block->n_entries = 0;
16487
16488 for (child_die = die->child;
16489 child_die && child_die->tag;
436c571c 16490 child_die = child_die->sibling)
4357ac6c
TT
16491 {
16492 /* Create the symbol in the DW_TAG_common_block block in the current
16493 symbol scope. */
e7c27a73 16494 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16495 if (sym != NULL)
16496 {
16497 struct attribute *member_loc;
16498
16499 common_block->contents[common_block->n_entries++] = sym;
16500
16501 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16502 cu);
16503 if (member_loc)
16504 {
16505 /* GDB has handled this for a long time, but it is
16506 not specified by DWARF. It seems to have been
16507 emitted by gfortran at least as recently as:
16508 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16509 complaint (_("Variable in common block has "
0971de02 16510 "DW_AT_data_member_location "
9d8780f0
SM
16511 "- DIE at %s [in module %s]"),
16512 sect_offset_str (child_die->sect_off),
518817b3 16513 objfile_name (objfile));
0971de02 16514
cd6c91b4 16515 if (member_loc->form_is_section_offset ())
0971de02 16516 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16517 else if (member_loc->form_is_constant ()
4fc6c0d5 16518 || member_loc->form_is_block ())
0971de02 16519 {
435d3d88 16520 if (attr != nullptr)
0971de02
TT
16521 mark_common_block_symbol_computed (sym, die, attr,
16522 member_loc, cu);
16523 }
16524 else
16525 dwarf2_complex_location_expr_complaint ();
16526 }
16527 }
c906108c 16528 }
4357ac6c
TT
16529
16530 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16531 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16532 }
16533}
16534
0114d602 16535/* Create a type for a C++ namespace. */
d9fa45fe 16536
0114d602
DJ
16537static struct type *
16538read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16539{
5e22e966 16540 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16541 const char *previous_prefix, *name;
9219021c 16542 int is_anonymous;
0114d602
DJ
16543 struct type *type;
16544
16545 /* For extensions, reuse the type of the original namespace. */
16546 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16547 {
16548 struct die_info *ext_die;
16549 struct dwarf2_cu *ext_cu = cu;
9a619af0 16550
0114d602
DJ
16551 ext_die = dwarf2_extension (die, &ext_cu);
16552 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16553
16554 /* EXT_CU may not be the same as CU.
02142a6c 16555 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16556 return set_die_type (die, type, cu);
16557 }
9219021c 16558
e142c38c 16559 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16560
16561 /* Now build the name of the current namespace. */
16562
0114d602
DJ
16563 previous_prefix = determine_prefix (die, cu);
16564 if (previous_prefix[0] != '\0')
16565 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16566 previous_prefix, name, 0, cu);
0114d602
DJ
16567
16568 /* Create the type. */
19f392bc 16569 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16570
60531b24 16571 return set_die_type (die, type, cu);
0114d602
DJ
16572}
16573
22cee43f 16574/* Read a namespace scope. */
0114d602
DJ
16575
16576static void
16577read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16578{
5e22e966 16579 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16580 int is_anonymous;
9219021c 16581
5c4e30ca
DC
16582 /* Add a symbol associated to this if we haven't seen the namespace
16583 before. Also, add a using directive if it's an anonymous
16584 namespace. */
9219021c 16585
f2f0e013 16586 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16587 {
16588 struct type *type;
16589
0114d602 16590 type = read_type_die (die, cu);
e7c27a73 16591 new_symbol (die, type, cu);
5c4e30ca 16592
e8e80198 16593 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16594 if (is_anonymous)
0114d602
DJ
16595 {
16596 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16597
eb1e02fd 16598 std::vector<const char *> excludes;
804d2729 16599 add_using_directive (using_directives (cu),
7d93a1e0 16600 previous_prefix, type->name (), NULL,
eb1e02fd 16601 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16602 }
5c4e30ca 16603 }
9219021c 16604
639d11d3 16605 if (die->child != NULL)
d9fa45fe 16606 {
639d11d3 16607 struct die_info *child_die = die->child;
6e70227d 16608
d9fa45fe
DC
16609 while (child_die && child_die->tag)
16610 {
e7c27a73 16611 process_die (child_die, cu);
436c571c 16612 child_die = child_die->sibling;
d9fa45fe
DC
16613 }
16614 }
38d518c9
EZ
16615}
16616
f55ee35c
JK
16617/* Read a Fortran module as type. This DIE can be only a declaration used for
16618 imported module. Still we need that type as local Fortran "use ... only"
16619 declaration imports depend on the created type in determine_prefix. */
16620
16621static struct type *
16622read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16623{
5e22e966 16624 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16625 const char *module_name;
f55ee35c
JK
16626 struct type *type;
16627
16628 module_name = dwarf2_name (die, cu);
19f392bc 16629 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16630
f55ee35c
JK
16631 return set_die_type (die, type, cu);
16632}
16633
5d7cb8df
JK
16634/* Read a Fortran module. */
16635
16636static void
16637read_module (struct die_info *die, struct dwarf2_cu *cu)
16638{
16639 struct die_info *child_die = die->child;
530e8392
KB
16640 struct type *type;
16641
16642 type = read_type_die (die, cu);
16643 new_symbol (die, type, cu);
5d7cb8df 16644
5d7cb8df
JK
16645 while (child_die && child_die->tag)
16646 {
16647 process_die (child_die, cu);
436c571c 16648 child_die = child_die->sibling;
5d7cb8df
JK
16649 }
16650}
16651
38d518c9
EZ
16652/* Return the name of the namespace represented by DIE. Set
16653 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16654 namespace. */
16655
16656static const char *
e142c38c 16657namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16658{
16659 struct die_info *current_die;
16660 const char *name = NULL;
16661
16662 /* Loop through the extensions until we find a name. */
16663
16664 for (current_die = die;
16665 current_die != NULL;
f2f0e013 16666 current_die = dwarf2_extension (die, &cu))
38d518c9 16667 {
96553a0c
DE
16668 /* We don't use dwarf2_name here so that we can detect the absence
16669 of a name -> anonymous namespace. */
7d45c7c3 16670 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16671
38d518c9
EZ
16672 if (name != NULL)
16673 break;
16674 }
16675
16676 /* Is it an anonymous namespace? */
16677
16678 *is_anonymous = (name == NULL);
16679 if (*is_anonymous)
2b1dbab0 16680 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16681
16682 return name;
d9fa45fe
DC
16683}
16684
c906108c
SS
16685/* Extract all information from a DW_TAG_pointer_type DIE and add to
16686 the user defined type vector. */
16687
f792889a 16688static struct type *
e7c27a73 16689read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16690{
5e22e966 16691 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16692 struct comp_unit_head *cu_header = &cu->header;
c906108c 16693 struct type *type;
8b2dbe47
KB
16694 struct attribute *attr_byte_size;
16695 struct attribute *attr_address_class;
16696 int byte_size, addr_class;
7e314c57
JK
16697 struct type *target_type;
16698
16699 target_type = die_type (die, cu);
c906108c 16700
7e314c57
JK
16701 /* The die_type call above may have already set the type for this DIE. */
16702 type = get_die_type (die, cu);
16703 if (type)
16704 return type;
16705
16706 type = lookup_pointer_type (target_type);
8b2dbe47 16707
e142c38c 16708 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16709 if (attr_byte_size)
16710 byte_size = DW_UNSND (attr_byte_size);
c906108c 16711 else
8b2dbe47
KB
16712 byte_size = cu_header->addr_size;
16713
e142c38c 16714 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16715 if (attr_address_class)
16716 addr_class = DW_UNSND (attr_address_class);
16717 else
16718 addr_class = DW_ADDR_none;
16719
2b4424c3
TT
16720 ULONGEST alignment = get_alignment (cu, die);
16721
16722 /* If the pointer size, alignment, or address class is different
16723 than the default, create a type variant marked as such and set
16724 the length accordingly. */
16725 if (TYPE_LENGTH (type) != byte_size
16726 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16727 && alignment != TYPE_RAW_ALIGN (type))
16728 || addr_class != DW_ADDR_none)
c906108c 16729 {
5e2b427d 16730 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16731 {
16732 int type_flags;
16733
849957d9 16734 type_flags = gdbarch_address_class_type_flags
5e2b427d 16735 (gdbarch, byte_size, addr_class);
876cecd0
TT
16736 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16737 == 0);
8b2dbe47
KB
16738 type = make_type_with_address_space (type, type_flags);
16739 }
16740 else if (TYPE_LENGTH (type) != byte_size)
16741 {
b98664d3 16742 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16743 }
2b4424c3
TT
16744 else if (TYPE_RAW_ALIGN (type) != alignment)
16745 {
b98664d3 16746 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16747 " - DIE at %s [in module %s]"),
16748 sect_offset_str (die->sect_off),
5e22e966 16749 objfile_name (cu->per_objfile->objfile));
2b4424c3 16750 }
6e70227d 16751 else
9a619af0
MS
16752 {
16753 /* Should we also complain about unhandled address classes? */
16754 }
c906108c 16755 }
8b2dbe47
KB
16756
16757 TYPE_LENGTH (type) = byte_size;
2b4424c3 16758 set_type_align (type, alignment);
f792889a 16759 return set_die_type (die, type, cu);
c906108c
SS
16760}
16761
16762/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16763 the user defined type vector. */
16764
f792889a 16765static struct type *
e7c27a73 16766read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16767{
16768 struct type *type;
16769 struct type *to_type;
16770 struct type *domain;
16771
e7c27a73
DJ
16772 to_type = die_type (die, cu);
16773 domain = die_containing_type (die, cu);
0d5de010 16774
7e314c57
JK
16775 /* The calls above may have already set the type for this DIE. */
16776 type = get_die_type (die, cu);
16777 if (type)
16778 return type;
16779
78134374 16780 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16781 type = lookup_methodptr_type (to_type);
78134374 16782 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16783 {
5e22e966 16784 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16785
16786 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16787 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16788 TYPE_VARARGS (to_type));
16789 type = lookup_methodptr_type (new_type);
16790 }
0d5de010
DJ
16791 else
16792 type = lookup_memberptr_type (to_type, domain);
c906108c 16793
f792889a 16794 return set_die_type (die, type, cu);
c906108c
SS
16795}
16796
4297a3f0 16797/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16798 the user defined type vector. */
16799
f792889a 16800static struct type *
4297a3f0
AV
16801read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16802 enum type_code refcode)
c906108c 16803{
e7c27a73 16804 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16805 struct type *type, *target_type;
c906108c
SS
16806 struct attribute *attr;
16807
4297a3f0
AV
16808 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16809
7e314c57
JK
16810 target_type = die_type (die, cu);
16811
16812 /* The die_type call above may have already set the type for this DIE. */
16813 type = get_die_type (die, cu);
16814 if (type)
16815 return type;
16816
4297a3f0 16817 type = lookup_reference_type (target_type, refcode);
e142c38c 16818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16819 if (attr != nullptr)
c906108c
SS
16820 {
16821 TYPE_LENGTH (type) = DW_UNSND (attr);
16822 }
16823 else
16824 {
107d2387 16825 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16826 }
2b4424c3 16827 maybe_set_alignment (cu, die, type);
f792889a 16828 return set_die_type (die, type, cu);
c906108c
SS
16829}
16830
cf363f18
MW
16831/* Add the given cv-qualifiers to the element type of the array. GCC
16832 outputs DWARF type qualifiers that apply to an array, not the
16833 element type. But GDB relies on the array element type to carry
16834 the cv-qualifiers. This mimics section 6.7.3 of the C99
16835 specification. */
16836
16837static struct type *
16838add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16839 struct type *base_type, int cnst, int voltl)
16840{
16841 struct type *el_type, *inner_array;
16842
16843 base_type = copy_type (base_type);
16844 inner_array = base_type;
16845
78134374 16846 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16847 {
16848 TYPE_TARGET_TYPE (inner_array) =
16849 copy_type (TYPE_TARGET_TYPE (inner_array));
16850 inner_array = TYPE_TARGET_TYPE (inner_array);
16851 }
16852
16853 el_type = TYPE_TARGET_TYPE (inner_array);
16854 cnst |= TYPE_CONST (el_type);
16855 voltl |= TYPE_VOLATILE (el_type);
16856 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16857
16858 return set_die_type (die, base_type, cu);
16859}
16860
f792889a 16861static struct type *
e7c27a73 16862read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16863{
f792889a 16864 struct type *base_type, *cv_type;
c906108c 16865
e7c27a73 16866 base_type = die_type (die, cu);
7e314c57
JK
16867
16868 /* The die_type call above may have already set the type for this DIE. */
16869 cv_type = get_die_type (die, cu);
16870 if (cv_type)
16871 return cv_type;
16872
2f608a3a
KW
16873 /* In case the const qualifier is applied to an array type, the element type
16874 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16875 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16876 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16877
f792889a
DJ
16878 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16879 return set_die_type (die, cv_type, cu);
c906108c
SS
16880}
16881
f792889a 16882static struct type *
e7c27a73 16883read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16884{
f792889a 16885 struct type *base_type, *cv_type;
c906108c 16886
e7c27a73 16887 base_type = die_type (die, cu);
7e314c57
JK
16888
16889 /* The die_type call above may have already set the type for this DIE. */
16890 cv_type = get_die_type (die, cu);
16891 if (cv_type)
16892 return cv_type;
16893
cf363f18
MW
16894 /* In case the volatile qualifier is applied to an array type, the
16895 element type is so qualified, not the array type (section 6.7.3
16896 of C99). */
78134374 16897 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16898 return add_array_cv_type (die, cu, base_type, 0, 1);
16899
f792889a
DJ
16900 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16901 return set_die_type (die, cv_type, cu);
c906108c
SS
16902}
16903
06d66ee9
TT
16904/* Handle DW_TAG_restrict_type. */
16905
16906static struct type *
16907read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16908{
16909 struct type *base_type, *cv_type;
16910
16911 base_type = die_type (die, cu);
16912
16913 /* The die_type call above may have already set the type for this DIE. */
16914 cv_type = get_die_type (die, cu);
16915 if (cv_type)
16916 return cv_type;
16917
16918 cv_type = make_restrict_type (base_type);
16919 return set_die_type (die, cv_type, cu);
16920}
16921
a2c2acaf
MW
16922/* Handle DW_TAG_atomic_type. */
16923
16924static struct type *
16925read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16926{
16927 struct type *base_type, *cv_type;
16928
16929 base_type = die_type (die, cu);
16930
16931 /* The die_type call above may have already set the type for this DIE. */
16932 cv_type = get_die_type (die, cu);
16933 if (cv_type)
16934 return cv_type;
16935
16936 cv_type = make_atomic_type (base_type);
16937 return set_die_type (die, cv_type, cu);
16938}
16939
c906108c
SS
16940/* Extract all information from a DW_TAG_string_type DIE and add to
16941 the user defined type vector. It isn't really a user defined type,
16942 but it behaves like one, with other DIE's using an AT_user_def_type
16943 attribute to reference it. */
16944
f792889a 16945static struct type *
e7c27a73 16946read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16947{
5e22e966 16948 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16949 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16950 struct type *type, *range_type, *index_type, *char_type;
16951 struct attribute *attr;
216a7e6b
AB
16952 struct dynamic_prop prop;
16953 bool length_is_constant = true;
16954 LONGEST length;
16955
16956 /* There are a couple of places where bit sizes might be made use of
16957 when parsing a DW_TAG_string_type, however, no producer that we know
16958 of make use of these. Handling bit sizes that are a multiple of the
16959 byte size is easy enough, but what about other bit sizes? Lets deal
16960 with that problem when we have to. Warn about these attributes being
16961 unsupported, then parse the type and ignore them like we always
16962 have. */
16963 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16964 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16965 {
16966 static bool warning_printed = false;
16967 if (!warning_printed)
16968 {
16969 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16970 "currently supported on DW_TAG_string_type."));
16971 warning_printed = true;
16972 }
16973 }
c906108c 16974
e142c38c 16975 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16976 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16977 {
16978 /* The string length describes the location at which the length of
16979 the string can be found. The size of the length field can be
16980 specified with one of the attributes below. */
16981 struct type *prop_type;
16982 struct attribute *len
16983 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16984 if (len == nullptr)
16985 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16986 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16987 {
16988 /* Pass 0 as the default as we know this attribute is constant
16989 and the default value will not be returned. */
0826b30a 16990 LONGEST sz = len->constant_value (0);
09ba997f 16991 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16992 }
16993 else
16994 {
16995 /* If the size is not specified then we assume it is the size of
16996 an address on this target. */
09ba997f 16997 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16998 }
16999
17000 /* Convert the attribute into a dynamic property. */
17001 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17002 length = 1;
17003 else
17004 length_is_constant = false;
17005 }
17006 else if (attr != nullptr)
17007 {
17008 /* This DW_AT_string_length just contains the length with no
17009 indirection. There's no need to create a dynamic property in this
17010 case. Pass 0 for the default value as we know it will not be
17011 returned in this case. */
0826b30a 17012 length = attr->constant_value (0);
216a7e6b
AB
17013 }
17014 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17015 {
216a7e6b 17016 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17017 length = attr->constant_value (1);
c906108c
SS
17018 }
17019 else
17020 {
216a7e6b
AB
17021 /* Use 1 as a fallback length if we have nothing else. */
17022 length = 1;
c906108c 17023 }
6ccb9162 17024
46bf5051 17025 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17026 if (length_is_constant)
17027 range_type = create_static_range_type (NULL, index_type, 1, length);
17028 else
17029 {
17030 struct dynamic_prop low_bound;
17031
17032 low_bound.kind = PROP_CONST;
17033 low_bound.data.const_val = 1;
17034 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17035 }
3b7538c0
UW
17036 char_type = language_string_char_type (cu->language_defn, gdbarch);
17037 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17038
f792889a 17039 return set_die_type (die, type, cu);
c906108c
SS
17040}
17041
4d804846
JB
17042/* Assuming that DIE corresponds to a function, returns nonzero
17043 if the function is prototyped. */
17044
17045static int
17046prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17047{
17048 struct attribute *attr;
17049
17050 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17051 if (attr && (DW_UNSND (attr) != 0))
17052 return 1;
17053
17054 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17055 is only meaningful for C, but the concept also extends to other
4d804846
JB
17056 languages that allow unprototyped functions (Eg: Objective C).
17057 For all other languages, assume that functions are always
17058 prototyped. */
17059 if (cu->language != language_c
17060 && cu->language != language_objc
17061 && cu->language != language_opencl)
17062 return 1;
17063
17064 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17065 prototyped and unprototyped functions; default to prototyped,
17066 since that is more common in modern code (and RealView warns
17067 about unprototyped functions). */
17068 if (producer_is_realview (cu->producer))
17069 return 1;
17070
17071 return 0;
17072}
17073
c906108c
SS
17074/* Handle DIES due to C code like:
17075
17076 struct foo
c5aa993b
JM
17077 {
17078 int (*funcp)(int a, long l);
17079 int b;
17080 };
c906108c 17081
0963b4bd 17082 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17083
f792889a 17084static struct type *
e7c27a73 17085read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17086{
5e22e966 17087 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17088 struct type *type; /* Type that this function returns. */
17089 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17090 struct attribute *attr;
17091
e7c27a73 17092 type = die_type (die, cu);
7e314c57
JK
17093
17094 /* The die_type call above may have already set the type for this DIE. */
17095 ftype = get_die_type (die, cu);
17096 if (ftype)
17097 return ftype;
17098
0c8b41f1 17099 ftype = lookup_function_type (type);
c906108c 17100
4d804846 17101 if (prototyped_function_p (die, cu))
a6c727b2 17102 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17103
c055b101
CV
17104 /* Store the calling convention in the type if it's available in
17105 the subroutine die. Otherwise set the calling convention to
17106 the default value DW_CC_normal. */
17107 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17108 if (attr != nullptr
17109 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17110 TYPE_CALLING_CONVENTION (ftype)
17111 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17112 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17113 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17114 else
17115 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17116
743649fd
MW
17117 /* Record whether the function returns normally to its caller or not
17118 if the DWARF producer set that information. */
17119 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17120 if (attr && (DW_UNSND (attr) != 0))
17121 TYPE_NO_RETURN (ftype) = 1;
17122
76c10ea2
GM
17123 /* We need to add the subroutine type to the die immediately so
17124 we don't infinitely recurse when dealing with parameters
0963b4bd 17125 declared as the same subroutine type. */
76c10ea2 17126 set_die_type (die, ftype, cu);
6e70227d 17127
639d11d3 17128 if (die->child != NULL)
c906108c 17129 {
bb5ed363 17130 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17131 struct die_info *child_die;
8072405b 17132 int nparams, iparams;
c906108c
SS
17133
17134 /* Count the number of parameters.
17135 FIXME: GDB currently ignores vararg functions, but knows about
17136 vararg member functions. */
8072405b 17137 nparams = 0;
639d11d3 17138 child_die = die->child;
c906108c
SS
17139 while (child_die && child_die->tag)
17140 {
17141 if (child_die->tag == DW_TAG_formal_parameter)
17142 nparams++;
17143 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17144 TYPE_VARARGS (ftype) = 1;
436c571c 17145 child_die = child_die->sibling;
c906108c
SS
17146 }
17147
17148 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17149 ftype->set_num_fields (nparams);
3cabb6b0
SM
17150 ftype->set_fields
17151 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17152
8072405b
JK
17153 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17154 even if we error out during the parameters reading below. */
17155 for (iparams = 0; iparams < nparams; iparams++)
17156 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17157
17158 iparams = 0;
639d11d3 17159 child_die = die->child;
c906108c
SS
17160 while (child_die && child_die->tag)
17161 {
17162 if (child_die->tag == DW_TAG_formal_parameter)
17163 {
3ce3b1ba
PA
17164 struct type *arg_type;
17165
17166 /* DWARF version 2 has no clean way to discern C++
17167 static and non-static member functions. G++ helps
17168 GDB by marking the first parameter for non-static
17169 member functions (which is the this pointer) as
17170 artificial. We pass this information to
17171 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17172
17173 DWARF version 3 added DW_AT_object_pointer, which GCC
17174 4.5 does not yet generate. */
e142c38c 17175 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17176 if (attr != nullptr)
c906108c
SS
17177 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17178 else
9c37b5ae 17179 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17180 arg_type = die_type (child_die, cu);
17181
17182 /* RealView does not mark THIS as const, which the testsuite
17183 expects. GCC marks THIS as const in method definitions,
17184 but not in the class specifications (GCC PR 43053). */
17185 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17186 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17187 {
17188 int is_this = 0;
17189 struct dwarf2_cu *arg_cu = cu;
17190 const char *name = dwarf2_name (child_die, cu);
17191
17192 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17193 if (attr != nullptr)
3ce3b1ba
PA
17194 {
17195 /* If the compiler emits this, use it. */
17196 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17197 is_this = 1;
17198 }
17199 else if (name && strcmp (name, "this") == 0)
17200 /* Function definitions will have the argument names. */
17201 is_this = 1;
17202 else if (name == NULL && iparams == 0)
17203 /* Declarations may not have the names, so like
17204 elsewhere in GDB, assume an artificial first
17205 argument is "this". */
17206 is_this = 1;
17207
17208 if (is_this)
17209 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17210 arg_type, 0);
17211 }
17212
17213 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17214 iparams++;
17215 }
436c571c 17216 child_die = child_die->sibling;
c906108c
SS
17217 }
17218 }
17219
76c10ea2 17220 return ftype;
c906108c
SS
17221}
17222
f792889a 17223static struct type *
e7c27a73 17224read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17225{
5e22e966 17226 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17227 const char *name = NULL;
3c8e0968 17228 struct type *this_type, *target_type;
c906108c 17229
94af9270 17230 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17231 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17232 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17233 set_die_type (die, this_type, cu);
3c8e0968
DE
17234 target_type = die_type (die, cu);
17235 if (target_type != this_type)
17236 TYPE_TARGET_TYPE (this_type) = target_type;
17237 else
17238 {
17239 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17240 spec and cause infinite loops in GDB. */
b98664d3 17241 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17242 "- DIE at %s [in module %s]"),
17243 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17244 TYPE_TARGET_TYPE (this_type) = NULL;
17245 }
e4003a34
TV
17246 if (name == NULL)
17247 {
17248 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17249 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17250 Handle these by just returning the target type, rather than
17251 constructing an anonymous typedef type and trying to handle this
17252 elsewhere. */
17253 set_die_type (die, target_type, cu);
17254 return target_type;
17255 }
f792889a 17256 return this_type;
c906108c
SS
17257}
17258
9b790ce7
UW
17259/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17260 (which may be different from NAME) to the architecture back-end to allow
17261 it to guess the correct format if necessary. */
17262
17263static struct type *
17264dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17265 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17266{
08feed99 17267 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17268 const struct floatformat **format;
17269 struct type *type;
17270
17271 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17272 if (format)
103a685e 17273 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17274 else
77b7c781 17275 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17276
17277 return type;
17278}
17279
eb77c9df
AB
17280/* Allocate an integer type of size BITS and name NAME. */
17281
17282static struct type *
17283dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17284 int bits, int unsigned_p, const char *name)
17285{
17286 struct type *type;
17287
17288 /* Versions of Intel's C Compiler generate an integer type called "void"
17289 instead of using DW_TAG_unspecified_type. This has been seen on
17290 at least versions 14, 17, and 18. */
35ee2dc2
AB
17291 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17292 && strcmp (name, "void") == 0)
eb77c9df
AB
17293 type = objfile_type (objfile)->builtin_void;
17294 else
17295 type = init_integer_type (objfile, bits, unsigned_p, name);
17296
17297 return type;
17298}
17299
8bdc1658
AB
17300/* Initialise and return a floating point type of size BITS suitable for
17301 use as a component of a complex number. The NAME_HINT is passed through
17302 when initialising the floating point type and is the name of the complex
17303 type.
17304
17305 As DWARF doesn't currently provide an explicit name for the components
17306 of a complex number, but it can be helpful to have these components
17307 named, we try to select a suitable name based on the size of the
17308 component. */
17309static struct type *
17310dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17311 struct objfile *objfile,
103a685e
TT
17312 int bits, const char *name_hint,
17313 enum bfd_endian byte_order)
8bdc1658 17314{
08feed99 17315 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17316 struct type *tt = nullptr;
17317
35add35e
AB
17318 /* Try to find a suitable floating point builtin type of size BITS.
17319 We're going to use the name of this type as the name for the complex
17320 target type that we are about to create. */
1db455a7 17321 switch (cu->language)
8bdc1658 17322 {
1db455a7
AB
17323 case language_fortran:
17324 switch (bits)
17325 {
17326 case 32:
17327 tt = builtin_f_type (gdbarch)->builtin_real;
17328 break;
17329 case 64:
17330 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17331 break;
17332 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17333 case 128:
17334 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17335 break;
17336 }
8bdc1658 17337 break;
1db455a7
AB
17338 default:
17339 switch (bits)
17340 {
17341 case 32:
17342 tt = builtin_type (gdbarch)->builtin_float;
17343 break;
17344 case 64:
17345 tt = builtin_type (gdbarch)->builtin_double;
17346 break;
17347 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17348 case 128:
17349 tt = builtin_type (gdbarch)->builtin_long_double;
17350 break;
17351 }
8bdc1658
AB
17352 break;
17353 }
17354
35add35e
AB
17355 /* If the type we found doesn't match the size we were looking for, then
17356 pretend we didn't find a type at all, the complex target type we
17357 create will then be nameless. */
a12e5744 17358 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17359 tt = nullptr;
17360
7d93a1e0 17361 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17362 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17363}
17364
c906108c
SS
17365/* Find a representation of a given base type and install
17366 it in the TYPE field of the die. */
17367
f792889a 17368static struct type *
e7c27a73 17369read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17370{
5e22e966 17371 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17372 struct type *type;
17373 struct attribute *attr;
19f392bc 17374 int encoding = 0, bits = 0;
15d034d0 17375 const char *name;
34877895 17376 gdbarch *arch;
c906108c 17377
e142c38c 17378 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17379 if (attr != nullptr)
34877895 17380 encoding = DW_UNSND (attr);
e142c38c 17381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17382 if (attr != nullptr)
34877895 17383 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17384 name = dwarf2_name (die, cu);
6ccb9162 17385 if (!name)
34877895 17386 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17387
08feed99 17388 arch = objfile->arch ();
103a685e
TT
17389 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17390
34877895
PJ
17391 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17392 if (attr)
103a685e
TT
17393 {
17394 int endianity = DW_UNSND (attr);
17395
17396 switch (endianity)
17397 {
17398 case DW_END_big:
17399 byte_order = BFD_ENDIAN_BIG;
17400 break;
17401 case DW_END_little:
17402 byte_order = BFD_ENDIAN_LITTLE;
17403 break;
17404 default:
17405 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17406 break;
17407 }
17408 }
6ccb9162
UW
17409
17410 switch (encoding)
c906108c 17411 {
6ccb9162
UW
17412 case DW_ATE_address:
17413 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17414 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17415 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17416 break;
17417 case DW_ATE_boolean:
19f392bc 17418 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17419 break;
17420 case DW_ATE_complex_float:
103a685e
TT
17421 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17422 byte_order);
78134374 17423 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17424 {
17425 if (name == nullptr)
17426 {
17427 struct obstack *obstack
5e22e966 17428 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17429 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17430 nullptr);
17431 }
17432 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17433 }
17434 else
17435 type = init_complex_type (name, type);
6ccb9162
UW
17436 break;
17437 case DW_ATE_decimal_float:
19f392bc 17438 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17439 break;
17440 case DW_ATE_float:
103a685e 17441 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17442 break;
17443 case DW_ATE_signed:
eb77c9df 17444 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17445 break;
17446 case DW_ATE_unsigned:
3b2b8fea
TT
17447 if (cu->language == language_fortran
17448 && name
61012eef 17449 && startswith (name, "character("))
19f392bc
UW
17450 type = init_character_type (objfile, bits, 1, name);
17451 else
eb77c9df 17452 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17453 break;
17454 case DW_ATE_signed_char:
6e70227d 17455 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17456 || cu->language == language_pascal
17457 || cu->language == language_fortran)
19f392bc
UW
17458 type = init_character_type (objfile, bits, 0, name);
17459 else
eb77c9df 17460 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17461 break;
17462 case DW_ATE_unsigned_char:
868a0084 17463 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17464 || cu->language == language_pascal
c44af4eb
TT
17465 || cu->language == language_fortran
17466 || cu->language == language_rust)
19f392bc
UW
17467 type = init_character_type (objfile, bits, 1, name);
17468 else
eb77c9df 17469 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17470 break;
75079b2b 17471 case DW_ATE_UTF:
53e710ac 17472 {
53e710ac
PA
17473 if (bits == 16)
17474 type = builtin_type (arch)->builtin_char16;
17475 else if (bits == 32)
17476 type = builtin_type (arch)->builtin_char32;
17477 else
17478 {
b98664d3 17479 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17480 bits);
eb77c9df 17481 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17482 }
17483 return set_die_type (die, type, cu);
17484 }
75079b2b
TT
17485 break;
17486
6ccb9162 17487 default:
b98664d3 17488 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17489 dwarf_type_encoding_name (encoding));
77b7c781 17490 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17491 break;
c906108c 17492 }
6ccb9162 17493
0114d602 17494 if (name && strcmp (name, "char") == 0)
876cecd0 17495 TYPE_NOSIGN (type) = 1;
0114d602 17496
2b4424c3
TT
17497 maybe_set_alignment (cu, die, type);
17498
103a685e 17499 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17500
f792889a 17501 return set_die_type (die, type, cu);
c906108c
SS
17502}
17503
80180f79
SA
17504/* Parse dwarf attribute if it's a block, reference or constant and put the
17505 resulting value of the attribute into struct bound_prop.
17506 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17507
17508static int
17509attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17510 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17511 struct type *default_type)
80180f79
SA
17512{
17513 struct dwarf2_property_baton *baton;
5e22e966 17514 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17515 struct objfile *objfile = per_objfile->objfile;
17516 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17517
9a49df9d
AB
17518 gdb_assert (default_type != NULL);
17519
80180f79
SA
17520 if (attr == NULL || prop == NULL)
17521 return 0;
17522
4fc6c0d5 17523 if (attr->form_is_block ())
80180f79 17524 {
8d749320 17525 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17526 baton->property_type = default_type;
80180f79 17527 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17528 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17529 baton->locexpr.size = DW_BLOCK (attr)->size;
17530 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17531 switch (attr->name)
17532 {
17533 case DW_AT_string_length:
17534 baton->locexpr.is_reference = true;
17535 break;
17536 default:
17537 baton->locexpr.is_reference = false;
17538 break;
17539 }
80180f79
SA
17540 prop->data.baton = baton;
17541 prop->kind = PROP_LOCEXPR;
17542 gdb_assert (prop->data.baton != NULL);
17543 }
cd6c91b4 17544 else if (attr->form_is_ref ())
80180f79
SA
17545 {
17546 struct dwarf2_cu *target_cu = cu;
17547 struct die_info *target_die;
17548 struct attribute *target_attr;
17549
17550 target_die = follow_die_ref (die, attr, &target_cu);
17551 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17552 if (target_attr == NULL)
17553 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17554 target_cu);
80180f79
SA
17555 if (target_attr == NULL)
17556 return 0;
17557
df25ebbd 17558 switch (target_attr->name)
80180f79 17559 {
df25ebbd 17560 case DW_AT_location:
cd6c91b4 17561 if (target_attr->form_is_section_offset ())
df25ebbd 17562 {
8d749320 17563 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17564 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17565 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17566 prop->data.baton = baton;
17567 prop->kind = PROP_LOCLIST;
17568 gdb_assert (prop->data.baton != NULL);
17569 }
4fc6c0d5 17570 else if (target_attr->form_is_block ())
df25ebbd 17571 {
8d749320 17572 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17573 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17574 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17575 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17576 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17577 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17578 baton->locexpr.is_reference = true;
df25ebbd
JB
17579 prop->data.baton = baton;
17580 prop->kind = PROP_LOCEXPR;
17581 gdb_assert (prop->data.baton != NULL);
17582 }
17583 else
17584 {
17585 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17586 "dynamic property");
17587 return 0;
17588 }
17589 break;
17590 case DW_AT_data_member_location:
17591 {
17592 LONGEST offset;
17593
17594 if (!handle_data_member_location (target_die, target_cu,
17595 &offset))
17596 return 0;
17597
8d749320 17598 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17599 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17600 target_cu);
df25ebbd
JB
17601 baton->offset_info.offset = offset;
17602 baton->offset_info.type = die_type (target_die, target_cu);
17603 prop->data.baton = baton;
17604 prop->kind = PROP_ADDR_OFFSET;
17605 break;
17606 }
80180f79
SA
17607 }
17608 }
cd6c91b4 17609 else if (attr->form_is_constant ())
80180f79 17610 {
0826b30a 17611 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17612 prop->kind = PROP_CONST;
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17617 dwarf2_name (die, cu));
17618 return 0;
17619 }
17620
17621 return 1;
17622}
17623
09ba997f 17624/* See read.h. */
9a49df9d 17625
09ba997f
TT
17626struct type *
17627dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17628{
09ba997f 17629 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17630 struct type *int_type;
17631
17632 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17633#define TRY_TYPE(F) \
17634 int_type = (unsigned_p \
17635 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17636 : objfile_type (objfile)->builtin_ ## F); \
17637 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17638 return int_type
17639
17640 TRY_TYPE (char);
17641 TRY_TYPE (short);
17642 TRY_TYPE (int);
17643 TRY_TYPE (long);
17644 TRY_TYPE (long_long);
17645
17646#undef TRY_TYPE
17647
17648 gdb_assert_not_reached ("unable to find suitable integer type");
17649}
17650
09ba997f 17651/* See read.h. */
11a8b164 17652
09ba997f
TT
17653struct type *
17654dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17655{
09ba997f
TT
17656 int addr_size = this->addr_size ();
17657 return int_type (addr_size, unsigned_p);
11a8b164
AB
17658}
17659
b86352cf
AB
17660/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17661 present (which is valid) then compute the default type based on the
17662 compilation units address size. */
17663
17664static struct type *
17665read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17666{
17667 struct type *index_type = die_type (die, cu);
17668
17669 /* Dwarf-2 specifications explicitly allows to create subrange types
17670 without specifying a base type.
17671 In that case, the base type must be set to the type of
17672 the lower bound, upper bound or count, in that order, if any of these
17673 three attributes references an object that has a type.
17674 If no base type is found, the Dwarf-2 specifications say that
17675 a signed integer type of size equal to the size of an address should
17676 be used.
17677 For the following C code: `extern char gdb_int [];'
17678 GCC produces an empty range DIE.
17679 FIXME: muller/2010-05-28: Possible references to object for low bound,
17680 high bound or count are not yet handled by this code. */
78134374 17681 if (index_type->code () == TYPE_CODE_VOID)
09ba997f 17682 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17683
17684 return index_type;
17685}
17686
a02abb62
JB
17687/* Read the given DW_AT_subrange DIE. */
17688
f792889a 17689static struct type *
a02abb62
JB
17690read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17691{
4c9ad8c2 17692 struct type *base_type, *orig_base_type;
a02abb62
JB
17693 struct type *range_type;
17694 struct attribute *attr;
729efb13 17695 struct dynamic_prop low, high;
4fae6e18 17696 int low_default_is_valid;
c451ebe5 17697 int high_bound_is_count = 0;
15d034d0 17698 const char *name;
d359392f 17699 ULONGEST negative_mask;
e77813c8 17700
b86352cf
AB
17701 orig_base_type = read_subrange_index_type (die, cu);
17702
4c9ad8c2
TT
17703 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17704 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17705 creating the range type, but we use the result of check_typedef
17706 when examining properties of the type. */
17707 base_type = check_typedef (orig_base_type);
a02abb62 17708
7e314c57
JK
17709 /* The die_type call above may have already set the type for this DIE. */
17710 range_type = get_die_type (die, cu);
17711 if (range_type)
17712 return range_type;
17713
729efb13
SA
17714 low.kind = PROP_CONST;
17715 high.kind = PROP_CONST;
17716 high.data.const_val = 0;
17717
4fae6e18
JK
17718 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17719 omitting DW_AT_lower_bound. */
17720 switch (cu->language)
6e70227d 17721 {
4fae6e18
JK
17722 case language_c:
17723 case language_cplus:
729efb13 17724 low.data.const_val = 0;
4fae6e18
JK
17725 low_default_is_valid = 1;
17726 break;
17727 case language_fortran:
729efb13 17728 low.data.const_val = 1;
4fae6e18
JK
17729 low_default_is_valid = 1;
17730 break;
17731 case language_d:
4fae6e18 17732 case language_objc:
c44af4eb 17733 case language_rust:
729efb13 17734 low.data.const_val = 0;
4fae6e18
JK
17735 low_default_is_valid = (cu->header.version >= 4);
17736 break;
17737 case language_ada:
17738 case language_m2:
17739 case language_pascal:
729efb13 17740 low.data.const_val = 1;
4fae6e18
JK
17741 low_default_is_valid = (cu->header.version >= 4);
17742 break;
17743 default:
729efb13 17744 low.data.const_val = 0;
4fae6e18
JK
17745 low_default_is_valid = 0;
17746 break;
a02abb62
JB
17747 }
17748
e142c38c 17749 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17750 if (attr != nullptr)
9a49df9d 17751 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17752 else if (!low_default_is_valid)
b98664d3 17753 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17754 "- DIE at %s [in module %s]"),
17755 sect_offset_str (die->sect_off),
5e22e966 17756 objfile_name (cu->per_objfile->objfile));
a02abb62 17757
506f5c41
TV
17758 struct attribute *attr_ub, *attr_count;
17759 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17760 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17761 {
506f5c41 17762 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17763 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17764 {
c451ebe5
SA
17765 /* If bounds are constant do the final calculation here. */
17766 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17767 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17768 else
17769 high_bound_is_count = 1;
c2ff108b 17770 }
506f5c41
TV
17771 else
17772 {
17773 if (attr_ub != NULL)
17774 complaint (_("Unresolved DW_AT_upper_bound "
17775 "- DIE at %s [in module %s]"),
17776 sect_offset_str (die->sect_off),
5e22e966 17777 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17778 if (attr_count != NULL)
17779 complaint (_("Unresolved DW_AT_count "
17780 "- DIE at %s [in module %s]"),
17781 sect_offset_str (die->sect_off),
5e22e966 17782 objfile_name (cu->per_objfile->objfile));
506f5c41 17783 }
e77813c8 17784 }
a02abb62 17785
4e962e74
TT
17786 LONGEST bias = 0;
17787 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17788 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17789 bias = bias_attr->constant_value (0);
4e962e74 17790
dbb9c2b1
JB
17791 /* Normally, the DWARF producers are expected to use a signed
17792 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17793 But this is unfortunately not always the case, as witnessed
17794 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17795 is used instead. To work around that ambiguity, we treat
17796 the bounds as signed, and thus sign-extend their values, when
17797 the base type is signed. */
6e70227d 17798 negative_mask =
d359392f 17799 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17800 if (low.kind == PROP_CONST
17801 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17802 low.data.const_val |= negative_mask;
17803 if (high.kind == PROP_CONST
17804 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17805 high.data.const_val |= negative_mask;
43bbcdc2 17806
5bbd8269
AB
17807 /* Check for bit and byte strides. */
17808 struct dynamic_prop byte_stride_prop;
17809 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17810 if (attr_byte_stride != nullptr)
17811 {
09ba997f 17812 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17813 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17814 prop_type);
17815 }
17816
17817 struct dynamic_prop bit_stride_prop;
17818 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17819 if (attr_bit_stride != nullptr)
17820 {
17821 /* It only makes sense to have either a bit or byte stride. */
17822 if (attr_byte_stride != nullptr)
17823 {
17824 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17825 "- DIE at %s [in module %s]"),
17826 sect_offset_str (die->sect_off),
5e22e966 17827 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17828 attr_bit_stride = nullptr;
17829 }
17830 else
17831 {
09ba997f 17832 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17833 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17834 prop_type);
17835 }
17836 }
17837
17838 if (attr_byte_stride != nullptr
17839 || attr_bit_stride != nullptr)
17840 {
17841 bool byte_stride_p = (attr_byte_stride != nullptr);
17842 struct dynamic_prop *stride
17843 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17844
17845 range_type
17846 = create_range_type_with_stride (NULL, orig_base_type, &low,
17847 &high, bias, stride, byte_stride_p);
17848 }
17849 else
17850 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17851
c451ebe5
SA
17852 if (high_bound_is_count)
17853 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17854
c2ff108b
JK
17855 /* Ada expects an empty array on no boundary attributes. */
17856 if (attr == NULL && cu->language != language_ada)
729efb13 17857 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17858
39cbfefa
DJ
17859 name = dwarf2_name (die, cu);
17860 if (name)
d0e39ea2 17861 range_type->set_name (name);
6e70227d 17862
e142c38c 17863 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17864 if (attr != nullptr)
a02abb62
JB
17865 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17866
2b4424c3
TT
17867 maybe_set_alignment (cu, die, range_type);
17868
7e314c57
JK
17869 set_die_type (die, range_type, cu);
17870
17871 /* set_die_type should be already done. */
b4ba55a1
JB
17872 set_descriptive_type (range_type, die, cu);
17873
7e314c57 17874 return range_type;
a02abb62 17875}
6e70227d 17876
f792889a 17877static struct type *
81a17f79
JB
17878read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17879{
17880 struct type *type;
81a17f79 17881
5e22e966 17882 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17883 type->set_name (dwarf2_name (die, cu));
81a17f79 17884
74a2f8ff 17885 /* In Ada, an unspecified type is typically used when the description
85102364 17886 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17887 such a type, we treat it as a stub, and try to resolve it later on,
17888 when needed. */
17889 if (cu->language == language_ada)
17890 TYPE_STUB (type) = 1;
17891
f792889a 17892 return set_die_type (die, type, cu);
81a17f79 17893}
a02abb62 17894
639d11d3
DC
17895/* Read a single die and all its descendents. Set the die's sibling
17896 field to NULL; set other fields in the die correctly, and set all
17897 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17898 location of the info_ptr after reading all of those dies. PARENT
17899 is the parent of the die in question. */
17900
17901static struct die_info *
dee91e82 17902read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17903 const gdb_byte *info_ptr,
17904 const gdb_byte **new_info_ptr,
dee91e82 17905 struct die_info *parent)
639d11d3
DC
17906{
17907 struct die_info *die;
d521ce57 17908 const gdb_byte *cur_ptr;
639d11d3 17909
3e225074 17910 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17911 if (die == NULL)
17912 {
17913 *new_info_ptr = cur_ptr;
17914 return NULL;
17915 }
93311388 17916 store_in_ref_table (die, reader->cu);
639d11d3 17917
3e225074 17918 if (die->has_children)
bf6af496 17919 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17920 else
17921 {
17922 die->child = NULL;
17923 *new_info_ptr = cur_ptr;
17924 }
17925
17926 die->sibling = NULL;
17927 die->parent = parent;
17928 return die;
17929}
17930
17931/* Read a die, all of its descendents, and all of its siblings; set
17932 all of the fields of all of the dies correctly. Arguments are as
17933 in read_die_and_children. */
17934
17935static struct die_info *
bf6af496 17936read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17937 const gdb_byte *info_ptr,
17938 const gdb_byte **new_info_ptr,
bf6af496 17939 struct die_info *parent)
639d11d3
DC
17940{
17941 struct die_info *first_die, *last_sibling;
d521ce57 17942 const gdb_byte *cur_ptr;
639d11d3 17943
c906108c 17944 cur_ptr = info_ptr;
639d11d3
DC
17945 first_die = last_sibling = NULL;
17946
17947 while (1)
c906108c 17948 {
639d11d3 17949 struct die_info *die
dee91e82 17950 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17951
1d325ec1 17952 if (die == NULL)
c906108c 17953 {
639d11d3
DC
17954 *new_info_ptr = cur_ptr;
17955 return first_die;
c906108c 17956 }
1d325ec1
DJ
17957
17958 if (!first_die)
17959 first_die = die;
c906108c 17960 else
1d325ec1
DJ
17961 last_sibling->sibling = die;
17962
17963 last_sibling = die;
c906108c 17964 }
c906108c
SS
17965}
17966
bf6af496
DE
17967/* Read a die, all of its descendents, and all of its siblings; set
17968 all of the fields of all of the dies correctly. Arguments are as
17969 in read_die_and_children.
17970 This the main entry point for reading a DIE and all its children. */
17971
17972static struct die_info *
17973read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17974 const gdb_byte *info_ptr,
17975 const gdb_byte **new_info_ptr,
bf6af496
DE
17976 struct die_info *parent)
17977{
17978 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17979 new_info_ptr, parent);
17980
b4f54984 17981 if (dwarf_die_debug)
bf6af496
DE
17982 {
17983 fprintf_unfiltered (gdb_stdlog,
17984 "Read die from %s@0x%x of %s:\n",
96b79293 17985 reader->die_section->get_name (),
bf6af496
DE
17986 (unsigned) (info_ptr - reader->die_section->buffer),
17987 bfd_get_filename (reader->abfd));
b4f54984 17988 dump_die (die, dwarf_die_debug);
bf6af496
DE
17989 }
17990
17991 return die;
17992}
17993
3019eac3
DE
17994/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17995 attributes.
17996 The caller is responsible for filling in the extra attributes
17997 and updating (*DIEP)->num_attrs.
17998 Set DIEP to point to a newly allocated die with its information,
3e225074 17999 except for its child, sibling, and parent fields. */
93311388 18000
d521ce57 18001static const gdb_byte *
3019eac3 18002read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18003 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18004 int num_extra_attrs)
93311388 18005{
b64f50a1 18006 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18007 struct abbrev_info *abbrev;
18008 struct die_info *die;
18009 struct dwarf2_cu *cu = reader->cu;
18010 bfd *abfd = reader->abfd;
18011
9c541725 18012 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18013 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18014 info_ptr += bytes_read;
18015 if (!abbrev_number)
18016 {
18017 *diep = NULL;
93311388
DE
18018 return info_ptr;
18019 }
18020
685af9cd 18021 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18022 if (!abbrev)
348e048f
DE
18023 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18024 abbrev_number,
18025 bfd_get_filename (abfd));
18026
3019eac3 18027 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18028 die->sect_off = sect_off;
93311388
DE
18029 die->tag = abbrev->tag;
18030 die->abbrev = abbrev_number;
3e225074 18031 die->has_children = abbrev->has_children;
93311388 18032
3019eac3
DE
18033 /* Make the result usable.
18034 The caller needs to update num_attrs after adding the extra
18035 attributes. */
93311388
DE
18036 die->num_attrs = abbrev->num_attrs;
18037
18a8505e 18038 std::vector<int> indexes_that_need_reprocess;
93311388 18039 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18040 {
18041 bool need_reprocess;
18042 info_ptr =
18043 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18044 info_ptr, &need_reprocess);
18045 if (need_reprocess)
18046 indexes_that_need_reprocess.push_back (i);
18047 }
18048
052c8bb8 18049 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18050 if (attr != nullptr)
18051 cu->str_offsets_base = DW_UNSND (attr);
93311388 18052
41144253 18053 attr = die->attr (DW_AT_loclists_base);
18054 if (attr != nullptr)
18055 cu->loclist_base = DW_UNSND (attr);
18056
a39fdb41 18057 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18058 if (maybe_addr_base.has_value ())
18059 cu->addr_base = *maybe_addr_base;
18060 for (int index : indexes_that_need_reprocess)
18061 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18062 *diep = die;
93311388
DE
18063 return info_ptr;
18064}
18065
3019eac3
DE
18066/* Read a die and all its attributes.
18067 Set DIEP to point to a newly allocated die with its information,
3e225074 18068 except for its child, sibling, and parent fields. */
3019eac3 18069
d521ce57 18070static const gdb_byte *
3019eac3 18071read_full_die (const struct die_reader_specs *reader,
3e225074 18072 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18073{
d521ce57 18074 const gdb_byte *result;
bf6af496 18075
3e225074 18076 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18077
b4f54984 18078 if (dwarf_die_debug)
bf6af496
DE
18079 {
18080 fprintf_unfiltered (gdb_stdlog,
18081 "Read die from %s@0x%x of %s:\n",
96b79293 18082 reader->die_section->get_name (),
bf6af496
DE
18083 (unsigned) (info_ptr - reader->die_section->buffer),
18084 bfd_get_filename (reader->abfd));
b4f54984 18085 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18086 }
18087
18088 return result;
3019eac3 18089}
433df2d4 18090\f
c906108c 18091
72bf9492
DJ
18092/* Returns nonzero if TAG represents a type that we might generate a partial
18093 symbol for. */
18094
18095static int
18096is_type_tag_for_partial (int tag)
18097{
18098 switch (tag)
18099 {
18100#if 0
18101 /* Some types that would be reasonable to generate partial symbols for,
18102 that we don't at present. */
18103 case DW_TAG_array_type:
18104 case DW_TAG_file_type:
18105 case DW_TAG_ptr_to_member_type:
18106 case DW_TAG_set_type:
18107 case DW_TAG_string_type:
18108 case DW_TAG_subroutine_type:
18109#endif
18110 case DW_TAG_base_type:
18111 case DW_TAG_class_type:
680b30c7 18112 case DW_TAG_interface_type:
72bf9492
DJ
18113 case DW_TAG_enumeration_type:
18114 case DW_TAG_structure_type:
18115 case DW_TAG_subrange_type:
18116 case DW_TAG_typedef:
18117 case DW_TAG_union_type:
18118 return 1;
18119 default:
18120 return 0;
18121 }
18122}
18123
18124/* Load all DIEs that are interesting for partial symbols into memory. */
18125
18126static struct partial_die_info *
dee91e82 18127load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18128 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18129{
dee91e82 18130 struct dwarf2_cu *cu = reader->cu;
5e22e966 18131 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18132 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18133 unsigned int bytes_read;
5afb4e99 18134 unsigned int load_all = 0;
72bf9492
DJ
18135 int nesting_level = 1;
18136
18137 parent_die = NULL;
18138 last_die = NULL;
18139
7adf1e79
DE
18140 gdb_assert (cu->per_cu != NULL);
18141 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18142 load_all = 1;
18143
72bf9492
DJ
18144 cu->partial_dies
18145 = htab_create_alloc_ex (cu->header.length / 12,
18146 partial_die_hash,
18147 partial_die_eq,
18148 NULL,
18149 &cu->comp_unit_obstack,
18150 hashtab_obstack_allocate,
18151 dummy_obstack_deallocate);
18152
72bf9492
DJ
18153 while (1)
18154 {
685af9cd 18155 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18156
18157 /* A NULL abbrev means the end of a series of children. */
18158 if (abbrev == NULL)
18159 {
18160 if (--nesting_level == 0)
cd9983dd
YQ
18161 return first_die;
18162
72bf9492
DJ
18163 info_ptr += bytes_read;
18164 last_die = parent_die;
18165 parent_die = parent_die->die_parent;
18166 continue;
18167 }
18168
98bfdba5
PA
18169 /* Check for template arguments. We never save these; if
18170 they're seen, we just mark the parent, and go on our way. */
18171 if (parent_die != NULL
18172 && cu->language == language_cplus
18173 && (abbrev->tag == DW_TAG_template_type_param
18174 || abbrev->tag == DW_TAG_template_value_param))
18175 {
18176 parent_die->has_template_arguments = 1;
18177
18178 if (!load_all)
18179 {
18180 /* We don't need a partial DIE for the template argument. */
dee91e82 18181 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18182 continue;
18183 }
18184 }
18185
0d99eb77 18186 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18187 Skip their other children. */
18188 if (!load_all
18189 && cu->language == language_cplus
18190 && parent_die != NULL
18191 && parent_die->tag == DW_TAG_subprogram)
18192 {
dee91e82 18193 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18194 continue;
18195 }
18196
5afb4e99
DJ
18197 /* Check whether this DIE is interesting enough to save. Normally
18198 we would not be interested in members here, but there may be
18199 later variables referencing them via DW_AT_specification (for
18200 static members). */
18201 if (!load_all
18202 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18203 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18204 && abbrev->tag != DW_TAG_enumerator
18205 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18206 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18207 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18208 && abbrev->tag != DW_TAG_variable
5afb4e99 18209 && abbrev->tag != DW_TAG_namespace
f55ee35c 18210 && abbrev->tag != DW_TAG_module
95554aad 18211 && abbrev->tag != DW_TAG_member
74921315
KS
18212 && abbrev->tag != DW_TAG_imported_unit
18213 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18214 {
18215 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18216 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18217 continue;
18218 }
18219
6f06d47b
YQ
18220 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18221 abbrev);
cd9983dd 18222
48fbe735 18223 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18224
18225 /* This two-pass algorithm for processing partial symbols has a
18226 high cost in cache pressure. Thus, handle some simple cases
18227 here which cover the majority of C partial symbols. DIEs
18228 which neither have specification tags in them, nor could have
18229 specification tags elsewhere pointing at them, can simply be
18230 processed and discarded.
18231
18232 This segment is also optional; scan_partial_symbols and
18233 add_partial_symbol will handle these DIEs if we chain
18234 them in normally. When compilers which do not emit large
18235 quantities of duplicate debug information are more common,
18236 this code can probably be removed. */
18237
18238 /* Any complete simple types at the top level (pretty much all
18239 of them, for a language without namespaces), can be processed
18240 directly. */
18241 if (parent_die == NULL
cd9983dd
YQ
18242 && pdi.has_specification == 0
18243 && pdi.is_declaration == 0
18244 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18245 || pdi.tag == DW_TAG_base_type
18246 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18247 {
cd9983dd 18248 if (building_psymtab && pdi.name != NULL)
31edb802 18249 add_psymbol_to_list (pdi.name, false,
79748972 18250 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18251 psymbol_placement::STATIC,
1762568f 18252 0, cu->language, objfile);
cd9983dd 18253 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18254 continue;
18255 }
18256
d8228535
JK
18257 /* The exception for DW_TAG_typedef with has_children above is
18258 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18259 type_name_or_error will error on such types later.
d8228535
JK
18260
18261 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18262 it could not find the child DIEs referenced later, this is checked
18263 above. In correct DWARF DW_TAG_typedef should have no children. */
18264
cd9983dd 18265 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18266 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18267 "- DIE at %s [in module %s]"),
cd9983dd 18268 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18269
72bf9492
DJ
18270 /* If we're at the second level, and we're an enumerator, and
18271 our parent has no specification (meaning possibly lives in a
18272 namespace elsewhere), then we can add the partial symbol now
18273 instead of queueing it. */
cd9983dd 18274 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18275 && parent_die != NULL
18276 && parent_die->die_parent == NULL
18277 && parent_die->tag == DW_TAG_enumeration_type
18278 && parent_die->has_specification == 0)
18279 {
cd9983dd 18280 if (pdi.name == NULL)
b98664d3 18281 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18282 else if (building_psymtab)
31edb802 18283 add_psymbol_to_list (pdi.name, false,
79748972 18284 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18285 cu->language == language_cplus
75aedd27
TT
18286 ? psymbol_placement::GLOBAL
18287 : psymbol_placement::STATIC,
1762568f 18288 0, cu->language, objfile);
72bf9492 18289
cd9983dd 18290 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18291 continue;
18292 }
18293
cd9983dd 18294 struct partial_die_info *part_die
6f06d47b 18295 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18296
72bf9492
DJ
18297 /* We'll save this DIE so link it in. */
18298 part_die->die_parent = parent_die;
18299 part_die->die_sibling = NULL;
18300 part_die->die_child = NULL;
18301
18302 if (last_die && last_die == parent_die)
18303 last_die->die_child = part_die;
18304 else if (last_die)
18305 last_die->die_sibling = part_die;
18306
18307 last_die = part_die;
18308
18309 if (first_die == NULL)
18310 first_die = part_die;
18311
18312 /* Maybe add the DIE to the hash table. Not all DIEs that we
18313 find interesting need to be in the hash table, because we
18314 also have the parent/sibling/child chains; only those that we
18315 might refer to by offset later during partial symbol reading.
18316
18317 For now this means things that might have be the target of a
18318 DW_AT_specification, DW_AT_abstract_origin, or
18319 DW_AT_extension. DW_AT_extension will refer only to
18320 namespaces; DW_AT_abstract_origin refers to functions (and
18321 many things under the function DIE, but we do not recurse
18322 into function DIEs during partial symbol reading) and
18323 possibly variables as well; DW_AT_specification refers to
18324 declarations. Declarations ought to have the DW_AT_declaration
18325 flag. It happens that GCC forgets to put it in sometimes, but
18326 only for functions, not for types.
18327
18328 Adding more things than necessary to the hash table is harmless
18329 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18330 wasted time in find_partial_die, when we reread the compilation
18331 unit with load_all_dies set. */
72bf9492 18332
5afb4e99 18333 if (load_all
72929c62 18334 || abbrev->tag == DW_TAG_constant
5afb4e99 18335 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18336 || abbrev->tag == DW_TAG_variable
18337 || abbrev->tag == DW_TAG_namespace
18338 || part_die->is_declaration)
18339 {
18340 void **slot;
18341
18342 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18343 to_underlying (part_die->sect_off),
18344 INSERT);
72bf9492
DJ
18345 *slot = part_die;
18346 }
18347
72bf9492 18348 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18349 we have no reason to follow the children of structures; for other
98bfdba5
PA
18350 languages we have to, so that we can get at method physnames
18351 to infer fully qualified class names, for DW_AT_specification,
18352 and for C++ template arguments. For C++, we also look one level
18353 inside functions to find template arguments (if the name of the
18354 function does not already contain the template arguments).
bc30ff58 18355
0a4b0913
AB
18356 For Ada and Fortran, we need to scan the children of subprograms
18357 and lexical blocks as well because these languages allow the
18358 definition of nested entities that could be interesting for the
18359 debugger, such as nested subprograms for instance. */
72bf9492 18360 if (last_die->has_children
5afb4e99
DJ
18361 && (load_all
18362 || last_die->tag == DW_TAG_namespace
f55ee35c 18363 || last_die->tag == DW_TAG_module
72bf9492 18364 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18365 || (cu->language == language_cplus
18366 && last_die->tag == DW_TAG_subprogram
18367 && (last_die->name == NULL
18368 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18369 || (cu->language != language_c
18370 && (last_die->tag == DW_TAG_class_type
680b30c7 18371 || last_die->tag == DW_TAG_interface_type
72bf9492 18372 || last_die->tag == DW_TAG_structure_type
bc30ff58 18373 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18374 || ((cu->language == language_ada
18375 || cu->language == language_fortran)
bc30ff58
JB
18376 && (last_die->tag == DW_TAG_subprogram
18377 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18378 {
18379 nesting_level++;
18380 parent_die = last_die;
18381 continue;
18382 }
18383
18384 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18385 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18386
18387 /* Back to the top, do it again. */
18388 }
18389}
18390
6f06d47b
YQ
18391partial_die_info::partial_die_info (sect_offset sect_off_,
18392 struct abbrev_info *abbrev)
18393 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18394{
18395}
18396
35cc7ed7
YQ
18397/* Read a minimal amount of information into the minimal die structure.
18398 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18399
48fbe735
YQ
18400const gdb_byte *
18401partial_die_info::read (const struct die_reader_specs *reader,
18402 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18403{
dee91e82 18404 struct dwarf2_cu *cu = reader->cu;
5e22e966 18405 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18406 unsigned int i;
c5aa993b 18407 int has_low_pc_attr = 0;
c906108c 18408 int has_high_pc_attr = 0;
91da1414 18409 int high_pc_relative = 0;
c906108c 18410
fd0a254f 18411 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18412 {
e7da7f8f 18413 attribute attr;
18a8505e 18414 bool need_reprocess;
e7da7f8f 18415 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18416 info_ptr, &need_reprocess);
18417 /* String and address offsets that need to do the reprocessing have
18418 already been read at this point, so there is no need to wait until
18419 the loop terminates to do the reprocessing. */
18420 if (need_reprocess)
e7da7f8f 18421 read_attribute_reprocess (reader, &attr);
c906108c 18422 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18423 partial symbol table. */
c906108c
SS
18424 switch (attr.name)
18425 {
18426 case DW_AT_name:
48fbe735 18427 switch (tag)
71c25dea
TT
18428 {
18429 case DW_TAG_compile_unit:
95554aad 18430 case DW_TAG_partial_unit:
348e048f 18431 case DW_TAG_type_unit:
71c25dea
TT
18432 /* Compilation units have a DW_AT_name that is a filename, not
18433 a source language identifier. */
18434 case DW_TAG_enumeration_type:
18435 case DW_TAG_enumerator:
18436 /* These tags always have simple identifiers already; no need
18437 to canonicalize them. */
48fbe735 18438 name = DW_STRING (&attr);
71c25dea
TT
18439 break;
18440 default:
48fbe735
YQ
18441 {
18442 struct objfile *objfile = dwarf2_per_objfile->objfile;
18443
18444 name
be1e3d3e 18445 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18446 }
71c25dea
TT
18447 break;
18448 }
c906108c 18449 break;
31ef98ae 18450 case DW_AT_linkage_name:
c906108c 18451 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18452 /* Note that both forms of linkage name might appear. We
18453 assume they will be the same, and we only store the last
18454 one we see. */
e61108c9 18455 linkage_name = attr.value_as_string ();
787de330
TT
18456 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18457 See https://github.com/rust-lang/rust/issues/32925. */
18458 if (cu->language == language_rust && linkage_name != NULL
18459 && strchr (linkage_name, '{') != NULL)
18460 linkage_name = NULL;
c906108c
SS
18461 break;
18462 case DW_AT_low_pc:
18463 has_low_pc_attr = 1;
cd6c91b4 18464 lowpc = attr.value_as_address ();
c906108c
SS
18465 break;
18466 case DW_AT_high_pc:
18467 has_high_pc_attr = 1;
cd6c91b4
TT
18468 highpc = attr.value_as_address ();
18469 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18470 high_pc_relative = 1;
c906108c
SS
18471 break;
18472 case DW_AT_location:
0963b4bd 18473 /* Support the .debug_loc offsets. */
4fc6c0d5 18474 if (attr.form_is_block ())
8e19ed76 18475 {
48fbe735 18476 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18477 }
cd6c91b4 18478 else if (attr.form_is_section_offset ())
8e19ed76 18479 {
4d3c2250 18480 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18481 }
18482 else
18483 {
4d3c2250
KB
18484 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18485 "partial symbol information");
8e19ed76 18486 }
c906108c 18487 break;
c906108c 18488 case DW_AT_external:
48fbe735 18489 is_external = DW_UNSND (&attr);
c906108c
SS
18490 break;
18491 case DW_AT_declaration:
48fbe735 18492 is_declaration = DW_UNSND (&attr);
c906108c
SS
18493 break;
18494 case DW_AT_type:
48fbe735 18495 has_type = 1;
c906108c
SS
18496 break;
18497 case DW_AT_abstract_origin:
18498 case DW_AT_specification:
72bf9492 18499 case DW_AT_extension:
48fbe735 18500 has_specification = 1;
0826b30a 18501 spec_offset = attr.get_ref_die_offset ();
48fbe735 18502 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18503 || cu->per_cu->is_dwz);
c906108c
SS
18504 break;
18505 case DW_AT_sibling:
18506 /* Ignore absolute siblings, they might point outside of
18507 the current compile unit. */
18508 if (attr.form == DW_FORM_ref_addr)
b98664d3 18509 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18510 else
b9502d3f 18511 {
48fbe735 18512 const gdb_byte *buffer = reader->buffer;
0826b30a 18513 sect_offset off = attr.get_ref_die_offset ();
9c541725 18514 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18515
18516 if (sibling_ptr < info_ptr)
b98664d3 18517 complaint (_("DW_AT_sibling points backwards"));
22869d73 18518 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18519 reader->die_section->overflow_complaint ();
b9502d3f 18520 else
48fbe735 18521 sibling = sibling_ptr;
b9502d3f 18522 }
c906108c 18523 break;
fa4028e9 18524 case DW_AT_byte_size:
48fbe735 18525 has_byte_size = 1;
fa4028e9 18526 break;
ff908ebf 18527 case DW_AT_const_value:
48fbe735 18528 has_const_value = 1;
ff908ebf 18529 break;
68511cec
CES
18530 case DW_AT_calling_convention:
18531 /* DWARF doesn't provide a way to identify a program's source-level
18532 entry point. DW_AT_calling_convention attributes are only meant
18533 to describe functions' calling conventions.
18534
18535 However, because it's a necessary piece of information in
0c1b455e
TT
18536 Fortran, and before DWARF 4 DW_CC_program was the only
18537 piece of debugging information whose definition refers to
18538 a 'main program' at all, several compilers marked Fortran
18539 main programs with DW_CC_program --- even when those
18540 functions use the standard calling conventions.
18541
18542 Although DWARF now specifies a way to provide this
18543 information, we support this practice for backward
18544 compatibility. */
68511cec 18545 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18546 && cu->language == language_fortran)
48fbe735 18547 main_subprogram = 1;
68511cec 18548 break;
481860b3
GB
18549 case DW_AT_inline:
18550 if (DW_UNSND (&attr) == DW_INL_inlined
18551 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18552 may_be_inlined = 1;
481860b3 18553 break;
95554aad
TT
18554
18555 case DW_AT_import:
48fbe735 18556 if (tag == DW_TAG_imported_unit)
36586728 18557 {
0826b30a 18558 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18559 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18560 || cu->per_cu->is_dwz);
18561 }
95554aad
TT
18562 break;
18563
0c1b455e 18564 case DW_AT_main_subprogram:
48fbe735 18565 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18566 break;
18567
05caa1d2
TT
18568 case DW_AT_ranges:
18569 {
18570 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18571 but that requires a full DIE, so instead we just
18572 reimplement it. */
18573 int need_ranges_base = tag != DW_TAG_compile_unit;
18574 unsigned int ranges_offset = (DW_UNSND (&attr)
18575 + (need_ranges_base
18576 ? cu->ranges_base
18577 : 0));
18578
18579 /* Value of the DW_AT_ranges attribute is the offset in the
18580 .debug_ranges section. */
18581 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18582 nullptr))
18583 has_pc_info = 1;
18584 }
18585 break;
18586
c906108c
SS
18587 default:
18588 break;
18589 }
18590 }
18591
10d06d82
TT
18592 /* For Ada, if both the name and the linkage name appear, we prefer
18593 the latter. This lets "catch exception" work better, regardless
18594 of the order in which the name and linkage name were emitted.
18595 Really, though, this is just a workaround for the fact that gdb
18596 doesn't store both the name and the linkage name. */
18597 if (cu->language == language_ada && linkage_name != nullptr)
18598 name = linkage_name;
18599
91da1414 18600 if (high_pc_relative)
48fbe735 18601 highpc += lowpc;
91da1414 18602
9373cf26
JK
18603 if (has_low_pc_attr && has_high_pc_attr)
18604 {
18605 /* When using the GNU linker, .gnu.linkonce. sections are used to
18606 eliminate duplicate copies of functions and vtables and such.
18607 The linker will arbitrarily choose one and discard the others.
18608 The AT_*_pc values for such functions refer to local labels in
18609 these sections. If the section from that file was discarded, the
18610 labels are not in the output, so the relocs get a value of 0.
18611 If this is a discarded function, mark the pc bounds as invalid,
18612 so that GDB will ignore it. */
5989a64e 18613 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18614 {
48fbe735 18615 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18616 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18617
b98664d3 18618 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18619 "for DIE at %s [in module %s]"),
48fbe735
YQ
18620 paddress (gdbarch, lowpc),
18621 sect_offset_str (sect_off),
9d8780f0 18622 objfile_name (objfile));
9373cf26
JK
18623 }
18624 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18625 else if (lowpc >= highpc)
9373cf26 18626 {
48fbe735 18627 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18628 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18629
b98664d3 18630 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18631 "for DIE at %s [in module %s]"),
48fbe735
YQ
18632 paddress (gdbarch, lowpc),
18633 paddress (gdbarch, highpc),
18634 sect_offset_str (sect_off),
9c541725 18635 objfile_name (objfile));
9373cf26
JK
18636 }
18637 else
48fbe735 18638 has_pc_info = 1;
9373cf26 18639 }
85cbf3d3 18640
c906108c
SS
18641 return info_ptr;
18642}
18643
72bf9492
DJ
18644/* Find a cached partial DIE at OFFSET in CU. */
18645
d590ff25
YQ
18646struct partial_die_info *
18647dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18648{
18649 struct partial_die_info *lookup_die = NULL;
6f06d47b 18650 struct partial_die_info part_die (sect_off);
72bf9492 18651
9a3c8263 18652 lookup_die = ((struct partial_die_info *)
d590ff25 18653 htab_find_with_hash (partial_dies, &part_die,
9c541725 18654 to_underlying (sect_off)));
72bf9492 18655
72bf9492
DJ
18656 return lookup_die;
18657}
18658
348e048f
DE
18659/* Find a partial DIE at OFFSET, which may or may not be in CU,
18660 except in the case of .debug_types DIEs which do not reference
18661 outside their CU (they do however referencing other types via
55f1336d 18662 DW_FORM_ref_sig8). */
72bf9492 18663
122cf0f2 18664static const struct cu_partial_die_info
9c541725 18665find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18666{
5e22e966 18667 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18668 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18669 struct dwarf2_per_cu_data *per_cu = NULL;
18670 struct partial_die_info *pd = NULL;
72bf9492 18671
36586728 18672 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18673 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18674 {
d590ff25 18675 pd = cu->find_partial_die (sect_off);
5afb4e99 18676 if (pd != NULL)
fb816e8b 18677 return { cu, pd };
0d99eb77
DE
18678 /* We missed recording what we needed.
18679 Load all dies and try again. */
18680 per_cu = cu->per_cu;
5afb4e99 18681 }
0d99eb77
DE
18682 else
18683 {
18684 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18685 if (cu->per_cu->is_debug_types)
0d99eb77 18686 {
9d8780f0
SM
18687 error (_("Dwarf Error: Type Unit at offset %s contains"
18688 " external reference to offset %s [in module %s].\n"),
18689 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18690 bfd_get_filename (objfile->obfd));
18691 }
9c541725 18692 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18693 dwarf2_per_objfile);
72bf9492 18694
0d99eb77 18695 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18696 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18697
0d99eb77 18698 per_cu->cu->last_used = 0;
d590ff25 18699 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18700 }
5afb4e99 18701
dee91e82
DE
18702 /* If we didn't find it, and not all dies have been loaded,
18703 load them all and try again. */
18704
5afb4e99
DJ
18705 if (pd == NULL && per_cu->load_all_dies == 0)
18706 {
5afb4e99 18707 per_cu->load_all_dies = 1;
fd820528
DE
18708
18709 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18710 THIS_CU->cu may already be in use. So we can't just free it and
18711 replace its DIEs with the ones we read in. Instead, we leave those
18712 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18713 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18714 set. */
ab432490 18715 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18716
d590ff25 18717 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18718 }
18719
18720 if (pd == NULL)
18721 internal_error (__FILE__, __LINE__,
9d8780f0 18722 _("could not find partial DIE %s "
3e43a32a 18723 "in cache [from module %s]\n"),
9d8780f0 18724 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18725 return { per_cu->cu, pd };
72bf9492
DJ
18726}
18727
abc72ce4
DE
18728/* See if we can figure out if the class lives in a namespace. We do
18729 this by looking for a member function; its demangled name will
18730 contain namespace info, if there is any. */
18731
18732static void
18733guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18734 struct dwarf2_cu *cu)
18735{
18736 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18737 what template types look like, because the demangler
18738 frequently doesn't give the same name as the debug info. We
18739 could fix this by only using the demangled name to get the
18740 prefix (but see comment in read_structure_type). */
18741
18742 struct partial_die_info *real_pdi;
18743 struct partial_die_info *child_pdi;
18744
18745 /* If this DIE (this DIE's specification, if any) has a parent, then
18746 we should not do this. We'll prepend the parent's fully qualified
18747 name when we create the partial symbol. */
18748
18749 real_pdi = struct_pdi;
18750 while (real_pdi->has_specification)
fb816e8b 18751 {
122cf0f2
AB
18752 auto res = find_partial_die (real_pdi->spec_offset,
18753 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18754 real_pdi = res.pdi;
18755 cu = res.cu;
18756 }
abc72ce4
DE
18757
18758 if (real_pdi->die_parent != NULL)
18759 return;
18760
18761 for (child_pdi = struct_pdi->die_child;
18762 child_pdi != NULL;
18763 child_pdi = child_pdi->die_sibling)
18764 {
18765 if (child_pdi->tag == DW_TAG_subprogram
18766 && child_pdi->linkage_name != NULL)
18767 {
43816ebc
TT
18768 gdb::unique_xmalloc_ptr<char> actual_class_name
18769 (language_class_name_from_physname (cu->language_defn,
18770 child_pdi->linkage_name));
abc72ce4
DE
18771 if (actual_class_name != NULL)
18772 {
5e22e966 18773 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18774 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18775 }
18776 break;
18777 }
18778 }
18779}
18780
25c11aca
TV
18781/* Return true if a DIE with TAG may have the DW_AT_const_value
18782 attribute. */
18783
18784static bool
18785can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18786{
18787 switch (tag)
18788 {
18789 case DW_TAG_constant:
18790 case DW_TAG_enumerator:
18791 case DW_TAG_formal_parameter:
18792 case DW_TAG_template_value_param:
18793 case DW_TAG_variable:
18794 return true;
18795 }
18796
18797 return false;
18798}
18799
52356b79
YQ
18800void
18801partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18802{
abc72ce4
DE
18803 /* Once we've fixed up a die, there's no point in doing so again.
18804 This also avoids a memory leak if we were to call
18805 guess_partial_die_structure_name multiple times. */
52356b79 18806 if (fixup_called)
abc72ce4
DE
18807 return;
18808
72bf9492
DJ
18809 /* If we found a reference attribute and the DIE has no name, try
18810 to find a name in the referred to DIE. */
18811
52356b79 18812 if (name == NULL && has_specification)
72bf9492
DJ
18813 {
18814 struct partial_die_info *spec_die;
72bf9492 18815
122cf0f2 18816 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18817 spec_die = res.pdi;
18818 cu = res.cu;
72bf9492 18819
52356b79 18820 spec_die->fixup (cu);
72bf9492
DJ
18821
18822 if (spec_die->name)
18823 {
52356b79 18824 name = spec_die->name;
72bf9492
DJ
18825
18826 /* Copy DW_AT_external attribute if it is set. */
18827 if (spec_die->is_external)
52356b79 18828 is_external = spec_die->is_external;
72bf9492
DJ
18829 }
18830 }
18831
25c11aca
TV
18832 if (!has_const_value && has_specification
18833 && can_have_DW_AT_const_value_p (tag))
18834 {
18835 struct partial_die_info *spec_die;
18836
18837 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18838 spec_die = res.pdi;
18839 cu = res.cu;
18840
18841 spec_die->fixup (cu);
18842
18843 if (spec_die->has_const_value)
18844 {
18845 /* Copy DW_AT_const_value attribute if it is set. */
18846 has_const_value = spec_die->has_const_value;
18847 }
18848 }
18849
72bf9492 18850 /* Set default names for some unnamed DIEs. */
72bf9492 18851
52356b79
YQ
18852 if (name == NULL && tag == DW_TAG_namespace)
18853 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18854
abc72ce4
DE
18855 /* If there is no parent die to provide a namespace, and there are
18856 children, see if we can determine the namespace from their linkage
122d1940 18857 name. */
abc72ce4 18858 if (cu->language == language_cplus
5e22e966 18859 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18860 && die_parent == NULL
18861 && has_children
18862 && (tag == DW_TAG_class_type
18863 || tag == DW_TAG_structure_type
18864 || tag == DW_TAG_union_type))
18865 guess_partial_die_structure_name (this, cu);
abc72ce4 18866
53832f31
TT
18867 /* GCC might emit a nameless struct or union that has a linkage
18868 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18869 if (name == NULL
18870 && (tag == DW_TAG_class_type
18871 || tag == DW_TAG_interface_type
18872 || tag == DW_TAG_structure_type
18873 || tag == DW_TAG_union_type)
18874 && linkage_name != NULL)
53832f31 18875 {
43816ebc
TT
18876 gdb::unique_xmalloc_ptr<char> demangled
18877 (gdb_demangle (linkage_name, DMGL_TYPES));
18878 if (demangled != nullptr)
53832f31 18879 {
96408a79
SA
18880 const char *base;
18881
18882 /* Strip any leading namespaces/classes, keep only the base name.
18883 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18884 base = strrchr (demangled.get (), ':');
18885 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18886 base++;
18887 else
43816ebc 18888 base = demangled.get ();
96408a79 18889
5e22e966 18890 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18891 name = objfile->intern (base);
53832f31
TT
18892 }
18893 }
18894
52356b79 18895 fixup_called = 1;
72bf9492
DJ
18896}
18897
41144253 18898/* Read the .debug_loclists header contents from the given SECTION in the
18899 HEADER. */
18900static void
18901read_loclist_header (struct loclist_header *header,
18902 struct dwarf2_section_info *section)
18903{
18904 unsigned int bytes_read;
18905 bfd *abfd = section->get_bfd_owner ();
18906 const gdb_byte *info_ptr = section->buffer;
18907 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18908 info_ptr += bytes_read;
18909 header->version = read_2_bytes (abfd, info_ptr);
18910 info_ptr += 2;
18911 header->addr_size = read_1_byte (abfd, info_ptr);
18912 info_ptr += 1;
18913 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18914 info_ptr += 1;
18915 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18916}
18917
18918/* Return the DW_AT_loclists_base value for the CU. */
18919static ULONGEST
18920lookup_loclist_base (struct dwarf2_cu *cu)
18921{
18922 /* For the .dwo unit, the loclist_base points to the first offset following
18923 the header. The header consists of the following entities-
18924 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18925 bit format)
18926 2. version (2 bytes)
18927 3. address size (1 byte)
18928 4. segment selector size (1 byte)
18929 5. offset entry count (4 bytes)
18930 These sizes are derived as per the DWARFv5 standard. */
18931 if (cu->dwo_unit != nullptr)
18932 {
18933 if (cu->header.initial_length_size == 4)
18934 return LOCLIST_HEADER_SIZE32;
18935 return LOCLIST_HEADER_SIZE64;
18936 }
18937 return cu->loclist_base;
18938}
18939
18940/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18941 array of offsets in the .debug_loclists section. */
18942static CORE_ADDR
18943read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18944{
5e22e966 18945 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18946 struct objfile *objfile = dwarf2_per_objfile->objfile;
18947 bfd *abfd = objfile->obfd;
18948 ULONGEST loclist_base = lookup_loclist_base (cu);
18949 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18950
18951 section->read (objfile);
18952 if (section->buffer == NULL)
18953 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18954 "section [in module %s]"), objfile_name (objfile));
18955 struct loclist_header header;
18956 read_loclist_header (&header, section);
18957 if (loclist_index >= header.offset_entry_count)
18958 complaint (_("DW_FORM_loclistx pointing outside of "
18959 ".debug_loclists offset array [in module %s]"),
18960 objfile_name (objfile));
18961 if (loclist_base + loclist_index * cu->header.offset_size
18962 >= section->size)
18963 complaint (_("DW_FORM_loclistx pointing outside of "
18964 ".debug_loclists section [in module %s]"),
18965 objfile_name (objfile));
18966 const gdb_byte *info_ptr
18967 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18968
18969 if (cu->header.offset_size == 4)
18970 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18971 else
18972 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18973}
18974
18a8505e
AT
18975/* Process the attributes that had to be skipped in the first round. These
18976 attributes are the ones that need str_offsets_base or addr_base attributes.
18977 They could not have been processed in the first round, because at the time
18978 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18979static void
18980read_attribute_reprocess (const struct die_reader_specs *reader,
18981 struct attribute *attr)
18a8505e
AT
18982{
18983 struct dwarf2_cu *cu = reader->cu;
18984 switch (attr->form)
18985 {
18986 case DW_FORM_addrx:
18987 case DW_FORM_GNU_addr_index:
18988 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18989 break;
41144253 18990 case DW_FORM_loclistx:
18991 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18992 break;
18a8505e
AT
18993 case DW_FORM_strx:
18994 case DW_FORM_strx1:
18995 case DW_FORM_strx2:
18996 case DW_FORM_strx3:
18997 case DW_FORM_strx4:
18998 case DW_FORM_GNU_str_index:
18999 {
19000 unsigned int str_index = DW_UNSND (attr);
19001 if (reader->dwo_file != NULL)
19002 {
19003 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19004 DW_STRING_IS_CANONICAL (attr) = 0;
19005 }
19006 else
19007 {
19008 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19009 DW_STRING_IS_CANONICAL (attr) = 0;
19010 }
19011 break;
19012 }
19013 default:
19014 gdb_assert_not_reached (_("Unexpected DWARF form."));
19015 }
19016}
19017
a8329558 19018/* Read an attribute value described by an attribute form. */
c906108c 19019
d521ce57 19020static const gdb_byte *
dee91e82
DE
19021read_attribute_value (const struct die_reader_specs *reader,
19022 struct attribute *attr, unsigned form,
18a8505e
AT
19023 LONGEST implicit_const, const gdb_byte *info_ptr,
19024 bool *need_reprocess)
c906108c 19025{
dee91e82 19026 struct dwarf2_cu *cu = reader->cu;
5e22e966 19027 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19028 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19029 bfd *abfd = reader->abfd;
e7c27a73 19030 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19031 unsigned int bytes_read;
19032 struct dwarf_block *blk;
18a8505e 19033 *need_reprocess = false;
c906108c 19034
aead7601 19035 attr->form = (enum dwarf_form) form;
a8329558 19036 switch (form)
c906108c 19037 {
c906108c 19038 case DW_FORM_ref_addr:
ae411497 19039 if (cu->header.version == 2)
c8a7a66f
TT
19040 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19041 &bytes_read);
ae411497 19042 else
8266302d
TT
19043 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19044 &bytes_read);
ae411497
TT
19045 info_ptr += bytes_read;
19046 break;
36586728 19047 case DW_FORM_GNU_ref_alt:
8266302d 19048 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19049 info_ptr += bytes_read;
19050 break;
ae411497 19051 case DW_FORM_addr:
08feed99
TT
19052 {
19053 struct gdbarch *gdbarch = objfile->arch ();
19054 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19055 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19056 info_ptr += bytes_read;
19057 }
c906108c
SS
19058 break;
19059 case DW_FORM_block2:
7b5a2f43 19060 blk = dwarf_alloc_block (cu);
c906108c
SS
19061 blk->size = read_2_bytes (abfd, info_ptr);
19062 info_ptr += 2;
19063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19064 info_ptr += blk->size;
19065 DW_BLOCK (attr) = blk;
19066 break;
19067 case DW_FORM_block4:
7b5a2f43 19068 blk = dwarf_alloc_block (cu);
c906108c
SS
19069 blk->size = read_4_bytes (abfd, info_ptr);
19070 info_ptr += 4;
19071 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19072 info_ptr += blk->size;
19073 DW_BLOCK (attr) = blk;
19074 break;
19075 case DW_FORM_data2:
19076 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19077 info_ptr += 2;
19078 break;
19079 case DW_FORM_data4:
19080 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19081 info_ptr += 4;
19082 break;
19083 case DW_FORM_data8:
19084 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19085 info_ptr += 8;
19086 break;
0224619f
JK
19087 case DW_FORM_data16:
19088 blk = dwarf_alloc_block (cu);
19089 blk->size = 16;
19090 blk->data = read_n_bytes (abfd, info_ptr, 16);
19091 info_ptr += 16;
19092 DW_BLOCK (attr) = blk;
19093 break;
2dc7f7b3 19094 case DW_FORM_sec_offset:
8266302d 19095 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19096 info_ptr += bytes_read;
19097 break;
41144253 19098 case DW_FORM_loclistx:
19099 {
19100 *need_reprocess = true;
19101 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19102 info_ptr += bytes_read;
19103 }
19104 break;
c906108c 19105 case DW_FORM_string:
9b1c24c8 19106 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19107 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19108 info_ptr += bytes_read;
19109 break;
4bdf3d34 19110 case DW_FORM_strp:
36586728
TT
19111 if (!cu->per_cu->is_dwz)
19112 {
ed2dc618
SM
19113 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19114 abfd, info_ptr, cu_header,
36586728
TT
19115 &bytes_read);
19116 DW_STRING_IS_CANONICAL (attr) = 0;
19117 info_ptr += bytes_read;
19118 break;
19119 }
19120 /* FALLTHROUGH */
43988095
JK
19121 case DW_FORM_line_strp:
19122 if (!cu->per_cu->is_dwz)
19123 {
86c0bb4c
TT
19124 DW_STRING (attr)
19125 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19126 &bytes_read);
43988095
JK
19127 DW_STRING_IS_CANONICAL (attr) = 0;
19128 info_ptr += bytes_read;
19129 break;
19130 }
19131 /* FALLTHROUGH */
36586728
TT
19132 case DW_FORM_GNU_strp_alt:
19133 {
c3699833 19134 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19135 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19136 &bytes_read);
36586728 19137
0314b390 19138 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19139 DW_STRING_IS_CANONICAL (attr) = 0;
19140 info_ptr += bytes_read;
19141 }
4bdf3d34 19142 break;
2dc7f7b3 19143 case DW_FORM_exprloc:
c906108c 19144 case DW_FORM_block:
7b5a2f43 19145 blk = dwarf_alloc_block (cu);
c906108c
SS
19146 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19147 info_ptr += bytes_read;
19148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19149 info_ptr += blk->size;
19150 DW_BLOCK (attr) = blk;
19151 break;
19152 case DW_FORM_block1:
7b5a2f43 19153 blk = dwarf_alloc_block (cu);
c906108c
SS
19154 blk->size = read_1_byte (abfd, info_ptr);
19155 info_ptr += 1;
19156 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19157 info_ptr += blk->size;
19158 DW_BLOCK (attr) = blk;
19159 break;
19160 case DW_FORM_data1:
19161 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19162 info_ptr += 1;
19163 break;
19164 case DW_FORM_flag:
19165 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19166 info_ptr += 1;
19167 break;
2dc7f7b3
TT
19168 case DW_FORM_flag_present:
19169 DW_UNSND (attr) = 1;
19170 break;
c906108c
SS
19171 case DW_FORM_sdata:
19172 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 break;
19175 case DW_FORM_udata:
18a8505e 19176 case DW_FORM_rnglistx:
c906108c
SS
19177 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19178 info_ptr += bytes_read;
19179 break;
19180 case DW_FORM_ref1:
9c541725 19181 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19182 + read_1_byte (abfd, info_ptr));
c906108c
SS
19183 info_ptr += 1;
19184 break;
19185 case DW_FORM_ref2:
9c541725 19186 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19187 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19188 info_ptr += 2;
19189 break;
19190 case DW_FORM_ref4:
9c541725 19191 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19192 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19193 info_ptr += 4;
19194 break;
613e1657 19195 case DW_FORM_ref8:
9c541725 19196 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19197 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19198 info_ptr += 8;
19199 break;
55f1336d 19200 case DW_FORM_ref_sig8:
ac9ec31b 19201 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19202 info_ptr += 8;
19203 break;
c906108c 19204 case DW_FORM_ref_udata:
9c541725 19205 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19206 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19207 info_ptr += bytes_read;
19208 break;
c906108c 19209 case DW_FORM_indirect:
a8329558
KW
19210 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19211 info_ptr += bytes_read;
43988095
JK
19212 if (form == DW_FORM_implicit_const)
19213 {
19214 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19215 info_ptr += bytes_read;
19216 }
19217 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19218 info_ptr, need_reprocess);
43988095
JK
19219 break;
19220 case DW_FORM_implicit_const:
19221 DW_SND (attr) = implicit_const;
a8329558 19222 break;
336d760d 19223 case DW_FORM_addrx:
3019eac3 19224 case DW_FORM_GNU_addr_index:
18a8505e
AT
19225 *need_reprocess = true;
19226 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19227 info_ptr += bytes_read;
19228 break;
cf532bd1 19229 case DW_FORM_strx:
15f18d14
AT
19230 case DW_FORM_strx1:
19231 case DW_FORM_strx2:
19232 case DW_FORM_strx3:
19233 case DW_FORM_strx4:
3019eac3 19234 case DW_FORM_GNU_str_index:
3019eac3 19235 {
15f18d14
AT
19236 ULONGEST str_index;
19237 if (form == DW_FORM_strx1)
19238 {
19239 str_index = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 }
19242 else if (form == DW_FORM_strx2)
19243 {
19244 str_index = read_2_bytes (abfd, info_ptr);
19245 info_ptr += 2;
19246 }
19247 else if (form == DW_FORM_strx3)
19248 {
19249 str_index = read_3_bytes (abfd, info_ptr);
19250 info_ptr += 3;
19251 }
19252 else if (form == DW_FORM_strx4)
19253 {
19254 str_index = read_4_bytes (abfd, info_ptr);
19255 info_ptr += 4;
19256 }
19257 else
19258 {
19259 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19260 info_ptr += bytes_read;
19261 }
18a8505e
AT
19262 *need_reprocess = true;
19263 DW_UNSND (attr) = str_index;
19264 }
3019eac3 19265 break;
c906108c 19266 default:
8a3fe4f8 19267 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19268 dwarf_form_name (form),
19269 bfd_get_filename (abfd));
c906108c 19270 }
28e94949 19271
36586728 19272 /* Super hack. */
cd6c91b4 19273 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19274 attr->form = DW_FORM_GNU_ref_alt;
19275
28e94949
JB
19276 /* We have seen instances where the compiler tried to emit a byte
19277 size attribute of -1 which ended up being encoded as an unsigned
19278 0xffffffff. Although 0xffffffff is technically a valid size value,
19279 an object of this size seems pretty unlikely so we can relatively
19280 safely treat these cases as if the size attribute was invalid and
19281 treat them as zero by default. */
19282 if (attr->name == DW_AT_byte_size
19283 && form == DW_FORM_data4
19284 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19285 {
19286 complaint
b98664d3 19287 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19288 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19289 DW_UNSND (attr) = 0;
19290 }
28e94949 19291
c906108c
SS
19292 return info_ptr;
19293}
19294
a8329558
KW
19295/* Read an attribute described by an abbreviated attribute. */
19296
d521ce57 19297static const gdb_byte *
dee91e82
DE
19298read_attribute (const struct die_reader_specs *reader,
19299 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19300 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19301{
19302 attr->name = abbrev->name;
43988095 19303 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19304 abbrev->implicit_const, info_ptr,
19305 need_reprocess);
a8329558
KW
19306}
19307
43988095
JK
19308/* Return pointer to string at .debug_str offset STR_OFFSET. */
19309
19310static const char *
ed2dc618 19311read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19312 LONGEST str_offset)
43988095 19313{
5989a64e
SM
19314 return dwarf2_per_objfile->per_bfd->str.read_string
19315 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19316}
19317
43988095
JK
19318/* Return pointer to string at .debug_str offset as read from BUF.
19319 BUF is assumed to be in a compilation unit described by CU_HEADER.
19320 Return *BYTES_READ_PTR count of bytes read from BUF. */
19321
d521ce57 19322static const char *
ed2dc618
SM
19323read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19324 const gdb_byte *buf,
cf2c3c16
TT
19325 const struct comp_unit_head *cu_header,
19326 unsigned int *bytes_read_ptr)
19327{
8266302d 19328 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19329
4f44ae6c 19330 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19331}
19332
86c0bb4c 19333/* See read.h. */
43988095 19334
86c0bb4c
TT
19335const char *
19336dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19337 const struct comp_unit_head *cu_header,
19338 unsigned int *bytes_read_ptr)
43988095 19339{
86c0bb4c 19340 bfd *abfd = objfile->obfd;
8266302d 19341 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19342
5989a64e 19343 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19344}
19345
3019eac3 19346/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19347 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19348 ADDR_SIZE is the size of addresses from the CU header. */
19349
19350static CORE_ADDR
ed2dc618 19351read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19352 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19353 int addr_size)
3019eac3
DE
19354{
19355 struct objfile *objfile = dwarf2_per_objfile->objfile;
19356 bfd *abfd = objfile->obfd;
19357 const gdb_byte *info_ptr;
18a8505e 19358 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19359
5989a64e
SM
19360 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19361 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19362 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19363 objfile_name (objfile));
18a8505e 19364 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19365 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19366 error (_("DW_FORM_addr_index pointing outside of "
19367 ".debug_addr section [in module %s]"),
4262abfb 19368 objfile_name (objfile));
5989a64e 19369 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19370 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19371 if (addr_size == 4)
19372 return bfd_get_32 (abfd, info_ptr);
19373 else
19374 return bfd_get_64 (abfd, info_ptr);
19375}
19376
19377/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19378
19379static CORE_ADDR
19380read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19381{
5e22e966 19382 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19383 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19384}
19385
19386/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19387
19388static CORE_ADDR
d521ce57 19389read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19390 unsigned int *bytes_read)
19391{
5e22e966 19392 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19393 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19394
19395 return read_addr_index (cu, addr_index);
19396}
19397
450a1bfc 19398/* See read.h. */
3019eac3
DE
19399
19400CORE_ADDR
450a1bfc 19401dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19402{
ed2dc618 19403 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19404 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19405 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19406 int addr_size;
19407
3019eac3
DE
19408 /* We need addr_base and addr_size.
19409 If we don't have PER_CU->cu, we have to get it.
19410 Nasty, but the alternative is storing the needed info in PER_CU,
19411 which at this point doesn't seem justified: it's not clear how frequently
19412 it would get used and it would increase the size of every PER_CU.
19413 Entry points like dwarf2_per_cu_addr_size do a similar thing
19414 so we're not in uncharted territory here.
19415 Alas we need to be a bit more complicated as addr_base is contained
19416 in the DIE.
19417
19418 We don't need to read the entire CU(/TU).
19419 We just need the header and top level die.
a1b64ce1 19420
3019eac3 19421 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19422 For now we skip this optimization. */
3019eac3
DE
19423
19424 if (cu != NULL)
19425 {
19426 addr_base = cu->addr_base;
19427 addr_size = cu->header.addr_size;
19428 }
19429 else
19430 {
ab432490 19431 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19432 addr_base = reader.cu->addr_base;
19433 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19434 }
19435
ed2dc618
SM
19436 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19437 addr_size);
3019eac3
DE
19438}
19439
18a8505e
AT
19440/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19441 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19442 DWO file. */
3019eac3 19443
d521ce57 19444static const char *
18a8505e
AT
19445read_str_index (struct dwarf2_cu *cu,
19446 struct dwarf2_section_info *str_section,
19447 struct dwarf2_section_info *str_offsets_section,
19448 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19449{
5e22e966 19450 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19451 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19452 const char *objf_name = objfile_name (objfile);
3019eac3 19453 bfd *abfd = objfile->obfd;
d521ce57 19454 const gdb_byte *info_ptr;
3019eac3 19455 ULONGEST str_offset;
cf532bd1 19456 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19457
96b79293
TT
19458 str_section->read (objfile);
19459 str_offsets_section->read (objfile);
73869dc2 19460 if (str_section->buffer == NULL)
18a8505e 19461 error (_("%s used without %s section"
9d8780f0 19462 " in CU at offset %s [in module %s]"),
96b79293 19463 form_name, str_section->get_name (),
18a8505e 19464 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19465 if (str_offsets_section->buffer == NULL)
18a8505e 19466 error (_("%s used without %s section"
9d8780f0 19467 " in CU at offset %s [in module %s]"),
96b79293 19468 form_name, str_section->get_name (),
18a8505e 19469 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19470 info_ptr = (str_offsets_section->buffer
18a8505e 19471 + str_offsets_base
3019eac3
DE
19472 + str_index * cu->header.offset_size);
19473 if (cu->header.offset_size == 4)
19474 str_offset = bfd_get_32 (abfd, info_ptr);
19475 else
19476 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19477 if (str_offset >= str_section->size)
57d63ce2 19478 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19479 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19480 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19481 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19482}
19483
18a8505e
AT
19484/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19485
19486static const char *
19487read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19488{
19489 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19490 ? reader->cu->header.addr_size : 0;
19491 return read_str_index (reader->cu,
19492 &reader->dwo_file->sections.str,
19493 &reader->dwo_file->sections.str_offsets,
19494 str_offsets_base, str_index);
19495}
19496
19497/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19498
19499static const char *
19500read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19501{
5e22e966 19502 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19503 const char *objf_name = objfile_name (objfile);
19504 static const char form_name[] = "DW_FORM_GNU_str_index";
19505 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19506
19507 if (!cu->str_offsets_base.has_value ())
19508 error (_("%s used in Fission stub without %s"
19509 " in CU at offset 0x%lx [in module %s]"),
19510 form_name, str_offsets_attr_name,
19511 (long) cu->header.offset_size, objf_name);
19512
19513 return read_str_index (cu,
5e22e966
SM
19514 &cu->per_objfile->per_bfd->str,
19515 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19516 *cu->str_offsets_base, str_index);
19517}
19518
3019eac3
DE
19519/* Return the length of an LEB128 number in BUF. */
19520
19521static int
19522leb128_size (const gdb_byte *buf)
19523{
19524 const gdb_byte *begin = buf;
19525 gdb_byte byte;
19526
19527 while (1)
19528 {
19529 byte = *buf++;
19530 if ((byte & 128) == 0)
19531 return buf - begin;
19532 }
19533}
19534
c906108c 19535static void
e142c38c 19536set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19537{
19538 switch (lang)
19539 {
19540 case DW_LANG_C89:
76bee0cc 19541 case DW_LANG_C99:
0cfd832f 19542 case DW_LANG_C11:
c906108c 19543 case DW_LANG_C:
d1be3247 19544 case DW_LANG_UPC:
e142c38c 19545 cu->language = language_c;
c906108c 19546 break;
9c37b5ae 19547 case DW_LANG_Java:
c906108c 19548 case DW_LANG_C_plus_plus:
0cfd832f
MW
19549 case DW_LANG_C_plus_plus_11:
19550 case DW_LANG_C_plus_plus_14:
e142c38c 19551 cu->language = language_cplus;
c906108c 19552 break;
6aecb9c2
JB
19553 case DW_LANG_D:
19554 cu->language = language_d;
19555 break;
c906108c
SS
19556 case DW_LANG_Fortran77:
19557 case DW_LANG_Fortran90:
b21b22e0 19558 case DW_LANG_Fortran95:
f7de9aab
MW
19559 case DW_LANG_Fortran03:
19560 case DW_LANG_Fortran08:
e142c38c 19561 cu->language = language_fortran;
c906108c 19562 break;
a766d390
DE
19563 case DW_LANG_Go:
19564 cu->language = language_go;
19565 break;
c906108c 19566 case DW_LANG_Mips_Assembler:
e142c38c 19567 cu->language = language_asm;
c906108c
SS
19568 break;
19569 case DW_LANG_Ada83:
8aaf0b47 19570 case DW_LANG_Ada95:
bc5f45f8
JB
19571 cu->language = language_ada;
19572 break;
72019c9c
GM
19573 case DW_LANG_Modula2:
19574 cu->language = language_m2;
19575 break;
fe8e67fd
PM
19576 case DW_LANG_Pascal83:
19577 cu->language = language_pascal;
19578 break;
22566fbd
DJ
19579 case DW_LANG_ObjC:
19580 cu->language = language_objc;
19581 break;
c44af4eb
TT
19582 case DW_LANG_Rust:
19583 case DW_LANG_Rust_old:
19584 cu->language = language_rust;
19585 break;
c906108c
SS
19586 case DW_LANG_Cobol74:
19587 case DW_LANG_Cobol85:
c906108c 19588 default:
e142c38c 19589 cu->language = language_minimal;
c906108c
SS
19590 break;
19591 }
e142c38c 19592 cu->language_defn = language_def (cu->language);
c906108c
SS
19593}
19594
19595/* Return the named attribute or NULL if not there. */
19596
19597static struct attribute *
e142c38c 19598dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19599{
a48e046c 19600 for (;;)
c906108c 19601 {
a48e046c
TT
19602 unsigned int i;
19603 struct attribute *spec = NULL;
19604
19605 for (i = 0; i < die->num_attrs; ++i)
19606 {
19607 if (die->attrs[i].name == name)
19608 return &die->attrs[i];
19609 if (die->attrs[i].name == DW_AT_specification
19610 || die->attrs[i].name == DW_AT_abstract_origin)
19611 spec = &die->attrs[i];
19612 }
19613
19614 if (!spec)
19615 break;
c906108c 19616
f2f0e013 19617 die = follow_die_ref (die, spec, &cu);
f2f0e013 19618 }
c5aa993b 19619
c906108c
SS
19620 return NULL;
19621}
19622
7d45c7c3
KB
19623/* Return the string associated with a string-typed attribute, or NULL if it
19624 is either not found or is of an incorrect type. */
19625
19626static const char *
19627dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19628{
19629 struct attribute *attr;
19630 const char *str = NULL;
19631
19632 attr = dwarf2_attr (die, name, cu);
19633
19634 if (attr != NULL)
19635 {
e61108c9
TT
19636 str = attr->value_as_string ();
19637 if (str == nullptr)
b98664d3 19638 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19639 "DIE at %s in module %s"),
19640 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19641 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19642 }
19643
19644 return str;
19645}
19646
a084a2a6 19647/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19648 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19649static const char *
19650dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19651{
19652 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19653 if (dwo_name == nullptr)
19654 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19655 return dwo_name;
19656}
19657
05cf31d1
JB
19658/* Return non-zero iff the attribute NAME is defined for the given DIE,
19659 and holds a non-zero value. This function should only be used for
2dc7f7b3 19660 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19661
19662static int
19663dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19664{
19665 struct attribute *attr = dwarf2_attr (die, name, cu);
19666
19667 return (attr && DW_UNSND (attr));
19668}
19669
3ca72b44 19670static int
e142c38c 19671die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19672{
05cf31d1
JB
19673 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19674 which value is non-zero. However, we have to be careful with
19675 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19676 (via dwarf2_flag_true_p) follows this attribute. So we may
19677 end up accidently finding a declaration attribute that belongs
19678 to a different DIE referenced by the specification attribute,
19679 even though the given DIE does not have a declaration attribute. */
19680 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19681 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19682}
19683
63d06c5c 19684/* Return the die giving the specification for DIE, if there is
f2f0e013 19685 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19686 containing the return value on output. If there is no
19687 specification, but there is an abstract origin, that is
19688 returned. */
63d06c5c
DC
19689
19690static struct die_info *
f2f0e013 19691die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19692{
f2f0e013
DJ
19693 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19694 *spec_cu);
63d06c5c 19695
edb3359d
DJ
19696 if (spec_attr == NULL)
19697 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19698
63d06c5c
DC
19699 if (spec_attr == NULL)
19700 return NULL;
19701 else
f2f0e013 19702 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19703}
c906108c 19704
527f3840
JK
19705/* Stub for free_line_header to match void * callback types. */
19706
19707static void
19708free_line_header_voidp (void *arg)
19709{
9a3c8263 19710 struct line_header *lh = (struct line_header *) arg;
527f3840 19711
fff8551c 19712 delete lh;
527f3840
JK
19713}
19714
83769d0b 19715/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19716
19717static struct dwarf2_section_info *
19718get_debug_line_section (struct dwarf2_cu *cu)
19719{
19720 struct dwarf2_section_info *section;
5e22e966 19721 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19722
19723 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19724 DWO file. */
19725 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19726 section = &cu->dwo_unit->dwo_file->sections.line;
19727 else if (cu->per_cu->is_dwz)
19728 {
c3699833 19729 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19730
19731 section = &dwz->line;
19732 }
19733 else
5989a64e 19734 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19735
19736 return section;
19737}
19738
debd256d 19739/* Read the statement program header starting at OFFSET in
3019eac3 19740 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19741 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19742 Returns NULL if there is a problem reading the header, e.g., if it
19743 has a version we don't understand.
debd256d
JB
19744
19745 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19746 the returned object point into the dwarf line section buffer,
19747 and must not be freed. */
ae2de4f8 19748
fff8551c 19749static line_header_up
9c541725 19750dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19751{
3019eac3 19752 struct dwarf2_section_info *section;
5e22e966 19753 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19754
36586728 19755 section = get_debug_line_section (cu);
96b79293 19756 section->read (dwarf2_per_objfile->objfile);
3019eac3 19757 if (section->buffer == NULL)
debd256d 19758 {
3019eac3 19759 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19760 complaint (_("missing .debug_line.dwo section"));
3019eac3 19761 else
b98664d3 19762 complaint (_("missing .debug_line section"));
debd256d
JB
19763 return 0;
19764 }
19765
0df7ad3a
TT
19766 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19767 dwarf2_per_objfile, section,
19768 &cu->header);
debd256d 19769}
c906108c 19770
c6da4cef 19771/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19772 Return the file name of the psymtab for the given file_entry.
c6da4cef 19773 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19774 If space for the result is malloc'd, *NAME_HOLDER will be set.
19775 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19776
d521ce57 19777static const char *
7ba99d21 19778psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19779 const dwarf2_psymtab *pst,
c89b44cd
TT
19780 const char *comp_dir,
19781 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19782{
d521ce57
TT
19783 const char *include_name = fe.name;
19784 const char *include_name_to_compare = include_name;
72b9f47f 19785 const char *pst_filename;
c6da4cef
DE
19786 int file_is_pst;
19787
8c43009f 19788 const char *dir_name = fe.include_dir (lh);
c6da4cef 19789
c89b44cd 19790 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19791 if (!IS_ABSOLUTE_PATH (include_name)
19792 && (dir_name != NULL || comp_dir != NULL))
19793 {
19794 /* Avoid creating a duplicate psymtab for PST.
19795 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19796 Before we do the comparison, however, we need to account
19797 for DIR_NAME and COMP_DIR.
19798 First prepend dir_name (if non-NULL). If we still don't
19799 have an absolute path prepend comp_dir (if non-NULL).
19800 However, the directory we record in the include-file's
19801 psymtab does not contain COMP_DIR (to match the
19802 corresponding symtab(s)).
19803
19804 Example:
19805
19806 bash$ cd /tmp
19807 bash$ gcc -g ./hello.c
19808 include_name = "hello.c"
19809 dir_name = "."
19810 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19811 DW_AT_name = "./hello.c"
19812
19813 */
c6da4cef
DE
19814
19815 if (dir_name != NULL)
19816 {
c89b44cd
TT
19817 name_holder->reset (concat (dir_name, SLASH_STRING,
19818 include_name, (char *) NULL));
19819 include_name = name_holder->get ();
c6da4cef 19820 include_name_to_compare = include_name;
c6da4cef
DE
19821 }
19822 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19823 {
c89b44cd
TT
19824 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19825 include_name, (char *) NULL));
19826 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19827 }
19828 }
19829
19830 pst_filename = pst->filename;
c89b44cd 19831 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19832 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19833 {
c89b44cd
TT
19834 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19835 pst_filename, (char *) NULL));
19836 pst_filename = copied_name.get ();
c6da4cef
DE
19837 }
19838
1e3fad37 19839 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19840
c6da4cef
DE
19841 if (file_is_pst)
19842 return NULL;
19843 return include_name;
19844}
19845
d9b3de22
DE
19846/* State machine to track the state of the line number program. */
19847
6f77053d 19848class lnp_state_machine
d9b3de22 19849{
6f77053d
PA
19850public:
19851 /* Initialize a machine state for the start of a line number
19852 program. */
804d2729
TT
19853 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19854 bool record_lines_p);
6f77053d 19855
8c43009f
PA
19856 file_entry *current_file ()
19857 {
19858 /* lh->file_names is 0-based, but the file name numbers in the
19859 statement program are 1-based. */
6f77053d
PA
19860 return m_line_header->file_name_at (m_file);
19861 }
19862
19863 /* Record the line in the state machine. END_SEQUENCE is true if
19864 we're processing the end of a sequence. */
19865 void record_line (bool end_sequence);
19866
7ab6656f
OJ
19867 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19868 nop-out rest of the lines in this sequence. */
6f77053d
PA
19869 void check_line_address (struct dwarf2_cu *cu,
19870 const gdb_byte *line_ptr,
7ab6656f 19871 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19872
19873 void handle_set_discriminator (unsigned int discriminator)
19874 {
19875 m_discriminator = discriminator;
19876 m_line_has_non_zero_discriminator |= discriminator != 0;
19877 }
19878
19879 /* Handle DW_LNE_set_address. */
19880 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19881 {
19882 m_op_index = 0;
19883 address += baseaddr;
19884 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19885 }
19886
19887 /* Handle DW_LNS_advance_pc. */
19888 void handle_advance_pc (CORE_ADDR adjust);
19889
19890 /* Handle a special opcode. */
19891 void handle_special_opcode (unsigned char op_code);
19892
19893 /* Handle DW_LNS_advance_line. */
19894 void handle_advance_line (int line_delta)
19895 {
19896 advance_line (line_delta);
19897 }
19898
19899 /* Handle DW_LNS_set_file. */
19900 void handle_set_file (file_name_index file);
19901
19902 /* Handle DW_LNS_negate_stmt. */
19903 void handle_negate_stmt ()
19904 {
19905 m_is_stmt = !m_is_stmt;
19906 }
19907
19908 /* Handle DW_LNS_const_add_pc. */
19909 void handle_const_add_pc ();
19910
19911 /* Handle DW_LNS_fixed_advance_pc. */
19912 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19913 {
19914 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19915 m_op_index = 0;
19916 }
19917
19918 /* Handle DW_LNS_copy. */
19919 void handle_copy ()
19920 {
19921 record_line (false);
19922 m_discriminator = 0;
19923 }
19924
19925 /* Handle DW_LNE_end_sequence. */
19926 void handle_end_sequence ()
19927 {
804d2729 19928 m_currently_recording_lines = true;
6f77053d
PA
19929 }
19930
19931private:
19932 /* Advance the line by LINE_DELTA. */
19933 void advance_line (int line_delta)
19934 {
19935 m_line += line_delta;
19936
19937 if (line_delta != 0)
19938 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19939 }
19940
804d2729
TT
19941 struct dwarf2_cu *m_cu;
19942
6f77053d
PA
19943 gdbarch *m_gdbarch;
19944
19945 /* True if we're recording lines.
19946 Otherwise we're building partial symtabs and are just interested in
19947 finding include files mentioned by the line number program. */
19948 bool m_record_lines_p;
19949
8c43009f 19950 /* The line number header. */
6f77053d 19951 line_header *m_line_header;
8c43009f 19952
6f77053d
PA
19953 /* These are part of the standard DWARF line number state machine,
19954 and initialized according to the DWARF spec. */
d9b3de22 19955
6f77053d 19956 unsigned char m_op_index = 0;
7ba99d21
AT
19957 /* The line table index of the current file. */
19958 file_name_index m_file = 1;
6f77053d
PA
19959 unsigned int m_line = 1;
19960
19961 /* These are initialized in the constructor. */
19962
19963 CORE_ADDR m_address;
19964 bool m_is_stmt;
19965 unsigned int m_discriminator;
d9b3de22
DE
19966
19967 /* Additional bits of state we need to track. */
19968
19969 /* The last file that we called dwarf2_start_subfile for.
19970 This is only used for TLLs. */
6f77053d 19971 unsigned int m_last_file = 0;
d9b3de22 19972 /* The last file a line number was recorded for. */
6f77053d 19973 struct subfile *m_last_subfile = NULL;
d9b3de22 19974
804d2729
TT
19975 /* When true, record the lines we decode. */
19976 bool m_currently_recording_lines = false;
d9b3de22
DE
19977
19978 /* The last line number that was recorded, used to coalesce
19979 consecutive entries for the same line. This can happen, for
19980 example, when discriminators are present. PR 17276. */
6f77053d
PA
19981 unsigned int m_last_line = 0;
19982 bool m_line_has_non_zero_discriminator = false;
8c43009f 19983};
d9b3de22 19984
6f77053d
PA
19985void
19986lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19987{
19988 CORE_ADDR addr_adj = (((m_op_index + adjust)
19989 / m_line_header->maximum_ops_per_instruction)
19990 * m_line_header->minimum_instruction_length);
19991 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19992 m_op_index = ((m_op_index + adjust)
19993 % m_line_header->maximum_ops_per_instruction);
19994}
d9b3de22 19995
6f77053d
PA
19996void
19997lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19998{
6f77053d 19999 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20000 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20001 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20002 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20003 / m_line_header->maximum_ops_per_instruction)
20004 * m_line_header->minimum_instruction_length);
20005 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20006 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20007 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20008
258bf0ee 20009 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20010 advance_line (line_delta);
20011 record_line (false);
20012 m_discriminator = 0;
20013}
d9b3de22 20014
6f77053d
PA
20015void
20016lnp_state_machine::handle_set_file (file_name_index file)
20017{
20018 m_file = file;
20019
20020 const file_entry *fe = current_file ();
20021 if (fe == NULL)
20022 dwarf2_debug_line_missing_file_complaint ();
20023 else if (m_record_lines_p)
20024 {
20025 const char *dir = fe->include_dir (m_line_header);
20026
c24bdb02 20027 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20028 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20029 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20030 }
20031}
20032
20033void
20034lnp_state_machine::handle_const_add_pc ()
20035{
20036 CORE_ADDR adjust
20037 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20038
20039 CORE_ADDR addr_adj
20040 = (((m_op_index + adjust)
20041 / m_line_header->maximum_ops_per_instruction)
20042 * m_line_header->minimum_instruction_length);
20043
20044 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20045 m_op_index = ((m_op_index + adjust)
20046 % m_line_header->maximum_ops_per_instruction);
20047}
d9b3de22 20048
a05a36a5
DE
20049/* Return non-zero if we should add LINE to the line number table.
20050 LINE is the line to add, LAST_LINE is the last line that was added,
20051 LAST_SUBFILE is the subfile for LAST_LINE.
20052 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20053 had a non-zero discriminator.
20054
20055 We have to be careful in the presence of discriminators.
20056 E.g., for this line:
20057
20058 for (i = 0; i < 100000; i++);
20059
20060 clang can emit four line number entries for that one line,
20061 each with a different discriminator.
20062 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20063
20064 However, we want gdb to coalesce all four entries into one.
20065 Otherwise the user could stepi into the middle of the line and
20066 gdb would get confused about whether the pc really was in the
20067 middle of the line.
20068
20069 Things are further complicated by the fact that two consecutive
20070 line number entries for the same line is a heuristic used by gcc
20071 to denote the end of the prologue. So we can't just discard duplicate
20072 entries, we have to be selective about it. The heuristic we use is
20073 that we only collapse consecutive entries for the same line if at least
20074 one of those entries has a non-zero discriminator. PR 17276.
20075
20076 Note: Addresses in the line number state machine can never go backwards
20077 within one sequence, thus this coalescing is ok. */
20078
20079static int
804d2729
TT
20080dwarf_record_line_p (struct dwarf2_cu *cu,
20081 unsigned int line, unsigned int last_line,
a05a36a5
DE
20082 int line_has_non_zero_discriminator,
20083 struct subfile *last_subfile)
20084{
c24bdb02 20085 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20086 return 1;
20087 if (line != last_line)
20088 return 1;
20089 /* Same line for the same file that we've seen already.
20090 As a last check, for pr 17276, only record the line if the line
20091 has never had a non-zero discriminator. */
20092 if (!line_has_non_zero_discriminator)
20093 return 1;
20094 return 0;
20095}
20096
804d2729
TT
20097/* Use the CU's builder to record line number LINE beginning at
20098 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20099
20100static void
d9b3de22 20101dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20102 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20103 struct dwarf2_cu *cu)
252a6764
DE
20104{
20105 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20106
27e0867f
DE
20107 if (dwarf_line_debug)
20108 {
20109 fprintf_unfiltered (gdb_stdlog,
20110 "Recording line %u, file %s, address %s\n",
20111 line, lbasename (subfile->name),
20112 paddress (gdbarch, address));
20113 }
20114
804d2729 20115 if (cu != nullptr)
8c95582d 20116 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20117}
20118
20119/* Subroutine of dwarf_decode_lines_1 to simplify it.
20120 Mark the end of a set of line number records.
d9b3de22 20121 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20122 If SUBFILE is NULL the request is ignored. */
20123
20124static void
20125dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20126 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20127{
27e0867f
DE
20128 if (subfile == NULL)
20129 return;
20130
20131 if (dwarf_line_debug)
20132 {
20133 fprintf_unfiltered (gdb_stdlog,
20134 "Finishing current line, file %s, address %s\n",
20135 lbasename (subfile->name),
20136 paddress (gdbarch, address));
20137 }
20138
8c95582d 20139 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20140}
20141
6f77053d
PA
20142void
20143lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20144{
d9b3de22
DE
20145 if (dwarf_line_debug)
20146 {
20147 fprintf_unfiltered (gdb_stdlog,
20148 "Processing actual line %u: file %u,"
94a72be7 20149 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20150 m_line, m_file,
6f77053d 20151 paddress (m_gdbarch, m_address),
94a72be7
AB
20152 m_is_stmt, m_discriminator,
20153 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20154 }
20155
6f77053d 20156 file_entry *fe = current_file ();
8c43009f
PA
20157
20158 if (fe == NULL)
d9b3de22
DE
20159 dwarf2_debug_line_missing_file_complaint ();
20160 /* For now we ignore lines not starting on an instruction boundary.
20161 But not when processing end_sequence for compatibility with the
20162 previous version of the code. */
6f77053d 20163 else if (m_op_index == 0 || end_sequence)
d9b3de22 20164 {
8c43009f 20165 fe->included_p = 1;
8c95582d 20166 if (m_record_lines_p)
d9b3de22 20167 {
c24bdb02 20168 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20169 || end_sequence)
d9b3de22 20170 {
804d2729
TT
20171 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20172 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20173 }
20174
20175 if (!end_sequence)
20176 {
8c95582d
AB
20177 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20178
804d2729 20179 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20180 m_line_has_non_zero_discriminator,
20181 m_last_subfile))
d9b3de22 20182 {
c24bdb02 20183 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20184 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20185 builder->get_current_subfile (),
8c95582d 20186 m_line, m_address, is_stmt,
804d2729 20187 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20188 }
c24bdb02 20189 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20190 m_last_line = m_line;
d9b3de22
DE
20191 }
20192 }
20193 }
20194}
20195
804d2729
TT
20196lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20197 line_header *lh, bool record_lines_p)
d9b3de22 20198{
804d2729 20199 m_cu = cu;
6f77053d
PA
20200 m_gdbarch = arch;
20201 m_record_lines_p = record_lines_p;
20202 m_line_header = lh;
d9b3de22 20203
804d2729 20204 m_currently_recording_lines = true;
d9b3de22 20205
d9b3de22
DE
20206 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20207 was a line entry for it so that the backend has a chance to adjust it
20208 and also record it in case it needs it. This is currently used by MIPS
20209 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20210 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20211 m_is_stmt = lh->default_is_stmt;
20212 m_discriminator = 0;
252a6764
DE
20213}
20214
6f77053d
PA
20215void
20216lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20217 const gdb_byte *line_ptr,
7ab6656f 20218 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20219{
7ab6656f
OJ
20220 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20221 the pc range of the CU. However, we restrict the test to only ADDRESS
20222 values of zero to preserve GDB's previous behaviour which is to handle
20223 the specific case of a function being GC'd by the linker. */
924c2928 20224
7ab6656f 20225 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20226 {
20227 /* This line table is for a function which has been
20228 GCd by the linker. Ignore it. PR gdb/12528 */
20229
5e22e966 20230 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20231 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20232
b98664d3 20233 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20234 line_offset, objfile_name (objfile));
804d2729
TT
20235 m_currently_recording_lines = false;
20236 /* Note: m_currently_recording_lines is left as false until we see
20237 DW_LNE_end_sequence. */
924c2928
DE
20238 }
20239}
20240
f3f5162e 20241/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20242 Process the line number information in LH.
20243 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20244 program in order to set included_p for every referenced header. */
debd256d 20245
c906108c 20246static void
43f3e411
DE
20247dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20248 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20249{
d521ce57
TT
20250 const gdb_byte *line_ptr, *extended_end;
20251 const gdb_byte *line_end;
a8c50c1f 20252 unsigned int bytes_read, extended_len;
699ca60a 20253 unsigned char op_code, extended_op;
e142c38c 20254 CORE_ADDR baseaddr;
5e22e966 20255 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20256 bfd *abfd = objfile->obfd;
08feed99 20257 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20258 /* True if we're recording line info (as opposed to building partial
20259 symtabs and just interested in finding include files mentioned by
20260 the line number program). */
20261 bool record_lines_p = !decode_for_pst_p;
e142c38c 20262
b3b3bada 20263 baseaddr = objfile->text_section_offset ();
c906108c 20264
debd256d
JB
20265 line_ptr = lh->statement_program_start;
20266 line_end = lh->statement_program_end;
c906108c
SS
20267
20268 /* Read the statement sequences until there's nothing left. */
20269 while (line_ptr < line_end)
20270 {
6f77053d
PA
20271 /* The DWARF line number program state machine. Reset the state
20272 machine at the start of each sequence. */
804d2729 20273 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20274 bool end_sequence = false;
d9b3de22 20275
8c43009f 20276 if (record_lines_p)
c906108c 20277 {
8c43009f
PA
20278 /* Start a subfile for the current file of the state
20279 machine. */
20280 const file_entry *fe = state_machine.current_file ();
20281
20282 if (fe != NULL)
804d2729 20283 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20284 }
20285
a738430d 20286 /* Decode the table. */
d9b3de22 20287 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20288 {
20289 op_code = read_1_byte (abfd, line_ptr);
20290 line_ptr += 1;
9aa1fe7e 20291
debd256d 20292 if (op_code >= lh->opcode_base)
6e70227d 20293 {
8e07a239 20294 /* Special opcode. */
6f77053d 20295 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20296 }
20297 else switch (op_code)
c906108c
SS
20298 {
20299 case DW_LNS_extended_op:
3e43a32a
MS
20300 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20301 &bytes_read);
473b7be6 20302 line_ptr += bytes_read;
a8c50c1f 20303 extended_end = line_ptr + extended_len;
c906108c
SS
20304 extended_op = read_1_byte (abfd, line_ptr);
20305 line_ptr += 1;
20306 switch (extended_op)
20307 {
20308 case DW_LNE_end_sequence:
6f77053d
PA
20309 state_machine.handle_end_sequence ();
20310 end_sequence = true;
c906108c
SS
20311 break;
20312 case DW_LNE_set_address:
d9b3de22
DE
20313 {
20314 CORE_ADDR address
c8a7a66f 20315 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20316 line_ptr += bytes_read;
6f77053d
PA
20317
20318 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20319 lowpc - baseaddr, address);
6f77053d 20320 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20321 }
c906108c
SS
20322 break;
20323 case DW_LNE_define_file:
debd256d 20324 {
d521ce57 20325 const char *cur_file;
ecfb656c
PA
20326 unsigned int mod_time, length;
20327 dir_index dindex;
6e70227d 20328
3e43a32a
MS
20329 cur_file = read_direct_string (abfd, line_ptr,
20330 &bytes_read);
debd256d 20331 line_ptr += bytes_read;
ecfb656c 20332 dindex = (dir_index)
debd256d
JB
20333 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20334 line_ptr += bytes_read;
20335 mod_time =
20336 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20337 line_ptr += bytes_read;
20338 length =
20339 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20340 line_ptr += bytes_read;
ecfb656c 20341 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20342 }
c906108c 20343 break;
d0c6ba3d 20344 case DW_LNE_set_discriminator:
6f77053d
PA
20345 {
20346 /* The discriminator is not interesting to the
20347 debugger; just ignore it. We still need to
20348 check its value though:
20349 if there are consecutive entries for the same
20350 (non-prologue) line we want to coalesce them.
20351 PR 17276. */
20352 unsigned int discr
20353 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20354 line_ptr += bytes_read;
20355
20356 state_machine.handle_set_discriminator (discr);
20357 }
d0c6ba3d 20358 break;
c906108c 20359 default:
b98664d3 20360 complaint (_("mangled .debug_line section"));
debd256d 20361 return;
c906108c 20362 }
a8c50c1f
DJ
20363 /* Make sure that we parsed the extended op correctly. If e.g.
20364 we expected a different address size than the producer used,
20365 we may have read the wrong number of bytes. */
20366 if (line_ptr != extended_end)
20367 {
b98664d3 20368 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20369 return;
20370 }
c906108c
SS
20371 break;
20372 case DW_LNS_copy:
6f77053d 20373 state_machine.handle_copy ();
c906108c
SS
20374 break;
20375 case DW_LNS_advance_pc:
2dc7f7b3
TT
20376 {
20377 CORE_ADDR adjust
20378 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20379 line_ptr += bytes_read;
6f77053d
PA
20380
20381 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20382 }
c906108c
SS
20383 break;
20384 case DW_LNS_advance_line:
a05a36a5
DE
20385 {
20386 int line_delta
20387 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20388 line_ptr += bytes_read;
6f77053d
PA
20389
20390 state_machine.handle_advance_line (line_delta);
a05a36a5 20391 }
c906108c
SS
20392 break;
20393 case DW_LNS_set_file:
d9b3de22 20394 {
6f77053d 20395 file_name_index file
ecfb656c
PA
20396 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20397 &bytes_read);
d9b3de22 20398 line_ptr += bytes_read;
8c43009f 20399
6f77053d 20400 state_machine.handle_set_file (file);
d9b3de22 20401 }
c906108c
SS
20402 break;
20403 case DW_LNS_set_column:
0ad93d4f 20404 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20405 line_ptr += bytes_read;
20406 break;
20407 case DW_LNS_negate_stmt:
6f77053d 20408 state_machine.handle_negate_stmt ();
c906108c
SS
20409 break;
20410 case DW_LNS_set_basic_block:
c906108c 20411 break;
c2c6d25f
JM
20412 /* Add to the address register of the state machine the
20413 address increment value corresponding to special opcode
a738430d
MK
20414 255. I.e., this value is scaled by the minimum
20415 instruction length since special opcode 255 would have
b021a221 20416 scaled the increment. */
c906108c 20417 case DW_LNS_const_add_pc:
6f77053d 20418 state_machine.handle_const_add_pc ();
c906108c
SS
20419 break;
20420 case DW_LNS_fixed_advance_pc:
3e29f34a 20421 {
6f77053d 20422 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20423 line_ptr += 2;
6f77053d
PA
20424
20425 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20426 }
c906108c 20427 break;
9aa1fe7e 20428 default:
a738430d
MK
20429 {
20430 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20431 int i;
a738430d 20432
debd256d 20433 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20434 {
20435 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20436 line_ptr += bytes_read;
20437 }
20438 }
c906108c
SS
20439 }
20440 }
d9b3de22
DE
20441
20442 if (!end_sequence)
20443 dwarf2_debug_line_missing_end_sequence_complaint ();
20444
20445 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20446 in which case we still finish recording the last line). */
6f77053d 20447 state_machine.record_line (true);
c906108c 20448 }
f3f5162e
DE
20449}
20450
20451/* Decode the Line Number Program (LNP) for the given line_header
20452 structure and CU. The actual information extracted and the type
20453 of structures created from the LNP depends on the value of PST.
20454
20455 1. If PST is NULL, then this procedure uses the data from the program
20456 to create all necessary symbol tables, and their linetables.
20457
20458 2. If PST is not NULL, this procedure reads the program to determine
20459 the list of files included by the unit represented by PST, and
20460 builds all the associated partial symbol tables.
20461
20462 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20463 It is used for relative paths in the line table.
20464 NOTE: When processing partial symtabs (pst != NULL),
20465 comp_dir == pst->dirname.
20466
20467 NOTE: It is important that psymtabs have the same file name (via strcmp)
20468 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20469 symtab we don't use it in the name of the psymtabs we create.
20470 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20471 A good testcase for this is mb-inline.exp.
20472
527f3840
JK
20473 LOWPC is the lowest address in CU (or 0 if not known).
20474
20475 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20476 for its PC<->lines mapping information. Otherwise only the filename
20477 table is read in. */
f3f5162e
DE
20478
20479static void
20480dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20481 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20482 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20483{
5e22e966 20484 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20485 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20486
527f3840
JK
20487 if (decode_mapping)
20488 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20489
20490 if (decode_for_pst_p)
20491 {
aaa75496
JB
20492 /* Now that we're done scanning the Line Header Program, we can
20493 create the psymtab of each included file. */
7ba99d21
AT
20494 for (auto &file_entry : lh->file_names ())
20495 if (file_entry.included_p == 1)
aaa75496 20496 {
c89b44cd 20497 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20498 const char *include_name =
7ba99d21
AT
20499 psymtab_include_file_name (lh, file_entry, pst,
20500 comp_dir, &name_holder);
c6da4cef 20501 if (include_name != NULL)
aaa75496
JB
20502 dwarf2_create_include_psymtab (include_name, pst, objfile);
20503 }
20504 }
cb1df416
DJ
20505 else
20506 {
20507 /* Make sure a symtab is created for every file, even files
20508 which contain only variables (i.e. no code with associated
20509 line numbers). */
c24bdb02
KS
20510 buildsym_compunit *builder = cu->get_builder ();
20511 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20512
7ba99d21 20513 for (auto &fe : lh->file_names ())
cb1df416 20514 {
804d2729 20515 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20516 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20517 {
c24bdb02 20518 builder->get_current_subfile ()->symtab
804d2729 20519 = allocate_symtab (cust,
c24bdb02 20520 builder->get_current_subfile ()->name);
43f3e411 20521 }
c24bdb02 20522 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20523 }
20524 }
c906108c
SS
20525}
20526
20527/* Start a subfile for DWARF. FILENAME is the name of the file and
20528 DIRNAME the name of the source directory which contains FILENAME
4d663531 20529 or NULL if not known.
c906108c
SS
20530 This routine tries to keep line numbers from identical absolute and
20531 relative file names in a common subfile.
20532
20533 Using the `list' example from the GDB testsuite, which resides in
20534 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20535 of /srcdir/list0.c yields the following debugging information for list0.c:
20536
c5aa993b 20537 DW_AT_name: /srcdir/list0.c
4d663531 20538 DW_AT_comp_dir: /compdir
357e46e7 20539 files.files[0].name: list0.h
c5aa993b 20540 files.files[0].dir: /srcdir
357e46e7 20541 files.files[1].name: list0.c
c5aa993b 20542 files.files[1].dir: /srcdir
c906108c
SS
20543
20544 The line number information for list0.c has to end up in a single
4f1520fb
FR
20545 subfile, so that `break /srcdir/list0.c:1' works as expected.
20546 start_subfile will ensure that this happens provided that we pass the
20547 concatenation of files.files[1].dir and files.files[1].name as the
20548 subfile's name. */
c906108c
SS
20549
20550static void
804d2729
TT
20551dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20552 const char *dirname)
c906108c 20553{
43816ebc 20554 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20555
4d663531 20556 /* In order not to lose the line information directory,
4f1520fb
FR
20557 we concatenate it to the filename when it makes sense.
20558 Note that the Dwarf3 standard says (speaking of filenames in line
20559 information): ``The directory index is ignored for file names
20560 that represent full path names''. Thus ignoring dirname in the
20561 `else' branch below isn't an issue. */
c906108c 20562
d5166ae1 20563 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20564 {
43816ebc
TT
20565 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20566 filename = copy.get ();
d521ce57 20567 }
c906108c 20568
c24bdb02 20569 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20570}
20571
804d2729
TT
20572/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20573 buildsym_compunit constructor. */
f4dc4d17 20574
c24bdb02
KS
20575struct compunit_symtab *
20576dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20577 CORE_ADDR low_pc)
f4dc4d17 20578{
c24bdb02 20579 gdb_assert (m_builder == nullptr);
43f3e411 20580
c24bdb02
KS
20581 m_builder.reset (new struct buildsym_compunit
20582 (per_cu->dwarf2_per_objfile->objfile,
20583 name, comp_dir, language, low_pc));
93b8bea4 20584
c24bdb02 20585 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20586
c24bdb02
KS
20587 get_builder ()->record_debugformat ("DWARF 2");
20588 get_builder ()->record_producer (producer);
f4dc4d17 20589
c24bdb02 20590 processing_has_namespace_info = false;
43f3e411 20591
c24bdb02 20592 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20593}
20594
4c2df51b
DJ
20595static void
20596var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20597 struct dwarf2_cu *cu)
4c2df51b 20598{
5e22e966 20599 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20600 struct comp_unit_head *cu_header = &cu->header;
20601
4c2df51b
DJ
20602 /* NOTE drow/2003-01-30: There used to be a comment and some special
20603 code here to turn a symbol with DW_AT_external and a
20604 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20605 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20606 with some versions of binutils) where shared libraries could have
20607 relocations against symbols in their debug information - the
20608 minimal symbol would have the right address, but the debug info
20609 would not. It's no longer necessary, because we will explicitly
20610 apply relocations when we read in the debug information now. */
20611
20612 /* A DW_AT_location attribute with no contents indicates that a
20613 variable has been optimized away. */
4fc6c0d5 20614 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20615 {
f1e6e072 20616 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20617 return;
20618 }
20619
20620 /* Handle one degenerate form of location expression specially, to
20621 preserve GDB's previous behavior when section offsets are
336d760d
AT
20622 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20623 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20624
4fc6c0d5 20625 if (attr->form_is_block ()
3019eac3
DE
20626 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20627 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20628 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20629 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20630 && (DW_BLOCK (attr)->size
20631 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20632 {
891d2f0b 20633 unsigned int dummy;
4c2df51b 20634
3019eac3 20635 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20636 SET_SYMBOL_VALUE_ADDRESS
20637 (sym, cu->header.read_address (objfile->obfd,
20638 DW_BLOCK (attr)->data + 1,
20639 &dummy));
3019eac3 20640 else
38583298
TT
20641 SET_SYMBOL_VALUE_ADDRESS
20642 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20643 &dummy));
f1e6e072 20644 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20645 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20646 SET_SYMBOL_VALUE_ADDRESS
20647 (sym,
20648 SYMBOL_VALUE_ADDRESS (sym)
20649 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20650 return;
20651 }
20652
20653 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20654 expression evaluator, and use LOC_COMPUTED only when necessary
20655 (i.e. when the value of a register or memory location is
20656 referenced, or a thread-local block, etc.). Then again, it might
20657 not be worthwhile. I'm assuming that it isn't unless performance
20658 or memory numbers show me otherwise. */
20659
f1e6e072 20660 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20661
f1e6e072 20662 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20663 cu->has_loclist = true;
4c2df51b
DJ
20664}
20665
c906108c
SS
20666/* Given a pointer to a DWARF information entry, figure out if we need
20667 to make a symbol table entry for it, and if so, create a new entry
20668 and return a pointer to it.
20669 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20670 used the passed type.
20671 If SPACE is not NULL, use it to hold the new symbol. If it is
20672 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20673
20674static struct symbol *
5e2db402
TT
20675new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20676 struct symbol *space)
c906108c 20677{
5e22e966 20678 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20679 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20680 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20681 struct symbol *sym = NULL;
15d034d0 20682 const char *name;
c906108c
SS
20683 struct attribute *attr = NULL;
20684 struct attribute *attr2 = NULL;
e142c38c 20685 CORE_ADDR baseaddr;
e37fd15a
SW
20686 struct pending **list_to_add = NULL;
20687
edb3359d 20688 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20689
b3b3bada 20690 baseaddr = objfile->text_section_offset ();
c906108c 20691
94af9270 20692 name = dwarf2_name (die, cu);
c906108c
SS
20693 if (name)
20694 {
34eaf542 20695 int suppress_add = 0;
94af9270 20696
34eaf542
TT
20697 if (space)
20698 sym = space;
20699 else
8c14c3a3 20700 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20701 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20702
20703 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20704 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20705 /* Fortran does not have mangling standard and the mangling does differ
20706 between gfortran, iFort etc. */
bcfe6157
TT
20707 const char *physname
20708 = (cu->language == language_fortran
20709 ? dwarf2_full_name (name, die, cu)
20710 : dwarf2_physname (name, die, cu));
20711 const char *linkagename = dw2_linkage_name (die, cu);
20712
20713 if (linkagename == nullptr || cu->language == language_ada)
20714 sym->set_linkage_name (physname);
20715 else
20716 {
20717 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20718 sym->set_linkage_name (linkagename);
20719 }
f55ee35c 20720
c906108c 20721 /* Default assumptions.
c5aa993b 20722 Use the passed type or decode it from the die. */
176620f1 20723 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20724 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20725 if (type != NULL)
20726 SYMBOL_TYPE (sym) = type;
20727 else
e7c27a73 20728 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20729 attr = dwarf2_attr (die,
20730 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20731 cu);
435d3d88 20732 if (attr != nullptr)
c906108c
SS
20733 {
20734 SYMBOL_LINE (sym) = DW_UNSND (attr);
20735 }
cb1df416 20736
edb3359d
DJ
20737 attr = dwarf2_attr (die,
20738 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20739 cu);
435d3d88 20740 if (attr != nullptr)
cb1df416 20741 {
ecfb656c 20742 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20743 struct file_entry *fe;
9a619af0 20744
ecfb656c
PA
20745 if (cu->line_header != NULL)
20746 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20747 else
20748 fe = NULL;
20749
20750 if (fe == NULL)
b98664d3 20751 complaint (_("file index out of range"));
8c43009f
PA
20752 else
20753 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20754 }
20755
c906108c
SS
20756 switch (die->tag)
20757 {
20758 case DW_TAG_label:
e142c38c 20759 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20760 if (attr != nullptr)
3e29f34a
MR
20761 {
20762 CORE_ADDR addr;
20763
cd6c91b4 20764 addr = attr->value_as_address ();
3e29f34a 20765 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20766 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20767 }
0f5238ed
TT
20768 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20769 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20770 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20771 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20772 break;
20773 case DW_TAG_subprogram:
20774 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20775 finish_block. */
f1e6e072 20776 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20777 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20778 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20779 || cu->language == language_ada
20780 || cu->language == language_fortran)
c906108c 20781 {
2cfa0c8d 20782 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20783 Ada and Fortran subprograms, whether marked external or
20784 not, are always stored as a global symbol, because we want
20785 to be able to access them globally. For instance, we want
20786 to be able to break on a nested subprogram without having
20787 to specify the context. */
c24bdb02 20788 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20789 }
20790 else
20791 {
e37fd15a 20792 list_to_add = cu->list_in_scope;
c906108c
SS
20793 }
20794 break;
edb3359d
DJ
20795 case DW_TAG_inlined_subroutine:
20796 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20797 finish_block. */
f1e6e072 20798 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20799 SYMBOL_INLINED (sym) = 1;
481860b3 20800 list_to_add = cu->list_in_scope;
edb3359d 20801 break;
34eaf542
TT
20802 case DW_TAG_template_value_param:
20803 suppress_add = 1;
20804 /* Fall through. */
72929c62 20805 case DW_TAG_constant:
c906108c 20806 case DW_TAG_variable:
254e6b9e 20807 case DW_TAG_member:
0963b4bd
MS
20808 /* Compilation with minimal debug info may result in
20809 variables with missing type entries. Change the
20810 misleading `void' type to something sensible. */
78134374 20811 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20812 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20813
e142c38c 20814 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20815 /* In the case of DW_TAG_member, we should only be called for
20816 static const members. */
20817 if (die->tag == DW_TAG_member)
20818 {
3863f96c
DE
20819 /* dwarf2_add_field uses die_is_declaration,
20820 so we do the same. */
254e6b9e
DE
20821 gdb_assert (die_is_declaration (die, cu));
20822 gdb_assert (attr);
20823 }
435d3d88 20824 if (attr != nullptr)
c906108c 20825 {
e7c27a73 20826 dwarf2_const_value (attr, sym, cu);
e142c38c 20827 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20828 if (!suppress_add)
34eaf542
TT
20829 {
20830 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20831 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20832 else
e37fd15a 20833 list_to_add = cu->list_in_scope;
34eaf542 20834 }
c906108c
SS
20835 break;
20836 }
e142c38c 20837 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20838 if (attr != nullptr)
c906108c 20839 {
e7c27a73 20840 var_decode_location (attr, sym, cu);
e142c38c 20841 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20842
20843 /* Fortran explicitly imports any global symbols to the local
20844 scope by DW_TAG_common_block. */
20845 if (cu->language == language_fortran && die->parent
20846 && die->parent->tag == DW_TAG_common_block)
20847 attr2 = NULL;
20848
caac4577
JG
20849 if (SYMBOL_CLASS (sym) == LOC_STATIC
20850 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20851 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20852 {
20853 /* When a static variable is eliminated by the linker,
20854 the corresponding debug information is not stripped
20855 out, but the variable address is set to null;
20856 do not add such variables into symbol table. */
20857 }
20858 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20859 {
4b610737
TT
20860 if (SYMBOL_CLASS (sym) == LOC_STATIC
20861 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20862 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20863 {
20864 /* A global static variable might be subject to
20865 copy relocation. We first check for a local
20866 minsym, though, because maybe the symbol was
20867 marked hidden, in which case this would not
20868 apply. */
20869 bound_minimal_symbol found
20870 = (lookup_minimal_symbol_linkage
987012b8 20871 (sym->linkage_name (), objfile));
4b610737
TT
20872 if (found.minsym != nullptr)
20873 sym->maybe_copied = 1;
20874 }
f55ee35c 20875
1c809c68
TT
20876 /* A variable with DW_AT_external is never static,
20877 but it may be block-scoped. */
804d2729 20878 list_to_add
c24bdb02
KS
20879 = ((cu->list_in_scope
20880 == cu->get_builder ()->get_file_symbols ())
20881 ? cu->get_builder ()->get_global_symbols ()
804d2729 20882 : cu->list_in_scope);
1c809c68 20883 }
c906108c 20884 else
e37fd15a 20885 list_to_add = cu->list_in_scope;
c906108c
SS
20886 }
20887 else
20888 {
20889 /* We do not know the address of this symbol.
c5aa993b
JM
20890 If it is an external symbol and we have type information
20891 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20892 The address of the variable will then be determined from
20893 the minimal symbol table whenever the variable is
20894 referenced. */
e142c38c 20895 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20896
20897 /* Fortran explicitly imports any global symbols to the local
20898 scope by DW_TAG_common_block. */
20899 if (cu->language == language_fortran && die->parent
20900 && die->parent->tag == DW_TAG_common_block)
20901 {
20902 /* SYMBOL_CLASS doesn't matter here because
20903 read_common_block is going to reset it. */
20904 if (!suppress_add)
20905 list_to_add = cu->list_in_scope;
20906 }
20907 else if (attr2 && (DW_UNSND (attr2) != 0)
20908 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20909 {
0fe7935b
DJ
20910 /* A variable with DW_AT_external is never static, but it
20911 may be block-scoped. */
804d2729 20912 list_to_add
c24bdb02
KS
20913 = ((cu->list_in_scope
20914 == cu->get_builder ()->get_file_symbols ())
20915 ? cu->get_builder ()->get_global_symbols ()
804d2729 20916 : cu->list_in_scope);
0fe7935b 20917
f1e6e072 20918 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20919 }
442ddf59
JK
20920 else if (!die_is_declaration (die, cu))
20921 {
20922 /* Use the default LOC_OPTIMIZED_OUT class. */
20923 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20924 if (!suppress_add)
20925 list_to_add = cu->list_in_scope;
442ddf59 20926 }
c906108c
SS
20927 }
20928 break;
20929 case DW_TAG_formal_parameter:
a60f3166
TT
20930 {
20931 /* If we are inside a function, mark this as an argument. If
20932 not, we might be looking at an argument to an inlined function
20933 when we do not have enough information to show inlined frames;
20934 pretend it's a local variable in that case so that the user can
20935 still see it. */
804d2729 20936 struct context_stack *curr
c24bdb02 20937 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20938 if (curr != nullptr && curr->name != nullptr)
20939 SYMBOL_IS_ARGUMENT (sym) = 1;
20940 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20941 if (attr != nullptr)
a60f3166
TT
20942 {
20943 var_decode_location (attr, sym, cu);
20944 }
20945 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20946 if (attr != nullptr)
a60f3166
TT
20947 {
20948 dwarf2_const_value (attr, sym, cu);
20949 }
f346a30d 20950
a60f3166
TT
20951 list_to_add = cu->list_in_scope;
20952 }
c906108c
SS
20953 break;
20954 case DW_TAG_unspecified_parameters:
20955 /* From varargs functions; gdb doesn't seem to have any
20956 interest in this information, so just ignore it for now.
20957 (FIXME?) */
20958 break;
34eaf542
TT
20959 case DW_TAG_template_type_param:
20960 suppress_add = 1;
20961 /* Fall through. */
c906108c 20962 case DW_TAG_class_type:
680b30c7 20963 case DW_TAG_interface_type:
c906108c
SS
20964 case DW_TAG_structure_type:
20965 case DW_TAG_union_type:
72019c9c 20966 case DW_TAG_set_type:
c906108c 20967 case DW_TAG_enumeration_type:
f1e6e072 20968 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20969 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20970
63d06c5c 20971 {
9c37b5ae 20972 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20973 really ever be static objects: otherwise, if you try
20974 to, say, break of a class's method and you're in a file
20975 which doesn't mention that class, it won't work unless
20976 the check for all static symbols in lookup_symbol_aux
20977 saves you. See the OtherFileClass tests in
20978 gdb.c++/namespace.exp. */
20979
e37fd15a 20980 if (!suppress_add)
34eaf542 20981 {
c24bdb02 20982 buildsym_compunit *builder = cu->get_builder ();
804d2729 20983 list_to_add
c24bdb02 20984 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20985 && cu->language == language_cplus
c24bdb02 20986 ? builder->get_global_symbols ()
804d2729 20987 : cu->list_in_scope);
63d06c5c 20988
64382290 20989 /* The semantics of C++ state that "struct foo {
9c37b5ae 20990 ... }" also defines a typedef for "foo". */
64382290 20991 if (cu->language == language_cplus
45280282 20992 || cu->language == language_ada
c44af4eb
TT
20993 || cu->language == language_d
20994 || cu->language == language_rust)
64382290
TT
20995 {
20996 /* The symbol's name is already allocated along
20997 with this objfile, so we don't need to
20998 duplicate it for the type. */
7d93a1e0 20999 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21000 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21001 }
63d06c5c
DC
21002 }
21003 }
c906108c
SS
21004 break;
21005 case DW_TAG_typedef:
f1e6e072 21006 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21007 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21008 list_to_add = cu->list_in_scope;
63d06c5c 21009 break;
c906108c 21010 case DW_TAG_base_type:
a02abb62 21011 case DW_TAG_subrange_type:
f1e6e072 21012 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21013 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21014 list_to_add = cu->list_in_scope;
c906108c
SS
21015 break;
21016 case DW_TAG_enumerator:
e142c38c 21017 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21018 if (attr != nullptr)
c906108c 21019 {
e7c27a73 21020 dwarf2_const_value (attr, sym, cu);
c906108c 21021 }
63d06c5c
DC
21022 {
21023 /* NOTE: carlton/2003-11-10: See comment above in the
21024 DW_TAG_class_type, etc. block. */
21025
804d2729 21026 list_to_add
c24bdb02 21027 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21028 && cu->language == language_cplus
c24bdb02 21029 ? cu->get_builder ()->get_global_symbols ()
804d2729 21030 : cu->list_in_scope);
63d06c5c 21031 }
c906108c 21032 break;
74921315 21033 case DW_TAG_imported_declaration:
5c4e30ca 21034 case DW_TAG_namespace:
f1e6e072 21035 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21036 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21037 break;
530e8392
KB
21038 case DW_TAG_module:
21039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21040 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21041 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21042 break;
4357ac6c 21043 case DW_TAG_common_block:
f1e6e072 21044 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21045 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21046 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21047 break;
c906108c
SS
21048 default:
21049 /* Not a tag we recognize. Hopefully we aren't processing
21050 trash data, but since we must specifically ignore things
21051 we don't recognize, there is nothing else we should do at
0963b4bd 21052 this point. */
b98664d3 21053 complaint (_("unsupported tag: '%s'"),
4d3c2250 21054 dwarf_tag_name (die->tag));
c906108c
SS
21055 break;
21056 }
df8a16a1 21057
e37fd15a
SW
21058 if (suppress_add)
21059 {
21060 sym->hash_next = objfile->template_symbols;
21061 objfile->template_symbols = sym;
21062 list_to_add = NULL;
21063 }
21064
21065 if (list_to_add != NULL)
d3cb6808 21066 add_symbol_to_list (sym, list_to_add);
e37fd15a 21067
df8a16a1
DJ
21068 /* For the benefit of old versions of GCC, check for anonymous
21069 namespaces based on the demangled name. */
4d4ec4e5 21070 if (!cu->processing_has_namespace_info
94af9270 21071 && cu->language == language_cplus)
c24bdb02 21072 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21073 }
21074 return (sym);
21075}
21076
98bfdba5
PA
21077/* Given an attr with a DW_FORM_dataN value in host byte order,
21078 zero-extend it as appropriate for the symbol's type. The DWARF
21079 standard (v4) is not entirely clear about the meaning of using
21080 DW_FORM_dataN for a constant with a signed type, where the type is
21081 wider than the data. The conclusion of a discussion on the DWARF
21082 list was that this is unspecified. We choose to always zero-extend
21083 because that is the interpretation long in use by GCC. */
c906108c 21084
98bfdba5 21085static gdb_byte *
ff39bb5e 21086dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21087 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21088{
5e22e966 21089 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21090 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21091 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21092 LONGEST l = DW_UNSND (attr);
21093
21094 if (bits < sizeof (*value) * 8)
21095 {
21096 l &= ((LONGEST) 1 << bits) - 1;
21097 *value = l;
21098 }
21099 else if (bits == sizeof (*value) * 8)
21100 *value = l;
21101 else
21102 {
224c3ddb 21103 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21104 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21105 return bytes;
21106 }
21107
21108 return NULL;
21109}
21110
21111/* Read a constant value from an attribute. Either set *VALUE, or if
21112 the value does not fit in *VALUE, set *BYTES - either already
21113 allocated on the objfile obstack, or newly allocated on OBSTACK,
21114 or, set *BATON, if we translated the constant to a location
21115 expression. */
21116
21117static void
ff39bb5e 21118dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21119 const char *name, struct obstack *obstack,
21120 struct dwarf2_cu *cu,
d521ce57 21121 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21122 struct dwarf2_locexpr_baton **baton)
21123{
5e22e966 21124 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21125 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21126 struct comp_unit_head *cu_header = &cu->header;
c906108c 21127 struct dwarf_block *blk;
98bfdba5
PA
21128 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21129 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21130
21131 *value = 0;
21132 *bytes = NULL;
21133 *baton = NULL;
c906108c
SS
21134
21135 switch (attr->form)
21136 {
21137 case DW_FORM_addr:
336d760d 21138 case DW_FORM_addrx:
3019eac3 21139 case DW_FORM_GNU_addr_index:
ac56253d 21140 {
ac56253d
TT
21141 gdb_byte *data;
21142
98bfdba5
PA
21143 if (TYPE_LENGTH (type) != cu_header->addr_size)
21144 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21145 cu_header->addr_size,
98bfdba5 21146 TYPE_LENGTH (type));
ac56253d
TT
21147 /* Symbols of this form are reasonably rare, so we just
21148 piggyback on the existing location code rather than writing
21149 a new implementation of symbol_computed_ops. */
8d749320 21150 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21151 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21152 (*baton)->per_cu = cu->per_cu;
21153 gdb_assert ((*baton)->per_cu);
ac56253d 21154
98bfdba5 21155 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21156 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21157 (*baton)->data = data;
ac56253d
TT
21158
21159 data[0] = DW_OP_addr;
21160 store_unsigned_integer (&data[1], cu_header->addr_size,
21161 byte_order, DW_ADDR (attr));
21162 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21163 }
c906108c 21164 break;
4ac36638 21165 case DW_FORM_string:
93b5768b 21166 case DW_FORM_strp:
cf532bd1 21167 case DW_FORM_strx:
3019eac3 21168 case DW_FORM_GNU_str_index:
36586728 21169 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21170 /* DW_STRING is already allocated on the objfile obstack, point
21171 directly to it. */
d521ce57 21172 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21173 break;
c906108c
SS
21174 case DW_FORM_block1:
21175 case DW_FORM_block2:
21176 case DW_FORM_block4:
21177 case DW_FORM_block:
2dc7f7b3 21178 case DW_FORM_exprloc:
0224619f 21179 case DW_FORM_data16:
c906108c 21180 blk = DW_BLOCK (attr);
98bfdba5
PA
21181 if (TYPE_LENGTH (type) != blk->size)
21182 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21183 TYPE_LENGTH (type));
21184 *bytes = blk->data;
c906108c 21185 break;
2df3850c
JM
21186
21187 /* The DW_AT_const_value attributes are supposed to carry the
21188 symbol's value "represented as it would be on the target
21189 architecture." By the time we get here, it's already been
21190 converted to host endianness, so we just need to sign- or
21191 zero-extend it as appropriate. */
21192 case DW_FORM_data1:
3aef2284 21193 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21194 break;
c906108c 21195 case DW_FORM_data2:
3aef2284 21196 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21197 break;
c906108c 21198 case DW_FORM_data4:
3aef2284 21199 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21200 break;
c906108c 21201 case DW_FORM_data8:
3aef2284 21202 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21203 break;
21204
c906108c 21205 case DW_FORM_sdata:
663c44ac 21206 case DW_FORM_implicit_const:
98bfdba5 21207 *value = DW_SND (attr);
2df3850c
JM
21208 break;
21209
c906108c 21210 case DW_FORM_udata:
98bfdba5 21211 *value = DW_UNSND (attr);
c906108c 21212 break;
2df3850c 21213
c906108c 21214 default:
b98664d3 21215 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21216 dwarf_form_name (attr->form));
98bfdba5 21217 *value = 0;
c906108c
SS
21218 break;
21219 }
21220}
21221
2df3850c 21222
98bfdba5
PA
21223/* Copy constant value from an attribute to a symbol. */
21224
2df3850c 21225static void
ff39bb5e 21226dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21227 struct dwarf2_cu *cu)
2df3850c 21228{
5e22e966 21229 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21230 LONGEST value;
d521ce57 21231 const gdb_byte *bytes;
98bfdba5 21232 struct dwarf2_locexpr_baton *baton;
2df3850c 21233
98bfdba5 21234 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21235 sym->print_name (),
98bfdba5
PA
21236 &objfile->objfile_obstack, cu,
21237 &value, &bytes, &baton);
2df3850c 21238
98bfdba5
PA
21239 if (baton != NULL)
21240 {
98bfdba5 21241 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21242 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21243 }
21244 else if (bytes != NULL)
21245 {
21246 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21247 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21248 }
21249 else
21250 {
21251 SYMBOL_VALUE (sym) = value;
f1e6e072 21252 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21253 }
2df3850c
JM
21254}
21255
c906108c
SS
21256/* Return the type of the die in question using its DW_AT_type attribute. */
21257
21258static struct type *
e7c27a73 21259die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21260{
c906108c 21261 struct attribute *type_attr;
c906108c 21262
e142c38c 21263 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21264 if (!type_attr)
21265 {
5e22e966 21266 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21267 /* A missing DW_AT_type represents a void type. */
518817b3 21268 return objfile_type (objfile)->builtin_void;
c906108c 21269 }
348e048f 21270
673bfd45 21271 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21272}
21273
b4ba55a1
JB
21274/* True iff CU's producer generates GNAT Ada auxiliary information
21275 that allows to find parallel types through that information instead
21276 of having to do expensive parallel lookups by type name. */
21277
21278static int
21279need_gnat_info (struct dwarf2_cu *cu)
21280{
de4cb04a
JB
21281 /* Assume that the Ada compiler was GNAT, which always produces
21282 the auxiliary information. */
21283 return (cu->language == language_ada);
b4ba55a1
JB
21284}
21285
b4ba55a1
JB
21286/* Return the auxiliary type of the die in question using its
21287 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21288 attribute is not present. */
21289
21290static struct type *
21291die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21292{
b4ba55a1 21293 struct attribute *type_attr;
b4ba55a1
JB
21294
21295 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21296 if (!type_attr)
21297 return NULL;
21298
673bfd45 21299 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21300}
21301
21302/* If DIE has a descriptive_type attribute, then set the TYPE's
21303 descriptive type accordingly. */
21304
21305static void
21306set_descriptive_type (struct type *type, struct die_info *die,
21307 struct dwarf2_cu *cu)
21308{
21309 struct type *descriptive_type = die_descriptive_type (die, cu);
21310
21311 if (descriptive_type)
21312 {
21313 ALLOCATE_GNAT_AUX_TYPE (type);
21314 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21315 }
21316}
21317
c906108c
SS
21318/* Return the containing type of the die in question using its
21319 DW_AT_containing_type attribute. */
21320
21321static struct type *
e7c27a73 21322die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21323{
c906108c 21324 struct attribute *type_attr;
5e22e966 21325 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21326
e142c38c 21327 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21328 if (!type_attr)
21329 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21330 "[in module %s]"), objfile_name (objfile));
33ac96f0 21331
673bfd45 21332 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21333}
21334
ac9ec31b
DE
21335/* Return an error marker type to use for the ill formed type in DIE/CU. */
21336
21337static struct type *
21338build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21339{
5e22e966 21340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21341 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21342 char *saved;
ac9ec31b 21343
528e1572
SM
21344 std::string message
21345 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21346 objfile_name (objfile),
21347 sect_offset_str (cu->header.sect_off),
21348 sect_offset_str (die->sect_off));
efba19b0 21349 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21350
19f392bc 21351 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21352}
21353
673bfd45 21354/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21355 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21356 DW_AT_containing_type.
673bfd45
DE
21357 If there is no type substitute an error marker. */
21358
c906108c 21359static struct type *
ff39bb5e 21360lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21361 struct dwarf2_cu *cu)
c906108c 21362{
5e22e966 21363 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21364 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21365 struct type *this_type;
21366
ac9ec31b
DE
21367 gdb_assert (attr->name == DW_AT_type
21368 || attr->name == DW_AT_GNAT_descriptive_type
21369 || attr->name == DW_AT_containing_type);
21370
673bfd45
DE
21371 /* First see if we have it cached. */
21372
36586728
TT
21373 if (attr->form == DW_FORM_GNU_ref_alt)
21374 {
21375 struct dwarf2_per_cu_data *per_cu;
0826b30a 21376 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21377
ed2dc618
SM
21378 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21379 dwarf2_per_objfile);
9c541725 21380 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21381 }
cd6c91b4 21382 else if (attr->form_is_ref ())
673bfd45 21383 {
0826b30a 21384 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21385
9c541725 21386 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21387 }
55f1336d 21388 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21389 {
ac9ec31b 21390 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21391
ac9ec31b 21392 return get_signatured_type (die, signature, cu);
673bfd45
DE
21393 }
21394 else
21395 {
b98664d3 21396 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21397 " at %s [in module %s]"),
21398 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21399 objfile_name (objfile));
ac9ec31b 21400 return build_error_marker_type (cu, die);
673bfd45
DE
21401 }
21402
21403 /* If not cached we need to read it in. */
21404
21405 if (this_type == NULL)
21406 {
ac9ec31b 21407 struct die_info *type_die = NULL;
673bfd45
DE
21408 struct dwarf2_cu *type_cu = cu;
21409
cd6c91b4 21410 if (attr->form_is_ref ())
ac9ec31b
DE
21411 type_die = follow_die_ref (die, attr, &type_cu);
21412 if (type_die == NULL)
21413 return build_error_marker_type (cu, die);
21414 /* If we find the type now, it's probably because the type came
3019eac3
DE
21415 from an inter-CU reference and the type's CU got expanded before
21416 ours. */
ac9ec31b 21417 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21418 }
21419
21420 /* If we still don't have a type use an error marker. */
21421
21422 if (this_type == NULL)
ac9ec31b 21423 return build_error_marker_type (cu, die);
673bfd45 21424
f792889a 21425 return this_type;
c906108c
SS
21426}
21427
673bfd45
DE
21428/* Return the type in DIE, CU.
21429 Returns NULL for invalid types.
21430
02142a6c 21431 This first does a lookup in die_type_hash,
673bfd45
DE
21432 and only reads the die in if necessary.
21433
21434 NOTE: This can be called when reading in partial or full symbols. */
21435
f792889a 21436static struct type *
e7c27a73 21437read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21438{
f792889a
DJ
21439 struct type *this_type;
21440
21441 this_type = get_die_type (die, cu);
21442 if (this_type)
21443 return this_type;
21444
673bfd45
DE
21445 return read_type_die_1 (die, cu);
21446}
21447
21448/* Read the type in DIE, CU.
21449 Returns NULL for invalid types. */
21450
21451static struct type *
21452read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21453{
21454 struct type *this_type = NULL;
21455
c906108c
SS
21456 switch (die->tag)
21457 {
21458 case DW_TAG_class_type:
680b30c7 21459 case DW_TAG_interface_type:
c906108c
SS
21460 case DW_TAG_structure_type:
21461 case DW_TAG_union_type:
f792889a 21462 this_type = read_structure_type (die, cu);
c906108c
SS
21463 break;
21464 case DW_TAG_enumeration_type:
f792889a 21465 this_type = read_enumeration_type (die, cu);
c906108c
SS
21466 break;
21467 case DW_TAG_subprogram:
21468 case DW_TAG_subroutine_type:
edb3359d 21469 case DW_TAG_inlined_subroutine:
f792889a 21470 this_type = read_subroutine_type (die, cu);
c906108c
SS
21471 break;
21472 case DW_TAG_array_type:
f792889a 21473 this_type = read_array_type (die, cu);
c906108c 21474 break;
72019c9c 21475 case DW_TAG_set_type:
f792889a 21476 this_type = read_set_type (die, cu);
72019c9c 21477 break;
c906108c 21478 case DW_TAG_pointer_type:
f792889a 21479 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21480 break;
21481 case DW_TAG_ptr_to_member_type:
f792889a 21482 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21483 break;
21484 case DW_TAG_reference_type:
4297a3f0
AV
21485 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21486 break;
21487 case DW_TAG_rvalue_reference_type:
21488 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21489 break;
21490 case DW_TAG_const_type:
f792889a 21491 this_type = read_tag_const_type (die, cu);
c906108c
SS
21492 break;
21493 case DW_TAG_volatile_type:
f792889a 21494 this_type = read_tag_volatile_type (die, cu);
c906108c 21495 break;
06d66ee9
TT
21496 case DW_TAG_restrict_type:
21497 this_type = read_tag_restrict_type (die, cu);
21498 break;
c906108c 21499 case DW_TAG_string_type:
f792889a 21500 this_type = read_tag_string_type (die, cu);
c906108c
SS
21501 break;
21502 case DW_TAG_typedef:
f792889a 21503 this_type = read_typedef (die, cu);
c906108c 21504 break;
a02abb62 21505 case DW_TAG_subrange_type:
f792889a 21506 this_type = read_subrange_type (die, cu);
a02abb62 21507 break;
c906108c 21508 case DW_TAG_base_type:
f792889a 21509 this_type = read_base_type (die, cu);
c906108c 21510 break;
81a17f79 21511 case DW_TAG_unspecified_type:
f792889a 21512 this_type = read_unspecified_type (die, cu);
81a17f79 21513 break;
0114d602
DJ
21514 case DW_TAG_namespace:
21515 this_type = read_namespace_type (die, cu);
21516 break;
f55ee35c
JK
21517 case DW_TAG_module:
21518 this_type = read_module_type (die, cu);
21519 break;
a2c2acaf
MW
21520 case DW_TAG_atomic_type:
21521 this_type = read_tag_atomic_type (die, cu);
21522 break;
c906108c 21523 default:
b98664d3 21524 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21525 dwarf_tag_name (die->tag));
c906108c
SS
21526 break;
21527 }
63d06c5c 21528
f792889a 21529 return this_type;
63d06c5c
DC
21530}
21531
abc72ce4
DE
21532/* See if we can figure out if the class lives in a namespace. We do
21533 this by looking for a member function; its demangled name will
21534 contain namespace info, if there is any.
21535 Return the computed name or NULL.
21536 Space for the result is allocated on the objfile's obstack.
21537 This is the full-die version of guess_partial_die_structure_name.
21538 In this case we know DIE has no useful parent. */
21539
43816ebc 21540static const char *
abc72ce4
DE
21541guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21542{
21543 struct die_info *spec_die;
21544 struct dwarf2_cu *spec_cu;
21545 struct die_info *child;
5e22e966 21546 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21547
21548 spec_cu = cu;
21549 spec_die = die_specification (die, &spec_cu);
21550 if (spec_die != NULL)
21551 {
21552 die = spec_die;
21553 cu = spec_cu;
21554 }
21555
21556 for (child = die->child;
21557 child != NULL;
21558 child = child->sibling)
21559 {
21560 if (child->tag == DW_TAG_subprogram)
21561 {
73b9be8b 21562 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21563
7d45c7c3 21564 if (linkage_name != NULL)
abc72ce4 21565 {
43816ebc
TT
21566 gdb::unique_xmalloc_ptr<char> actual_name
21567 (language_class_name_from_physname (cu->language_defn,
21568 linkage_name));
21569 const char *name = NULL;
abc72ce4
DE
21570
21571 if (actual_name != NULL)
21572 {
15d034d0 21573 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21574
21575 if (die_name != NULL
43816ebc 21576 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21577 {
21578 /* Strip off the class name from the full name.
21579 We want the prefix. */
21580 int die_name_len = strlen (die_name);
43816ebc
TT
21581 int actual_name_len = strlen (actual_name.get ());
21582 const char *ptr = actual_name.get ();
abc72ce4
DE
21583
21584 /* Test for '::' as a sanity check. */
21585 if (actual_name_len > die_name_len + 2
43816ebc 21586 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21587 name = obstack_strndup (
e3b94546 21588 &objfile->per_bfd->storage_obstack,
43816ebc 21589 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21590 }
21591 }
abc72ce4
DE
21592 return name;
21593 }
21594 }
21595 }
21596
21597 return NULL;
21598}
21599
96408a79
SA
21600/* GCC might emit a nameless typedef that has a linkage name. Determine the
21601 prefix part in such case. See
21602 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21603
a121b7c1 21604static const char *
96408a79
SA
21605anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21606{
21607 struct attribute *attr;
e6a959d6 21608 const char *base;
96408a79
SA
21609
21610 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21611 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21612 return NULL;
21613
7d45c7c3 21614 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21615 return NULL;
21616
73b9be8b 21617 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21618 if (attr == NULL || DW_STRING (attr) == NULL)
21619 return NULL;
21620
21621 /* dwarf2_name had to be already called. */
21622 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21623
21624 /* Strip the base name, keep any leading namespaces/classes. */
21625 base = strrchr (DW_STRING (attr), ':');
21626 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21627 return "";
21628
5e22e966 21629 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21630 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21631 DW_STRING (attr),
21632 &base[-1] - DW_STRING (attr));
96408a79
SA
21633}
21634
fdde2d81 21635/* Return the name of the namespace/class that DIE is defined within,
0114d602 21636 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21637
0114d602
DJ
21638 For example, if we're within the method foo() in the following
21639 code:
21640
21641 namespace N {
21642 class C {
21643 void foo () {
21644 }
21645 };
21646 }
21647
21648 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21649
0d5cff50 21650static const char *
e142c38c 21651determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21652{
5e22e966 21653 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21654 struct die_info *parent, *spec_die;
21655 struct dwarf2_cu *spec_cu;
21656 struct type *parent_type;
a121b7c1 21657 const char *retval;
63d06c5c 21658
9c37b5ae 21659 if (cu->language != language_cplus
c44af4eb
TT
21660 && cu->language != language_fortran && cu->language != language_d
21661 && cu->language != language_rust)
0114d602
DJ
21662 return "";
21663
96408a79
SA
21664 retval = anonymous_struct_prefix (die, cu);
21665 if (retval)
21666 return retval;
21667
0114d602
DJ
21668 /* We have to be careful in the presence of DW_AT_specification.
21669 For example, with GCC 3.4, given the code
21670
21671 namespace N {
21672 void foo() {
21673 // Definition of N::foo.
21674 }
21675 }
21676
21677 then we'll have a tree of DIEs like this:
21678
21679 1: DW_TAG_compile_unit
21680 2: DW_TAG_namespace // N
21681 3: DW_TAG_subprogram // declaration of N::foo
21682 4: DW_TAG_subprogram // definition of N::foo
21683 DW_AT_specification // refers to die #3
21684
21685 Thus, when processing die #4, we have to pretend that we're in
21686 the context of its DW_AT_specification, namely the contex of die
21687 #3. */
21688 spec_cu = cu;
21689 spec_die = die_specification (die, &spec_cu);
21690 if (spec_die == NULL)
21691 parent = die->parent;
21692 else
63d06c5c 21693 {
0114d602
DJ
21694 parent = spec_die->parent;
21695 cu = spec_cu;
63d06c5c 21696 }
0114d602
DJ
21697
21698 if (parent == NULL)
21699 return "";
98bfdba5
PA
21700 else if (parent->building_fullname)
21701 {
21702 const char *name;
21703 const char *parent_name;
21704
21705 /* It has been seen on RealView 2.2 built binaries,
21706 DW_TAG_template_type_param types actually _defined_ as
21707 children of the parent class:
21708
21709 enum E {};
21710 template class <class Enum> Class{};
21711 Class<enum E> class_e;
21712
21713 1: DW_TAG_class_type (Class)
21714 2: DW_TAG_enumeration_type (E)
21715 3: DW_TAG_enumerator (enum1:0)
21716 3: DW_TAG_enumerator (enum2:1)
21717 ...
21718 2: DW_TAG_template_type_param
21719 DW_AT_type DW_FORM_ref_udata (E)
21720
21721 Besides being broken debug info, it can put GDB into an
21722 infinite loop. Consider:
21723
21724 When we're building the full name for Class<E>, we'll start
21725 at Class, and go look over its template type parameters,
21726 finding E. We'll then try to build the full name of E, and
21727 reach here. We're now trying to build the full name of E,
21728 and look over the parent DIE for containing scope. In the
21729 broken case, if we followed the parent DIE of E, we'd again
21730 find Class, and once again go look at its template type
21731 arguments, etc., etc. Simply don't consider such parent die
21732 as source-level parent of this die (it can't be, the language
21733 doesn't allow it), and break the loop here. */
21734 name = dwarf2_name (die, cu);
21735 parent_name = dwarf2_name (parent, cu);
b98664d3 21736 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21737 name ? name : "<unknown>",
21738 parent_name ? parent_name : "<unknown>");
21739 return "";
21740 }
63d06c5c 21741 else
0114d602
DJ
21742 switch (parent->tag)
21743 {
63d06c5c 21744 case DW_TAG_namespace:
0114d602 21745 parent_type = read_type_die (parent, cu);
acebe513
UW
21746 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21747 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21748 Work around this problem here. */
21749 if (cu->language == language_cplus
7d93a1e0 21750 && strcmp (parent_type->name (), "::") == 0)
acebe513 21751 return "";
0114d602 21752 /* We give a name to even anonymous namespaces. */
7d93a1e0 21753 return parent_type->name ();
63d06c5c 21754 case DW_TAG_class_type:
680b30c7 21755 case DW_TAG_interface_type:
63d06c5c 21756 case DW_TAG_structure_type:
0114d602 21757 case DW_TAG_union_type:
f55ee35c 21758 case DW_TAG_module:
0114d602 21759 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21760 if (parent_type->name () != NULL)
21761 return parent_type->name ();
0114d602
DJ
21762 else
21763 /* An anonymous structure is only allowed non-static data
21764 members; no typedefs, no member functions, et cetera.
21765 So it does not need a prefix. */
21766 return "";
abc72ce4 21767 case DW_TAG_compile_unit:
95554aad 21768 case DW_TAG_partial_unit:
abc72ce4
DE
21769 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21770 if (cu->language == language_cplus
5989a64e 21771 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21772 && die->child != NULL
21773 && (die->tag == DW_TAG_class_type
21774 || die->tag == DW_TAG_structure_type
21775 || die->tag == DW_TAG_union_type))
21776 {
43816ebc 21777 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21778 if (name != NULL)
21779 return name;
21780 }
21781 return "";
0a4b0913
AB
21782 case DW_TAG_subprogram:
21783 /* Nested subroutines in Fortran get a prefix with the name
21784 of the parent's subroutine. */
21785 if (cu->language == language_fortran)
21786 {
21787 if ((die->tag == DW_TAG_subprogram)
21788 && (dwarf2_name (parent, cu) != NULL))
21789 return dwarf2_name (parent, cu);
21790 }
21791 return determine_prefix (parent, cu);
3d567982
TT
21792 case DW_TAG_enumeration_type:
21793 parent_type = read_type_die (parent, cu);
21794 if (TYPE_DECLARED_CLASS (parent_type))
21795 {
7d93a1e0
SM
21796 if (parent_type->name () != NULL)
21797 return parent_type->name ();
3d567982
TT
21798 return "";
21799 }
21800 /* Fall through. */
63d06c5c 21801 default:
8176b9b8 21802 return determine_prefix (parent, cu);
63d06c5c 21803 }
63d06c5c
DC
21804}
21805
3e43a32a
MS
21806/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21807 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21808 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21809 an obconcat, otherwise allocate storage for the result. The CU argument is
21810 used to determine the language and hence, the appropriate separator. */
987504bb 21811
f55ee35c 21812#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21813
21814static char *
f55ee35c
JK
21815typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21816 int physname, struct dwarf2_cu *cu)
63d06c5c 21817{
f55ee35c 21818 const char *lead = "";
5c315b68 21819 const char *sep;
63d06c5c 21820
3e43a32a
MS
21821 if (suffix == NULL || suffix[0] == '\0'
21822 || prefix == NULL || prefix[0] == '\0')
987504bb 21823 sep = "";
45280282
IB
21824 else if (cu->language == language_d)
21825 {
21826 /* For D, the 'main' function could be defined in any module, but it
21827 should never be prefixed. */
21828 if (strcmp (suffix, "D main") == 0)
21829 {
21830 prefix = "";
21831 sep = "";
21832 }
21833 else
21834 sep = ".";
21835 }
f55ee35c
JK
21836 else if (cu->language == language_fortran && physname)
21837 {
21838 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21839 DW_AT_MIPS_linkage_name is preferred and used instead. */
21840
21841 lead = "__";
21842 sep = "_MOD_";
21843 }
987504bb
JJ
21844 else
21845 sep = "::";
63d06c5c 21846
6dd47d34
DE
21847 if (prefix == NULL)
21848 prefix = "";
21849 if (suffix == NULL)
21850 suffix = "";
21851
987504bb
JJ
21852 if (obs == NULL)
21853 {
3e43a32a 21854 char *retval
224c3ddb
SM
21855 = ((char *)
21856 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21857
f55ee35c
JK
21858 strcpy (retval, lead);
21859 strcat (retval, prefix);
6dd47d34
DE
21860 strcat (retval, sep);
21861 strcat (retval, suffix);
63d06c5c
DC
21862 return retval;
21863 }
987504bb
JJ
21864 else
21865 {
21866 /* We have an obstack. */
f55ee35c 21867 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21868 }
63d06c5c
DC
21869}
21870
71c25dea
TT
21871/* Get name of a die, return NULL if not found. */
21872
15d034d0
TT
21873static const char *
21874dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21875 struct objfile *objfile)
71c25dea
TT
21876{
21877 if (name && cu->language == language_cplus)
21878 {
596dc4ad
TT
21879 gdb::unique_xmalloc_ptr<char> canon_name
21880 = cp_canonicalize_string (name);
71c25dea 21881
596dc4ad
TT
21882 if (canon_name != nullptr)
21883 name = objfile->intern (canon_name.get ());
71c25dea
TT
21884 }
21885
21886 return name;
c906108c
SS
21887}
21888
96553a0c
DE
21889/* Get name of a die, return NULL if not found.
21890 Anonymous namespaces are converted to their magic string. */
9219021c 21891
15d034d0 21892static const char *
e142c38c 21893dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21894{
21895 struct attribute *attr;
5e22e966 21896 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21897
e142c38c 21898 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21899 if ((!attr || !DW_STRING (attr))
96553a0c 21900 && die->tag != DW_TAG_namespace
53832f31
TT
21901 && die->tag != DW_TAG_class_type
21902 && die->tag != DW_TAG_interface_type
21903 && die->tag != DW_TAG_structure_type
21904 && die->tag != DW_TAG_union_type)
71c25dea
TT
21905 return NULL;
21906
21907 switch (die->tag)
21908 {
21909 case DW_TAG_compile_unit:
95554aad 21910 case DW_TAG_partial_unit:
71c25dea
TT
21911 /* Compilation units have a DW_AT_name that is a filename, not
21912 a source language identifier. */
21913 case DW_TAG_enumeration_type:
21914 case DW_TAG_enumerator:
21915 /* These tags always have simple identifiers already; no need
21916 to canonicalize them. */
21917 return DW_STRING (attr);
907af001 21918
96553a0c
DE
21919 case DW_TAG_namespace:
21920 if (attr != NULL && DW_STRING (attr) != NULL)
21921 return DW_STRING (attr);
21922 return CP_ANONYMOUS_NAMESPACE_STR;
21923
907af001
UW
21924 case DW_TAG_class_type:
21925 case DW_TAG_interface_type:
21926 case DW_TAG_structure_type:
21927 case DW_TAG_union_type:
21928 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21929 structures or unions. These were of the form "._%d" in GCC 4.1,
21930 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21931 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21932 if (attr && DW_STRING (attr)
61012eef
GB
21933 && (startswith (DW_STRING (attr), "._")
21934 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21935 return NULL;
53832f31
TT
21936
21937 /* GCC might emit a nameless typedef that has a linkage name. See
21938 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21939 if (!attr || DW_STRING (attr) == NULL)
21940 {
73b9be8b 21941 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21942 if (attr == NULL || DW_STRING (attr) == NULL)
21943 return NULL;
21944
df5c6c50
JK
21945 /* Avoid demangling DW_STRING (attr) the second time on a second
21946 call for the same DIE. */
21947 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21948 {
43816ebc
TT
21949 gdb::unique_xmalloc_ptr<char> demangled
21950 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21951 if (demangled == nullptr)
21952 return nullptr;
43816ebc 21953
be1e3d3e 21954 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21955 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21956 }
67430cd0
TT
21957
21958 /* Strip any leading namespaces/classes, keep only the base name.
21959 DW_AT_name for named DIEs does not contain the prefixes. */
21960 const char *base = strrchr (DW_STRING (attr), ':');
21961 if (base && base > DW_STRING (attr) && base[-1] == ':')
21962 return &base[1];
21963 else
21964 return DW_STRING (attr);
53832f31 21965 }
907af001
UW
21966 break;
21967
71c25dea 21968 default:
907af001
UW
21969 break;
21970 }
21971
21972 if (!DW_STRING_IS_CANONICAL (attr))
21973 {
be1e3d3e
TT
21974 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21975 objfile);
907af001 21976 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21977 }
907af001 21978 return DW_STRING (attr);
9219021c
DC
21979}
21980
21981/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21982 is none. *EXT_CU is the CU containing DIE on input, and the CU
21983 containing the return value on output. */
9219021c
DC
21984
21985static struct die_info *
f2f0e013 21986dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21987{
21988 struct attribute *attr;
9219021c 21989
f2f0e013 21990 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21991 if (attr == NULL)
21992 return NULL;
21993
f2f0e013 21994 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21995}
21996
f9aca02d 21997static void
d97bc12b 21998dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21999{
22000 unsigned int i;
22001
d97bc12b 22002 print_spaces (indent, f);
9d8780f0 22003 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22004 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22005 sect_offset_str (die->sect_off));
d97bc12b
DE
22006
22007 if (die->parent != NULL)
22008 {
22009 print_spaces (indent, f);
9d8780f0
SM
22010 fprintf_unfiltered (f, " parent at offset: %s\n",
22011 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22012 }
22013
22014 print_spaces (indent, f);
22015 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22016 dwarf_bool_name (die->child != NULL));
c906108c 22017
d97bc12b
DE
22018 print_spaces (indent, f);
22019 fprintf_unfiltered (f, " attributes:\n");
22020
c906108c
SS
22021 for (i = 0; i < die->num_attrs; ++i)
22022 {
d97bc12b
DE
22023 print_spaces (indent, f);
22024 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22025 dwarf_attr_name (die->attrs[i].name),
22026 dwarf_form_name (die->attrs[i].form));
d97bc12b 22027
c906108c
SS
22028 switch (die->attrs[i].form)
22029 {
c906108c 22030 case DW_FORM_addr:
336d760d 22031 case DW_FORM_addrx:
3019eac3 22032 case DW_FORM_GNU_addr_index:
d97bc12b 22033 fprintf_unfiltered (f, "address: ");
5af949e3 22034 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22035 break;
22036 case DW_FORM_block2:
22037 case DW_FORM_block4:
22038 case DW_FORM_block:
22039 case DW_FORM_block1:
56eb65bd
SP
22040 fprintf_unfiltered (f, "block: size %s",
22041 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22042 break;
2dc7f7b3 22043 case DW_FORM_exprloc:
56eb65bd
SP
22044 fprintf_unfiltered (f, "expression: size %s",
22045 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22046 break;
0224619f
JK
22047 case DW_FORM_data16:
22048 fprintf_unfiltered (f, "constant of 16 bytes");
22049 break;
4568ecf9
DE
22050 case DW_FORM_ref_addr:
22051 fprintf_unfiltered (f, "ref address: ");
22052 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22053 break;
36586728
TT
22054 case DW_FORM_GNU_ref_alt:
22055 fprintf_unfiltered (f, "alt ref address: ");
22056 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22057 break;
10b3939b
DJ
22058 case DW_FORM_ref1:
22059 case DW_FORM_ref2:
22060 case DW_FORM_ref4:
4568ecf9
DE
22061 case DW_FORM_ref8:
22062 case DW_FORM_ref_udata:
d97bc12b 22063 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22064 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22065 break;
c906108c
SS
22066 case DW_FORM_data1:
22067 case DW_FORM_data2:
22068 case DW_FORM_data4:
ce5d95e1 22069 case DW_FORM_data8:
c906108c
SS
22070 case DW_FORM_udata:
22071 case DW_FORM_sdata:
43bbcdc2
PH
22072 fprintf_unfiltered (f, "constant: %s",
22073 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22074 break;
2dc7f7b3
TT
22075 case DW_FORM_sec_offset:
22076 fprintf_unfiltered (f, "section offset: %s",
22077 pulongest (DW_UNSND (&die->attrs[i])));
22078 break;
55f1336d 22079 case DW_FORM_ref_sig8:
ac9ec31b
DE
22080 fprintf_unfiltered (f, "signature: %s",
22081 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22082 break;
c906108c 22083 case DW_FORM_string:
4bdf3d34 22084 case DW_FORM_strp:
43988095 22085 case DW_FORM_line_strp:
cf532bd1 22086 case DW_FORM_strx:
3019eac3 22087 case DW_FORM_GNU_str_index:
36586728 22088 case DW_FORM_GNU_strp_alt:
8285870a 22089 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22090 DW_STRING (&die->attrs[i])
8285870a
JK
22091 ? DW_STRING (&die->attrs[i]) : "",
22092 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22093 break;
22094 case DW_FORM_flag:
22095 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22096 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22097 else
d97bc12b 22098 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22099 break;
2dc7f7b3
TT
22100 case DW_FORM_flag_present:
22101 fprintf_unfiltered (f, "flag: TRUE");
22102 break;
a8329558 22103 case DW_FORM_indirect:
0963b4bd
MS
22104 /* The reader will have reduced the indirect form to
22105 the "base form" so this form should not occur. */
5f48f8f3 22106 fprintf_unfiltered (f,
3e43a32a 22107 "unexpected attribute form: DW_FORM_indirect");
a8329558 22108 break;
663c44ac
JK
22109 case DW_FORM_implicit_const:
22110 fprintf_unfiltered (f, "constant: %s",
22111 plongest (DW_SND (&die->attrs[i])));
22112 break;
c906108c 22113 default:
d97bc12b 22114 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22115 die->attrs[i].form);
d97bc12b 22116 break;
c906108c 22117 }
d97bc12b 22118 fprintf_unfiltered (f, "\n");
c906108c
SS
22119 }
22120}
22121
f9aca02d 22122static void
d97bc12b 22123dump_die_for_error (struct die_info *die)
c906108c 22124{
d97bc12b
DE
22125 dump_die_shallow (gdb_stderr, 0, die);
22126}
22127
22128static void
22129dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22130{
22131 int indent = level * 4;
22132
22133 gdb_assert (die != NULL);
22134
22135 if (level >= max_level)
22136 return;
22137
22138 dump_die_shallow (f, indent, die);
22139
22140 if (die->child != NULL)
c906108c 22141 {
d97bc12b
DE
22142 print_spaces (indent, f);
22143 fprintf_unfiltered (f, " Children:");
22144 if (level + 1 < max_level)
22145 {
22146 fprintf_unfiltered (f, "\n");
22147 dump_die_1 (f, level + 1, max_level, die->child);
22148 }
22149 else
22150 {
3e43a32a
MS
22151 fprintf_unfiltered (f,
22152 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22153 }
22154 }
22155
22156 if (die->sibling != NULL && level > 0)
22157 {
22158 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22159 }
22160}
22161
d97bc12b
DE
22162/* This is called from the pdie macro in gdbinit.in.
22163 It's not static so gcc will keep a copy callable from gdb. */
22164
22165void
22166dump_die (struct die_info *die, int max_level)
22167{
22168 dump_die_1 (gdb_stdlog, 0, max_level, die);
22169}
22170
f9aca02d 22171static void
51545339 22172store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22173{
51545339 22174 void **slot;
c906108c 22175
9c541725
PA
22176 slot = htab_find_slot_with_hash (cu->die_hash, die,
22177 to_underlying (die->sect_off),
b64f50a1 22178 INSERT);
51545339
DJ
22179
22180 *slot = die;
c906108c
SS
22181}
22182
348e048f
DE
22183/* Follow reference or signature attribute ATTR of SRC_DIE.
22184 On entry *REF_CU is the CU of SRC_DIE.
22185 On exit *REF_CU is the CU of the result. */
22186
22187static struct die_info *
ff39bb5e 22188follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22189 struct dwarf2_cu **ref_cu)
22190{
22191 struct die_info *die;
22192
cd6c91b4 22193 if (attr->form_is_ref ())
348e048f 22194 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22195 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22196 die = follow_die_sig (src_die, attr, ref_cu);
22197 else
22198 {
22199 dump_die_for_error (src_die);
22200 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22201 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22202 }
22203
22204 return die;
03dd20cc
DJ
22205}
22206
5c631832 22207/* Follow reference OFFSET.
673bfd45
DE
22208 On entry *REF_CU is the CU of the source die referencing OFFSET.
22209 On exit *REF_CU is the CU of the result.
22210 Returns NULL if OFFSET is invalid. */
f504f079 22211
f9aca02d 22212static struct die_info *
9c541725 22213follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22214 struct dwarf2_cu **ref_cu)
c906108c 22215{
10b3939b 22216 struct die_info temp_die;
f2f0e013 22217 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22218 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22219
348e048f
DE
22220 gdb_assert (cu->per_cu != NULL);
22221
98bfdba5
PA
22222 target_cu = cu;
22223
3019eac3 22224 if (cu->per_cu->is_debug_types)
348e048f
DE
22225 {
22226 /* .debug_types CUs cannot reference anything outside their CU.
22227 If they need to, they have to reference a signatured type via
55f1336d 22228 DW_FORM_ref_sig8. */
4057dfde 22229 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22230 return NULL;
348e048f 22231 }
36586728 22232 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22233 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22234 {
22235 struct dwarf2_per_cu_data *per_cu;
9a619af0 22236
9c541725 22237 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22238 dwarf2_per_objfile);
03dd20cc
DJ
22239
22240 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22241 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
ab432490 22242 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22243
10b3939b
DJ
22244 target_cu = per_cu->cu;
22245 }
98bfdba5
PA
22246 else if (cu->dies == NULL)
22247 {
22248 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22249 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22250 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22251 language_minimal);
98bfdba5 22252 }
c906108c 22253
f2f0e013 22254 *ref_cu = target_cu;
9c541725 22255 temp_die.sect_off = sect_off;
c24bdb02
KS
22256
22257 if (target_cu != cu)
22258 target_cu->ancestor = cu;
22259
9a3c8263 22260 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22261 &temp_die,
22262 to_underlying (sect_off));
5c631832 22263}
10b3939b 22264
5c631832
JK
22265/* Follow reference attribute ATTR of SRC_DIE.
22266 On entry *REF_CU is the CU of SRC_DIE.
22267 On exit *REF_CU is the CU of the result. */
22268
22269static struct die_info *
ff39bb5e 22270follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22271 struct dwarf2_cu **ref_cu)
22272{
0826b30a 22273 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22274 struct dwarf2_cu *cu = *ref_cu;
22275 struct die_info *die;
22276
9c541725 22277 die = follow_die_offset (sect_off,
36586728
TT
22278 (attr->form == DW_FORM_GNU_ref_alt
22279 || cu->per_cu->is_dwz),
22280 ref_cu);
5c631832 22281 if (!die)
9d8780f0
SM
22282 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22283 "at %s [in module %s]"),
22284 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22285 objfile_name (cu->per_objfile->objfile));
348e048f 22286
5c631832
JK
22287 return die;
22288}
22289
d4c9a4f8 22290/* See read.h. */
5c631832
JK
22291
22292struct dwarf2_locexpr_baton
9c541725 22293dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22294 dwarf2_per_cu_data *per_cu,
8b9737bf 22295 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22296 void *baton, bool resolve_abstract_p)
5c631832 22297{
918dd910 22298 struct dwarf2_cu *cu;
5c631832
JK
22299 struct die_info *die;
22300 struct attribute *attr;
22301 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22302 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22303 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22304
918dd910 22305 if (per_cu->cu == NULL)
ab432490 22306 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22307 cu = per_cu->cu;
cc12ce38
DE
22308 if (cu == NULL)
22309 {
22310 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22311 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22312 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22313 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22314 }
918dd910 22315
9c541725 22316 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22317 if (!die)
9d8780f0
SM
22318 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22319 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22320
22321 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22322 if (!attr && resolve_abstract_p
5989a64e
SM
22323 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22324 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22325 {
22326 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22327 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22328 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22329
3360b6e7 22330 for (const auto &cand_off
5989a64e 22331 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22332 {
3360b6e7
TV
22333 struct dwarf2_cu *cand_cu = cu;
22334 struct die_info *cand
22335 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22336 if (!cand
22337 || !cand->parent
e4a62c65
TV
22338 || cand->parent->tag != DW_TAG_subprogram)
22339 continue;
22340
22341 CORE_ADDR pc_low, pc_high;
22342 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22343 if (pc_low == ((CORE_ADDR) -1))
22344 continue;
22345 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22346 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22347 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22348 continue;
22349
22350 die = cand;
22351 attr = dwarf2_attr (die, DW_AT_location, cu);
22352 break;
22353 }
22354 }
22355
5c631832
JK
22356 if (!attr)
22357 {
e103e986
JK
22358 /* DWARF: "If there is no such attribute, then there is no effect.".
22359 DATA is ignored if SIZE is 0. */
5c631832 22360
e103e986 22361 retval.data = NULL;
5c631832
JK
22362 retval.size = 0;
22363 }
cd6c91b4 22364 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22365 {
22366 struct dwarf2_loclist_baton loclist_baton;
22367 CORE_ADDR pc = (*get_frame_pc) (baton);
22368 size_t size;
22369
22370 fill_in_loclist_baton (cu, &loclist_baton, attr);
22371
22372 retval.data = dwarf2_find_location_expression (&loclist_baton,
22373 &size, pc);
22374 retval.size = size;
22375 }
5c631832
JK
22376 else
22377 {
4fc6c0d5 22378 if (!attr->form_is_block ())
9d8780f0 22379 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22380 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22381 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22382
22383 retval.data = DW_BLOCK (attr)->data;
22384 retval.size = DW_BLOCK (attr)->size;
22385 }
a50264ba 22386 retval.per_objfile = dwarf2_per_objfile;
5c631832 22387 retval.per_cu = cu->per_cu;
918dd910 22388
ed2dc618 22389 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22390
5c631832 22391 return retval;
348e048f
DE
22392}
22393
d4c9a4f8 22394/* See read.h. */
8b9737bf
TT
22395
22396struct dwarf2_locexpr_baton
22397dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22398 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22399 CORE_ADDR (*get_frame_pc) (void *baton),
22400 void *baton)
22401{
9c541725 22402 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22403
9c541725 22404 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22405}
22406
b6807d98
TT
22407/* Write a constant of a given type as target-ordered bytes into
22408 OBSTACK. */
22409
22410static const gdb_byte *
22411write_constant_as_bytes (struct obstack *obstack,
22412 enum bfd_endian byte_order,
22413 struct type *type,
22414 ULONGEST value,
22415 LONGEST *len)
22416{
22417 gdb_byte *result;
22418
22419 *len = TYPE_LENGTH (type);
224c3ddb 22420 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22421 store_unsigned_integer (result, *len, byte_order, value);
22422
22423 return result;
22424}
22425
d4c9a4f8 22426/* See read.h. */
b6807d98
TT
22427
22428const gdb_byte *
9c541725 22429dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22430 dwarf2_per_cu_data *per_cu,
22431 obstack *obstack,
b6807d98
TT
22432 LONGEST *len)
22433{
22434 struct dwarf2_cu *cu;
22435 struct die_info *die;
22436 struct attribute *attr;
22437 const gdb_byte *result = NULL;
22438 struct type *type;
22439 LONGEST value;
22440 enum bfd_endian byte_order;
e3b94546 22441 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22442
b6807d98 22443 if (per_cu->cu == NULL)
ab432490 22444 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
b6807d98 22445 cu = per_cu->cu;
cc12ce38
DE
22446 if (cu == NULL)
22447 {
22448 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22449 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22450 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22451 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22452 }
b6807d98 22453
9c541725 22454 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22455 if (!die)
9d8780f0
SM
22456 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22457 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22458
22459 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22460 if (attr == NULL)
22461 return NULL;
22462
e3b94546 22463 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22464 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22465
22466 switch (attr->form)
22467 {
22468 case DW_FORM_addr:
336d760d 22469 case DW_FORM_addrx:
b6807d98
TT
22470 case DW_FORM_GNU_addr_index:
22471 {
22472 gdb_byte *tem;
22473
22474 *len = cu->header.addr_size;
224c3ddb 22475 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22476 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22477 result = tem;
22478 }
22479 break;
22480 case DW_FORM_string:
22481 case DW_FORM_strp:
cf532bd1 22482 case DW_FORM_strx:
b6807d98
TT
22483 case DW_FORM_GNU_str_index:
22484 case DW_FORM_GNU_strp_alt:
22485 /* DW_STRING is already allocated on the objfile obstack, point
22486 directly to it. */
22487 result = (const gdb_byte *) DW_STRING (attr);
22488 *len = strlen (DW_STRING (attr));
22489 break;
22490 case DW_FORM_block1:
22491 case DW_FORM_block2:
22492 case DW_FORM_block4:
22493 case DW_FORM_block:
22494 case DW_FORM_exprloc:
0224619f 22495 case DW_FORM_data16:
b6807d98
TT
22496 result = DW_BLOCK (attr)->data;
22497 *len = DW_BLOCK (attr)->size;
22498 break;
22499
22500 /* The DW_AT_const_value attributes are supposed to carry the
22501 symbol's value "represented as it would be on the target
22502 architecture." By the time we get here, it's already been
22503 converted to host endianness, so we just need to sign- or
22504 zero-extend it as appropriate. */
22505 case DW_FORM_data1:
22506 type = die_type (die, cu);
22507 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22508 if (result == NULL)
22509 result = write_constant_as_bytes (obstack, byte_order,
22510 type, value, len);
22511 break;
22512 case DW_FORM_data2:
22513 type = die_type (die, cu);
22514 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22515 if (result == NULL)
22516 result = write_constant_as_bytes (obstack, byte_order,
22517 type, value, len);
22518 break;
22519 case DW_FORM_data4:
22520 type = die_type (die, cu);
22521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22522 if (result == NULL)
22523 result = write_constant_as_bytes (obstack, byte_order,
22524 type, value, len);
22525 break;
22526 case DW_FORM_data8:
22527 type = die_type (die, cu);
22528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22529 if (result == NULL)
22530 result = write_constant_as_bytes (obstack, byte_order,
22531 type, value, len);
22532 break;
22533
22534 case DW_FORM_sdata:
663c44ac 22535 case DW_FORM_implicit_const:
b6807d98
TT
22536 type = die_type (die, cu);
22537 result = write_constant_as_bytes (obstack, byte_order,
22538 type, DW_SND (attr), len);
22539 break;
22540
22541 case DW_FORM_udata:
22542 type = die_type (die, cu);
22543 result = write_constant_as_bytes (obstack, byte_order,
22544 type, DW_UNSND (attr), len);
22545 break;
22546
22547 default:
b98664d3 22548 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22549 dwarf_form_name (attr->form));
22550 break;
22551 }
22552
22553 return result;
22554}
22555
d4c9a4f8 22556/* See read.h. */
7942e96e
AA
22557
22558struct type *
9c541725 22559dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22560 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22561{
22562 struct dwarf2_cu *cu;
22563 struct die_info *die;
22564
7942e96e 22565 if (per_cu->cu == NULL)
ab432490 22566 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
7942e96e
AA
22567 cu = per_cu->cu;
22568 if (!cu)
22569 return NULL;
22570
9c541725 22571 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22572 if (!die)
22573 return NULL;
22574
22575 return die_type (die, cu);
22576}
22577
8cb5117c 22578/* See read.h. */
8a9b8146
TT
22579
22580struct type *
b64f50a1 22581dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22582 struct dwarf2_per_cu_data *per_cu)
22583{
9c541725 22584 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22585 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22586}
22587
ac9ec31b 22588/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22589 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22590 On exit *REF_CU is the CU of the result.
22591 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22592
22593static struct die_info *
ac9ec31b
DE
22594follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22595 struct dwarf2_cu **ref_cu)
348e048f 22596{
348e048f 22597 struct die_info temp_die;
c24bdb02 22598 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22599 struct die_info *die;
22600
ac9ec31b
DE
22601 /* While it might be nice to assert sig_type->type == NULL here,
22602 we can get here for DW_AT_imported_declaration where we need
22603 the DIE not the type. */
348e048f
DE
22604
22605 /* If necessary, add it to the queue and load its DIEs. */
22606
95554aad 22607 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
ab432490 22608 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
348e048f 22609
348e048f 22610 sig_cu = sig_type->per_cu.cu;
69d751e3 22611 gdb_assert (sig_cu != NULL);
9c541725
PA
22612 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22613 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22614 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22615 to_underlying (temp_die.sect_off));
348e048f
DE
22616 if (die)
22617 {
5e22e966 22618 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
ed2dc618 22619
796a7ff8
DE
22620 /* For .gdb_index version 7 keep track of included TUs.
22621 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22622 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22623 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22624 {
ae640021 22625 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22626 }
22627
348e048f 22628 *ref_cu = sig_cu;
c24bdb02
KS
22629 if (sig_cu != cu)
22630 sig_cu->ancestor = cu;
22631
348e048f
DE
22632 return die;
22633 }
22634
ac9ec31b
DE
22635 return NULL;
22636}
22637
22638/* Follow signatured type referenced by ATTR in SRC_DIE.
22639 On entry *REF_CU is the CU of SRC_DIE.
22640 On exit *REF_CU is the CU of the result.
22641 The result is the DIE of the type.
22642 If the referenced type cannot be found an error is thrown. */
22643
22644static struct die_info *
ff39bb5e 22645follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22646 struct dwarf2_cu **ref_cu)
22647{
22648 ULONGEST signature = DW_SIGNATURE (attr);
22649 struct signatured_type *sig_type;
22650 struct die_info *die;
22651
22652 gdb_assert (attr->form == DW_FORM_ref_sig8);
22653
a2ce51a0 22654 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22655 /* sig_type will be NULL if the signatured type is missing from
22656 the debug info. */
22657 if (sig_type == NULL)
22658 {
22659 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22660 " from DIE at %s [in module %s]"),
22661 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22662 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22663 }
22664
22665 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22666 if (die == NULL)
22667 {
22668 dump_die_for_error (src_die);
22669 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22670 " from DIE at %s [in module %s]"),
22671 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22672 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22673 }
22674
22675 return die;
22676}
22677
22678/* Get the type specified by SIGNATURE referenced in DIE/CU,
22679 reading in and processing the type unit if necessary. */
22680
22681static struct type *
22682get_signatured_type (struct die_info *die, ULONGEST signature,
22683 struct dwarf2_cu *cu)
22684{
5e22e966 22685 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22686 struct signatured_type *sig_type;
22687 struct dwarf2_cu *type_cu;
22688 struct die_info *type_die;
22689 struct type *type;
22690
a2ce51a0 22691 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22692 /* sig_type will be NULL if the signatured type is missing from
22693 the debug info. */
22694 if (sig_type == NULL)
22695 {
b98664d3 22696 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22697 " from DIE at %s [in module %s]"),
22698 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22699 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22700 return build_error_marker_type (cu, die);
22701 }
22702
22703 /* If we already know the type we're done. */
22704 if (sig_type->type != NULL)
22705 return sig_type->type;
22706
22707 type_cu = cu;
22708 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22709 if (type_die != NULL)
22710 {
22711 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22712 is created. This is important, for example, because for c++ classes
22713 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22714 type = read_type_die (type_die, type_cu);
22715 if (type == NULL)
22716 {
b98664d3 22717 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22718 " referenced from DIE at %s [in module %s]"),
22719 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22720 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22721 type = build_error_marker_type (cu, die);
22722 }
22723 }
22724 else
22725 {
b98664d3 22726 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22727 " from DIE at %s [in module %s]"),
22728 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22729 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22730 type = build_error_marker_type (cu, die);
22731 }
22732 sig_type->type = type;
22733
22734 return type;
22735}
22736
22737/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22738 reading in and processing the type unit if necessary. */
22739
22740static struct type *
ff39bb5e 22741get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22742 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22743{
22744 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22745 if (attr->form_is_ref ())
ac9ec31b
DE
22746 {
22747 struct dwarf2_cu *type_cu = cu;
22748 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22749
22750 return read_type_die (type_die, type_cu);
22751 }
22752 else if (attr->form == DW_FORM_ref_sig8)
22753 {
22754 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22755 }
22756 else
22757 {
5e22e966 22758 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22759
b98664d3 22760 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22761 " at %s [in module %s]"),
22762 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22763 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22764 return build_error_marker_type (cu, die);
22765 }
348e048f
DE
22766}
22767
e5fe5e75 22768/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22769
22770static void
ab432490
SM
22771load_full_type_unit (dwarf2_per_cu_data *per_cu,
22772 dwarf2_per_objfile *per_objfile)
348e048f 22773{
52dc124a 22774 struct signatured_type *sig_type;
348e048f 22775
f4dc4d17 22776 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22777 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22778
6721b2ec
DE
22779 /* We have the per_cu, but we need the signatured_type.
22780 Fortunately this is an easy translation. */
22781 gdb_assert (per_cu->is_debug_types);
22782 sig_type = (struct signatured_type *) per_cu;
348e048f 22783
6721b2ec 22784 gdb_assert (per_cu->cu == NULL);
348e048f 22785
ab432490 22786 read_signatured_type (sig_type, per_objfile);
348e048f 22787
6721b2ec 22788 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22789}
22790
3019eac3
DE
22791/* Read in a signatured type and build its CU and DIEs.
22792 If the type is a stub for the real type in a DWO file,
22793 read in the real type from the DWO file as well. */
dee91e82
DE
22794
22795static void
ab432490
SM
22796read_signatured_type (signatured_type *sig_type,
22797 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22798{
22799 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22800
3019eac3 22801 gdb_assert (per_cu->is_debug_types);
dee91e82 22802 gdb_assert (per_cu->cu == NULL);
348e048f 22803
ab432490 22804 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22805
22806 if (!reader.dummy_p)
22807 {
22808 struct dwarf2_cu *cu = reader.cu;
22809 const gdb_byte *info_ptr = reader.info_ptr;
22810
22811 gdb_assert (cu->die_hash == NULL);
22812 cu->die_hash =
22813 htab_create_alloc_ex (cu->header.length / 12,
22814 die_hash,
22815 die_eq,
22816 NULL,
22817 &cu->comp_unit_obstack,
22818 hashtab_obstack_allocate,
22819 dummy_obstack_deallocate);
22820
3e225074 22821 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22822 reader.comp_unit_die->child
22823 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22824 reader.comp_unit_die);
22825 cu->dies = reader.comp_unit_die;
22826 /* comp_unit_die is not stored in die_hash, no need. */
22827
22828 /* We try not to read any attributes in this function, because
22829 not all CUs needed for references have been loaded yet, and
22830 symbol table processing isn't initialized. But we have to
22831 set the CU language, or we won't be able to build types
22832 correctly. Similarly, if we do not read the producer, we can
22833 not apply producer-specific interpretation. */
22834 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22835
22836 reader.keep ();
c0ab21c2
TT
22837 }
22838
7ee85ab1 22839 sig_type->per_cu.tu_read = 1;
c906108c
SS
22840}
22841
c906108c
SS
22842/* Decode simple location descriptions.
22843 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22844 the location and return the value. If COMPUTED is non-null, it is
22845 set to true to indicate that decoding was successful, and false
22846 otherwise. If COMPUTED is null, then this function may emit a
22847 complaint. */
c906108c
SS
22848
22849static CORE_ADDR
7d79de9a 22850decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22851{
5e22e966 22852 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22853 size_t i;
22854 size_t size = blk->size;
d521ce57 22855 const gdb_byte *data = blk->data;
21ae7a4d
JK
22856 CORE_ADDR stack[64];
22857 int stacki;
22858 unsigned int bytes_read, unsnd;
22859 gdb_byte op;
c906108c 22860
7d79de9a
TT
22861 if (computed != nullptr)
22862 *computed = false;
22863
21ae7a4d
JK
22864 i = 0;
22865 stacki = 0;
22866 stack[stacki] = 0;
22867 stack[++stacki] = 0;
22868
22869 while (i < size)
22870 {
22871 op = data[i++];
22872 switch (op)
22873 {
22874 case DW_OP_lit0:
22875 case DW_OP_lit1:
22876 case DW_OP_lit2:
22877 case DW_OP_lit3:
22878 case DW_OP_lit4:
22879 case DW_OP_lit5:
22880 case DW_OP_lit6:
22881 case DW_OP_lit7:
22882 case DW_OP_lit8:
22883 case DW_OP_lit9:
22884 case DW_OP_lit10:
22885 case DW_OP_lit11:
22886 case DW_OP_lit12:
22887 case DW_OP_lit13:
22888 case DW_OP_lit14:
22889 case DW_OP_lit15:
22890 case DW_OP_lit16:
22891 case DW_OP_lit17:
22892 case DW_OP_lit18:
22893 case DW_OP_lit19:
22894 case DW_OP_lit20:
22895 case DW_OP_lit21:
22896 case DW_OP_lit22:
22897 case DW_OP_lit23:
22898 case DW_OP_lit24:
22899 case DW_OP_lit25:
22900 case DW_OP_lit26:
22901 case DW_OP_lit27:
22902 case DW_OP_lit28:
22903 case DW_OP_lit29:
22904 case DW_OP_lit30:
22905 case DW_OP_lit31:
22906 stack[++stacki] = op - DW_OP_lit0;
22907 break;
f1bea926 22908
21ae7a4d
JK
22909 case DW_OP_reg0:
22910 case DW_OP_reg1:
22911 case DW_OP_reg2:
22912 case DW_OP_reg3:
22913 case DW_OP_reg4:
22914 case DW_OP_reg5:
22915 case DW_OP_reg6:
22916 case DW_OP_reg7:
22917 case DW_OP_reg8:
22918 case DW_OP_reg9:
22919 case DW_OP_reg10:
22920 case DW_OP_reg11:
22921 case DW_OP_reg12:
22922 case DW_OP_reg13:
22923 case DW_OP_reg14:
22924 case DW_OP_reg15:
22925 case DW_OP_reg16:
22926 case DW_OP_reg17:
22927 case DW_OP_reg18:
22928 case DW_OP_reg19:
22929 case DW_OP_reg20:
22930 case DW_OP_reg21:
22931 case DW_OP_reg22:
22932 case DW_OP_reg23:
22933 case DW_OP_reg24:
22934 case DW_OP_reg25:
22935 case DW_OP_reg26:
22936 case DW_OP_reg27:
22937 case DW_OP_reg28:
22938 case DW_OP_reg29:
22939 case DW_OP_reg30:
22940 case DW_OP_reg31:
22941 stack[++stacki] = op - DW_OP_reg0;
22942 if (i < size)
7d79de9a
TT
22943 {
22944 if (computed == nullptr)
22945 dwarf2_complex_location_expr_complaint ();
22946 else
22947 return 0;
22948 }
21ae7a4d 22949 break;
c906108c 22950
21ae7a4d
JK
22951 case DW_OP_regx:
22952 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22953 i += bytes_read;
22954 stack[++stacki] = unsnd;
22955 if (i < size)
7d79de9a
TT
22956 {
22957 if (computed == nullptr)
22958 dwarf2_complex_location_expr_complaint ();
22959 else
22960 return 0;
22961 }
21ae7a4d 22962 break;
c906108c 22963
21ae7a4d 22964 case DW_OP_addr:
c8a7a66f
TT
22965 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22966 &bytes_read);
21ae7a4d
JK
22967 i += bytes_read;
22968 break;
d53d4ac5 22969
21ae7a4d
JK
22970 case DW_OP_const1u:
22971 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22972 i += 1;
22973 break;
22974
22975 case DW_OP_const1s:
22976 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22977 i += 1;
22978 break;
22979
22980 case DW_OP_const2u:
22981 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22982 i += 2;
22983 break;
22984
22985 case DW_OP_const2s:
22986 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22987 i += 2;
22988 break;
d53d4ac5 22989
21ae7a4d
JK
22990 case DW_OP_const4u:
22991 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22992 i += 4;
22993 break;
22994
22995 case DW_OP_const4s:
22996 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22997 i += 4;
22998 break;
22999
585861ea
JK
23000 case DW_OP_const8u:
23001 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23002 i += 8;
23003 break;
23004
21ae7a4d
JK
23005 case DW_OP_constu:
23006 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23007 &bytes_read);
23008 i += bytes_read;
23009 break;
23010
23011 case DW_OP_consts:
23012 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23013 i += bytes_read;
23014 break;
23015
23016 case DW_OP_dup:
23017 stack[stacki + 1] = stack[stacki];
23018 stacki++;
23019 break;
23020
23021 case DW_OP_plus:
23022 stack[stacki - 1] += stack[stacki];
23023 stacki--;
23024 break;
23025
23026 case DW_OP_plus_uconst:
23027 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23028 &bytes_read);
23029 i += bytes_read;
23030 break;
23031
23032 case DW_OP_minus:
23033 stack[stacki - 1] -= stack[stacki];
23034 stacki--;
23035 break;
23036
23037 case DW_OP_deref:
23038 /* If we're not the last op, then we definitely can't encode
23039 this using GDB's address_class enum. This is valid for partial
23040 global symbols, although the variable's address will be bogus
23041 in the psymtab. */
23042 if (i < size)
7d79de9a
TT
23043 {
23044 if (computed == nullptr)
23045 dwarf2_complex_location_expr_complaint ();
23046 else
23047 return 0;
23048 }
21ae7a4d
JK
23049 break;
23050
23051 case DW_OP_GNU_push_tls_address:
4aa4e28b 23052 case DW_OP_form_tls_address:
21ae7a4d
JK
23053 /* The top of the stack has the offset from the beginning
23054 of the thread control block at which the variable is located. */
23055 /* Nothing should follow this operator, so the top of stack would
23056 be returned. */
23057 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23058 address will be bogus in the psymtab. Make it always at least
23059 non-zero to not look as a variable garbage collected by linker
23060 which have DW_OP_addr 0. */
21ae7a4d 23061 if (i < size)
7d79de9a
TT
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
585861ea 23068 stack[stacki]++;
21ae7a4d
JK
23069 break;
23070
23071 case DW_OP_GNU_uninit:
7d79de9a
TT
23072 if (computed != nullptr)
23073 return 0;
21ae7a4d
JK
23074 break;
23075
336d760d 23076 case DW_OP_addrx:
3019eac3 23077 case DW_OP_GNU_addr_index:
49f6c839 23078 case DW_OP_GNU_const_index:
3019eac3
DE
23079 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23080 &bytes_read);
23081 i += bytes_read;
23082 break;
23083
21ae7a4d 23084 default:
7d79de9a
TT
23085 if (computed == nullptr)
23086 {
23087 const char *name = get_DW_OP_name (op);
21ae7a4d 23088
7d79de9a
TT
23089 if (name)
23090 complaint (_("unsupported stack op: '%s'"),
23091 name);
23092 else
23093 complaint (_("unsupported stack op: '%02x'"),
23094 op);
23095 }
21ae7a4d
JK
23096
23097 return (stack[stacki]);
d53d4ac5 23098 }
3c6e0cb3 23099
21ae7a4d
JK
23100 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23101 outside of the allocated space. Also enforce minimum>0. */
23102 if (stacki >= ARRAY_SIZE (stack) - 1)
23103 {
7d79de9a
TT
23104 if (computed == nullptr)
23105 complaint (_("location description stack overflow"));
21ae7a4d
JK
23106 return 0;
23107 }
23108
23109 if (stacki <= 0)
23110 {
7d79de9a
TT
23111 if (computed == nullptr)
23112 complaint (_("location description stack underflow"));
21ae7a4d
JK
23113 return 0;
23114 }
23115 }
7d79de9a
TT
23116
23117 if (computed != nullptr)
23118 *computed = true;
21ae7a4d 23119 return (stack[stacki]);
c906108c
SS
23120}
23121
23122/* memory allocation interface */
23123
c906108c 23124static struct dwarf_block *
7b5a2f43 23125dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23126{
8d749320 23127 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23128}
23129
c906108c 23130static struct die_info *
b60c80d6 23131dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23132{
23133 struct die_info *die;
b60c80d6
DJ
23134 size_t size = sizeof (struct die_info);
23135
23136 if (num_attrs > 1)
23137 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23138
b60c80d6 23139 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23140 memset (die, 0, sizeof (struct die_info));
23141 return (die);
23142}
2e276125
JB
23143
23144\f
a036ba48 23145
c90ec28a 23146/* Macro support. */
cf2c3c16 23147
9eac9650
TT
23148/* An overload of dwarf_decode_macros that finds the correct section
23149 and ensures it is read in before calling the other overload. */
23150
23151static void
23152dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23153 int section_is_gnu)
23154{
5e22e966 23155 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23156 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23157 const struct line_header *lh = cu->line_header;
9eac9650
TT
23158 unsigned int offset_size = cu->header.offset_size;
23159 struct dwarf2_section_info *section;
23160 const char *section_name;
23161
23162 if (cu->dwo_unit != nullptr)
23163 {
23164 if (section_is_gnu)
23165 {
23166 section = &cu->dwo_unit->dwo_file->sections.macro;
23167 section_name = ".debug_macro.dwo";
23168 }
23169 else
23170 {
23171 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23172 section_name = ".debug_macinfo.dwo";
23173 }
23174 }
23175 else
23176 {
23177 if (section_is_gnu)
23178 {
5989a64e 23179 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23180 section_name = ".debug_macro";
23181 }
23182 else
23183 {
5989a64e 23184 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23185 section_name = ".debug_macinfo";
23186 }
23187 }
23188
23189 section->read (objfile);
23190 if (section->buffer == nullptr)
23191 {
23192 complaint (_("missing %s section"), section_name);
23193 return;
23194 }
23195
23196 buildsym_compunit *builder = cu->get_builder ();
23197
23198 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23199 offset_size, offset, section_is_gnu);
23200}
23201
3019eac3
DE
23202/* Return the .debug_loc section to use for CU.
23203 For DWO files use .debug_loc.dwo. */
23204
23205static struct dwarf2_section_info *
23206cu_debug_loc_section (struct dwarf2_cu *cu)
23207{
5e22e966 23208 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23209
3019eac3 23210 if (cu->dwo_unit)
43988095
JK
23211 {
23212 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23213
43988095
JK
23214 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23215 }
5989a64e
SM
23216 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23217 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23218}
23219
8cf6f0b1
TT
23220/* A helper function that fills in a dwarf2_loclist_baton. */
23221
23222static void
23223fill_in_loclist_baton (struct dwarf2_cu *cu,
23224 struct dwarf2_loclist_baton *baton,
ff39bb5e 23225 const struct attribute *attr)
8cf6f0b1 23226{
5e22e966 23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23228 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23229
96b79293 23230 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23231
a50264ba 23232 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23233 baton->per_cu = cu->per_cu;
23234 gdb_assert (baton->per_cu);
23235 /* We don't know how long the location list is, but make sure we
23236 don't run off the edge of the section. */
3019eac3
DE
23237 baton->size = section->size - DW_UNSND (attr);
23238 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23239 if (cu->base_address.has_value ())
23240 baton->base_address = *cu->base_address;
23241 else
23242 baton->base_address = 0;
f664829e 23243 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23244}
23245
4c2df51b 23246static void
ff39bb5e 23247dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23248 struct dwarf2_cu *cu, int is_block)
4c2df51b 23249{
5e22e966 23250 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23251 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23252 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23253
cd6c91b4 23254 if (attr->form_is_section_offset ()
3019eac3 23255 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23256 the section. If so, fall through to the complaint in the
23257 other branch. */
2c7d5afc 23258 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23259 {
0d53c4c4 23260 struct dwarf2_loclist_baton *baton;
4c2df51b 23261
8d749320 23262 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23263
8cf6f0b1 23264 fill_in_loclist_baton (cu, baton, attr);
be391dca 23265
2b24b6e4 23266 if (!cu->base_address.has_value ())
b98664d3 23267 complaint (_("Location list used without "
3e43a32a 23268 "specifying the CU base address."));
4c2df51b 23269
f1e6e072
TT
23270 SYMBOL_ACLASS_INDEX (sym) = (is_block
23271 ? dwarf2_loclist_block_index
23272 : dwarf2_loclist_index);
0d53c4c4
DJ
23273 SYMBOL_LOCATION_BATON (sym) = baton;
23274 }
23275 else
23276 {
23277 struct dwarf2_locexpr_baton *baton;
23278
8d749320 23279 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23280 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23281 baton->per_cu = cu->per_cu;
23282 gdb_assert (baton->per_cu);
0d53c4c4 23283
4fc6c0d5 23284 if (attr->form_is_block ())
0d53c4c4
DJ
23285 {
23286 /* Note that we're just copying the block's data pointer
23287 here, not the actual data. We're still pointing into the
6502dd73
DJ
23288 info_buffer for SYM's objfile; right now we never release
23289 that buffer, but when we do clean up properly this may
23290 need to change. */
0d53c4c4
DJ
23291 baton->size = DW_BLOCK (attr)->size;
23292 baton->data = DW_BLOCK (attr)->data;
23293 }
23294 else
23295 {
23296 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23297 sym->natural_name ());
0d53c4c4 23298 baton->size = 0;
0d53c4c4 23299 }
6e70227d 23300
f1e6e072
TT
23301 SYMBOL_ACLASS_INDEX (sym) = (is_block
23302 ? dwarf2_locexpr_block_index
23303 : dwarf2_locexpr_index);
0d53c4c4
DJ
23304 SYMBOL_LOCATION_BATON (sym) = baton;
23305 }
4c2df51b 23306}
6502dd73 23307
09ba997f 23308/* See read.h. */
ae0d2f24
UW
23309
23310struct objfile *
09ba997f 23311dwarf2_per_cu_data::objfile () const
ae0d2f24 23312{
09ba997f 23313 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23314
23315 /* Return the master objfile, so that we can report and look up the
23316 correct file containing this variable. */
23317 if (objfile->separate_debug_objfile_backlink)
23318 objfile = objfile->separate_debug_objfile_backlink;
23319
23320 return objfile;
23321}
23322
96408a79
SA
23323/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23324 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23325 CU_HEADERP first. */
23326
23327static const struct comp_unit_head *
23328per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23329 const struct dwarf2_per_cu_data *per_cu)
96408a79 23330{
d521ce57 23331 const gdb_byte *info_ptr;
96408a79
SA
23332
23333 if (per_cu->cu)
23334 return &per_cu->cu->header;
23335
9c541725 23336 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23337
23338 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23339 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23340 rcuh_kind::COMPILE);
96408a79
SA
23341
23342 return cu_headerp;
23343}
23344
09ba997f 23345/* See read.h. */
ae0d2f24 23346
98714339 23347int
09ba997f 23348dwarf2_per_cu_data::addr_size () const
ae0d2f24 23349{
96408a79
SA
23350 struct comp_unit_head cu_header_local;
23351 const struct comp_unit_head *cu_headerp;
c471e790 23352
09ba997f 23353 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23354
23355 return cu_headerp->addr_size;
ae0d2f24
UW
23356}
23357
09ba997f 23358/* See read.h. */
9eae7c52
TT
23359
23360int
09ba997f 23361dwarf2_per_cu_data::offset_size () const
9eae7c52 23362{
96408a79
SA
23363 struct comp_unit_head cu_header_local;
23364 const struct comp_unit_head *cu_headerp;
9c6c53f7 23365
09ba997f 23366 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23367
23368 return cu_headerp->offset_size;
23369}
23370
09ba997f 23371/* See read.h. */
96408a79
SA
23372
23373int
09ba997f 23374dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23375{
23376 struct comp_unit_head cu_header_local;
23377 const struct comp_unit_head *cu_headerp;
23378
09ba997f 23379 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23380
23381 if (cu_headerp->version == 2)
23382 return cu_headerp->addr_size;
23383 else
23384 return cu_headerp->offset_size;
181cebd4
JK
23385}
23386
09ba997f 23387/* See read.h. */
9aa1f1e3
TT
23388
23389CORE_ADDR
09ba997f 23390dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23391{
09ba997f
TT
23392 struct objfile *objfile = dwarf2_per_objfile->objfile;
23393
23394 return objfile->text_section_offset ();
9aa1f1e3
TT
23395}
23396
09ba997f
TT
23397/* See read.h. */
23398
23399struct type *
23400dwarf2_per_cu_data::addr_type () const
9a49df9d 23401{
09ba997f 23402 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23403 struct type *void_type = objfile_type (objfile)->builtin_void;
23404 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23405 int addr_size = this->addr_size ();
9a49df9d
AB
23406
23407 if (TYPE_LENGTH (addr_type) == addr_size)
23408 return addr_type;
23409
09ba997f 23410 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23411 return addr_type;
23412}
23413
22b6cd70
TT
23414/* A helper function for dwarf2_find_containing_comp_unit that returns
23415 the index of the result, and that searches a vector. It will
23416 return a result even if the offset in question does not actually
23417 occur in any CU. This is separate so that it can be unit
23418 tested. */
ae038cb0 23419
22b6cd70
TT
23420static int
23421dwarf2_find_containing_comp_unit
23422 (sect_offset sect_off,
23423 unsigned int offset_in_dwz,
23424 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23425{
ae038cb0
DJ
23426 int low, high;
23427
ae038cb0 23428 low = 0;
22b6cd70 23429 high = all_comp_units.size () - 1;
ae038cb0
DJ
23430 while (high > low)
23431 {
36586728 23432 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23433 int mid = low + (high - low) / 2;
9a619af0 23434
22b6cd70 23435 mid_cu = all_comp_units[mid];
36586728 23436 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23437 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23438 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23439 high = mid;
23440 else
23441 low = mid + 1;
23442 }
23443 gdb_assert (low == high);
22b6cd70
TT
23444 return low;
23445}
23446
23447/* Locate the .debug_info compilation unit from CU's objfile which contains
23448 the DIE at OFFSET. Raises an error on failure. */
23449
23450static struct dwarf2_per_cu_data *
23451dwarf2_find_containing_comp_unit (sect_offset sect_off,
23452 unsigned int offset_in_dwz,
23453 struct dwarf2_per_objfile *dwarf2_per_objfile)
23454{
23455 int low
23456 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23457 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23458 struct dwarf2_per_cu_data *this_cu
5989a64e 23459 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23460
45b8ae0c 23461 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23462 {
36586728 23463 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23464 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23465 "offset %s [in module %s]"),
23466 sect_offset_str (sect_off),
ed2dc618 23467 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23468
5989a64e 23469 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23470 <= sect_off);
5989a64e 23471 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23472 }
23473 else
23474 {
5989a64e 23475 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23476 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23477 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23478 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23479 return this_cu;
23480 }
23481}
23482
22b6cd70
TT
23483#if GDB_SELF_TEST
23484
23485namespace selftests {
23486namespace find_containing_comp_unit {
23487
23488static void
23489run_test ()
23490{
23491 struct dwarf2_per_cu_data one {};
23492 struct dwarf2_per_cu_data two {};
23493 struct dwarf2_per_cu_data three {};
23494 struct dwarf2_per_cu_data four {};
23495
23496 one.length = 5;
23497 two.sect_off = sect_offset (one.length);
23498 two.length = 7;
23499
23500 three.length = 5;
23501 three.is_dwz = 1;
23502 four.sect_off = sect_offset (three.length);
23503 four.length = 7;
23504 four.is_dwz = 1;
23505
23506 std::vector<dwarf2_per_cu_data *> units;
23507 units.push_back (&one);
23508 units.push_back (&two);
23509 units.push_back (&three);
23510 units.push_back (&four);
23511
23512 int result;
23513
23514 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23515 SELF_CHECK (units[result] == &one);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23517 SELF_CHECK (units[result] == &one);
23518 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23519 SELF_CHECK (units[result] == &two);
23520
23521 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23522 SELF_CHECK (units[result] == &three);
23523 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23524 SELF_CHECK (units[result] == &three);
23525 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23526 SELF_CHECK (units[result] == &four);
23527}
23528
23529}
23530}
23531
23532#endif /* GDB_SELF_TEST */
23533
9e021579 23534/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23535
9e021579
SM
23536dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23537 dwarf2_per_objfile *per_objfile)
23538 : per_cu (per_cu),
23539 per_objfile (per_objfile),
9068261f
AB
23540 mark (false),
23541 has_loclist (false),
23542 checked_producer (false),
23543 producer_is_gxx_lt_4_6 (false),
23544 producer_is_gcc_lt_4_3 (false),
eb77c9df 23545 producer_is_icc (false),
9068261f 23546 producer_is_icc_lt_14 (false),
c258c396 23547 producer_is_codewarrior (false),
9068261f 23548 processing_has_namespace_info (false)
93311388 23549{
fcd3b13d
SM
23550 per_cu->cu = this;
23551}
23552
23553/* Destroy a dwarf2_cu. */
23554
23555dwarf2_cu::~dwarf2_cu ()
23556{
23557 per_cu->cu = NULL;
9816fde3
JK
23558}
23559
23560/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23561
23562static void
95554aad
TT
23563prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23564 enum language pretend_language)
9816fde3
JK
23565{
23566 struct attribute *attr;
23567
23568 /* Set the language we're debugging. */
23569 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23570 if (attr != nullptr)
9816fde3
JK
23571 set_cu_language (DW_UNSND (attr), cu);
23572 else
9cded63f 23573 {
95554aad 23574 cu->language = pretend_language;
9cded63f
TT
23575 cu->language_defn = language_def (cu->language);
23576 }
dee91e82 23577
7d45c7c3 23578 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23579}
23580
ae038cb0
DJ
23581/* Increase the age counter on each cached compilation unit, and free
23582 any that are too old. */
23583
23584static void
ed2dc618 23585age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23586{
23587 struct dwarf2_per_cu_data *per_cu, **last_chain;
23588
5989a64e
SM
23589 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23590 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23591 while (per_cu != NULL)
23592 {
23593 per_cu->cu->last_used ++;
b4f54984 23594 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23595 dwarf2_mark (per_cu->cu);
23596 per_cu = per_cu->cu->read_in_chain;
23597 }
23598
5989a64e
SM
23599 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23600 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23601 while (per_cu != NULL)
23602 {
23603 struct dwarf2_per_cu_data *next_cu;
23604
23605 next_cu = per_cu->cu->read_in_chain;
23606
23607 if (!per_cu->cu->mark)
23608 {
fcd3b13d 23609 delete per_cu->cu;
ae038cb0
DJ
23610 *last_chain = next_cu;
23611 }
23612 else
23613 last_chain = &per_cu->cu->read_in_chain;
23614
23615 per_cu = next_cu;
23616 }
23617}
23618
23619/* Remove a single compilation unit from the cache. */
23620
23621static void
dee91e82 23622free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23623{
23624 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23625 struct dwarf2_per_objfile *dwarf2_per_objfile
23626 = target_per_cu->dwarf2_per_objfile;
ae038cb0 23627
5989a64e
SM
23628 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23629 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23630 while (per_cu != NULL)
23631 {
23632 struct dwarf2_per_cu_data *next_cu;
23633
23634 next_cu = per_cu->cu->read_in_chain;
23635
dee91e82 23636 if (per_cu == target_per_cu)
ae038cb0 23637 {
fcd3b13d 23638 delete per_cu->cu;
dee91e82 23639 per_cu->cu = NULL;
ae038cb0
DJ
23640 *last_chain = next_cu;
23641 break;
23642 }
23643 else
23644 last_chain = &per_cu->cu->read_in_chain;
23645
23646 per_cu = next_cu;
23647 }
23648}
23649
dee91e82
DE
23650/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23651 We store these in a hash table separate from the DIEs, and preserve them
23652 when the DIEs are flushed out of cache.
23653
23654 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23655 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23656 or the type may come from a DWO file. Furthermore, while it's more logical
23657 to use per_cu->section+offset, with Fission the section with the data is in
23658 the DWO file but we don't know that section at the point we need it.
23659 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23660 because we can enter the lookup routine, get_die_type_at_offset, from
23661 outside this file, and thus won't necessarily have PER_CU->cu.
23662 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23663
dee91e82 23664struct dwarf2_per_cu_offset_and_type
1c379e20 23665{
dee91e82 23666 const struct dwarf2_per_cu_data *per_cu;
9c541725 23667 sect_offset sect_off;
1c379e20
DJ
23668 struct type *type;
23669};
23670
dee91e82 23671/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23672
23673static hashval_t
dee91e82 23674per_cu_offset_and_type_hash (const void *item)
1c379e20 23675{
9a3c8263
SM
23676 const struct dwarf2_per_cu_offset_and_type *ofs
23677 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23678
9c541725 23679 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23680}
23681
dee91e82 23682/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23683
23684static int
dee91e82 23685per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23686{
9a3c8263
SM
23687 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23688 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23689 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23690 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23691
dee91e82 23692 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23693 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23694}
23695
23696/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23697 table if necessary. For convenience, return TYPE.
23698
23699 The DIEs reading must have careful ordering to:
85102364 23700 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23701 reading current DIE.
23702 * Not trying to dereference contents of still incompletely read in types
23703 while reading in other DIEs.
23704 * Enable referencing still incompletely read in types just by a pointer to
23705 the type without accessing its fields.
23706
23707 Therefore caller should follow these rules:
23708 * Try to fetch any prerequisite types we may need to build this DIE type
23709 before building the type and calling set_die_type.
e71ec853 23710 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23711 possible before fetching more types to complete the current type.
23712 * Make the type as complete as possible before fetching more types. */
1c379e20 23713
f792889a 23714static struct type *
1c379e20
DJ
23715set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23716{
5e22e966 23717 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23718 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23719 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23720 struct attribute *attr;
23721 struct dynamic_prop prop;
1c379e20 23722
b4ba55a1
JB
23723 /* For Ada types, make sure that the gnat-specific data is always
23724 initialized (if not already set). There are a few types where
23725 we should not be doing so, because the type-specific area is
23726 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23727 where the type-specific area is used to store the floatformat).
23728 But this is not a problem, because the gnat-specific information
23729 is actually not needed for these types. */
23730 if (need_gnat_info (cu)
78134374
SM
23731 && type->code () != TYPE_CODE_FUNC
23732 && type->code () != TYPE_CODE_FLT
23733 && type->code () != TYPE_CODE_METHODPTR
23734 && type->code () != TYPE_CODE_MEMBERPTR
23735 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23736 && !HAVE_GNAT_AUX_INFO (type))
23737 INIT_GNAT_SPECIFIC (type);
23738
3f2f83dd
KB
23739 /* Read DW_AT_allocated and set in type. */
23740 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23741 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23742 {
09ba997f 23743 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23744 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23745 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23746 }
23747 else if (attr != NULL)
23748 {
b98664d3 23749 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23750 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23751 sect_offset_str (die->sect_off));
3f2f83dd
KB
23752 }
23753
23754 /* Read DW_AT_associated and set in type. */
23755 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23756 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23757 {
09ba997f 23758 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23759 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23760 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23761 }
23762 else if (attr != NULL)
23763 {
b98664d3 23764 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23765 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23766 sect_offset_str (die->sect_off));
3f2f83dd
KB
23767 }
23768
3cdcd0ce
JB
23769 /* Read DW_AT_data_location and set in type. */
23770 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23771 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23772 cu->per_cu->addr_type ()))
5c54719c 23773 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23774
ae090bdb
SM
23775 if (dwarf2_per_objfile->die_type_hash == NULL)
23776 dwarf2_per_objfile->die_type_hash
0335378b
TT
23777 = htab_up (htab_create_alloc (127,
23778 per_cu_offset_and_type_hash,
23779 per_cu_offset_and_type_eq,
23780 NULL, xcalloc, xfree));
1c379e20 23781
dee91e82 23782 ofs.per_cu = cu->per_cu;
9c541725 23783 ofs.sect_off = die->sect_off;
1c379e20 23784 ofs.type = type;
dee91e82 23785 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23786 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23787 if (*slot)
b98664d3 23788 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23789 sect_offset_str (die->sect_off));
8d749320
SM
23790 *slot = XOBNEW (&objfile->objfile_obstack,
23791 struct dwarf2_per_cu_offset_and_type);
1c379e20 23792 **slot = ofs;
f792889a 23793 return type;
1c379e20
DJ
23794}
23795
9c541725 23796/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23797 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23798
23799static struct type *
9c541725 23800get_die_type_at_offset (sect_offset sect_off,
673bfd45 23801 struct dwarf2_per_cu_data *per_cu)
1c379e20 23802{
dee91e82 23803 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23804 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23805
ae090bdb 23806 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23807 return NULL;
1c379e20 23808
dee91e82 23809 ofs.per_cu = per_cu;
9c541725 23810 ofs.sect_off = sect_off;
9a3c8263 23811 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23812 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23813 if (slot)
23814 return slot->type;
23815 else
23816 return NULL;
23817}
23818
02142a6c 23819/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23820 or return NULL if DIE does not have a saved type. */
23821
23822static struct type *
23823get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23824{
9c541725 23825 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23826}
23827
10b3939b
DJ
23828/* Add a dependence relationship from CU to REF_PER_CU. */
23829
23830static void
23831dwarf2_add_dependence (struct dwarf2_cu *cu,
23832 struct dwarf2_per_cu_data *ref_per_cu)
23833{
23834 void **slot;
23835
23836 if (cu->dependencies == NULL)
23837 cu->dependencies
23838 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23839 NULL, &cu->comp_unit_obstack,
23840 hashtab_obstack_allocate,
23841 dummy_obstack_deallocate);
23842
23843 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23844 if (*slot == NULL)
23845 *slot = ref_per_cu;
23846}
1c379e20 23847
f504f079
DE
23848/* Subroutine of dwarf2_mark to pass to htab_traverse.
23849 Set the mark field in every compilation unit in the
ae038cb0
DJ
23850 cache that we must keep because we are keeping CU. */
23851
10b3939b
DJ
23852static int
23853dwarf2_mark_helper (void **slot, void *data)
23854{
23855 struct dwarf2_per_cu_data *per_cu;
23856
23857 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23858
23859 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23860 reading of the chain. As such dependencies remain valid it is not much
23861 useful to track and undo them during QUIT cleanups. */
23862 if (per_cu->cu == NULL)
23863 return 1;
23864
10b3939b
DJ
23865 if (per_cu->cu->mark)
23866 return 1;
9068261f 23867 per_cu->cu->mark = true;
10b3939b
DJ
23868
23869 if (per_cu->cu->dependencies != NULL)
23870 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23871
23872 return 1;
23873}
23874
f504f079
DE
23875/* Set the mark field in CU and in every other compilation unit in the
23876 cache that we must keep because we are keeping CU. */
23877
ae038cb0
DJ
23878static void
23879dwarf2_mark (struct dwarf2_cu *cu)
23880{
23881 if (cu->mark)
23882 return;
9068261f 23883 cu->mark = true;
10b3939b
DJ
23884 if (cu->dependencies != NULL)
23885 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23886}
23887
23888static void
23889dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23890{
23891 while (per_cu)
23892 {
9068261f 23893 per_cu->cu->mark = false;
ae038cb0
DJ
23894 per_cu = per_cu->cu->read_in_chain;
23895 }
72bf9492
DJ
23896}
23897
72bf9492
DJ
23898/* Trivial hash function for partial_die_info: the hash value of a DIE
23899 is its offset in .debug_info for this objfile. */
23900
23901static hashval_t
23902partial_die_hash (const void *item)
23903{
9a3c8263
SM
23904 const struct partial_die_info *part_die
23905 = (const struct partial_die_info *) item;
9a619af0 23906
9c541725 23907 return to_underlying (part_die->sect_off);
72bf9492
DJ
23908}
23909
23910/* Trivial comparison function for partial_die_info structures: two DIEs
23911 are equal if they have the same offset. */
23912
23913static int
23914partial_die_eq (const void *item_lhs, const void *item_rhs)
23915{
9a3c8263
SM
23916 const struct partial_die_info *part_die_lhs
23917 = (const struct partial_die_info *) item_lhs;
23918 const struct partial_die_info *part_die_rhs
23919 = (const struct partial_die_info *) item_rhs;
9a619af0 23920
9c541725 23921 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23922}
23923
3c3bb058
AB
23924struct cmd_list_element *set_dwarf_cmdlist;
23925struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23926
9291a0cd 23927static void
cd4fb1b2
SM
23928show_check_physname (struct ui_file *file, int from_tty,
23929 struct cmd_list_element *c, const char *value)
9291a0cd 23930{
cd4fb1b2
SM
23931 fprintf_filtered (file,
23932 _("Whether to check \"physname\" is %s.\n"),
23933 value);
9291a0cd
TT
23934}
23935
6c265988 23936void _initialize_dwarf2_read ();
cd4fb1b2 23937void
6c265988 23938_initialize_dwarf2_read ()
9291a0cd 23939{
0743fc83 23940 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23941Set DWARF specific variables.\n\
590042fc 23942Configure DWARF variables such as the cache size."),
0743fc83
TT
23943 &set_dwarf_cmdlist, "maintenance set dwarf ",
23944 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23945
0743fc83 23946 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23947Show DWARF specific variables.\n\
23948Show DWARF variables such as the cache size."),
0743fc83
TT
23949 &show_dwarf_cmdlist, "maintenance show dwarf ",
23950 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23951
cd4fb1b2
SM
23952 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23953 &dwarf_max_cache_age, _("\
23954Set the upper bound on the age of cached DWARF compilation units."), _("\
23955Show the upper bound on the age of cached DWARF compilation units."), _("\
23956A higher limit means that cached compilation units will be stored\n\
23957in memory longer, and more total memory will be used. Zero disables\n\
23958caching, which can slow down startup."),
23959 NULL,
23960 show_dwarf_max_cache_age,
23961 &set_dwarf_cmdlist,
23962 &show_dwarf_cmdlist);
156942c7 23963
cd4fb1b2
SM
23964 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23965Set debugging of the DWARF reader."), _("\
23966Show debugging of the DWARF reader."), _("\
23967When enabled (non-zero), debugging messages are printed during DWARF\n\
23968reading and symtab expansion. A value of 1 (one) provides basic\n\
23969information. A value greater than 1 provides more verbose information."),
23970 NULL,
23971 NULL,
23972 &setdebuglist, &showdebuglist);
9291a0cd 23973
cd4fb1b2
SM
23974 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23975Set debugging of the DWARF DIE reader."), _("\
23976Show debugging of the DWARF DIE reader."), _("\
23977When enabled (non-zero), DIEs are dumped after they are read in.\n\
23978The value is the maximum depth to print."),
23979 NULL,
23980 NULL,
23981 &setdebuglist, &showdebuglist);
9291a0cd 23982
cd4fb1b2
SM
23983 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23984Set debugging of the dwarf line reader."), _("\
23985Show debugging of the dwarf line reader."), _("\
23986When enabled (non-zero), line number entries are dumped as they are read in.\n\
23987A value of 1 (one) provides basic information.\n\
23988A value greater than 1 provides more verbose information."),
23989 NULL,
23990 NULL,
23991 &setdebuglist, &showdebuglist);
437afbb8 23992
cd4fb1b2
SM
23993 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23994Set cross-checking of \"physname\" code against demangler."), _("\
23995Show cross-checking of \"physname\" code against demangler."), _("\
23996When enabled, GDB's internal \"physname\" code is checked against\n\
23997the demangler."),
23998 NULL, show_check_physname,
23999 &setdebuglist, &showdebuglist);
900e11f9 24000
e615022a
DE
24001 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24002 no_class, &use_deprecated_index_sections, _("\
24003Set whether to use deprecated gdb_index sections."), _("\
24004Show whether to use deprecated gdb_index sections."), _("\
24005When enabled, deprecated .gdb_index sections are used anyway.\n\
24006Normally they are ignored either because of a missing feature or\n\
24007performance issue.\n\
24008Warning: This option must be enabled before gdb reads the file."),
24009 NULL,
24010 NULL,
24011 &setlist, &showlist);
24012
f1e6e072
TT
24013 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24014 &dwarf2_locexpr_funcs);
24015 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24016 &dwarf2_loclist_funcs);
24017
24018 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24019 &dwarf2_block_frame_base_locexpr_funcs);
24020 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24021 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24022
24023#if GDB_SELF_TEST
24024 selftests::register_test ("dw2_expand_symtabs_matching",
24025 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24026 selftests::register_test ("dwarf2_find_containing_comp_unit",
24027 selftests::find_containing_comp_unit::run_test);
c62446b1 24028#endif
6502dd73 24029}
This page took 5.400842 seconds and 4 git commands to generate.