Pass dwarf2_per_bfd instead of dwarf2_per_objfile to some index-related functions
[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
293e7e51
SM
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
d00adf39 434 /* The header of the compilation unit. */
fcd3b13d 435 struct comp_unit_head header {};
e142c38c 436
d00adf39 437 /* Base address of this compilation unit. */
2b24b6e4 438 gdb::optional<CORE_ADDR> base_address;
d00adf39 439
e142c38c 440 /* The language we are debugging. */
fcd3b13d
SM
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
e142c38c 443
fcd3b13d 444 const char *producer = nullptr;
b0f35d58 445
c24bdb02 446private:
804d2729
TT
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
c24bdb02 449 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 450
c24bdb02 451public:
e142c38c
DJ
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
fcd3b13d 461 struct pending **list_in_scope = nullptr;
e142c38c 462
b64f50a1
JK
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 465 htab_t partial_dies = nullptr;
72bf9492
DJ
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
fcd3b13d 469 auto_obstack comp_unit_obstack;
72bf9492 470
ae038cb0
DJ
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
fcd3b13d 475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 476
69d751e3 477 /* Backlink to our per_cu entry. */
ae038cb0
DJ
478 struct dwarf2_per_cu_data *per_cu;
479
9e021579
SM
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
ae038cb0 483 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 484 int last_used = 0;
ae038cb0 485
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
fcd3b13d 488 htab_t die_hash = nullptr;
10b3939b
DJ
489
490 /* Full DIEs if read in. */
fcd3b13d 491 struct die_info *dies = nullptr;
10b3939b
DJ
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
fcd3b13d 496 htab_t dependencies = nullptr;
10b3939b 497
cb1df416 498 /* Header data from the line table, during full symbol processing. */
fcd3b13d 499 struct line_header *line_header = nullptr;
4c8aa72d 500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
fcd3b13d 505 die_info *line_header_die_owner = nullptr;
cb1df416 506
3da10d80
KS
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
c89b44cd 509 std::vector<delayed_method_info> method_list;
3da10d80 510
96408a79 511 /* To be copied to symtab->call_site_htab. */
fcd3b13d 512 htab_t call_site_htab = nullptr;
96408a79 513
034e5797
DE
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
18a8505e 518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
fcd3b13d 523 struct dwo_unit *dwo_unit = nullptr;
3019eac3 524
18a8505e 525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 527 gdb::optional<ULONGEST> addr_base;
3019eac3 528
18a8505e 529 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 530 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 531 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 538 ULONGEST ranges_base = 0;
2e3cf129 539
41144253 540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
c9317f21
TT
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
18a8505e
AT
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
ae038cb0 557 /* Mark used when releasing cached dies. */
9068261f 558 bool mark : 1;
ae038cb0 559
8be455d7
JK
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 564 bool has_loclist : 1;
ba919b58 565
9068261f 566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
9068261f
AB
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 573 bool producer_is_icc : 1;
9068261f 574 bool producer_is_icc_lt_14 : 1;
c258c396 575 bool producer_is_codewarrior : 1;
4d4ec4e5 576
9068261f 577 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
9068261f 581 bool processing_has_namespace_info : 1;
d590ff25
YQ
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
e7c27a73
DJ
602};
603
094b34ac
DE
604/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607struct stmt_list_hash
608{
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 613 sect_offset line_sect_off;
094b34ac
DE
614};
615
5989a64e 616/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
617 an object of this type. This contains elements of type unit groups
618 that can be shared across objfiles. The non-shareable parts are in
619 type_unit_group_unshareable. */
f4dc4d17
DE
620
621struct type_unit_group
622{
0186c6a7 623 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
624 To simplify things we create an artificial CU that "includes" all the
625 type units using this stmt_list so that the rest of the code still has
197400e8 626 a "per_cu" handle on the symtab. */
094b34ac
DE
627 struct dwarf2_per_cu_data per_cu;
628
0186c6a7
DE
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
a8b3b8e9 632 std::vector<signatured_type *> *tus;
f4dc4d17 633
094b34ac
DE
634 /* The data used to construct the hash key. */
635 struct stmt_list_hash hash;
f4dc4d17
DE
636};
637
73869dc2 638/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
639
640struct dwo_sections
641{
642 struct dwarf2_section_info abbrev;
3019eac3
DE
643 struct dwarf2_section_info line;
644 struct dwarf2_section_info loc;
43988095 645 struct dwarf2_section_info loclists;
09262596
DE
646 struct dwarf2_section_info macinfo;
647 struct dwarf2_section_info macro;
3019eac3
DE
648 struct dwarf2_section_info str;
649 struct dwarf2_section_info str_offsets;
80626a55
DE
650 /* In the case of a virtual DWO file, these two are unused. */
651 struct dwarf2_section_info info;
fd5866f6 652 std::vector<dwarf2_section_info> types;
3019eac3
DE
653};
654
c88ee1f0 655/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
656
657struct dwo_unit
658{
659 /* Backlink to the containing struct dwo_file. */
660 struct dwo_file *dwo_file;
661
662 /* The "id" that distinguishes this CU/TU.
663 .debug_info calls this "dwo_id", .debug_types calls this "signature".
664 Since signatures came first, we stick with it for consistency. */
665 ULONGEST signature;
666
667 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 668 struct dwarf2_section_info *section;
3019eac3 669
9c541725
PA
670 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
671 sect_offset sect_off;
3019eac3
DE
672 unsigned int length;
673
674 /* For types, offset in the type's DIE of the type defined by this TU. */
675 cu_offset type_offset_in_tu;
676};
677
73869dc2
DE
678/* include/dwarf2.h defines the DWP section codes.
679 It defines a max value but it doesn't define a min value, which we
680 use for error checking, so provide one. */
681
682enum dwp_v2_section_ids
683{
684 DW_SECT_MIN = 1
685};
686
80626a55 687/* Data for one DWO file.
57d63ce2
DE
688
689 This includes virtual DWO files (a virtual DWO file is a DWO file as it
690 appears in a DWP file). DWP files don't really have DWO files per se -
691 comdat folding of types "loses" the DWO file they came from, and from
692 a high level view DWP files appear to contain a mass of random types.
693 However, to maintain consistency with the non-DWP case we pretend DWP
694 files contain virtual DWO files, and we assign each TU with one virtual
695 DWO file (generally based on the line and abbrev section offsets -
696 a heuristic that seems to work in practice). */
3019eac3
DE
697
698struct dwo_file
699{
51ac9db5
SM
700 dwo_file () = default;
701 DISABLE_COPY_AND_ASSIGN (dwo_file);
702
18a8505e 703 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
704 For virtual DWO files the name is constructed from the section offsets
705 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
706 from related CU+TUs. */
51ac9db5 707 const char *dwo_name = nullptr;
0ac5b59e
DE
708
709 /* The DW_AT_comp_dir attribute. */
51ac9db5 710 const char *comp_dir = nullptr;
3019eac3 711
80626a55
DE
712 /* The bfd, when the file is open. Otherwise this is NULL.
713 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 714 gdb_bfd_ref_ptr dbfd;
3019eac3 715
73869dc2
DE
716 /* The sections that make up this DWO file.
717 Remember that for virtual DWO files in DWP V2, these are virtual
718 sections (for lack of a better name). */
51ac9db5 719 struct dwo_sections sections {};
3019eac3 720
33c5cd75
DB
721 /* The CUs in the file.
722 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
723 an extension to handle LLVM's Link Time Optimization output (where
724 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 725 htab_up cus;
3019eac3
DE
726
727 /* Table of TUs in the file.
728 Each element is a struct dwo_unit. */
b0b6a987 729 htab_up tus;
3019eac3
DE
730};
731
80626a55
DE
732/* These sections are what may appear in a DWP file. */
733
734struct dwp_sections
735{
73869dc2 736 /* These are used by both DWP version 1 and 2. */
80626a55
DE
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info cu_index;
739 struct dwarf2_section_info tu_index;
73869dc2
DE
740
741 /* These are only used by DWP version 2 files.
742 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
743 sections are referenced by section number, and are not recorded here.
744 In DWP version 2 there is at most one copy of all these sections, each
745 section being (effectively) comprised of the concatenation of all of the
746 individual sections that exist in the version 1 format.
747 To keep the code simple we treat each of these concatenated pieces as a
748 section itself (a virtual section?). */
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info info;
751 struct dwarf2_section_info line;
752 struct dwarf2_section_info loc;
753 struct dwarf2_section_info macinfo;
754 struct dwarf2_section_info macro;
755 struct dwarf2_section_info str_offsets;
756 struct dwarf2_section_info types;
80626a55
DE
757};
758
73869dc2
DE
759/* These sections are what may appear in a virtual DWO file in DWP version 1.
760 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 761
73869dc2 762struct virtual_v1_dwo_sections
80626a55
DE
763{
764 struct dwarf2_section_info abbrev;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info macinfo;
768 struct dwarf2_section_info macro;
769 struct dwarf2_section_info str_offsets;
770 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 771 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
772 struct dwarf2_section_info info_or_types;
773};
774
73869dc2
DE
775/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
776 In version 2, the sections of the DWO files are concatenated together
777 and stored in one section of that name. Thus each ELF section contains
778 several "virtual" sections. */
779
780struct virtual_v2_dwo_sections
781{
782 bfd_size_type abbrev_offset;
783 bfd_size_type abbrev_size;
784
785 bfd_size_type line_offset;
786 bfd_size_type line_size;
787
788 bfd_size_type loc_offset;
789 bfd_size_type loc_size;
790
791 bfd_size_type macinfo_offset;
792 bfd_size_type macinfo_size;
793
794 bfd_size_type macro_offset;
795 bfd_size_type macro_size;
796
797 bfd_size_type str_offsets_offset;
798 bfd_size_type str_offsets_size;
799
800 /* Each DWP hash table entry records one CU or one TU.
801 That is recorded here, and copied to dwo_unit.section. */
802 bfd_size_type info_or_types_offset;
803 bfd_size_type info_or_types_size;
804};
805
80626a55
DE
806/* Contents of DWP hash tables. */
807
808struct dwp_hash_table
809{
73869dc2 810 uint32_t version, nr_columns;
80626a55 811 uint32_t nr_units, nr_slots;
73869dc2
DE
812 const gdb_byte *hash_table, *unit_table;
813 union
814 {
815 struct
816 {
817 const gdb_byte *indices;
818 } v1;
819 struct
820 {
821 /* This is indexed by column number and gives the id of the section
822 in that column. */
823#define MAX_NR_V2_DWO_SECTIONS \
824 (1 /* .debug_info or .debug_types */ \
825 + 1 /* .debug_abbrev */ \
826 + 1 /* .debug_line */ \
827 + 1 /* .debug_loc */ \
828 + 1 /* .debug_str_offsets */ \
829 + 1 /* .debug_macro or .debug_macinfo */)
830 int section_ids[MAX_NR_V2_DWO_SECTIONS];
831 const gdb_byte *offsets;
832 const gdb_byte *sizes;
833 } v2;
834 } section_pool;
80626a55
DE
835};
836
837/* Data for one DWP file. */
838
839struct dwp_file
840{
400174b1
TT
841 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
842 : name (name_),
843 dbfd (std::move (abfd))
844 {
845 }
846
80626a55
DE
847 /* Name of the file. */
848 const char *name;
849
73869dc2 850 /* File format version. */
400174b1 851 int version = 0;
73869dc2 852
93417882 853 /* The bfd. */
400174b1 854 gdb_bfd_ref_ptr dbfd;
80626a55
DE
855
856 /* Section info for this file. */
400174b1 857 struct dwp_sections sections {};
80626a55 858
57d63ce2 859 /* Table of CUs in the file. */
400174b1 860 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
861
862 /* Table of TUs in the file. */
400174b1 863 const struct dwp_hash_table *tus = nullptr;
80626a55 864
19ac8c2e 865 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
866 htab_up loaded_cus;
867 htab_up loaded_tus;
80626a55 868
73869dc2
DE
869 /* Table to map ELF section numbers to their sections.
870 This is only needed for the DWP V1 file format. */
400174b1
TT
871 unsigned int num_sections = 0;
872 asection **elf_sections = nullptr;
80626a55
DE
873};
874
0963b4bd
MS
875/* Struct used to pass misc. parameters to read_die_and_children, et
876 al. which are used for both .debug_info and .debug_types dies.
877 All parameters here are unchanging for the life of the call. This
dee91e82 878 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
879
880struct die_reader_specs
881{
a32a8923 882 /* The bfd of die_section. */
93311388
DE
883 bfd* abfd;
884
885 /* The CU of the DIE we are parsing. */
886 struct dwarf2_cu *cu;
887
80626a55 888 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
889 struct dwo_file *dwo_file;
890
dee91e82 891 /* The section the die comes from.
3019eac3 892 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
893 struct dwarf2_section_info *die_section;
894
895 /* die_section->buffer. */
d521ce57 896 const gdb_byte *buffer;
f664829e
DE
897
898 /* The end of the buffer. */
899 const gdb_byte *buffer_end;
a2ce51a0 900
685af9cd
TT
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
c0ab21c2
TT
905/* A subclass of die_reader_specs that holds storage and has complex
906 constructor and destructor behavior. */
907
908class cutu_reader : public die_reader_specs
909{
910public:
911
ab432490
SM
912 cutu_reader (dwarf2_per_cu_data *this_cu,
913 dwarf2_per_objfile *per_objfile,
c0ab21c2 914 struct abbrev_table *abbrev_table,
6751ebae 915 int use_existing_cu,
c0ab21c2
TT
916 bool skip_partial);
917
918 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 919 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
920 struct dwarf2_cu *parent_cu = nullptr,
921 struct dwo_file *dwo_file = nullptr);
922
c0ab21c2
TT
923 DISABLE_COPY_AND_ASSIGN (cutu_reader);
924
925 const gdb_byte *info_ptr = nullptr;
926 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
927 bool dummy_p = false;
928
6751ebae
TT
929 /* Release the new CU, putting it on the chain. This cannot be done
930 for dummy CUs. */
931 void keep ();
932
c0ab21c2 933private:
9e021579
SM
934 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
935 dwarf2_per_objfile *per_objfile,
6751ebae 936 int use_existing_cu);
c0ab21c2
TT
937
938 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
939 std::unique_ptr<dwarf2_cu> m_new_cu;
940
941 /* The ordinary abbreviation table. */
942 abbrev_table_up m_abbrev_table_holder;
943
944 /* The DWO abbreviation table. */
945 abbrev_table_up m_dwo_abbrev_table;
946};
dee91e82 947
c906108c 948/* When we construct a partial symbol table entry we only
0963b4bd 949 need this much information. */
6f06d47b 950struct partial_die_info : public allocate_on_obstack
c906108c 951 {
6f06d47b
YQ
952 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
953
954 /* Disable assign but still keep copy ctor, which is needed
955 load_partial_dies. */
956 partial_die_info& operator=(const partial_die_info& rhs) = delete;
957
52356b79
YQ
958 /* Adjust the partial die before generating a symbol for it. This
959 function may set the is_external flag or change the DIE's
960 name. */
961 void fixup (struct dwarf2_cu *cu);
962
48fbe735
YQ
963 /* Read a minimal amount of information into the minimal die
964 structure. */
965 const gdb_byte *read (const struct die_reader_specs *reader,
966 const struct abbrev_info &abbrev,
967 const gdb_byte *info_ptr);
968
72bf9492 969 /* Offset of this DIE. */
6f06d47b 970 const sect_offset sect_off;
72bf9492
DJ
971
972 /* DWARF-2 tag for this DIE. */
6f06d47b 973 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 974
72bf9492 975 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
976 const unsigned int has_children : 1;
977
72bf9492
DJ
978 unsigned int is_external : 1;
979 unsigned int is_declaration : 1;
980 unsigned int has_type : 1;
981 unsigned int has_specification : 1;
982 unsigned int has_pc_info : 1;
481860b3 983 unsigned int may_be_inlined : 1;
72bf9492 984
0c1b455e
TT
985 /* This DIE has been marked DW_AT_main_subprogram. */
986 unsigned int main_subprogram : 1;
987
72bf9492
DJ
988 /* Flag set if the SCOPE field of this structure has been
989 computed. */
990 unsigned int scope_set : 1;
991
fa4028e9
JB
992 /* Flag set if the DIE has a byte_size attribute. */
993 unsigned int has_byte_size : 1;
994
ff908ebf
AW
995 /* Flag set if the DIE has a DW_AT_const_value attribute. */
996 unsigned int has_const_value : 1;
997
98bfdba5
PA
998 /* Flag set if any of the DIE's children are template arguments. */
999 unsigned int has_template_arguments : 1;
1000
52356b79 1001 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1002 unsigned int fixup_called : 1;
1003
36586728
TT
1004 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1005 unsigned int is_dwz : 1;
1006
1007 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1008 unsigned int spec_is_dwz : 1;
1009
72bf9492 1010 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1011 sometimes a default name for unnamed DIEs. */
6f06d47b 1012 const char *name = nullptr;
72bf9492 1013
abc72ce4 1014 /* The linkage name, if present. */
6f06d47b 1015 const char *linkage_name = nullptr;
abc72ce4 1016
72bf9492
DJ
1017 /* The scope to prepend to our children. This is generally
1018 allocated on the comp_unit_obstack, so will disappear
1019 when this compilation unit leaves the cache. */
6f06d47b 1020 const char *scope = nullptr;
72bf9492 1021
95554aad
TT
1022 /* Some data associated with the partial DIE. The tag determines
1023 which field is live. */
1024 union
1025 {
1026 /* The location description associated with this DIE, if any. */
1027 struct dwarf_block *locdesc;
1028 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1029 sect_offset sect_off;
6f06d47b 1030 } d {};
72bf9492
DJ
1031
1032 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1033 CORE_ADDR lowpc = 0;
1034 CORE_ADDR highpc = 0;
72bf9492 1035
93311388 1036 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1037 DW_AT_sibling, if any. */
48fbe735
YQ
1038 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1039 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1040 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1041
1042 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1043 DW_AT_specification (or DW_AT_abstract_origin or
1044 DW_AT_extension). */
6f06d47b 1045 sect_offset spec_offset {};
72bf9492
DJ
1046
1047 /* Pointers to this DIE's parent, first child, and next sibling,
1048 if any. */
6f06d47b
YQ
1049 struct partial_die_info *die_parent = nullptr;
1050 struct partial_die_info *die_child = nullptr;
1051 struct partial_die_info *die_sibling = nullptr;
1052
1053 friend struct partial_die_info *
1054 dwarf2_cu::find_partial_die (sect_offset sect_off);
1055
1056 private:
1057 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1058 partial_die_info (sect_offset sect_off)
1059 : partial_die_info (sect_off, DW_TAG_padding, 0)
1060 {
1061 }
1062
1063 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1064 int has_children_)
1065 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1066 {
1067 is_external = 0;
1068 is_declaration = 0;
1069 has_type = 0;
1070 has_specification = 0;
1071 has_pc_info = 0;
1072 may_be_inlined = 0;
1073 main_subprogram = 0;
1074 scope_set = 0;
1075 has_byte_size = 0;
1076 has_const_value = 0;
1077 has_template_arguments = 0;
1078 fixup_called = 0;
1079 is_dwz = 0;
1080 spec_is_dwz = 0;
1081 }
c906108c
SS
1082 };
1083
c906108c
SS
1084/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1085 but this would require a corresponding change in unpack_field_as_long
1086 and friends. */
1087static int bits_per_byte = 8;
1088
9c6a1327
TT
1089struct variant_part_builder;
1090
1091/* When reading a variant, we track a bit more information about the
1092 field, and store it in an object of this type. */
2ddeaf8a
TT
1093
1094struct variant_field
1095{
9c6a1327
TT
1096 int first_field = -1;
1097 int last_field = -1;
1098
1099 /* A variant can contain other variant parts. */
1100 std::vector<variant_part_builder> variant_parts;
1101
2ddeaf8a
TT
1102 /* If we see a DW_TAG_variant, then this will be set if this is the
1103 default branch. */
9c6a1327
TT
1104 bool default_branch = false;
1105 /* If we see a DW_AT_discr_value, then this will be the discriminant
1106 value. */
1107 ULONGEST discriminant_value = 0;
1108 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1109 data. */
1110 struct dwarf_block *discr_list_data = nullptr;
1111};
1112
1113/* This represents a DW_TAG_variant_part. */
1114
1115struct variant_part_builder
1116{
1117 /* The offset of the discriminant field. */
1118 sect_offset discriminant_offset {};
1119
1120 /* Variants that are direct children of this variant part. */
1121 std::vector<variant_field> variants;
1122
1123 /* True if we're currently reading a variant. */
1124 bool processing_variant = false;
2ddeaf8a
TT
1125};
1126
52059ffd
TT
1127struct nextfield
1128{
be2daae6
TT
1129 int accessibility = 0;
1130 int virtuality = 0;
9c6a1327
TT
1131 /* Variant parts need to find the discriminant, which is a DIE
1132 reference. We track the section offset of each field to make
1133 this link. */
1134 sect_offset offset;
be2daae6 1135 struct field field {};
52059ffd
TT
1136};
1137
1138struct fnfieldlist
1139{
be2daae6
TT
1140 const char *name = nullptr;
1141 std::vector<struct fn_field> fnfields;
52059ffd
TT
1142};
1143
c906108c
SS
1144/* The routines that read and process dies for a C struct or C++ class
1145 pass lists of data member fields and lists of member function fields
1146 in an instance of a field_info structure, as defined below. */
1147struct field_info
c5aa993b 1148 {
0963b4bd 1149 /* List of data member and baseclasses fields. */
be2daae6
TT
1150 std::vector<struct nextfield> fields;
1151 std::vector<struct nextfield> baseclasses;
c906108c 1152
85102364 1153 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1154 int non_public_fields = 0;
c906108c 1155
c5aa993b
JM
1156 /* Member function fieldlist array, contains name of possibly overloaded
1157 member function, number of overloaded member functions and a pointer
1158 to the head of the member function field chain. */
be2daae6 1159 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1160
1161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1162 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1163 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1164
1165 /* Nested types defined by this class and the number of elements in this
1166 list. */
be2daae6 1167 std::vector<struct decl_field> nested_types_list;
317f7127 1168
9c6a1327
TT
1169 /* If non-null, this is the variant part we are currently
1170 reading. */
1171 variant_part_builder *current_variant_part = nullptr;
1172 /* This holds all the top-level variant parts attached to the type
1173 we're reading. */
1174 std::vector<variant_part_builder> variant_parts;
1175
317f7127
TT
1176 /* Return the total number of fields (including baseclasses). */
1177 int nfields () const
1178 {
1179 return fields.size () + baseclasses.size ();
1180 }
c5aa993b 1181 };
c906108c 1182
ae038cb0
DJ
1183/* Loaded secondary compilation units are kept in memory until they
1184 have not been referenced for the processing of this many
1185 compilation units. Set this to zero to disable caching. Cache
1186 sizes of up to at least twenty will improve startup time for
1187 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1188static int dwarf_max_cache_age = 5;
920d2a44 1189static void
b4f54984
DE
1190show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1191 struct cmd_list_element *c, const char *value)
920d2a44 1192{
3e43a32a 1193 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1194 "DWARF compilation units is %s.\n"),
920d2a44
AC
1195 value);
1196}
4390d890 1197\f
c906108c
SS
1198/* local function prototypes */
1199
918dd910
JK
1200static void dwarf2_find_base_address (struct die_info *die,
1201 struct dwarf2_cu *cu);
1202
891813be 1203static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1204 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1205 const char *name);
0018ea6f 1206
f1902523
JK
1207static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
3e225074 1209 struct die_info *type_unit_die);
f1902523 1210
ed2dc618
SM
1211static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1213
72bf9492
DJ
1214static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1216 int, struct dwarf2_cu *);
c906108c 1217
72bf9492
DJ
1218static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
63d06c5c 1220
72bf9492
DJ
1221static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1223 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1224
5d7cb8df 1225static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1226 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1227 struct dwarf2_cu *cu);
1228
72bf9492
DJ
1229static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
91c24f0a 1231
bc30ff58
JB
1232static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1234 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1235
d521ce57 1236static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1237
dee91e82 1238static struct partial_die_info *load_partial_dies
d521ce57 1239 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1240
fb816e8b
TV
1241/* A pair of partial_die_info and compilation unit. */
1242struct cu_partial_die_info
1243{
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
122cf0f2
AB
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
405feb71 1252 { /* Nothing. */ }
122cf0f2
AB
1253
1254private:
1255 cu_partial_die_info () = delete;
fb816e8b
TV
1256};
1257
122cf0f2
AB
1258static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
72bf9492 1260
d521ce57
TT
1261static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1263 const gdb_byte *, bool *need_reprocess);
1264
1265static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1269
ed2dc618
SM
1270static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1273
ed2dc618
SM
1274static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1277
ed2dc618 1278static const char *read_indirect_string_at_offset
4f44ae6c 1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1280
d521ce57
TT
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
3019eac3
DE
1283 unsigned int *);
1284
18a8505e
AT
1285static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
3019eac3 1290
e142c38c 1291static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1292
e142c38c
DJ
1293static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
c906108c 1295
7d45c7c3
KB
1296static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
a084a2a6
AT
1299static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
05cf31d1
JB
1301static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
e142c38c 1304static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1305
e142c38c 1306static struct die_info *die_specification (struct die_info *die,
f2f0e013 1307 struct dwarf2_cu **);
63d06c5c 1308
9c541725 1309static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1310 struct dwarf2_cu *cu);
debd256d 1311
f3f5162e 1312static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1313 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1314 CORE_ADDR, int decode_mapping);
c906108c 1315
804d2729
TT
1316static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
c906108c 1318
a14ed312 1319static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1320 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1321
ff39bb5e 1322static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1323 struct dwarf2_cu *);
c906108c 1324
ff39bb5e 1325static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
12df843f 1329 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1330 const gdb_byte **bytes,
98bfdba5 1331 struct dwarf2_locexpr_baton **baton);
2df3850c 1332
e7c27a73 1333static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1334
b4ba55a1
JB
1335static int need_gnat_info (struct dwarf2_cu *);
1336
3e43a32a
MS
1337static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
b4ba55a1
JB
1339
1340static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
e7c27a73
DJ
1343static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
c906108c 1345
ff39bb5e 1346static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1347 struct dwarf2_cu *);
c906108c 1348
f792889a 1349static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1350
673bfd45
DE
1351static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
0d5cff50 1353static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1354
6e70227d 1355static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
63d06c5c 1358
e7c27a73 1359static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1360
348e048f
DE
1361static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
e7c27a73 1363static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1364
e7c27a73 1365static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1366
96408a79
SA
1367static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
71a3c369
TT
1369static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
ff013f42 1371static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1372 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1373
41144253 1374/* Return the .debug_loclists section to use for cu. */
1375static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
3a2b436a 1377/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1378 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1379enum pc_bounds_kind
1380{
e385593e 1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1382 PC_BOUNDS_NOT_PRESENT,
1383
e385593e
JK
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
3a2b436a
JK
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393};
1394
1395static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
891813be 1398 dwarf2_psymtab *);
c906108c 1399
fae299cd
DC
1400static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
801e3a5b
JB
1404static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
a14ed312 1407static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
a14ed312 1410static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1411 struct type *, struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1414 struct die_info *, struct type *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
a14ed312 1417static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1418 struct type *,
1419 struct dwarf2_cu *);
c906108c 1420
134d01f1 1421static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1424
e7c27a73 1425static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1426
5d7cb8df
JK
1427static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
804d2729 1429static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1430
27aa8d6a
SW
1431static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
74921315
KS
1433static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
f55ee35c
JK
1435static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
38d518c9 1438static const char *namespace_name (struct die_info *die,
e142c38c 1439 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1440
134d01f1 1441static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1442
7d79de9a
TT
1443static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
c906108c 1445
6e70227d 1446static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1447 struct dwarf2_cu *);
1448
bf6af496 1449static struct die_info *read_die_and_siblings_1
d521ce57 1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1451 struct die_info *);
639d11d3 1452
dee91e82 1453static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
639d11d3
DC
1456 struct die_info *parent);
1457
d521ce57
TT
1458static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
3e225074 1460 int);
3019eac3 1461
d521ce57 1462static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1463 struct die_info **, const gdb_byte *);
93311388 1464
e7c27a73 1465static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1466
15d034d0 1467static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1468 struct objfile *);
71c25dea 1469
15d034d0 1470static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1471
15d034d0 1472static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
ca69b9e6
DE
1476static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
e142c38c 1479static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1480 struct dwarf2_cu **);
9219021c 1481
d97bc12b
DE
1482static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484static void dump_die_for_error (struct die_info *);
1485
1486static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
c906108c 1488
d97bc12b 1489/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1490
51545339 1491static void store_in_ref_table (struct die_info *,
10b3939b 1492 struct dwarf2_cu *);
c906108c 1493
348e048f 1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1495 const struct attribute *,
348e048f
DE
1496 struct dwarf2_cu **);
1497
10b3939b 1498static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1499 const struct attribute *,
f2f0e013 1500 struct dwarf2_cu **);
c906108c 1501
348e048f 1502static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1503 const struct attribute *,
348e048f
DE
1504 struct dwarf2_cu **);
1505
ac9ec31b
DE
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1510 const struct attribute *,
ac9ec31b
DE
1511 struct dwarf2_cu *);
1512
ab432490
SM
1513static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1514 dwarf2_per_objfile *per_objfile);
348e048f 1515
ab432490
SM
1516static void read_signatured_type (signatured_type *sig_type,
1517 dwarf2_per_objfile *per_objfile);
348e048f 1518
63e43d3a
PMR
1519static int attr_to_dynamic_prop (const struct attribute *attr,
1520 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1521 struct dynamic_prop *prop, struct type *type);
63e43d3a 1522
c906108c
SS
1523/* memory allocation interface */
1524
7b5a2f43 1525static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1526
b60c80d6 1527static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1528
43f3e411 1529static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1530
8cf6f0b1
TT
1531static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1532 struct dwarf2_loclist_baton *baton,
ff39bb5e 1533 const struct attribute *attr);
8cf6f0b1 1534
ff39bb5e 1535static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1536 struct symbol *sym,
f1e6e072
TT
1537 struct dwarf2_cu *cu,
1538 int is_block);
4c2df51b 1539
d521ce57
TT
1540static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct abbrev_info *abbrev);
4bb7a0a7 1543
72bf9492
DJ
1544static hashval_t partial_die_hash (const void *item);
1545
1546static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1547
ae038cb0 1548static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1549 (sect_offset sect_off, unsigned int offset_in_dwz,
1550 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1551
9816fde3 1552static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1553 struct die_info *comp_unit_die,
1554 enum language pretend_language);
93311388 1555
ed2dc618 1556static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
120ce1b5
SM
1558static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1559 dwarf2_per_objfile *per_objfile);
ae038cb0 1560
f792889a
DJ
1561static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1c379e20 1563
ed2dc618 1564static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1565
ed2dc618 1566static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1567
ab432490
SM
1568static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
10b3939b 1572
47b14e86
SM
1573static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 enum language pretend_language);
10b3939b 1576
47b14e86
SM
1577static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1578 dwarf2_per_objfile *per_objfile,
1579 enum language pretend_language);
f4dc4d17 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,
aa66c379
SM
1589 dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile);
673bfd45 1591
f792889a 1592static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1593
120ce1b5
SM
1594static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1595 dwarf2_per_objfile *per_objfile,
95554aad
TT
1596 enum language pretend_language);
1597
ed2dc618 1598static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1599
b303c6f6
AB
1600/* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605class dwarf2_queue_guard
1606{
1607public:
39856def
TT
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
b303c6f6
AB
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
39856def
TT
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
5989a64e 1619 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1620 }
b303c6f6 1621
39856def 1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1623
39856def
TT
1624private:
1625 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1626};
1627
39856def
TT
1628dwarf2_queue_item::~dwarf2_queue_item ()
1629{
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
120ce1b5 1635 free_one_cached_comp_unit (per_cu, per_objfile);
39856def
TT
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1669 ULONGEST signature, int is_debug_types);
a2ce51a0 1670
ed2dc618
SM
1671static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1673
3019eac3 1674static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
3019eac3
DE
1677
1678static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1680
89e63ee4
DE
1681static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
263db9a1
TT
1683/* A unique pointer to a dwo_file. */
1684
51ac9db5 1685typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1686
ed2dc618 1687static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1688
1b80a9fa 1689static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1690
1691static void free_line_header_voidp (void *arg);
4390d890
DE
1692\f
1693/* Various complaints about symbol reading that don't abort the process. */
1694
4390d890
DE
1695static void
1696dwarf2_debug_line_missing_file_complaint (void)
1697{
b98664d3 1698 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1699}
1700
1701static void
1702dwarf2_debug_line_missing_end_sequence_complaint (void)
1703{
b98664d3 1704 complaint (_(".debug_line section has line "
4390d890
DE
1705 "program sequence without an end"));
1706}
1707
1708static void
1709dwarf2_complex_location_expr_complaint (void)
1710{
b98664d3 1711 complaint (_("location expression too complex"));
4390d890
DE
1712}
1713
1714static void
1715dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717{
b98664d3 1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1719 arg1, arg2, arg3);
1720}
1721
4390d890
DE
1722static void
1723dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724{
b98664d3 1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1726 arg1, arg2);
1727}
527f3840
JK
1728
1729/* Hash function for line_header_hash. */
1730
1731static hashval_t
1732line_header_hash (const struct line_header *ofs)
1733{
9c541725 1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1735}
1736
1737/* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash_voidp (const void *item)
1741{
9a3c8263 1742 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1743
1744 return line_header_hash (ofs);
1745}
1746
1747/* Equality function for line_header_hash. */
1748
1749static int
1750line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751{
9a3c8263
SM
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1754
9c541725 1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757}
1758
4390d890 1759\f
9291a0cd 1760
330cdd98
PA
1761/* See declaration. */
1762
5989a64e
SM
1763dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
c3699833
SM
1765 : obfd (obfd),
1766 can_copy (can_copy_)
330cdd98
PA
1767{
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
330cdd98
PA
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773}
1774
5989a64e 1775dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1776{
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
b76e467d 1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1781 per_cu->imported_symtabs_free ();
fc8e7e75 1782
b2bdb8cf 1783 for (signatured_type *sig_type : all_type_units)
ae640021 1784 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1785
5989a64e 1786 /* Everything else should be on this->obstack. */
330cdd98
PA
1787}
1788
1789/* See declaration. */
1790
1791void
5989a64e 1792dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1793{
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
fcd3b13d 1800 delete per_cu->cu;
330cdd98
PA
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804}
1805
11ed8cad
TT
1806/* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809class free_cached_comp_units
1810{
1811public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
5989a64e 1820 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828};
1829
af758d11
SM
1830/* See read.h. */
1831
1832bool
1833dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838}
1839
1840/* See read.h. */
1841
1842compunit_symtab *
1843dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848}
1849
1850/* See read.h. */
1851
1852void
1853dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855{
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860}
1861
c906108c 1862/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
c906108c
SS
1868
1869int
251d32d9 1870dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
c906108c 1873{
97cbe998
SDJ
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
ed2dc618
SM
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
5bfd760d 1888
5989a64e
SM
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1893}
1894
251d32d9
TG
1895/* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
233a11ab
CS
1897
1898static int
251d32d9
TG
1899section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
233a11ab 1901{
251d32d9
TG
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
233a11ab
CS
1909}
1910
330cdd98 1911/* See declaration. */
c906108c 1912
330cdd98 1913void
5989a64e
SM
1914dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
c906108c 1916{
fd361982 1917 flagword aflag = bfd_section_flags (sectp);
251d32d9 1918
dc7650b8
JK
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
950b7495
KS
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.info))
c906108c 1932 {
330cdd98 1933 this->info.s.section = sectp;
fd361982 1934 this->info.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1937 {
330cdd98 1938 this->abbrev.s.section = sectp;
fd361982 1939 this->abbrev.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.line))
c906108c 1942 {
330cdd98 1943 this->line.s.section = sectp;
fd361982 1944 this->line.size = bfd_section_size (sectp);
c906108c 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.loc))
c906108c 1947 {
330cdd98 1948 this->loc.s.section = sectp;
fd361982 1949 this->loc.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.loclists))
43988095 1952 {
330cdd98 1953 this->loclists.s.section = sectp;
fd361982 1954 this->loclists.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1957 {
330cdd98 1958 this->macinfo.s.section = sectp;
fd361982 1959 this->macinfo.size = bfd_section_size (sectp);
c906108c 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1962 {
330cdd98 1963 this->macro.s.section = sectp;
fd361982 1964 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.str))
c906108c 1967 {
330cdd98 1968 this->str.s.section = sectp;
fd361982 1969 this->str.size = bfd_section_size (sectp);
c906108c 1970 }
18a8505e
AT
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.line_str))
43988095 1977 {
330cdd98 1978 this->line_str.s.section = sectp;
fd361982 1979 this->line_str.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1982 {
330cdd98 1983 this->addr.s.section = sectp;
fd361982 1984 this->addr.size = bfd_section_size (sectp);
3019eac3 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1987 {
330cdd98 1988 this->frame.s.section = sectp;
fd361982 1989 this->frame.size = bfd_section_size (sectp);
b6af0555 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1992 {
330cdd98 1993 this->eh_frame.s.section = sectp;
fd361982 1994 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1997 {
330cdd98 1998 this->ranges.s.section = sectp;
fd361982 1999 this->ranges.size = bfd_section_size (sectp);
af34e669 2000 }
330cdd98 2001 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2002 {
330cdd98 2003 this->rnglists.s.section = sectp;
fd361982 2004 this->rnglists.size = bfd_section_size (sectp);
43988095 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.types))
348e048f 2007 {
8b70b953
TT
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
049412e3 2011 type_section.s.section = sectp;
fd361982 2012 type_section.size = bfd_section_size (sectp);
8b70b953 2013
fd5866f6 2014 this->types.push_back (type_section);
348e048f 2015 }
330cdd98 2016 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2017 {
330cdd98 2018 this->gdb_index.s.section = sectp;
fd361982 2019 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2020 }
927aa2e7
JK
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
fd361982 2024 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
fd361982 2029 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2030 }
dce234bc 2031
fd361982
AM
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
330cdd98 2034 this->has_section_at_zero = true;
c906108c
SS
2035}
2036
dce234bc 2037/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2038 SECTION_NAME. */
af34e669 2039
dce234bc 2040void
3017a003
TG
2041dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
d521ce57 2043 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2044 bfd_size_type *sizep)
2045{
5bfd760d 2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2047 struct dwarf2_section_info *info;
a3b2a86b
TT
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
3017a003
TG
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
5989a64e 2061 info = &data->per_bfd->frame;
3017a003
TG
2062 break;
2063 case DWARF2_EH_FRAME:
5989a64e 2064 info = &data->per_bfd->eh_frame;
3017a003
TG
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
dce234bc 2069
96b79293 2070 info->read (objfile);
dce234bc 2071
96b79293 2072 *sectp = info->get_bfd_section ();
dce234bc
PP
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075}
2076
36586728
TT
2077/* A helper function to find the sections for a .dwz file. */
2078
2079static void
2080locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081{
9a3c8263 2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
049412e3 2088 dwz_file->abbrev.s.section = sectp;
fd361982 2089 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
049412e3 2093 dwz_file->info.s.section = sectp;
fd361982 2094 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
049412e3 2098 dwz_file->str.s.section = sectp;
fd361982 2099 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
049412e3 2103 dwz_file->line.s.section = sectp;
fd361982 2104 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
049412e3 2108 dwz_file->macro.s.section = sectp;
fd361982 2109 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2110 }
2ec9a5e0
TT
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
049412e3 2113 dwz_file->gdb_index.s.section = sectp;
fd361982 2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2115 }
927aa2e7
JK
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
fd361982 2119 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2120 }
36586728
TT
2121}
2122
c4973306 2123/* See dwarf2read.h. */
36586728 2124
c4973306 2125struct dwz_file *
c3699833 2126dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2127{
36586728 2128 const char *filename;
acd13123 2129 bfd_size_type buildid_len_arg;
dc294be5
TT
2130 size_t buildid_len;
2131 bfd_byte *buildid;
36586728 2132
c3699833
SM
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
36586728 2135
4db1a1dc 2136 bfd_set_error (bfd_error_no_error);
791afaa2 2137 gdb::unique_xmalloc_ptr<char> data
c3699833 2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2139 &buildid_len_arg, &buildid));
4db1a1dc
TT
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
791afaa2
TT
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2149
acd13123
TT
2150 buildid_len = (size_t) buildid_len_arg;
2151
791afaa2 2152 filename = data.get ();
d721ba37
PA
2153
2154 std::string abs_storage;
36586728
TT
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
14278e1f 2157 gdb::unique_xmalloc_ptr<char> abs
c3699833 2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2159
14278e1f 2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2161 filename = abs_storage.c_str ();
36586728
TT
2162 }
2163
dc294be5
TT
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
ad80db5b 2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2167 if (dwz_bfd != NULL)
36586728 2168 {
192b62ce 2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2170 dwz_bfd.reset (nullptr);
36586728
TT
2171 }
2172
dc294be5
TT
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
0d79cdc4
AM
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2179 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
ad80db5b 2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
dc294be5
TT
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2201 bfd_get_filename (per_bfd->obfd));
dc294be5 2202
7ff8cb8c
TT
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2205
7ff8cb8c
TT
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
36586728 2208
c3699833
SM
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
36586728 2212}
9291a0cd 2213\f
7b9f3c50
DE
2214/* DWARF quick_symbols_functions support. */
2215
2216/* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221struct quick_file_names
2222{
094b34ac
DE
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
7b9f3c50
DE
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236};
2237
2238/* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241struct dwarf2_per_cu_quick_data
2242{
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
5989a64e 2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2246 struct quick_file_names *file_names;
2247
7b9f3c50
DE
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255};
2256
094b34ac
DE
2257/* Utility hash function for a stmt_list_hash. */
2258
2259static hashval_t
2260hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261{
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2266 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2267 return v;
2268}
2269
2270/* Utility equality function for a stmt_list_hash. */
2271
2272static int
2273eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275{
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
9c541725 2282 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2283}
2284
7b9f3c50
DE
2285/* Hash function for a quick_file_names. */
2286
2287static hashval_t
2288hash_file_name_entry (const void *e)
2289{
9a3c8263
SM
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
7b9f3c50 2292
094b34ac 2293 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2294}
2295
2296/* Equality function for a quick_file_names. */
2297
2298static int
2299eq_file_name_entry (const void *a, const void *b)
2300{
9a3c8263
SM
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2303
094b34ac 2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2305}
2306
2307/* Delete function for a quick_file_names. */
2308
2309static void
2310delete_file_name_entry (void *e)
2311{
9a3c8263 2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
45940949
TT
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
7b9f3c50
DE
2324}
2325
2326/* Create a quick_file_names hash table. */
2327
5895093f 2328static htab_up
7b9f3c50
DE
2329create_quick_file_names_table (unsigned int nr_initial_entries)
2330{
5895093f
TT
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2334}
9291a0cd 2335
ab432490
SM
2336/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2339
2340static void
ab432490
SM
2341load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
918dd910 2343{
3019eac3 2344 if (per_cu->is_debug_types)
ab432490 2345 load_full_type_unit (per_cu, per_objfile);
918dd910 2346 else
ab432490 2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2348
cc12ce38
DE
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2dc860c0
DE
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2353}
2354
97a1449a 2355/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2356
9291a0cd 2357static void
97a1449a
SM
2358dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
9291a0cd 2361{
f4dc4d17
DE
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
197400e8 2364 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2365 return;
2366
b303c6f6
AB
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
39856def 2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2371
af758d11 2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2373 {
120ce1b5 2374 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
ab432490 2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
cc12ce38 2381 && per_cu->cu != NULL
89e63ee4 2382 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2385 /* DWP files aren't supported yet. */
ed2dc618 2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2387 queue_and_load_all_dwo_tus (per_cu);
95554aad 2388 }
9291a0cd 2389
ed2dc618 2390 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
ed2dc618 2394 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2395}
2396
97a1449a
SM
2397/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2fdf6df6 2401
43f3e411 2402static struct compunit_symtab *
97a1449a
SM
2403dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
9291a0cd 2406{
5989a64e 2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2410 {
11ed8cad 2411 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2412 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2414 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2415 }
f194fefb 2416
af758d11 2417 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2418}
2419
ff4c9fec 2420/* See declaration. */
f4dc4d17 2421
ff4c9fec 2422dwarf2_per_cu_data *
5989a64e 2423dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2424{
b76e467d 2425 if (index >= this->all_comp_units.size ())
ff4c9fec 2426 {
b76e467d 2427 index -= this->all_comp_units.size ();
b2bdb8cf 2428 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2429 return &this->all_type_units[index]->per_cu;
2430 }
f4dc4d17 2431
ff4c9fec
SM
2432 return this->all_comp_units[index];
2433}
f4dc4d17 2434
ff4c9fec 2435/* See declaration. */
2fdf6df6 2436
ff4c9fec 2437dwarf2_per_cu_data *
5989a64e 2438dwarf2_per_bfd::get_cu (int index)
1fd400ff 2439{
b76e467d 2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2441
ff4c9fec 2442 return this->all_comp_units[index];
f4dc4d17
DE
2443}
2444
ff4c9fec 2445/* See declaration. */
f4dc4d17 2446
ff4c9fec 2447signatured_type *
5989a64e 2448dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2449{
b2bdb8cf 2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_type_units[index];
1fd400ff
TT
2453}
2454
d3473f0c
TT
2455/* See read.h. */
2456
2457dwarf2_per_cu_data *
5989a64e 2458dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2459{
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2461 result->per_bfd = this;
d3473f0c
TT
2462 result->index = m_num_psymtabs++;
2463 return result;
2464}
2465
2466/* See read.h. */
2467
2468signatured_type *
5989a64e 2469dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2470{
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2472 result->per_cu.per_bfd = this;
d3473f0c
TT
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475}
2476
168c9250 2477/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2478 obstack, and constructed with the specified field values. */
4b514bc8
JK
2479
2480static dwarf2_per_cu_data *
168c9250
SM
2481create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
4b514bc8 2485{
168c9250 2486 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
4b514bc8 2489 the_cu->section = section;
168c9250 2490 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2491 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494}
2495
2ec9a5e0
TT
2496/* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2fdf6df6 2498
74a0d9f6 2499static void
168c9250 2500create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
b76e467d 2503 int is_dwz)
9291a0cd 2504{
12359b5e 2505 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2506 {
74a0d9f6 2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2512 cu_list += 2 * 8;
2513
b76e467d 2514 dwarf2_per_cu_data *per_cu
168c9250
SM
2515 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2516 length);
2517 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2518 }
9291a0cd
TT
2519}
2520
2ec9a5e0 2521/* Read the CU list from the mapped index, and use it to create all
168c9250 2522 the CU objects for PER_BFD. */
2ec9a5e0 2523
74a0d9f6 2524static void
168c9250 2525create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528{
168c9250
SM
2529 gdb_assert (per_bfd->all_comp_units.empty ());
2530 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2531
168c9250
SM
2532 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2533 &per_bfd->info, 0);
2ec9a5e0
TT
2534
2535 if (dwz_elements == 0)
74a0d9f6 2536 return;
2ec9a5e0 2537
168c9250
SM
2538 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2539 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2540 &dwz->info, 1);
2ec9a5e0
TT
2541}
2542
1fd400ff 2543/* Create the signatured type hash table from the index. */
673bfd45 2544
74a0d9f6 2545static void
12359b5e 2546create_signatured_type_table_from_index
168c9250
SM
2547 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2548 const gdb_byte *bytes, offset_type elements)
1fd400ff 2549{
168c9250
SM
2550 gdb_assert (per_bfd->all_type_units.empty ());
2551 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2552
298e9637 2553 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2554
12359b5e 2555 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2556 {
52dc124a 2557 struct signatured_type *sig_type;
9c541725 2558 ULONGEST signature;
1fd400ff 2559 void **slot;
9c541725 2560 cu_offset type_offset_in_tu;
1fd400ff 2561
74a0d9f6 2562 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2563 sect_offset sect_off
2564 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2565 type_offset_in_tu
2566 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2567 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2568 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2569 bytes += 3 * 8;
2570
168c9250 2571 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2572 sig_type->signature = signature;
9c541725 2573 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2574 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2575 sig_type->per_cu.section = section;
9c541725 2576 sig_type->per_cu.sect_off = sect_off;
52dc124a 2577 sig_type->per_cu.v.quick
168c9250 2578 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2579 struct dwarf2_per_cu_quick_data);
2580
b0b6a987 2581 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2582 *slot = sig_type;
1fd400ff 2583
168c9250 2584 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2585 }
2586
168c9250 2587 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2588}
2589
927aa2e7
JK
2590/* Create the signatured type hash table from .debug_names. */
2591
2592static void
2593create_signatured_type_table_from_debug_names
ed2dc618 2594 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2595 const mapped_debug_names &map,
2596 struct dwarf2_section_info *section,
2597 struct dwarf2_section_info *abbrev_section)
2598{
ed2dc618
SM
2599 struct objfile *objfile = dwarf2_per_objfile->objfile;
2600
96b79293
TT
2601 section->read (objfile);
2602 abbrev_section->read (objfile);
927aa2e7 2603
5989a64e
SM
2604 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2605 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2606
298e9637 2607 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2608
2609 for (uint32_t i = 0; i < map.tu_count; ++i)
2610 {
2611 struct signatured_type *sig_type;
927aa2e7 2612 void **slot;
927aa2e7
JK
2613
2614 sect_offset sect_off
2615 = (sect_offset) (extract_unsigned_integer
2616 (map.tu_table_reordered + i * map.offset_size,
2617 map.offset_size,
2618 map.dwarf5_byte_order));
2619
2620 comp_unit_head cu_header;
ed2dc618
SM
2621 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2622 abbrev_section,
927aa2e7
JK
2623 section->buffer + to_underlying (sect_off),
2624 rcuh_kind::TYPE);
2625
5989a64e 2626 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2627 sig_type->signature = cu_header.signature;
2628 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2629 sig_type->per_cu.is_debug_types = 1;
2630 sig_type->per_cu.section = section;
2631 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2632 sig_type->per_cu.v.quick
5989a64e 2633 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2634 struct dwarf2_per_cu_quick_data);
2635
b0b6a987 2636 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2637 *slot = sig_type;
2638
5989a64e 2639 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2640 }
2641
5989a64e 2642 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2643}
2644
9291a0cd
TT
2645/* Read the address map data from the mapped index, and use it to
2646 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2647
9291a0cd 2648static void
ed2dc618
SM
2649create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2650 struct mapped_index *index)
9291a0cd 2651{
ed2dc618 2652 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2653 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2654 const gdb_byte *iter, *end;
9291a0cd 2655 struct addrmap *mutable_map;
9291a0cd
TT
2656 CORE_ADDR baseaddr;
2657
8268c778
PA
2658 auto_obstack temp_obstack;
2659
9291a0cd
TT
2660 mutable_map = addrmap_create_mutable (&temp_obstack);
2661
f00a2de2
PA
2662 iter = index->address_table.data ();
2663 end = iter + index->address_table.size ();
9291a0cd 2664
b3b3bada 2665 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2666
2667 while (iter < end)
2668 {
2669 ULONGEST hi, lo, cu_index;
2670 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2671 iter += 8;
2672 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2673 iter += 8;
2674 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2675 iter += 4;
f652bce2 2676
24a55014 2677 if (lo > hi)
f652bce2 2678 {
b98664d3 2679 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2680 hex_string (lo), hex_string (hi));
24a55014 2681 continue;
f652bce2 2682 }
24a55014 2683
5989a64e 2684 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2685 {
b98664d3 2686 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2687 (unsigned) cu_index);
24a55014 2688 continue;
f652bce2 2689 }
24a55014 2690
79748972
TT
2691 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2692 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2693 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2694 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2695 }
2696
d320c2b5 2697 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2698 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2699}
2700
927aa2e7
JK
2701/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2702 populate the objfile's psymtabs_addrmap. */
2703
2704static void
ed2dc618 2705create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2706 struct dwarf2_section_info *section)
2707{
ed2dc618 2708 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2709 bfd *abfd = objfile->obfd;
08feed99 2710 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2711 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2712
2713 auto_obstack temp_obstack;
2714 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2715
2716 std::unordered_map<sect_offset,
2717 dwarf2_per_cu_data *,
2718 gdb::hash_enum<sect_offset>>
2719 debug_info_offset_to_per_cu;
5989a64e 2720 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2721 {
927aa2e7
JK
2722 const auto insertpair
2723 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2724 if (!insertpair.second)
2725 {
2726 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2727 "debug_info_offset %s, ignoring .debug_aranges."),
2728 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2729 return;
2730 }
2731 }
2732
96b79293 2733 section->read (objfile);
927aa2e7
JK
2734
2735 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2736
2737 const gdb_byte *addr = section->buffer;
2738
2739 while (addr < section->buffer + section->size)
2740 {
2741 const gdb_byte *const entry_addr = addr;
2742 unsigned int bytes_read;
2743
2744 const LONGEST entry_length = read_initial_length (abfd, addr,
2745 &bytes_read);
2746 addr += bytes_read;
2747
2748 const gdb_byte *const entry_end = addr + entry_length;
2749 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2750 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2751 if (addr + entry_length > section->buffer + section->size)
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "length %s exceeds section length %s, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 plongest (bytes_read + entry_length),
2759 pulongest (section->size));
2760 return;
2761 }
2762
2763 /* The version number. */
2764 const uint16_t version = read_2_bytes (abfd, addr);
2765 addr += 2;
2766 if (version != 2)
2767 {
47e3f474 2768 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2769 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2770 objfile_name (objfile),
2771 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2772 return;
2773 }
2774
2775 const uint64_t debug_info_offset
2776 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2777 addr += offset_size;
2778 const auto per_cu_it
2779 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2780 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2781 {
47e3f474 2782 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2783 "debug_info_offset %s does not exists, "
2784 "ignoring .debug_aranges."),
47e3f474
TV
2785 objfile_name (objfile),
2786 plongest (entry_addr - section->buffer),
927aa2e7
JK
2787 pulongest (debug_info_offset));
2788 return;
2789 }
2790 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2791
2792 const uint8_t address_size = *addr++;
2793 if (address_size < 1 || address_size > 8)
2794 {
47e3f474 2795 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2796 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2797 objfile_name (objfile),
2798 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2799 return;
2800 }
2801
2802 const uint8_t segment_selector_size = *addr++;
2803 if (segment_selector_size != 0)
2804 {
47e3f474 2805 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2806 "segment_selector_size %u is not supported, "
2807 "ignoring .debug_aranges."),
47e3f474
TV
2808 objfile_name (objfile),
2809 plongest (entry_addr - section->buffer),
927aa2e7
JK
2810 segment_selector_size);
2811 return;
2812 }
2813
2814 /* Must pad to an alignment boundary that is twice the address
2815 size. It is undocumented by the DWARF standard but GCC does
2816 use it. */
2817 for (size_t padding = ((-(addr - section->buffer))
2818 & (2 * address_size - 1));
2819 padding > 0; padding--)
2820 if (*addr++ != 0)
2821 {
47e3f474 2822 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2823 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2824 objfile_name (objfile),
2825 plongest (entry_addr - section->buffer));
927aa2e7
JK
2826 return;
2827 }
2828
2829 for (;;)
2830 {
2831 if (addr + 2 * address_size > entry_end)
2832 {
47e3f474 2833 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2834 "address list is not properly terminated, "
2835 "ignoring .debug_aranges."),
47e3f474
TV
2836 objfile_name (objfile),
2837 plongest (entry_addr - section->buffer));
927aa2e7
JK
2838 return;
2839 }
2840 ULONGEST start = extract_unsigned_integer (addr, address_size,
2841 dwarf5_byte_order);
2842 addr += address_size;
2843 ULONGEST length = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 if (start == 0 && length == 0)
2847 break;
5989a64e 2848 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2849 {
2850 /* Symbol was eliminated due to a COMDAT group. */
2851 continue;
2852 }
2853 ULONGEST end = start + length;
79748972
TT
2854 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2855 - baseaddr);
2856 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2857 - baseaddr);
927aa2e7
JK
2858 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2859 }
2860 }
2861
d320c2b5 2862 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2863 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2864}
2865
9291a0cd
TT
2866/* Find a slot in the mapped index INDEX for the object named NAME.
2867 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2868 constant pool and return true. If NAME cannot be found, return
2869 false. */
2fdf6df6 2870
109483d9 2871static bool
9291a0cd
TT
2872find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2873 offset_type **vec_out)
2874{
0cf03b49 2875 offset_type hash;
9291a0cd 2876 offset_type slot, step;
559a7a62 2877 int (*cmp) (const char *, const char *);
9291a0cd 2878
791afaa2 2879 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2880 if (current_language->la_language == language_cplus
45280282
IB
2881 || current_language->la_language == language_fortran
2882 || current_language->la_language == language_d)
0cf03b49
JK
2883 {
2884 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2885 not contain any. */
a8719064 2886
72998fb3 2887 if (strchr (name, '(') != NULL)
0cf03b49 2888 {
109483d9 2889 without_params = cp_remove_params (name);
0cf03b49 2890
72998fb3 2891 if (without_params != NULL)
791afaa2 2892 name = without_params.get ();
0cf03b49
JK
2893 }
2894 }
2895
559a7a62 2896 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2897 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2898 simulate our NAME being searched is also lowercased. */
2899 hash = mapped_index_string_hash ((index->version == 4
2900 && case_sensitivity == case_sensitive_off
2901 ? 5 : index->version),
2902 name);
2903
f00a2de2
PA
2904 slot = hash & (index->symbol_table.size () - 1);
2905 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2906 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2907
2908 for (;;)
2909 {
9291a0cd 2910 const char *str;
f00a2de2
PA
2911
2912 const auto &bucket = index->symbol_table[slot];
2913 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2914 return false;
9291a0cd 2915
f00a2de2 2916 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2917 if (!cmp (name, str))
9291a0cd
TT
2918 {
2919 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2920 + MAYBE_SWAP (bucket.vec));
109483d9 2921 return true;
9291a0cd
TT
2922 }
2923
f00a2de2 2924 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2925 }
2926}
2927
4485a1c1
SM
2928/* A helper function that reads the .gdb_index from BUFFER and fills
2929 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2930 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2931 ok to use deprecated sections.
2932
2933 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2934 out parameters that are filled in with information about the CU and
2935 TU lists in the section.
2936
4485a1c1 2937 Returns true if all went well, false otherwise. */
2fdf6df6 2938
d33bc52e 2939static bool
3810f182 2940read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2941 bool deprecated_ok,
2942 gdb::array_view<const gdb_byte> buffer,
2943 struct mapped_index *map,
2944 const gdb_byte **cu_list,
2945 offset_type *cu_list_elements,
2946 const gdb_byte **types_list,
2947 offset_type *types_list_elements)
2948{
2949 const gdb_byte *addr = &buffer[0];
82430852 2950
9291a0cd 2951 /* Version check. */
4485a1c1 2952 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2953 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2954 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2955 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2956 indices. */
831adc1f 2957 if (version < 4)
481860b3
GB
2958 {
2959 static int warning_printed = 0;
2960 if (!warning_printed)
2961 {
2962 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2963 filename);
481860b3
GB
2964 warning_printed = 1;
2965 }
2966 return 0;
2967 }
2968 /* Index version 4 uses a different hash function than index version
2969 5 and later.
2970
2971 Versions earlier than 6 did not emit psymbols for inlined
2972 functions. Using these files will cause GDB not to be able to
2973 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2974 indices unless the user has done
2975 "set use-deprecated-index-sections on". */
2ec9a5e0 2976 if (version < 6 && !deprecated_ok)
481860b3
GB
2977 {
2978 static int warning_printed = 0;
2979 if (!warning_printed)
2980 {
e615022a
DE
2981 warning (_("\
2982Skipping deprecated .gdb_index section in %s.\n\
2983Do \"set use-deprecated-index-sections on\" before the file is read\n\
2984to use the section anyway."),
2ec9a5e0 2985 filename);
481860b3
GB
2986 warning_printed = 1;
2987 }
2988 return 0;
2989 }
796a7ff8 2990 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2991 of the TU (for symbols coming from TUs),
2992 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2993 Plus gold-generated indices can have duplicate entries for global symbols,
2994 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2995 These are just performance bugs, and we can't distinguish gdb-generated
2996 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2997
481860b3 2998 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2999 longer backward compatible. */
796a7ff8 3000 if (version > 8)
594e8718 3001 return 0;
9291a0cd 3002
559a7a62 3003 map->version = version;
9291a0cd 3004
4485a1c1 3005 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3006
4485a1c1 3007 int i = 0;
2ec9a5e0
TT
3008 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3009 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3010 / 8);
1fd400ff
TT
3011 ++i;
3012
2ec9a5e0
TT
3013 *types_list = addr + MAYBE_SWAP (metadata[i]);
3014 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3015 - MAYBE_SWAP (metadata[i]))
3016 / 8);
987d643c 3017 ++i;
1fd400ff 3018
f00a2de2
PA
3019 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3020 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3021 map->address_table
3022 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3023 ++i;
3024
f00a2de2
PA
3025 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3026 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3027 map->symbol_table
3028 = gdb::array_view<mapped_index::symbol_table_slot>
3029 ((mapped_index::symbol_table_slot *) symbol_table,
3030 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3031
f00a2de2 3032 ++i;
f9d83a0b 3033 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3034
2ec9a5e0
TT
3035 return 1;
3036}
3037
4485a1c1
SM
3038/* Callback types for dwarf2_read_gdb_index. */
3039
3040typedef gdb::function_view
5989a64e 3041 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3042 get_gdb_index_contents_ftype;
3043typedef gdb::function_view
3044 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3045 get_gdb_index_contents_dwz_ftype;
3046
927aa2e7 3047/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3048 elements of all the CUs and return 1. Otherwise, return 0. */
3049
3050static int
4485a1c1
SM
3051dwarf2_read_gdb_index
3052 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3053 get_gdb_index_contents_ftype get_gdb_index_contents,
3054 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3055{
2ec9a5e0
TT
3056 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3057 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3058 struct dwz_file *dwz;
12359b5e 3059 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3060
4485a1c1 3061 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3062 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3063
3064 if (main_index_contents.empty ())
3065 return 0;
3066
3063847f 3067 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3068 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3069 use_deprecated_index_sections,
3070 main_index_contents, map.get (), &cu_list,
3071 &cu_list_elements, &types_list,
3072 &types_list_elements))
2ec9a5e0
TT
3073 return 0;
3074
0fefef59 3075 /* Don't use the index if it's empty. */
3063847f 3076 if (map->symbol_table.empty ())
0fefef59
DE
3077 return 0;
3078
2ec9a5e0
TT
3079 /* If there is a .dwz file, read it so we can get its CU list as
3080 well. */
c3699833 3081 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3082 if (dwz != NULL)
2ec9a5e0 3083 {
2ec9a5e0
TT
3084 struct mapped_index dwz_map;
3085 const gdb_byte *dwz_types_ignore;
3086 offset_type dwz_types_elements_ignore;
3087
4485a1c1
SM
3088 gdb::array_view<const gdb_byte> dwz_index_content
3089 = get_gdb_index_contents_dwz (objfile, dwz);
3090
3091 if (dwz_index_content.empty ())
3092 return 0;
3093
3810f182 3094 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3095 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3096 &dwz_list, &dwz_list_elements,
3097 &dwz_types_ignore,
3098 &dwz_types_elements_ignore))
2ec9a5e0
TT
3099 {
3100 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3101 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3102 return 0;
3103 }
3104 }
3105
168c9250 3106 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3107 dwz_list, dwz_list_elements);
1fd400ff 3108
8b70b953
TT
3109 if (types_list_elements)
3110 {
8b70b953
TT
3111 /* We can only handle a single .debug_types when we have an
3112 index. */
5989a64e 3113 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3114 return 0;
3115
5989a64e 3116 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3117
168c9250
SM
3118 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3119 section, types_list,
3120 types_list_elements);
8b70b953 3121 }
9291a0cd 3122
3063847f 3123 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3124
5989a64e
SM
3125 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3126 dwarf2_per_objfile->per_bfd->using_index = 1;
3127 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3128 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3129
3130 return 1;
3131}
3132
dee91e82 3133/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3134
dee91e82
DE
3135static void
3136dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3137 const gdb_byte *info_ptr,
3e225074 3138 struct die_info *comp_unit_die)
9291a0cd 3139{
dee91e82 3140 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3141 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3142 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3143 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3144 struct attribute *attr;
7b9f3c50
DE
3145 void **slot;
3146 struct quick_file_names *qfn;
9291a0cd 3147
0186c6a7
DE
3148 gdb_assert (! this_cu->is_debug_types);
3149
07261596
TT
3150 /* Our callers never want to match partial units -- instead they
3151 will match the enclosing full CU. */
3152 if (comp_unit_die->tag == DW_TAG_partial_unit)
3153 {
3154 this_cu->v.quick->no_file_data = 1;
3155 return;
3156 }
3157
0186c6a7 3158 lh_cu = this_cu;
7b9f3c50 3159 slot = NULL;
dee91e82 3160
fff8551c 3161 line_header_up lh;
9c541725 3162 sect_offset line_offset {};
fff8551c 3163
dee91e82 3164 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3165 if (attr != nullptr)
9291a0cd 3166 {
7b9f3c50
DE
3167 struct quick_file_names find_entry;
3168
9c541725 3169 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3170
3171 /* We may have already read in this line header (TU line header sharing).
3172 If we have we're done. */
094b34ac 3173 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3174 find_entry.hash.line_sect_off = line_offset;
5989a64e 3175 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3176 &find_entry, INSERT);
3177 if (*slot != NULL)
3178 {
9a3c8263 3179 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3180 return;
7b9f3c50
DE
3181 }
3182
3019eac3 3183 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3184 }
3185 if (lh == NULL)
3186 {
094b34ac 3187 lh_cu->v.quick->no_file_data = 1;
dee91e82 3188 return;
9291a0cd
TT
3189 }
3190
5989a64e 3191 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3192 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3193 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3194 gdb_assert (slot != NULL);
3195 *slot = qfn;
9291a0cd 3196
d721ba37 3197 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3198
aa391654
TT
3199 int offset = 0;
3200 if (strcmp (fnd.name, "<unknown>") != 0)
3201 ++offset;
3202
7ba99d21 3203 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3204 qfn->file_names =
5989a64e 3205 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3206 qfn->num_file_names);
aa391654
TT
3207 if (offset != 0)
3208 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3209 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3210 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3211 fnd.comp_dir).release ();
7b9f3c50 3212 qfn->real_names = NULL;
9291a0cd 3213
094b34ac 3214 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3215}
3216
3217/* A helper for the "quick" functions which attempts to read the line
3218 table for THIS_CU. */
3219
3220static struct quick_file_names *
ab432490
SM
3221dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3222 dwarf2_per_objfile *per_objfile)
dee91e82 3223{
0186c6a7
DE
3224 /* This should never be called for TUs. */
3225 gdb_assert (! this_cu->is_debug_types);
3226 /* Nor type unit groups. */
197400e8 3227 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3228
dee91e82
DE
3229 if (this_cu->v.quick->file_names != NULL)
3230 return this_cu->v.quick->file_names;
3231 /* If we know there is no line data, no point in looking again. */
3232 if (this_cu->v.quick->no_file_data)
3233 return NULL;
3234
ab432490 3235 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3236 if (!reader.dummy_p)
3e225074 3237 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3238
3239 if (this_cu->v.quick->no_file_data)
3240 return NULL;
3241 return this_cu->v.quick->file_names;
9291a0cd
TT
3242}
3243
3244/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3245 real path for a given file name from the line table. */
2fdf6df6 3246
9291a0cd 3247static const char *
45940949 3248dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3249 struct quick_file_names *qfn, int index)
9291a0cd 3250{
7b9f3c50 3251 if (qfn->real_names == NULL)
5989a64e 3252 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3253 qfn->num_file_names, const char *);
9291a0cd 3254
7b9f3c50 3255 if (qfn->real_names[index] == NULL)
14278e1f 3256 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3257
7b9f3c50 3258 return qfn->real_names[index];
9291a0cd
TT
3259}
3260
3261static struct symtab *
3262dw2_find_last_source_symtab (struct objfile *objfile)
3263{
ed2dc618
SM
3264 struct dwarf2_per_objfile *dwarf2_per_objfile
3265 = get_dwarf2_per_objfile (objfile);
5989a64e 3266 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3267 compunit_symtab *cust
3268 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3269
43f3e411
DE
3270 if (cust == NULL)
3271 return NULL;
ed2dc618 3272
43f3e411 3273 return compunit_primary_filetab (cust);
9291a0cd
TT
3274}
3275
7b9f3c50
DE
3276/* Traversal function for dw2_forget_cached_source_info. */
3277
3278static int
3279dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3280{
7b9f3c50 3281 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3282
7b9f3c50 3283 if (file_data->real_names)
9291a0cd 3284 {
7b9f3c50 3285 int i;
9291a0cd 3286
7b9f3c50 3287 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3288 {
7b9f3c50
DE
3289 xfree ((void*) file_data->real_names[i]);
3290 file_data->real_names[i] = NULL;
9291a0cd
TT
3291 }
3292 }
7b9f3c50
DE
3293
3294 return 1;
3295}
3296
3297static void
3298dw2_forget_cached_source_info (struct objfile *objfile)
3299{
ed2dc618
SM
3300 struct dwarf2_per_objfile *dwarf2_per_objfile
3301 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3302
5989a64e 3303 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3304 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3305}
3306
f8eba3c6
TT
3307/* Helper function for dw2_map_symtabs_matching_filename that expands
3308 the symtabs and calls the iterator. */
3309
3310static int
3311dw2_map_expand_apply (struct objfile *objfile,
3312 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3313 const char *name, const char *real_path,
14bc53a8 3314 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3315{
43f3e411 3316 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3317
3318 /* Don't visit already-expanded CUs. */
af758d11
SM
3319 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3320 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3321 return 0;
3322
3323 /* This may expand more than one symtab, and we want to iterate over
3324 all of them. */
97a1449a 3325 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3326
14bc53a8
PA
3327 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3328 last_made, callback);
f8eba3c6
TT
3329}
3330
3331/* Implementation of the map_symtabs_matching_filename method. */
3332
14bc53a8
PA
3333static bool
3334dw2_map_symtabs_matching_filename
3335 (struct objfile *objfile, const char *name, const char *real_path,
3336 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3337{
c011a4f4 3338 const char *name_basename = lbasename (name);
ed2dc618
SM
3339 struct dwarf2_per_objfile *dwarf2_per_objfile
3340 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3341
848e3e78
DE
3342 /* The rule is CUs specify all the files, including those used by
3343 any TU, so there's no need to scan TUs here. */
f4dc4d17 3344
5989a64e 3345 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3346 {
3d7bb9d9 3347 /* We only need to look at symtabs not already expanded. */
af758d11 3348 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3349 continue;
3350
ab432490
SM
3351 quick_file_names *file_data
3352 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3353 if (file_data == NULL)
9291a0cd
TT
3354 continue;
3355
b76e467d 3356 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3357 {
7b9f3c50 3358 const char *this_name = file_data->file_names[j];
da235a7c 3359 const char *this_real_name;
9291a0cd 3360
af529f8f 3361 if (compare_filenames_for_search (this_name, name))
9291a0cd 3362 {
f5b95b50 3363 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3364 callback))
3365 return true;
288e77a7 3366 continue;
4aac40c8 3367 }
9291a0cd 3368
c011a4f4
DE
3369 /* Before we invoke realpath, which can get expensive when many
3370 files are involved, do a quick comparison of the basenames. */
3371 if (! basenames_may_differ
3372 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3373 continue;
3374
45940949
TT
3375 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3376 file_data, j);
da235a7c 3377 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3378 {
da235a7c 3379 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3380 callback))
3381 return true;
288e77a7 3382 continue;
da235a7c 3383 }
9291a0cd 3384
da235a7c
JK
3385 if (real_path != NULL)
3386 {
af529f8f
JK
3387 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3388 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3389 if (this_real_name != NULL
af529f8f 3390 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3391 {
f5b95b50 3392 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3393 callback))
3394 return true;
288e77a7 3395 continue;
9291a0cd
TT
3396 }
3397 }
3398 }
3399 }
3400
14bc53a8 3401 return false;
9291a0cd
TT
3402}
3403
da51c347
DE
3404/* Struct used to manage iterating over all CUs looking for a symbol. */
3405
3406struct dw2_symtab_iterator
9291a0cd 3407{
ed2dc618
SM
3408 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3409 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3410 /* If set, only look for symbols that match that block. Valid values are
3411 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3412 gdb::optional<block_enum> block_index;
da51c347
DE
3413 /* The kind of symbol we're looking for. */
3414 domain_enum domain;
3415 /* The list of CUs from the index entry of the symbol,
3416 or NULL if not found. */
3417 offset_type *vec;
3418 /* The next element in VEC to look at. */
3419 int next;
3420 /* The number of elements in VEC, or zero if there is no match. */
3421 int length;
8943b874
DE
3422 /* Have we seen a global version of the symbol?
3423 If so we can ignore all further global instances.
3424 This is to work around gold/15646, inefficient gold-generated
3425 indices. */
3426 int global_seen;
da51c347 3427};
9291a0cd 3428
2b79f376 3429/* Initialize the index symtab iterator ITER. */
2fdf6df6 3430
9291a0cd 3431static void
da51c347 3432dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3433 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3434 gdb::optional<block_enum> block_index,
da51c347
DE
3435 domain_enum domain,
3436 const char *name)
3437{
ed2dc618 3438 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3439 iter->block_index = block_index;
3440 iter->domain = domain;
3441 iter->next = 0;
8943b874 3442 iter->global_seen = 0;
da51c347 3443
5989a64e 3444 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3445
3446 /* index is NULL if OBJF_READNOW. */
3447 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3448 iter->length = MAYBE_SWAP (*iter->vec);
3449 else
3450 {
3451 iter->vec = NULL;
3452 iter->length = 0;
3453 }
3454}
3455
3456/* Return the next matching CU or NULL if there are no more. */
3457
3458static struct dwarf2_per_cu_data *
3459dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3460{
ed2dc618
SM
3461 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3462
da51c347
DE
3463 for ( ; iter->next < iter->length; ++iter->next)
3464 {
3465 offset_type cu_index_and_attrs =
3466 MAYBE_SWAP (iter->vec[iter->next + 1]);
3467 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3468 gdb_index_symbol_kind symbol_kind =
3469 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3470 /* Only check the symbol attributes if they're present.
3471 Indices prior to version 7 don't record them,
3472 and indices >= 7 may elide them for certain symbols
3473 (gold does this). */
3474 int attrs_valid =
5989a64e 3475 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3476 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3477
3190f0c6 3478 /* Don't crash on bad data. */
5989a64e
SM
3479 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3480 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3481 {
b98664d3 3482 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3483 " [in module %s]"),
3484 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3485 continue;
3486 }
3487
5989a64e 3488 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3489
da51c347 3490 /* Skip if already read in. */
af758d11 3491 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3492 continue;
3493
8943b874
DE
3494 /* Check static vs global. */
3495 if (attrs_valid)
3496 {
2b79f376
SM
3497 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3498
3499 if (iter->block_index.has_value ())
3500 {
3501 bool want_static = *iter->block_index == STATIC_BLOCK;
3502
3503 if (is_static != want_static)
3504 continue;
3505 }
3506
8943b874
DE
3507 /* Work around gold/15646. */
3508 if (!is_static && iter->global_seen)
3509 continue;
3510 if (!is_static)
3511 iter->global_seen = 1;
3512 }
da51c347
DE
3513
3514 /* Only check the symbol's kind if it has one. */
3515 if (attrs_valid)
3516 {
3517 switch (iter->domain)
3518 {
3519 case VAR_DOMAIN:
3520 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3521 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3522 /* Some types are also in VAR_DOMAIN. */
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3524 continue;
3525 break;
3526 case STRUCT_DOMAIN:
3527 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3528 continue;
3529 break;
3530 case LABEL_DOMAIN:
3531 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3532 continue;
3533 break;
59c35742
AB
3534 case MODULE_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3536 continue;
3537 break;
da51c347
DE
3538 default:
3539 break;
3540 }
3541 }
3542
3543 ++iter->next;
3544 return per_cu;
3545 }
3546
3547 return NULL;
3548}
3549
43f3e411 3550static struct compunit_symtab *
c7f839cb 3551dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3552 const char *name, domain_enum domain)
9291a0cd 3553{
43f3e411 3554 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3555 struct dwarf2_per_objfile *dwarf2_per_objfile
3556 = get_dwarf2_per_objfile (objfile);
9291a0cd 3557
b5ec771e
PA
3558 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3559
ed2dc618
SM
3560 struct dw2_symtab_iterator iter;
3561 struct dwarf2_per_cu_data *per_cu;
da51c347 3562
2b79f376 3563 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3564
ed2dc618
SM
3565 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3566 {
3567 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3568 struct compunit_symtab *stab
3569 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3570 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3571 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3572
ed2dc618
SM
3573 sym = block_find_symbol (block, name, domain,
3574 block_find_non_opaque_type_preferred,
3575 &with_opaque);
b2e2f908 3576
ed2dc618
SM
3577 /* Some caution must be observed with overloaded functions
3578 and methods, since the index will not contain any overload
3579 information (but NAME might contain it). */
da51c347 3580
ed2dc618
SM
3581 if (sym != NULL
3582 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3583 return stab;
3584 if (with_opaque != NULL
3585 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3586 stab_best = stab;
da51c347 3587
ed2dc618 3588 /* Keep looking through other CUs. */
9291a0cd 3589 }
9291a0cd 3590
da51c347 3591 return stab_best;
9291a0cd
TT
3592}
3593
3594static void
3595dw2_print_stats (struct objfile *objfile)
3596{
ed2dc618
SM
3597 struct dwarf2_per_objfile *dwarf2_per_objfile
3598 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3599 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3600 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3601 int count = 0;
9291a0cd 3602
ed2dc618 3603 for (int i = 0; i < total; ++i)
9291a0cd 3604 {
5989a64e 3605 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3606
af758d11 3607 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3608 ++count;
3609 }
e4a48d9d 3610 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3611 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3612}
3613
779bd270
DE
3614/* This dumps minimal information about the index.
3615 It is called via "mt print objfiles".
3616 One use is to verify .gdb_index has been loaded by the
3617 gdb.dwarf2/gdb-index.exp testcase. */
3618
9291a0cd
TT
3619static void
3620dw2_dump (struct objfile *objfile)
3621{
ed2dc618
SM
3622 struct dwarf2_per_objfile *dwarf2_per_objfile
3623 = get_dwarf2_per_objfile (objfile);
3624
5989a64e 3625 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3626 printf_filtered (".gdb_index:");
5989a64e 3627 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3628 {
3629 printf_filtered (" version %d\n",
5989a64e 3630 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3631 }
3632 else
3633 printf_filtered (" faked for \"readnow\"\n");
3634 printf_filtered ("\n");
9291a0cd
TT
3635}
3636
9291a0cd
TT
3637static void
3638dw2_expand_symtabs_for_function (struct objfile *objfile,
3639 const char *func_name)
3640{
ed2dc618
SM
3641 struct dwarf2_per_objfile *dwarf2_per_objfile
3642 = get_dwarf2_per_objfile (objfile);
da51c347 3643
ed2dc618
SM
3644 struct dw2_symtab_iterator iter;
3645 struct dwarf2_per_cu_data *per_cu;
da51c347 3646
2b79f376 3647 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3648
ed2dc618 3649 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3650 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3651
9291a0cd
TT
3652}
3653
3654static void
3655dw2_expand_all_symtabs (struct objfile *objfile)
3656{
ed2dc618
SM
3657 struct dwarf2_per_objfile *dwarf2_per_objfile
3658 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3659 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3660 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3661
ed2dc618 3662 for (int i = 0; i < total_units; ++i)
9291a0cd 3663 {
5989a64e 3664 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3665
58f0c718
TT
3666 /* We don't want to directly expand a partial CU, because if we
3667 read it with the wrong language, then assertion failures can
3668 be triggered later on. See PR symtab/23010. So, tell
3669 dw2_instantiate_symtab to skip partial CUs -- any important
3670 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3671 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3672 }
3673}
3674
3675static void
652a8996
JK
3676dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3677 const char *fullname)
9291a0cd 3678{
ed2dc618
SM
3679 struct dwarf2_per_objfile *dwarf2_per_objfile
3680 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3681
3682 /* We don't need to consider type units here.
3683 This is only called for examining code, e.g. expand_line_sal.
3684 There can be an order of magnitude (or more) more type units
3685 than comp units, and we avoid them if we can. */
3686
5989a64e 3687 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3688 {
3d7bb9d9 3689 /* We only need to look at symtabs not already expanded. */
af758d11 3690 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3691 continue;
3692
ab432490
SM
3693 quick_file_names *file_data
3694 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3695 if (file_data == NULL)
9291a0cd
TT
3696 continue;
3697
b76e467d 3698 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3699 {
652a8996
JK
3700 const char *this_fullname = file_data->file_names[j];
3701
3702 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3703 {
97a1449a 3704 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3705 break;
3706 }
3707 }
3708 }
3709}
3710
9a0bacfb
TV
3711static void
3712dw2_expand_symtabs_matching_symbol
3713 (mapped_index_base &index,
3714 const lookup_name_info &lookup_name_in,
3715 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3716 enum search_domain kind,
3717 gdb::function_view<bool (offset_type)> match_callback);
3718
3719static void
3720dw2_expand_symtabs_matching_one
97a1449a
SM
3721 (dwarf2_per_cu_data *per_cu,
3722 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3723 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3724 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3725
9291a0cd 3726static void
199b4314
TT
3727dw2_map_matching_symbols
3728 (struct objfile *objfile,
b054970d 3729 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3730 int global,
3731 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3732 symbol_compare_ftype *ordered_compare)
9291a0cd 3733{
1aa98955
TV
3734 /* Used for Ada. */
3735 struct dwarf2_per_objfile *dwarf2_per_objfile
3736 = get_dwarf2_per_objfile (objfile);
3737
9a0bacfb
TV
3738 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3739
5989a64e 3740 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3741 {
3742 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3743 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3744 using GNU index. */
5989a64e 3745 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3746
9a0bacfb
TV
3747 const char *match_name = name.ada ().lookup_name ().c_str ();
3748 auto matcher = [&] (const char *symname)
3749 {
3750 if (ordered_compare == nullptr)
3751 return true;
3752 return ordered_compare (symname, match_name) == 0;
3753 };
3754
3755 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3756 [&] (offset_type namei)
3757 {
3758 struct dw2_symtab_iterator iter;
3759 struct dwarf2_per_cu_data *per_cu;
3760
3761 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3762 match_name);
3763 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3764 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3765 nullptr);
9a0bacfb
TV
3766 return true;
3767 });
3768 }
3769 else
3770 {
3771 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3772 proceed assuming all symtabs have been read in. */
3773 }
1aa98955
TV
3774
3775 for (compunit_symtab *cust : objfile->compunits ())
3776 {
3777 const struct block *block;
3778
3779 if (cust == NULL)
3780 continue;
3781 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3782 if (!iterate_over_symbols_terminated (block, name,
3783 domain, callback))
3784 return;
3785 }
9291a0cd
TT
3786}
3787
e1ef7d7a
PA
3788/* Starting from a search name, return the string that finds the upper
3789 bound of all strings that start with SEARCH_NAME in a sorted name
3790 list. Returns the empty string to indicate that the upper bound is
3791 the end of the list. */
3792
3793static std::string
3794make_sort_after_prefix_name (const char *search_name)
3795{
3796 /* When looking to complete "func", we find the upper bound of all
3797 symbols that start with "func" by looking for where we'd insert
3798 the closest string that would follow "func" in lexicographical
3799 order. Usually, that's "func"-with-last-character-incremented,
3800 i.e. "fund". Mind non-ASCII characters, though. Usually those
3801 will be UTF-8 multi-byte sequences, but we can't be certain.
3802 Especially mind the 0xff character, which is a valid character in
3803 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3804 rule out compilers allowing it in identifiers. Note that
3805 conveniently, strcmp/strcasecmp are specified to compare
3806 characters interpreted as unsigned char. So what we do is treat
3807 the whole string as a base 256 number composed of a sequence of
3808 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3809 to 0, and carries 1 to the following more-significant position.
3810 If the very first character in SEARCH_NAME ends up incremented
3811 and carries/overflows, then the upper bound is the end of the
3812 list. The string after the empty string is also the empty
3813 string.
3814
3815 Some examples of this operation:
3816
3817 SEARCH_NAME => "+1" RESULT
3818
3819 "abc" => "abd"
3820 "ab\xff" => "ac"
3821 "\xff" "a" "\xff" => "\xff" "b"
3822 "\xff" => ""
3823 "\xff\xff" => ""
3824 "" => ""
3825
3826 Then, with these symbols for example:
3827
3828 func
3829 func1
3830 fund
3831
3832 completing "func" looks for symbols between "func" and
3833 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3834 which finds "func" and "func1", but not "fund".
3835
3836 And with:
3837
3838 funcÿ (Latin1 'ÿ' [0xff])
3839 funcÿ1
3840 fund
3841
3842 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3843 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3844
3845 And with:
3846
3847 ÿÿ (Latin1 'ÿ' [0xff])
3848 ÿÿ1
3849
3850 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3851 the end of the list.
3852 */
3853 std::string after = search_name;
3854 while (!after.empty () && (unsigned char) after.back () == 0xff)
3855 after.pop_back ();
3856 if (!after.empty ())
3857 after.back () = (unsigned char) after.back () + 1;
3858 return after;
3859}
3860
5c58de74 3861/* See declaration. */
61d96d7e 3862
5c58de74
PA
3863std::pair<std::vector<name_component>::const_iterator,
3864 std::vector<name_component>::const_iterator>
44ed8f3e 3865mapped_index_base::find_name_components_bounds
3b00ef10 3866 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3867{
5c58de74
PA
3868 auto *name_cmp
3869 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3870
3b00ef10 3871 const char *lang_name
e0802d59 3872 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3873
3f563c84
PA
3874 /* Comparison function object for lower_bound that matches against a
3875 given symbol name. */
3876 auto lookup_compare_lower = [&] (const name_component &elem,
3877 const char *name)
3878 {
5c58de74 3879 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3880 const char *elem_name = elem_qualified + elem.name_offset;
3881 return name_cmp (elem_name, name) < 0;
3882 };
3883
3884 /* Comparison function object for upper_bound that matches against a
3885 given symbol name. */
3886 auto lookup_compare_upper = [&] (const char *name,
3887 const name_component &elem)
3888 {
5c58de74 3889 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3890 const char *elem_name = elem_qualified + elem.name_offset;
3891 return name_cmp (name, elem_name) < 0;
3892 };
3893
5c58de74
PA
3894 auto begin = this->name_components.begin ();
3895 auto end = this->name_components.end ();
3f563c84
PA
3896
3897 /* Find the lower bound. */
3898 auto lower = [&] ()
3899 {
3b00ef10 3900 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3901 return begin;
3902 else
3b00ef10 3903 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3904 } ();
3905
3906 /* Find the upper bound. */
3907 auto upper = [&] ()
3908 {
5c58de74 3909 if (lookup_name_without_params.completion_mode ())
3f563c84 3910 {
e1ef7d7a
PA
3911 /* In completion mode, we want UPPER to point past all
3912 symbols names that have the same prefix. I.e., with
3913 these symbols, and completing "func":
3914
3915 function << lower bound
3916 function1
3917 other_function << upper bound
3918
3919 We find the upper bound by looking for the insertion
3920 point of "func"-with-last-character-incremented,
3921 i.e. "fund". */
3b00ef10 3922 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3923 if (after.empty ())
3f563c84 3924 return end;
e6b2f5ef
PA
3925 return std::lower_bound (lower, end, after.c_str (),
3926 lookup_compare_lower);
3f563c84
PA
3927 }
3928 else
3b00ef10 3929 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3930 } ();
3931
5c58de74
PA
3932 return {lower, upper};
3933}
3934
3935/* See declaration. */
3936
3937void
44ed8f3e 3938mapped_index_base::build_name_components ()
5c58de74
PA
3939{
3940 if (!this->name_components.empty ())
3941 return;
3942
3943 this->name_components_casing = case_sensitivity;
3944 auto *name_cmp
3945 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3946
3947 /* The code below only knows how to break apart components of C++
3948 symbol names (and other languages that use '::' as
3b00ef10 3949 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3950 auto count = this->symbol_name_count ();
3951 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3952 {
44ed8f3e 3953 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3954 continue;
3955
3956 const char *name = this->symbol_name_at (idx);
3957
3958 /* Add each name component to the name component table. */
3959 unsigned int previous_len = 0;
3b00ef10
TT
3960
3961 if (strstr (name, "::") != nullptr)
3962 {
3963 for (unsigned int current_len = cp_find_first_component (name);
3964 name[current_len] != '\0';
3965 current_len += cp_find_first_component (name + current_len))
3966 {
3967 gdb_assert (name[current_len] == ':');
3968 this->name_components.push_back ({previous_len, idx});
3969 /* Skip the '::'. */
3970 current_len += 2;
3971 previous_len = current_len;
3972 }
3973 }
3974 else
5c58de74 3975 {
3b00ef10
TT
3976 /* Handle the Ada encoded (aka mangled) form here. */
3977 for (const char *iter = strstr (name, "__");
3978 iter != nullptr;
3979 iter = strstr (iter, "__"))
3980 {
3981 this->name_components.push_back ({previous_len, idx});
3982 iter += 2;
3983 previous_len = iter - name;
3984 }
5c58de74 3985 }
3b00ef10 3986
5c58de74
PA
3987 this->name_components.push_back ({previous_len, idx});
3988 }
3989
3990 /* Sort name_components elements by name. */
3991 auto name_comp_compare = [&] (const name_component &left,
3992 const name_component &right)
3993 {
3994 const char *left_qualified = this->symbol_name_at (left.idx);
3995 const char *right_qualified = this->symbol_name_at (right.idx);
3996
3997 const char *left_name = left_qualified + left.name_offset;
3998 const char *right_name = right_qualified + right.name_offset;
3999
4000 return name_cmp (left_name, right_name) < 0;
4001 };
4002
4003 std::sort (this->name_components.begin (),
4004 this->name_components.end (),
4005 name_comp_compare);
4006}
4007
4008/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4009 mapped_index_base instead of the containing objfile. This is split
4010 to a separate function in order to be able to unit test the
4011 name_components matching using a mock mapped_index_base. For each
5c58de74 4012 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4013 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4014
4015static void
4016dw2_expand_symtabs_matching_symbol
44ed8f3e 4017 (mapped_index_base &index,
5c58de74
PA
4018 const lookup_name_info &lookup_name_in,
4019 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4020 enum search_domain kind,
3b00ef10 4021 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4022{
4023 lookup_name_info lookup_name_without_params
4024 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4025
4026 /* Build the symbol name component sorted vector, if we haven't
4027 yet. */
4028 index.build_name_components ();
4029
3f563c84
PA
4030 /* The same symbol may appear more than once in the range though.
4031 E.g., if we're looking for symbols that complete "w", and we have
4032 a symbol named "w1::w2", we'll find the two name components for
4033 that same symbol in the range. To be sure we only call the
4034 callback once per symbol, we first collect the symbol name
4035 indexes that matched in a temporary vector and ignore
4036 duplicates. */
4037 std::vector<offset_type> matches;
3f563c84 4038
3b00ef10
TT
4039 struct name_and_matcher
4040 {
4041 symbol_name_matcher_ftype *matcher;
ecc6c606 4042 const char *name;
3b00ef10
TT
4043
4044 bool operator== (const name_and_matcher &other) const
3f563c84 4045 {
ecc6c606 4046 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4047 }
4048 };
4049
4050 /* A vector holding all the different symbol name matchers, for all
4051 languages. */
4052 std::vector<name_and_matcher> matchers;
4053
4054 for (int i = 0; i < nr_languages; i++)
4055 {
4056 enum language lang_e = (enum language) i;
4057
4058 const language_defn *lang = language_def (lang_e);
4059 symbol_name_matcher_ftype *name_matcher
4060 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4061
3b00ef10
TT
4062 name_and_matcher key {
4063 name_matcher,
4064 lookup_name_without_params.language_lookup_name (lang_e)
4065 };
4066
4067 /* Don't insert the same comparison routine more than once.
4068 Note that we do this linear walk. This is not a problem in
4069 practice because the number of supported languages is
4070 low. */
4071 if (std::find (matchers.begin (), matchers.end (), key)
4072 != matchers.end ())
9291a0cd 4073 continue;
3b00ef10
TT
4074 matchers.push_back (std::move (key));
4075
4076 auto bounds
4077 = index.find_name_components_bounds (lookup_name_without_params,
4078 lang_e);
4079
4080 /* Now for each symbol name in range, check to see if we have a name
4081 match, and if so, call the MATCH_CALLBACK callback. */
4082
4083 for (; bounds.first != bounds.second; ++bounds.first)
4084 {
4085 const char *qualified = index.symbol_name_at (bounds.first->idx);
4086
4087 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4088 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4089 continue;
9291a0cd 4090
3b00ef10
TT
4091 matches.push_back (bounds.first->idx);
4092 }
3f563c84
PA
4093 }
4094
4095 std::sort (matches.begin (), matches.end ());
4096
4097 /* Finally call the callback, once per match. */
4098 ULONGEST prev = -1;
4099 for (offset_type idx : matches)
4100 {
4101 if (prev != idx)
4102 {
3b00ef10
TT
4103 if (!match_callback (idx))
4104 break;
3f563c84
PA
4105 prev = idx;
4106 }
4107 }
4108
4109 /* Above we use a type wider than idx's for 'prev', since 0 and
4110 (offset_type)-1 are both possible values. */
4111 static_assert (sizeof (prev) > sizeof (offset_type), "");
4112}
4113
c62446b1
PA
4114#if GDB_SELF_TEST
4115
4116namespace selftests { namespace dw2_expand_symtabs_matching {
4117
a3c5fafd
PA
4118/* A mock .gdb_index/.debug_names-like name index table, enough to
4119 exercise dw2_expand_symtabs_matching_symbol, which works with the
4120 mapped_index_base interface. Builds an index from the symbol list
4121 passed as parameter to the constructor. */
4122class mock_mapped_index : public mapped_index_base
c62446b1
PA
4123{
4124public:
a3c5fafd
PA
4125 mock_mapped_index (gdb::array_view<const char *> symbols)
4126 : m_symbol_table (symbols)
c62446b1
PA
4127 {}
4128
a3c5fafd 4129 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4130
a3c5fafd 4131 /* Return the number of names in the symbol table. */
632e107b 4132 size_t symbol_name_count () const override
c62446b1 4133 {
a3c5fafd 4134 return m_symbol_table.size ();
c62446b1
PA
4135 }
4136
a3c5fafd 4137 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4138 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4139 {
4140 return m_symbol_table[idx];
4141 }
c62446b1 4142
a3c5fafd
PA
4143private:
4144 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4145};
4146
4147/* Convenience function that converts a NULL pointer to a "<null>"
4148 string, to pass to print routines. */
4149
4150static const char *
4151string_or_null (const char *str)
4152{
4153 return str != NULL ? str : "<null>";
4154}
4155
4156/* Check if a lookup_name_info built from
4157 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4158 index. EXPECTED_LIST is the list of expected matches, in expected
4159 matching order. If no match expected, then an empty list is
4160 specified. Returns true on success. On failure prints a warning
4161 indicating the file:line that failed, and returns false. */
4162
4163static bool
4164check_match (const char *file, int line,
4165 mock_mapped_index &mock_index,
4166 const char *name, symbol_name_match_type match_type,
4167 bool completion_mode,
4168 std::initializer_list<const char *> expected_list)
4169{
4170 lookup_name_info lookup_name (name, match_type, completion_mode);
4171
4172 bool matched = true;
4173
4174 auto mismatch = [&] (const char *expected_str,
4175 const char *got)
4176 {
4177 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4178 "expected=\"%s\", got=\"%s\"\n"),
4179 file, line,
4180 (match_type == symbol_name_match_type::FULL
4181 ? "FULL" : "WILD"),
4182 name, string_or_null (expected_str), string_or_null (got));
4183 matched = false;
4184 };
4185
4186 auto expected_it = expected_list.begin ();
4187 auto expected_end = expected_list.end ();
4188
a3c5fafd 4189 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4190 NULL, ALL_DOMAIN,
4191 [&] (offset_type idx)
4192 {
a3c5fafd 4193 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4194 const char *expected_str
4195 = expected_it == expected_end ? NULL : *expected_it++;
4196
4197 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4198 mismatch (expected_str, matched_name);
3b00ef10 4199 return true;
c62446b1
PA
4200 });
4201
4202 const char *expected_str
4203 = expected_it == expected_end ? NULL : *expected_it++;
4204 if (expected_str != NULL)
4205 mismatch (expected_str, NULL);
4206
4207 return matched;
4208}
4209
4210/* The symbols added to the mock mapped_index for testing (in
4211 canonical form). */
4212static const char *test_symbols[] = {
4213 "function",
4214 "std::bar",
4215 "std::zfunction",
4216 "std::zfunction2",
4217 "w1::w2",
4218 "ns::foo<char*>",
4219 "ns::foo<int>",
4220 "ns::foo<long>",
a20714ff
PA
4221 "ns2::tmpl<int>::foo2",
4222 "(anonymous namespace)::A::B::C",
c62446b1 4223
e1ef7d7a
PA
4224 /* These are used to check that the increment-last-char in the
4225 matching algorithm for completion doesn't match "t1_fund" when
4226 completing "t1_func". */
4227 "t1_func",
4228 "t1_func1",
4229 "t1_fund",
4230 "t1_fund1",
4231
4232 /* A UTF-8 name with multi-byte sequences to make sure that
4233 cp-name-parser understands this as a single identifier ("função"
4234 is "function" in PT). */
4235 u8"u8função",
4236
4237 /* \377 (0xff) is Latin1 'ÿ'. */
4238 "yfunc\377",
4239
4240 /* \377 (0xff) is Latin1 'ÿ'. */
4241 "\377",
4242 "\377\377123",
4243
c62446b1
PA
4244 /* A name with all sorts of complications. Starts with "z" to make
4245 it easier for the completion tests below. */
4246#define Z_SYM_NAME \
4247 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4248 "::tuple<(anonymous namespace)::ui*, " \
4249 "std::default_delete<(anonymous namespace)::ui>, void>"
4250
4251 Z_SYM_NAME
4252};
4253
a3c5fafd
PA
4254/* Returns true if the mapped_index_base::find_name_component_bounds
4255 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4256 in completion mode. */
5c58de74
PA
4257
4258static bool
a3c5fafd 4259check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4260 const char *search_name,
4261 gdb::array_view<const char *> expected_syms)
4262{
4263 lookup_name_info lookup_name (search_name,
4264 symbol_name_match_type::FULL, true);
4265
3b00ef10
TT
4266 auto bounds = index.find_name_components_bounds (lookup_name,
4267 language_cplus);
5c58de74
PA
4268
4269 size_t distance = std::distance (bounds.first, bounds.second);
4270 if (distance != expected_syms.size ())
4271 return false;
4272
4273 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4274 {
4275 auto nc_elem = bounds.first + exp_elem;
4276 const char *qualified = index.symbol_name_at (nc_elem->idx);
4277 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4278 return false;
4279 }
4280
4281 return true;
4282}
4283
4284/* Test the lower-level mapped_index::find_name_component_bounds
4285 method. */
4286
c62446b1 4287static void
5c58de74
PA
4288test_mapped_index_find_name_component_bounds ()
4289{
4290 mock_mapped_index mock_index (test_symbols);
4291
a3c5fafd 4292 mock_index.build_name_components ();
5c58de74
PA
4293
4294 /* Test the lower-level mapped_index::find_name_component_bounds
4295 method in completion mode. */
4296 {
4297 static const char *expected_syms[] = {
4298 "t1_func",
4299 "t1_func1",
5c58de74
PA
4300 };
4301
a3c5fafd 4302 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4303 "t1_func", expected_syms));
4304 }
4305
4306 /* Check that the increment-last-char in the name matching algorithm
4307 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4308 {
4309 static const char *expected_syms1[] = {
4310 "\377",
4311 "\377\377123",
4312 };
a3c5fafd 4313 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4314 "\377", expected_syms1));
4315
4316 static const char *expected_syms2[] = {
4317 "\377\377123",
4318 };
a3c5fafd 4319 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4320 "\377\377", expected_syms2));
4321 }
4322}
4323
4324/* Test dw2_expand_symtabs_matching_symbol. */
4325
4326static void
4327test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4328{
4329 mock_mapped_index mock_index (test_symbols);
4330
4331 /* We let all tests run until the end even if some fails, for debug
4332 convenience. */
4333 bool any_mismatch = false;
4334
4335 /* Create the expected symbols list (an initializer_list). Needed
4336 because lists have commas, and we need to pass them to CHECK,
4337 which is a macro. */
4338#define EXPECT(...) { __VA_ARGS__ }
4339
4340 /* Wrapper for check_match that passes down the current
4341 __FILE__/__LINE__. */
4342#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4343 any_mismatch |= !check_match (__FILE__, __LINE__, \
4344 mock_index, \
4345 NAME, MATCH_TYPE, COMPLETION_MODE, \
4346 EXPECTED_LIST)
4347
4348 /* Identity checks. */
4349 for (const char *sym : test_symbols)
4350 {
4351 /* Should be able to match all existing symbols. */
4352 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4353 EXPECT (sym));
4354
4355 /* Should be able to match all existing symbols with
4356 parameters. */
4357 std::string with_params = std::string (sym) + "(int)";
4358 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4359 EXPECT (sym));
4360
4361 /* Should be able to match all existing symbols with
4362 parameters and qualifiers. */
4363 with_params = std::string (sym) + " ( int ) const";
4364 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4365 EXPECT (sym));
4366
4367 /* This should really find sym, but cp-name-parser.y doesn't
4368 know about lvalue/rvalue qualifiers yet. */
4369 with_params = std::string (sym) + " ( int ) &&";
4370 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4371 {});
4372 }
4373
e1ef7d7a
PA
4374 /* Check that the name matching algorithm for completion doesn't get
4375 confused with Latin1 'ÿ' / 0xff. */
4376 {
4377 static const char str[] = "\377";
4378 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4379 EXPECT ("\377", "\377\377123"));
4380 }
4381
4382 /* Check that the increment-last-char in the matching algorithm for
4383 completion doesn't match "t1_fund" when completing "t1_func". */
4384 {
4385 static const char str[] = "t1_func";
4386 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4387 EXPECT ("t1_func", "t1_func1"));
4388 }
4389
c62446b1
PA
4390 /* Check that completion mode works at each prefix of the expected
4391 symbol name. */
4392 {
4393 static const char str[] = "function(int)";
4394 size_t len = strlen (str);
4395 std::string lookup;
4396
4397 for (size_t i = 1; i < len; i++)
4398 {
4399 lookup.assign (str, i);
4400 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4401 EXPECT ("function"));
4402 }
4403 }
4404
4405 /* While "w" is a prefix of both components, the match function
4406 should still only be called once. */
4407 {
4408 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4409 EXPECT ("w1::w2"));
a20714ff
PA
4410 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4411 EXPECT ("w1::w2"));
c62446b1
PA
4412 }
4413
4414 /* Same, with a "complicated" symbol. */
4415 {
4416 static const char str[] = Z_SYM_NAME;
4417 size_t len = strlen (str);
4418 std::string lookup;
4419
4420 for (size_t i = 1; i < len; i++)
4421 {
4422 lookup.assign (str, i);
4423 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4424 EXPECT (Z_SYM_NAME));
4425 }
4426 }
4427
4428 /* In FULL mode, an incomplete symbol doesn't match. */
4429 {
4430 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4431 {});
4432 }
4433
4434 /* A complete symbol with parameters matches any overload, since the
4435 index has no overload info. */
4436 {
4437 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4438 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4439 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
4441 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4443 }
4444
4445 /* Check that whitespace is ignored appropriately. A symbol with a
4446 template argument list. */
4447 {
4448 static const char expected[] = "ns::foo<int>";
4449 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4450 EXPECT (expected));
a20714ff
PA
4451 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4452 EXPECT (expected));
c62446b1
PA
4453 }
4454
4455 /* Check that whitespace is ignored appropriately. A symbol with a
4456 template argument list that includes a pointer. */
4457 {
4458 static const char expected[] = "ns::foo<char*>";
4459 /* Try both completion and non-completion modes. */
4460 static const bool completion_mode[2] = {false, true};
4461 for (size_t i = 0; i < 2; i++)
4462 {
4463 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4464 completion_mode[i], EXPECT (expected));
a20714ff
PA
4465 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4466 completion_mode[i], EXPECT (expected));
c62446b1
PA
4467
4468 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4469 completion_mode[i], EXPECT (expected));
a20714ff
PA
4470 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4471 completion_mode[i], EXPECT (expected));
c62446b1
PA
4472 }
4473 }
4474
4475 {
4476 /* Check method qualifiers are ignored. */
4477 static const char expected[] = "ns::foo<char*>";
4478 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4479 symbol_name_match_type::FULL, true, EXPECT (expected));
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4482 CHECK_MATCH ("foo < char * > ( int ) const",
4483 symbol_name_match_type::WILD, true, EXPECT (expected));
4484 CHECK_MATCH ("foo < char * > ( int ) &&",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4486 }
4487
4488 /* Test lookup names that don't match anything. */
4489 {
a20714ff
PA
4490 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4491 {});
4492
c62446b1
PA
4493 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4494 {});
4495 }
4496
a20714ff
PA
4497 /* Some wild matching tests, exercising "(anonymous namespace)",
4498 which should not be confused with a parameter list. */
4499 {
4500 static const char *syms[] = {
4501 "A::B::C",
4502 "B::C",
4503 "C",
4504 "A :: B :: C ( int )",
4505 "B :: C ( int )",
4506 "C ( int )",
4507 };
4508
4509 for (const char *s : syms)
4510 {
4511 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4512 EXPECT ("(anonymous namespace)::A::B::C"));
4513 }
4514 }
4515
4516 {
4517 static const char expected[] = "ns2::tmpl<int>::foo2";
4518 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4519 EXPECT (expected));
4520 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 }
4523
c62446b1
PA
4524 SELF_CHECK (!any_mismatch);
4525
4526#undef EXPECT
4527#undef CHECK_MATCH
4528}
4529
5c58de74
PA
4530static void
4531run_test ()
4532{
4533 test_mapped_index_find_name_component_bounds ();
4534 test_dw2_expand_symtabs_matching_symbol ();
4535}
4536
c62446b1
PA
4537}} // namespace selftests::dw2_expand_symtabs_matching
4538
4539#endif /* GDB_SELF_TEST */
4540
4b514bc8
JK
4541/* If FILE_MATCHER is NULL or if PER_CU has
4542 dwarf2_per_cu_quick_data::MARK set (see
4543 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4544 EXPANSION_NOTIFY on it. */
4545
4546static void
4547dw2_expand_symtabs_matching_one
97a1449a
SM
4548 (dwarf2_per_cu_data *per_cu,
4549 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4550 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4551 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4552{
4553 if (file_matcher == NULL || per_cu->v.quick->mark)
4554 {
af758d11 4555 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4556
97a1449a
SM
4557 compunit_symtab *symtab
4558 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4559 gdb_assert (symtab != nullptr);
4b514bc8 4560
af758d11
SM
4561 if (expansion_notify != NULL && symtab_was_null)
4562 expansion_notify (symtab);
4b514bc8
JK
4563 }
4564}
4565
3f563c84
PA
4566/* Helper for dw2_expand_matching symtabs. Called on each symbol
4567 matched, to expand corresponding CUs that were marked. IDX is the
4568 index of the symbol name that matched. */
4569
4570static void
4571dw2_expand_marked_cus
97a1449a 4572 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4573 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4575 search_domain kind)
4576{
3f563c84
PA
4577 offset_type *vec, vec_len, vec_idx;
4578 bool global_seen = false;
5989a64e 4579 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4580
61920122 4581 vec = (offset_type *) (index.constant_pool
f00a2de2 4582 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4583 vec_len = MAYBE_SWAP (vec[0]);
4584 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4585 {
61920122
PA
4586 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4587 /* This value is only valid for index versions >= 7. */
4588 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4589 gdb_index_symbol_kind symbol_kind =
4590 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4591 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4592 /* Only check the symbol attributes if they're present.
4593 Indices prior to version 7 don't record them,
4594 and indices >= 7 may elide them for certain symbols
4595 (gold does this). */
4596 int attrs_valid =
4597 (index.version >= 7
4598 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4599
4600 /* Work around gold/15646. */
4601 if (attrs_valid)
9291a0cd 4602 {
61920122
PA
4603 if (!is_static && global_seen)
4604 continue;
4605 if (!is_static)
4606 global_seen = true;
4607 }
3190f0c6 4608
61920122
PA
4609 /* Only check the symbol's kind if it has one. */
4610 if (attrs_valid)
4611 {
4612 switch (kind)
8943b874 4613 {
61920122
PA
4614 case VARIABLES_DOMAIN:
4615 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4616 continue;
4617 break;
4618 case FUNCTIONS_DOMAIN:
4619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4620 continue;
61920122
PA
4621 break;
4622 case TYPES_DOMAIN:
4623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4624 continue;
4625 break;
59c35742
AB
4626 case MODULES_DOMAIN:
4627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4628 continue;
4629 break;
61920122
PA
4630 default:
4631 break;
8943b874 4632 }
61920122 4633 }
8943b874 4634
61920122 4635 /* Don't crash on bad data. */
5989a64e
SM
4636 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4637 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4638 {
b98664d3 4639 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4640 " [in module %s]"),
4641 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4642 continue;
4643 }
4644
5989a64e 4645 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4646 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4647 expansion_notify);
61920122
PA
4648 }
4649}
4650
4b514bc8
JK
4651/* If FILE_MATCHER is non-NULL, set all the
4652 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4653 that match FILE_MATCHER. */
4654
61920122 4655static void
4b514bc8 4656dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4657 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4658 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4659{
4b514bc8 4660 if (file_matcher == NULL)
61920122
PA
4661 return;
4662
4b514bc8
JK
4663 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4664 htab_eq_pointer,
4665 NULL, xcalloc, xfree));
4666 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4667 htab_eq_pointer,
4668 NULL, xcalloc, xfree));
61920122 4669
4b514bc8
JK
4670 /* The rule is CUs specify all the files, including those used by
4671 any TU, so there's no need to scan TUs here. */
61920122 4672
5989a64e 4673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4674 {
927aa2e7
JK
4675 QUIT;
4676
4677 per_cu->v.quick->mark = 0;
4678
4679 /* We only need to look at symtabs not already expanded. */
af758d11 4680 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4681 continue;
4682
ab432490
SM
4683 quick_file_names *file_data
4684 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4685 if (file_data == NULL)
4686 continue;
4687
4688 if (htab_find (visited_not_found.get (), file_data) != NULL)
4689 continue;
4690 else if (htab_find (visited_found.get (), file_data) != NULL)
4691 {
4692 per_cu->v.quick->mark = 1;
4693 continue;
4694 }
4695
b76e467d 4696 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4697 {
4698 const char *this_real_name;
4699
4700 if (file_matcher (file_data->file_names[j], false))
4701 {
4702 per_cu->v.quick->mark = 1;
4703 break;
4704 }
4705
4706 /* Before we invoke realpath, which can get expensive when many
4707 files are involved, do a quick comparison of the basenames. */
4708 if (!basenames_may_differ
4709 && !file_matcher (lbasename (file_data->file_names[j]),
4710 true))
4711 continue;
4712
45940949
TT
4713 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4714 file_data, j);
927aa2e7
JK
4715 if (file_matcher (this_real_name, false))
4716 {
4717 per_cu->v.quick->mark = 1;
4718 break;
4719 }
4720 }
4721
b76e467d
SM
4722 void **slot = htab_find_slot (per_cu->v.quick->mark
4723 ? visited_found.get ()
4724 : visited_not_found.get (),
4725 file_data, INSERT);
927aa2e7
JK
4726 *slot = file_data;
4727 }
4728}
4729
4730static void
4731dw2_expand_symtabs_matching
4732 (struct objfile *objfile,
4733 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4734 const lookup_name_info *lookup_name,
927aa2e7
JK
4735 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4736 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4737 enum search_domain kind)
4738{
ed2dc618
SM
4739 struct dwarf2_per_objfile *dwarf2_per_objfile
4740 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4741
4742 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4743 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4744 return;
4745
ed2dc618 4746 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4747
c1a66c06
TV
4748 if (symbol_matcher == NULL && lookup_name == NULL)
4749 {
5989a64e 4750 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4751 {
4752 QUIT;
4753
97a1449a
SM
4754 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4755 file_matcher, expansion_notify);
c1a66c06
TV
4756 }
4757 return;
4758 }
4759
5989a64e 4760 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4761
c1a66c06 4762 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4763 symbol_matcher,
4764 kind, [&] (offset_type idx)
4765 {
ed2dc618 4766 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4767 expansion_notify, kind);
3b00ef10 4768 return true;
927aa2e7
JK
4769 });
4770}
4771
4772/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4773 symtab. */
4774
4775static struct compunit_symtab *
4776recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4777 CORE_ADDR pc)
4778{
4779 int i;
4780
4781 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4782 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4783 return cust;
4784
4785 if (cust->includes == NULL)
4786 return NULL;
4787
4788 for (i = 0; cust->includes[i]; ++i)
4789 {
4790 struct compunit_symtab *s = cust->includes[i];
4791
4792 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4793 if (s != NULL)
4794 return s;
4795 }
4796
4797 return NULL;
4798}
4799
4800static struct compunit_symtab *
4801dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4802 struct bound_minimal_symbol msymbol,
4803 CORE_ADDR pc,
4804 struct obj_section *section,
4805 int warn_if_readin)
4806{
4807 struct dwarf2_per_cu_data *data;
4808 struct compunit_symtab *result;
4809
d320c2b5 4810 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4811 return NULL;
4812
b3b3bada 4813 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4814 data = (struct dwarf2_per_cu_data *) addrmap_find
4815 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4816 if (!data)
4817 return NULL;
4818
af758d11
SM
4819 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4820 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4821 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4822 paddress (objfile->arch (), pc));
927aa2e7 4823
97a1449a
SM
4824 result = recursively_find_pc_sect_compunit_symtab
4825 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4826
927aa2e7
JK
4827 gdb_assert (result != NULL);
4828 return result;
4829}
4830
4831static void
4832dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4833 void *data, int need_fullname)
4834{
ed2dc618
SM
4835 struct dwarf2_per_objfile *dwarf2_per_objfile
4836 = get_dwarf2_per_objfile (objfile);
927aa2e7 4837
5989a64e 4838 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4839 {
5989a64e 4840 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4841
4842 htab_up visited (htab_create_alloc (10,
4843 htab_hash_pointer, htab_eq_pointer,
4844 NULL, xcalloc, xfree));
4845
4846 /* The rule is CUs specify all the files, including those used
4847 by any TU, so there's no need to scan TUs here. We can
4848 ignore file names coming from already-expanded CUs. */
4849
5989a64e 4850 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4851 {
af758d11 4852 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4853 {
4854 void **slot = htab_find_slot (visited.get (),
4855 per_cu->v.quick->file_names,
4856 INSERT);
4857
4858 *slot = per_cu->v.quick->file_names;
4859 }
4860 }
4861
5989a64e 4862 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4863 {
927aa2e7 4864 /* We only need to look at symtabs not already expanded. */
af758d11 4865 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4866 continue;
4867
ab432490
SM
4868 quick_file_names *file_data
4869 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4870 if (file_data == NULL)
4871 continue;
4872
b76e467d 4873 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4874 if (*slot)
4875 {
4876 /* Already visited. */
4877 continue;
4878 }
4879 *slot = file_data;
4880
4881 for (int j = 0; j < file_data->num_file_names; ++j)
4882 {
4883 const char *filename = file_data->file_names[j];
5989a64e 4884 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4885 }
4886 }
4887 }
4888
5989a64e 4889 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4890 {
4891 gdb::unique_xmalloc_ptr<char> this_real_name;
4892
4893 if (need_fullname)
4894 this_real_name = gdb_realpath (filename);
4895 (*fun) (filename, this_real_name.get (), data);
4896 });
4897}
4898
4899static int
4900dw2_has_symbols (struct objfile *objfile)
4901{
4902 return 1;
4903}
4904
4905const struct quick_symbol_functions dwarf2_gdb_index_functions =
4906{
4907 dw2_has_symbols,
4908 dw2_find_last_source_symtab,
4909 dw2_forget_cached_source_info,
4910 dw2_map_symtabs_matching_filename,
4911 dw2_lookup_symbol,
d3214198 4912 NULL,
927aa2e7
JK
4913 dw2_print_stats,
4914 dw2_dump,
927aa2e7
JK
4915 dw2_expand_symtabs_for_function,
4916 dw2_expand_all_symtabs,
4917 dw2_expand_symtabs_with_fullname,
4918 dw2_map_matching_symbols,
4919 dw2_expand_symtabs_matching,
4920 dw2_find_pc_sect_compunit_symtab,
4921 NULL,
4922 dw2_map_symbol_filenames
4923};
4924
4925/* DWARF-5 debug_names reader. */
4926
4927/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4928static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4929
4930/* A helper function that reads the .debug_names section in SECTION
4931 and fills in MAP. FILENAME is the name of the file containing the
4932 section; it is used for error reporting.
4933
4934 Returns true if all went well, false otherwise. */
4935
4936static bool
4937read_debug_names_from_section (struct objfile *objfile,
4938 const char *filename,
4939 struct dwarf2_section_info *section,
4940 mapped_debug_names &map)
4941{
96b79293 4942 if (section->empty ())
927aa2e7
JK
4943 return false;
4944
4945 /* Older elfutils strip versions could keep the section in the main
4946 executable while splitting it for the separate debug info file. */
96b79293 4947 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4948 return false;
4949
96b79293 4950 section->read (objfile);
927aa2e7 4951
08feed99 4952 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4953
4954 const gdb_byte *addr = section->buffer;
4955
96b79293 4956 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4957
4958 unsigned int bytes_read;
4959 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4960 addr += bytes_read;
4961
4962 map.dwarf5_is_dwarf64 = bytes_read != 4;
4963 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4964 if (bytes_read + length != section->size)
4965 {
4966 /* There may be multiple per-CU indices. */
4967 warning (_("Section .debug_names in %s length %s does not match "
4968 "section length %s, ignoring .debug_names."),
4969 filename, plongest (bytes_read + length),
4970 pulongest (section->size));
4971 return false;
4972 }
4973
4974 /* The version number. */
4975 uint16_t version = read_2_bytes (abfd, addr);
4976 addr += 2;
4977 if (version != 5)
4978 {
4979 warning (_("Section .debug_names in %s has unsupported version %d, "
4980 "ignoring .debug_names."),
4981 filename, version);
4982 return false;
4983 }
4984
4985 /* Padding. */
4986 uint16_t padding = read_2_bytes (abfd, addr);
4987 addr += 2;
4988 if (padding != 0)
4989 {
4990 warning (_("Section .debug_names in %s has unsupported padding %d, "
4991 "ignoring .debug_names."),
4992 filename, padding);
4993 return false;
4994 }
4995
4996 /* comp_unit_count - The number of CUs in the CU list. */
4997 map.cu_count = read_4_bytes (abfd, addr);
4998 addr += 4;
4999
5000 /* local_type_unit_count - The number of TUs in the local TU
5001 list. */
5002 map.tu_count = read_4_bytes (abfd, addr);
5003 addr += 4;
5004
5005 /* foreign_type_unit_count - The number of TUs in the foreign TU
5006 list. */
5007 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5008 addr += 4;
5009 if (foreign_tu_count != 0)
5010 {
5011 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5012 "ignoring .debug_names."),
5013 filename, static_cast<unsigned long> (foreign_tu_count));
5014 return false;
5015 }
5016
5017 /* bucket_count - The number of hash buckets in the hash lookup
5018 table. */
5019 map.bucket_count = read_4_bytes (abfd, addr);
5020 addr += 4;
5021
5022 /* name_count - The number of unique names in the index. */
5023 map.name_count = read_4_bytes (abfd, addr);
5024 addr += 4;
5025
5026 /* abbrev_table_size - The size in bytes of the abbreviations
5027 table. */
5028 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5029 addr += 4;
5030
5031 /* augmentation_string_size - The size in bytes of the augmentation
5032 string. This value is rounded up to a multiple of 4. */
5033 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5034 addr += 4;
5035 map.augmentation_is_gdb = ((augmentation_string_size
5036 == sizeof (dwarf5_augmentation))
5037 && memcmp (addr, dwarf5_augmentation,
5038 sizeof (dwarf5_augmentation)) == 0);
5039 augmentation_string_size += (-augmentation_string_size) & 3;
5040 addr += augmentation_string_size;
5041
5042 /* List of CUs */
5043 map.cu_table_reordered = addr;
5044 addr += map.cu_count * map.offset_size;
5045
5046 /* List of Local TUs */
5047 map.tu_table_reordered = addr;
5048 addr += map.tu_count * map.offset_size;
5049
5050 /* Hash Lookup Table */
5051 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5052 addr += map.bucket_count * 4;
5053 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.name_count * 4;
5055
5056 /* Name Table */
5057 map.name_table_string_offs_reordered = addr;
5058 addr += map.name_count * map.offset_size;
5059 map.name_table_entry_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061
5062 const gdb_byte *abbrev_table_start = addr;
5063 for (;;)
5064 {
927aa2e7
JK
5065 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5066 addr += bytes_read;
5067 if (index_num == 0)
5068 break;
5069
5070 const auto insertpair
5071 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5072 if (!insertpair.second)
5073 {
5074 warning (_("Section .debug_names in %s has duplicate index %s, "
5075 "ignoring .debug_names."),
5076 filename, pulongest (index_num));
5077 return false;
5078 }
5079 mapped_debug_names::index_val &indexval = insertpair.first->second;
5080 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5081 addr += bytes_read;
5082
5083 for (;;)
5084 {
5085 mapped_debug_names::index_val::attr attr;
5086 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5087 addr += bytes_read;
5088 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 if (attr.form == DW_FORM_implicit_const)
5091 {
5092 attr.implicit_const = read_signed_leb128 (abfd, addr,
5093 &bytes_read);
5094 addr += bytes_read;
5095 }
5096 if (attr.dw_idx == 0 && attr.form == 0)
5097 break;
5098 indexval.attr_vec.push_back (std::move (attr));
5099 }
5100 }
5101 if (addr != abbrev_table_start + abbrev_table_size)
5102 {
5103 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5104 "of size %s vs. written as %u, ignoring .debug_names."),
5105 filename, plongest (addr - abbrev_table_start),
5106 abbrev_table_size);
927aa2e7
JK
5107 return false;
5108 }
5109 map.entry_pool = addr;
5110
5111 return true;
5112}
5113
5114/* A helper for create_cus_from_debug_names that handles the MAP's CU
5115 list. */
5116
5117static void
168c9250 5118create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5119 const mapped_debug_names &map,
5120 dwarf2_section_info &section,
b76e467d 5121 bool is_dwz)
927aa2e7 5122{
3ee6bb11
TV
5123 if (!map.augmentation_is_gdb)
5124 {
5125 for (uint32_t i = 0; i < map.cu_count; ++i)
5126 {
5127 sect_offset sect_off
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 /* We don't know the length of the CU, because the CU list in a
5133 .debug_names index can be incomplete, so we can't use the start of
5134 the next CU as end of this CU. We create the CUs here with length 0,
5135 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5136 dwarf2_per_cu_data *per_cu
168c9250
SM
5137 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5138 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5139 }
5140 }
5141
927aa2e7
JK
5142 sect_offset sect_off_prev;
5143 for (uint32_t i = 0; i <= map.cu_count; ++i)
5144 {
5145 sect_offset sect_off_next;
5146 if (i < map.cu_count)
5147 {
5148 sect_off_next
5149 = (sect_offset) (extract_unsigned_integer
5150 (map.cu_table_reordered + i * map.offset_size,
5151 map.offset_size,
5152 map.dwarf5_byte_order));
5153 }
5154 else
5155 sect_off_next = (sect_offset) section.size;
5156 if (i >= 1)
5157 {
5158 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5159 dwarf2_per_cu_data *per_cu
168c9250 5160 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5161 sect_off_prev, length);
168c9250 5162 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5163 }
5164 sect_off_prev = sect_off_next;
5165 }
5166}
5167
5168/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5169 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5170
5171static void
168c9250 5172create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5173 const mapped_debug_names &map,
5174 const mapped_debug_names &dwz_map)
5175{
168c9250
SM
5176 gdb_assert (per_bfd->all_comp_units.empty ());
5177 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5178
168c9250 5179 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5180 false /* is_dwz */);
927aa2e7
JK
5181
5182 if (dwz_map.cu_count == 0)
5183 return;
5184
168c9250
SM
5185 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5186 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5187 true /* is_dwz */);
927aa2e7
JK
5188}
5189
5190/* Read .debug_names. If everything went ok, initialize the "quick"
5191 elements of all the CUs and return true. Otherwise, return false. */
5192
5193static bool
ed2dc618 5194dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5195{
22ca247e
TT
5196 std::unique_ptr<mapped_debug_names> map
5197 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5198 mapped_debug_names dwz_map (dwarf2_per_objfile);
5199 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5200
5201 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5202 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5203 *map))
927aa2e7
JK
5204 return false;
5205
5206 /* Don't use the index if it's empty. */
22ca247e 5207 if (map->name_count == 0)
927aa2e7
JK
5208 return false;
5209
5210 /* If there is a .dwz file, read it so we can get its CU list as
5211 well. */
c3699833 5212 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5213 if (dwz != NULL)
5214 {
5215 if (!read_debug_names_from_section (objfile,
00f93c44 5216 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5217 &dwz->debug_names, dwz_map))
5218 {
5219 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5220 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5221 return false;
5222 }
5223 }
5224
168c9250 5225 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5226
22ca247e 5227 if (map->tu_count != 0)
927aa2e7
JK
5228 {
5229 /* We can only handle a single .debug_types when we have an
5230 index. */
5989a64e 5231 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5232 return false;
5233
5989a64e 5234 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5235
5236 create_signatured_type_table_from_debug_names
5989a64e 5237 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5238 }
5239
ed2dc618 5240 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5241 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5242
5989a64e
SM
5243 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5244 dwarf2_per_objfile->per_bfd->using_index = 1;
5245 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5246 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5247
5248 return true;
5249}
5250
927aa2e7
JK
5251/* Type used to manage iterating over all CUs looking for a symbol for
5252 .debug_names. */
5253
5254class dw2_debug_names_iterator
5255{
5256public:
927aa2e7 5257 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5258 gdb::optional<block_enum> block_index,
5259 domain_enum domain,
927aa2e7 5260 const char *name)
2b79f376 5261 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5262 m_addr (find_vec_in_debug_names (map, name))
5263 {}
5264
5265 dw2_debug_names_iterator (const mapped_debug_names &map,
5266 search_domain search, uint32_t namei)
5267 : m_map (map),
5268 m_search (search),
5269 m_addr (find_vec_in_debug_names (map, namei))
5270 {}
5271
3b00ef10
TT
5272 dw2_debug_names_iterator (const mapped_debug_names &map,
5273 block_enum block_index, domain_enum domain,
5274 uint32_t namei)
5275 : m_map (map), m_block_index (block_index), m_domain (domain),
5276 m_addr (find_vec_in_debug_names (map, namei))
5277 {}
5278
927aa2e7
JK
5279 /* Return the next matching CU or NULL if there are no more. */
5280 dwarf2_per_cu_data *next ();
5281
5282private:
5283 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5284 const char *name);
5285 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5286 uint32_t namei);
5287
5288 /* The internalized form of .debug_names. */
5289 const mapped_debug_names &m_map;
5290
2b79f376
SM
5291 /* If set, only look for symbols that match that block. Valid values are
5292 GLOBAL_BLOCK and STATIC_BLOCK. */
5293 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5294
5295 /* The kind of symbol we're looking for. */
5296 const domain_enum m_domain = UNDEF_DOMAIN;
5297 const search_domain m_search = ALL_DOMAIN;
5298
5299 /* The list of CUs from the index entry of the symbol, or NULL if
5300 not found. */
5301 const gdb_byte *m_addr;
5302};
5303
5304const char *
5305mapped_debug_names::namei_to_name (uint32_t namei) const
5306{
5307 const ULONGEST namei_string_offs
5308 = extract_unsigned_integer ((name_table_string_offs_reordered
5309 + namei * offset_size),
5310 offset_size,
5311 dwarf5_byte_order);
4f44ae6c
TT
5312 return read_indirect_string_at_offset (dwarf2_per_objfile,
5313 namei_string_offs);
927aa2e7
JK
5314}
5315
5316/* Find a slot in .debug_names for the object named NAME. If NAME is
5317 found, return pointer to its pool data. If NAME cannot be found,
5318 return NULL. */
5319
5320const gdb_byte *
5321dw2_debug_names_iterator::find_vec_in_debug_names
5322 (const mapped_debug_names &map, const char *name)
5323{
5324 int (*cmp) (const char *, const char *);
5325
54ee4252 5326 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5327 if (current_language->la_language == language_cplus
5328 || current_language->la_language == language_fortran
5329 || current_language->la_language == language_d)
5330 {
5331 /* NAME is already canonical. Drop any qualifiers as
5332 .debug_names does not contain any. */
5333
5334 if (strchr (name, '(') != NULL)
5335 {
54ee4252 5336 without_params = cp_remove_params (name);
927aa2e7 5337 if (without_params != NULL)
54ee4252 5338 name = without_params.get ();
927aa2e7
JK
5339 }
5340 }
5341
5342 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5343
5344 const uint32_t full_hash = dwarf5_djb_hash (name);
5345 uint32_t namei
5346 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5347 (map.bucket_table_reordered
5348 + (full_hash % map.bucket_count)), 4,
5349 map.dwarf5_byte_order);
5350 if (namei == 0)
5351 return NULL;
5352 --namei;
5353 if (namei >= map.name_count)
5354 {
b98664d3 5355 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5356 "[in module %s]"),
5357 namei, map.name_count,
ed2dc618 5358 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5359 return NULL;
5360 }
5361
5362 for (;;)
5363 {
5364 const uint32_t namei_full_hash
5365 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5366 (map.hash_table_reordered + namei), 4,
5367 map.dwarf5_byte_order);
5368 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5369 return NULL;
5370
5371 if (full_hash == namei_full_hash)
5372 {
5373 const char *const namei_string = map.namei_to_name (namei);
5374
5375#if 0 /* An expensive sanity check. */
5376 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5377 {
b98664d3 5378 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5379 "[in module %s]"),
5380 namei, objfile_name (dwarf2_per_objfile->objfile));
5381 return NULL;
5382 }
5383#endif
5384
5385 if (cmp (namei_string, name) == 0)
5386 {
5387 const ULONGEST namei_entry_offs
5388 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5389 + namei * map.offset_size),
5390 map.offset_size, map.dwarf5_byte_order);
5391 return map.entry_pool + namei_entry_offs;
5392 }
5393 }
5394
5395 ++namei;
5396 if (namei >= map.name_count)
5397 return NULL;
5398 }
5399}
5400
5401const gdb_byte *
5402dw2_debug_names_iterator::find_vec_in_debug_names
5403 (const mapped_debug_names &map, uint32_t namei)
5404{
5405 if (namei >= map.name_count)
5406 {
b98664d3 5407 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5408 "[in module %s]"),
5409 namei, map.name_count,
ed2dc618 5410 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5411 return NULL;
5412 }
5413
5414 const ULONGEST namei_entry_offs
5415 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5416 + namei * map.offset_size),
5417 map.offset_size, map.dwarf5_byte_order);
5418 return map.entry_pool + namei_entry_offs;
5419}
5420
5421/* See dw2_debug_names_iterator. */
5422
5423dwarf2_per_cu_data *
5424dw2_debug_names_iterator::next ()
5425{
5426 if (m_addr == NULL)
5427 return NULL;
5428
ed2dc618
SM
5429 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5430 struct objfile *objfile = dwarf2_per_objfile->objfile;
5431 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5432
5433 again:
5434
5435 unsigned int bytes_read;
5436 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5437 m_addr += bytes_read;
5438 if (abbrev == 0)
5439 return NULL;
5440
5441 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5442 if (indexval_it == m_map.abbrev_map.cend ())
5443 {
b98664d3 5444 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5445 "[in module %s]"),
ed2dc618 5446 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5447 return NULL;
5448 }
5449 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5450 enum class symbol_linkage {
5451 unknown,
5452 static_,
5453 extern_,
23c13d42 5454 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5455 dwarf2_per_cu_data *per_cu = NULL;
5456 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5457 {
5458 ULONGEST ull;
5459 switch (attr.form)
5460 {
5461 case DW_FORM_implicit_const:
5462 ull = attr.implicit_const;
5463 break;
5464 case DW_FORM_flag_present:
5465 ull = 1;
5466 break;
5467 case DW_FORM_udata:
5468 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5469 m_addr += bytes_read;
5470 break;
6dc55ce9 5471 case DW_FORM_ref4:
5472 ull = read_4_bytes (abfd, m_addr);
5473 m_addr += 4;
5474 break;
5475 case DW_FORM_ref8:
5476 ull = read_8_bytes (abfd, m_addr);
5477 m_addr += 8;
5478 break;
5479 case DW_FORM_ref_sig8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
927aa2e7 5483 default:
b98664d3 5484 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5485 dwarf_form_name (attr.form),
ed2dc618 5486 objfile_name (objfile));
927aa2e7
JK
5487 return NULL;
5488 }
5489 switch (attr.dw_idx)
5490 {
5491 case DW_IDX_compile_unit:
5492 /* Don't crash on bad data. */
5989a64e 5493 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5494 {
b98664d3 5495 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5496 " [in module %s]"),
5497 pulongest (ull),
5498 objfile_name (dwarf2_per_objfile->objfile));
5499 continue;
5500 }
5989a64e 5501 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5502 break;
8af5c486
JK
5503 case DW_IDX_type_unit:
5504 /* Don't crash on bad data. */
5989a64e 5505 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5506 {
b98664d3 5507 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5508 " [in module %s]"),
5509 pulongest (ull),
5510 objfile_name (dwarf2_per_objfile->objfile));
5511 continue;
5512 }
5989a64e 5513 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5514 break;
6dc55ce9 5515 case DW_IDX_die_offset:
5516 /* In a per-CU index (as opposed to a per-module index), index
5517 entries without CU attribute implicitly refer to the single CU. */
5518 if (per_cu == NULL)
5989a64e 5519 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5520 break;
927aa2e7
JK
5521 case DW_IDX_GNU_internal:
5522 if (!m_map.augmentation_is_gdb)
5523 break;
23c13d42 5524 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5525 break;
5526 case DW_IDX_GNU_external:
5527 if (!m_map.augmentation_is_gdb)
5528 break;
23c13d42 5529 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5530 break;
5531 }
5532 }
5533
5534 /* Skip if already read in. */
af758d11 5535 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5536 goto again;
5537
5538 /* Check static vs global. */
23c13d42 5539 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5540 {
2b79f376 5541 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5542 const bool symbol_is_static =
5543 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5544 if (want_static != symbol_is_static)
2b79f376 5545 goto again;
927aa2e7
JK
5546 }
5547
5548 /* Match dw2_symtab_iter_next, symbol_kind
5549 and debug_names::psymbol_tag. */
5550 switch (m_domain)
5551 {
5552 case VAR_DOMAIN:
5553 switch (indexval.dwarf_tag)
5554 {
5555 case DW_TAG_variable:
5556 case DW_TAG_subprogram:
5557 /* Some types are also in VAR_DOMAIN. */
5558 case DW_TAG_typedef:
5559 case DW_TAG_structure_type:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
5565 case STRUCT_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case DW_TAG_typedef:
5569 case DW_TAG_structure_type:
5570 break;
5571 default:
5572 goto again;
5573 }
5574 break;
5575 case LABEL_DOMAIN:
5576 switch (indexval.dwarf_tag)
5577 {
5578 case 0:
5579 case DW_TAG_variable:
5580 break;
5581 default:
5582 goto again;
5583 }
5584 break;
59c35742
AB
5585 case MODULE_DOMAIN:
5586 switch (indexval.dwarf_tag)
5587 {
5588 case DW_TAG_module:
5589 break;
5590 default:
5591 goto again;
5592 }
5593 break;
927aa2e7
JK
5594 default:
5595 break;
5596 }
5597
5598 /* Match dw2_expand_symtabs_matching, symbol_kind and
5599 debug_names::psymbol_tag. */
5600 switch (m_search)
4b514bc8 5601 {
927aa2e7
JK
5602 case VARIABLES_DOMAIN:
5603 switch (indexval.dwarf_tag)
4b514bc8 5604 {
927aa2e7
JK
5605 case DW_TAG_variable:
5606 break;
5607 default:
5608 goto again;
4b514bc8 5609 }
927aa2e7
JK
5610 break;
5611 case FUNCTIONS_DOMAIN:
5612 switch (indexval.dwarf_tag)
4b514bc8 5613 {
927aa2e7
JK
5614 case DW_TAG_subprogram:
5615 break;
5616 default:
5617 goto again;
4b514bc8 5618 }
927aa2e7
JK
5619 break;
5620 case TYPES_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_typedef:
5624 case DW_TAG_structure_type:
5625 break;
5626 default:
5627 goto again;
5628 }
5629 break;
59c35742
AB
5630 case MODULES_DOMAIN:
5631 switch (indexval.dwarf_tag)
5632 {
5633 case DW_TAG_module:
5634 break;
5635 default:
5636 goto again;
5637 }
927aa2e7
JK
5638 default:
5639 break;
4b514bc8 5640 }
927aa2e7
JK
5641
5642 return per_cu;
4b514bc8 5643}
61920122 5644
927aa2e7 5645static struct compunit_symtab *
c7f839cb 5646dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5647 const char *name, domain_enum domain)
4b514bc8 5648{
ed2dc618
SM
5649 struct dwarf2_per_objfile *dwarf2_per_objfile
5650 = get_dwarf2_per_objfile (objfile);
61920122 5651
5989a64e 5652 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5653 if (!mapp)
61920122 5654 {
927aa2e7
JK
5655 /* index is NULL if OBJF_READNOW. */
5656 return NULL;
5657 }
5658 const auto &map = *mapp;
9291a0cd 5659
2b79f376 5660 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5661
927aa2e7
JK
5662 struct compunit_symtab *stab_best = NULL;
5663 struct dwarf2_per_cu_data *per_cu;
5664 while ((per_cu = iter.next ()) != NULL)
5665 {
5666 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5667 compunit_symtab *stab
5668 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5669 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5670 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5671
927aa2e7
JK
5672 sym = block_find_symbol (block, name, domain,
5673 block_find_non_opaque_type_preferred,
5674 &with_opaque);
9703b513 5675
927aa2e7
JK
5676 /* Some caution must be observed with overloaded functions and
5677 methods, since the index will not contain any overload
5678 information (but NAME might contain it). */
a3ec0bb1 5679
927aa2e7 5680 if (sym != NULL
987012b8 5681 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5682 return stab;
5683 if (with_opaque != NULL
987012b8 5684 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5685 stab_best = stab;
9703b513 5686
927aa2e7 5687 /* Keep looking through other CUs. */
9703b513
TT
5688 }
5689
927aa2e7 5690 return stab_best;
9703b513
TT
5691}
5692
927aa2e7
JK
5693/* This dumps minimal information about .debug_names. It is called
5694 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5695 uses this to verify that .debug_names has been loaded. */
9291a0cd 5696
927aa2e7
JK
5697static void
5698dw2_debug_names_dump (struct objfile *objfile)
5699{
ed2dc618
SM
5700 struct dwarf2_per_objfile *dwarf2_per_objfile
5701 = get_dwarf2_per_objfile (objfile);
5702
5989a64e 5703 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5704 printf_filtered (".debug_names:");
5989a64e 5705 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5706 printf_filtered (" exists\n");
5707 else
5708 printf_filtered (" faked for \"readnow\"\n");
5709 printf_filtered ("\n");
9291a0cd
TT
5710}
5711
9291a0cd 5712static void
927aa2e7
JK
5713dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5714 const char *func_name)
9291a0cd 5715{
ed2dc618
SM
5716 struct dwarf2_per_objfile *dwarf2_per_objfile
5717 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5718
5989a64e
SM
5719 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5720 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5721 {
5989a64e 5722 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5723
2b79f376 5724 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5725
927aa2e7
JK
5726 struct dwarf2_per_cu_data *per_cu;
5727 while ((per_cu = iter.next ()) != NULL)
97a1449a 5728 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5729 }
5730}
24c79950 5731
3b00ef10
TT
5732static void
5733dw2_debug_names_map_matching_symbols
5734 (struct objfile *objfile,
5735 const lookup_name_info &name, domain_enum domain,
5736 int global,
5737 gdb::function_view<symbol_found_callback_ftype> callback,
5738 symbol_compare_ftype *ordered_compare)
5739{
5740 struct dwarf2_per_objfile *dwarf2_per_objfile
5741 = get_dwarf2_per_objfile (objfile);
5742
5743 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5744 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5745 return;
5746
5989a64e 5747 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5748 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5749
5750 const char *match_name = name.ada ().lookup_name ().c_str ();
5751 auto matcher = [&] (const char *symname)
5752 {
5753 if (ordered_compare == nullptr)
5754 return true;
5755 return ordered_compare (symname, match_name) == 0;
5756 };
5757
5758 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5759 [&] (offset_type namei)
5760 {
5761 /* The name was matched, now expand corresponding CUs that were
5762 marked. */
5763 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5764
5765 struct dwarf2_per_cu_data *per_cu;
5766 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5767 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5768 nullptr);
3b00ef10
TT
5769 return true;
5770 });
5771
5772 /* It's a shame we couldn't do this inside the
5773 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5774 that have already been expanded. Instead, this loop matches what
5775 the psymtab code does. */
5989a64e 5776 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5777 {
af758d11
SM
5778 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5779 if (symtab != nullptr)
3b00ef10
TT
5780 {
5781 const struct block *block
af758d11 5782 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5783 if (!iterate_over_symbols_terminated (block, name,
5784 domain, callback))
5785 break;
5786 }
5787 }
5788}
5789
927aa2e7
JK
5790static void
5791dw2_debug_names_expand_symtabs_matching
5792 (struct objfile *objfile,
5793 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5794 const lookup_name_info *lookup_name,
927aa2e7
JK
5795 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5796 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5797 enum search_domain kind)
5798{
ed2dc618
SM
5799 struct dwarf2_per_objfile *dwarf2_per_objfile
5800 = get_dwarf2_per_objfile (objfile);
9291a0cd 5801
927aa2e7 5802 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5803 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5804 return;
9291a0cd 5805
ed2dc618 5806 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5807
c1a66c06
TV
5808 if (symbol_matcher == NULL && lookup_name == NULL)
5809 {
5989a64e 5810 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5811 {
5812 QUIT;
5813
97a1449a
SM
5814 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5815 file_matcher, expansion_notify);
c1a66c06
TV
5816 }
5817 return;
5818 }
5819
5989a64e 5820 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5821
c1a66c06 5822 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5823 symbol_matcher,
5824 kind, [&] (offset_type namei)
927aa2e7 5825 {
927aa2e7
JK
5826 /* The name was matched, now expand corresponding CUs that were
5827 marked. */
5828 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5829
927aa2e7
JK
5830 struct dwarf2_per_cu_data *per_cu;
5831 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5832 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5833 file_matcher, expansion_notify);
3b00ef10 5834 return true;
44ed8f3e 5835 });
9291a0cd
TT
5836}
5837
927aa2e7 5838const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5839{
5840 dw2_has_symbols,
5841 dw2_find_last_source_symtab,
5842 dw2_forget_cached_source_info,
f8eba3c6 5843 dw2_map_symtabs_matching_filename,
927aa2e7 5844 dw2_debug_names_lookup_symbol,
d3214198 5845 NULL,
9291a0cd 5846 dw2_print_stats,
927aa2e7 5847 dw2_debug_names_dump,
927aa2e7 5848 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5849 dw2_expand_all_symtabs,
652a8996 5850 dw2_expand_symtabs_with_fullname,
3b00ef10 5851 dw2_debug_names_map_matching_symbols,
927aa2e7 5852 dw2_debug_names_expand_symtabs_matching,
43f3e411 5853 dw2_find_pc_sect_compunit_symtab,
71a3c369 5854 NULL,
9291a0cd
TT
5855 dw2_map_symbol_filenames
5856};
5857
4485a1c1 5858/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5859 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5860
5861template <typename T>
5862static gdb::array_view<const gdb_byte>
5863get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5864{
5865 dwarf2_section_info *section = &section_owner->gdb_index;
5866
96b79293 5867 if (section->empty ())
4485a1c1
SM
5868 return {};
5869
5870 /* Older elfutils strip versions could keep the section in the main
5871 executable while splitting it for the separate debug info file. */
96b79293 5872 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5873 return {};
5874
96b79293 5875 section->read (obj);
4485a1c1 5876
8bebfcda
PA
5877 /* dwarf2_section_info::size is a bfd_size_type, while
5878 gdb::array_view works with size_t. On 32-bit hosts, with
5879 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5880 is 32-bit. So we need an explicit narrowing conversion here.
5881 This is fine, because it's impossible to allocate or mmap an
5882 array/buffer larger than what size_t can represent. */
5883 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5884}
5885
87d6a7aa
SM
5886/* Lookup the index cache for the contents of the index associated to
5887 DWARF2_OBJ. */
5888
5889static gdb::array_view<const gdb_byte>
5989a64e 5890get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5891{
5892 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5893 if (build_id == nullptr)
5894 return {};
5895
5896 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5897 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5898}
5899
5900/* Same as the above, but for DWZ. */
5901
5902static gdb::array_view<const gdb_byte>
5903get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5904{
5905 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5906 if (build_id == nullptr)
5907 return {};
5908
5909 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5910}
5911
3c0aa29a 5912/* See symfile.h. */
9291a0cd 5913
3c0aa29a
PA
5914bool
5915dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5916{
ed2dc618
SM
5917 struct dwarf2_per_objfile *dwarf2_per_objfile
5918 = get_dwarf2_per_objfile (objfile);
5919
9291a0cd
TT
5920 /* If we're about to read full symbols, don't bother with the
5921 indices. In this case we also don't care if some other debug
5922 format is making psymtabs, because they are all about to be
5923 expanded anyway. */
5924 if ((objfile->flags & OBJF_READNOW))
5925 {
5989a64e 5926 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5927 create_all_comp_units (dwarf2_per_objfile);
5928 create_all_type_units (dwarf2_per_objfile);
5989a64e 5929 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5930 = create_quick_file_names_table
5989a64e 5931 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5932 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5933
5989a64e
SM
5934 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5935 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5936 {
5989a64e 5937 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5938
5989a64e 5939 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5940 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5941 }
5942
5943 /* Return 1 so that gdb sees the "quick" functions. However,
5944 these functions will be no-ops because we will have expanded
5945 all symtabs. */
3c0aa29a
PA
5946 *index_kind = dw_index_kind::GDB_INDEX;
5947 return true;
9291a0cd
TT
5948 }
5949
ed2dc618 5950 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5951 {
5952 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5953 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5954 return true;
5955 }
927aa2e7 5956
4485a1c1 5957 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5958 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5959 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5960 {
5961 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5962 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5963 return true;
5964 }
9291a0cd 5965
87d6a7aa
SM
5966 /* ... otherwise, try to find the index in the index cache. */
5967 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5968 get_gdb_index_contents_from_cache,
5969 get_gdb_index_contents_from_cache_dwz))
5970 {
5971 global_index_cache.hit ();
5972 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5973 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5974 return true;
5975 }
5976
5977 global_index_cache.miss ();
3c0aa29a 5978 return false;
9291a0cd
TT
5979}
5980
5981\f
5982
dce234bc
PP
5983/* Build a partial symbol table. */
5984
5985void
f29dff0a 5986dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5987{
ed2dc618
SM
5988 struct dwarf2_per_objfile *dwarf2_per_objfile
5989 = get_dwarf2_per_objfile (objfile);
c9bf0622 5990
6eee24ce 5991 init_psymbol_list (objfile, 1024);
c906108c 5992
a70b8144 5993 try
c9bf0622
TT
5994 {
5995 /* This isn't really ideal: all the data we allocate on the
5996 objfile's obstack is still uselessly kept around. However,
5997 freeing it seems unsafe. */
906768f9 5998 psymtab_discarder psymtabs (objfile);
ed2dc618 5999 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6000 psymtabs.keep ();
87d6a7aa 6001
af758d11
SM
6002 dwarf2_per_objfile->resize_symtabs ();
6003
87d6a7aa
SM
6004 /* (maybe) store an index in the cache. */
6005 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6006 }
230d2906 6007 catch (const gdb_exception_error &except)
492d29ea
PA
6008 {
6009 exception_print (gdb_stderr, except);
6010 }
c906108c 6011}
c906108c 6012
3b80fe9b
DE
6013/* Find the base address of the compilation unit for range lists and
6014 location lists. It will normally be specified by DW_AT_low_pc.
6015 In DWARF-3 draft 4, the base address could be overridden by
6016 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6017 compilation units with discontinuous ranges. */
6018
6019static void
6020dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6021{
6022 struct attribute *attr;
6023
2b24b6e4 6024 cu->base_address.reset ();
3b80fe9b
DE
6025
6026 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6027 if (attr != nullptr)
2b24b6e4 6028 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6029 else
6030 {
6031 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6032 if (attr != nullptr)
2b24b6e4 6033 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6034 }
6035}
6036
36586728
TT
6037/* Helper function that returns the proper abbrev section for
6038 THIS_CU. */
6039
6040static struct dwarf2_section_info *
6041get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6042{
6043 struct dwarf2_section_info *abbrev;
c3699833 6044 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6045
6046 if (this_cu->is_dwz)
c3699833 6047 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6048 else
c3699833 6049 abbrev = &per_bfd->abbrev;
36586728
TT
6050
6051 return abbrev;
6052}
6053
f4dc4d17
DE
6054/* Fetch the abbreviation table offset from a comp or type unit header. */
6055
6056static sect_offset
ed2dc618
SM
6057read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6058 struct dwarf2_section_info *section,
9c541725 6059 sect_offset sect_off)
f4dc4d17 6060{
96b79293 6061 bfd *abfd = section->get_bfd_owner ();
d521ce57 6062 const gdb_byte *info_ptr;
ac298888 6063 unsigned int initial_length_size, offset_size;
43988095 6064 uint16_t version;
f4dc4d17 6065
96b79293 6066 section->read (dwarf2_per_objfile->objfile);
9c541725 6067 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6068 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6069 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6070 info_ptr += initial_length_size;
6071
6072 version = read_2_bytes (abfd, info_ptr);
6073 info_ptr += 2;
6074 if (version >= 5)
6075 {
6076 /* Skip unit type and address size. */
6077 info_ptr += 2;
6078 }
6079
24aa364d 6080 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6081}
6082
b83470bf
TT
6083/* A partial symtab that is used only for include files. */
6084struct dwarf2_include_psymtab : public partial_symtab
6085{
6086 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6087 : partial_symtab (filename, objfile)
6088 {
6089 }
6090
6091 void read_symtab (struct objfile *objfile) override
6092 {
194d088f
TV
6093 /* It's an include file, no symbols to read for it.
6094 Everything is in the includer symtab. */
6095
6096 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6097 expansion of the includer psymtab. We use the dependencies[0] field to
6098 model the includer. But if we go the regular route of calling
6099 expand_psymtab here, and having expand_psymtab call expand_dependencies
6100 to expand the includer, we'll only use expand_psymtab on the includer
6101 (making it a non-toplevel psymtab), while if we expand the includer via
6102 another path, we'll use read_symtab (making it a toplevel psymtab).
6103 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6104 psymtab, and trigger read_symtab on the includer here directly. */
6105 includer ()->read_symtab (objfile);
b83470bf
TT
6106 }
6107
6108 void expand_psymtab (struct objfile *objfile) override
6109 {
194d088f
TV
6110 /* This is not called by read_symtab, and should not be called by any
6111 expand_dependencies. */
6112 gdb_assert (false);
b83470bf
TT
6113 }
6114
5717c425 6115 bool readin_p (struct objfile *objfile) const override
b83470bf 6116 {
5717c425 6117 return includer ()->readin_p (objfile);
b83470bf
TT
6118 }
6119
5717c425 6120 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6121 {
6122 return nullptr;
6123 }
6124
6125private:
194d088f
TV
6126 partial_symtab *includer () const
6127 {
6128 /* An include psymtab has exactly one dependency: the psymtab that
6129 includes it. */
6130 gdb_assert (this->number_of_dependencies == 1);
6131 return this->dependencies[0];
6132 }
b83470bf
TT
6133};
6134
aaa75496
JB
6135/* Allocate a new partial symtab for file named NAME and mark this new
6136 partial symtab as being an include of PST. */
6137
6138static void
891813be 6139dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6140 struct objfile *objfile)
6141{
b83470bf 6142 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6143
fbd9ab74 6144 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6145 subpst->dirname = pst->dirname;
fbd9ab74 6146
a9342b62 6147 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6148 subpst->dependencies[0] = pst;
6149 subpst->number_of_dependencies = 1;
aaa75496
JB
6150}
6151
6152/* Read the Line Number Program data and extract the list of files
6153 included by the source file represented by PST. Build an include
d85a05f0 6154 partial symtab for each of these included files. */
aaa75496
JB
6155
6156static void
6157dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6158 struct die_info *die,
891813be 6159 dwarf2_psymtab *pst)
aaa75496 6160{
fff8551c 6161 line_header_up lh;
d85a05f0 6162 struct attribute *attr;
aaa75496 6163
d85a05f0 6164 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6165 if (attr != nullptr)
9c541725 6166 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6167 if (lh == NULL)
6168 return; /* No linetable, so no includes. */
6169
79748972
TT
6170 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6171 that we pass in the raw text_low here; that is ok because we're
6172 only decoding the line table to make include partial symtabs, and
6173 so the addresses aren't really used. */
4ae976d1 6174 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6175 pst->raw_text_low (), 1);
aaa75496
JB
6176}
6177
348e048f 6178static hashval_t
52dc124a 6179hash_signatured_type (const void *item)
348e048f 6180{
9a3c8263
SM
6181 const struct signatured_type *sig_type
6182 = (const struct signatured_type *) item;
9a619af0 6183
348e048f 6184 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6185 return sig_type->signature;
348e048f
DE
6186}
6187
6188static int
52dc124a 6189eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6190{
9a3c8263
SM
6191 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6192 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6193
348e048f
DE
6194 return lhs->signature == rhs->signature;
6195}
6196
1fd400ff
TT
6197/* Allocate a hash table for signatured types. */
6198
b0b6a987 6199static htab_up
298e9637 6200allocate_signatured_type_table ()
1fd400ff 6201{
b0b6a987
TT
6202 return htab_up (htab_create_alloc (41,
6203 hash_signatured_type,
6204 eq_signatured_type,
6205 NULL, xcalloc, xfree));
1fd400ff
TT
6206}
6207
d467dd73 6208/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6209
6210static int
d467dd73 6211add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6212{
9a3c8263 6213 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6214 std::vector<signatured_type *> *all_type_units
6215 = (std::vector<signatured_type *> *) datum;
1fd400ff 6216
b2bdb8cf 6217 all_type_units->push_back (sigt);
1fd400ff
TT
6218
6219 return 1;
6220}
6221
78d4d2c5 6222/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6223 and fill them into TYPES_HTAB. It will process only type units,
6224 therefore DW_UT_type. */
c88ee1f0 6225
78d4d2c5 6226static void
ed2dc618
SM
6227create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6228 struct dwo_file *dwo_file,
b0b6a987 6229 dwarf2_section_info *section, htab_up &types_htab,
43988095 6230 rcuh_kind section_kind)
348e048f 6231{
3019eac3 6232 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6233 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6234 bfd *abfd;
6235 const gdb_byte *info_ptr, *end_ptr;
348e048f 6236
4bdcc0c1
DE
6237 abbrev_section = (dwo_file != NULL
6238 ? &dwo_file->sections.abbrev
5989a64e 6239 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6240
b4f54984 6241 if (dwarf_read_debug)
43988095 6242 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6243 section->get_name (),
6244 abbrev_section->get_file_name ());
09406207 6245
96b79293 6246 section->read (objfile);
78d4d2c5 6247 info_ptr = section->buffer;
348e048f 6248
78d4d2c5
JK
6249 if (info_ptr == NULL)
6250 return;
348e048f 6251
78d4d2c5
JK
6252 /* We can't set abfd until now because the section may be empty or
6253 not present, in which case the bfd is unknown. */
96b79293 6254 abfd = section->get_bfd_owner ();
348e048f 6255
c0ab21c2
TT
6256 /* We don't use cutu_reader here because we don't need to read
6257 any dies: the signature is in the header. */
3019eac3 6258
78d4d2c5
JK
6259 end_ptr = info_ptr + section->size;
6260 while (info_ptr < end_ptr)
6261 {
78d4d2c5
JK
6262 struct signatured_type *sig_type;
6263 struct dwo_unit *dwo_tu;
6264 void **slot;
6265 const gdb_byte *ptr = info_ptr;
6266 struct comp_unit_head header;
6267 unsigned int length;
8b70b953 6268
9c541725 6269 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6270
a49dd8dd
JK
6271 /* Initialize it due to a false compiler warning. */
6272 header.signature = -1;
9c541725 6273 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6274
78d4d2c5
JK
6275 /* We need to read the type's signature in order to build the hash
6276 table, but we don't need anything else just yet. */
348e048f 6277
ed2dc618 6278 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6279 abbrev_section, ptr, section_kind);
348e048f 6280
4057dfde 6281 length = header.get_length ();
6caca83c 6282
78d4d2c5
JK
6283 /* Skip dummy type units. */
6284 if (ptr >= info_ptr + length
43988095
JK
6285 || peek_abbrev_code (abfd, ptr) == 0
6286 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6287 {
6288 info_ptr += length;
6289 continue;
6290 }
dee91e82 6291
78d4d2c5
JK
6292 if (types_htab == NULL)
6293 {
6294 if (dwo_file)
298e9637 6295 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6296 else
298e9637 6297 types_htab = allocate_signatured_type_table ();
78d4d2c5 6298 }
8b70b953 6299
78d4d2c5
JK
6300 if (dwo_file)
6301 {
6302 sig_type = NULL;
5989a64e 6303 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6304 struct dwo_unit);
6305 dwo_tu->dwo_file = dwo_file;
43988095 6306 dwo_tu->signature = header.signature;
9c541725 6307 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6308 dwo_tu->section = section;
9c541725 6309 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6310 dwo_tu->length = length;
6311 }
6312 else
6313 {
6314 /* N.B.: type_offset is not usable if this type uses a DWO file.
6315 The real type_offset is in the DWO file. */
6316 dwo_tu = NULL;
5989a64e 6317 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6318 sig_type->signature = header.signature;
9c541725 6319 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6320 sig_type->per_cu.is_debug_types = 1;
6321 sig_type->per_cu.section = section;
9c541725 6322 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6323 sig_type->per_cu.length = length;
6324 }
6325
b0b6a987 6326 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6327 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6328 INSERT);
6329 gdb_assert (slot != NULL);
6330 if (*slot != NULL)
6331 {
9c541725 6332 sect_offset dup_sect_off;
0349ea22 6333
3019eac3
DE
6334 if (dwo_file)
6335 {
78d4d2c5
JK
6336 const struct dwo_unit *dup_tu
6337 = (const struct dwo_unit *) *slot;
6338
9c541725 6339 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6340 }
6341 else
6342 {
78d4d2c5
JK
6343 const struct signatured_type *dup_tu
6344 = (const struct signatured_type *) *slot;
6345
9c541725 6346 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6347 }
8b70b953 6348
b98664d3 6349 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6350 " the entry at offset %s, signature %s"),
6351 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6352 hex_string (header.signature));
78d4d2c5
JK
6353 }
6354 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6355
78d4d2c5 6356 if (dwarf_read_debug > 1)
9d8780f0
SM
6357 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6358 sect_offset_str (sect_off),
43988095 6359 hex_string (header.signature));
3019eac3 6360
78d4d2c5
JK
6361 info_ptr += length;
6362 }
6363}
3019eac3 6364
78d4d2c5
JK
6365/* Create the hash table of all entries in the .debug_types
6366 (or .debug_types.dwo) section(s).
6367 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6368 otherwise it is NULL.
b3c8eb43 6369
78d4d2c5 6370 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6371
78d4d2c5 6372 Note: This function processes DWO files only, not DWP files. */
348e048f 6373
78d4d2c5 6374static void
ed2dc618
SM
6375create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6376 struct dwo_file *dwo_file,
fd5866f6 6377 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6378 htab_up &types_htab)
78d4d2c5 6379{
fd5866f6
SM
6380 for (dwarf2_section_info &section : type_sections)
6381 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6382 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6383}
6384
6385/* Create the hash table of all entries in the .debug_types section,
6386 and initialize all_type_units.
6387 The result is zero if there is an error (e.g. missing .debug_types section),
6388 otherwise non-zero. */
6389
6390static int
ed2dc618 6391create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6392{
b0b6a987 6393 htab_up types_htab;
3019eac3 6394
ed2dc618 6395 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6396 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6397 rcuh_kind::COMPILE);
ed2dc618 6398 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6399 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6400 if (types_htab == NULL)
6401 {
5989a64e 6402 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6403 return 0;
6404 }
6405
5989a64e 6406 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6407
5989a64e
SM
6408 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6409 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6410 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6411
5989a64e 6412 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6413 add_signatured_type_cu_to_table,
5989a64e 6414 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6415
348e048f
DE
6416 return 1;
6417}
6418
5989a64e 6419/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6420 If SLOT is non-NULL, it is the entry to use in the hash table.
6421 Otherwise we find one. */
6422
6423static struct signatured_type *
ed2dc618
SM
6424add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6425 void **slot)
6aa5f3a6 6426{
5989a64e
SM
6427 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6428 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6429 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6430
5989a64e 6431 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6432
af758d11
SM
6433 dwarf2_per_objfile->resize_symtabs ();
6434
5989a64e 6435 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6436 sig_type->signature = sig;
6437 sig_type->per_cu.is_debug_types = 1;
5989a64e 6438 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6439 {
6440 sig_type->per_cu.v.quick =
5989a64e 6441 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6442 struct dwarf2_per_cu_quick_data);
6443 }
6444
6445 if (slot == NULL)
6446 {
5989a64e 6447 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6448 sig_type, INSERT);
6449 }
6450 gdb_assert (*slot == NULL);
6451 *slot = sig_type;
6452 /* The rest of sig_type must be filled in by the caller. */
6453 return sig_type;
6454}
6455
a2ce51a0
DE
6456/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6457 Fill in SIG_ENTRY with DWO_ENTRY. */
6458
6459static void
ed2dc618 6460fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6461 struct signatured_type *sig_entry,
6462 struct dwo_unit *dwo_entry)
6463{
1859c670
SM
6464 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6465
7ee85ab1 6466 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6467 gdb_assert (! sig_entry->per_cu.queued);
6468 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6469 if (per_bfd->using_index)
6aa5f3a6
DE
6470 {
6471 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6472 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6473 }
6474 else
6475 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6476 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6477 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6478 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6479 gdb_assert (sig_entry->dwo_unit == NULL);
6480
6481 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6482 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6483 sig_entry->per_cu.length = dwo_entry->length;
6484 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6485 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6486 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6487 sig_entry->dwo_unit = dwo_entry;
6488}
6489
6490/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6491 If we haven't read the TU yet, create the signatured_type data structure
6492 for a TU to be read in directly from a DWO file, bypassing the stub.
6493 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6494 using .gdb_index, then when reading a CU we want to stay in the DWO file
6495 containing that CU. Otherwise we could end up reading several other DWO
6496 files (due to comdat folding) to process the transitive closure of all the
6497 mentioned TUs, and that can be slow. The current DWO file will have every
6498 type signature that it needs.
a2ce51a0
DE
6499 We only do this for .gdb_index because in the psymtab case we already have
6500 to read all the DWOs to build the type unit groups. */
6501
6502static struct signatured_type *
6503lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6504{
5e22e966 6505 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6506 struct dwo_file *dwo_file;
6507 struct dwo_unit find_dwo_entry, *dwo_entry;
6508 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6509 void **slot;
a2ce51a0 6510
5989a64e 6511 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6512
6aa5f3a6
DE
6513 /* If TU skeletons have been removed then we may not have read in any
6514 TUs yet. */
5989a64e
SM
6515 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6516 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6517
6518 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6519 Use the global signatured_types array to do our own comdat-folding
6520 of types. If this is the first time we're reading this TU, and
6521 the TU has an entry in .gdb_index, replace the recorded data from
6522 .gdb_index with this TU. */
a2ce51a0 6523
a2ce51a0 6524 find_sig_entry.signature = sig;
5989a64e 6525 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6526 &find_sig_entry, INSERT);
9a3c8263 6527 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6528
6529 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6530 read. Don't reassign the global entry to point to this DWO if that's
6531 the case. Also note that if the TU is already being read, it may not
6532 have come from a DWO, the program may be a mix of Fission-compiled
6533 code and non-Fission-compiled code. */
6534
6535 /* Have we already tried to read this TU?
6536 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6537 needn't exist in the global table yet). */
6538 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6539 return sig_entry;
6540
6aa5f3a6
DE
6541 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6542 dwo_unit of the TU itself. */
6543 dwo_file = cu->dwo_unit->dwo_file;
6544
a2ce51a0
DE
6545 /* Ok, this is the first time we're reading this TU. */
6546 if (dwo_file->tus == NULL)
6547 return NULL;
6548 find_dwo_entry.signature = sig;
b0b6a987
TT
6549 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6550 &find_dwo_entry);
a2ce51a0
DE
6551 if (dwo_entry == NULL)
6552 return NULL;
6553
6aa5f3a6
DE
6554 /* If the global table doesn't have an entry for this TU, add one. */
6555 if (sig_entry == NULL)
ed2dc618 6556 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6557
ed2dc618 6558 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6559 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6560 return sig_entry;
6561}
6562
a2ce51a0
DE
6563/* Subroutine of lookup_signatured_type.
6564 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6565 then try the DWP file. If the TU stub (skeleton) has been removed then
6566 it won't be in .gdb_index. */
a2ce51a0
DE
6567
6568static struct signatured_type *
6569lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6570{
5e22e966 6571 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6572 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6573 struct dwo_unit *dwo_entry;
6574 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6575 void **slot;
a2ce51a0 6576
5989a64e 6577 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6578 gdb_assert (dwp_file != NULL);
6579
6aa5f3a6
DE
6580 /* If TU skeletons have been removed then we may not have read in any
6581 TUs yet. */
5989a64e
SM
6582 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6583 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6584
6aa5f3a6 6585 find_sig_entry.signature = sig;
5989a64e 6586 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6587 &find_sig_entry, INSERT);
9a3c8263 6588 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6589
6590 /* Have we already tried to read this TU?
6591 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6592 needn't exist in the global table yet). */
6593 if (sig_entry != NULL)
6594 return sig_entry;
6595
a2ce51a0
DE
6596 if (dwp_file->tus == NULL)
6597 return NULL;
ed2dc618 6598 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6599 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6600 if (dwo_entry == NULL)
6601 return NULL;
6602
ed2dc618
SM
6603 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6604 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6605
a2ce51a0
DE
6606 return sig_entry;
6607}
6608
380bca97 6609/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6610 Returns NULL if signature SIG is not present in the table.
6611 It is up to the caller to complain about this. */
348e048f
DE
6612
6613static struct signatured_type *
a2ce51a0 6614lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6615{
5e22e966 6616 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6617
a2ce51a0 6618 if (cu->dwo_unit
5989a64e 6619 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6620 {
6621 /* We're in a DWO/DWP file, and we're using .gdb_index.
6622 These cases require special processing. */
ed2dc618 6623 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6624 return lookup_dwo_signatured_type (cu, sig);
6625 else
6626 return lookup_dwp_signatured_type (cu, sig);
6627 }
6628 else
6629 {
6630 struct signatured_type find_entry, *entry;
348e048f 6631
5989a64e 6632 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6633 return NULL;
6634 find_entry.signature = sig;
9a3c8263 6635 entry = ((struct signatured_type *)
5989a64e 6636 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6637 &find_entry));
a2ce51a0
DE
6638 return entry;
6639 }
348e048f 6640}
18a8505e 6641
42e7ad6c 6642/* Low level DIE reading support. */
348e048f 6643
d85a05f0
DJ
6644/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6645
6646static void
6647init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6648 struct dwarf2_cu *cu,
3019eac3 6649 struct dwarf2_section_info *section,
685af9cd
TT
6650 struct dwo_file *dwo_file,
6651 struct abbrev_table *abbrev_table)
d85a05f0 6652{
fceca515 6653 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6654 reader->abfd = section->get_bfd_owner ();
d85a05f0 6655 reader->cu = cu;
3019eac3 6656 reader->dwo_file = dwo_file;
dee91e82
DE
6657 reader->die_section = section;
6658 reader->buffer = section->buffer;
f664829e 6659 reader->buffer_end = section->buffer + section->size;
685af9cd 6660 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6661}
6662
c0ab21c2 6663/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6664 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6665 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6666 already.
6667
6668 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6669 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6670 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6671 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6672 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6673 STUB_COMP_DIR may be non-NULL.
3e225074 6674 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6675 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6676 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6677 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6678 kept around for at least as long as *RESULT_READER.
6679
b0c7bfa9
DE
6680 The result is non-zero if a valid (non-dummy) DIE was found. */
6681
6682static int
4ab09049 6683read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6684 struct dwo_unit *dwo_unit,
b0c7bfa9 6685 struct die_info *stub_comp_unit_die,
a2ce51a0 6686 const char *stub_comp_dir,
b0c7bfa9 6687 struct die_reader_specs *result_reader,
d521ce57 6688 const gdb_byte **result_info_ptr,
b0c7bfa9 6689 struct die_info **result_comp_unit_die,
685af9cd 6690 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6691{
4ab09049
SM
6692 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6693 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6694 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6695 bfd *abfd;
d521ce57 6696 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6697 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6698 int i,num_extra_attrs;
6699 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6700 struct die_info *comp_unit_die;
6701
b0aeadb3
DE
6702 /* At most one of these may be provided. */
6703 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6704
b0c7bfa9
DE
6705 /* These attributes aren't processed until later:
6706 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6707 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6708 referenced later. However, these attributes are found in the stub
6709 which we won't have later. In order to not impose this complication
6710 on the rest of the code, we read them here and copy them to the
6711 DWO CU/TU die. */
b0c7bfa9
DE
6712
6713 stmt_list = NULL;
6714 low_pc = NULL;
6715 high_pc = NULL;
6716 ranges = NULL;
6717 comp_dir = NULL;
6718
6719 if (stub_comp_unit_die != NULL)
6720 {
6721 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6722 DWO file. */
4ab09049 6723 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6724 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6725 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6726 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6727 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6728 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6729
a39fdb41 6730 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6731
18a8505e
AT
6732 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6733 here (if needed). We need the value before we can process
6734 DW_AT_ranges. */
a39fdb41 6735 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6736 }
a2ce51a0
DE
6737 else if (stub_comp_dir != NULL)
6738 {
6739 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6740 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6741 comp_dir->name = DW_AT_comp_dir;
6742 comp_dir->form = DW_FORM_string;
6743 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6744 DW_STRING (comp_dir) = stub_comp_dir;
6745 }
b0c7bfa9
DE
6746
6747 /* Set up for reading the DWO CU/TU. */
6748 cu->dwo_unit = dwo_unit;
685af9cd 6749 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6750 section->read (objfile);
6751 abfd = section->get_bfd_owner ();
9c541725
PA
6752 begin_info_ptr = info_ptr = (section->buffer
6753 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6754 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6755
4ab09049 6756 if (per_cu->is_debug_types)
b0c7bfa9 6757 {
4ab09049 6758 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6759
ed2dc618
SM
6760 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6761 &cu->header, section,
b0c7bfa9 6762 dwo_abbrev_section,
43988095 6763 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6764 /* This is not an assert because it can be caused by bad debug info. */
43988095 6765 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6766 {
6767 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6768 " TU at offset %s [in module %s]"),
a2ce51a0 6769 hex_string (sig_type->signature),
43988095 6770 hex_string (cu->header.signature),
9d8780f0 6771 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6772 bfd_get_filename (abfd));
6773 }
9c541725 6774 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6775 /* For DWOs coming from DWP files, we don't know the CU length
6776 nor the type's offset in the TU until now. */
4057dfde 6777 dwo_unit->length = cu->header.get_length ();
9c541725 6778 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6779
6780 /* Establish the type offset that can be used to lookup the type.
6781 For DWO files, we don't know it until now. */
9c541725
PA
6782 sig_type->type_offset_in_section
6783 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6784 }
6785 else
6786 {
ed2dc618
SM
6787 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6788 &cu->header, section,
b0c7bfa9 6789 dwo_abbrev_section,
43988095 6790 info_ptr, rcuh_kind::COMPILE);
9c541725 6791 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6792 /* For DWOs coming from DWP files, we don't know the CU length
6793 until now. */
4057dfde 6794 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6795 }
6796
685af9cd 6797 *result_dwo_abbrev_table
86de1d91
TT
6798 = abbrev_table::read (objfile, dwo_abbrev_section,
6799 cu->header.abbrev_sect_off);
685af9cd
TT
6800 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6801 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6802
6803 /* Read in the die, but leave space to copy over the attributes
6804 from the stub. This has the benefit of simplifying the rest of
6805 the code - all the work to maintain the illusion of a single
6806 DW_TAG_{compile,type}_unit DIE is done here. */
6807 num_extra_attrs = ((stmt_list != NULL)
6808 + (low_pc != NULL)
6809 + (high_pc != NULL)
6810 + (ranges != NULL)
6811 + (comp_dir != NULL));
6812 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6813 num_extra_attrs);
b0c7bfa9
DE
6814
6815 /* Copy over the attributes from the stub to the DIE we just read in. */
6816 comp_unit_die = *result_comp_unit_die;
6817 i = comp_unit_die->num_attrs;
6818 if (stmt_list != NULL)
6819 comp_unit_die->attrs[i++] = *stmt_list;
6820 if (low_pc != NULL)
6821 comp_unit_die->attrs[i++] = *low_pc;
6822 if (high_pc != NULL)
6823 comp_unit_die->attrs[i++] = *high_pc;
6824 if (ranges != NULL)
6825 comp_unit_die->attrs[i++] = *ranges;
6826 if (comp_dir != NULL)
6827 comp_unit_die->attrs[i++] = *comp_dir;
6828 comp_unit_die->num_attrs += num_extra_attrs;
6829
b4f54984 6830 if (dwarf_die_debug)
bf6af496
DE
6831 {
6832 fprintf_unfiltered (gdb_stdlog,
6833 "Read die from %s@0x%x of %s:\n",
96b79293 6834 section->get_name (),
bf6af496
DE
6835 (unsigned) (begin_info_ptr - section->buffer),
6836 bfd_get_filename (abfd));
b4f54984 6837 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6838 }
6839
b0c7bfa9
DE
6840 /* Skip dummy compilation units. */
6841 if (info_ptr >= begin_info_ptr + dwo_unit->length
6842 || peek_abbrev_code (abfd, info_ptr) == 0)
6843 return 0;
6844
6845 *result_info_ptr = info_ptr;
6846 return 1;
6847}
6848
a084a2a6
AT
6849/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6850 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6851 signature is part of the header. */
6852static gdb::optional<ULONGEST>
6853lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6854{
6855 if (cu->header.version >= 5)
6856 return cu->header.signature;
6857 struct attribute *attr;
6858 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6859 if (attr == nullptr)
6860 return gdb::optional<ULONGEST> ();
6861 return DW_UNSND (attr);
6862}
6863
c0ab21c2 6864/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6865 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6866 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6867
6868static struct dwo_unit *
4ab09049 6869lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6870{
4ab09049 6871 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6872 struct dwo_unit *dwo_unit;
c0ab21c2 6873 const char *comp_dir;
b0c7bfa9 6874
a2ce51a0
DE
6875 gdb_assert (cu != NULL);
6876
b0c7bfa9 6877 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6878 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6879 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6880
4ab09049
SM
6881 if (per_cu->is_debug_types)
6882 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6883 else
6884 {
a084a2a6 6885 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6886
a084a2a6 6887 if (!signature.has_value ())
b0c7bfa9
DE
6888 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6889 " [in module %s]"),
4ab09049
SM
6890 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6891
6892 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6893 }
6894
b0c7bfa9
DE
6895 return dwo_unit;
6896}
6897
c0ab21c2 6898/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6899 See it for a description of the parameters.
fcd3b13d 6900 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6901
c0ab21c2 6902void
9e021579
SM
6903cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6904 dwarf2_per_objfile *per_objfile,
6751ebae 6905 int use_existing_cu)
a2ce51a0 6906{
a2ce51a0 6907 struct signatured_type *sig_type;
a2ce51a0
DE
6908
6909 /* Verify we can do the following downcast, and that we have the
6910 data we need. */
6911 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6912 sig_type = (struct signatured_type *) this_cu;
6913 gdb_assert (sig_type->dwo_unit != NULL);
6914
6aa5f3a6
DE
6915 if (use_existing_cu && this_cu->cu != NULL)
6916 {
6917 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6918 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6919 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6920 }
6921 else
6922 {
6923 /* If !use_existing_cu, this_cu->cu must be NULL. */
6924 gdb_assert (this_cu->cu == NULL);
9e021579 6925 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6926 }
6927
6928 /* A future optimization, if needed, would be to use an existing
6929 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6930 could share abbrev tables. */
a2ce51a0 6931
4ab09049 6932 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6933 NULL /* stub_comp_unit_die */,
6934 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6935 this, &info_ptr,
3e225074 6936 &comp_unit_die,
c0ab21c2 6937 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6938 {
6939 /* Dummy die. */
c0ab21c2 6940 dummy_p = true;
a2ce51a0 6941 }
a2ce51a0
DE
6942}
6943
fd820528 6944/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6945 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6946
f4dc4d17
DE
6947 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6948 Otherwise the table specified in the comp unit header is read in and used.
6949 This is an optimization for when we already have the abbrev table.
6950
dee91e82 6951 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6952 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6953
ab432490
SM
6954cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6955 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6956 struct abbrev_table *abbrev_table,
6751ebae 6957 int use_existing_cu,
c0ab21c2
TT
6958 bool skip_partial)
6959 : die_reader_specs {},
6751ebae 6960 m_this_cu (this_cu)
c906108c 6961{
dee91e82 6962 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6963 struct dwarf2_section_info *section = this_cu->section;
96b79293 6964 bfd *abfd = section->get_bfd_owner ();
dee91e82 6965 struct dwarf2_cu *cu;
c0ab21c2 6966 const gdb_byte *begin_info_ptr;
dee91e82 6967 struct signatured_type *sig_type = NULL;
4bdcc0c1 6968 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6969 /* Non-zero if CU currently points to a DWO file and we need to
6970 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6971 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6972 int rereading_dwo_cu = 0;
c906108c 6973
b4f54984 6974 if (dwarf_die_debug)
9d8780f0 6975 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6976 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6977 sect_offset_str (this_cu->sect_off));
09406207 6978
a2ce51a0
DE
6979 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6980 file (instead of going through the stub), short-circuit all of this. */
6981 if (this_cu->reading_dwo_directly)
6982 {
6983 /* Narrow down the scope of possibilities to have to understand. */
6984 gdb_assert (this_cu->is_debug_types);
6985 gdb_assert (abbrev_table == NULL);
9e021579 6986 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6987 return;
6988 }
6989
dee91e82 6990 /* This is cheap if the section is already read in. */
96b79293 6991 section->read (objfile);
dee91e82 6992
9c541725 6993 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6994
6995 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6996
6997 if (use_existing_cu && this_cu->cu != NULL)
6998 {
6999 cu = this_cu->cu;
42e7ad6c
DE
7000 /* If this CU is from a DWO file we need to start over, we need to
7001 refetch the attributes from the skeleton CU.
7002 This could be optimized by retrieving those attributes from when we
7003 were here the first time: the previous comp_unit_die was stored in
7004 comp_unit_obstack. But there's no data yet that we need this
7005 optimization. */
7006 if (cu->dwo_unit != NULL)
7007 rereading_dwo_cu = 1;
dee91e82
DE
7008 }
7009 else
7010 {
7011 /* If !use_existing_cu, this_cu->cu must be NULL. */
7012 gdb_assert (this_cu->cu == NULL);
9e021579 7013 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7014 cu = m_new_cu.get ();
42e7ad6c 7015 }
dee91e82 7016
b0c7bfa9 7017 /* Get the header. */
9c541725 7018 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7019 {
7020 /* We already have the header, there's no need to read it in again. */
9c541725 7021 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7022 }
7023 else
7024 {
3019eac3 7025 if (this_cu->is_debug_types)
dee91e82 7026 {
ed2dc618
SM
7027 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7028 &cu->header, section,
4bdcc0c1 7029 abbrev_section, info_ptr,
43988095 7030 rcuh_kind::TYPE);
dee91e82 7031
42e7ad6c
DE
7032 /* Since per_cu is the first member of struct signatured_type,
7033 we can go from a pointer to one to a pointer to the other. */
7034 sig_type = (struct signatured_type *) this_cu;
43988095 7035 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7036 gdb_assert (sig_type->type_offset_in_tu
7037 == cu->header.type_cu_offset_in_tu);
7038 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7039
42e7ad6c
DE
7040 /* LENGTH has not been set yet for type units if we're
7041 using .gdb_index. */
4057dfde 7042 this_cu->length = cu->header.get_length ();
3019eac3
DE
7043
7044 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7045 sig_type->type_offset_in_section =
7046 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7047
7048 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7049 }
7050 else
7051 {
ed2dc618
SM
7052 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7053 &cu->header, section,
4bdcc0c1 7054 abbrev_section,
43988095
JK
7055 info_ptr,
7056 rcuh_kind::COMPILE);
dee91e82 7057
9c541725 7058 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7059 if (this_cu->length == 0)
7060 this_cu->length = cu->header.get_length ();
7061 else
7062 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7063 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7064 }
7065 }
10b3939b 7066
6caca83c 7067 /* Skip dummy compilation units. */
dee91e82 7068 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7069 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7070 {
7071 dummy_p = true;
7072 return;
7073 }
6caca83c 7074
433df2d4
DE
7075 /* If we don't have them yet, read the abbrevs for this compilation unit.
7076 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7077 done. */
f4dc4d17 7078 if (abbrev_table != NULL)
685af9cd
TT
7079 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7080 else
f4dc4d17 7081 {
c0ab21c2 7082 m_abbrev_table_holder
86de1d91
TT
7083 = abbrev_table::read (objfile, abbrev_section,
7084 cu->header.abbrev_sect_off);
c0ab21c2 7085 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7086 }
af703f96 7087
dee91e82 7088 /* Read the top level CU/TU die. */
c0ab21c2 7089 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7090 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7091
58f0c718 7092 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7093 {
7094 dummy_p = true;
7095 return;
7096 }
58f0c718 7097
b0c7bfa9 7098 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7099 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7100 table from the DWO file and pass the ownership over to us. It will be
7101 referenced from READER, so we must make sure to free it after we're done
7102 with READER.
7103
b0c7bfa9
DE
7104 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7105 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7106 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7107 if (dwo_name != nullptr)
3019eac3 7108 {
3019eac3 7109 struct dwo_unit *dwo_unit;
b0c7bfa9 7110 struct die_info *dwo_comp_unit_die;
3019eac3 7111
3e225074 7112 if (comp_unit_die->has_children)
6a506a2d 7113 {
b98664d3 7114 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7115 " has children (offset %s) [in module %s]"),
7116 sect_offset_str (this_cu->sect_off),
7117 bfd_get_filename (abfd));
6a506a2d 7118 }
4ab09049 7119 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7120 if (dwo_unit != NULL)
3019eac3 7121 {
4ab09049 7122 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7123 comp_unit_die, NULL,
c0ab21c2 7124 this, &info_ptr,
3e225074 7125 &dwo_comp_unit_die,
c0ab21c2 7126 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7127 {
7128 /* Dummy die. */
c0ab21c2 7129 dummy_p = true;
6a506a2d
DE
7130 return;
7131 }
7132 comp_unit_die = dwo_comp_unit_die;
7133 }
7134 else
7135 {
7136 /* Yikes, we couldn't find the rest of the DIE, we only have
7137 the stub. A complaint has already been logged. There's
7138 not much more we can do except pass on the stub DIE to
7139 die_reader_func. We don't want to throw an error on bad
7140 debug info. */
3019eac3
DE
7141 }
7142 }
c0ab21c2 7143}
3019eac3 7144
6751ebae
TT
7145void
7146cutu_reader::keep ()
c0ab21c2 7147{
b0c7bfa9 7148 /* Done, clean up. */
6751ebae
TT
7149 gdb_assert (!dummy_p);
7150 if (m_new_cu != NULL)
348e048f 7151 {
e3beb21d
SM
7152 /* We know that m_this_cu->cu is set, since we are in the process of
7153 parsing the CU. */
7154 gdb_assert (m_this_cu->cu != nullptr);
7155 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7156
fcd3b13d 7157 /* Link this CU into read_in_chain. */
5989a64e
SM
7158 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7159 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7160 /* The chain owns it now. */
c0ab21c2 7161 m_new_cu.release ();
348e048f 7162 }
dee91e82
DE
7163}
7164
18a8505e
AT
7165/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7166 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7167 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7168
7169 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7170 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7171
7172 We fill in THIS_CU->length.
7173
dee91e82 7174 THIS_CU->cu is always freed when done.
3019eac3 7175 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7176 to care whether it refers to the "main" CU or the DWO CU.
7177
7178 When parent_cu is passed, it is used to provide a default value for
7179 str_offsets_base and addr_base from the parent. */
dee91e82 7180
ab432490
SM
7181cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7182 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7183 struct dwarf2_cu *parent_cu,
7184 struct dwo_file *dwo_file)
7185 : die_reader_specs {},
7186 m_this_cu (this_cu)
dee91e82
DE
7187{
7188 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7189 struct dwarf2_section_info *section = this_cu->section;
96b79293 7190 bfd *abfd = section->get_bfd_owner ();
33e80786 7191 struct dwarf2_section_info *abbrev_section;
d521ce57 7192 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7193
b4f54984 7194 if (dwarf_die_debug)
9d8780f0 7195 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7196 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7197 sect_offset_str (this_cu->sect_off));
09406207 7198
dee91e82
DE
7199 gdb_assert (this_cu->cu == NULL);
7200
33e80786
DE
7201 abbrev_section = (dwo_file != NULL
7202 ? &dwo_file->sections.abbrev
7203 : get_abbrev_section_for_cu (this_cu));
7204
dee91e82 7205 /* This is cheap if the section is already read in. */
96b79293 7206 section->read (objfile);
dee91e82 7207
9e021579 7208 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7209
9c541725 7210 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7211 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7212 &m_new_cu->header, section,
4bdcc0c1 7213 abbrev_section, info_ptr,
43988095
JK
7214 (this_cu->is_debug_types
7215 ? rcuh_kind::TYPE
7216 : rcuh_kind::COMPILE));
dee91e82 7217
18a8505e
AT
7218 if (parent_cu != nullptr)
7219 {
c0ab21c2
TT
7220 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7221 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7222 }
4057dfde 7223 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7224
7225 /* Skip dummy compilation units. */
7226 if (info_ptr >= begin_info_ptr + this_cu->length
7227 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7228 {
7229 dummy_p = true;
7230 return;
7231 }
72bf9492 7232
c0ab21c2 7233 m_abbrev_table_holder
86de1d91
TT
7234 = abbrev_table::read (objfile, abbrev_section,
7235 m_new_cu->header.abbrev_sect_off);
dee91e82 7236
c0ab21c2
TT
7237 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7238 m_abbrev_table_holder.get ());
3e225074 7239 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7240}
7241
0018ea6f
DE
7242\f
7243/* Type Unit Groups.
dee91e82 7244
0018ea6f
DE
7245 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7246 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7247 so that all types coming from the same compilation (.o file) are grouped
7248 together. A future step could be to put the types in the same symtab as
7249 the CU the types ultimately came from. */
ff013f42 7250
f4dc4d17
DE
7251static hashval_t
7252hash_type_unit_group (const void *item)
7253{
9a3c8263
SM
7254 const struct type_unit_group *tu_group
7255 = (const struct type_unit_group *) item;
f4dc4d17 7256
094b34ac 7257 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7258}
348e048f
DE
7259
7260static int
f4dc4d17 7261eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7262{
9a3c8263
SM
7263 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7264 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7265
094b34ac 7266 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7267}
348e048f 7268
f4dc4d17
DE
7269/* Allocate a hash table for type unit groups. */
7270
eaa5fa8b 7271static htab_up
298e9637 7272allocate_type_unit_groups_table ()
f4dc4d17 7273{
eaa5fa8b
TT
7274 return htab_up (htab_create_alloc (3,
7275 hash_type_unit_group,
7276 eq_type_unit_group,
7277 NULL, xcalloc, xfree));
f4dc4d17 7278}
dee91e82 7279
f4dc4d17
DE
7280/* Type units that don't have DW_AT_stmt_list are grouped into their own
7281 partial symtabs. We combine several TUs per psymtab to not let the size
7282 of any one psymtab grow too big. */
7283#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7284#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7285
094b34ac 7286/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7287 Create the type_unit_group object used to hold one or more TUs. */
7288
7289static struct type_unit_group *
094b34ac 7290create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7291{
5e22e966 7292 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7293 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7294 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7295 struct type_unit_group *tu_group;
f4dc4d17 7296
5989a64e 7297 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7298 struct type_unit_group);
094b34ac 7299 per_cu = &tu_group->per_cu;
1859c670 7300 per_cu->per_bfd = per_bfd;
f4dc4d17 7301
1859c670 7302 if (per_bfd->using_index)
094b34ac 7303 {
1859c670 7304 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7305 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7306 }
7307 else
7308 {
9c541725 7309 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7310 dwarf2_psymtab *pst;
528e1572 7311 std::string name;
094b34ac
DE
7312
7313 /* Give the symtab a useful name for debug purposes. */
7314 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7315 name = string_printf ("<type_units_%d>",
7316 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7317 else
528e1572 7318 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7319
7aa104c4 7320 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7321 pst->anonymous = true;
094b34ac 7322 }
f4dc4d17 7323
094b34ac 7324 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7325 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7326
7327 return tu_group;
7328}
7329
094b34ac
DE
7330/* Look up the type_unit_group for type unit CU, and create it if necessary.
7331 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7332
7333static struct type_unit_group *
ff39bb5e 7334get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7335{
5e22e966 7336 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7337 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7338 struct type_unit_group *tu_group;
7339 void **slot;
7340 unsigned int line_offset;
7341 struct type_unit_group type_unit_group_for_lookup;
7342
5989a64e
SM
7343 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7344 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7345
7346 /* Do we need to create a new group, or can we use an existing one? */
7347
7348 if (stmt_list)
7349 {
7350 line_offset = DW_UNSND (stmt_list);
7351 ++tu_stats->nr_symtab_sharers;
7352 }
7353 else
7354 {
7355 /* Ugh, no stmt_list. Rare, but we have to handle it.
7356 We can do various things here like create one group per TU or
7357 spread them over multiple groups to split up the expansion work.
7358 To avoid worst case scenarios (too many groups or too large groups)
7359 we, umm, group them in bunches. */
7360 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7361 | (tu_stats->nr_stmt_less_type_units
7362 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7363 ++tu_stats->nr_stmt_less_type_units;
7364 }
7365
094b34ac 7366 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7367 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7368 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7369 &type_unit_group_for_lookup, INSERT);
7370 if (*slot != NULL)
7371 {
9a3c8263 7372 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7373 gdb_assert (tu_group != NULL);
7374 }
7375 else
7376 {
9c541725 7377 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7378 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7379 *slot = tu_group;
7380 ++tu_stats->nr_symtabs;
7381 }
7382
7383 return tu_group;
7384}
0018ea6f
DE
7385\f
7386/* Partial symbol tables. */
7387
7388/* Create a psymtab named NAME and assign it to PER_CU.
7389
7390 The caller must fill in the following details:
7391 dirname, textlow, texthigh. */
7392
891813be 7393static dwarf2_psymtab *
7aa104c4
SM
7394create_partial_symtab (dwarf2_per_cu_data *per_cu,
7395 dwarf2_per_objfile *per_objfile,
7396 const char *name)
0018ea6f 7397{
7aa104c4 7398 struct objfile *objfile = per_objfile->objfile;
891813be 7399 dwarf2_psymtab *pst;
0018ea6f 7400
9f4e76a4 7401 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7402
6d94535f 7403 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7404
7405 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7406 per_cu->v.psymtab = pst;
7407
7408 return pst;
7409}
7410
c0ab21c2 7411/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7412
7413static void
7414process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7415 const gdb_byte *info_ptr,
0018ea6f 7416 struct die_info *comp_unit_die,
c0ab21c2 7417 enum language pretend_language)
0018ea6f
DE
7418{
7419 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7420 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7421 struct objfile *objfile = per_objfile->objfile;
08feed99 7422 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7423 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7424 CORE_ADDR baseaddr;
7425 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7426 dwarf2_psymtab *pst;
3a2b436a 7427 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7428 const char *filename;
0018ea6f 7429
0018ea6f
DE
7430 gdb_assert (! per_cu->is_debug_types);
7431
c0ab21c2 7432 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7433
0018ea6f 7434 /* Allocate a new partial symbol table structure. */
2e927613
TV
7435 gdb::unique_xmalloc_ptr<char> debug_filename;
7436 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7437 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7438 if (filename == NULL)
0018ea6f 7439 filename = "";
2e927613
TV
7440 else if (strcmp (filename, artificial) == 0)
7441 {
7442 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7443 sect_offset_str (per_cu->sect_off),
7444 (char *) NULL));
2e927613
TV
7445 filename = debug_filename.get ();
7446 }
0018ea6f 7447
7aa104c4 7448 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7449
7450 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7451 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7452
b3b3bada 7453 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7454
7455 dwarf2_find_base_address (comp_unit_die, cu);
7456
7457 /* Possibly set the default values of LOWPC and HIGHPC from
7458 `DW_AT_ranges'. */
3a2b436a
JK
7459 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7460 &best_highpc, cu, pst);
7461 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7462 {
7463 CORE_ADDR low
7464 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7465 - baseaddr);
7466 CORE_ADDR high
7467 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7468 - baseaddr - 1);
7469 /* Store the contiguous range if it is not empty; it can be
7470 empty for CUs with no code. */
d320c2b5
TT
7471 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7472 low, high, pst);
79748972 7473 }
0018ea6f
DE
7474
7475 /* Check if comp unit has_children.
7476 If so, read the rest of the partial symbols from this comp unit.
7477 If not, there's no more debug_info for this comp unit. */
3e225074 7478 if (comp_unit_die->has_children)
0018ea6f
DE
7479 {
7480 struct partial_die_info *first_die;
7481 CORE_ADDR lowpc, highpc;
7482
7483 lowpc = ((CORE_ADDR) -1);
7484 highpc = ((CORE_ADDR) 0);
7485
7486 first_die = load_partial_dies (reader, info_ptr, 1);
7487
7488 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7489 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7490
7491 /* If we didn't find a lowpc, set it to highpc to avoid
7492 complaints from `maint check'. */
7493 if (lowpc == ((CORE_ADDR) -1))
7494 lowpc = highpc;
7495
7496 /* If the compilation unit didn't have an explicit address range,
7497 then use the information extracted from its child dies. */
e385593e 7498 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7499 {
7500 best_lowpc = lowpc;
7501 best_highpc = highpc;
7502 }
7503 }
4ae976d1 7504 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7505 best_lowpc + baseaddr)
7506 - baseaddr);
4ae976d1 7507 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7508 best_highpc + baseaddr)
7509 - baseaddr);
0018ea6f 7510
8763cede 7511 end_psymtab_common (objfile, pst);
0018ea6f 7512
ae640021 7513 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7514 {
7515 int i;
ae640021 7516 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7517
7518 /* Fill in 'dependencies' here; we fill in 'users' in a
7519 post-pass. */
7520 pst->number_of_dependencies = len;
a9342b62
TT
7521 pst->dependencies
7522 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7523 for (i = 0; i < len; ++i)
7524 {
7525 pst->dependencies[i]
7526 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7527 }
0018ea6f 7528
ae640021 7529 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7530 }
7531
7532 /* Get the list of files included in the current compilation unit,
7533 and build a psymtab for each of them. */
7534 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7535
b4f54984 7536 if (dwarf_read_debug)
b926417a
TT
7537 fprintf_unfiltered (gdb_stdlog,
7538 "Psymtab for %s unit @%s: %s - %s"
7539 ", %d global, %d static syms\n",
7540 per_cu->is_debug_types ? "type" : "comp",
7541 sect_offset_str (per_cu->sect_off),
7542 paddress (gdbarch, pst->text_low (objfile)),
7543 paddress (gdbarch, pst->text_high (objfile)),
7544 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7545}
7546
7547/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7548 Process compilation unit THIS_CU for a psymtab. */
7549
7550static void
ab432490
SM
7551process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7552 dwarf2_per_objfile *per_objfile,
135f5437 7553 bool want_partial_unit,
b93601f3 7554 enum language pretend_language)
0018ea6f
DE
7555{
7556 /* If this compilation unit was already read in, free the
7557 cached copy in order to read it in again. This is
7558 necessary because we skipped some symbols when we first
7559 read in the compilation unit (see load_partial_dies).
7560 This problem could be avoided, but the benefit is unclear. */
7561 if (this_cu->cu != NULL)
120ce1b5 7562 free_one_cached_comp_unit (this_cu, per_objfile);
0018ea6f 7563
ab432490 7564 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7565
58990295
TV
7566 switch (reader.comp_unit_die->tag)
7567 {
7568 case DW_TAG_compile_unit:
7569 this_cu->unit_type = DW_UT_compile;
7570 break;
7571 case DW_TAG_partial_unit:
7572 this_cu->unit_type = DW_UT_partial;
7573 break;
7574 default:
7575 abort ();
7576 }
7577
c0ab21c2 7578 if (reader.dummy_p)
f1902523 7579 {
c0ab21c2 7580 /* Nothing. */
f1902523 7581 }
c0ab21c2 7582 else if (this_cu->is_debug_types)
3e225074
TT
7583 build_type_psymtabs_reader (&reader, reader.info_ptr,
7584 reader.comp_unit_die);
135f5437
TT
7585 else if (want_partial_unit
7586 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7587 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die,
c0ab21c2 7589 pretend_language);
0018ea6f 7590
58990295
TV
7591 this_cu->lang = this_cu->cu->language;
7592
0018ea6f 7593 /* Age out any secondary CUs. */
f6e649dd 7594 age_cached_comp_units (per_objfile);
0018ea6f 7595}
f4dc4d17
DE
7596
7597/* Reader function for build_type_psymtabs. */
7598
7599static void
7600build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7601 const gdb_byte *info_ptr,
3e225074 7602 struct die_info *type_unit_die)
f4dc4d17 7603{
5e22e966 7604 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7605 struct objfile *objfile = dwarf2_per_objfile->objfile;
7606 struct dwarf2_cu *cu = reader->cu;
7607 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7608 struct signatured_type *sig_type;
f4dc4d17
DE
7609 struct type_unit_group *tu_group;
7610 struct attribute *attr;
7611 struct partial_die_info *first_die;
7612 CORE_ADDR lowpc, highpc;
891813be 7613 dwarf2_psymtab *pst;
f4dc4d17 7614
0186c6a7
DE
7615 gdb_assert (per_cu->is_debug_types);
7616 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7617
3e225074 7618 if (! type_unit_die->has_children)
f4dc4d17
DE
7619 return;
7620
052c8bb8 7621 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7622 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7623
df07e2c7 7624 if (tu_group->tus == nullptr)
a8b3b8e9 7625 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7626 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7627
7628 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7629 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7630 pst->anonymous = true;
f4dc4d17
DE
7631
7632 first_die = load_partial_dies (reader, info_ptr, 1);
7633
7634 lowpc = (CORE_ADDR) -1;
7635 highpc = (CORE_ADDR) 0;
7636 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7637
8763cede 7638 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7639}
7640
73051182
DE
7641/* Struct used to sort TUs by their abbreviation table offset. */
7642
7643struct tu_abbrev_offset
7644{
b2bdb8cf
SM
7645 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7646 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7647 {}
7648
7649 signatured_type *sig_type;
73051182
DE
7650 sect_offset abbrev_offset;
7651};
7652
484cf504 7653/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7654
484cf504
TT
7655static bool
7656sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7657 const struct tu_abbrev_offset &b)
73051182 7658{
484cf504 7659 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7660}
7661
7662/* Efficiently read all the type units.
7663 This does the bulk of the work for build_type_psymtabs.
7664
7665 The efficiency is because we sort TUs by the abbrev table they use and
7666 only read each abbrev table once. In one program there are 200K TUs
7667 sharing 8K abbrev tables.
7668
7669 The main purpose of this function is to support building the
5989a64e 7670 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7671 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7672 can collapse the search space by grouping them by stmt_list.
7673 The savings can be significant, in the same program from above the 200K TUs
7674 share 8K stmt_list tables.
7675
7676 FUNC is expected to call get_type_unit_group, which will create the
7677 struct type_unit_group if necessary and add it to
5989a64e 7678 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7679
7680static void
ed2dc618 7681build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7682{
5989a64e 7683 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7684 abbrev_table_up abbrev_table;
73051182 7685 sect_offset abbrev_offset;
73051182
DE
7686
7687 /* It's up to the caller to not call us multiple times. */
5989a64e 7688 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7689
5989a64e 7690 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7691 return;
7692
7693 /* TUs typically share abbrev tables, and there can be way more TUs than
7694 abbrev tables. Sort by abbrev table to reduce the number of times we
7695 read each abbrev table in.
7696 Alternatives are to punt or to maintain a cache of abbrev tables.
7697 This is simpler and efficient enough for now.
7698
7699 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7700 symtab to use). Typically TUs with the same abbrev offset have the same
7701 stmt_list value too so in practice this should work well.
7702
7703 The basic algorithm here is:
7704
7705 sort TUs by abbrev table
7706 for each TU with same abbrev table:
7707 read abbrev table if first user
7708 read TU top level DIE
7709 [IWBN if DWO skeletons had DW_AT_stmt_list]
7710 call FUNC */
7711
b4f54984 7712 if (dwarf_read_debug)
73051182
DE
7713 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7714
7715 /* Sort in a separate table to maintain the order of all_type_units
7716 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7717 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7718 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7719
5989a64e 7720 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7721 sorted_by_abbrev.emplace_back
7722 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7723 sig_type->per_cu.section,
7724 sig_type->per_cu.sect_off));
73051182 7725
484cf504
TT
7726 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7727 sort_tu_by_abbrev_offset);
73051182 7728
9c541725 7729 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7730
b2bdb8cf 7731 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7732 {
73051182
DE
7733 /* Switch to the next abbrev table if necessary. */
7734 if (abbrev_table == NULL
b2bdb8cf 7735 || tu.abbrev_offset != abbrev_offset)
73051182 7736 {
b2bdb8cf 7737 abbrev_offset = tu.abbrev_offset;
73051182 7738 abbrev_table =
86de1d91 7739 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7740 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7741 abbrev_offset);
73051182
DE
7742 ++tu_stats->nr_uniq_abbrev_tables;
7743 }
7744
ab432490
SM
7745 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7746 abbrev_table.get (), 0, false);
c0ab21c2
TT
7747 if (!reader.dummy_p)
7748 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7749 reader.comp_unit_die);
73051182 7750 }
6aa5f3a6 7751}
73051182 7752
6aa5f3a6
DE
7753/* Print collected type unit statistics. */
7754
7755static void
ed2dc618 7756print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7757{
5989a64e 7758 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7759
7760 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7761 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7762 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7763 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7764 tu_stats->nr_uniq_abbrev_tables);
7765 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7766 tu_stats->nr_symtabs);
7767 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7768 tu_stats->nr_symtab_sharers);
7769 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7770 tu_stats->nr_stmt_less_type_units);
7771 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7772 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7773}
7774
f4dc4d17
DE
7775/* Traversal function for build_type_psymtabs. */
7776
7777static int
7778build_type_psymtab_dependencies (void **slot, void *info)
7779{
ed2dc618
SM
7780 struct dwarf2_per_objfile *dwarf2_per_objfile
7781 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7782 struct objfile *objfile = dwarf2_per_objfile->objfile;
7783 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7784 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7785 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7786 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7787 int i;
7788
7789 gdb_assert (len > 0);
197400e8 7790 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7791
7792 pst->number_of_dependencies = len;
a9342b62 7793 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7794 for (i = 0; i < len; ++i)
f4dc4d17 7795 {
df07e2c7 7796 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7797 gdb_assert (iter->per_cu.is_debug_types);
7798 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7799 iter->type_unit_group = tu_group;
f4dc4d17
DE
7800 }
7801
df07e2c7
AB
7802 delete tu_group->tus;
7803 tu_group->tus = nullptr;
348e048f
DE
7804
7805 return 1;
7806}
7807
7808/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7809 Build partial symbol tables for the .debug_types comp-units. */
7810
7811static void
ed2dc618 7812build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7813{
ed2dc618 7814 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7815 return;
7816
ed2dc618 7817 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7818}
f4dc4d17 7819
6aa5f3a6
DE
7820/* Traversal function for process_skeletonless_type_unit.
7821 Read a TU in a DWO file and build partial symbols for it. */
7822
7823static int
7824process_skeletonless_type_unit (void **slot, void *info)
7825{
7826 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7827 struct dwarf2_per_objfile *dwarf2_per_objfile
7828 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7829 struct signatured_type find_entry, *entry;
7830
7831 /* If this TU doesn't exist in the global table, add it and read it in. */
7832
5989a64e
SM
7833 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7834 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7835
7836 find_entry.signature = dwo_unit->signature;
5989a64e 7837 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7838 &find_entry, INSERT);
6aa5f3a6
DE
7839 /* If we've already seen this type there's nothing to do. What's happening
7840 is we're doing our own version of comdat-folding here. */
7841 if (*slot != NULL)
7842 return 1;
7843
7844 /* This does the job that create_all_type_units would have done for
7845 this TU. */
ed2dc618
SM
7846 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7847 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7848 *slot = entry;
7849
7850 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7851 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7852 if (!reader.dummy_p)
7853 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7854 reader.comp_unit_die);
6aa5f3a6
DE
7855
7856 return 1;
7857}
7858
7859/* Traversal function for process_skeletonless_type_units. */
7860
7861static int
7862process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7863{
7864 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7865
7866 if (dwo_file->tus != NULL)
b0b6a987
TT
7867 htab_traverse_noresize (dwo_file->tus.get (),
7868 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7869
7870 return 1;
7871}
7872
7873/* Scan all TUs of DWO files, verifying we've processed them.
7874 This is needed in case a TU was emitted without its skeleton.
7875 Note: This can't be done until we know what all the DWO files are. */
7876
7877static void
ed2dc618 7878process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7879{
7880 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7881 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7882 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7883 {
5989a64e 7884 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7885 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7886 dwarf2_per_objfile);
6aa5f3a6 7887 }
348e048f
DE
7888}
7889
ed2dc618 7890/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7891
7892static void
ed2dc618 7893set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7894{
5989a64e 7895 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7896 {
891813be 7897 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7898
36586728
TT
7899 if (pst == NULL)
7900 continue;
7901
b76e467d 7902 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7903 {
7904 /* Set the 'user' field only if it is not already set. */
7905 if (pst->dependencies[j]->user == NULL)
7906 pst->dependencies[j]->user = pst;
7907 }
7908 }
7909}
7910
93311388
DE
7911/* Build the partial symbol table by doing a quick pass through the
7912 .debug_info and .debug_abbrev sections. */
72bf9492 7913
93311388 7914static void
ed2dc618 7915dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7916{
ed2dc618 7917 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7918
b4f54984 7919 if (dwarf_read_debug)
45cfd468
DE
7920 {
7921 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7922 objfile_name (objfile));
45cfd468
DE
7923 }
7924
76935768 7925 scoped_restore restore_reading_psyms
5989a64e 7926 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7927 true);
98bfdba5 7928
5989a64e 7929 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7930
93311388
DE
7931 /* Any cached compilation units will be linked by the per-objfile
7932 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7933 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7934
ed2dc618 7935 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7936
ed2dc618 7937 create_all_comp_units (dwarf2_per_objfile);
c906108c 7938
60606b2c
TT
7939 /* Create a temporary address map on a temporary obstack. We later
7940 copy this to the final obstack. */
8268c778 7941 auto_obstack temp_obstack;
791afaa2
TT
7942
7943 scoped_restore save_psymtabs_addrmap
d320c2b5 7944 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7945 addrmap_create_mutable (&temp_obstack));
72bf9492 7946
5989a64e 7947 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7948 {
7949 if (per_cu->v.psymtab != NULL)
7950 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7951 continue;
ab432490
SM
7952 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7953 language_minimal);
3d5afab3 7954 }
ff013f42 7955
6aa5f3a6 7956 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7957 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7958
7959 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7960 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7961 {
5989a64e 7962 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7963 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7964 }
7965
b4f54984 7966 if (dwarf_read_debug)
ed2dc618 7967 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7968
ed2dc618 7969 set_partial_user (dwarf2_per_objfile);
95554aad 7970
d320c2b5
TT
7971 objfile->partial_symtabs->psymtabs_addrmap
7972 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7973 objfile->partial_symtabs->obstack ());
791afaa2
TT
7974 /* At this point we want to keep the address map. */
7975 save_psymtabs_addrmap.release ();
ff013f42 7976
b4f54984 7977 if (dwarf_read_debug)
45cfd468 7978 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7979 objfile_name (objfile));
ae038cb0
DJ
7980}
7981
dee91e82
DE
7982/* Load the partial DIEs for a secondary CU into memory.
7983 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7984
dee91e82 7985static void
ab432490
SM
7986load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7987 dwarf2_per_objfile *per_objfile)
dee91e82 7988{
ab432490 7989 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
7990
7991 if (!reader.dummy_p)
7992 {
7993 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7994 language_minimal);
7995
7996 /* Check if comp unit has_children.
7997 If so, read the rest of the partial symbols from this comp unit.
7998 If not, there's no more debug_info for this comp unit. */
3e225074 7999 if (reader.comp_unit_die->has_children)
c0ab21c2 8000 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8001
8002 reader.keep ();
c0ab21c2 8003 }
ae038cb0
DJ
8004}
8005
ae038cb0 8006static void
ed2dc618 8007read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8008 struct dwarf2_section_info *section,
f1902523 8009 struct dwarf2_section_info *abbrev_section,
b76e467d 8010 unsigned int is_dwz)
ae038cb0 8011{
d521ce57 8012 const gdb_byte *info_ptr;
ed2dc618 8013 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8014
b4f54984 8015 if (dwarf_read_debug)
bf6af496 8016 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8017 section->get_name (),
8018 section->get_file_name ());
bf6af496 8019
96b79293 8020 section->read (objfile);
ae038cb0 8021
36586728 8022 info_ptr = section->buffer;
6e70227d 8023
36586728 8024 while (info_ptr < section->buffer + section->size)
ae038cb0 8025 {
ae038cb0 8026 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8027
9c541725 8028 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8029
f1902523 8030 comp_unit_head cu_header;
ed2dc618
SM
8031 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8032 abbrev_section, info_ptr,
8033 rcuh_kind::COMPILE);
ae038cb0
DJ
8034
8035 /* Save the compilation unit for later lookup. */
f1902523 8036 if (cu_header.unit_type != DW_UT_type)
5989a64e 8037 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8038 else
8039 {
5989a64e 8040 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8041 sig_type->signature = cu_header.signature;
8042 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8043 this_cu = &sig_type->per_cu;
8044 }
8045 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8046 this_cu->sect_off = sect_off;
f1902523 8047 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8048 this_cu->is_dwz = is_dwz;
8a0459fd 8049 this_cu->section = section;
ae038cb0 8050
5989a64e 8051 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8052
8053 info_ptr = info_ptr + this_cu->length;
8054 }
36586728
TT
8055}
8056
8057/* Create a list of all compilation units in OBJFILE.
8058 This is only done for -readnow and building partial symtabs. */
8059
8060static void
ed2dc618 8061create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8062{
5989a64e
SM
8063 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8064 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8065 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8066
c3699833 8067 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8068 if (dwz != NULL)
ed2dc618 8069 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8070 1);
c906108c
SS
8071}
8072
5734ee8b 8073/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8074 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8075 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8076 DW_AT_ranges). See the comments of add_partial_subprogram on how
8077 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8078
72bf9492
DJ
8079static void
8080scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8081 CORE_ADDR *highpc, int set_addrmap,
8082 struct dwarf2_cu *cu)
c906108c 8083{
72bf9492 8084 struct partial_die_info *pdi;
c906108c 8085
91c24f0a
DC
8086 /* Now, march along the PDI's, descending into ones which have
8087 interesting children but skipping the children of the other ones,
8088 until we reach the end of the compilation unit. */
c906108c 8089
72bf9492 8090 pdi = first_die;
91c24f0a 8091
72bf9492
DJ
8092 while (pdi != NULL)
8093 {
52356b79 8094 pdi->fixup (cu);
c906108c 8095
f55ee35c 8096 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8097 children, so we need to look at them. Ditto for anonymous
8098 enums. */
933c6fe4 8099
72bf9492 8100 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8101 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8102 || pdi->tag == DW_TAG_imported_unit
8103 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8104 {
72bf9492 8105 switch (pdi->tag)
c906108c
SS
8106 {
8107 case DW_TAG_subprogram:
b1dc1806 8108 case DW_TAG_inlined_subroutine:
cdc07690 8109 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8110 break;
72929c62 8111 case DW_TAG_constant:
c906108c
SS
8112 case DW_TAG_variable:
8113 case DW_TAG_typedef:
91c24f0a 8114 case DW_TAG_union_type:
317d2668
TV
8115 if (!pdi->is_declaration
8116 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8117 {
72bf9492 8118 add_partial_symbol (pdi, cu);
63d06c5c
DC
8119 }
8120 break;
c906108c 8121 case DW_TAG_class_type:
680b30c7 8122 case DW_TAG_interface_type:
c906108c 8123 case DW_TAG_structure_type:
72bf9492 8124 if (!pdi->is_declaration)
c906108c 8125 {
72bf9492 8126 add_partial_symbol (pdi, cu);
c906108c 8127 }
b7fee5a3
KS
8128 if ((cu->language == language_rust
8129 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8130 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8131 set_addrmap, cu);
c906108c 8132 break;
91c24f0a 8133 case DW_TAG_enumeration_type:
72bf9492
DJ
8134 if (!pdi->is_declaration)
8135 add_partial_enumeration (pdi, cu);
c906108c
SS
8136 break;
8137 case DW_TAG_base_type:
a02abb62 8138 case DW_TAG_subrange_type:
c906108c 8139 /* File scope base type definitions are added to the partial
c5aa993b 8140 symbol table. */
72bf9492 8141 add_partial_symbol (pdi, cu);
c906108c 8142 break;
d9fa45fe 8143 case DW_TAG_namespace:
cdc07690 8144 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8145 break;
5d7cb8df 8146 case DW_TAG_module:
59c35742
AB
8147 if (!pdi->is_declaration)
8148 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8149 break;
95554aad
TT
8150 case DW_TAG_imported_unit:
8151 {
8152 struct dwarf2_per_cu_data *per_cu;
8153
f4dc4d17
DE
8154 /* For now we don't handle imported units in type units. */
8155 if (cu->per_cu->is_debug_types)
8156 {
8157 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8158 " supported in type units [in module %s]"),
5e22e966 8159 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8160 }
8161
e3b94546 8162 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8163 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8164
8165 /* Go read the partial unit, if needed. */
8166 if (per_cu->v.psymtab == NULL)
ab432490
SM
8167 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8168 cu->language);
95554aad 8169
ae640021 8170 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8171 }
8172 break;
74921315
KS
8173 case DW_TAG_imported_declaration:
8174 add_partial_symbol (pdi, cu);
8175 break;
c906108c
SS
8176 default:
8177 break;
8178 }
8179 }
8180
72bf9492
DJ
8181 /* If the die has a sibling, skip to the sibling. */
8182
8183 pdi = pdi->die_sibling;
8184 }
8185}
8186
8187/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8188
72bf9492 8189 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8190 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8191 Enumerators are an exception; they use the scope of their parent
8192 enumeration type, i.e. the name of the enumeration type is not
8193 prepended to the enumerator.
91c24f0a 8194
72bf9492
DJ
8195 There are two complexities. One is DW_AT_specification; in this
8196 case "parent" means the parent of the target of the specification,
8197 instead of the direct parent of the DIE. The other is compilers
8198 which do not emit DW_TAG_namespace; in this case we try to guess
8199 the fully qualified name of structure types from their members'
8200 linkage names. This must be done using the DIE's children rather
8201 than the children of any DW_AT_specification target. We only need
8202 to do this for structures at the top level, i.e. if the target of
8203 any DW_AT_specification (if any; otherwise the DIE itself) does not
8204 have a parent. */
8205
8206/* Compute the scope prefix associated with PDI's parent, in
8207 compilation unit CU. The result will be allocated on CU's
8208 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8209 field. NULL is returned if no prefix is necessary. */
15d034d0 8210static const char *
72bf9492
DJ
8211partial_die_parent_scope (struct partial_die_info *pdi,
8212 struct dwarf2_cu *cu)
8213{
15d034d0 8214 const char *grandparent_scope;
72bf9492 8215 struct partial_die_info *parent, *real_pdi;
91c24f0a 8216
72bf9492
DJ
8217 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8218 then this means the parent of the specification DIE. */
8219
8220 real_pdi = pdi;
72bf9492 8221 while (real_pdi->has_specification)
fb816e8b 8222 {
122cf0f2
AB
8223 auto res = find_partial_die (real_pdi->spec_offset,
8224 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8225 real_pdi = res.pdi;
8226 cu = res.cu;
8227 }
72bf9492
DJ
8228
8229 parent = real_pdi->die_parent;
8230 if (parent == NULL)
8231 return NULL;
8232
8233 if (parent->scope_set)
8234 return parent->scope;
8235
52356b79 8236 parent->fixup (cu);
72bf9492 8237
10b3939b 8238 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8239
acebe513
UW
8240 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8241 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8242 Work around this problem here. */
8243 if (cu->language == language_cplus
6e70227d 8244 && parent->tag == DW_TAG_namespace
acebe513
UW
8245 && strcmp (parent->name, "::") == 0
8246 && grandparent_scope == NULL)
8247 {
8248 parent->scope = NULL;
8249 parent->scope_set = 1;
8250 return NULL;
8251 }
8252
0a4b0913 8253 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8254 if (pdi->tag == DW_TAG_enumerator)
8255 /* Enumerators should not get the name of the enumeration as a prefix. */
8256 parent->scope = grandparent_scope;
8257 else if (parent->tag == DW_TAG_namespace
f55ee35c 8258 || parent->tag == DW_TAG_module
72bf9492
DJ
8259 || parent->tag == DW_TAG_structure_type
8260 || parent->tag == DW_TAG_class_type
680b30c7 8261 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8262 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8263 || parent->tag == DW_TAG_enumeration_type
8264 || (cu->language == language_fortran
8265 && parent->tag == DW_TAG_subprogram
8266 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8267 {
8268 if (grandparent_scope == NULL)
8269 parent->scope = parent->name;
8270 else
3e43a32a
MS
8271 parent->scope = typename_concat (&cu->comp_unit_obstack,
8272 grandparent_scope,
f55ee35c 8273 parent->name, 0, cu);
72bf9492 8274 }
72bf9492
DJ
8275 else
8276 {
8277 /* FIXME drow/2004-04-01: What should we be doing with
8278 function-local names? For partial symbols, we should probably be
8279 ignoring them. */
fa9c3fa0
TT
8280 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8281 dwarf_tag_name (parent->tag),
8282 sect_offset_str (pdi->sect_off));
72bf9492 8283 parent->scope = grandparent_scope;
c906108c
SS
8284 }
8285
72bf9492
DJ
8286 parent->scope_set = 1;
8287 return parent->scope;
8288}
8289
8290/* Return the fully scoped name associated with PDI, from compilation unit
8291 CU. The result will be allocated with malloc. */
4568ecf9 8292
43816ebc 8293static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8294partial_die_full_name (struct partial_die_info *pdi,
8295 struct dwarf2_cu *cu)
8296{
15d034d0 8297 const char *parent_scope;
72bf9492 8298
98bfdba5
PA
8299 /* If this is a template instantiation, we can not work out the
8300 template arguments from partial DIEs. So, unfortunately, we have
8301 to go through the full DIEs. At least any work we do building
8302 types here will be reused if full symbols are loaded later. */
8303 if (pdi->has_template_arguments)
8304 {
52356b79 8305 pdi->fixup (cu);
98bfdba5
PA
8306
8307 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8308 {
8309 struct die_info *die;
8310 struct attribute attr;
8311 struct dwarf2_cu *ref_cu = cu;
8312
b64f50a1 8313 /* DW_FORM_ref_addr is using section offset. */
b4069958 8314 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8315 attr.form = DW_FORM_ref_addr;
9c541725 8316 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8317 die = follow_die_ref (NULL, &attr, &ref_cu);
8318
43816ebc 8319 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8320 }
8321 }
8322
72bf9492
DJ
8323 parent_scope = partial_die_parent_scope (pdi, cu);
8324 if (parent_scope == NULL)
8325 return NULL;
8326 else
43816ebc
TT
8327 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8328 pdi->name, 0, cu));
c906108c
SS
8329}
8330
8331static void
72bf9492 8332add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8333{
5e22e966 8334 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8335 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8336 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8337 CORE_ADDR addr = 0;
15d034d0 8338 const char *actual_name = NULL;
e142c38c
DJ
8339 CORE_ADDR baseaddr;
8340
b3b3bada 8341 baseaddr = objfile->text_section_offset ();
c906108c 8342
43816ebc
TT
8343 gdb::unique_xmalloc_ptr<char> built_actual_name
8344 = partial_die_full_name (pdi, cu);
15d034d0 8345 if (built_actual_name != NULL)
43816ebc 8346 actual_name = built_actual_name.get ();
63d06c5c 8347
72bf9492
DJ
8348 if (actual_name == NULL)
8349 actual_name = pdi->name;
8350
76e288d1
TT
8351 partial_symbol psymbol;
8352 memset (&psymbol, 0, sizeof (psymbol));
8353 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8354 psymbol.ginfo.section = -1;
8355
8356 /* The code below indicates that the psymbol should be installed by
8357 setting this. */
8358 gdb::optional<psymbol_placement> where;
8359
c906108c
SS
8360 switch (pdi->tag)
8361 {
b1dc1806 8362 case DW_TAG_inlined_subroutine:
c906108c 8363 case DW_TAG_subprogram:
79748972
TT
8364 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8365 - baseaddr);
0a4b0913
AB
8366 if (pdi->is_external
8367 || cu->language == language_ada
8368 || (cu->language == language_fortran
8369 && pdi->die_parent != NULL
8370 && pdi->die_parent->tag == DW_TAG_subprogram))
8371 {
8372 /* Normally, only "external" DIEs are part of the global scope.
8373 But in Ada and Fortran, we want to be able to access nested
8374 procedures globally. So all Ada and Fortran subprograms are
8375 stored in the global scope. */
76e288d1 8376 where = psymbol_placement::GLOBAL;
c906108c
SS
8377 }
8378 else
76e288d1
TT
8379 where = psymbol_placement::STATIC;
8380
8381 psymbol.domain = VAR_DOMAIN;
8382 psymbol.aclass = LOC_BLOCK;
8383 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8384 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8385
8386 if (pdi->main_subprogram && actual_name != NULL)
8387 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8388 break;
72929c62 8389 case DW_TAG_constant:
76e288d1
TT
8390 psymbol.domain = VAR_DOMAIN;
8391 psymbol.aclass = LOC_STATIC;
8392 where = (pdi->is_external
8393 ? psymbol_placement::GLOBAL
8394 : psymbol_placement::STATIC);
72929c62 8395 break;
c906108c 8396 case DW_TAG_variable:
95554aad
TT
8397 if (pdi->d.locdesc)
8398 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8399
95554aad 8400 if (pdi->d.locdesc
caac4577 8401 && addr == 0
5989a64e 8402 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8403 {
8404 /* A global or static variable may also have been stripped
8405 out by the linker if unused, in which case its address
8406 will be nullified; do not add such variables into partial
8407 symbol table then. */
8408 }
8409 else if (pdi->is_external)
c906108c
SS
8410 {
8411 /* Global Variable.
8412 Don't enter into the minimal symbol tables as there is
8413 a minimal symbol table entry from the ELF symbols already.
8414 Enter into partial symbol table if it has a location
8415 descriptor or a type.
8416 If the location descriptor is missing, new_symbol will create
8417 a LOC_UNRESOLVED symbol, the address of the variable will then
8418 be determined from the minimal symbol table whenever the variable
8419 is referenced.
8420 The address for the partial symbol table entry is not
8421 used by GDB, but it comes in handy for debugging partial symbol
8422 table building. */
8423
95554aad 8424 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8425 {
8426 psymbol.domain = VAR_DOMAIN;
8427 psymbol.aclass = LOC_STATIC;
8428 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8429 psymbol.ginfo.value.address = addr;
8430 where = psymbol_placement::GLOBAL;
8431 }
c906108c
SS
8432 }
8433 else
8434 {
ff908ebf
AW
8435 int has_loc = pdi->d.locdesc != NULL;
8436
8437 /* Static Variable. Skip symbols whose value we cannot know (those
8438 without location descriptors or constant values). */
8439 if (!has_loc && !pdi->has_const_value)
43816ebc 8440 return;
ff908ebf 8441
76e288d1
TT
8442 psymbol.domain = VAR_DOMAIN;
8443 psymbol.aclass = LOC_STATIC;
8444 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8445 if (has_loc)
8446 psymbol.ginfo.value.address = addr;
8447 where = psymbol_placement::STATIC;
c906108c
SS
8448 }
8449 break;
8450 case DW_TAG_typedef:
8451 case DW_TAG_base_type:
a02abb62 8452 case DW_TAG_subrange_type:
76e288d1
TT
8453 psymbol.domain = VAR_DOMAIN;
8454 psymbol.aclass = LOC_TYPEDEF;
8455 where = psymbol_placement::STATIC;
c906108c 8456 break;
74921315 8457 case DW_TAG_imported_declaration:
72bf9492 8458 case DW_TAG_namespace:
76e288d1
TT
8459 psymbol.domain = VAR_DOMAIN;
8460 psymbol.aclass = LOC_TYPEDEF;
8461 where = psymbol_placement::GLOBAL;
72bf9492 8462 break;
530e8392 8463 case DW_TAG_module:
a5fd13a9
BH
8464 /* With Fortran 77 there might be a "BLOCK DATA" module
8465 available without any name. If so, we skip the module as it
8466 doesn't bring any value. */
8467 if (actual_name != nullptr)
76e288d1
TT
8468 {
8469 psymbol.domain = MODULE_DOMAIN;
8470 psymbol.aclass = LOC_TYPEDEF;
8471 where = psymbol_placement::GLOBAL;
8472 }
530e8392 8473 break;
c906108c 8474 case DW_TAG_class_type:
680b30c7 8475 case DW_TAG_interface_type:
c906108c
SS
8476 case DW_TAG_structure_type:
8477 case DW_TAG_union_type:
8478 case DW_TAG_enumeration_type:
fa4028e9
JB
8479 /* Skip external references. The DWARF standard says in the section
8480 about "Structure, Union, and Class Type Entries": "An incomplete
8481 structure, union or class type is represented by a structure,
8482 union or class entry that does not have a byte size attribute
8483 and that has a DW_AT_declaration attribute." */
8484 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8485 return;
fa4028e9 8486
63d06c5c
DC
8487 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8488 static vs. global. */
76e288d1
TT
8489 psymbol.domain = STRUCT_DOMAIN;
8490 psymbol.aclass = LOC_TYPEDEF;
8491 where = (cu->language == language_cplus
8492 ? psymbol_placement::GLOBAL
8493 : psymbol_placement::STATIC);
c906108c
SS
8494 break;
8495 case DW_TAG_enumerator:
76e288d1
TT
8496 psymbol.domain = VAR_DOMAIN;
8497 psymbol.aclass = LOC_CONST;
8498 where = (cu->language == language_cplus
8499 ? psymbol_placement::GLOBAL
8500 : psymbol_placement::STATIC);
c906108c
SS
8501 break;
8502 default:
8503 break;
8504 }
76e288d1
TT
8505
8506 if (where.has_value ())
8507 {
f049a313
TT
8508 if (built_actual_name != nullptr)
8509 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8510 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8511 psymbol.ginfo.set_linkage_name (actual_name);
8512 else
8513 {
8514 psymbol.ginfo.set_demangled_name (actual_name,
8515 &objfile->objfile_obstack);
8516 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8517 }
76e288d1
TT
8518 add_psymbol_to_list (psymbol, *where, objfile);
8519 }
c906108c
SS
8520}
8521
5c4e30ca
DC
8522/* Read a partial die corresponding to a namespace; also, add a symbol
8523 corresponding to that namespace to the symbol table. NAMESPACE is
8524 the name of the enclosing namespace. */
91c24f0a 8525
72bf9492
DJ
8526static void
8527add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8528 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8529 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8530{
72bf9492 8531 /* Add a symbol for the namespace. */
e7c27a73 8532
72bf9492 8533 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8534
8535 /* Now scan partial symbols in that namespace. */
8536
91c24f0a 8537 if (pdi->has_children)
cdc07690 8538 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8539}
8540
5d7cb8df
JK
8541/* Read a partial die corresponding to a Fortran module. */
8542
8543static void
8544add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8545 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8546{
530e8392
KB
8547 /* Add a symbol for the namespace. */
8548
8549 add_partial_symbol (pdi, cu);
8550
f55ee35c 8551 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8552
8553 if (pdi->has_children)
cdc07690 8554 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8555}
8556
b1dc1806
XR
8557/* Read a partial die corresponding to a subprogram or an inlined
8558 subprogram and create a partial symbol for that subprogram.
8559 When the CU language allows it, this routine also defines a partial
8560 symbol for each nested subprogram that this subprogram contains.
8561 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8562 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8563
cdc07690
YQ
8564 PDI may also be a lexical block, in which case we simply search
8565 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8566 Again, this is only performed when the CU language allows this
8567 type of definitions. */
8568
8569static void
8570add_partial_subprogram (struct partial_die_info *pdi,
8571 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8572 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8573{
b1dc1806 8574 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8575 {
8576 if (pdi->has_pc_info)
8577 {
8578 if (pdi->lowpc < *lowpc)
8579 *lowpc = pdi->lowpc;
8580 if (pdi->highpc > *highpc)
8581 *highpc = pdi->highpc;
cdc07690 8582 if (set_addrmap)
5734ee8b 8583 {
5e22e966 8584 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8585 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8586 CORE_ADDR baseaddr;
b926417a
TT
8587 CORE_ADDR this_highpc;
8588 CORE_ADDR this_lowpc;
5734ee8b 8589
b3b3bada 8590 baseaddr = objfile->text_section_offset ();
b926417a
TT
8591 this_lowpc
8592 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8593 pdi->lowpc + baseaddr)
8594 - baseaddr);
8595 this_highpc
8596 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8597 pdi->highpc + baseaddr)
8598 - baseaddr);
d320c2b5 8599 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8600 this_lowpc, this_highpc - 1,
9291a0cd 8601 cu->per_cu->v.psymtab);
5734ee8b 8602 }
481860b3
GB
8603 }
8604
8605 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8606 {
bc30ff58 8607 if (!pdi->is_declaration)
e8d05480
JB
8608 /* Ignore subprogram DIEs that do not have a name, they are
8609 illegal. Do not emit a complaint at this point, we will
8610 do so when we convert this psymtab into a symtab. */
8611 if (pdi->name)
8612 add_partial_symbol (pdi, cu);
bc30ff58
JB
8613 }
8614 }
6e70227d 8615
bc30ff58
JB
8616 if (! pdi->has_children)
8617 return;
8618
0a4b0913 8619 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8620 {
8621 pdi = pdi->die_child;
8622 while (pdi != NULL)
8623 {
52356b79 8624 pdi->fixup (cu);
bc30ff58 8625 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8626 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8627 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8628 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8629 pdi = pdi->die_sibling;
8630 }
8631 }
8632}
8633
91c24f0a
DC
8634/* Read a partial die corresponding to an enumeration type. */
8635
72bf9492
DJ
8636static void
8637add_partial_enumeration (struct partial_die_info *enum_pdi,
8638 struct dwarf2_cu *cu)
91c24f0a 8639{
72bf9492 8640 struct partial_die_info *pdi;
91c24f0a
DC
8641
8642 if (enum_pdi->name != NULL)
72bf9492
DJ
8643 add_partial_symbol (enum_pdi, cu);
8644
8645 pdi = enum_pdi->die_child;
8646 while (pdi)
91c24f0a 8647 {
72bf9492 8648 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8649 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8650 else
72bf9492
DJ
8651 add_partial_symbol (pdi, cu);
8652 pdi = pdi->die_sibling;
91c24f0a 8653 }
91c24f0a
DC
8654}
8655
6caca83c
CC
8656/* Return the initial uleb128 in the die at INFO_PTR. */
8657
8658static unsigned int
d521ce57 8659peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8660{
8661 unsigned int bytes_read;
8662
8663 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8664}
8665
685af9cd
TT
8666/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8667 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8668
4bb7a0a7
DJ
8669 Return the corresponding abbrev, or NULL if the number is zero (indicating
8670 an empty DIE). In either case *BYTES_READ will be set to the length of
8671 the initial number. */
8672
8673static struct abbrev_info *
685af9cd
TT
8674peek_die_abbrev (const die_reader_specs &reader,
8675 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8676{
685af9cd 8677 dwarf2_cu *cu = reader.cu;
5e22e966 8678 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8679 unsigned int abbrev_number
8680 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8681
8682 if (abbrev_number == 0)
8683 return NULL;
8684
685af9cd 8685 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8686 if (!abbrev)
8687 {
422b9917 8688 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8689 " at offset %s [in module %s]"),
422b9917 8690 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8691 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8692 }
8693
8694 return abbrev;
8695}
8696
93311388
DE
8697/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8698 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8699 DIE. Any children of the skipped DIEs will also be skipped. */
8700
d521ce57
TT
8701static const gdb_byte *
8702skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8703{
4bb7a0a7
DJ
8704 while (1)
8705 {
685af9cd
TT
8706 unsigned int bytes_read;
8707 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8708
4bb7a0a7
DJ
8709 if (abbrev == NULL)
8710 return info_ptr + bytes_read;
8711 else
dee91e82 8712 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8713 }
8714}
8715
93311388
DE
8716/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8717 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8718 abbrev corresponding to that skipped uleb128 should be passed in
8719 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8720 children. */
8721
d521ce57
TT
8722static const gdb_byte *
8723skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8724 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8725{
8726 unsigned int bytes_read;
8727 struct attribute attr;
dee91e82
DE
8728 bfd *abfd = reader->abfd;
8729 struct dwarf2_cu *cu = reader->cu;
d521ce57 8730 const gdb_byte *buffer = reader->buffer;
f664829e 8731 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8732 unsigned int form, i;
8733
8734 for (i = 0; i < abbrev->num_attrs; i++)
8735 {
8736 /* The only abbrev we care about is DW_AT_sibling. */
8737 if (abbrev->attrs[i].name == DW_AT_sibling)
8738 {
18a8505e
AT
8739 bool ignored;
8740 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8741 &ignored);
4bb7a0a7 8742 if (attr.form == DW_FORM_ref_addr)
b98664d3 8743 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8744 else
b9502d3f 8745 {
0826b30a 8746 sect_offset off = attr.get_ref_die_offset ();
9c541725 8747 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8748
8749 if (sibling_ptr < info_ptr)
b98664d3 8750 complaint (_("DW_AT_sibling points backwards"));
22869d73 8751 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8752 reader->die_section->overflow_complaint ();
b9502d3f
WN
8753 else
8754 return sibling_ptr;
8755 }
4bb7a0a7
DJ
8756 }
8757
8758 /* If it isn't DW_AT_sibling, skip this attribute. */
8759 form = abbrev->attrs[i].form;
8760 skip_attribute:
8761 switch (form)
8762 {
4bb7a0a7 8763 case DW_FORM_ref_addr:
ae411497
TT
8764 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8765 and later it is offset sized. */
8766 if (cu->header.version == 2)
8767 info_ptr += cu->header.addr_size;
8768 else
8769 info_ptr += cu->header.offset_size;
8770 break;
36586728
TT
8771 case DW_FORM_GNU_ref_alt:
8772 info_ptr += cu->header.offset_size;
8773 break;
ae411497 8774 case DW_FORM_addr:
4bb7a0a7
DJ
8775 info_ptr += cu->header.addr_size;
8776 break;
8777 case DW_FORM_data1:
8778 case DW_FORM_ref1:
8779 case DW_FORM_flag:
8fe0f950 8780 case DW_FORM_strx1:
4bb7a0a7
DJ
8781 info_ptr += 1;
8782 break;
2dc7f7b3 8783 case DW_FORM_flag_present:
43988095 8784 case DW_FORM_implicit_const:
2dc7f7b3 8785 break;
4bb7a0a7
DJ
8786 case DW_FORM_data2:
8787 case DW_FORM_ref2:
8fe0f950 8788 case DW_FORM_strx2:
4bb7a0a7
DJ
8789 info_ptr += 2;
8790 break;
8fe0f950
AT
8791 case DW_FORM_strx3:
8792 info_ptr += 3;
8793 break;
4bb7a0a7
DJ
8794 case DW_FORM_data4:
8795 case DW_FORM_ref4:
8fe0f950 8796 case DW_FORM_strx4:
4bb7a0a7
DJ
8797 info_ptr += 4;
8798 break;
8799 case DW_FORM_data8:
8800 case DW_FORM_ref8:
55f1336d 8801 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8802 info_ptr += 8;
8803 break;
0224619f
JK
8804 case DW_FORM_data16:
8805 info_ptr += 16;
8806 break;
4bb7a0a7 8807 case DW_FORM_string:
9b1c24c8 8808 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8809 info_ptr += bytes_read;
8810 break;
2dc7f7b3 8811 case DW_FORM_sec_offset:
4bb7a0a7 8812 case DW_FORM_strp:
36586728 8813 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8814 info_ptr += cu->header.offset_size;
8815 break;
2dc7f7b3 8816 case DW_FORM_exprloc:
4bb7a0a7
DJ
8817 case DW_FORM_block:
8818 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8819 info_ptr += bytes_read;
8820 break;
8821 case DW_FORM_block1:
8822 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8823 break;
8824 case DW_FORM_block2:
8825 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8826 break;
8827 case DW_FORM_block4:
8828 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8829 break;
336d760d 8830 case DW_FORM_addrx:
cf532bd1 8831 case DW_FORM_strx:
4bb7a0a7
DJ
8832 case DW_FORM_sdata:
8833 case DW_FORM_udata:
8834 case DW_FORM_ref_udata:
3019eac3
DE
8835 case DW_FORM_GNU_addr_index:
8836 case DW_FORM_GNU_str_index:
18a8505e 8837 case DW_FORM_rnglistx:
41144253 8838 case DW_FORM_loclistx:
d521ce57 8839 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8840 break;
8841 case DW_FORM_indirect:
8842 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8843 info_ptr += bytes_read;
8844 /* We need to continue parsing from here, so just go back to
8845 the top. */
8846 goto skip_attribute;
8847
8848 default:
3e43a32a
MS
8849 error (_("Dwarf Error: Cannot handle %s "
8850 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8851 dwarf_form_name (form),
8852 bfd_get_filename (abfd));
8853 }
8854 }
8855
8856 if (abbrev->has_children)
dee91e82 8857 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8858 else
8859 return info_ptr;
8860}
8861
93311388 8862/* Locate ORIG_PDI's sibling.
dee91e82 8863 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8864
d521ce57 8865static const gdb_byte *
dee91e82
DE
8866locate_pdi_sibling (const struct die_reader_specs *reader,
8867 struct partial_die_info *orig_pdi,
d521ce57 8868 const gdb_byte *info_ptr)
91c24f0a
DC
8869{
8870 /* Do we know the sibling already? */
72bf9492 8871
91c24f0a
DC
8872 if (orig_pdi->sibling)
8873 return orig_pdi->sibling;
8874
8875 /* Are there any children to deal with? */
8876
8877 if (!orig_pdi->has_children)
8878 return info_ptr;
8879
4bb7a0a7 8880 /* Skip the children the long way. */
91c24f0a 8881
dee91e82 8882 return skip_children (reader, info_ptr);
91c24f0a
DC
8883}
8884
257e7a09 8885/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8886 not NULL. */
c906108c 8887
891813be
TT
8888void
8889dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8890{
ed2dc618
SM
8891 struct dwarf2_per_objfile *dwarf2_per_objfile
8892 = get_dwarf2_per_objfile (objfile);
8893
af758d11
SM
8894 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8895
077cbab2
TT
8896 /* If this psymtab is constructed from a debug-only objfile, the
8897 has_section_at_zero flag will not necessarily be correct. We
8898 can get the correct value for this flag by looking at the data
8899 associated with the (presumably stripped) associated objfile. */
8900 if (objfile->separate_debug_objfile_backlink)
c906108c 8901 {
077cbab2
TT
8902 struct dwarf2_per_objfile *dpo_backlink
8903 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8904
5989a64e
SM
8905 dwarf2_per_objfile->per_bfd->has_section_at_zero
8906 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8907 }
98bfdba5 8908
8566b89b 8909 expand_psymtab (objfile);
95554aad 8910
ed2dc618 8911 process_cu_includes (dwarf2_per_objfile);
c906108c 8912}
9cdd5dbd
DE
8913\f
8914/* Reading in full CUs. */
c906108c 8915
10b3939b
DJ
8916/* Add PER_CU to the queue. */
8917
8918static void
120ce1b5
SM
8919queue_comp_unit (dwarf2_per_cu_data *per_cu,
8920 dwarf2_per_objfile *per_objfile,
95554aad 8921 enum language pretend_language)
10b3939b 8922{
10b3939b 8923 per_cu->queued = 1;
120ce1b5 8924 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8925}
8926
89e63ee4
DE
8927/* If PER_CU is not yet queued, add it to the queue.
8928 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8929 dependency.
0907af0c 8930 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8931 meaning either PER_CU is already queued or it is already loaded.
8932
8933 N.B. There is an invariant here that if a CU is queued then it is loaded.
8934 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8935
8936static int
89e63ee4 8937maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8938 dwarf2_per_cu_data *per_cu,
8939 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8940 enum language pretend_language)
8941{
8942 /* We may arrive here during partial symbol reading, if we need full
8943 DIEs to process an unusual case (e.g. template arguments). Do
8944 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8945 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8946 {
8947 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8948 return 1;
8949 return 0;
8950 }
8951
8952 /* Mark the dependence relation so that we don't flush PER_CU
8953 too early. */
89e63ee4
DE
8954 if (dependent_cu != NULL)
8955 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8956
8957 /* If it's already on the queue, we have nothing to do. */
8958 if (per_cu->queued)
8959 return 0;
8960
8961 /* If the compilation unit is already loaded, just mark it as
8962 used. */
8963 if (per_cu->cu != NULL)
8964 {
8965 per_cu->cu->last_used = 0;
8966 return 0;
8967 }
8968
8969 /* Add it to the queue. */
120ce1b5 8970 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
8971
8972 return 1;
8973}
8974
10b3939b
DJ
8975/* Process the queue. */
8976
8977static void
ed2dc618 8978process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8979{
b4f54984 8980 if (dwarf_read_debug)
45cfd468
DE
8981 {
8982 fprintf_unfiltered (gdb_stdlog,
8983 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8984 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8985 }
8986
03dd20cc
DJ
8987 /* The queue starts out with one item, but following a DIE reference
8988 may load a new CU, adding it to the end of the queue. */
5989a64e 8989 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8990 {
5989a64e 8991 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8992
af758d11 8993 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8994 /* Skip dummy CUs. */
39856def 8995 && item.per_cu->cu != NULL)
f4dc4d17 8996 {
39856def 8997 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8998 unsigned int debug_print_threshold;
247f5c4f 8999 char buf[100];
f4dc4d17 9000
247f5c4f 9001 if (per_cu->is_debug_types)
f4dc4d17 9002 {
247f5c4f
DE
9003 struct signatured_type *sig_type =
9004 (struct signatured_type *) per_cu;
9005
9d8780f0 9006 sprintf (buf, "TU %s at offset %s",
73be47f5 9007 hex_string (sig_type->signature),
9d8780f0 9008 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9009 /* There can be 100s of TUs.
9010 Only print them in verbose mode. */
9011 debug_print_threshold = 2;
f4dc4d17 9012 }
247f5c4f 9013 else
73be47f5 9014 {
9d8780f0
SM
9015 sprintf (buf, "CU at offset %s",
9016 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9017 debug_print_threshold = 1;
9018 }
247f5c4f 9019
b4f54984 9020 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9021 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9022
9023 if (per_cu->is_debug_types)
47b14e86
SM
9024 process_full_type_unit (per_cu, dwarf2_per_objfile,
9025 item.pretend_language);
f4dc4d17 9026 else
47b14e86
SM
9027 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9028 item.pretend_language);
f4dc4d17 9029
b4f54984 9030 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9031 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9032 }
10b3939b 9033
39856def 9034 item.per_cu->queued = 0;
5989a64e 9035 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9036 }
9037
b4f54984 9038 if (dwarf_read_debug)
45cfd468
DE
9039 {
9040 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9041 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9042 }
10b3939b
DJ
9043}
9044
10b3939b
DJ
9045/* Read in full symbols for PST, and anything it depends on. */
9046
8566b89b
TT
9047void
9048dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9049{
af758d11 9050 gdb_assert (!readin_p (objfile));
95554aad 9051
48993951 9052 expand_dependencies (objfile);
aaa75496 9053
97a1449a
SM
9054 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9055 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9056 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9057}
9058
af758d11
SM
9059/* See psympriv.h. */
9060
9061bool
9062dwarf2_psymtab::readin_p (struct objfile *objfile) const
9063{
9064 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9065 return per_objfile->symtab_set_p (per_cu_data);
9066}
9067
9068/* See psympriv.h. */
9069
9070compunit_symtab *
9071dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9072{
9073 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9074 return per_objfile->get_symtab (per_cu_data);
9075}
9076
dee91e82
DE
9077/* Trivial hash function for die_info: the hash value of a DIE
9078 is its offset in .debug_info for this objfile. */
10b3939b 9079
dee91e82
DE
9080static hashval_t
9081die_hash (const void *item)
10b3939b 9082{
9a3c8263 9083 const struct die_info *die = (const struct die_info *) item;
6502dd73 9084
9c541725 9085 return to_underlying (die->sect_off);
dee91e82 9086}
63d06c5c 9087
dee91e82
DE
9088/* Trivial comparison function for die_info structures: two DIEs
9089 are equal if they have the same offset. */
98bfdba5 9090
dee91e82
DE
9091static int
9092die_eq (const void *item_lhs, const void *item_rhs)
9093{
9a3c8263
SM
9094 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9095 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9096
9c541725 9097 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9098}
c906108c 9099
c0ab21c2 9100/* Load the DIEs associated with PER_CU into memory. */
c906108c 9101
dee91e82 9102static void
ab432490
SM
9103load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9104 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9105 bool skip_partial,
9106 enum language pretend_language)
dee91e82 9107{
c0ab21c2
TT
9108 gdb_assert (! this_cu->is_debug_types);
9109
ab432490 9110 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9111 if (reader.dummy_p)
9112 return;
9113
9114 struct dwarf2_cu *cu = reader.cu;
9115 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9116
dee91e82
DE
9117 gdb_assert (cu->die_hash == NULL);
9118 cu->die_hash =
9119 htab_create_alloc_ex (cu->header.length / 12,
9120 die_hash,
9121 die_eq,
9122 NULL,
9123 &cu->comp_unit_obstack,
9124 hashtab_obstack_allocate,
9125 dummy_obstack_deallocate);
e142c38c 9126
3e225074 9127 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9128 reader.comp_unit_die->child
9129 = read_die_and_siblings (&reader, reader.info_ptr,
9130 &info_ptr, reader.comp_unit_die);
9131 cu->dies = reader.comp_unit_die;
dee91e82 9132 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9133
9134 /* We try not to read any attributes in this function, because not
9cdd5dbd 9135 all CUs needed for references have been loaded yet, and symbol
10b3939b 9136 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9137 or we won't be able to build types correctly.
9138 Similarly, if we do not read the producer, we can not apply
9139 producer-specific interpretation. */
c0ab21c2 9140 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9141
9142 reader.keep ();
10b3939b
DJ
9143}
9144
3da10d80
KS
9145/* Add a DIE to the delayed physname list. */
9146
9147static void
9148add_to_method_list (struct type *type, int fnfield_index, int index,
9149 const char *name, struct die_info *die,
9150 struct dwarf2_cu *cu)
9151{
9152 struct delayed_method_info mi;
9153 mi.type = type;
9154 mi.fnfield_index = fnfield_index;
9155 mi.index = index;
9156 mi.name = name;
9157 mi.die = die;
c89b44cd 9158 cu->method_list.push_back (mi);
3da10d80
KS
9159}
9160
3693fdb3
PA
9161/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9162 "const" / "volatile". If so, decrements LEN by the length of the
9163 modifier and return true. Otherwise return false. */
9164
9165template<size_t N>
9166static bool
9167check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9168{
9169 size_t mod_len = sizeof (mod) - 1;
9170 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9171 {
9172 len -= mod_len;
9173 return true;
9174 }
9175 return false;
9176}
9177
3da10d80
KS
9178/* Compute the physnames of any methods on the CU's method list.
9179
9180 The computation of method physnames is delayed in order to avoid the
9181 (bad) condition that one of the method's formal parameters is of an as yet
9182 incomplete type. */
9183
9184static void
9185compute_delayed_physnames (struct dwarf2_cu *cu)
9186{
3693fdb3 9187 /* Only C++ delays computing physnames. */
c89b44cd 9188 if (cu->method_list.empty ())
3693fdb3
PA
9189 return;
9190 gdb_assert (cu->language == language_cplus);
9191
52941706 9192 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9193 {
1d06ead6 9194 const char *physname;
3da10d80 9195 struct fn_fieldlist *fn_flp
c89b44cd
TT
9196 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9197 physname = dwarf2_physname (mi.name, mi.die, cu);
9198 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9199 = physname ? physname : "";
3693fdb3
PA
9200
9201 /* Since there's no tag to indicate whether a method is a
9202 const/volatile overload, extract that information out of the
9203 demangled name. */
9204 if (physname != NULL)
9205 {
9206 size_t len = strlen (physname);
9207
9208 while (1)
9209 {
9210 if (physname[len] == ')') /* shortcut */
9211 break;
9212 else if (check_modifier (physname, len, " const"))
c89b44cd 9213 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9214 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9215 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9216 else
9217 break;
9218 }
9219 }
3da10d80 9220 }
c89b44cd
TT
9221
9222 /* The list is no longer needed. */
9223 cu->method_list.clear ();
3da10d80
KS
9224}
9225
a766d390
DE
9226/* Go objects should be embedded in a DW_TAG_module DIE,
9227 and it's not clear if/how imported objects will appear.
9228 To keep Go support simple until that's worked out,
9229 go back through what we've read and create something usable.
9230 We could do this while processing each DIE, and feels kinda cleaner,
9231 but that way is more invasive.
9232 This is to, for example, allow the user to type "p var" or "b main"
9233 without having to specify the package name, and allow lookups
9234 of module.object to work in contexts that use the expression
9235 parser. */
9236
9237static void
9238fixup_go_packaging (struct dwarf2_cu *cu)
9239{
421d1616 9240 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9241 struct pending *list;
9242 int i;
9243
c24bdb02 9244 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9245 list != NULL;
9246 list = list->next)
a766d390
DE
9247 {
9248 for (i = 0; i < list->nsyms; ++i)
9249 {
9250 struct symbol *sym = list->symbol[i];
9251
c1b5c1eb 9252 if (sym->language () == language_go
a766d390
DE
9253 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9254 {
421d1616
TT
9255 gdb::unique_xmalloc_ptr<char> this_package_name
9256 (go_symbol_package_name (sym));
a766d390
DE
9257
9258 if (this_package_name == NULL)
9259 continue;
9260 if (package_name == NULL)
421d1616 9261 package_name = std::move (this_package_name);
a766d390
DE
9262 else
9263 {
5e22e966 9264 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9265 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9266 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9267 (symbol_symtab (sym) != NULL
9268 ? symtab_to_filename_for_display
9269 (symbol_symtab (sym))
e3b94546 9270 : objfile_name (objfile)),
421d1616 9271 this_package_name.get (), package_name.get ());
a766d390
DE
9272 }
9273 }
9274 }
9275 }
9276
9277 if (package_name != NULL)
9278 {
5e22e966 9279 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9280 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9281 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9282 saved_package_name);
a766d390
DE
9283 struct symbol *sym;
9284
8c14c3a3 9285 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9286 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9287 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9288 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9289 e.g., "main" finds the "main" module and not C's main(). */
9290 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9291 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9292 SYMBOL_TYPE (sym) = type;
9293
c24bdb02 9294 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9295 }
9296}
9297
c9317f21
TT
9298/* Allocate a fully-qualified name consisting of the two parts on the
9299 obstack. */
9300
9301static const char *
9302rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9303{
9304 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9305}
9306
9c6a1327
TT
9307/* A helper that allocates a variant part to attach to a Rust enum
9308 type. OBSTACK is where the results should be allocated. TYPE is
9309 the type we're processing. DISCRIMINANT_INDEX is the index of the
9310 discriminant. It must be the index of one of the fields of TYPE.
9311 DEFAULT_INDEX is the index of the default field; or -1 if there is
9312 no default. RANGES is indexed by "effective" field number (the
9313 field index, but omitting the discriminant and default fields) and
9314 must hold the discriminant values used by the variants. Note that
9315 RANGES must have a lifetime at least as long as OBSTACK -- either
9316 already allocated on it, or static. */
c9317f21 9317
9c6a1327
TT
9318static void
9319alloc_rust_variant (struct obstack *obstack, struct type *type,
9320 int discriminant_index, int default_index,
9321 gdb::array_view<discriminant_range> ranges)
9322{
9323 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9324 must be handled by the caller. */
9325 gdb_assert (discriminant_index >= 0
1f704f76 9326 && discriminant_index < type->num_fields ());
c9317f21 9327 gdb_assert (default_index == -1
1f704f76 9328 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9329
9c6a1327 9330 /* We have one variant for each non-discriminant field. */
1f704f76 9331 int n_variants = type->num_fields () - 1;
c9317f21 9332
9c6a1327
TT
9333 variant *variants = new (obstack) variant[n_variants];
9334 int var_idx = 0;
9335 int range_idx = 0;
1f704f76 9336 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9337 {
9338 if (i == discriminant_index)
9339 continue;
c9317f21 9340
9c6a1327
TT
9341 variants[var_idx].first_field = i;
9342 variants[var_idx].last_field = i + 1;
9343
9344 /* The default field does not need a range, but other fields do.
9345 We skipped the discriminant above. */
9346 if (i != default_index)
9347 {
9348 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9349 ++range_idx;
9350 }
c9317f21 9351
9c6a1327
TT
9352 ++var_idx;
9353 }
9354
9355 gdb_assert (range_idx == ranges.size ());
9356 gdb_assert (var_idx == n_variants);
9357
9358 variant_part *part = new (obstack) variant_part;
9359 part->discriminant_index = discriminant_index;
9360 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9361 discriminant_index));
9362 part->variants = gdb::array_view<variant> (variants, n_variants);
9363
9364 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9365 gdb::array_view<variant_part> *prop_value
9366 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9367
9c6a1327
TT
9368 struct dynamic_prop prop;
9369 prop.kind = PROP_VARIANT_PARTS;
9370 prop.data.variant_parts = prop_value;
9371
5c54719c 9372 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9373}
9374
9375/* Some versions of rustc emitted enums in an unusual way.
9376
9377 Ordinary enums were emitted as unions. The first element of each
9378 structure in the union was named "RUST$ENUM$DISR". This element
9379 held the discriminant.
9380
9381 These versions of Rust also implemented the "non-zero"
9382 optimization. When the enum had two values, and one is empty and
9383 the other holds a pointer that cannot be zero, the pointer is used
9384 as the discriminant, with a zero value meaning the empty variant.
9385 Here, the union's first member is of the form
9386 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9387 where the fieldnos are the indices of the fields that should be
9388 traversed in order to find the field (which may be several fields deep)
9389 and the variantname is the name of the variant of the case when the
9390 field is zero.
9391
9392 This function recognizes whether TYPE is of one of these forms,
9393 and, if so, smashes it to be a variant type. */
9394
9395static void
9396quirk_rust_enum (struct type *type, struct objfile *objfile)
9397{
78134374 9398 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9399
9400 /* We don't need to deal with empty enums. */
1f704f76 9401 if (type->num_fields () == 0)
c9317f21
TT
9402 return;
9403
9404#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9405 if (type->num_fields () == 1
c9317f21
TT
9406 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9407 {
9408 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9409
9410 /* Decode the field name to find the offset of the
9411 discriminant. */
9412 ULONGEST bit_offset = 0;
9413 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9414 while (name[0] >= '0' && name[0] <= '9')
9415 {
9416 char *tail;
9417 unsigned long index = strtoul (name, &tail, 10);
9418 name = tail;
9419 if (*name != '$'
1f704f76 9420 || index >= field_type->num_fields ()
c9317f21
TT
9421 || (TYPE_FIELD_LOC_KIND (field_type, index)
9422 != FIELD_LOC_KIND_BITPOS))
9423 {
b98664d3 9424 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9425 "[in module %s]"),
9426 TYPE_FIELD_NAME (type, 0),
9427 objfile_name (objfile));
9428 return;
9429 }
9430 ++name;
9431
9432 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9433 field_type = TYPE_FIELD_TYPE (field_type, index);
9434 }
9435
9c6a1327
TT
9436 /* Smash this type to be a structure type. We have to do this
9437 because the type has already been recorded. */
67607e24 9438 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9439 type->set_num_fields (3);
9c6a1327 9440 /* Save the field we care about. */
ceacbf6e 9441 struct field saved_field = type->field (0);
3cabb6b0
SM
9442 type->set_fields
9443 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9444
9c6a1327
TT
9445 /* Put the discriminant at index 0. */
9446 TYPE_FIELD_TYPE (type, 0) = field_type;
9447 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9448 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9449 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9450
9451 /* The order of fields doesn't really matter, so put the real
9452 field at index 1 and the data-less field at index 2. */
ceacbf6e 9453 type->field (1) = saved_field;
9c6a1327 9454 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9455 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9456 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9457 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9458 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9459
9460 const char *dataless_name
7d93a1e0 9461 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9462 name);
9463 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9464 dataless_name);
9c6a1327 9465 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9466 /* NAME points into the original discriminant name, which
9467 already has the correct lifetime. */
9c6a1327 9468 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9469 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9470
9c6a1327
TT
9471 /* Indicate that this is a variant type. */
9472 static discriminant_range ranges[1] = { { 0, 0 } };
9473 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9474 }
77c2dba3
TT
9475 /* A union with a single anonymous field is probably an old-style
9476 univariant enum. */
1f704f76 9477 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9478 {
c9317f21
TT
9479 /* Smash this type to be a structure type. We have to do this
9480 because the type has already been recorded. */
67607e24 9481 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9482
9c6a1327 9483 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9484 const char *variant_name
7d93a1e0 9485 = rust_last_path_segment (field_type->name ());
9c6a1327 9486 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9487 field_type->set_name
9488 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9489 type->name (), variant_name));
c9317f21
TT
9490 }
9491 else
9492 {
9493 struct type *disr_type = nullptr;
1f704f76 9494 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9495 {
9496 disr_type = TYPE_FIELD_TYPE (type, i);
9497
78134374 9498 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9499 {
9500 /* All fields of a true enum will be structs. */
9501 return;
9502 }
1f704f76 9503 else if (disr_type->num_fields () == 0)
c9317f21
TT
9504 {
9505 /* Could be data-less variant, so keep going. */
a037790e 9506 disr_type = nullptr;
c9317f21
TT
9507 }
9508 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9509 "RUST$ENUM$DISR") != 0)
9510 {
9511 /* Not a Rust enum. */
9512 return;
9513 }
9514 else
9515 {
9516 /* Found one. */
9517 break;
9518 }
9519 }
9520
9521 /* If we got here without a discriminant, then it's probably
9522 just a union. */
9523 if (disr_type == nullptr)
9524 return;
9525
9526 /* Smash this type to be a structure type. We have to do this
9527 because the type has already been recorded. */
67607e24 9528 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9529
9c6a1327 9530 /* Make space for the discriminant field. */
ceacbf6e 9531 struct field *disr_field = &disr_type->field (0);
9c6a1327 9532 field *new_fields
1f704f76 9533 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9534 * sizeof (struct field)));
80fc5e77 9535 memcpy (new_fields + 1, type->fields (),
1f704f76 9536 type->num_fields () * sizeof (struct field));
3cabb6b0 9537 type->set_fields (new_fields);
1f704f76 9538 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9539
9540 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9541 type->field (0) = *disr_field;
9c6a1327
TT
9542 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9543 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9544
9545 /* We need a way to find the correct discriminant given a
9546 variant name. For convenience we build a map here. */
9547 struct type *enum_type = FIELD_TYPE (*disr_field);
9548 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9549 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9550 {
9551 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9552 {
9553 const char *name
9554 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9555 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9556 }
9557 }
9558
1f704f76 9559 int n_fields = type->num_fields ();
9c6a1327
TT
9560 /* We don't need a range entry for the discriminant, but we do
9561 need one for every other field, as there is no default
9562 variant. */
9563 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9564 discriminant_range,
9565 n_fields - 1);
c9317f21
TT
9566 /* Skip the discriminant here. */
9567 for (int i = 1; i < n_fields; ++i)
9568 {
9569 /* Find the final word in the name of this variant's type.
9570 That name can be used to look up the correct
9571 discriminant. */
9572 const char *variant_name
7d93a1e0 9573 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9574
9575 auto iter = discriminant_map.find (variant_name);
9576 if (iter != discriminant_map.end ())
9c6a1327
TT
9577 {
9578 ranges[i].low = iter->second;
9579 ranges[i].high = iter->second;
9580 }
c9317f21 9581
bedda9ac 9582 /* Remove the discriminant field, if it exists. */
9c6a1327 9583 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9584 if (sub_type->num_fields () > 0)
bedda9ac 9585 {
5e33d5f4 9586 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9587 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9588 }
9c6a1327 9589 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9590 sub_type->set_name
9591 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9592 type->name (), variant_name));
c9317f21 9593 }
9c6a1327
TT
9594
9595 /* Indicate that this is a variant type. */
9596 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9597 gdb::array_view<discriminant_range> (ranges,
9598 n_fields - 1));
c9317f21
TT
9599 }
9600}
9601
9602/* Rewrite some Rust unions to be structures with variants parts. */
9603
9604static void
9605rust_union_quirks (struct dwarf2_cu *cu)
9606{
9607 gdb_assert (cu->language == language_rust);
52941706 9608 for (type *type_ : cu->rust_unions)
5e22e966 9609 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9610 /* We don't need this any more. */
9611 cu->rust_unions.clear ();
c9317f21
TT
9612}
9613
8adb8487
TT
9614/* See read.h. */
9615
9616type_unit_group_unshareable *
9617dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9618{
9619 auto iter = this->m_type_units.find (tu_group);
9620 if (iter != this->m_type_units.end ())
9621 return iter->second.get ();
9622
9623 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9624 type_unit_group_unshareable *result = uniq.get ();
9625 this->m_type_units[tu_group] = std::move (uniq);
9626 return result;
9627}
9628
e286671b
TT
9629struct type *
9630dwarf2_per_objfile::get_type_for_signatured_type
9631 (signatured_type *sig_type) const
9632{
9633 auto iter = this->m_type_map.find (sig_type);
9634 if (iter == this->m_type_map.end ())
9635 return nullptr;
9636
9637 return iter->second;
9638}
9639
9640void dwarf2_per_objfile::set_type_for_signatured_type
9641 (signatured_type *sig_type, struct type *type)
9642{
9643 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9644
9645 this->m_type_map[sig_type] = type;
9646}
9647
95554aad
TT
9648/* A helper function for computing the list of all symbol tables
9649 included by PER_CU. */
9650
9651static void
4c39bc03 9652recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9653 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9654 dwarf2_per_cu_data *per_cu,
9655 dwarf2_per_objfile *per_objfile,
43f3e411 9656 struct compunit_symtab *immediate_parent)
95554aad 9657{
af758d11 9658 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9659 if (*slot != NULL)
9660 {
9661 /* This inclusion and its children have been processed. */
9662 return;
9663 }
9664
9665 *slot = per_cu;
af758d11 9666
95554aad 9667 /* Only add a CU if it has a symbol table. */
43182c09 9668 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9669 if (cust != NULL)
ec94af83
DE
9670 {
9671 /* If this is a type unit only add its symbol table if we haven't
9672 seen it yet (type unit per_cu's can share symtabs). */
9673 if (per_cu->is_debug_types)
9674 {
43f3e411 9675 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9676 if (*slot == NULL)
9677 {
43f3e411 9678 *slot = cust;
4c39bc03 9679 result->push_back (cust);
43f3e411
DE
9680 if (cust->user == NULL)
9681 cust->user = immediate_parent;
ec94af83
DE
9682 }
9683 }
9684 else
f9125b6c 9685 {
4c39bc03 9686 result->push_back (cust);
43f3e411
DE
9687 if (cust->user == NULL)
9688 cust->user = immediate_parent;
f9125b6c 9689 }
ec94af83 9690 }
95554aad 9691
ae640021
AB
9692 if (!per_cu->imported_symtabs_empty ())
9693 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9694 {
9695 recursively_compute_inclusions (result, all_children,
43182c09
SM
9696 all_type_symtabs, ptr, per_objfile,
9697 cust);
ae640021 9698 }
95554aad
TT
9699}
9700
43f3e411 9701/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9702 PER_CU. */
9703
9704static void
43182c09
SM
9705compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9706 dwarf2_per_objfile *per_objfile)
95554aad 9707{
f4dc4d17
DE
9708 gdb_assert (! per_cu->is_debug_types);
9709
ae640021 9710 if (!per_cu->imported_symtabs_empty ())
95554aad 9711 {
ae640021 9712 int len;
4c39bc03 9713 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9714 htab_t all_children, all_type_symtabs;
43182c09 9715 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9716
9717 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9718 if (cust == NULL)
95554aad
TT
9719 return;
9720
9721 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9722 NULL, xcalloc, xfree);
ec94af83
DE
9723 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9724 NULL, xcalloc, xfree);
95554aad 9725
ae640021 9726 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9727 {
9728 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9729 all_type_symtabs, ptr, per_objfile,
9730 cust);
ec94af83 9731 }
95554aad 9732
ec94af83 9733 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9734 len = result_symtabs.size ();
43f3e411 9735 cust->includes
f6e649dd 9736 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9737 struct compunit_symtab *, len + 1);
4c39bc03
TT
9738 memcpy (cust->includes, result_symtabs.data (),
9739 len * sizeof (compunit_symtab *));
43f3e411 9740 cust->includes[len] = NULL;
95554aad 9741
95554aad 9742 htab_delete (all_children);
ec94af83 9743 htab_delete (all_type_symtabs);
95554aad
TT
9744 }
9745}
9746
9747/* Compute the 'includes' field for the symtabs of all the CUs we just
9748 read. */
9749
9750static void
ed2dc618 9751process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9752{
5989a64e 9753 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9754 {
9755 if (! iter->is_debug_types)
43182c09 9756 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9757 }
95554aad 9758
5989a64e 9759 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9760}
9761
9cdd5dbd 9762/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9763 already been loaded into memory. */
9764
9765static void
47b14e86
SM
9766process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9767 dwarf2_per_objfile *dwarf2_per_objfile,
95554aad 9768 enum language pretend_language)
10b3939b 9769{
10b3939b 9770 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9771 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9772 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9773 CORE_ADDR lowpc, highpc;
43f3e411 9774 struct compunit_symtab *cust;
10b3939b 9775 CORE_ADDR baseaddr;
4359dff1 9776 struct block *static_block;
3e29f34a 9777 CORE_ADDR addr;
10b3939b 9778
b3b3bada 9779 baseaddr = objfile->text_section_offset ();
10b3939b 9780
c89b44cd
TT
9781 /* Clear the list here in case something was left over. */
9782 cu->method_list.clear ();
10b3939b 9783
95554aad
TT
9784 cu->language = pretend_language;
9785 cu->language_defn = language_def (cu->language);
9786
c906108c 9787 /* Do line number decoding in read_file_scope () */
10b3939b 9788 process_die (cu->dies, cu);
c906108c 9789
a766d390
DE
9790 /* For now fudge the Go package. */
9791 if (cu->language == language_go)
9792 fixup_go_packaging (cu);
9793
5f48f8f3 9794 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9795 should be complete, and it should now be safe to compute all of the
9796 physnames. */
9797 compute_delayed_physnames (cu);
3da10d80 9798
c9317f21
TT
9799 if (cu->language == language_rust)
9800 rust_union_quirks (cu);
9801
fae299cd
DC
9802 /* Some compilers don't define a DW_AT_high_pc attribute for the
9803 compilation unit. If the DW_AT_high_pc is missing, synthesize
9804 it, by scanning the DIE's below the compilation unit. */
10b3939b 9805 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9806
3e29f34a 9807 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9808 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9809
9810 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9811 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9812 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9813 addrmap to help ensure it has an accurate map of pc values belonging to
9814 this comp unit. */
9815 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9816
c24bdb02 9817 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9818 SECT_OFF_TEXT (objfile),
9819 0);
c906108c 9820
43f3e411 9821 if (cust != NULL)
c906108c 9822 {
df15bd07 9823 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9824
8be455d7
JK
9825 /* Set symtab language to language from DW_AT_language. If the
9826 compilation is from a C file generated by language preprocessors, do
9827 not set the language if it was already deduced by start_subfile. */
43f3e411 9828 if (!(cu->language == language_c
40e3ad0e 9829 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9830 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9831
9832 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9833 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9834 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9835 there were bugs in prologue debug info, fixed later in GCC-4.5
9836 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9837
9838 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9839 needed, it would be wrong due to missing DW_AT_producer there.
9840
9841 Still one can confuse GDB by using non-standard GCC compilation
9842 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9843 */
ab260dad 9844 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9845 cust->locations_valid = 1;
e0d00bc7
JK
9846
9847 if (gcc_4_minor >= 5)
43f3e411 9848 cust->epilogue_unwind_valid = 1;
96408a79 9849
43f3e411 9850 cust->call_site_htab = cu->call_site_htab;
c906108c 9851 }
9291a0cd 9852
af758d11 9853 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9854
95554aad 9855 /* Push it for inclusion processing later. */
5989a64e 9856 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9857
9858 /* Not needed any more. */
c24bdb02 9859 cu->reset_builder ();
f4dc4d17 9860}
45cfd468 9861
f4dc4d17
DE
9862/* Generate full symbol information for type unit PER_CU, whose DIEs have
9863 already been loaded into memory. */
9864
9865static void
47b14e86
SM
9866process_full_type_unit (dwarf2_per_cu_data *per_cu,
9867 dwarf2_per_objfile *dwarf2_per_objfile,
f4dc4d17
DE
9868 enum language pretend_language)
9869{
9870 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9871 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9872 struct compunit_symtab *cust;
0186c6a7
DE
9873 struct signatured_type *sig_type;
9874
9875 gdb_assert (per_cu->is_debug_types);
9876 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9877
c89b44cd
TT
9878 /* Clear the list here in case something was left over. */
9879 cu->method_list.clear ();
f4dc4d17 9880
f4dc4d17
DE
9881 cu->language = pretend_language;
9882 cu->language_defn = language_def (cu->language);
9883
9884 /* The symbol tables are set up in read_type_unit_scope. */
9885 process_die (cu->dies, cu);
9886
9887 /* For now fudge the Go package. */
9888 if (cu->language == language_go)
9889 fixup_go_packaging (cu);
9890
5f48f8f3 9891 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9892 should be complete, and it should now be safe to compute all of the
9893 physnames. */
9894 compute_delayed_physnames (cu);
f4dc4d17 9895
c9317f21
TT
9896 if (cu->language == language_rust)
9897 rust_union_quirks (cu);
9898
f4dc4d17
DE
9899 /* TUs share symbol tables.
9900 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9901 of it with end_expandable_symtab. Otherwise, complete the addition of
9902 this TU's symbols to the existing symtab. */
8adb8487
TT
9903 type_unit_group_unshareable *tug_unshare =
9904 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9905 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9906 {
c24bdb02
KS
9907 buildsym_compunit *builder = cu->get_builder ();
9908 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9909 tug_unshare->compunit_symtab = cust;
f4dc4d17 9910
43f3e411 9911 if (cust != NULL)
f4dc4d17
DE
9912 {
9913 /* Set symtab language to language from DW_AT_language. If the
9914 compilation is from a C file generated by language preprocessors,
9915 do not set the language if it was already deduced by
9916 start_subfile. */
43f3e411
DE
9917 if (!(cu->language == language_c
9918 && COMPUNIT_FILETABS (cust)->language != language_c))
9919 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9920 }
9921 }
9922 else
9923 {
c24bdb02 9924 cu->get_builder ()->augment_type_symtab ();
8adb8487 9925 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9926 }
9927
af758d11 9928 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9929
9930 /* Not needed any more. */
c24bdb02 9931 cu->reset_builder ();
c906108c
SS
9932}
9933
95554aad
TT
9934/* Process an imported unit DIE. */
9935
9936static void
9937process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9938{
9939 struct attribute *attr;
9940
f4dc4d17
DE
9941 /* For now we don't handle imported units in type units. */
9942 if (cu->per_cu->is_debug_types)
9943 {
9944 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9945 " supported in type units [in module %s]"),
5e22e966 9946 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9947 }
9948
95554aad
TT
9949 attr = dwarf2_attr (die, DW_AT_import, cu);
9950 if (attr != NULL)
9951 {
0826b30a 9952 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9953 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9955 dwarf2_per_cu_data *per_cu
ab432490 9956 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9957
58990295
TV
9958 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9959 into another compilation unit, at root level. Regard this as a hint,
9960 and ignore it. */
9961 if (die->parent && die->parent->parent == NULL
9962 && per_cu->unit_type == DW_UT_compile
9963 && per_cu->lang == language_cplus)
9964 return;
9965
69d751e3 9966 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9967 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 9968 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9969
ae640021 9970 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9971 }
9972}
9973
4c8aa72d
PA
9974/* RAII object that represents a process_die scope: i.e.,
9975 starts/finishes processing a DIE. */
9976class process_die_scope
adde2bff 9977{
4c8aa72d
PA
9978public:
9979 process_die_scope (die_info *die, dwarf2_cu *cu)
9980 : m_die (die), m_cu (cu)
9981 {
9982 /* We should only be processing DIEs not already in process. */
9983 gdb_assert (!m_die->in_process);
9984 m_die->in_process = true;
9985 }
8c3cb9fa 9986
4c8aa72d
PA
9987 ~process_die_scope ()
9988 {
9989 m_die->in_process = false;
9990
9991 /* If we're done processing the DIE for the CU that owns the line
9992 header, we don't need the line header anymore. */
9993 if (m_cu->line_header_die_owner == m_die)
9994 {
9995 delete m_cu->line_header;
9996 m_cu->line_header = NULL;
9997 m_cu->line_header_die_owner = NULL;
9998 }
9999 }
10000
10001private:
10002 die_info *m_die;
10003 dwarf2_cu *m_cu;
10004};
adde2bff 10005
c906108c
SS
10006/* Process a die and its children. */
10007
10008static void
e7c27a73 10009process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10010{
4c8aa72d 10011 process_die_scope scope (die, cu);
adde2bff 10012
c906108c
SS
10013 switch (die->tag)
10014 {
10015 case DW_TAG_padding:
10016 break;
10017 case DW_TAG_compile_unit:
95554aad 10018 case DW_TAG_partial_unit:
e7c27a73 10019 read_file_scope (die, cu);
c906108c 10020 break;
348e048f
DE
10021 case DW_TAG_type_unit:
10022 read_type_unit_scope (die, cu);
10023 break;
c906108c 10024 case DW_TAG_subprogram:
0a4b0913
AB
10025 /* Nested subprograms in Fortran get a prefix. */
10026 if (cu->language == language_fortran
10027 && die->parent != NULL
10028 && die->parent->tag == DW_TAG_subprogram)
10029 cu->processing_has_namespace_info = true;
10030 /* Fall through. */
c906108c 10031 case DW_TAG_inlined_subroutine:
edb3359d 10032 read_func_scope (die, cu);
c906108c
SS
10033 break;
10034 case DW_TAG_lexical_block:
14898363
L
10035 case DW_TAG_try_block:
10036 case DW_TAG_catch_block:
e7c27a73 10037 read_lexical_block_scope (die, cu);
c906108c 10038 break;
216f72a1 10039 case DW_TAG_call_site:
96408a79
SA
10040 case DW_TAG_GNU_call_site:
10041 read_call_site_scope (die, cu);
10042 break;
c906108c 10043 case DW_TAG_class_type:
680b30c7 10044 case DW_TAG_interface_type:
c906108c
SS
10045 case DW_TAG_structure_type:
10046 case DW_TAG_union_type:
134d01f1 10047 process_structure_scope (die, cu);
c906108c
SS
10048 break;
10049 case DW_TAG_enumeration_type:
134d01f1 10050 process_enumeration_scope (die, cu);
c906108c 10051 break;
134d01f1 10052
f792889a
DJ
10053 /* These dies have a type, but processing them does not create
10054 a symbol or recurse to process the children. Therefore we can
10055 read them on-demand through read_type_die. */
c906108c 10056 case DW_TAG_subroutine_type:
72019c9c 10057 case DW_TAG_set_type:
c906108c 10058 case DW_TAG_array_type:
c906108c 10059 case DW_TAG_pointer_type:
c906108c 10060 case DW_TAG_ptr_to_member_type:
c906108c 10061 case DW_TAG_reference_type:
4297a3f0 10062 case DW_TAG_rvalue_reference_type:
c906108c 10063 case DW_TAG_string_type:
c906108c 10064 break;
134d01f1 10065
c906108c 10066 case DW_TAG_base_type:
a02abb62 10067 case DW_TAG_subrange_type:
cb249c71 10068 case DW_TAG_typedef:
134d01f1
DJ
10069 /* Add a typedef symbol for the type definition, if it has a
10070 DW_AT_name. */
f792889a 10071 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10072 break;
c906108c 10073 case DW_TAG_common_block:
e7c27a73 10074 read_common_block (die, cu);
c906108c
SS
10075 break;
10076 case DW_TAG_common_inclusion:
10077 break;
d9fa45fe 10078 case DW_TAG_namespace:
9068261f 10079 cu->processing_has_namespace_info = true;
e7c27a73 10080 read_namespace (die, cu);
d9fa45fe 10081 break;
5d7cb8df 10082 case DW_TAG_module:
9068261f 10083 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10084 read_module (die, cu);
10085 break;
d9fa45fe 10086 case DW_TAG_imported_declaration:
9068261f 10087 cu->processing_has_namespace_info = true;
74921315
KS
10088 if (read_namespace_alias (die, cu))
10089 break;
86a73007
TT
10090 /* The declaration is not a global namespace alias. */
10091 /* Fall through. */
d9fa45fe 10092 case DW_TAG_imported_module:
9068261f 10093 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10094 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10095 || cu->language != language_fortran))
b98664d3 10096 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10097 dwarf_tag_name (die->tag));
10098 read_import_statement (die, cu);
d9fa45fe 10099 break;
95554aad
TT
10100
10101 case DW_TAG_imported_unit:
10102 process_imported_unit_die (die, cu);
10103 break;
10104
71a3c369
TT
10105 case DW_TAG_variable:
10106 read_variable (die, cu);
10107 break;
10108
c906108c 10109 default:
e7c27a73 10110 new_symbol (die, NULL, cu);
c906108c
SS
10111 break;
10112 }
10113}
ca69b9e6
DE
10114\f
10115/* DWARF name computation. */
c906108c 10116
94af9270
KS
10117/* A helper function for dwarf2_compute_name which determines whether DIE
10118 needs to have the name of the scope prepended to the name listed in the
10119 die. */
10120
10121static int
10122die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10123{
1c809c68
TT
10124 struct attribute *attr;
10125
94af9270
KS
10126 switch (die->tag)
10127 {
10128 case DW_TAG_namespace:
10129 case DW_TAG_typedef:
10130 case DW_TAG_class_type:
10131 case DW_TAG_interface_type:
10132 case DW_TAG_structure_type:
10133 case DW_TAG_union_type:
10134 case DW_TAG_enumeration_type:
10135 case DW_TAG_enumerator:
10136 case DW_TAG_subprogram:
08a76f8a 10137 case DW_TAG_inlined_subroutine:
94af9270 10138 case DW_TAG_member:
74921315 10139 case DW_TAG_imported_declaration:
94af9270
KS
10140 return 1;
10141
10142 case DW_TAG_variable:
c2b0a229 10143 case DW_TAG_constant:
94af9270
KS
10144 /* We only need to prefix "globally" visible variables. These include
10145 any variable marked with DW_AT_external or any variable that
10146 lives in a namespace. [Variables in anonymous namespaces
10147 require prefixing, but they are not DW_AT_external.] */
10148
10149 if (dwarf2_attr (die, DW_AT_specification, cu))
10150 {
10151 struct dwarf2_cu *spec_cu = cu;
9a619af0 10152
94af9270
KS
10153 return die_needs_namespace (die_specification (die, &spec_cu),
10154 spec_cu);
10155 }
10156
1c809c68 10157 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10158 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10159 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10160 return 0;
10161 /* A variable in a lexical block of some kind does not need a
10162 namespace, even though in C++ such variables may be external
10163 and have a mangled name. */
10164 if (die->parent->tag == DW_TAG_lexical_block
10165 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10166 || die->parent->tag == DW_TAG_catch_block
10167 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10168 return 0;
10169 return 1;
94af9270
KS
10170
10171 default:
10172 return 0;
10173 }
10174}
10175
73b9be8b
KS
10176/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10177 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10178 defined for the given DIE. */
10179
10180static struct attribute *
10181dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10182{
10183 struct attribute *attr;
10184
10185 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10186 if (attr == NULL)
10187 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10188
10189 return attr;
10190}
10191
10192/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10193 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10194 defined for the given DIE. */
10195
10196static const char *
10197dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10198{
10199 const char *linkage_name;
10200
10201 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10202 if (linkage_name == NULL)
10203 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10204
787de330
TT
10205 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10206 See https://github.com/rust-lang/rust/issues/32925. */
10207 if (cu->language == language_rust && linkage_name != NULL
10208 && strchr (linkage_name, '{') != NULL)
10209 linkage_name = NULL;
10210
73b9be8b
KS
10211 return linkage_name;
10212}
10213
94af9270 10214/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10215 compute the physname for the object, which include a method's:
9c37b5ae 10216 - formal parameters (C++),
a766d390 10217 - receiver type (Go),
a766d390
DE
10218
10219 The term "physname" is a bit confusing.
10220 For C++, for example, it is the demangled name.
10221 For Go, for example, it's the mangled name.
94af9270 10222
af6b7be1
JB
10223 For Ada, return the DIE's linkage name rather than the fully qualified
10224 name. PHYSNAME is ignored..
10225
5989a64e 10226 The result is allocated on the objfile->per_bfd's obstack and
45940949 10227 canonicalized. */
94af9270
KS
10228
10229static const char *
15d034d0
TT
10230dwarf2_compute_name (const char *name,
10231 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10232 int physname)
10233{
5e22e966 10234 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10235
94af9270
KS
10236 if (name == NULL)
10237 name = dwarf2_name (die, cu);
10238
2ee7123e
DE
10239 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10240 but otherwise compute it by typename_concat inside GDB.
10241 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10242 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10243 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10244 will set the demangled name to the result of dwarf2_full_name, and it is
10245 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10246 if (cu->language == language_ada
10247 || (cu->language == language_fortran && physname))
10248 {
10249 /* For Ada unit, we prefer the linkage name over the name, as
10250 the former contains the exported name, which the user expects
10251 to be able to reference. Ideally, we want the user to be able
10252 to reference this entity using either natural or linkage name,
10253 but we haven't started looking at this enhancement yet. */
73b9be8b 10254 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10255
2ee7123e
DE
10256 if (linkage_name != NULL)
10257 return linkage_name;
f55ee35c
JK
10258 }
10259
94af9270
KS
10260 /* These are the only languages we know how to qualify names in. */
10261 if (name != NULL
9c37b5ae 10262 && (cu->language == language_cplus
c44af4eb
TT
10263 || cu->language == language_fortran || cu->language == language_d
10264 || cu->language == language_rust))
94af9270
KS
10265 {
10266 if (die_needs_namespace (die, cu))
10267 {
0d5cff50 10268 const char *prefix;
34a68019 10269 const char *canonical_name = NULL;
94af9270 10270
d7e74731
PA
10271 string_file buf;
10272
94af9270 10273 prefix = determine_prefix (die, cu);
94af9270
KS
10274 if (*prefix != '\0')
10275 {
43816ebc
TT
10276 gdb::unique_xmalloc_ptr<char> prefixed_name
10277 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10278
43816ebc 10279 buf.puts (prefixed_name.get ());
94af9270
KS
10280 }
10281 else
d7e74731 10282 buf.puts (name);
94af9270 10283
98bfdba5
PA
10284 /* Template parameters may be specified in the DIE's DW_AT_name, or
10285 as children with DW_TAG_template_type_param or
10286 DW_TAG_value_type_param. If the latter, add them to the name
10287 here. If the name already has template parameters, then
10288 skip this step; some versions of GCC emit both, and
10289 it is more efficient to use the pre-computed name.
10290
10291 Something to keep in mind about this process: it is very
10292 unlikely, or in some cases downright impossible, to produce
10293 something that will match the mangled name of a function.
10294 If the definition of the function has the same debug info,
10295 we should be able to match up with it anyway. But fallbacks
10296 using the minimal symbol, for instance to find a method
10297 implemented in a stripped copy of libstdc++, will not work.
10298 If we do not have debug info for the definition, we will have to
10299 match them up some other way.
10300
10301 When we do name matching there is a related problem with function
10302 templates; two instantiated function templates are allowed to
10303 differ only by their return types, which we do not add here. */
10304
10305 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10306 {
10307 struct attribute *attr;
10308 struct die_info *child;
10309 int first = 1;
10310
10311 die->building_fullname = 1;
10312
10313 for (child = die->child; child != NULL; child = child->sibling)
10314 {
10315 struct type *type;
12df843f 10316 LONGEST value;
d521ce57 10317 const gdb_byte *bytes;
98bfdba5
PA
10318 struct dwarf2_locexpr_baton *baton;
10319 struct value *v;
10320
10321 if (child->tag != DW_TAG_template_type_param
10322 && child->tag != DW_TAG_template_value_param)
10323 continue;
10324
10325 if (first)
10326 {
d7e74731 10327 buf.puts ("<");
98bfdba5
PA
10328 first = 0;
10329 }
10330 else
d7e74731 10331 buf.puts (", ");
98bfdba5
PA
10332
10333 attr = dwarf2_attr (child, DW_AT_type, cu);
10334 if (attr == NULL)
10335 {
b98664d3 10336 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10337 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10338 continue;
10339 }
10340 type = die_type (child, cu);
10341
10342 if (child->tag == DW_TAG_template_type_param)
10343 {
c1ec8cea
TT
10344 c_print_type (type, "", &buf, -1, 0, cu->language,
10345 &type_print_raw_options);
98bfdba5
PA
10346 continue;
10347 }
10348
10349 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10350 if (attr == NULL)
10351 {
b98664d3 10352 complaint (_("template parameter missing "
3e43a32a 10353 "DW_AT_const_value"));
d7e74731 10354 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10355 continue;
10356 }
10357
10358 dwarf2_const_value_attr (attr, type, name,
10359 &cu->comp_unit_obstack, cu,
10360 &value, &bytes, &baton);
10361
10362 if (TYPE_NOSIGN (type))
10363 /* GDB prints characters as NUMBER 'CHAR'. If that's
10364 changed, this can use value_print instead. */
d7e74731 10365 c_printchar (value, type, &buf);
98bfdba5
PA
10366 else
10367 {
10368 struct value_print_options opts;
10369
10370 if (baton != NULL)
10371 v = dwarf2_evaluate_loc_desc (type, NULL,
10372 baton->data,
10373 baton->size,
9f47c707
SM
10374 baton->per_cu,
10375 baton->per_objfile);
98bfdba5
PA
10376 else if (bytes != NULL)
10377 {
10378 v = allocate_value (type);
10379 memcpy (value_contents_writeable (v), bytes,
10380 TYPE_LENGTH (type));
10381 }
10382 else
10383 v = value_from_longest (type, value);
10384
3e43a32a
MS
10385 /* Specify decimal so that we do not depend on
10386 the radix. */
98bfdba5
PA
10387 get_formatted_print_options (&opts, 'd');
10388 opts.raw = 1;
d7e74731 10389 value_print (v, &buf, &opts);
98bfdba5 10390 release_value (v);
98bfdba5
PA
10391 }
10392 }
10393
10394 die->building_fullname = 0;
10395
10396 if (!first)
10397 {
10398 /* Close the argument list, with a space if necessary
10399 (nested templates). */
d7e74731
PA
10400 if (!buf.empty () && buf.string ().back () == '>')
10401 buf.puts (" >");
98bfdba5 10402 else
d7e74731 10403 buf.puts (">");
98bfdba5
PA
10404 }
10405 }
10406
9c37b5ae 10407 /* For C++ methods, append formal parameter type
94af9270 10408 information, if PHYSNAME. */
6e70227d 10409
94af9270 10410 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10411 && cu->language == language_cplus)
94af9270
KS
10412 {
10413 struct type *type = read_type_die (die, cu);
10414
d7e74731 10415 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10416 &type_print_raw_options);
94af9270 10417
9c37b5ae 10418 if (cu->language == language_cplus)
94af9270 10419 {
60430eff
DJ
10420 /* Assume that an artificial first parameter is
10421 "this", but do not crash if it is not. RealView
10422 marks unnamed (and thus unused) parameters as
10423 artificial; there is no way to differentiate
10424 the two cases. */
1f704f76 10425 if (type->num_fields () > 0
94af9270 10426 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10427 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10428 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10429 0))))
d7e74731 10430 buf.puts (" const");
94af9270
KS
10431 }
10432 }
10433
d7e74731 10434 const std::string &intermediate_name = buf.string ();
94af9270
KS
10435
10436 if (cu->language == language_cplus)
34a68019 10437 canonical_name
322a8516 10438 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10439 objfile);
34a68019
TT
10440
10441 /* If we only computed INTERMEDIATE_NAME, or if
10442 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10443 intern it. */
322a8516 10444 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10445 name = objfile->intern (intermediate_name);
34a68019
TT
10446 else
10447 name = canonical_name;
94af9270
KS
10448 }
10449 }
10450
10451 return name;
10452}
10453
0114d602
DJ
10454/* Return the fully qualified name of DIE, based on its DW_AT_name.
10455 If scope qualifiers are appropriate they will be added. The result
34a68019 10456 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10457 not have a name. NAME may either be from a previous call to
10458 dwarf2_name or NULL.
10459
9c37b5ae 10460 The output string will be canonicalized (if C++). */
0114d602
DJ
10461
10462static const char *
15d034d0 10463dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10464{
94af9270
KS
10465 return dwarf2_compute_name (name, die, cu, 0);
10466}
0114d602 10467
94af9270
KS
10468/* Construct a physname for the given DIE in CU. NAME may either be
10469 from a previous call to dwarf2_name or NULL. The result will be
10470 allocated on the objfile_objstack or NULL if the DIE does not have a
10471 name.
0114d602 10472
9c37b5ae 10473 The output string will be canonicalized (if C++). */
0114d602 10474
94af9270 10475static const char *
15d034d0 10476dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10477{
5e22e966 10478 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10479 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10480 int need_copy = 1;
10481
10482 /* In this case dwarf2_compute_name is just a shortcut not building anything
10483 on its own. */
10484 if (!die_needs_namespace (die, cu))
10485 return dwarf2_compute_name (name, die, cu, 1);
10486
906bb4c5
TT
10487 if (cu->language != language_rust)
10488 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10489
10490 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10491 has computed. */
791afaa2 10492 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10493 if (mangled != NULL)
900e11f9 10494 {
900e11f9 10495
59cc4834
JB
10496 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10497 {
10498 /* Do nothing (do not demangle the symbol name). */
10499 }
10500 else if (cu->language == language_go)
a766d390 10501 {
5e2db402
TT
10502 /* This is a lie, but we already lie to the caller new_symbol.
10503 new_symbol assumes we return the mangled name.
a766d390 10504 This just undoes that lie until things are cleaned up. */
a766d390
DE
10505 }
10506 else
10507 {
0eb876f5
JB
10508 /* Use DMGL_RET_DROP for C++ template functions to suppress
10509 their return type. It is easier for GDB users to search
10510 for such functions as `name(params)' than `long name(params)'.
10511 In such case the minimal symbol names do not match the full
10512 symbol names but for template functions there is never a need
10513 to look up their definition from their declaration so
10514 the only disadvantage remains the minimal symbol variant
10515 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10516 demangled.reset (gdb_demangle (mangled,
10517 (DMGL_PARAMS | DMGL_ANSI
10518 | DMGL_RET_DROP)));
a766d390 10519 }
900e11f9 10520 if (demangled)
791afaa2 10521 canon = demangled.get ();
900e11f9
JK
10522 else
10523 {
10524 canon = mangled;
10525 need_copy = 0;
10526 }
10527 }
10528
10529 if (canon == NULL || check_physname)
10530 {
10531 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10532
10533 if (canon != NULL && strcmp (physname, canon) != 0)
10534 {
10535 /* It may not mean a bug in GDB. The compiler could also
10536 compute DW_AT_linkage_name incorrectly. But in such case
10537 GDB would need to be bug-to-bug compatible. */
10538
b98664d3 10539 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10540 "(from linkage <%s>) - DIE at %s [in module %s]"),
10541 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10542 objfile_name (objfile));
900e11f9
JK
10543
10544 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10545 is available here - over computed PHYSNAME. It is safer
10546 against both buggy GDB and buggy compilers. */
10547
10548 retval = canon;
10549 }
10550 else
10551 {
10552 retval = physname;
10553 need_copy = 0;
10554 }
10555 }
10556 else
10557 retval = canon;
10558
10559 if (need_copy)
be1e3d3e 10560 retval = objfile->intern (retval);
900e11f9 10561
900e11f9 10562 return retval;
0114d602
DJ
10563}
10564
74921315
KS
10565/* Inspect DIE in CU for a namespace alias. If one exists, record
10566 a new symbol for it.
10567
10568 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10569
10570static int
10571read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10572{
10573 struct attribute *attr;
10574
10575 /* If the die does not have a name, this is not a namespace
10576 alias. */
10577 attr = dwarf2_attr (die, DW_AT_name, cu);
10578 if (attr != NULL)
10579 {
10580 int num;
10581 struct die_info *d = die;
10582 struct dwarf2_cu *imported_cu = cu;
10583
10584 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10585 keep inspecting DIEs until we hit the underlying import. */
10586#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10587 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10588 {
10589 attr = dwarf2_attr (d, DW_AT_import, cu);
10590 if (attr == NULL)
10591 break;
10592
10593 d = follow_die_ref (d, attr, &imported_cu);
10594 if (d->tag != DW_TAG_imported_declaration)
10595 break;
10596 }
10597
10598 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10599 {
b98664d3 10600 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10601 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10602 return 0;
10603 }
10604
10605 if (attr != NULL)
10606 {
10607 struct type *type;
0826b30a 10608 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10609
aa66c379 10610 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10611 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10612 {
10613 /* This declaration is a global namespace alias. Add
10614 a symbol for it whose type is the aliased namespace. */
10615 new_symbol (die, type, cu);
10616 return 1;
10617 }
10618 }
10619 }
10620
10621 return 0;
10622}
10623
22cee43f 10624/* Return the using directives repository (global or local?) to use in the
804d2729 10625 current context for CU.
22cee43f
PMR
10626
10627 For Ada, imported declarations can materialize renamings, which *may* be
10628 global. However it is impossible (for now?) in DWARF to distinguish
10629 "external" imported declarations and "static" ones. As all imported
10630 declarations seem to be static in all other languages, make them all CU-wide
10631 global only in Ada. */
10632
10633static struct using_direct **
804d2729 10634using_directives (struct dwarf2_cu *cu)
22cee43f 10635{
c24bdb02
KS
10636 if (cu->language == language_ada
10637 && cu->get_builder ()->outermost_context_p ())
10638 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10639 else
c24bdb02 10640 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10641}
10642
27aa8d6a
SW
10643/* Read the import statement specified by the given die and record it. */
10644
10645static void
10646read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10647{
5e22e966 10648 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10649 struct attribute *import_attr;
32019081 10650 struct die_info *imported_die, *child_die;
de4affc9 10651 struct dwarf2_cu *imported_cu;
27aa8d6a 10652 const char *imported_name;
794684b6 10653 const char *imported_name_prefix;
13387711
SW
10654 const char *canonical_name;
10655 const char *import_alias;
10656 const char *imported_declaration = NULL;
794684b6 10657 const char *import_prefix;
eb1e02fd 10658 std::vector<const char *> excludes;
13387711 10659
27aa8d6a
SW
10660 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10661 if (import_attr == NULL)
10662 {
b98664d3 10663 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10664 dwarf_tag_name (die->tag));
10665 return;
10666 }
10667
de4affc9
CC
10668 imported_cu = cu;
10669 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10670 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10671 if (imported_name == NULL)
10672 {
10673 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10674
10675 The import in the following code:
10676 namespace A
10677 {
10678 typedef int B;
10679 }
10680
10681 int main ()
10682 {
10683 using A::B;
10684 B b;
10685 return b;
10686 }
10687
10688 ...
10689 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10690 <52> DW_AT_decl_file : 1
10691 <53> DW_AT_decl_line : 6
10692 <54> DW_AT_import : <0x75>
10693 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10694 <59> DW_AT_name : B
10695 <5b> DW_AT_decl_file : 1
10696 <5c> DW_AT_decl_line : 2
10697 <5d> DW_AT_type : <0x6e>
10698 ...
10699 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10700 <76> DW_AT_byte_size : 4
10701 <77> DW_AT_encoding : 5 (signed)
10702
10703 imports the wrong die ( 0x75 instead of 0x58 ).
10704 This case will be ignored until the gcc bug is fixed. */
10705 return;
10706 }
10707
82856980
SW
10708 /* Figure out the local name after import. */
10709 import_alias = dwarf2_name (die, cu);
27aa8d6a 10710
794684b6
SW
10711 /* Figure out where the statement is being imported to. */
10712 import_prefix = determine_prefix (die, cu);
10713
10714 /* Figure out what the scope of the imported die is and prepend it
10715 to the name of the imported die. */
de4affc9 10716 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10717
f55ee35c
JK
10718 if (imported_die->tag != DW_TAG_namespace
10719 && imported_die->tag != DW_TAG_module)
794684b6 10720 {
13387711
SW
10721 imported_declaration = imported_name;
10722 canonical_name = imported_name_prefix;
794684b6 10723 }
13387711 10724 else if (strlen (imported_name_prefix) > 0)
12aaed36 10725 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10726 imported_name_prefix,
10727 (cu->language == language_d ? "." : "::"),
10728 imported_name, (char *) NULL);
13387711
SW
10729 else
10730 canonical_name = imported_name;
794684b6 10731
32019081
JK
10732 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10733 for (child_die = die->child; child_die && child_die->tag;
436c571c 10734 child_die = child_die->sibling)
32019081
JK
10735 {
10736 /* DWARF-4: A Fortran use statement with a “rename list” may be
10737 represented by an imported module entry with an import attribute
10738 referring to the module and owned entries corresponding to those
10739 entities that are renamed as part of being imported. */
10740
10741 if (child_die->tag != DW_TAG_imported_declaration)
10742 {
b98664d3 10743 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10744 "- DIE at %s [in module %s]"),
10745 sect_offset_str (child_die->sect_off),
10746 objfile_name (objfile));
32019081
JK
10747 continue;
10748 }
10749
10750 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10751 if (import_attr == NULL)
10752 {
b98664d3 10753 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10754 dwarf_tag_name (child_die->tag));
10755 continue;
10756 }
10757
10758 imported_cu = cu;
10759 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10760 &imported_cu);
10761 imported_name = dwarf2_name (imported_die, imported_cu);
10762 if (imported_name == NULL)
10763 {
b98664d3 10764 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10765 "imported name - DIE at %s [in module %s]"),
10766 sect_offset_str (child_die->sect_off),
10767 objfile_name (objfile));
32019081
JK
10768 continue;
10769 }
10770
eb1e02fd 10771 excludes.push_back (imported_name);
32019081
JK
10772
10773 process_die (child_die, cu);
10774 }
10775
804d2729 10776 add_using_directive (using_directives (cu),
22cee43f
PMR
10777 import_prefix,
10778 canonical_name,
10779 import_alias,
10780 imported_declaration,
10781 excludes,
10782 0,
10783 &objfile->objfile_obstack);
27aa8d6a
SW
10784}
10785
5230b05a
WT
10786/* ICC<14 does not output the required DW_AT_declaration on incomplete
10787 types, but gives them a size of zero. Starting with version 14,
10788 ICC is compatible with GCC. */
10789
9068261f 10790static bool
5230b05a
WT
10791producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10792{
10793 if (!cu->checked_producer)
10794 check_producer (cu);
10795
10796 return cu->producer_is_icc_lt_14;
10797}
10798
eb77c9df
AB
10799/* ICC generates a DW_AT_type for C void functions. This was observed on
10800 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10801 which says that void functions should not have a DW_AT_type. */
10802
10803static bool
10804producer_is_icc (struct dwarf2_cu *cu)
10805{
10806 if (!cu->checked_producer)
10807 check_producer (cu);
10808
10809 return cu->producer_is_icc;
10810}
10811
1b80a9fa
JK
10812/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10813 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10814 this, it was first present in GCC release 4.3.0. */
10815
9068261f 10816static bool
1b80a9fa
JK
10817producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10818{
10819 if (!cu->checked_producer)
10820 check_producer (cu);
10821
10822 return cu->producer_is_gcc_lt_4_3;
10823}
10824
d721ba37
PA
10825static file_and_directory
10826find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10827{
d721ba37
PA
10828 file_and_directory res;
10829
9291a0cd
TT
10830 /* Find the filename. Do not use dwarf2_name here, since the filename
10831 is not a source language identifier. */
d721ba37
PA
10832 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10833 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10834
d721ba37
PA
10835 if (res.comp_dir == NULL
10836 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10837 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10838 {
d721ba37
PA
10839 res.comp_dir_storage = ldirname (res.name);
10840 if (!res.comp_dir_storage.empty ())
10841 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10842 }
d721ba37 10843 if (res.comp_dir != NULL)
9291a0cd
TT
10844 {
10845 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10846 directory, get rid of it. */
d721ba37 10847 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10848
d721ba37
PA
10849 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10850 res.comp_dir = cp + 1;
9291a0cd
TT
10851 }
10852
d721ba37
PA
10853 if (res.name == NULL)
10854 res.name = "<unknown>";
10855
10856 return res;
9291a0cd
TT
10857}
10858
f4dc4d17
DE
10859/* Handle DW_AT_stmt_list for a compilation unit.
10860 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10861 COMP_DIR is the compilation directory. LOWPC is passed to
10862 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10863
10864static void
10865handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10866 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10867{
5e22e966 10868 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10869 struct attribute *attr;
527f3840
JK
10870 struct line_header line_header_local;
10871 hashval_t line_header_local_hash;
527f3840
JK
10872 void **slot;
10873 int decode_mapping;
2ab95328 10874
f4dc4d17
DE
10875 gdb_assert (! cu->per_cu->is_debug_types);
10876
2ab95328 10877 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10878 if (attr == NULL)
10879 return;
10880
9c541725 10881 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10882
10883 /* The line header hash table is only created if needed (it exists to
10884 prevent redundant reading of the line table for partial_units).
10885 If we're given a partial_unit, we'll need it. If we're given a
10886 compile_unit, then use the line header hash table if it's already
10887 created, but don't create one just yet. */
10888
5989a64e 10889 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10890 && die->tag == DW_TAG_partial_unit)
2ab95328 10891 {
5989a64e 10892 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10893 .reset (htab_create_alloc (127, line_header_hash_voidp,
10894 line_header_eq_voidp,
10895 free_line_header_voidp,
10896 xcalloc, xfree));
527f3840 10897 }
2ab95328 10898
9c541725 10899 line_header_local.sect_off = line_offset;
527f3840
JK
10900 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10901 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10902 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10903 {
5989a64e 10904 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10905 &line_header_local,
10906 line_header_local_hash, NO_INSERT);
10907
10908 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10909 is not present in *SLOT (since if there is something in *SLOT then
10910 it will be for a partial_unit). */
10911 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10912 {
527f3840 10913 gdb_assert (*slot != NULL);
9a3c8263 10914 cu->line_header = (struct line_header *) *slot;
527f3840 10915 return;
dee91e82 10916 }
2ab95328 10917 }
527f3840
JK
10918
10919 /* dwarf_decode_line_header does not yet provide sufficient information.
10920 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10921 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10922 if (lh == NULL)
527f3840 10923 return;
4c8aa72d
PA
10924
10925 cu->line_header = lh.release ();
10926 cu->line_header_die_owner = die;
527f3840 10927
5989a64e 10928 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10929 slot = NULL;
10930 else
10931 {
5989a64e 10932 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10933 &line_header_local,
10934 line_header_local_hash, INSERT);
10935 gdb_assert (slot != NULL);
10936 }
10937 if (slot != NULL && *slot == NULL)
10938 {
10939 /* This newly decoded line number information unit will be owned
10940 by line_header_hash hash table. */
10941 *slot = cu->line_header;
4c8aa72d 10942 cu->line_header_die_owner = NULL;
527f3840
JK
10943 }
10944 else
10945 {
10946 /* We cannot free any current entry in (*slot) as that struct line_header
10947 may be already used by multiple CUs. Create only temporary decoded
10948 line_header for this CU - it may happen at most once for each line
10949 number information unit. And if we're not using line_header_hash
10950 then this is what we want as well. */
10951 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10952 }
10953 decode_mapping = (die->tag != DW_TAG_partial_unit);
10954 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10955 decode_mapping);
fff8551c 10956
2ab95328
TT
10957}
10958
95554aad 10959/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10960
c906108c 10961static void
e7c27a73 10962read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10963{
5e22e966 10964 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10965 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10966 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10967 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10968 CORE_ADDR highpc = ((CORE_ADDR) 0);
10969 struct attribute *attr;
c906108c 10970 struct die_info *child_die;
e142c38c 10971 CORE_ADDR baseaddr;
6e70227d 10972
380618d6 10973 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10974 baseaddr = objfile->text_section_offset ();
c906108c 10975
fae299cd 10976 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10977
10978 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10979 from finish_block. */
2acceee2 10980 if (lowpc == ((CORE_ADDR) -1))
c906108c 10981 lowpc = highpc;
3e29f34a 10982 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10983
d721ba37 10984 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10985
f4b8a18d
KW
10986 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10987 standardised yet. As a workaround for the language detection we fall
10988 back to the DW_AT_producer string. */
10989 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10990 cu->language = language_opencl;
10991
3019eac3
DE
10992 /* Similar hack for Go. */
10993 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10994 set_cu_language (DW_LANG_Go, cu);
10995
c24bdb02 10996 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10997
10998 /* Decode line number information if present. We do this before
10999 processing child DIEs, so that the line header table is available
11000 for DW_AT_decl_file. */
d721ba37 11001 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11002
11003 /* Process all dies in compilation unit. */
11004 if (die->child != NULL)
11005 {
11006 child_die = die->child;
11007 while (child_die && child_die->tag)
11008 {
11009 process_die (child_die, cu);
436c571c 11010 child_die = child_die->sibling;
3019eac3
DE
11011 }
11012 }
11013
11014 /* Decode macro information, if present. Dwarf 2 macro information
11015 refers to information in the line number info statement program
11016 header, so we can only read it if we've read the header
11017 successfully. */
0af92d60
JK
11018 attr = dwarf2_attr (die, DW_AT_macros, cu);
11019 if (attr == NULL)
11020 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11021 if (attr && cu->line_header)
11022 {
11023 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11024 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11025
43f3e411 11026 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11027 }
11028 else
11029 {
11030 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11031 if (attr && cu->line_header)
11032 {
11033 unsigned int macro_offset = DW_UNSND (attr);
11034
43f3e411 11035 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11036 }
11037 }
3019eac3
DE
11038}
11039
c24bdb02
KS
11040void
11041dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11042{
f4dc4d17
DE
11043 struct type_unit_group *tu_group;
11044 int first_time;
3019eac3 11045 struct attribute *attr;
9c541725 11046 unsigned int i;
0186c6a7 11047 struct signatured_type *sig_type;
3019eac3 11048
f4dc4d17 11049 gdb_assert (per_cu->is_debug_types);
0186c6a7 11050 sig_type = (struct signatured_type *) per_cu;
3019eac3 11051
c24bdb02 11052 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11053
f4dc4d17 11054 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11055 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11056 if (sig_type->type_unit_group == NULL)
c24bdb02 11057 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11058 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11059
11060 /* If we've already processed this stmt_list there's no real need to
11061 do it again, we could fake it and just recreate the part we need
11062 (file name,index -> symtab mapping). If data shows this optimization
11063 is useful we can do it then. */
8adb8487
TT
11064 type_unit_group_unshareable *tug_unshare
11065 = per_objfile->get_type_unit_group_unshareable (tu_group);
11066 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11067
11068 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11069 debug info. */
fff8551c 11070 line_header_up lh;
f4dc4d17 11071 if (attr != NULL)
3019eac3 11072 {
9c541725 11073 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11074 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11075 }
11076 if (lh == NULL)
11077 {
11078 if (first_time)
c24bdb02 11079 start_symtab ("", NULL, 0);
f4dc4d17
DE
11080 else
11081 {
8adb8487 11082 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11083 gdb_assert (m_builder == nullptr);
8adb8487 11084 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11085 m_builder.reset (new struct buildsym_compunit
11086 (COMPUNIT_OBJFILE (cust), "",
11087 COMPUNIT_DIRNAME (cust),
11088 compunit_language (cust),
11089 0, cust));
770479f2 11090 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11091 }
f4dc4d17 11092 return;
3019eac3
DE
11093 }
11094
c24bdb02
KS
11095 line_header = lh.release ();
11096 line_header_die_owner = die;
3019eac3 11097
f4dc4d17
DE
11098 if (first_time)
11099 {
c24bdb02 11100 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11101
1fd60fc0
DE
11102 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11103 still initializing it, and our caller (a few levels up)
11104 process_full_type_unit still needs to know if this is the first
11105 time. */
11106
8adb8487 11107 tug_unshare->symtabs
4ac93832
TT
11108 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11109 struct symtab *, line_header->file_names_size ());
3019eac3 11110
7ba99d21
AT
11111 auto &file_names = line_header->file_names ();
11112 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11113 {
7ba99d21 11114 file_entry &fe = file_names[i];
c24bdb02
KS
11115 dwarf2_start_subfile (this, fe.name,
11116 fe.include_dir (line_header));
11117 buildsym_compunit *b = get_builder ();
11118 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11119 {
4c8aa72d
PA
11120 /* NOTE: start_subfile will recognize when it's been
11121 passed a file it has already seen. So we can't
11122 assume there's a simple mapping from
11123 cu->line_header->file_names to subfiles, plus
11124 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11125 b->get_current_subfile ()->symtab
11126 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11127 }
11128
c24bdb02 11129 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11130 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11131 }
11132 }
11133 else
3019eac3 11134 {
c24bdb02 11135 gdb_assert (m_builder == nullptr);
8adb8487 11136 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11137 m_builder.reset (new struct buildsym_compunit
11138 (COMPUNIT_OBJFILE (cust), "",
11139 COMPUNIT_DIRNAME (cust),
11140 compunit_language (cust),
11141 0, cust));
770479f2 11142 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11143
7ba99d21
AT
11144 auto &file_names = line_header->file_names ();
11145 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11146 {
7ba99d21 11147 file_entry &fe = file_names[i];
8adb8487 11148 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11149 }
3019eac3
DE
11150 }
11151
f4dc4d17
DE
11152 /* The main symtab is allocated last. Type units don't have DW_AT_name
11153 so they don't have a "real" (so to speak) symtab anyway.
11154 There is later code that will assign the main symtab to all symbols
11155 that don't have one. We need to handle the case of a symbol with a
11156 missing symtab (DW_AT_decl_file) anyway. */
11157}
3019eac3 11158
f4dc4d17
DE
11159/* Process DW_TAG_type_unit.
11160 For TUs we want to skip the first top level sibling if it's not the
11161 actual type being defined by this TU. In this case the first top
11162 level sibling is there to provide context only. */
3019eac3 11163
f4dc4d17
DE
11164static void
11165read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11166{
11167 struct die_info *child_die;
3019eac3 11168
f4dc4d17
DE
11169 prepare_one_comp_unit (cu, die, language_minimal);
11170
11171 /* Initialize (or reinitialize) the machinery for building symtabs.
11172 We do this before processing child DIEs, so that the line header table
11173 is available for DW_AT_decl_file. */
c24bdb02 11174 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11175
11176 if (die->child != NULL)
11177 {
11178 child_die = die->child;
11179 while (child_die && child_die->tag)
11180 {
11181 process_die (child_die, cu);
436c571c 11182 child_die = child_die->sibling;
f4dc4d17
DE
11183 }
11184 }
3019eac3
DE
11185}
11186\f
80626a55
DE
11187/* DWO/DWP files.
11188
11189 http://gcc.gnu.org/wiki/DebugFission
11190 http://gcc.gnu.org/wiki/DebugFissionDWP
11191
11192 To simplify handling of both DWO files ("object" files with the DWARF info)
11193 and DWP files (a file with the DWOs packaged up into one file), we treat
11194 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11195
11196static hashval_t
11197hash_dwo_file (const void *item)
11198{
9a3c8263 11199 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11200 hashval_t hash;
3019eac3 11201
a2ce51a0
DE
11202 hash = htab_hash_string (dwo_file->dwo_name);
11203 if (dwo_file->comp_dir != NULL)
11204 hash += htab_hash_string (dwo_file->comp_dir);
11205 return hash;
3019eac3
DE
11206}
11207
11208static int
11209eq_dwo_file (const void *item_lhs, const void *item_rhs)
11210{
9a3c8263
SM
11211 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11212 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11213
a2ce51a0
DE
11214 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11215 return 0;
11216 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11217 return lhs->comp_dir == rhs->comp_dir;
11218 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11219}
11220
11221/* Allocate a hash table for DWO files. */
11222
51ac9db5 11223static htab_up
298e9637 11224allocate_dwo_file_hash_table ()
3019eac3 11225{
51ac9db5
SM
11226 auto delete_dwo_file = [] (void *item)
11227 {
11228 struct dwo_file *dwo_file = (struct dwo_file *) item;
11229
11230 delete dwo_file;
11231 };
11232
bc68fb19
TT
11233 return htab_up (htab_create_alloc (41,
11234 hash_dwo_file,
11235 eq_dwo_file,
11236 delete_dwo_file,
11237 xcalloc, xfree));
3019eac3
DE
11238}
11239
80626a55
DE
11240/* Lookup DWO file DWO_NAME. */
11241
11242static void **
ed2dc618
SM
11243lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11244 const char *dwo_name,
11245 const char *comp_dir)
80626a55
DE
11246{
11247 struct dwo_file find_entry;
11248 void **slot;
11249
5989a64e
SM
11250 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11251 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11252
0ac5b59e
DE
11253 find_entry.dwo_name = dwo_name;
11254 find_entry.comp_dir = comp_dir;
5989a64e 11255 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11256 INSERT);
80626a55
DE
11257
11258 return slot;
11259}
11260
3019eac3
DE
11261static hashval_t
11262hash_dwo_unit (const void *item)
11263{
9a3c8263 11264 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11265
11266 /* This drops the top 32 bits of the id, but is ok for a hash. */
11267 return dwo_unit->signature;
11268}
11269
11270static int
11271eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11272{
9a3c8263
SM
11273 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11274 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11275
11276 /* The signature is assumed to be unique within the DWO file.
11277 So while object file CU dwo_id's always have the value zero,
11278 that's OK, assuming each object file DWO file has only one CU,
11279 and that's the rule for now. */
11280 return lhs->signature == rhs->signature;
11281}
11282
11283/* Allocate a hash table for DWO CUs,TUs.
11284 There is one of these tables for each of CUs,TUs for each DWO file. */
11285
b0b6a987 11286static htab_up
298e9637 11287allocate_dwo_unit_table ()
3019eac3
DE
11288{
11289 /* Start out with a pretty small number.
11290 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11291 return htab_up (htab_create_alloc (3,
11292 hash_dwo_unit,
11293 eq_dwo_unit,
11294 NULL, xcalloc, xfree));
3019eac3
DE
11295}
11296
19c3d4c9 11297/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11298
11299static void
19c3d4c9
DE
11300create_dwo_cu_reader (const struct die_reader_specs *reader,
11301 const gdb_byte *info_ptr,
11302 struct die_info *comp_unit_die,
c0ab21c2
TT
11303 struct dwo_file *dwo_file,
11304 struct dwo_unit *dwo_unit)
3019eac3
DE
11305{
11306 struct dwarf2_cu *cu = reader->cu;
9c541725 11307 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11308 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11309
a084a2a6
AT
11310 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11311 if (!signature.has_value ())
3019eac3 11312 {
b98664d3 11313 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11314 " its dwo_id [in module %s]"),
9d8780f0 11315 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11316 return;
11317 }
11318
3019eac3 11319 dwo_unit->dwo_file = dwo_file;
a084a2a6 11320 dwo_unit->signature = *signature;
8a0459fd 11321 dwo_unit->section = section;
9c541725 11322 dwo_unit->sect_off = sect_off;
3019eac3
DE
11323 dwo_unit->length = cu->per_cu->length;
11324
b4f54984 11325 if (dwarf_read_debug)
9d8780f0
SM
11326 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11327 sect_offset_str (sect_off),
9c541725 11328 hex_string (dwo_unit->signature));
3019eac3
DE
11329}
11330
33c5cd75 11331/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11332 Note: This function processes DWO files only, not DWP files. */
3019eac3 11333
33c5cd75 11334static void
ed2dc618 11335create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11336 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11337 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11338{
11339 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11340 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11341 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11342
96b79293 11343 section.read (objfile);
33c5cd75 11344 info_ptr = section.buffer;
3019eac3
DE
11345
11346 if (info_ptr == NULL)
33c5cd75 11347 return;
3019eac3 11348
b4f54984 11349 if (dwarf_read_debug)
19c3d4c9
DE
11350 {
11351 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11352 section.get_name (),
11353 section.get_file_name ());
19c3d4c9 11354 }
3019eac3 11355
33c5cd75 11356 end_ptr = info_ptr + section.size;
3019eac3
DE
11357 while (info_ptr < end_ptr)
11358 {
11359 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11360 struct dwo_unit read_unit {};
33c5cd75
DB
11361 struct dwo_unit *dwo_unit;
11362 void **slot;
11363 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11364
11365 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11366 per_cu.per_bfd = per_bfd;
3019eac3 11367 per_cu.is_debug_types = 0;
33c5cd75
DB
11368 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11369 per_cu.section = &section;
11370
ab432490 11371 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11372 if (!reader.dummy_p)
11373 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11374 &dwo_file, &read_unit);
33c5cd75
DB
11375 info_ptr += per_cu.length;
11376
11377 // If the unit could not be parsed, skip it.
c0ab21c2 11378 if (read_unit.dwo_file == NULL)
33c5cd75 11379 continue;
3019eac3 11380
33c5cd75 11381 if (cus_htab == NULL)
298e9637 11382 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11383
1859c670 11384 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11385 struct dwo_unit);
c0ab21c2 11386 *dwo_unit = read_unit;
b0b6a987 11387 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11388 gdb_assert (slot != NULL);
11389 if (*slot != NULL)
19c3d4c9 11390 {
33c5cd75
DB
11391 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11392 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11393
b98664d3 11394 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11395 " the entry at offset %s, signature %s"),
11396 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11397 hex_string (dwo_unit->signature));
19c3d4c9 11398 }
33c5cd75 11399 *slot = (void *)dwo_unit;
3019eac3 11400 }
3019eac3
DE
11401}
11402
80626a55
DE
11403/* DWP file .debug_{cu,tu}_index section format:
11404 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11405
d2415c6c
DE
11406 DWP Version 1:
11407
80626a55
DE
11408 Both index sections have the same format, and serve to map a 64-bit
11409 signature to a set of section numbers. Each section begins with a header,
11410 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11411 indexes, and a pool of 32-bit section numbers. The index sections will be
11412 aligned at 8-byte boundaries in the file.
11413
d2415c6c
DE
11414 The index section header consists of:
11415
11416 V, 32 bit version number
11417 -, 32 bits unused
11418 N, 32 bit number of compilation units or type units in the index
11419 M, 32 bit number of slots in the hash table
80626a55 11420
d2415c6c 11421 Numbers are recorded using the byte order of the application binary.
80626a55 11422
d2415c6c
DE
11423 The hash table begins at offset 16 in the section, and consists of an array
11424 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11425 order of the application binary). Unused slots in the hash table are 0.
11426 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11427
d2415c6c
DE
11428 The parallel table begins immediately after the hash table
11429 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11430 array of 32-bit indexes (using the byte order of the application binary),
11431 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11432 table contains a 32-bit index into the pool of section numbers. For unused
11433 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11434
73869dc2
DE
11435 The pool of section numbers begins immediately following the hash table
11436 (at offset 16 + 12 * M from the beginning of the section). The pool of
11437 section numbers consists of an array of 32-bit words (using the byte order
11438 of the application binary). Each item in the array is indexed starting
11439 from 0. The hash table entry provides the index of the first section
11440 number in the set. Additional section numbers in the set follow, and the
11441 set is terminated by a 0 entry (section number 0 is not used in ELF).
11442
11443 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11444 section must be the first entry in the set, and the .debug_abbrev.dwo must
11445 be the second entry. Other members of the set may follow in any order.
11446
11447 ---
11448
11449 DWP Version 2:
11450
11451 DWP Version 2 combines all the .debug_info, etc. sections into one,
11452 and the entries in the index tables are now offsets into these sections.
11453 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11454 section.
11455
11456 Index Section Contents:
11457 Header
11458 Hash Table of Signatures dwp_hash_table.hash_table
11459 Parallel Table of Indices dwp_hash_table.unit_table
11460 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11461 Table of Section Sizes dwp_hash_table.v2.sizes
11462
11463 The index section header consists of:
11464
11465 V, 32 bit version number
11466 L, 32 bit number of columns in the table of section offsets
11467 N, 32 bit number of compilation units or type units in the index
11468 M, 32 bit number of slots in the hash table
11469
11470 Numbers are recorded using the byte order of the application binary.
11471
11472 The hash table has the same format as version 1.
11473 The parallel table of indices has the same format as version 1,
11474 except that the entries are origin-1 indices into the table of sections
11475 offsets and the table of section sizes.
11476
11477 The table of offsets begins immediately following the parallel table
11478 (at offset 16 + 12 * M from the beginning of the section). The table is
11479 a two-dimensional array of 32-bit words (using the byte order of the
11480 application binary), with L columns and N+1 rows, in row-major order.
11481 Each row in the array is indexed starting from 0. The first row provides
11482 a key to the remaining rows: each column in this row provides an identifier
11483 for a debug section, and the offsets in the same column of subsequent rows
11484 refer to that section. The section identifiers are:
11485
11486 DW_SECT_INFO 1 .debug_info.dwo
11487 DW_SECT_TYPES 2 .debug_types.dwo
11488 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11489 DW_SECT_LINE 4 .debug_line.dwo
11490 DW_SECT_LOC 5 .debug_loc.dwo
11491 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11492 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11493 DW_SECT_MACRO 8 .debug_macro.dwo
11494
11495 The offsets provided by the CU and TU index sections are the base offsets
11496 for the contributions made by each CU or TU to the corresponding section
11497 in the package file. Each CU and TU header contains an abbrev_offset
11498 field, used to find the abbreviations table for that CU or TU within the
11499 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11500 be interpreted as relative to the base offset given in the index section.
11501 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11502 should be interpreted as relative to the base offset for .debug_line.dwo,
11503 and offsets into other debug sections obtained from DWARF attributes should
11504 also be interpreted as relative to the corresponding base offset.
11505
11506 The table of sizes begins immediately following the table of offsets.
11507 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11508 with L columns and N rows, in row-major order. Each row in the array is
11509 indexed starting from 1 (row 0 is shared by the two tables).
11510
11511 ---
11512
11513 Hash table lookup is handled the same in version 1 and 2:
11514
11515 We assume that N and M will not exceed 2^32 - 1.
11516 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11517
d2415c6c
DE
11518 Given a 64-bit compilation unit signature or a type signature S, an entry
11519 in the hash table is located as follows:
80626a55 11520
d2415c6c
DE
11521 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11522 the low-order k bits all set to 1.
80626a55 11523
d2415c6c 11524 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11525
d2415c6c
DE
11526 3) If the hash table entry at index H matches the signature, use that
11527 entry. If the hash table entry at index H is unused (all zeroes),
11528 terminate the search: the signature is not present in the table.
80626a55 11529
d2415c6c 11530 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11531
d2415c6c 11532 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11533 to stop at an unused slot or find the match. */
80626a55
DE
11534
11535/* Create a hash table to map DWO IDs to their CU/TU entry in
11536 .debug_{info,types}.dwo in DWP_FILE.
11537 Returns NULL if there isn't one.
11538 Note: This function processes DWP files only, not DWO files. */
11539
11540static struct dwp_hash_table *
ed2dc618
SM
11541create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11542 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11543{
11544 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11545 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11546 const gdb_byte *index_ptr, *index_end;
80626a55 11547 struct dwarf2_section_info *index;
73869dc2 11548 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11549 struct dwp_hash_table *htab;
11550
11551 if (is_debug_types)
11552 index = &dwp_file->sections.tu_index;
11553 else
11554 index = &dwp_file->sections.cu_index;
11555
96b79293 11556 if (index->empty ())
80626a55 11557 return NULL;
96b79293 11558 index->read (objfile);
80626a55
DE
11559
11560 index_ptr = index->buffer;
11561 index_end = index_ptr + index->size;
11562
11563 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11564 index_ptr += 4;
11565 if (version == 2)
11566 nr_columns = read_4_bytes (dbfd, index_ptr);
11567 else
11568 nr_columns = 0;
11569 index_ptr += 4;
80626a55
DE
11570 nr_units = read_4_bytes (dbfd, index_ptr);
11571 index_ptr += 4;
11572 nr_slots = read_4_bytes (dbfd, index_ptr);
11573 index_ptr += 4;
11574
73869dc2 11575 if (version != 1 && version != 2)
80626a55 11576 {
21aa081e 11577 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11578 " [in module %s]"),
21aa081e 11579 pulongest (version), dwp_file->name);
80626a55
DE
11580 }
11581 if (nr_slots != (nr_slots & -nr_slots))
11582 {
21aa081e 11583 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11584 " is not power of 2 [in module %s]"),
21aa081e 11585 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11586 }
11587
5989a64e 11588 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11589 htab->version = version;
11590 htab->nr_columns = nr_columns;
80626a55
DE
11591 htab->nr_units = nr_units;
11592 htab->nr_slots = nr_slots;
11593 htab->hash_table = index_ptr;
11594 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11595
11596 /* Exit early if the table is empty. */
11597 if (nr_slots == 0 || nr_units == 0
11598 || (version == 2 && nr_columns == 0))
11599 {
11600 /* All must be zero. */
11601 if (nr_slots != 0 || nr_units != 0
11602 || (version == 2 && nr_columns != 0))
11603 {
b98664d3 11604 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11605 " all zero [in modules %s]"),
11606 dwp_file->name);
11607 }
11608 return htab;
11609 }
11610
11611 if (version == 1)
11612 {
11613 htab->section_pool.v1.indices =
11614 htab->unit_table + sizeof (uint32_t) * nr_slots;
11615 /* It's harder to decide whether the section is too small in v1.
11616 V1 is deprecated anyway so we punt. */
11617 }
11618 else
11619 {
11620 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11621 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11622 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11623 /* Reverse map for error checking. */
11624 int ids_seen[DW_SECT_MAX + 1];
11625 int i;
11626
11627 if (nr_columns < 2)
11628 {
11629 error (_("Dwarf Error: bad DWP hash table, too few columns"
11630 " in section table [in module %s]"),
11631 dwp_file->name);
11632 }
11633 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, too many columns"
11636 " in section table [in module %s]"),
11637 dwp_file->name);
11638 }
04fd5eed
GB
11639 memset (ids, 255, sizeof_ids);
11640 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11641 for (i = 0; i < nr_columns; ++i)
11642 {
11643 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11644
11645 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11646 {
11647 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11648 " in section table [in module %s]"),
11649 id, dwp_file->name);
11650 }
11651 if (ids_seen[id] != -1)
11652 {
11653 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11654 " id %d in section table [in module %s]"),
11655 id, dwp_file->name);
11656 }
11657 ids_seen[id] = i;
11658 ids[i] = id;
11659 }
11660 /* Must have exactly one info or types section. */
11661 if (((ids_seen[DW_SECT_INFO] != -1)
11662 + (ids_seen[DW_SECT_TYPES] != -1))
11663 != 1)
11664 {
11665 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11666 " DWO info/types section [in module %s]"),
11667 dwp_file->name);
11668 }
11669 /* Must have an abbrev section. */
11670 if (ids_seen[DW_SECT_ABBREV] == -1)
11671 {
11672 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11673 " section [in module %s]"),
11674 dwp_file->name);
11675 }
11676 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11677 htab->section_pool.v2.sizes =
11678 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11679 * nr_units * nr_columns);
11680 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11681 * nr_units * nr_columns))
11682 > index_end)
11683 {
11684 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11685 " [in module %s]"),
11686 dwp_file->name);
11687 }
11688 }
80626a55
DE
11689
11690 return htab;
11691}
11692
11693/* Update SECTIONS with the data from SECTP.
11694
11695 This function is like the other "locate" section routines that are
11696 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11697 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11698
11699 The result is non-zero for success, or zero if an error was found. */
11700
11701static int
73869dc2
DE
11702locate_v1_virtual_dwo_sections (asection *sectp,
11703 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11704{
11705 const struct dwop_section_names *names = &dwop_section_names;
11706
11707 if (section_is_p (sectp->name, &names->abbrev_dwo))
11708 {
11709 /* There can be only one. */
049412e3 11710 if (sections->abbrev.s.section != NULL)
80626a55 11711 return 0;
049412e3 11712 sections->abbrev.s.section = sectp;
fd361982 11713 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11714 }
11715 else if (section_is_p (sectp->name, &names->info_dwo)
11716 || section_is_p (sectp->name, &names->types_dwo))
11717 {
11718 /* There can be only one. */
049412e3 11719 if (sections->info_or_types.s.section != NULL)
80626a55 11720 return 0;
049412e3 11721 sections->info_or_types.s.section = sectp;
fd361982 11722 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11723 }
11724 else if (section_is_p (sectp->name, &names->line_dwo))
11725 {
11726 /* There can be only one. */
049412e3 11727 if (sections->line.s.section != NULL)
80626a55 11728 return 0;
049412e3 11729 sections->line.s.section = sectp;
fd361982 11730 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11731 }
11732 else if (section_is_p (sectp->name, &names->loc_dwo))
11733 {
11734 /* There can be only one. */
049412e3 11735 if (sections->loc.s.section != NULL)
80626a55 11736 return 0;
049412e3 11737 sections->loc.s.section = sectp;
fd361982 11738 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11739 }
11740 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11741 {
11742 /* There can be only one. */
049412e3 11743 if (sections->macinfo.s.section != NULL)
80626a55 11744 return 0;
049412e3 11745 sections->macinfo.s.section = sectp;
fd361982 11746 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11747 }
11748 else if (section_is_p (sectp->name, &names->macro_dwo))
11749 {
11750 /* There can be only one. */
049412e3 11751 if (sections->macro.s.section != NULL)
80626a55 11752 return 0;
049412e3 11753 sections->macro.s.section = sectp;
fd361982 11754 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11755 }
11756 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11757 {
11758 /* There can be only one. */
049412e3 11759 if (sections->str_offsets.s.section != NULL)
80626a55 11760 return 0;
049412e3 11761 sections->str_offsets.s.section = sectp;
fd361982 11762 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11763 }
11764 else
11765 {
11766 /* No other kind of section is valid. */
11767 return 0;
11768 }
11769
11770 return 1;
11771}
11772
73869dc2
DE
11773/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11774 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11775 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11776 This is for DWP version 1 files. */
80626a55
DE
11777
11778static struct dwo_unit *
ed2dc618
SM
11779create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11780 struct dwp_file *dwp_file,
73869dc2
DE
11781 uint32_t unit_index,
11782 const char *comp_dir,
11783 ULONGEST signature, int is_debug_types)
80626a55 11784{
73869dc2
DE
11785 const struct dwp_hash_table *dwp_htab =
11786 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11787 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11788 const char *kind = is_debug_types ? "TU" : "CU";
11789 struct dwo_file *dwo_file;
11790 struct dwo_unit *dwo_unit;
73869dc2 11791 struct virtual_v1_dwo_sections sections;
80626a55 11792 void **dwo_file_slot;
80626a55
DE
11793 int i;
11794
73869dc2
DE
11795 gdb_assert (dwp_file->version == 1);
11796
b4f54984 11797 if (dwarf_read_debug)
80626a55 11798 {
73869dc2 11799 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11800 kind,
73869dc2 11801 pulongest (unit_index), hex_string (signature),
80626a55
DE
11802 dwp_file->name);
11803 }
11804
19ac8c2e 11805 /* Fetch the sections of this DWO unit.
80626a55
DE
11806 Put a limit on the number of sections we look for so that bad data
11807 doesn't cause us to loop forever. */
11808
73869dc2 11809#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11810 (1 /* .debug_info or .debug_types */ \
11811 + 1 /* .debug_abbrev */ \
11812 + 1 /* .debug_line */ \
11813 + 1 /* .debug_loc */ \
11814 + 1 /* .debug_str_offsets */ \
19ac8c2e 11815 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11816 + 1 /* trailing zero */)
11817
11818 memset (&sections, 0, sizeof (sections));
80626a55 11819
73869dc2 11820 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11821 {
11822 asection *sectp;
11823 uint32_t section_nr =
11824 read_4_bytes (dbfd,
73869dc2
DE
11825 dwp_htab->section_pool.v1.indices
11826 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11827
11828 if (section_nr == 0)
11829 break;
11830 if (section_nr >= dwp_file->num_sections)
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, section number too large"
11833 " [in module %s]"),
11834 dwp_file->name);
11835 }
11836
11837 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11838 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11839 {
11840 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11841 " [in module %s]"),
11842 dwp_file->name);
11843 }
11844 }
11845
11846 if (i < 2
96b79293
TT
11847 || sections.info_or_types.empty ()
11848 || sections.abbrev.empty ())
80626a55
DE
11849 {
11850 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11851 " [in module %s]"),
11852 dwp_file->name);
11853 }
73869dc2 11854 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11855 {
11856 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11857 " [in module %s]"),
11858 dwp_file->name);
11859 }
11860
11861 /* It's easier for the rest of the code if we fake a struct dwo_file and
11862 have dwo_unit "live" in that. At least for now.
11863
11864 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11865 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11866 file, we can combine them back into a virtual DWO file to save space
11867 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11868 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11869
791afaa2
TT
11870 std::string virtual_dwo_name =
11871 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11872 sections.abbrev.get_id (),
11873 sections.line.get_id (),
11874 sections.loc.get_id (),
11875 sections.str_offsets.get_id ());
80626a55 11876 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11877 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11878 virtual_dwo_name.c_str (),
11879 comp_dir);
80626a55
DE
11880 /* Create one if necessary. */
11881 if (*dwo_file_slot == NULL)
11882 {
b4f54984 11883 if (dwarf_read_debug)
80626a55
DE
11884 {
11885 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11886 virtual_dwo_name.c_str ());
80626a55 11887 }
51ac9db5 11888 dwo_file = new struct dwo_file;
45940949 11889 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11890 dwo_file->comp_dir = comp_dir;
80626a55
DE
11891 dwo_file->sections.abbrev = sections.abbrev;
11892 dwo_file->sections.line = sections.line;
11893 dwo_file->sections.loc = sections.loc;
11894 dwo_file->sections.macinfo = sections.macinfo;
11895 dwo_file->sections.macro = sections.macro;
11896 dwo_file->sections.str_offsets = sections.str_offsets;
11897 /* The "str" section is global to the entire DWP file. */
11898 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11899 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11900 there's no need to record it in dwo_file.
11901 Also, we can't simply record type sections in dwo_file because
11902 we record a pointer into the vector in dwo_unit. As we collect more
11903 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11904 for it, invalidating all copies of pointers into the previous
11905 contents. */
80626a55
DE
11906 *dwo_file_slot = dwo_file;
11907 }
11908 else
11909 {
b4f54984 11910 if (dwarf_read_debug)
80626a55
DE
11911 {
11912 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11913 virtual_dwo_name.c_str ());
80626a55 11914 }
9a3c8263 11915 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11916 }
80626a55 11917
5989a64e 11918 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11919 dwo_unit->dwo_file = dwo_file;
11920 dwo_unit->signature = signature;
8d749320 11921 dwo_unit->section =
5989a64e 11922 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11923 *dwo_unit->section = sections.info_or_types;
57d63ce2 11924 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11925
11926 return dwo_unit;
11927}
11928
73869dc2
DE
11929/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11930 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11931 piece within that section used by a TU/CU, return a virtual section
11932 of just that piece. */
11933
11934static struct dwarf2_section_info
ed2dc618
SM
11935create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11936 struct dwarf2_section_info *section,
73869dc2
DE
11937 bfd_size_type offset, bfd_size_type size)
11938{
11939 struct dwarf2_section_info result;
11940 asection *sectp;
11941
11942 gdb_assert (section != NULL);
11943 gdb_assert (!section->is_virtual);
11944
11945 memset (&result, 0, sizeof (result));
11946 result.s.containing_section = section;
dc4ccb6f 11947 result.is_virtual = true;
73869dc2
DE
11948
11949 if (size == 0)
11950 return result;
11951
96b79293 11952 sectp = section->get_bfd_section ();
73869dc2
DE
11953
11954 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11955 bounds of the real section. This is a pretty-rare event, so just
11956 flag an error (easier) instead of a warning and trying to cope. */
11957 if (sectp == NULL
fd361982 11958 || offset + size > bfd_section_size (sectp))
73869dc2 11959 {
73869dc2
DE
11960 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11961 " in section %s [in module %s]"),
fd361982 11962 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11963 objfile_name (dwarf2_per_objfile->objfile));
11964 }
11965
11966 result.virtual_offset = offset;
11967 result.size = size;
11968 return result;
11969}
11970
11971/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11972 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11973 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11974 This is for DWP version 2 files. */
11975
11976static struct dwo_unit *
ed2dc618
SM
11977create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11978 struct dwp_file *dwp_file,
73869dc2
DE
11979 uint32_t unit_index,
11980 const char *comp_dir,
11981 ULONGEST signature, int is_debug_types)
11982{
73869dc2
DE
11983 const struct dwp_hash_table *dwp_htab =
11984 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11985 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11986 const char *kind = is_debug_types ? "TU" : "CU";
11987 struct dwo_file *dwo_file;
11988 struct dwo_unit *dwo_unit;
11989 struct virtual_v2_dwo_sections sections;
11990 void **dwo_file_slot;
73869dc2
DE
11991 int i;
11992
11993 gdb_assert (dwp_file->version == 2);
11994
b4f54984 11995 if (dwarf_read_debug)
73869dc2
DE
11996 {
11997 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11998 kind,
11999 pulongest (unit_index), hex_string (signature),
12000 dwp_file->name);
12001 }
12002
12003 /* Fetch the section offsets of this DWO unit. */
12004
12005 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12006
12007 for (i = 0; i < dwp_htab->nr_columns; ++i)
12008 {
12009 uint32_t offset = read_4_bytes (dbfd,
12010 dwp_htab->section_pool.v2.offsets
12011 + (((unit_index - 1) * dwp_htab->nr_columns
12012 + i)
12013 * sizeof (uint32_t)));
12014 uint32_t size = read_4_bytes (dbfd,
12015 dwp_htab->section_pool.v2.sizes
12016 + (((unit_index - 1) * dwp_htab->nr_columns
12017 + i)
12018 * sizeof (uint32_t)));
12019
12020 switch (dwp_htab->section_pool.v2.section_ids[i])
12021 {
12022 case DW_SECT_INFO:
12023 case DW_SECT_TYPES:
12024 sections.info_or_types_offset = offset;
12025 sections.info_or_types_size = size;
12026 break;
12027 case DW_SECT_ABBREV:
12028 sections.abbrev_offset = offset;
12029 sections.abbrev_size = size;
12030 break;
12031 case DW_SECT_LINE:
12032 sections.line_offset = offset;
12033 sections.line_size = size;
12034 break;
12035 case DW_SECT_LOC:
12036 sections.loc_offset = offset;
12037 sections.loc_size = size;
12038 break;
12039 case DW_SECT_STR_OFFSETS:
12040 sections.str_offsets_offset = offset;
12041 sections.str_offsets_size = size;
12042 break;
12043 case DW_SECT_MACINFO:
12044 sections.macinfo_offset = offset;
12045 sections.macinfo_size = size;
12046 break;
12047 case DW_SECT_MACRO:
12048 sections.macro_offset = offset;
12049 sections.macro_size = size;
12050 break;
12051 }
12052 }
12053
12054 /* It's easier for the rest of the code if we fake a struct dwo_file and
12055 have dwo_unit "live" in that. At least for now.
12056
12057 The DWP file can be made up of a random collection of CUs and TUs.
12058 However, for each CU + set of TUs that came from the same original DWO
12059 file, we can combine them back into a virtual DWO file to save space
12060 (fewer struct dwo_file objects to allocate). Remember that for really
12061 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12062
791afaa2
TT
12063 std::string virtual_dwo_name =
12064 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12065 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12066 (long) (sections.line_size ? sections.line_offset : 0),
12067 (long) (sections.loc_size ? sections.loc_offset : 0),
12068 (long) (sections.str_offsets_size
12069 ? sections.str_offsets_offset : 0));
73869dc2 12070 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12071 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12072 virtual_dwo_name.c_str (),
12073 comp_dir);
73869dc2
DE
12074 /* Create one if necessary. */
12075 if (*dwo_file_slot == NULL)
12076 {
b4f54984 12077 if (dwarf_read_debug)
73869dc2
DE
12078 {
12079 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12080 virtual_dwo_name.c_str ());
73869dc2 12081 }
51ac9db5 12082 dwo_file = new struct dwo_file;
45940949 12083 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12084 dwo_file->comp_dir = comp_dir;
12085 dwo_file->sections.abbrev =
ed2dc618 12086 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12087 sections.abbrev_offset, sections.abbrev_size);
12088 dwo_file->sections.line =
ed2dc618 12089 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12090 sections.line_offset, sections.line_size);
12091 dwo_file->sections.loc =
ed2dc618 12092 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12093 sections.loc_offset, sections.loc_size);
12094 dwo_file->sections.macinfo =
ed2dc618 12095 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12096 sections.macinfo_offset, sections.macinfo_size);
12097 dwo_file->sections.macro =
ed2dc618 12098 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12099 sections.macro_offset, sections.macro_size);
12100 dwo_file->sections.str_offsets =
ed2dc618
SM
12101 create_dwp_v2_section (dwarf2_per_objfile,
12102 &dwp_file->sections.str_offsets,
73869dc2
DE
12103 sections.str_offsets_offset,
12104 sections.str_offsets_size);
12105 /* The "str" section is global to the entire DWP file. */
12106 dwo_file->sections.str = dwp_file->sections.str;
12107 /* The info or types section is assigned below to dwo_unit,
12108 there's no need to record it in dwo_file.
12109 Also, we can't simply record type sections in dwo_file because
12110 we record a pointer into the vector in dwo_unit. As we collect more
12111 types we'll grow the vector and eventually have to reallocate space
12112 for it, invalidating all copies of pointers into the previous
12113 contents. */
12114 *dwo_file_slot = dwo_file;
12115 }
12116 else
12117 {
b4f54984 12118 if (dwarf_read_debug)
73869dc2
DE
12119 {
12120 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12121 virtual_dwo_name.c_str ());
73869dc2 12122 }
9a3c8263 12123 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12124 }
73869dc2 12125
5989a64e 12126 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12127 dwo_unit->dwo_file = dwo_file;
12128 dwo_unit->signature = signature;
8d749320 12129 dwo_unit->section =
5989a64e 12130 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12131 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12132 is_debug_types
73869dc2
DE
12133 ? &dwp_file->sections.types
12134 : &dwp_file->sections.info,
12135 sections.info_or_types_offset,
12136 sections.info_or_types_size);
12137 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12138
12139 return dwo_unit;
12140}
12141
57d63ce2
DE
12142/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12143 Returns NULL if the signature isn't found. */
80626a55
DE
12144
12145static struct dwo_unit *
ed2dc618
SM
12146lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12147 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12148 ULONGEST signature, int is_debug_types)
80626a55 12149{
57d63ce2
DE
12150 const struct dwp_hash_table *dwp_htab =
12151 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12152 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12153 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12154 uint32_t hash = signature & mask;
12155 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12156 unsigned int i;
12157 void **slot;
870f88f7 12158 struct dwo_unit find_dwo_cu;
80626a55
DE
12159
12160 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12161 find_dwo_cu.signature = signature;
19ac8c2e 12162 slot = htab_find_slot (is_debug_types
48b490f2
TT
12163 ? dwp_file->loaded_tus.get ()
12164 : dwp_file->loaded_cus.get (),
19ac8c2e 12165 &find_dwo_cu, INSERT);
80626a55
DE
12166
12167 if (*slot != NULL)
9a3c8263 12168 return (struct dwo_unit *) *slot;
80626a55
DE
12169
12170 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12171 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12172 {
12173 ULONGEST signature_in_table;
12174
12175 signature_in_table =
57d63ce2 12176 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12177 if (signature_in_table == signature)
12178 {
57d63ce2
DE
12179 uint32_t unit_index =
12180 read_4_bytes (dbfd,
12181 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12182
73869dc2
DE
12183 if (dwp_file->version == 1)
12184 {
ed2dc618
SM
12185 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12186 dwp_file, unit_index,
73869dc2
DE
12187 comp_dir, signature,
12188 is_debug_types);
12189 }
12190 else
12191 {
ed2dc618
SM
12192 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12193 dwp_file, unit_index,
73869dc2
DE
12194 comp_dir, signature,
12195 is_debug_types);
12196 }
9a3c8263 12197 return (struct dwo_unit *) *slot;
80626a55
DE
12198 }
12199 if (signature_in_table == 0)
12200 return NULL;
12201 hash = (hash + hash2) & mask;
12202 }
12203
12204 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12205 " [in module %s]"),
12206 dwp_file->name);
12207}
12208
ab5088bf 12209/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12210 Open the file specified by FILE_NAME and hand it off to BFD for
12211 preliminary analysis. Return a newly initialized bfd *, which
12212 includes a canonicalized copy of FILE_NAME.
80626a55 12213 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12214 SEARCH_CWD is true if the current directory is to be searched.
12215 It will be searched before debug-file-directory.
13aaf454
DE
12216 If successful, the file is added to the bfd include table of the
12217 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12218 If unable to find/open the file, return NULL.
3019eac3
DE
12219 NOTE: This function is derived from symfile_bfd_open. */
12220
192b62ce 12221static gdb_bfd_ref_ptr
ed2dc618
SM
12222try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12223 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12224{
24b9144d 12225 int desc;
9c02c129
DE
12226 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12227 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12228 to debug_file_directory. */
e0cc99a6 12229 const char *search_path;
9c02c129
DE
12230 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12231
e0cc99a6 12232 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12233 if (search_cwd)
12234 {
12235 if (*debug_file_directory != '\0')
e0cc99a6
TT
12236 {
12237 search_path_holder.reset (concat (".", dirname_separator_string,
12238 debug_file_directory,
12239 (char *) NULL));
12240 search_path = search_path_holder.get ();
12241 }
6ac97d4c 12242 else
e0cc99a6 12243 search_path = ".";
6ac97d4c 12244 }
9c02c129 12245 else
e0cc99a6 12246 search_path = debug_file_directory;
3019eac3 12247
24b9144d 12248 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12249 if (is_dwp)
12250 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12251
12252 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12253 desc = openp (search_path, flags, file_name,
3019eac3
DE
12254 O_RDONLY | O_BINARY, &absolute_name);
12255 if (desc < 0)
12256 return NULL;
12257
e0cc99a6
TT
12258 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12259 gnutarget, desc));
9c02c129
DE
12260 if (sym_bfd == NULL)
12261 return NULL;
192b62ce 12262 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12263
192b62ce
TT
12264 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12265 return NULL;
3019eac3 12266
13aaf454
DE
12267 /* Success. Record the bfd as having been included by the objfile's bfd.
12268 This is important because things like demangled_names_hash lives in the
12269 objfile's per_bfd space and may have references to things like symbol
12270 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12271 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12272
3019eac3
DE
12273 return sym_bfd;
12274}
12275
ab5088bf 12276/* Try to open DWO file FILE_NAME.
3019eac3
DE
12277 COMP_DIR is the DW_AT_comp_dir attribute.
12278 The result is the bfd handle of the file.
12279 If there is a problem finding or opening the file, return NULL.
12280 Upon success, the canonicalized path of the file is stored in the bfd,
12281 same as symfile_bfd_open. */
12282
192b62ce 12283static gdb_bfd_ref_ptr
ed2dc618
SM
12284open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12285 const char *file_name, const char *comp_dir)
3019eac3 12286{
80626a55 12287 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12288 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12289 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12290
12291 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12292
12293 if (comp_dir != NULL)
12294 {
43816ebc
TT
12295 gdb::unique_xmalloc_ptr<char> path_to_try
12296 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12297
12298 /* NOTE: If comp_dir is a relative path, this will also try the
12299 search path, which seems useful. */
ed2dc618 12300 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12301 path_to_try.get (),
ed2dc618 12302 0 /*is_dwp*/,
192b62ce 12303 1 /*search_cwd*/));
3019eac3
DE
12304 if (abfd != NULL)
12305 return abfd;
12306 }
12307
12308 /* That didn't work, try debug-file-directory, which, despite its name,
12309 is a list of paths. */
12310
12311 if (*debug_file_directory == '\0')
12312 return NULL;
12313
ed2dc618
SM
12314 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12315 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12316}
12317
80626a55
DE
12318/* This function is mapped across the sections and remembers the offset and
12319 size of each of the DWO debugging sections we are interested in. */
12320
12321static void
12322dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12323{
9a3c8263 12324 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12325 const struct dwop_section_names *names = &dwop_section_names;
12326
12327 if (section_is_p (sectp->name, &names->abbrev_dwo))
12328 {
049412e3 12329 dwo_sections->abbrev.s.section = sectp;
fd361982 12330 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12331 }
12332 else if (section_is_p (sectp->name, &names->info_dwo))
12333 {
049412e3 12334 dwo_sections->info.s.section = sectp;
fd361982 12335 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12336 }
12337 else if (section_is_p (sectp->name, &names->line_dwo))
12338 {
049412e3 12339 dwo_sections->line.s.section = sectp;
fd361982 12340 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12341 }
12342 else if (section_is_p (sectp->name, &names->loc_dwo))
12343 {
049412e3 12344 dwo_sections->loc.s.section = sectp;
fd361982 12345 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12346 }
41144253 12347 else if (section_is_p (sectp->name, &names->loclists_dwo))
12348 {
12349 dwo_sections->loclists.s.section = sectp;
12350 dwo_sections->loclists.size = bfd_section_size (sectp);
12351 }
80626a55
DE
12352 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12353 {
049412e3 12354 dwo_sections->macinfo.s.section = sectp;
fd361982 12355 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12356 }
12357 else if (section_is_p (sectp->name, &names->macro_dwo))
12358 {
049412e3 12359 dwo_sections->macro.s.section = sectp;
fd361982 12360 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12361 }
12362 else if (section_is_p (sectp->name, &names->str_dwo))
12363 {
049412e3 12364 dwo_sections->str.s.section = sectp;
fd361982 12365 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12366 }
12367 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12368 {
049412e3 12369 dwo_sections->str_offsets.s.section = sectp;
fd361982 12370 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12371 }
12372 else if (section_is_p (sectp->name, &names->types_dwo))
12373 {
12374 struct dwarf2_section_info type_section;
12375
12376 memset (&type_section, 0, sizeof (type_section));
049412e3 12377 type_section.s.section = sectp;
fd361982 12378 type_section.size = bfd_section_size (sectp);
fd5866f6 12379 dwo_sections->types.push_back (type_section);
80626a55
DE
12380 }
12381}
12382
ab5088bf 12383/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12384 by PER_CU. This is for the non-DWP case.
80626a55 12385 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12386
12387static struct dwo_file *
4ab09049
SM
12388open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12389 const char *comp_dir)
3019eac3 12390{
4ab09049 12391 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12392
fb1eb2f9 12393 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12394 if (dbfd == NULL)
12395 {
b4f54984 12396 if (dwarf_read_debug)
80626a55
DE
12397 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12398 return NULL;
12399 }
263db9a1 12400
51ac9db5 12401 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12402 dwo_file->dwo_name = dwo_name;
12403 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12404 dwo_file->dbfd = std::move (dbfd);
3019eac3 12405
fb1eb2f9 12406 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12407 &dwo_file->sections);
3019eac3 12408
4ab09049 12409 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12410 dwo_file->sections.info, dwo_file->cus);
3019eac3 12411
263db9a1 12412 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12413 dwo_file->sections.types, dwo_file->tus);
3019eac3 12414
b4f54984 12415 if (dwarf_read_debug)
80626a55
DE
12416 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12417
263db9a1 12418 return dwo_file.release ();
3019eac3
DE
12419}
12420
80626a55 12421/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12422 size of each of the DWP debugging sections common to version 1 and 2 that
12423 we are interested in. */
3019eac3 12424
80626a55 12425static void
73869dc2
DE
12426dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12427 void *dwp_file_ptr)
3019eac3 12428{
9a3c8263 12429 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12430 const struct dwop_section_names *names = &dwop_section_names;
12431 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12432
80626a55 12433 /* Record the ELF section number for later lookup: this is what the
73869dc2 12434 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12435 gdb_assert (elf_section_nr < dwp_file->num_sections);
12436 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12437
80626a55
DE
12438 /* Look for specific sections that we need. */
12439 if (section_is_p (sectp->name, &names->str_dwo))
12440 {
049412e3 12441 dwp_file->sections.str.s.section = sectp;
fd361982 12442 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12443 }
12444 else if (section_is_p (sectp->name, &names->cu_index))
12445 {
049412e3 12446 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12447 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12448 }
12449 else if (section_is_p (sectp->name, &names->tu_index))
12450 {
049412e3 12451 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12452 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12453 }
12454}
3019eac3 12455
73869dc2
DE
12456/* This function is mapped across the sections and remembers the offset and
12457 size of each of the DWP version 2 debugging sections that we are interested
12458 in. This is split into a separate function because we don't know if we
12459 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12460
12461static void
12462dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12463{
9a3c8263 12464 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12465 const struct dwop_section_names *names = &dwop_section_names;
12466 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12467
12468 /* Record the ELF section number for later lookup: this is what the
12469 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12470 gdb_assert (elf_section_nr < dwp_file->num_sections);
12471 dwp_file->elf_sections[elf_section_nr] = sectp;
12472
12473 /* Look for specific sections that we need. */
12474 if (section_is_p (sectp->name, &names->abbrev_dwo))
12475 {
049412e3 12476 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12477 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12478 }
12479 else if (section_is_p (sectp->name, &names->info_dwo))
12480 {
049412e3 12481 dwp_file->sections.info.s.section = sectp;
fd361982 12482 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12483 }
12484 else if (section_is_p (sectp->name, &names->line_dwo))
12485 {
049412e3 12486 dwp_file->sections.line.s.section = sectp;
fd361982 12487 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12488 }
12489 else if (section_is_p (sectp->name, &names->loc_dwo))
12490 {
049412e3 12491 dwp_file->sections.loc.s.section = sectp;
fd361982 12492 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12493 }
12494 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12495 {
049412e3 12496 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12497 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12498 }
12499 else if (section_is_p (sectp->name, &names->macro_dwo))
12500 {
049412e3 12501 dwp_file->sections.macro.s.section = sectp;
fd361982 12502 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12503 }
12504 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12505 {
049412e3 12506 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12507 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12508 }
12509 else if (section_is_p (sectp->name, &names->types_dwo))
12510 {
049412e3 12511 dwp_file->sections.types.s.section = sectp;
fd361982 12512 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12513 }
12514}
12515
80626a55 12516/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12517
80626a55
DE
12518static hashval_t
12519hash_dwp_loaded_cutus (const void *item)
12520{
9a3c8263 12521 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12522
80626a55
DE
12523 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12524 return dwo_unit->signature;
3019eac3
DE
12525}
12526
80626a55 12527/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12528
80626a55
DE
12529static int
12530eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12531{
9a3c8263
SM
12532 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12533 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12534
80626a55
DE
12535 return dua->signature == dub->signature;
12536}
3019eac3 12537
80626a55 12538/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12539
48b490f2 12540static htab_up
298e9637 12541allocate_dwp_loaded_cutus_table ()
80626a55 12542{
48b490f2
TT
12543 return htab_up (htab_create_alloc (3,
12544 hash_dwp_loaded_cutus,
12545 eq_dwp_loaded_cutus,
12546 NULL, xcalloc, xfree));
80626a55 12547}
3019eac3 12548
ab5088bf
DE
12549/* Try to open DWP file FILE_NAME.
12550 The result is the bfd handle of the file.
12551 If there is a problem finding or opening the file, return NULL.
12552 Upon success, the canonicalized path of the file is stored in the bfd,
12553 same as symfile_bfd_open. */
12554
192b62ce 12555static gdb_bfd_ref_ptr
ed2dc618
SM
12556open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12557 const char *file_name)
ab5088bf 12558{
ed2dc618
SM
12559 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12560 1 /*is_dwp*/,
192b62ce 12561 1 /*search_cwd*/));
6ac97d4c
DE
12562 if (abfd != NULL)
12563 return abfd;
12564
12565 /* Work around upstream bug 15652.
12566 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12567 [Whether that's a "bug" is debatable, but it is getting in our way.]
12568 We have no real idea where the dwp file is, because gdb's realpath-ing
12569 of the executable's path may have discarded the needed info.
12570 [IWBN if the dwp file name was recorded in the executable, akin to
12571 .gnu_debuglink, but that doesn't exist yet.]
12572 Strip the directory from FILE_NAME and search again. */
12573 if (*debug_file_directory != '\0')
12574 {
12575 /* Don't implicitly search the current directory here.
12576 If the user wants to search "." to handle this case,
12577 it must be added to debug-file-directory. */
ed2dc618
SM
12578 return try_open_dwop_file (dwarf2_per_objfile,
12579 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12580 0 /*search_cwd*/);
12581 }
12582
12583 return NULL;
ab5088bf
DE
12584}
12585
80626a55
DE
12586/* Initialize the use of the DWP file for the current objfile.
12587 By convention the name of the DWP file is ${objfile}.dwp.
12588 The result is NULL if it can't be found. */
a766d390 12589
400174b1 12590static std::unique_ptr<struct dwp_file>
ed2dc618 12591open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12592{
12593 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12594
82bf32bc
JK
12595 /* Try to find first .dwp for the binary file before any symbolic links
12596 resolving. */
6c447423
DE
12597
12598 /* If the objfile is a debug file, find the name of the real binary
12599 file and get the name of dwp file from there. */
d721ba37 12600 std::string dwp_name;
6c447423
DE
12601 if (objfile->separate_debug_objfile_backlink != NULL)
12602 {
12603 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12604 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12605
d721ba37 12606 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12607 }
12608 else
d721ba37
PA
12609 dwp_name = objfile->original_name;
12610
12611 dwp_name += ".dwp";
80626a55 12612
ed2dc618 12613 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12614 if (dbfd == NULL
12615 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12616 {
12617 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12618 dwp_name = objfile_name (objfile);
12619 dwp_name += ".dwp";
ed2dc618 12620 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12621 }
12622
80626a55
DE
12623 if (dbfd == NULL)
12624 {
b4f54984 12625 if (dwarf_read_debug)
d721ba37 12626 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12627 return std::unique_ptr<dwp_file> ();
3019eac3 12628 }
400174b1
TT
12629
12630 const char *name = bfd_get_filename (dbfd.get ());
12631 std::unique_ptr<struct dwp_file> dwp_file
12632 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12633
0a0f4c01 12634 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12635 dwp_file->elf_sections =
5989a64e 12636 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12637 dwp_file->num_sections, asection *);
12638
400174b1
TT
12639 bfd_map_over_sections (dwp_file->dbfd.get (),
12640 dwarf2_locate_common_dwp_sections,
12641 dwp_file.get ());
80626a55 12642
400174b1
TT
12643 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12644 0);
80626a55 12645
400174b1
TT
12646 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12647 1);
80626a55 12648
73869dc2 12649 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12650 if (dwp_file->cus && dwp_file->tus
12651 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12652 {
12653 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12654 pretty bizarre. We use pulongest here because that's the established
4d65956b 12655 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12656 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12657 " TU version %s [in DWP file %s]"),
12658 pulongest (dwp_file->cus->version),
d721ba37 12659 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12660 }
08302ed2
DE
12661
12662 if (dwp_file->cus)
12663 dwp_file->version = dwp_file->cus->version;
12664 else if (dwp_file->tus)
12665 dwp_file->version = dwp_file->tus->version;
12666 else
12667 dwp_file->version = 2;
73869dc2
DE
12668
12669 if (dwp_file->version == 2)
400174b1
TT
12670 bfd_map_over_sections (dwp_file->dbfd.get (),
12671 dwarf2_locate_v2_dwp_sections,
12672 dwp_file.get ());
73869dc2 12673
298e9637
SM
12674 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12675 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12676
b4f54984 12677 if (dwarf_read_debug)
80626a55
DE
12678 {
12679 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12680 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12681 " %s CUs, %s TUs\n",
12682 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12683 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12684 }
12685
12686 return dwp_file;
3019eac3 12687}
c906108c 12688
ab5088bf
DE
12689/* Wrapper around open_and_init_dwp_file, only open it once. */
12690
12691static struct dwp_file *
ed2dc618 12692get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12693{
5989a64e 12694 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12695 {
5989a64e 12696 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12697 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12698 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12699 }
5989a64e 12700 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12701}
12702
80626a55
DE
12703/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12704 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12705 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12706 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12707 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12708
12709 This is called, for example, when wanting to read a variable with a
12710 complex location. Therefore we don't want to do file i/o for every call.
12711 Therefore we don't want to look for a DWO file on every call.
12712 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12713 then we check if we've already seen DWO_NAME, and only THEN do we check
12714 for a DWO file.
12715
1c658ad5 12716 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12717 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12718
3019eac3 12719static struct dwo_unit *
4ab09049 12720lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12721 ULONGEST signature, int is_debug_types)
3019eac3 12722{
4ab09049 12723 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12724 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12725 const char *kind = is_debug_types ? "TU" : "CU";
12726 void **dwo_file_slot;
3019eac3 12727 struct dwo_file *dwo_file;
80626a55 12728 struct dwp_file *dwp_file;
cb1df416 12729
6a506a2d
DE
12730 /* First see if there's a DWP file.
12731 If we have a DWP file but didn't find the DWO inside it, don't
12732 look for the original DWO file. It makes gdb behave differently
12733 depending on whether one is debugging in the build tree. */
cf2c3c16 12734
ed2dc618 12735 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12736 if (dwp_file != NULL)
cf2c3c16 12737 {
80626a55
DE
12738 const struct dwp_hash_table *dwp_htab =
12739 is_debug_types ? dwp_file->tus : dwp_file->cus;
12740
12741 if (dwp_htab != NULL)
12742 {
12743 struct dwo_unit *dwo_cutu =
ed2dc618 12744 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12745 signature, is_debug_types);
80626a55
DE
12746
12747 if (dwo_cutu != NULL)
12748 {
b4f54984 12749 if (dwarf_read_debug)
80626a55
DE
12750 {
12751 fprintf_unfiltered (gdb_stdlog,
12752 "Virtual DWO %s %s found: @%s\n",
12753 kind, hex_string (signature),
12754 host_address_to_string (dwo_cutu));
12755 }
12756 return dwo_cutu;
12757 }
12758 }
12759 }
6a506a2d 12760 else
80626a55 12761 {
6a506a2d 12762 /* No DWP file, look for the DWO file. */
80626a55 12763
ed2dc618
SM
12764 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12765 dwo_name, comp_dir);
6a506a2d 12766 if (*dwo_file_slot == NULL)
80626a55 12767 {
6a506a2d 12768 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12769 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12770 }
6a506a2d 12771 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12772 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12773
6a506a2d 12774 if (dwo_file != NULL)
19c3d4c9 12775 {
6a506a2d
DE
12776 struct dwo_unit *dwo_cutu = NULL;
12777
12778 if (is_debug_types && dwo_file->tus)
12779 {
12780 struct dwo_unit find_dwo_cutu;
12781
12782 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12783 find_dwo_cutu.signature = signature;
9a3c8263 12784 dwo_cutu
b0b6a987
TT
12785 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12786 &find_dwo_cutu);
6a506a2d 12787 }
33c5cd75 12788 else if (!is_debug_types && dwo_file->cus)
80626a55 12789 {
33c5cd75
DB
12790 struct dwo_unit find_dwo_cutu;
12791
12792 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12793 find_dwo_cutu.signature = signature;
b0b6a987 12794 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12795 &find_dwo_cutu);
6a506a2d
DE
12796 }
12797
12798 if (dwo_cutu != NULL)
12799 {
b4f54984 12800 if (dwarf_read_debug)
6a506a2d
DE
12801 {
12802 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12803 kind, dwo_name, hex_string (signature),
12804 host_address_to_string (dwo_cutu));
12805 }
12806 return dwo_cutu;
80626a55
DE
12807 }
12808 }
2e276125 12809 }
9cdd5dbd 12810
80626a55
DE
12811 /* We didn't find it. This could mean a dwo_id mismatch, or
12812 someone deleted the DWO/DWP file, or the search path isn't set up
12813 correctly to find the file. */
12814
b4f54984 12815 if (dwarf_read_debug)
80626a55
DE
12816 {
12817 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12818 kind, dwo_name, hex_string (signature));
12819 }
3019eac3 12820
6656a72d
DE
12821 /* This is a warning and not a complaint because it can be caused by
12822 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12823 {
12824 /* Print the name of the DWP file if we looked there, helps the user
12825 better diagnose the problem. */
791afaa2 12826 std::string dwp_text;
43942612
DE
12827
12828 if (dwp_file != NULL)
791afaa2
TT
12829 dwp_text = string_printf (" [in DWP file %s]",
12830 lbasename (dwp_file->name));
43942612 12831
9d8780f0 12832 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12833 " [in module %s]"),
4ab09049
SM
12834 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12835 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12836 }
3019eac3 12837 return NULL;
5fb290d7
DJ
12838}
12839
80626a55
DE
12840/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12841 See lookup_dwo_cutu_unit for details. */
12842
12843static struct dwo_unit *
4ab09049 12844lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12845 ULONGEST signature)
12846{
4ab09049
SM
12847 gdb_assert (!cu->per_cu->is_debug_types);
12848
12849 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12850}
12851
12852/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12853 See lookup_dwo_cutu_unit for details. */
12854
12855static struct dwo_unit *
4ab09049 12856lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12857{
4ab09049
SM
12858 gdb_assert (cu->per_cu->is_debug_types);
12859
12860 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12861
12862 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12863}
12864
89e63ee4
DE
12865/* Traversal function for queue_and_load_all_dwo_tus. */
12866
12867static int
12868queue_and_load_dwo_tu (void **slot, void *info)
12869{
12870 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12871 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12872 ULONGEST signature = dwo_unit->signature;
d460f660 12873 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12874
12875 if (sig_type != NULL)
12876 {
12877 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12878
12879 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12880 a real dependency of PER_CU on SIG_TYPE. That is detected later
12881 while processing PER_CU. */
120ce1b5 12882 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12883 load_full_type_unit (sig_cu, cu->per_objfile);
12884 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12885 }
12886
12887 return 1;
12888}
12889
12890/* Queue all TUs contained in the DWO of PER_CU to be read in.
12891 The DWO may have the only definition of the type, though it may not be
12892 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12893 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12894
12895static void
12896queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12897{
12898 struct dwo_unit *dwo_unit;
12899 struct dwo_file *dwo_file;
12900
12901 gdb_assert (!per_cu->is_debug_types);
89e63ee4 12902 gdb_assert (per_cu->cu != NULL);
64874a40 12903 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
89e63ee4
DE
12904
12905 dwo_unit = per_cu->cu->dwo_unit;
12906 gdb_assert (dwo_unit != NULL);
12907
12908 dwo_file = dwo_unit->dwo_file;
12909 if (dwo_file->tus != NULL)
b0b6a987 12910 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12911 per_cu->cu);
89e63ee4
DE
12912}
12913
3019eac3 12914/* Read in various DIEs. */
348e048f 12915
d389af10 12916/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12917 Inherit only the children of the DW_AT_abstract_origin DIE not being
12918 already referenced by DW_AT_abstract_origin from the children of the
12919 current DIE. */
d389af10
JK
12920
12921static void
12922inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12923{
12924 struct die_info *child_die;
791afaa2 12925 sect_offset *offsetp;
d389af10
JK
12926 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12927 struct die_info *origin_die;
12928 /* Iterator of the ORIGIN_DIE children. */
12929 struct die_info *origin_child_die;
d389af10 12930 struct attribute *attr;
cd02d79d
PA
12931 struct dwarf2_cu *origin_cu;
12932 struct pending **origin_previous_list_in_scope;
d389af10
JK
12933
12934 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12935 if (!attr)
12936 return;
12937
cd02d79d
PA
12938 /* Note that following die references may follow to a die in a
12939 different cu. */
12940
12941 origin_cu = cu;
12942 origin_die = follow_die_ref (die, attr, &origin_cu);
12943
12944 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12945 symbols in. */
12946 origin_previous_list_in_scope = origin_cu->list_in_scope;
12947 origin_cu->list_in_scope = cu->list_in_scope;
12948
edb3359d
DJ
12949 if (die->tag != origin_die->tag
12950 && !(die->tag == DW_TAG_inlined_subroutine
12951 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12952 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12953 sect_offset_str (die->sect_off),
12954 sect_offset_str (origin_die->sect_off));
d389af10 12955
791afaa2 12956 std::vector<sect_offset> offsets;
d389af10 12957
3ea89b92
PMR
12958 for (child_die = die->child;
12959 child_die && child_die->tag;
436c571c 12960 child_die = child_die->sibling)
3ea89b92
PMR
12961 {
12962 struct die_info *child_origin_die;
12963 struct dwarf2_cu *child_origin_cu;
12964
12965 /* We are trying to process concrete instance entries:
216f72a1 12966 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12967 it's not relevant to our analysis here. i.e. detecting DIEs that are
12968 present in the abstract instance but not referenced in the concrete
12969 one. */
216f72a1
JK
12970 if (child_die->tag == DW_TAG_call_site
12971 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12972 continue;
12973
c38f313d
DJ
12974 /* For each CHILD_DIE, find the corresponding child of
12975 ORIGIN_DIE. If there is more than one layer of
12976 DW_AT_abstract_origin, follow them all; there shouldn't be,
12977 but GCC versions at least through 4.4 generate this (GCC PR
12978 40573). */
3ea89b92
PMR
12979 child_origin_die = child_die;
12980 child_origin_cu = cu;
c38f313d
DJ
12981 while (1)
12982 {
cd02d79d
PA
12983 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12984 child_origin_cu);
c38f313d
DJ
12985 if (attr == NULL)
12986 break;
cd02d79d
PA
12987 child_origin_die = follow_die_ref (child_origin_die, attr,
12988 &child_origin_cu);
c38f313d
DJ
12989 }
12990
d389af10
JK
12991 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12992 counterpart may exist. */
c38f313d 12993 if (child_origin_die != child_die)
d389af10 12994 {
edb3359d
DJ
12995 if (child_die->tag != child_origin_die->tag
12996 && !(child_die->tag == DW_TAG_inlined_subroutine
12997 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12998 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12999 "different tags"),
9d8780f0
SM
13000 sect_offset_str (child_die->sect_off),
13001 sect_offset_str (child_origin_die->sect_off));
c38f313d 13002 if (child_origin_die->parent != origin_die)
b98664d3 13003 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13004 "different parents"),
9d8780f0
SM
13005 sect_offset_str (child_die->sect_off),
13006 sect_offset_str (child_origin_die->sect_off));
c38f313d 13007 else
791afaa2 13008 offsets.push_back (child_origin_die->sect_off);
d389af10 13009 }
d389af10 13010 }
791afaa2
TT
13011 std::sort (offsets.begin (), offsets.end ());
13012 sect_offset *offsets_end = offsets.data () + offsets.size ();
13013 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13014 if (offsetp[-1] == *offsetp)
b98664d3 13015 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13016 "to DIE %s as their abstract origin"),
13017 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13018
791afaa2 13019 offsetp = offsets.data ();
d389af10
JK
13020 origin_child_die = origin_die->child;
13021 while (origin_child_die && origin_child_die->tag)
13022 {
13023 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13024 while (offsetp < offsets_end
9c541725 13025 && *offsetp < origin_child_die->sect_off)
d389af10 13026 offsetp++;
b64f50a1 13027 if (offsetp >= offsets_end
9c541725 13028 || *offsetp > origin_child_die->sect_off)
d389af10 13029 {
adde2bff
DE
13030 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13031 Check whether we're already processing ORIGIN_CHILD_DIE.
13032 This can happen with mutually referenced abstract_origins.
13033 PR 16581. */
13034 if (!origin_child_die->in_process)
13035 process_die (origin_child_die, origin_cu);
d389af10 13036 }
436c571c 13037 origin_child_die = origin_child_die->sibling;
d389af10 13038 }
cd02d79d 13039 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13040
13041 if (cu != origin_cu)
13042 compute_delayed_physnames (origin_cu);
d389af10
JK
13043}
13044
c906108c 13045static void
e7c27a73 13046read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13047{
5e22e966 13048 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13049 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13050 struct context_stack *newobj;
c906108c
SS
13051 CORE_ADDR lowpc;
13052 CORE_ADDR highpc;
13053 struct die_info *child_die;
edb3359d 13054 struct attribute *attr, *call_line, *call_file;
15d034d0 13055 const char *name;
e142c38c 13056 CORE_ADDR baseaddr;
801e3a5b 13057 struct block *block;
edb3359d 13058 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13059 std::vector<struct symbol *> template_args;
34eaf542 13060 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13061
13062 if (inlined_func)
13063 {
13064 /* If we do not have call site information, we can't show the
13065 caller of this inlined function. That's too confusing, so
13066 only use the scope for local variables. */
13067 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13068 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13069 if (call_line == NULL || call_file == NULL)
13070 {
13071 read_lexical_block_scope (die, cu);
13072 return;
13073 }
13074 }
c906108c 13075
b3b3bada 13076 baseaddr = objfile->text_section_offset ();
e142c38c 13077
94af9270 13078 name = dwarf2_name (die, cu);
c906108c 13079
e8d05480
JB
13080 /* Ignore functions with missing or empty names. These are actually
13081 illegal according to the DWARF standard. */
13082 if (name == NULL)
13083 {
b98664d3 13084 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13085 sect_offset_str (die->sect_off));
e8d05480
JB
13086 return;
13087 }
13088
13089 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13090 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13091 <= PC_BOUNDS_INVALID)
e8d05480 13092 {
ae4d0c03
PM
13093 attr = dwarf2_attr (die, DW_AT_external, cu);
13094 if (!attr || !DW_UNSND (attr))
b98664d3 13095 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13096 "for subprogram DIE at %s"),
13097 sect_offset_str (die->sect_off));
e8d05480
JB
13098 return;
13099 }
c906108c 13100
3e29f34a
MR
13101 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13102 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13103
34eaf542
TT
13104 /* If we have any template arguments, then we must allocate a
13105 different sort of symbol. */
436c571c 13106 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13107 {
13108 if (child_die->tag == DW_TAG_template_type_param
13109 || child_die->tag == DW_TAG_template_value_param)
13110 {
8c14c3a3 13111 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13112 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13113 break;
13114 }
13115 }
13116
c24bdb02 13117 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13118 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13119 (struct symbol *) templ_func);
4c2df51b 13120
81873cc8 13121 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13122 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13123 cu->language);
13124
4cecd739
DJ
13125 /* If there is a location expression for DW_AT_frame_base, record
13126 it. */
e142c38c 13127 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13128 if (attr != nullptr)
fe978cb0 13129 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13130
63e43d3a
PMR
13131 /* If there is a location for the static link, record it. */
13132 newobj->static_link = NULL;
13133 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13134 if (attr != nullptr)
63e43d3a 13135 {
224c3ddb
SM
13136 newobj->static_link
13137 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13138 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13139 cu->addr_type ());
63e43d3a
PMR
13140 }
13141
c24bdb02 13142 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13143
639d11d3 13144 if (die->child != NULL)
c906108c 13145 {
639d11d3 13146 child_die = die->child;
c906108c
SS
13147 while (child_die && child_die->tag)
13148 {
34eaf542
TT
13149 if (child_die->tag == DW_TAG_template_type_param
13150 || child_die->tag == DW_TAG_template_value_param)
13151 {
13152 struct symbol *arg = new_symbol (child_die, NULL, cu);
13153
f1078f66 13154 if (arg != NULL)
2f4732b0 13155 template_args.push_back (arg);
34eaf542
TT
13156 }
13157 else
13158 process_die (child_die, cu);
436c571c 13159 child_die = child_die->sibling;
c906108c
SS
13160 }
13161 }
13162
d389af10
JK
13163 inherit_abstract_dies (die, cu);
13164
4a811a97
UW
13165 /* If we have a DW_AT_specification, we might need to import using
13166 directives from the context of the specification DIE. See the
13167 comment in determine_prefix. */
13168 if (cu->language == language_cplus
13169 && dwarf2_attr (die, DW_AT_specification, cu))
13170 {
13171 struct dwarf2_cu *spec_cu = cu;
13172 struct die_info *spec_die = die_specification (die, &spec_cu);
13173
13174 while (spec_die)
13175 {
13176 child_die = spec_die->child;
13177 while (child_die && child_die->tag)
13178 {
13179 if (child_die->tag == DW_TAG_imported_module)
13180 process_die (child_die, spec_cu);
436c571c 13181 child_die = child_die->sibling;
4a811a97
UW
13182 }
13183
13184 /* In some cases, GCC generates specification DIEs that
13185 themselves contain DW_AT_specification attributes. */
13186 spec_die = die_specification (spec_die, &spec_cu);
13187 }
13188 }
13189
c24bdb02 13190 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13191 /* Make a block for the local symbols within. */
c24bdb02 13192 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13193 cstk.static_link, lowpc, highpc);
801e3a5b 13194
df8a16a1 13195 /* For C++, set the block's scope. */
45280282
IB
13196 if ((cu->language == language_cplus
13197 || cu->language == language_fortran
c44af4eb
TT
13198 || cu->language == language_d
13199 || cu->language == language_rust)
4d4ec4e5 13200 && cu->processing_has_namespace_info)
195a3f6c
TT
13201 block_set_scope (block, determine_prefix (die, cu),
13202 &objfile->objfile_obstack);
df8a16a1 13203
801e3a5b
JB
13204 /* If we have address ranges, record them. */
13205 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13206
a60f3166 13207 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13208
34eaf542 13209 /* Attach template arguments to function. */
2f4732b0 13210 if (!template_args.empty ())
34eaf542
TT
13211 {
13212 gdb_assert (templ_func != NULL);
13213
2f4732b0 13214 templ_func->n_template_arguments = template_args.size ();
34eaf542 13215 templ_func->template_arguments
8d749320
SM
13216 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13217 templ_func->n_template_arguments);
34eaf542 13218 memcpy (templ_func->template_arguments,
2f4732b0 13219 template_args.data (),
34eaf542 13220 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13221
13222 /* Make sure that the symtab is set on the new symbols. Even
13223 though they don't appear in this symtab directly, other parts
13224 of gdb assume that symbols do, and this is reasonably
13225 true. */
8634679f 13226 for (symbol *sym : template_args)
3e1d3d8c 13227 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13228 }
13229
208d8187
JB
13230 /* In C++, we can have functions nested inside functions (e.g., when
13231 a function declares a class that has methods). This means that
13232 when we finish processing a function scope, we may need to go
13233 back to building a containing block's symbol lists. */
c24bdb02
KS
13234 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13235 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13236
921e78cf
JB
13237 /* If we've finished processing a top-level function, subsequent
13238 symbols go in the file symbol list. */
c24bdb02
KS
13239 if (cu->get_builder ()->outermost_context_p ())
13240 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13241}
13242
13243/* Process all the DIES contained within a lexical block scope. Start
13244 a new scope, process the dies, and then close the scope. */
13245
13246static void
e7c27a73 13247read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13248{
5e22e966 13249 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13250 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13251 CORE_ADDR lowpc, highpc;
13252 struct die_info *child_die;
e142c38c
DJ
13253 CORE_ADDR baseaddr;
13254
b3b3bada 13255 baseaddr = objfile->text_section_offset ();
c906108c
SS
13256
13257 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13258 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13259 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13260 be nasty. Might be easier to properly extend generic blocks to
af34e669 13261 describe ranges. */
e385593e
JK
13262 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13263 {
13264 case PC_BOUNDS_NOT_PRESENT:
13265 /* DW_TAG_lexical_block has no attributes, process its children as if
13266 there was no wrapping by that DW_TAG_lexical_block.
13267 GCC does no longer produces such DWARF since GCC r224161. */
13268 for (child_die = die->child;
13269 child_die != NULL && child_die->tag;
436c571c 13270 child_die = child_die->sibling)
4f7bc5ed
TT
13271 {
13272 /* We might already be processing this DIE. This can happen
13273 in an unusual circumstance -- where a subroutine A
13274 appears lexically in another subroutine B, but A actually
13275 inlines B. The recursion is broken here, rather than in
13276 inherit_abstract_dies, because it seems better to simply
13277 drop concrete children here. */
13278 if (!child_die->in_process)
13279 process_die (child_die, cu);
13280 }
e385593e
JK
13281 return;
13282 case PC_BOUNDS_INVALID:
13283 return;
13284 }
3e29f34a
MR
13285 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13286 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13287
c24bdb02 13288 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13289 if (die->child != NULL)
c906108c 13290 {
639d11d3 13291 child_die = die->child;
c906108c
SS
13292 while (child_die && child_die->tag)
13293 {
e7c27a73 13294 process_die (child_die, cu);
436c571c 13295 child_die = child_die->sibling;
c906108c
SS
13296 }
13297 }
3ea89b92 13298 inherit_abstract_dies (die, cu);
c24bdb02 13299 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13300
c24bdb02
KS
13301 if (*cu->get_builder ()->get_local_symbols () != NULL
13302 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13303 {
801e3a5b 13304 struct block *block
c24bdb02 13305 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13306 cstk.start_addr, highpc);
801e3a5b
JB
13307
13308 /* Note that recording ranges after traversing children, as we
13309 do here, means that recording a parent's ranges entails
13310 walking across all its children's ranges as they appear in
13311 the address map, which is quadratic behavior.
13312
13313 It would be nicer to record the parent's ranges before
13314 traversing its children, simply overriding whatever you find
13315 there. But since we don't even decide whether to create a
13316 block until after we've traversed its children, that's hard
13317 to do. */
13318 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13319 }
c24bdb02
KS
13320 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13321 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13322}
13323
216f72a1 13324/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13325
13326static void
13327read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13328{
5e22e966 13329 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13330 struct objfile *objfile = per_objfile->objfile;
08feed99 13331 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13332 CORE_ADDR pc, baseaddr;
13333 struct attribute *attr;
13334 struct call_site *call_site, call_site_local;
13335 void **slot;
13336 int nparams;
13337 struct die_info *child_die;
13338
b3b3bada 13339 baseaddr = objfile->text_section_offset ();
96408a79 13340
216f72a1
JK
13341 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13342 if (attr == NULL)
13343 {
13344 /* This was a pre-DWARF-5 GNU extension alias
13345 for DW_AT_call_return_pc. */
13346 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13347 }
96408a79
SA
13348 if (!attr)
13349 {
b98664d3 13350 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13351 "DIE %s [in module %s]"),
13352 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13353 return;
13354 }
cd6c91b4 13355 pc = attr->value_as_address () + baseaddr;
3e29f34a 13356 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13357
13358 if (cu->call_site_htab == NULL)
13359 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13360 NULL, &objfile->objfile_obstack,
13361 hashtab_obstack_allocate, NULL);
13362 call_site_local.pc = pc;
13363 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13364 if (*slot != NULL)
13365 {
b98664d3 13366 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13367 "DIE %s [in module %s]"),
13368 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13369 objfile_name (objfile));
96408a79
SA
13370 return;
13371 }
13372
13373 /* Count parameters at the caller. */
13374
13375 nparams = 0;
13376 for (child_die = die->child; child_die && child_die->tag;
436c571c 13377 child_die = child_die->sibling)
96408a79 13378 {
216f72a1
JK
13379 if (child_die->tag != DW_TAG_call_site_parameter
13380 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13381 {
b98664d3 13382 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13383 "DW_TAG_call_site child DIE %s [in module %s]"),
13384 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13385 objfile_name (objfile));
96408a79
SA
13386 continue;
13387 }
13388
13389 nparams++;
13390 }
13391
224c3ddb
SM
13392 call_site
13393 = ((struct call_site *)
13394 obstack_alloc (&objfile->objfile_obstack,
13395 sizeof (*call_site)
13396 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13397 *slot = call_site;
13398 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13399 call_site->pc = pc;
13400
216f72a1
JK
13401 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13402 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13403 {
13404 struct die_info *func_die;
13405
13406 /* Skip also over DW_TAG_inlined_subroutine. */
13407 for (func_die = die->parent;
13408 func_die && func_die->tag != DW_TAG_subprogram
13409 && func_die->tag != DW_TAG_subroutine_type;
13410 func_die = func_die->parent);
13411
216f72a1
JK
13412 /* DW_AT_call_all_calls is a superset
13413 of DW_AT_call_all_tail_calls. */
96408a79 13414 if (func_die
216f72a1 13415 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13416 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13417 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13418 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13419 {
13420 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13421 not complete. But keep CALL_SITE for look ups via call_site_htab,
13422 both the initial caller containing the real return address PC and
13423 the final callee containing the current PC of a chain of tail
13424 calls do not need to have the tail call list complete. But any
13425 function candidate for a virtual tail call frame searched via
13426 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13427 determined unambiguously. */
13428 }
13429 else
13430 {
13431 struct type *func_type = NULL;
13432
13433 if (func_die)
13434 func_type = get_die_type (func_die, cu);
13435 if (func_type != NULL)
13436 {
78134374 13437 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13438
13439 /* Enlist this call site to the function. */
13440 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13441 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13442 }
13443 else
b98664d3 13444 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13445 "DIE %s [in module %s]"),
13446 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13447 }
13448 }
13449
216f72a1
JK
13450 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13451 if (attr == NULL)
13452 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13453 if (attr == NULL)
13454 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13455 if (attr == NULL)
216f72a1
JK
13456 {
13457 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13458 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13459 }
96408a79 13460 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13461 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13462 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13463 else if (attr->form_is_block ())
96408a79
SA
13464 {
13465 struct dwarf2_locexpr_baton *dlbaton;
13466
8d749320 13467 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13468 dlbaton->data = DW_BLOCK (attr)->data;
13469 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13470 dlbaton->per_objfile = per_objfile;
96408a79
SA
13471 dlbaton->per_cu = cu->per_cu;
13472
13473 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13474 }
cd6c91b4 13475 else if (attr->form_is_ref ())
96408a79 13476 {
96408a79
SA
13477 struct dwarf2_cu *target_cu = cu;
13478 struct die_info *target_die;
13479
ac9ec31b 13480 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13481 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13482 if (die_is_declaration (target_die, target_cu))
13483 {
7d45c7c3 13484 const char *target_physname;
9112db09
JK
13485
13486 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13487 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13488 if (target_physname == NULL)
9112db09 13489 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13490 if (target_physname == NULL)
b98664d3 13491 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13492 "physname, for referencing DIE %s [in module %s]"),
13493 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13494 else
7d455152 13495 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13496 }
13497 else
13498 {
13499 CORE_ADDR lowpc;
13500
13501 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13502 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13503 <= PC_BOUNDS_INVALID)
b98664d3 13504 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13505 "low pc, for referencing DIE %s [in module %s]"),
13506 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13507 else
3e29f34a
MR
13508 {
13509 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13510 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13511 }
96408a79
SA
13512 }
13513 }
13514 else
b98664d3 13515 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13516 "block nor reference, for DIE %s [in module %s]"),
13517 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13518
13519 call_site->per_cu = cu->per_cu;
9f47c707 13520 call_site->per_objfile = per_objfile;
96408a79
SA
13521
13522 for (child_die = die->child;
13523 child_die && child_die->tag;
436c571c 13524 child_die = child_die->sibling)
96408a79 13525 {
96408a79 13526 struct call_site_parameter *parameter;
1788b2d3 13527 struct attribute *loc, *origin;
96408a79 13528
216f72a1
JK
13529 if (child_die->tag != DW_TAG_call_site_parameter
13530 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13531 {
13532 /* Already printed the complaint above. */
13533 continue;
13534 }
13535
13536 gdb_assert (call_site->parameter_count < nparams);
13537 parameter = &call_site->parameter[call_site->parameter_count];
13538
1788b2d3
JK
13539 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13540 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13541 register is contained in DW_AT_call_value. */
96408a79 13542
24c5c679 13543 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13544 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13545 if (origin == NULL)
13546 {
13547 /* This was a pre-DWARF-5 GNU extension alias
13548 for DW_AT_call_parameter. */
13549 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13550 }
cd6c91b4 13551 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13552 {
1788b2d3 13553 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13554
0826b30a 13555 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13556 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13557 {
13558 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13559 binding can be done only inside one CU. Such referenced DIE
13560 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13561 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13562 "DW_TAG_call_site child DIE %s [in module %s]"),
13563 sect_offset_str (child_die->sect_off),
9c541725 13564 objfile_name (objfile));
d76b7dbc
JK
13565 continue;
13566 }
9c541725
PA
13567 parameter->u.param_cu_off
13568 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13569 }
4fc6c0d5 13570 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13571 {
b98664d3 13572 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13573 "DW_TAG_call_site child DIE %s [in module %s]"),
13574 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13575 continue;
13576 }
24c5c679 13577 else
96408a79 13578 {
24c5c679
JK
13579 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13580 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13581 if (parameter->u.dwarf_reg != -1)
13582 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13583 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13584 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13585 &parameter->u.fb_offset))
13586 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13587 else
13588 {
b98664d3 13589 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13590 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13591 "DW_TAG_call_site child DIE %s "
24c5c679 13592 "[in module %s]"),
9d8780f0 13593 sect_offset_str (child_die->sect_off),
9c541725 13594 objfile_name (objfile));
24c5c679
JK
13595 continue;
13596 }
96408a79
SA
13597 }
13598
216f72a1
JK
13599 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13600 if (attr == NULL)
13601 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13602 if (attr == NULL || !attr->form_is_block ())
96408a79 13603 {
b98664d3 13604 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13605 "DW_TAG_call_site child DIE %s [in module %s]"),
13606 sect_offset_str (child_die->sect_off),
9c541725 13607 objfile_name (objfile));
96408a79
SA
13608 continue;
13609 }
13610 parameter->value = DW_BLOCK (attr)->data;
13611 parameter->value_size = DW_BLOCK (attr)->size;
13612
13613 /* Parameters are not pre-cleared by memset above. */
13614 parameter->data_value = NULL;
13615 parameter->data_value_size = 0;
13616 call_site->parameter_count++;
13617
216f72a1
JK
13618 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13619 if (attr == NULL)
13620 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13621 if (attr != nullptr)
96408a79 13622 {
4fc6c0d5 13623 if (!attr->form_is_block ())
b98664d3 13624 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13625 "DW_TAG_call_site child DIE %s [in module %s]"),
13626 sect_offset_str (child_die->sect_off),
9c541725 13627 objfile_name (objfile));
96408a79
SA
13628 else
13629 {
13630 parameter->data_value = DW_BLOCK (attr)->data;
13631 parameter->data_value_size = DW_BLOCK (attr)->size;
13632 }
13633 }
13634 }
13635}
13636
71a3c369
TT
13637/* Helper function for read_variable. If DIE represents a virtual
13638 table, then return the type of the concrete object that is
13639 associated with the virtual table. Otherwise, return NULL. */
13640
13641static struct type *
13642rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13643{
13644 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13645 if (attr == NULL)
13646 return NULL;
13647
13648 /* Find the type DIE. */
13649 struct die_info *type_die = NULL;
13650 struct dwarf2_cu *type_cu = cu;
13651
cd6c91b4 13652 if (attr->form_is_ref ())
71a3c369
TT
13653 type_die = follow_die_ref (die, attr, &type_cu);
13654 if (type_die == NULL)
13655 return NULL;
13656
13657 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13658 return NULL;
13659 return die_containing_type (type_die, type_cu);
13660}
13661
13662/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13663
13664static void
13665read_variable (struct die_info *die, struct dwarf2_cu *cu)
13666{
13667 struct rust_vtable_symbol *storage = NULL;
13668
13669 if (cu->language == language_rust)
13670 {
13671 struct type *containing_type = rust_containing_type (die, cu);
13672
13673 if (containing_type != NULL)
13674 {
5e22e966 13675 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13676
8c14c3a3 13677 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13678 storage->concrete_type = containing_type;
cf724bc9 13679 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13680 }
13681 }
13682
e4a62c65
TV
13683 struct symbol *res = new_symbol (die, NULL, cu, storage);
13684 struct attribute *abstract_origin
13685 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13686 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13687 if (res == NULL && loc && abstract_origin)
13688 {
13689 /* We have a variable without a name, but with a location and an abstract
13690 origin. This may be a concrete instance of an abstract variable
13691 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13692 later. */
13693 struct dwarf2_cu *origin_cu = cu;
13694 struct die_info *origin_die
13695 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13696 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13697 per_objfile->per_bfd->abstract_to_concrete
13698 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13699 }
71a3c369
TT
13700}
13701
43988095
JK
13702/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13703 reading .debug_rnglists.
13704 Callback's type should be:
13705 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13706 Return true if the attributes are present and valid, otherwise,
13707 return false. */
13708
13709template <typename Callback>
13710static bool
13711dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13712 Callback &&callback)
13713{
5e22e966 13714 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13715 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13716 bfd *obfd = objfile->obfd;
43988095 13717 /* Base address selection entry. */
2b24b6e4 13718 gdb::optional<CORE_ADDR> base;
43988095 13719 const gdb_byte *buffer;
43988095
JK
13720 CORE_ADDR baseaddr;
13721 bool overflow = false;
13722
43988095
JK
13723 base = cu->base_address;
13724
5989a64e
SM
13725 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13726 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13727 {
b98664d3 13728 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13729 offset);
13730 return false;
13731 }
5989a64e 13732 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13733
b3b3bada 13734 baseaddr = objfile->text_section_offset ();
43988095
JK
13735
13736 while (1)
13737 {
7814882a
JK
13738 /* Initialize it due to a false compiler warning. */
13739 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13740 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13741 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13742 unsigned int bytes_read;
13743
13744 if (buffer == buf_end)
13745 {
13746 overflow = true;
13747 break;
13748 }
13749 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13750 switch (rlet)
13751 {
13752 case DW_RLE_end_of_list:
13753 break;
13754 case DW_RLE_base_address:
13755 if (buffer + cu->header.addr_size > buf_end)
13756 {
13757 overflow = true;
13758 break;
13759 }
c8a7a66f 13760 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13761 buffer += bytes_read;
13762 break;
13763 case DW_RLE_start_length:
13764 if (buffer + cu->header.addr_size > buf_end)
13765 {
13766 overflow = true;
13767 break;
13768 }
c8a7a66f
TT
13769 range_beginning = cu->header.read_address (obfd, buffer,
13770 &bytes_read);
43988095
JK
13771 buffer += bytes_read;
13772 range_end = (range_beginning
13773 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13774 buffer += bytes_read;
13775 if (buffer > buf_end)
13776 {
13777 overflow = true;
13778 break;
13779 }
13780 break;
13781 case DW_RLE_offset_pair:
13782 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13783 buffer += bytes_read;
13784 if (buffer > buf_end)
13785 {
13786 overflow = true;
13787 break;
13788 }
13789 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13790 buffer += bytes_read;
13791 if (buffer > buf_end)
13792 {
13793 overflow = true;
13794 break;
13795 }
13796 break;
13797 case DW_RLE_start_end:
13798 if (buffer + 2 * cu->header.addr_size > buf_end)
13799 {
13800 overflow = true;
13801 break;
13802 }
c8a7a66f
TT
13803 range_beginning = cu->header.read_address (obfd, buffer,
13804 &bytes_read);
43988095 13805 buffer += bytes_read;
c8a7a66f 13806 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13807 buffer += bytes_read;
13808 break;
13809 default:
b98664d3 13810 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13811 return false;
13812 }
13813 if (rlet == DW_RLE_end_of_list || overflow)
13814 break;
13815 if (rlet == DW_RLE_base_address)
13816 continue;
13817
2b24b6e4 13818 if (!base.has_value ())
43988095
JK
13819 {
13820 /* We have no valid base address for the ranges
13821 data. */
b98664d3 13822 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13823 return false;
13824 }
13825
13826 if (range_beginning > range_end)
13827 {
13828 /* Inverted range entries are invalid. */
b98664d3 13829 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13830 return false;
13831 }
13832
13833 /* Empty range entries have no effect. */
13834 if (range_beginning == range_end)
13835 continue;
13836
2b24b6e4
TT
13837 range_beginning += *base;
13838 range_end += *base;
43988095
JK
13839
13840 /* A not-uncommon case of bad debug info.
13841 Don't pollute the addrmap with bad data. */
13842 if (range_beginning + baseaddr == 0
5989a64e 13843 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13844 {
b98664d3 13845 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13846 " [in module %s]"), objfile_name (objfile));
13847 continue;
13848 }
13849
13850 callback (range_beginning, range_end);
13851 }
13852
13853 if (overflow)
13854 {
b98664d3 13855 complaint (_("Offset %d is not terminated "
43988095
JK
13856 "for DW_AT_ranges attribute"),
13857 offset);
13858 return false;
13859 }
13860
13861 return true;
13862}
13863
13864/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13865 Callback's type should be:
13866 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13867 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13868
43988095 13869template <typename Callback>
43039443 13870static int
5f46c5a5 13871dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13872 Callback &&callback)
43039443 13873{
5e22e966
SM
13874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13875 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13876 struct comp_unit_head *cu_header = &cu->header;
13877 bfd *obfd = objfile->obfd;
13878 unsigned int addr_size = cu_header->addr_size;
13879 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13880 /* Base address selection entry. */
2b24b6e4 13881 gdb::optional<CORE_ADDR> base;
43039443 13882 unsigned int dummy;
d521ce57 13883 const gdb_byte *buffer;
ff013f42 13884 CORE_ADDR baseaddr;
43039443 13885
43988095
JK
13886 if (cu_header->version >= 5)
13887 return dwarf2_rnglists_process (offset, cu, callback);
13888
d00adf39 13889 base = cu->base_address;
43039443 13890
5e22e966
SM
13891 per_objfile->per_bfd->ranges.read (objfile);
13892 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13893 {
b98664d3 13894 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13895 offset);
13896 return 0;
13897 }
5e22e966 13898 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13899
b3b3bada 13900 baseaddr = objfile->text_section_offset ();
ff013f42 13901
43039443
JK
13902 while (1)
13903 {
13904 CORE_ADDR range_beginning, range_end;
13905
c8a7a66f 13906 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13907 buffer += addr_size;
c8a7a66f 13908 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13909 buffer += addr_size;
13910 offset += 2 * addr_size;
13911
13912 /* An end of list marker is a pair of zero addresses. */
13913 if (range_beginning == 0 && range_end == 0)
13914 /* Found the end of list entry. */
13915 break;
13916
13917 /* Each base address selection entry is a pair of 2 values.
13918 The first is the largest possible address, the second is
13919 the base address. Check for a base address here. */
13920 if ((range_beginning & mask) == mask)
13921 {
28d2bfb9
AB
13922 /* If we found the largest possible address, then we already
13923 have the base address in range_end. */
13924 base = range_end;
43039443
JK
13925 continue;
13926 }
13927
2b24b6e4 13928 if (!base.has_value ())
43039443
JK
13929 {
13930 /* We have no valid base address for the ranges
13931 data. */
b98664d3 13932 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13933 return 0;
13934 }
13935
9277c30c
UW
13936 if (range_beginning > range_end)
13937 {
13938 /* Inverted range entries are invalid. */
b98664d3 13939 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13940 return 0;
13941 }
13942
13943 /* Empty range entries have no effect. */
13944 if (range_beginning == range_end)
13945 continue;
13946
2b24b6e4
TT
13947 range_beginning += *base;
13948 range_end += *base;
43039443 13949
01093045
DE
13950 /* A not-uncommon case of bad debug info.
13951 Don't pollute the addrmap with bad data. */
13952 if (range_beginning + baseaddr == 0
5e22e966 13953 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13954 {
b98664d3 13955 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13956 " [in module %s]"), objfile_name (objfile));
01093045
DE
13957 continue;
13958 }
13959
5f46c5a5
JK
13960 callback (range_beginning, range_end);
13961 }
13962
13963 return 1;
13964}
13965
13966/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13967 Return 1 if the attributes are present and valid, otherwise, return 0.
13968 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13969
13970static int
13971dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13972 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13973 dwarf2_psymtab *ranges_pst)
5f46c5a5 13974{
5e22e966 13975 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13976 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13977 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13978 int low_set = 0;
13979 CORE_ADDR low = 0;
13980 CORE_ADDR high = 0;
13981 int retval;
13982
13983 retval = dwarf2_ranges_process (offset, cu,
13984 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13985 {
9277c30c 13986 if (ranges_pst != NULL)
3e29f34a
MR
13987 {
13988 CORE_ADDR lowpc;
13989 CORE_ADDR highpc;
13990
79748972
TT
13991 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13992 range_beginning + baseaddr)
13993 - baseaddr);
13994 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13995 range_end + baseaddr)
13996 - baseaddr);
d320c2b5
TT
13997 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13998 lowpc, highpc - 1, ranges_pst);
3e29f34a 13999 }
ff013f42 14000
43039443
JK
14001 /* FIXME: This is recording everything as a low-high
14002 segment of consecutive addresses. We should have a
14003 data structure for discontiguous block ranges
14004 instead. */
14005 if (! low_set)
14006 {
14007 low = range_beginning;
14008 high = range_end;
14009 low_set = 1;
14010 }
14011 else
14012 {
14013 if (range_beginning < low)
14014 low = range_beginning;
14015 if (range_end > high)
14016 high = range_end;
14017 }
5f46c5a5
JK
14018 });
14019 if (!retval)
14020 return 0;
43039443
JK
14021
14022 if (! low_set)
14023 /* If the first entry is an end-of-list marker, the range
14024 describes an empty scope, i.e. no instructions. */
14025 return 0;
14026
14027 if (low_return)
14028 *low_return = low;
14029 if (high_return)
14030 *high_return = high;
14031 return 1;
14032}
14033
3a2b436a
JK
14034/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14035 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14036 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14037
3a2b436a 14038static enum pc_bounds_kind
af34e669 14039dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14040 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14041 dwarf2_psymtab *pst)
c906108c 14042{
5e22e966 14043 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14044 struct attribute *attr;
91da1414 14045 struct attribute *attr_high;
af34e669
DJ
14046 CORE_ADDR low = 0;
14047 CORE_ADDR high = 0;
e385593e 14048 enum pc_bounds_kind ret;
c906108c 14049
91da1414
MW
14050 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14051 if (attr_high)
af34e669 14052 {
e142c38c 14053 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14054 if (attr != nullptr)
91da1414 14055 {
cd6c91b4
TT
14056 low = attr->value_as_address ();
14057 high = attr_high->value_as_address ();
14058 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14059 high += low;
91da1414 14060 }
af34e669
DJ
14061 else
14062 /* Found high w/o low attribute. */
e385593e 14063 return PC_BOUNDS_INVALID;
af34e669
DJ
14064
14065 /* Found consecutive range of addresses. */
3a2b436a 14066 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14067 }
c906108c 14068 else
af34e669 14069 {
e142c38c 14070 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14071 if (attr != NULL)
14072 {
18a8505e 14073 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14074 We take advantage of the fact that DW_AT_ranges does not appear
14075 in DW_TAG_compile_unit of DWO files. */
14076 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14077 unsigned int ranges_offset = (DW_UNSND (attr)
14078 + (need_ranges_base
14079 ? cu->ranges_base
14080 : 0));
2e3cf129 14081
af34e669 14082 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14083 .debug_ranges section. */
2e3cf129 14084 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14085 return PC_BOUNDS_INVALID;
43039443 14086 /* Found discontinuous range of addresses. */
3a2b436a 14087 ret = PC_BOUNDS_RANGES;
af34e669 14088 }
e385593e
JK
14089 else
14090 return PC_BOUNDS_NOT_PRESENT;
af34e669 14091 }
c906108c 14092
48fbe735 14093 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14094 if (high <= low)
e385593e 14095 return PC_BOUNDS_INVALID;
c906108c
SS
14096
14097 /* When using the GNU linker, .gnu.linkonce. sections are used to
14098 eliminate duplicate copies of functions and vtables and such.
14099 The linker will arbitrarily choose one and discard the others.
14100 The AT_*_pc values for such functions refer to local labels in
14101 these sections. If the section from that file was discarded, the
14102 labels are not in the output, so the relocs get a value of 0.
14103 If this is a discarded function, mark the pc bounds as invalid,
14104 so that GDB will ignore it. */
5989a64e 14105 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14106 return PC_BOUNDS_INVALID;
c906108c
SS
14107
14108 *lowpc = low;
96408a79
SA
14109 if (highpc)
14110 *highpc = high;
af34e669 14111 return ret;
c906108c
SS
14112}
14113
b084d499
JB
14114/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14115 its low and high PC addresses. Do nothing if these addresses could not
14116 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14117 and HIGHPC to the high address if greater than HIGHPC. */
14118
14119static void
14120dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14121 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14122 struct dwarf2_cu *cu)
14123{
14124 CORE_ADDR low, high;
14125 struct die_info *child = die->child;
14126
e385593e 14127 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14128 {
325fac50
PA
14129 *lowpc = std::min (*lowpc, low);
14130 *highpc = std::max (*highpc, high);
b084d499
JB
14131 }
14132
14133 /* If the language does not allow nested subprograms (either inside
14134 subprograms or lexical blocks), we're done. */
14135 if (cu->language != language_ada)
14136 return;
6e70227d 14137
b084d499
JB
14138 /* Check all the children of the given DIE. If it contains nested
14139 subprograms, then check their pc bounds. Likewise, we need to
14140 check lexical blocks as well, as they may also contain subprogram
14141 definitions. */
14142 while (child && child->tag)
14143 {
14144 if (child->tag == DW_TAG_subprogram
14145 || child->tag == DW_TAG_lexical_block)
14146 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14147 child = child->sibling;
b084d499
JB
14148 }
14149}
14150
fae299cd
DC
14151/* Get the low and high pc's represented by the scope DIE, and store
14152 them in *LOWPC and *HIGHPC. If the correct values can't be
14153 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14154
14155static void
14156get_scope_pc_bounds (struct die_info *die,
14157 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14158 struct dwarf2_cu *cu)
14159{
14160 CORE_ADDR best_low = (CORE_ADDR) -1;
14161 CORE_ADDR best_high = (CORE_ADDR) 0;
14162 CORE_ADDR current_low, current_high;
14163
3a2b436a 14164 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14165 >= PC_BOUNDS_RANGES)
fae299cd
DC
14166 {
14167 best_low = current_low;
14168 best_high = current_high;
14169 }
14170 else
14171 {
14172 struct die_info *child = die->child;
14173
14174 while (child && child->tag)
14175 {
14176 switch (child->tag) {
14177 case DW_TAG_subprogram:
b084d499 14178 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14179 break;
14180 case DW_TAG_namespace:
f55ee35c 14181 case DW_TAG_module:
fae299cd
DC
14182 /* FIXME: carlton/2004-01-16: Should we do this for
14183 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14184 that current GCC's always emit the DIEs corresponding
14185 to definitions of methods of classes as children of a
14186 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14187 the DIEs giving the declarations, which could be
14188 anywhere). But I don't see any reason why the
14189 standards says that they have to be there. */
14190 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14191
14192 if (current_low != ((CORE_ADDR) -1))
14193 {
325fac50
PA
14194 best_low = std::min (best_low, current_low);
14195 best_high = std::max (best_high, current_high);
fae299cd
DC
14196 }
14197 break;
14198 default:
0963b4bd 14199 /* Ignore. */
fae299cd
DC
14200 break;
14201 }
14202
436c571c 14203 child = child->sibling;
fae299cd
DC
14204 }
14205 }
14206
14207 *lowpc = best_low;
14208 *highpc = best_high;
14209}
14210
801e3a5b
JB
14211/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14212 in DIE. */
380bca97 14213
801e3a5b
JB
14214static void
14215dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14216 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14217{
5e22e966 14218 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14219 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14220 struct attribute *attr;
91da1414 14221 struct attribute *attr_high;
801e3a5b 14222
91da1414
MW
14223 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14224 if (attr_high)
801e3a5b 14225 {
801e3a5b 14226 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14227 if (attr != nullptr)
801e3a5b 14228 {
cd6c91b4
TT
14229 CORE_ADDR low = attr->value_as_address ();
14230 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14231
cd6c91b4 14232 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14233 high += low;
9a619af0 14234
3e29f34a
MR
14235 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14236 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14237 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14238 }
14239 }
14240
14241 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14242 if (attr != nullptr)
801e3a5b 14243 {
18a8505e 14244 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14245 We take advantage of the fact that DW_AT_ranges does not appear
14246 in DW_TAG_compile_unit of DWO files. */
14247 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14248
14249 /* The value of the DW_AT_ranges attribute is the offset of the
14250 address range list in the .debug_ranges section. */
ab435259
DE
14251 unsigned long offset = (DW_UNSND (attr)
14252 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14253
2d5f09ec 14254 std::vector<blockrange> blockvec;
5f46c5a5
JK
14255 dwarf2_ranges_process (offset, cu,
14256 [&] (CORE_ADDR start, CORE_ADDR end)
14257 {
58fdfd2c
JK
14258 start += baseaddr;
14259 end += baseaddr;
5f46c5a5
JK
14260 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14261 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14262 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14263 blockvec.emplace_back (start, end);
5f46c5a5 14264 });
2d5f09ec
KB
14265
14266 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14267 }
14268}
14269
685b1105
JK
14270/* Check whether the producer field indicates either of GCC < 4.6, or the
14271 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14272
685b1105
JK
14273static void
14274check_producer (struct dwarf2_cu *cu)
60d5a603 14275{
38360086 14276 int major, minor;
60d5a603
JK
14277
14278 if (cu->producer == NULL)
14279 {
14280 /* For unknown compilers expect their behavior is DWARF version
14281 compliant.
14282
14283 GCC started to support .debug_types sections by -gdwarf-4 since
14284 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14285 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14286 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14287 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14288 }
b1ffba5a 14289 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14290 {
38360086
MW
14291 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14292 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14293 }
5230b05a 14294 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14295 {
14296 cu->producer_is_icc = true;
14297 cu->producer_is_icc_lt_14 = major < 14;
14298 }
c258c396
JD
14299 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14300 cu->producer_is_codewarrior = true;
685b1105
JK
14301 else
14302 {
14303 /* For other non-GCC compilers, expect their behavior is DWARF version
14304 compliant. */
60d5a603
JK
14305 }
14306
9068261f 14307 cu->checked_producer = true;
685b1105 14308}
ba919b58 14309
685b1105
JK
14310/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14311 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14312 during 4.6.0 experimental. */
14313
9068261f 14314static bool
685b1105
JK
14315producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14316{
14317 if (!cu->checked_producer)
14318 check_producer (cu);
14319
14320 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14321}
14322
c258c396
JD
14323
14324/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14325 with incorrect is_stmt attributes. */
14326
14327static bool
14328producer_is_codewarrior (struct dwarf2_cu *cu)
14329{
14330 if (!cu->checked_producer)
14331 check_producer (cu);
14332
14333 return cu->producer_is_codewarrior;
14334}
14335
405feb71 14336/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14337 DW_AT_accessibility. */
14338
14339static enum dwarf_access_attribute
14340dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14341{
14342 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14343 {
14344 /* The default DWARF 2 accessibility for members is public, the default
14345 accessibility for inheritance is private. */
14346
14347 if (die->tag != DW_TAG_inheritance)
14348 return DW_ACCESS_public;
14349 else
14350 return DW_ACCESS_private;
14351 }
14352 else
14353 {
14354 /* DWARF 3+ defines the default accessibility a different way. The same
14355 rules apply now for DW_TAG_inheritance as for the members and it only
14356 depends on the container kind. */
14357
14358 if (die->parent->tag == DW_TAG_class_type)
14359 return DW_ACCESS_private;
14360 else
14361 return DW_ACCESS_public;
14362 }
14363}
14364
74ac6d43
TT
14365/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14366 offset. If the attribute was not found return 0, otherwise return
14367 1. If it was found but could not properly be handled, set *OFFSET
14368 to 0. */
14369
14370static int
14371handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14372 LONGEST *offset)
14373{
14374 struct attribute *attr;
14375
14376 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14377 if (attr != NULL)
14378 {
14379 *offset = 0;
14380
14381 /* Note that we do not check for a section offset first here.
14382 This is because DW_AT_data_member_location is new in DWARF 4,
14383 so if we see it, we can assume that a constant form is really
14384 a constant and not a section offset. */
cd6c91b4 14385 if (attr->form_is_constant ())
0826b30a 14386 *offset = attr->constant_value (0);
cd6c91b4 14387 else if (attr->form_is_section_offset ())
74ac6d43 14388 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14389 else if (attr->form_is_block ())
74ac6d43
TT
14390 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14391 else
14392 dwarf2_complex_location_expr_complaint ();
14393
14394 return 1;
14395 }
14396
14397 return 0;
14398}
14399
7d79de9a
TT
14400/* Look for DW_AT_data_member_location and store the results in FIELD. */
14401
14402static void
14403handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14404 struct field *field)
14405{
14406 struct attribute *attr;
14407
14408 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14409 if (attr != NULL)
14410 {
14411 if (attr->form_is_constant ())
14412 {
14413 LONGEST offset = attr->constant_value (0);
14414 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14415 }
14416 else if (attr->form_is_section_offset ())
14417 dwarf2_complex_location_expr_complaint ();
14418 else if (attr->form_is_block ())
14419 {
14420 bool handled;
14421 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14422 if (handled)
14423 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14424 else
14425 {
5e22e966
SM
14426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14427 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14428 struct dwarf2_locexpr_baton *dlbaton
14429 = XOBNEW (&objfile->objfile_obstack,
14430 struct dwarf2_locexpr_baton);
14431 dlbaton->data = DW_BLOCK (attr)->data;
14432 dlbaton->size = DW_BLOCK (attr)->size;
14433 /* When using this baton, we want to compute the address
14434 of the field, not the value. This is why
14435 is_reference is set to false here. */
14436 dlbaton->is_reference = false;
5e22e966 14437 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14438 dlbaton->per_cu = cu->per_cu;
14439
14440 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14441 }
14442 }
14443 else
14444 dwarf2_complex_location_expr_complaint ();
14445 }
14446}
14447
c906108c
SS
14448/* Add an aggregate field to the field list. */
14449
14450static void
107d2387 14451dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14452 struct dwarf2_cu *cu)
6e70227d 14453{
5e22e966 14454 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14455 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14456 struct nextfield *new_field;
14457 struct attribute *attr;
14458 struct field *fp;
15d034d0 14459 const char *fieldname = "";
c906108c 14460
7d0ccb61
DJ
14461 if (die->tag == DW_TAG_inheritance)
14462 {
be2daae6
TT
14463 fip->baseclasses.emplace_back ();
14464 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14465 }
14466 else
14467 {
be2daae6
TT
14468 fip->fields.emplace_back ();
14469 new_field = &fip->fields.back ();
7d0ccb61 14470 }
be2daae6 14471
9c6a1327
TT
14472 new_field->offset = die->sect_off;
14473
e142c38c 14474 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14475 if (attr != nullptr)
c906108c 14476 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14477 else
14478 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14479 if (new_field->accessibility != DW_ACCESS_public)
14480 fip->non_public_fields = 1;
60d5a603 14481
e142c38c 14482 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14483 if (attr != nullptr)
c906108c 14484 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14485 else
14486 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14487
14488 fp = &new_field->field;
a9a9bd0f 14489
e142c38c 14490 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14491 {
a9a9bd0f 14492 /* Data member other than a C++ static data member. */
6e70227d 14493
c906108c 14494 /* Get type of field. */
e7c27a73 14495 fp->type = die_type (die, cu);
c906108c 14496
d6a843b5 14497 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14498
c906108c 14499 /* Get bit size of field (zero if none). */
e142c38c 14500 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14501 if (attr != nullptr)
c906108c
SS
14502 {
14503 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14504 }
14505 else
14506 {
14507 FIELD_BITSIZE (*fp) = 0;
14508 }
14509
14510 /* Get bit offset of field. */
7d79de9a 14511 handle_data_member_location (die, cu, fp);
e142c38c 14512 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14513 if (attr != nullptr)
c906108c 14514 {
d5a22e77 14515 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14516 {
14517 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14518 additional bit offset from the MSB of the containing
14519 anonymous object to the MSB of the field. We don't
14520 have to do anything special since we don't need to
14521 know the size of the anonymous object. */
f41f5e61 14522 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14523 }
14524 else
14525 {
14526 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14527 MSB of the anonymous object, subtract off the number of
14528 bits from the MSB of the field to the MSB of the
14529 object, and then subtract off the number of bits of
14530 the field itself. The result is the bit offset of
14531 the LSB of the field. */
c906108c
SS
14532 int anonymous_size;
14533 int bit_offset = DW_UNSND (attr);
14534
e142c38c 14535 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14536 if (attr != nullptr)
c906108c
SS
14537 {
14538 /* The size of the anonymous object containing
14539 the bit field is explicit, so use the
14540 indicated size (in bytes). */
14541 anonymous_size = DW_UNSND (attr);
14542 }
14543 else
14544 {
14545 /* The size of the anonymous object containing
14546 the bit field must be inferred from the type
14547 attribute of the data member containing the
14548 bit field. */
14549 anonymous_size = TYPE_LENGTH (fp->type);
14550 }
f41f5e61
PA
14551 SET_FIELD_BITPOS (*fp,
14552 (FIELD_BITPOS (*fp)
14553 + anonymous_size * bits_per_byte
14554 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14555 }
14556 }
da5b30da
AA
14557 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14558 if (attr != NULL)
14559 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14560 + attr->constant_value (0)));
c906108c
SS
14561
14562 /* Get name of field. */
39cbfefa
DJ
14563 fieldname = dwarf2_name (die, cu);
14564 if (fieldname == NULL)
14565 fieldname = "";
d8151005
DJ
14566
14567 /* The name is already allocated along with this objfile, so we don't
14568 need to duplicate it for the type. */
14569 fp->name = fieldname;
c906108c
SS
14570
14571 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14572 pointer or virtual base class pointer) to private. */
e142c38c 14573 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14574 {
d48cc9dd 14575 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14576 new_field->accessibility = DW_ACCESS_private;
14577 fip->non_public_fields = 1;
14578 }
14579 }
a9a9bd0f 14580 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14581 {
a9a9bd0f
DC
14582 /* C++ static member. */
14583
14584 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14585 is a declaration, but all versions of G++ as of this writing
14586 (so through at least 3.2.1) incorrectly generate
14587 DW_TAG_variable tags. */
6e70227d 14588
ff355380 14589 const char *physname;
c906108c 14590
a9a9bd0f 14591 /* Get name of field. */
39cbfefa
DJ
14592 fieldname = dwarf2_name (die, cu);
14593 if (fieldname == NULL)
c906108c
SS
14594 return;
14595
254e6b9e 14596 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14597 if (attr
14598 /* Only create a symbol if this is an external value.
14599 new_symbol checks this and puts the value in the global symbol
14600 table, which we want. If it is not external, new_symbol
14601 will try to put the value in cu->list_in_scope which is wrong. */
14602 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14603 {
14604 /* A static const member, not much different than an enum as far as
14605 we're concerned, except that we can support more types. */
14606 new_symbol (die, NULL, cu);
14607 }
14608
2df3850c 14609 /* Get physical name. */
ff355380 14610 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14611
d8151005
DJ
14612 /* The name is already allocated along with this objfile, so we don't
14613 need to duplicate it for the type. */
14614 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14615 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14616 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14617 }
14618 else if (die->tag == DW_TAG_inheritance)
14619 {
74ac6d43 14620 /* C++ base class field. */
7d79de9a 14621 handle_data_member_location (die, cu, fp);
c906108c 14622 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14623 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14624 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14625 }
2ddeaf8a
TT
14626 else
14627 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14628}
14629
883fd55a
KS
14630/* Can the type given by DIE define another type? */
14631
14632static bool
14633type_can_define_types (const struct die_info *die)
14634{
14635 switch (die->tag)
14636 {
14637 case DW_TAG_typedef:
14638 case DW_TAG_class_type:
14639 case DW_TAG_structure_type:
14640 case DW_TAG_union_type:
14641 case DW_TAG_enumeration_type:
14642 return true;
14643
14644 default:
14645 return false;
14646 }
14647}
14648
14649/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14650
14651static void
883fd55a
KS
14652dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14653 struct dwarf2_cu *cu)
6e70227d 14654{
be2daae6
TT
14655 struct decl_field fp;
14656 memset (&fp, 0, sizeof (fp));
98751a41 14657
883fd55a 14658 gdb_assert (type_can_define_types (die));
98751a41 14659
883fd55a 14660 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14661 fp.name = dwarf2_name (die, cu);
14662 fp.type = read_type_die (die, cu);
98751a41 14663
c191a687
KS
14664 /* Save accessibility. */
14665 enum dwarf_access_attribute accessibility;
14666 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14667 if (attr != NULL)
14668 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14669 else
14670 accessibility = dwarf2_default_access_attribute (die, cu);
14671 switch (accessibility)
14672 {
14673 case DW_ACCESS_public:
14674 /* The assumed value if neither private nor protected. */
14675 break;
14676 case DW_ACCESS_private:
be2daae6 14677 fp.is_private = 1;
c191a687
KS
14678 break;
14679 case DW_ACCESS_protected:
be2daae6 14680 fp.is_protected = 1;
c191a687
KS
14681 break;
14682 default:
b98664d3 14683 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14684 }
14685
883fd55a 14686 if (die->tag == DW_TAG_typedef)
be2daae6 14687 fip->typedef_field_list.push_back (fp);
883fd55a 14688 else
be2daae6 14689 fip->nested_types_list.push_back (fp);
98751a41
JK
14690}
14691
9c6a1327
TT
14692/* A convenience typedef that's used when finding the discriminant
14693 field for a variant part. */
1b95cdb7
SM
14694typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14695 offset_map_type;
9c6a1327
TT
14696
14697/* Compute the discriminant range for a given variant. OBSTACK is
14698 where the results will be stored. VARIANT is the variant to
14699 process. IS_UNSIGNED indicates whether the discriminant is signed
14700 or unsigned. */
14701
14702static const gdb::array_view<discriminant_range>
14703convert_variant_range (struct obstack *obstack, const variant_field &variant,
14704 bool is_unsigned)
14705{
14706 std::vector<discriminant_range> ranges;
14707
14708 if (variant.default_branch)
14709 return {};
14710
14711 if (variant.discr_list_data == nullptr)
14712 {
14713 discriminant_range r
14714 = {variant.discriminant_value, variant.discriminant_value};
14715 ranges.push_back (r);
14716 }
14717 else
14718 {
14719 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14720 variant.discr_list_data->size);
14721 while (!data.empty ())
14722 {
14723 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14724 {
14725 complaint (_("invalid discriminant marker: %d"), data[0]);
14726 break;
14727 }
14728 bool is_range = data[0] == DW_DSC_range;
14729 data = data.slice (1);
14730
14731 ULONGEST low, high;
14732 unsigned int bytes_read;
14733
14734 if (data.empty ())
14735 {
14736 complaint (_("DW_AT_discr_list missing low value"));
14737 break;
14738 }
14739 if (is_unsigned)
14740 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14741 else
14742 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14743 &bytes_read);
14744 data = data.slice (bytes_read);
14745
14746 if (is_range)
14747 {
14748 if (data.empty ())
14749 {
14750 complaint (_("DW_AT_discr_list missing high value"));
14751 break;
14752 }
14753 if (is_unsigned)
14754 high = read_unsigned_leb128 (nullptr, data.data (),
14755 &bytes_read);
14756 else
14757 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14758 &bytes_read);
14759 data = data.slice (bytes_read);
14760 }
14761 else
14762 high = low;
14763
14764 ranges.push_back ({ low, high });
14765 }
14766 }
14767
14768 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14769 ranges.size ());
14770 std::copy (ranges.begin (), ranges.end (), result);
14771 return gdb::array_view<discriminant_range> (result, ranges.size ());
14772}
14773
14774static const gdb::array_view<variant_part> create_variant_parts
14775 (struct obstack *obstack,
14776 const offset_map_type &offset_map,
14777 struct field_info *fi,
14778 const std::vector<variant_part_builder> &variant_parts);
14779
14780/* Fill in a "struct variant" for a given variant field. RESULT is
14781 the variant to fill in. OBSTACK is where any needed allocations
14782 will be done. OFFSET_MAP holds the mapping from section offsets to
14783 fields for the type. FI describes the fields of the type we're
14784 processing. FIELD is the variant field we're converting. */
14785
14786static void
14787create_one_variant (variant &result, struct obstack *obstack,
14788 const offset_map_type &offset_map,
14789 struct field_info *fi, const variant_field &field)
14790{
14791 result.discriminants = convert_variant_range (obstack, field, false);
14792 result.first_field = field.first_field + fi->baseclasses.size ();
14793 result.last_field = field.last_field + fi->baseclasses.size ();
14794 result.parts = create_variant_parts (obstack, offset_map, fi,
14795 field.variant_parts);
14796}
14797
14798/* Fill in a "struct variant_part" for a given variant part. RESULT
14799 is the variant part to fill in. OBSTACK is where any needed
14800 allocations will be done. OFFSET_MAP holds the mapping from
14801 section offsets to fields for the type. FI describes the fields of
14802 the type we're processing. BUILDER is the variant part to be
14803 converted. */
14804
14805static void
14806create_one_variant_part (variant_part &result,
14807 struct obstack *obstack,
14808 const offset_map_type &offset_map,
14809 struct field_info *fi,
14810 const variant_part_builder &builder)
14811{
14812 auto iter = offset_map.find (builder.discriminant_offset);
14813 if (iter == offset_map.end ())
14814 {
14815 result.discriminant_index = -1;
14816 /* Doesn't matter. */
14817 result.is_unsigned = false;
14818 }
14819 else
14820 {
14821 result.discriminant_index = iter->second;
14822 result.is_unsigned
14823 = TYPE_UNSIGNED (FIELD_TYPE
14824 (fi->fields[result.discriminant_index].field));
14825 }
14826
14827 size_t n = builder.variants.size ();
14828 variant *output = new (obstack) variant[n];
14829 for (size_t i = 0; i < n; ++i)
14830 create_one_variant (output[i], obstack, offset_map, fi,
14831 builder.variants[i]);
14832
14833 result.variants = gdb::array_view<variant> (output, n);
14834}
14835
14836/* Create a vector of variant parts that can be attached to a type.
14837 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14838 holds the mapping from section offsets to fields for the type. FI
14839 describes the fields of the type we're processing. VARIANT_PARTS
14840 is the vector to convert. */
14841
14842static const gdb::array_view<variant_part>
14843create_variant_parts (struct obstack *obstack,
14844 const offset_map_type &offset_map,
14845 struct field_info *fi,
14846 const std::vector<variant_part_builder> &variant_parts)
14847{
14848 if (variant_parts.empty ())
14849 return {};
14850
14851 size_t n = variant_parts.size ();
14852 variant_part *result = new (obstack) variant_part[n];
14853 for (size_t i = 0; i < n; ++i)
14854 create_one_variant_part (result[i], obstack, offset_map, fi,
14855 variant_parts[i]);
14856
14857 return gdb::array_view<variant_part> (result, n);
14858}
14859
14860/* Compute the variant part vector for FIP, attaching it to TYPE when
14861 done. */
14862
14863static void
14864add_variant_property (struct field_info *fip, struct type *type,
14865 struct dwarf2_cu *cu)
14866{
14867 /* Map section offsets of fields to their field index. Note the
14868 field index here does not take the number of baseclasses into
14869 account. */
14870 offset_map_type offset_map;
14871 for (int i = 0; i < fip->fields.size (); ++i)
14872 offset_map[fip->fields[i].offset] = i;
14873
5e22e966 14874 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14875 gdb::array_view<variant_part> parts
14876 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14877 fip->variant_parts);
14878
14879 struct dynamic_prop prop;
14880 prop.kind = PROP_VARIANT_PARTS;
14881 prop.data.variant_parts
14882 = ((gdb::array_view<variant_part> *)
14883 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14884
5c54719c 14885 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14886}
14887
c906108c
SS
14888/* Create the vector of fields, and attach it to the type. */
14889
14890static void
fba45db2 14891dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14892 struct dwarf2_cu *cu)
c906108c 14893{
317f7127 14894 int nfields = fip->nfields ();
c906108c
SS
14895
14896 /* Record the field count, allocate space for the array of fields,
14897 and create blank accessibility bitfields if necessary. */
5e33d5f4 14898 type->set_num_fields (nfields);
3cabb6b0
SM
14899 type->set_fields
14900 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14901
b4ba55a1 14902 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14903 {
14904 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14905
14906 TYPE_FIELD_PRIVATE_BITS (type) =
14907 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14908 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14909
14910 TYPE_FIELD_PROTECTED_BITS (type) =
14911 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14912 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14913
774b6a14
TT
14914 TYPE_FIELD_IGNORE_BITS (type) =
14915 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14916 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14917 }
14918
14919 /* If the type has baseclasses, allocate and clear a bit vector for
14920 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14921 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14922 {
be2daae6 14923 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14924 unsigned char *pointer;
c906108c
SS
14925
14926 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14927 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14928 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14929 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14930 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14931 }
14932
9c6a1327
TT
14933 if (!fip->variant_parts.empty ())
14934 add_variant_property (fip, type, cu);
2ddeaf8a 14935
be2daae6
TT
14936 /* Copy the saved-up fields into the field vector. */
14937 for (int i = 0; i < nfields; ++i)
c906108c 14938 {
be2daae6
TT
14939 struct nextfield &field
14940 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14941 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14942
ceacbf6e 14943 type->field (i) = field.field;
be2daae6 14944 switch (field.accessibility)
c906108c 14945 {
c5aa993b 14946 case DW_ACCESS_private:
b4ba55a1 14947 if (cu->language != language_ada)
be2daae6 14948 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14949 break;
c906108c 14950
c5aa993b 14951 case DW_ACCESS_protected:
b4ba55a1 14952 if (cu->language != language_ada)
be2daae6 14953 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14954 break;
c906108c 14955
c5aa993b
JM
14956 case DW_ACCESS_public:
14957 break;
c906108c 14958
c5aa993b
JM
14959 default:
14960 /* Unknown accessibility. Complain and treat it as public. */
14961 {
b98664d3 14962 complaint (_("unsupported accessibility %d"),
be2daae6 14963 field.accessibility);
c5aa993b
JM
14964 }
14965 break;
c906108c 14966 }
be2daae6 14967 if (i < fip->baseclasses.size ())
c906108c 14968 {
be2daae6 14969 switch (field.virtuality)
c906108c 14970 {
c5aa993b
JM
14971 case DW_VIRTUALITY_virtual:
14972 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14973 if (cu->language == language_ada)
a73c6dcd 14974 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14975 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14976 break;
c906108c
SS
14977 }
14978 }
c906108c
SS
14979 }
14980}
14981
7d27a96d
TT
14982/* Return true if this member function is a constructor, false
14983 otherwise. */
14984
14985static int
14986dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14987{
14988 const char *fieldname;
fe978cb0 14989 const char *type_name;
7d27a96d
TT
14990 int len;
14991
14992 if (die->parent == NULL)
14993 return 0;
14994
14995 if (die->parent->tag != DW_TAG_structure_type
14996 && die->parent->tag != DW_TAG_union_type
14997 && die->parent->tag != DW_TAG_class_type)
14998 return 0;
14999
15000 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15001 type_name = dwarf2_name (die->parent, cu);
15002 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15003 return 0;
15004
15005 len = strlen (fieldname);
fe978cb0
PA
15006 return (strncmp (fieldname, type_name, len) == 0
15007 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15008}
15009
e35000a7
TBA
15010/* Check if the given VALUE is a recognized enum
15011 dwarf_defaulted_attribute constant according to DWARF5 spec,
15012 Table 7.24. */
15013
15014static bool
15015is_valid_DW_AT_defaulted (ULONGEST value)
15016{
15017 switch (value)
15018 {
15019 case DW_DEFAULTED_no:
15020 case DW_DEFAULTED_in_class:
15021 case DW_DEFAULTED_out_of_class:
15022 return true;
15023 }
15024
3142e908 15025 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15026 return false;
15027}
15028
c906108c
SS
15029/* Add a member function to the proper fieldlist. */
15030
15031static void
107d2387 15032dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15033 struct type *type, struct dwarf2_cu *cu)
c906108c 15034{
5e22e966 15035 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15036 struct attribute *attr;
c906108c 15037 int i;
be2daae6 15038 struct fnfieldlist *flp = nullptr;
c906108c 15039 struct fn_field *fnp;
15d034d0 15040 const char *fieldname;
f792889a 15041 struct type *this_type;
60d5a603 15042 enum dwarf_access_attribute accessibility;
c906108c 15043
b4ba55a1 15044 if (cu->language == language_ada)
a73c6dcd 15045 error (_("unexpected member function in Ada type"));
b4ba55a1 15046
2df3850c 15047 /* Get name of member function. */
39cbfefa
DJ
15048 fieldname = dwarf2_name (die, cu);
15049 if (fieldname == NULL)
2df3850c 15050 return;
c906108c 15051
c906108c 15052 /* Look up member function name in fieldlist. */
be2daae6 15053 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15054 {
27bfe10e 15055 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15056 {
15057 flp = &fip->fnfieldlists[i];
15058 break;
15059 }
c906108c
SS
15060 }
15061
be2daae6
TT
15062 /* Create a new fnfieldlist if necessary. */
15063 if (flp == nullptr)
c906108c 15064 {
be2daae6
TT
15065 fip->fnfieldlists.emplace_back ();
15066 flp = &fip->fnfieldlists.back ();
c906108c 15067 flp->name = fieldname;
be2daae6 15068 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15069 }
15070
be2daae6
TT
15071 /* Create a new member function field and add it to the vector of
15072 fnfieldlists. */
15073 flp->fnfields.emplace_back ();
15074 fnp = &flp->fnfields.back ();
3da10d80
KS
15075
15076 /* Delay processing of the physname until later. */
9c37b5ae 15077 if (cu->language == language_cplus)
be2daae6
TT
15078 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15079 die, cu);
3da10d80
KS
15080 else
15081 {
1d06ead6 15082 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15083 fnp->physname = physname ? physname : "";
15084 }
15085
c906108c 15086 fnp->type = alloc_type (objfile);
f792889a 15087 this_type = read_type_die (die, cu);
78134374 15088 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15089 {
1f704f76 15090 int nparams = this_type->num_fields ();
c906108c 15091
f792889a 15092 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15093 of the method itself (TYPE_CODE_METHOD). */
15094 smash_to_method_type (fnp->type, type,
f792889a 15095 TYPE_TARGET_TYPE (this_type),
80fc5e77 15096 this_type->fields (),
1f704f76 15097 this_type->num_fields (),
f792889a 15098 TYPE_VARARGS (this_type));
c906108c
SS
15099
15100 /* Handle static member functions.
c5aa993b 15101 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15102 member functions. G++ helps GDB by marking the first
15103 parameter for non-static member functions (which is the this
15104 pointer) as artificial. We obtain this information from
15105 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15106 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15107 fnp->voffset = VOFFSET_STATIC;
15108 }
15109 else
b98664d3 15110 complaint (_("member function type missing for '%s'"),
3da10d80 15111 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15112
15113 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15114 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15115 fnp->fcontext = die_containing_type (die, cu);
c906108c 15116
3e43a32a
MS
15117 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15118 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15119
15120 /* Get accessibility. */
e142c38c 15121 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15122 if (attr != nullptr)
aead7601 15123 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15124 else
15125 accessibility = dwarf2_default_access_attribute (die, cu);
15126 switch (accessibility)
c906108c 15127 {
60d5a603
JK
15128 case DW_ACCESS_private:
15129 fnp->is_private = 1;
15130 break;
15131 case DW_ACCESS_protected:
15132 fnp->is_protected = 1;
15133 break;
c906108c
SS
15134 }
15135
b02dede2 15136 /* Check for artificial methods. */
e142c38c 15137 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15138 if (attr && DW_UNSND (attr) != 0)
15139 fnp->is_artificial = 1;
15140
e35000a7
TBA
15141 /* Check for defaulted methods. */
15142 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15143 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15144 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15145
15146 /* Check for deleted methods. */
15147 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15148 if (attr != nullptr && DW_UNSND (attr) != 0)
15149 fnp->is_deleted = 1;
15150
7d27a96d
TT
15151 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15152
0d564a31 15153 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15154 function. For older versions of GCC, this is an offset in the
15155 appropriate virtual table, as specified by DW_AT_containing_type.
15156 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15157 to the object address. */
15158
e142c38c 15159 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15160 if (attr != nullptr)
8e19ed76 15161 {
4fc6c0d5 15162 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15163 {
aec5aa8b
TT
15164 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15165 {
15166 /* Old-style GCC. */
15167 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15168 }
15169 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15170 || (DW_BLOCK (attr)->size > 1
15171 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15172 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15173 {
aec5aa8b
TT
15174 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15175 if ((fnp->voffset % cu->header.addr_size) != 0)
15176 dwarf2_complex_location_expr_complaint ();
15177 else
15178 fnp->voffset /= cu->header.addr_size;
15179 fnp->voffset += 2;
15180 }
15181 else
15182 dwarf2_complex_location_expr_complaint ();
15183
15184 if (!fnp->fcontext)
7e993ebf
KS
15185 {
15186 /* If there is no `this' field and no DW_AT_containing_type,
15187 we cannot actually find a base class context for the
15188 vtable! */
1f704f76 15189 if (this_type->num_fields () == 0
7e993ebf
KS
15190 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15191 {
b98664d3 15192 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15193 "function \"%s\" (offset %s)"),
15194 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15195 }
15196 else
15197 {
15198 fnp->fcontext
15199 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15200 }
15201 }
aec5aa8b 15202 }
cd6c91b4 15203 else if (attr->form_is_section_offset ())
8e19ed76 15204 {
4d3c2250 15205 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15206 }
15207 else
15208 {
4d3c2250
KB
15209 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15210 fieldname);
8e19ed76 15211 }
0d564a31 15212 }
d48cc9dd
DJ
15213 else
15214 {
15215 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15216 if (attr && DW_UNSND (attr))
15217 {
15218 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15219 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15220 "but the vtable offset is not specified"),
9d8780f0 15221 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15222 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15223 TYPE_CPLUS_DYNAMIC (type) = 1;
15224 }
15225 }
c906108c
SS
15226}
15227
15228/* Create the vector of member function fields, and attach it to the type. */
15229
15230static void
fba45db2 15231dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15232 struct dwarf2_cu *cu)
c906108c 15233{
b4ba55a1 15234 if (cu->language == language_ada)
a73c6dcd 15235 error (_("unexpected member functions in Ada type"));
b4ba55a1 15236
c906108c
SS
15237 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15238 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15239 TYPE_ALLOC (type,
15240 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15241
be2daae6 15242 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15243 {
be2daae6 15244 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15245 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15246
be2daae6
TT
15247 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15248 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15249 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15250 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15251
15252 for (int k = 0; k < nf.fnfields.size (); ++k)
15253 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15254 }
15255
be2daae6 15256 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15257}
15258
1168df01
JB
15259/* Returns non-zero if NAME is the name of a vtable member in CU's
15260 language, zero otherwise. */
15261static int
15262is_vtable_name (const char *name, struct dwarf2_cu *cu)
15263{
15264 static const char vptr[] = "_vptr";
15265
9c37b5ae
TT
15266 /* Look for the C++ form of the vtable. */
15267 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15268 return 1;
15269
15270 return 0;
15271}
15272
c0dd20ea 15273/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15274 functions, with the ABI-specified layout. If TYPE describes
15275 such a structure, smash it into a member function type.
61049d3b
DJ
15276
15277 GCC shouldn't do this; it should just output pointer to member DIEs.
15278 This is GCC PR debug/28767. */
c0dd20ea 15279
0b92b5bb
TT
15280static void
15281quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15282{
09e2d7c7 15283 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15284
15285 /* Check for a structure with no name and two children. */
1f704f76 15286 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15287 return;
c0dd20ea
DJ
15288
15289 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15290 if (TYPE_FIELD_NAME (type, 0) == NULL
15291 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15292 || TYPE_FIELD_NAME (type, 1) == NULL
15293 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15294 return;
c0dd20ea
DJ
15295
15296 /* Find the type of the method. */
0b92b5bb 15297 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15298 if (pfn_type == NULL
78134374
SM
15299 || pfn_type->code () != TYPE_CODE_PTR
15300 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15301 return;
c0dd20ea
DJ
15302
15303 /* Look for the "this" argument. */
15304 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15305 if (pfn_type->num_fields () == 0
0b92b5bb 15306 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15307 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15308 return;
c0dd20ea 15309
09e2d7c7 15310 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15311 new_type = alloc_type (objfile);
09e2d7c7 15312 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15313 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15314 TYPE_VARARGS (pfn_type));
0b92b5bb 15315 smash_to_methodptr_type (type, new_type);
c0dd20ea 15316}
1168df01 15317
2b4424c3
TT
15318/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15319 appropriate error checking and issuing complaints if there is a
15320 problem. */
15321
15322static ULONGEST
15323get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15324{
15325 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15326
15327 if (attr == nullptr)
15328 return 0;
15329
cd6c91b4 15330 if (!attr->form_is_constant ())
2b4424c3 15331 {
b98664d3 15332 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15333 " - DIE at %s [in module %s]"),
15334 sect_offset_str (die->sect_off),
5e22e966 15335 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15336 return 0;
15337 }
15338
15339 ULONGEST align;
15340 if (attr->form == DW_FORM_sdata)
15341 {
15342 LONGEST val = DW_SND (attr);
15343 if (val < 0)
15344 {
b98664d3 15345 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15346 " - DIE at %s [in module %s]"),
15347 sect_offset_str (die->sect_off),
5e22e966 15348 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15349 return 0;
15350 }
15351 align = val;
15352 }
15353 else
15354 align = DW_UNSND (attr);
15355
15356 if (align == 0)
15357 {
b98664d3 15358 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15359 " - DIE at %s [in module %s]"),
15360 sect_offset_str (die->sect_off),
5e22e966 15361 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15362 return 0;
15363 }
15364 if ((align & (align - 1)) != 0)
15365 {
b98664d3 15366 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15367 " - DIE at %s [in module %s]"),
15368 sect_offset_str (die->sect_off),
5e22e966 15369 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15370 return 0;
15371 }
15372
15373 return align;
15374}
15375
15376/* If the DIE has a DW_AT_alignment attribute, use its value to set
15377 the alignment for TYPE. */
15378
15379static void
15380maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15381 struct type *type)
15382{
15383 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15384 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15385 " - DIE at %s [in module %s]"),
15386 sect_offset_str (die->sect_off),
5e22e966 15387 objfile_name (cu->per_objfile->objfile));
2b4424c3 15388}
685b1105 15389
e35000a7
TBA
15390/* Check if the given VALUE is a valid enum dwarf_calling_convention
15391 constant for a type, according to DWARF5 spec, Table 5.5. */
15392
15393static bool
15394is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15395{
15396 switch (value)
15397 {
15398 case DW_CC_normal:
15399 case DW_CC_pass_by_reference:
15400 case DW_CC_pass_by_value:
15401 return true;
15402
15403 default:
15404 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15405 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15406 return false;
15407 }
15408}
15409
d0922fcf
TBA
15410/* Check if the given VALUE is a valid enum dwarf_calling_convention
15411 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15412 also according to GNU-specific values (see include/dwarf2.h). */
15413
15414static bool
15415is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15416{
15417 switch (value)
15418 {
15419 case DW_CC_normal:
15420 case DW_CC_program:
15421 case DW_CC_nocall:
15422 return true;
15423
15424 case DW_CC_GNU_renesas_sh:
15425 case DW_CC_GNU_borland_fastcall_i386:
15426 case DW_CC_GDB_IBM_OpenCL:
15427 return true;
15428
15429 default:
15430 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15431 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15432 return false;
15433 }
15434}
15435
c906108c 15436/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15437 (definition) to create a type for the structure or union. Fill in
15438 the type's name and general properties; the members will not be
83655187
DE
15439 processed until process_structure_scope. A symbol table entry for
15440 the type will also not be done until process_structure_scope (assuming
15441 the type has a name).
c906108c 15442
c767944b
DJ
15443 NOTE: we need to call these functions regardless of whether or not the
15444 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15445 structure or union. This gets the type entered into our set of
83655187 15446 user defined types. */
c906108c 15447
f792889a 15448static struct type *
134d01f1 15449read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15450{
5e22e966 15451 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15452 struct type *type;
15453 struct attribute *attr;
15d034d0 15454 const char *name;
c906108c 15455
348e048f
DE
15456 /* If the definition of this type lives in .debug_types, read that type.
15457 Don't follow DW_AT_specification though, that will take us back up
15458 the chain and we want to go down. */
052c8bb8 15459 attr = die->attr (DW_AT_signature);
435d3d88 15460 if (attr != nullptr)
348e048f 15461 {
ac9ec31b 15462 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15463
ac9ec31b 15464 /* The type's CU may not be the same as CU.
02142a6c 15465 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15466 return set_die_type (die, type, cu);
15467 }
15468
c0dd20ea 15469 type = alloc_type (objfile);
c906108c 15470 INIT_CPLUS_SPECIFIC (type);
93311388 15471
39cbfefa
DJ
15472 name = dwarf2_name (die, cu);
15473 if (name != NULL)
c906108c 15474 {
987504bb 15475 if (cu->language == language_cplus
c44af4eb
TT
15476 || cu->language == language_d
15477 || cu->language == language_rust)
63d06c5c 15478 {
15d034d0 15479 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15480
15481 /* dwarf2_full_name might have already finished building the DIE's
15482 type. If so, there is no need to continue. */
15483 if (get_die_type (die, cu) != NULL)
15484 return get_die_type (die, cu);
15485
d0e39ea2 15486 type->set_name (full_name);
63d06c5c
DC
15487 }
15488 else
15489 {
d8151005
DJ
15490 /* The name is already allocated along with this objfile, so
15491 we don't need to duplicate it for the type. */
d0e39ea2 15492 type->set_name (name);
63d06c5c 15493 }
c906108c
SS
15494 }
15495
15496 if (die->tag == DW_TAG_structure_type)
15497 {
67607e24 15498 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15499 }
15500 else if (die->tag == DW_TAG_union_type)
15501 {
67607e24 15502 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15503 }
15504 else
15505 {
67607e24 15506 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15507 }
15508
0cc2414c
TT
15509 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15510 TYPE_DECLARED_CLASS (type) = 1;
15511
e35000a7
TBA
15512 /* Store the calling convention in the type if it's available in
15513 the die. Otherwise the calling convention remains set to
15514 the default value DW_CC_normal. */
15515 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15516 if (attr != nullptr
15517 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15518 {
15519 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15520 TYPE_CPLUS_CALLING_CONVENTION (type)
15521 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15522 }
15523
e142c38c 15524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15525 if (attr != nullptr)
c906108c 15526 {
cd6c91b4 15527 if (attr->form_is_constant ())
155bfbd3
JB
15528 TYPE_LENGTH (type) = DW_UNSND (attr);
15529 else
15530 {
f8e89861 15531 struct dynamic_prop prop;
293e7e51 15532 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15533 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15534 TYPE_LENGTH (type) = 0;
15535 }
c906108c
SS
15536 }
15537 else
15538 {
15539 TYPE_LENGTH (type) = 0;
15540 }
15541
2b4424c3
TT
15542 maybe_set_alignment (cu, die, type);
15543
5230b05a 15544 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15545 {
5230b05a
WT
15546 /* ICC<14 does not output the required DW_AT_declaration on
15547 incomplete types, but gives them a size of zero. */
422b1cb0 15548 TYPE_STUB (type) = 1;
685b1105
JK
15549 }
15550 else
15551 TYPE_STUB_SUPPORTED (type) = 1;
15552
dc718098 15553 if (die_is_declaration (die, cu))
876cecd0 15554 TYPE_STUB (type) = 1;
a6c727b2
DJ
15555 else if (attr == NULL && die->child == NULL
15556 && producer_is_realview (cu->producer))
15557 /* RealView does not output the required DW_AT_declaration
15558 on incomplete types. */
15559 TYPE_STUB (type) = 1;
dc718098 15560
c906108c
SS
15561 /* We need to add the type field to the die immediately so we don't
15562 infinitely recurse when dealing with pointers to the structure
0963b4bd 15563 type within the structure itself. */
1c379e20 15564 set_die_type (die, type, cu);
c906108c 15565
7e314c57
JK
15566 /* set_die_type should be already done. */
15567 set_descriptive_type (type, die, cu);
15568
c767944b
DJ
15569 return type;
15570}
15571
9c6a1327
TT
15572static void handle_struct_member_die
15573 (struct die_info *child_die,
15574 struct type *type,
15575 struct field_info *fi,
15576 std::vector<struct symbol *> *template_args,
15577 struct dwarf2_cu *cu);
15578
15579/* A helper for handle_struct_member_die that handles
15580 DW_TAG_variant_part. */
15581
15582static void
15583handle_variant_part (struct die_info *die, struct type *type,
15584 struct field_info *fi,
15585 std::vector<struct symbol *> *template_args,
15586 struct dwarf2_cu *cu)
15587{
15588 variant_part_builder *new_part;
15589 if (fi->current_variant_part == nullptr)
15590 {
15591 fi->variant_parts.emplace_back ();
15592 new_part = &fi->variant_parts.back ();
15593 }
15594 else if (!fi->current_variant_part->processing_variant)
15595 {
15596 complaint (_("nested DW_TAG_variant_part seen "
15597 "- DIE at %s [in module %s]"),
15598 sect_offset_str (die->sect_off),
5e22e966 15599 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15600 return;
15601 }
15602 else
15603 {
15604 variant_field &current = fi->current_variant_part->variants.back ();
15605 current.variant_parts.emplace_back ();
15606 new_part = &current.variant_parts.back ();
15607 }
15608
15609 /* When we recurse, we want callees to add to this new variant
15610 part. */
15611 scoped_restore save_current_variant_part
15612 = make_scoped_restore (&fi->current_variant_part, new_part);
15613
15614 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15615 if (discr == NULL)
15616 {
15617 /* It's a univariant form, an extension we support. */
15618 }
15619 else if (discr->form_is_ref ())
15620 {
15621 struct dwarf2_cu *target_cu = cu;
15622 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15623
15624 new_part->discriminant_offset = target_die->sect_off;
15625 }
15626 else
15627 {
15628 complaint (_("DW_AT_discr does not have DIE reference form"
15629 " - DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
5e22e966 15631 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15632 }
15633
15634 for (die_info *child_die = die->child;
15635 child_die != NULL;
15636 child_die = child_die->sibling)
15637 handle_struct_member_die (child_die, type, fi, template_args, cu);
15638}
15639
15640/* A helper for handle_struct_member_die that handles
15641 DW_TAG_variant. */
15642
15643static void
15644handle_variant (struct die_info *die, struct type *type,
15645 struct field_info *fi,
15646 std::vector<struct symbol *> *template_args,
15647 struct dwarf2_cu *cu)
15648{
15649 if (fi->current_variant_part == nullptr)
15650 {
15651 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15652 "- DIE at %s [in module %s]"),
15653 sect_offset_str (die->sect_off),
5e22e966 15654 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15655 return;
15656 }
15657 if (fi->current_variant_part->processing_variant)
15658 {
15659 complaint (_("nested DW_TAG_variant seen "
15660 "- DIE at %s [in module %s]"),
15661 sect_offset_str (die->sect_off),
5e22e966 15662 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15663 return;
15664 }
15665
15666 scoped_restore save_processing_variant
15667 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15668 true);
15669
15670 fi->current_variant_part->variants.emplace_back ();
15671 variant_field &variant = fi->current_variant_part->variants.back ();
15672 variant.first_field = fi->fields.size ();
15673
15674 /* In a variant we want to get the discriminant and also add a
15675 field for our sole member child. */
15676 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15677 if (discr == nullptr)
15678 {
15679 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15680 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15681 variant.default_branch = true;
15682 else
15683 variant.discr_list_data = DW_BLOCK (discr);
15684 }
15685 else
15686 variant.discriminant_value = DW_UNSND (discr);
15687
15688 for (die_info *variant_child = die->child;
15689 variant_child != NULL;
15690 variant_child = variant_child->sibling)
15691 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15692
15693 variant.last_field = fi->fields.size ();
15694}
15695
2ddeaf8a
TT
15696/* A helper for process_structure_scope that handles a single member
15697 DIE. */
15698
15699static void
15700handle_struct_member_die (struct die_info *child_die, struct type *type,
15701 struct field_info *fi,
15702 std::vector<struct symbol *> *template_args,
15703 struct dwarf2_cu *cu)
15704{
15705 if (child_die->tag == DW_TAG_member
9c6a1327 15706 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15707 {
15708 /* NOTE: carlton/2002-11-05: A C++ static data member
15709 should be a DW_TAG_member that is a declaration, but
15710 all versions of G++ as of this writing (so through at
15711 least 3.2.1) incorrectly generate DW_TAG_variable
15712 tags for them instead. */
15713 dwarf2_add_field (fi, child_die, cu);
15714 }
15715 else if (child_die->tag == DW_TAG_subprogram)
15716 {
15717 /* Rust doesn't have member functions in the C++ sense.
15718 However, it does emit ordinary functions as children
15719 of a struct DIE. */
15720 if (cu->language == language_rust)
15721 read_func_scope (child_die, cu);
15722 else
15723 {
15724 /* C++ member function. */
15725 dwarf2_add_member_fn (fi, child_die, type, cu);
15726 }
15727 }
15728 else if (child_die->tag == DW_TAG_inheritance)
15729 {
15730 /* C++ base class field. */
15731 dwarf2_add_field (fi, child_die, cu);
15732 }
15733 else if (type_can_define_types (child_die))
15734 dwarf2_add_type_defn (fi, child_die, cu);
15735 else if (child_die->tag == DW_TAG_template_type_param
15736 || child_die->tag == DW_TAG_template_value_param)
15737 {
15738 struct symbol *arg = new_symbol (child_die, NULL, cu);
15739
15740 if (arg != NULL)
15741 template_args->push_back (arg);
15742 }
9c6a1327
TT
15743 else if (child_die->tag == DW_TAG_variant_part)
15744 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15745 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15746 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15747}
15748
c767944b
DJ
15749/* Finish creating a structure or union type, including filling in
15750 its members and creating a symbol for it. */
15751
15752static void
15753process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15754{
5e22e966 15755 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15756 struct die_info *child_die;
c767944b
DJ
15757 struct type *type;
15758
15759 type = get_die_type (die, cu);
15760 if (type == NULL)
15761 type = read_structure_type (die, cu);
15762
3e1d3d8c 15763 bool has_template_parameters = false;
e142c38c 15764 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15765 {
15766 struct field_info fi;
2f4732b0 15767 std::vector<struct symbol *> template_args;
c906108c 15768
639d11d3 15769 child_die = die->child;
c906108c
SS
15770
15771 while (child_die && child_die->tag)
15772 {
2ddeaf8a 15773 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15774 child_die = child_die->sibling;
c906108c
SS
15775 }
15776
34eaf542 15777 /* Attach template arguments to type. */
2f4732b0 15778 if (!template_args.empty ())
34eaf542 15779 {
3e1d3d8c 15780 has_template_parameters = true;
34eaf542 15781 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15782 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15783 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15784 = XOBNEWVEC (&objfile->objfile_obstack,
15785 struct symbol *,
15786 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15787 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15788 template_args.data (),
34eaf542
TT
15789 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15790 * sizeof (struct symbol *)));
34eaf542
TT
15791 }
15792
c906108c 15793 /* Attach fields and member functions to the type. */
317f7127 15794 if (fi.nfields () > 0)
e7c27a73 15795 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15796 if (!fi.fnfieldlists.empty ())
c906108c 15797 {
e7c27a73 15798 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15799
c5aa993b 15800 /* Get the type which refers to the base class (possibly this
c906108c 15801 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15802 class from the DW_AT_containing_type attribute. This use of
15803 DW_AT_containing_type is a GNU extension. */
c906108c 15804
e142c38c 15805 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15806 {
e7c27a73 15807 struct type *t = die_containing_type (die, cu);
c906108c 15808
ae6ae975 15809 set_type_vptr_basetype (type, t);
c906108c
SS
15810 if (type == t)
15811 {
c906108c
SS
15812 int i;
15813
15814 /* Our own class provides vtbl ptr. */
1f704f76 15815 for (i = t->num_fields () - 1;
c906108c
SS
15816 i >= TYPE_N_BASECLASSES (t);
15817 --i)
15818 {
0d5cff50 15819 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15820
1168df01 15821 if (is_vtable_name (fieldname, cu))
c906108c 15822 {
ae6ae975 15823 set_type_vptr_fieldno (type, i);
c906108c
SS
15824 break;
15825 }
15826 }
15827
15828 /* Complain if virtual function table field not found. */
15829 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15830 complaint (_("virtual function table pointer "
3e43a32a 15831 "not found when defining class '%s'"),
7d93a1e0 15832 type->name () ? type->name () : "");
c906108c
SS
15833 }
15834 else
15835 {
ae6ae975 15836 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15837 }
15838 }
f6235d4c 15839 else if (cu->producer
61012eef 15840 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15841 {
15842 /* The IBM XLC compiler does not provide direct indication
15843 of the containing type, but the vtable pointer is
15844 always named __vfp. */
15845
15846 int i;
15847
1f704f76 15848 for (i = type->num_fields () - 1;
f6235d4c
EZ
15849 i >= TYPE_N_BASECLASSES (type);
15850 --i)
15851 {
15852 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15853 {
ae6ae975
DE
15854 set_type_vptr_fieldno (type, i);
15855 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15856 break;
15857 }
15858 }
15859 }
c906108c 15860 }
98751a41
JK
15861
15862 /* Copy fi.typedef_field_list linked list elements content into the
15863 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15864 if (!fi.typedef_field_list.empty ())
98751a41 15865 {
be2daae6 15866 int count = fi.typedef_field_list.size ();
98751a41 15867
a0d7a4ff 15868 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15869 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15870 = ((struct decl_field *)
be2daae6
TT
15871 TYPE_ALLOC (type,
15872 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15873 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15874
be2daae6
TT
15875 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15876 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15877 }
c767944b 15878
883fd55a
KS
15879 /* Copy fi.nested_types_list linked list elements content into the
15880 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15881 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15882 {
be2daae6 15883 int count = fi.nested_types_list.size ();
883fd55a
KS
15884
15885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15886 TYPE_NESTED_TYPES_ARRAY (type)
15887 = ((struct decl_field *)
be2daae6
TT
15888 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15889 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15890
be2daae6
TT
15891 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15892 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15893 }
c906108c 15894 }
63d06c5c 15895
bb5ed363 15896 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15897 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15898 cu->rust_unions.push_back (type);
0b92b5bb 15899
90aeadfc
DC
15900 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15901 snapshots) has been known to create a die giving a declaration
15902 for a class that has, as a child, a die giving a definition for a
15903 nested class. So we have to process our children even if the
15904 current die is a declaration. Normally, of course, a declaration
15905 won't have any children at all. */
134d01f1 15906
ca040673
DE
15907 child_die = die->child;
15908
90aeadfc
DC
15909 while (child_die != NULL && child_die->tag)
15910 {
15911 if (child_die->tag == DW_TAG_member
15912 || child_die->tag == DW_TAG_variable
34eaf542
TT
15913 || child_die->tag == DW_TAG_inheritance
15914 || child_die->tag == DW_TAG_template_value_param
15915 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15916 {
90aeadfc 15917 /* Do nothing. */
134d01f1 15918 }
90aeadfc
DC
15919 else
15920 process_die (child_die, cu);
134d01f1 15921
436c571c 15922 child_die = child_die->sibling;
134d01f1
DJ
15923 }
15924
fa4028e9
JB
15925 /* Do not consider external references. According to the DWARF standard,
15926 these DIEs are identified by the fact that they have no byte_size
15927 attribute, and a declaration attribute. */
15928 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15929 || !die_is_declaration (die, cu)
15930 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15931 {
15932 struct symbol *sym = new_symbol (die, type, cu);
15933
15934 if (has_template_parameters)
15935 {
a776957c
TT
15936 struct symtab *symtab;
15937 if (sym != nullptr)
15938 symtab = symbol_symtab (sym);
15939 else if (cu->line_header != nullptr)
15940 {
15941 /* Any related symtab will do. */
15942 symtab
7ba99d21 15943 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15944 }
15945 else
15946 {
15947 symtab = nullptr;
15948 complaint (_("could not find suitable "
15949 "symtab for template parameter"
15950 " - DIE at %s [in module %s]"),
15951 sect_offset_str (die->sect_off),
15952 objfile_name (objfile));
15953 }
15954
15955 if (symtab != nullptr)
15956 {
15957 /* Make sure that the symtab is set on the new symbols.
15958 Even though they don't appear in this symtab directly,
15959 other parts of gdb assume that symbols do, and this is
15960 reasonably true. */
15961 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15962 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15963 }
3e1d3d8c
TT
15964 }
15965 }
134d01f1
DJ
15966}
15967
ed6acedd
TT
15968/* Assuming DIE is an enumeration type, and TYPE is its associated
15969 type, update TYPE using some information only available in DIE's
15970 children. In particular, the fields are computed. */
55426c9d
JB
15971
15972static void
15973update_enumeration_type_from_children (struct die_info *die,
15974 struct type *type,
15975 struct dwarf2_cu *cu)
15976{
60f7655a 15977 struct die_info *child_die;
55426c9d
JB
15978 int unsigned_enum = 1;
15979 int flag_enum = 1;
55426c9d 15980
8268c778 15981 auto_obstack obstack;
ed6acedd 15982 std::vector<struct field> fields;
55426c9d 15983
60f7655a
DE
15984 for (child_die = die->child;
15985 child_die != NULL && child_die->tag;
436c571c 15986 child_die = child_die->sibling)
55426c9d
JB
15987 {
15988 struct attribute *attr;
15989 LONGEST value;
15990 const gdb_byte *bytes;
15991 struct dwarf2_locexpr_baton *baton;
15992 const char *name;
60f7655a 15993
55426c9d
JB
15994 if (child_die->tag != DW_TAG_enumerator)
15995 continue;
15996
15997 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15998 if (attr == NULL)
15999 continue;
16000
16001 name = dwarf2_name (child_die, cu);
16002 if (name == NULL)
16003 name = "<anonymous enumerator>";
16004
16005 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16006 &value, &bytes, &baton);
16007 if (value < 0)
16008 {
16009 unsigned_enum = 0;
16010 flag_enum = 0;
16011 }
55426c9d 16012 else
edd45eb0
SM
16013 {
16014 if (count_one_bits_ll (value) >= 2)
16015 flag_enum = 0;
edd45eb0 16016 }
55426c9d 16017
ed6acedd
TT
16018 fields.emplace_back ();
16019 struct field &field = fields.back ();
16020 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16021 SET_FIELD_ENUMVAL (field, value);
16022 }
16023
16024 if (!fields.empty ())
16025 {
5e33d5f4 16026 type->set_num_fields (fields.size ());
3cabb6b0
SM
16027 type->set_fields
16028 ((struct field *)
16029 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16030 memcpy (type->fields (), fields.data (),
ed6acedd 16031 sizeof (struct field) * fields.size ());
55426c9d
JB
16032 }
16033
16034 if (unsigned_enum)
16035 TYPE_UNSIGNED (type) = 1;
16036 if (flag_enum)
16037 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16038}
16039
134d01f1
DJ
16040/* Given a DW_AT_enumeration_type die, set its type. We do not
16041 complete the type's fields yet, or create any symbols. */
c906108c 16042
f792889a 16043static struct type *
134d01f1 16044read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16045{
5e22e966 16046 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16047 struct type *type;
c906108c 16048 struct attribute *attr;
0114d602 16049 const char *name;
134d01f1 16050
348e048f
DE
16051 /* If the definition of this type lives in .debug_types, read that type.
16052 Don't follow DW_AT_specification though, that will take us back up
16053 the chain and we want to go down. */
052c8bb8 16054 attr = die->attr (DW_AT_signature);
435d3d88 16055 if (attr != nullptr)
348e048f 16056 {
ac9ec31b 16057 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16058
ac9ec31b 16059 /* The type's CU may not be the same as CU.
02142a6c 16060 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16061 return set_die_type (die, type, cu);
16062 }
16063
c906108c
SS
16064 type = alloc_type (objfile);
16065
67607e24 16066 type->set_code (TYPE_CODE_ENUM);
94af9270 16067 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16068 if (name != NULL)
d0e39ea2 16069 type->set_name (name);
c906108c 16070
0626fc76
TT
16071 attr = dwarf2_attr (die, DW_AT_type, cu);
16072 if (attr != NULL)
16073 {
16074 struct type *underlying_type = die_type (die, cu);
16075
16076 TYPE_TARGET_TYPE (type) = underlying_type;
16077 }
16078
e142c38c 16079 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16080 if (attr != nullptr)
c906108c
SS
16081 {
16082 TYPE_LENGTH (type) = DW_UNSND (attr);
16083 }
16084 else
16085 {
16086 TYPE_LENGTH (type) = 0;
16087 }
16088
2b4424c3
TT
16089 maybe_set_alignment (cu, die, type);
16090
137033e9
JB
16091 /* The enumeration DIE can be incomplete. In Ada, any type can be
16092 declared as private in the package spec, and then defined only
16093 inside the package body. Such types are known as Taft Amendment
16094 Types. When another package uses such a type, an incomplete DIE
16095 may be generated by the compiler. */
02eb380e 16096 if (die_is_declaration (die, cu))
876cecd0 16097 TYPE_STUB (type) = 1;
02eb380e 16098
0626fc76
TT
16099 /* If this type has an underlying type that is not a stub, then we
16100 may use its attributes. We always use the "unsigned" attribute
16101 in this situation, because ordinarily we guess whether the type
16102 is unsigned -- but the guess can be wrong and the underlying type
16103 can tell us the reality. However, we defer to a local size
16104 attribute if one exists, because this lets the compiler override
16105 the underlying type if needed. */
16106 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16107 {
9e7c9a03
HD
16108 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16109 underlying_type = check_typedef (underlying_type);
16110 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16111 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16112 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16113 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16114 && TYPE_RAW_ALIGN (underlying_type) != 0)
16115 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16116 }
16117
3d567982
TT
16118 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16119
ed6acedd
TT
16120 set_die_type (die, type, cu);
16121
16122 /* Finish the creation of this type by using the enum's children.
16123 Note that, as usual, this must come after set_die_type to avoid
16124 infinite recursion when trying to compute the names of the
16125 enumerators. */
16126 update_enumeration_type_from_children (die, type, cu);
16127
16128 return type;
134d01f1
DJ
16129}
16130
16131/* Given a pointer to a die which begins an enumeration, process all
16132 the dies that define the members of the enumeration, and create the
16133 symbol for the enumeration type.
16134
16135 NOTE: We reverse the order of the element list. */
16136
16137static void
16138process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16139{
f792889a 16140 struct type *this_type;
134d01f1 16141
f792889a
DJ
16142 this_type = get_die_type (die, cu);
16143 if (this_type == NULL)
16144 this_type = read_enumeration_type (die, cu);
9dc481d3 16145
639d11d3 16146 if (die->child != NULL)
c906108c 16147 {
9dc481d3 16148 struct die_info *child_die;
15d034d0 16149 const char *name;
9dc481d3 16150
639d11d3 16151 child_die = die->child;
c906108c
SS
16152 while (child_die && child_die->tag)
16153 {
16154 if (child_die->tag != DW_TAG_enumerator)
16155 {
e7c27a73 16156 process_die (child_die, cu);
c906108c
SS
16157 }
16158 else
16159 {
39cbfefa
DJ
16160 name = dwarf2_name (child_die, cu);
16161 if (name)
ed6acedd 16162 new_symbol (child_die, this_type, cu);
c906108c
SS
16163 }
16164
436c571c 16165 child_die = child_die->sibling;
c906108c 16166 }
c906108c 16167 }
134d01f1 16168
6c83ed52
TT
16169 /* If we are reading an enum from a .debug_types unit, and the enum
16170 is a declaration, and the enum is not the signatured type in the
16171 unit, then we do not want to add a symbol for it. Adding a
16172 symbol would in some cases obscure the true definition of the
16173 enum, giving users an incomplete type when the definition is
16174 actually available. Note that we do not want to do this for all
16175 enums which are just declarations, because C++0x allows forward
16176 enum declarations. */
3019eac3 16177 if (cu->per_cu->is_debug_types
6c83ed52
TT
16178 && die_is_declaration (die, cu))
16179 {
52dc124a 16180 struct signatured_type *sig_type;
6c83ed52 16181
c0f78cd4 16182 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16183 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16184 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16185 return;
16186 }
16187
f792889a 16188 new_symbol (die, this_type, cu);
c906108c
SS
16189}
16190
16191/* Extract all information from a DW_TAG_array_type DIE and put it in
16192 the DIE's type field. For now, this only handles one dimensional
16193 arrays. */
16194
f792889a 16195static struct type *
e7c27a73 16196read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16197{
5e22e966 16198 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16199 struct die_info *child_die;
7e314c57 16200 struct type *type;
c906108c 16201 struct type *element_type, *range_type, *index_type;
c906108c 16202 struct attribute *attr;
15d034d0 16203 const char *name;
a405673c 16204 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16205 unsigned int bit_stride = 0;
c906108c 16206
e7c27a73 16207 element_type = die_type (die, cu);
c906108c 16208
7e314c57
JK
16209 /* The die_type call above may have already set the type for this DIE. */
16210 type = get_die_type (die, cu);
16211 if (type)
16212 return type;
16213
dc53a7ad
JB
16214 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16215 if (attr != NULL)
a405673c
JB
16216 {
16217 int stride_ok;
293e7e51 16218 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16219
16220 byte_stride_prop
16221 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16222 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16223 prop_type);
a405673c
JB
16224 if (!stride_ok)
16225 {
b98664d3 16226 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16227 " - DIE at %s [in module %s]"),
16228 sect_offset_str (die->sect_off),
5e22e966 16229 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16230 /* Ignore this attribute. We will likely not be able to print
16231 arrays of this type correctly, but there is little we can do
16232 to help if we cannot read the attribute's value. */
16233 byte_stride_prop = NULL;
16234 }
16235 }
dc53a7ad
JB
16236
16237 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16238 if (attr != NULL)
16239 bit_stride = DW_UNSND (attr);
16240
c906108c
SS
16241 /* Irix 6.2 native cc creates array types without children for
16242 arrays with unspecified length. */
639d11d3 16243 if (die->child == NULL)
c906108c 16244 {
46bf5051 16245 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16246 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16247 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16248 byte_stride_prop, bit_stride);
f792889a 16249 return set_die_type (die, type, cu);
c906108c
SS
16250 }
16251
791afaa2 16252 std::vector<struct type *> range_types;
639d11d3 16253 child_die = die->child;
c906108c
SS
16254 while (child_die && child_die->tag)
16255 {
16256 if (child_die->tag == DW_TAG_subrange_type)
16257 {
f792889a 16258 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16259
f792889a 16260 if (child_type != NULL)
a02abb62 16261 {
0963b4bd
MS
16262 /* The range type was succesfully read. Save it for the
16263 array type creation. */
791afaa2 16264 range_types.push_back (child_type);
a02abb62 16265 }
c906108c 16266 }
436c571c 16267 child_die = child_die->sibling;
c906108c
SS
16268 }
16269
16270 /* Dwarf2 dimensions are output from left to right, create the
16271 necessary array types in backwards order. */
7ca2d3a3 16272
c906108c 16273 type = element_type;
7ca2d3a3
DL
16274
16275 if (read_array_order (die, cu) == DW_ORD_col_major)
16276 {
16277 int i = 0;
9a619af0 16278
791afaa2 16279 while (i < range_types.size ())
dc53a7ad 16280 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16281 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16282 }
16283 else
16284 {
791afaa2 16285 size_t ndim = range_types.size ();
7ca2d3a3 16286 while (ndim-- > 0)
dc53a7ad 16287 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16288 byte_stride_prop, bit_stride);
7ca2d3a3 16289 }
c906108c 16290
f5f8a009
EZ
16291 /* Understand Dwarf2 support for vector types (like they occur on
16292 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16293 array type. This is not part of the Dwarf2/3 standard yet, but a
16294 custom vendor extension. The main difference between a regular
16295 array and the vector variant is that vectors are passed by value
16296 to functions. */
e142c38c 16297 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16298 if (attr != nullptr)
ea37ba09 16299 make_vector_type (type);
f5f8a009 16300
dbc98a8b
KW
16301 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16302 implementation may choose to implement triple vectors using this
16303 attribute. */
16304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16305 if (attr != nullptr)
dbc98a8b
KW
16306 {
16307 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16308 TYPE_LENGTH (type) = DW_UNSND (attr);
16309 else
b98664d3 16310 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16311 "than the total size of elements"));
dbc98a8b
KW
16312 }
16313
39cbfefa
DJ
16314 name = dwarf2_name (die, cu);
16315 if (name)
d0e39ea2 16316 type->set_name (name);
6e70227d 16317
2b4424c3
TT
16318 maybe_set_alignment (cu, die, type);
16319
0963b4bd 16320 /* Install the type in the die. */
7e314c57
JK
16321 set_die_type (die, type, cu);
16322
16323 /* set_die_type should be already done. */
b4ba55a1
JB
16324 set_descriptive_type (type, die, cu);
16325
7e314c57 16326 return type;
c906108c
SS
16327}
16328
7ca2d3a3 16329static enum dwarf_array_dim_ordering
6e70227d 16330read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16331{
16332 struct attribute *attr;
16333
16334 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16335
435d3d88 16336 if (attr != nullptr)
aead7601 16337 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16338
0963b4bd
MS
16339 /* GNU F77 is a special case, as at 08/2004 array type info is the
16340 opposite order to the dwarf2 specification, but data is still
16341 laid out as per normal fortran.
7ca2d3a3 16342
0963b4bd
MS
16343 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16344 version checking. */
7ca2d3a3 16345
905e0470
PM
16346 if (cu->language == language_fortran
16347 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16348 {
16349 return DW_ORD_row_major;
16350 }
16351
6e70227d 16352 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16353 {
16354 case array_column_major:
16355 return DW_ORD_col_major;
16356 case array_row_major:
16357 default:
16358 return DW_ORD_row_major;
16359 };
16360}
16361
72019c9c 16362/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16363 the DIE's type field. */
72019c9c 16364
f792889a 16365static struct type *
72019c9c
GM
16366read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16367{
7e314c57
JK
16368 struct type *domain_type, *set_type;
16369 struct attribute *attr;
f792889a 16370
7e314c57
JK
16371 domain_type = die_type (die, cu);
16372
16373 /* The die_type call above may have already set the type for this DIE. */
16374 set_type = get_die_type (die, cu);
16375 if (set_type)
16376 return set_type;
16377
16378 set_type = create_set_type (NULL, domain_type);
16379
16380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16381 if (attr != nullptr)
d09039dd 16382 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16383
2b4424c3
TT
16384 maybe_set_alignment (cu, die, set_type);
16385
f792889a 16386 return set_die_type (die, set_type, cu);
72019c9c 16387}
7ca2d3a3 16388
0971de02
TT
16389/* A helper for read_common_block that creates a locexpr baton.
16390 SYM is the symbol which we are marking as computed.
16391 COMMON_DIE is the DIE for the common block.
16392 COMMON_LOC is the location expression attribute for the common
16393 block itself.
16394 MEMBER_LOC is the location expression attribute for the particular
16395 member of the common block that we are processing.
16396 CU is the CU from which the above come. */
16397
16398static void
16399mark_common_block_symbol_computed (struct symbol *sym,
16400 struct die_info *common_die,
16401 struct attribute *common_loc,
16402 struct attribute *member_loc,
16403 struct dwarf2_cu *cu)
16404{
5e22e966 16405 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16406 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16407 struct dwarf2_locexpr_baton *baton;
16408 gdb_byte *ptr;
16409 unsigned int cu_off;
08feed99 16410 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16411 LONGEST offset = 0;
16412
16413 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16414 gdb_assert (common_loc->form_is_block ());
16415 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16416 || member_loc->form_is_constant ());
0971de02 16417
8d749320 16418 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16419 baton->per_objfile = per_objfile;
0971de02
TT
16420 baton->per_cu = cu->per_cu;
16421 gdb_assert (baton->per_cu);
16422
16423 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16424
cd6c91b4 16425 if (member_loc->form_is_constant ())
0971de02 16426 {
0826b30a 16427 offset = member_loc->constant_value (0);
0971de02
TT
16428 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16429 }
16430 else
16431 baton->size += DW_BLOCK (member_loc)->size;
16432
224c3ddb 16433 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16434 baton->data = ptr;
16435
16436 *ptr++ = DW_OP_call4;
9c541725 16437 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16438 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16439 ptr += 4;
16440
cd6c91b4 16441 if (member_loc->form_is_constant ())
0971de02
TT
16442 {
16443 *ptr++ = DW_OP_addr;
16444 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16445 ptr += cu->header.addr_size;
16446 }
16447 else
16448 {
16449 /* We have to copy the data here, because DW_OP_call4 will only
16450 use a DW_AT_location attribute. */
16451 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16452 ptr += DW_BLOCK (member_loc)->size;
16453 }
16454
16455 *ptr++ = DW_OP_plus;
16456 gdb_assert (ptr - baton->data == baton->size);
16457
0971de02 16458 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16459 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16460}
16461
4357ac6c
TT
16462/* Create appropriate locally-scoped variables for all the
16463 DW_TAG_common_block entries. Also create a struct common_block
16464 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16465 is used to separate the common blocks name namespace from regular
4357ac6c 16466 variable names. */
c906108c
SS
16467
16468static void
e7c27a73 16469read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16470{
0971de02
TT
16471 struct attribute *attr;
16472
16473 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16474 if (attr != nullptr)
0971de02
TT
16475 {
16476 /* Support the .debug_loc offsets. */
4fc6c0d5 16477 if (attr->form_is_block ())
0971de02
TT
16478 {
16479 /* Ok. */
16480 }
cd6c91b4 16481 else if (attr->form_is_section_offset ())
0971de02
TT
16482 {
16483 dwarf2_complex_location_expr_complaint ();
16484 attr = NULL;
16485 }
16486 else
16487 {
16488 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16489 "common block member");
16490 attr = NULL;
16491 }
16492 }
16493
639d11d3 16494 if (die->child != NULL)
c906108c 16495 {
5e22e966 16496 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16497 struct die_info *child_die;
16498 size_t n_entries = 0, size;
16499 struct common_block *common_block;
16500 struct symbol *sym;
74ac6d43 16501
4357ac6c
TT
16502 for (child_die = die->child;
16503 child_die && child_die->tag;
436c571c 16504 child_die = child_die->sibling)
4357ac6c
TT
16505 ++n_entries;
16506
16507 size = (sizeof (struct common_block)
16508 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16509 common_block
16510 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16511 size);
4357ac6c
TT
16512 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16513 common_block->n_entries = 0;
16514
16515 for (child_die = die->child;
16516 child_die && child_die->tag;
436c571c 16517 child_die = child_die->sibling)
4357ac6c
TT
16518 {
16519 /* Create the symbol in the DW_TAG_common_block block in the current
16520 symbol scope. */
e7c27a73 16521 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16522 if (sym != NULL)
16523 {
16524 struct attribute *member_loc;
16525
16526 common_block->contents[common_block->n_entries++] = sym;
16527
16528 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16529 cu);
16530 if (member_loc)
16531 {
16532 /* GDB has handled this for a long time, but it is
16533 not specified by DWARF. It seems to have been
16534 emitted by gfortran at least as recently as:
16535 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16536 complaint (_("Variable in common block has "
0971de02 16537 "DW_AT_data_member_location "
9d8780f0
SM
16538 "- DIE at %s [in module %s]"),
16539 sect_offset_str (child_die->sect_off),
518817b3 16540 objfile_name (objfile));
0971de02 16541
cd6c91b4 16542 if (member_loc->form_is_section_offset ())
0971de02 16543 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16544 else if (member_loc->form_is_constant ()
4fc6c0d5 16545 || member_loc->form_is_block ())
0971de02 16546 {
435d3d88 16547 if (attr != nullptr)
0971de02
TT
16548 mark_common_block_symbol_computed (sym, die, attr,
16549 member_loc, cu);
16550 }
16551 else
16552 dwarf2_complex_location_expr_complaint ();
16553 }
16554 }
c906108c 16555 }
4357ac6c
TT
16556
16557 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16558 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16559 }
16560}
16561
0114d602 16562/* Create a type for a C++ namespace. */
d9fa45fe 16563
0114d602
DJ
16564static struct type *
16565read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16566{
5e22e966 16567 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16568 const char *previous_prefix, *name;
9219021c 16569 int is_anonymous;
0114d602
DJ
16570 struct type *type;
16571
16572 /* For extensions, reuse the type of the original namespace. */
16573 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16574 {
16575 struct die_info *ext_die;
16576 struct dwarf2_cu *ext_cu = cu;
9a619af0 16577
0114d602
DJ
16578 ext_die = dwarf2_extension (die, &ext_cu);
16579 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16580
16581 /* EXT_CU may not be the same as CU.
02142a6c 16582 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16583 return set_die_type (die, type, cu);
16584 }
9219021c 16585
e142c38c 16586 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16587
16588 /* Now build the name of the current namespace. */
16589
0114d602
DJ
16590 previous_prefix = determine_prefix (die, cu);
16591 if (previous_prefix[0] != '\0')
16592 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16593 previous_prefix, name, 0, cu);
0114d602
DJ
16594
16595 /* Create the type. */
19f392bc 16596 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16597
60531b24 16598 return set_die_type (die, type, cu);
0114d602
DJ
16599}
16600
22cee43f 16601/* Read a namespace scope. */
0114d602
DJ
16602
16603static void
16604read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16605{
5e22e966 16606 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16607 int is_anonymous;
9219021c 16608
5c4e30ca
DC
16609 /* Add a symbol associated to this if we haven't seen the namespace
16610 before. Also, add a using directive if it's an anonymous
16611 namespace. */
9219021c 16612
f2f0e013 16613 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16614 {
16615 struct type *type;
16616
0114d602 16617 type = read_type_die (die, cu);
e7c27a73 16618 new_symbol (die, type, cu);
5c4e30ca 16619
e8e80198 16620 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16621 if (is_anonymous)
0114d602
DJ
16622 {
16623 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16624
eb1e02fd 16625 std::vector<const char *> excludes;
804d2729 16626 add_using_directive (using_directives (cu),
7d93a1e0 16627 previous_prefix, type->name (), NULL,
eb1e02fd 16628 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16629 }
5c4e30ca 16630 }
9219021c 16631
639d11d3 16632 if (die->child != NULL)
d9fa45fe 16633 {
639d11d3 16634 struct die_info *child_die = die->child;
6e70227d 16635
d9fa45fe
DC
16636 while (child_die && child_die->tag)
16637 {
e7c27a73 16638 process_die (child_die, cu);
436c571c 16639 child_die = child_die->sibling;
d9fa45fe
DC
16640 }
16641 }
38d518c9
EZ
16642}
16643
f55ee35c
JK
16644/* Read a Fortran module as type. This DIE can be only a declaration used for
16645 imported module. Still we need that type as local Fortran "use ... only"
16646 declaration imports depend on the created type in determine_prefix. */
16647
16648static struct type *
16649read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16650{
5e22e966 16651 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16652 const char *module_name;
f55ee35c
JK
16653 struct type *type;
16654
16655 module_name = dwarf2_name (die, cu);
19f392bc 16656 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16657
f55ee35c
JK
16658 return set_die_type (die, type, cu);
16659}
16660
5d7cb8df
JK
16661/* Read a Fortran module. */
16662
16663static void
16664read_module (struct die_info *die, struct dwarf2_cu *cu)
16665{
16666 struct die_info *child_die = die->child;
530e8392
KB
16667 struct type *type;
16668
16669 type = read_type_die (die, cu);
16670 new_symbol (die, type, cu);
5d7cb8df 16671
5d7cb8df
JK
16672 while (child_die && child_die->tag)
16673 {
16674 process_die (child_die, cu);
436c571c 16675 child_die = child_die->sibling;
5d7cb8df
JK
16676 }
16677}
16678
38d518c9
EZ
16679/* Return the name of the namespace represented by DIE. Set
16680 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16681 namespace. */
16682
16683static const char *
e142c38c 16684namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16685{
16686 struct die_info *current_die;
16687 const char *name = NULL;
16688
16689 /* Loop through the extensions until we find a name. */
16690
16691 for (current_die = die;
16692 current_die != NULL;
f2f0e013 16693 current_die = dwarf2_extension (die, &cu))
38d518c9 16694 {
96553a0c
DE
16695 /* We don't use dwarf2_name here so that we can detect the absence
16696 of a name -> anonymous namespace. */
7d45c7c3 16697 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16698
38d518c9
EZ
16699 if (name != NULL)
16700 break;
16701 }
16702
16703 /* Is it an anonymous namespace? */
16704
16705 *is_anonymous = (name == NULL);
16706 if (*is_anonymous)
2b1dbab0 16707 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16708
16709 return name;
d9fa45fe
DC
16710}
16711
c906108c
SS
16712/* Extract all information from a DW_TAG_pointer_type DIE and add to
16713 the user defined type vector. */
16714
f792889a 16715static struct type *
e7c27a73 16716read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16717{
5e22e966 16718 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16719 struct comp_unit_head *cu_header = &cu->header;
c906108c 16720 struct type *type;
8b2dbe47
KB
16721 struct attribute *attr_byte_size;
16722 struct attribute *attr_address_class;
16723 int byte_size, addr_class;
7e314c57
JK
16724 struct type *target_type;
16725
16726 target_type = die_type (die, cu);
c906108c 16727
7e314c57
JK
16728 /* The die_type call above may have already set the type for this DIE. */
16729 type = get_die_type (die, cu);
16730 if (type)
16731 return type;
16732
16733 type = lookup_pointer_type (target_type);
8b2dbe47 16734
e142c38c 16735 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16736 if (attr_byte_size)
16737 byte_size = DW_UNSND (attr_byte_size);
c906108c 16738 else
8b2dbe47
KB
16739 byte_size = cu_header->addr_size;
16740
e142c38c 16741 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16742 if (attr_address_class)
16743 addr_class = DW_UNSND (attr_address_class);
16744 else
16745 addr_class = DW_ADDR_none;
16746
2b4424c3
TT
16747 ULONGEST alignment = get_alignment (cu, die);
16748
16749 /* If the pointer size, alignment, or address class is different
16750 than the default, create a type variant marked as such and set
16751 the length accordingly. */
16752 if (TYPE_LENGTH (type) != byte_size
16753 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16754 && alignment != TYPE_RAW_ALIGN (type))
16755 || addr_class != DW_ADDR_none)
c906108c 16756 {
5e2b427d 16757 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16758 {
16759 int type_flags;
16760
849957d9 16761 type_flags = gdbarch_address_class_type_flags
5e2b427d 16762 (gdbarch, byte_size, addr_class);
876cecd0
TT
16763 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16764 == 0);
8b2dbe47
KB
16765 type = make_type_with_address_space (type, type_flags);
16766 }
16767 else if (TYPE_LENGTH (type) != byte_size)
16768 {
b98664d3 16769 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16770 }
2b4424c3
TT
16771 else if (TYPE_RAW_ALIGN (type) != alignment)
16772 {
b98664d3 16773 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16774 " - DIE at %s [in module %s]"),
16775 sect_offset_str (die->sect_off),
5e22e966 16776 objfile_name (cu->per_objfile->objfile));
2b4424c3 16777 }
6e70227d 16778 else
9a619af0
MS
16779 {
16780 /* Should we also complain about unhandled address classes? */
16781 }
c906108c 16782 }
8b2dbe47
KB
16783
16784 TYPE_LENGTH (type) = byte_size;
2b4424c3 16785 set_type_align (type, alignment);
f792889a 16786 return set_die_type (die, type, cu);
c906108c
SS
16787}
16788
16789/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16790 the user defined type vector. */
16791
f792889a 16792static struct type *
e7c27a73 16793read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16794{
16795 struct type *type;
16796 struct type *to_type;
16797 struct type *domain;
16798
e7c27a73
DJ
16799 to_type = die_type (die, cu);
16800 domain = die_containing_type (die, cu);
0d5de010 16801
7e314c57
JK
16802 /* The calls above may have already set the type for this DIE. */
16803 type = get_die_type (die, cu);
16804 if (type)
16805 return type;
16806
78134374 16807 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16808 type = lookup_methodptr_type (to_type);
78134374 16809 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16810 {
5e22e966 16811 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16812
16813 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16814 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16815 TYPE_VARARGS (to_type));
16816 type = lookup_methodptr_type (new_type);
16817 }
0d5de010
DJ
16818 else
16819 type = lookup_memberptr_type (to_type, domain);
c906108c 16820
f792889a 16821 return set_die_type (die, type, cu);
c906108c
SS
16822}
16823
4297a3f0 16824/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16825 the user defined type vector. */
16826
f792889a 16827static struct type *
4297a3f0
AV
16828read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16829 enum type_code refcode)
c906108c 16830{
e7c27a73 16831 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16832 struct type *type, *target_type;
c906108c
SS
16833 struct attribute *attr;
16834
4297a3f0
AV
16835 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16836
7e314c57
JK
16837 target_type = die_type (die, cu);
16838
16839 /* The die_type call above may have already set the type for this DIE. */
16840 type = get_die_type (die, cu);
16841 if (type)
16842 return type;
16843
4297a3f0 16844 type = lookup_reference_type (target_type, refcode);
e142c38c 16845 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16846 if (attr != nullptr)
c906108c
SS
16847 {
16848 TYPE_LENGTH (type) = DW_UNSND (attr);
16849 }
16850 else
16851 {
107d2387 16852 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16853 }
2b4424c3 16854 maybe_set_alignment (cu, die, type);
f792889a 16855 return set_die_type (die, type, cu);
c906108c
SS
16856}
16857
cf363f18
MW
16858/* Add the given cv-qualifiers to the element type of the array. GCC
16859 outputs DWARF type qualifiers that apply to an array, not the
16860 element type. But GDB relies on the array element type to carry
16861 the cv-qualifiers. This mimics section 6.7.3 of the C99
16862 specification. */
16863
16864static struct type *
16865add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16866 struct type *base_type, int cnst, int voltl)
16867{
16868 struct type *el_type, *inner_array;
16869
16870 base_type = copy_type (base_type);
16871 inner_array = base_type;
16872
78134374 16873 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16874 {
16875 TYPE_TARGET_TYPE (inner_array) =
16876 copy_type (TYPE_TARGET_TYPE (inner_array));
16877 inner_array = TYPE_TARGET_TYPE (inner_array);
16878 }
16879
16880 el_type = TYPE_TARGET_TYPE (inner_array);
16881 cnst |= TYPE_CONST (el_type);
16882 voltl |= TYPE_VOLATILE (el_type);
16883 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16884
16885 return set_die_type (die, base_type, cu);
16886}
16887
f792889a 16888static struct type *
e7c27a73 16889read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16890{
f792889a 16891 struct type *base_type, *cv_type;
c906108c 16892
e7c27a73 16893 base_type = die_type (die, cu);
7e314c57
JK
16894
16895 /* The die_type call above may have already set the type for this DIE. */
16896 cv_type = get_die_type (die, cu);
16897 if (cv_type)
16898 return cv_type;
16899
2f608a3a
KW
16900 /* In case the const qualifier is applied to an array type, the element type
16901 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16902 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16903 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16904
f792889a
DJ
16905 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16906 return set_die_type (die, cv_type, cu);
c906108c
SS
16907}
16908
f792889a 16909static struct type *
e7c27a73 16910read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16911{
f792889a 16912 struct type *base_type, *cv_type;
c906108c 16913
e7c27a73 16914 base_type = die_type (die, cu);
7e314c57
JK
16915
16916 /* The die_type call above may have already set the type for this DIE. */
16917 cv_type = get_die_type (die, cu);
16918 if (cv_type)
16919 return cv_type;
16920
cf363f18
MW
16921 /* In case the volatile qualifier is applied to an array type, the
16922 element type is so qualified, not the array type (section 6.7.3
16923 of C99). */
78134374 16924 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16925 return add_array_cv_type (die, cu, base_type, 0, 1);
16926
f792889a
DJ
16927 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16928 return set_die_type (die, cv_type, cu);
c906108c
SS
16929}
16930
06d66ee9
TT
16931/* Handle DW_TAG_restrict_type. */
16932
16933static struct type *
16934read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16935{
16936 struct type *base_type, *cv_type;
16937
16938 base_type = die_type (die, cu);
16939
16940 /* The die_type call above may have already set the type for this DIE. */
16941 cv_type = get_die_type (die, cu);
16942 if (cv_type)
16943 return cv_type;
16944
16945 cv_type = make_restrict_type (base_type);
16946 return set_die_type (die, cv_type, cu);
16947}
16948
a2c2acaf
MW
16949/* Handle DW_TAG_atomic_type. */
16950
16951static struct type *
16952read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16953{
16954 struct type *base_type, *cv_type;
16955
16956 base_type = die_type (die, cu);
16957
16958 /* The die_type call above may have already set the type for this DIE. */
16959 cv_type = get_die_type (die, cu);
16960 if (cv_type)
16961 return cv_type;
16962
16963 cv_type = make_atomic_type (base_type);
16964 return set_die_type (die, cv_type, cu);
16965}
16966
c906108c
SS
16967/* Extract all information from a DW_TAG_string_type DIE and add to
16968 the user defined type vector. It isn't really a user defined type,
16969 but it behaves like one, with other DIE's using an AT_user_def_type
16970 attribute to reference it. */
16971
f792889a 16972static struct type *
e7c27a73 16973read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16974{
5e22e966 16975 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16976 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16977 struct type *type, *range_type, *index_type, *char_type;
16978 struct attribute *attr;
216a7e6b
AB
16979 struct dynamic_prop prop;
16980 bool length_is_constant = true;
16981 LONGEST length;
16982
16983 /* There are a couple of places where bit sizes might be made use of
16984 when parsing a DW_TAG_string_type, however, no producer that we know
16985 of make use of these. Handling bit sizes that are a multiple of the
16986 byte size is easy enough, but what about other bit sizes? Lets deal
16987 with that problem when we have to. Warn about these attributes being
16988 unsupported, then parse the type and ignore them like we always
16989 have. */
16990 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16991 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16992 {
16993 static bool warning_printed = false;
16994 if (!warning_printed)
16995 {
16996 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16997 "currently supported on DW_TAG_string_type."));
16998 warning_printed = true;
16999 }
17000 }
c906108c 17001
e142c38c 17002 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17003 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17004 {
17005 /* The string length describes the location at which the length of
17006 the string can be found. The size of the length field can be
17007 specified with one of the attributes below. */
17008 struct type *prop_type;
17009 struct attribute *len
17010 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17011 if (len == nullptr)
17012 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17013 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17014 {
17015 /* Pass 0 as the default as we know this attribute is constant
17016 and the default value will not be returned. */
0826b30a 17017 LONGEST sz = len->constant_value (0);
293e7e51 17018 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17019 }
17020 else
17021 {
17022 /* If the size is not specified then we assume it is the size of
17023 an address on this target. */
293e7e51 17024 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17025 }
17026
17027 /* Convert the attribute into a dynamic property. */
17028 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17029 length = 1;
17030 else
17031 length_is_constant = false;
17032 }
17033 else if (attr != nullptr)
17034 {
17035 /* This DW_AT_string_length just contains the length with no
17036 indirection. There's no need to create a dynamic property in this
17037 case. Pass 0 for the default value as we know it will not be
17038 returned in this case. */
0826b30a 17039 length = attr->constant_value (0);
216a7e6b
AB
17040 }
17041 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17042 {
216a7e6b 17043 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17044 length = attr->constant_value (1);
c906108c
SS
17045 }
17046 else
17047 {
216a7e6b
AB
17048 /* Use 1 as a fallback length if we have nothing else. */
17049 length = 1;
c906108c 17050 }
6ccb9162 17051
46bf5051 17052 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17053 if (length_is_constant)
17054 range_type = create_static_range_type (NULL, index_type, 1, length);
17055 else
17056 {
17057 struct dynamic_prop low_bound;
17058
17059 low_bound.kind = PROP_CONST;
17060 low_bound.data.const_val = 1;
17061 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17062 }
3b7538c0
UW
17063 char_type = language_string_char_type (cu->language_defn, gdbarch);
17064 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17065
f792889a 17066 return set_die_type (die, type, cu);
c906108c
SS
17067}
17068
4d804846
JB
17069/* Assuming that DIE corresponds to a function, returns nonzero
17070 if the function is prototyped. */
17071
17072static int
17073prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17074{
17075 struct attribute *attr;
17076
17077 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17078 if (attr && (DW_UNSND (attr) != 0))
17079 return 1;
17080
17081 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17082 is only meaningful for C, but the concept also extends to other
4d804846
JB
17083 languages that allow unprototyped functions (Eg: Objective C).
17084 For all other languages, assume that functions are always
17085 prototyped. */
17086 if (cu->language != language_c
17087 && cu->language != language_objc
17088 && cu->language != language_opencl)
17089 return 1;
17090
17091 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17092 prototyped and unprototyped functions; default to prototyped,
17093 since that is more common in modern code (and RealView warns
17094 about unprototyped functions). */
17095 if (producer_is_realview (cu->producer))
17096 return 1;
17097
17098 return 0;
17099}
17100
c906108c
SS
17101/* Handle DIES due to C code like:
17102
17103 struct foo
c5aa993b
JM
17104 {
17105 int (*funcp)(int a, long l);
17106 int b;
17107 };
c906108c 17108
0963b4bd 17109 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17110
f792889a 17111static struct type *
e7c27a73 17112read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17113{
5e22e966 17114 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17115 struct type *type; /* Type that this function returns. */
17116 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17117 struct attribute *attr;
17118
e7c27a73 17119 type = die_type (die, cu);
7e314c57
JK
17120
17121 /* The die_type call above may have already set the type for this DIE. */
17122 ftype = get_die_type (die, cu);
17123 if (ftype)
17124 return ftype;
17125
0c8b41f1 17126 ftype = lookup_function_type (type);
c906108c 17127
4d804846 17128 if (prototyped_function_p (die, cu))
a6c727b2 17129 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17130
c055b101
CV
17131 /* Store the calling convention in the type if it's available in
17132 the subroutine die. Otherwise set the calling convention to
17133 the default value DW_CC_normal. */
17134 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17135 if (attr != nullptr
17136 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17137 TYPE_CALLING_CONVENTION (ftype)
17138 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17139 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17140 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17141 else
17142 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17143
743649fd
MW
17144 /* Record whether the function returns normally to its caller or not
17145 if the DWARF producer set that information. */
17146 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17147 if (attr && (DW_UNSND (attr) != 0))
17148 TYPE_NO_RETURN (ftype) = 1;
17149
76c10ea2
GM
17150 /* We need to add the subroutine type to the die immediately so
17151 we don't infinitely recurse when dealing with parameters
0963b4bd 17152 declared as the same subroutine type. */
76c10ea2 17153 set_die_type (die, ftype, cu);
6e70227d 17154
639d11d3 17155 if (die->child != NULL)
c906108c 17156 {
bb5ed363 17157 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17158 struct die_info *child_die;
8072405b 17159 int nparams, iparams;
c906108c
SS
17160
17161 /* Count the number of parameters.
17162 FIXME: GDB currently ignores vararg functions, but knows about
17163 vararg member functions. */
8072405b 17164 nparams = 0;
639d11d3 17165 child_die = die->child;
c906108c
SS
17166 while (child_die && child_die->tag)
17167 {
17168 if (child_die->tag == DW_TAG_formal_parameter)
17169 nparams++;
17170 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17171 TYPE_VARARGS (ftype) = 1;
436c571c 17172 child_die = child_die->sibling;
c906108c
SS
17173 }
17174
17175 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17176 ftype->set_num_fields (nparams);
3cabb6b0
SM
17177 ftype->set_fields
17178 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17179
8072405b
JK
17180 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17181 even if we error out during the parameters reading below. */
17182 for (iparams = 0; iparams < nparams; iparams++)
17183 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17184
17185 iparams = 0;
639d11d3 17186 child_die = die->child;
c906108c
SS
17187 while (child_die && child_die->tag)
17188 {
17189 if (child_die->tag == DW_TAG_formal_parameter)
17190 {
3ce3b1ba
PA
17191 struct type *arg_type;
17192
17193 /* DWARF version 2 has no clean way to discern C++
17194 static and non-static member functions. G++ helps
17195 GDB by marking the first parameter for non-static
17196 member functions (which is the this pointer) as
17197 artificial. We pass this information to
17198 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17199
17200 DWARF version 3 added DW_AT_object_pointer, which GCC
17201 4.5 does not yet generate. */
e142c38c 17202 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17203 if (attr != nullptr)
c906108c
SS
17204 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17205 else
9c37b5ae 17206 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17207 arg_type = die_type (child_die, cu);
17208
17209 /* RealView does not mark THIS as const, which the testsuite
17210 expects. GCC marks THIS as const in method definitions,
17211 but not in the class specifications (GCC PR 43053). */
17212 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17213 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17214 {
17215 int is_this = 0;
17216 struct dwarf2_cu *arg_cu = cu;
17217 const char *name = dwarf2_name (child_die, cu);
17218
17219 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17220 if (attr != nullptr)
3ce3b1ba
PA
17221 {
17222 /* If the compiler emits this, use it. */
17223 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17224 is_this = 1;
17225 }
17226 else if (name && strcmp (name, "this") == 0)
17227 /* Function definitions will have the argument names. */
17228 is_this = 1;
17229 else if (name == NULL && iparams == 0)
17230 /* Declarations may not have the names, so like
17231 elsewhere in GDB, assume an artificial first
17232 argument is "this". */
17233 is_this = 1;
17234
17235 if (is_this)
17236 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17237 arg_type, 0);
17238 }
17239
17240 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17241 iparams++;
17242 }
436c571c 17243 child_die = child_die->sibling;
c906108c
SS
17244 }
17245 }
17246
76c10ea2 17247 return ftype;
c906108c
SS
17248}
17249
f792889a 17250static struct type *
e7c27a73 17251read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17252{
5e22e966 17253 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17254 const char *name = NULL;
3c8e0968 17255 struct type *this_type, *target_type;
c906108c 17256
94af9270 17257 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17258 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17259 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17260 set_die_type (die, this_type, cu);
3c8e0968
DE
17261 target_type = die_type (die, cu);
17262 if (target_type != this_type)
17263 TYPE_TARGET_TYPE (this_type) = target_type;
17264 else
17265 {
17266 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17267 spec and cause infinite loops in GDB. */
b98664d3 17268 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17269 "- DIE at %s [in module %s]"),
17270 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17271 TYPE_TARGET_TYPE (this_type) = NULL;
17272 }
e4003a34
TV
17273 if (name == NULL)
17274 {
17275 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17276 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17277 Handle these by just returning the target type, rather than
17278 constructing an anonymous typedef type and trying to handle this
17279 elsewhere. */
17280 set_die_type (die, target_type, cu);
17281 return target_type;
17282 }
f792889a 17283 return this_type;
c906108c
SS
17284}
17285
9b790ce7
UW
17286/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17287 (which may be different from NAME) to the architecture back-end to allow
17288 it to guess the correct format if necessary. */
17289
17290static struct type *
17291dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17292 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17293{
08feed99 17294 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17295 const struct floatformat **format;
17296 struct type *type;
17297
17298 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17299 if (format)
103a685e 17300 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17301 else
77b7c781 17302 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17303
17304 return type;
17305}
17306
eb77c9df
AB
17307/* Allocate an integer type of size BITS and name NAME. */
17308
17309static struct type *
17310dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17311 int bits, int unsigned_p, const char *name)
17312{
17313 struct type *type;
17314
17315 /* Versions of Intel's C Compiler generate an integer type called "void"
17316 instead of using DW_TAG_unspecified_type. This has been seen on
17317 at least versions 14, 17, and 18. */
35ee2dc2
AB
17318 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17319 && strcmp (name, "void") == 0)
eb77c9df
AB
17320 type = objfile_type (objfile)->builtin_void;
17321 else
17322 type = init_integer_type (objfile, bits, unsigned_p, name);
17323
17324 return type;
17325}
17326
8bdc1658
AB
17327/* Initialise and return a floating point type of size BITS suitable for
17328 use as a component of a complex number. The NAME_HINT is passed through
17329 when initialising the floating point type and is the name of the complex
17330 type.
17331
17332 As DWARF doesn't currently provide an explicit name for the components
17333 of a complex number, but it can be helpful to have these components
17334 named, we try to select a suitable name based on the size of the
17335 component. */
17336static struct type *
17337dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17338 struct objfile *objfile,
103a685e
TT
17339 int bits, const char *name_hint,
17340 enum bfd_endian byte_order)
8bdc1658 17341{
08feed99 17342 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17343 struct type *tt = nullptr;
17344
35add35e
AB
17345 /* Try to find a suitable floating point builtin type of size BITS.
17346 We're going to use the name of this type as the name for the complex
17347 target type that we are about to create. */
1db455a7 17348 switch (cu->language)
8bdc1658 17349 {
1db455a7
AB
17350 case language_fortran:
17351 switch (bits)
17352 {
17353 case 32:
17354 tt = builtin_f_type (gdbarch)->builtin_real;
17355 break;
17356 case 64:
17357 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17358 break;
17359 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17360 case 128:
17361 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17362 break;
17363 }
8bdc1658 17364 break;
1db455a7
AB
17365 default:
17366 switch (bits)
17367 {
17368 case 32:
17369 tt = builtin_type (gdbarch)->builtin_float;
17370 break;
17371 case 64:
17372 tt = builtin_type (gdbarch)->builtin_double;
17373 break;
17374 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17375 case 128:
17376 tt = builtin_type (gdbarch)->builtin_long_double;
17377 break;
17378 }
8bdc1658
AB
17379 break;
17380 }
17381
35add35e
AB
17382 /* If the type we found doesn't match the size we were looking for, then
17383 pretend we didn't find a type at all, the complex target type we
17384 create will then be nameless. */
a12e5744 17385 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17386 tt = nullptr;
17387
7d93a1e0 17388 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17389 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17390}
17391
c906108c
SS
17392/* Find a representation of a given base type and install
17393 it in the TYPE field of the die. */
17394
f792889a 17395static struct type *
e7c27a73 17396read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17397{
5e22e966 17398 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17399 struct type *type;
17400 struct attribute *attr;
19f392bc 17401 int encoding = 0, bits = 0;
15d034d0 17402 const char *name;
34877895 17403 gdbarch *arch;
c906108c 17404
e142c38c 17405 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17406 if (attr != nullptr)
34877895 17407 encoding = DW_UNSND (attr);
e142c38c 17408 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17409 if (attr != nullptr)
34877895 17410 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17411 name = dwarf2_name (die, cu);
6ccb9162 17412 if (!name)
34877895 17413 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17414
08feed99 17415 arch = objfile->arch ();
103a685e
TT
17416 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17417
34877895
PJ
17418 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17419 if (attr)
103a685e
TT
17420 {
17421 int endianity = DW_UNSND (attr);
17422
17423 switch (endianity)
17424 {
17425 case DW_END_big:
17426 byte_order = BFD_ENDIAN_BIG;
17427 break;
17428 case DW_END_little:
17429 byte_order = BFD_ENDIAN_LITTLE;
17430 break;
17431 default:
17432 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17433 break;
17434 }
17435 }
6ccb9162
UW
17436
17437 switch (encoding)
c906108c 17438 {
6ccb9162
UW
17439 case DW_ATE_address:
17440 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17441 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17442 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17443 break;
17444 case DW_ATE_boolean:
19f392bc 17445 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17446 break;
17447 case DW_ATE_complex_float:
103a685e
TT
17448 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17449 byte_order);
78134374 17450 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17451 {
17452 if (name == nullptr)
17453 {
17454 struct obstack *obstack
5e22e966 17455 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17456 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17457 nullptr);
17458 }
17459 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17460 }
17461 else
17462 type = init_complex_type (name, type);
6ccb9162
UW
17463 break;
17464 case DW_ATE_decimal_float:
19f392bc 17465 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17466 break;
17467 case DW_ATE_float:
103a685e 17468 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17469 break;
17470 case DW_ATE_signed:
eb77c9df 17471 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17472 break;
17473 case DW_ATE_unsigned:
3b2b8fea
TT
17474 if (cu->language == language_fortran
17475 && name
61012eef 17476 && startswith (name, "character("))
19f392bc
UW
17477 type = init_character_type (objfile, bits, 1, name);
17478 else
eb77c9df 17479 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17480 break;
17481 case DW_ATE_signed_char:
6e70227d 17482 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17483 || cu->language == language_pascal
17484 || cu->language == language_fortran)
19f392bc
UW
17485 type = init_character_type (objfile, bits, 0, name);
17486 else
eb77c9df 17487 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17488 break;
17489 case DW_ATE_unsigned_char:
868a0084 17490 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17491 || cu->language == language_pascal
c44af4eb
TT
17492 || cu->language == language_fortran
17493 || cu->language == language_rust)
19f392bc
UW
17494 type = init_character_type (objfile, bits, 1, name);
17495 else
eb77c9df 17496 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17497 break;
75079b2b 17498 case DW_ATE_UTF:
53e710ac 17499 {
53e710ac
PA
17500 if (bits == 16)
17501 type = builtin_type (arch)->builtin_char16;
17502 else if (bits == 32)
17503 type = builtin_type (arch)->builtin_char32;
17504 else
17505 {
b98664d3 17506 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17507 bits);
eb77c9df 17508 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17509 }
17510 return set_die_type (die, type, cu);
17511 }
75079b2b
TT
17512 break;
17513
6ccb9162 17514 default:
b98664d3 17515 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17516 dwarf_type_encoding_name (encoding));
77b7c781 17517 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17518 break;
c906108c 17519 }
6ccb9162 17520
0114d602 17521 if (name && strcmp (name, "char") == 0)
876cecd0 17522 TYPE_NOSIGN (type) = 1;
0114d602 17523
2b4424c3
TT
17524 maybe_set_alignment (cu, die, type);
17525
103a685e 17526 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17527
f792889a 17528 return set_die_type (die, type, cu);
c906108c
SS
17529}
17530
80180f79
SA
17531/* Parse dwarf attribute if it's a block, reference or constant and put the
17532 resulting value of the attribute into struct bound_prop.
17533 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17534
17535static int
17536attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17537 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17538 struct type *default_type)
80180f79
SA
17539{
17540 struct dwarf2_property_baton *baton;
5e22e966 17541 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17542 struct objfile *objfile = per_objfile->objfile;
17543 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17544
9a49df9d
AB
17545 gdb_assert (default_type != NULL);
17546
80180f79
SA
17547 if (attr == NULL || prop == NULL)
17548 return 0;
17549
4fc6c0d5 17550 if (attr->form_is_block ())
80180f79 17551 {
8d749320 17552 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17553 baton->property_type = default_type;
80180f79 17554 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17555 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17556 baton->locexpr.size = DW_BLOCK (attr)->size;
17557 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17558 switch (attr->name)
17559 {
17560 case DW_AT_string_length:
17561 baton->locexpr.is_reference = true;
17562 break;
17563 default:
17564 baton->locexpr.is_reference = false;
17565 break;
17566 }
80180f79
SA
17567 prop->data.baton = baton;
17568 prop->kind = PROP_LOCEXPR;
17569 gdb_assert (prop->data.baton != NULL);
17570 }
cd6c91b4 17571 else if (attr->form_is_ref ())
80180f79
SA
17572 {
17573 struct dwarf2_cu *target_cu = cu;
17574 struct die_info *target_die;
17575 struct attribute *target_attr;
17576
17577 target_die = follow_die_ref (die, attr, &target_cu);
17578 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17579 if (target_attr == NULL)
17580 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17581 target_cu);
80180f79
SA
17582 if (target_attr == NULL)
17583 return 0;
17584
df25ebbd 17585 switch (target_attr->name)
80180f79 17586 {
df25ebbd 17587 case DW_AT_location:
cd6c91b4 17588 if (target_attr->form_is_section_offset ())
df25ebbd 17589 {
8d749320 17590 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17591 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17592 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17593 prop->data.baton = baton;
17594 prop->kind = PROP_LOCLIST;
17595 gdb_assert (prop->data.baton != NULL);
17596 }
4fc6c0d5 17597 else if (target_attr->form_is_block ())
df25ebbd 17598 {
8d749320 17599 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17600 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17601 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17602 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17603 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17604 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17605 baton->locexpr.is_reference = true;
df25ebbd
JB
17606 prop->data.baton = baton;
17607 prop->kind = PROP_LOCEXPR;
17608 gdb_assert (prop->data.baton != NULL);
17609 }
17610 else
17611 {
17612 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17613 "dynamic property");
17614 return 0;
17615 }
17616 break;
17617 case DW_AT_data_member_location:
17618 {
17619 LONGEST offset;
17620
17621 if (!handle_data_member_location (target_die, target_cu,
17622 &offset))
17623 return 0;
17624
8d749320 17625 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17626 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17627 target_cu);
df25ebbd
JB
17628 baton->offset_info.offset = offset;
17629 baton->offset_info.type = die_type (target_die, target_cu);
17630 prop->data.baton = baton;
17631 prop->kind = PROP_ADDR_OFFSET;
17632 break;
17633 }
80180f79
SA
17634 }
17635 }
cd6c91b4 17636 else if (attr->form_is_constant ())
80180f79 17637 {
0826b30a 17638 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17639 prop->kind = PROP_CONST;
17640 }
17641 else
17642 {
17643 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17644 dwarf2_name (die, cu));
17645 return 0;
17646 }
17647
17648 return 1;
17649}
17650
09ba997f 17651/* See read.h. */
9a49df9d 17652
09ba997f 17653struct type *
293e7e51 17654dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17655{
9a49df9d
AB
17656 struct type *int_type;
17657
17658 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17659#define TRY_TYPE(F) \
17660 int_type = (unsigned_p \
17661 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17662 : objfile_type (objfile)->builtin_ ## F); \
17663 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17664 return int_type
17665
17666 TRY_TYPE (char);
17667 TRY_TYPE (short);
17668 TRY_TYPE (int);
17669 TRY_TYPE (long);
17670 TRY_TYPE (long_long);
17671
17672#undef TRY_TYPE
17673
17674 gdb_assert_not_reached ("unable to find suitable integer type");
17675}
17676
09ba997f 17677/* See read.h. */
11a8b164 17678
09ba997f 17679struct type *
293e7e51 17680dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17681{
293e7e51
SM
17682 int addr_size = this->per_cu->addr_size ();
17683 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17684}
17685
b86352cf
AB
17686/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17687 present (which is valid) then compute the default type based on the
17688 compilation units address size. */
17689
17690static struct type *
17691read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17692{
17693 struct type *index_type = die_type (die, cu);
17694
17695 /* Dwarf-2 specifications explicitly allows to create subrange types
17696 without specifying a base type.
17697 In that case, the base type must be set to the type of
17698 the lower bound, upper bound or count, in that order, if any of these
17699 three attributes references an object that has a type.
17700 If no base type is found, the Dwarf-2 specifications say that
17701 a signed integer type of size equal to the size of an address should
17702 be used.
17703 For the following C code: `extern char gdb_int [];'
17704 GCC produces an empty range DIE.
17705 FIXME: muller/2010-05-28: Possible references to object for low bound,
17706 high bound or count are not yet handled by this code. */
78134374 17707 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17708 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17709
17710 return index_type;
17711}
17712
a02abb62
JB
17713/* Read the given DW_AT_subrange DIE. */
17714
f792889a 17715static struct type *
a02abb62
JB
17716read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17717{
4c9ad8c2 17718 struct type *base_type, *orig_base_type;
a02abb62
JB
17719 struct type *range_type;
17720 struct attribute *attr;
729efb13 17721 struct dynamic_prop low, high;
4fae6e18 17722 int low_default_is_valid;
c451ebe5 17723 int high_bound_is_count = 0;
15d034d0 17724 const char *name;
d359392f 17725 ULONGEST negative_mask;
e77813c8 17726
b86352cf
AB
17727 orig_base_type = read_subrange_index_type (die, cu);
17728
4c9ad8c2
TT
17729 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17730 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17731 creating the range type, but we use the result of check_typedef
17732 when examining properties of the type. */
17733 base_type = check_typedef (orig_base_type);
a02abb62 17734
7e314c57
JK
17735 /* The die_type call above may have already set the type for this DIE. */
17736 range_type = get_die_type (die, cu);
17737 if (range_type)
17738 return range_type;
17739
729efb13
SA
17740 low.kind = PROP_CONST;
17741 high.kind = PROP_CONST;
17742 high.data.const_val = 0;
17743
4fae6e18
JK
17744 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17745 omitting DW_AT_lower_bound. */
17746 switch (cu->language)
6e70227d 17747 {
4fae6e18
JK
17748 case language_c:
17749 case language_cplus:
729efb13 17750 low.data.const_val = 0;
4fae6e18
JK
17751 low_default_is_valid = 1;
17752 break;
17753 case language_fortran:
729efb13 17754 low.data.const_val = 1;
4fae6e18
JK
17755 low_default_is_valid = 1;
17756 break;
17757 case language_d:
4fae6e18 17758 case language_objc:
c44af4eb 17759 case language_rust:
729efb13 17760 low.data.const_val = 0;
4fae6e18
JK
17761 low_default_is_valid = (cu->header.version >= 4);
17762 break;
17763 case language_ada:
17764 case language_m2:
17765 case language_pascal:
729efb13 17766 low.data.const_val = 1;
4fae6e18
JK
17767 low_default_is_valid = (cu->header.version >= 4);
17768 break;
17769 default:
729efb13 17770 low.data.const_val = 0;
4fae6e18
JK
17771 low_default_is_valid = 0;
17772 break;
a02abb62
JB
17773 }
17774
e142c38c 17775 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17776 if (attr != nullptr)
9a49df9d 17777 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17778 else if (!low_default_is_valid)
b98664d3 17779 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17780 "- DIE at %s [in module %s]"),
17781 sect_offset_str (die->sect_off),
5e22e966 17782 objfile_name (cu->per_objfile->objfile));
a02abb62 17783
506f5c41
TV
17784 struct attribute *attr_ub, *attr_count;
17785 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17786 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17787 {
506f5c41 17788 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17789 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17790 {
c451ebe5
SA
17791 /* If bounds are constant do the final calculation here. */
17792 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17793 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17794 else
17795 high_bound_is_count = 1;
c2ff108b 17796 }
506f5c41
TV
17797 else
17798 {
17799 if (attr_ub != NULL)
17800 complaint (_("Unresolved DW_AT_upper_bound "
17801 "- DIE at %s [in module %s]"),
17802 sect_offset_str (die->sect_off),
5e22e966 17803 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17804 if (attr_count != NULL)
17805 complaint (_("Unresolved DW_AT_count "
17806 "- DIE at %s [in module %s]"),
17807 sect_offset_str (die->sect_off),
5e22e966 17808 objfile_name (cu->per_objfile->objfile));
506f5c41 17809 }
e77813c8 17810 }
a02abb62 17811
4e962e74
TT
17812 LONGEST bias = 0;
17813 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17814 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17815 bias = bias_attr->constant_value (0);
4e962e74 17816
dbb9c2b1
JB
17817 /* Normally, the DWARF producers are expected to use a signed
17818 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17819 But this is unfortunately not always the case, as witnessed
17820 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17821 is used instead. To work around that ambiguity, we treat
17822 the bounds as signed, and thus sign-extend their values, when
17823 the base type is signed. */
6e70227d 17824 negative_mask =
d359392f 17825 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17826 if (low.kind == PROP_CONST
17827 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17828 low.data.const_val |= negative_mask;
17829 if (high.kind == PROP_CONST
17830 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17831 high.data.const_val |= negative_mask;
43bbcdc2 17832
5bbd8269
AB
17833 /* Check for bit and byte strides. */
17834 struct dynamic_prop byte_stride_prop;
17835 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17836 if (attr_byte_stride != nullptr)
17837 {
293e7e51 17838 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17839 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17840 prop_type);
17841 }
17842
17843 struct dynamic_prop bit_stride_prop;
17844 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17845 if (attr_bit_stride != nullptr)
17846 {
17847 /* It only makes sense to have either a bit or byte stride. */
17848 if (attr_byte_stride != nullptr)
17849 {
17850 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17851 "- DIE at %s [in module %s]"),
17852 sect_offset_str (die->sect_off),
5e22e966 17853 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17854 attr_bit_stride = nullptr;
17855 }
17856 else
17857 {
293e7e51 17858 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17859 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17860 prop_type);
17861 }
17862 }
17863
17864 if (attr_byte_stride != nullptr
17865 || attr_bit_stride != nullptr)
17866 {
17867 bool byte_stride_p = (attr_byte_stride != nullptr);
17868 struct dynamic_prop *stride
17869 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17870
17871 range_type
17872 = create_range_type_with_stride (NULL, orig_base_type, &low,
17873 &high, bias, stride, byte_stride_p);
17874 }
17875 else
17876 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17877
c451ebe5
SA
17878 if (high_bound_is_count)
17879 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17880
c2ff108b
JK
17881 /* Ada expects an empty array on no boundary attributes. */
17882 if (attr == NULL && cu->language != language_ada)
729efb13 17883 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17884
39cbfefa
DJ
17885 name = dwarf2_name (die, cu);
17886 if (name)
d0e39ea2 17887 range_type->set_name (name);
6e70227d 17888
e142c38c 17889 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17890 if (attr != nullptr)
a02abb62
JB
17891 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17892
2b4424c3
TT
17893 maybe_set_alignment (cu, die, range_type);
17894
7e314c57
JK
17895 set_die_type (die, range_type, cu);
17896
17897 /* set_die_type should be already done. */
b4ba55a1
JB
17898 set_descriptive_type (range_type, die, cu);
17899
7e314c57 17900 return range_type;
a02abb62 17901}
6e70227d 17902
f792889a 17903static struct type *
81a17f79
JB
17904read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17905{
17906 struct type *type;
81a17f79 17907
5e22e966 17908 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17909 type->set_name (dwarf2_name (die, cu));
81a17f79 17910
74a2f8ff 17911 /* In Ada, an unspecified type is typically used when the description
85102364 17912 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17913 such a type, we treat it as a stub, and try to resolve it later on,
17914 when needed. */
17915 if (cu->language == language_ada)
17916 TYPE_STUB (type) = 1;
17917
f792889a 17918 return set_die_type (die, type, cu);
81a17f79 17919}
a02abb62 17920
639d11d3
DC
17921/* Read a single die and all its descendents. Set the die's sibling
17922 field to NULL; set other fields in the die correctly, and set all
17923 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17924 location of the info_ptr after reading all of those dies. PARENT
17925 is the parent of the die in question. */
17926
17927static struct die_info *
dee91e82 17928read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17929 const gdb_byte *info_ptr,
17930 const gdb_byte **new_info_ptr,
dee91e82 17931 struct die_info *parent)
639d11d3
DC
17932{
17933 struct die_info *die;
d521ce57 17934 const gdb_byte *cur_ptr;
639d11d3 17935
3e225074 17936 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17937 if (die == NULL)
17938 {
17939 *new_info_ptr = cur_ptr;
17940 return NULL;
17941 }
93311388 17942 store_in_ref_table (die, reader->cu);
639d11d3 17943
3e225074 17944 if (die->has_children)
bf6af496 17945 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17946 else
17947 {
17948 die->child = NULL;
17949 *new_info_ptr = cur_ptr;
17950 }
17951
17952 die->sibling = NULL;
17953 die->parent = parent;
17954 return die;
17955}
17956
17957/* Read a die, all of its descendents, and all of its siblings; set
17958 all of the fields of all of the dies correctly. Arguments are as
17959 in read_die_and_children. */
17960
17961static struct die_info *
bf6af496 17962read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17963 const gdb_byte *info_ptr,
17964 const gdb_byte **new_info_ptr,
bf6af496 17965 struct die_info *parent)
639d11d3
DC
17966{
17967 struct die_info *first_die, *last_sibling;
d521ce57 17968 const gdb_byte *cur_ptr;
639d11d3 17969
c906108c 17970 cur_ptr = info_ptr;
639d11d3
DC
17971 first_die = last_sibling = NULL;
17972
17973 while (1)
c906108c 17974 {
639d11d3 17975 struct die_info *die
dee91e82 17976 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17977
1d325ec1 17978 if (die == NULL)
c906108c 17979 {
639d11d3
DC
17980 *new_info_ptr = cur_ptr;
17981 return first_die;
c906108c 17982 }
1d325ec1
DJ
17983
17984 if (!first_die)
17985 first_die = die;
c906108c 17986 else
1d325ec1
DJ
17987 last_sibling->sibling = die;
17988
17989 last_sibling = die;
c906108c 17990 }
c906108c
SS
17991}
17992
bf6af496
DE
17993/* Read a die, all of its descendents, and all of its siblings; set
17994 all of the fields of all of the dies correctly. Arguments are as
17995 in read_die_and_children.
17996 This the main entry point for reading a DIE and all its children. */
17997
17998static struct die_info *
17999read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18000 const gdb_byte *info_ptr,
18001 const gdb_byte **new_info_ptr,
bf6af496
DE
18002 struct die_info *parent)
18003{
18004 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18005 new_info_ptr, parent);
18006
b4f54984 18007 if (dwarf_die_debug)
bf6af496
DE
18008 {
18009 fprintf_unfiltered (gdb_stdlog,
18010 "Read die from %s@0x%x of %s:\n",
96b79293 18011 reader->die_section->get_name (),
bf6af496
DE
18012 (unsigned) (info_ptr - reader->die_section->buffer),
18013 bfd_get_filename (reader->abfd));
b4f54984 18014 dump_die (die, dwarf_die_debug);
bf6af496
DE
18015 }
18016
18017 return die;
18018}
18019
3019eac3
DE
18020/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18021 attributes.
18022 The caller is responsible for filling in the extra attributes
18023 and updating (*DIEP)->num_attrs.
18024 Set DIEP to point to a newly allocated die with its information,
3e225074 18025 except for its child, sibling, and parent fields. */
93311388 18026
d521ce57 18027static const gdb_byte *
3019eac3 18028read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18029 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18030 int num_extra_attrs)
93311388 18031{
b64f50a1 18032 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18033 struct abbrev_info *abbrev;
18034 struct die_info *die;
18035 struct dwarf2_cu *cu = reader->cu;
18036 bfd *abfd = reader->abfd;
18037
9c541725 18038 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18039 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18040 info_ptr += bytes_read;
18041 if (!abbrev_number)
18042 {
18043 *diep = NULL;
93311388
DE
18044 return info_ptr;
18045 }
18046
685af9cd 18047 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18048 if (!abbrev)
348e048f
DE
18049 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18050 abbrev_number,
18051 bfd_get_filename (abfd));
18052
3019eac3 18053 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18054 die->sect_off = sect_off;
93311388
DE
18055 die->tag = abbrev->tag;
18056 die->abbrev = abbrev_number;
3e225074 18057 die->has_children = abbrev->has_children;
93311388 18058
3019eac3
DE
18059 /* Make the result usable.
18060 The caller needs to update num_attrs after adding the extra
18061 attributes. */
93311388
DE
18062 die->num_attrs = abbrev->num_attrs;
18063
18a8505e 18064 std::vector<int> indexes_that_need_reprocess;
93311388 18065 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18066 {
18067 bool need_reprocess;
18068 info_ptr =
18069 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18070 info_ptr, &need_reprocess);
18071 if (need_reprocess)
18072 indexes_that_need_reprocess.push_back (i);
18073 }
18074
052c8bb8 18075 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18076 if (attr != nullptr)
18077 cu->str_offsets_base = DW_UNSND (attr);
93311388 18078
41144253 18079 attr = die->attr (DW_AT_loclists_base);
18080 if (attr != nullptr)
18081 cu->loclist_base = DW_UNSND (attr);
18082
a39fdb41 18083 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18084 if (maybe_addr_base.has_value ())
18085 cu->addr_base = *maybe_addr_base;
18086 for (int index : indexes_that_need_reprocess)
18087 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18088 *diep = die;
93311388
DE
18089 return info_ptr;
18090}
18091
3019eac3
DE
18092/* Read a die and all its attributes.
18093 Set DIEP to point to a newly allocated die with its information,
3e225074 18094 except for its child, sibling, and parent fields. */
3019eac3 18095
d521ce57 18096static const gdb_byte *
3019eac3 18097read_full_die (const struct die_reader_specs *reader,
3e225074 18098 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18099{
d521ce57 18100 const gdb_byte *result;
bf6af496 18101
3e225074 18102 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18103
b4f54984 18104 if (dwarf_die_debug)
bf6af496
DE
18105 {
18106 fprintf_unfiltered (gdb_stdlog,
18107 "Read die from %s@0x%x of %s:\n",
96b79293 18108 reader->die_section->get_name (),
bf6af496
DE
18109 (unsigned) (info_ptr - reader->die_section->buffer),
18110 bfd_get_filename (reader->abfd));
b4f54984 18111 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18112 }
18113
18114 return result;
3019eac3 18115}
433df2d4 18116\f
c906108c 18117
72bf9492
DJ
18118/* Returns nonzero if TAG represents a type that we might generate a partial
18119 symbol for. */
18120
18121static int
18122is_type_tag_for_partial (int tag)
18123{
18124 switch (tag)
18125 {
18126#if 0
18127 /* Some types that would be reasonable to generate partial symbols for,
18128 that we don't at present. */
18129 case DW_TAG_array_type:
18130 case DW_TAG_file_type:
18131 case DW_TAG_ptr_to_member_type:
18132 case DW_TAG_set_type:
18133 case DW_TAG_string_type:
18134 case DW_TAG_subroutine_type:
18135#endif
18136 case DW_TAG_base_type:
18137 case DW_TAG_class_type:
680b30c7 18138 case DW_TAG_interface_type:
72bf9492
DJ
18139 case DW_TAG_enumeration_type:
18140 case DW_TAG_structure_type:
18141 case DW_TAG_subrange_type:
18142 case DW_TAG_typedef:
18143 case DW_TAG_union_type:
18144 return 1;
18145 default:
18146 return 0;
18147 }
18148}
18149
18150/* Load all DIEs that are interesting for partial symbols into memory. */
18151
18152static struct partial_die_info *
dee91e82 18153load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18154 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18155{
dee91e82 18156 struct dwarf2_cu *cu = reader->cu;
5e22e966 18157 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18158 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18159 unsigned int bytes_read;
5afb4e99 18160 unsigned int load_all = 0;
72bf9492
DJ
18161 int nesting_level = 1;
18162
18163 parent_die = NULL;
18164 last_die = NULL;
18165
7adf1e79
DE
18166 gdb_assert (cu->per_cu != NULL);
18167 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18168 load_all = 1;
18169
72bf9492
DJ
18170 cu->partial_dies
18171 = htab_create_alloc_ex (cu->header.length / 12,
18172 partial_die_hash,
18173 partial_die_eq,
18174 NULL,
18175 &cu->comp_unit_obstack,
18176 hashtab_obstack_allocate,
18177 dummy_obstack_deallocate);
18178
72bf9492
DJ
18179 while (1)
18180 {
685af9cd 18181 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18182
18183 /* A NULL abbrev means the end of a series of children. */
18184 if (abbrev == NULL)
18185 {
18186 if (--nesting_level == 0)
cd9983dd
YQ
18187 return first_die;
18188
72bf9492
DJ
18189 info_ptr += bytes_read;
18190 last_die = parent_die;
18191 parent_die = parent_die->die_parent;
18192 continue;
18193 }
18194
98bfdba5
PA
18195 /* Check for template arguments. We never save these; if
18196 they're seen, we just mark the parent, and go on our way. */
18197 if (parent_die != NULL
18198 && cu->language == language_cplus
18199 && (abbrev->tag == DW_TAG_template_type_param
18200 || abbrev->tag == DW_TAG_template_value_param))
18201 {
18202 parent_die->has_template_arguments = 1;
18203
18204 if (!load_all)
18205 {
18206 /* We don't need a partial DIE for the template argument. */
dee91e82 18207 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18208 continue;
18209 }
18210 }
18211
0d99eb77 18212 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18213 Skip their other children. */
18214 if (!load_all
18215 && cu->language == language_cplus
18216 && parent_die != NULL
18217 && parent_die->tag == DW_TAG_subprogram)
18218 {
dee91e82 18219 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18220 continue;
18221 }
18222
5afb4e99
DJ
18223 /* Check whether this DIE is interesting enough to save. Normally
18224 we would not be interested in members here, but there may be
18225 later variables referencing them via DW_AT_specification (for
18226 static members). */
18227 if (!load_all
18228 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18229 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18230 && abbrev->tag != DW_TAG_enumerator
18231 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18232 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18233 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18234 && abbrev->tag != DW_TAG_variable
5afb4e99 18235 && abbrev->tag != DW_TAG_namespace
f55ee35c 18236 && abbrev->tag != DW_TAG_module
95554aad 18237 && abbrev->tag != DW_TAG_member
74921315
KS
18238 && abbrev->tag != DW_TAG_imported_unit
18239 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18240 {
18241 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18242 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18243 continue;
18244 }
18245
6f06d47b
YQ
18246 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18247 abbrev);
cd9983dd 18248
48fbe735 18249 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18250
18251 /* This two-pass algorithm for processing partial symbols has a
18252 high cost in cache pressure. Thus, handle some simple cases
18253 here which cover the majority of C partial symbols. DIEs
18254 which neither have specification tags in them, nor could have
18255 specification tags elsewhere pointing at them, can simply be
18256 processed and discarded.
18257
18258 This segment is also optional; scan_partial_symbols and
18259 add_partial_symbol will handle these DIEs if we chain
18260 them in normally. When compilers which do not emit large
18261 quantities of duplicate debug information are more common,
18262 this code can probably be removed. */
18263
18264 /* Any complete simple types at the top level (pretty much all
18265 of them, for a language without namespaces), can be processed
18266 directly. */
18267 if (parent_die == NULL
cd9983dd
YQ
18268 && pdi.has_specification == 0
18269 && pdi.is_declaration == 0
18270 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18271 || pdi.tag == DW_TAG_base_type
18272 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18273 {
cd9983dd 18274 if (building_psymtab && pdi.name != NULL)
31edb802 18275 add_psymbol_to_list (pdi.name, false,
79748972 18276 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18277 psymbol_placement::STATIC,
1762568f 18278 0, cu->language, objfile);
cd9983dd 18279 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18280 continue;
18281 }
18282
d8228535
JK
18283 /* The exception for DW_TAG_typedef with has_children above is
18284 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18285 type_name_or_error will error on such types later.
d8228535
JK
18286
18287 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18288 it could not find the child DIEs referenced later, this is checked
18289 above. In correct DWARF DW_TAG_typedef should have no children. */
18290
cd9983dd 18291 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18292 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18293 "- DIE at %s [in module %s]"),
cd9983dd 18294 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18295
72bf9492
DJ
18296 /* If we're at the second level, and we're an enumerator, and
18297 our parent has no specification (meaning possibly lives in a
18298 namespace elsewhere), then we can add the partial symbol now
18299 instead of queueing it. */
cd9983dd 18300 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18301 && parent_die != NULL
18302 && parent_die->die_parent == NULL
18303 && parent_die->tag == DW_TAG_enumeration_type
18304 && parent_die->has_specification == 0)
18305 {
cd9983dd 18306 if (pdi.name == NULL)
b98664d3 18307 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18308 else if (building_psymtab)
31edb802 18309 add_psymbol_to_list (pdi.name, false,
79748972 18310 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18311 cu->language == language_cplus
75aedd27
TT
18312 ? psymbol_placement::GLOBAL
18313 : psymbol_placement::STATIC,
1762568f 18314 0, cu->language, objfile);
72bf9492 18315
cd9983dd 18316 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18317 continue;
18318 }
18319
cd9983dd 18320 struct partial_die_info *part_die
6f06d47b 18321 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18322
72bf9492
DJ
18323 /* We'll save this DIE so link it in. */
18324 part_die->die_parent = parent_die;
18325 part_die->die_sibling = NULL;
18326 part_die->die_child = NULL;
18327
18328 if (last_die && last_die == parent_die)
18329 last_die->die_child = part_die;
18330 else if (last_die)
18331 last_die->die_sibling = part_die;
18332
18333 last_die = part_die;
18334
18335 if (first_die == NULL)
18336 first_die = part_die;
18337
18338 /* Maybe add the DIE to the hash table. Not all DIEs that we
18339 find interesting need to be in the hash table, because we
18340 also have the parent/sibling/child chains; only those that we
18341 might refer to by offset later during partial symbol reading.
18342
18343 For now this means things that might have be the target of a
18344 DW_AT_specification, DW_AT_abstract_origin, or
18345 DW_AT_extension. DW_AT_extension will refer only to
18346 namespaces; DW_AT_abstract_origin refers to functions (and
18347 many things under the function DIE, but we do not recurse
18348 into function DIEs during partial symbol reading) and
18349 possibly variables as well; DW_AT_specification refers to
18350 declarations. Declarations ought to have the DW_AT_declaration
18351 flag. It happens that GCC forgets to put it in sometimes, but
18352 only for functions, not for types.
18353
18354 Adding more things than necessary to the hash table is harmless
18355 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18356 wasted time in find_partial_die, when we reread the compilation
18357 unit with load_all_dies set. */
72bf9492 18358
5afb4e99 18359 if (load_all
72929c62 18360 || abbrev->tag == DW_TAG_constant
5afb4e99 18361 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18362 || abbrev->tag == DW_TAG_variable
18363 || abbrev->tag == DW_TAG_namespace
18364 || part_die->is_declaration)
18365 {
18366 void **slot;
18367
18368 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18369 to_underlying (part_die->sect_off),
18370 INSERT);
72bf9492
DJ
18371 *slot = part_die;
18372 }
18373
72bf9492 18374 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18375 we have no reason to follow the children of structures; for other
98bfdba5
PA
18376 languages we have to, so that we can get at method physnames
18377 to infer fully qualified class names, for DW_AT_specification,
18378 and for C++ template arguments. For C++, we also look one level
18379 inside functions to find template arguments (if the name of the
18380 function does not already contain the template arguments).
bc30ff58 18381
0a4b0913
AB
18382 For Ada and Fortran, we need to scan the children of subprograms
18383 and lexical blocks as well because these languages allow the
18384 definition of nested entities that could be interesting for the
18385 debugger, such as nested subprograms for instance. */
72bf9492 18386 if (last_die->has_children
5afb4e99
DJ
18387 && (load_all
18388 || last_die->tag == DW_TAG_namespace
f55ee35c 18389 || last_die->tag == DW_TAG_module
72bf9492 18390 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18391 || (cu->language == language_cplus
18392 && last_die->tag == DW_TAG_subprogram
18393 && (last_die->name == NULL
18394 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18395 || (cu->language != language_c
18396 && (last_die->tag == DW_TAG_class_type
680b30c7 18397 || last_die->tag == DW_TAG_interface_type
72bf9492 18398 || last_die->tag == DW_TAG_structure_type
bc30ff58 18399 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18400 || ((cu->language == language_ada
18401 || cu->language == language_fortran)
bc30ff58
JB
18402 && (last_die->tag == DW_TAG_subprogram
18403 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18404 {
18405 nesting_level++;
18406 parent_die = last_die;
18407 continue;
18408 }
18409
18410 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18411 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18412
18413 /* Back to the top, do it again. */
18414 }
18415}
18416
6f06d47b
YQ
18417partial_die_info::partial_die_info (sect_offset sect_off_,
18418 struct abbrev_info *abbrev)
18419 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18420{
18421}
18422
35cc7ed7
YQ
18423/* Read a minimal amount of information into the minimal die structure.
18424 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18425
48fbe735
YQ
18426const gdb_byte *
18427partial_die_info::read (const struct die_reader_specs *reader,
18428 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18429{
dee91e82 18430 struct dwarf2_cu *cu = reader->cu;
5e22e966 18431 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18432 unsigned int i;
c5aa993b 18433 int has_low_pc_attr = 0;
c906108c 18434 int has_high_pc_attr = 0;
91da1414 18435 int high_pc_relative = 0;
c906108c 18436
fd0a254f 18437 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18438 {
e7da7f8f 18439 attribute attr;
18a8505e 18440 bool need_reprocess;
e7da7f8f 18441 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18442 info_ptr, &need_reprocess);
18443 /* String and address offsets that need to do the reprocessing have
18444 already been read at this point, so there is no need to wait until
18445 the loop terminates to do the reprocessing. */
18446 if (need_reprocess)
e7da7f8f 18447 read_attribute_reprocess (reader, &attr);
c906108c 18448 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18449 partial symbol table. */
c906108c
SS
18450 switch (attr.name)
18451 {
18452 case DW_AT_name:
48fbe735 18453 switch (tag)
71c25dea
TT
18454 {
18455 case DW_TAG_compile_unit:
95554aad 18456 case DW_TAG_partial_unit:
348e048f 18457 case DW_TAG_type_unit:
71c25dea
TT
18458 /* Compilation units have a DW_AT_name that is a filename, not
18459 a source language identifier. */
18460 case DW_TAG_enumeration_type:
18461 case DW_TAG_enumerator:
18462 /* These tags always have simple identifiers already; no need
18463 to canonicalize them. */
48fbe735 18464 name = DW_STRING (&attr);
71c25dea
TT
18465 break;
18466 default:
48fbe735
YQ
18467 {
18468 struct objfile *objfile = dwarf2_per_objfile->objfile;
18469
18470 name
be1e3d3e 18471 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18472 }
71c25dea
TT
18473 break;
18474 }
c906108c 18475 break;
31ef98ae 18476 case DW_AT_linkage_name:
c906108c 18477 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18478 /* Note that both forms of linkage name might appear. We
18479 assume they will be the same, and we only store the last
18480 one we see. */
e61108c9 18481 linkage_name = attr.value_as_string ();
787de330
TT
18482 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18483 See https://github.com/rust-lang/rust/issues/32925. */
18484 if (cu->language == language_rust && linkage_name != NULL
18485 && strchr (linkage_name, '{') != NULL)
18486 linkage_name = NULL;
c906108c
SS
18487 break;
18488 case DW_AT_low_pc:
18489 has_low_pc_attr = 1;
cd6c91b4 18490 lowpc = attr.value_as_address ();
c906108c
SS
18491 break;
18492 case DW_AT_high_pc:
18493 has_high_pc_attr = 1;
cd6c91b4
TT
18494 highpc = attr.value_as_address ();
18495 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18496 high_pc_relative = 1;
c906108c
SS
18497 break;
18498 case DW_AT_location:
0963b4bd 18499 /* Support the .debug_loc offsets. */
4fc6c0d5 18500 if (attr.form_is_block ())
8e19ed76 18501 {
48fbe735 18502 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18503 }
cd6c91b4 18504 else if (attr.form_is_section_offset ())
8e19ed76 18505 {
4d3c2250 18506 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18507 }
18508 else
18509 {
4d3c2250
KB
18510 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18511 "partial symbol information");
8e19ed76 18512 }
c906108c 18513 break;
c906108c 18514 case DW_AT_external:
48fbe735 18515 is_external = DW_UNSND (&attr);
c906108c
SS
18516 break;
18517 case DW_AT_declaration:
48fbe735 18518 is_declaration = DW_UNSND (&attr);
c906108c
SS
18519 break;
18520 case DW_AT_type:
48fbe735 18521 has_type = 1;
c906108c
SS
18522 break;
18523 case DW_AT_abstract_origin:
18524 case DW_AT_specification:
72bf9492 18525 case DW_AT_extension:
48fbe735 18526 has_specification = 1;
0826b30a 18527 spec_offset = attr.get_ref_die_offset ();
48fbe735 18528 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18529 || cu->per_cu->is_dwz);
c906108c
SS
18530 break;
18531 case DW_AT_sibling:
18532 /* Ignore absolute siblings, they might point outside of
18533 the current compile unit. */
18534 if (attr.form == DW_FORM_ref_addr)
b98664d3 18535 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18536 else
b9502d3f 18537 {
48fbe735 18538 const gdb_byte *buffer = reader->buffer;
0826b30a 18539 sect_offset off = attr.get_ref_die_offset ();
9c541725 18540 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18541
18542 if (sibling_ptr < info_ptr)
b98664d3 18543 complaint (_("DW_AT_sibling points backwards"));
22869d73 18544 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18545 reader->die_section->overflow_complaint ();
b9502d3f 18546 else
48fbe735 18547 sibling = sibling_ptr;
b9502d3f 18548 }
c906108c 18549 break;
fa4028e9 18550 case DW_AT_byte_size:
48fbe735 18551 has_byte_size = 1;
fa4028e9 18552 break;
ff908ebf 18553 case DW_AT_const_value:
48fbe735 18554 has_const_value = 1;
ff908ebf 18555 break;
68511cec
CES
18556 case DW_AT_calling_convention:
18557 /* DWARF doesn't provide a way to identify a program's source-level
18558 entry point. DW_AT_calling_convention attributes are only meant
18559 to describe functions' calling conventions.
18560
18561 However, because it's a necessary piece of information in
0c1b455e
TT
18562 Fortran, and before DWARF 4 DW_CC_program was the only
18563 piece of debugging information whose definition refers to
18564 a 'main program' at all, several compilers marked Fortran
18565 main programs with DW_CC_program --- even when those
18566 functions use the standard calling conventions.
18567
18568 Although DWARF now specifies a way to provide this
18569 information, we support this practice for backward
18570 compatibility. */
68511cec 18571 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18572 && cu->language == language_fortran)
48fbe735 18573 main_subprogram = 1;
68511cec 18574 break;
481860b3
GB
18575 case DW_AT_inline:
18576 if (DW_UNSND (&attr) == DW_INL_inlined
18577 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18578 may_be_inlined = 1;
481860b3 18579 break;
95554aad
TT
18580
18581 case DW_AT_import:
48fbe735 18582 if (tag == DW_TAG_imported_unit)
36586728 18583 {
0826b30a 18584 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18585 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18586 || cu->per_cu->is_dwz);
18587 }
95554aad
TT
18588 break;
18589
0c1b455e 18590 case DW_AT_main_subprogram:
48fbe735 18591 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18592 break;
18593
05caa1d2
TT
18594 case DW_AT_ranges:
18595 {
18596 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18597 but that requires a full DIE, so instead we just
18598 reimplement it. */
18599 int need_ranges_base = tag != DW_TAG_compile_unit;
18600 unsigned int ranges_offset = (DW_UNSND (&attr)
18601 + (need_ranges_base
18602 ? cu->ranges_base
18603 : 0));
18604
18605 /* Value of the DW_AT_ranges attribute is the offset in the
18606 .debug_ranges section. */
18607 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18608 nullptr))
18609 has_pc_info = 1;
18610 }
18611 break;
18612
c906108c
SS
18613 default:
18614 break;
18615 }
18616 }
18617
10d06d82
TT
18618 /* For Ada, if both the name and the linkage name appear, we prefer
18619 the latter. This lets "catch exception" work better, regardless
18620 of the order in which the name and linkage name were emitted.
18621 Really, though, this is just a workaround for the fact that gdb
18622 doesn't store both the name and the linkage name. */
18623 if (cu->language == language_ada && linkage_name != nullptr)
18624 name = linkage_name;
18625
91da1414 18626 if (high_pc_relative)
48fbe735 18627 highpc += lowpc;
91da1414 18628
9373cf26
JK
18629 if (has_low_pc_attr && has_high_pc_attr)
18630 {
18631 /* When using the GNU linker, .gnu.linkonce. sections are used to
18632 eliminate duplicate copies of functions and vtables and such.
18633 The linker will arbitrarily choose one and discard the others.
18634 The AT_*_pc values for such functions refer to local labels in
18635 these sections. If the section from that file was discarded, the
18636 labels are not in the output, so the relocs get a value of 0.
18637 If this is a discarded function, mark the pc bounds as invalid,
18638 so that GDB will ignore it. */
5989a64e 18639 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18640 {
48fbe735 18641 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18642 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18643
b98664d3 18644 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18645 "for DIE at %s [in module %s]"),
48fbe735
YQ
18646 paddress (gdbarch, lowpc),
18647 sect_offset_str (sect_off),
9d8780f0 18648 objfile_name (objfile));
9373cf26
JK
18649 }
18650 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18651 else if (lowpc >= highpc)
9373cf26 18652 {
48fbe735 18653 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18654 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18655
b98664d3 18656 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18657 "for DIE at %s [in module %s]"),
48fbe735
YQ
18658 paddress (gdbarch, lowpc),
18659 paddress (gdbarch, highpc),
18660 sect_offset_str (sect_off),
9c541725 18661 objfile_name (objfile));
9373cf26
JK
18662 }
18663 else
48fbe735 18664 has_pc_info = 1;
9373cf26 18665 }
85cbf3d3 18666
c906108c
SS
18667 return info_ptr;
18668}
18669
72bf9492
DJ
18670/* Find a cached partial DIE at OFFSET in CU. */
18671
d590ff25
YQ
18672struct partial_die_info *
18673dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18674{
18675 struct partial_die_info *lookup_die = NULL;
6f06d47b 18676 struct partial_die_info part_die (sect_off);
72bf9492 18677
9a3c8263 18678 lookup_die = ((struct partial_die_info *)
d590ff25 18679 htab_find_with_hash (partial_dies, &part_die,
9c541725 18680 to_underlying (sect_off)));
72bf9492 18681
72bf9492
DJ
18682 return lookup_die;
18683}
18684
348e048f
DE
18685/* Find a partial DIE at OFFSET, which may or may not be in CU,
18686 except in the case of .debug_types DIEs which do not reference
18687 outside their CU (they do however referencing other types via
55f1336d 18688 DW_FORM_ref_sig8). */
72bf9492 18689
122cf0f2 18690static const struct cu_partial_die_info
9c541725 18691find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18692{
5e22e966 18693 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18694 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18695 struct dwarf2_per_cu_data *per_cu = NULL;
18696 struct partial_die_info *pd = NULL;
72bf9492 18697
36586728 18698 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18699 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18700 {
d590ff25 18701 pd = cu->find_partial_die (sect_off);
5afb4e99 18702 if (pd != NULL)
fb816e8b 18703 return { cu, pd };
0d99eb77
DE
18704 /* We missed recording what we needed.
18705 Load all dies and try again. */
18706 per_cu = cu->per_cu;
5afb4e99 18707 }
0d99eb77
DE
18708 else
18709 {
18710 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18711 if (cu->per_cu->is_debug_types)
0d99eb77 18712 {
9d8780f0
SM
18713 error (_("Dwarf Error: Type Unit at offset %s contains"
18714 " external reference to offset %s [in module %s].\n"),
18715 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18716 bfd_get_filename (objfile->obfd));
18717 }
9c541725 18718 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18719 dwarf2_per_objfile);
72bf9492 18720
0d99eb77 18721 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18722 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18723
0d99eb77 18724 per_cu->cu->last_used = 0;
d590ff25 18725 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18726 }
5afb4e99 18727
dee91e82
DE
18728 /* If we didn't find it, and not all dies have been loaded,
18729 load them all and try again. */
18730
5afb4e99
DJ
18731 if (pd == NULL && per_cu->load_all_dies == 0)
18732 {
5afb4e99 18733 per_cu->load_all_dies = 1;
fd820528
DE
18734
18735 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18736 THIS_CU->cu may already be in use. So we can't just free it and
18737 replace its DIEs with the ones we read in. Instead, we leave those
18738 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18739 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18740 set. */
ab432490 18741 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18742
d590ff25 18743 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18744 }
18745
18746 if (pd == NULL)
18747 internal_error (__FILE__, __LINE__,
9d8780f0 18748 _("could not find partial DIE %s "
3e43a32a 18749 "in cache [from module %s]\n"),
9d8780f0 18750 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18751 return { per_cu->cu, pd };
72bf9492
DJ
18752}
18753
abc72ce4
DE
18754/* See if we can figure out if the class lives in a namespace. We do
18755 this by looking for a member function; its demangled name will
18756 contain namespace info, if there is any. */
18757
18758static void
18759guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18760 struct dwarf2_cu *cu)
18761{
18762 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18763 what template types look like, because the demangler
18764 frequently doesn't give the same name as the debug info. We
18765 could fix this by only using the demangled name to get the
18766 prefix (but see comment in read_structure_type). */
18767
18768 struct partial_die_info *real_pdi;
18769 struct partial_die_info *child_pdi;
18770
18771 /* If this DIE (this DIE's specification, if any) has a parent, then
18772 we should not do this. We'll prepend the parent's fully qualified
18773 name when we create the partial symbol. */
18774
18775 real_pdi = struct_pdi;
18776 while (real_pdi->has_specification)
fb816e8b 18777 {
122cf0f2
AB
18778 auto res = find_partial_die (real_pdi->spec_offset,
18779 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18780 real_pdi = res.pdi;
18781 cu = res.cu;
18782 }
abc72ce4
DE
18783
18784 if (real_pdi->die_parent != NULL)
18785 return;
18786
18787 for (child_pdi = struct_pdi->die_child;
18788 child_pdi != NULL;
18789 child_pdi = child_pdi->die_sibling)
18790 {
18791 if (child_pdi->tag == DW_TAG_subprogram
18792 && child_pdi->linkage_name != NULL)
18793 {
43816ebc
TT
18794 gdb::unique_xmalloc_ptr<char> actual_class_name
18795 (language_class_name_from_physname (cu->language_defn,
18796 child_pdi->linkage_name));
abc72ce4
DE
18797 if (actual_class_name != NULL)
18798 {
5e22e966 18799 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18800 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18801 }
18802 break;
18803 }
18804 }
18805}
18806
25c11aca
TV
18807/* Return true if a DIE with TAG may have the DW_AT_const_value
18808 attribute. */
18809
18810static bool
18811can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18812{
18813 switch (tag)
18814 {
18815 case DW_TAG_constant:
18816 case DW_TAG_enumerator:
18817 case DW_TAG_formal_parameter:
18818 case DW_TAG_template_value_param:
18819 case DW_TAG_variable:
18820 return true;
18821 }
18822
18823 return false;
18824}
18825
52356b79
YQ
18826void
18827partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18828{
abc72ce4
DE
18829 /* Once we've fixed up a die, there's no point in doing so again.
18830 This also avoids a memory leak if we were to call
18831 guess_partial_die_structure_name multiple times. */
52356b79 18832 if (fixup_called)
abc72ce4
DE
18833 return;
18834
72bf9492
DJ
18835 /* If we found a reference attribute and the DIE has no name, try
18836 to find a name in the referred to DIE. */
18837
52356b79 18838 if (name == NULL && has_specification)
72bf9492
DJ
18839 {
18840 struct partial_die_info *spec_die;
72bf9492 18841
122cf0f2 18842 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18843 spec_die = res.pdi;
18844 cu = res.cu;
72bf9492 18845
52356b79 18846 spec_die->fixup (cu);
72bf9492
DJ
18847
18848 if (spec_die->name)
18849 {
52356b79 18850 name = spec_die->name;
72bf9492
DJ
18851
18852 /* Copy DW_AT_external attribute if it is set. */
18853 if (spec_die->is_external)
52356b79 18854 is_external = spec_die->is_external;
72bf9492
DJ
18855 }
18856 }
18857
25c11aca
TV
18858 if (!has_const_value && has_specification
18859 && can_have_DW_AT_const_value_p (tag))
18860 {
18861 struct partial_die_info *spec_die;
18862
18863 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18864 spec_die = res.pdi;
18865 cu = res.cu;
18866
18867 spec_die->fixup (cu);
18868
18869 if (spec_die->has_const_value)
18870 {
18871 /* Copy DW_AT_const_value attribute if it is set. */
18872 has_const_value = spec_die->has_const_value;
18873 }
18874 }
18875
72bf9492 18876 /* Set default names for some unnamed DIEs. */
72bf9492 18877
52356b79
YQ
18878 if (name == NULL && tag == DW_TAG_namespace)
18879 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18880
abc72ce4
DE
18881 /* If there is no parent die to provide a namespace, and there are
18882 children, see if we can determine the namespace from their linkage
122d1940 18883 name. */
abc72ce4 18884 if (cu->language == language_cplus
5e22e966 18885 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18886 && die_parent == NULL
18887 && has_children
18888 && (tag == DW_TAG_class_type
18889 || tag == DW_TAG_structure_type
18890 || tag == DW_TAG_union_type))
18891 guess_partial_die_structure_name (this, cu);
abc72ce4 18892
53832f31
TT
18893 /* GCC might emit a nameless struct or union that has a linkage
18894 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18895 if (name == NULL
18896 && (tag == DW_TAG_class_type
18897 || tag == DW_TAG_interface_type
18898 || tag == DW_TAG_structure_type
18899 || tag == DW_TAG_union_type)
18900 && linkage_name != NULL)
53832f31 18901 {
43816ebc
TT
18902 gdb::unique_xmalloc_ptr<char> demangled
18903 (gdb_demangle (linkage_name, DMGL_TYPES));
18904 if (demangled != nullptr)
53832f31 18905 {
96408a79
SA
18906 const char *base;
18907
18908 /* Strip any leading namespaces/classes, keep only the base name.
18909 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18910 base = strrchr (demangled.get (), ':');
18911 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18912 base++;
18913 else
43816ebc 18914 base = demangled.get ();
96408a79 18915
5e22e966 18916 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18917 name = objfile->intern (base);
53832f31
TT
18918 }
18919 }
18920
52356b79 18921 fixup_called = 1;
72bf9492
DJ
18922}
18923
41144253 18924/* Read the .debug_loclists header contents from the given SECTION in the
18925 HEADER. */
18926static void
18927read_loclist_header (struct loclist_header *header,
18928 struct dwarf2_section_info *section)
18929{
18930 unsigned int bytes_read;
18931 bfd *abfd = section->get_bfd_owner ();
18932 const gdb_byte *info_ptr = section->buffer;
18933 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18934 info_ptr += bytes_read;
18935 header->version = read_2_bytes (abfd, info_ptr);
18936 info_ptr += 2;
18937 header->addr_size = read_1_byte (abfd, info_ptr);
18938 info_ptr += 1;
18939 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18940 info_ptr += 1;
18941 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18942}
18943
18944/* Return the DW_AT_loclists_base value for the CU. */
18945static ULONGEST
18946lookup_loclist_base (struct dwarf2_cu *cu)
18947{
18948 /* For the .dwo unit, the loclist_base points to the first offset following
18949 the header. The header consists of the following entities-
18950 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18951 bit format)
18952 2. version (2 bytes)
18953 3. address size (1 byte)
18954 4. segment selector size (1 byte)
18955 5. offset entry count (4 bytes)
18956 These sizes are derived as per the DWARFv5 standard. */
18957 if (cu->dwo_unit != nullptr)
18958 {
18959 if (cu->header.initial_length_size == 4)
18960 return LOCLIST_HEADER_SIZE32;
18961 return LOCLIST_HEADER_SIZE64;
18962 }
18963 return cu->loclist_base;
18964}
18965
18966/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18967 array of offsets in the .debug_loclists section. */
18968static CORE_ADDR
18969read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18970{
5e22e966 18971 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18972 struct objfile *objfile = dwarf2_per_objfile->objfile;
18973 bfd *abfd = objfile->obfd;
18974 ULONGEST loclist_base = lookup_loclist_base (cu);
18975 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18976
18977 section->read (objfile);
18978 if (section->buffer == NULL)
18979 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18980 "section [in module %s]"), objfile_name (objfile));
18981 struct loclist_header header;
18982 read_loclist_header (&header, section);
18983 if (loclist_index >= header.offset_entry_count)
18984 complaint (_("DW_FORM_loclistx pointing outside of "
18985 ".debug_loclists offset array [in module %s]"),
18986 objfile_name (objfile));
18987 if (loclist_base + loclist_index * cu->header.offset_size
18988 >= section->size)
18989 complaint (_("DW_FORM_loclistx pointing outside of "
18990 ".debug_loclists section [in module %s]"),
18991 objfile_name (objfile));
18992 const gdb_byte *info_ptr
18993 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18994
18995 if (cu->header.offset_size == 4)
18996 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18997 else
18998 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18999}
19000
18a8505e
AT
19001/* Process the attributes that had to be skipped in the first round. These
19002 attributes are the ones that need str_offsets_base or addr_base attributes.
19003 They could not have been processed in the first round, because at the time
19004 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19005static void
19006read_attribute_reprocess (const struct die_reader_specs *reader,
19007 struct attribute *attr)
18a8505e
AT
19008{
19009 struct dwarf2_cu *cu = reader->cu;
19010 switch (attr->form)
19011 {
19012 case DW_FORM_addrx:
19013 case DW_FORM_GNU_addr_index:
19014 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19015 break;
41144253 19016 case DW_FORM_loclistx:
19017 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19018 break;
18a8505e
AT
19019 case DW_FORM_strx:
19020 case DW_FORM_strx1:
19021 case DW_FORM_strx2:
19022 case DW_FORM_strx3:
19023 case DW_FORM_strx4:
19024 case DW_FORM_GNU_str_index:
19025 {
19026 unsigned int str_index = DW_UNSND (attr);
19027 if (reader->dwo_file != NULL)
19028 {
19029 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19030 DW_STRING_IS_CANONICAL (attr) = 0;
19031 }
19032 else
19033 {
19034 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19035 DW_STRING_IS_CANONICAL (attr) = 0;
19036 }
19037 break;
19038 }
19039 default:
19040 gdb_assert_not_reached (_("Unexpected DWARF form."));
19041 }
19042}
19043
a8329558 19044/* Read an attribute value described by an attribute form. */
c906108c 19045
d521ce57 19046static const gdb_byte *
dee91e82
DE
19047read_attribute_value (const struct die_reader_specs *reader,
19048 struct attribute *attr, unsigned form,
18a8505e
AT
19049 LONGEST implicit_const, const gdb_byte *info_ptr,
19050 bool *need_reprocess)
c906108c 19051{
dee91e82 19052 struct dwarf2_cu *cu = reader->cu;
5e22e966 19053 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19054 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19055 bfd *abfd = reader->abfd;
e7c27a73 19056 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19057 unsigned int bytes_read;
19058 struct dwarf_block *blk;
18a8505e 19059 *need_reprocess = false;
c906108c 19060
aead7601 19061 attr->form = (enum dwarf_form) form;
a8329558 19062 switch (form)
c906108c 19063 {
c906108c 19064 case DW_FORM_ref_addr:
ae411497 19065 if (cu->header.version == 2)
c8a7a66f
TT
19066 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19067 &bytes_read);
ae411497 19068 else
8266302d
TT
19069 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19070 &bytes_read);
ae411497
TT
19071 info_ptr += bytes_read;
19072 break;
36586728 19073 case DW_FORM_GNU_ref_alt:
8266302d 19074 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19075 info_ptr += bytes_read;
19076 break;
ae411497 19077 case DW_FORM_addr:
08feed99
TT
19078 {
19079 struct gdbarch *gdbarch = objfile->arch ();
19080 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19081 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19082 info_ptr += bytes_read;
19083 }
c906108c
SS
19084 break;
19085 case DW_FORM_block2:
7b5a2f43 19086 blk = dwarf_alloc_block (cu);
c906108c
SS
19087 blk->size = read_2_bytes (abfd, info_ptr);
19088 info_ptr += 2;
19089 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19090 info_ptr += blk->size;
19091 DW_BLOCK (attr) = blk;
19092 break;
19093 case DW_FORM_block4:
7b5a2f43 19094 blk = dwarf_alloc_block (cu);
c906108c
SS
19095 blk->size = read_4_bytes (abfd, info_ptr);
19096 info_ptr += 4;
19097 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19098 info_ptr += blk->size;
19099 DW_BLOCK (attr) = blk;
19100 break;
19101 case DW_FORM_data2:
19102 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19103 info_ptr += 2;
19104 break;
19105 case DW_FORM_data4:
19106 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19107 info_ptr += 4;
19108 break;
19109 case DW_FORM_data8:
19110 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19111 info_ptr += 8;
19112 break;
0224619f
JK
19113 case DW_FORM_data16:
19114 blk = dwarf_alloc_block (cu);
19115 blk->size = 16;
19116 blk->data = read_n_bytes (abfd, info_ptr, 16);
19117 info_ptr += 16;
19118 DW_BLOCK (attr) = blk;
19119 break;
2dc7f7b3 19120 case DW_FORM_sec_offset:
8266302d 19121 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19122 info_ptr += bytes_read;
19123 break;
41144253 19124 case DW_FORM_loclistx:
19125 {
19126 *need_reprocess = true;
19127 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19128 info_ptr += bytes_read;
19129 }
19130 break;
c906108c 19131 case DW_FORM_string:
9b1c24c8 19132 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19133 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19134 info_ptr += bytes_read;
19135 break;
4bdf3d34 19136 case DW_FORM_strp:
36586728
TT
19137 if (!cu->per_cu->is_dwz)
19138 {
ed2dc618
SM
19139 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19140 abfd, info_ptr, cu_header,
36586728
TT
19141 &bytes_read);
19142 DW_STRING_IS_CANONICAL (attr) = 0;
19143 info_ptr += bytes_read;
19144 break;
19145 }
19146 /* FALLTHROUGH */
43988095
JK
19147 case DW_FORM_line_strp:
19148 if (!cu->per_cu->is_dwz)
19149 {
86c0bb4c
TT
19150 DW_STRING (attr)
19151 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19152 &bytes_read);
43988095
JK
19153 DW_STRING_IS_CANONICAL (attr) = 0;
19154 info_ptr += bytes_read;
19155 break;
19156 }
19157 /* FALLTHROUGH */
36586728
TT
19158 case DW_FORM_GNU_strp_alt:
19159 {
c3699833 19160 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19161 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19162 &bytes_read);
36586728 19163
0314b390 19164 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19165 DW_STRING_IS_CANONICAL (attr) = 0;
19166 info_ptr += bytes_read;
19167 }
4bdf3d34 19168 break;
2dc7f7b3 19169 case DW_FORM_exprloc:
c906108c 19170 case DW_FORM_block:
7b5a2f43 19171 blk = dwarf_alloc_block (cu);
c906108c
SS
19172 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19173 info_ptr += bytes_read;
19174 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19175 info_ptr += blk->size;
19176 DW_BLOCK (attr) = blk;
19177 break;
19178 case DW_FORM_block1:
7b5a2f43 19179 blk = dwarf_alloc_block (cu);
c906108c
SS
19180 blk->size = read_1_byte (abfd, info_ptr);
19181 info_ptr += 1;
19182 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19183 info_ptr += blk->size;
19184 DW_BLOCK (attr) = blk;
19185 break;
19186 case DW_FORM_data1:
19187 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19188 info_ptr += 1;
19189 break;
19190 case DW_FORM_flag:
19191 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19192 info_ptr += 1;
19193 break;
2dc7f7b3
TT
19194 case DW_FORM_flag_present:
19195 DW_UNSND (attr) = 1;
19196 break;
c906108c
SS
19197 case DW_FORM_sdata:
19198 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19199 info_ptr += bytes_read;
19200 break;
19201 case DW_FORM_udata:
18a8505e 19202 case DW_FORM_rnglistx:
c906108c
SS
19203 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19204 info_ptr += bytes_read;
19205 break;
19206 case DW_FORM_ref1:
9c541725 19207 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19208 + read_1_byte (abfd, info_ptr));
c906108c
SS
19209 info_ptr += 1;
19210 break;
19211 case DW_FORM_ref2:
9c541725 19212 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19213 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19214 info_ptr += 2;
19215 break;
19216 case DW_FORM_ref4:
9c541725 19217 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19218 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19219 info_ptr += 4;
19220 break;
613e1657 19221 case DW_FORM_ref8:
9c541725 19222 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19223 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19224 info_ptr += 8;
19225 break;
55f1336d 19226 case DW_FORM_ref_sig8:
ac9ec31b 19227 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19228 info_ptr += 8;
19229 break;
c906108c 19230 case DW_FORM_ref_udata:
9c541725 19231 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19232 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19233 info_ptr += bytes_read;
19234 break;
c906108c 19235 case DW_FORM_indirect:
a8329558
KW
19236 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19237 info_ptr += bytes_read;
43988095
JK
19238 if (form == DW_FORM_implicit_const)
19239 {
19240 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19241 info_ptr += bytes_read;
19242 }
19243 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19244 info_ptr, need_reprocess);
43988095
JK
19245 break;
19246 case DW_FORM_implicit_const:
19247 DW_SND (attr) = implicit_const;
a8329558 19248 break;
336d760d 19249 case DW_FORM_addrx:
3019eac3 19250 case DW_FORM_GNU_addr_index:
18a8505e
AT
19251 *need_reprocess = true;
19252 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19253 info_ptr += bytes_read;
19254 break;
cf532bd1 19255 case DW_FORM_strx:
15f18d14
AT
19256 case DW_FORM_strx1:
19257 case DW_FORM_strx2:
19258 case DW_FORM_strx3:
19259 case DW_FORM_strx4:
3019eac3 19260 case DW_FORM_GNU_str_index:
3019eac3 19261 {
15f18d14
AT
19262 ULONGEST str_index;
19263 if (form == DW_FORM_strx1)
19264 {
19265 str_index = read_1_byte (abfd, info_ptr);
19266 info_ptr += 1;
19267 }
19268 else if (form == DW_FORM_strx2)
19269 {
19270 str_index = read_2_bytes (abfd, info_ptr);
19271 info_ptr += 2;
19272 }
19273 else if (form == DW_FORM_strx3)
19274 {
19275 str_index = read_3_bytes (abfd, info_ptr);
19276 info_ptr += 3;
19277 }
19278 else if (form == DW_FORM_strx4)
19279 {
19280 str_index = read_4_bytes (abfd, info_ptr);
19281 info_ptr += 4;
19282 }
19283 else
19284 {
19285 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19286 info_ptr += bytes_read;
19287 }
18a8505e
AT
19288 *need_reprocess = true;
19289 DW_UNSND (attr) = str_index;
19290 }
3019eac3 19291 break;
c906108c 19292 default:
8a3fe4f8 19293 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19294 dwarf_form_name (form),
19295 bfd_get_filename (abfd));
c906108c 19296 }
28e94949 19297
36586728 19298 /* Super hack. */
cd6c91b4 19299 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19300 attr->form = DW_FORM_GNU_ref_alt;
19301
28e94949
JB
19302 /* We have seen instances where the compiler tried to emit a byte
19303 size attribute of -1 which ended up being encoded as an unsigned
19304 0xffffffff. Although 0xffffffff is technically a valid size value,
19305 an object of this size seems pretty unlikely so we can relatively
19306 safely treat these cases as if the size attribute was invalid and
19307 treat them as zero by default. */
19308 if (attr->name == DW_AT_byte_size
19309 && form == DW_FORM_data4
19310 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19311 {
19312 complaint
b98664d3 19313 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19314 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19315 DW_UNSND (attr) = 0;
19316 }
28e94949 19317
c906108c
SS
19318 return info_ptr;
19319}
19320
a8329558
KW
19321/* Read an attribute described by an abbreviated attribute. */
19322
d521ce57 19323static const gdb_byte *
dee91e82
DE
19324read_attribute (const struct die_reader_specs *reader,
19325 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19326 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19327{
19328 attr->name = abbrev->name;
43988095 19329 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19330 abbrev->implicit_const, info_ptr,
19331 need_reprocess);
a8329558
KW
19332}
19333
43988095
JK
19334/* Return pointer to string at .debug_str offset STR_OFFSET. */
19335
19336static const char *
ed2dc618 19337read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19338 LONGEST str_offset)
43988095 19339{
5989a64e
SM
19340 return dwarf2_per_objfile->per_bfd->str.read_string
19341 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19342}
19343
43988095
JK
19344/* Return pointer to string at .debug_str offset as read from BUF.
19345 BUF is assumed to be in a compilation unit described by CU_HEADER.
19346 Return *BYTES_READ_PTR count of bytes read from BUF. */
19347
d521ce57 19348static const char *
ed2dc618
SM
19349read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19350 const gdb_byte *buf,
cf2c3c16
TT
19351 const struct comp_unit_head *cu_header,
19352 unsigned int *bytes_read_ptr)
19353{
8266302d 19354 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19355
4f44ae6c 19356 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19357}
19358
86c0bb4c 19359/* See read.h. */
43988095 19360
86c0bb4c
TT
19361const char *
19362dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19363 const struct comp_unit_head *cu_header,
19364 unsigned int *bytes_read_ptr)
43988095 19365{
86c0bb4c 19366 bfd *abfd = objfile->obfd;
8266302d 19367 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19368
5989a64e 19369 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19370}
19371
3019eac3 19372/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19373 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19374 ADDR_SIZE is the size of addresses from the CU header. */
19375
19376static CORE_ADDR
ed2dc618 19377read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19378 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19379 int addr_size)
3019eac3
DE
19380{
19381 struct objfile *objfile = dwarf2_per_objfile->objfile;
19382 bfd *abfd = objfile->obfd;
19383 const gdb_byte *info_ptr;
18a8505e 19384 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19385
5989a64e
SM
19386 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19387 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19388 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19389 objfile_name (objfile));
18a8505e 19390 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19391 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19392 error (_("DW_FORM_addr_index pointing outside of "
19393 ".debug_addr section [in module %s]"),
4262abfb 19394 objfile_name (objfile));
5989a64e 19395 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19396 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19397 if (addr_size == 4)
19398 return bfd_get_32 (abfd, info_ptr);
19399 else
19400 return bfd_get_64 (abfd, info_ptr);
19401}
19402
19403/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19404
19405static CORE_ADDR
19406read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19407{
5e22e966 19408 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19409 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19410}
19411
19412/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19413
19414static CORE_ADDR
d521ce57 19415read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19416 unsigned int *bytes_read)
19417{
5e22e966 19418 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19419 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19420
19421 return read_addr_index (cu, addr_index);
19422}
19423
450a1bfc 19424/* See read.h. */
3019eac3
DE
19425
19426CORE_ADDR
82ca3f51
SM
19427dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19428 dwarf2_per_objfile *dwarf2_per_objfile,
19429 unsigned int addr_index)
3019eac3 19430{
3019eac3 19431 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19432 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19433 int addr_size;
19434
3019eac3
DE
19435 /* We need addr_base and addr_size.
19436 If we don't have PER_CU->cu, we have to get it.
19437 Nasty, but the alternative is storing the needed info in PER_CU,
19438 which at this point doesn't seem justified: it's not clear how frequently
19439 it would get used and it would increase the size of every PER_CU.
19440 Entry points like dwarf2_per_cu_addr_size do a similar thing
19441 so we're not in uncharted territory here.
19442 Alas we need to be a bit more complicated as addr_base is contained
19443 in the DIE.
19444
19445 We don't need to read the entire CU(/TU).
19446 We just need the header and top level die.
a1b64ce1 19447
3019eac3 19448 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19449 For now we skip this optimization. */
3019eac3
DE
19450
19451 if (cu != NULL)
19452 {
19453 addr_base = cu->addr_base;
19454 addr_size = cu->header.addr_size;
19455 }
19456 else
19457 {
ab432490 19458 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19459 addr_base = reader.cu->addr_base;
19460 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19461 }
19462
ed2dc618
SM
19463 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19464 addr_size);
3019eac3
DE
19465}
19466
18a8505e
AT
19467/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19468 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19469 DWO file. */
3019eac3 19470
d521ce57 19471static const char *
18a8505e
AT
19472read_str_index (struct dwarf2_cu *cu,
19473 struct dwarf2_section_info *str_section,
19474 struct dwarf2_section_info *str_offsets_section,
19475 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19476{
5e22e966 19477 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19478 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19479 const char *objf_name = objfile_name (objfile);
3019eac3 19480 bfd *abfd = objfile->obfd;
d521ce57 19481 const gdb_byte *info_ptr;
3019eac3 19482 ULONGEST str_offset;
cf532bd1 19483 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19484
96b79293
TT
19485 str_section->read (objfile);
19486 str_offsets_section->read (objfile);
73869dc2 19487 if (str_section->buffer == NULL)
18a8505e 19488 error (_("%s used without %s section"
9d8780f0 19489 " in CU at offset %s [in module %s]"),
96b79293 19490 form_name, str_section->get_name (),
18a8505e 19491 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19492 if (str_offsets_section->buffer == NULL)
18a8505e 19493 error (_("%s used without %s section"
9d8780f0 19494 " in CU at offset %s [in module %s]"),
96b79293 19495 form_name, str_section->get_name (),
18a8505e 19496 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19497 info_ptr = (str_offsets_section->buffer
18a8505e 19498 + str_offsets_base
3019eac3
DE
19499 + str_index * cu->header.offset_size);
19500 if (cu->header.offset_size == 4)
19501 str_offset = bfd_get_32 (abfd, info_ptr);
19502 else
19503 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19504 if (str_offset >= str_section->size)
57d63ce2 19505 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19506 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19507 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19508 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19509}
19510
18a8505e
AT
19511/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19512
19513static const char *
19514read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19515{
19516 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19517 ? reader->cu->header.addr_size : 0;
19518 return read_str_index (reader->cu,
19519 &reader->dwo_file->sections.str,
19520 &reader->dwo_file->sections.str_offsets,
19521 str_offsets_base, str_index);
19522}
19523
19524/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19525
19526static const char *
19527read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19528{
5e22e966 19529 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19530 const char *objf_name = objfile_name (objfile);
19531 static const char form_name[] = "DW_FORM_GNU_str_index";
19532 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19533
19534 if (!cu->str_offsets_base.has_value ())
19535 error (_("%s used in Fission stub without %s"
19536 " in CU at offset 0x%lx [in module %s]"),
19537 form_name, str_offsets_attr_name,
19538 (long) cu->header.offset_size, objf_name);
19539
19540 return read_str_index (cu,
5e22e966
SM
19541 &cu->per_objfile->per_bfd->str,
19542 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19543 *cu->str_offsets_base, str_index);
19544}
19545
3019eac3
DE
19546/* Return the length of an LEB128 number in BUF. */
19547
19548static int
19549leb128_size (const gdb_byte *buf)
19550{
19551 const gdb_byte *begin = buf;
19552 gdb_byte byte;
19553
19554 while (1)
19555 {
19556 byte = *buf++;
19557 if ((byte & 128) == 0)
19558 return buf - begin;
19559 }
19560}
19561
c906108c 19562static void
e142c38c 19563set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19564{
19565 switch (lang)
19566 {
19567 case DW_LANG_C89:
76bee0cc 19568 case DW_LANG_C99:
0cfd832f 19569 case DW_LANG_C11:
c906108c 19570 case DW_LANG_C:
d1be3247 19571 case DW_LANG_UPC:
e142c38c 19572 cu->language = language_c;
c906108c 19573 break;
9c37b5ae 19574 case DW_LANG_Java:
c906108c 19575 case DW_LANG_C_plus_plus:
0cfd832f
MW
19576 case DW_LANG_C_plus_plus_11:
19577 case DW_LANG_C_plus_plus_14:
e142c38c 19578 cu->language = language_cplus;
c906108c 19579 break;
6aecb9c2
JB
19580 case DW_LANG_D:
19581 cu->language = language_d;
19582 break;
c906108c
SS
19583 case DW_LANG_Fortran77:
19584 case DW_LANG_Fortran90:
b21b22e0 19585 case DW_LANG_Fortran95:
f7de9aab
MW
19586 case DW_LANG_Fortran03:
19587 case DW_LANG_Fortran08:
e142c38c 19588 cu->language = language_fortran;
c906108c 19589 break;
a766d390
DE
19590 case DW_LANG_Go:
19591 cu->language = language_go;
19592 break;
c906108c 19593 case DW_LANG_Mips_Assembler:
e142c38c 19594 cu->language = language_asm;
c906108c
SS
19595 break;
19596 case DW_LANG_Ada83:
8aaf0b47 19597 case DW_LANG_Ada95:
bc5f45f8
JB
19598 cu->language = language_ada;
19599 break;
72019c9c
GM
19600 case DW_LANG_Modula2:
19601 cu->language = language_m2;
19602 break;
fe8e67fd
PM
19603 case DW_LANG_Pascal83:
19604 cu->language = language_pascal;
19605 break;
22566fbd
DJ
19606 case DW_LANG_ObjC:
19607 cu->language = language_objc;
19608 break;
c44af4eb
TT
19609 case DW_LANG_Rust:
19610 case DW_LANG_Rust_old:
19611 cu->language = language_rust;
19612 break;
c906108c
SS
19613 case DW_LANG_Cobol74:
19614 case DW_LANG_Cobol85:
c906108c 19615 default:
e142c38c 19616 cu->language = language_minimal;
c906108c
SS
19617 break;
19618 }
e142c38c 19619 cu->language_defn = language_def (cu->language);
c906108c
SS
19620}
19621
19622/* Return the named attribute or NULL if not there. */
19623
19624static struct attribute *
e142c38c 19625dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19626{
a48e046c 19627 for (;;)
c906108c 19628 {
a48e046c
TT
19629 unsigned int i;
19630 struct attribute *spec = NULL;
19631
19632 for (i = 0; i < die->num_attrs; ++i)
19633 {
19634 if (die->attrs[i].name == name)
19635 return &die->attrs[i];
19636 if (die->attrs[i].name == DW_AT_specification
19637 || die->attrs[i].name == DW_AT_abstract_origin)
19638 spec = &die->attrs[i];
19639 }
19640
19641 if (!spec)
19642 break;
c906108c 19643
f2f0e013 19644 die = follow_die_ref (die, spec, &cu);
f2f0e013 19645 }
c5aa993b 19646
c906108c
SS
19647 return NULL;
19648}
19649
7d45c7c3
KB
19650/* Return the string associated with a string-typed attribute, or NULL if it
19651 is either not found or is of an incorrect type. */
19652
19653static const char *
19654dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19655{
19656 struct attribute *attr;
19657 const char *str = NULL;
19658
19659 attr = dwarf2_attr (die, name, cu);
19660
19661 if (attr != NULL)
19662 {
e61108c9
TT
19663 str = attr->value_as_string ();
19664 if (str == nullptr)
b98664d3 19665 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19666 "DIE at %s in module %s"),
19667 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19668 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19669 }
19670
19671 return str;
19672}
19673
a084a2a6 19674/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19675 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19676static const char *
19677dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19678{
19679 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19680 if (dwo_name == nullptr)
19681 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19682 return dwo_name;
19683}
19684
05cf31d1
JB
19685/* Return non-zero iff the attribute NAME is defined for the given DIE,
19686 and holds a non-zero value. This function should only be used for
2dc7f7b3 19687 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19688
19689static int
19690dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19691{
19692 struct attribute *attr = dwarf2_attr (die, name, cu);
19693
19694 return (attr && DW_UNSND (attr));
19695}
19696
3ca72b44 19697static int
e142c38c 19698die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19699{
05cf31d1
JB
19700 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19701 which value is non-zero. However, we have to be careful with
19702 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19703 (via dwarf2_flag_true_p) follows this attribute. So we may
19704 end up accidently finding a declaration attribute that belongs
19705 to a different DIE referenced by the specification attribute,
19706 even though the given DIE does not have a declaration attribute. */
19707 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19708 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19709}
19710
63d06c5c 19711/* Return the die giving the specification for DIE, if there is
f2f0e013 19712 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19713 containing the return value on output. If there is no
19714 specification, but there is an abstract origin, that is
19715 returned. */
63d06c5c
DC
19716
19717static struct die_info *
f2f0e013 19718die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19719{
f2f0e013
DJ
19720 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19721 *spec_cu);
63d06c5c 19722
edb3359d
DJ
19723 if (spec_attr == NULL)
19724 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19725
63d06c5c
DC
19726 if (spec_attr == NULL)
19727 return NULL;
19728 else
f2f0e013 19729 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19730}
c906108c 19731
527f3840
JK
19732/* Stub for free_line_header to match void * callback types. */
19733
19734static void
19735free_line_header_voidp (void *arg)
19736{
9a3c8263 19737 struct line_header *lh = (struct line_header *) arg;
527f3840 19738
fff8551c 19739 delete lh;
527f3840
JK
19740}
19741
83769d0b 19742/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19743
19744static struct dwarf2_section_info *
19745get_debug_line_section (struct dwarf2_cu *cu)
19746{
19747 struct dwarf2_section_info *section;
5e22e966 19748 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19749
19750 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19751 DWO file. */
19752 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19753 section = &cu->dwo_unit->dwo_file->sections.line;
19754 else if (cu->per_cu->is_dwz)
19755 {
c3699833 19756 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19757
19758 section = &dwz->line;
19759 }
19760 else
5989a64e 19761 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19762
19763 return section;
19764}
19765
debd256d 19766/* Read the statement program header starting at OFFSET in
3019eac3 19767 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19768 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19769 Returns NULL if there is a problem reading the header, e.g., if it
19770 has a version we don't understand.
debd256d
JB
19771
19772 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19773 the returned object point into the dwarf line section buffer,
19774 and must not be freed. */
ae2de4f8 19775
fff8551c 19776static line_header_up
9c541725 19777dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19778{
3019eac3 19779 struct dwarf2_section_info *section;
5e22e966 19780 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19781
36586728 19782 section = get_debug_line_section (cu);
96b79293 19783 section->read (dwarf2_per_objfile->objfile);
3019eac3 19784 if (section->buffer == NULL)
debd256d 19785 {
3019eac3 19786 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19787 complaint (_("missing .debug_line.dwo section"));
3019eac3 19788 else
b98664d3 19789 complaint (_("missing .debug_line section"));
debd256d
JB
19790 return 0;
19791 }
19792
0df7ad3a
TT
19793 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19794 dwarf2_per_objfile, section,
19795 &cu->header);
debd256d 19796}
c906108c 19797
c6da4cef 19798/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19799 Return the file name of the psymtab for the given file_entry.
c6da4cef 19800 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19801 If space for the result is malloc'd, *NAME_HOLDER will be set.
19802 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19803
d521ce57 19804static const char *
7ba99d21 19805psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19806 const dwarf2_psymtab *pst,
c89b44cd
TT
19807 const char *comp_dir,
19808 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19809{
d521ce57
TT
19810 const char *include_name = fe.name;
19811 const char *include_name_to_compare = include_name;
72b9f47f 19812 const char *pst_filename;
c6da4cef
DE
19813 int file_is_pst;
19814
8c43009f 19815 const char *dir_name = fe.include_dir (lh);
c6da4cef 19816
c89b44cd 19817 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19818 if (!IS_ABSOLUTE_PATH (include_name)
19819 && (dir_name != NULL || comp_dir != NULL))
19820 {
19821 /* Avoid creating a duplicate psymtab for PST.
19822 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19823 Before we do the comparison, however, we need to account
19824 for DIR_NAME and COMP_DIR.
19825 First prepend dir_name (if non-NULL). If we still don't
19826 have an absolute path prepend comp_dir (if non-NULL).
19827 However, the directory we record in the include-file's
19828 psymtab does not contain COMP_DIR (to match the
19829 corresponding symtab(s)).
19830
19831 Example:
19832
19833 bash$ cd /tmp
19834 bash$ gcc -g ./hello.c
19835 include_name = "hello.c"
19836 dir_name = "."
19837 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19838 DW_AT_name = "./hello.c"
19839
19840 */
c6da4cef
DE
19841
19842 if (dir_name != NULL)
19843 {
c89b44cd
TT
19844 name_holder->reset (concat (dir_name, SLASH_STRING,
19845 include_name, (char *) NULL));
19846 include_name = name_holder->get ();
c6da4cef 19847 include_name_to_compare = include_name;
c6da4cef
DE
19848 }
19849 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19850 {
c89b44cd
TT
19851 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19852 include_name, (char *) NULL));
19853 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19854 }
19855 }
19856
19857 pst_filename = pst->filename;
c89b44cd 19858 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19859 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19860 {
c89b44cd
TT
19861 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19862 pst_filename, (char *) NULL));
19863 pst_filename = copied_name.get ();
c6da4cef
DE
19864 }
19865
1e3fad37 19866 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19867
c6da4cef
DE
19868 if (file_is_pst)
19869 return NULL;
19870 return include_name;
19871}
19872
d9b3de22
DE
19873/* State machine to track the state of the line number program. */
19874
6f77053d 19875class lnp_state_machine
d9b3de22 19876{
6f77053d
PA
19877public:
19878 /* Initialize a machine state for the start of a line number
19879 program. */
804d2729
TT
19880 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19881 bool record_lines_p);
6f77053d 19882
8c43009f
PA
19883 file_entry *current_file ()
19884 {
19885 /* lh->file_names is 0-based, but the file name numbers in the
19886 statement program are 1-based. */
6f77053d
PA
19887 return m_line_header->file_name_at (m_file);
19888 }
19889
19890 /* Record the line in the state machine. END_SEQUENCE is true if
19891 we're processing the end of a sequence. */
19892 void record_line (bool end_sequence);
19893
7ab6656f
OJ
19894 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19895 nop-out rest of the lines in this sequence. */
6f77053d
PA
19896 void check_line_address (struct dwarf2_cu *cu,
19897 const gdb_byte *line_ptr,
7ab6656f 19898 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19899
19900 void handle_set_discriminator (unsigned int discriminator)
19901 {
19902 m_discriminator = discriminator;
19903 m_line_has_non_zero_discriminator |= discriminator != 0;
19904 }
19905
19906 /* Handle DW_LNE_set_address. */
19907 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19908 {
19909 m_op_index = 0;
19910 address += baseaddr;
19911 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19912 }
19913
19914 /* Handle DW_LNS_advance_pc. */
19915 void handle_advance_pc (CORE_ADDR adjust);
19916
19917 /* Handle a special opcode. */
19918 void handle_special_opcode (unsigned char op_code);
19919
19920 /* Handle DW_LNS_advance_line. */
19921 void handle_advance_line (int line_delta)
19922 {
19923 advance_line (line_delta);
19924 }
19925
19926 /* Handle DW_LNS_set_file. */
19927 void handle_set_file (file_name_index file);
19928
19929 /* Handle DW_LNS_negate_stmt. */
19930 void handle_negate_stmt ()
19931 {
19932 m_is_stmt = !m_is_stmt;
19933 }
19934
19935 /* Handle DW_LNS_const_add_pc. */
19936 void handle_const_add_pc ();
19937
19938 /* Handle DW_LNS_fixed_advance_pc. */
19939 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19940 {
19941 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19942 m_op_index = 0;
19943 }
19944
19945 /* Handle DW_LNS_copy. */
19946 void handle_copy ()
19947 {
19948 record_line (false);
19949 m_discriminator = 0;
19950 }
19951
19952 /* Handle DW_LNE_end_sequence. */
19953 void handle_end_sequence ()
19954 {
804d2729 19955 m_currently_recording_lines = true;
6f77053d
PA
19956 }
19957
19958private:
19959 /* Advance the line by LINE_DELTA. */
19960 void advance_line (int line_delta)
19961 {
19962 m_line += line_delta;
19963
19964 if (line_delta != 0)
19965 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19966 }
19967
804d2729
TT
19968 struct dwarf2_cu *m_cu;
19969
6f77053d
PA
19970 gdbarch *m_gdbarch;
19971
19972 /* True if we're recording lines.
19973 Otherwise we're building partial symtabs and are just interested in
19974 finding include files mentioned by the line number program. */
19975 bool m_record_lines_p;
19976
8c43009f 19977 /* The line number header. */
6f77053d 19978 line_header *m_line_header;
8c43009f 19979
6f77053d
PA
19980 /* These are part of the standard DWARF line number state machine,
19981 and initialized according to the DWARF spec. */
d9b3de22 19982
6f77053d 19983 unsigned char m_op_index = 0;
7ba99d21
AT
19984 /* The line table index of the current file. */
19985 file_name_index m_file = 1;
6f77053d
PA
19986 unsigned int m_line = 1;
19987
19988 /* These are initialized in the constructor. */
19989
19990 CORE_ADDR m_address;
19991 bool m_is_stmt;
19992 unsigned int m_discriminator;
d9b3de22
DE
19993
19994 /* Additional bits of state we need to track. */
19995
19996 /* The last file that we called dwarf2_start_subfile for.
19997 This is only used for TLLs. */
6f77053d 19998 unsigned int m_last_file = 0;
d9b3de22 19999 /* The last file a line number was recorded for. */
6f77053d 20000 struct subfile *m_last_subfile = NULL;
d9b3de22 20001
804d2729
TT
20002 /* When true, record the lines we decode. */
20003 bool m_currently_recording_lines = false;
d9b3de22
DE
20004
20005 /* The last line number that was recorded, used to coalesce
20006 consecutive entries for the same line. This can happen, for
20007 example, when discriminators are present. PR 17276. */
6f77053d
PA
20008 unsigned int m_last_line = 0;
20009 bool m_line_has_non_zero_discriminator = false;
8c43009f 20010};
d9b3de22 20011
6f77053d
PA
20012void
20013lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20014{
20015 CORE_ADDR addr_adj = (((m_op_index + adjust)
20016 / m_line_header->maximum_ops_per_instruction)
20017 * m_line_header->minimum_instruction_length);
20018 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20019 m_op_index = ((m_op_index + adjust)
20020 % m_line_header->maximum_ops_per_instruction);
20021}
d9b3de22 20022
6f77053d
PA
20023void
20024lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20025{
6f77053d 20026 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20027 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20028 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20029 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20030 / m_line_header->maximum_ops_per_instruction)
20031 * m_line_header->minimum_instruction_length);
20032 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20033 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20034 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20035
258bf0ee 20036 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20037 advance_line (line_delta);
20038 record_line (false);
20039 m_discriminator = 0;
20040}
d9b3de22 20041
6f77053d
PA
20042void
20043lnp_state_machine::handle_set_file (file_name_index file)
20044{
20045 m_file = file;
20046
20047 const file_entry *fe = current_file ();
20048 if (fe == NULL)
20049 dwarf2_debug_line_missing_file_complaint ();
20050 else if (m_record_lines_p)
20051 {
20052 const char *dir = fe->include_dir (m_line_header);
20053
c24bdb02 20054 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20055 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20056 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20057 }
20058}
20059
20060void
20061lnp_state_machine::handle_const_add_pc ()
20062{
20063 CORE_ADDR adjust
20064 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20065
20066 CORE_ADDR addr_adj
20067 = (((m_op_index + adjust)
20068 / m_line_header->maximum_ops_per_instruction)
20069 * m_line_header->minimum_instruction_length);
20070
20071 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20072 m_op_index = ((m_op_index + adjust)
20073 % m_line_header->maximum_ops_per_instruction);
20074}
d9b3de22 20075
a05a36a5
DE
20076/* Return non-zero if we should add LINE to the line number table.
20077 LINE is the line to add, LAST_LINE is the last line that was added,
20078 LAST_SUBFILE is the subfile for LAST_LINE.
20079 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20080 had a non-zero discriminator.
20081
20082 We have to be careful in the presence of discriminators.
20083 E.g., for this line:
20084
20085 for (i = 0; i < 100000; i++);
20086
20087 clang can emit four line number entries for that one line,
20088 each with a different discriminator.
20089 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20090
20091 However, we want gdb to coalesce all four entries into one.
20092 Otherwise the user could stepi into the middle of the line and
20093 gdb would get confused about whether the pc really was in the
20094 middle of the line.
20095
20096 Things are further complicated by the fact that two consecutive
20097 line number entries for the same line is a heuristic used by gcc
20098 to denote the end of the prologue. So we can't just discard duplicate
20099 entries, we have to be selective about it. The heuristic we use is
20100 that we only collapse consecutive entries for the same line if at least
20101 one of those entries has a non-zero discriminator. PR 17276.
20102
20103 Note: Addresses in the line number state machine can never go backwards
20104 within one sequence, thus this coalescing is ok. */
20105
20106static int
804d2729
TT
20107dwarf_record_line_p (struct dwarf2_cu *cu,
20108 unsigned int line, unsigned int last_line,
a05a36a5
DE
20109 int line_has_non_zero_discriminator,
20110 struct subfile *last_subfile)
20111{
c24bdb02 20112 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20113 return 1;
20114 if (line != last_line)
20115 return 1;
20116 /* Same line for the same file that we've seen already.
20117 As a last check, for pr 17276, only record the line if the line
20118 has never had a non-zero discriminator. */
20119 if (!line_has_non_zero_discriminator)
20120 return 1;
20121 return 0;
20122}
20123
804d2729
TT
20124/* Use the CU's builder to record line number LINE beginning at
20125 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20126
20127static void
d9b3de22 20128dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20129 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20130 struct dwarf2_cu *cu)
252a6764
DE
20131{
20132 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20133
27e0867f
DE
20134 if (dwarf_line_debug)
20135 {
20136 fprintf_unfiltered (gdb_stdlog,
20137 "Recording line %u, file %s, address %s\n",
20138 line, lbasename (subfile->name),
20139 paddress (gdbarch, address));
20140 }
20141
804d2729 20142 if (cu != nullptr)
8c95582d 20143 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20144}
20145
20146/* Subroutine of dwarf_decode_lines_1 to simplify it.
20147 Mark the end of a set of line number records.
d9b3de22 20148 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20149 If SUBFILE is NULL the request is ignored. */
20150
20151static void
20152dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20153 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20154{
27e0867f
DE
20155 if (subfile == NULL)
20156 return;
20157
20158 if (dwarf_line_debug)
20159 {
20160 fprintf_unfiltered (gdb_stdlog,
20161 "Finishing current line, file %s, address %s\n",
20162 lbasename (subfile->name),
20163 paddress (gdbarch, address));
20164 }
20165
8c95582d 20166 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20167}
20168
6f77053d
PA
20169void
20170lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20171{
d9b3de22
DE
20172 if (dwarf_line_debug)
20173 {
20174 fprintf_unfiltered (gdb_stdlog,
20175 "Processing actual line %u: file %u,"
94a72be7 20176 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20177 m_line, m_file,
6f77053d 20178 paddress (m_gdbarch, m_address),
94a72be7
AB
20179 m_is_stmt, m_discriminator,
20180 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20181 }
20182
6f77053d 20183 file_entry *fe = current_file ();
8c43009f
PA
20184
20185 if (fe == NULL)
d9b3de22
DE
20186 dwarf2_debug_line_missing_file_complaint ();
20187 /* For now we ignore lines not starting on an instruction boundary.
20188 But not when processing end_sequence for compatibility with the
20189 previous version of the code. */
6f77053d 20190 else if (m_op_index == 0 || end_sequence)
d9b3de22 20191 {
8c43009f 20192 fe->included_p = 1;
8c95582d 20193 if (m_record_lines_p)
d9b3de22 20194 {
c24bdb02 20195 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20196 || end_sequence)
d9b3de22 20197 {
804d2729
TT
20198 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20199 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20200 }
20201
20202 if (!end_sequence)
20203 {
8c95582d
AB
20204 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20205
804d2729 20206 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20207 m_line_has_non_zero_discriminator,
20208 m_last_subfile))
d9b3de22 20209 {
c24bdb02 20210 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20211 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20212 builder->get_current_subfile (),
8c95582d 20213 m_line, m_address, is_stmt,
804d2729 20214 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20215 }
c24bdb02 20216 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20217 m_last_line = m_line;
d9b3de22
DE
20218 }
20219 }
20220 }
20221}
20222
804d2729
TT
20223lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20224 line_header *lh, bool record_lines_p)
d9b3de22 20225{
804d2729 20226 m_cu = cu;
6f77053d
PA
20227 m_gdbarch = arch;
20228 m_record_lines_p = record_lines_p;
20229 m_line_header = lh;
d9b3de22 20230
804d2729 20231 m_currently_recording_lines = true;
d9b3de22 20232
d9b3de22
DE
20233 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20234 was a line entry for it so that the backend has a chance to adjust it
20235 and also record it in case it needs it. This is currently used by MIPS
20236 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20237 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20238 m_is_stmt = lh->default_is_stmt;
20239 m_discriminator = 0;
252a6764
DE
20240}
20241
6f77053d
PA
20242void
20243lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20244 const gdb_byte *line_ptr,
7ab6656f 20245 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20246{
7ab6656f
OJ
20247 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20248 the pc range of the CU. However, we restrict the test to only ADDRESS
20249 values of zero to preserve GDB's previous behaviour which is to handle
20250 the specific case of a function being GC'd by the linker. */
924c2928 20251
7ab6656f 20252 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20253 {
20254 /* This line table is for a function which has been
20255 GCd by the linker. Ignore it. PR gdb/12528 */
20256
5e22e966 20257 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20258 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20259
b98664d3 20260 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20261 line_offset, objfile_name (objfile));
804d2729
TT
20262 m_currently_recording_lines = false;
20263 /* Note: m_currently_recording_lines is left as false until we see
20264 DW_LNE_end_sequence. */
924c2928
DE
20265 }
20266}
20267
f3f5162e 20268/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20269 Process the line number information in LH.
20270 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20271 program in order to set included_p for every referenced header. */
debd256d 20272
c906108c 20273static void
43f3e411
DE
20274dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20275 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20276{
d521ce57
TT
20277 const gdb_byte *line_ptr, *extended_end;
20278 const gdb_byte *line_end;
a8c50c1f 20279 unsigned int bytes_read, extended_len;
699ca60a 20280 unsigned char op_code, extended_op;
e142c38c 20281 CORE_ADDR baseaddr;
5e22e966 20282 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20283 bfd *abfd = objfile->obfd;
08feed99 20284 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20285 /* True if we're recording line info (as opposed to building partial
20286 symtabs and just interested in finding include files mentioned by
20287 the line number program). */
20288 bool record_lines_p = !decode_for_pst_p;
e142c38c 20289
b3b3bada 20290 baseaddr = objfile->text_section_offset ();
c906108c 20291
debd256d
JB
20292 line_ptr = lh->statement_program_start;
20293 line_end = lh->statement_program_end;
c906108c
SS
20294
20295 /* Read the statement sequences until there's nothing left. */
20296 while (line_ptr < line_end)
20297 {
6f77053d
PA
20298 /* The DWARF line number program state machine. Reset the state
20299 machine at the start of each sequence. */
804d2729 20300 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20301 bool end_sequence = false;
d9b3de22 20302
8c43009f 20303 if (record_lines_p)
c906108c 20304 {
8c43009f
PA
20305 /* Start a subfile for the current file of the state
20306 machine. */
20307 const file_entry *fe = state_machine.current_file ();
20308
20309 if (fe != NULL)
804d2729 20310 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20311 }
20312
a738430d 20313 /* Decode the table. */
d9b3de22 20314 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20315 {
20316 op_code = read_1_byte (abfd, line_ptr);
20317 line_ptr += 1;
9aa1fe7e 20318
debd256d 20319 if (op_code >= lh->opcode_base)
6e70227d 20320 {
8e07a239 20321 /* Special opcode. */
6f77053d 20322 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20323 }
20324 else switch (op_code)
c906108c
SS
20325 {
20326 case DW_LNS_extended_op:
3e43a32a
MS
20327 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20328 &bytes_read);
473b7be6 20329 line_ptr += bytes_read;
a8c50c1f 20330 extended_end = line_ptr + extended_len;
c906108c
SS
20331 extended_op = read_1_byte (abfd, line_ptr);
20332 line_ptr += 1;
20333 switch (extended_op)
20334 {
20335 case DW_LNE_end_sequence:
6f77053d
PA
20336 state_machine.handle_end_sequence ();
20337 end_sequence = true;
c906108c
SS
20338 break;
20339 case DW_LNE_set_address:
d9b3de22
DE
20340 {
20341 CORE_ADDR address
c8a7a66f 20342 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20343 line_ptr += bytes_read;
6f77053d
PA
20344
20345 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20346 lowpc - baseaddr, address);
6f77053d 20347 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20348 }
c906108c
SS
20349 break;
20350 case DW_LNE_define_file:
debd256d 20351 {
d521ce57 20352 const char *cur_file;
ecfb656c
PA
20353 unsigned int mod_time, length;
20354 dir_index dindex;
6e70227d 20355
3e43a32a
MS
20356 cur_file = read_direct_string (abfd, line_ptr,
20357 &bytes_read);
debd256d 20358 line_ptr += bytes_read;
ecfb656c 20359 dindex = (dir_index)
debd256d
JB
20360 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20361 line_ptr += bytes_read;
20362 mod_time =
20363 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20364 line_ptr += bytes_read;
20365 length =
20366 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20367 line_ptr += bytes_read;
ecfb656c 20368 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20369 }
c906108c 20370 break;
d0c6ba3d 20371 case DW_LNE_set_discriminator:
6f77053d
PA
20372 {
20373 /* The discriminator is not interesting to the
20374 debugger; just ignore it. We still need to
20375 check its value though:
20376 if there are consecutive entries for the same
20377 (non-prologue) line we want to coalesce them.
20378 PR 17276. */
20379 unsigned int discr
20380 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20381 line_ptr += bytes_read;
20382
20383 state_machine.handle_set_discriminator (discr);
20384 }
d0c6ba3d 20385 break;
c906108c 20386 default:
b98664d3 20387 complaint (_("mangled .debug_line section"));
debd256d 20388 return;
c906108c 20389 }
a8c50c1f
DJ
20390 /* Make sure that we parsed the extended op correctly. If e.g.
20391 we expected a different address size than the producer used,
20392 we may have read the wrong number of bytes. */
20393 if (line_ptr != extended_end)
20394 {
b98664d3 20395 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20396 return;
20397 }
c906108c
SS
20398 break;
20399 case DW_LNS_copy:
6f77053d 20400 state_machine.handle_copy ();
c906108c
SS
20401 break;
20402 case DW_LNS_advance_pc:
2dc7f7b3
TT
20403 {
20404 CORE_ADDR adjust
20405 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20406 line_ptr += bytes_read;
6f77053d
PA
20407
20408 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20409 }
c906108c
SS
20410 break;
20411 case DW_LNS_advance_line:
a05a36a5
DE
20412 {
20413 int line_delta
20414 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20415 line_ptr += bytes_read;
6f77053d
PA
20416
20417 state_machine.handle_advance_line (line_delta);
a05a36a5 20418 }
c906108c
SS
20419 break;
20420 case DW_LNS_set_file:
d9b3de22 20421 {
6f77053d 20422 file_name_index file
ecfb656c
PA
20423 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20424 &bytes_read);
d9b3de22 20425 line_ptr += bytes_read;
8c43009f 20426
6f77053d 20427 state_machine.handle_set_file (file);
d9b3de22 20428 }
c906108c
SS
20429 break;
20430 case DW_LNS_set_column:
0ad93d4f 20431 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20432 line_ptr += bytes_read;
20433 break;
20434 case DW_LNS_negate_stmt:
6f77053d 20435 state_machine.handle_negate_stmt ();
c906108c
SS
20436 break;
20437 case DW_LNS_set_basic_block:
c906108c 20438 break;
c2c6d25f
JM
20439 /* Add to the address register of the state machine the
20440 address increment value corresponding to special opcode
a738430d
MK
20441 255. I.e., this value is scaled by the minimum
20442 instruction length since special opcode 255 would have
b021a221 20443 scaled the increment. */
c906108c 20444 case DW_LNS_const_add_pc:
6f77053d 20445 state_machine.handle_const_add_pc ();
c906108c
SS
20446 break;
20447 case DW_LNS_fixed_advance_pc:
3e29f34a 20448 {
6f77053d 20449 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20450 line_ptr += 2;
6f77053d
PA
20451
20452 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20453 }
c906108c 20454 break;
9aa1fe7e 20455 default:
a738430d
MK
20456 {
20457 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20458 int i;
a738430d 20459
debd256d 20460 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20461 {
20462 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20463 line_ptr += bytes_read;
20464 }
20465 }
c906108c
SS
20466 }
20467 }
d9b3de22
DE
20468
20469 if (!end_sequence)
20470 dwarf2_debug_line_missing_end_sequence_complaint ();
20471
20472 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20473 in which case we still finish recording the last line). */
6f77053d 20474 state_machine.record_line (true);
c906108c 20475 }
f3f5162e
DE
20476}
20477
20478/* Decode the Line Number Program (LNP) for the given line_header
20479 structure and CU. The actual information extracted and the type
20480 of structures created from the LNP depends on the value of PST.
20481
20482 1. If PST is NULL, then this procedure uses the data from the program
20483 to create all necessary symbol tables, and their linetables.
20484
20485 2. If PST is not NULL, this procedure reads the program to determine
20486 the list of files included by the unit represented by PST, and
20487 builds all the associated partial symbol tables.
20488
20489 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20490 It is used for relative paths in the line table.
20491 NOTE: When processing partial symtabs (pst != NULL),
20492 comp_dir == pst->dirname.
20493
20494 NOTE: It is important that psymtabs have the same file name (via strcmp)
20495 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20496 symtab we don't use it in the name of the psymtabs we create.
20497 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20498 A good testcase for this is mb-inline.exp.
20499
527f3840
JK
20500 LOWPC is the lowest address in CU (or 0 if not known).
20501
20502 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20503 for its PC<->lines mapping information. Otherwise only the filename
20504 table is read in. */
f3f5162e
DE
20505
20506static void
20507dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20508 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20509 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20510{
5e22e966 20511 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20512 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20513
527f3840
JK
20514 if (decode_mapping)
20515 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20516
20517 if (decode_for_pst_p)
20518 {
aaa75496
JB
20519 /* Now that we're done scanning the Line Header Program, we can
20520 create the psymtab of each included file. */
7ba99d21
AT
20521 for (auto &file_entry : lh->file_names ())
20522 if (file_entry.included_p == 1)
aaa75496 20523 {
c89b44cd 20524 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20525 const char *include_name =
7ba99d21
AT
20526 psymtab_include_file_name (lh, file_entry, pst,
20527 comp_dir, &name_holder);
c6da4cef 20528 if (include_name != NULL)
aaa75496
JB
20529 dwarf2_create_include_psymtab (include_name, pst, objfile);
20530 }
20531 }
cb1df416
DJ
20532 else
20533 {
20534 /* Make sure a symtab is created for every file, even files
20535 which contain only variables (i.e. no code with associated
20536 line numbers). */
c24bdb02
KS
20537 buildsym_compunit *builder = cu->get_builder ();
20538 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20539
7ba99d21 20540 for (auto &fe : lh->file_names ())
cb1df416 20541 {
804d2729 20542 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20543 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20544 {
c24bdb02 20545 builder->get_current_subfile ()->symtab
804d2729 20546 = allocate_symtab (cust,
c24bdb02 20547 builder->get_current_subfile ()->name);
43f3e411 20548 }
c24bdb02 20549 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20550 }
20551 }
c906108c
SS
20552}
20553
20554/* Start a subfile for DWARF. FILENAME is the name of the file and
20555 DIRNAME the name of the source directory which contains FILENAME
4d663531 20556 or NULL if not known.
c906108c
SS
20557 This routine tries to keep line numbers from identical absolute and
20558 relative file names in a common subfile.
20559
20560 Using the `list' example from the GDB testsuite, which resides in
20561 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20562 of /srcdir/list0.c yields the following debugging information for list0.c:
20563
c5aa993b 20564 DW_AT_name: /srcdir/list0.c
4d663531 20565 DW_AT_comp_dir: /compdir
357e46e7 20566 files.files[0].name: list0.h
c5aa993b 20567 files.files[0].dir: /srcdir
357e46e7 20568 files.files[1].name: list0.c
c5aa993b 20569 files.files[1].dir: /srcdir
c906108c
SS
20570
20571 The line number information for list0.c has to end up in a single
4f1520fb
FR
20572 subfile, so that `break /srcdir/list0.c:1' works as expected.
20573 start_subfile will ensure that this happens provided that we pass the
20574 concatenation of files.files[1].dir and files.files[1].name as the
20575 subfile's name. */
c906108c
SS
20576
20577static void
804d2729
TT
20578dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20579 const char *dirname)
c906108c 20580{
43816ebc 20581 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20582
4d663531 20583 /* In order not to lose the line information directory,
4f1520fb
FR
20584 we concatenate it to the filename when it makes sense.
20585 Note that the Dwarf3 standard says (speaking of filenames in line
20586 information): ``The directory index is ignored for file names
20587 that represent full path names''. Thus ignoring dirname in the
20588 `else' branch below isn't an issue. */
c906108c 20589
d5166ae1 20590 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20591 {
43816ebc
TT
20592 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20593 filename = copy.get ();
d521ce57 20594 }
c906108c 20595
c24bdb02 20596 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20597}
20598
804d2729
TT
20599/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20600 buildsym_compunit constructor. */
f4dc4d17 20601
c24bdb02
KS
20602struct compunit_symtab *
20603dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20604 CORE_ADDR low_pc)
f4dc4d17 20605{
c24bdb02 20606 gdb_assert (m_builder == nullptr);
43f3e411 20607
c24bdb02 20608 m_builder.reset (new struct buildsym_compunit
f6e649dd 20609 (this->per_objfile->objfile,
c24bdb02 20610 name, comp_dir, language, low_pc));
93b8bea4 20611
c24bdb02 20612 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20613
c24bdb02
KS
20614 get_builder ()->record_debugformat ("DWARF 2");
20615 get_builder ()->record_producer (producer);
f4dc4d17 20616
c24bdb02 20617 processing_has_namespace_info = false;
43f3e411 20618
c24bdb02 20619 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20620}
20621
4c2df51b
DJ
20622static void
20623var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20624 struct dwarf2_cu *cu)
4c2df51b 20625{
5e22e966 20626 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20627 struct comp_unit_head *cu_header = &cu->header;
20628
4c2df51b
DJ
20629 /* NOTE drow/2003-01-30: There used to be a comment and some special
20630 code here to turn a symbol with DW_AT_external and a
20631 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20632 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20633 with some versions of binutils) where shared libraries could have
20634 relocations against symbols in their debug information - the
20635 minimal symbol would have the right address, but the debug info
20636 would not. It's no longer necessary, because we will explicitly
20637 apply relocations when we read in the debug information now. */
20638
20639 /* A DW_AT_location attribute with no contents indicates that a
20640 variable has been optimized away. */
4fc6c0d5 20641 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20642 {
f1e6e072 20643 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20644 return;
20645 }
20646
20647 /* Handle one degenerate form of location expression specially, to
20648 preserve GDB's previous behavior when section offsets are
336d760d
AT
20649 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20650 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20651
4fc6c0d5 20652 if (attr->form_is_block ()
3019eac3
DE
20653 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20654 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20655 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20656 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20657 && (DW_BLOCK (attr)->size
20658 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20659 {
891d2f0b 20660 unsigned int dummy;
4c2df51b 20661
3019eac3 20662 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20663 SET_SYMBOL_VALUE_ADDRESS
20664 (sym, cu->header.read_address (objfile->obfd,
20665 DW_BLOCK (attr)->data + 1,
20666 &dummy));
3019eac3 20667 else
38583298
TT
20668 SET_SYMBOL_VALUE_ADDRESS
20669 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20670 &dummy));
f1e6e072 20671 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20672 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20673 SET_SYMBOL_VALUE_ADDRESS
20674 (sym,
20675 SYMBOL_VALUE_ADDRESS (sym)
20676 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20677 return;
20678 }
20679
20680 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20681 expression evaluator, and use LOC_COMPUTED only when necessary
20682 (i.e. when the value of a register or memory location is
20683 referenced, or a thread-local block, etc.). Then again, it might
20684 not be worthwhile. I'm assuming that it isn't unless performance
20685 or memory numbers show me otherwise. */
20686
f1e6e072 20687 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20688
f1e6e072 20689 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20690 cu->has_loclist = true;
4c2df51b
DJ
20691}
20692
c906108c
SS
20693/* Given a pointer to a DWARF information entry, figure out if we need
20694 to make a symbol table entry for it, and if so, create a new entry
20695 and return a pointer to it.
20696 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20697 used the passed type.
20698 If SPACE is not NULL, use it to hold the new symbol. If it is
20699 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20700
20701static struct symbol *
5e2db402
TT
20702new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20703 struct symbol *space)
c906108c 20704{
5e22e966 20705 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20706 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20707 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20708 struct symbol *sym = NULL;
15d034d0 20709 const char *name;
c906108c
SS
20710 struct attribute *attr = NULL;
20711 struct attribute *attr2 = NULL;
e142c38c 20712 CORE_ADDR baseaddr;
e37fd15a
SW
20713 struct pending **list_to_add = NULL;
20714
edb3359d 20715 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20716
b3b3bada 20717 baseaddr = objfile->text_section_offset ();
c906108c 20718
94af9270 20719 name = dwarf2_name (die, cu);
c906108c
SS
20720 if (name)
20721 {
34eaf542 20722 int suppress_add = 0;
94af9270 20723
34eaf542
TT
20724 if (space)
20725 sym = space;
20726 else
8c14c3a3 20727 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20728 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20729
20730 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20731 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20732 /* Fortran does not have mangling standard and the mangling does differ
20733 between gfortran, iFort etc. */
bcfe6157
TT
20734 const char *physname
20735 = (cu->language == language_fortran
20736 ? dwarf2_full_name (name, die, cu)
20737 : dwarf2_physname (name, die, cu));
20738 const char *linkagename = dw2_linkage_name (die, cu);
20739
20740 if (linkagename == nullptr || cu->language == language_ada)
20741 sym->set_linkage_name (physname);
20742 else
20743 {
20744 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20745 sym->set_linkage_name (linkagename);
20746 }
f55ee35c 20747
c906108c 20748 /* Default assumptions.
c5aa993b 20749 Use the passed type or decode it from the die. */
176620f1 20750 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20751 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20752 if (type != NULL)
20753 SYMBOL_TYPE (sym) = type;
20754 else
e7c27a73 20755 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20756 attr = dwarf2_attr (die,
20757 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20758 cu);
435d3d88 20759 if (attr != nullptr)
c906108c
SS
20760 {
20761 SYMBOL_LINE (sym) = DW_UNSND (attr);
20762 }
cb1df416 20763
edb3359d
DJ
20764 attr = dwarf2_attr (die,
20765 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20766 cu);
435d3d88 20767 if (attr != nullptr)
cb1df416 20768 {
ecfb656c 20769 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20770 struct file_entry *fe;
9a619af0 20771
ecfb656c
PA
20772 if (cu->line_header != NULL)
20773 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20774 else
20775 fe = NULL;
20776
20777 if (fe == NULL)
b98664d3 20778 complaint (_("file index out of range"));
8c43009f
PA
20779 else
20780 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20781 }
20782
c906108c
SS
20783 switch (die->tag)
20784 {
20785 case DW_TAG_label:
e142c38c 20786 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20787 if (attr != nullptr)
3e29f34a
MR
20788 {
20789 CORE_ADDR addr;
20790
cd6c91b4 20791 addr = attr->value_as_address ();
3e29f34a 20792 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20793 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20794 }
0f5238ed
TT
20795 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20796 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20797 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20798 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20799 break;
20800 case DW_TAG_subprogram:
20801 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20802 finish_block. */
f1e6e072 20803 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20804 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20805 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20806 || cu->language == language_ada
20807 || cu->language == language_fortran)
c906108c 20808 {
2cfa0c8d 20809 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20810 Ada and Fortran subprograms, whether marked external or
20811 not, are always stored as a global symbol, because we want
20812 to be able to access them globally. For instance, we want
20813 to be able to break on a nested subprogram without having
20814 to specify the context. */
c24bdb02 20815 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20816 }
20817 else
20818 {
e37fd15a 20819 list_to_add = cu->list_in_scope;
c906108c
SS
20820 }
20821 break;
edb3359d
DJ
20822 case DW_TAG_inlined_subroutine:
20823 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20824 finish_block. */
f1e6e072 20825 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20826 SYMBOL_INLINED (sym) = 1;
481860b3 20827 list_to_add = cu->list_in_scope;
edb3359d 20828 break;
34eaf542
TT
20829 case DW_TAG_template_value_param:
20830 suppress_add = 1;
20831 /* Fall through. */
72929c62 20832 case DW_TAG_constant:
c906108c 20833 case DW_TAG_variable:
254e6b9e 20834 case DW_TAG_member:
0963b4bd
MS
20835 /* Compilation with minimal debug info may result in
20836 variables with missing type entries. Change the
20837 misleading `void' type to something sensible. */
78134374 20838 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20839 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20840
e142c38c 20841 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20842 /* In the case of DW_TAG_member, we should only be called for
20843 static const members. */
20844 if (die->tag == DW_TAG_member)
20845 {
3863f96c
DE
20846 /* dwarf2_add_field uses die_is_declaration,
20847 so we do the same. */
254e6b9e
DE
20848 gdb_assert (die_is_declaration (die, cu));
20849 gdb_assert (attr);
20850 }
435d3d88 20851 if (attr != nullptr)
c906108c 20852 {
e7c27a73 20853 dwarf2_const_value (attr, sym, cu);
e142c38c 20854 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20855 if (!suppress_add)
34eaf542
TT
20856 {
20857 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20858 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20859 else
e37fd15a 20860 list_to_add = cu->list_in_scope;
34eaf542 20861 }
c906108c
SS
20862 break;
20863 }
e142c38c 20864 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20865 if (attr != nullptr)
c906108c 20866 {
e7c27a73 20867 var_decode_location (attr, sym, cu);
e142c38c 20868 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20869
20870 /* Fortran explicitly imports any global symbols to the local
20871 scope by DW_TAG_common_block. */
20872 if (cu->language == language_fortran && die->parent
20873 && die->parent->tag == DW_TAG_common_block)
20874 attr2 = NULL;
20875
caac4577
JG
20876 if (SYMBOL_CLASS (sym) == LOC_STATIC
20877 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20878 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20879 {
20880 /* When a static variable is eliminated by the linker,
20881 the corresponding debug information is not stripped
20882 out, but the variable address is set to null;
20883 do not add such variables into symbol table. */
20884 }
20885 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20886 {
4b610737
TT
20887 if (SYMBOL_CLASS (sym) == LOC_STATIC
20888 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20889 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20890 {
20891 /* A global static variable might be subject to
20892 copy relocation. We first check for a local
20893 minsym, though, because maybe the symbol was
20894 marked hidden, in which case this would not
20895 apply. */
20896 bound_minimal_symbol found
20897 = (lookup_minimal_symbol_linkage
987012b8 20898 (sym->linkage_name (), objfile));
4b610737
TT
20899 if (found.minsym != nullptr)
20900 sym->maybe_copied = 1;
20901 }
f55ee35c 20902
1c809c68
TT
20903 /* A variable with DW_AT_external is never static,
20904 but it may be block-scoped. */
804d2729 20905 list_to_add
c24bdb02
KS
20906 = ((cu->list_in_scope
20907 == cu->get_builder ()->get_file_symbols ())
20908 ? cu->get_builder ()->get_global_symbols ()
804d2729 20909 : cu->list_in_scope);
1c809c68 20910 }
c906108c 20911 else
e37fd15a 20912 list_to_add = cu->list_in_scope;
c906108c
SS
20913 }
20914 else
20915 {
20916 /* We do not know the address of this symbol.
c5aa993b
JM
20917 If it is an external symbol and we have type information
20918 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20919 The address of the variable will then be determined from
20920 the minimal symbol table whenever the variable is
20921 referenced. */
e142c38c 20922 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20923
20924 /* Fortran explicitly imports any global symbols to the local
20925 scope by DW_TAG_common_block. */
20926 if (cu->language == language_fortran && die->parent
20927 && die->parent->tag == DW_TAG_common_block)
20928 {
20929 /* SYMBOL_CLASS doesn't matter here because
20930 read_common_block is going to reset it. */
20931 if (!suppress_add)
20932 list_to_add = cu->list_in_scope;
20933 }
20934 else if (attr2 && (DW_UNSND (attr2) != 0)
20935 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20936 {
0fe7935b
DJ
20937 /* A variable with DW_AT_external is never static, but it
20938 may be block-scoped. */
804d2729 20939 list_to_add
c24bdb02
KS
20940 = ((cu->list_in_scope
20941 == cu->get_builder ()->get_file_symbols ())
20942 ? cu->get_builder ()->get_global_symbols ()
804d2729 20943 : cu->list_in_scope);
0fe7935b 20944
f1e6e072 20945 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20946 }
442ddf59
JK
20947 else if (!die_is_declaration (die, cu))
20948 {
20949 /* Use the default LOC_OPTIMIZED_OUT class. */
20950 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20951 if (!suppress_add)
20952 list_to_add = cu->list_in_scope;
442ddf59 20953 }
c906108c
SS
20954 }
20955 break;
20956 case DW_TAG_formal_parameter:
a60f3166
TT
20957 {
20958 /* If we are inside a function, mark this as an argument. If
20959 not, we might be looking at an argument to an inlined function
20960 when we do not have enough information to show inlined frames;
20961 pretend it's a local variable in that case so that the user can
20962 still see it. */
804d2729 20963 struct context_stack *curr
c24bdb02 20964 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20965 if (curr != nullptr && curr->name != nullptr)
20966 SYMBOL_IS_ARGUMENT (sym) = 1;
20967 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20968 if (attr != nullptr)
a60f3166
TT
20969 {
20970 var_decode_location (attr, sym, cu);
20971 }
20972 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20973 if (attr != nullptr)
a60f3166
TT
20974 {
20975 dwarf2_const_value (attr, sym, cu);
20976 }
f346a30d 20977
a60f3166
TT
20978 list_to_add = cu->list_in_scope;
20979 }
c906108c
SS
20980 break;
20981 case DW_TAG_unspecified_parameters:
20982 /* From varargs functions; gdb doesn't seem to have any
20983 interest in this information, so just ignore it for now.
20984 (FIXME?) */
20985 break;
34eaf542
TT
20986 case DW_TAG_template_type_param:
20987 suppress_add = 1;
20988 /* Fall through. */
c906108c 20989 case DW_TAG_class_type:
680b30c7 20990 case DW_TAG_interface_type:
c906108c
SS
20991 case DW_TAG_structure_type:
20992 case DW_TAG_union_type:
72019c9c 20993 case DW_TAG_set_type:
c906108c 20994 case DW_TAG_enumeration_type:
f1e6e072 20995 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20996 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20997
63d06c5c 20998 {
9c37b5ae 20999 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21000 really ever be static objects: otherwise, if you try
21001 to, say, break of a class's method and you're in a file
21002 which doesn't mention that class, it won't work unless
21003 the check for all static symbols in lookup_symbol_aux
21004 saves you. See the OtherFileClass tests in
21005 gdb.c++/namespace.exp. */
21006
e37fd15a 21007 if (!suppress_add)
34eaf542 21008 {
c24bdb02 21009 buildsym_compunit *builder = cu->get_builder ();
804d2729 21010 list_to_add
c24bdb02 21011 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21012 && cu->language == language_cplus
c24bdb02 21013 ? builder->get_global_symbols ()
804d2729 21014 : cu->list_in_scope);
63d06c5c 21015
64382290 21016 /* The semantics of C++ state that "struct foo {
9c37b5ae 21017 ... }" also defines a typedef for "foo". */
64382290 21018 if (cu->language == language_cplus
45280282 21019 || cu->language == language_ada
c44af4eb
TT
21020 || cu->language == language_d
21021 || cu->language == language_rust)
64382290
TT
21022 {
21023 /* The symbol's name is already allocated along
21024 with this objfile, so we don't need to
21025 duplicate it for the type. */
7d93a1e0 21026 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21027 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21028 }
63d06c5c
DC
21029 }
21030 }
c906108c
SS
21031 break;
21032 case DW_TAG_typedef:
f1e6e072 21033 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21034 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21035 list_to_add = cu->list_in_scope;
63d06c5c 21036 break;
c906108c 21037 case DW_TAG_base_type:
a02abb62 21038 case DW_TAG_subrange_type:
f1e6e072 21039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21040 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21041 list_to_add = cu->list_in_scope;
c906108c
SS
21042 break;
21043 case DW_TAG_enumerator:
e142c38c 21044 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21045 if (attr != nullptr)
c906108c 21046 {
e7c27a73 21047 dwarf2_const_value (attr, sym, cu);
c906108c 21048 }
63d06c5c
DC
21049 {
21050 /* NOTE: carlton/2003-11-10: See comment above in the
21051 DW_TAG_class_type, etc. block. */
21052
804d2729 21053 list_to_add
c24bdb02 21054 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21055 && cu->language == language_cplus
c24bdb02 21056 ? cu->get_builder ()->get_global_symbols ()
804d2729 21057 : cu->list_in_scope);
63d06c5c 21058 }
c906108c 21059 break;
74921315 21060 case DW_TAG_imported_declaration:
5c4e30ca 21061 case DW_TAG_namespace:
f1e6e072 21062 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21063 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21064 break;
530e8392
KB
21065 case DW_TAG_module:
21066 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21067 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21068 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21069 break;
4357ac6c 21070 case DW_TAG_common_block:
f1e6e072 21071 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21072 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21073 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21074 break;
c906108c
SS
21075 default:
21076 /* Not a tag we recognize. Hopefully we aren't processing
21077 trash data, but since we must specifically ignore things
21078 we don't recognize, there is nothing else we should do at
0963b4bd 21079 this point. */
b98664d3 21080 complaint (_("unsupported tag: '%s'"),
4d3c2250 21081 dwarf_tag_name (die->tag));
c906108c
SS
21082 break;
21083 }
df8a16a1 21084
e37fd15a
SW
21085 if (suppress_add)
21086 {
21087 sym->hash_next = objfile->template_symbols;
21088 objfile->template_symbols = sym;
21089 list_to_add = NULL;
21090 }
21091
21092 if (list_to_add != NULL)
d3cb6808 21093 add_symbol_to_list (sym, list_to_add);
e37fd15a 21094
df8a16a1
DJ
21095 /* For the benefit of old versions of GCC, check for anonymous
21096 namespaces based on the demangled name. */
4d4ec4e5 21097 if (!cu->processing_has_namespace_info
94af9270 21098 && cu->language == language_cplus)
c24bdb02 21099 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21100 }
21101 return (sym);
21102}
21103
98bfdba5
PA
21104/* Given an attr with a DW_FORM_dataN value in host byte order,
21105 zero-extend it as appropriate for the symbol's type. The DWARF
21106 standard (v4) is not entirely clear about the meaning of using
21107 DW_FORM_dataN for a constant with a signed type, where the type is
21108 wider than the data. The conclusion of a discussion on the DWARF
21109 list was that this is unspecified. We choose to always zero-extend
21110 because that is the interpretation long in use by GCC. */
c906108c 21111
98bfdba5 21112static gdb_byte *
ff39bb5e 21113dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21114 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21115{
5e22e966 21116 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21117 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21118 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21119 LONGEST l = DW_UNSND (attr);
21120
21121 if (bits < sizeof (*value) * 8)
21122 {
21123 l &= ((LONGEST) 1 << bits) - 1;
21124 *value = l;
21125 }
21126 else if (bits == sizeof (*value) * 8)
21127 *value = l;
21128 else
21129 {
224c3ddb 21130 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21131 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21132 return bytes;
21133 }
21134
21135 return NULL;
21136}
21137
21138/* Read a constant value from an attribute. Either set *VALUE, or if
21139 the value does not fit in *VALUE, set *BYTES - either already
21140 allocated on the objfile obstack, or newly allocated on OBSTACK,
21141 or, set *BATON, if we translated the constant to a location
21142 expression. */
21143
21144static void
ff39bb5e 21145dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21146 const char *name, struct obstack *obstack,
21147 struct dwarf2_cu *cu,
d521ce57 21148 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21149 struct dwarf2_locexpr_baton **baton)
21150{
5e22e966 21151 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21152 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21153 struct comp_unit_head *cu_header = &cu->header;
c906108c 21154 struct dwarf_block *blk;
98bfdba5
PA
21155 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21156 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21157
21158 *value = 0;
21159 *bytes = NULL;
21160 *baton = NULL;
c906108c
SS
21161
21162 switch (attr->form)
21163 {
21164 case DW_FORM_addr:
336d760d 21165 case DW_FORM_addrx:
3019eac3 21166 case DW_FORM_GNU_addr_index:
ac56253d 21167 {
ac56253d
TT
21168 gdb_byte *data;
21169
98bfdba5
PA
21170 if (TYPE_LENGTH (type) != cu_header->addr_size)
21171 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21172 cu_header->addr_size,
98bfdba5 21173 TYPE_LENGTH (type));
ac56253d
TT
21174 /* Symbols of this form are reasonably rare, so we just
21175 piggyback on the existing location code rather than writing
21176 a new implementation of symbol_computed_ops. */
8d749320 21177 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21178 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21179 (*baton)->per_cu = cu->per_cu;
21180 gdb_assert ((*baton)->per_cu);
ac56253d 21181
98bfdba5 21182 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21183 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21184 (*baton)->data = data;
ac56253d
TT
21185
21186 data[0] = DW_OP_addr;
21187 store_unsigned_integer (&data[1], cu_header->addr_size,
21188 byte_order, DW_ADDR (attr));
21189 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21190 }
c906108c 21191 break;
4ac36638 21192 case DW_FORM_string:
93b5768b 21193 case DW_FORM_strp:
cf532bd1 21194 case DW_FORM_strx:
3019eac3 21195 case DW_FORM_GNU_str_index:
36586728 21196 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21197 /* DW_STRING is already allocated on the objfile obstack, point
21198 directly to it. */
d521ce57 21199 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21200 break;
c906108c
SS
21201 case DW_FORM_block1:
21202 case DW_FORM_block2:
21203 case DW_FORM_block4:
21204 case DW_FORM_block:
2dc7f7b3 21205 case DW_FORM_exprloc:
0224619f 21206 case DW_FORM_data16:
c906108c 21207 blk = DW_BLOCK (attr);
98bfdba5
PA
21208 if (TYPE_LENGTH (type) != blk->size)
21209 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21210 TYPE_LENGTH (type));
21211 *bytes = blk->data;
c906108c 21212 break;
2df3850c
JM
21213
21214 /* The DW_AT_const_value attributes are supposed to carry the
21215 symbol's value "represented as it would be on the target
21216 architecture." By the time we get here, it's already been
21217 converted to host endianness, so we just need to sign- or
21218 zero-extend it as appropriate. */
21219 case DW_FORM_data1:
3aef2284 21220 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21221 break;
c906108c 21222 case DW_FORM_data2:
3aef2284 21223 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21224 break;
c906108c 21225 case DW_FORM_data4:
3aef2284 21226 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21227 break;
c906108c 21228 case DW_FORM_data8:
3aef2284 21229 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21230 break;
21231
c906108c 21232 case DW_FORM_sdata:
663c44ac 21233 case DW_FORM_implicit_const:
98bfdba5 21234 *value = DW_SND (attr);
2df3850c
JM
21235 break;
21236
c906108c 21237 case DW_FORM_udata:
98bfdba5 21238 *value = DW_UNSND (attr);
c906108c 21239 break;
2df3850c 21240
c906108c 21241 default:
b98664d3 21242 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21243 dwarf_form_name (attr->form));
98bfdba5 21244 *value = 0;
c906108c
SS
21245 break;
21246 }
21247}
21248
2df3850c 21249
98bfdba5
PA
21250/* Copy constant value from an attribute to a symbol. */
21251
2df3850c 21252static void
ff39bb5e 21253dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21254 struct dwarf2_cu *cu)
2df3850c 21255{
5e22e966 21256 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21257 LONGEST value;
d521ce57 21258 const gdb_byte *bytes;
98bfdba5 21259 struct dwarf2_locexpr_baton *baton;
2df3850c 21260
98bfdba5 21261 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21262 sym->print_name (),
98bfdba5
PA
21263 &objfile->objfile_obstack, cu,
21264 &value, &bytes, &baton);
2df3850c 21265
98bfdba5
PA
21266 if (baton != NULL)
21267 {
98bfdba5 21268 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21269 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21270 }
21271 else if (bytes != NULL)
21272 {
21273 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21274 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21275 }
21276 else
21277 {
21278 SYMBOL_VALUE (sym) = value;
f1e6e072 21279 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21280 }
2df3850c
JM
21281}
21282
c906108c
SS
21283/* Return the type of the die in question using its DW_AT_type attribute. */
21284
21285static struct type *
e7c27a73 21286die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21287{
c906108c 21288 struct attribute *type_attr;
c906108c 21289
e142c38c 21290 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21291 if (!type_attr)
21292 {
5e22e966 21293 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21294 /* A missing DW_AT_type represents a void type. */
518817b3 21295 return objfile_type (objfile)->builtin_void;
c906108c 21296 }
348e048f 21297
673bfd45 21298 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21299}
21300
b4ba55a1
JB
21301/* True iff CU's producer generates GNAT Ada auxiliary information
21302 that allows to find parallel types through that information instead
21303 of having to do expensive parallel lookups by type name. */
21304
21305static int
21306need_gnat_info (struct dwarf2_cu *cu)
21307{
de4cb04a
JB
21308 /* Assume that the Ada compiler was GNAT, which always produces
21309 the auxiliary information. */
21310 return (cu->language == language_ada);
b4ba55a1
JB
21311}
21312
b4ba55a1
JB
21313/* Return the auxiliary type of the die in question using its
21314 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21315 attribute is not present. */
21316
21317static struct type *
21318die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21319{
b4ba55a1 21320 struct attribute *type_attr;
b4ba55a1
JB
21321
21322 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21323 if (!type_attr)
21324 return NULL;
21325
673bfd45 21326 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21327}
21328
21329/* If DIE has a descriptive_type attribute, then set the TYPE's
21330 descriptive type accordingly. */
21331
21332static void
21333set_descriptive_type (struct type *type, struct die_info *die,
21334 struct dwarf2_cu *cu)
21335{
21336 struct type *descriptive_type = die_descriptive_type (die, cu);
21337
21338 if (descriptive_type)
21339 {
21340 ALLOCATE_GNAT_AUX_TYPE (type);
21341 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21342 }
21343}
21344
c906108c
SS
21345/* Return the containing type of the die in question using its
21346 DW_AT_containing_type attribute. */
21347
21348static struct type *
e7c27a73 21349die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21350{
c906108c 21351 struct attribute *type_attr;
5e22e966 21352 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21353
e142c38c 21354 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21355 if (!type_attr)
21356 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21357 "[in module %s]"), objfile_name (objfile));
33ac96f0 21358
673bfd45 21359 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21360}
21361
ac9ec31b
DE
21362/* Return an error marker type to use for the ill formed type in DIE/CU. */
21363
21364static struct type *
21365build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21366{
5e22e966 21367 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21368 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21369 char *saved;
ac9ec31b 21370
528e1572
SM
21371 std::string message
21372 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21373 objfile_name (objfile),
21374 sect_offset_str (cu->header.sect_off),
21375 sect_offset_str (die->sect_off));
efba19b0 21376 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21377
19f392bc 21378 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21379}
21380
673bfd45 21381/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21382 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21383 DW_AT_containing_type.
673bfd45
DE
21384 If there is no type substitute an error marker. */
21385
c906108c 21386static struct type *
ff39bb5e 21387lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21388 struct dwarf2_cu *cu)
c906108c 21389{
5e22e966 21390 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21391 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21392 struct type *this_type;
21393
ac9ec31b
DE
21394 gdb_assert (attr->name == DW_AT_type
21395 || attr->name == DW_AT_GNAT_descriptive_type
21396 || attr->name == DW_AT_containing_type);
21397
673bfd45
DE
21398 /* First see if we have it cached. */
21399
36586728
TT
21400 if (attr->form == DW_FORM_GNU_ref_alt)
21401 {
21402 struct dwarf2_per_cu_data *per_cu;
0826b30a 21403 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21404
ed2dc618
SM
21405 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21406 dwarf2_per_objfile);
aa66c379 21407 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21408 }
cd6c91b4 21409 else if (attr->form_is_ref ())
673bfd45 21410 {
0826b30a 21411 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21412
aa66c379
SM
21413 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21414 dwarf2_per_objfile);
673bfd45 21415 }
55f1336d 21416 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21417 {
ac9ec31b 21418 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21419
ac9ec31b 21420 return get_signatured_type (die, signature, cu);
673bfd45
DE
21421 }
21422 else
21423 {
b98664d3 21424 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21425 " at %s [in module %s]"),
21426 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21427 objfile_name (objfile));
ac9ec31b 21428 return build_error_marker_type (cu, die);
673bfd45
DE
21429 }
21430
21431 /* If not cached we need to read it in. */
21432
21433 if (this_type == NULL)
21434 {
ac9ec31b 21435 struct die_info *type_die = NULL;
673bfd45
DE
21436 struct dwarf2_cu *type_cu = cu;
21437
cd6c91b4 21438 if (attr->form_is_ref ())
ac9ec31b
DE
21439 type_die = follow_die_ref (die, attr, &type_cu);
21440 if (type_die == NULL)
21441 return build_error_marker_type (cu, die);
21442 /* If we find the type now, it's probably because the type came
3019eac3
DE
21443 from an inter-CU reference and the type's CU got expanded before
21444 ours. */
ac9ec31b 21445 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21446 }
21447
21448 /* If we still don't have a type use an error marker. */
21449
21450 if (this_type == NULL)
ac9ec31b 21451 return build_error_marker_type (cu, die);
673bfd45 21452
f792889a 21453 return this_type;
c906108c
SS
21454}
21455
673bfd45
DE
21456/* Return the type in DIE, CU.
21457 Returns NULL for invalid types.
21458
02142a6c 21459 This first does a lookup in die_type_hash,
673bfd45
DE
21460 and only reads the die in if necessary.
21461
21462 NOTE: This can be called when reading in partial or full symbols. */
21463
f792889a 21464static struct type *
e7c27a73 21465read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21466{
f792889a
DJ
21467 struct type *this_type;
21468
21469 this_type = get_die_type (die, cu);
21470 if (this_type)
21471 return this_type;
21472
673bfd45
DE
21473 return read_type_die_1 (die, cu);
21474}
21475
21476/* Read the type in DIE, CU.
21477 Returns NULL for invalid types. */
21478
21479static struct type *
21480read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21481{
21482 struct type *this_type = NULL;
21483
c906108c
SS
21484 switch (die->tag)
21485 {
21486 case DW_TAG_class_type:
680b30c7 21487 case DW_TAG_interface_type:
c906108c
SS
21488 case DW_TAG_structure_type:
21489 case DW_TAG_union_type:
f792889a 21490 this_type = read_structure_type (die, cu);
c906108c
SS
21491 break;
21492 case DW_TAG_enumeration_type:
f792889a 21493 this_type = read_enumeration_type (die, cu);
c906108c
SS
21494 break;
21495 case DW_TAG_subprogram:
21496 case DW_TAG_subroutine_type:
edb3359d 21497 case DW_TAG_inlined_subroutine:
f792889a 21498 this_type = read_subroutine_type (die, cu);
c906108c
SS
21499 break;
21500 case DW_TAG_array_type:
f792889a 21501 this_type = read_array_type (die, cu);
c906108c 21502 break;
72019c9c 21503 case DW_TAG_set_type:
f792889a 21504 this_type = read_set_type (die, cu);
72019c9c 21505 break;
c906108c 21506 case DW_TAG_pointer_type:
f792889a 21507 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21508 break;
21509 case DW_TAG_ptr_to_member_type:
f792889a 21510 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21511 break;
21512 case DW_TAG_reference_type:
4297a3f0
AV
21513 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21514 break;
21515 case DW_TAG_rvalue_reference_type:
21516 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21517 break;
21518 case DW_TAG_const_type:
f792889a 21519 this_type = read_tag_const_type (die, cu);
c906108c
SS
21520 break;
21521 case DW_TAG_volatile_type:
f792889a 21522 this_type = read_tag_volatile_type (die, cu);
c906108c 21523 break;
06d66ee9
TT
21524 case DW_TAG_restrict_type:
21525 this_type = read_tag_restrict_type (die, cu);
21526 break;
c906108c 21527 case DW_TAG_string_type:
f792889a 21528 this_type = read_tag_string_type (die, cu);
c906108c
SS
21529 break;
21530 case DW_TAG_typedef:
f792889a 21531 this_type = read_typedef (die, cu);
c906108c 21532 break;
a02abb62 21533 case DW_TAG_subrange_type:
f792889a 21534 this_type = read_subrange_type (die, cu);
a02abb62 21535 break;
c906108c 21536 case DW_TAG_base_type:
f792889a 21537 this_type = read_base_type (die, cu);
c906108c 21538 break;
81a17f79 21539 case DW_TAG_unspecified_type:
f792889a 21540 this_type = read_unspecified_type (die, cu);
81a17f79 21541 break;
0114d602
DJ
21542 case DW_TAG_namespace:
21543 this_type = read_namespace_type (die, cu);
21544 break;
f55ee35c
JK
21545 case DW_TAG_module:
21546 this_type = read_module_type (die, cu);
21547 break;
a2c2acaf
MW
21548 case DW_TAG_atomic_type:
21549 this_type = read_tag_atomic_type (die, cu);
21550 break;
c906108c 21551 default:
b98664d3 21552 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21553 dwarf_tag_name (die->tag));
c906108c
SS
21554 break;
21555 }
63d06c5c 21556
f792889a 21557 return this_type;
63d06c5c
DC
21558}
21559
abc72ce4
DE
21560/* See if we can figure out if the class lives in a namespace. We do
21561 this by looking for a member function; its demangled name will
21562 contain namespace info, if there is any.
21563 Return the computed name or NULL.
21564 Space for the result is allocated on the objfile's obstack.
21565 This is the full-die version of guess_partial_die_structure_name.
21566 In this case we know DIE has no useful parent. */
21567
43816ebc 21568static const char *
abc72ce4
DE
21569guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21570{
21571 struct die_info *spec_die;
21572 struct dwarf2_cu *spec_cu;
21573 struct die_info *child;
5e22e966 21574 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21575
21576 spec_cu = cu;
21577 spec_die = die_specification (die, &spec_cu);
21578 if (spec_die != NULL)
21579 {
21580 die = spec_die;
21581 cu = spec_cu;
21582 }
21583
21584 for (child = die->child;
21585 child != NULL;
21586 child = child->sibling)
21587 {
21588 if (child->tag == DW_TAG_subprogram)
21589 {
73b9be8b 21590 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21591
7d45c7c3 21592 if (linkage_name != NULL)
abc72ce4 21593 {
43816ebc
TT
21594 gdb::unique_xmalloc_ptr<char> actual_name
21595 (language_class_name_from_physname (cu->language_defn,
21596 linkage_name));
21597 const char *name = NULL;
abc72ce4
DE
21598
21599 if (actual_name != NULL)
21600 {
15d034d0 21601 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21602
21603 if (die_name != NULL
43816ebc 21604 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21605 {
21606 /* Strip off the class name from the full name.
21607 We want the prefix. */
21608 int die_name_len = strlen (die_name);
43816ebc
TT
21609 int actual_name_len = strlen (actual_name.get ());
21610 const char *ptr = actual_name.get ();
abc72ce4
DE
21611
21612 /* Test for '::' as a sanity check. */
21613 if (actual_name_len > die_name_len + 2
43816ebc 21614 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21615 name = obstack_strndup (
e3b94546 21616 &objfile->per_bfd->storage_obstack,
43816ebc 21617 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21618 }
21619 }
abc72ce4
DE
21620 return name;
21621 }
21622 }
21623 }
21624
21625 return NULL;
21626}
21627
96408a79
SA
21628/* GCC might emit a nameless typedef that has a linkage name. Determine the
21629 prefix part in such case. See
21630 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21631
a121b7c1 21632static const char *
96408a79
SA
21633anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21634{
21635 struct attribute *attr;
e6a959d6 21636 const char *base;
96408a79
SA
21637
21638 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21639 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21640 return NULL;
21641
7d45c7c3 21642 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21643 return NULL;
21644
73b9be8b 21645 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21646 if (attr == NULL || DW_STRING (attr) == NULL)
21647 return NULL;
21648
21649 /* dwarf2_name had to be already called. */
21650 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21651
21652 /* Strip the base name, keep any leading namespaces/classes. */
21653 base = strrchr (DW_STRING (attr), ':');
21654 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21655 return "";
21656
5e22e966 21657 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21658 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21659 DW_STRING (attr),
21660 &base[-1] - DW_STRING (attr));
96408a79
SA
21661}
21662
fdde2d81 21663/* Return the name of the namespace/class that DIE is defined within,
0114d602 21664 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21665
0114d602
DJ
21666 For example, if we're within the method foo() in the following
21667 code:
21668
21669 namespace N {
21670 class C {
21671 void foo () {
21672 }
21673 };
21674 }
21675
21676 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21677
0d5cff50 21678static const char *
e142c38c 21679determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21680{
5e22e966 21681 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21682 struct die_info *parent, *spec_die;
21683 struct dwarf2_cu *spec_cu;
21684 struct type *parent_type;
a121b7c1 21685 const char *retval;
63d06c5c 21686
9c37b5ae 21687 if (cu->language != language_cplus
c44af4eb
TT
21688 && cu->language != language_fortran && cu->language != language_d
21689 && cu->language != language_rust)
0114d602
DJ
21690 return "";
21691
96408a79
SA
21692 retval = anonymous_struct_prefix (die, cu);
21693 if (retval)
21694 return retval;
21695
0114d602
DJ
21696 /* We have to be careful in the presence of DW_AT_specification.
21697 For example, with GCC 3.4, given the code
21698
21699 namespace N {
21700 void foo() {
21701 // Definition of N::foo.
21702 }
21703 }
21704
21705 then we'll have a tree of DIEs like this:
21706
21707 1: DW_TAG_compile_unit
21708 2: DW_TAG_namespace // N
21709 3: DW_TAG_subprogram // declaration of N::foo
21710 4: DW_TAG_subprogram // definition of N::foo
21711 DW_AT_specification // refers to die #3
21712
21713 Thus, when processing die #4, we have to pretend that we're in
21714 the context of its DW_AT_specification, namely the contex of die
21715 #3. */
21716 spec_cu = cu;
21717 spec_die = die_specification (die, &spec_cu);
21718 if (spec_die == NULL)
21719 parent = die->parent;
21720 else
63d06c5c 21721 {
0114d602
DJ
21722 parent = spec_die->parent;
21723 cu = spec_cu;
63d06c5c 21724 }
0114d602
DJ
21725
21726 if (parent == NULL)
21727 return "";
98bfdba5
PA
21728 else if (parent->building_fullname)
21729 {
21730 const char *name;
21731 const char *parent_name;
21732
21733 /* It has been seen on RealView 2.2 built binaries,
21734 DW_TAG_template_type_param types actually _defined_ as
21735 children of the parent class:
21736
21737 enum E {};
21738 template class <class Enum> Class{};
21739 Class<enum E> class_e;
21740
21741 1: DW_TAG_class_type (Class)
21742 2: DW_TAG_enumeration_type (E)
21743 3: DW_TAG_enumerator (enum1:0)
21744 3: DW_TAG_enumerator (enum2:1)
21745 ...
21746 2: DW_TAG_template_type_param
21747 DW_AT_type DW_FORM_ref_udata (E)
21748
21749 Besides being broken debug info, it can put GDB into an
21750 infinite loop. Consider:
21751
21752 When we're building the full name for Class<E>, we'll start
21753 at Class, and go look over its template type parameters,
21754 finding E. We'll then try to build the full name of E, and
21755 reach here. We're now trying to build the full name of E,
21756 and look over the parent DIE for containing scope. In the
21757 broken case, if we followed the parent DIE of E, we'd again
21758 find Class, and once again go look at its template type
21759 arguments, etc., etc. Simply don't consider such parent die
21760 as source-level parent of this die (it can't be, the language
21761 doesn't allow it), and break the loop here. */
21762 name = dwarf2_name (die, cu);
21763 parent_name = dwarf2_name (parent, cu);
b98664d3 21764 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21765 name ? name : "<unknown>",
21766 parent_name ? parent_name : "<unknown>");
21767 return "";
21768 }
63d06c5c 21769 else
0114d602
DJ
21770 switch (parent->tag)
21771 {
63d06c5c 21772 case DW_TAG_namespace:
0114d602 21773 parent_type = read_type_die (parent, cu);
acebe513
UW
21774 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21775 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21776 Work around this problem here. */
21777 if (cu->language == language_cplus
7d93a1e0 21778 && strcmp (parent_type->name (), "::") == 0)
acebe513 21779 return "";
0114d602 21780 /* We give a name to even anonymous namespaces. */
7d93a1e0 21781 return parent_type->name ();
63d06c5c 21782 case DW_TAG_class_type:
680b30c7 21783 case DW_TAG_interface_type:
63d06c5c 21784 case DW_TAG_structure_type:
0114d602 21785 case DW_TAG_union_type:
f55ee35c 21786 case DW_TAG_module:
0114d602 21787 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21788 if (parent_type->name () != NULL)
21789 return parent_type->name ();
0114d602
DJ
21790 else
21791 /* An anonymous structure is only allowed non-static data
21792 members; no typedefs, no member functions, et cetera.
21793 So it does not need a prefix. */
21794 return "";
abc72ce4 21795 case DW_TAG_compile_unit:
95554aad 21796 case DW_TAG_partial_unit:
abc72ce4
DE
21797 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21798 if (cu->language == language_cplus
5989a64e 21799 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21800 && die->child != NULL
21801 && (die->tag == DW_TAG_class_type
21802 || die->tag == DW_TAG_structure_type
21803 || die->tag == DW_TAG_union_type))
21804 {
43816ebc 21805 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21806 if (name != NULL)
21807 return name;
21808 }
21809 return "";
0a4b0913
AB
21810 case DW_TAG_subprogram:
21811 /* Nested subroutines in Fortran get a prefix with the name
21812 of the parent's subroutine. */
21813 if (cu->language == language_fortran)
21814 {
21815 if ((die->tag == DW_TAG_subprogram)
21816 && (dwarf2_name (parent, cu) != NULL))
21817 return dwarf2_name (parent, cu);
21818 }
21819 return determine_prefix (parent, cu);
3d567982
TT
21820 case DW_TAG_enumeration_type:
21821 parent_type = read_type_die (parent, cu);
21822 if (TYPE_DECLARED_CLASS (parent_type))
21823 {
7d93a1e0
SM
21824 if (parent_type->name () != NULL)
21825 return parent_type->name ();
3d567982
TT
21826 return "";
21827 }
21828 /* Fall through. */
63d06c5c 21829 default:
8176b9b8 21830 return determine_prefix (parent, cu);
63d06c5c 21831 }
63d06c5c
DC
21832}
21833
3e43a32a
MS
21834/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21835 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21836 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21837 an obconcat, otherwise allocate storage for the result. The CU argument is
21838 used to determine the language and hence, the appropriate separator. */
987504bb 21839
f55ee35c 21840#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21841
21842static char *
f55ee35c
JK
21843typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21844 int physname, struct dwarf2_cu *cu)
63d06c5c 21845{
f55ee35c 21846 const char *lead = "";
5c315b68 21847 const char *sep;
63d06c5c 21848
3e43a32a
MS
21849 if (suffix == NULL || suffix[0] == '\0'
21850 || prefix == NULL || prefix[0] == '\0')
987504bb 21851 sep = "";
45280282
IB
21852 else if (cu->language == language_d)
21853 {
21854 /* For D, the 'main' function could be defined in any module, but it
21855 should never be prefixed. */
21856 if (strcmp (suffix, "D main") == 0)
21857 {
21858 prefix = "";
21859 sep = "";
21860 }
21861 else
21862 sep = ".";
21863 }
f55ee35c
JK
21864 else if (cu->language == language_fortran && physname)
21865 {
21866 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21867 DW_AT_MIPS_linkage_name is preferred and used instead. */
21868
21869 lead = "__";
21870 sep = "_MOD_";
21871 }
987504bb
JJ
21872 else
21873 sep = "::";
63d06c5c 21874
6dd47d34
DE
21875 if (prefix == NULL)
21876 prefix = "";
21877 if (suffix == NULL)
21878 suffix = "";
21879
987504bb
JJ
21880 if (obs == NULL)
21881 {
3e43a32a 21882 char *retval
224c3ddb
SM
21883 = ((char *)
21884 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21885
f55ee35c
JK
21886 strcpy (retval, lead);
21887 strcat (retval, prefix);
6dd47d34
DE
21888 strcat (retval, sep);
21889 strcat (retval, suffix);
63d06c5c
DC
21890 return retval;
21891 }
987504bb
JJ
21892 else
21893 {
21894 /* We have an obstack. */
f55ee35c 21895 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21896 }
63d06c5c
DC
21897}
21898
71c25dea
TT
21899/* Get name of a die, return NULL if not found. */
21900
15d034d0
TT
21901static const char *
21902dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21903 struct objfile *objfile)
71c25dea
TT
21904{
21905 if (name && cu->language == language_cplus)
21906 {
596dc4ad
TT
21907 gdb::unique_xmalloc_ptr<char> canon_name
21908 = cp_canonicalize_string (name);
71c25dea 21909
596dc4ad
TT
21910 if (canon_name != nullptr)
21911 name = objfile->intern (canon_name.get ());
71c25dea
TT
21912 }
21913
21914 return name;
c906108c
SS
21915}
21916
96553a0c
DE
21917/* Get name of a die, return NULL if not found.
21918 Anonymous namespaces are converted to their magic string. */
9219021c 21919
15d034d0 21920static const char *
e142c38c 21921dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21922{
21923 struct attribute *attr;
5e22e966 21924 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21925
e142c38c 21926 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21927 if ((!attr || !DW_STRING (attr))
96553a0c 21928 && die->tag != DW_TAG_namespace
53832f31
TT
21929 && die->tag != DW_TAG_class_type
21930 && die->tag != DW_TAG_interface_type
21931 && die->tag != DW_TAG_structure_type
21932 && die->tag != DW_TAG_union_type)
71c25dea
TT
21933 return NULL;
21934
21935 switch (die->tag)
21936 {
21937 case DW_TAG_compile_unit:
95554aad 21938 case DW_TAG_partial_unit:
71c25dea
TT
21939 /* Compilation units have a DW_AT_name that is a filename, not
21940 a source language identifier. */
21941 case DW_TAG_enumeration_type:
21942 case DW_TAG_enumerator:
21943 /* These tags always have simple identifiers already; no need
21944 to canonicalize them. */
21945 return DW_STRING (attr);
907af001 21946
96553a0c
DE
21947 case DW_TAG_namespace:
21948 if (attr != NULL && DW_STRING (attr) != NULL)
21949 return DW_STRING (attr);
21950 return CP_ANONYMOUS_NAMESPACE_STR;
21951
907af001
UW
21952 case DW_TAG_class_type:
21953 case DW_TAG_interface_type:
21954 case DW_TAG_structure_type:
21955 case DW_TAG_union_type:
21956 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21957 structures or unions. These were of the form "._%d" in GCC 4.1,
21958 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21959 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21960 if (attr && DW_STRING (attr)
61012eef
GB
21961 && (startswith (DW_STRING (attr), "._")
21962 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21963 return NULL;
53832f31
TT
21964
21965 /* GCC might emit a nameless typedef that has a linkage name. See
21966 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21967 if (!attr || DW_STRING (attr) == NULL)
21968 {
73b9be8b 21969 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21970 if (attr == NULL || DW_STRING (attr) == NULL)
21971 return NULL;
21972
df5c6c50
JK
21973 /* Avoid demangling DW_STRING (attr) the second time on a second
21974 call for the same DIE. */
21975 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21976 {
43816ebc
TT
21977 gdb::unique_xmalloc_ptr<char> demangled
21978 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21979 if (demangled == nullptr)
21980 return nullptr;
43816ebc 21981
be1e3d3e 21982 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21983 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21984 }
67430cd0
TT
21985
21986 /* Strip any leading namespaces/classes, keep only the base name.
21987 DW_AT_name for named DIEs does not contain the prefixes. */
21988 const char *base = strrchr (DW_STRING (attr), ':');
21989 if (base && base > DW_STRING (attr) && base[-1] == ':')
21990 return &base[1];
21991 else
21992 return DW_STRING (attr);
53832f31 21993 }
907af001
UW
21994 break;
21995
71c25dea 21996 default:
907af001
UW
21997 break;
21998 }
21999
22000 if (!DW_STRING_IS_CANONICAL (attr))
22001 {
be1e3d3e
TT
22002 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22003 objfile);
907af001 22004 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22005 }
907af001 22006 return DW_STRING (attr);
9219021c
DC
22007}
22008
22009/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22010 is none. *EXT_CU is the CU containing DIE on input, and the CU
22011 containing the return value on output. */
9219021c
DC
22012
22013static struct die_info *
f2f0e013 22014dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22015{
22016 struct attribute *attr;
9219021c 22017
f2f0e013 22018 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22019 if (attr == NULL)
22020 return NULL;
22021
f2f0e013 22022 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22023}
22024
f9aca02d 22025static void
d97bc12b 22026dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22027{
22028 unsigned int i;
22029
d97bc12b 22030 print_spaces (indent, f);
9d8780f0 22031 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22032 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22033 sect_offset_str (die->sect_off));
d97bc12b
DE
22034
22035 if (die->parent != NULL)
22036 {
22037 print_spaces (indent, f);
9d8780f0
SM
22038 fprintf_unfiltered (f, " parent at offset: %s\n",
22039 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22040 }
22041
22042 print_spaces (indent, f);
22043 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22044 dwarf_bool_name (die->child != NULL));
c906108c 22045
d97bc12b
DE
22046 print_spaces (indent, f);
22047 fprintf_unfiltered (f, " attributes:\n");
22048
c906108c
SS
22049 for (i = 0; i < die->num_attrs; ++i)
22050 {
d97bc12b
DE
22051 print_spaces (indent, f);
22052 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22053 dwarf_attr_name (die->attrs[i].name),
22054 dwarf_form_name (die->attrs[i].form));
d97bc12b 22055
c906108c
SS
22056 switch (die->attrs[i].form)
22057 {
c906108c 22058 case DW_FORM_addr:
336d760d 22059 case DW_FORM_addrx:
3019eac3 22060 case DW_FORM_GNU_addr_index:
d97bc12b 22061 fprintf_unfiltered (f, "address: ");
5af949e3 22062 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22063 break;
22064 case DW_FORM_block2:
22065 case DW_FORM_block4:
22066 case DW_FORM_block:
22067 case DW_FORM_block1:
56eb65bd
SP
22068 fprintf_unfiltered (f, "block: size %s",
22069 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22070 break;
2dc7f7b3 22071 case DW_FORM_exprloc:
56eb65bd
SP
22072 fprintf_unfiltered (f, "expression: size %s",
22073 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22074 break;
0224619f
JK
22075 case DW_FORM_data16:
22076 fprintf_unfiltered (f, "constant of 16 bytes");
22077 break;
4568ecf9
DE
22078 case DW_FORM_ref_addr:
22079 fprintf_unfiltered (f, "ref address: ");
22080 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22081 break;
36586728
TT
22082 case DW_FORM_GNU_ref_alt:
22083 fprintf_unfiltered (f, "alt ref address: ");
22084 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22085 break;
10b3939b
DJ
22086 case DW_FORM_ref1:
22087 case DW_FORM_ref2:
22088 case DW_FORM_ref4:
4568ecf9
DE
22089 case DW_FORM_ref8:
22090 case DW_FORM_ref_udata:
d97bc12b 22091 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22092 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22093 break;
c906108c
SS
22094 case DW_FORM_data1:
22095 case DW_FORM_data2:
22096 case DW_FORM_data4:
ce5d95e1 22097 case DW_FORM_data8:
c906108c
SS
22098 case DW_FORM_udata:
22099 case DW_FORM_sdata:
43bbcdc2
PH
22100 fprintf_unfiltered (f, "constant: %s",
22101 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22102 break;
2dc7f7b3
TT
22103 case DW_FORM_sec_offset:
22104 fprintf_unfiltered (f, "section offset: %s",
22105 pulongest (DW_UNSND (&die->attrs[i])));
22106 break;
55f1336d 22107 case DW_FORM_ref_sig8:
ac9ec31b
DE
22108 fprintf_unfiltered (f, "signature: %s",
22109 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22110 break;
c906108c 22111 case DW_FORM_string:
4bdf3d34 22112 case DW_FORM_strp:
43988095 22113 case DW_FORM_line_strp:
cf532bd1 22114 case DW_FORM_strx:
3019eac3 22115 case DW_FORM_GNU_str_index:
36586728 22116 case DW_FORM_GNU_strp_alt:
8285870a 22117 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22118 DW_STRING (&die->attrs[i])
8285870a
JK
22119 ? DW_STRING (&die->attrs[i]) : "",
22120 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22121 break;
22122 case DW_FORM_flag:
22123 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22124 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22125 else
d97bc12b 22126 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22127 break;
2dc7f7b3
TT
22128 case DW_FORM_flag_present:
22129 fprintf_unfiltered (f, "flag: TRUE");
22130 break;
a8329558 22131 case DW_FORM_indirect:
0963b4bd
MS
22132 /* The reader will have reduced the indirect form to
22133 the "base form" so this form should not occur. */
5f48f8f3 22134 fprintf_unfiltered (f,
3e43a32a 22135 "unexpected attribute form: DW_FORM_indirect");
a8329558 22136 break;
663c44ac
JK
22137 case DW_FORM_implicit_const:
22138 fprintf_unfiltered (f, "constant: %s",
22139 plongest (DW_SND (&die->attrs[i])));
22140 break;
c906108c 22141 default:
d97bc12b 22142 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22143 die->attrs[i].form);
d97bc12b 22144 break;
c906108c 22145 }
d97bc12b 22146 fprintf_unfiltered (f, "\n");
c906108c
SS
22147 }
22148}
22149
f9aca02d 22150static void
d97bc12b 22151dump_die_for_error (struct die_info *die)
c906108c 22152{
d97bc12b
DE
22153 dump_die_shallow (gdb_stderr, 0, die);
22154}
22155
22156static void
22157dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22158{
22159 int indent = level * 4;
22160
22161 gdb_assert (die != NULL);
22162
22163 if (level >= max_level)
22164 return;
22165
22166 dump_die_shallow (f, indent, die);
22167
22168 if (die->child != NULL)
c906108c 22169 {
d97bc12b
DE
22170 print_spaces (indent, f);
22171 fprintf_unfiltered (f, " Children:");
22172 if (level + 1 < max_level)
22173 {
22174 fprintf_unfiltered (f, "\n");
22175 dump_die_1 (f, level + 1, max_level, die->child);
22176 }
22177 else
22178 {
3e43a32a
MS
22179 fprintf_unfiltered (f,
22180 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22181 }
22182 }
22183
22184 if (die->sibling != NULL && level > 0)
22185 {
22186 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22187 }
22188}
22189
d97bc12b
DE
22190/* This is called from the pdie macro in gdbinit.in.
22191 It's not static so gcc will keep a copy callable from gdb. */
22192
22193void
22194dump_die (struct die_info *die, int max_level)
22195{
22196 dump_die_1 (gdb_stdlog, 0, max_level, die);
22197}
22198
f9aca02d 22199static void
51545339 22200store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22201{
51545339 22202 void **slot;
c906108c 22203
9c541725
PA
22204 slot = htab_find_slot_with_hash (cu->die_hash, die,
22205 to_underlying (die->sect_off),
b64f50a1 22206 INSERT);
51545339
DJ
22207
22208 *slot = die;
c906108c
SS
22209}
22210
348e048f
DE
22211/* Follow reference or signature attribute ATTR of SRC_DIE.
22212 On entry *REF_CU is the CU of SRC_DIE.
22213 On exit *REF_CU is the CU of the result. */
22214
22215static struct die_info *
ff39bb5e 22216follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22217 struct dwarf2_cu **ref_cu)
22218{
22219 struct die_info *die;
22220
cd6c91b4 22221 if (attr->form_is_ref ())
348e048f 22222 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22223 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22224 die = follow_die_sig (src_die, attr, ref_cu);
22225 else
22226 {
22227 dump_die_for_error (src_die);
22228 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22229 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22230 }
22231
22232 return die;
03dd20cc
DJ
22233}
22234
5c631832 22235/* Follow reference OFFSET.
673bfd45
DE
22236 On entry *REF_CU is the CU of the source die referencing OFFSET.
22237 On exit *REF_CU is the CU of the result.
22238 Returns NULL if OFFSET is invalid. */
f504f079 22239
f9aca02d 22240static struct die_info *
9c541725 22241follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22242 struct dwarf2_cu **ref_cu)
c906108c 22243{
10b3939b 22244 struct die_info temp_die;
f2f0e013 22245 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22247
348e048f
DE
22248 gdb_assert (cu->per_cu != NULL);
22249
98bfdba5
PA
22250 target_cu = cu;
22251
3019eac3 22252 if (cu->per_cu->is_debug_types)
348e048f
DE
22253 {
22254 /* .debug_types CUs cannot reference anything outside their CU.
22255 If they need to, they have to reference a signatured type via
55f1336d 22256 DW_FORM_ref_sig8. */
4057dfde 22257 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22258 return NULL;
348e048f 22259 }
36586728 22260 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22261 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22262 {
22263 struct dwarf2_per_cu_data *per_cu;
9a619af0 22264
9c541725 22265 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22266 dwarf2_per_objfile);
03dd20cc
DJ
22267
22268 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22269 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22270 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22271
10b3939b
DJ
22272 target_cu = per_cu->cu;
22273 }
98bfdba5
PA
22274 else if (cu->dies == NULL)
22275 {
22276 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22277 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22278 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22279 language_minimal);
98bfdba5 22280 }
c906108c 22281
f2f0e013 22282 *ref_cu = target_cu;
9c541725 22283 temp_die.sect_off = sect_off;
c24bdb02
KS
22284
22285 if (target_cu != cu)
22286 target_cu->ancestor = cu;
22287
9a3c8263 22288 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22289 &temp_die,
22290 to_underlying (sect_off));
5c631832 22291}
10b3939b 22292
5c631832
JK
22293/* Follow reference attribute ATTR of SRC_DIE.
22294 On entry *REF_CU is the CU of SRC_DIE.
22295 On exit *REF_CU is the CU of the result. */
22296
22297static struct die_info *
ff39bb5e 22298follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22299 struct dwarf2_cu **ref_cu)
22300{
0826b30a 22301 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22302 struct dwarf2_cu *cu = *ref_cu;
22303 struct die_info *die;
22304
9c541725 22305 die = follow_die_offset (sect_off,
36586728
TT
22306 (attr->form == DW_FORM_GNU_ref_alt
22307 || cu->per_cu->is_dwz),
22308 ref_cu);
5c631832 22309 if (!die)
9d8780f0
SM
22310 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22311 "at %s [in module %s]"),
22312 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22313 objfile_name (cu->per_objfile->objfile));
348e048f 22314
5c631832
JK
22315 return die;
22316}
22317
d4c9a4f8 22318/* See read.h. */
5c631832
JK
22319
22320struct dwarf2_locexpr_baton
9c541725 22321dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22322 dwarf2_per_cu_data *per_cu,
14095eb3 22323 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22324 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22325 void *baton, bool resolve_abstract_p)
5c631832 22326{
918dd910 22327 struct dwarf2_cu *cu;
5c631832
JK
22328 struct die_info *die;
22329 struct attribute *attr;
22330 struct dwarf2_locexpr_baton retval;
12359b5e 22331 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22332
918dd910 22333 if (per_cu->cu == NULL)
ab432490 22334 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22335 cu = per_cu->cu;
cc12ce38
DE
22336 if (cu == NULL)
22337 {
22338 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22339 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22340 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22341 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22342 }
918dd910 22343
9c541725 22344 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22345 if (!die)
9d8780f0
SM
22346 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22347 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22348
22349 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22350 if (!attr && resolve_abstract_p
5989a64e
SM
22351 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22352 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22353 {
22354 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22355 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22356 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22357
3360b6e7 22358 for (const auto &cand_off
5989a64e 22359 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22360 {
3360b6e7
TV
22361 struct dwarf2_cu *cand_cu = cu;
22362 struct die_info *cand
22363 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22364 if (!cand
22365 || !cand->parent
e4a62c65
TV
22366 || cand->parent->tag != DW_TAG_subprogram)
22367 continue;
22368
22369 CORE_ADDR pc_low, pc_high;
22370 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22371 if (pc_low == ((CORE_ADDR) -1))
22372 continue;
22373 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22374 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22375 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22376 continue;
22377
22378 die = cand;
22379 attr = dwarf2_attr (die, DW_AT_location, cu);
22380 break;
22381 }
22382 }
22383
5c631832
JK
22384 if (!attr)
22385 {
e103e986
JK
22386 /* DWARF: "If there is no such attribute, then there is no effect.".
22387 DATA is ignored if SIZE is 0. */
5c631832 22388
e103e986 22389 retval.data = NULL;
5c631832
JK
22390 retval.size = 0;
22391 }
cd6c91b4 22392 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22393 {
22394 struct dwarf2_loclist_baton loclist_baton;
22395 CORE_ADDR pc = (*get_frame_pc) (baton);
22396 size_t size;
22397
22398 fill_in_loclist_baton (cu, &loclist_baton, attr);
22399
22400 retval.data = dwarf2_find_location_expression (&loclist_baton,
22401 &size, pc);
22402 retval.size = size;
22403 }
5c631832
JK
22404 else
22405 {
4fc6c0d5 22406 if (!attr->form_is_block ())
9d8780f0 22407 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22408 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22409 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22410
22411 retval.data = DW_BLOCK (attr)->data;
22412 retval.size = DW_BLOCK (attr)->size;
22413 }
a50264ba 22414 retval.per_objfile = dwarf2_per_objfile;
5c631832 22415 retval.per_cu = cu->per_cu;
918dd910 22416
ed2dc618 22417 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22418
5c631832 22419 return retval;
348e048f
DE
22420}
22421
d4c9a4f8 22422/* See read.h. */
8b9737bf
TT
22423
22424struct dwarf2_locexpr_baton
22425dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22426 dwarf2_per_cu_data *per_cu,
14095eb3 22427 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22428 CORE_ADDR (*get_frame_pc) (void *baton),
22429 void *baton)
22430{
9c541725 22431 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22432
14095eb3
SM
22433 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22434 get_frame_pc, baton);
8b9737bf
TT
22435}
22436
b6807d98
TT
22437/* Write a constant of a given type as target-ordered bytes into
22438 OBSTACK. */
22439
22440static const gdb_byte *
22441write_constant_as_bytes (struct obstack *obstack,
22442 enum bfd_endian byte_order,
22443 struct type *type,
22444 ULONGEST value,
22445 LONGEST *len)
22446{
22447 gdb_byte *result;
22448
22449 *len = TYPE_LENGTH (type);
224c3ddb 22450 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22451 store_unsigned_integer (result, *len, byte_order, value);
22452
22453 return result;
22454}
22455
d4c9a4f8 22456/* See read.h. */
b6807d98
TT
22457
22458const gdb_byte *
9c541725 22459dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22460 dwarf2_per_cu_data *per_cu,
14095eb3 22461 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22462 obstack *obstack,
b6807d98
TT
22463 LONGEST *len)
22464{
22465 struct dwarf2_cu *cu;
22466 struct die_info *die;
22467 struct attribute *attr;
22468 const gdb_byte *result = NULL;
22469 struct type *type;
22470 LONGEST value;
22471 enum bfd_endian byte_order;
14095eb3 22472 struct objfile *objfile = per_objfile->objfile;
b6807d98 22473
b6807d98 22474 if (per_cu->cu == NULL)
14095eb3 22475 load_cu (per_cu, per_objfile, false);
b6807d98 22476 cu = per_cu->cu;
cc12ce38
DE
22477 if (cu == NULL)
22478 {
22479 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22480 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22481 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22482 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22483 }
b6807d98 22484
9c541725 22485 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22486 if (!die)
9d8780f0
SM
22487 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22488 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22489
22490 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22491 if (attr == NULL)
22492 return NULL;
22493
e3b94546 22494 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22495 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22496
22497 switch (attr->form)
22498 {
22499 case DW_FORM_addr:
336d760d 22500 case DW_FORM_addrx:
b6807d98
TT
22501 case DW_FORM_GNU_addr_index:
22502 {
22503 gdb_byte *tem;
22504
22505 *len = cu->header.addr_size;
224c3ddb 22506 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22507 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22508 result = tem;
22509 }
22510 break;
22511 case DW_FORM_string:
22512 case DW_FORM_strp:
cf532bd1 22513 case DW_FORM_strx:
b6807d98
TT
22514 case DW_FORM_GNU_str_index:
22515 case DW_FORM_GNU_strp_alt:
22516 /* DW_STRING is already allocated on the objfile obstack, point
22517 directly to it. */
22518 result = (const gdb_byte *) DW_STRING (attr);
22519 *len = strlen (DW_STRING (attr));
22520 break;
22521 case DW_FORM_block1:
22522 case DW_FORM_block2:
22523 case DW_FORM_block4:
22524 case DW_FORM_block:
22525 case DW_FORM_exprloc:
0224619f 22526 case DW_FORM_data16:
b6807d98
TT
22527 result = DW_BLOCK (attr)->data;
22528 *len = DW_BLOCK (attr)->size;
22529 break;
22530
22531 /* The DW_AT_const_value attributes are supposed to carry the
22532 symbol's value "represented as it would be on the target
22533 architecture." By the time we get here, it's already been
22534 converted to host endianness, so we just need to sign- or
22535 zero-extend it as appropriate. */
22536 case DW_FORM_data1:
22537 type = die_type (die, cu);
22538 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22539 if (result == NULL)
22540 result = write_constant_as_bytes (obstack, byte_order,
22541 type, value, len);
22542 break;
22543 case DW_FORM_data2:
22544 type = die_type (die, cu);
22545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22546 if (result == NULL)
22547 result = write_constant_as_bytes (obstack, byte_order,
22548 type, value, len);
22549 break;
22550 case DW_FORM_data4:
22551 type = die_type (die, cu);
22552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22553 if (result == NULL)
22554 result = write_constant_as_bytes (obstack, byte_order,
22555 type, value, len);
22556 break;
22557 case DW_FORM_data8:
22558 type = die_type (die, cu);
22559 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22560 if (result == NULL)
22561 result = write_constant_as_bytes (obstack, byte_order,
22562 type, value, len);
22563 break;
22564
22565 case DW_FORM_sdata:
663c44ac 22566 case DW_FORM_implicit_const:
b6807d98
TT
22567 type = die_type (die, cu);
22568 result = write_constant_as_bytes (obstack, byte_order,
22569 type, DW_SND (attr), len);
22570 break;
22571
22572 case DW_FORM_udata:
22573 type = die_type (die, cu);
22574 result = write_constant_as_bytes (obstack, byte_order,
22575 type, DW_UNSND (attr), len);
22576 break;
22577
22578 default:
b98664d3 22579 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22580 dwarf_form_name (attr->form));
22581 break;
22582 }
22583
22584 return result;
22585}
22586
d4c9a4f8 22587/* See read.h. */
7942e96e
AA
22588
22589struct type *
9c541725 22590dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22591 dwarf2_per_cu_data *per_cu,
22592 dwarf2_per_objfile *per_objfile)
7942e96e
AA
22593{
22594 struct dwarf2_cu *cu;
22595 struct die_info *die;
22596
7942e96e 22597 if (per_cu->cu == NULL)
14095eb3 22598 load_cu (per_cu, per_objfile, false);
7942e96e
AA
22599 cu = per_cu->cu;
22600 if (!cu)
22601 return NULL;
22602
9c541725 22603 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22604 if (!die)
22605 return NULL;
22606
22607 return die_type (die, cu);
22608}
22609
8cb5117c 22610/* See read.h. */
8a9b8146
TT
22611
22612struct type *
b64f50a1 22613dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22614 dwarf2_per_cu_data *per_cu,
22615 dwarf2_per_objfile *per_objfile)
8a9b8146 22616{
9c541725 22617 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22618 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22619}
22620
ac9ec31b 22621/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22622 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22623 On exit *REF_CU is the CU of the result.
22624 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22625
22626static struct die_info *
ac9ec31b
DE
22627follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22628 struct dwarf2_cu **ref_cu)
348e048f 22629{
348e048f 22630 struct die_info temp_die;
c24bdb02 22631 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22632 struct die_info *die;
120ce1b5
SM
22633 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22634
348e048f 22635
ac9ec31b
DE
22636 /* While it might be nice to assert sig_type->type == NULL here,
22637 we can get here for DW_AT_imported_declaration where we need
22638 the DIE not the type. */
348e048f
DE
22639
22640 /* If necessary, add it to the queue and load its DIEs. */
22641
120ce1b5
SM
22642 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22643 language_minimal))
22644 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22645
348e048f 22646 sig_cu = sig_type->per_cu.cu;
69d751e3 22647 gdb_assert (sig_cu != NULL);
9c541725
PA
22648 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22649 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22650 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22651 to_underlying (temp_die.sect_off));
348e048f
DE
22652 if (die)
22653 {
796a7ff8
DE
22654 /* For .gdb_index version 7 keep track of included TUs.
22655 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22656 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22657 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22658 {
ae640021 22659 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22660 }
22661
348e048f 22662 *ref_cu = sig_cu;
c24bdb02
KS
22663 if (sig_cu != cu)
22664 sig_cu->ancestor = cu;
22665
348e048f
DE
22666 return die;
22667 }
22668
ac9ec31b
DE
22669 return NULL;
22670}
22671
22672/* Follow signatured type referenced by ATTR in SRC_DIE.
22673 On entry *REF_CU is the CU of SRC_DIE.
22674 On exit *REF_CU is the CU of the result.
22675 The result is the DIE of the type.
22676 If the referenced type cannot be found an error is thrown. */
22677
22678static struct die_info *
ff39bb5e 22679follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22680 struct dwarf2_cu **ref_cu)
22681{
22682 ULONGEST signature = DW_SIGNATURE (attr);
22683 struct signatured_type *sig_type;
22684 struct die_info *die;
22685
22686 gdb_assert (attr->form == DW_FORM_ref_sig8);
22687
a2ce51a0 22688 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22689 /* sig_type will be NULL if the signatured type is missing from
22690 the debug info. */
22691 if (sig_type == NULL)
22692 {
22693 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22694 " from DIE at %s [in module %s]"),
22695 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22696 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22697 }
22698
22699 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22700 if (die == NULL)
22701 {
22702 dump_die_for_error (src_die);
22703 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22704 " from DIE at %s [in module %s]"),
22705 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22706 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22707 }
22708
22709 return die;
22710}
22711
22712/* Get the type specified by SIGNATURE referenced in DIE/CU,
22713 reading in and processing the type unit if necessary. */
22714
22715static struct type *
22716get_signatured_type (struct die_info *die, ULONGEST signature,
22717 struct dwarf2_cu *cu)
22718{
5e22e966 22719 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22720 struct signatured_type *sig_type;
22721 struct dwarf2_cu *type_cu;
22722 struct die_info *type_die;
22723 struct type *type;
22724
a2ce51a0 22725 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22726 /* sig_type will be NULL if the signatured type is missing from
22727 the debug info. */
22728 if (sig_type == NULL)
22729 {
b98664d3 22730 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22731 " from DIE at %s [in module %s]"),
22732 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22733 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22734 return build_error_marker_type (cu, die);
22735 }
22736
22737 /* If we already know the type we're done. */
e286671b
TT
22738 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22739 if (type != nullptr)
22740 return type;
ac9ec31b
DE
22741
22742 type_cu = cu;
22743 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22744 if (type_die != NULL)
22745 {
22746 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22747 is created. This is important, for example, because for c++ classes
22748 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22749 type = read_type_die (type_die, type_cu);
22750 if (type == NULL)
22751 {
b98664d3 22752 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22753 " referenced from DIE at %s [in module %s]"),
22754 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22755 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22756 type = build_error_marker_type (cu, die);
22757 }
22758 }
22759 else
22760 {
b98664d3 22761 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22762 " from DIE at %s [in module %s]"),
22763 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22764 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22765 type = build_error_marker_type (cu, die);
22766 }
e286671b
TT
22767
22768 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22769
22770 return type;
22771}
22772
22773/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22774 reading in and processing the type unit if necessary. */
22775
22776static struct type *
ff39bb5e 22777get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22778 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22779{
22780 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22781 if (attr->form_is_ref ())
ac9ec31b
DE
22782 {
22783 struct dwarf2_cu *type_cu = cu;
22784 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22785
22786 return read_type_die (type_die, type_cu);
22787 }
22788 else if (attr->form == DW_FORM_ref_sig8)
22789 {
22790 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22791 }
22792 else
22793 {
5e22e966 22794 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22795
b98664d3 22796 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22797 " at %s [in module %s]"),
22798 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22799 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22800 return build_error_marker_type (cu, die);
22801 }
348e048f
DE
22802}
22803
e5fe5e75 22804/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22805
22806static void
ab432490
SM
22807load_full_type_unit (dwarf2_per_cu_data *per_cu,
22808 dwarf2_per_objfile *per_objfile)
348e048f 22809{
52dc124a 22810 struct signatured_type *sig_type;
348e048f 22811
f4dc4d17 22812 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22813 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22814
6721b2ec
DE
22815 /* We have the per_cu, but we need the signatured_type.
22816 Fortunately this is an easy translation. */
22817 gdb_assert (per_cu->is_debug_types);
22818 sig_type = (struct signatured_type *) per_cu;
348e048f 22819
6721b2ec 22820 gdb_assert (per_cu->cu == NULL);
348e048f 22821
ab432490 22822 read_signatured_type (sig_type, per_objfile);
348e048f 22823
6721b2ec 22824 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22825}
22826
3019eac3
DE
22827/* Read in a signatured type and build its CU and DIEs.
22828 If the type is a stub for the real type in a DWO file,
22829 read in the real type from the DWO file as well. */
dee91e82
DE
22830
22831static void
ab432490
SM
22832read_signatured_type (signatured_type *sig_type,
22833 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22834{
22835 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22836
3019eac3 22837 gdb_assert (per_cu->is_debug_types);
dee91e82 22838 gdb_assert (per_cu->cu == NULL);
348e048f 22839
ab432490 22840 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22841
22842 if (!reader.dummy_p)
22843 {
22844 struct dwarf2_cu *cu = reader.cu;
22845 const gdb_byte *info_ptr = reader.info_ptr;
22846
22847 gdb_assert (cu->die_hash == NULL);
22848 cu->die_hash =
22849 htab_create_alloc_ex (cu->header.length / 12,
22850 die_hash,
22851 die_eq,
22852 NULL,
22853 &cu->comp_unit_obstack,
22854 hashtab_obstack_allocate,
22855 dummy_obstack_deallocate);
22856
3e225074 22857 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22858 reader.comp_unit_die->child
22859 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22860 reader.comp_unit_die);
22861 cu->dies = reader.comp_unit_die;
22862 /* comp_unit_die is not stored in die_hash, no need. */
22863
22864 /* We try not to read any attributes in this function, because
22865 not all CUs needed for references have been loaded yet, and
22866 symbol table processing isn't initialized. But we have to
22867 set the CU language, or we won't be able to build types
22868 correctly. Similarly, if we do not read the producer, we can
22869 not apply producer-specific interpretation. */
22870 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22871
22872 reader.keep ();
c0ab21c2
TT
22873 }
22874
7ee85ab1 22875 sig_type->per_cu.tu_read = 1;
c906108c
SS
22876}
22877
c906108c
SS
22878/* Decode simple location descriptions.
22879 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22880 the location and return the value. If COMPUTED is non-null, it is
22881 set to true to indicate that decoding was successful, and false
22882 otherwise. If COMPUTED is null, then this function may emit a
22883 complaint. */
c906108c
SS
22884
22885static CORE_ADDR
7d79de9a 22886decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22887{
5e22e966 22888 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22889 size_t i;
22890 size_t size = blk->size;
d521ce57 22891 const gdb_byte *data = blk->data;
21ae7a4d
JK
22892 CORE_ADDR stack[64];
22893 int stacki;
22894 unsigned int bytes_read, unsnd;
22895 gdb_byte op;
c906108c 22896
7d79de9a
TT
22897 if (computed != nullptr)
22898 *computed = false;
22899
21ae7a4d
JK
22900 i = 0;
22901 stacki = 0;
22902 stack[stacki] = 0;
22903 stack[++stacki] = 0;
22904
22905 while (i < size)
22906 {
22907 op = data[i++];
22908 switch (op)
22909 {
22910 case DW_OP_lit0:
22911 case DW_OP_lit1:
22912 case DW_OP_lit2:
22913 case DW_OP_lit3:
22914 case DW_OP_lit4:
22915 case DW_OP_lit5:
22916 case DW_OP_lit6:
22917 case DW_OP_lit7:
22918 case DW_OP_lit8:
22919 case DW_OP_lit9:
22920 case DW_OP_lit10:
22921 case DW_OP_lit11:
22922 case DW_OP_lit12:
22923 case DW_OP_lit13:
22924 case DW_OP_lit14:
22925 case DW_OP_lit15:
22926 case DW_OP_lit16:
22927 case DW_OP_lit17:
22928 case DW_OP_lit18:
22929 case DW_OP_lit19:
22930 case DW_OP_lit20:
22931 case DW_OP_lit21:
22932 case DW_OP_lit22:
22933 case DW_OP_lit23:
22934 case DW_OP_lit24:
22935 case DW_OP_lit25:
22936 case DW_OP_lit26:
22937 case DW_OP_lit27:
22938 case DW_OP_lit28:
22939 case DW_OP_lit29:
22940 case DW_OP_lit30:
22941 case DW_OP_lit31:
22942 stack[++stacki] = op - DW_OP_lit0;
22943 break;
f1bea926 22944
21ae7a4d
JK
22945 case DW_OP_reg0:
22946 case DW_OP_reg1:
22947 case DW_OP_reg2:
22948 case DW_OP_reg3:
22949 case DW_OP_reg4:
22950 case DW_OP_reg5:
22951 case DW_OP_reg6:
22952 case DW_OP_reg7:
22953 case DW_OP_reg8:
22954 case DW_OP_reg9:
22955 case DW_OP_reg10:
22956 case DW_OP_reg11:
22957 case DW_OP_reg12:
22958 case DW_OP_reg13:
22959 case DW_OP_reg14:
22960 case DW_OP_reg15:
22961 case DW_OP_reg16:
22962 case DW_OP_reg17:
22963 case DW_OP_reg18:
22964 case DW_OP_reg19:
22965 case DW_OP_reg20:
22966 case DW_OP_reg21:
22967 case DW_OP_reg22:
22968 case DW_OP_reg23:
22969 case DW_OP_reg24:
22970 case DW_OP_reg25:
22971 case DW_OP_reg26:
22972 case DW_OP_reg27:
22973 case DW_OP_reg28:
22974 case DW_OP_reg29:
22975 case DW_OP_reg30:
22976 case DW_OP_reg31:
22977 stack[++stacki] = op - DW_OP_reg0;
22978 if (i < size)
7d79de9a
TT
22979 {
22980 if (computed == nullptr)
22981 dwarf2_complex_location_expr_complaint ();
22982 else
22983 return 0;
22984 }
21ae7a4d 22985 break;
c906108c 22986
21ae7a4d
JK
22987 case DW_OP_regx:
22988 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22989 i += bytes_read;
22990 stack[++stacki] = unsnd;
22991 if (i < size)
7d79de9a
TT
22992 {
22993 if (computed == nullptr)
22994 dwarf2_complex_location_expr_complaint ();
22995 else
22996 return 0;
22997 }
21ae7a4d 22998 break;
c906108c 22999
21ae7a4d 23000 case DW_OP_addr:
c8a7a66f
TT
23001 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23002 &bytes_read);
21ae7a4d
JK
23003 i += bytes_read;
23004 break;
d53d4ac5 23005
21ae7a4d
JK
23006 case DW_OP_const1u:
23007 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23008 i += 1;
23009 break;
23010
23011 case DW_OP_const1s:
23012 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23013 i += 1;
23014 break;
23015
23016 case DW_OP_const2u:
23017 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23018 i += 2;
23019 break;
23020
23021 case DW_OP_const2s:
23022 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23023 i += 2;
23024 break;
d53d4ac5 23025
21ae7a4d
JK
23026 case DW_OP_const4u:
23027 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23028 i += 4;
23029 break;
23030
23031 case DW_OP_const4s:
23032 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23033 i += 4;
23034 break;
23035
585861ea
JK
23036 case DW_OP_const8u:
23037 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23038 i += 8;
23039 break;
23040
21ae7a4d
JK
23041 case DW_OP_constu:
23042 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23043 &bytes_read);
23044 i += bytes_read;
23045 break;
23046
23047 case DW_OP_consts:
23048 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23049 i += bytes_read;
23050 break;
23051
23052 case DW_OP_dup:
23053 stack[stacki + 1] = stack[stacki];
23054 stacki++;
23055 break;
23056
23057 case DW_OP_plus:
23058 stack[stacki - 1] += stack[stacki];
23059 stacki--;
23060 break;
23061
23062 case DW_OP_plus_uconst:
23063 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23064 &bytes_read);
23065 i += bytes_read;
23066 break;
23067
23068 case DW_OP_minus:
23069 stack[stacki - 1] -= stack[stacki];
23070 stacki--;
23071 break;
23072
23073 case DW_OP_deref:
23074 /* If we're not the last op, then we definitely can't encode
23075 this using GDB's address_class enum. This is valid for partial
23076 global symbols, although the variable's address will be bogus
23077 in the psymtab. */
23078 if (i < size)
7d79de9a
TT
23079 {
23080 if (computed == nullptr)
23081 dwarf2_complex_location_expr_complaint ();
23082 else
23083 return 0;
23084 }
21ae7a4d
JK
23085 break;
23086
23087 case DW_OP_GNU_push_tls_address:
4aa4e28b 23088 case DW_OP_form_tls_address:
21ae7a4d
JK
23089 /* The top of the stack has the offset from the beginning
23090 of the thread control block at which the variable is located. */
23091 /* Nothing should follow this operator, so the top of stack would
23092 be returned. */
23093 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23094 address will be bogus in the psymtab. Make it always at least
23095 non-zero to not look as a variable garbage collected by linker
23096 which have DW_OP_addr 0. */
21ae7a4d 23097 if (i < size)
7d79de9a
TT
23098 {
23099 if (computed == nullptr)
23100 dwarf2_complex_location_expr_complaint ();
23101 else
23102 return 0;
23103 }
585861ea 23104 stack[stacki]++;
21ae7a4d
JK
23105 break;
23106
23107 case DW_OP_GNU_uninit:
7d79de9a
TT
23108 if (computed != nullptr)
23109 return 0;
21ae7a4d
JK
23110 break;
23111
336d760d 23112 case DW_OP_addrx:
3019eac3 23113 case DW_OP_GNU_addr_index:
49f6c839 23114 case DW_OP_GNU_const_index:
3019eac3
DE
23115 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23116 &bytes_read);
23117 i += bytes_read;
23118 break;
23119
21ae7a4d 23120 default:
7d79de9a
TT
23121 if (computed == nullptr)
23122 {
23123 const char *name = get_DW_OP_name (op);
21ae7a4d 23124
7d79de9a
TT
23125 if (name)
23126 complaint (_("unsupported stack op: '%s'"),
23127 name);
23128 else
23129 complaint (_("unsupported stack op: '%02x'"),
23130 op);
23131 }
21ae7a4d
JK
23132
23133 return (stack[stacki]);
d53d4ac5 23134 }
3c6e0cb3 23135
21ae7a4d
JK
23136 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23137 outside of the allocated space. Also enforce minimum>0. */
23138 if (stacki >= ARRAY_SIZE (stack) - 1)
23139 {
7d79de9a
TT
23140 if (computed == nullptr)
23141 complaint (_("location description stack overflow"));
21ae7a4d
JK
23142 return 0;
23143 }
23144
23145 if (stacki <= 0)
23146 {
7d79de9a
TT
23147 if (computed == nullptr)
23148 complaint (_("location description stack underflow"));
21ae7a4d
JK
23149 return 0;
23150 }
23151 }
7d79de9a
TT
23152
23153 if (computed != nullptr)
23154 *computed = true;
21ae7a4d 23155 return (stack[stacki]);
c906108c
SS
23156}
23157
23158/* memory allocation interface */
23159
c906108c 23160static struct dwarf_block *
7b5a2f43 23161dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23162{
8d749320 23163 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23164}
23165
c906108c 23166static struct die_info *
b60c80d6 23167dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23168{
23169 struct die_info *die;
b60c80d6
DJ
23170 size_t size = sizeof (struct die_info);
23171
23172 if (num_attrs > 1)
23173 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23174
b60c80d6 23175 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23176 memset (die, 0, sizeof (struct die_info));
23177 return (die);
23178}
2e276125
JB
23179
23180\f
a036ba48 23181
c90ec28a 23182/* Macro support. */
cf2c3c16 23183
9eac9650
TT
23184/* An overload of dwarf_decode_macros that finds the correct section
23185 and ensures it is read in before calling the other overload. */
23186
23187static void
23188dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23189 int section_is_gnu)
23190{
5e22e966 23191 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23192 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23193 const struct line_header *lh = cu->line_header;
9eac9650
TT
23194 unsigned int offset_size = cu->header.offset_size;
23195 struct dwarf2_section_info *section;
23196 const char *section_name;
23197
23198 if (cu->dwo_unit != nullptr)
23199 {
23200 if (section_is_gnu)
23201 {
23202 section = &cu->dwo_unit->dwo_file->sections.macro;
23203 section_name = ".debug_macro.dwo";
23204 }
23205 else
23206 {
23207 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23208 section_name = ".debug_macinfo.dwo";
23209 }
23210 }
23211 else
23212 {
23213 if (section_is_gnu)
23214 {
5989a64e 23215 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23216 section_name = ".debug_macro";
23217 }
23218 else
23219 {
5989a64e 23220 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23221 section_name = ".debug_macinfo";
23222 }
23223 }
23224
23225 section->read (objfile);
23226 if (section->buffer == nullptr)
23227 {
23228 complaint (_("missing %s section"), section_name);
23229 return;
23230 }
23231
23232 buildsym_compunit *builder = cu->get_builder ();
23233
23234 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23235 offset_size, offset, section_is_gnu);
23236}
23237
3019eac3
DE
23238/* Return the .debug_loc section to use for CU.
23239 For DWO files use .debug_loc.dwo. */
23240
23241static struct dwarf2_section_info *
23242cu_debug_loc_section (struct dwarf2_cu *cu)
23243{
5e22e966 23244 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23245
3019eac3 23246 if (cu->dwo_unit)
43988095
JK
23247 {
23248 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23249
43988095
JK
23250 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23251 }
5989a64e
SM
23252 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23253 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23254}
23255
8cf6f0b1
TT
23256/* A helper function that fills in a dwarf2_loclist_baton. */
23257
23258static void
23259fill_in_loclist_baton (struct dwarf2_cu *cu,
23260 struct dwarf2_loclist_baton *baton,
ff39bb5e 23261 const struct attribute *attr)
8cf6f0b1 23262{
5e22e966 23263 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23264 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23265
96b79293 23266 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23267
a50264ba 23268 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23269 baton->per_cu = cu->per_cu;
23270 gdb_assert (baton->per_cu);
23271 /* We don't know how long the location list is, but make sure we
23272 don't run off the edge of the section. */
3019eac3
DE
23273 baton->size = section->size - DW_UNSND (attr);
23274 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23275 if (cu->base_address.has_value ())
23276 baton->base_address = *cu->base_address;
23277 else
23278 baton->base_address = 0;
f664829e 23279 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23280}
23281
4c2df51b 23282static void
ff39bb5e 23283dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23284 struct dwarf2_cu *cu, int is_block)
4c2df51b 23285{
5e22e966 23286 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23287 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23288 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23289
cd6c91b4 23290 if (attr->form_is_section_offset ()
3019eac3 23291 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23292 the section. If so, fall through to the complaint in the
23293 other branch. */
2c7d5afc 23294 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23295 {
0d53c4c4 23296 struct dwarf2_loclist_baton *baton;
4c2df51b 23297
8d749320 23298 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23299
8cf6f0b1 23300 fill_in_loclist_baton (cu, baton, attr);
be391dca 23301
2b24b6e4 23302 if (!cu->base_address.has_value ())
b98664d3 23303 complaint (_("Location list used without "
3e43a32a 23304 "specifying the CU base address."));
4c2df51b 23305
f1e6e072
TT
23306 SYMBOL_ACLASS_INDEX (sym) = (is_block
23307 ? dwarf2_loclist_block_index
23308 : dwarf2_loclist_index);
0d53c4c4
DJ
23309 SYMBOL_LOCATION_BATON (sym) = baton;
23310 }
23311 else
23312 {
23313 struct dwarf2_locexpr_baton *baton;
23314
8d749320 23315 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23316 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23317 baton->per_cu = cu->per_cu;
23318 gdb_assert (baton->per_cu);
0d53c4c4 23319
4fc6c0d5 23320 if (attr->form_is_block ())
0d53c4c4
DJ
23321 {
23322 /* Note that we're just copying the block's data pointer
23323 here, not the actual data. We're still pointing into the
6502dd73
DJ
23324 info_buffer for SYM's objfile; right now we never release
23325 that buffer, but when we do clean up properly this may
23326 need to change. */
0d53c4c4
DJ
23327 baton->size = DW_BLOCK (attr)->size;
23328 baton->data = DW_BLOCK (attr)->data;
23329 }
23330 else
23331 {
23332 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23333 sym->natural_name ());
0d53c4c4 23334 baton->size = 0;
0d53c4c4 23335 }
6e70227d 23336
f1e6e072
TT
23337 SYMBOL_ACLASS_INDEX (sym) = (is_block
23338 ? dwarf2_locexpr_block_index
23339 : dwarf2_locexpr_index);
0d53c4c4
DJ
23340 SYMBOL_LOCATION_BATON (sym) = baton;
23341 }
4c2df51b 23342}
6502dd73 23343
96408a79
SA
23344/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23345 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23346 CU_HEADERP first. */
23347
23348static const struct comp_unit_head *
23349per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23350 const struct dwarf2_per_cu_data *per_cu)
96408a79 23351{
d521ce57 23352 const gdb_byte *info_ptr;
96408a79
SA
23353
23354 if (per_cu->cu)
23355 return &per_cu->cu->header;
23356
9c541725 23357 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23358
23359 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23360 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23361 rcuh_kind::COMPILE);
96408a79
SA
23362
23363 return cu_headerp;
23364}
23365
09ba997f 23366/* See read.h. */
ae0d2f24 23367
98714339 23368int
09ba997f 23369dwarf2_per_cu_data::addr_size () const
ae0d2f24 23370{
96408a79
SA
23371 struct comp_unit_head cu_header_local;
23372 const struct comp_unit_head *cu_headerp;
c471e790 23373
09ba997f 23374 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23375
23376 return cu_headerp->addr_size;
ae0d2f24
UW
23377}
23378
09ba997f 23379/* See read.h. */
9eae7c52
TT
23380
23381int
09ba997f 23382dwarf2_per_cu_data::offset_size () const
9eae7c52 23383{
96408a79
SA
23384 struct comp_unit_head cu_header_local;
23385 const struct comp_unit_head *cu_headerp;
9c6c53f7 23386
09ba997f 23387 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23388
23389 return cu_headerp->offset_size;
23390}
23391
09ba997f 23392/* See read.h. */
96408a79
SA
23393
23394int
09ba997f 23395dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23396{
23397 struct comp_unit_head cu_header_local;
23398 const struct comp_unit_head *cu_headerp;
23399
09ba997f 23400 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23401
23402 if (cu_headerp->version == 2)
23403 return cu_headerp->addr_size;
23404 else
23405 return cu_headerp->offset_size;
181cebd4
JK
23406}
23407
09ba997f 23408/* See read.h. */
9aa1f1e3 23409
09ba997f 23410struct type *
293e7e51 23411dwarf2_cu::addr_type () const
9a49df9d 23412{
293e7e51 23413 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23414 struct type *void_type = objfile_type (objfile)->builtin_void;
23415 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23416 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23417
23418 if (TYPE_LENGTH (addr_type) == addr_size)
23419 return addr_type;
23420
09ba997f 23421 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23422 return addr_type;
23423}
23424
22b6cd70
TT
23425/* A helper function for dwarf2_find_containing_comp_unit that returns
23426 the index of the result, and that searches a vector. It will
23427 return a result even if the offset in question does not actually
23428 occur in any CU. This is separate so that it can be unit
23429 tested. */
ae038cb0 23430
22b6cd70
TT
23431static int
23432dwarf2_find_containing_comp_unit
23433 (sect_offset sect_off,
23434 unsigned int offset_in_dwz,
23435 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23436{
ae038cb0
DJ
23437 int low, high;
23438
ae038cb0 23439 low = 0;
22b6cd70 23440 high = all_comp_units.size () - 1;
ae038cb0
DJ
23441 while (high > low)
23442 {
36586728 23443 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23444 int mid = low + (high - low) / 2;
9a619af0 23445
22b6cd70 23446 mid_cu = all_comp_units[mid];
36586728 23447 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23448 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23449 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23450 high = mid;
23451 else
23452 low = mid + 1;
23453 }
23454 gdb_assert (low == high);
22b6cd70
TT
23455 return low;
23456}
23457
23458/* Locate the .debug_info compilation unit from CU's objfile which contains
23459 the DIE at OFFSET. Raises an error on failure. */
23460
23461static struct dwarf2_per_cu_data *
23462dwarf2_find_containing_comp_unit (sect_offset sect_off,
23463 unsigned int offset_in_dwz,
23464 struct dwarf2_per_objfile *dwarf2_per_objfile)
23465{
23466 int low
23467 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23468 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23469 struct dwarf2_per_cu_data *this_cu
5989a64e 23470 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23471
45b8ae0c 23472 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23473 {
36586728 23474 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23475 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23476 "offset %s [in module %s]"),
23477 sect_offset_str (sect_off),
ed2dc618 23478 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23479
5989a64e 23480 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23481 <= sect_off);
5989a64e 23482 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23483 }
23484 else
23485 {
5989a64e 23486 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23487 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23488 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23489 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23490 return this_cu;
23491 }
23492}
23493
22b6cd70
TT
23494#if GDB_SELF_TEST
23495
23496namespace selftests {
23497namespace find_containing_comp_unit {
23498
23499static void
23500run_test ()
23501{
23502 struct dwarf2_per_cu_data one {};
23503 struct dwarf2_per_cu_data two {};
23504 struct dwarf2_per_cu_data three {};
23505 struct dwarf2_per_cu_data four {};
23506
23507 one.length = 5;
23508 two.sect_off = sect_offset (one.length);
23509 two.length = 7;
23510
23511 three.length = 5;
23512 three.is_dwz = 1;
23513 four.sect_off = sect_offset (three.length);
23514 four.length = 7;
23515 four.is_dwz = 1;
23516
23517 std::vector<dwarf2_per_cu_data *> units;
23518 units.push_back (&one);
23519 units.push_back (&two);
23520 units.push_back (&three);
23521 units.push_back (&four);
23522
23523 int result;
23524
23525 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23526 SELF_CHECK (units[result] == &one);
23527 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23528 SELF_CHECK (units[result] == &one);
23529 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23530 SELF_CHECK (units[result] == &two);
23531
23532 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23533 SELF_CHECK (units[result] == &three);
23534 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23535 SELF_CHECK (units[result] == &three);
23536 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23537 SELF_CHECK (units[result] == &four);
23538}
23539
23540}
23541}
23542
23543#endif /* GDB_SELF_TEST */
23544
9e021579 23545/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23546
9e021579
SM
23547dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23548 dwarf2_per_objfile *per_objfile)
23549 : per_cu (per_cu),
23550 per_objfile (per_objfile),
9068261f
AB
23551 mark (false),
23552 has_loclist (false),
23553 checked_producer (false),
23554 producer_is_gxx_lt_4_6 (false),
23555 producer_is_gcc_lt_4_3 (false),
eb77c9df 23556 producer_is_icc (false),
9068261f 23557 producer_is_icc_lt_14 (false),
c258c396 23558 producer_is_codewarrior (false),
9068261f 23559 processing_has_namespace_info (false)
93311388 23560{
fcd3b13d
SM
23561 per_cu->cu = this;
23562}
23563
23564/* Destroy a dwarf2_cu. */
23565
23566dwarf2_cu::~dwarf2_cu ()
23567{
23568 per_cu->cu = NULL;
9816fde3
JK
23569}
23570
23571/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23572
23573static void
95554aad
TT
23574prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23575 enum language pretend_language)
9816fde3
JK
23576{
23577 struct attribute *attr;
23578
23579 /* Set the language we're debugging. */
23580 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23581 if (attr != nullptr)
9816fde3
JK
23582 set_cu_language (DW_UNSND (attr), cu);
23583 else
9cded63f 23584 {
95554aad 23585 cu->language = pretend_language;
9cded63f
TT
23586 cu->language_defn = language_def (cu->language);
23587 }
dee91e82 23588
7d45c7c3 23589 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23590}
23591
ae038cb0
DJ
23592/* Increase the age counter on each cached compilation unit, and free
23593 any that are too old. */
23594
23595static void
ed2dc618 23596age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23597{
23598 struct dwarf2_per_cu_data *per_cu, **last_chain;
23599
5989a64e
SM
23600 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23601 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23602 while (per_cu != NULL)
23603 {
23604 per_cu->cu->last_used ++;
b4f54984 23605 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23606 dwarf2_mark (per_cu->cu);
23607 per_cu = per_cu->cu->read_in_chain;
23608 }
23609
5989a64e
SM
23610 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23611 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23612 while (per_cu != NULL)
23613 {
23614 struct dwarf2_per_cu_data *next_cu;
23615
23616 next_cu = per_cu->cu->read_in_chain;
23617
23618 if (!per_cu->cu->mark)
23619 {
fcd3b13d 23620 delete per_cu->cu;
ae038cb0
DJ
23621 *last_chain = next_cu;
23622 }
23623 else
23624 last_chain = &per_cu->cu->read_in_chain;
23625
23626 per_cu = next_cu;
23627 }
23628}
23629
23630/* Remove a single compilation unit from the cache. */
23631
23632static void
120ce1b5
SM
23633free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23634 dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23635{
23636 struct dwarf2_per_cu_data *per_cu, **last_chain;
23637
5989a64e
SM
23638 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23639 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23640 while (per_cu != NULL)
23641 {
23642 struct dwarf2_per_cu_data *next_cu;
23643
23644 next_cu = per_cu->cu->read_in_chain;
23645
dee91e82 23646 if (per_cu == target_per_cu)
ae038cb0 23647 {
fcd3b13d 23648 delete per_cu->cu;
dee91e82 23649 per_cu->cu = NULL;
ae038cb0
DJ
23650 *last_chain = next_cu;
23651 break;
23652 }
23653 else
23654 last_chain = &per_cu->cu->read_in_chain;
23655
23656 per_cu = next_cu;
23657 }
23658}
23659
dee91e82
DE
23660/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23661 We store these in a hash table separate from the DIEs, and preserve them
23662 when the DIEs are flushed out of cache.
23663
23664 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23665 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23666 or the type may come from a DWO file. Furthermore, while it's more logical
23667 to use per_cu->section+offset, with Fission the section with the data is in
23668 the DWO file but we don't know that section at the point we need it.
23669 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23670 because we can enter the lookup routine, get_die_type_at_offset, from
23671 outside this file, and thus won't necessarily have PER_CU->cu.
23672 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23673
dee91e82 23674struct dwarf2_per_cu_offset_and_type
1c379e20 23675{
dee91e82 23676 const struct dwarf2_per_cu_data *per_cu;
9c541725 23677 sect_offset sect_off;
1c379e20
DJ
23678 struct type *type;
23679};
23680
dee91e82 23681/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23682
23683static hashval_t
dee91e82 23684per_cu_offset_and_type_hash (const void *item)
1c379e20 23685{
9a3c8263
SM
23686 const struct dwarf2_per_cu_offset_and_type *ofs
23687 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23688
9c541725 23689 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23690}
23691
dee91e82 23692/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23693
23694static int
dee91e82 23695per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23696{
9a3c8263
SM
23697 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23698 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23699 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23700 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23701
dee91e82 23702 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23703 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23704}
23705
23706/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23707 table if necessary. For convenience, return TYPE.
23708
23709 The DIEs reading must have careful ordering to:
85102364 23710 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23711 reading current DIE.
23712 * Not trying to dereference contents of still incompletely read in types
23713 while reading in other DIEs.
23714 * Enable referencing still incompletely read in types just by a pointer to
23715 the type without accessing its fields.
23716
23717 Therefore caller should follow these rules:
23718 * Try to fetch any prerequisite types we may need to build this DIE type
23719 before building the type and calling set_die_type.
e71ec853 23720 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23721 possible before fetching more types to complete the current type.
23722 * Make the type as complete as possible before fetching more types. */
1c379e20 23723
f792889a 23724static struct type *
1c379e20
DJ
23725set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23726{
5e22e966 23727 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23728 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23729 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23730 struct attribute *attr;
23731 struct dynamic_prop prop;
1c379e20 23732
b4ba55a1
JB
23733 /* For Ada types, make sure that the gnat-specific data is always
23734 initialized (if not already set). There are a few types where
23735 we should not be doing so, because the type-specific area is
23736 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23737 where the type-specific area is used to store the floatformat).
23738 But this is not a problem, because the gnat-specific information
23739 is actually not needed for these types. */
23740 if (need_gnat_info (cu)
78134374
SM
23741 && type->code () != TYPE_CODE_FUNC
23742 && type->code () != TYPE_CODE_FLT
23743 && type->code () != TYPE_CODE_METHODPTR
23744 && type->code () != TYPE_CODE_MEMBERPTR
23745 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23746 && !HAVE_GNAT_AUX_INFO (type))
23747 INIT_GNAT_SPECIFIC (type);
23748
3f2f83dd
KB
23749 /* Read DW_AT_allocated and set in type. */
23750 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23751 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23752 {
293e7e51 23753 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23754 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23755 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23756 }
23757 else if (attr != NULL)
23758 {
b98664d3 23759 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23760 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23761 sect_offset_str (die->sect_off));
3f2f83dd
KB
23762 }
23763
23764 /* Read DW_AT_associated and set in type. */
23765 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23766 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23767 {
293e7e51 23768 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23769 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23770 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23771 }
23772 else if (attr != NULL)
23773 {
b98664d3 23774 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23775 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23776 sect_offset_str (die->sect_off));
3f2f83dd
KB
23777 }
23778
3cdcd0ce
JB
23779 /* Read DW_AT_data_location and set in type. */
23780 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23781 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23782 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23783
ae090bdb
SM
23784 if (dwarf2_per_objfile->die_type_hash == NULL)
23785 dwarf2_per_objfile->die_type_hash
0335378b
TT
23786 = htab_up (htab_create_alloc (127,
23787 per_cu_offset_and_type_hash,
23788 per_cu_offset_and_type_eq,
23789 NULL, xcalloc, xfree));
1c379e20 23790
dee91e82 23791 ofs.per_cu = cu->per_cu;
9c541725 23792 ofs.sect_off = die->sect_off;
1c379e20 23793 ofs.type = type;
dee91e82 23794 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23795 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23796 if (*slot)
b98664d3 23797 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23798 sect_offset_str (die->sect_off));
8d749320
SM
23799 *slot = XOBNEW (&objfile->objfile_obstack,
23800 struct dwarf2_per_cu_offset_and_type);
1c379e20 23801 **slot = ofs;
f792889a 23802 return type;
1c379e20
DJ
23803}
23804
9c541725 23805/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23806 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23807
23808static struct type *
9c541725 23809get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23810 dwarf2_per_cu_data *per_cu,
23811 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23812{
dee91e82 23813 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23814
ae090bdb 23815 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23816 return NULL;
1c379e20 23817
dee91e82 23818 ofs.per_cu = per_cu;
9c541725 23819 ofs.sect_off = sect_off;
9a3c8263 23820 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23821 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23822 if (slot)
23823 return slot->type;
23824 else
23825 return NULL;
23826}
23827
02142a6c 23828/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23829 or return NULL if DIE does not have a saved type. */
23830
23831static struct type *
23832get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23833{
aa66c379 23834 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23835}
23836
10b3939b
DJ
23837/* Add a dependence relationship from CU to REF_PER_CU. */
23838
23839static void
23840dwarf2_add_dependence (struct dwarf2_cu *cu,
23841 struct dwarf2_per_cu_data *ref_per_cu)
23842{
23843 void **slot;
23844
23845 if (cu->dependencies == NULL)
23846 cu->dependencies
23847 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23848 NULL, &cu->comp_unit_obstack,
23849 hashtab_obstack_allocate,
23850 dummy_obstack_deallocate);
23851
23852 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23853 if (*slot == NULL)
23854 *slot = ref_per_cu;
23855}
1c379e20 23856
f504f079
DE
23857/* Subroutine of dwarf2_mark to pass to htab_traverse.
23858 Set the mark field in every compilation unit in the
ae038cb0
DJ
23859 cache that we must keep because we are keeping CU. */
23860
10b3939b
DJ
23861static int
23862dwarf2_mark_helper (void **slot, void *data)
23863{
23864 struct dwarf2_per_cu_data *per_cu;
23865
23866 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23867
23868 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23869 reading of the chain. As such dependencies remain valid it is not much
23870 useful to track and undo them during QUIT cleanups. */
23871 if (per_cu->cu == NULL)
23872 return 1;
23873
10b3939b
DJ
23874 if (per_cu->cu->mark)
23875 return 1;
9068261f 23876 per_cu->cu->mark = true;
10b3939b
DJ
23877
23878 if (per_cu->cu->dependencies != NULL)
23879 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23880
23881 return 1;
23882}
23883
f504f079
DE
23884/* Set the mark field in CU and in every other compilation unit in the
23885 cache that we must keep because we are keeping CU. */
23886
ae038cb0
DJ
23887static void
23888dwarf2_mark (struct dwarf2_cu *cu)
23889{
23890 if (cu->mark)
23891 return;
9068261f 23892 cu->mark = true;
10b3939b
DJ
23893 if (cu->dependencies != NULL)
23894 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23895}
23896
23897static void
23898dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23899{
23900 while (per_cu)
23901 {
9068261f 23902 per_cu->cu->mark = false;
ae038cb0
DJ
23903 per_cu = per_cu->cu->read_in_chain;
23904 }
72bf9492
DJ
23905}
23906
72bf9492
DJ
23907/* Trivial hash function for partial_die_info: the hash value of a DIE
23908 is its offset in .debug_info for this objfile. */
23909
23910static hashval_t
23911partial_die_hash (const void *item)
23912{
9a3c8263
SM
23913 const struct partial_die_info *part_die
23914 = (const struct partial_die_info *) item;
9a619af0 23915
9c541725 23916 return to_underlying (part_die->sect_off);
72bf9492
DJ
23917}
23918
23919/* Trivial comparison function for partial_die_info structures: two DIEs
23920 are equal if they have the same offset. */
23921
23922static int
23923partial_die_eq (const void *item_lhs, const void *item_rhs)
23924{
9a3c8263
SM
23925 const struct partial_die_info *part_die_lhs
23926 = (const struct partial_die_info *) item_lhs;
23927 const struct partial_die_info *part_die_rhs
23928 = (const struct partial_die_info *) item_rhs;
9a619af0 23929
9c541725 23930 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23931}
23932
3c3bb058
AB
23933struct cmd_list_element *set_dwarf_cmdlist;
23934struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23935
9291a0cd 23936static void
cd4fb1b2
SM
23937show_check_physname (struct ui_file *file, int from_tty,
23938 struct cmd_list_element *c, const char *value)
9291a0cd 23939{
cd4fb1b2
SM
23940 fprintf_filtered (file,
23941 _("Whether to check \"physname\" is %s.\n"),
23942 value);
9291a0cd
TT
23943}
23944
6c265988 23945void _initialize_dwarf2_read ();
cd4fb1b2 23946void
6c265988 23947_initialize_dwarf2_read ()
9291a0cd 23948{
0743fc83 23949 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23950Set DWARF specific variables.\n\
590042fc 23951Configure DWARF variables such as the cache size."),
0743fc83
TT
23952 &set_dwarf_cmdlist, "maintenance set dwarf ",
23953 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23954
0743fc83 23955 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23956Show DWARF specific variables.\n\
23957Show DWARF variables such as the cache size."),
0743fc83
TT
23958 &show_dwarf_cmdlist, "maintenance show dwarf ",
23959 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23960
cd4fb1b2
SM
23961 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23962 &dwarf_max_cache_age, _("\
23963Set the upper bound on the age of cached DWARF compilation units."), _("\
23964Show the upper bound on the age of cached DWARF compilation units."), _("\
23965A higher limit means that cached compilation units will be stored\n\
23966in memory longer, and more total memory will be used. Zero disables\n\
23967caching, which can slow down startup."),
23968 NULL,
23969 show_dwarf_max_cache_age,
23970 &set_dwarf_cmdlist,
23971 &show_dwarf_cmdlist);
156942c7 23972
cd4fb1b2
SM
23973 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23974Set debugging of the DWARF reader."), _("\
23975Show debugging of the DWARF reader."), _("\
23976When enabled (non-zero), debugging messages are printed during DWARF\n\
23977reading and symtab expansion. A value of 1 (one) provides basic\n\
23978information. A value greater than 1 provides more verbose information."),
23979 NULL,
23980 NULL,
23981 &setdebuglist, &showdebuglist);
9291a0cd 23982
cd4fb1b2
SM
23983 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23984Set debugging of the DWARF DIE reader."), _("\
23985Show debugging of the DWARF DIE reader."), _("\
23986When enabled (non-zero), DIEs are dumped after they are read in.\n\
23987The value is the maximum depth to print."),
23988 NULL,
23989 NULL,
23990 &setdebuglist, &showdebuglist);
9291a0cd 23991
cd4fb1b2
SM
23992 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23993Set debugging of the dwarf line reader."), _("\
23994Show debugging of the dwarf line reader."), _("\
23995When enabled (non-zero), line number entries are dumped as they are read in.\n\
23996A value of 1 (one) provides basic information.\n\
23997A value greater than 1 provides more verbose information."),
23998 NULL,
23999 NULL,
24000 &setdebuglist, &showdebuglist);
437afbb8 24001
cd4fb1b2
SM
24002 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24003Set cross-checking of \"physname\" code against demangler."), _("\
24004Show cross-checking of \"physname\" code against demangler."), _("\
24005When enabled, GDB's internal \"physname\" code is checked against\n\
24006the demangler."),
24007 NULL, show_check_physname,
24008 &setdebuglist, &showdebuglist);
900e11f9 24009
e615022a
DE
24010 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24011 no_class, &use_deprecated_index_sections, _("\
24012Set whether to use deprecated gdb_index sections."), _("\
24013Show whether to use deprecated gdb_index sections."), _("\
24014When enabled, deprecated .gdb_index sections are used anyway.\n\
24015Normally they are ignored either because of a missing feature or\n\
24016performance issue.\n\
24017Warning: This option must be enabled before gdb reads the file."),
24018 NULL,
24019 NULL,
24020 &setlist, &showlist);
24021
f1e6e072
TT
24022 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24023 &dwarf2_locexpr_funcs);
24024 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24025 &dwarf2_loclist_funcs);
24026
24027 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24028 &dwarf2_block_frame_base_locexpr_funcs);
24029 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24030 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24031
24032#if GDB_SELF_TEST
24033 selftests::register_test ("dw2_expand_symtabs_matching",
24034 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24035 selftests::register_test ("dwarf2_find_containing_comp_unit",
24036 selftests::find_containing_comp_unit::run_test);
c62446b1 24037#endif
6502dd73 24038}
This page took 8.038892 seconds and 4 git commands to generate.