Pass dwarf2_cu to process_full_{comp,type}_unit
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
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
8fc0b21d 1573static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1574 enum language pretend_language);
10b3939b 1575
8fc0b21d 1576static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1577 enum language pretend_language);
f4dc4d17 1578
10b3939b
DJ
1579static void dwarf2_add_dependence (struct dwarf2_cu *,
1580 struct dwarf2_per_cu_data *);
1581
ae038cb0
DJ
1582static void dwarf2_mark (struct dwarf2_cu *);
1583
1584static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1585
b64f50a1 1586static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1587 dwarf2_per_cu_data *per_cu,
1588 dwarf2_per_objfile *per_objfile);
673bfd45 1589
f792889a 1590static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1591
120ce1b5
SM
1592static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1593 dwarf2_per_objfile *per_objfile,
95554aad
TT
1594 enum language pretend_language);
1595
ed2dc618 1596static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1597
b303c6f6
AB
1598/* Class, the destructor of which frees all allocated queue entries. This
1599 will only have work to do if an error was thrown while processing the
1600 dwarf. If no error was thrown then the queue entries should have all
1601 been processed, and freed, as we went along. */
1602
1603class dwarf2_queue_guard
1604{
1605public:
39856def
TT
1606 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1607 : m_per_objfile (per_objfile)
1608 {
1609 }
b303c6f6
AB
1610
1611 /* Free any entries remaining on the queue. There should only be
1612 entries left if we hit an error while processing the dwarf. */
1613 ~dwarf2_queue_guard ()
1614 {
39856def
TT
1615 /* Ensure that no memory is allocated by the queue. */
1616 std::queue<dwarf2_queue_item> empty;
5989a64e 1617 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1618 }
b303c6f6 1619
39856def 1620 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1621
39856def
TT
1622private:
1623 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1624};
1625
39856def
TT
1626dwarf2_queue_item::~dwarf2_queue_item ()
1627{
1628 /* Anything still marked queued is likely to be in an
1629 inconsistent state, so discard it. */
1630 if (per_cu->queued)
1631 {
1632 if (per_cu->cu != NULL)
120ce1b5 1633 free_one_cached_comp_unit (per_cu, per_objfile);
39856def
TT
1634 per_cu->queued = 0;
1635 }
1636}
1637
d721ba37
PA
1638/* The return type of find_file_and_directory. Note, the enclosed
1639 string pointers are only valid while this object is valid. */
1640
1641struct file_and_directory
1642{
1643 /* The filename. This is never NULL. */
1644 const char *name;
1645
1646 /* The compilation directory. NULL if not known. If we needed to
1647 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1648 points directly to the DW_AT_comp_dir string attribute owned by
1649 the obstack that owns the DIE. */
1650 const char *comp_dir;
1651
1652 /* If we needed to build a new string for comp_dir, this is what
1653 owns the storage. */
1654 std::string comp_dir_storage;
1655};
1656
1657static file_and_directory find_file_and_directory (struct die_info *die,
1658 struct dwarf2_cu *cu);
9291a0cd 1659
298e9637 1660static htab_up allocate_signatured_type_table ();
1fd400ff 1661
298e9637 1662static htab_up allocate_dwo_unit_table ();
3019eac3 1663
57d63ce2 1664static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1665 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1666 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1667 ULONGEST signature, int is_debug_types);
a2ce51a0 1668
ed2dc618
SM
1669static struct dwp_file *get_dwp_file
1670 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1671
3019eac3 1672static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1673 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1674 ULONGEST signature);
3019eac3
DE
1675
1676static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1677 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1678
89e63ee4
DE
1679static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1680
263db9a1
TT
1681/* A unique pointer to a dwo_file. */
1682
51ac9db5 1683typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1684
ed2dc618 1685static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1686
1b80a9fa 1687static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1688
1689static void free_line_header_voidp (void *arg);
4390d890
DE
1690\f
1691/* Various complaints about symbol reading that don't abort the process. */
1692
4390d890
DE
1693static void
1694dwarf2_debug_line_missing_file_complaint (void)
1695{
b98664d3 1696 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_debug_line_missing_end_sequence_complaint (void)
1701{
b98664d3 1702 complaint (_(".debug_line section has line "
4390d890
DE
1703 "program sequence without an end"));
1704}
1705
1706static void
1707dwarf2_complex_location_expr_complaint (void)
1708{
b98664d3 1709 complaint (_("location expression too complex"));
4390d890
DE
1710}
1711
1712static void
1713dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1714 int arg3)
1715{
b98664d3 1716 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1717 arg1, arg2, arg3);
1718}
1719
4390d890
DE
1720static void
1721dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1722{
b98664d3 1723 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1724 arg1, arg2);
1725}
527f3840
JK
1726
1727/* Hash function for line_header_hash. */
1728
1729static hashval_t
1730line_header_hash (const struct line_header *ofs)
1731{
9c541725 1732 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1733}
1734
1735/* Hash function for htab_create_alloc_ex for line_header_hash. */
1736
1737static hashval_t
1738line_header_hash_voidp (const void *item)
1739{
9a3c8263 1740 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1741
1742 return line_header_hash (ofs);
1743}
1744
1745/* Equality function for line_header_hash. */
1746
1747static int
1748line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1749{
9a3c8263
SM
1750 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1751 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1752
9c541725 1753 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1754 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1755}
1756
4390d890 1757\f
9291a0cd 1758
330cdd98
PA
1759/* See declaration. */
1760
5989a64e
SM
1761dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1762 bool can_copy_)
c3699833
SM
1763 : obfd (obfd),
1764 can_copy (can_copy_)
330cdd98
PA
1765{
1766 if (names == NULL)
1767 names = &dwarf2_elf_names;
1768
330cdd98
PA
1769 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1770 locate_sections (obfd, sec, *names);
1771}
1772
5989a64e 1773dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1774{
1775 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1776 free_cached_comp_units ();
1777
b76e467d 1778 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1779 per_cu->imported_symtabs_free ();
fc8e7e75 1780
b2bdb8cf 1781 for (signatured_type *sig_type : all_type_units)
ae640021 1782 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1783
5989a64e 1784 /* Everything else should be on this->obstack. */
330cdd98
PA
1785}
1786
1787/* See declaration. */
1788
1789void
5989a64e 1790dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1791{
1792 dwarf2_per_cu_data *per_cu = read_in_chain;
1793 dwarf2_per_cu_data **last_chain = &read_in_chain;
1794 while (per_cu != NULL)
1795 {
1796 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1797
fcd3b13d 1798 delete per_cu->cu;
330cdd98
PA
1799 *last_chain = next_cu;
1800 per_cu = next_cu;
1801 }
1802}
1803
11ed8cad
TT
1804/* A helper class that calls free_cached_comp_units on
1805 destruction. */
1806
1807class free_cached_comp_units
1808{
1809public:
1810
1811 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1812 : m_per_objfile (per_objfile)
1813 {
1814 }
1815
1816 ~free_cached_comp_units ()
1817 {
5989a64e 1818 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1819 }
1820
1821 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1822
1823private:
1824
1825 dwarf2_per_objfile *m_per_objfile;
1826};
1827
af758d11
SM
1828/* See read.h. */
1829
1830bool
1831dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1832{
1833 gdb_assert (per_cu->index < this->m_symtabs.size ());
1834
1835 return this->m_symtabs[per_cu->index] != nullptr;
1836}
1837
1838/* See read.h. */
1839
1840compunit_symtab *
1841dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1842{
1843 gdb_assert (per_cu->index < this->m_symtabs.size ());
1844
1845 return this->m_symtabs[per_cu->index];
1846}
1847
1848/* See read.h. */
1849
1850void
1851dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1852 compunit_symtab *symtab)
1853{
1854 gdb_assert (per_cu->index < this->m_symtabs.size ());
1855 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1856
1857 this->m_symtabs[per_cu->index] = symtab;
1858}
1859
c906108c 1860/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1861 information and return true if we have enough to do something.
1862 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1863 ELF names are used. CAN_COPY is true for formats where symbol
1864 interposition is possible and so symbol values must follow copy
1865 relocation rules. */
c906108c
SS
1866
1867int
251d32d9 1868dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1869 const struct dwarf2_debug_sections *names,
1870 bool can_copy)
c906108c 1871{
97cbe998
SDJ
1872 if (objfile->flags & OBJF_READNEVER)
1873 return 0;
1874
ed2dc618
SM
1875 struct dwarf2_per_objfile *dwarf2_per_objfile
1876 = get_dwarf2_per_objfile (objfile);
1877
1878 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1879 {
1880 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1881 sharing yet). */
1882 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1883
1884 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1885 }
5bfd760d 1886
5989a64e
SM
1887 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1888 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1889 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1890 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1891}
1892
251d32d9
TG
1893/* When loading sections, we look either for uncompressed section or for
1894 compressed section names. */
233a11ab
CS
1895
1896static int
251d32d9
TG
1897section_is_p (const char *section_name,
1898 const struct dwarf2_section_names *names)
233a11ab 1899{
251d32d9
TG
1900 if (names->normal != NULL
1901 && strcmp (section_name, names->normal) == 0)
1902 return 1;
1903 if (names->compressed != NULL
1904 && strcmp (section_name, names->compressed) == 0)
1905 return 1;
1906 return 0;
233a11ab
CS
1907}
1908
330cdd98 1909/* See declaration. */
c906108c 1910
330cdd98 1911void
5989a64e
SM
1912dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1913 const dwarf2_debug_sections &names)
c906108c 1914{
fd361982 1915 flagword aflag = bfd_section_flags (sectp);
251d32d9 1916
dc7650b8
JK
1917 if ((aflag & SEC_HAS_CONTENTS) == 0)
1918 {
1919 }
950b7495
KS
1920 else if (elf_section_data (sectp)->this_hdr.sh_size
1921 > bfd_get_file_size (abfd))
1922 {
1923 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1924 warning (_("Discarding section %s which has a section size (%s"
1925 ") larger than the file size [in module %s]"),
1926 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1927 bfd_get_filename (abfd));
1928 }
330cdd98 1929 else if (section_is_p (sectp->name, &names.info))
c906108c 1930 {
330cdd98 1931 this->info.s.section = sectp;
fd361982 1932 this->info.size = bfd_section_size (sectp);
c906108c 1933 }
330cdd98 1934 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1935 {
330cdd98 1936 this->abbrev.s.section = sectp;
fd361982 1937 this->abbrev.size = bfd_section_size (sectp);
c906108c 1938 }
330cdd98 1939 else if (section_is_p (sectp->name, &names.line))
c906108c 1940 {
330cdd98 1941 this->line.s.section = sectp;
fd361982 1942 this->line.size = bfd_section_size (sectp);
c906108c 1943 }
330cdd98 1944 else if (section_is_p (sectp->name, &names.loc))
c906108c 1945 {
330cdd98 1946 this->loc.s.section = sectp;
fd361982 1947 this->loc.size = bfd_section_size (sectp);
c906108c 1948 }
330cdd98 1949 else if (section_is_p (sectp->name, &names.loclists))
43988095 1950 {
330cdd98 1951 this->loclists.s.section = sectp;
fd361982 1952 this->loclists.size = bfd_section_size (sectp);
43988095 1953 }
330cdd98 1954 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1955 {
330cdd98 1956 this->macinfo.s.section = sectp;
fd361982 1957 this->macinfo.size = bfd_section_size (sectp);
c906108c 1958 }
330cdd98 1959 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1960 {
330cdd98 1961 this->macro.s.section = sectp;
fd361982 1962 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1963 }
330cdd98 1964 else if (section_is_p (sectp->name, &names.str))
c906108c 1965 {
330cdd98 1966 this->str.s.section = sectp;
fd361982 1967 this->str.size = bfd_section_size (sectp);
c906108c 1968 }
18a8505e
AT
1969 else if (section_is_p (sectp->name, &names.str_offsets))
1970 {
1971 this->str_offsets.s.section = sectp;
1972 this->str_offsets.size = bfd_section_size (sectp);
1973 }
330cdd98 1974 else if (section_is_p (sectp->name, &names.line_str))
43988095 1975 {
330cdd98 1976 this->line_str.s.section = sectp;
fd361982 1977 this->line_str.size = bfd_section_size (sectp);
43988095 1978 }
330cdd98 1979 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1980 {
330cdd98 1981 this->addr.s.section = sectp;
fd361982 1982 this->addr.size = bfd_section_size (sectp);
3019eac3 1983 }
330cdd98 1984 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1985 {
330cdd98 1986 this->frame.s.section = sectp;
fd361982 1987 this->frame.size = bfd_section_size (sectp);
b6af0555 1988 }
330cdd98 1989 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1990 {
330cdd98 1991 this->eh_frame.s.section = sectp;
fd361982 1992 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1993 }
330cdd98 1994 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1995 {
330cdd98 1996 this->ranges.s.section = sectp;
fd361982 1997 this->ranges.size = bfd_section_size (sectp);
af34e669 1998 }
330cdd98 1999 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2000 {
330cdd98 2001 this->rnglists.s.section = sectp;
fd361982 2002 this->rnglists.size = bfd_section_size (sectp);
43988095 2003 }
330cdd98 2004 else if (section_is_p (sectp->name, &names.types))
348e048f 2005 {
8b70b953
TT
2006 struct dwarf2_section_info type_section;
2007
2008 memset (&type_section, 0, sizeof (type_section));
049412e3 2009 type_section.s.section = sectp;
fd361982 2010 type_section.size = bfd_section_size (sectp);
8b70b953 2011
fd5866f6 2012 this->types.push_back (type_section);
348e048f 2013 }
330cdd98 2014 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2015 {
330cdd98 2016 this->gdb_index.s.section = sectp;
fd361982 2017 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2018 }
927aa2e7
JK
2019 else if (section_is_p (sectp->name, &names.debug_names))
2020 {
2021 this->debug_names.s.section = sectp;
fd361982 2022 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2023 }
2024 else if (section_is_p (sectp->name, &names.debug_aranges))
2025 {
2026 this->debug_aranges.s.section = sectp;
fd361982 2027 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2028 }
dce234bc 2029
fd361982
AM
2030 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2031 && bfd_section_vma (sectp) == 0)
330cdd98 2032 this->has_section_at_zero = true;
c906108c
SS
2033}
2034
dce234bc 2035/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2036 SECTION_NAME. */
af34e669 2037
dce234bc 2038void
3017a003
TG
2039dwarf2_get_section_info (struct objfile *objfile,
2040 enum dwarf2_section_enum sect,
d521ce57 2041 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2042 bfd_size_type *sizep)
2043{
5bfd760d 2044 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2045 struct dwarf2_section_info *info;
a3b2a86b
TT
2046
2047 /* We may see an objfile without any DWARF, in which case we just
2048 return nothing. */
2049 if (data == NULL)
2050 {
2051 *sectp = NULL;
2052 *bufp = NULL;
2053 *sizep = 0;
2054 return;
2055 }
3017a003
TG
2056 switch (sect)
2057 {
2058 case DWARF2_DEBUG_FRAME:
5989a64e 2059 info = &data->per_bfd->frame;
3017a003
TG
2060 break;
2061 case DWARF2_EH_FRAME:
5989a64e 2062 info = &data->per_bfd->eh_frame;
3017a003
TG
2063 break;
2064 default:
2065 gdb_assert_not_reached ("unexpected section");
2066 }
dce234bc 2067
96b79293 2068 info->read (objfile);
dce234bc 2069
96b79293 2070 *sectp = info->get_bfd_section ();
dce234bc
PP
2071 *bufp = info->buffer;
2072 *sizep = info->size;
2073}
2074
36586728
TT
2075/* A helper function to find the sections for a .dwz file. */
2076
2077static void
2078locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2079{
9a3c8263 2080 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2081
2082 /* Note that we only support the standard ELF names, because .dwz
2083 is ELF-only (at the time of writing). */
2084 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2085 {
049412e3 2086 dwz_file->abbrev.s.section = sectp;
fd361982 2087 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2088 }
2089 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2090 {
049412e3 2091 dwz_file->info.s.section = sectp;
fd361982 2092 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2093 }
2094 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2095 {
049412e3 2096 dwz_file->str.s.section = sectp;
fd361982 2097 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2098 }
2099 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2100 {
049412e3 2101 dwz_file->line.s.section = sectp;
fd361982 2102 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2103 }
2104 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2105 {
049412e3 2106 dwz_file->macro.s.section = sectp;
fd361982 2107 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2108 }
2ec9a5e0
TT
2109 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2110 {
049412e3 2111 dwz_file->gdb_index.s.section = sectp;
fd361982 2112 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2113 }
927aa2e7
JK
2114 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2115 {
2116 dwz_file->debug_names.s.section = sectp;
fd361982 2117 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2118 }
36586728
TT
2119}
2120
c4973306 2121/* See dwarf2read.h. */
36586728 2122
c4973306 2123struct dwz_file *
c3699833 2124dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2125{
36586728 2126 const char *filename;
acd13123 2127 bfd_size_type buildid_len_arg;
dc294be5
TT
2128 size_t buildid_len;
2129 bfd_byte *buildid;
36586728 2130
c3699833
SM
2131 if (per_bfd->dwz_file != NULL)
2132 return per_bfd->dwz_file.get ();
36586728 2133
4db1a1dc 2134 bfd_set_error (bfd_error_no_error);
791afaa2 2135 gdb::unique_xmalloc_ptr<char> data
c3699833 2136 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2137 &buildid_len_arg, &buildid));
4db1a1dc
TT
2138 if (data == NULL)
2139 {
2140 if (bfd_get_error () == bfd_error_no_error)
2141 return NULL;
2142 error (_("could not read '.gnu_debugaltlink' section: %s"),
2143 bfd_errmsg (bfd_get_error ()));
2144 }
791afaa2
TT
2145
2146 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2147
acd13123
TT
2148 buildid_len = (size_t) buildid_len_arg;
2149
791afaa2 2150 filename = data.get ();
d721ba37
PA
2151
2152 std::string abs_storage;
36586728
TT
2153 if (!IS_ABSOLUTE_PATH (filename))
2154 {
14278e1f 2155 gdb::unique_xmalloc_ptr<char> abs
c3699833 2156 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2157
14278e1f 2158 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2159 filename = abs_storage.c_str ();
36586728
TT
2160 }
2161
dc294be5
TT
2162 /* First try the file name given in the section. If that doesn't
2163 work, try to use the build-id instead. */
ad80db5b 2164 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2165 if (dwz_bfd != NULL)
36586728 2166 {
192b62ce 2167 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2168 dwz_bfd.reset (nullptr);
36586728
TT
2169 }
2170
dc294be5
TT
2171 if (dwz_bfd == NULL)
2172 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2173
0d79cdc4
AM
2174 if (dwz_bfd == nullptr)
2175 {
2176 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2177 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2178
2179 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2180 buildid_len,
2181 origname,
2182 &alt_filename));
2183
2184 if (fd.get () >= 0)
2185 {
2186 /* File successfully retrieved from server. */
ad80db5b 2187 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2188
2189 if (dwz_bfd == nullptr)
2190 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2191 alt_filename.get ());
2192 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2193 dwz_bfd.reset (nullptr);
2194 }
2195 }
2196
dc294be5
TT
2197 if (dwz_bfd == NULL)
2198 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2199 bfd_get_filename (per_bfd->obfd));
dc294be5 2200
7ff8cb8c
TT
2201 std::unique_ptr<struct dwz_file> result
2202 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2203
7ff8cb8c
TT
2204 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2205 result.get ());
36586728 2206
c3699833
SM
2207 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2208 per_bfd->dwz_file = std::move (result);
2209 return per_bfd->dwz_file.get ();
36586728 2210}
9291a0cd 2211\f
7b9f3c50
DE
2212/* DWARF quick_symbols_functions support. */
2213
2214/* TUs can share .debug_line entries, and there can be a lot more TUs than
2215 unique line tables, so we maintain a separate table of all .debug_line
2216 derived entries to support the sharing.
2217 All the quick functions need is the list of file names. We discard the
2218 line_header when we're done and don't need to record it here. */
2219struct quick_file_names
2220{
094b34ac
DE
2221 /* The data used to construct the hash key. */
2222 struct stmt_list_hash hash;
7b9f3c50
DE
2223
2224 /* The number of entries in file_names, real_names. */
2225 unsigned int num_file_names;
2226
2227 /* The file names from the line table, after being run through
2228 file_full_name. */
2229 const char **file_names;
2230
2231 /* The file names from the line table after being run through
2232 gdb_realpath. These are computed lazily. */
2233 const char **real_names;
2234};
2235
2236/* When using the index (and thus not using psymtabs), each CU has an
2237 object of this type. This is used to hold information needed by
2238 the various "quick" methods. */
2239struct dwarf2_per_cu_quick_data
2240{
2241 /* The file table. This can be NULL if there was no file table
2242 or it's currently not read in.
5989a64e 2243 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2244 struct quick_file_names *file_names;
2245
7b9f3c50
DE
2246 /* A temporary mark bit used when iterating over all CUs in
2247 expand_symtabs_matching. */
2248 unsigned int mark : 1;
2249
2250 /* True if we've tried to read the file table and found there isn't one.
2251 There will be no point in trying to read it again next time. */
2252 unsigned int no_file_data : 1;
2253};
2254
094b34ac
DE
2255/* Utility hash function for a stmt_list_hash. */
2256
2257static hashval_t
2258hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2259{
2260 hashval_t v = 0;
2261
2262 if (stmt_list_hash->dwo_unit != NULL)
2263 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2264 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2265 return v;
2266}
2267
2268/* Utility equality function for a stmt_list_hash. */
2269
2270static int
2271eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2272 const struct stmt_list_hash *rhs)
2273{
2274 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2275 return 0;
2276 if (lhs->dwo_unit != NULL
2277 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2278 return 0;
2279
9c541725 2280 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2281}
2282
7b9f3c50
DE
2283/* Hash function for a quick_file_names. */
2284
2285static hashval_t
2286hash_file_name_entry (const void *e)
2287{
9a3c8263
SM
2288 const struct quick_file_names *file_data
2289 = (const struct quick_file_names *) e;
7b9f3c50 2290
094b34ac 2291 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2292}
2293
2294/* Equality function for a quick_file_names. */
2295
2296static int
2297eq_file_name_entry (const void *a, const void *b)
2298{
9a3c8263
SM
2299 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2300 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2301
094b34ac 2302 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2303}
2304
2305/* Delete function for a quick_file_names. */
2306
2307static void
2308delete_file_name_entry (void *e)
2309{
9a3c8263 2310 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2311 int i;
2312
2313 for (i = 0; i < file_data->num_file_names; ++i)
2314 {
2315 xfree ((void*) file_data->file_names[i]);
2316 if (file_data->real_names)
2317 xfree ((void*) file_data->real_names[i]);
2318 }
2319
45940949
TT
2320 /* The space for the struct itself lives on the obstack, so we don't
2321 free it here. */
7b9f3c50
DE
2322}
2323
2324/* Create a quick_file_names hash table. */
2325
5895093f 2326static htab_up
7b9f3c50
DE
2327create_quick_file_names_table (unsigned int nr_initial_entries)
2328{
5895093f
TT
2329 return htab_up (htab_create_alloc (nr_initial_entries,
2330 hash_file_name_entry, eq_file_name_entry,
2331 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2332}
9291a0cd 2333
ab432490
SM
2334/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2335 function is unrelated to symtabs, symtab would have to be created afterwards.
2336 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2337
2338static void
ab432490
SM
2339load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2340 bool skip_partial)
918dd910 2341{
3019eac3 2342 if (per_cu->is_debug_types)
ab432490 2343 load_full_type_unit (per_cu, per_objfile);
918dd910 2344 else
ab432490 2345 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2346
cc12ce38
DE
2347 if (per_cu->cu == NULL)
2348 return; /* Dummy CU. */
2dc860c0
DE
2349
2350 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2351}
2352
97a1449a 2353/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2354
9291a0cd 2355static void
97a1449a
SM
2356dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2357 dwarf2_per_objfile *dwarf2_per_objfile,
2358 bool skip_partial)
9291a0cd 2359{
f4dc4d17
DE
2360 /* Skip type_unit_groups, reading the type units they contain
2361 is handled elsewhere. */
197400e8 2362 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2363 return;
2364
b303c6f6
AB
2365 /* The destructor of dwarf2_queue_guard frees any entries left on
2366 the queue. After this point we're guaranteed to leave this function
2367 with the dwarf queue empty. */
39856def 2368 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2369
af758d11 2370 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2371 {
120ce1b5 2372 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
ab432490 2373 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2374
2375 /* If we just loaded a CU from a DWO, and we're working with an index
2376 that may badly handle TUs, load all the TUs in that DWO as well.
2377 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2378 if (!per_cu->is_debug_types
cc12ce38 2379 && per_cu->cu != NULL
89e63ee4 2380 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2381 && dwarf2_per_objfile->per_bfd->index_table != NULL
2382 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2383 /* DWP files aren't supported yet. */
ed2dc618 2384 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2385 queue_and_load_all_dwo_tus (per_cu);
95554aad 2386 }
9291a0cd 2387
ed2dc618 2388 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2389
2390 /* Age the cache, releasing compilation units that have not
2391 been used recently. */
ed2dc618 2392 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2393}
2394
97a1449a
SM
2395/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2396 the per-objfile for which this symtab is instantiated.
2397
2398 Returns the resulting symbol table. */
2fdf6df6 2399
43f3e411 2400static struct compunit_symtab *
97a1449a
SM
2401dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2402 dwarf2_per_objfile *dwarf2_per_objfile,
2403 bool skip_partial)
9291a0cd 2404{
5989a64e 2405 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2406
2407 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2408 {
11ed8cad 2409 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2410 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2411 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2412 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2413 }
f194fefb 2414
af758d11 2415 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2416}
2417
ff4c9fec 2418/* See declaration. */
f4dc4d17 2419
ff4c9fec 2420dwarf2_per_cu_data *
5989a64e 2421dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2422{
b76e467d 2423 if (index >= this->all_comp_units.size ())
ff4c9fec 2424 {
b76e467d 2425 index -= this->all_comp_units.size ();
b2bdb8cf 2426 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2427 return &this->all_type_units[index]->per_cu;
2428 }
f4dc4d17 2429
ff4c9fec
SM
2430 return this->all_comp_units[index];
2431}
f4dc4d17 2432
ff4c9fec 2433/* See declaration. */
2fdf6df6 2434
ff4c9fec 2435dwarf2_per_cu_data *
5989a64e 2436dwarf2_per_bfd::get_cu (int index)
1fd400ff 2437{
b76e467d 2438 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2439
ff4c9fec 2440 return this->all_comp_units[index];
f4dc4d17
DE
2441}
2442
ff4c9fec 2443/* See declaration. */
f4dc4d17 2444
ff4c9fec 2445signatured_type *
5989a64e 2446dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2447{
b2bdb8cf 2448 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2449
ff4c9fec 2450 return this->all_type_units[index];
1fd400ff
TT
2451}
2452
d3473f0c
TT
2453/* See read.h. */
2454
2455dwarf2_per_cu_data *
5989a64e 2456dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2457{
2458 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2459 result->per_bfd = this;
d3473f0c
TT
2460 result->index = m_num_psymtabs++;
2461 return result;
2462}
2463
2464/* See read.h. */
2465
2466signatured_type *
5989a64e 2467dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2468{
2469 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2470 result->per_cu.per_bfd = this;
d3473f0c
TT
2471 result->per_cu.index = m_num_psymtabs++;
2472 return result;
2473}
2474
168c9250 2475/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2476 obstack, and constructed with the specified field values. */
4b514bc8
JK
2477
2478static dwarf2_per_cu_data *
168c9250
SM
2479create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2480 struct dwarf2_section_info *section,
2481 int is_dwz,
2482 sect_offset sect_off, ULONGEST length)
4b514bc8 2483{
168c9250 2484 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2485 the_cu->sect_off = sect_off;
2486 the_cu->length = length;
4b514bc8 2487 the_cu->section = section;
168c9250 2488 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2489 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2490 the_cu->is_dwz = is_dwz;
2491 return the_cu;
2492}
2493
2ec9a5e0
TT
2494/* A helper for create_cus_from_index that handles a given list of
2495 CUs. */
2fdf6df6 2496
74a0d9f6 2497static void
168c9250 2498create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2499 const gdb_byte *cu_list, offset_type n_elements,
2500 struct dwarf2_section_info *section,
b76e467d 2501 int is_dwz)
9291a0cd 2502{
12359b5e 2503 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2504 {
74a0d9f6 2505 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2506
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2509 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2510 cu_list += 2 * 8;
2511
b76e467d 2512 dwarf2_per_cu_data *per_cu
168c9250
SM
2513 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2514 length);
2515 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2516 }
9291a0cd
TT
2517}
2518
2ec9a5e0 2519/* Read the CU list from the mapped index, and use it to create all
168c9250 2520 the CU objects for PER_BFD. */
2ec9a5e0 2521
74a0d9f6 2522static void
168c9250 2523create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2524 const gdb_byte *cu_list, offset_type cu_list_elements,
2525 const gdb_byte *dwz_list, offset_type dwz_elements)
2526{
168c9250
SM
2527 gdb_assert (per_bfd->all_comp_units.empty ());
2528 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2529
168c9250
SM
2530 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2531 &per_bfd->info, 0);
2ec9a5e0
TT
2532
2533 if (dwz_elements == 0)
74a0d9f6 2534 return;
2ec9a5e0 2535
168c9250
SM
2536 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2537 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2538 &dwz->info, 1);
2ec9a5e0
TT
2539}
2540
1fd400ff 2541/* Create the signatured type hash table from the index. */
673bfd45 2542
74a0d9f6 2543static void
12359b5e 2544create_signatured_type_table_from_index
168c9250
SM
2545 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2546 const gdb_byte *bytes, offset_type elements)
1fd400ff 2547{
168c9250
SM
2548 gdb_assert (per_bfd->all_type_units.empty ());
2549 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2550
298e9637 2551 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2552
12359b5e 2553 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2554 {
52dc124a 2555 struct signatured_type *sig_type;
9c541725 2556 ULONGEST signature;
1fd400ff 2557 void **slot;
9c541725 2558 cu_offset type_offset_in_tu;
1fd400ff 2559
74a0d9f6 2560 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2561 sect_offset sect_off
2562 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2563 type_offset_in_tu
2564 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2565 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2566 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2567 bytes += 3 * 8;
2568
168c9250 2569 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2570 sig_type->signature = signature;
9c541725 2571 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2572 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2573 sig_type->per_cu.section = section;
9c541725 2574 sig_type->per_cu.sect_off = sect_off;
52dc124a 2575 sig_type->per_cu.v.quick
168c9250 2576 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2577 struct dwarf2_per_cu_quick_data);
2578
b0b6a987 2579 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2580 *slot = sig_type;
1fd400ff 2581
168c9250 2582 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2583 }
2584
168c9250 2585 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2586}
2587
927aa2e7
JK
2588/* Create the signatured type hash table from .debug_names. */
2589
2590static void
2591create_signatured_type_table_from_debug_names
ed2dc618 2592 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2593 const mapped_debug_names &map,
2594 struct dwarf2_section_info *section,
2595 struct dwarf2_section_info *abbrev_section)
2596{
ed2dc618
SM
2597 struct objfile *objfile = dwarf2_per_objfile->objfile;
2598
96b79293
TT
2599 section->read (objfile);
2600 abbrev_section->read (objfile);
927aa2e7 2601
5989a64e
SM
2602 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2603 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2604
298e9637 2605 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2606
2607 for (uint32_t i = 0; i < map.tu_count; ++i)
2608 {
2609 struct signatured_type *sig_type;
927aa2e7 2610 void **slot;
927aa2e7
JK
2611
2612 sect_offset sect_off
2613 = (sect_offset) (extract_unsigned_integer
2614 (map.tu_table_reordered + i * map.offset_size,
2615 map.offset_size,
2616 map.dwarf5_byte_order));
2617
2618 comp_unit_head cu_header;
ed2dc618
SM
2619 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2620 abbrev_section,
927aa2e7
JK
2621 section->buffer + to_underlying (sect_off),
2622 rcuh_kind::TYPE);
2623
5989a64e 2624 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2625 sig_type->signature = cu_header.signature;
2626 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2627 sig_type->per_cu.is_debug_types = 1;
2628 sig_type->per_cu.section = section;
2629 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2630 sig_type->per_cu.v.quick
5989a64e 2631 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2632 struct dwarf2_per_cu_quick_data);
2633
b0b6a987 2634 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2635 *slot = sig_type;
2636
5989a64e 2637 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2638 }
2639
5989a64e 2640 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2641}
2642
9291a0cd
TT
2643/* Read the address map data from the mapped index, and use it to
2644 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2645
9291a0cd 2646static void
ed2dc618
SM
2647create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2648 struct mapped_index *index)
9291a0cd 2649{
ed2dc618 2650 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2651 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2652 const gdb_byte *iter, *end;
9291a0cd 2653 struct addrmap *mutable_map;
9291a0cd
TT
2654 CORE_ADDR baseaddr;
2655
8268c778
PA
2656 auto_obstack temp_obstack;
2657
9291a0cd
TT
2658 mutable_map = addrmap_create_mutable (&temp_obstack);
2659
f00a2de2
PA
2660 iter = index->address_table.data ();
2661 end = iter + index->address_table.size ();
9291a0cd 2662
b3b3bada 2663 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2664
2665 while (iter < end)
2666 {
2667 ULONGEST hi, lo, cu_index;
2668 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2669 iter += 8;
2670 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2671 iter += 8;
2672 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2673 iter += 4;
f652bce2 2674
24a55014 2675 if (lo > hi)
f652bce2 2676 {
b98664d3 2677 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2678 hex_string (lo), hex_string (hi));
24a55014 2679 continue;
f652bce2 2680 }
24a55014 2681
5989a64e 2682 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2683 {
b98664d3 2684 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2685 (unsigned) cu_index);
24a55014 2686 continue;
f652bce2 2687 }
24a55014 2688
79748972
TT
2689 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2690 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2691 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2692 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2693 }
2694
d320c2b5 2695 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2696 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2697}
2698
927aa2e7
JK
2699/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2700 populate the objfile's psymtabs_addrmap. */
2701
2702static void
ed2dc618 2703create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2704 struct dwarf2_section_info *section)
2705{
ed2dc618 2706 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2707 bfd *abfd = objfile->obfd;
08feed99 2708 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2709 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2710
2711 auto_obstack temp_obstack;
2712 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2713
2714 std::unordered_map<sect_offset,
2715 dwarf2_per_cu_data *,
2716 gdb::hash_enum<sect_offset>>
2717 debug_info_offset_to_per_cu;
5989a64e 2718 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2719 {
927aa2e7
JK
2720 const auto insertpair
2721 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2722 if (!insertpair.second)
2723 {
2724 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2725 "debug_info_offset %s, ignoring .debug_aranges."),
2726 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2727 return;
2728 }
2729 }
2730
96b79293 2731 section->read (objfile);
927aa2e7
JK
2732
2733 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2734
2735 const gdb_byte *addr = section->buffer;
2736
2737 while (addr < section->buffer + section->size)
2738 {
2739 const gdb_byte *const entry_addr = addr;
2740 unsigned int bytes_read;
2741
2742 const LONGEST entry_length = read_initial_length (abfd, addr,
2743 &bytes_read);
2744 addr += bytes_read;
2745
2746 const gdb_byte *const entry_end = addr + entry_length;
2747 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2748 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2749 if (addr + entry_length > section->buffer + section->size)
2750 {
47e3f474 2751 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2752 "length %s exceeds section length %s, "
2753 "ignoring .debug_aranges."),
47e3f474
TV
2754 objfile_name (objfile),
2755 plongest (entry_addr - section->buffer),
927aa2e7
JK
2756 plongest (bytes_read + entry_length),
2757 pulongest (section->size));
2758 return;
2759 }
2760
2761 /* The version number. */
2762 const uint16_t version = read_2_bytes (abfd, addr);
2763 addr += 2;
2764 if (version != 2)
2765 {
47e3f474 2766 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2767 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2770 return;
2771 }
2772
2773 const uint64_t debug_info_offset
2774 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2775 addr += offset_size;
2776 const auto per_cu_it
2777 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2778 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2779 {
47e3f474 2780 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2781 "debug_info_offset %s does not exists, "
2782 "ignoring .debug_aranges."),
47e3f474
TV
2783 objfile_name (objfile),
2784 plongest (entry_addr - section->buffer),
927aa2e7
JK
2785 pulongest (debug_info_offset));
2786 return;
2787 }
2788 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2789
2790 const uint8_t address_size = *addr++;
2791 if (address_size < 1 || address_size > 8)
2792 {
47e3f474 2793 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2794 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2795 objfile_name (objfile),
2796 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2797 return;
2798 }
2799
2800 const uint8_t segment_selector_size = *addr++;
2801 if (segment_selector_size != 0)
2802 {
47e3f474 2803 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2804 "segment_selector_size %u is not supported, "
2805 "ignoring .debug_aranges."),
47e3f474
TV
2806 objfile_name (objfile),
2807 plongest (entry_addr - section->buffer),
927aa2e7
JK
2808 segment_selector_size);
2809 return;
2810 }
2811
2812 /* Must pad to an alignment boundary that is twice the address
2813 size. It is undocumented by the DWARF standard but GCC does
2814 use it. */
2815 for (size_t padding = ((-(addr - section->buffer))
2816 & (2 * address_size - 1));
2817 padding > 0; padding--)
2818 if (*addr++ != 0)
2819 {
47e3f474 2820 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2821 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2822 objfile_name (objfile),
2823 plongest (entry_addr - section->buffer));
927aa2e7
JK
2824 return;
2825 }
2826
2827 for (;;)
2828 {
2829 if (addr + 2 * address_size > entry_end)
2830 {
47e3f474 2831 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2832 "address list is not properly terminated, "
2833 "ignoring .debug_aranges."),
47e3f474
TV
2834 objfile_name (objfile),
2835 plongest (entry_addr - section->buffer));
927aa2e7
JK
2836 return;
2837 }
2838 ULONGEST start = extract_unsigned_integer (addr, address_size,
2839 dwarf5_byte_order);
2840 addr += address_size;
2841 ULONGEST length = extract_unsigned_integer (addr, address_size,
2842 dwarf5_byte_order);
2843 addr += address_size;
2844 if (start == 0 && length == 0)
2845 break;
5989a64e 2846 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2847 {
2848 /* Symbol was eliminated due to a COMDAT group. */
2849 continue;
2850 }
2851 ULONGEST end = start + length;
79748972
TT
2852 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2853 - baseaddr);
2854 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2855 - baseaddr);
927aa2e7
JK
2856 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2857 }
2858 }
2859
d320c2b5 2860 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2861 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2862}
2863
9291a0cd
TT
2864/* Find a slot in the mapped index INDEX for the object named NAME.
2865 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2866 constant pool and return true. If NAME cannot be found, return
2867 false. */
2fdf6df6 2868
109483d9 2869static bool
9291a0cd
TT
2870find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2871 offset_type **vec_out)
2872{
0cf03b49 2873 offset_type hash;
9291a0cd 2874 offset_type slot, step;
559a7a62 2875 int (*cmp) (const char *, const char *);
9291a0cd 2876
791afaa2 2877 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2878 if (current_language->la_language == language_cplus
45280282
IB
2879 || current_language->la_language == language_fortran
2880 || current_language->la_language == language_d)
0cf03b49
JK
2881 {
2882 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2883 not contain any. */
a8719064 2884
72998fb3 2885 if (strchr (name, '(') != NULL)
0cf03b49 2886 {
109483d9 2887 without_params = cp_remove_params (name);
0cf03b49 2888
72998fb3 2889 if (without_params != NULL)
791afaa2 2890 name = without_params.get ();
0cf03b49
JK
2891 }
2892 }
2893
559a7a62 2894 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2895 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2896 simulate our NAME being searched is also lowercased. */
2897 hash = mapped_index_string_hash ((index->version == 4
2898 && case_sensitivity == case_sensitive_off
2899 ? 5 : index->version),
2900 name);
2901
f00a2de2
PA
2902 slot = hash & (index->symbol_table.size () - 1);
2903 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2904 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2905
2906 for (;;)
2907 {
9291a0cd 2908 const char *str;
f00a2de2
PA
2909
2910 const auto &bucket = index->symbol_table[slot];
2911 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2912 return false;
9291a0cd 2913
f00a2de2 2914 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2915 if (!cmp (name, str))
9291a0cd
TT
2916 {
2917 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2918 + MAYBE_SWAP (bucket.vec));
109483d9 2919 return true;
9291a0cd
TT
2920 }
2921
f00a2de2 2922 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2923 }
2924}
2925
4485a1c1
SM
2926/* A helper function that reads the .gdb_index from BUFFER and fills
2927 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2928 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2929 ok to use deprecated sections.
2930
2931 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2932 out parameters that are filled in with information about the CU and
2933 TU lists in the section.
2934
4485a1c1 2935 Returns true if all went well, false otherwise. */
2fdf6df6 2936
d33bc52e 2937static bool
3810f182 2938read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2939 bool deprecated_ok,
2940 gdb::array_view<const gdb_byte> buffer,
2941 struct mapped_index *map,
2942 const gdb_byte **cu_list,
2943 offset_type *cu_list_elements,
2944 const gdb_byte **types_list,
2945 offset_type *types_list_elements)
2946{
2947 const gdb_byte *addr = &buffer[0];
82430852 2948
9291a0cd 2949 /* Version check. */
4485a1c1 2950 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2951 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2952 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2953 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2954 indices. */
831adc1f 2955 if (version < 4)
481860b3
GB
2956 {
2957 static int warning_printed = 0;
2958 if (!warning_printed)
2959 {
2960 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2961 filename);
481860b3
GB
2962 warning_printed = 1;
2963 }
2964 return 0;
2965 }
2966 /* Index version 4 uses a different hash function than index version
2967 5 and later.
2968
2969 Versions earlier than 6 did not emit psymbols for inlined
2970 functions. Using these files will cause GDB not to be able to
2971 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2972 indices unless the user has done
2973 "set use-deprecated-index-sections on". */
2ec9a5e0 2974 if (version < 6 && !deprecated_ok)
481860b3
GB
2975 {
2976 static int warning_printed = 0;
2977 if (!warning_printed)
2978 {
e615022a
DE
2979 warning (_("\
2980Skipping deprecated .gdb_index section in %s.\n\
2981Do \"set use-deprecated-index-sections on\" before the file is read\n\
2982to use the section anyway."),
2ec9a5e0 2983 filename);
481860b3
GB
2984 warning_printed = 1;
2985 }
2986 return 0;
2987 }
796a7ff8 2988 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2989 of the TU (for symbols coming from TUs),
2990 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2991 Plus gold-generated indices can have duplicate entries for global symbols,
2992 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2993 These are just performance bugs, and we can't distinguish gdb-generated
2994 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2995
481860b3 2996 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2997 longer backward compatible. */
796a7ff8 2998 if (version > 8)
594e8718 2999 return 0;
9291a0cd 3000
559a7a62 3001 map->version = version;
9291a0cd 3002
4485a1c1 3003 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3004
4485a1c1 3005 int i = 0;
2ec9a5e0
TT
3006 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3007 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3008 / 8);
1fd400ff
TT
3009 ++i;
3010
2ec9a5e0
TT
3011 *types_list = addr + MAYBE_SWAP (metadata[i]);
3012 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3013 - MAYBE_SWAP (metadata[i]))
3014 / 8);
987d643c 3015 ++i;
1fd400ff 3016
f00a2de2
PA
3017 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3018 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3019 map->address_table
3020 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3021 ++i;
3022
f00a2de2
PA
3023 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3024 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3025 map->symbol_table
3026 = gdb::array_view<mapped_index::symbol_table_slot>
3027 ((mapped_index::symbol_table_slot *) symbol_table,
3028 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3029
f00a2de2 3030 ++i;
f9d83a0b 3031 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3032
2ec9a5e0
TT
3033 return 1;
3034}
3035
4485a1c1
SM
3036/* Callback types for dwarf2_read_gdb_index. */
3037
3038typedef gdb::function_view
5989a64e 3039 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3040 get_gdb_index_contents_ftype;
3041typedef gdb::function_view
3042 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3043 get_gdb_index_contents_dwz_ftype;
3044
927aa2e7 3045/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3046 elements of all the CUs and return 1. Otherwise, return 0. */
3047
3048static int
4485a1c1
SM
3049dwarf2_read_gdb_index
3050 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3051 get_gdb_index_contents_ftype get_gdb_index_contents,
3052 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3053{
2ec9a5e0
TT
3054 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3055 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3056 struct dwz_file *dwz;
12359b5e 3057 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3058
4485a1c1 3059 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3060 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3061
3062 if (main_index_contents.empty ())
3063 return 0;
3064
3063847f 3065 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3066 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3067 use_deprecated_index_sections,
3068 main_index_contents, map.get (), &cu_list,
3069 &cu_list_elements, &types_list,
3070 &types_list_elements))
2ec9a5e0
TT
3071 return 0;
3072
0fefef59 3073 /* Don't use the index if it's empty. */
3063847f 3074 if (map->symbol_table.empty ())
0fefef59
DE
3075 return 0;
3076
2ec9a5e0
TT
3077 /* If there is a .dwz file, read it so we can get its CU list as
3078 well. */
c3699833 3079 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3080 if (dwz != NULL)
2ec9a5e0 3081 {
2ec9a5e0
TT
3082 struct mapped_index dwz_map;
3083 const gdb_byte *dwz_types_ignore;
3084 offset_type dwz_types_elements_ignore;
3085
4485a1c1
SM
3086 gdb::array_view<const gdb_byte> dwz_index_content
3087 = get_gdb_index_contents_dwz (objfile, dwz);
3088
3089 if (dwz_index_content.empty ())
3090 return 0;
3091
3810f182 3092 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3093 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3094 &dwz_list, &dwz_list_elements,
3095 &dwz_types_ignore,
3096 &dwz_types_elements_ignore))
2ec9a5e0
TT
3097 {
3098 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3099 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3100 return 0;
3101 }
3102 }
3103
168c9250 3104 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3105 dwz_list, dwz_list_elements);
1fd400ff 3106
8b70b953
TT
3107 if (types_list_elements)
3108 {
8b70b953
TT
3109 /* We can only handle a single .debug_types when we have an
3110 index. */
5989a64e 3111 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3112 return 0;
3113
5989a64e 3114 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3115
168c9250
SM
3116 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3117 section, types_list,
3118 types_list_elements);
8b70b953 3119 }
9291a0cd 3120
3063847f 3121 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3122
5989a64e
SM
3123 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3124 dwarf2_per_objfile->per_bfd->using_index = 1;
3125 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3126 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3127
3128 return 1;
3129}
3130
dee91e82 3131/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3132
dee91e82
DE
3133static void
3134dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3135 const gdb_byte *info_ptr,
3e225074 3136 struct die_info *comp_unit_die)
9291a0cd 3137{
dee91e82 3138 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3139 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3140 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3141 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3142 struct attribute *attr;
7b9f3c50
DE
3143 void **slot;
3144 struct quick_file_names *qfn;
9291a0cd 3145
0186c6a7
DE
3146 gdb_assert (! this_cu->is_debug_types);
3147
07261596
TT
3148 /* Our callers never want to match partial units -- instead they
3149 will match the enclosing full CU. */
3150 if (comp_unit_die->tag == DW_TAG_partial_unit)
3151 {
3152 this_cu->v.quick->no_file_data = 1;
3153 return;
3154 }
3155
0186c6a7 3156 lh_cu = this_cu;
7b9f3c50 3157 slot = NULL;
dee91e82 3158
fff8551c 3159 line_header_up lh;
9c541725 3160 sect_offset line_offset {};
fff8551c 3161
dee91e82 3162 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3163 if (attr != nullptr)
9291a0cd 3164 {
7b9f3c50
DE
3165 struct quick_file_names find_entry;
3166
9c541725 3167 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3168
3169 /* We may have already read in this line header (TU line header sharing).
3170 If we have we're done. */
094b34ac 3171 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3172 find_entry.hash.line_sect_off = line_offset;
5989a64e 3173 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3174 &find_entry, INSERT);
3175 if (*slot != NULL)
3176 {
9a3c8263 3177 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3178 return;
7b9f3c50
DE
3179 }
3180
3019eac3 3181 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3182 }
3183 if (lh == NULL)
3184 {
094b34ac 3185 lh_cu->v.quick->no_file_data = 1;
dee91e82 3186 return;
9291a0cd
TT
3187 }
3188
5989a64e 3189 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3190 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3191 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3192 gdb_assert (slot != NULL);
3193 *slot = qfn;
9291a0cd 3194
d721ba37 3195 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3196
aa391654
TT
3197 int offset = 0;
3198 if (strcmp (fnd.name, "<unknown>") != 0)
3199 ++offset;
3200
7ba99d21 3201 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3202 qfn->file_names =
5989a64e 3203 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3204 qfn->num_file_names);
aa391654
TT
3205 if (offset != 0)
3206 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3207 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3208 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3209 fnd.comp_dir).release ();
7b9f3c50 3210 qfn->real_names = NULL;
9291a0cd 3211
094b34ac 3212 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3213}
3214
3215/* A helper for the "quick" functions which attempts to read the line
3216 table for THIS_CU. */
3217
3218static struct quick_file_names *
ab432490
SM
3219dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3220 dwarf2_per_objfile *per_objfile)
dee91e82 3221{
0186c6a7
DE
3222 /* This should never be called for TUs. */
3223 gdb_assert (! this_cu->is_debug_types);
3224 /* Nor type unit groups. */
197400e8 3225 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3226
dee91e82
DE
3227 if (this_cu->v.quick->file_names != NULL)
3228 return this_cu->v.quick->file_names;
3229 /* If we know there is no line data, no point in looking again. */
3230 if (this_cu->v.quick->no_file_data)
3231 return NULL;
3232
ab432490 3233 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3234 if (!reader.dummy_p)
3e225074 3235 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3236
3237 if (this_cu->v.quick->no_file_data)
3238 return NULL;
3239 return this_cu->v.quick->file_names;
9291a0cd
TT
3240}
3241
3242/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3243 real path for a given file name from the line table. */
2fdf6df6 3244
9291a0cd 3245static const char *
45940949 3246dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3247 struct quick_file_names *qfn, int index)
9291a0cd 3248{
7b9f3c50 3249 if (qfn->real_names == NULL)
5989a64e 3250 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3251 qfn->num_file_names, const char *);
9291a0cd 3252
7b9f3c50 3253 if (qfn->real_names[index] == NULL)
14278e1f 3254 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3255
7b9f3c50 3256 return qfn->real_names[index];
9291a0cd
TT
3257}
3258
3259static struct symtab *
3260dw2_find_last_source_symtab (struct objfile *objfile)
3261{
ed2dc618
SM
3262 struct dwarf2_per_objfile *dwarf2_per_objfile
3263 = get_dwarf2_per_objfile (objfile);
5989a64e 3264 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3265 compunit_symtab *cust
3266 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3267
43f3e411
DE
3268 if (cust == NULL)
3269 return NULL;
ed2dc618 3270
43f3e411 3271 return compunit_primary_filetab (cust);
9291a0cd
TT
3272}
3273
7b9f3c50
DE
3274/* Traversal function for dw2_forget_cached_source_info. */
3275
3276static int
3277dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3278{
7b9f3c50 3279 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3280
7b9f3c50 3281 if (file_data->real_names)
9291a0cd 3282 {
7b9f3c50 3283 int i;
9291a0cd 3284
7b9f3c50 3285 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3286 {
7b9f3c50
DE
3287 xfree ((void*) file_data->real_names[i]);
3288 file_data->real_names[i] = NULL;
9291a0cd
TT
3289 }
3290 }
7b9f3c50
DE
3291
3292 return 1;
3293}
3294
3295static void
3296dw2_forget_cached_source_info (struct objfile *objfile)
3297{
ed2dc618
SM
3298 struct dwarf2_per_objfile *dwarf2_per_objfile
3299 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3300
5989a64e 3301 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3302 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3303}
3304
f8eba3c6
TT
3305/* Helper function for dw2_map_symtabs_matching_filename that expands
3306 the symtabs and calls the iterator. */
3307
3308static int
3309dw2_map_expand_apply (struct objfile *objfile,
3310 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3311 const char *name, const char *real_path,
14bc53a8 3312 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3313{
43f3e411 3314 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3315
3316 /* Don't visit already-expanded CUs. */
af758d11
SM
3317 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3318 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3319 return 0;
3320
3321 /* This may expand more than one symtab, and we want to iterate over
3322 all of them. */
97a1449a 3323 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3324
14bc53a8
PA
3325 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3326 last_made, callback);
f8eba3c6
TT
3327}
3328
3329/* Implementation of the map_symtabs_matching_filename method. */
3330
14bc53a8
PA
3331static bool
3332dw2_map_symtabs_matching_filename
3333 (struct objfile *objfile, const char *name, const char *real_path,
3334 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3335{
c011a4f4 3336 const char *name_basename = lbasename (name);
ed2dc618
SM
3337 struct dwarf2_per_objfile *dwarf2_per_objfile
3338 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3339
848e3e78
DE
3340 /* The rule is CUs specify all the files, including those used by
3341 any TU, so there's no need to scan TUs here. */
f4dc4d17 3342
5989a64e 3343 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3344 {
3d7bb9d9 3345 /* We only need to look at symtabs not already expanded. */
af758d11 3346 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3347 continue;
3348
ab432490
SM
3349 quick_file_names *file_data
3350 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3351 if (file_data == NULL)
9291a0cd
TT
3352 continue;
3353
b76e467d 3354 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3355 {
7b9f3c50 3356 const char *this_name = file_data->file_names[j];
da235a7c 3357 const char *this_real_name;
9291a0cd 3358
af529f8f 3359 if (compare_filenames_for_search (this_name, name))
9291a0cd 3360 {
f5b95b50 3361 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3362 callback))
3363 return true;
288e77a7 3364 continue;
4aac40c8 3365 }
9291a0cd 3366
c011a4f4
DE
3367 /* Before we invoke realpath, which can get expensive when many
3368 files are involved, do a quick comparison of the basenames. */
3369 if (! basenames_may_differ
3370 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3371 continue;
3372
45940949
TT
3373 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3374 file_data, j);
da235a7c 3375 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3376 {
da235a7c 3377 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3378 callback))
3379 return true;
288e77a7 3380 continue;
da235a7c 3381 }
9291a0cd 3382
da235a7c
JK
3383 if (real_path != NULL)
3384 {
af529f8f
JK
3385 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3386 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3387 if (this_real_name != NULL
af529f8f 3388 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3389 {
f5b95b50 3390 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3391 callback))
3392 return true;
288e77a7 3393 continue;
9291a0cd
TT
3394 }
3395 }
3396 }
3397 }
3398
14bc53a8 3399 return false;
9291a0cd
TT
3400}
3401
da51c347
DE
3402/* Struct used to manage iterating over all CUs looking for a symbol. */
3403
3404struct dw2_symtab_iterator
9291a0cd 3405{
ed2dc618
SM
3406 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3407 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3408 /* If set, only look for symbols that match that block. Valid values are
3409 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3410 gdb::optional<block_enum> block_index;
da51c347
DE
3411 /* The kind of symbol we're looking for. */
3412 domain_enum domain;
3413 /* The list of CUs from the index entry of the symbol,
3414 or NULL if not found. */
3415 offset_type *vec;
3416 /* The next element in VEC to look at. */
3417 int next;
3418 /* The number of elements in VEC, or zero if there is no match. */
3419 int length;
8943b874
DE
3420 /* Have we seen a global version of the symbol?
3421 If so we can ignore all further global instances.
3422 This is to work around gold/15646, inefficient gold-generated
3423 indices. */
3424 int global_seen;
da51c347 3425};
9291a0cd 3426
2b79f376 3427/* Initialize the index symtab iterator ITER. */
2fdf6df6 3428
9291a0cd 3429static void
da51c347 3430dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3431 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3432 gdb::optional<block_enum> block_index,
da51c347
DE
3433 domain_enum domain,
3434 const char *name)
3435{
ed2dc618 3436 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3437 iter->block_index = block_index;
3438 iter->domain = domain;
3439 iter->next = 0;
8943b874 3440 iter->global_seen = 0;
da51c347 3441
5989a64e 3442 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3443
3444 /* index is NULL if OBJF_READNOW. */
3445 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3446 iter->length = MAYBE_SWAP (*iter->vec);
3447 else
3448 {
3449 iter->vec = NULL;
3450 iter->length = 0;
3451 }
3452}
3453
3454/* Return the next matching CU or NULL if there are no more. */
3455
3456static struct dwarf2_per_cu_data *
3457dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3458{
ed2dc618
SM
3459 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3460
da51c347
DE
3461 for ( ; iter->next < iter->length; ++iter->next)
3462 {
3463 offset_type cu_index_and_attrs =
3464 MAYBE_SWAP (iter->vec[iter->next + 1]);
3465 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3466 gdb_index_symbol_kind symbol_kind =
3467 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3468 /* Only check the symbol attributes if they're present.
3469 Indices prior to version 7 don't record them,
3470 and indices >= 7 may elide them for certain symbols
3471 (gold does this). */
3472 int attrs_valid =
5989a64e 3473 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3474 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3475
3190f0c6 3476 /* Don't crash on bad data. */
5989a64e
SM
3477 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3478 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3479 {
b98664d3 3480 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3481 " [in module %s]"),
3482 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3483 continue;
3484 }
3485
5989a64e 3486 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3487
da51c347 3488 /* Skip if already read in. */
af758d11 3489 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3490 continue;
3491
8943b874
DE
3492 /* Check static vs global. */
3493 if (attrs_valid)
3494 {
2b79f376
SM
3495 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3496
3497 if (iter->block_index.has_value ())
3498 {
3499 bool want_static = *iter->block_index == STATIC_BLOCK;
3500
3501 if (is_static != want_static)
3502 continue;
3503 }
3504
8943b874
DE
3505 /* Work around gold/15646. */
3506 if (!is_static && iter->global_seen)
3507 continue;
3508 if (!is_static)
3509 iter->global_seen = 1;
3510 }
da51c347
DE
3511
3512 /* Only check the symbol's kind if it has one. */
3513 if (attrs_valid)
3514 {
3515 switch (iter->domain)
3516 {
3517 case VAR_DOMAIN:
3518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3519 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3520 /* Some types are also in VAR_DOMAIN. */
3521 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3522 continue;
3523 break;
3524 case STRUCT_DOMAIN:
3525 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case LABEL_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3530 continue;
3531 break;
59c35742
AB
3532 case MODULE_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
da51c347
DE
3536 default:
3537 break;
3538 }
3539 }
3540
3541 ++iter->next;
3542 return per_cu;
3543 }
3544
3545 return NULL;
3546}
3547
43f3e411 3548static struct compunit_symtab *
c7f839cb 3549dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3550 const char *name, domain_enum domain)
9291a0cd 3551{
43f3e411 3552 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3553 struct dwarf2_per_objfile *dwarf2_per_objfile
3554 = get_dwarf2_per_objfile (objfile);
9291a0cd 3555
b5ec771e
PA
3556 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3557
ed2dc618
SM
3558 struct dw2_symtab_iterator iter;
3559 struct dwarf2_per_cu_data *per_cu;
da51c347 3560
2b79f376 3561 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3562
ed2dc618
SM
3563 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3564 {
3565 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3566 struct compunit_symtab *stab
3567 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3568 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3569 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3570
ed2dc618
SM
3571 sym = block_find_symbol (block, name, domain,
3572 block_find_non_opaque_type_preferred,
3573 &with_opaque);
b2e2f908 3574
ed2dc618
SM
3575 /* Some caution must be observed with overloaded functions
3576 and methods, since the index will not contain any overload
3577 information (but NAME might contain it). */
da51c347 3578
ed2dc618
SM
3579 if (sym != NULL
3580 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3581 return stab;
3582 if (with_opaque != NULL
3583 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3584 stab_best = stab;
da51c347 3585
ed2dc618 3586 /* Keep looking through other CUs. */
9291a0cd 3587 }
9291a0cd 3588
da51c347 3589 return stab_best;
9291a0cd
TT
3590}
3591
3592static void
3593dw2_print_stats (struct objfile *objfile)
3594{
ed2dc618
SM
3595 struct dwarf2_per_objfile *dwarf2_per_objfile
3596 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3597 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3598 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3599 int count = 0;
9291a0cd 3600
ed2dc618 3601 for (int i = 0; i < total; ++i)
9291a0cd 3602 {
5989a64e 3603 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3604
af758d11 3605 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3606 ++count;
3607 }
e4a48d9d 3608 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3609 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3610}
3611
779bd270
DE
3612/* This dumps minimal information about the index.
3613 It is called via "mt print objfiles".
3614 One use is to verify .gdb_index has been loaded by the
3615 gdb.dwarf2/gdb-index.exp testcase. */
3616
9291a0cd
TT
3617static void
3618dw2_dump (struct objfile *objfile)
3619{
ed2dc618
SM
3620 struct dwarf2_per_objfile *dwarf2_per_objfile
3621 = get_dwarf2_per_objfile (objfile);
3622
5989a64e 3623 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3624 printf_filtered (".gdb_index:");
5989a64e 3625 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3626 {
3627 printf_filtered (" version %d\n",
5989a64e 3628 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3629 }
3630 else
3631 printf_filtered (" faked for \"readnow\"\n");
3632 printf_filtered ("\n");
9291a0cd
TT
3633}
3634
9291a0cd
TT
3635static void
3636dw2_expand_symtabs_for_function (struct objfile *objfile,
3637 const char *func_name)
3638{
ed2dc618
SM
3639 struct dwarf2_per_objfile *dwarf2_per_objfile
3640 = get_dwarf2_per_objfile (objfile);
da51c347 3641
ed2dc618
SM
3642 struct dw2_symtab_iterator iter;
3643 struct dwarf2_per_cu_data *per_cu;
da51c347 3644
2b79f376 3645 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3646
ed2dc618 3647 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3648 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3649
9291a0cd
TT
3650}
3651
3652static void
3653dw2_expand_all_symtabs (struct objfile *objfile)
3654{
ed2dc618
SM
3655 struct dwarf2_per_objfile *dwarf2_per_objfile
3656 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3657 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3658 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3659
ed2dc618 3660 for (int i = 0; i < total_units; ++i)
9291a0cd 3661 {
5989a64e 3662 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3663
58f0c718
TT
3664 /* We don't want to directly expand a partial CU, because if we
3665 read it with the wrong language, then assertion failures can
3666 be triggered later on. See PR symtab/23010. So, tell
3667 dw2_instantiate_symtab to skip partial CUs -- any important
3668 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3669 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3670 }
3671}
3672
3673static void
652a8996
JK
3674dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3675 const char *fullname)
9291a0cd 3676{
ed2dc618
SM
3677 struct dwarf2_per_objfile *dwarf2_per_objfile
3678 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3679
3680 /* We don't need to consider type units here.
3681 This is only called for examining code, e.g. expand_line_sal.
3682 There can be an order of magnitude (or more) more type units
3683 than comp units, and we avoid them if we can. */
3684
5989a64e 3685 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3686 {
3d7bb9d9 3687 /* We only need to look at symtabs not already expanded. */
af758d11 3688 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3689 continue;
3690
ab432490
SM
3691 quick_file_names *file_data
3692 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3693 if (file_data == NULL)
9291a0cd
TT
3694 continue;
3695
b76e467d 3696 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3697 {
652a8996
JK
3698 const char *this_fullname = file_data->file_names[j];
3699
3700 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3701 {
97a1449a 3702 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3703 break;
3704 }
3705 }
3706 }
3707}
3708
9a0bacfb
TV
3709static void
3710dw2_expand_symtabs_matching_symbol
3711 (mapped_index_base &index,
3712 const lookup_name_info &lookup_name_in,
3713 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3714 enum search_domain kind,
3715 gdb::function_view<bool (offset_type)> match_callback);
3716
3717static void
3718dw2_expand_symtabs_matching_one
97a1449a
SM
3719 (dwarf2_per_cu_data *per_cu,
3720 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3721 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3722 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3723
9291a0cd 3724static void
199b4314
TT
3725dw2_map_matching_symbols
3726 (struct objfile *objfile,
b054970d 3727 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3728 int global,
3729 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3730 symbol_compare_ftype *ordered_compare)
9291a0cd 3731{
1aa98955
TV
3732 /* Used for Ada. */
3733 struct dwarf2_per_objfile *dwarf2_per_objfile
3734 = get_dwarf2_per_objfile (objfile);
3735
9a0bacfb
TV
3736 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3737
5989a64e 3738 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3739 {
3740 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3741 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3742 using GNU index. */
5989a64e 3743 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3744
9a0bacfb
TV
3745 const char *match_name = name.ada ().lookup_name ().c_str ();
3746 auto matcher = [&] (const char *symname)
3747 {
3748 if (ordered_compare == nullptr)
3749 return true;
3750 return ordered_compare (symname, match_name) == 0;
3751 };
3752
3753 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3754 [&] (offset_type namei)
3755 {
3756 struct dw2_symtab_iterator iter;
3757 struct dwarf2_per_cu_data *per_cu;
3758
3759 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3760 match_name);
3761 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3762 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3763 nullptr);
9a0bacfb
TV
3764 return true;
3765 });
3766 }
3767 else
3768 {
3769 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3770 proceed assuming all symtabs have been read in. */
3771 }
1aa98955
TV
3772
3773 for (compunit_symtab *cust : objfile->compunits ())
3774 {
3775 const struct block *block;
3776
3777 if (cust == NULL)
3778 continue;
3779 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3780 if (!iterate_over_symbols_terminated (block, name,
3781 domain, callback))
3782 return;
3783 }
9291a0cd
TT
3784}
3785
e1ef7d7a
PA
3786/* Starting from a search name, return the string that finds the upper
3787 bound of all strings that start with SEARCH_NAME in a sorted name
3788 list. Returns the empty string to indicate that the upper bound is
3789 the end of the list. */
3790
3791static std::string
3792make_sort_after_prefix_name (const char *search_name)
3793{
3794 /* When looking to complete "func", we find the upper bound of all
3795 symbols that start with "func" by looking for where we'd insert
3796 the closest string that would follow "func" in lexicographical
3797 order. Usually, that's "func"-with-last-character-incremented,
3798 i.e. "fund". Mind non-ASCII characters, though. Usually those
3799 will be UTF-8 multi-byte sequences, but we can't be certain.
3800 Especially mind the 0xff character, which is a valid character in
3801 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3802 rule out compilers allowing it in identifiers. Note that
3803 conveniently, strcmp/strcasecmp are specified to compare
3804 characters interpreted as unsigned char. So what we do is treat
3805 the whole string as a base 256 number composed of a sequence of
3806 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3807 to 0, and carries 1 to the following more-significant position.
3808 If the very first character in SEARCH_NAME ends up incremented
3809 and carries/overflows, then the upper bound is the end of the
3810 list. The string after the empty string is also the empty
3811 string.
3812
3813 Some examples of this operation:
3814
3815 SEARCH_NAME => "+1" RESULT
3816
3817 "abc" => "abd"
3818 "ab\xff" => "ac"
3819 "\xff" "a" "\xff" => "\xff" "b"
3820 "\xff" => ""
3821 "\xff\xff" => ""
3822 "" => ""
3823
3824 Then, with these symbols for example:
3825
3826 func
3827 func1
3828 fund
3829
3830 completing "func" looks for symbols between "func" and
3831 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3832 which finds "func" and "func1", but not "fund".
3833
3834 And with:
3835
3836 funcÿ (Latin1 'ÿ' [0xff])
3837 funcÿ1
3838 fund
3839
3840 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3841 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3842
3843 And with:
3844
3845 ÿÿ (Latin1 'ÿ' [0xff])
3846 ÿÿ1
3847
3848 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3849 the end of the list.
3850 */
3851 std::string after = search_name;
3852 while (!after.empty () && (unsigned char) after.back () == 0xff)
3853 after.pop_back ();
3854 if (!after.empty ())
3855 after.back () = (unsigned char) after.back () + 1;
3856 return after;
3857}
3858
5c58de74 3859/* See declaration. */
61d96d7e 3860
5c58de74
PA
3861std::pair<std::vector<name_component>::const_iterator,
3862 std::vector<name_component>::const_iterator>
44ed8f3e 3863mapped_index_base::find_name_components_bounds
3b00ef10 3864 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3865{
5c58de74
PA
3866 auto *name_cmp
3867 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3868
3b00ef10 3869 const char *lang_name
e0802d59 3870 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3871
3f563c84
PA
3872 /* Comparison function object for lower_bound that matches against a
3873 given symbol name. */
3874 auto lookup_compare_lower = [&] (const name_component &elem,
3875 const char *name)
3876 {
5c58de74 3877 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3878 const char *elem_name = elem_qualified + elem.name_offset;
3879 return name_cmp (elem_name, name) < 0;
3880 };
3881
3882 /* Comparison function object for upper_bound that matches against a
3883 given symbol name. */
3884 auto lookup_compare_upper = [&] (const char *name,
3885 const name_component &elem)
3886 {
5c58de74 3887 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3888 const char *elem_name = elem_qualified + elem.name_offset;
3889 return name_cmp (name, elem_name) < 0;
3890 };
3891
5c58de74
PA
3892 auto begin = this->name_components.begin ();
3893 auto end = this->name_components.end ();
3f563c84
PA
3894
3895 /* Find the lower bound. */
3896 auto lower = [&] ()
3897 {
3b00ef10 3898 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3899 return begin;
3900 else
3b00ef10 3901 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3902 } ();
3903
3904 /* Find the upper bound. */
3905 auto upper = [&] ()
3906 {
5c58de74 3907 if (lookup_name_without_params.completion_mode ())
3f563c84 3908 {
e1ef7d7a
PA
3909 /* In completion mode, we want UPPER to point past all
3910 symbols names that have the same prefix. I.e., with
3911 these symbols, and completing "func":
3912
3913 function << lower bound
3914 function1
3915 other_function << upper bound
3916
3917 We find the upper bound by looking for the insertion
3918 point of "func"-with-last-character-incremented,
3919 i.e. "fund". */
3b00ef10 3920 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3921 if (after.empty ())
3f563c84 3922 return end;
e6b2f5ef
PA
3923 return std::lower_bound (lower, end, after.c_str (),
3924 lookup_compare_lower);
3f563c84
PA
3925 }
3926 else
3b00ef10 3927 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3928 } ();
3929
5c58de74
PA
3930 return {lower, upper};
3931}
3932
3933/* See declaration. */
3934
3935void
44ed8f3e 3936mapped_index_base::build_name_components ()
5c58de74
PA
3937{
3938 if (!this->name_components.empty ())
3939 return;
3940
3941 this->name_components_casing = case_sensitivity;
3942 auto *name_cmp
3943 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3944
3945 /* The code below only knows how to break apart components of C++
3946 symbol names (and other languages that use '::' as
3b00ef10 3947 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3948 auto count = this->symbol_name_count ();
3949 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3950 {
44ed8f3e 3951 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3952 continue;
3953
3954 const char *name = this->symbol_name_at (idx);
3955
3956 /* Add each name component to the name component table. */
3957 unsigned int previous_len = 0;
3b00ef10
TT
3958
3959 if (strstr (name, "::") != nullptr)
3960 {
3961 for (unsigned int current_len = cp_find_first_component (name);
3962 name[current_len] != '\0';
3963 current_len += cp_find_first_component (name + current_len))
3964 {
3965 gdb_assert (name[current_len] == ':');
3966 this->name_components.push_back ({previous_len, idx});
3967 /* Skip the '::'. */
3968 current_len += 2;
3969 previous_len = current_len;
3970 }
3971 }
3972 else
5c58de74 3973 {
3b00ef10
TT
3974 /* Handle the Ada encoded (aka mangled) form here. */
3975 for (const char *iter = strstr (name, "__");
3976 iter != nullptr;
3977 iter = strstr (iter, "__"))
3978 {
3979 this->name_components.push_back ({previous_len, idx});
3980 iter += 2;
3981 previous_len = iter - name;
3982 }
5c58de74 3983 }
3b00ef10 3984
5c58de74
PA
3985 this->name_components.push_back ({previous_len, idx});
3986 }
3987
3988 /* Sort name_components elements by name. */
3989 auto name_comp_compare = [&] (const name_component &left,
3990 const name_component &right)
3991 {
3992 const char *left_qualified = this->symbol_name_at (left.idx);
3993 const char *right_qualified = this->symbol_name_at (right.idx);
3994
3995 const char *left_name = left_qualified + left.name_offset;
3996 const char *right_name = right_qualified + right.name_offset;
3997
3998 return name_cmp (left_name, right_name) < 0;
3999 };
4000
4001 std::sort (this->name_components.begin (),
4002 this->name_components.end (),
4003 name_comp_compare);
4004}
4005
4006/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4007 mapped_index_base instead of the containing objfile. This is split
4008 to a separate function in order to be able to unit test the
4009 name_components matching using a mock mapped_index_base. For each
5c58de74 4010 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4011 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4012
4013static void
4014dw2_expand_symtabs_matching_symbol
44ed8f3e 4015 (mapped_index_base &index,
5c58de74
PA
4016 const lookup_name_info &lookup_name_in,
4017 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4018 enum search_domain kind,
3b00ef10 4019 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4020{
4021 lookup_name_info lookup_name_without_params
4022 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4023
4024 /* Build the symbol name component sorted vector, if we haven't
4025 yet. */
4026 index.build_name_components ();
4027
3f563c84
PA
4028 /* The same symbol may appear more than once in the range though.
4029 E.g., if we're looking for symbols that complete "w", and we have
4030 a symbol named "w1::w2", we'll find the two name components for
4031 that same symbol in the range. To be sure we only call the
4032 callback once per symbol, we first collect the symbol name
4033 indexes that matched in a temporary vector and ignore
4034 duplicates. */
4035 std::vector<offset_type> matches;
3f563c84 4036
3b00ef10
TT
4037 struct name_and_matcher
4038 {
4039 symbol_name_matcher_ftype *matcher;
ecc6c606 4040 const char *name;
3b00ef10
TT
4041
4042 bool operator== (const name_and_matcher &other) const
3f563c84 4043 {
ecc6c606 4044 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4045 }
4046 };
4047
4048 /* A vector holding all the different symbol name matchers, for all
4049 languages. */
4050 std::vector<name_and_matcher> matchers;
4051
4052 for (int i = 0; i < nr_languages; i++)
4053 {
4054 enum language lang_e = (enum language) i;
4055
4056 const language_defn *lang = language_def (lang_e);
4057 symbol_name_matcher_ftype *name_matcher
4058 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4059
3b00ef10
TT
4060 name_and_matcher key {
4061 name_matcher,
4062 lookup_name_without_params.language_lookup_name (lang_e)
4063 };
4064
4065 /* Don't insert the same comparison routine more than once.
4066 Note that we do this linear walk. This is not a problem in
4067 practice because the number of supported languages is
4068 low. */
4069 if (std::find (matchers.begin (), matchers.end (), key)
4070 != matchers.end ())
9291a0cd 4071 continue;
3b00ef10
TT
4072 matchers.push_back (std::move (key));
4073
4074 auto bounds
4075 = index.find_name_components_bounds (lookup_name_without_params,
4076 lang_e);
4077
4078 /* Now for each symbol name in range, check to see if we have a name
4079 match, and if so, call the MATCH_CALLBACK callback. */
4080
4081 for (; bounds.first != bounds.second; ++bounds.first)
4082 {
4083 const char *qualified = index.symbol_name_at (bounds.first->idx);
4084
4085 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4086 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4087 continue;
9291a0cd 4088
3b00ef10
TT
4089 matches.push_back (bounds.first->idx);
4090 }
3f563c84
PA
4091 }
4092
4093 std::sort (matches.begin (), matches.end ());
4094
4095 /* Finally call the callback, once per match. */
4096 ULONGEST prev = -1;
4097 for (offset_type idx : matches)
4098 {
4099 if (prev != idx)
4100 {
3b00ef10
TT
4101 if (!match_callback (idx))
4102 break;
3f563c84
PA
4103 prev = idx;
4104 }
4105 }
4106
4107 /* Above we use a type wider than idx's for 'prev', since 0 and
4108 (offset_type)-1 are both possible values. */
4109 static_assert (sizeof (prev) > sizeof (offset_type), "");
4110}
4111
c62446b1
PA
4112#if GDB_SELF_TEST
4113
4114namespace selftests { namespace dw2_expand_symtabs_matching {
4115
a3c5fafd
PA
4116/* A mock .gdb_index/.debug_names-like name index table, enough to
4117 exercise dw2_expand_symtabs_matching_symbol, which works with the
4118 mapped_index_base interface. Builds an index from the symbol list
4119 passed as parameter to the constructor. */
4120class mock_mapped_index : public mapped_index_base
c62446b1
PA
4121{
4122public:
a3c5fafd
PA
4123 mock_mapped_index (gdb::array_view<const char *> symbols)
4124 : m_symbol_table (symbols)
c62446b1
PA
4125 {}
4126
a3c5fafd 4127 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4128
a3c5fafd 4129 /* Return the number of names in the symbol table. */
632e107b 4130 size_t symbol_name_count () const override
c62446b1 4131 {
a3c5fafd 4132 return m_symbol_table.size ();
c62446b1
PA
4133 }
4134
a3c5fafd 4135 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4136 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4137 {
4138 return m_symbol_table[idx];
4139 }
c62446b1 4140
a3c5fafd
PA
4141private:
4142 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4143};
4144
4145/* Convenience function that converts a NULL pointer to a "<null>"
4146 string, to pass to print routines. */
4147
4148static const char *
4149string_or_null (const char *str)
4150{
4151 return str != NULL ? str : "<null>";
4152}
4153
4154/* Check if a lookup_name_info built from
4155 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4156 index. EXPECTED_LIST is the list of expected matches, in expected
4157 matching order. If no match expected, then an empty list is
4158 specified. Returns true on success. On failure prints a warning
4159 indicating the file:line that failed, and returns false. */
4160
4161static bool
4162check_match (const char *file, int line,
4163 mock_mapped_index &mock_index,
4164 const char *name, symbol_name_match_type match_type,
4165 bool completion_mode,
4166 std::initializer_list<const char *> expected_list)
4167{
4168 lookup_name_info lookup_name (name, match_type, completion_mode);
4169
4170 bool matched = true;
4171
4172 auto mismatch = [&] (const char *expected_str,
4173 const char *got)
4174 {
4175 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4176 "expected=\"%s\", got=\"%s\"\n"),
4177 file, line,
4178 (match_type == symbol_name_match_type::FULL
4179 ? "FULL" : "WILD"),
4180 name, string_or_null (expected_str), string_or_null (got));
4181 matched = false;
4182 };
4183
4184 auto expected_it = expected_list.begin ();
4185 auto expected_end = expected_list.end ();
4186
a3c5fafd 4187 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4188 NULL, ALL_DOMAIN,
4189 [&] (offset_type idx)
4190 {
a3c5fafd 4191 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4192 const char *expected_str
4193 = expected_it == expected_end ? NULL : *expected_it++;
4194
4195 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4196 mismatch (expected_str, matched_name);
3b00ef10 4197 return true;
c62446b1
PA
4198 });
4199
4200 const char *expected_str
4201 = expected_it == expected_end ? NULL : *expected_it++;
4202 if (expected_str != NULL)
4203 mismatch (expected_str, NULL);
4204
4205 return matched;
4206}
4207
4208/* The symbols added to the mock mapped_index for testing (in
4209 canonical form). */
4210static const char *test_symbols[] = {
4211 "function",
4212 "std::bar",
4213 "std::zfunction",
4214 "std::zfunction2",
4215 "w1::w2",
4216 "ns::foo<char*>",
4217 "ns::foo<int>",
4218 "ns::foo<long>",
a20714ff
PA
4219 "ns2::tmpl<int>::foo2",
4220 "(anonymous namespace)::A::B::C",
c62446b1 4221
e1ef7d7a
PA
4222 /* These are used to check that the increment-last-char in the
4223 matching algorithm for completion doesn't match "t1_fund" when
4224 completing "t1_func". */
4225 "t1_func",
4226 "t1_func1",
4227 "t1_fund",
4228 "t1_fund1",
4229
4230 /* A UTF-8 name with multi-byte sequences to make sure that
4231 cp-name-parser understands this as a single identifier ("função"
4232 is "function" in PT). */
4233 u8"u8função",
4234
4235 /* \377 (0xff) is Latin1 'ÿ'. */
4236 "yfunc\377",
4237
4238 /* \377 (0xff) is Latin1 'ÿ'. */
4239 "\377",
4240 "\377\377123",
4241
c62446b1
PA
4242 /* A name with all sorts of complications. Starts with "z" to make
4243 it easier for the completion tests below. */
4244#define Z_SYM_NAME \
4245 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4246 "::tuple<(anonymous namespace)::ui*, " \
4247 "std::default_delete<(anonymous namespace)::ui>, void>"
4248
4249 Z_SYM_NAME
4250};
4251
a3c5fafd
PA
4252/* Returns true if the mapped_index_base::find_name_component_bounds
4253 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4254 in completion mode. */
5c58de74
PA
4255
4256static bool
a3c5fafd 4257check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4258 const char *search_name,
4259 gdb::array_view<const char *> expected_syms)
4260{
4261 lookup_name_info lookup_name (search_name,
4262 symbol_name_match_type::FULL, true);
4263
3b00ef10
TT
4264 auto bounds = index.find_name_components_bounds (lookup_name,
4265 language_cplus);
5c58de74
PA
4266
4267 size_t distance = std::distance (bounds.first, bounds.second);
4268 if (distance != expected_syms.size ())
4269 return false;
4270
4271 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4272 {
4273 auto nc_elem = bounds.first + exp_elem;
4274 const char *qualified = index.symbol_name_at (nc_elem->idx);
4275 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4276 return false;
4277 }
4278
4279 return true;
4280}
4281
4282/* Test the lower-level mapped_index::find_name_component_bounds
4283 method. */
4284
c62446b1 4285static void
5c58de74
PA
4286test_mapped_index_find_name_component_bounds ()
4287{
4288 mock_mapped_index mock_index (test_symbols);
4289
a3c5fafd 4290 mock_index.build_name_components ();
5c58de74
PA
4291
4292 /* Test the lower-level mapped_index::find_name_component_bounds
4293 method in completion mode. */
4294 {
4295 static const char *expected_syms[] = {
4296 "t1_func",
4297 "t1_func1",
5c58de74
PA
4298 };
4299
a3c5fafd 4300 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4301 "t1_func", expected_syms));
4302 }
4303
4304 /* Check that the increment-last-char in the name matching algorithm
4305 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4306 {
4307 static const char *expected_syms1[] = {
4308 "\377",
4309 "\377\377123",
4310 };
a3c5fafd 4311 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4312 "\377", expected_syms1));
4313
4314 static const char *expected_syms2[] = {
4315 "\377\377123",
4316 };
a3c5fafd 4317 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4318 "\377\377", expected_syms2));
4319 }
4320}
4321
4322/* Test dw2_expand_symtabs_matching_symbol. */
4323
4324static void
4325test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4326{
4327 mock_mapped_index mock_index (test_symbols);
4328
4329 /* We let all tests run until the end even if some fails, for debug
4330 convenience. */
4331 bool any_mismatch = false;
4332
4333 /* Create the expected symbols list (an initializer_list). Needed
4334 because lists have commas, and we need to pass them to CHECK,
4335 which is a macro. */
4336#define EXPECT(...) { __VA_ARGS__ }
4337
4338 /* Wrapper for check_match that passes down the current
4339 __FILE__/__LINE__. */
4340#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4341 any_mismatch |= !check_match (__FILE__, __LINE__, \
4342 mock_index, \
4343 NAME, MATCH_TYPE, COMPLETION_MODE, \
4344 EXPECTED_LIST)
4345
4346 /* Identity checks. */
4347 for (const char *sym : test_symbols)
4348 {
4349 /* Should be able to match all existing symbols. */
4350 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4351 EXPECT (sym));
4352
4353 /* Should be able to match all existing symbols with
4354 parameters. */
4355 std::string with_params = std::string (sym) + "(int)";
4356 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4357 EXPECT (sym));
4358
4359 /* Should be able to match all existing symbols with
4360 parameters and qualifiers. */
4361 with_params = std::string (sym) + " ( int ) const";
4362 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4363 EXPECT (sym));
4364
4365 /* This should really find sym, but cp-name-parser.y doesn't
4366 know about lvalue/rvalue qualifiers yet. */
4367 with_params = std::string (sym) + " ( int ) &&";
4368 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4369 {});
4370 }
4371
e1ef7d7a
PA
4372 /* Check that the name matching algorithm for completion doesn't get
4373 confused with Latin1 'ÿ' / 0xff. */
4374 {
4375 static const char str[] = "\377";
4376 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4377 EXPECT ("\377", "\377\377123"));
4378 }
4379
4380 /* Check that the increment-last-char in the matching algorithm for
4381 completion doesn't match "t1_fund" when completing "t1_func". */
4382 {
4383 static const char str[] = "t1_func";
4384 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4385 EXPECT ("t1_func", "t1_func1"));
4386 }
4387
c62446b1
PA
4388 /* Check that completion mode works at each prefix of the expected
4389 symbol name. */
4390 {
4391 static const char str[] = "function(int)";
4392 size_t len = strlen (str);
4393 std::string lookup;
4394
4395 for (size_t i = 1; i < len; i++)
4396 {
4397 lookup.assign (str, i);
4398 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4399 EXPECT ("function"));
4400 }
4401 }
4402
4403 /* While "w" is a prefix of both components, the match function
4404 should still only be called once. */
4405 {
4406 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4407 EXPECT ("w1::w2"));
a20714ff
PA
4408 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4409 EXPECT ("w1::w2"));
c62446b1
PA
4410 }
4411
4412 /* Same, with a "complicated" symbol. */
4413 {
4414 static const char str[] = Z_SYM_NAME;
4415 size_t len = strlen (str);
4416 std::string lookup;
4417
4418 for (size_t i = 1; i < len; i++)
4419 {
4420 lookup.assign (str, i);
4421 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4422 EXPECT (Z_SYM_NAME));
4423 }
4424 }
4425
4426 /* In FULL mode, an incomplete symbol doesn't match. */
4427 {
4428 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4429 {});
4430 }
4431
4432 /* A complete symbol with parameters matches any overload, since the
4433 index has no overload info. */
4434 {
4435 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4436 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4437 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4438 EXPECT ("std::zfunction", "std::zfunction2"));
4439 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4441 }
4442
4443 /* Check that whitespace is ignored appropriately. A symbol with a
4444 template argument list. */
4445 {
4446 static const char expected[] = "ns::foo<int>";
4447 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4448 EXPECT (expected));
a20714ff
PA
4449 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4450 EXPECT (expected));
c62446b1
PA
4451 }
4452
4453 /* Check that whitespace is ignored appropriately. A symbol with a
4454 template argument list that includes a pointer. */
4455 {
4456 static const char expected[] = "ns::foo<char*>";
4457 /* Try both completion and non-completion modes. */
4458 static const bool completion_mode[2] = {false, true};
4459 for (size_t i = 0; i < 2; i++)
4460 {
4461 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4462 completion_mode[i], EXPECT (expected));
a20714ff
PA
4463 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4464 completion_mode[i], EXPECT (expected));
c62446b1
PA
4465
4466 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4467 completion_mode[i], EXPECT (expected));
a20714ff
PA
4468 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4469 completion_mode[i], EXPECT (expected));
c62446b1
PA
4470 }
4471 }
4472
4473 {
4474 /* Check method qualifiers are ignored. */
4475 static const char expected[] = "ns::foo<char*>";
4476 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4477 symbol_name_match_type::FULL, true, EXPECT (expected));
4478 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4479 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4480 CHECK_MATCH ("foo < char * > ( int ) const",
4481 symbol_name_match_type::WILD, true, EXPECT (expected));
4482 CHECK_MATCH ("foo < char * > ( int ) &&",
4483 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4484 }
4485
4486 /* Test lookup names that don't match anything. */
4487 {
a20714ff
PA
4488 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4489 {});
4490
c62446b1
PA
4491 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4492 {});
4493 }
4494
a20714ff
PA
4495 /* Some wild matching tests, exercising "(anonymous namespace)",
4496 which should not be confused with a parameter list. */
4497 {
4498 static const char *syms[] = {
4499 "A::B::C",
4500 "B::C",
4501 "C",
4502 "A :: B :: C ( int )",
4503 "B :: C ( int )",
4504 "C ( int )",
4505 };
4506
4507 for (const char *s : syms)
4508 {
4509 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4510 EXPECT ("(anonymous namespace)::A::B::C"));
4511 }
4512 }
4513
4514 {
4515 static const char expected[] = "ns2::tmpl<int>::foo2";
4516 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4517 EXPECT (expected));
4518 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4519 EXPECT (expected));
4520 }
4521
c62446b1
PA
4522 SELF_CHECK (!any_mismatch);
4523
4524#undef EXPECT
4525#undef CHECK_MATCH
4526}
4527
5c58de74
PA
4528static void
4529run_test ()
4530{
4531 test_mapped_index_find_name_component_bounds ();
4532 test_dw2_expand_symtabs_matching_symbol ();
4533}
4534
c62446b1
PA
4535}} // namespace selftests::dw2_expand_symtabs_matching
4536
4537#endif /* GDB_SELF_TEST */
4538
4b514bc8
JK
4539/* If FILE_MATCHER is NULL or if PER_CU has
4540 dwarf2_per_cu_quick_data::MARK set (see
4541 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4542 EXPANSION_NOTIFY on it. */
4543
4544static void
4545dw2_expand_symtabs_matching_one
97a1449a
SM
4546 (dwarf2_per_cu_data *per_cu,
4547 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4548 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4549 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4550{
4551 if (file_matcher == NULL || per_cu->v.quick->mark)
4552 {
af758d11 4553 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4554
97a1449a
SM
4555 compunit_symtab *symtab
4556 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4557 gdb_assert (symtab != nullptr);
4b514bc8 4558
af758d11
SM
4559 if (expansion_notify != NULL && symtab_was_null)
4560 expansion_notify (symtab);
4b514bc8
JK
4561 }
4562}
4563
3f563c84
PA
4564/* Helper for dw2_expand_matching symtabs. Called on each symbol
4565 matched, to expand corresponding CUs that were marked. IDX is the
4566 index of the symbol name that matched. */
4567
4568static void
4569dw2_expand_marked_cus
97a1449a 4570 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4571 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4572 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4573 search_domain kind)
4574{
3f563c84
PA
4575 offset_type *vec, vec_len, vec_idx;
4576 bool global_seen = false;
5989a64e 4577 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4578
61920122 4579 vec = (offset_type *) (index.constant_pool
f00a2de2 4580 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4581 vec_len = MAYBE_SWAP (vec[0]);
4582 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4583 {
61920122
PA
4584 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4585 /* This value is only valid for index versions >= 7. */
4586 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4587 gdb_index_symbol_kind symbol_kind =
4588 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4589 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4590 /* Only check the symbol attributes if they're present.
4591 Indices prior to version 7 don't record them,
4592 and indices >= 7 may elide them for certain symbols
4593 (gold does this). */
4594 int attrs_valid =
4595 (index.version >= 7
4596 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4597
4598 /* Work around gold/15646. */
4599 if (attrs_valid)
9291a0cd 4600 {
61920122
PA
4601 if (!is_static && global_seen)
4602 continue;
4603 if (!is_static)
4604 global_seen = true;
4605 }
3190f0c6 4606
61920122
PA
4607 /* Only check the symbol's kind if it has one. */
4608 if (attrs_valid)
4609 {
4610 switch (kind)
8943b874 4611 {
61920122
PA
4612 case VARIABLES_DOMAIN:
4613 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4614 continue;
4615 break;
4616 case FUNCTIONS_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4618 continue;
61920122
PA
4619 break;
4620 case TYPES_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4622 continue;
4623 break;
59c35742
AB
4624 case MODULES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4626 continue;
4627 break;
61920122
PA
4628 default:
4629 break;
8943b874 4630 }
61920122 4631 }
8943b874 4632
61920122 4633 /* Don't crash on bad data. */
5989a64e
SM
4634 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4635 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4636 {
b98664d3 4637 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4638 " [in module %s]"),
4639 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4640 continue;
4641 }
4642
5989a64e 4643 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4644 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4645 expansion_notify);
61920122
PA
4646 }
4647}
4648
4b514bc8
JK
4649/* If FILE_MATCHER is non-NULL, set all the
4650 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4651 that match FILE_MATCHER. */
4652
61920122 4653static void
4b514bc8 4654dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4655 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4656 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4657{
4b514bc8 4658 if (file_matcher == NULL)
61920122
PA
4659 return;
4660
4b514bc8
JK
4661 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4662 htab_eq_pointer,
4663 NULL, xcalloc, xfree));
4664 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4665 htab_eq_pointer,
4666 NULL, xcalloc, xfree));
61920122 4667
4b514bc8
JK
4668 /* The rule is CUs specify all the files, including those used by
4669 any TU, so there's no need to scan TUs here. */
61920122 4670
5989a64e 4671 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4672 {
927aa2e7
JK
4673 QUIT;
4674
4675 per_cu->v.quick->mark = 0;
4676
4677 /* We only need to look at symtabs not already expanded. */
af758d11 4678 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4679 continue;
4680
ab432490
SM
4681 quick_file_names *file_data
4682 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4683 if (file_data == NULL)
4684 continue;
4685
4686 if (htab_find (visited_not_found.get (), file_data) != NULL)
4687 continue;
4688 else if (htab_find (visited_found.get (), file_data) != NULL)
4689 {
4690 per_cu->v.quick->mark = 1;
4691 continue;
4692 }
4693
b76e467d 4694 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4695 {
4696 const char *this_real_name;
4697
4698 if (file_matcher (file_data->file_names[j], false))
4699 {
4700 per_cu->v.quick->mark = 1;
4701 break;
4702 }
4703
4704 /* Before we invoke realpath, which can get expensive when many
4705 files are involved, do a quick comparison of the basenames. */
4706 if (!basenames_may_differ
4707 && !file_matcher (lbasename (file_data->file_names[j]),
4708 true))
4709 continue;
4710
45940949
TT
4711 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4712 file_data, j);
927aa2e7
JK
4713 if (file_matcher (this_real_name, false))
4714 {
4715 per_cu->v.quick->mark = 1;
4716 break;
4717 }
4718 }
4719
b76e467d
SM
4720 void **slot = htab_find_slot (per_cu->v.quick->mark
4721 ? visited_found.get ()
4722 : visited_not_found.get (),
4723 file_data, INSERT);
927aa2e7
JK
4724 *slot = file_data;
4725 }
4726}
4727
4728static void
4729dw2_expand_symtabs_matching
4730 (struct objfile *objfile,
4731 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4732 const lookup_name_info *lookup_name,
927aa2e7
JK
4733 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4734 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4735 enum search_domain kind)
4736{
ed2dc618
SM
4737 struct dwarf2_per_objfile *dwarf2_per_objfile
4738 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4739
4740 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4741 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4742 return;
4743
ed2dc618 4744 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4745
c1a66c06
TV
4746 if (symbol_matcher == NULL && lookup_name == NULL)
4747 {
5989a64e 4748 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4749 {
4750 QUIT;
4751
97a1449a
SM
4752 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4753 file_matcher, expansion_notify);
c1a66c06
TV
4754 }
4755 return;
4756 }
4757
5989a64e 4758 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4759
c1a66c06 4760 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4761 symbol_matcher,
4762 kind, [&] (offset_type idx)
4763 {
ed2dc618 4764 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4765 expansion_notify, kind);
3b00ef10 4766 return true;
927aa2e7
JK
4767 });
4768}
4769
4770/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4771 symtab. */
4772
4773static struct compunit_symtab *
4774recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4775 CORE_ADDR pc)
4776{
4777 int i;
4778
4779 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4780 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4781 return cust;
4782
4783 if (cust->includes == NULL)
4784 return NULL;
4785
4786 for (i = 0; cust->includes[i]; ++i)
4787 {
4788 struct compunit_symtab *s = cust->includes[i];
4789
4790 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4791 if (s != NULL)
4792 return s;
4793 }
4794
4795 return NULL;
4796}
4797
4798static struct compunit_symtab *
4799dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4800 struct bound_minimal_symbol msymbol,
4801 CORE_ADDR pc,
4802 struct obj_section *section,
4803 int warn_if_readin)
4804{
4805 struct dwarf2_per_cu_data *data;
4806 struct compunit_symtab *result;
4807
d320c2b5 4808 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4809 return NULL;
4810
b3b3bada 4811 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4812 data = (struct dwarf2_per_cu_data *) addrmap_find
4813 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4814 if (!data)
4815 return NULL;
4816
af758d11
SM
4817 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4818 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4819 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4820 paddress (objfile->arch (), pc));
927aa2e7 4821
97a1449a
SM
4822 result = recursively_find_pc_sect_compunit_symtab
4823 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4824
927aa2e7
JK
4825 gdb_assert (result != NULL);
4826 return result;
4827}
4828
4829static void
4830dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4831 void *data, int need_fullname)
4832{
ed2dc618
SM
4833 struct dwarf2_per_objfile *dwarf2_per_objfile
4834 = get_dwarf2_per_objfile (objfile);
927aa2e7 4835
5989a64e 4836 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4837 {
5989a64e 4838 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4839
4840 htab_up visited (htab_create_alloc (10,
4841 htab_hash_pointer, htab_eq_pointer,
4842 NULL, xcalloc, xfree));
4843
4844 /* The rule is CUs specify all the files, including those used
4845 by any TU, so there's no need to scan TUs here. We can
4846 ignore file names coming from already-expanded CUs. */
4847
5989a64e 4848 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4849 {
af758d11 4850 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4851 {
4852 void **slot = htab_find_slot (visited.get (),
4853 per_cu->v.quick->file_names,
4854 INSERT);
4855
4856 *slot = per_cu->v.quick->file_names;
4857 }
4858 }
4859
5989a64e 4860 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4861 {
927aa2e7 4862 /* We only need to look at symtabs not already expanded. */
af758d11 4863 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4864 continue;
4865
ab432490
SM
4866 quick_file_names *file_data
4867 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4868 if (file_data == NULL)
4869 continue;
4870
b76e467d 4871 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4872 if (*slot)
4873 {
4874 /* Already visited. */
4875 continue;
4876 }
4877 *slot = file_data;
4878
4879 for (int j = 0; j < file_data->num_file_names; ++j)
4880 {
4881 const char *filename = file_data->file_names[j];
5989a64e 4882 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4883 }
4884 }
4885 }
4886
5989a64e 4887 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4888 {
4889 gdb::unique_xmalloc_ptr<char> this_real_name;
4890
4891 if (need_fullname)
4892 this_real_name = gdb_realpath (filename);
4893 (*fun) (filename, this_real_name.get (), data);
4894 });
4895}
4896
4897static int
4898dw2_has_symbols (struct objfile *objfile)
4899{
4900 return 1;
4901}
4902
4903const struct quick_symbol_functions dwarf2_gdb_index_functions =
4904{
4905 dw2_has_symbols,
4906 dw2_find_last_source_symtab,
4907 dw2_forget_cached_source_info,
4908 dw2_map_symtabs_matching_filename,
4909 dw2_lookup_symbol,
d3214198 4910 NULL,
927aa2e7
JK
4911 dw2_print_stats,
4912 dw2_dump,
927aa2e7
JK
4913 dw2_expand_symtabs_for_function,
4914 dw2_expand_all_symtabs,
4915 dw2_expand_symtabs_with_fullname,
4916 dw2_map_matching_symbols,
4917 dw2_expand_symtabs_matching,
4918 dw2_find_pc_sect_compunit_symtab,
4919 NULL,
4920 dw2_map_symbol_filenames
4921};
4922
4923/* DWARF-5 debug_names reader. */
4924
4925/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4926static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4927
4928/* A helper function that reads the .debug_names section in SECTION
4929 and fills in MAP. FILENAME is the name of the file containing the
4930 section; it is used for error reporting.
4931
4932 Returns true if all went well, false otherwise. */
4933
4934static bool
4935read_debug_names_from_section (struct objfile *objfile,
4936 const char *filename,
4937 struct dwarf2_section_info *section,
4938 mapped_debug_names &map)
4939{
96b79293 4940 if (section->empty ())
927aa2e7
JK
4941 return false;
4942
4943 /* Older elfutils strip versions could keep the section in the main
4944 executable while splitting it for the separate debug info file. */
96b79293 4945 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4946 return false;
4947
96b79293 4948 section->read (objfile);
927aa2e7 4949
08feed99 4950 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4951
4952 const gdb_byte *addr = section->buffer;
4953
96b79293 4954 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4955
4956 unsigned int bytes_read;
4957 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4958 addr += bytes_read;
4959
4960 map.dwarf5_is_dwarf64 = bytes_read != 4;
4961 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4962 if (bytes_read + length != section->size)
4963 {
4964 /* There may be multiple per-CU indices. */
4965 warning (_("Section .debug_names in %s length %s does not match "
4966 "section length %s, ignoring .debug_names."),
4967 filename, plongest (bytes_read + length),
4968 pulongest (section->size));
4969 return false;
4970 }
4971
4972 /* The version number. */
4973 uint16_t version = read_2_bytes (abfd, addr);
4974 addr += 2;
4975 if (version != 5)
4976 {
4977 warning (_("Section .debug_names in %s has unsupported version %d, "
4978 "ignoring .debug_names."),
4979 filename, version);
4980 return false;
4981 }
4982
4983 /* Padding. */
4984 uint16_t padding = read_2_bytes (abfd, addr);
4985 addr += 2;
4986 if (padding != 0)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported padding %d, "
4989 "ignoring .debug_names."),
4990 filename, padding);
4991 return false;
4992 }
4993
4994 /* comp_unit_count - The number of CUs in the CU list. */
4995 map.cu_count = read_4_bytes (abfd, addr);
4996 addr += 4;
4997
4998 /* local_type_unit_count - The number of TUs in the local TU
4999 list. */
5000 map.tu_count = read_4_bytes (abfd, addr);
5001 addr += 4;
5002
5003 /* foreign_type_unit_count - The number of TUs in the foreign TU
5004 list. */
5005 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5006 addr += 4;
5007 if (foreign_tu_count != 0)
5008 {
5009 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5010 "ignoring .debug_names."),
5011 filename, static_cast<unsigned long> (foreign_tu_count));
5012 return false;
5013 }
5014
5015 /* bucket_count - The number of hash buckets in the hash lookup
5016 table. */
5017 map.bucket_count = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* name_count - The number of unique names in the index. */
5021 map.name_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* abbrev_table_size - The size in bytes of the abbreviations
5025 table. */
5026 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5027 addr += 4;
5028
5029 /* augmentation_string_size - The size in bytes of the augmentation
5030 string. This value is rounded up to a multiple of 4. */
5031 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5032 addr += 4;
5033 map.augmentation_is_gdb = ((augmentation_string_size
5034 == sizeof (dwarf5_augmentation))
5035 && memcmp (addr, dwarf5_augmentation,
5036 sizeof (dwarf5_augmentation)) == 0);
5037 augmentation_string_size += (-augmentation_string_size) & 3;
5038 addr += augmentation_string_size;
5039
5040 /* List of CUs */
5041 map.cu_table_reordered = addr;
5042 addr += map.cu_count * map.offset_size;
5043
5044 /* List of Local TUs */
5045 map.tu_table_reordered = addr;
5046 addr += map.tu_count * map.offset_size;
5047
5048 /* Hash Lookup Table */
5049 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5050 addr += map.bucket_count * 4;
5051 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5052 addr += map.name_count * 4;
5053
5054 /* Name Table */
5055 map.name_table_string_offs_reordered = addr;
5056 addr += map.name_count * map.offset_size;
5057 map.name_table_entry_offs_reordered = addr;
5058 addr += map.name_count * map.offset_size;
5059
5060 const gdb_byte *abbrev_table_start = addr;
5061 for (;;)
5062 {
927aa2e7
JK
5063 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5064 addr += bytes_read;
5065 if (index_num == 0)
5066 break;
5067
5068 const auto insertpair
5069 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5070 if (!insertpair.second)
5071 {
5072 warning (_("Section .debug_names in %s has duplicate index %s, "
5073 "ignoring .debug_names."),
5074 filename, pulongest (index_num));
5075 return false;
5076 }
5077 mapped_debug_names::index_val &indexval = insertpair.first->second;
5078 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5079 addr += bytes_read;
5080
5081 for (;;)
5082 {
5083 mapped_debug_names::index_val::attr attr;
5084 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5085 addr += bytes_read;
5086 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5087 addr += bytes_read;
5088 if (attr.form == DW_FORM_implicit_const)
5089 {
5090 attr.implicit_const = read_signed_leb128 (abfd, addr,
5091 &bytes_read);
5092 addr += bytes_read;
5093 }
5094 if (attr.dw_idx == 0 && attr.form == 0)
5095 break;
5096 indexval.attr_vec.push_back (std::move (attr));
5097 }
5098 }
5099 if (addr != abbrev_table_start + abbrev_table_size)
5100 {
5101 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5102 "of size %s vs. written as %u, ignoring .debug_names."),
5103 filename, plongest (addr - abbrev_table_start),
5104 abbrev_table_size);
927aa2e7
JK
5105 return false;
5106 }
5107 map.entry_pool = addr;
5108
5109 return true;
5110}
5111
5112/* A helper for create_cus_from_debug_names that handles the MAP's CU
5113 list. */
5114
5115static void
168c9250 5116create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5117 const mapped_debug_names &map,
5118 dwarf2_section_info &section,
b76e467d 5119 bool is_dwz)
927aa2e7 5120{
3ee6bb11
TV
5121 if (!map.augmentation_is_gdb)
5122 {
5123 for (uint32_t i = 0; i < map.cu_count; ++i)
5124 {
5125 sect_offset sect_off
5126 = (sect_offset) (extract_unsigned_integer
5127 (map.cu_table_reordered + i * map.offset_size,
5128 map.offset_size,
5129 map.dwarf5_byte_order));
5130 /* We don't know the length of the CU, because the CU list in a
5131 .debug_names index can be incomplete, so we can't use the start of
5132 the next CU as end of this CU. We create the CUs here with length 0,
5133 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5134 dwarf2_per_cu_data *per_cu
168c9250
SM
5135 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5136 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5137 }
5138 }
5139
927aa2e7
JK
5140 sect_offset sect_off_prev;
5141 for (uint32_t i = 0; i <= map.cu_count; ++i)
5142 {
5143 sect_offset sect_off_next;
5144 if (i < map.cu_count)
5145 {
5146 sect_off_next
5147 = (sect_offset) (extract_unsigned_integer
5148 (map.cu_table_reordered + i * map.offset_size,
5149 map.offset_size,
5150 map.dwarf5_byte_order));
5151 }
5152 else
5153 sect_off_next = (sect_offset) section.size;
5154 if (i >= 1)
5155 {
5156 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5157 dwarf2_per_cu_data *per_cu
168c9250 5158 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5159 sect_off_prev, length);
168c9250 5160 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5161 }
5162 sect_off_prev = sect_off_next;
5163 }
5164}
5165
5166/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5167 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5168
5169static void
168c9250 5170create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5171 const mapped_debug_names &map,
5172 const mapped_debug_names &dwz_map)
5173{
168c9250
SM
5174 gdb_assert (per_bfd->all_comp_units.empty ());
5175 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5176
168c9250 5177 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5178 false /* is_dwz */);
927aa2e7
JK
5179
5180 if (dwz_map.cu_count == 0)
5181 return;
5182
168c9250
SM
5183 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5184 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5185 true /* is_dwz */);
927aa2e7
JK
5186}
5187
5188/* Read .debug_names. If everything went ok, initialize the "quick"
5189 elements of all the CUs and return true. Otherwise, return false. */
5190
5191static bool
ed2dc618 5192dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5193{
22ca247e
TT
5194 std::unique_ptr<mapped_debug_names> map
5195 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5196 mapped_debug_names dwz_map (dwarf2_per_objfile);
5197 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5198
5199 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5200 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5201 *map))
927aa2e7
JK
5202 return false;
5203
5204 /* Don't use the index if it's empty. */
22ca247e 5205 if (map->name_count == 0)
927aa2e7
JK
5206 return false;
5207
5208 /* If there is a .dwz file, read it so we can get its CU list as
5209 well. */
c3699833 5210 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5211 if (dwz != NULL)
5212 {
5213 if (!read_debug_names_from_section (objfile,
00f93c44 5214 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5215 &dwz->debug_names, dwz_map))
5216 {
5217 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5218 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5219 return false;
5220 }
5221 }
5222
168c9250 5223 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5224
22ca247e 5225 if (map->tu_count != 0)
927aa2e7
JK
5226 {
5227 /* We can only handle a single .debug_types when we have an
5228 index. */
5989a64e 5229 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5230 return false;
5231
5989a64e 5232 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5233
5234 create_signatured_type_table_from_debug_names
5989a64e 5235 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5236 }
5237
ed2dc618 5238 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5239 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5240
5989a64e
SM
5241 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5242 dwarf2_per_objfile->per_bfd->using_index = 1;
5243 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5244 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5245
5246 return true;
5247}
5248
927aa2e7
JK
5249/* Type used to manage iterating over all CUs looking for a symbol for
5250 .debug_names. */
5251
5252class dw2_debug_names_iterator
5253{
5254public:
927aa2e7 5255 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5256 gdb::optional<block_enum> block_index,
5257 domain_enum domain,
927aa2e7 5258 const char *name)
2b79f376 5259 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5260 m_addr (find_vec_in_debug_names (map, name))
5261 {}
5262
5263 dw2_debug_names_iterator (const mapped_debug_names &map,
5264 search_domain search, uint32_t namei)
5265 : m_map (map),
5266 m_search (search),
5267 m_addr (find_vec_in_debug_names (map, namei))
5268 {}
5269
3b00ef10
TT
5270 dw2_debug_names_iterator (const mapped_debug_names &map,
5271 block_enum block_index, domain_enum domain,
5272 uint32_t namei)
5273 : m_map (map), m_block_index (block_index), m_domain (domain),
5274 m_addr (find_vec_in_debug_names (map, namei))
5275 {}
5276
927aa2e7
JK
5277 /* Return the next matching CU or NULL if there are no more. */
5278 dwarf2_per_cu_data *next ();
5279
5280private:
5281 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5282 const char *name);
5283 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5284 uint32_t namei);
5285
5286 /* The internalized form of .debug_names. */
5287 const mapped_debug_names &m_map;
5288
2b79f376
SM
5289 /* If set, only look for symbols that match that block. Valid values are
5290 GLOBAL_BLOCK and STATIC_BLOCK. */
5291 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5292
5293 /* The kind of symbol we're looking for. */
5294 const domain_enum m_domain = UNDEF_DOMAIN;
5295 const search_domain m_search = ALL_DOMAIN;
5296
5297 /* The list of CUs from the index entry of the symbol, or NULL if
5298 not found. */
5299 const gdb_byte *m_addr;
5300};
5301
5302const char *
5303mapped_debug_names::namei_to_name (uint32_t namei) const
5304{
5305 const ULONGEST namei_string_offs
5306 = extract_unsigned_integer ((name_table_string_offs_reordered
5307 + namei * offset_size),
5308 offset_size,
5309 dwarf5_byte_order);
4f44ae6c
TT
5310 return read_indirect_string_at_offset (dwarf2_per_objfile,
5311 namei_string_offs);
927aa2e7
JK
5312}
5313
5314/* Find a slot in .debug_names for the object named NAME. If NAME is
5315 found, return pointer to its pool data. If NAME cannot be found,
5316 return NULL. */
5317
5318const gdb_byte *
5319dw2_debug_names_iterator::find_vec_in_debug_names
5320 (const mapped_debug_names &map, const char *name)
5321{
5322 int (*cmp) (const char *, const char *);
5323
54ee4252 5324 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5325 if (current_language->la_language == language_cplus
5326 || current_language->la_language == language_fortran
5327 || current_language->la_language == language_d)
5328 {
5329 /* NAME is already canonical. Drop any qualifiers as
5330 .debug_names does not contain any. */
5331
5332 if (strchr (name, '(') != NULL)
5333 {
54ee4252 5334 without_params = cp_remove_params (name);
927aa2e7 5335 if (without_params != NULL)
54ee4252 5336 name = without_params.get ();
927aa2e7
JK
5337 }
5338 }
5339
5340 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5341
5342 const uint32_t full_hash = dwarf5_djb_hash (name);
5343 uint32_t namei
5344 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5345 (map.bucket_table_reordered
5346 + (full_hash % map.bucket_count)), 4,
5347 map.dwarf5_byte_order);
5348 if (namei == 0)
5349 return NULL;
5350 --namei;
5351 if (namei >= map.name_count)
5352 {
b98664d3 5353 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5354 "[in module %s]"),
5355 namei, map.name_count,
ed2dc618 5356 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5357 return NULL;
5358 }
5359
5360 for (;;)
5361 {
5362 const uint32_t namei_full_hash
5363 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5364 (map.hash_table_reordered + namei), 4,
5365 map.dwarf5_byte_order);
5366 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5367 return NULL;
5368
5369 if (full_hash == namei_full_hash)
5370 {
5371 const char *const namei_string = map.namei_to_name (namei);
5372
5373#if 0 /* An expensive sanity check. */
5374 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5375 {
b98664d3 5376 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5377 "[in module %s]"),
5378 namei, objfile_name (dwarf2_per_objfile->objfile));
5379 return NULL;
5380 }
5381#endif
5382
5383 if (cmp (namei_string, name) == 0)
5384 {
5385 const ULONGEST namei_entry_offs
5386 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5387 + namei * map.offset_size),
5388 map.offset_size, map.dwarf5_byte_order);
5389 return map.entry_pool + namei_entry_offs;
5390 }
5391 }
5392
5393 ++namei;
5394 if (namei >= map.name_count)
5395 return NULL;
5396 }
5397}
5398
5399const gdb_byte *
5400dw2_debug_names_iterator::find_vec_in_debug_names
5401 (const mapped_debug_names &map, uint32_t namei)
5402{
5403 if (namei >= map.name_count)
5404 {
b98664d3 5405 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5406 "[in module %s]"),
5407 namei, map.name_count,
ed2dc618 5408 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5409 return NULL;
5410 }
5411
5412 const ULONGEST namei_entry_offs
5413 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5414 + namei * map.offset_size),
5415 map.offset_size, map.dwarf5_byte_order);
5416 return map.entry_pool + namei_entry_offs;
5417}
5418
5419/* See dw2_debug_names_iterator. */
5420
5421dwarf2_per_cu_data *
5422dw2_debug_names_iterator::next ()
5423{
5424 if (m_addr == NULL)
5425 return NULL;
5426
ed2dc618
SM
5427 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5428 struct objfile *objfile = dwarf2_per_objfile->objfile;
5429 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5430
5431 again:
5432
5433 unsigned int bytes_read;
5434 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5435 m_addr += bytes_read;
5436 if (abbrev == 0)
5437 return NULL;
5438
5439 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5440 if (indexval_it == m_map.abbrev_map.cend ())
5441 {
b98664d3 5442 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5443 "[in module %s]"),
ed2dc618 5444 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5445 return NULL;
5446 }
5447 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5448 enum class symbol_linkage {
5449 unknown,
5450 static_,
5451 extern_,
23c13d42 5452 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5453 dwarf2_per_cu_data *per_cu = NULL;
5454 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5455 {
5456 ULONGEST ull;
5457 switch (attr.form)
5458 {
5459 case DW_FORM_implicit_const:
5460 ull = attr.implicit_const;
5461 break;
5462 case DW_FORM_flag_present:
5463 ull = 1;
5464 break;
5465 case DW_FORM_udata:
5466 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5467 m_addr += bytes_read;
5468 break;
6dc55ce9 5469 case DW_FORM_ref4:
5470 ull = read_4_bytes (abfd, m_addr);
5471 m_addr += 4;
5472 break;
5473 case DW_FORM_ref8:
5474 ull = read_8_bytes (abfd, m_addr);
5475 m_addr += 8;
5476 break;
5477 case DW_FORM_ref_sig8:
5478 ull = read_8_bytes (abfd, m_addr);
5479 m_addr += 8;
5480 break;
927aa2e7 5481 default:
b98664d3 5482 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5483 dwarf_form_name (attr.form),
ed2dc618 5484 objfile_name (objfile));
927aa2e7
JK
5485 return NULL;
5486 }
5487 switch (attr.dw_idx)
5488 {
5489 case DW_IDX_compile_unit:
5490 /* Don't crash on bad data. */
5989a64e 5491 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5492 {
b98664d3 5493 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5494 " [in module %s]"),
5495 pulongest (ull),
5496 objfile_name (dwarf2_per_objfile->objfile));
5497 continue;
5498 }
5989a64e 5499 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5500 break;
8af5c486
JK
5501 case DW_IDX_type_unit:
5502 /* Don't crash on bad data. */
5989a64e 5503 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5504 {
b98664d3 5505 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5506 " [in module %s]"),
5507 pulongest (ull),
5508 objfile_name (dwarf2_per_objfile->objfile));
5509 continue;
5510 }
5989a64e 5511 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5512 break;
6dc55ce9 5513 case DW_IDX_die_offset:
5514 /* In a per-CU index (as opposed to a per-module index), index
5515 entries without CU attribute implicitly refer to the single CU. */
5516 if (per_cu == NULL)
5989a64e 5517 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5518 break;
927aa2e7
JK
5519 case DW_IDX_GNU_internal:
5520 if (!m_map.augmentation_is_gdb)
5521 break;
23c13d42 5522 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5523 break;
5524 case DW_IDX_GNU_external:
5525 if (!m_map.augmentation_is_gdb)
5526 break;
23c13d42 5527 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5528 break;
5529 }
5530 }
5531
5532 /* Skip if already read in. */
af758d11 5533 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5534 goto again;
5535
5536 /* Check static vs global. */
23c13d42 5537 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5538 {
2b79f376 5539 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5540 const bool symbol_is_static =
5541 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5542 if (want_static != symbol_is_static)
2b79f376 5543 goto again;
927aa2e7
JK
5544 }
5545
5546 /* Match dw2_symtab_iter_next, symbol_kind
5547 and debug_names::psymbol_tag. */
5548 switch (m_domain)
5549 {
5550 case VAR_DOMAIN:
5551 switch (indexval.dwarf_tag)
5552 {
5553 case DW_TAG_variable:
5554 case DW_TAG_subprogram:
5555 /* Some types are also in VAR_DOMAIN. */
5556 case DW_TAG_typedef:
5557 case DW_TAG_structure_type:
5558 break;
5559 default:
5560 goto again;
5561 }
5562 break;
5563 case STRUCT_DOMAIN:
5564 switch (indexval.dwarf_tag)
5565 {
5566 case DW_TAG_typedef:
5567 case DW_TAG_structure_type:
5568 break;
5569 default:
5570 goto again;
5571 }
5572 break;
5573 case LABEL_DOMAIN:
5574 switch (indexval.dwarf_tag)
5575 {
5576 case 0:
5577 case DW_TAG_variable:
5578 break;
5579 default:
5580 goto again;
5581 }
5582 break;
59c35742
AB
5583 case MODULE_DOMAIN:
5584 switch (indexval.dwarf_tag)
5585 {
5586 case DW_TAG_module:
5587 break;
5588 default:
5589 goto again;
5590 }
5591 break;
927aa2e7
JK
5592 default:
5593 break;
5594 }
5595
5596 /* Match dw2_expand_symtabs_matching, symbol_kind and
5597 debug_names::psymbol_tag. */
5598 switch (m_search)
4b514bc8 5599 {
927aa2e7
JK
5600 case VARIABLES_DOMAIN:
5601 switch (indexval.dwarf_tag)
4b514bc8 5602 {
927aa2e7
JK
5603 case DW_TAG_variable:
5604 break;
5605 default:
5606 goto again;
4b514bc8 5607 }
927aa2e7
JK
5608 break;
5609 case FUNCTIONS_DOMAIN:
5610 switch (indexval.dwarf_tag)
4b514bc8 5611 {
927aa2e7
JK
5612 case DW_TAG_subprogram:
5613 break;
5614 default:
5615 goto again;
4b514bc8 5616 }
927aa2e7
JK
5617 break;
5618 case TYPES_DOMAIN:
5619 switch (indexval.dwarf_tag)
5620 {
5621 case DW_TAG_typedef:
5622 case DW_TAG_structure_type:
5623 break;
5624 default:
5625 goto again;
5626 }
5627 break;
59c35742
AB
5628 case MODULES_DOMAIN:
5629 switch (indexval.dwarf_tag)
5630 {
5631 case DW_TAG_module:
5632 break;
5633 default:
5634 goto again;
5635 }
927aa2e7
JK
5636 default:
5637 break;
4b514bc8 5638 }
927aa2e7
JK
5639
5640 return per_cu;
4b514bc8 5641}
61920122 5642
927aa2e7 5643static struct compunit_symtab *
c7f839cb 5644dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5645 const char *name, domain_enum domain)
4b514bc8 5646{
ed2dc618
SM
5647 struct dwarf2_per_objfile *dwarf2_per_objfile
5648 = get_dwarf2_per_objfile (objfile);
61920122 5649
5989a64e 5650 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5651 if (!mapp)
61920122 5652 {
927aa2e7
JK
5653 /* index is NULL if OBJF_READNOW. */
5654 return NULL;
5655 }
5656 const auto &map = *mapp;
9291a0cd 5657
2b79f376 5658 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5659
927aa2e7
JK
5660 struct compunit_symtab *stab_best = NULL;
5661 struct dwarf2_per_cu_data *per_cu;
5662 while ((per_cu = iter.next ()) != NULL)
5663 {
5664 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5665 compunit_symtab *stab
5666 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5667 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5668 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5669
927aa2e7
JK
5670 sym = block_find_symbol (block, name, domain,
5671 block_find_non_opaque_type_preferred,
5672 &with_opaque);
9703b513 5673
927aa2e7
JK
5674 /* Some caution must be observed with overloaded functions and
5675 methods, since the index will not contain any overload
5676 information (but NAME might contain it). */
a3ec0bb1 5677
927aa2e7 5678 if (sym != NULL
987012b8 5679 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5680 return stab;
5681 if (with_opaque != NULL
987012b8 5682 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5683 stab_best = stab;
9703b513 5684
927aa2e7 5685 /* Keep looking through other CUs. */
9703b513
TT
5686 }
5687
927aa2e7 5688 return stab_best;
9703b513
TT
5689}
5690
927aa2e7
JK
5691/* This dumps minimal information about .debug_names. It is called
5692 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5693 uses this to verify that .debug_names has been loaded. */
9291a0cd 5694
927aa2e7
JK
5695static void
5696dw2_debug_names_dump (struct objfile *objfile)
5697{
ed2dc618
SM
5698 struct dwarf2_per_objfile *dwarf2_per_objfile
5699 = get_dwarf2_per_objfile (objfile);
5700
5989a64e 5701 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5702 printf_filtered (".debug_names:");
5989a64e 5703 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5704 printf_filtered (" exists\n");
5705 else
5706 printf_filtered (" faked for \"readnow\"\n");
5707 printf_filtered ("\n");
9291a0cd
TT
5708}
5709
9291a0cd 5710static void
927aa2e7
JK
5711dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5712 const char *func_name)
9291a0cd 5713{
ed2dc618
SM
5714 struct dwarf2_per_objfile *dwarf2_per_objfile
5715 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5716
5989a64e
SM
5717 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5718 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5719 {
5989a64e 5720 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5721
2b79f376 5722 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5723
927aa2e7
JK
5724 struct dwarf2_per_cu_data *per_cu;
5725 while ((per_cu = iter.next ()) != NULL)
97a1449a 5726 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5727 }
5728}
24c79950 5729
3b00ef10
TT
5730static void
5731dw2_debug_names_map_matching_symbols
5732 (struct objfile *objfile,
5733 const lookup_name_info &name, domain_enum domain,
5734 int global,
5735 gdb::function_view<symbol_found_callback_ftype> callback,
5736 symbol_compare_ftype *ordered_compare)
5737{
5738 struct dwarf2_per_objfile *dwarf2_per_objfile
5739 = get_dwarf2_per_objfile (objfile);
5740
5741 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5742 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5743 return;
5744
5989a64e 5745 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5746 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5747
5748 const char *match_name = name.ada ().lookup_name ().c_str ();
5749 auto matcher = [&] (const char *symname)
5750 {
5751 if (ordered_compare == nullptr)
5752 return true;
5753 return ordered_compare (symname, match_name) == 0;
5754 };
5755
5756 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5757 [&] (offset_type namei)
5758 {
5759 /* The name was matched, now expand corresponding CUs that were
5760 marked. */
5761 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5762
5763 struct dwarf2_per_cu_data *per_cu;
5764 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5765 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5766 nullptr);
3b00ef10
TT
5767 return true;
5768 });
5769
5770 /* It's a shame we couldn't do this inside the
5771 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5772 that have already been expanded. Instead, this loop matches what
5773 the psymtab code does. */
5989a64e 5774 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5775 {
af758d11
SM
5776 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5777 if (symtab != nullptr)
3b00ef10
TT
5778 {
5779 const struct block *block
af758d11 5780 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5781 if (!iterate_over_symbols_terminated (block, name,
5782 domain, callback))
5783 break;
5784 }
5785 }
5786}
5787
927aa2e7
JK
5788static void
5789dw2_debug_names_expand_symtabs_matching
5790 (struct objfile *objfile,
5791 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5792 const lookup_name_info *lookup_name,
927aa2e7
JK
5793 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5794 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5795 enum search_domain kind)
5796{
ed2dc618
SM
5797 struct dwarf2_per_objfile *dwarf2_per_objfile
5798 = get_dwarf2_per_objfile (objfile);
9291a0cd 5799
927aa2e7 5800 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5801 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5802 return;
9291a0cd 5803
ed2dc618 5804 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5805
c1a66c06
TV
5806 if (symbol_matcher == NULL && lookup_name == NULL)
5807 {
5989a64e 5808 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5809 {
5810 QUIT;
5811
97a1449a
SM
5812 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5813 file_matcher, expansion_notify);
c1a66c06
TV
5814 }
5815 return;
5816 }
5817
5989a64e 5818 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5819
c1a66c06 5820 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5821 symbol_matcher,
5822 kind, [&] (offset_type namei)
927aa2e7 5823 {
927aa2e7
JK
5824 /* The name was matched, now expand corresponding CUs that were
5825 marked. */
5826 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5827
927aa2e7
JK
5828 struct dwarf2_per_cu_data *per_cu;
5829 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5830 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5831 file_matcher, expansion_notify);
3b00ef10 5832 return true;
44ed8f3e 5833 });
9291a0cd
TT
5834}
5835
927aa2e7 5836const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5837{
5838 dw2_has_symbols,
5839 dw2_find_last_source_symtab,
5840 dw2_forget_cached_source_info,
f8eba3c6 5841 dw2_map_symtabs_matching_filename,
927aa2e7 5842 dw2_debug_names_lookup_symbol,
d3214198 5843 NULL,
9291a0cd 5844 dw2_print_stats,
927aa2e7 5845 dw2_debug_names_dump,
927aa2e7 5846 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5847 dw2_expand_all_symtabs,
652a8996 5848 dw2_expand_symtabs_with_fullname,
3b00ef10 5849 dw2_debug_names_map_matching_symbols,
927aa2e7 5850 dw2_debug_names_expand_symtabs_matching,
43f3e411 5851 dw2_find_pc_sect_compunit_symtab,
71a3c369 5852 NULL,
9291a0cd
TT
5853 dw2_map_symbol_filenames
5854};
5855
4485a1c1 5856/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5857 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5858
5859template <typename T>
5860static gdb::array_view<const gdb_byte>
5861get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5862{
5863 dwarf2_section_info *section = &section_owner->gdb_index;
5864
96b79293 5865 if (section->empty ())
4485a1c1
SM
5866 return {};
5867
5868 /* Older elfutils strip versions could keep the section in the main
5869 executable while splitting it for the separate debug info file. */
96b79293 5870 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5871 return {};
5872
96b79293 5873 section->read (obj);
4485a1c1 5874
8bebfcda
PA
5875 /* dwarf2_section_info::size is a bfd_size_type, while
5876 gdb::array_view works with size_t. On 32-bit hosts, with
5877 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5878 is 32-bit. So we need an explicit narrowing conversion here.
5879 This is fine, because it's impossible to allocate or mmap an
5880 array/buffer larger than what size_t can represent. */
5881 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5882}
5883
87d6a7aa
SM
5884/* Lookup the index cache for the contents of the index associated to
5885 DWARF2_OBJ. */
5886
5887static gdb::array_view<const gdb_byte>
5989a64e 5888get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5889{
5890 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5891 if (build_id == nullptr)
5892 return {};
5893
5894 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5895 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5896}
5897
5898/* Same as the above, but for DWZ. */
5899
5900static gdb::array_view<const gdb_byte>
5901get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5902{
5903 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5904 if (build_id == nullptr)
5905 return {};
5906
5907 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5908}
5909
3c0aa29a 5910/* See symfile.h. */
9291a0cd 5911
3c0aa29a
PA
5912bool
5913dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5914{
ed2dc618
SM
5915 struct dwarf2_per_objfile *dwarf2_per_objfile
5916 = get_dwarf2_per_objfile (objfile);
5917
9291a0cd
TT
5918 /* If we're about to read full symbols, don't bother with the
5919 indices. In this case we also don't care if some other debug
5920 format is making psymtabs, because they are all about to be
5921 expanded anyway. */
5922 if ((objfile->flags & OBJF_READNOW))
5923 {
5989a64e 5924 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5925 create_all_comp_units (dwarf2_per_objfile);
5926 create_all_type_units (dwarf2_per_objfile);
5989a64e 5927 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5928 = create_quick_file_names_table
5989a64e 5929 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5930 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5931
5989a64e
SM
5932 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5933 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5934 {
5989a64e 5935 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5936
5989a64e 5937 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5938 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5939 }
5940
5941 /* Return 1 so that gdb sees the "quick" functions. However,
5942 these functions will be no-ops because we will have expanded
5943 all symtabs. */
3c0aa29a
PA
5944 *index_kind = dw_index_kind::GDB_INDEX;
5945 return true;
9291a0cd
TT
5946 }
5947
ed2dc618 5948 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5949 {
5950 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5951 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5952 return true;
5953 }
927aa2e7 5954
4485a1c1 5955 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5956 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5957 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5958 {
5959 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5960 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5961 return true;
5962 }
9291a0cd 5963
87d6a7aa
SM
5964 /* ... otherwise, try to find the index in the index cache. */
5965 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5966 get_gdb_index_contents_from_cache,
5967 get_gdb_index_contents_from_cache_dwz))
5968 {
5969 global_index_cache.hit ();
5970 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5971 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5972 return true;
5973 }
5974
5975 global_index_cache.miss ();
3c0aa29a 5976 return false;
9291a0cd
TT
5977}
5978
5979\f
5980
dce234bc
PP
5981/* Build a partial symbol table. */
5982
5983void
f29dff0a 5984dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5985{
ed2dc618
SM
5986 struct dwarf2_per_objfile *dwarf2_per_objfile
5987 = get_dwarf2_per_objfile (objfile);
c9bf0622 5988
6eee24ce 5989 init_psymbol_list (objfile, 1024);
c906108c 5990
a70b8144 5991 try
c9bf0622
TT
5992 {
5993 /* This isn't really ideal: all the data we allocate on the
5994 objfile's obstack is still uselessly kept around. However,
5995 freeing it seems unsafe. */
906768f9 5996 psymtab_discarder psymtabs (objfile);
ed2dc618 5997 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5998 psymtabs.keep ();
87d6a7aa 5999
af758d11
SM
6000 dwarf2_per_objfile->resize_symtabs ();
6001
87d6a7aa
SM
6002 /* (maybe) store an index in the cache. */
6003 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6004 }
230d2906 6005 catch (const gdb_exception_error &except)
492d29ea
PA
6006 {
6007 exception_print (gdb_stderr, except);
6008 }
c906108c 6009}
c906108c 6010
3b80fe9b
DE
6011/* Find the base address of the compilation unit for range lists and
6012 location lists. It will normally be specified by DW_AT_low_pc.
6013 In DWARF-3 draft 4, the base address could be overridden by
6014 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6015 compilation units with discontinuous ranges. */
6016
6017static void
6018dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6019{
6020 struct attribute *attr;
6021
2b24b6e4 6022 cu->base_address.reset ();
3b80fe9b
DE
6023
6024 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6025 if (attr != nullptr)
2b24b6e4 6026 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6027 else
6028 {
6029 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6030 if (attr != nullptr)
2b24b6e4 6031 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6032 }
6033}
6034
36586728
TT
6035/* Helper function that returns the proper abbrev section for
6036 THIS_CU. */
6037
6038static struct dwarf2_section_info *
6039get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6040{
6041 struct dwarf2_section_info *abbrev;
c3699833 6042 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6043
6044 if (this_cu->is_dwz)
c3699833 6045 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6046 else
c3699833 6047 abbrev = &per_bfd->abbrev;
36586728
TT
6048
6049 return abbrev;
6050}
6051
f4dc4d17
DE
6052/* Fetch the abbreviation table offset from a comp or type unit header. */
6053
6054static sect_offset
ed2dc618
SM
6055read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6056 struct dwarf2_section_info *section,
9c541725 6057 sect_offset sect_off)
f4dc4d17 6058{
96b79293 6059 bfd *abfd = section->get_bfd_owner ();
d521ce57 6060 const gdb_byte *info_ptr;
ac298888 6061 unsigned int initial_length_size, offset_size;
43988095 6062 uint16_t version;
f4dc4d17 6063
96b79293 6064 section->read (dwarf2_per_objfile->objfile);
9c541725 6065 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6066 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6067 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6068 info_ptr += initial_length_size;
6069
6070 version = read_2_bytes (abfd, info_ptr);
6071 info_ptr += 2;
6072 if (version >= 5)
6073 {
6074 /* Skip unit type and address size. */
6075 info_ptr += 2;
6076 }
6077
24aa364d 6078 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6079}
6080
b83470bf
TT
6081/* A partial symtab that is used only for include files. */
6082struct dwarf2_include_psymtab : public partial_symtab
6083{
6084 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6085 : partial_symtab (filename, objfile)
6086 {
6087 }
6088
6089 void read_symtab (struct objfile *objfile) override
6090 {
194d088f
TV
6091 /* It's an include file, no symbols to read for it.
6092 Everything is in the includer symtab. */
6093
6094 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6095 expansion of the includer psymtab. We use the dependencies[0] field to
6096 model the includer. But if we go the regular route of calling
6097 expand_psymtab here, and having expand_psymtab call expand_dependencies
6098 to expand the includer, we'll only use expand_psymtab on the includer
6099 (making it a non-toplevel psymtab), while if we expand the includer via
6100 another path, we'll use read_symtab (making it a toplevel psymtab).
6101 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6102 psymtab, and trigger read_symtab on the includer here directly. */
6103 includer ()->read_symtab (objfile);
b83470bf
TT
6104 }
6105
6106 void expand_psymtab (struct objfile *objfile) override
6107 {
194d088f
TV
6108 /* This is not called by read_symtab, and should not be called by any
6109 expand_dependencies. */
6110 gdb_assert (false);
b83470bf
TT
6111 }
6112
5717c425 6113 bool readin_p (struct objfile *objfile) const override
b83470bf 6114 {
5717c425 6115 return includer ()->readin_p (objfile);
b83470bf
TT
6116 }
6117
5717c425 6118 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6119 {
6120 return nullptr;
6121 }
6122
6123private:
194d088f
TV
6124 partial_symtab *includer () const
6125 {
6126 /* An include psymtab has exactly one dependency: the psymtab that
6127 includes it. */
6128 gdb_assert (this->number_of_dependencies == 1);
6129 return this->dependencies[0];
6130 }
b83470bf
TT
6131};
6132
aaa75496
JB
6133/* Allocate a new partial symtab for file named NAME and mark this new
6134 partial symtab as being an include of PST. */
6135
6136static void
891813be 6137dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6138 struct objfile *objfile)
6139{
b83470bf 6140 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6141
fbd9ab74 6142 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6143 subpst->dirname = pst->dirname;
fbd9ab74 6144
a9342b62 6145 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6146 subpst->dependencies[0] = pst;
6147 subpst->number_of_dependencies = 1;
aaa75496
JB
6148}
6149
6150/* Read the Line Number Program data and extract the list of files
6151 included by the source file represented by PST. Build an include
d85a05f0 6152 partial symtab for each of these included files. */
aaa75496
JB
6153
6154static void
6155dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6156 struct die_info *die,
891813be 6157 dwarf2_psymtab *pst)
aaa75496 6158{
fff8551c 6159 line_header_up lh;
d85a05f0 6160 struct attribute *attr;
aaa75496 6161
d85a05f0 6162 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6163 if (attr != nullptr)
9c541725 6164 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6165 if (lh == NULL)
6166 return; /* No linetable, so no includes. */
6167
79748972
TT
6168 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6169 that we pass in the raw text_low here; that is ok because we're
6170 only decoding the line table to make include partial symtabs, and
6171 so the addresses aren't really used. */
4ae976d1 6172 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6173 pst->raw_text_low (), 1);
aaa75496
JB
6174}
6175
348e048f 6176static hashval_t
52dc124a 6177hash_signatured_type (const void *item)
348e048f 6178{
9a3c8263
SM
6179 const struct signatured_type *sig_type
6180 = (const struct signatured_type *) item;
9a619af0 6181
348e048f 6182 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6183 return sig_type->signature;
348e048f
DE
6184}
6185
6186static int
52dc124a 6187eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6188{
9a3c8263
SM
6189 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6190 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6191
348e048f
DE
6192 return lhs->signature == rhs->signature;
6193}
6194
1fd400ff
TT
6195/* Allocate a hash table for signatured types. */
6196
b0b6a987 6197static htab_up
298e9637 6198allocate_signatured_type_table ()
1fd400ff 6199{
b0b6a987
TT
6200 return htab_up (htab_create_alloc (41,
6201 hash_signatured_type,
6202 eq_signatured_type,
6203 NULL, xcalloc, xfree));
1fd400ff
TT
6204}
6205
d467dd73 6206/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6207
6208static int
d467dd73 6209add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6210{
9a3c8263 6211 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6212 std::vector<signatured_type *> *all_type_units
6213 = (std::vector<signatured_type *> *) datum;
1fd400ff 6214
b2bdb8cf 6215 all_type_units->push_back (sigt);
1fd400ff
TT
6216
6217 return 1;
6218}
6219
78d4d2c5 6220/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6221 and fill them into TYPES_HTAB. It will process only type units,
6222 therefore DW_UT_type. */
c88ee1f0 6223
78d4d2c5 6224static void
ed2dc618
SM
6225create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6226 struct dwo_file *dwo_file,
b0b6a987 6227 dwarf2_section_info *section, htab_up &types_htab,
43988095 6228 rcuh_kind section_kind)
348e048f 6229{
3019eac3 6230 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6231 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6232 bfd *abfd;
6233 const gdb_byte *info_ptr, *end_ptr;
348e048f 6234
4bdcc0c1
DE
6235 abbrev_section = (dwo_file != NULL
6236 ? &dwo_file->sections.abbrev
5989a64e 6237 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6238
b4f54984 6239 if (dwarf_read_debug)
43988095 6240 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6241 section->get_name (),
6242 abbrev_section->get_file_name ());
09406207 6243
96b79293 6244 section->read (objfile);
78d4d2c5 6245 info_ptr = section->buffer;
348e048f 6246
78d4d2c5
JK
6247 if (info_ptr == NULL)
6248 return;
348e048f 6249
78d4d2c5
JK
6250 /* We can't set abfd until now because the section may be empty or
6251 not present, in which case the bfd is unknown. */
96b79293 6252 abfd = section->get_bfd_owner ();
348e048f 6253
c0ab21c2
TT
6254 /* We don't use cutu_reader here because we don't need to read
6255 any dies: the signature is in the header. */
3019eac3 6256
78d4d2c5
JK
6257 end_ptr = info_ptr + section->size;
6258 while (info_ptr < end_ptr)
6259 {
78d4d2c5
JK
6260 struct signatured_type *sig_type;
6261 struct dwo_unit *dwo_tu;
6262 void **slot;
6263 const gdb_byte *ptr = info_ptr;
6264 struct comp_unit_head header;
6265 unsigned int length;
8b70b953 6266
9c541725 6267 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6268
a49dd8dd
JK
6269 /* Initialize it due to a false compiler warning. */
6270 header.signature = -1;
9c541725 6271 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6272
78d4d2c5
JK
6273 /* We need to read the type's signature in order to build the hash
6274 table, but we don't need anything else just yet. */
348e048f 6275
ed2dc618 6276 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6277 abbrev_section, ptr, section_kind);
348e048f 6278
4057dfde 6279 length = header.get_length ();
6caca83c 6280
78d4d2c5
JK
6281 /* Skip dummy type units. */
6282 if (ptr >= info_ptr + length
43988095
JK
6283 || peek_abbrev_code (abfd, ptr) == 0
6284 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6285 {
6286 info_ptr += length;
6287 continue;
6288 }
dee91e82 6289
78d4d2c5
JK
6290 if (types_htab == NULL)
6291 {
6292 if (dwo_file)
298e9637 6293 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6294 else
298e9637 6295 types_htab = allocate_signatured_type_table ();
78d4d2c5 6296 }
8b70b953 6297
78d4d2c5
JK
6298 if (dwo_file)
6299 {
6300 sig_type = NULL;
5989a64e 6301 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6302 struct dwo_unit);
6303 dwo_tu->dwo_file = dwo_file;
43988095 6304 dwo_tu->signature = header.signature;
9c541725 6305 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6306 dwo_tu->section = section;
9c541725 6307 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6308 dwo_tu->length = length;
6309 }
6310 else
6311 {
6312 /* N.B.: type_offset is not usable if this type uses a DWO file.
6313 The real type_offset is in the DWO file. */
6314 dwo_tu = NULL;
5989a64e 6315 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6316 sig_type->signature = header.signature;
9c541725 6317 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6318 sig_type->per_cu.is_debug_types = 1;
6319 sig_type->per_cu.section = section;
9c541725 6320 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6321 sig_type->per_cu.length = length;
6322 }
6323
b0b6a987 6324 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6325 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6326 INSERT);
6327 gdb_assert (slot != NULL);
6328 if (*slot != NULL)
6329 {
9c541725 6330 sect_offset dup_sect_off;
0349ea22 6331
3019eac3
DE
6332 if (dwo_file)
6333 {
78d4d2c5
JK
6334 const struct dwo_unit *dup_tu
6335 = (const struct dwo_unit *) *slot;
6336
9c541725 6337 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6338 }
6339 else
6340 {
78d4d2c5
JK
6341 const struct signatured_type *dup_tu
6342 = (const struct signatured_type *) *slot;
6343
9c541725 6344 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6345 }
8b70b953 6346
b98664d3 6347 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6348 " the entry at offset %s, signature %s"),
6349 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6350 hex_string (header.signature));
78d4d2c5
JK
6351 }
6352 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6353
78d4d2c5 6354 if (dwarf_read_debug > 1)
9d8780f0
SM
6355 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6356 sect_offset_str (sect_off),
43988095 6357 hex_string (header.signature));
3019eac3 6358
78d4d2c5
JK
6359 info_ptr += length;
6360 }
6361}
3019eac3 6362
78d4d2c5
JK
6363/* Create the hash table of all entries in the .debug_types
6364 (or .debug_types.dwo) section(s).
6365 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6366 otherwise it is NULL.
b3c8eb43 6367
78d4d2c5 6368 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6369
78d4d2c5 6370 Note: This function processes DWO files only, not DWP files. */
348e048f 6371
78d4d2c5 6372static void
ed2dc618
SM
6373create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6374 struct dwo_file *dwo_file,
fd5866f6 6375 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6376 htab_up &types_htab)
78d4d2c5 6377{
fd5866f6
SM
6378 for (dwarf2_section_info &section : type_sections)
6379 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6380 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6381}
6382
6383/* Create the hash table of all entries in the .debug_types section,
6384 and initialize all_type_units.
6385 The result is zero if there is an error (e.g. missing .debug_types section),
6386 otherwise non-zero. */
6387
6388static int
ed2dc618 6389create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6390{
b0b6a987 6391 htab_up types_htab;
3019eac3 6392
ed2dc618 6393 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6394 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6395 rcuh_kind::COMPILE);
ed2dc618 6396 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6397 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6398 if (types_htab == NULL)
6399 {
5989a64e 6400 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6401 return 0;
6402 }
6403
5989a64e 6404 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6405
5989a64e
SM
6406 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6407 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6408 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6409
5989a64e 6410 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6411 add_signatured_type_cu_to_table,
5989a64e 6412 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6413
348e048f
DE
6414 return 1;
6415}
6416
5989a64e 6417/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6418 If SLOT is non-NULL, it is the entry to use in the hash table.
6419 Otherwise we find one. */
6420
6421static struct signatured_type *
ed2dc618
SM
6422add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6423 void **slot)
6aa5f3a6 6424{
5989a64e
SM
6425 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6426 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6427 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6428
5989a64e 6429 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6430
af758d11
SM
6431 dwarf2_per_objfile->resize_symtabs ();
6432
5989a64e 6433 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6434 sig_type->signature = sig;
6435 sig_type->per_cu.is_debug_types = 1;
5989a64e 6436 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6437 {
6438 sig_type->per_cu.v.quick =
5989a64e 6439 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6440 struct dwarf2_per_cu_quick_data);
6441 }
6442
6443 if (slot == NULL)
6444 {
5989a64e 6445 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6446 sig_type, INSERT);
6447 }
6448 gdb_assert (*slot == NULL);
6449 *slot = sig_type;
6450 /* The rest of sig_type must be filled in by the caller. */
6451 return sig_type;
6452}
6453
a2ce51a0
DE
6454/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6455 Fill in SIG_ENTRY with DWO_ENTRY. */
6456
6457static void
ed2dc618 6458fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6459 struct signatured_type *sig_entry,
6460 struct dwo_unit *dwo_entry)
6461{
1859c670
SM
6462 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6463
7ee85ab1 6464 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6465 gdb_assert (! sig_entry->per_cu.queued);
6466 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6467 if (per_bfd->using_index)
6aa5f3a6
DE
6468 {
6469 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6470 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6471 }
6472 else
6473 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6474 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6475 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6476 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6477 gdb_assert (sig_entry->dwo_unit == NULL);
6478
6479 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6480 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6481 sig_entry->per_cu.length = dwo_entry->length;
6482 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6483 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6484 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6485 sig_entry->dwo_unit = dwo_entry;
6486}
6487
6488/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6489 If we haven't read the TU yet, create the signatured_type data structure
6490 for a TU to be read in directly from a DWO file, bypassing the stub.
6491 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6492 using .gdb_index, then when reading a CU we want to stay in the DWO file
6493 containing that CU. Otherwise we could end up reading several other DWO
6494 files (due to comdat folding) to process the transitive closure of all the
6495 mentioned TUs, and that can be slow. The current DWO file will have every
6496 type signature that it needs.
a2ce51a0
DE
6497 We only do this for .gdb_index because in the psymtab case we already have
6498 to read all the DWOs to build the type unit groups. */
6499
6500static struct signatured_type *
6501lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6502{
5e22e966 6503 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6504 struct dwo_file *dwo_file;
6505 struct dwo_unit find_dwo_entry, *dwo_entry;
6506 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6507 void **slot;
a2ce51a0 6508
5989a64e 6509 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6510
6aa5f3a6
DE
6511 /* If TU skeletons have been removed then we may not have read in any
6512 TUs yet. */
5989a64e
SM
6513 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6514 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6515
6516 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6517 Use the global signatured_types array to do our own comdat-folding
6518 of types. If this is the first time we're reading this TU, and
6519 the TU has an entry in .gdb_index, replace the recorded data from
6520 .gdb_index with this TU. */
a2ce51a0 6521
a2ce51a0 6522 find_sig_entry.signature = sig;
5989a64e 6523 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6524 &find_sig_entry, INSERT);
9a3c8263 6525 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6526
6527 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6528 read. Don't reassign the global entry to point to this DWO if that's
6529 the case. Also note that if the TU is already being read, it may not
6530 have come from a DWO, the program may be a mix of Fission-compiled
6531 code and non-Fission-compiled code. */
6532
6533 /* Have we already tried to read this TU?
6534 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6535 needn't exist in the global table yet). */
6536 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6537 return sig_entry;
6538
6aa5f3a6
DE
6539 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6540 dwo_unit of the TU itself. */
6541 dwo_file = cu->dwo_unit->dwo_file;
6542
a2ce51a0
DE
6543 /* Ok, this is the first time we're reading this TU. */
6544 if (dwo_file->tus == NULL)
6545 return NULL;
6546 find_dwo_entry.signature = sig;
b0b6a987
TT
6547 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6548 &find_dwo_entry);
a2ce51a0
DE
6549 if (dwo_entry == NULL)
6550 return NULL;
6551
6aa5f3a6
DE
6552 /* If the global table doesn't have an entry for this TU, add one. */
6553 if (sig_entry == NULL)
ed2dc618 6554 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6555
ed2dc618 6556 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6557 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6558 return sig_entry;
6559}
6560
a2ce51a0
DE
6561/* Subroutine of lookup_signatured_type.
6562 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6563 then try the DWP file. If the TU stub (skeleton) has been removed then
6564 it won't be in .gdb_index. */
a2ce51a0
DE
6565
6566static struct signatured_type *
6567lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6568{
5e22e966 6569 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6570 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6571 struct dwo_unit *dwo_entry;
6572 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6573 void **slot;
a2ce51a0 6574
5989a64e 6575 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6576 gdb_assert (dwp_file != NULL);
6577
6aa5f3a6
DE
6578 /* If TU skeletons have been removed then we may not have read in any
6579 TUs yet. */
5989a64e
SM
6580 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6581 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6582
6aa5f3a6 6583 find_sig_entry.signature = sig;
5989a64e 6584 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6585 &find_sig_entry, INSERT);
9a3c8263 6586 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6587
6588 /* Have we already tried to read this TU?
6589 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6590 needn't exist in the global table yet). */
6591 if (sig_entry != NULL)
6592 return sig_entry;
6593
a2ce51a0
DE
6594 if (dwp_file->tus == NULL)
6595 return NULL;
ed2dc618 6596 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6597 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6598 if (dwo_entry == NULL)
6599 return NULL;
6600
ed2dc618
SM
6601 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6602 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6603
a2ce51a0
DE
6604 return sig_entry;
6605}
6606
380bca97 6607/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6608 Returns NULL if signature SIG is not present in the table.
6609 It is up to the caller to complain about this. */
348e048f
DE
6610
6611static struct signatured_type *
a2ce51a0 6612lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6613{
5e22e966 6614 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6615
a2ce51a0 6616 if (cu->dwo_unit
5989a64e 6617 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6618 {
6619 /* We're in a DWO/DWP file, and we're using .gdb_index.
6620 These cases require special processing. */
ed2dc618 6621 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6622 return lookup_dwo_signatured_type (cu, sig);
6623 else
6624 return lookup_dwp_signatured_type (cu, sig);
6625 }
6626 else
6627 {
6628 struct signatured_type find_entry, *entry;
348e048f 6629
5989a64e 6630 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6631 return NULL;
6632 find_entry.signature = sig;
9a3c8263 6633 entry = ((struct signatured_type *)
5989a64e 6634 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6635 &find_entry));
a2ce51a0
DE
6636 return entry;
6637 }
348e048f 6638}
18a8505e 6639
42e7ad6c 6640/* Low level DIE reading support. */
348e048f 6641
d85a05f0
DJ
6642/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6643
6644static void
6645init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6646 struct dwarf2_cu *cu,
3019eac3 6647 struct dwarf2_section_info *section,
685af9cd
TT
6648 struct dwo_file *dwo_file,
6649 struct abbrev_table *abbrev_table)
d85a05f0 6650{
fceca515 6651 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6652 reader->abfd = section->get_bfd_owner ();
d85a05f0 6653 reader->cu = cu;
3019eac3 6654 reader->dwo_file = dwo_file;
dee91e82
DE
6655 reader->die_section = section;
6656 reader->buffer = section->buffer;
f664829e 6657 reader->buffer_end = section->buffer + section->size;
685af9cd 6658 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6659}
6660
c0ab21c2 6661/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6662 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6663 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6664 already.
6665
6666 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6667 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6668 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6669 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6670 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6671 STUB_COMP_DIR may be non-NULL.
3e225074 6672 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6673 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6674 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6675 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6676 kept around for at least as long as *RESULT_READER.
6677
b0c7bfa9
DE
6678 The result is non-zero if a valid (non-dummy) DIE was found. */
6679
6680static int
4ab09049 6681read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6682 struct dwo_unit *dwo_unit,
b0c7bfa9 6683 struct die_info *stub_comp_unit_die,
a2ce51a0 6684 const char *stub_comp_dir,
b0c7bfa9 6685 struct die_reader_specs *result_reader,
d521ce57 6686 const gdb_byte **result_info_ptr,
b0c7bfa9 6687 struct die_info **result_comp_unit_die,
685af9cd 6688 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6689{
4ab09049
SM
6690 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6691 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6692 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6693 bfd *abfd;
d521ce57 6694 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6695 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6696 int i,num_extra_attrs;
6697 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6698 struct die_info *comp_unit_die;
6699
b0aeadb3
DE
6700 /* At most one of these may be provided. */
6701 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6702
b0c7bfa9
DE
6703 /* These attributes aren't processed until later:
6704 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6705 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6706 referenced later. However, these attributes are found in the stub
6707 which we won't have later. In order to not impose this complication
6708 on the rest of the code, we read them here and copy them to the
6709 DWO CU/TU die. */
b0c7bfa9
DE
6710
6711 stmt_list = NULL;
6712 low_pc = NULL;
6713 high_pc = NULL;
6714 ranges = NULL;
6715 comp_dir = NULL;
6716
6717 if (stub_comp_unit_die != NULL)
6718 {
6719 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6720 DWO file. */
4ab09049 6721 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6722 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6723 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6724 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6725 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6726 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6727
a39fdb41 6728 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6729
18a8505e
AT
6730 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6731 here (if needed). We need the value before we can process
6732 DW_AT_ranges. */
a39fdb41 6733 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6734 }
a2ce51a0
DE
6735 else if (stub_comp_dir != NULL)
6736 {
6737 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6738 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6739 comp_dir->name = DW_AT_comp_dir;
6740 comp_dir->form = DW_FORM_string;
6741 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6742 DW_STRING (comp_dir) = stub_comp_dir;
6743 }
b0c7bfa9
DE
6744
6745 /* Set up for reading the DWO CU/TU. */
6746 cu->dwo_unit = dwo_unit;
685af9cd 6747 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6748 section->read (objfile);
6749 abfd = section->get_bfd_owner ();
9c541725
PA
6750 begin_info_ptr = info_ptr = (section->buffer
6751 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6752 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6753
4ab09049 6754 if (per_cu->is_debug_types)
b0c7bfa9 6755 {
4ab09049 6756 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6757
ed2dc618
SM
6758 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6759 &cu->header, section,
b0c7bfa9 6760 dwo_abbrev_section,
43988095 6761 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6762 /* This is not an assert because it can be caused by bad debug info. */
43988095 6763 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6764 {
6765 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6766 " TU at offset %s [in module %s]"),
a2ce51a0 6767 hex_string (sig_type->signature),
43988095 6768 hex_string (cu->header.signature),
9d8780f0 6769 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6770 bfd_get_filename (abfd));
6771 }
9c541725 6772 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6773 /* For DWOs coming from DWP files, we don't know the CU length
6774 nor the type's offset in the TU until now. */
4057dfde 6775 dwo_unit->length = cu->header.get_length ();
9c541725 6776 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6777
6778 /* Establish the type offset that can be used to lookup the type.
6779 For DWO files, we don't know it until now. */
9c541725
PA
6780 sig_type->type_offset_in_section
6781 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6782 }
6783 else
6784 {
ed2dc618
SM
6785 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6786 &cu->header, section,
b0c7bfa9 6787 dwo_abbrev_section,
43988095 6788 info_ptr, rcuh_kind::COMPILE);
9c541725 6789 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6790 /* For DWOs coming from DWP files, we don't know the CU length
6791 until now. */
4057dfde 6792 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6793 }
6794
685af9cd 6795 *result_dwo_abbrev_table
86de1d91
TT
6796 = abbrev_table::read (objfile, dwo_abbrev_section,
6797 cu->header.abbrev_sect_off);
685af9cd
TT
6798 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6799 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6800
6801 /* Read in the die, but leave space to copy over the attributes
6802 from the stub. This has the benefit of simplifying the rest of
6803 the code - all the work to maintain the illusion of a single
6804 DW_TAG_{compile,type}_unit DIE is done here. */
6805 num_extra_attrs = ((stmt_list != NULL)
6806 + (low_pc != NULL)
6807 + (high_pc != NULL)
6808 + (ranges != NULL)
6809 + (comp_dir != NULL));
6810 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6811 num_extra_attrs);
b0c7bfa9
DE
6812
6813 /* Copy over the attributes from the stub to the DIE we just read in. */
6814 comp_unit_die = *result_comp_unit_die;
6815 i = comp_unit_die->num_attrs;
6816 if (stmt_list != NULL)
6817 comp_unit_die->attrs[i++] = *stmt_list;
6818 if (low_pc != NULL)
6819 comp_unit_die->attrs[i++] = *low_pc;
6820 if (high_pc != NULL)
6821 comp_unit_die->attrs[i++] = *high_pc;
6822 if (ranges != NULL)
6823 comp_unit_die->attrs[i++] = *ranges;
6824 if (comp_dir != NULL)
6825 comp_unit_die->attrs[i++] = *comp_dir;
6826 comp_unit_die->num_attrs += num_extra_attrs;
6827
b4f54984 6828 if (dwarf_die_debug)
bf6af496
DE
6829 {
6830 fprintf_unfiltered (gdb_stdlog,
6831 "Read die from %s@0x%x of %s:\n",
96b79293 6832 section->get_name (),
bf6af496
DE
6833 (unsigned) (begin_info_ptr - section->buffer),
6834 bfd_get_filename (abfd));
b4f54984 6835 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6836 }
6837
b0c7bfa9
DE
6838 /* Skip dummy compilation units. */
6839 if (info_ptr >= begin_info_ptr + dwo_unit->length
6840 || peek_abbrev_code (abfd, info_ptr) == 0)
6841 return 0;
6842
6843 *result_info_ptr = info_ptr;
6844 return 1;
6845}
6846
a084a2a6
AT
6847/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6848 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6849 signature is part of the header. */
6850static gdb::optional<ULONGEST>
6851lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6852{
6853 if (cu->header.version >= 5)
6854 return cu->header.signature;
6855 struct attribute *attr;
6856 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6857 if (attr == nullptr)
6858 return gdb::optional<ULONGEST> ();
6859 return DW_UNSND (attr);
6860}
6861
c0ab21c2 6862/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6863 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6864 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6865
6866static struct dwo_unit *
4ab09049 6867lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6868{
4ab09049 6869 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6870 struct dwo_unit *dwo_unit;
c0ab21c2 6871 const char *comp_dir;
b0c7bfa9 6872
a2ce51a0
DE
6873 gdb_assert (cu != NULL);
6874
b0c7bfa9 6875 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6876 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6877 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6878
4ab09049
SM
6879 if (per_cu->is_debug_types)
6880 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6881 else
6882 {
a084a2a6 6883 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6884
a084a2a6 6885 if (!signature.has_value ())
b0c7bfa9
DE
6886 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6887 " [in module %s]"),
4ab09049
SM
6888 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6889
6890 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6891 }
6892
b0c7bfa9
DE
6893 return dwo_unit;
6894}
6895
c0ab21c2 6896/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6897 See it for a description of the parameters.
fcd3b13d 6898 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6899
c0ab21c2 6900void
9e021579
SM
6901cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6902 dwarf2_per_objfile *per_objfile,
6751ebae 6903 int use_existing_cu)
a2ce51a0 6904{
a2ce51a0 6905 struct signatured_type *sig_type;
a2ce51a0
DE
6906
6907 /* Verify we can do the following downcast, and that we have the
6908 data we need. */
6909 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6910 sig_type = (struct signatured_type *) this_cu;
6911 gdb_assert (sig_type->dwo_unit != NULL);
6912
6aa5f3a6
DE
6913 if (use_existing_cu && this_cu->cu != NULL)
6914 {
6915 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6916 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6917 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6918 }
6919 else
6920 {
6921 /* If !use_existing_cu, this_cu->cu must be NULL. */
6922 gdb_assert (this_cu->cu == NULL);
9e021579 6923 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6924 }
6925
6926 /* A future optimization, if needed, would be to use an existing
6927 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6928 could share abbrev tables. */
a2ce51a0 6929
4ab09049 6930 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6931 NULL /* stub_comp_unit_die */,
6932 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6933 this, &info_ptr,
3e225074 6934 &comp_unit_die,
c0ab21c2 6935 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6936 {
6937 /* Dummy die. */
c0ab21c2 6938 dummy_p = true;
a2ce51a0 6939 }
a2ce51a0
DE
6940}
6941
fd820528 6942/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6943 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6944
f4dc4d17
DE
6945 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6946 Otherwise the table specified in the comp unit header is read in and used.
6947 This is an optimization for when we already have the abbrev table.
6948
dee91e82 6949 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6950 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6951
ab432490
SM
6952cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6953 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6954 struct abbrev_table *abbrev_table,
6751ebae 6955 int use_existing_cu,
c0ab21c2
TT
6956 bool skip_partial)
6957 : die_reader_specs {},
6751ebae 6958 m_this_cu (this_cu)
c906108c 6959{
dee91e82 6960 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6961 struct dwarf2_section_info *section = this_cu->section;
96b79293 6962 bfd *abfd = section->get_bfd_owner ();
dee91e82 6963 struct dwarf2_cu *cu;
c0ab21c2 6964 const gdb_byte *begin_info_ptr;
dee91e82 6965 struct signatured_type *sig_type = NULL;
4bdcc0c1 6966 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6967 /* Non-zero if CU currently points to a DWO file and we need to
6968 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6969 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6970 int rereading_dwo_cu = 0;
c906108c 6971
b4f54984 6972 if (dwarf_die_debug)
9d8780f0 6973 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6974 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6975 sect_offset_str (this_cu->sect_off));
09406207 6976
a2ce51a0
DE
6977 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6978 file (instead of going through the stub), short-circuit all of this. */
6979 if (this_cu->reading_dwo_directly)
6980 {
6981 /* Narrow down the scope of possibilities to have to understand. */
6982 gdb_assert (this_cu->is_debug_types);
6983 gdb_assert (abbrev_table == NULL);
9e021579 6984 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6985 return;
6986 }
6987
dee91e82 6988 /* This is cheap if the section is already read in. */
96b79293 6989 section->read (objfile);
dee91e82 6990
9c541725 6991 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6992
6993 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6994
6995 if (use_existing_cu && this_cu->cu != NULL)
6996 {
6997 cu = this_cu->cu;
42e7ad6c
DE
6998 /* If this CU is from a DWO file we need to start over, we need to
6999 refetch the attributes from the skeleton CU.
7000 This could be optimized by retrieving those attributes from when we
7001 were here the first time: the previous comp_unit_die was stored in
7002 comp_unit_obstack. But there's no data yet that we need this
7003 optimization. */
7004 if (cu->dwo_unit != NULL)
7005 rereading_dwo_cu = 1;
dee91e82
DE
7006 }
7007 else
7008 {
7009 /* If !use_existing_cu, this_cu->cu must be NULL. */
7010 gdb_assert (this_cu->cu == NULL);
9e021579 7011 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7012 cu = m_new_cu.get ();
42e7ad6c 7013 }
dee91e82 7014
b0c7bfa9 7015 /* Get the header. */
9c541725 7016 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7017 {
7018 /* We already have the header, there's no need to read it in again. */
9c541725 7019 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7020 }
7021 else
7022 {
3019eac3 7023 if (this_cu->is_debug_types)
dee91e82 7024 {
ed2dc618
SM
7025 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7026 &cu->header, section,
4bdcc0c1 7027 abbrev_section, info_ptr,
43988095 7028 rcuh_kind::TYPE);
dee91e82 7029
42e7ad6c
DE
7030 /* Since per_cu is the first member of struct signatured_type,
7031 we can go from a pointer to one to a pointer to the other. */
7032 sig_type = (struct signatured_type *) this_cu;
43988095 7033 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7034 gdb_assert (sig_type->type_offset_in_tu
7035 == cu->header.type_cu_offset_in_tu);
7036 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7037
42e7ad6c
DE
7038 /* LENGTH has not been set yet for type units if we're
7039 using .gdb_index. */
4057dfde 7040 this_cu->length = cu->header.get_length ();
3019eac3
DE
7041
7042 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7043 sig_type->type_offset_in_section =
7044 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7045
7046 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7047 }
7048 else
7049 {
ed2dc618
SM
7050 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7051 &cu->header, section,
4bdcc0c1 7052 abbrev_section,
43988095
JK
7053 info_ptr,
7054 rcuh_kind::COMPILE);
dee91e82 7055
9c541725 7056 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7057 if (this_cu->length == 0)
7058 this_cu->length = cu->header.get_length ();
7059 else
7060 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7061 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7062 }
7063 }
10b3939b 7064
6caca83c 7065 /* Skip dummy compilation units. */
dee91e82 7066 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7067 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7068 {
7069 dummy_p = true;
7070 return;
7071 }
6caca83c 7072
433df2d4
DE
7073 /* If we don't have them yet, read the abbrevs for this compilation unit.
7074 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7075 done. */
f4dc4d17 7076 if (abbrev_table != NULL)
685af9cd
TT
7077 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7078 else
f4dc4d17 7079 {
c0ab21c2 7080 m_abbrev_table_holder
86de1d91
TT
7081 = abbrev_table::read (objfile, abbrev_section,
7082 cu->header.abbrev_sect_off);
c0ab21c2 7083 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7084 }
af703f96 7085
dee91e82 7086 /* Read the top level CU/TU die. */
c0ab21c2 7087 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7088 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7089
58f0c718 7090 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7091 {
7092 dummy_p = true;
7093 return;
7094 }
58f0c718 7095
b0c7bfa9 7096 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7097 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7098 table from the DWO file and pass the ownership over to us. It will be
7099 referenced from READER, so we must make sure to free it after we're done
7100 with READER.
7101
b0c7bfa9
DE
7102 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7103 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7104 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7105 if (dwo_name != nullptr)
3019eac3 7106 {
3019eac3 7107 struct dwo_unit *dwo_unit;
b0c7bfa9 7108 struct die_info *dwo_comp_unit_die;
3019eac3 7109
3e225074 7110 if (comp_unit_die->has_children)
6a506a2d 7111 {
b98664d3 7112 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7113 " has children (offset %s) [in module %s]"),
7114 sect_offset_str (this_cu->sect_off),
7115 bfd_get_filename (abfd));
6a506a2d 7116 }
4ab09049 7117 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7118 if (dwo_unit != NULL)
3019eac3 7119 {
4ab09049 7120 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7121 comp_unit_die, NULL,
c0ab21c2 7122 this, &info_ptr,
3e225074 7123 &dwo_comp_unit_die,
c0ab21c2 7124 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7125 {
7126 /* Dummy die. */
c0ab21c2 7127 dummy_p = true;
6a506a2d
DE
7128 return;
7129 }
7130 comp_unit_die = dwo_comp_unit_die;
7131 }
7132 else
7133 {
7134 /* Yikes, we couldn't find the rest of the DIE, we only have
7135 the stub. A complaint has already been logged. There's
7136 not much more we can do except pass on the stub DIE to
7137 die_reader_func. We don't want to throw an error on bad
7138 debug info. */
3019eac3
DE
7139 }
7140 }
c0ab21c2 7141}
3019eac3 7142
6751ebae
TT
7143void
7144cutu_reader::keep ()
c0ab21c2 7145{
b0c7bfa9 7146 /* Done, clean up. */
6751ebae
TT
7147 gdb_assert (!dummy_p);
7148 if (m_new_cu != NULL)
348e048f 7149 {
e3beb21d
SM
7150 /* We know that m_this_cu->cu is set, since we are in the process of
7151 parsing the CU. */
7152 gdb_assert (m_this_cu->cu != nullptr);
7153 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7154
fcd3b13d 7155 /* Link this CU into read_in_chain. */
5989a64e
SM
7156 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7157 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7158 /* The chain owns it now. */
c0ab21c2 7159 m_new_cu.release ();
348e048f 7160 }
dee91e82
DE
7161}
7162
18a8505e
AT
7163/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7164 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7165 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7166
7167 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7168 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7169
7170 We fill in THIS_CU->length.
7171
dee91e82 7172 THIS_CU->cu is always freed when done.
3019eac3 7173 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7174 to care whether it refers to the "main" CU or the DWO CU.
7175
7176 When parent_cu is passed, it is used to provide a default value for
7177 str_offsets_base and addr_base from the parent. */
dee91e82 7178
ab432490
SM
7179cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7180 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7181 struct dwarf2_cu *parent_cu,
7182 struct dwo_file *dwo_file)
7183 : die_reader_specs {},
7184 m_this_cu (this_cu)
dee91e82
DE
7185{
7186 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7187 struct dwarf2_section_info *section = this_cu->section;
96b79293 7188 bfd *abfd = section->get_bfd_owner ();
33e80786 7189 struct dwarf2_section_info *abbrev_section;
d521ce57 7190 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7191
b4f54984 7192 if (dwarf_die_debug)
9d8780f0 7193 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7194 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7195 sect_offset_str (this_cu->sect_off));
09406207 7196
dee91e82
DE
7197 gdb_assert (this_cu->cu == NULL);
7198
33e80786
DE
7199 abbrev_section = (dwo_file != NULL
7200 ? &dwo_file->sections.abbrev
7201 : get_abbrev_section_for_cu (this_cu));
7202
dee91e82 7203 /* This is cheap if the section is already read in. */
96b79293 7204 section->read (objfile);
dee91e82 7205
9e021579 7206 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7207
9c541725 7208 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7209 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7210 &m_new_cu->header, section,
4bdcc0c1 7211 abbrev_section, info_ptr,
43988095
JK
7212 (this_cu->is_debug_types
7213 ? rcuh_kind::TYPE
7214 : rcuh_kind::COMPILE));
dee91e82 7215
18a8505e
AT
7216 if (parent_cu != nullptr)
7217 {
c0ab21c2
TT
7218 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7219 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7220 }
4057dfde 7221 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7222
7223 /* Skip dummy compilation units. */
7224 if (info_ptr >= begin_info_ptr + this_cu->length
7225 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7226 {
7227 dummy_p = true;
7228 return;
7229 }
72bf9492 7230
c0ab21c2 7231 m_abbrev_table_holder
86de1d91
TT
7232 = abbrev_table::read (objfile, abbrev_section,
7233 m_new_cu->header.abbrev_sect_off);
dee91e82 7234
c0ab21c2
TT
7235 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7236 m_abbrev_table_holder.get ());
3e225074 7237 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7238}
7239
0018ea6f
DE
7240\f
7241/* Type Unit Groups.
dee91e82 7242
0018ea6f
DE
7243 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7244 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7245 so that all types coming from the same compilation (.o file) are grouped
7246 together. A future step could be to put the types in the same symtab as
7247 the CU the types ultimately came from. */
ff013f42 7248
f4dc4d17
DE
7249static hashval_t
7250hash_type_unit_group (const void *item)
7251{
9a3c8263
SM
7252 const struct type_unit_group *tu_group
7253 = (const struct type_unit_group *) item;
f4dc4d17 7254
094b34ac 7255 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7256}
348e048f
DE
7257
7258static int
f4dc4d17 7259eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7260{
9a3c8263
SM
7261 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7262 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7263
094b34ac 7264 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7265}
348e048f 7266
f4dc4d17
DE
7267/* Allocate a hash table for type unit groups. */
7268
eaa5fa8b 7269static htab_up
298e9637 7270allocate_type_unit_groups_table ()
f4dc4d17 7271{
eaa5fa8b
TT
7272 return htab_up (htab_create_alloc (3,
7273 hash_type_unit_group,
7274 eq_type_unit_group,
7275 NULL, xcalloc, xfree));
f4dc4d17 7276}
dee91e82 7277
f4dc4d17
DE
7278/* Type units that don't have DW_AT_stmt_list are grouped into their own
7279 partial symtabs. We combine several TUs per psymtab to not let the size
7280 of any one psymtab grow too big. */
7281#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7282#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7283
094b34ac 7284/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7285 Create the type_unit_group object used to hold one or more TUs. */
7286
7287static struct type_unit_group *
094b34ac 7288create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7289{
5e22e966 7290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7291 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7292 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7293 struct type_unit_group *tu_group;
f4dc4d17 7294
5989a64e 7295 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7296 struct type_unit_group);
094b34ac 7297 per_cu = &tu_group->per_cu;
1859c670 7298 per_cu->per_bfd = per_bfd;
f4dc4d17 7299
1859c670 7300 if (per_bfd->using_index)
094b34ac 7301 {
1859c670 7302 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7303 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7304 }
7305 else
7306 {
9c541725 7307 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7308 dwarf2_psymtab *pst;
528e1572 7309 std::string name;
094b34ac
DE
7310
7311 /* Give the symtab a useful name for debug purposes. */
7312 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7313 name = string_printf ("<type_units_%d>",
7314 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7315 else
528e1572 7316 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7317
7aa104c4 7318 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7319 pst->anonymous = true;
094b34ac 7320 }
f4dc4d17 7321
094b34ac 7322 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7323 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7324
7325 return tu_group;
7326}
7327
094b34ac
DE
7328/* Look up the type_unit_group for type unit CU, and create it if necessary.
7329 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7330
7331static struct type_unit_group *
ff39bb5e 7332get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7333{
5e22e966 7334 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7335 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7336 struct type_unit_group *tu_group;
7337 void **slot;
7338 unsigned int line_offset;
7339 struct type_unit_group type_unit_group_for_lookup;
7340
5989a64e
SM
7341 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7342 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7343
7344 /* Do we need to create a new group, or can we use an existing one? */
7345
7346 if (stmt_list)
7347 {
7348 line_offset = DW_UNSND (stmt_list);
7349 ++tu_stats->nr_symtab_sharers;
7350 }
7351 else
7352 {
7353 /* Ugh, no stmt_list. Rare, but we have to handle it.
7354 We can do various things here like create one group per TU or
7355 spread them over multiple groups to split up the expansion work.
7356 To avoid worst case scenarios (too many groups or too large groups)
7357 we, umm, group them in bunches. */
7358 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7359 | (tu_stats->nr_stmt_less_type_units
7360 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7361 ++tu_stats->nr_stmt_less_type_units;
7362 }
7363
094b34ac 7364 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7365 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7366 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7367 &type_unit_group_for_lookup, INSERT);
7368 if (*slot != NULL)
7369 {
9a3c8263 7370 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7371 gdb_assert (tu_group != NULL);
7372 }
7373 else
7374 {
9c541725 7375 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7376 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7377 *slot = tu_group;
7378 ++tu_stats->nr_symtabs;
7379 }
7380
7381 return tu_group;
7382}
0018ea6f
DE
7383\f
7384/* Partial symbol tables. */
7385
7386/* Create a psymtab named NAME and assign it to PER_CU.
7387
7388 The caller must fill in the following details:
7389 dirname, textlow, texthigh. */
7390
891813be 7391static dwarf2_psymtab *
7aa104c4
SM
7392create_partial_symtab (dwarf2_per_cu_data *per_cu,
7393 dwarf2_per_objfile *per_objfile,
7394 const char *name)
0018ea6f 7395{
7aa104c4 7396 struct objfile *objfile = per_objfile->objfile;
891813be 7397 dwarf2_psymtab *pst;
0018ea6f 7398
9f4e76a4 7399 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7400
6d94535f 7401 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7402
7403 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7404 per_cu->v.psymtab = pst;
7405
7406 return pst;
7407}
7408
c0ab21c2 7409/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7410
7411static void
7412process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7413 const gdb_byte *info_ptr,
0018ea6f 7414 struct die_info *comp_unit_die,
c0ab21c2 7415 enum language pretend_language)
0018ea6f
DE
7416{
7417 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7419 struct objfile *objfile = per_objfile->objfile;
08feed99 7420 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7421 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7422 CORE_ADDR baseaddr;
7423 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7424 dwarf2_psymtab *pst;
3a2b436a 7425 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7426 const char *filename;
0018ea6f 7427
0018ea6f
DE
7428 gdb_assert (! per_cu->is_debug_types);
7429
c0ab21c2 7430 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7431
0018ea6f 7432 /* Allocate a new partial symbol table structure. */
2e927613
TV
7433 gdb::unique_xmalloc_ptr<char> debug_filename;
7434 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7435 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7436 if (filename == NULL)
0018ea6f 7437 filename = "";
2e927613
TV
7438 else if (strcmp (filename, artificial) == 0)
7439 {
7440 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7441 sect_offset_str (per_cu->sect_off),
7442 (char *) NULL));
2e927613
TV
7443 filename = debug_filename.get ();
7444 }
0018ea6f 7445
7aa104c4 7446 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7447
7448 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7449 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7450
b3b3bada 7451 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7452
7453 dwarf2_find_base_address (comp_unit_die, cu);
7454
7455 /* Possibly set the default values of LOWPC and HIGHPC from
7456 `DW_AT_ranges'. */
3a2b436a
JK
7457 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7458 &best_highpc, cu, pst);
7459 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7460 {
7461 CORE_ADDR low
7462 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7463 - baseaddr);
7464 CORE_ADDR high
7465 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7466 - baseaddr - 1);
7467 /* Store the contiguous range if it is not empty; it can be
7468 empty for CUs with no code. */
d320c2b5
TT
7469 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7470 low, high, pst);
79748972 7471 }
0018ea6f
DE
7472
7473 /* Check if comp unit has_children.
7474 If so, read the rest of the partial symbols from this comp unit.
7475 If not, there's no more debug_info for this comp unit. */
3e225074 7476 if (comp_unit_die->has_children)
0018ea6f
DE
7477 {
7478 struct partial_die_info *first_die;
7479 CORE_ADDR lowpc, highpc;
7480
7481 lowpc = ((CORE_ADDR) -1);
7482 highpc = ((CORE_ADDR) 0);
7483
7484 first_die = load_partial_dies (reader, info_ptr, 1);
7485
7486 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7487 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7488
7489 /* If we didn't find a lowpc, set it to highpc to avoid
7490 complaints from `maint check'. */
7491 if (lowpc == ((CORE_ADDR) -1))
7492 lowpc = highpc;
7493
7494 /* If the compilation unit didn't have an explicit address range,
7495 then use the information extracted from its child dies. */
e385593e 7496 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7497 {
7498 best_lowpc = lowpc;
7499 best_highpc = highpc;
7500 }
7501 }
4ae976d1 7502 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7503 best_lowpc + baseaddr)
7504 - baseaddr);
4ae976d1 7505 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7506 best_highpc + baseaddr)
7507 - baseaddr);
0018ea6f 7508
8763cede 7509 end_psymtab_common (objfile, pst);
0018ea6f 7510
ae640021 7511 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7512 {
7513 int i;
ae640021 7514 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7515
7516 /* Fill in 'dependencies' here; we fill in 'users' in a
7517 post-pass. */
7518 pst->number_of_dependencies = len;
a9342b62
TT
7519 pst->dependencies
7520 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7521 for (i = 0; i < len; ++i)
7522 {
7523 pst->dependencies[i]
7524 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7525 }
0018ea6f 7526
ae640021 7527 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7528 }
7529
7530 /* Get the list of files included in the current compilation unit,
7531 and build a psymtab for each of them. */
7532 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7533
b4f54984 7534 if (dwarf_read_debug)
b926417a
TT
7535 fprintf_unfiltered (gdb_stdlog,
7536 "Psymtab for %s unit @%s: %s - %s"
7537 ", %d global, %d static syms\n",
7538 per_cu->is_debug_types ? "type" : "comp",
7539 sect_offset_str (per_cu->sect_off),
7540 paddress (gdbarch, pst->text_low (objfile)),
7541 paddress (gdbarch, pst->text_high (objfile)),
7542 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7543}
7544
7545/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7546 Process compilation unit THIS_CU for a psymtab. */
7547
7548static void
ab432490
SM
7549process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7550 dwarf2_per_objfile *per_objfile,
135f5437 7551 bool want_partial_unit,
b93601f3 7552 enum language pretend_language)
0018ea6f
DE
7553{
7554 /* If this compilation unit was already read in, free the
7555 cached copy in order to read it in again. This is
7556 necessary because we skipped some symbols when we first
7557 read in the compilation unit (see load_partial_dies).
7558 This problem could be avoided, but the benefit is unclear. */
7559 if (this_cu->cu != NULL)
120ce1b5 7560 free_one_cached_comp_unit (this_cu, per_objfile);
0018ea6f 7561
ab432490 7562 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7563
58990295
TV
7564 switch (reader.comp_unit_die->tag)
7565 {
7566 case DW_TAG_compile_unit:
7567 this_cu->unit_type = DW_UT_compile;
7568 break;
7569 case DW_TAG_partial_unit:
7570 this_cu->unit_type = DW_UT_partial;
7571 break;
7572 default:
7573 abort ();
7574 }
7575
c0ab21c2 7576 if (reader.dummy_p)
f1902523 7577 {
c0ab21c2 7578 /* Nothing. */
f1902523 7579 }
c0ab21c2 7580 else if (this_cu->is_debug_types)
3e225074
TT
7581 build_type_psymtabs_reader (&reader, reader.info_ptr,
7582 reader.comp_unit_die);
135f5437
TT
7583 else if (want_partial_unit
7584 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7585 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7586 reader.comp_unit_die,
c0ab21c2 7587 pretend_language);
0018ea6f 7588
58990295
TV
7589 this_cu->lang = this_cu->cu->language;
7590
0018ea6f 7591 /* Age out any secondary CUs. */
f6e649dd 7592 age_cached_comp_units (per_objfile);
0018ea6f 7593}
f4dc4d17
DE
7594
7595/* Reader function for build_type_psymtabs. */
7596
7597static void
7598build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7599 const gdb_byte *info_ptr,
3e225074 7600 struct die_info *type_unit_die)
f4dc4d17 7601{
5e22e966 7602 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7603 struct objfile *objfile = dwarf2_per_objfile->objfile;
7604 struct dwarf2_cu *cu = reader->cu;
7605 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7606 struct signatured_type *sig_type;
f4dc4d17
DE
7607 struct type_unit_group *tu_group;
7608 struct attribute *attr;
7609 struct partial_die_info *first_die;
7610 CORE_ADDR lowpc, highpc;
891813be 7611 dwarf2_psymtab *pst;
f4dc4d17 7612
0186c6a7
DE
7613 gdb_assert (per_cu->is_debug_types);
7614 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7615
3e225074 7616 if (! type_unit_die->has_children)
f4dc4d17
DE
7617 return;
7618
052c8bb8 7619 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7620 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7621
df07e2c7 7622 if (tu_group->tus == nullptr)
a8b3b8e9 7623 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7624 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7625
7626 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7627 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7628 pst->anonymous = true;
f4dc4d17
DE
7629
7630 first_die = load_partial_dies (reader, info_ptr, 1);
7631
7632 lowpc = (CORE_ADDR) -1;
7633 highpc = (CORE_ADDR) 0;
7634 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7635
8763cede 7636 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7637}
7638
73051182
DE
7639/* Struct used to sort TUs by their abbreviation table offset. */
7640
7641struct tu_abbrev_offset
7642{
b2bdb8cf
SM
7643 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7644 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7645 {}
7646
7647 signatured_type *sig_type;
73051182
DE
7648 sect_offset abbrev_offset;
7649};
7650
484cf504 7651/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7652
484cf504
TT
7653static bool
7654sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7655 const struct tu_abbrev_offset &b)
73051182 7656{
484cf504 7657 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7658}
7659
7660/* Efficiently read all the type units.
7661 This does the bulk of the work for build_type_psymtabs.
7662
7663 The efficiency is because we sort TUs by the abbrev table they use and
7664 only read each abbrev table once. In one program there are 200K TUs
7665 sharing 8K abbrev tables.
7666
7667 The main purpose of this function is to support building the
5989a64e 7668 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7669 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7670 can collapse the search space by grouping them by stmt_list.
7671 The savings can be significant, in the same program from above the 200K TUs
7672 share 8K stmt_list tables.
7673
7674 FUNC is expected to call get_type_unit_group, which will create the
7675 struct type_unit_group if necessary and add it to
5989a64e 7676 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7677
7678static void
ed2dc618 7679build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7680{
5989a64e 7681 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7682 abbrev_table_up abbrev_table;
73051182 7683 sect_offset abbrev_offset;
73051182
DE
7684
7685 /* It's up to the caller to not call us multiple times. */
5989a64e 7686 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7687
5989a64e 7688 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7689 return;
7690
7691 /* TUs typically share abbrev tables, and there can be way more TUs than
7692 abbrev tables. Sort by abbrev table to reduce the number of times we
7693 read each abbrev table in.
7694 Alternatives are to punt or to maintain a cache of abbrev tables.
7695 This is simpler and efficient enough for now.
7696
7697 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7698 symtab to use). Typically TUs with the same abbrev offset have the same
7699 stmt_list value too so in practice this should work well.
7700
7701 The basic algorithm here is:
7702
7703 sort TUs by abbrev table
7704 for each TU with same abbrev table:
7705 read abbrev table if first user
7706 read TU top level DIE
7707 [IWBN if DWO skeletons had DW_AT_stmt_list]
7708 call FUNC */
7709
b4f54984 7710 if (dwarf_read_debug)
73051182
DE
7711 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7712
7713 /* Sort in a separate table to maintain the order of all_type_units
7714 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7715 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7716 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7717
5989a64e 7718 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7719 sorted_by_abbrev.emplace_back
7720 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7721 sig_type->per_cu.section,
7722 sig_type->per_cu.sect_off));
73051182 7723
484cf504
TT
7724 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7725 sort_tu_by_abbrev_offset);
73051182 7726
9c541725 7727 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7728
b2bdb8cf 7729 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7730 {
73051182
DE
7731 /* Switch to the next abbrev table if necessary. */
7732 if (abbrev_table == NULL
b2bdb8cf 7733 || tu.abbrev_offset != abbrev_offset)
73051182 7734 {
b2bdb8cf 7735 abbrev_offset = tu.abbrev_offset;
73051182 7736 abbrev_table =
86de1d91 7737 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7738 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7739 abbrev_offset);
73051182
DE
7740 ++tu_stats->nr_uniq_abbrev_tables;
7741 }
7742
ab432490
SM
7743 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7744 abbrev_table.get (), 0, false);
c0ab21c2
TT
7745 if (!reader.dummy_p)
7746 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7747 reader.comp_unit_die);
73051182 7748 }
6aa5f3a6 7749}
73051182 7750
6aa5f3a6
DE
7751/* Print collected type unit statistics. */
7752
7753static void
ed2dc618 7754print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7755{
5989a64e 7756 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7757
7758 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7759 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7760 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7761 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7762 tu_stats->nr_uniq_abbrev_tables);
7763 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7764 tu_stats->nr_symtabs);
7765 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7766 tu_stats->nr_symtab_sharers);
7767 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7768 tu_stats->nr_stmt_less_type_units);
7769 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7770 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7771}
7772
f4dc4d17
DE
7773/* Traversal function for build_type_psymtabs. */
7774
7775static int
7776build_type_psymtab_dependencies (void **slot, void *info)
7777{
ed2dc618
SM
7778 struct dwarf2_per_objfile *dwarf2_per_objfile
7779 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7780 struct objfile *objfile = dwarf2_per_objfile->objfile;
7781 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7782 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7783 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7784 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7785 int i;
7786
7787 gdb_assert (len > 0);
197400e8 7788 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7789
7790 pst->number_of_dependencies = len;
a9342b62 7791 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7792 for (i = 0; i < len; ++i)
f4dc4d17 7793 {
df07e2c7 7794 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7795 gdb_assert (iter->per_cu.is_debug_types);
7796 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7797 iter->type_unit_group = tu_group;
f4dc4d17
DE
7798 }
7799
df07e2c7
AB
7800 delete tu_group->tus;
7801 tu_group->tus = nullptr;
348e048f
DE
7802
7803 return 1;
7804}
7805
7806/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7807 Build partial symbol tables for the .debug_types comp-units. */
7808
7809static void
ed2dc618 7810build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7811{
ed2dc618 7812 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7813 return;
7814
ed2dc618 7815 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7816}
f4dc4d17 7817
6aa5f3a6
DE
7818/* Traversal function for process_skeletonless_type_unit.
7819 Read a TU in a DWO file and build partial symbols for it. */
7820
7821static int
7822process_skeletonless_type_unit (void **slot, void *info)
7823{
7824 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7825 struct dwarf2_per_objfile *dwarf2_per_objfile
7826 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7827 struct signatured_type find_entry, *entry;
7828
7829 /* If this TU doesn't exist in the global table, add it and read it in. */
7830
5989a64e
SM
7831 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7832 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7833
7834 find_entry.signature = dwo_unit->signature;
5989a64e 7835 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7836 &find_entry, INSERT);
6aa5f3a6
DE
7837 /* If we've already seen this type there's nothing to do. What's happening
7838 is we're doing our own version of comdat-folding here. */
7839 if (*slot != NULL)
7840 return 1;
7841
7842 /* This does the job that create_all_type_units would have done for
7843 this TU. */
ed2dc618
SM
7844 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7845 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7846 *slot = entry;
7847
7848 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7849 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7850 if (!reader.dummy_p)
7851 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7852 reader.comp_unit_die);
6aa5f3a6
DE
7853
7854 return 1;
7855}
7856
7857/* Traversal function for process_skeletonless_type_units. */
7858
7859static int
7860process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7861{
7862 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7863
7864 if (dwo_file->tus != NULL)
b0b6a987
TT
7865 htab_traverse_noresize (dwo_file->tus.get (),
7866 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7867
7868 return 1;
7869}
7870
7871/* Scan all TUs of DWO files, verifying we've processed them.
7872 This is needed in case a TU was emitted without its skeleton.
7873 Note: This can't be done until we know what all the DWO files are. */
7874
7875static void
ed2dc618 7876process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7877{
7878 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7879 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7880 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7881 {
5989a64e 7882 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7883 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7884 dwarf2_per_objfile);
6aa5f3a6 7885 }
348e048f
DE
7886}
7887
ed2dc618 7888/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7889
7890static void
ed2dc618 7891set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7892{
5989a64e 7893 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7894 {
891813be 7895 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7896
36586728
TT
7897 if (pst == NULL)
7898 continue;
7899
b76e467d 7900 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7901 {
7902 /* Set the 'user' field only if it is not already set. */
7903 if (pst->dependencies[j]->user == NULL)
7904 pst->dependencies[j]->user = pst;
7905 }
7906 }
7907}
7908
93311388
DE
7909/* Build the partial symbol table by doing a quick pass through the
7910 .debug_info and .debug_abbrev sections. */
72bf9492 7911
93311388 7912static void
ed2dc618 7913dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7914{
ed2dc618 7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7916
b4f54984 7917 if (dwarf_read_debug)
45cfd468
DE
7918 {
7919 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7920 objfile_name (objfile));
45cfd468
DE
7921 }
7922
76935768 7923 scoped_restore restore_reading_psyms
5989a64e 7924 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7925 true);
98bfdba5 7926
5989a64e 7927 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7928
93311388
DE
7929 /* Any cached compilation units will be linked by the per-objfile
7930 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7931 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7932
ed2dc618 7933 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7934
ed2dc618 7935 create_all_comp_units (dwarf2_per_objfile);
c906108c 7936
60606b2c
TT
7937 /* Create a temporary address map on a temporary obstack. We later
7938 copy this to the final obstack. */
8268c778 7939 auto_obstack temp_obstack;
791afaa2
TT
7940
7941 scoped_restore save_psymtabs_addrmap
d320c2b5 7942 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7943 addrmap_create_mutable (&temp_obstack));
72bf9492 7944
5989a64e 7945 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7946 {
7947 if (per_cu->v.psymtab != NULL)
7948 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7949 continue;
ab432490
SM
7950 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7951 language_minimal);
3d5afab3 7952 }
ff013f42 7953
6aa5f3a6 7954 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7955 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7956
7957 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7958 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7959 {
5989a64e 7960 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7961 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7962 }
7963
b4f54984 7964 if (dwarf_read_debug)
ed2dc618 7965 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7966
ed2dc618 7967 set_partial_user (dwarf2_per_objfile);
95554aad 7968
d320c2b5
TT
7969 objfile->partial_symtabs->psymtabs_addrmap
7970 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7971 objfile->partial_symtabs->obstack ());
791afaa2
TT
7972 /* At this point we want to keep the address map. */
7973 save_psymtabs_addrmap.release ();
ff013f42 7974
b4f54984 7975 if (dwarf_read_debug)
45cfd468 7976 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7977 objfile_name (objfile));
ae038cb0
DJ
7978}
7979
dee91e82
DE
7980/* Load the partial DIEs for a secondary CU into memory.
7981 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7982
dee91e82 7983static void
ab432490
SM
7984load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7985 dwarf2_per_objfile *per_objfile)
dee91e82 7986{
ab432490 7987 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
7988
7989 if (!reader.dummy_p)
7990 {
7991 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7992 language_minimal);
7993
7994 /* Check if comp unit has_children.
7995 If so, read the rest of the partial symbols from this comp unit.
7996 If not, there's no more debug_info for this comp unit. */
3e225074 7997 if (reader.comp_unit_die->has_children)
c0ab21c2 7998 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7999
8000 reader.keep ();
c0ab21c2 8001 }
ae038cb0
DJ
8002}
8003
ae038cb0 8004static void
ed2dc618 8005read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8006 struct dwarf2_section_info *section,
f1902523 8007 struct dwarf2_section_info *abbrev_section,
b76e467d 8008 unsigned int is_dwz)
ae038cb0 8009{
d521ce57 8010 const gdb_byte *info_ptr;
ed2dc618 8011 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8012
b4f54984 8013 if (dwarf_read_debug)
bf6af496 8014 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8015 section->get_name (),
8016 section->get_file_name ());
bf6af496 8017
96b79293 8018 section->read (objfile);
ae038cb0 8019
36586728 8020 info_ptr = section->buffer;
6e70227d 8021
36586728 8022 while (info_ptr < section->buffer + section->size)
ae038cb0 8023 {
ae038cb0 8024 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8025
9c541725 8026 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8027
f1902523 8028 comp_unit_head cu_header;
ed2dc618
SM
8029 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8030 abbrev_section, info_ptr,
8031 rcuh_kind::COMPILE);
ae038cb0
DJ
8032
8033 /* Save the compilation unit for later lookup. */
f1902523 8034 if (cu_header.unit_type != DW_UT_type)
5989a64e 8035 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8036 else
8037 {
5989a64e 8038 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8039 sig_type->signature = cu_header.signature;
8040 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8041 this_cu = &sig_type->per_cu;
8042 }
8043 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8044 this_cu->sect_off = sect_off;
f1902523 8045 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8046 this_cu->is_dwz = is_dwz;
8a0459fd 8047 this_cu->section = section;
ae038cb0 8048
5989a64e 8049 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8050
8051 info_ptr = info_ptr + this_cu->length;
8052 }
36586728
TT
8053}
8054
8055/* Create a list of all compilation units in OBJFILE.
8056 This is only done for -readnow and building partial symtabs. */
8057
8058static void
ed2dc618 8059create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8060{
5989a64e
SM
8061 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8062 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8063 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8064
c3699833 8065 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8066 if (dwz != NULL)
ed2dc618 8067 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8068 1);
c906108c
SS
8069}
8070
5734ee8b 8071/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8072 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8073 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8074 DW_AT_ranges). See the comments of add_partial_subprogram on how
8075 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8076
72bf9492
DJ
8077static void
8078scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8079 CORE_ADDR *highpc, int set_addrmap,
8080 struct dwarf2_cu *cu)
c906108c 8081{
72bf9492 8082 struct partial_die_info *pdi;
c906108c 8083
91c24f0a
DC
8084 /* Now, march along the PDI's, descending into ones which have
8085 interesting children but skipping the children of the other ones,
8086 until we reach the end of the compilation unit. */
c906108c 8087
72bf9492 8088 pdi = first_die;
91c24f0a 8089
72bf9492
DJ
8090 while (pdi != NULL)
8091 {
52356b79 8092 pdi->fixup (cu);
c906108c 8093
f55ee35c 8094 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8095 children, so we need to look at them. Ditto for anonymous
8096 enums. */
933c6fe4 8097
72bf9492 8098 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8099 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8100 || pdi->tag == DW_TAG_imported_unit
8101 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8102 {
72bf9492 8103 switch (pdi->tag)
c906108c
SS
8104 {
8105 case DW_TAG_subprogram:
b1dc1806 8106 case DW_TAG_inlined_subroutine:
cdc07690 8107 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8108 break;
72929c62 8109 case DW_TAG_constant:
c906108c
SS
8110 case DW_TAG_variable:
8111 case DW_TAG_typedef:
91c24f0a 8112 case DW_TAG_union_type:
317d2668
TV
8113 if (!pdi->is_declaration
8114 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8115 {
72bf9492 8116 add_partial_symbol (pdi, cu);
63d06c5c
DC
8117 }
8118 break;
c906108c 8119 case DW_TAG_class_type:
680b30c7 8120 case DW_TAG_interface_type:
c906108c 8121 case DW_TAG_structure_type:
72bf9492 8122 if (!pdi->is_declaration)
c906108c 8123 {
72bf9492 8124 add_partial_symbol (pdi, cu);
c906108c 8125 }
b7fee5a3
KS
8126 if ((cu->language == language_rust
8127 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8128 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8129 set_addrmap, cu);
c906108c 8130 break;
91c24f0a 8131 case DW_TAG_enumeration_type:
72bf9492
DJ
8132 if (!pdi->is_declaration)
8133 add_partial_enumeration (pdi, cu);
c906108c
SS
8134 break;
8135 case DW_TAG_base_type:
a02abb62 8136 case DW_TAG_subrange_type:
c906108c 8137 /* File scope base type definitions are added to the partial
c5aa993b 8138 symbol table. */
72bf9492 8139 add_partial_symbol (pdi, cu);
c906108c 8140 break;
d9fa45fe 8141 case DW_TAG_namespace:
cdc07690 8142 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8143 break;
5d7cb8df 8144 case DW_TAG_module:
59c35742
AB
8145 if (!pdi->is_declaration)
8146 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8147 break;
95554aad
TT
8148 case DW_TAG_imported_unit:
8149 {
8150 struct dwarf2_per_cu_data *per_cu;
8151
f4dc4d17
DE
8152 /* For now we don't handle imported units in type units. */
8153 if (cu->per_cu->is_debug_types)
8154 {
8155 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8156 " supported in type units [in module %s]"),
5e22e966 8157 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8158 }
8159
e3b94546 8160 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8161 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8162
8163 /* Go read the partial unit, if needed. */
8164 if (per_cu->v.psymtab == NULL)
ab432490
SM
8165 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8166 cu->language);
95554aad 8167
ae640021 8168 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8169 }
8170 break;
74921315
KS
8171 case DW_TAG_imported_declaration:
8172 add_partial_symbol (pdi, cu);
8173 break;
c906108c
SS
8174 default:
8175 break;
8176 }
8177 }
8178
72bf9492
DJ
8179 /* If the die has a sibling, skip to the sibling. */
8180
8181 pdi = pdi->die_sibling;
8182 }
8183}
8184
8185/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8186
72bf9492 8187 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8188 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8189 Enumerators are an exception; they use the scope of their parent
8190 enumeration type, i.e. the name of the enumeration type is not
8191 prepended to the enumerator.
91c24f0a 8192
72bf9492
DJ
8193 There are two complexities. One is DW_AT_specification; in this
8194 case "parent" means the parent of the target of the specification,
8195 instead of the direct parent of the DIE. The other is compilers
8196 which do not emit DW_TAG_namespace; in this case we try to guess
8197 the fully qualified name of structure types from their members'
8198 linkage names. This must be done using the DIE's children rather
8199 than the children of any DW_AT_specification target. We only need
8200 to do this for structures at the top level, i.e. if the target of
8201 any DW_AT_specification (if any; otherwise the DIE itself) does not
8202 have a parent. */
8203
8204/* Compute the scope prefix associated with PDI's parent, in
8205 compilation unit CU. The result will be allocated on CU's
8206 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8207 field. NULL is returned if no prefix is necessary. */
15d034d0 8208static const char *
72bf9492
DJ
8209partial_die_parent_scope (struct partial_die_info *pdi,
8210 struct dwarf2_cu *cu)
8211{
15d034d0 8212 const char *grandparent_scope;
72bf9492 8213 struct partial_die_info *parent, *real_pdi;
91c24f0a 8214
72bf9492
DJ
8215 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8216 then this means the parent of the specification DIE. */
8217
8218 real_pdi = pdi;
72bf9492 8219 while (real_pdi->has_specification)
fb816e8b 8220 {
122cf0f2
AB
8221 auto res = find_partial_die (real_pdi->spec_offset,
8222 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8223 real_pdi = res.pdi;
8224 cu = res.cu;
8225 }
72bf9492
DJ
8226
8227 parent = real_pdi->die_parent;
8228 if (parent == NULL)
8229 return NULL;
8230
8231 if (parent->scope_set)
8232 return parent->scope;
8233
52356b79 8234 parent->fixup (cu);
72bf9492 8235
10b3939b 8236 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8237
acebe513
UW
8238 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8239 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8240 Work around this problem here. */
8241 if (cu->language == language_cplus
6e70227d 8242 && parent->tag == DW_TAG_namespace
acebe513
UW
8243 && strcmp (parent->name, "::") == 0
8244 && grandparent_scope == NULL)
8245 {
8246 parent->scope = NULL;
8247 parent->scope_set = 1;
8248 return NULL;
8249 }
8250
0a4b0913 8251 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8252 if (pdi->tag == DW_TAG_enumerator)
8253 /* Enumerators should not get the name of the enumeration as a prefix. */
8254 parent->scope = grandparent_scope;
8255 else if (parent->tag == DW_TAG_namespace
f55ee35c 8256 || parent->tag == DW_TAG_module
72bf9492
DJ
8257 || parent->tag == DW_TAG_structure_type
8258 || parent->tag == DW_TAG_class_type
680b30c7 8259 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8260 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8261 || parent->tag == DW_TAG_enumeration_type
8262 || (cu->language == language_fortran
8263 && parent->tag == DW_TAG_subprogram
8264 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8265 {
8266 if (grandparent_scope == NULL)
8267 parent->scope = parent->name;
8268 else
3e43a32a
MS
8269 parent->scope = typename_concat (&cu->comp_unit_obstack,
8270 grandparent_scope,
f55ee35c 8271 parent->name, 0, cu);
72bf9492 8272 }
72bf9492
DJ
8273 else
8274 {
8275 /* FIXME drow/2004-04-01: What should we be doing with
8276 function-local names? For partial symbols, we should probably be
8277 ignoring them. */
fa9c3fa0
TT
8278 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8279 dwarf_tag_name (parent->tag),
8280 sect_offset_str (pdi->sect_off));
72bf9492 8281 parent->scope = grandparent_scope;
c906108c
SS
8282 }
8283
72bf9492
DJ
8284 parent->scope_set = 1;
8285 return parent->scope;
8286}
8287
8288/* Return the fully scoped name associated with PDI, from compilation unit
8289 CU. The result will be allocated with malloc. */
4568ecf9 8290
43816ebc 8291static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8292partial_die_full_name (struct partial_die_info *pdi,
8293 struct dwarf2_cu *cu)
8294{
15d034d0 8295 const char *parent_scope;
72bf9492 8296
98bfdba5
PA
8297 /* If this is a template instantiation, we can not work out the
8298 template arguments from partial DIEs. So, unfortunately, we have
8299 to go through the full DIEs. At least any work we do building
8300 types here will be reused if full symbols are loaded later. */
8301 if (pdi->has_template_arguments)
8302 {
52356b79 8303 pdi->fixup (cu);
98bfdba5
PA
8304
8305 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8306 {
8307 struct die_info *die;
8308 struct attribute attr;
8309 struct dwarf2_cu *ref_cu = cu;
8310
b64f50a1 8311 /* DW_FORM_ref_addr is using section offset. */
b4069958 8312 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8313 attr.form = DW_FORM_ref_addr;
9c541725 8314 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8315 die = follow_die_ref (NULL, &attr, &ref_cu);
8316
43816ebc 8317 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8318 }
8319 }
8320
72bf9492
DJ
8321 parent_scope = partial_die_parent_scope (pdi, cu);
8322 if (parent_scope == NULL)
8323 return NULL;
8324 else
43816ebc
TT
8325 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8326 pdi->name, 0, cu));
c906108c
SS
8327}
8328
8329static void
72bf9492 8330add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8331{
5e22e966 8332 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8333 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8334 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8335 CORE_ADDR addr = 0;
15d034d0 8336 const char *actual_name = NULL;
e142c38c
DJ
8337 CORE_ADDR baseaddr;
8338
b3b3bada 8339 baseaddr = objfile->text_section_offset ();
c906108c 8340
43816ebc
TT
8341 gdb::unique_xmalloc_ptr<char> built_actual_name
8342 = partial_die_full_name (pdi, cu);
15d034d0 8343 if (built_actual_name != NULL)
43816ebc 8344 actual_name = built_actual_name.get ();
63d06c5c 8345
72bf9492
DJ
8346 if (actual_name == NULL)
8347 actual_name = pdi->name;
8348
76e288d1
TT
8349 partial_symbol psymbol;
8350 memset (&psymbol, 0, sizeof (psymbol));
8351 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8352 psymbol.ginfo.section = -1;
8353
8354 /* The code below indicates that the psymbol should be installed by
8355 setting this. */
8356 gdb::optional<psymbol_placement> where;
8357
c906108c
SS
8358 switch (pdi->tag)
8359 {
b1dc1806 8360 case DW_TAG_inlined_subroutine:
c906108c 8361 case DW_TAG_subprogram:
79748972
TT
8362 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8363 - baseaddr);
0a4b0913
AB
8364 if (pdi->is_external
8365 || cu->language == language_ada
8366 || (cu->language == language_fortran
8367 && pdi->die_parent != NULL
8368 && pdi->die_parent->tag == DW_TAG_subprogram))
8369 {
8370 /* Normally, only "external" DIEs are part of the global scope.
8371 But in Ada and Fortran, we want to be able to access nested
8372 procedures globally. So all Ada and Fortran subprograms are
8373 stored in the global scope. */
76e288d1 8374 where = psymbol_placement::GLOBAL;
c906108c
SS
8375 }
8376 else
76e288d1
TT
8377 where = psymbol_placement::STATIC;
8378
8379 psymbol.domain = VAR_DOMAIN;
8380 psymbol.aclass = LOC_BLOCK;
8381 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8382 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8383
8384 if (pdi->main_subprogram && actual_name != NULL)
8385 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8386 break;
72929c62 8387 case DW_TAG_constant:
76e288d1
TT
8388 psymbol.domain = VAR_DOMAIN;
8389 psymbol.aclass = LOC_STATIC;
8390 where = (pdi->is_external
8391 ? psymbol_placement::GLOBAL
8392 : psymbol_placement::STATIC);
72929c62 8393 break;
c906108c 8394 case DW_TAG_variable:
95554aad
TT
8395 if (pdi->d.locdesc)
8396 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8397
95554aad 8398 if (pdi->d.locdesc
caac4577 8399 && addr == 0
5989a64e 8400 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8401 {
8402 /* A global or static variable may also have been stripped
8403 out by the linker if unused, in which case its address
8404 will be nullified; do not add such variables into partial
8405 symbol table then. */
8406 }
8407 else if (pdi->is_external)
c906108c
SS
8408 {
8409 /* Global Variable.
8410 Don't enter into the minimal symbol tables as there is
8411 a minimal symbol table entry from the ELF symbols already.
8412 Enter into partial symbol table if it has a location
8413 descriptor or a type.
8414 If the location descriptor is missing, new_symbol will create
8415 a LOC_UNRESOLVED symbol, the address of the variable will then
8416 be determined from the minimal symbol table whenever the variable
8417 is referenced.
8418 The address for the partial symbol table entry is not
8419 used by GDB, but it comes in handy for debugging partial symbol
8420 table building. */
8421
95554aad 8422 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8423 {
8424 psymbol.domain = VAR_DOMAIN;
8425 psymbol.aclass = LOC_STATIC;
8426 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8427 psymbol.ginfo.value.address = addr;
8428 where = psymbol_placement::GLOBAL;
8429 }
c906108c
SS
8430 }
8431 else
8432 {
ff908ebf
AW
8433 int has_loc = pdi->d.locdesc != NULL;
8434
8435 /* Static Variable. Skip symbols whose value we cannot know (those
8436 without location descriptors or constant values). */
8437 if (!has_loc && !pdi->has_const_value)
43816ebc 8438 return;
ff908ebf 8439
76e288d1
TT
8440 psymbol.domain = VAR_DOMAIN;
8441 psymbol.aclass = LOC_STATIC;
8442 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8443 if (has_loc)
8444 psymbol.ginfo.value.address = addr;
8445 where = psymbol_placement::STATIC;
c906108c
SS
8446 }
8447 break;
8448 case DW_TAG_typedef:
8449 case DW_TAG_base_type:
a02abb62 8450 case DW_TAG_subrange_type:
76e288d1
TT
8451 psymbol.domain = VAR_DOMAIN;
8452 psymbol.aclass = LOC_TYPEDEF;
8453 where = psymbol_placement::STATIC;
c906108c 8454 break;
74921315 8455 case DW_TAG_imported_declaration:
72bf9492 8456 case DW_TAG_namespace:
76e288d1
TT
8457 psymbol.domain = VAR_DOMAIN;
8458 psymbol.aclass = LOC_TYPEDEF;
8459 where = psymbol_placement::GLOBAL;
72bf9492 8460 break;
530e8392 8461 case DW_TAG_module:
a5fd13a9
BH
8462 /* With Fortran 77 there might be a "BLOCK DATA" module
8463 available without any name. If so, we skip the module as it
8464 doesn't bring any value. */
8465 if (actual_name != nullptr)
76e288d1
TT
8466 {
8467 psymbol.domain = MODULE_DOMAIN;
8468 psymbol.aclass = LOC_TYPEDEF;
8469 where = psymbol_placement::GLOBAL;
8470 }
530e8392 8471 break;
c906108c 8472 case DW_TAG_class_type:
680b30c7 8473 case DW_TAG_interface_type:
c906108c
SS
8474 case DW_TAG_structure_type:
8475 case DW_TAG_union_type:
8476 case DW_TAG_enumeration_type:
fa4028e9
JB
8477 /* Skip external references. The DWARF standard says in the section
8478 about "Structure, Union, and Class Type Entries": "An incomplete
8479 structure, union or class type is represented by a structure,
8480 union or class entry that does not have a byte size attribute
8481 and that has a DW_AT_declaration attribute." */
8482 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8483 return;
fa4028e9 8484
63d06c5c
DC
8485 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8486 static vs. global. */
76e288d1
TT
8487 psymbol.domain = STRUCT_DOMAIN;
8488 psymbol.aclass = LOC_TYPEDEF;
8489 where = (cu->language == language_cplus
8490 ? psymbol_placement::GLOBAL
8491 : psymbol_placement::STATIC);
c906108c
SS
8492 break;
8493 case DW_TAG_enumerator:
76e288d1
TT
8494 psymbol.domain = VAR_DOMAIN;
8495 psymbol.aclass = LOC_CONST;
8496 where = (cu->language == language_cplus
8497 ? psymbol_placement::GLOBAL
8498 : psymbol_placement::STATIC);
c906108c
SS
8499 break;
8500 default:
8501 break;
8502 }
76e288d1
TT
8503
8504 if (where.has_value ())
8505 {
f049a313
TT
8506 if (built_actual_name != nullptr)
8507 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8508 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8509 psymbol.ginfo.set_linkage_name (actual_name);
8510 else
8511 {
8512 psymbol.ginfo.set_demangled_name (actual_name,
8513 &objfile->objfile_obstack);
8514 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8515 }
76e288d1
TT
8516 add_psymbol_to_list (psymbol, *where, objfile);
8517 }
c906108c
SS
8518}
8519
5c4e30ca
DC
8520/* Read a partial die corresponding to a namespace; also, add a symbol
8521 corresponding to that namespace to the symbol table. NAMESPACE is
8522 the name of the enclosing namespace. */
91c24f0a 8523
72bf9492
DJ
8524static void
8525add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8526 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8527 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8528{
72bf9492 8529 /* Add a symbol for the namespace. */
e7c27a73 8530
72bf9492 8531 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8532
8533 /* Now scan partial symbols in that namespace. */
8534
91c24f0a 8535 if (pdi->has_children)
cdc07690 8536 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8537}
8538
5d7cb8df
JK
8539/* Read a partial die corresponding to a Fortran module. */
8540
8541static void
8542add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8543 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8544{
530e8392
KB
8545 /* Add a symbol for the namespace. */
8546
8547 add_partial_symbol (pdi, cu);
8548
f55ee35c 8549 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8550
8551 if (pdi->has_children)
cdc07690 8552 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8553}
8554
b1dc1806
XR
8555/* Read a partial die corresponding to a subprogram or an inlined
8556 subprogram and create a partial symbol for that subprogram.
8557 When the CU language allows it, this routine also defines a partial
8558 symbol for each nested subprogram that this subprogram contains.
8559 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8560 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8561
cdc07690
YQ
8562 PDI may also be a lexical block, in which case we simply search
8563 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8564 Again, this is only performed when the CU language allows this
8565 type of definitions. */
8566
8567static void
8568add_partial_subprogram (struct partial_die_info *pdi,
8569 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8570 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8571{
b1dc1806 8572 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8573 {
8574 if (pdi->has_pc_info)
8575 {
8576 if (pdi->lowpc < *lowpc)
8577 *lowpc = pdi->lowpc;
8578 if (pdi->highpc > *highpc)
8579 *highpc = pdi->highpc;
cdc07690 8580 if (set_addrmap)
5734ee8b 8581 {
5e22e966 8582 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8583 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8584 CORE_ADDR baseaddr;
b926417a
TT
8585 CORE_ADDR this_highpc;
8586 CORE_ADDR this_lowpc;
5734ee8b 8587
b3b3bada 8588 baseaddr = objfile->text_section_offset ();
b926417a
TT
8589 this_lowpc
8590 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8591 pdi->lowpc + baseaddr)
8592 - baseaddr);
8593 this_highpc
8594 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8595 pdi->highpc + baseaddr)
8596 - baseaddr);
d320c2b5 8597 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8598 this_lowpc, this_highpc - 1,
9291a0cd 8599 cu->per_cu->v.psymtab);
5734ee8b 8600 }
481860b3
GB
8601 }
8602
8603 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8604 {
bc30ff58 8605 if (!pdi->is_declaration)
e8d05480
JB
8606 /* Ignore subprogram DIEs that do not have a name, they are
8607 illegal. Do not emit a complaint at this point, we will
8608 do so when we convert this psymtab into a symtab. */
8609 if (pdi->name)
8610 add_partial_symbol (pdi, cu);
bc30ff58
JB
8611 }
8612 }
6e70227d 8613
bc30ff58
JB
8614 if (! pdi->has_children)
8615 return;
8616
0a4b0913 8617 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8618 {
8619 pdi = pdi->die_child;
8620 while (pdi != NULL)
8621 {
52356b79 8622 pdi->fixup (cu);
bc30ff58 8623 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8624 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8625 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8626 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8627 pdi = pdi->die_sibling;
8628 }
8629 }
8630}
8631
91c24f0a
DC
8632/* Read a partial die corresponding to an enumeration type. */
8633
72bf9492
DJ
8634static void
8635add_partial_enumeration (struct partial_die_info *enum_pdi,
8636 struct dwarf2_cu *cu)
91c24f0a 8637{
72bf9492 8638 struct partial_die_info *pdi;
91c24f0a
DC
8639
8640 if (enum_pdi->name != NULL)
72bf9492
DJ
8641 add_partial_symbol (enum_pdi, cu);
8642
8643 pdi = enum_pdi->die_child;
8644 while (pdi)
91c24f0a 8645 {
72bf9492 8646 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8647 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8648 else
72bf9492
DJ
8649 add_partial_symbol (pdi, cu);
8650 pdi = pdi->die_sibling;
91c24f0a 8651 }
91c24f0a
DC
8652}
8653
6caca83c
CC
8654/* Return the initial uleb128 in the die at INFO_PTR. */
8655
8656static unsigned int
d521ce57 8657peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8658{
8659 unsigned int bytes_read;
8660
8661 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8662}
8663
685af9cd
TT
8664/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8665 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8666
4bb7a0a7
DJ
8667 Return the corresponding abbrev, or NULL if the number is zero (indicating
8668 an empty DIE). In either case *BYTES_READ will be set to the length of
8669 the initial number. */
8670
8671static struct abbrev_info *
685af9cd
TT
8672peek_die_abbrev (const die_reader_specs &reader,
8673 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8674{
685af9cd 8675 dwarf2_cu *cu = reader.cu;
5e22e966 8676 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8677 unsigned int abbrev_number
8678 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8679
8680 if (abbrev_number == 0)
8681 return NULL;
8682
685af9cd 8683 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8684 if (!abbrev)
8685 {
422b9917 8686 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8687 " at offset %s [in module %s]"),
422b9917 8688 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8689 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8690 }
8691
8692 return abbrev;
8693}
8694
93311388
DE
8695/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8696 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8697 DIE. Any children of the skipped DIEs will also be skipped. */
8698
d521ce57
TT
8699static const gdb_byte *
8700skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8701{
4bb7a0a7
DJ
8702 while (1)
8703 {
685af9cd
TT
8704 unsigned int bytes_read;
8705 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8706
4bb7a0a7
DJ
8707 if (abbrev == NULL)
8708 return info_ptr + bytes_read;
8709 else
dee91e82 8710 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8711 }
8712}
8713
93311388
DE
8714/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8715 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8716 abbrev corresponding to that skipped uleb128 should be passed in
8717 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8718 children. */
8719
d521ce57
TT
8720static const gdb_byte *
8721skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8722 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8723{
8724 unsigned int bytes_read;
8725 struct attribute attr;
dee91e82
DE
8726 bfd *abfd = reader->abfd;
8727 struct dwarf2_cu *cu = reader->cu;
d521ce57 8728 const gdb_byte *buffer = reader->buffer;
f664829e 8729 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8730 unsigned int form, i;
8731
8732 for (i = 0; i < abbrev->num_attrs; i++)
8733 {
8734 /* The only abbrev we care about is DW_AT_sibling. */
8735 if (abbrev->attrs[i].name == DW_AT_sibling)
8736 {
18a8505e
AT
8737 bool ignored;
8738 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8739 &ignored);
4bb7a0a7 8740 if (attr.form == DW_FORM_ref_addr)
b98664d3 8741 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8742 else
b9502d3f 8743 {
0826b30a 8744 sect_offset off = attr.get_ref_die_offset ();
9c541725 8745 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8746
8747 if (sibling_ptr < info_ptr)
b98664d3 8748 complaint (_("DW_AT_sibling points backwards"));
22869d73 8749 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8750 reader->die_section->overflow_complaint ();
b9502d3f
WN
8751 else
8752 return sibling_ptr;
8753 }
4bb7a0a7
DJ
8754 }
8755
8756 /* If it isn't DW_AT_sibling, skip this attribute. */
8757 form = abbrev->attrs[i].form;
8758 skip_attribute:
8759 switch (form)
8760 {
4bb7a0a7 8761 case DW_FORM_ref_addr:
ae411497
TT
8762 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8763 and later it is offset sized. */
8764 if (cu->header.version == 2)
8765 info_ptr += cu->header.addr_size;
8766 else
8767 info_ptr += cu->header.offset_size;
8768 break;
36586728
TT
8769 case DW_FORM_GNU_ref_alt:
8770 info_ptr += cu->header.offset_size;
8771 break;
ae411497 8772 case DW_FORM_addr:
4bb7a0a7
DJ
8773 info_ptr += cu->header.addr_size;
8774 break;
8775 case DW_FORM_data1:
8776 case DW_FORM_ref1:
8777 case DW_FORM_flag:
8fe0f950 8778 case DW_FORM_strx1:
4bb7a0a7
DJ
8779 info_ptr += 1;
8780 break;
2dc7f7b3 8781 case DW_FORM_flag_present:
43988095 8782 case DW_FORM_implicit_const:
2dc7f7b3 8783 break;
4bb7a0a7
DJ
8784 case DW_FORM_data2:
8785 case DW_FORM_ref2:
8fe0f950 8786 case DW_FORM_strx2:
4bb7a0a7
DJ
8787 info_ptr += 2;
8788 break;
8fe0f950
AT
8789 case DW_FORM_strx3:
8790 info_ptr += 3;
8791 break;
4bb7a0a7
DJ
8792 case DW_FORM_data4:
8793 case DW_FORM_ref4:
8fe0f950 8794 case DW_FORM_strx4:
4bb7a0a7
DJ
8795 info_ptr += 4;
8796 break;
8797 case DW_FORM_data8:
8798 case DW_FORM_ref8:
55f1336d 8799 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8800 info_ptr += 8;
8801 break;
0224619f
JK
8802 case DW_FORM_data16:
8803 info_ptr += 16;
8804 break;
4bb7a0a7 8805 case DW_FORM_string:
9b1c24c8 8806 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8807 info_ptr += bytes_read;
8808 break;
2dc7f7b3 8809 case DW_FORM_sec_offset:
4bb7a0a7 8810 case DW_FORM_strp:
36586728 8811 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8812 info_ptr += cu->header.offset_size;
8813 break;
2dc7f7b3 8814 case DW_FORM_exprloc:
4bb7a0a7
DJ
8815 case DW_FORM_block:
8816 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8817 info_ptr += bytes_read;
8818 break;
8819 case DW_FORM_block1:
8820 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8821 break;
8822 case DW_FORM_block2:
8823 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8824 break;
8825 case DW_FORM_block4:
8826 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8827 break;
336d760d 8828 case DW_FORM_addrx:
cf532bd1 8829 case DW_FORM_strx:
4bb7a0a7
DJ
8830 case DW_FORM_sdata:
8831 case DW_FORM_udata:
8832 case DW_FORM_ref_udata:
3019eac3
DE
8833 case DW_FORM_GNU_addr_index:
8834 case DW_FORM_GNU_str_index:
18a8505e 8835 case DW_FORM_rnglistx:
41144253 8836 case DW_FORM_loclistx:
d521ce57 8837 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8838 break;
8839 case DW_FORM_indirect:
8840 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8841 info_ptr += bytes_read;
8842 /* We need to continue parsing from here, so just go back to
8843 the top. */
8844 goto skip_attribute;
8845
8846 default:
3e43a32a
MS
8847 error (_("Dwarf Error: Cannot handle %s "
8848 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8849 dwarf_form_name (form),
8850 bfd_get_filename (abfd));
8851 }
8852 }
8853
8854 if (abbrev->has_children)
dee91e82 8855 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8856 else
8857 return info_ptr;
8858}
8859
93311388 8860/* Locate ORIG_PDI's sibling.
dee91e82 8861 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8862
d521ce57 8863static const gdb_byte *
dee91e82
DE
8864locate_pdi_sibling (const struct die_reader_specs *reader,
8865 struct partial_die_info *orig_pdi,
d521ce57 8866 const gdb_byte *info_ptr)
91c24f0a
DC
8867{
8868 /* Do we know the sibling already? */
72bf9492 8869
91c24f0a
DC
8870 if (orig_pdi->sibling)
8871 return orig_pdi->sibling;
8872
8873 /* Are there any children to deal with? */
8874
8875 if (!orig_pdi->has_children)
8876 return info_ptr;
8877
4bb7a0a7 8878 /* Skip the children the long way. */
91c24f0a 8879
dee91e82 8880 return skip_children (reader, info_ptr);
91c24f0a
DC
8881}
8882
257e7a09 8883/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8884 not NULL. */
c906108c 8885
891813be
TT
8886void
8887dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8888{
ed2dc618
SM
8889 struct dwarf2_per_objfile *dwarf2_per_objfile
8890 = get_dwarf2_per_objfile (objfile);
8891
af758d11
SM
8892 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8893
077cbab2
TT
8894 /* If this psymtab is constructed from a debug-only objfile, the
8895 has_section_at_zero flag will not necessarily be correct. We
8896 can get the correct value for this flag by looking at the data
8897 associated with the (presumably stripped) associated objfile. */
8898 if (objfile->separate_debug_objfile_backlink)
c906108c 8899 {
077cbab2
TT
8900 struct dwarf2_per_objfile *dpo_backlink
8901 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8902
5989a64e
SM
8903 dwarf2_per_objfile->per_bfd->has_section_at_zero
8904 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8905 }
98bfdba5 8906
8566b89b 8907 expand_psymtab (objfile);
95554aad 8908
ed2dc618 8909 process_cu_includes (dwarf2_per_objfile);
c906108c 8910}
9cdd5dbd
DE
8911\f
8912/* Reading in full CUs. */
c906108c 8913
10b3939b
DJ
8914/* Add PER_CU to the queue. */
8915
8916static void
120ce1b5
SM
8917queue_comp_unit (dwarf2_per_cu_data *per_cu,
8918 dwarf2_per_objfile *per_objfile,
95554aad 8919 enum language pretend_language)
10b3939b 8920{
10b3939b 8921 per_cu->queued = 1;
120ce1b5 8922 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8923}
8924
89e63ee4
DE
8925/* If PER_CU is not yet queued, add it to the queue.
8926 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8927 dependency.
0907af0c 8928 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8929 meaning either PER_CU is already queued or it is already loaded.
8930
8931 N.B. There is an invariant here that if a CU is queued then it is loaded.
8932 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8933
8934static int
89e63ee4 8935maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8936 dwarf2_per_cu_data *per_cu,
8937 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8938 enum language pretend_language)
8939{
8940 /* We may arrive here during partial symbol reading, if we need full
8941 DIEs to process an unusual case (e.g. template arguments). Do
8942 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8943 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8944 {
8945 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8946 return 1;
8947 return 0;
8948 }
8949
8950 /* Mark the dependence relation so that we don't flush PER_CU
8951 too early. */
89e63ee4
DE
8952 if (dependent_cu != NULL)
8953 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8954
8955 /* If it's already on the queue, we have nothing to do. */
8956 if (per_cu->queued)
8957 return 0;
8958
8959 /* If the compilation unit is already loaded, just mark it as
8960 used. */
8961 if (per_cu->cu != NULL)
8962 {
8963 per_cu->cu->last_used = 0;
8964 return 0;
8965 }
8966
8967 /* Add it to the queue. */
120ce1b5 8968 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
8969
8970 return 1;
8971}
8972
10b3939b
DJ
8973/* Process the queue. */
8974
8975static void
ed2dc618 8976process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8977{
b4f54984 8978 if (dwarf_read_debug)
45cfd468
DE
8979 {
8980 fprintf_unfiltered (gdb_stdlog,
8981 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8982 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8983 }
8984
03dd20cc
DJ
8985 /* The queue starts out with one item, but following a DIE reference
8986 may load a new CU, adding it to the end of the queue. */
5989a64e 8987 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8988 {
5989a64e 8989 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8990
af758d11 8991 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8992 /* Skip dummy CUs. */
39856def 8993 && item.per_cu->cu != NULL)
f4dc4d17 8994 {
39856def 8995 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8996 unsigned int debug_print_threshold;
247f5c4f 8997 char buf[100];
f4dc4d17 8998
247f5c4f 8999 if (per_cu->is_debug_types)
f4dc4d17 9000 {
247f5c4f
DE
9001 struct signatured_type *sig_type =
9002 (struct signatured_type *) per_cu;
9003
9d8780f0 9004 sprintf (buf, "TU %s at offset %s",
73be47f5 9005 hex_string (sig_type->signature),
9d8780f0 9006 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9007 /* There can be 100s of TUs.
9008 Only print them in verbose mode. */
9009 debug_print_threshold = 2;
f4dc4d17 9010 }
247f5c4f 9011 else
73be47f5 9012 {
9d8780f0
SM
9013 sprintf (buf, "CU at offset %s",
9014 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9015 debug_print_threshold = 1;
9016 }
247f5c4f 9017
b4f54984 9018 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9019 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9020
9021 if (per_cu->is_debug_types)
8fc0b21d 9022 process_full_type_unit (per_cu->cu, item.pretend_language);
f4dc4d17 9023 else
8fc0b21d 9024 process_full_comp_unit (per_cu->cu, item.pretend_language);
f4dc4d17 9025
b4f54984 9026 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9027 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9028 }
10b3939b 9029
39856def 9030 item.per_cu->queued = 0;
5989a64e 9031 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9032 }
9033
b4f54984 9034 if (dwarf_read_debug)
45cfd468
DE
9035 {
9036 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9037 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9038 }
10b3939b
DJ
9039}
9040
10b3939b
DJ
9041/* Read in full symbols for PST, and anything it depends on. */
9042
8566b89b
TT
9043void
9044dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9045{
af758d11 9046 gdb_assert (!readin_p (objfile));
95554aad 9047
48993951 9048 expand_dependencies (objfile);
aaa75496 9049
97a1449a
SM
9050 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9051 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9052 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9053}
9054
af758d11
SM
9055/* See psympriv.h. */
9056
9057bool
9058dwarf2_psymtab::readin_p (struct objfile *objfile) const
9059{
9060 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9061 return per_objfile->symtab_set_p (per_cu_data);
9062}
9063
9064/* See psympriv.h. */
9065
9066compunit_symtab *
9067dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9068{
9069 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9070 return per_objfile->get_symtab (per_cu_data);
9071}
9072
dee91e82
DE
9073/* Trivial hash function for die_info: the hash value of a DIE
9074 is its offset in .debug_info for this objfile. */
10b3939b 9075
dee91e82
DE
9076static hashval_t
9077die_hash (const void *item)
10b3939b 9078{
9a3c8263 9079 const struct die_info *die = (const struct die_info *) item;
6502dd73 9080
9c541725 9081 return to_underlying (die->sect_off);
dee91e82 9082}
63d06c5c 9083
dee91e82
DE
9084/* Trivial comparison function for die_info structures: two DIEs
9085 are equal if they have the same offset. */
98bfdba5 9086
dee91e82
DE
9087static int
9088die_eq (const void *item_lhs, const void *item_rhs)
9089{
9a3c8263
SM
9090 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9091 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9092
9c541725 9093 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9094}
c906108c 9095
c0ab21c2 9096/* Load the DIEs associated with PER_CU into memory. */
c906108c 9097
dee91e82 9098static void
ab432490
SM
9099load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9100 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9101 bool skip_partial,
9102 enum language pretend_language)
dee91e82 9103{
c0ab21c2
TT
9104 gdb_assert (! this_cu->is_debug_types);
9105
ab432490 9106 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9107 if (reader.dummy_p)
9108 return;
9109
9110 struct dwarf2_cu *cu = reader.cu;
9111 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9112
dee91e82
DE
9113 gdb_assert (cu->die_hash == NULL);
9114 cu->die_hash =
9115 htab_create_alloc_ex (cu->header.length / 12,
9116 die_hash,
9117 die_eq,
9118 NULL,
9119 &cu->comp_unit_obstack,
9120 hashtab_obstack_allocate,
9121 dummy_obstack_deallocate);
e142c38c 9122
3e225074 9123 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9124 reader.comp_unit_die->child
9125 = read_die_and_siblings (&reader, reader.info_ptr,
9126 &info_ptr, reader.comp_unit_die);
9127 cu->dies = reader.comp_unit_die;
dee91e82 9128 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9129
9130 /* We try not to read any attributes in this function, because not
9cdd5dbd 9131 all CUs needed for references have been loaded yet, and symbol
10b3939b 9132 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9133 or we won't be able to build types correctly.
9134 Similarly, if we do not read the producer, we can not apply
9135 producer-specific interpretation. */
c0ab21c2 9136 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9137
9138 reader.keep ();
10b3939b
DJ
9139}
9140
3da10d80
KS
9141/* Add a DIE to the delayed physname list. */
9142
9143static void
9144add_to_method_list (struct type *type, int fnfield_index, int index,
9145 const char *name, struct die_info *die,
9146 struct dwarf2_cu *cu)
9147{
9148 struct delayed_method_info mi;
9149 mi.type = type;
9150 mi.fnfield_index = fnfield_index;
9151 mi.index = index;
9152 mi.name = name;
9153 mi.die = die;
c89b44cd 9154 cu->method_list.push_back (mi);
3da10d80
KS
9155}
9156
3693fdb3
PA
9157/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9158 "const" / "volatile". If so, decrements LEN by the length of the
9159 modifier and return true. Otherwise return false. */
9160
9161template<size_t N>
9162static bool
9163check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9164{
9165 size_t mod_len = sizeof (mod) - 1;
9166 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9167 {
9168 len -= mod_len;
9169 return true;
9170 }
9171 return false;
9172}
9173
3da10d80
KS
9174/* Compute the physnames of any methods on the CU's method list.
9175
9176 The computation of method physnames is delayed in order to avoid the
9177 (bad) condition that one of the method's formal parameters is of an as yet
9178 incomplete type. */
9179
9180static void
9181compute_delayed_physnames (struct dwarf2_cu *cu)
9182{
3693fdb3 9183 /* Only C++ delays computing physnames. */
c89b44cd 9184 if (cu->method_list.empty ())
3693fdb3
PA
9185 return;
9186 gdb_assert (cu->language == language_cplus);
9187
52941706 9188 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9189 {
1d06ead6 9190 const char *physname;
3da10d80 9191 struct fn_fieldlist *fn_flp
c89b44cd
TT
9192 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9193 physname = dwarf2_physname (mi.name, mi.die, cu);
9194 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9195 = physname ? physname : "";
3693fdb3
PA
9196
9197 /* Since there's no tag to indicate whether a method is a
9198 const/volatile overload, extract that information out of the
9199 demangled name. */
9200 if (physname != NULL)
9201 {
9202 size_t len = strlen (physname);
9203
9204 while (1)
9205 {
9206 if (physname[len] == ')') /* shortcut */
9207 break;
9208 else if (check_modifier (physname, len, " const"))
c89b44cd 9209 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9210 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9211 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9212 else
9213 break;
9214 }
9215 }
3da10d80 9216 }
c89b44cd
TT
9217
9218 /* The list is no longer needed. */
9219 cu->method_list.clear ();
3da10d80
KS
9220}
9221
a766d390
DE
9222/* Go objects should be embedded in a DW_TAG_module DIE,
9223 and it's not clear if/how imported objects will appear.
9224 To keep Go support simple until that's worked out,
9225 go back through what we've read and create something usable.
9226 We could do this while processing each DIE, and feels kinda cleaner,
9227 but that way is more invasive.
9228 This is to, for example, allow the user to type "p var" or "b main"
9229 without having to specify the package name, and allow lookups
9230 of module.object to work in contexts that use the expression
9231 parser. */
9232
9233static void
9234fixup_go_packaging (struct dwarf2_cu *cu)
9235{
421d1616 9236 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9237 struct pending *list;
9238 int i;
9239
c24bdb02 9240 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9241 list != NULL;
9242 list = list->next)
a766d390
DE
9243 {
9244 for (i = 0; i < list->nsyms; ++i)
9245 {
9246 struct symbol *sym = list->symbol[i];
9247
c1b5c1eb 9248 if (sym->language () == language_go
a766d390
DE
9249 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9250 {
421d1616
TT
9251 gdb::unique_xmalloc_ptr<char> this_package_name
9252 (go_symbol_package_name (sym));
a766d390
DE
9253
9254 if (this_package_name == NULL)
9255 continue;
9256 if (package_name == NULL)
421d1616 9257 package_name = std::move (this_package_name);
a766d390
DE
9258 else
9259 {
5e22e966 9260 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9261 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9262 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9263 (symbol_symtab (sym) != NULL
9264 ? symtab_to_filename_for_display
9265 (symbol_symtab (sym))
e3b94546 9266 : objfile_name (objfile)),
421d1616 9267 this_package_name.get (), package_name.get ());
a766d390
DE
9268 }
9269 }
9270 }
9271 }
9272
9273 if (package_name != NULL)
9274 {
5e22e966 9275 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9276 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9277 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9278 saved_package_name);
a766d390
DE
9279 struct symbol *sym;
9280
8c14c3a3 9281 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9282 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9283 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9284 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9285 e.g., "main" finds the "main" module and not C's main(). */
9286 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9287 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9288 SYMBOL_TYPE (sym) = type;
9289
c24bdb02 9290 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9291 }
9292}
9293
c9317f21
TT
9294/* Allocate a fully-qualified name consisting of the two parts on the
9295 obstack. */
9296
9297static const char *
9298rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9299{
9300 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9301}
9302
9c6a1327
TT
9303/* A helper that allocates a variant part to attach to a Rust enum
9304 type. OBSTACK is where the results should be allocated. TYPE is
9305 the type we're processing. DISCRIMINANT_INDEX is the index of the
9306 discriminant. It must be the index of one of the fields of TYPE.
9307 DEFAULT_INDEX is the index of the default field; or -1 if there is
9308 no default. RANGES is indexed by "effective" field number (the
9309 field index, but omitting the discriminant and default fields) and
9310 must hold the discriminant values used by the variants. Note that
9311 RANGES must have a lifetime at least as long as OBSTACK -- either
9312 already allocated on it, or static. */
c9317f21 9313
9c6a1327
TT
9314static void
9315alloc_rust_variant (struct obstack *obstack, struct type *type,
9316 int discriminant_index, int default_index,
9317 gdb::array_view<discriminant_range> ranges)
9318{
9319 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9320 must be handled by the caller. */
9321 gdb_assert (discriminant_index >= 0
1f704f76 9322 && discriminant_index < type->num_fields ());
c9317f21 9323 gdb_assert (default_index == -1
1f704f76 9324 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9325
9c6a1327 9326 /* We have one variant for each non-discriminant field. */
1f704f76 9327 int n_variants = type->num_fields () - 1;
c9317f21 9328
9c6a1327
TT
9329 variant *variants = new (obstack) variant[n_variants];
9330 int var_idx = 0;
9331 int range_idx = 0;
1f704f76 9332 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9333 {
9334 if (i == discriminant_index)
9335 continue;
c9317f21 9336
9c6a1327
TT
9337 variants[var_idx].first_field = i;
9338 variants[var_idx].last_field = i + 1;
9339
9340 /* The default field does not need a range, but other fields do.
9341 We skipped the discriminant above. */
9342 if (i != default_index)
9343 {
9344 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9345 ++range_idx;
9346 }
c9317f21 9347
9c6a1327
TT
9348 ++var_idx;
9349 }
9350
9351 gdb_assert (range_idx == ranges.size ());
9352 gdb_assert (var_idx == n_variants);
9353
9354 variant_part *part = new (obstack) variant_part;
9355 part->discriminant_index = discriminant_index;
9356 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9357 discriminant_index));
9358 part->variants = gdb::array_view<variant> (variants, n_variants);
9359
9360 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9361 gdb::array_view<variant_part> *prop_value
9362 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9363
9c6a1327
TT
9364 struct dynamic_prop prop;
9365 prop.kind = PROP_VARIANT_PARTS;
9366 prop.data.variant_parts = prop_value;
9367
5c54719c 9368 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9369}
9370
9371/* Some versions of rustc emitted enums in an unusual way.
9372
9373 Ordinary enums were emitted as unions. The first element of each
9374 structure in the union was named "RUST$ENUM$DISR". This element
9375 held the discriminant.
9376
9377 These versions of Rust also implemented the "non-zero"
9378 optimization. When the enum had two values, and one is empty and
9379 the other holds a pointer that cannot be zero, the pointer is used
9380 as the discriminant, with a zero value meaning the empty variant.
9381 Here, the union's first member is of the form
9382 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9383 where the fieldnos are the indices of the fields that should be
9384 traversed in order to find the field (which may be several fields deep)
9385 and the variantname is the name of the variant of the case when the
9386 field is zero.
9387
9388 This function recognizes whether TYPE is of one of these forms,
9389 and, if so, smashes it to be a variant type. */
9390
9391static void
9392quirk_rust_enum (struct type *type, struct objfile *objfile)
9393{
78134374 9394 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9395
9396 /* We don't need to deal with empty enums. */
1f704f76 9397 if (type->num_fields () == 0)
c9317f21
TT
9398 return;
9399
9400#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9401 if (type->num_fields () == 1
c9317f21
TT
9402 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9403 {
9404 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9405
9406 /* Decode the field name to find the offset of the
9407 discriminant. */
9408 ULONGEST bit_offset = 0;
9409 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9410 while (name[0] >= '0' && name[0] <= '9')
9411 {
9412 char *tail;
9413 unsigned long index = strtoul (name, &tail, 10);
9414 name = tail;
9415 if (*name != '$'
1f704f76 9416 || index >= field_type->num_fields ()
c9317f21
TT
9417 || (TYPE_FIELD_LOC_KIND (field_type, index)
9418 != FIELD_LOC_KIND_BITPOS))
9419 {
b98664d3 9420 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9421 "[in module %s]"),
9422 TYPE_FIELD_NAME (type, 0),
9423 objfile_name (objfile));
9424 return;
9425 }
9426 ++name;
9427
9428 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9429 field_type = TYPE_FIELD_TYPE (field_type, index);
9430 }
9431
9c6a1327
TT
9432 /* Smash this type to be a structure type. We have to do this
9433 because the type has already been recorded. */
67607e24 9434 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9435 type->set_num_fields (3);
9c6a1327 9436 /* Save the field we care about. */
ceacbf6e 9437 struct field saved_field = type->field (0);
3cabb6b0
SM
9438 type->set_fields
9439 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9440
9c6a1327
TT
9441 /* Put the discriminant at index 0. */
9442 TYPE_FIELD_TYPE (type, 0) = field_type;
9443 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9444 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9445 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9446
9447 /* The order of fields doesn't really matter, so put the real
9448 field at index 1 and the data-less field at index 2. */
ceacbf6e 9449 type->field (1) = saved_field;
9c6a1327 9450 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9451 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9452 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9453 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9454 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9455
9456 const char *dataless_name
7d93a1e0 9457 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9458 name);
9459 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9460 dataless_name);
9c6a1327 9461 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9462 /* NAME points into the original discriminant name, which
9463 already has the correct lifetime. */
9c6a1327 9464 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9465 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9466
9c6a1327
TT
9467 /* Indicate that this is a variant type. */
9468 static discriminant_range ranges[1] = { { 0, 0 } };
9469 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9470 }
77c2dba3
TT
9471 /* A union with a single anonymous field is probably an old-style
9472 univariant enum. */
1f704f76 9473 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9474 {
c9317f21
TT
9475 /* Smash this type to be a structure type. We have to do this
9476 because the type has already been recorded. */
67607e24 9477 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9478
9c6a1327 9479 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9480 const char *variant_name
7d93a1e0 9481 = rust_last_path_segment (field_type->name ());
9c6a1327 9482 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9483 field_type->set_name
9484 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9485 type->name (), variant_name));
c9317f21
TT
9486 }
9487 else
9488 {
9489 struct type *disr_type = nullptr;
1f704f76 9490 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9491 {
9492 disr_type = TYPE_FIELD_TYPE (type, i);
9493
78134374 9494 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9495 {
9496 /* All fields of a true enum will be structs. */
9497 return;
9498 }
1f704f76 9499 else if (disr_type->num_fields () == 0)
c9317f21
TT
9500 {
9501 /* Could be data-less variant, so keep going. */
a037790e 9502 disr_type = nullptr;
c9317f21
TT
9503 }
9504 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9505 "RUST$ENUM$DISR") != 0)
9506 {
9507 /* Not a Rust enum. */
9508 return;
9509 }
9510 else
9511 {
9512 /* Found one. */
9513 break;
9514 }
9515 }
9516
9517 /* If we got here without a discriminant, then it's probably
9518 just a union. */
9519 if (disr_type == nullptr)
9520 return;
9521
9522 /* Smash this type to be a structure type. We have to do this
9523 because the type has already been recorded. */
67607e24 9524 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9525
9c6a1327 9526 /* Make space for the discriminant field. */
ceacbf6e 9527 struct field *disr_field = &disr_type->field (0);
9c6a1327 9528 field *new_fields
1f704f76 9529 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9530 * sizeof (struct field)));
80fc5e77 9531 memcpy (new_fields + 1, type->fields (),
1f704f76 9532 type->num_fields () * sizeof (struct field));
3cabb6b0 9533 type->set_fields (new_fields);
1f704f76 9534 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9535
9536 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9537 type->field (0) = *disr_field;
9c6a1327
TT
9538 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9539 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9540
9541 /* We need a way to find the correct discriminant given a
9542 variant name. For convenience we build a map here. */
9543 struct type *enum_type = FIELD_TYPE (*disr_field);
9544 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9545 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9546 {
9547 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9548 {
9549 const char *name
9550 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9551 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9552 }
9553 }
9554
1f704f76 9555 int n_fields = type->num_fields ();
9c6a1327
TT
9556 /* We don't need a range entry for the discriminant, but we do
9557 need one for every other field, as there is no default
9558 variant. */
9559 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9560 discriminant_range,
9561 n_fields - 1);
c9317f21
TT
9562 /* Skip the discriminant here. */
9563 for (int i = 1; i < n_fields; ++i)
9564 {
9565 /* Find the final word in the name of this variant's type.
9566 That name can be used to look up the correct
9567 discriminant. */
9568 const char *variant_name
7d93a1e0 9569 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9570
9571 auto iter = discriminant_map.find (variant_name);
9572 if (iter != discriminant_map.end ())
9c6a1327
TT
9573 {
9574 ranges[i].low = iter->second;
9575 ranges[i].high = iter->second;
9576 }
c9317f21 9577
bedda9ac 9578 /* Remove the discriminant field, if it exists. */
9c6a1327 9579 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9580 if (sub_type->num_fields () > 0)
bedda9ac 9581 {
5e33d5f4 9582 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9583 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9584 }
9c6a1327 9585 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9586 sub_type->set_name
9587 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9588 type->name (), variant_name));
c9317f21 9589 }
9c6a1327
TT
9590
9591 /* Indicate that this is a variant type. */
9592 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9593 gdb::array_view<discriminant_range> (ranges,
9594 n_fields - 1));
c9317f21
TT
9595 }
9596}
9597
9598/* Rewrite some Rust unions to be structures with variants parts. */
9599
9600static void
9601rust_union_quirks (struct dwarf2_cu *cu)
9602{
9603 gdb_assert (cu->language == language_rust);
52941706 9604 for (type *type_ : cu->rust_unions)
5e22e966 9605 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9606 /* We don't need this any more. */
9607 cu->rust_unions.clear ();
c9317f21
TT
9608}
9609
8adb8487
TT
9610/* See read.h. */
9611
9612type_unit_group_unshareable *
9613dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9614{
9615 auto iter = this->m_type_units.find (tu_group);
9616 if (iter != this->m_type_units.end ())
9617 return iter->second.get ();
9618
9619 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9620 type_unit_group_unshareable *result = uniq.get ();
9621 this->m_type_units[tu_group] = std::move (uniq);
9622 return result;
9623}
9624
e286671b
TT
9625struct type *
9626dwarf2_per_objfile::get_type_for_signatured_type
9627 (signatured_type *sig_type) const
9628{
9629 auto iter = this->m_type_map.find (sig_type);
9630 if (iter == this->m_type_map.end ())
9631 return nullptr;
9632
9633 return iter->second;
9634}
9635
9636void dwarf2_per_objfile::set_type_for_signatured_type
9637 (signatured_type *sig_type, struct type *type)
9638{
9639 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9640
9641 this->m_type_map[sig_type] = type;
9642}
9643
95554aad
TT
9644/* A helper function for computing the list of all symbol tables
9645 included by PER_CU. */
9646
9647static void
4c39bc03 9648recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9649 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9650 dwarf2_per_cu_data *per_cu,
9651 dwarf2_per_objfile *per_objfile,
43f3e411 9652 struct compunit_symtab *immediate_parent)
95554aad 9653{
af758d11 9654 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9655 if (*slot != NULL)
9656 {
9657 /* This inclusion and its children have been processed. */
9658 return;
9659 }
9660
9661 *slot = per_cu;
af758d11 9662
95554aad 9663 /* Only add a CU if it has a symbol table. */
43182c09 9664 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9665 if (cust != NULL)
ec94af83
DE
9666 {
9667 /* If this is a type unit only add its symbol table if we haven't
9668 seen it yet (type unit per_cu's can share symtabs). */
9669 if (per_cu->is_debug_types)
9670 {
43f3e411 9671 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9672 if (*slot == NULL)
9673 {
43f3e411 9674 *slot = cust;
4c39bc03 9675 result->push_back (cust);
43f3e411
DE
9676 if (cust->user == NULL)
9677 cust->user = immediate_parent;
ec94af83
DE
9678 }
9679 }
9680 else
f9125b6c 9681 {
4c39bc03 9682 result->push_back (cust);
43f3e411
DE
9683 if (cust->user == NULL)
9684 cust->user = immediate_parent;
f9125b6c 9685 }
ec94af83 9686 }
95554aad 9687
ae640021
AB
9688 if (!per_cu->imported_symtabs_empty ())
9689 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9690 {
9691 recursively_compute_inclusions (result, all_children,
43182c09
SM
9692 all_type_symtabs, ptr, per_objfile,
9693 cust);
ae640021 9694 }
95554aad
TT
9695}
9696
43f3e411 9697/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9698 PER_CU. */
9699
9700static void
43182c09
SM
9701compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9702 dwarf2_per_objfile *per_objfile)
95554aad 9703{
f4dc4d17
DE
9704 gdb_assert (! per_cu->is_debug_types);
9705
ae640021 9706 if (!per_cu->imported_symtabs_empty ())
95554aad 9707 {
ae640021 9708 int len;
4c39bc03 9709 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9710 htab_t all_children, all_type_symtabs;
43182c09 9711 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9712
9713 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9714 if (cust == NULL)
95554aad
TT
9715 return;
9716
9717 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9718 NULL, xcalloc, xfree);
ec94af83
DE
9719 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9720 NULL, xcalloc, xfree);
95554aad 9721
ae640021 9722 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9723 {
9724 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9725 all_type_symtabs, ptr, per_objfile,
9726 cust);
ec94af83 9727 }
95554aad 9728
ec94af83 9729 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9730 len = result_symtabs.size ();
43f3e411 9731 cust->includes
f6e649dd 9732 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9733 struct compunit_symtab *, len + 1);
4c39bc03
TT
9734 memcpy (cust->includes, result_symtabs.data (),
9735 len * sizeof (compunit_symtab *));
43f3e411 9736 cust->includes[len] = NULL;
95554aad 9737
95554aad 9738 htab_delete (all_children);
ec94af83 9739 htab_delete (all_type_symtabs);
95554aad
TT
9740 }
9741}
9742
9743/* Compute the 'includes' field for the symtabs of all the CUs we just
9744 read. */
9745
9746static void
ed2dc618 9747process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9748{
5989a64e 9749 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9750 {
9751 if (! iter->is_debug_types)
43182c09 9752 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9753 }
95554aad 9754
5989a64e 9755 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9756}
9757
8fc0b21d 9758/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9759 already been loaded into memory. */
9760
9761static void
8fc0b21d 9762process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9763{
8fc0b21d 9764 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9765 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9766 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9767 CORE_ADDR lowpc, highpc;
43f3e411 9768 struct compunit_symtab *cust;
10b3939b 9769 CORE_ADDR baseaddr;
4359dff1 9770 struct block *static_block;
3e29f34a 9771 CORE_ADDR addr;
10b3939b 9772
b3b3bada 9773 baseaddr = objfile->text_section_offset ();
10b3939b 9774
c89b44cd
TT
9775 /* Clear the list here in case something was left over. */
9776 cu->method_list.clear ();
10b3939b 9777
95554aad
TT
9778 cu->language = pretend_language;
9779 cu->language_defn = language_def (cu->language);
9780
c906108c 9781 /* Do line number decoding in read_file_scope () */
10b3939b 9782 process_die (cu->dies, cu);
c906108c 9783
a766d390
DE
9784 /* For now fudge the Go package. */
9785 if (cu->language == language_go)
9786 fixup_go_packaging (cu);
9787
5f48f8f3 9788 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9789 should be complete, and it should now be safe to compute all of the
9790 physnames. */
9791 compute_delayed_physnames (cu);
3da10d80 9792
c9317f21
TT
9793 if (cu->language == language_rust)
9794 rust_union_quirks (cu);
9795
fae299cd
DC
9796 /* Some compilers don't define a DW_AT_high_pc attribute for the
9797 compilation unit. If the DW_AT_high_pc is missing, synthesize
9798 it, by scanning the DIE's below the compilation unit. */
10b3939b 9799 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9800
3e29f34a 9801 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9802 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9803
9804 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9805 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9806 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9807 addrmap to help ensure it has an accurate map of pc values belonging to
9808 this comp unit. */
9809 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9810
c24bdb02 9811 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9812 SECT_OFF_TEXT (objfile),
9813 0);
c906108c 9814
43f3e411 9815 if (cust != NULL)
c906108c 9816 {
df15bd07 9817 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9818
8be455d7
JK
9819 /* Set symtab language to language from DW_AT_language. If the
9820 compilation is from a C file generated by language preprocessors, do
9821 not set the language if it was already deduced by start_subfile. */
43f3e411 9822 if (!(cu->language == language_c
40e3ad0e 9823 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9824 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9825
9826 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9827 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9828 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9829 there were bugs in prologue debug info, fixed later in GCC-4.5
9830 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9831
9832 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9833 needed, it would be wrong due to missing DW_AT_producer there.
9834
9835 Still one can confuse GDB by using non-standard GCC compilation
9836 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9837 */
ab260dad 9838 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9839 cust->locations_valid = 1;
e0d00bc7
JK
9840
9841 if (gcc_4_minor >= 5)
43f3e411 9842 cust->epilogue_unwind_valid = 1;
96408a79 9843
43f3e411 9844 cust->call_site_htab = cu->call_site_htab;
c906108c 9845 }
9291a0cd 9846
8fc0b21d 9847 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9848
95554aad 9849 /* Push it for inclusion processing later. */
8fc0b21d 9850 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9851
9852 /* Not needed any more. */
c24bdb02 9853 cu->reset_builder ();
f4dc4d17 9854}
45cfd468 9855
8fc0b21d 9856/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9857 already been loaded into memory. */
9858
9859static void
8fc0b21d 9860process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9861 enum language pretend_language)
9862{
8fc0b21d 9863 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9864 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9865 struct compunit_symtab *cust;
0186c6a7
DE
9866 struct signatured_type *sig_type;
9867
8fc0b21d
SM
9868 gdb_assert (cu->per_cu->is_debug_types);
9869 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9870
c89b44cd
TT
9871 /* Clear the list here in case something was left over. */
9872 cu->method_list.clear ();
f4dc4d17 9873
f4dc4d17
DE
9874 cu->language = pretend_language;
9875 cu->language_defn = language_def (cu->language);
9876
9877 /* The symbol tables are set up in read_type_unit_scope. */
9878 process_die (cu->dies, cu);
9879
9880 /* For now fudge the Go package. */
9881 if (cu->language == language_go)
9882 fixup_go_packaging (cu);
9883
5f48f8f3 9884 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9885 should be complete, and it should now be safe to compute all of the
9886 physnames. */
9887 compute_delayed_physnames (cu);
f4dc4d17 9888
c9317f21
TT
9889 if (cu->language == language_rust)
9890 rust_union_quirks (cu);
9891
f4dc4d17
DE
9892 /* TUs share symbol tables.
9893 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9894 of it with end_expandable_symtab. Otherwise, complete the addition of
9895 this TU's symbols to the existing symtab. */
8adb8487
TT
9896 type_unit_group_unshareable *tug_unshare =
9897 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9898 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9899 {
c24bdb02
KS
9900 buildsym_compunit *builder = cu->get_builder ();
9901 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9902 tug_unshare->compunit_symtab = cust;
f4dc4d17 9903
43f3e411 9904 if (cust != NULL)
f4dc4d17
DE
9905 {
9906 /* Set symtab language to language from DW_AT_language. If the
9907 compilation is from a C file generated by language preprocessors,
9908 do not set the language if it was already deduced by
9909 start_subfile. */
43f3e411
DE
9910 if (!(cu->language == language_c
9911 && COMPUNIT_FILETABS (cust)->language != language_c))
9912 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9913 }
9914 }
9915 else
9916 {
c24bdb02 9917 cu->get_builder ()->augment_type_symtab ();
8adb8487 9918 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9919 }
9920
8fc0b21d 9921 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9922
9923 /* Not needed any more. */
c24bdb02 9924 cu->reset_builder ();
c906108c
SS
9925}
9926
95554aad
TT
9927/* Process an imported unit DIE. */
9928
9929static void
9930process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9931{
9932 struct attribute *attr;
9933
f4dc4d17
DE
9934 /* For now we don't handle imported units in type units. */
9935 if (cu->per_cu->is_debug_types)
9936 {
9937 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9938 " supported in type units [in module %s]"),
5e22e966 9939 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9940 }
9941
95554aad
TT
9942 attr = dwarf2_attr (die, DW_AT_import, cu);
9943 if (attr != NULL)
9944 {
0826b30a 9945 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9946 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9947 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9948 dwarf2_per_cu_data *per_cu
ab432490 9949 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9950
58990295
TV
9951 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9952 into another compilation unit, at root level. Regard this as a hint,
9953 and ignore it. */
9954 if (die->parent && die->parent->parent == NULL
9955 && per_cu->unit_type == DW_UT_compile
9956 && per_cu->lang == language_cplus)
9957 return;
9958
69d751e3 9959 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9960 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 9961 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9962
ae640021 9963 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9964 }
9965}
9966
4c8aa72d
PA
9967/* RAII object that represents a process_die scope: i.e.,
9968 starts/finishes processing a DIE. */
9969class process_die_scope
adde2bff 9970{
4c8aa72d
PA
9971public:
9972 process_die_scope (die_info *die, dwarf2_cu *cu)
9973 : m_die (die), m_cu (cu)
9974 {
9975 /* We should only be processing DIEs not already in process. */
9976 gdb_assert (!m_die->in_process);
9977 m_die->in_process = true;
9978 }
8c3cb9fa 9979
4c8aa72d
PA
9980 ~process_die_scope ()
9981 {
9982 m_die->in_process = false;
9983
9984 /* If we're done processing the DIE for the CU that owns the line
9985 header, we don't need the line header anymore. */
9986 if (m_cu->line_header_die_owner == m_die)
9987 {
9988 delete m_cu->line_header;
9989 m_cu->line_header = NULL;
9990 m_cu->line_header_die_owner = NULL;
9991 }
9992 }
9993
9994private:
9995 die_info *m_die;
9996 dwarf2_cu *m_cu;
9997};
adde2bff 9998
c906108c
SS
9999/* Process a die and its children. */
10000
10001static void
e7c27a73 10002process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10003{
4c8aa72d 10004 process_die_scope scope (die, cu);
adde2bff 10005
c906108c
SS
10006 switch (die->tag)
10007 {
10008 case DW_TAG_padding:
10009 break;
10010 case DW_TAG_compile_unit:
95554aad 10011 case DW_TAG_partial_unit:
e7c27a73 10012 read_file_scope (die, cu);
c906108c 10013 break;
348e048f
DE
10014 case DW_TAG_type_unit:
10015 read_type_unit_scope (die, cu);
10016 break;
c906108c 10017 case DW_TAG_subprogram:
0a4b0913
AB
10018 /* Nested subprograms in Fortran get a prefix. */
10019 if (cu->language == language_fortran
10020 && die->parent != NULL
10021 && die->parent->tag == DW_TAG_subprogram)
10022 cu->processing_has_namespace_info = true;
10023 /* Fall through. */
c906108c 10024 case DW_TAG_inlined_subroutine:
edb3359d 10025 read_func_scope (die, cu);
c906108c
SS
10026 break;
10027 case DW_TAG_lexical_block:
14898363
L
10028 case DW_TAG_try_block:
10029 case DW_TAG_catch_block:
e7c27a73 10030 read_lexical_block_scope (die, cu);
c906108c 10031 break;
216f72a1 10032 case DW_TAG_call_site:
96408a79
SA
10033 case DW_TAG_GNU_call_site:
10034 read_call_site_scope (die, cu);
10035 break;
c906108c 10036 case DW_TAG_class_type:
680b30c7 10037 case DW_TAG_interface_type:
c906108c
SS
10038 case DW_TAG_structure_type:
10039 case DW_TAG_union_type:
134d01f1 10040 process_structure_scope (die, cu);
c906108c
SS
10041 break;
10042 case DW_TAG_enumeration_type:
134d01f1 10043 process_enumeration_scope (die, cu);
c906108c 10044 break;
134d01f1 10045
f792889a
DJ
10046 /* These dies have a type, but processing them does not create
10047 a symbol or recurse to process the children. Therefore we can
10048 read them on-demand through read_type_die. */
c906108c 10049 case DW_TAG_subroutine_type:
72019c9c 10050 case DW_TAG_set_type:
c906108c 10051 case DW_TAG_array_type:
c906108c 10052 case DW_TAG_pointer_type:
c906108c 10053 case DW_TAG_ptr_to_member_type:
c906108c 10054 case DW_TAG_reference_type:
4297a3f0 10055 case DW_TAG_rvalue_reference_type:
c906108c 10056 case DW_TAG_string_type:
c906108c 10057 break;
134d01f1 10058
c906108c 10059 case DW_TAG_base_type:
a02abb62 10060 case DW_TAG_subrange_type:
cb249c71 10061 case DW_TAG_typedef:
134d01f1
DJ
10062 /* Add a typedef symbol for the type definition, if it has a
10063 DW_AT_name. */
f792889a 10064 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10065 break;
c906108c 10066 case DW_TAG_common_block:
e7c27a73 10067 read_common_block (die, cu);
c906108c
SS
10068 break;
10069 case DW_TAG_common_inclusion:
10070 break;
d9fa45fe 10071 case DW_TAG_namespace:
9068261f 10072 cu->processing_has_namespace_info = true;
e7c27a73 10073 read_namespace (die, cu);
d9fa45fe 10074 break;
5d7cb8df 10075 case DW_TAG_module:
9068261f 10076 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10077 read_module (die, cu);
10078 break;
d9fa45fe 10079 case DW_TAG_imported_declaration:
9068261f 10080 cu->processing_has_namespace_info = true;
74921315
KS
10081 if (read_namespace_alias (die, cu))
10082 break;
86a73007
TT
10083 /* The declaration is not a global namespace alias. */
10084 /* Fall through. */
d9fa45fe 10085 case DW_TAG_imported_module:
9068261f 10086 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10087 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10088 || cu->language != language_fortran))
b98664d3 10089 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10090 dwarf_tag_name (die->tag));
10091 read_import_statement (die, cu);
d9fa45fe 10092 break;
95554aad
TT
10093
10094 case DW_TAG_imported_unit:
10095 process_imported_unit_die (die, cu);
10096 break;
10097
71a3c369
TT
10098 case DW_TAG_variable:
10099 read_variable (die, cu);
10100 break;
10101
c906108c 10102 default:
e7c27a73 10103 new_symbol (die, NULL, cu);
c906108c
SS
10104 break;
10105 }
10106}
ca69b9e6
DE
10107\f
10108/* DWARF name computation. */
c906108c 10109
94af9270
KS
10110/* A helper function for dwarf2_compute_name which determines whether DIE
10111 needs to have the name of the scope prepended to the name listed in the
10112 die. */
10113
10114static int
10115die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10116{
1c809c68
TT
10117 struct attribute *attr;
10118
94af9270
KS
10119 switch (die->tag)
10120 {
10121 case DW_TAG_namespace:
10122 case DW_TAG_typedef:
10123 case DW_TAG_class_type:
10124 case DW_TAG_interface_type:
10125 case DW_TAG_structure_type:
10126 case DW_TAG_union_type:
10127 case DW_TAG_enumeration_type:
10128 case DW_TAG_enumerator:
10129 case DW_TAG_subprogram:
08a76f8a 10130 case DW_TAG_inlined_subroutine:
94af9270 10131 case DW_TAG_member:
74921315 10132 case DW_TAG_imported_declaration:
94af9270
KS
10133 return 1;
10134
10135 case DW_TAG_variable:
c2b0a229 10136 case DW_TAG_constant:
94af9270
KS
10137 /* We only need to prefix "globally" visible variables. These include
10138 any variable marked with DW_AT_external or any variable that
10139 lives in a namespace. [Variables in anonymous namespaces
10140 require prefixing, but they are not DW_AT_external.] */
10141
10142 if (dwarf2_attr (die, DW_AT_specification, cu))
10143 {
10144 struct dwarf2_cu *spec_cu = cu;
9a619af0 10145
94af9270
KS
10146 return die_needs_namespace (die_specification (die, &spec_cu),
10147 spec_cu);
10148 }
10149
1c809c68 10150 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10151 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10152 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10153 return 0;
10154 /* A variable in a lexical block of some kind does not need a
10155 namespace, even though in C++ such variables may be external
10156 and have a mangled name. */
10157 if (die->parent->tag == DW_TAG_lexical_block
10158 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10159 || die->parent->tag == DW_TAG_catch_block
10160 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10161 return 0;
10162 return 1;
94af9270
KS
10163
10164 default:
10165 return 0;
10166 }
10167}
10168
73b9be8b
KS
10169/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10170 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10171 defined for the given DIE. */
10172
10173static struct attribute *
10174dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10175{
10176 struct attribute *attr;
10177
10178 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10179 if (attr == NULL)
10180 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10181
10182 return attr;
10183}
10184
10185/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10186 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10187 defined for the given DIE. */
10188
10189static const char *
10190dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10191{
10192 const char *linkage_name;
10193
10194 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10195 if (linkage_name == NULL)
10196 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10197
787de330
TT
10198 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10199 See https://github.com/rust-lang/rust/issues/32925. */
10200 if (cu->language == language_rust && linkage_name != NULL
10201 && strchr (linkage_name, '{') != NULL)
10202 linkage_name = NULL;
10203
73b9be8b
KS
10204 return linkage_name;
10205}
10206
94af9270 10207/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10208 compute the physname for the object, which include a method's:
9c37b5ae 10209 - formal parameters (C++),
a766d390 10210 - receiver type (Go),
a766d390
DE
10211
10212 The term "physname" is a bit confusing.
10213 For C++, for example, it is the demangled name.
10214 For Go, for example, it's the mangled name.
94af9270 10215
af6b7be1
JB
10216 For Ada, return the DIE's linkage name rather than the fully qualified
10217 name. PHYSNAME is ignored..
10218
5989a64e 10219 The result is allocated on the objfile->per_bfd's obstack and
45940949 10220 canonicalized. */
94af9270
KS
10221
10222static const char *
15d034d0
TT
10223dwarf2_compute_name (const char *name,
10224 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10225 int physname)
10226{
5e22e966 10227 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10228
94af9270
KS
10229 if (name == NULL)
10230 name = dwarf2_name (die, cu);
10231
2ee7123e
DE
10232 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10233 but otherwise compute it by typename_concat inside GDB.
10234 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10235 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10236 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10237 will set the demangled name to the result of dwarf2_full_name, and it is
10238 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10239 if (cu->language == language_ada
10240 || (cu->language == language_fortran && physname))
10241 {
10242 /* For Ada unit, we prefer the linkage name over the name, as
10243 the former contains the exported name, which the user expects
10244 to be able to reference. Ideally, we want the user to be able
10245 to reference this entity using either natural or linkage name,
10246 but we haven't started looking at this enhancement yet. */
73b9be8b 10247 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10248
2ee7123e
DE
10249 if (linkage_name != NULL)
10250 return linkage_name;
f55ee35c
JK
10251 }
10252
94af9270
KS
10253 /* These are the only languages we know how to qualify names in. */
10254 if (name != NULL
9c37b5ae 10255 && (cu->language == language_cplus
c44af4eb
TT
10256 || cu->language == language_fortran || cu->language == language_d
10257 || cu->language == language_rust))
94af9270
KS
10258 {
10259 if (die_needs_namespace (die, cu))
10260 {
0d5cff50 10261 const char *prefix;
34a68019 10262 const char *canonical_name = NULL;
94af9270 10263
d7e74731
PA
10264 string_file buf;
10265
94af9270 10266 prefix = determine_prefix (die, cu);
94af9270
KS
10267 if (*prefix != '\0')
10268 {
43816ebc
TT
10269 gdb::unique_xmalloc_ptr<char> prefixed_name
10270 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10271
43816ebc 10272 buf.puts (prefixed_name.get ());
94af9270
KS
10273 }
10274 else
d7e74731 10275 buf.puts (name);
94af9270 10276
98bfdba5
PA
10277 /* Template parameters may be specified in the DIE's DW_AT_name, or
10278 as children with DW_TAG_template_type_param or
10279 DW_TAG_value_type_param. If the latter, add them to the name
10280 here. If the name already has template parameters, then
10281 skip this step; some versions of GCC emit both, and
10282 it is more efficient to use the pre-computed name.
10283
10284 Something to keep in mind about this process: it is very
10285 unlikely, or in some cases downright impossible, to produce
10286 something that will match the mangled name of a function.
10287 If the definition of the function has the same debug info,
10288 we should be able to match up with it anyway. But fallbacks
10289 using the minimal symbol, for instance to find a method
10290 implemented in a stripped copy of libstdc++, will not work.
10291 If we do not have debug info for the definition, we will have to
10292 match them up some other way.
10293
10294 When we do name matching there is a related problem with function
10295 templates; two instantiated function templates are allowed to
10296 differ only by their return types, which we do not add here. */
10297
10298 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10299 {
10300 struct attribute *attr;
10301 struct die_info *child;
10302 int first = 1;
10303
10304 die->building_fullname = 1;
10305
10306 for (child = die->child; child != NULL; child = child->sibling)
10307 {
10308 struct type *type;
12df843f 10309 LONGEST value;
d521ce57 10310 const gdb_byte *bytes;
98bfdba5
PA
10311 struct dwarf2_locexpr_baton *baton;
10312 struct value *v;
10313
10314 if (child->tag != DW_TAG_template_type_param
10315 && child->tag != DW_TAG_template_value_param)
10316 continue;
10317
10318 if (first)
10319 {
d7e74731 10320 buf.puts ("<");
98bfdba5
PA
10321 first = 0;
10322 }
10323 else
d7e74731 10324 buf.puts (", ");
98bfdba5
PA
10325
10326 attr = dwarf2_attr (child, DW_AT_type, cu);
10327 if (attr == NULL)
10328 {
b98664d3 10329 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10330 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10331 continue;
10332 }
10333 type = die_type (child, cu);
10334
10335 if (child->tag == DW_TAG_template_type_param)
10336 {
c1ec8cea
TT
10337 c_print_type (type, "", &buf, -1, 0, cu->language,
10338 &type_print_raw_options);
98bfdba5
PA
10339 continue;
10340 }
10341
10342 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10343 if (attr == NULL)
10344 {
b98664d3 10345 complaint (_("template parameter missing "
3e43a32a 10346 "DW_AT_const_value"));
d7e74731 10347 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10348 continue;
10349 }
10350
10351 dwarf2_const_value_attr (attr, type, name,
10352 &cu->comp_unit_obstack, cu,
10353 &value, &bytes, &baton);
10354
10355 if (TYPE_NOSIGN (type))
10356 /* GDB prints characters as NUMBER 'CHAR'. If that's
10357 changed, this can use value_print instead. */
d7e74731 10358 c_printchar (value, type, &buf);
98bfdba5
PA
10359 else
10360 {
10361 struct value_print_options opts;
10362
10363 if (baton != NULL)
10364 v = dwarf2_evaluate_loc_desc (type, NULL,
10365 baton->data,
10366 baton->size,
9f47c707
SM
10367 baton->per_cu,
10368 baton->per_objfile);
98bfdba5
PA
10369 else if (bytes != NULL)
10370 {
10371 v = allocate_value (type);
10372 memcpy (value_contents_writeable (v), bytes,
10373 TYPE_LENGTH (type));
10374 }
10375 else
10376 v = value_from_longest (type, value);
10377
3e43a32a
MS
10378 /* Specify decimal so that we do not depend on
10379 the radix. */
98bfdba5
PA
10380 get_formatted_print_options (&opts, 'd');
10381 opts.raw = 1;
d7e74731 10382 value_print (v, &buf, &opts);
98bfdba5 10383 release_value (v);
98bfdba5
PA
10384 }
10385 }
10386
10387 die->building_fullname = 0;
10388
10389 if (!first)
10390 {
10391 /* Close the argument list, with a space if necessary
10392 (nested templates). */
d7e74731
PA
10393 if (!buf.empty () && buf.string ().back () == '>')
10394 buf.puts (" >");
98bfdba5 10395 else
d7e74731 10396 buf.puts (">");
98bfdba5
PA
10397 }
10398 }
10399
9c37b5ae 10400 /* For C++ methods, append formal parameter type
94af9270 10401 information, if PHYSNAME. */
6e70227d 10402
94af9270 10403 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10404 && cu->language == language_cplus)
94af9270
KS
10405 {
10406 struct type *type = read_type_die (die, cu);
10407
d7e74731 10408 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10409 &type_print_raw_options);
94af9270 10410
9c37b5ae 10411 if (cu->language == language_cplus)
94af9270 10412 {
60430eff
DJ
10413 /* Assume that an artificial first parameter is
10414 "this", but do not crash if it is not. RealView
10415 marks unnamed (and thus unused) parameters as
10416 artificial; there is no way to differentiate
10417 the two cases. */
1f704f76 10418 if (type->num_fields () > 0
94af9270 10419 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10420 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10421 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10422 0))))
d7e74731 10423 buf.puts (" const");
94af9270
KS
10424 }
10425 }
10426
d7e74731 10427 const std::string &intermediate_name = buf.string ();
94af9270
KS
10428
10429 if (cu->language == language_cplus)
34a68019 10430 canonical_name
322a8516 10431 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10432 objfile);
34a68019
TT
10433
10434 /* If we only computed INTERMEDIATE_NAME, or if
10435 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10436 intern it. */
322a8516 10437 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10438 name = objfile->intern (intermediate_name);
34a68019
TT
10439 else
10440 name = canonical_name;
94af9270
KS
10441 }
10442 }
10443
10444 return name;
10445}
10446
0114d602
DJ
10447/* Return the fully qualified name of DIE, based on its DW_AT_name.
10448 If scope qualifiers are appropriate they will be added. The result
34a68019 10449 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10450 not have a name. NAME may either be from a previous call to
10451 dwarf2_name or NULL.
10452
9c37b5ae 10453 The output string will be canonicalized (if C++). */
0114d602
DJ
10454
10455static const char *
15d034d0 10456dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10457{
94af9270
KS
10458 return dwarf2_compute_name (name, die, cu, 0);
10459}
0114d602 10460
94af9270
KS
10461/* Construct a physname for the given DIE in CU. NAME may either be
10462 from a previous call to dwarf2_name or NULL. The result will be
10463 allocated on the objfile_objstack or NULL if the DIE does not have a
10464 name.
0114d602 10465
9c37b5ae 10466 The output string will be canonicalized (if C++). */
0114d602 10467
94af9270 10468static const char *
15d034d0 10469dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10470{
5e22e966 10471 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10472 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10473 int need_copy = 1;
10474
10475 /* In this case dwarf2_compute_name is just a shortcut not building anything
10476 on its own. */
10477 if (!die_needs_namespace (die, cu))
10478 return dwarf2_compute_name (name, die, cu, 1);
10479
906bb4c5
TT
10480 if (cu->language != language_rust)
10481 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10482
10483 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10484 has computed. */
791afaa2 10485 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10486 if (mangled != NULL)
900e11f9 10487 {
900e11f9 10488
59cc4834
JB
10489 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10490 {
10491 /* Do nothing (do not demangle the symbol name). */
10492 }
10493 else if (cu->language == language_go)
a766d390 10494 {
5e2db402
TT
10495 /* This is a lie, but we already lie to the caller new_symbol.
10496 new_symbol assumes we return the mangled name.
a766d390 10497 This just undoes that lie until things are cleaned up. */
a766d390
DE
10498 }
10499 else
10500 {
0eb876f5
JB
10501 /* Use DMGL_RET_DROP for C++ template functions to suppress
10502 their return type. It is easier for GDB users to search
10503 for such functions as `name(params)' than `long name(params)'.
10504 In such case the minimal symbol names do not match the full
10505 symbol names but for template functions there is never a need
10506 to look up their definition from their declaration so
10507 the only disadvantage remains the minimal symbol variant
10508 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10509 demangled.reset (gdb_demangle (mangled,
10510 (DMGL_PARAMS | DMGL_ANSI
10511 | DMGL_RET_DROP)));
a766d390 10512 }
900e11f9 10513 if (demangled)
791afaa2 10514 canon = demangled.get ();
900e11f9
JK
10515 else
10516 {
10517 canon = mangled;
10518 need_copy = 0;
10519 }
10520 }
10521
10522 if (canon == NULL || check_physname)
10523 {
10524 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10525
10526 if (canon != NULL && strcmp (physname, canon) != 0)
10527 {
10528 /* It may not mean a bug in GDB. The compiler could also
10529 compute DW_AT_linkage_name incorrectly. But in such case
10530 GDB would need to be bug-to-bug compatible. */
10531
b98664d3 10532 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10533 "(from linkage <%s>) - DIE at %s [in module %s]"),
10534 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10535 objfile_name (objfile));
900e11f9
JK
10536
10537 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10538 is available here - over computed PHYSNAME. It is safer
10539 against both buggy GDB and buggy compilers. */
10540
10541 retval = canon;
10542 }
10543 else
10544 {
10545 retval = physname;
10546 need_copy = 0;
10547 }
10548 }
10549 else
10550 retval = canon;
10551
10552 if (need_copy)
be1e3d3e 10553 retval = objfile->intern (retval);
900e11f9 10554
900e11f9 10555 return retval;
0114d602
DJ
10556}
10557
74921315
KS
10558/* Inspect DIE in CU for a namespace alias. If one exists, record
10559 a new symbol for it.
10560
10561 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10562
10563static int
10564read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10565{
10566 struct attribute *attr;
10567
10568 /* If the die does not have a name, this is not a namespace
10569 alias. */
10570 attr = dwarf2_attr (die, DW_AT_name, cu);
10571 if (attr != NULL)
10572 {
10573 int num;
10574 struct die_info *d = die;
10575 struct dwarf2_cu *imported_cu = cu;
10576
10577 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10578 keep inspecting DIEs until we hit the underlying import. */
10579#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10580 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10581 {
10582 attr = dwarf2_attr (d, DW_AT_import, cu);
10583 if (attr == NULL)
10584 break;
10585
10586 d = follow_die_ref (d, attr, &imported_cu);
10587 if (d->tag != DW_TAG_imported_declaration)
10588 break;
10589 }
10590
10591 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10592 {
b98664d3 10593 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10594 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10595 return 0;
10596 }
10597
10598 if (attr != NULL)
10599 {
10600 struct type *type;
0826b30a 10601 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10602
aa66c379 10603 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10604 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10605 {
10606 /* This declaration is a global namespace alias. Add
10607 a symbol for it whose type is the aliased namespace. */
10608 new_symbol (die, type, cu);
10609 return 1;
10610 }
10611 }
10612 }
10613
10614 return 0;
10615}
10616
22cee43f 10617/* Return the using directives repository (global or local?) to use in the
804d2729 10618 current context for CU.
22cee43f
PMR
10619
10620 For Ada, imported declarations can materialize renamings, which *may* be
10621 global. However it is impossible (for now?) in DWARF to distinguish
10622 "external" imported declarations and "static" ones. As all imported
10623 declarations seem to be static in all other languages, make them all CU-wide
10624 global only in Ada. */
10625
10626static struct using_direct **
804d2729 10627using_directives (struct dwarf2_cu *cu)
22cee43f 10628{
c24bdb02
KS
10629 if (cu->language == language_ada
10630 && cu->get_builder ()->outermost_context_p ())
10631 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10632 else
c24bdb02 10633 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10634}
10635
27aa8d6a
SW
10636/* Read the import statement specified by the given die and record it. */
10637
10638static void
10639read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10640{
5e22e966 10641 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10642 struct attribute *import_attr;
32019081 10643 struct die_info *imported_die, *child_die;
de4affc9 10644 struct dwarf2_cu *imported_cu;
27aa8d6a 10645 const char *imported_name;
794684b6 10646 const char *imported_name_prefix;
13387711
SW
10647 const char *canonical_name;
10648 const char *import_alias;
10649 const char *imported_declaration = NULL;
794684b6 10650 const char *import_prefix;
eb1e02fd 10651 std::vector<const char *> excludes;
13387711 10652
27aa8d6a
SW
10653 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10654 if (import_attr == NULL)
10655 {
b98664d3 10656 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10657 dwarf_tag_name (die->tag));
10658 return;
10659 }
10660
de4affc9
CC
10661 imported_cu = cu;
10662 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10663 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10664 if (imported_name == NULL)
10665 {
10666 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10667
10668 The import in the following code:
10669 namespace A
10670 {
10671 typedef int B;
10672 }
10673
10674 int main ()
10675 {
10676 using A::B;
10677 B b;
10678 return b;
10679 }
10680
10681 ...
10682 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10683 <52> DW_AT_decl_file : 1
10684 <53> DW_AT_decl_line : 6
10685 <54> DW_AT_import : <0x75>
10686 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10687 <59> DW_AT_name : B
10688 <5b> DW_AT_decl_file : 1
10689 <5c> DW_AT_decl_line : 2
10690 <5d> DW_AT_type : <0x6e>
10691 ...
10692 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10693 <76> DW_AT_byte_size : 4
10694 <77> DW_AT_encoding : 5 (signed)
10695
10696 imports the wrong die ( 0x75 instead of 0x58 ).
10697 This case will be ignored until the gcc bug is fixed. */
10698 return;
10699 }
10700
82856980
SW
10701 /* Figure out the local name after import. */
10702 import_alias = dwarf2_name (die, cu);
27aa8d6a 10703
794684b6
SW
10704 /* Figure out where the statement is being imported to. */
10705 import_prefix = determine_prefix (die, cu);
10706
10707 /* Figure out what the scope of the imported die is and prepend it
10708 to the name of the imported die. */
de4affc9 10709 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10710
f55ee35c
JK
10711 if (imported_die->tag != DW_TAG_namespace
10712 && imported_die->tag != DW_TAG_module)
794684b6 10713 {
13387711
SW
10714 imported_declaration = imported_name;
10715 canonical_name = imported_name_prefix;
794684b6 10716 }
13387711 10717 else if (strlen (imported_name_prefix) > 0)
12aaed36 10718 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10719 imported_name_prefix,
10720 (cu->language == language_d ? "." : "::"),
10721 imported_name, (char *) NULL);
13387711
SW
10722 else
10723 canonical_name = imported_name;
794684b6 10724
32019081
JK
10725 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10726 for (child_die = die->child; child_die && child_die->tag;
436c571c 10727 child_die = child_die->sibling)
32019081
JK
10728 {
10729 /* DWARF-4: A Fortran use statement with a “rename list” may be
10730 represented by an imported module entry with an import attribute
10731 referring to the module and owned entries corresponding to those
10732 entities that are renamed as part of being imported. */
10733
10734 if (child_die->tag != DW_TAG_imported_declaration)
10735 {
b98664d3 10736 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10737 "- DIE at %s [in module %s]"),
10738 sect_offset_str (child_die->sect_off),
10739 objfile_name (objfile));
32019081
JK
10740 continue;
10741 }
10742
10743 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10744 if (import_attr == NULL)
10745 {
b98664d3 10746 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10747 dwarf_tag_name (child_die->tag));
10748 continue;
10749 }
10750
10751 imported_cu = cu;
10752 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10753 &imported_cu);
10754 imported_name = dwarf2_name (imported_die, imported_cu);
10755 if (imported_name == NULL)
10756 {
b98664d3 10757 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10758 "imported name - DIE at %s [in module %s]"),
10759 sect_offset_str (child_die->sect_off),
10760 objfile_name (objfile));
32019081
JK
10761 continue;
10762 }
10763
eb1e02fd 10764 excludes.push_back (imported_name);
32019081
JK
10765
10766 process_die (child_die, cu);
10767 }
10768
804d2729 10769 add_using_directive (using_directives (cu),
22cee43f
PMR
10770 import_prefix,
10771 canonical_name,
10772 import_alias,
10773 imported_declaration,
10774 excludes,
10775 0,
10776 &objfile->objfile_obstack);
27aa8d6a
SW
10777}
10778
5230b05a
WT
10779/* ICC<14 does not output the required DW_AT_declaration on incomplete
10780 types, but gives them a size of zero. Starting with version 14,
10781 ICC is compatible with GCC. */
10782
9068261f 10783static bool
5230b05a
WT
10784producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10785{
10786 if (!cu->checked_producer)
10787 check_producer (cu);
10788
10789 return cu->producer_is_icc_lt_14;
10790}
10791
eb77c9df
AB
10792/* ICC generates a DW_AT_type for C void functions. This was observed on
10793 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10794 which says that void functions should not have a DW_AT_type. */
10795
10796static bool
10797producer_is_icc (struct dwarf2_cu *cu)
10798{
10799 if (!cu->checked_producer)
10800 check_producer (cu);
10801
10802 return cu->producer_is_icc;
10803}
10804
1b80a9fa
JK
10805/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10806 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10807 this, it was first present in GCC release 4.3.0. */
10808
9068261f 10809static bool
1b80a9fa
JK
10810producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10811{
10812 if (!cu->checked_producer)
10813 check_producer (cu);
10814
10815 return cu->producer_is_gcc_lt_4_3;
10816}
10817
d721ba37
PA
10818static file_and_directory
10819find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10820{
d721ba37
PA
10821 file_and_directory res;
10822
9291a0cd
TT
10823 /* Find the filename. Do not use dwarf2_name here, since the filename
10824 is not a source language identifier. */
d721ba37
PA
10825 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10826 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10827
d721ba37
PA
10828 if (res.comp_dir == NULL
10829 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10830 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10831 {
d721ba37
PA
10832 res.comp_dir_storage = ldirname (res.name);
10833 if (!res.comp_dir_storage.empty ())
10834 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10835 }
d721ba37 10836 if (res.comp_dir != NULL)
9291a0cd
TT
10837 {
10838 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10839 directory, get rid of it. */
d721ba37 10840 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10841
d721ba37
PA
10842 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10843 res.comp_dir = cp + 1;
9291a0cd
TT
10844 }
10845
d721ba37
PA
10846 if (res.name == NULL)
10847 res.name = "<unknown>";
10848
10849 return res;
9291a0cd
TT
10850}
10851
f4dc4d17
DE
10852/* Handle DW_AT_stmt_list for a compilation unit.
10853 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10854 COMP_DIR is the compilation directory. LOWPC is passed to
10855 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10856
10857static void
10858handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10859 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10860{
5e22e966 10861 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10862 struct attribute *attr;
527f3840
JK
10863 struct line_header line_header_local;
10864 hashval_t line_header_local_hash;
527f3840
JK
10865 void **slot;
10866 int decode_mapping;
2ab95328 10867
f4dc4d17
DE
10868 gdb_assert (! cu->per_cu->is_debug_types);
10869
2ab95328 10870 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10871 if (attr == NULL)
10872 return;
10873
9c541725 10874 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10875
10876 /* The line header hash table is only created if needed (it exists to
10877 prevent redundant reading of the line table for partial_units).
10878 If we're given a partial_unit, we'll need it. If we're given a
10879 compile_unit, then use the line header hash table if it's already
10880 created, but don't create one just yet. */
10881
5989a64e 10882 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10883 && die->tag == DW_TAG_partial_unit)
2ab95328 10884 {
5989a64e 10885 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10886 .reset (htab_create_alloc (127, line_header_hash_voidp,
10887 line_header_eq_voidp,
10888 free_line_header_voidp,
10889 xcalloc, xfree));
527f3840 10890 }
2ab95328 10891
9c541725 10892 line_header_local.sect_off = line_offset;
527f3840
JK
10893 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10894 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10895 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10896 {
5989a64e 10897 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10898 &line_header_local,
10899 line_header_local_hash, NO_INSERT);
10900
10901 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10902 is not present in *SLOT (since if there is something in *SLOT then
10903 it will be for a partial_unit). */
10904 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10905 {
527f3840 10906 gdb_assert (*slot != NULL);
9a3c8263 10907 cu->line_header = (struct line_header *) *slot;
527f3840 10908 return;
dee91e82 10909 }
2ab95328 10910 }
527f3840
JK
10911
10912 /* dwarf_decode_line_header does not yet provide sufficient information.
10913 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10914 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10915 if (lh == NULL)
527f3840 10916 return;
4c8aa72d
PA
10917
10918 cu->line_header = lh.release ();
10919 cu->line_header_die_owner = die;
527f3840 10920
5989a64e 10921 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10922 slot = NULL;
10923 else
10924 {
5989a64e 10925 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10926 &line_header_local,
10927 line_header_local_hash, INSERT);
10928 gdb_assert (slot != NULL);
10929 }
10930 if (slot != NULL && *slot == NULL)
10931 {
10932 /* This newly decoded line number information unit will be owned
10933 by line_header_hash hash table. */
10934 *slot = cu->line_header;
4c8aa72d 10935 cu->line_header_die_owner = NULL;
527f3840
JK
10936 }
10937 else
10938 {
10939 /* We cannot free any current entry in (*slot) as that struct line_header
10940 may be already used by multiple CUs. Create only temporary decoded
10941 line_header for this CU - it may happen at most once for each line
10942 number information unit. And if we're not using line_header_hash
10943 then this is what we want as well. */
10944 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10945 }
10946 decode_mapping = (die->tag != DW_TAG_partial_unit);
10947 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10948 decode_mapping);
fff8551c 10949
2ab95328
TT
10950}
10951
95554aad 10952/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10953
c906108c 10954static void
e7c27a73 10955read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10956{
5e22e966 10957 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10958 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10959 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10960 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10961 CORE_ADDR highpc = ((CORE_ADDR) 0);
10962 struct attribute *attr;
c906108c 10963 struct die_info *child_die;
e142c38c 10964 CORE_ADDR baseaddr;
6e70227d 10965
380618d6 10966 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10967 baseaddr = objfile->text_section_offset ();
c906108c 10968
fae299cd 10969 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10970
10971 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10972 from finish_block. */
2acceee2 10973 if (lowpc == ((CORE_ADDR) -1))
c906108c 10974 lowpc = highpc;
3e29f34a 10975 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10976
d721ba37 10977 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10978
f4b8a18d
KW
10979 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10980 standardised yet. As a workaround for the language detection we fall
10981 back to the DW_AT_producer string. */
10982 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10983 cu->language = language_opencl;
10984
3019eac3
DE
10985 /* Similar hack for Go. */
10986 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10987 set_cu_language (DW_LANG_Go, cu);
10988
c24bdb02 10989 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10990
10991 /* Decode line number information if present. We do this before
10992 processing child DIEs, so that the line header table is available
10993 for DW_AT_decl_file. */
d721ba37 10994 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10995
10996 /* Process all dies in compilation unit. */
10997 if (die->child != NULL)
10998 {
10999 child_die = die->child;
11000 while (child_die && child_die->tag)
11001 {
11002 process_die (child_die, cu);
436c571c 11003 child_die = child_die->sibling;
3019eac3
DE
11004 }
11005 }
11006
11007 /* Decode macro information, if present. Dwarf 2 macro information
11008 refers to information in the line number info statement program
11009 header, so we can only read it if we've read the header
11010 successfully. */
0af92d60
JK
11011 attr = dwarf2_attr (die, DW_AT_macros, cu);
11012 if (attr == NULL)
11013 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11014 if (attr && cu->line_header)
11015 {
11016 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11017 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11018
43f3e411 11019 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11020 }
11021 else
11022 {
11023 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11024 if (attr && cu->line_header)
11025 {
11026 unsigned int macro_offset = DW_UNSND (attr);
11027
43f3e411 11028 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11029 }
11030 }
3019eac3
DE
11031}
11032
c24bdb02
KS
11033void
11034dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11035{
f4dc4d17
DE
11036 struct type_unit_group *tu_group;
11037 int first_time;
3019eac3 11038 struct attribute *attr;
9c541725 11039 unsigned int i;
0186c6a7 11040 struct signatured_type *sig_type;
3019eac3 11041
f4dc4d17 11042 gdb_assert (per_cu->is_debug_types);
0186c6a7 11043 sig_type = (struct signatured_type *) per_cu;
3019eac3 11044
c24bdb02 11045 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11046
f4dc4d17 11047 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11048 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11049 if (sig_type->type_unit_group == NULL)
c24bdb02 11050 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11051 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11052
11053 /* If we've already processed this stmt_list there's no real need to
11054 do it again, we could fake it and just recreate the part we need
11055 (file name,index -> symtab mapping). If data shows this optimization
11056 is useful we can do it then. */
8adb8487
TT
11057 type_unit_group_unshareable *tug_unshare
11058 = per_objfile->get_type_unit_group_unshareable (tu_group);
11059 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11060
11061 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11062 debug info. */
fff8551c 11063 line_header_up lh;
f4dc4d17 11064 if (attr != NULL)
3019eac3 11065 {
9c541725 11066 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11067 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11068 }
11069 if (lh == NULL)
11070 {
11071 if (first_time)
c24bdb02 11072 start_symtab ("", NULL, 0);
f4dc4d17
DE
11073 else
11074 {
8adb8487 11075 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11076 gdb_assert (m_builder == nullptr);
8adb8487 11077 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11078 m_builder.reset (new struct buildsym_compunit
11079 (COMPUNIT_OBJFILE (cust), "",
11080 COMPUNIT_DIRNAME (cust),
11081 compunit_language (cust),
11082 0, cust));
770479f2 11083 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11084 }
f4dc4d17 11085 return;
3019eac3
DE
11086 }
11087
c24bdb02
KS
11088 line_header = lh.release ();
11089 line_header_die_owner = die;
3019eac3 11090
f4dc4d17
DE
11091 if (first_time)
11092 {
c24bdb02 11093 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11094
1fd60fc0
DE
11095 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11096 still initializing it, and our caller (a few levels up)
11097 process_full_type_unit still needs to know if this is the first
11098 time. */
11099
8adb8487 11100 tug_unshare->symtabs
4ac93832
TT
11101 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11102 struct symtab *, line_header->file_names_size ());
3019eac3 11103
7ba99d21
AT
11104 auto &file_names = line_header->file_names ();
11105 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11106 {
7ba99d21 11107 file_entry &fe = file_names[i];
c24bdb02
KS
11108 dwarf2_start_subfile (this, fe.name,
11109 fe.include_dir (line_header));
11110 buildsym_compunit *b = get_builder ();
11111 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11112 {
4c8aa72d
PA
11113 /* NOTE: start_subfile will recognize when it's been
11114 passed a file it has already seen. So we can't
11115 assume there's a simple mapping from
11116 cu->line_header->file_names to subfiles, plus
11117 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11118 b->get_current_subfile ()->symtab
11119 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11120 }
11121
c24bdb02 11122 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11123 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11124 }
11125 }
11126 else
3019eac3 11127 {
c24bdb02 11128 gdb_assert (m_builder == nullptr);
8adb8487 11129 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11130 m_builder.reset (new struct buildsym_compunit
11131 (COMPUNIT_OBJFILE (cust), "",
11132 COMPUNIT_DIRNAME (cust),
11133 compunit_language (cust),
11134 0, cust));
770479f2 11135 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11136
7ba99d21
AT
11137 auto &file_names = line_header->file_names ();
11138 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11139 {
7ba99d21 11140 file_entry &fe = file_names[i];
8adb8487 11141 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11142 }
3019eac3
DE
11143 }
11144
f4dc4d17
DE
11145 /* The main symtab is allocated last. Type units don't have DW_AT_name
11146 so they don't have a "real" (so to speak) symtab anyway.
11147 There is later code that will assign the main symtab to all symbols
11148 that don't have one. We need to handle the case of a symbol with a
11149 missing symtab (DW_AT_decl_file) anyway. */
11150}
3019eac3 11151
f4dc4d17
DE
11152/* Process DW_TAG_type_unit.
11153 For TUs we want to skip the first top level sibling if it's not the
11154 actual type being defined by this TU. In this case the first top
11155 level sibling is there to provide context only. */
3019eac3 11156
f4dc4d17
DE
11157static void
11158read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11159{
11160 struct die_info *child_die;
3019eac3 11161
f4dc4d17
DE
11162 prepare_one_comp_unit (cu, die, language_minimal);
11163
11164 /* Initialize (or reinitialize) the machinery for building symtabs.
11165 We do this before processing child DIEs, so that the line header table
11166 is available for DW_AT_decl_file. */
c24bdb02 11167 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11168
11169 if (die->child != NULL)
11170 {
11171 child_die = die->child;
11172 while (child_die && child_die->tag)
11173 {
11174 process_die (child_die, cu);
436c571c 11175 child_die = child_die->sibling;
f4dc4d17
DE
11176 }
11177 }
3019eac3
DE
11178}
11179\f
80626a55
DE
11180/* DWO/DWP files.
11181
11182 http://gcc.gnu.org/wiki/DebugFission
11183 http://gcc.gnu.org/wiki/DebugFissionDWP
11184
11185 To simplify handling of both DWO files ("object" files with the DWARF info)
11186 and DWP files (a file with the DWOs packaged up into one file), we treat
11187 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11188
11189static hashval_t
11190hash_dwo_file (const void *item)
11191{
9a3c8263 11192 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11193 hashval_t hash;
3019eac3 11194
a2ce51a0
DE
11195 hash = htab_hash_string (dwo_file->dwo_name);
11196 if (dwo_file->comp_dir != NULL)
11197 hash += htab_hash_string (dwo_file->comp_dir);
11198 return hash;
3019eac3
DE
11199}
11200
11201static int
11202eq_dwo_file (const void *item_lhs, const void *item_rhs)
11203{
9a3c8263
SM
11204 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11205 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11206
a2ce51a0
DE
11207 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11208 return 0;
11209 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11210 return lhs->comp_dir == rhs->comp_dir;
11211 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11212}
11213
11214/* Allocate a hash table for DWO files. */
11215
51ac9db5 11216static htab_up
298e9637 11217allocate_dwo_file_hash_table ()
3019eac3 11218{
51ac9db5
SM
11219 auto delete_dwo_file = [] (void *item)
11220 {
11221 struct dwo_file *dwo_file = (struct dwo_file *) item;
11222
11223 delete dwo_file;
11224 };
11225
bc68fb19
TT
11226 return htab_up (htab_create_alloc (41,
11227 hash_dwo_file,
11228 eq_dwo_file,
11229 delete_dwo_file,
11230 xcalloc, xfree));
3019eac3
DE
11231}
11232
80626a55
DE
11233/* Lookup DWO file DWO_NAME. */
11234
11235static void **
ed2dc618
SM
11236lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11237 const char *dwo_name,
11238 const char *comp_dir)
80626a55
DE
11239{
11240 struct dwo_file find_entry;
11241 void **slot;
11242
5989a64e
SM
11243 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11244 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11245
0ac5b59e
DE
11246 find_entry.dwo_name = dwo_name;
11247 find_entry.comp_dir = comp_dir;
5989a64e 11248 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11249 INSERT);
80626a55
DE
11250
11251 return slot;
11252}
11253
3019eac3
DE
11254static hashval_t
11255hash_dwo_unit (const void *item)
11256{
9a3c8263 11257 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11258
11259 /* This drops the top 32 bits of the id, but is ok for a hash. */
11260 return dwo_unit->signature;
11261}
11262
11263static int
11264eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11265{
9a3c8263
SM
11266 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11267 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11268
11269 /* The signature is assumed to be unique within the DWO file.
11270 So while object file CU dwo_id's always have the value zero,
11271 that's OK, assuming each object file DWO file has only one CU,
11272 and that's the rule for now. */
11273 return lhs->signature == rhs->signature;
11274}
11275
11276/* Allocate a hash table for DWO CUs,TUs.
11277 There is one of these tables for each of CUs,TUs for each DWO file. */
11278
b0b6a987 11279static htab_up
298e9637 11280allocate_dwo_unit_table ()
3019eac3
DE
11281{
11282 /* Start out with a pretty small number.
11283 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11284 return htab_up (htab_create_alloc (3,
11285 hash_dwo_unit,
11286 eq_dwo_unit,
11287 NULL, xcalloc, xfree));
3019eac3
DE
11288}
11289
19c3d4c9 11290/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11291
11292static void
19c3d4c9
DE
11293create_dwo_cu_reader (const struct die_reader_specs *reader,
11294 const gdb_byte *info_ptr,
11295 struct die_info *comp_unit_die,
c0ab21c2
TT
11296 struct dwo_file *dwo_file,
11297 struct dwo_unit *dwo_unit)
3019eac3
DE
11298{
11299 struct dwarf2_cu *cu = reader->cu;
9c541725 11300 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11301 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11302
a084a2a6
AT
11303 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11304 if (!signature.has_value ())
3019eac3 11305 {
b98664d3 11306 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11307 " its dwo_id [in module %s]"),
9d8780f0 11308 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11309 return;
11310 }
11311
3019eac3 11312 dwo_unit->dwo_file = dwo_file;
a084a2a6 11313 dwo_unit->signature = *signature;
8a0459fd 11314 dwo_unit->section = section;
9c541725 11315 dwo_unit->sect_off = sect_off;
3019eac3
DE
11316 dwo_unit->length = cu->per_cu->length;
11317
b4f54984 11318 if (dwarf_read_debug)
9d8780f0
SM
11319 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11320 sect_offset_str (sect_off),
9c541725 11321 hex_string (dwo_unit->signature));
3019eac3
DE
11322}
11323
33c5cd75 11324/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11325 Note: This function processes DWO files only, not DWP files. */
3019eac3 11326
33c5cd75 11327static void
ed2dc618 11328create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11329 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11330 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11331{
11332 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11333 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11334 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11335
96b79293 11336 section.read (objfile);
33c5cd75 11337 info_ptr = section.buffer;
3019eac3
DE
11338
11339 if (info_ptr == NULL)
33c5cd75 11340 return;
3019eac3 11341
b4f54984 11342 if (dwarf_read_debug)
19c3d4c9
DE
11343 {
11344 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11345 section.get_name (),
11346 section.get_file_name ());
19c3d4c9 11347 }
3019eac3 11348
33c5cd75 11349 end_ptr = info_ptr + section.size;
3019eac3
DE
11350 while (info_ptr < end_ptr)
11351 {
11352 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11353 struct dwo_unit read_unit {};
33c5cd75
DB
11354 struct dwo_unit *dwo_unit;
11355 void **slot;
11356 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11357
11358 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11359 per_cu.per_bfd = per_bfd;
3019eac3 11360 per_cu.is_debug_types = 0;
33c5cd75
DB
11361 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11362 per_cu.section = &section;
11363
ab432490 11364 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11365 if (!reader.dummy_p)
11366 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11367 &dwo_file, &read_unit);
33c5cd75
DB
11368 info_ptr += per_cu.length;
11369
11370 // If the unit could not be parsed, skip it.
c0ab21c2 11371 if (read_unit.dwo_file == NULL)
33c5cd75 11372 continue;
3019eac3 11373
33c5cd75 11374 if (cus_htab == NULL)
298e9637 11375 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11376
1859c670 11377 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11378 struct dwo_unit);
c0ab21c2 11379 *dwo_unit = read_unit;
b0b6a987 11380 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11381 gdb_assert (slot != NULL);
11382 if (*slot != NULL)
19c3d4c9 11383 {
33c5cd75
DB
11384 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11385 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11386
b98664d3 11387 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11388 " the entry at offset %s, signature %s"),
11389 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11390 hex_string (dwo_unit->signature));
19c3d4c9 11391 }
33c5cd75 11392 *slot = (void *)dwo_unit;
3019eac3 11393 }
3019eac3
DE
11394}
11395
80626a55
DE
11396/* DWP file .debug_{cu,tu}_index section format:
11397 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11398
d2415c6c
DE
11399 DWP Version 1:
11400
80626a55
DE
11401 Both index sections have the same format, and serve to map a 64-bit
11402 signature to a set of section numbers. Each section begins with a header,
11403 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11404 indexes, and a pool of 32-bit section numbers. The index sections will be
11405 aligned at 8-byte boundaries in the file.
11406
d2415c6c
DE
11407 The index section header consists of:
11408
11409 V, 32 bit version number
11410 -, 32 bits unused
11411 N, 32 bit number of compilation units or type units in the index
11412 M, 32 bit number of slots in the hash table
80626a55 11413
d2415c6c 11414 Numbers are recorded using the byte order of the application binary.
80626a55 11415
d2415c6c
DE
11416 The hash table begins at offset 16 in the section, and consists of an array
11417 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11418 order of the application binary). Unused slots in the hash table are 0.
11419 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11420
d2415c6c
DE
11421 The parallel table begins immediately after the hash table
11422 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11423 array of 32-bit indexes (using the byte order of the application binary),
11424 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11425 table contains a 32-bit index into the pool of section numbers. For unused
11426 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11427
73869dc2
DE
11428 The pool of section numbers begins immediately following the hash table
11429 (at offset 16 + 12 * M from the beginning of the section). The pool of
11430 section numbers consists of an array of 32-bit words (using the byte order
11431 of the application binary). Each item in the array is indexed starting
11432 from 0. The hash table entry provides the index of the first section
11433 number in the set. Additional section numbers in the set follow, and the
11434 set is terminated by a 0 entry (section number 0 is not used in ELF).
11435
11436 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11437 section must be the first entry in the set, and the .debug_abbrev.dwo must
11438 be the second entry. Other members of the set may follow in any order.
11439
11440 ---
11441
11442 DWP Version 2:
11443
11444 DWP Version 2 combines all the .debug_info, etc. sections into one,
11445 and the entries in the index tables are now offsets into these sections.
11446 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11447 section.
11448
11449 Index Section Contents:
11450 Header
11451 Hash Table of Signatures dwp_hash_table.hash_table
11452 Parallel Table of Indices dwp_hash_table.unit_table
11453 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11454 Table of Section Sizes dwp_hash_table.v2.sizes
11455
11456 The index section header consists of:
11457
11458 V, 32 bit version number
11459 L, 32 bit number of columns in the table of section offsets
11460 N, 32 bit number of compilation units or type units in the index
11461 M, 32 bit number of slots in the hash table
11462
11463 Numbers are recorded using the byte order of the application binary.
11464
11465 The hash table has the same format as version 1.
11466 The parallel table of indices has the same format as version 1,
11467 except that the entries are origin-1 indices into the table of sections
11468 offsets and the table of section sizes.
11469
11470 The table of offsets begins immediately following the parallel table
11471 (at offset 16 + 12 * M from the beginning of the section). The table is
11472 a two-dimensional array of 32-bit words (using the byte order of the
11473 application binary), with L columns and N+1 rows, in row-major order.
11474 Each row in the array is indexed starting from 0. The first row provides
11475 a key to the remaining rows: each column in this row provides an identifier
11476 for a debug section, and the offsets in the same column of subsequent rows
11477 refer to that section. The section identifiers are:
11478
11479 DW_SECT_INFO 1 .debug_info.dwo
11480 DW_SECT_TYPES 2 .debug_types.dwo
11481 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11482 DW_SECT_LINE 4 .debug_line.dwo
11483 DW_SECT_LOC 5 .debug_loc.dwo
11484 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11485 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11486 DW_SECT_MACRO 8 .debug_macro.dwo
11487
11488 The offsets provided by the CU and TU index sections are the base offsets
11489 for the contributions made by each CU or TU to the corresponding section
11490 in the package file. Each CU and TU header contains an abbrev_offset
11491 field, used to find the abbreviations table for that CU or TU within the
11492 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11493 be interpreted as relative to the base offset given in the index section.
11494 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11495 should be interpreted as relative to the base offset for .debug_line.dwo,
11496 and offsets into other debug sections obtained from DWARF attributes should
11497 also be interpreted as relative to the corresponding base offset.
11498
11499 The table of sizes begins immediately following the table of offsets.
11500 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11501 with L columns and N rows, in row-major order. Each row in the array is
11502 indexed starting from 1 (row 0 is shared by the two tables).
11503
11504 ---
11505
11506 Hash table lookup is handled the same in version 1 and 2:
11507
11508 We assume that N and M will not exceed 2^32 - 1.
11509 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11510
d2415c6c
DE
11511 Given a 64-bit compilation unit signature or a type signature S, an entry
11512 in the hash table is located as follows:
80626a55 11513
d2415c6c
DE
11514 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11515 the low-order k bits all set to 1.
80626a55 11516
d2415c6c 11517 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11518
d2415c6c
DE
11519 3) If the hash table entry at index H matches the signature, use that
11520 entry. If the hash table entry at index H is unused (all zeroes),
11521 terminate the search: the signature is not present in the table.
80626a55 11522
d2415c6c 11523 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11524
d2415c6c 11525 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11526 to stop at an unused slot or find the match. */
80626a55
DE
11527
11528/* Create a hash table to map DWO IDs to their CU/TU entry in
11529 .debug_{info,types}.dwo in DWP_FILE.
11530 Returns NULL if there isn't one.
11531 Note: This function processes DWP files only, not DWO files. */
11532
11533static struct dwp_hash_table *
ed2dc618
SM
11534create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11535 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11536{
11537 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11538 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11539 const gdb_byte *index_ptr, *index_end;
80626a55 11540 struct dwarf2_section_info *index;
73869dc2 11541 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11542 struct dwp_hash_table *htab;
11543
11544 if (is_debug_types)
11545 index = &dwp_file->sections.tu_index;
11546 else
11547 index = &dwp_file->sections.cu_index;
11548
96b79293 11549 if (index->empty ())
80626a55 11550 return NULL;
96b79293 11551 index->read (objfile);
80626a55
DE
11552
11553 index_ptr = index->buffer;
11554 index_end = index_ptr + index->size;
11555
11556 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11557 index_ptr += 4;
11558 if (version == 2)
11559 nr_columns = read_4_bytes (dbfd, index_ptr);
11560 else
11561 nr_columns = 0;
11562 index_ptr += 4;
80626a55
DE
11563 nr_units = read_4_bytes (dbfd, index_ptr);
11564 index_ptr += 4;
11565 nr_slots = read_4_bytes (dbfd, index_ptr);
11566 index_ptr += 4;
11567
73869dc2 11568 if (version != 1 && version != 2)
80626a55 11569 {
21aa081e 11570 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11571 " [in module %s]"),
21aa081e 11572 pulongest (version), dwp_file->name);
80626a55
DE
11573 }
11574 if (nr_slots != (nr_slots & -nr_slots))
11575 {
21aa081e 11576 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11577 " is not power of 2 [in module %s]"),
21aa081e 11578 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11579 }
11580
5989a64e 11581 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11582 htab->version = version;
11583 htab->nr_columns = nr_columns;
80626a55
DE
11584 htab->nr_units = nr_units;
11585 htab->nr_slots = nr_slots;
11586 htab->hash_table = index_ptr;
11587 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11588
11589 /* Exit early if the table is empty. */
11590 if (nr_slots == 0 || nr_units == 0
11591 || (version == 2 && nr_columns == 0))
11592 {
11593 /* All must be zero. */
11594 if (nr_slots != 0 || nr_units != 0
11595 || (version == 2 && nr_columns != 0))
11596 {
b98664d3 11597 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11598 " all zero [in modules %s]"),
11599 dwp_file->name);
11600 }
11601 return htab;
11602 }
11603
11604 if (version == 1)
11605 {
11606 htab->section_pool.v1.indices =
11607 htab->unit_table + sizeof (uint32_t) * nr_slots;
11608 /* It's harder to decide whether the section is too small in v1.
11609 V1 is deprecated anyway so we punt. */
11610 }
11611 else
11612 {
11613 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11614 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11615 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11616 /* Reverse map for error checking. */
11617 int ids_seen[DW_SECT_MAX + 1];
11618 int i;
11619
11620 if (nr_columns < 2)
11621 {
11622 error (_("Dwarf Error: bad DWP hash table, too few columns"
11623 " in section table [in module %s]"),
11624 dwp_file->name);
11625 }
11626 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11627 {
11628 error (_("Dwarf Error: bad DWP hash table, too many columns"
11629 " in section table [in module %s]"),
11630 dwp_file->name);
11631 }
04fd5eed
GB
11632 memset (ids, 255, sizeof_ids);
11633 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11634 for (i = 0; i < nr_columns; ++i)
11635 {
11636 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11637
11638 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11639 {
11640 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11641 " in section table [in module %s]"),
11642 id, dwp_file->name);
11643 }
11644 if (ids_seen[id] != -1)
11645 {
11646 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11647 " id %d in section table [in module %s]"),
11648 id, dwp_file->name);
11649 }
11650 ids_seen[id] = i;
11651 ids[i] = id;
11652 }
11653 /* Must have exactly one info or types section. */
11654 if (((ids_seen[DW_SECT_INFO] != -1)
11655 + (ids_seen[DW_SECT_TYPES] != -1))
11656 != 1)
11657 {
11658 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11659 " DWO info/types section [in module %s]"),
11660 dwp_file->name);
11661 }
11662 /* Must have an abbrev section. */
11663 if (ids_seen[DW_SECT_ABBREV] == -1)
11664 {
11665 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11666 " section [in module %s]"),
11667 dwp_file->name);
11668 }
11669 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11670 htab->section_pool.v2.sizes =
11671 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11672 * nr_units * nr_columns);
11673 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11674 * nr_units * nr_columns))
11675 > index_end)
11676 {
11677 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11678 " [in module %s]"),
11679 dwp_file->name);
11680 }
11681 }
80626a55
DE
11682
11683 return htab;
11684}
11685
11686/* Update SECTIONS with the data from SECTP.
11687
11688 This function is like the other "locate" section routines that are
11689 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11690 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11691
11692 The result is non-zero for success, or zero if an error was found. */
11693
11694static int
73869dc2
DE
11695locate_v1_virtual_dwo_sections (asection *sectp,
11696 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11697{
11698 const struct dwop_section_names *names = &dwop_section_names;
11699
11700 if (section_is_p (sectp->name, &names->abbrev_dwo))
11701 {
11702 /* There can be only one. */
049412e3 11703 if (sections->abbrev.s.section != NULL)
80626a55 11704 return 0;
049412e3 11705 sections->abbrev.s.section = sectp;
fd361982 11706 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11707 }
11708 else if (section_is_p (sectp->name, &names->info_dwo)
11709 || section_is_p (sectp->name, &names->types_dwo))
11710 {
11711 /* There can be only one. */
049412e3 11712 if (sections->info_or_types.s.section != NULL)
80626a55 11713 return 0;
049412e3 11714 sections->info_or_types.s.section = sectp;
fd361982 11715 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11716 }
11717 else if (section_is_p (sectp->name, &names->line_dwo))
11718 {
11719 /* There can be only one. */
049412e3 11720 if (sections->line.s.section != NULL)
80626a55 11721 return 0;
049412e3 11722 sections->line.s.section = sectp;
fd361982 11723 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11724 }
11725 else if (section_is_p (sectp->name, &names->loc_dwo))
11726 {
11727 /* There can be only one. */
049412e3 11728 if (sections->loc.s.section != NULL)
80626a55 11729 return 0;
049412e3 11730 sections->loc.s.section = sectp;
fd361982 11731 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11732 }
11733 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11734 {
11735 /* There can be only one. */
049412e3 11736 if (sections->macinfo.s.section != NULL)
80626a55 11737 return 0;
049412e3 11738 sections->macinfo.s.section = sectp;
fd361982 11739 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11740 }
11741 else if (section_is_p (sectp->name, &names->macro_dwo))
11742 {
11743 /* There can be only one. */
049412e3 11744 if (sections->macro.s.section != NULL)
80626a55 11745 return 0;
049412e3 11746 sections->macro.s.section = sectp;
fd361982 11747 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11748 }
11749 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11750 {
11751 /* There can be only one. */
049412e3 11752 if (sections->str_offsets.s.section != NULL)
80626a55 11753 return 0;
049412e3 11754 sections->str_offsets.s.section = sectp;
fd361982 11755 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11756 }
11757 else
11758 {
11759 /* No other kind of section is valid. */
11760 return 0;
11761 }
11762
11763 return 1;
11764}
11765
73869dc2
DE
11766/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11767 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11768 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11769 This is for DWP version 1 files. */
80626a55
DE
11770
11771static struct dwo_unit *
ed2dc618
SM
11772create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11773 struct dwp_file *dwp_file,
73869dc2
DE
11774 uint32_t unit_index,
11775 const char *comp_dir,
11776 ULONGEST signature, int is_debug_types)
80626a55 11777{
73869dc2
DE
11778 const struct dwp_hash_table *dwp_htab =
11779 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11780 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11781 const char *kind = is_debug_types ? "TU" : "CU";
11782 struct dwo_file *dwo_file;
11783 struct dwo_unit *dwo_unit;
73869dc2 11784 struct virtual_v1_dwo_sections sections;
80626a55 11785 void **dwo_file_slot;
80626a55
DE
11786 int i;
11787
73869dc2
DE
11788 gdb_assert (dwp_file->version == 1);
11789
b4f54984 11790 if (dwarf_read_debug)
80626a55 11791 {
73869dc2 11792 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11793 kind,
73869dc2 11794 pulongest (unit_index), hex_string (signature),
80626a55
DE
11795 dwp_file->name);
11796 }
11797
19ac8c2e 11798 /* Fetch the sections of this DWO unit.
80626a55
DE
11799 Put a limit on the number of sections we look for so that bad data
11800 doesn't cause us to loop forever. */
11801
73869dc2 11802#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11803 (1 /* .debug_info or .debug_types */ \
11804 + 1 /* .debug_abbrev */ \
11805 + 1 /* .debug_line */ \
11806 + 1 /* .debug_loc */ \
11807 + 1 /* .debug_str_offsets */ \
19ac8c2e 11808 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11809 + 1 /* trailing zero */)
11810
11811 memset (&sections, 0, sizeof (sections));
80626a55 11812
73869dc2 11813 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11814 {
11815 asection *sectp;
11816 uint32_t section_nr =
11817 read_4_bytes (dbfd,
73869dc2
DE
11818 dwp_htab->section_pool.v1.indices
11819 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11820
11821 if (section_nr == 0)
11822 break;
11823 if (section_nr >= dwp_file->num_sections)
11824 {
11825 error (_("Dwarf Error: bad DWP hash table, section number too large"
11826 " [in module %s]"),
11827 dwp_file->name);
11828 }
11829
11830 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11831 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11832 {
11833 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11834 " [in module %s]"),
11835 dwp_file->name);
11836 }
11837 }
11838
11839 if (i < 2
96b79293
TT
11840 || sections.info_or_types.empty ()
11841 || sections.abbrev.empty ())
80626a55
DE
11842 {
11843 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11844 " [in module %s]"),
11845 dwp_file->name);
11846 }
73869dc2 11847 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11848 {
11849 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11850 " [in module %s]"),
11851 dwp_file->name);
11852 }
11853
11854 /* It's easier for the rest of the code if we fake a struct dwo_file and
11855 have dwo_unit "live" in that. At least for now.
11856
11857 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11858 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11859 file, we can combine them back into a virtual DWO file to save space
11860 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11861 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11862
791afaa2
TT
11863 std::string virtual_dwo_name =
11864 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11865 sections.abbrev.get_id (),
11866 sections.line.get_id (),
11867 sections.loc.get_id (),
11868 sections.str_offsets.get_id ());
80626a55 11869 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11870 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11871 virtual_dwo_name.c_str (),
11872 comp_dir);
80626a55
DE
11873 /* Create one if necessary. */
11874 if (*dwo_file_slot == NULL)
11875 {
b4f54984 11876 if (dwarf_read_debug)
80626a55
DE
11877 {
11878 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11879 virtual_dwo_name.c_str ());
80626a55 11880 }
51ac9db5 11881 dwo_file = new struct dwo_file;
45940949 11882 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11883 dwo_file->comp_dir = comp_dir;
80626a55
DE
11884 dwo_file->sections.abbrev = sections.abbrev;
11885 dwo_file->sections.line = sections.line;
11886 dwo_file->sections.loc = sections.loc;
11887 dwo_file->sections.macinfo = sections.macinfo;
11888 dwo_file->sections.macro = sections.macro;
11889 dwo_file->sections.str_offsets = sections.str_offsets;
11890 /* The "str" section is global to the entire DWP file. */
11891 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11892 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11893 there's no need to record it in dwo_file.
11894 Also, we can't simply record type sections in dwo_file because
11895 we record a pointer into the vector in dwo_unit. As we collect more
11896 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11897 for it, invalidating all copies of pointers into the previous
11898 contents. */
80626a55
DE
11899 *dwo_file_slot = dwo_file;
11900 }
11901 else
11902 {
b4f54984 11903 if (dwarf_read_debug)
80626a55
DE
11904 {
11905 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11906 virtual_dwo_name.c_str ());
80626a55 11907 }
9a3c8263 11908 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11909 }
80626a55 11910
5989a64e 11911 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11912 dwo_unit->dwo_file = dwo_file;
11913 dwo_unit->signature = signature;
8d749320 11914 dwo_unit->section =
5989a64e 11915 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11916 *dwo_unit->section = sections.info_or_types;
57d63ce2 11917 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11918
11919 return dwo_unit;
11920}
11921
73869dc2
DE
11922/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11923 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11924 piece within that section used by a TU/CU, return a virtual section
11925 of just that piece. */
11926
11927static struct dwarf2_section_info
ed2dc618
SM
11928create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11929 struct dwarf2_section_info *section,
73869dc2
DE
11930 bfd_size_type offset, bfd_size_type size)
11931{
11932 struct dwarf2_section_info result;
11933 asection *sectp;
11934
11935 gdb_assert (section != NULL);
11936 gdb_assert (!section->is_virtual);
11937
11938 memset (&result, 0, sizeof (result));
11939 result.s.containing_section = section;
dc4ccb6f 11940 result.is_virtual = true;
73869dc2
DE
11941
11942 if (size == 0)
11943 return result;
11944
96b79293 11945 sectp = section->get_bfd_section ();
73869dc2
DE
11946
11947 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11948 bounds of the real section. This is a pretty-rare event, so just
11949 flag an error (easier) instead of a warning and trying to cope. */
11950 if (sectp == NULL
fd361982 11951 || offset + size > bfd_section_size (sectp))
73869dc2 11952 {
73869dc2
DE
11953 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11954 " in section %s [in module %s]"),
fd361982 11955 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11956 objfile_name (dwarf2_per_objfile->objfile));
11957 }
11958
11959 result.virtual_offset = offset;
11960 result.size = size;
11961 return result;
11962}
11963
11964/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11965 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11966 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11967 This is for DWP version 2 files. */
11968
11969static struct dwo_unit *
ed2dc618
SM
11970create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11971 struct dwp_file *dwp_file,
73869dc2
DE
11972 uint32_t unit_index,
11973 const char *comp_dir,
11974 ULONGEST signature, int is_debug_types)
11975{
73869dc2
DE
11976 const struct dwp_hash_table *dwp_htab =
11977 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11978 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11979 const char *kind = is_debug_types ? "TU" : "CU";
11980 struct dwo_file *dwo_file;
11981 struct dwo_unit *dwo_unit;
11982 struct virtual_v2_dwo_sections sections;
11983 void **dwo_file_slot;
73869dc2
DE
11984 int i;
11985
11986 gdb_assert (dwp_file->version == 2);
11987
b4f54984 11988 if (dwarf_read_debug)
73869dc2
DE
11989 {
11990 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11991 kind,
11992 pulongest (unit_index), hex_string (signature),
11993 dwp_file->name);
11994 }
11995
11996 /* Fetch the section offsets of this DWO unit. */
11997
11998 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11999
12000 for (i = 0; i < dwp_htab->nr_columns; ++i)
12001 {
12002 uint32_t offset = read_4_bytes (dbfd,
12003 dwp_htab->section_pool.v2.offsets
12004 + (((unit_index - 1) * dwp_htab->nr_columns
12005 + i)
12006 * sizeof (uint32_t)));
12007 uint32_t size = read_4_bytes (dbfd,
12008 dwp_htab->section_pool.v2.sizes
12009 + (((unit_index - 1) * dwp_htab->nr_columns
12010 + i)
12011 * sizeof (uint32_t)));
12012
12013 switch (dwp_htab->section_pool.v2.section_ids[i])
12014 {
12015 case DW_SECT_INFO:
12016 case DW_SECT_TYPES:
12017 sections.info_or_types_offset = offset;
12018 sections.info_or_types_size = size;
12019 break;
12020 case DW_SECT_ABBREV:
12021 sections.abbrev_offset = offset;
12022 sections.abbrev_size = size;
12023 break;
12024 case DW_SECT_LINE:
12025 sections.line_offset = offset;
12026 sections.line_size = size;
12027 break;
12028 case DW_SECT_LOC:
12029 sections.loc_offset = offset;
12030 sections.loc_size = size;
12031 break;
12032 case DW_SECT_STR_OFFSETS:
12033 sections.str_offsets_offset = offset;
12034 sections.str_offsets_size = size;
12035 break;
12036 case DW_SECT_MACINFO:
12037 sections.macinfo_offset = offset;
12038 sections.macinfo_size = size;
12039 break;
12040 case DW_SECT_MACRO:
12041 sections.macro_offset = offset;
12042 sections.macro_size = size;
12043 break;
12044 }
12045 }
12046
12047 /* It's easier for the rest of the code if we fake a struct dwo_file and
12048 have dwo_unit "live" in that. At least for now.
12049
12050 The DWP file can be made up of a random collection of CUs and TUs.
12051 However, for each CU + set of TUs that came from the same original DWO
12052 file, we can combine them back into a virtual DWO file to save space
12053 (fewer struct dwo_file objects to allocate). Remember that for really
12054 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12055
791afaa2
TT
12056 std::string virtual_dwo_name =
12057 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12058 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12059 (long) (sections.line_size ? sections.line_offset : 0),
12060 (long) (sections.loc_size ? sections.loc_offset : 0),
12061 (long) (sections.str_offsets_size
12062 ? sections.str_offsets_offset : 0));
73869dc2 12063 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12064 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12065 virtual_dwo_name.c_str (),
12066 comp_dir);
73869dc2
DE
12067 /* Create one if necessary. */
12068 if (*dwo_file_slot == NULL)
12069 {
b4f54984 12070 if (dwarf_read_debug)
73869dc2
DE
12071 {
12072 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12073 virtual_dwo_name.c_str ());
73869dc2 12074 }
51ac9db5 12075 dwo_file = new struct dwo_file;
45940949 12076 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12077 dwo_file->comp_dir = comp_dir;
12078 dwo_file->sections.abbrev =
ed2dc618 12079 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12080 sections.abbrev_offset, sections.abbrev_size);
12081 dwo_file->sections.line =
ed2dc618 12082 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12083 sections.line_offset, sections.line_size);
12084 dwo_file->sections.loc =
ed2dc618 12085 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12086 sections.loc_offset, sections.loc_size);
12087 dwo_file->sections.macinfo =
ed2dc618 12088 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12089 sections.macinfo_offset, sections.macinfo_size);
12090 dwo_file->sections.macro =
ed2dc618 12091 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12092 sections.macro_offset, sections.macro_size);
12093 dwo_file->sections.str_offsets =
ed2dc618
SM
12094 create_dwp_v2_section (dwarf2_per_objfile,
12095 &dwp_file->sections.str_offsets,
73869dc2
DE
12096 sections.str_offsets_offset,
12097 sections.str_offsets_size);
12098 /* The "str" section is global to the entire DWP file. */
12099 dwo_file->sections.str = dwp_file->sections.str;
12100 /* The info or types section is assigned below to dwo_unit,
12101 there's no need to record it in dwo_file.
12102 Also, we can't simply record type sections in dwo_file because
12103 we record a pointer into the vector in dwo_unit. As we collect more
12104 types we'll grow the vector and eventually have to reallocate space
12105 for it, invalidating all copies of pointers into the previous
12106 contents. */
12107 *dwo_file_slot = dwo_file;
12108 }
12109 else
12110 {
b4f54984 12111 if (dwarf_read_debug)
73869dc2
DE
12112 {
12113 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12114 virtual_dwo_name.c_str ());
73869dc2 12115 }
9a3c8263 12116 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12117 }
73869dc2 12118
5989a64e 12119 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12120 dwo_unit->dwo_file = dwo_file;
12121 dwo_unit->signature = signature;
8d749320 12122 dwo_unit->section =
5989a64e 12123 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12124 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12125 is_debug_types
73869dc2
DE
12126 ? &dwp_file->sections.types
12127 : &dwp_file->sections.info,
12128 sections.info_or_types_offset,
12129 sections.info_or_types_size);
12130 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12131
12132 return dwo_unit;
12133}
12134
57d63ce2
DE
12135/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12136 Returns NULL if the signature isn't found. */
80626a55
DE
12137
12138static struct dwo_unit *
ed2dc618
SM
12139lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12140 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12141 ULONGEST signature, int is_debug_types)
80626a55 12142{
57d63ce2
DE
12143 const struct dwp_hash_table *dwp_htab =
12144 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12145 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12146 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12147 uint32_t hash = signature & mask;
12148 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12149 unsigned int i;
12150 void **slot;
870f88f7 12151 struct dwo_unit find_dwo_cu;
80626a55
DE
12152
12153 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12154 find_dwo_cu.signature = signature;
19ac8c2e 12155 slot = htab_find_slot (is_debug_types
48b490f2
TT
12156 ? dwp_file->loaded_tus.get ()
12157 : dwp_file->loaded_cus.get (),
19ac8c2e 12158 &find_dwo_cu, INSERT);
80626a55
DE
12159
12160 if (*slot != NULL)
9a3c8263 12161 return (struct dwo_unit *) *slot;
80626a55
DE
12162
12163 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12164 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12165 {
12166 ULONGEST signature_in_table;
12167
12168 signature_in_table =
57d63ce2 12169 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12170 if (signature_in_table == signature)
12171 {
57d63ce2
DE
12172 uint32_t unit_index =
12173 read_4_bytes (dbfd,
12174 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12175
73869dc2
DE
12176 if (dwp_file->version == 1)
12177 {
ed2dc618
SM
12178 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12179 dwp_file, unit_index,
73869dc2
DE
12180 comp_dir, signature,
12181 is_debug_types);
12182 }
12183 else
12184 {
ed2dc618
SM
12185 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12186 dwp_file, unit_index,
73869dc2
DE
12187 comp_dir, signature,
12188 is_debug_types);
12189 }
9a3c8263 12190 return (struct dwo_unit *) *slot;
80626a55
DE
12191 }
12192 if (signature_in_table == 0)
12193 return NULL;
12194 hash = (hash + hash2) & mask;
12195 }
12196
12197 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12198 " [in module %s]"),
12199 dwp_file->name);
12200}
12201
ab5088bf 12202/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12203 Open the file specified by FILE_NAME and hand it off to BFD for
12204 preliminary analysis. Return a newly initialized bfd *, which
12205 includes a canonicalized copy of FILE_NAME.
80626a55 12206 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12207 SEARCH_CWD is true if the current directory is to be searched.
12208 It will be searched before debug-file-directory.
13aaf454
DE
12209 If successful, the file is added to the bfd include table of the
12210 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12211 If unable to find/open the file, return NULL.
3019eac3
DE
12212 NOTE: This function is derived from symfile_bfd_open. */
12213
192b62ce 12214static gdb_bfd_ref_ptr
ed2dc618
SM
12215try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12216 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12217{
24b9144d 12218 int desc;
9c02c129
DE
12219 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12220 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12221 to debug_file_directory. */
e0cc99a6 12222 const char *search_path;
9c02c129
DE
12223 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12224
e0cc99a6 12225 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12226 if (search_cwd)
12227 {
12228 if (*debug_file_directory != '\0')
e0cc99a6
TT
12229 {
12230 search_path_holder.reset (concat (".", dirname_separator_string,
12231 debug_file_directory,
12232 (char *) NULL));
12233 search_path = search_path_holder.get ();
12234 }
6ac97d4c 12235 else
e0cc99a6 12236 search_path = ".";
6ac97d4c 12237 }
9c02c129 12238 else
e0cc99a6 12239 search_path = debug_file_directory;
3019eac3 12240
24b9144d 12241 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12242 if (is_dwp)
12243 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12244
12245 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12246 desc = openp (search_path, flags, file_name,
3019eac3
DE
12247 O_RDONLY | O_BINARY, &absolute_name);
12248 if (desc < 0)
12249 return NULL;
12250
e0cc99a6
TT
12251 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12252 gnutarget, desc));
9c02c129
DE
12253 if (sym_bfd == NULL)
12254 return NULL;
192b62ce 12255 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12256
192b62ce
TT
12257 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12258 return NULL;
3019eac3 12259
13aaf454
DE
12260 /* Success. Record the bfd as having been included by the objfile's bfd.
12261 This is important because things like demangled_names_hash lives in the
12262 objfile's per_bfd space and may have references to things like symbol
12263 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12264 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12265
3019eac3
DE
12266 return sym_bfd;
12267}
12268
ab5088bf 12269/* Try to open DWO file FILE_NAME.
3019eac3
DE
12270 COMP_DIR is the DW_AT_comp_dir attribute.
12271 The result is the bfd handle of the file.
12272 If there is a problem finding or opening the file, return NULL.
12273 Upon success, the canonicalized path of the file is stored in the bfd,
12274 same as symfile_bfd_open. */
12275
192b62ce 12276static gdb_bfd_ref_ptr
ed2dc618
SM
12277open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12278 const char *file_name, const char *comp_dir)
3019eac3 12279{
80626a55 12280 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12281 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12282 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12283
12284 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12285
12286 if (comp_dir != NULL)
12287 {
43816ebc
TT
12288 gdb::unique_xmalloc_ptr<char> path_to_try
12289 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12290
12291 /* NOTE: If comp_dir is a relative path, this will also try the
12292 search path, which seems useful. */
ed2dc618 12293 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12294 path_to_try.get (),
ed2dc618 12295 0 /*is_dwp*/,
192b62ce 12296 1 /*search_cwd*/));
3019eac3
DE
12297 if (abfd != NULL)
12298 return abfd;
12299 }
12300
12301 /* That didn't work, try debug-file-directory, which, despite its name,
12302 is a list of paths. */
12303
12304 if (*debug_file_directory == '\0')
12305 return NULL;
12306
ed2dc618
SM
12307 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12308 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12309}
12310
80626a55
DE
12311/* This function is mapped across the sections and remembers the offset and
12312 size of each of the DWO debugging sections we are interested in. */
12313
12314static void
12315dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12316{
9a3c8263 12317 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12318 const struct dwop_section_names *names = &dwop_section_names;
12319
12320 if (section_is_p (sectp->name, &names->abbrev_dwo))
12321 {
049412e3 12322 dwo_sections->abbrev.s.section = sectp;
fd361982 12323 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12324 }
12325 else if (section_is_p (sectp->name, &names->info_dwo))
12326 {
049412e3 12327 dwo_sections->info.s.section = sectp;
fd361982 12328 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12329 }
12330 else if (section_is_p (sectp->name, &names->line_dwo))
12331 {
049412e3 12332 dwo_sections->line.s.section = sectp;
fd361982 12333 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12334 }
12335 else if (section_is_p (sectp->name, &names->loc_dwo))
12336 {
049412e3 12337 dwo_sections->loc.s.section = sectp;
fd361982 12338 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12339 }
41144253 12340 else if (section_is_p (sectp->name, &names->loclists_dwo))
12341 {
12342 dwo_sections->loclists.s.section = sectp;
12343 dwo_sections->loclists.size = bfd_section_size (sectp);
12344 }
80626a55
DE
12345 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12346 {
049412e3 12347 dwo_sections->macinfo.s.section = sectp;
fd361982 12348 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12349 }
12350 else if (section_is_p (sectp->name, &names->macro_dwo))
12351 {
049412e3 12352 dwo_sections->macro.s.section = sectp;
fd361982 12353 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12354 }
12355 else if (section_is_p (sectp->name, &names->str_dwo))
12356 {
049412e3 12357 dwo_sections->str.s.section = sectp;
fd361982 12358 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12359 }
12360 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12361 {
049412e3 12362 dwo_sections->str_offsets.s.section = sectp;
fd361982 12363 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12364 }
12365 else if (section_is_p (sectp->name, &names->types_dwo))
12366 {
12367 struct dwarf2_section_info type_section;
12368
12369 memset (&type_section, 0, sizeof (type_section));
049412e3 12370 type_section.s.section = sectp;
fd361982 12371 type_section.size = bfd_section_size (sectp);
fd5866f6 12372 dwo_sections->types.push_back (type_section);
80626a55
DE
12373 }
12374}
12375
ab5088bf 12376/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12377 by PER_CU. This is for the non-DWP case.
80626a55 12378 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12379
12380static struct dwo_file *
4ab09049
SM
12381open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12382 const char *comp_dir)
3019eac3 12383{
4ab09049 12384 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12385
fb1eb2f9 12386 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12387 if (dbfd == NULL)
12388 {
b4f54984 12389 if (dwarf_read_debug)
80626a55
DE
12390 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12391 return NULL;
12392 }
263db9a1 12393
51ac9db5 12394 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12395 dwo_file->dwo_name = dwo_name;
12396 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12397 dwo_file->dbfd = std::move (dbfd);
3019eac3 12398
fb1eb2f9 12399 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12400 &dwo_file->sections);
3019eac3 12401
4ab09049 12402 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12403 dwo_file->sections.info, dwo_file->cus);
3019eac3 12404
263db9a1 12405 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12406 dwo_file->sections.types, dwo_file->tus);
3019eac3 12407
b4f54984 12408 if (dwarf_read_debug)
80626a55
DE
12409 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12410
263db9a1 12411 return dwo_file.release ();
3019eac3
DE
12412}
12413
80626a55 12414/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12415 size of each of the DWP debugging sections common to version 1 and 2 that
12416 we are interested in. */
3019eac3 12417
80626a55 12418static void
73869dc2
DE
12419dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12420 void *dwp_file_ptr)
3019eac3 12421{
9a3c8263 12422 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12423 const struct dwop_section_names *names = &dwop_section_names;
12424 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12425
80626a55 12426 /* Record the ELF section number for later lookup: this is what the
73869dc2 12427 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12428 gdb_assert (elf_section_nr < dwp_file->num_sections);
12429 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12430
80626a55
DE
12431 /* Look for specific sections that we need. */
12432 if (section_is_p (sectp->name, &names->str_dwo))
12433 {
049412e3 12434 dwp_file->sections.str.s.section = sectp;
fd361982 12435 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12436 }
12437 else if (section_is_p (sectp->name, &names->cu_index))
12438 {
049412e3 12439 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12440 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12441 }
12442 else if (section_is_p (sectp->name, &names->tu_index))
12443 {
049412e3 12444 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12445 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12446 }
12447}
3019eac3 12448
73869dc2
DE
12449/* This function is mapped across the sections and remembers the offset and
12450 size of each of the DWP version 2 debugging sections that we are interested
12451 in. This is split into a separate function because we don't know if we
12452 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12453
12454static void
12455dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12456{
9a3c8263 12457 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12458 const struct dwop_section_names *names = &dwop_section_names;
12459 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12460
12461 /* Record the ELF section number for later lookup: this is what the
12462 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12463 gdb_assert (elf_section_nr < dwp_file->num_sections);
12464 dwp_file->elf_sections[elf_section_nr] = sectp;
12465
12466 /* Look for specific sections that we need. */
12467 if (section_is_p (sectp->name, &names->abbrev_dwo))
12468 {
049412e3 12469 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12470 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12471 }
12472 else if (section_is_p (sectp->name, &names->info_dwo))
12473 {
049412e3 12474 dwp_file->sections.info.s.section = sectp;
fd361982 12475 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12476 }
12477 else if (section_is_p (sectp->name, &names->line_dwo))
12478 {
049412e3 12479 dwp_file->sections.line.s.section = sectp;
fd361982 12480 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12481 }
12482 else if (section_is_p (sectp->name, &names->loc_dwo))
12483 {
049412e3 12484 dwp_file->sections.loc.s.section = sectp;
fd361982 12485 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12486 }
12487 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12488 {
049412e3 12489 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12490 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12491 }
12492 else if (section_is_p (sectp->name, &names->macro_dwo))
12493 {
049412e3 12494 dwp_file->sections.macro.s.section = sectp;
fd361982 12495 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12496 }
12497 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12498 {
049412e3 12499 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12500 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12501 }
12502 else if (section_is_p (sectp->name, &names->types_dwo))
12503 {
049412e3 12504 dwp_file->sections.types.s.section = sectp;
fd361982 12505 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12506 }
12507}
12508
80626a55 12509/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12510
80626a55
DE
12511static hashval_t
12512hash_dwp_loaded_cutus (const void *item)
12513{
9a3c8263 12514 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12515
80626a55
DE
12516 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12517 return dwo_unit->signature;
3019eac3
DE
12518}
12519
80626a55 12520/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12521
80626a55
DE
12522static int
12523eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12524{
9a3c8263
SM
12525 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12526 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12527
80626a55
DE
12528 return dua->signature == dub->signature;
12529}
3019eac3 12530
80626a55 12531/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12532
48b490f2 12533static htab_up
298e9637 12534allocate_dwp_loaded_cutus_table ()
80626a55 12535{
48b490f2
TT
12536 return htab_up (htab_create_alloc (3,
12537 hash_dwp_loaded_cutus,
12538 eq_dwp_loaded_cutus,
12539 NULL, xcalloc, xfree));
80626a55 12540}
3019eac3 12541
ab5088bf
DE
12542/* Try to open DWP file FILE_NAME.
12543 The result is the bfd handle of the file.
12544 If there is a problem finding or opening the file, return NULL.
12545 Upon success, the canonicalized path of the file is stored in the bfd,
12546 same as symfile_bfd_open. */
12547
192b62ce 12548static gdb_bfd_ref_ptr
ed2dc618
SM
12549open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12550 const char *file_name)
ab5088bf 12551{
ed2dc618
SM
12552 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12553 1 /*is_dwp*/,
192b62ce 12554 1 /*search_cwd*/));
6ac97d4c
DE
12555 if (abfd != NULL)
12556 return abfd;
12557
12558 /* Work around upstream bug 15652.
12559 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12560 [Whether that's a "bug" is debatable, but it is getting in our way.]
12561 We have no real idea where the dwp file is, because gdb's realpath-ing
12562 of the executable's path may have discarded the needed info.
12563 [IWBN if the dwp file name was recorded in the executable, akin to
12564 .gnu_debuglink, but that doesn't exist yet.]
12565 Strip the directory from FILE_NAME and search again. */
12566 if (*debug_file_directory != '\0')
12567 {
12568 /* Don't implicitly search the current directory here.
12569 If the user wants to search "." to handle this case,
12570 it must be added to debug-file-directory. */
ed2dc618
SM
12571 return try_open_dwop_file (dwarf2_per_objfile,
12572 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12573 0 /*search_cwd*/);
12574 }
12575
12576 return NULL;
ab5088bf
DE
12577}
12578
80626a55
DE
12579/* Initialize the use of the DWP file for the current objfile.
12580 By convention the name of the DWP file is ${objfile}.dwp.
12581 The result is NULL if it can't be found. */
a766d390 12582
400174b1 12583static std::unique_ptr<struct dwp_file>
ed2dc618 12584open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12585{
12586 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12587
82bf32bc
JK
12588 /* Try to find first .dwp for the binary file before any symbolic links
12589 resolving. */
6c447423
DE
12590
12591 /* If the objfile is a debug file, find the name of the real binary
12592 file and get the name of dwp file from there. */
d721ba37 12593 std::string dwp_name;
6c447423
DE
12594 if (objfile->separate_debug_objfile_backlink != NULL)
12595 {
12596 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12597 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12598
d721ba37 12599 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12600 }
12601 else
d721ba37
PA
12602 dwp_name = objfile->original_name;
12603
12604 dwp_name += ".dwp";
80626a55 12605
ed2dc618 12606 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12607 if (dbfd == NULL
12608 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12609 {
12610 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12611 dwp_name = objfile_name (objfile);
12612 dwp_name += ".dwp";
ed2dc618 12613 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12614 }
12615
80626a55
DE
12616 if (dbfd == NULL)
12617 {
b4f54984 12618 if (dwarf_read_debug)
d721ba37 12619 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12620 return std::unique_ptr<dwp_file> ();
3019eac3 12621 }
400174b1
TT
12622
12623 const char *name = bfd_get_filename (dbfd.get ());
12624 std::unique_ptr<struct dwp_file> dwp_file
12625 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12626
0a0f4c01 12627 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12628 dwp_file->elf_sections =
5989a64e 12629 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12630 dwp_file->num_sections, asection *);
12631
400174b1
TT
12632 bfd_map_over_sections (dwp_file->dbfd.get (),
12633 dwarf2_locate_common_dwp_sections,
12634 dwp_file.get ());
80626a55 12635
400174b1
TT
12636 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12637 0);
80626a55 12638
400174b1
TT
12639 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12640 1);
80626a55 12641
73869dc2 12642 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12643 if (dwp_file->cus && dwp_file->tus
12644 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12645 {
12646 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12647 pretty bizarre. We use pulongest here because that's the established
4d65956b 12648 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12649 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12650 " TU version %s [in DWP file %s]"),
12651 pulongest (dwp_file->cus->version),
d721ba37 12652 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12653 }
08302ed2
DE
12654
12655 if (dwp_file->cus)
12656 dwp_file->version = dwp_file->cus->version;
12657 else if (dwp_file->tus)
12658 dwp_file->version = dwp_file->tus->version;
12659 else
12660 dwp_file->version = 2;
73869dc2
DE
12661
12662 if (dwp_file->version == 2)
400174b1
TT
12663 bfd_map_over_sections (dwp_file->dbfd.get (),
12664 dwarf2_locate_v2_dwp_sections,
12665 dwp_file.get ());
73869dc2 12666
298e9637
SM
12667 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12668 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12669
b4f54984 12670 if (dwarf_read_debug)
80626a55
DE
12671 {
12672 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12673 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12674 " %s CUs, %s TUs\n",
12675 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12676 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12677 }
12678
12679 return dwp_file;
3019eac3 12680}
c906108c 12681
ab5088bf
DE
12682/* Wrapper around open_and_init_dwp_file, only open it once. */
12683
12684static struct dwp_file *
ed2dc618 12685get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12686{
5989a64e 12687 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12688 {
5989a64e 12689 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12690 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12691 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12692 }
5989a64e 12693 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12694}
12695
80626a55
DE
12696/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12697 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12698 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12699 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12700 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12701
12702 This is called, for example, when wanting to read a variable with a
12703 complex location. Therefore we don't want to do file i/o for every call.
12704 Therefore we don't want to look for a DWO file on every call.
12705 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12706 then we check if we've already seen DWO_NAME, and only THEN do we check
12707 for a DWO file.
12708
1c658ad5 12709 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12710 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12711
3019eac3 12712static struct dwo_unit *
4ab09049 12713lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12714 ULONGEST signature, int is_debug_types)
3019eac3 12715{
4ab09049 12716 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12717 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12718 const char *kind = is_debug_types ? "TU" : "CU";
12719 void **dwo_file_slot;
3019eac3 12720 struct dwo_file *dwo_file;
80626a55 12721 struct dwp_file *dwp_file;
cb1df416 12722
6a506a2d
DE
12723 /* First see if there's a DWP file.
12724 If we have a DWP file but didn't find the DWO inside it, don't
12725 look for the original DWO file. It makes gdb behave differently
12726 depending on whether one is debugging in the build tree. */
cf2c3c16 12727
ed2dc618 12728 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12729 if (dwp_file != NULL)
cf2c3c16 12730 {
80626a55
DE
12731 const struct dwp_hash_table *dwp_htab =
12732 is_debug_types ? dwp_file->tus : dwp_file->cus;
12733
12734 if (dwp_htab != NULL)
12735 {
12736 struct dwo_unit *dwo_cutu =
ed2dc618 12737 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12738 signature, is_debug_types);
80626a55
DE
12739
12740 if (dwo_cutu != NULL)
12741 {
b4f54984 12742 if (dwarf_read_debug)
80626a55
DE
12743 {
12744 fprintf_unfiltered (gdb_stdlog,
12745 "Virtual DWO %s %s found: @%s\n",
12746 kind, hex_string (signature),
12747 host_address_to_string (dwo_cutu));
12748 }
12749 return dwo_cutu;
12750 }
12751 }
12752 }
6a506a2d 12753 else
80626a55 12754 {
6a506a2d 12755 /* No DWP file, look for the DWO file. */
80626a55 12756
ed2dc618
SM
12757 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12758 dwo_name, comp_dir);
6a506a2d 12759 if (*dwo_file_slot == NULL)
80626a55 12760 {
6a506a2d 12761 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12762 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12763 }
6a506a2d 12764 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12765 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12766
6a506a2d 12767 if (dwo_file != NULL)
19c3d4c9 12768 {
6a506a2d
DE
12769 struct dwo_unit *dwo_cutu = NULL;
12770
12771 if (is_debug_types && dwo_file->tus)
12772 {
12773 struct dwo_unit find_dwo_cutu;
12774
12775 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12776 find_dwo_cutu.signature = signature;
9a3c8263 12777 dwo_cutu
b0b6a987
TT
12778 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12779 &find_dwo_cutu);
6a506a2d 12780 }
33c5cd75 12781 else if (!is_debug_types && dwo_file->cus)
80626a55 12782 {
33c5cd75
DB
12783 struct dwo_unit find_dwo_cutu;
12784
12785 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12786 find_dwo_cutu.signature = signature;
b0b6a987 12787 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12788 &find_dwo_cutu);
6a506a2d
DE
12789 }
12790
12791 if (dwo_cutu != NULL)
12792 {
b4f54984 12793 if (dwarf_read_debug)
6a506a2d
DE
12794 {
12795 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12796 kind, dwo_name, hex_string (signature),
12797 host_address_to_string (dwo_cutu));
12798 }
12799 return dwo_cutu;
80626a55
DE
12800 }
12801 }
2e276125 12802 }
9cdd5dbd 12803
80626a55
DE
12804 /* We didn't find it. This could mean a dwo_id mismatch, or
12805 someone deleted the DWO/DWP file, or the search path isn't set up
12806 correctly to find the file. */
12807
b4f54984 12808 if (dwarf_read_debug)
80626a55
DE
12809 {
12810 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12811 kind, dwo_name, hex_string (signature));
12812 }
3019eac3 12813
6656a72d
DE
12814 /* This is a warning and not a complaint because it can be caused by
12815 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12816 {
12817 /* Print the name of the DWP file if we looked there, helps the user
12818 better diagnose the problem. */
791afaa2 12819 std::string dwp_text;
43942612
DE
12820
12821 if (dwp_file != NULL)
791afaa2
TT
12822 dwp_text = string_printf (" [in DWP file %s]",
12823 lbasename (dwp_file->name));
43942612 12824
9d8780f0 12825 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12826 " [in module %s]"),
4ab09049
SM
12827 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12828 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12829 }
3019eac3 12830 return NULL;
5fb290d7
DJ
12831}
12832
80626a55
DE
12833/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12834 See lookup_dwo_cutu_unit for details. */
12835
12836static struct dwo_unit *
4ab09049 12837lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12838 ULONGEST signature)
12839{
4ab09049
SM
12840 gdb_assert (!cu->per_cu->is_debug_types);
12841
12842 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12843}
12844
12845/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12846 See lookup_dwo_cutu_unit for details. */
12847
12848static struct dwo_unit *
4ab09049 12849lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12850{
4ab09049
SM
12851 gdb_assert (cu->per_cu->is_debug_types);
12852
12853 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12854
12855 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12856}
12857
89e63ee4
DE
12858/* Traversal function for queue_and_load_all_dwo_tus. */
12859
12860static int
12861queue_and_load_dwo_tu (void **slot, void *info)
12862{
12863 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12864 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12865 ULONGEST signature = dwo_unit->signature;
d460f660 12866 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12867
12868 if (sig_type != NULL)
12869 {
12870 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12871
12872 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12873 a real dependency of PER_CU on SIG_TYPE. That is detected later
12874 while processing PER_CU. */
120ce1b5 12875 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12876 load_full_type_unit (sig_cu, cu->per_objfile);
12877 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12878 }
12879
12880 return 1;
12881}
12882
12883/* Queue all TUs contained in the DWO of PER_CU to be read in.
12884 The DWO may have the only definition of the type, though it may not be
12885 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12886 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12887
12888static void
12889queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12890{
12891 struct dwo_unit *dwo_unit;
12892 struct dwo_file *dwo_file;
12893
12894 gdb_assert (!per_cu->is_debug_types);
89e63ee4 12895 gdb_assert (per_cu->cu != NULL);
64874a40 12896 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
89e63ee4
DE
12897
12898 dwo_unit = per_cu->cu->dwo_unit;
12899 gdb_assert (dwo_unit != NULL);
12900
12901 dwo_file = dwo_unit->dwo_file;
12902 if (dwo_file->tus != NULL)
b0b6a987 12903 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12904 per_cu->cu);
89e63ee4
DE
12905}
12906
3019eac3 12907/* Read in various DIEs. */
348e048f 12908
d389af10 12909/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12910 Inherit only the children of the DW_AT_abstract_origin DIE not being
12911 already referenced by DW_AT_abstract_origin from the children of the
12912 current DIE. */
d389af10
JK
12913
12914static void
12915inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12916{
12917 struct die_info *child_die;
791afaa2 12918 sect_offset *offsetp;
d389af10
JK
12919 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12920 struct die_info *origin_die;
12921 /* Iterator of the ORIGIN_DIE children. */
12922 struct die_info *origin_child_die;
d389af10 12923 struct attribute *attr;
cd02d79d
PA
12924 struct dwarf2_cu *origin_cu;
12925 struct pending **origin_previous_list_in_scope;
d389af10
JK
12926
12927 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12928 if (!attr)
12929 return;
12930
cd02d79d
PA
12931 /* Note that following die references may follow to a die in a
12932 different cu. */
12933
12934 origin_cu = cu;
12935 origin_die = follow_die_ref (die, attr, &origin_cu);
12936
12937 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12938 symbols in. */
12939 origin_previous_list_in_scope = origin_cu->list_in_scope;
12940 origin_cu->list_in_scope = cu->list_in_scope;
12941
edb3359d
DJ
12942 if (die->tag != origin_die->tag
12943 && !(die->tag == DW_TAG_inlined_subroutine
12944 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12945 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12946 sect_offset_str (die->sect_off),
12947 sect_offset_str (origin_die->sect_off));
d389af10 12948
791afaa2 12949 std::vector<sect_offset> offsets;
d389af10 12950
3ea89b92
PMR
12951 for (child_die = die->child;
12952 child_die && child_die->tag;
436c571c 12953 child_die = child_die->sibling)
3ea89b92
PMR
12954 {
12955 struct die_info *child_origin_die;
12956 struct dwarf2_cu *child_origin_cu;
12957
12958 /* We are trying to process concrete instance entries:
216f72a1 12959 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12960 it's not relevant to our analysis here. i.e. detecting DIEs that are
12961 present in the abstract instance but not referenced in the concrete
12962 one. */
216f72a1
JK
12963 if (child_die->tag == DW_TAG_call_site
12964 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12965 continue;
12966
c38f313d
DJ
12967 /* For each CHILD_DIE, find the corresponding child of
12968 ORIGIN_DIE. If there is more than one layer of
12969 DW_AT_abstract_origin, follow them all; there shouldn't be,
12970 but GCC versions at least through 4.4 generate this (GCC PR
12971 40573). */
3ea89b92
PMR
12972 child_origin_die = child_die;
12973 child_origin_cu = cu;
c38f313d
DJ
12974 while (1)
12975 {
cd02d79d
PA
12976 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12977 child_origin_cu);
c38f313d
DJ
12978 if (attr == NULL)
12979 break;
cd02d79d
PA
12980 child_origin_die = follow_die_ref (child_origin_die, attr,
12981 &child_origin_cu);
c38f313d
DJ
12982 }
12983
d389af10
JK
12984 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12985 counterpart may exist. */
c38f313d 12986 if (child_origin_die != child_die)
d389af10 12987 {
edb3359d
DJ
12988 if (child_die->tag != child_origin_die->tag
12989 && !(child_die->tag == DW_TAG_inlined_subroutine
12990 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12991 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12992 "different tags"),
9d8780f0
SM
12993 sect_offset_str (child_die->sect_off),
12994 sect_offset_str (child_origin_die->sect_off));
c38f313d 12995 if (child_origin_die->parent != origin_die)
b98664d3 12996 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12997 "different parents"),
9d8780f0
SM
12998 sect_offset_str (child_die->sect_off),
12999 sect_offset_str (child_origin_die->sect_off));
c38f313d 13000 else
791afaa2 13001 offsets.push_back (child_origin_die->sect_off);
d389af10 13002 }
d389af10 13003 }
791afaa2
TT
13004 std::sort (offsets.begin (), offsets.end ());
13005 sect_offset *offsets_end = offsets.data () + offsets.size ();
13006 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13007 if (offsetp[-1] == *offsetp)
b98664d3 13008 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13009 "to DIE %s as their abstract origin"),
13010 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13011
791afaa2 13012 offsetp = offsets.data ();
d389af10
JK
13013 origin_child_die = origin_die->child;
13014 while (origin_child_die && origin_child_die->tag)
13015 {
13016 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13017 while (offsetp < offsets_end
9c541725 13018 && *offsetp < origin_child_die->sect_off)
d389af10 13019 offsetp++;
b64f50a1 13020 if (offsetp >= offsets_end
9c541725 13021 || *offsetp > origin_child_die->sect_off)
d389af10 13022 {
adde2bff
DE
13023 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13024 Check whether we're already processing ORIGIN_CHILD_DIE.
13025 This can happen with mutually referenced abstract_origins.
13026 PR 16581. */
13027 if (!origin_child_die->in_process)
13028 process_die (origin_child_die, origin_cu);
d389af10 13029 }
436c571c 13030 origin_child_die = origin_child_die->sibling;
d389af10 13031 }
cd02d79d 13032 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13033
13034 if (cu != origin_cu)
13035 compute_delayed_physnames (origin_cu);
d389af10
JK
13036}
13037
c906108c 13038static void
e7c27a73 13039read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13040{
5e22e966 13041 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13042 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13043 struct context_stack *newobj;
c906108c
SS
13044 CORE_ADDR lowpc;
13045 CORE_ADDR highpc;
13046 struct die_info *child_die;
edb3359d 13047 struct attribute *attr, *call_line, *call_file;
15d034d0 13048 const char *name;
e142c38c 13049 CORE_ADDR baseaddr;
801e3a5b 13050 struct block *block;
edb3359d 13051 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13052 std::vector<struct symbol *> template_args;
34eaf542 13053 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13054
13055 if (inlined_func)
13056 {
13057 /* If we do not have call site information, we can't show the
13058 caller of this inlined function. That's too confusing, so
13059 only use the scope for local variables. */
13060 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13061 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13062 if (call_line == NULL || call_file == NULL)
13063 {
13064 read_lexical_block_scope (die, cu);
13065 return;
13066 }
13067 }
c906108c 13068
b3b3bada 13069 baseaddr = objfile->text_section_offset ();
e142c38c 13070
94af9270 13071 name = dwarf2_name (die, cu);
c906108c 13072
e8d05480
JB
13073 /* Ignore functions with missing or empty names. These are actually
13074 illegal according to the DWARF standard. */
13075 if (name == NULL)
13076 {
b98664d3 13077 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13078 sect_offset_str (die->sect_off));
e8d05480
JB
13079 return;
13080 }
13081
13082 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13083 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13084 <= PC_BOUNDS_INVALID)
e8d05480 13085 {
ae4d0c03
PM
13086 attr = dwarf2_attr (die, DW_AT_external, cu);
13087 if (!attr || !DW_UNSND (attr))
b98664d3 13088 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13089 "for subprogram DIE at %s"),
13090 sect_offset_str (die->sect_off));
e8d05480
JB
13091 return;
13092 }
c906108c 13093
3e29f34a
MR
13094 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13095 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13096
34eaf542
TT
13097 /* If we have any template arguments, then we must allocate a
13098 different sort of symbol. */
436c571c 13099 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13100 {
13101 if (child_die->tag == DW_TAG_template_type_param
13102 || child_die->tag == DW_TAG_template_value_param)
13103 {
8c14c3a3 13104 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13105 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13106 break;
13107 }
13108 }
13109
c24bdb02 13110 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13111 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13112 (struct symbol *) templ_func);
4c2df51b 13113
81873cc8 13114 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13115 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13116 cu->language);
13117
4cecd739
DJ
13118 /* If there is a location expression for DW_AT_frame_base, record
13119 it. */
e142c38c 13120 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13121 if (attr != nullptr)
fe978cb0 13122 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13123
63e43d3a
PMR
13124 /* If there is a location for the static link, record it. */
13125 newobj->static_link = NULL;
13126 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13127 if (attr != nullptr)
63e43d3a 13128 {
224c3ddb
SM
13129 newobj->static_link
13130 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13131 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13132 cu->addr_type ());
63e43d3a
PMR
13133 }
13134
c24bdb02 13135 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13136
639d11d3 13137 if (die->child != NULL)
c906108c 13138 {
639d11d3 13139 child_die = die->child;
c906108c
SS
13140 while (child_die && child_die->tag)
13141 {
34eaf542
TT
13142 if (child_die->tag == DW_TAG_template_type_param
13143 || child_die->tag == DW_TAG_template_value_param)
13144 {
13145 struct symbol *arg = new_symbol (child_die, NULL, cu);
13146
f1078f66 13147 if (arg != NULL)
2f4732b0 13148 template_args.push_back (arg);
34eaf542
TT
13149 }
13150 else
13151 process_die (child_die, cu);
436c571c 13152 child_die = child_die->sibling;
c906108c
SS
13153 }
13154 }
13155
d389af10
JK
13156 inherit_abstract_dies (die, cu);
13157
4a811a97
UW
13158 /* If we have a DW_AT_specification, we might need to import using
13159 directives from the context of the specification DIE. See the
13160 comment in determine_prefix. */
13161 if (cu->language == language_cplus
13162 && dwarf2_attr (die, DW_AT_specification, cu))
13163 {
13164 struct dwarf2_cu *spec_cu = cu;
13165 struct die_info *spec_die = die_specification (die, &spec_cu);
13166
13167 while (spec_die)
13168 {
13169 child_die = spec_die->child;
13170 while (child_die && child_die->tag)
13171 {
13172 if (child_die->tag == DW_TAG_imported_module)
13173 process_die (child_die, spec_cu);
436c571c 13174 child_die = child_die->sibling;
4a811a97
UW
13175 }
13176
13177 /* In some cases, GCC generates specification DIEs that
13178 themselves contain DW_AT_specification attributes. */
13179 spec_die = die_specification (spec_die, &spec_cu);
13180 }
13181 }
13182
c24bdb02 13183 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13184 /* Make a block for the local symbols within. */
c24bdb02 13185 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13186 cstk.static_link, lowpc, highpc);
801e3a5b 13187
df8a16a1 13188 /* For C++, set the block's scope. */
45280282
IB
13189 if ((cu->language == language_cplus
13190 || cu->language == language_fortran
c44af4eb
TT
13191 || cu->language == language_d
13192 || cu->language == language_rust)
4d4ec4e5 13193 && cu->processing_has_namespace_info)
195a3f6c
TT
13194 block_set_scope (block, determine_prefix (die, cu),
13195 &objfile->objfile_obstack);
df8a16a1 13196
801e3a5b
JB
13197 /* If we have address ranges, record them. */
13198 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13199
a60f3166 13200 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13201
34eaf542 13202 /* Attach template arguments to function. */
2f4732b0 13203 if (!template_args.empty ())
34eaf542
TT
13204 {
13205 gdb_assert (templ_func != NULL);
13206
2f4732b0 13207 templ_func->n_template_arguments = template_args.size ();
34eaf542 13208 templ_func->template_arguments
8d749320
SM
13209 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13210 templ_func->n_template_arguments);
34eaf542 13211 memcpy (templ_func->template_arguments,
2f4732b0 13212 template_args.data (),
34eaf542 13213 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13214
13215 /* Make sure that the symtab is set on the new symbols. Even
13216 though they don't appear in this symtab directly, other parts
13217 of gdb assume that symbols do, and this is reasonably
13218 true. */
8634679f 13219 for (symbol *sym : template_args)
3e1d3d8c 13220 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13221 }
13222
208d8187
JB
13223 /* In C++, we can have functions nested inside functions (e.g., when
13224 a function declares a class that has methods). This means that
13225 when we finish processing a function scope, we may need to go
13226 back to building a containing block's symbol lists. */
c24bdb02
KS
13227 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13228 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13229
921e78cf
JB
13230 /* If we've finished processing a top-level function, subsequent
13231 symbols go in the file symbol list. */
c24bdb02
KS
13232 if (cu->get_builder ()->outermost_context_p ())
13233 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13234}
13235
13236/* Process all the DIES contained within a lexical block scope. Start
13237 a new scope, process the dies, and then close the scope. */
13238
13239static void
e7c27a73 13240read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13241{
5e22e966 13242 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13243 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13244 CORE_ADDR lowpc, highpc;
13245 struct die_info *child_die;
e142c38c
DJ
13246 CORE_ADDR baseaddr;
13247
b3b3bada 13248 baseaddr = objfile->text_section_offset ();
c906108c
SS
13249
13250 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13251 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13252 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13253 be nasty. Might be easier to properly extend generic blocks to
af34e669 13254 describe ranges. */
e385593e
JK
13255 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13256 {
13257 case PC_BOUNDS_NOT_PRESENT:
13258 /* DW_TAG_lexical_block has no attributes, process its children as if
13259 there was no wrapping by that DW_TAG_lexical_block.
13260 GCC does no longer produces such DWARF since GCC r224161. */
13261 for (child_die = die->child;
13262 child_die != NULL && child_die->tag;
436c571c 13263 child_die = child_die->sibling)
4f7bc5ed
TT
13264 {
13265 /* We might already be processing this DIE. This can happen
13266 in an unusual circumstance -- where a subroutine A
13267 appears lexically in another subroutine B, but A actually
13268 inlines B. The recursion is broken here, rather than in
13269 inherit_abstract_dies, because it seems better to simply
13270 drop concrete children here. */
13271 if (!child_die->in_process)
13272 process_die (child_die, cu);
13273 }
e385593e
JK
13274 return;
13275 case PC_BOUNDS_INVALID:
13276 return;
13277 }
3e29f34a
MR
13278 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13279 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13280
c24bdb02 13281 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13282 if (die->child != NULL)
c906108c 13283 {
639d11d3 13284 child_die = die->child;
c906108c
SS
13285 while (child_die && child_die->tag)
13286 {
e7c27a73 13287 process_die (child_die, cu);
436c571c 13288 child_die = child_die->sibling;
c906108c
SS
13289 }
13290 }
3ea89b92 13291 inherit_abstract_dies (die, cu);
c24bdb02 13292 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13293
c24bdb02
KS
13294 if (*cu->get_builder ()->get_local_symbols () != NULL
13295 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13296 {
801e3a5b 13297 struct block *block
c24bdb02 13298 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13299 cstk.start_addr, highpc);
801e3a5b
JB
13300
13301 /* Note that recording ranges after traversing children, as we
13302 do here, means that recording a parent's ranges entails
13303 walking across all its children's ranges as they appear in
13304 the address map, which is quadratic behavior.
13305
13306 It would be nicer to record the parent's ranges before
13307 traversing its children, simply overriding whatever you find
13308 there. But since we don't even decide whether to create a
13309 block until after we've traversed its children, that's hard
13310 to do. */
13311 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13312 }
c24bdb02
KS
13313 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13314 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13315}
13316
216f72a1 13317/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13318
13319static void
13320read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13321{
5e22e966 13322 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13323 struct objfile *objfile = per_objfile->objfile;
08feed99 13324 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13325 CORE_ADDR pc, baseaddr;
13326 struct attribute *attr;
13327 struct call_site *call_site, call_site_local;
13328 void **slot;
13329 int nparams;
13330 struct die_info *child_die;
13331
b3b3bada 13332 baseaddr = objfile->text_section_offset ();
96408a79 13333
216f72a1
JK
13334 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13335 if (attr == NULL)
13336 {
13337 /* This was a pre-DWARF-5 GNU extension alias
13338 for DW_AT_call_return_pc. */
13339 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13340 }
96408a79
SA
13341 if (!attr)
13342 {
b98664d3 13343 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13344 "DIE %s [in module %s]"),
13345 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13346 return;
13347 }
cd6c91b4 13348 pc = attr->value_as_address () + baseaddr;
3e29f34a 13349 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13350
13351 if (cu->call_site_htab == NULL)
13352 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13353 NULL, &objfile->objfile_obstack,
13354 hashtab_obstack_allocate, NULL);
13355 call_site_local.pc = pc;
13356 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13357 if (*slot != NULL)
13358 {
b98664d3 13359 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13360 "DIE %s [in module %s]"),
13361 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13362 objfile_name (objfile));
96408a79
SA
13363 return;
13364 }
13365
13366 /* Count parameters at the caller. */
13367
13368 nparams = 0;
13369 for (child_die = die->child; child_die && child_die->tag;
436c571c 13370 child_die = child_die->sibling)
96408a79 13371 {
216f72a1
JK
13372 if (child_die->tag != DW_TAG_call_site_parameter
13373 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13374 {
b98664d3 13375 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13376 "DW_TAG_call_site child DIE %s [in module %s]"),
13377 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13378 objfile_name (objfile));
96408a79
SA
13379 continue;
13380 }
13381
13382 nparams++;
13383 }
13384
224c3ddb
SM
13385 call_site
13386 = ((struct call_site *)
13387 obstack_alloc (&objfile->objfile_obstack,
13388 sizeof (*call_site)
13389 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13390 *slot = call_site;
13391 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13392 call_site->pc = pc;
13393
216f72a1
JK
13394 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13395 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13396 {
13397 struct die_info *func_die;
13398
13399 /* Skip also over DW_TAG_inlined_subroutine. */
13400 for (func_die = die->parent;
13401 func_die && func_die->tag != DW_TAG_subprogram
13402 && func_die->tag != DW_TAG_subroutine_type;
13403 func_die = func_die->parent);
13404
216f72a1
JK
13405 /* DW_AT_call_all_calls is a superset
13406 of DW_AT_call_all_tail_calls. */
96408a79 13407 if (func_die
216f72a1 13408 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13409 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13410 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13411 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13412 {
13413 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13414 not complete. But keep CALL_SITE for look ups via call_site_htab,
13415 both the initial caller containing the real return address PC and
13416 the final callee containing the current PC of a chain of tail
13417 calls do not need to have the tail call list complete. But any
13418 function candidate for a virtual tail call frame searched via
13419 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13420 determined unambiguously. */
13421 }
13422 else
13423 {
13424 struct type *func_type = NULL;
13425
13426 if (func_die)
13427 func_type = get_die_type (func_die, cu);
13428 if (func_type != NULL)
13429 {
78134374 13430 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13431
13432 /* Enlist this call site to the function. */
13433 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13434 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13435 }
13436 else
b98664d3 13437 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13438 "DIE %s [in module %s]"),
13439 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13440 }
13441 }
13442
216f72a1
JK
13443 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13444 if (attr == NULL)
13445 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13446 if (attr == NULL)
13447 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13448 if (attr == NULL)
216f72a1
JK
13449 {
13450 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13451 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13452 }
96408a79 13453 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13454 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13455 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13456 else if (attr->form_is_block ())
96408a79
SA
13457 {
13458 struct dwarf2_locexpr_baton *dlbaton;
13459
8d749320 13460 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13461 dlbaton->data = DW_BLOCK (attr)->data;
13462 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13463 dlbaton->per_objfile = per_objfile;
96408a79
SA
13464 dlbaton->per_cu = cu->per_cu;
13465
13466 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13467 }
cd6c91b4 13468 else if (attr->form_is_ref ())
96408a79 13469 {
96408a79
SA
13470 struct dwarf2_cu *target_cu = cu;
13471 struct die_info *target_die;
13472
ac9ec31b 13473 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13474 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13475 if (die_is_declaration (target_die, target_cu))
13476 {
7d45c7c3 13477 const char *target_physname;
9112db09
JK
13478
13479 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13480 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13481 if (target_physname == NULL)
9112db09 13482 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13483 if (target_physname == NULL)
b98664d3 13484 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13485 "physname, for referencing DIE %s [in module %s]"),
13486 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13487 else
7d455152 13488 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13489 }
13490 else
13491 {
13492 CORE_ADDR lowpc;
13493
13494 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13495 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13496 <= PC_BOUNDS_INVALID)
b98664d3 13497 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13498 "low pc, for referencing DIE %s [in module %s]"),
13499 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13500 else
3e29f34a
MR
13501 {
13502 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13503 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13504 }
96408a79
SA
13505 }
13506 }
13507 else
b98664d3 13508 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13509 "block nor reference, for DIE %s [in module %s]"),
13510 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13511
13512 call_site->per_cu = cu->per_cu;
9f47c707 13513 call_site->per_objfile = per_objfile;
96408a79
SA
13514
13515 for (child_die = die->child;
13516 child_die && child_die->tag;
436c571c 13517 child_die = child_die->sibling)
96408a79 13518 {
96408a79 13519 struct call_site_parameter *parameter;
1788b2d3 13520 struct attribute *loc, *origin;
96408a79 13521
216f72a1
JK
13522 if (child_die->tag != DW_TAG_call_site_parameter
13523 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13524 {
13525 /* Already printed the complaint above. */
13526 continue;
13527 }
13528
13529 gdb_assert (call_site->parameter_count < nparams);
13530 parameter = &call_site->parameter[call_site->parameter_count];
13531
1788b2d3
JK
13532 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13533 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13534 register is contained in DW_AT_call_value. */
96408a79 13535
24c5c679 13536 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13537 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13538 if (origin == NULL)
13539 {
13540 /* This was a pre-DWARF-5 GNU extension alias
13541 for DW_AT_call_parameter. */
13542 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13543 }
cd6c91b4 13544 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13545 {
1788b2d3 13546 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13547
0826b30a 13548 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13549 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13550 {
13551 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13552 binding can be done only inside one CU. Such referenced DIE
13553 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13554 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13555 "DW_TAG_call_site child DIE %s [in module %s]"),
13556 sect_offset_str (child_die->sect_off),
9c541725 13557 objfile_name (objfile));
d76b7dbc
JK
13558 continue;
13559 }
9c541725
PA
13560 parameter->u.param_cu_off
13561 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13562 }
4fc6c0d5 13563 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13564 {
b98664d3 13565 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13566 "DW_TAG_call_site child DIE %s [in module %s]"),
13567 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13568 continue;
13569 }
24c5c679 13570 else
96408a79 13571 {
24c5c679
JK
13572 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13573 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13574 if (parameter->u.dwarf_reg != -1)
13575 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13576 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13577 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13578 &parameter->u.fb_offset))
13579 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13580 else
13581 {
b98664d3 13582 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13583 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13584 "DW_TAG_call_site child DIE %s "
24c5c679 13585 "[in module %s]"),
9d8780f0 13586 sect_offset_str (child_die->sect_off),
9c541725 13587 objfile_name (objfile));
24c5c679
JK
13588 continue;
13589 }
96408a79
SA
13590 }
13591
216f72a1
JK
13592 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13593 if (attr == NULL)
13594 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13595 if (attr == NULL || !attr->form_is_block ())
96408a79 13596 {
b98664d3 13597 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13598 "DW_TAG_call_site child DIE %s [in module %s]"),
13599 sect_offset_str (child_die->sect_off),
9c541725 13600 objfile_name (objfile));
96408a79
SA
13601 continue;
13602 }
13603 parameter->value = DW_BLOCK (attr)->data;
13604 parameter->value_size = DW_BLOCK (attr)->size;
13605
13606 /* Parameters are not pre-cleared by memset above. */
13607 parameter->data_value = NULL;
13608 parameter->data_value_size = 0;
13609 call_site->parameter_count++;
13610
216f72a1
JK
13611 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13612 if (attr == NULL)
13613 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13614 if (attr != nullptr)
96408a79 13615 {
4fc6c0d5 13616 if (!attr->form_is_block ())
b98664d3 13617 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13618 "DW_TAG_call_site child DIE %s [in module %s]"),
13619 sect_offset_str (child_die->sect_off),
9c541725 13620 objfile_name (objfile));
96408a79
SA
13621 else
13622 {
13623 parameter->data_value = DW_BLOCK (attr)->data;
13624 parameter->data_value_size = DW_BLOCK (attr)->size;
13625 }
13626 }
13627 }
13628}
13629
71a3c369
TT
13630/* Helper function for read_variable. If DIE represents a virtual
13631 table, then return the type of the concrete object that is
13632 associated with the virtual table. Otherwise, return NULL. */
13633
13634static struct type *
13635rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13636{
13637 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13638 if (attr == NULL)
13639 return NULL;
13640
13641 /* Find the type DIE. */
13642 struct die_info *type_die = NULL;
13643 struct dwarf2_cu *type_cu = cu;
13644
cd6c91b4 13645 if (attr->form_is_ref ())
71a3c369
TT
13646 type_die = follow_die_ref (die, attr, &type_cu);
13647 if (type_die == NULL)
13648 return NULL;
13649
13650 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13651 return NULL;
13652 return die_containing_type (type_die, type_cu);
13653}
13654
13655/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13656
13657static void
13658read_variable (struct die_info *die, struct dwarf2_cu *cu)
13659{
13660 struct rust_vtable_symbol *storage = NULL;
13661
13662 if (cu->language == language_rust)
13663 {
13664 struct type *containing_type = rust_containing_type (die, cu);
13665
13666 if (containing_type != NULL)
13667 {
5e22e966 13668 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13669
8c14c3a3 13670 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13671 storage->concrete_type = containing_type;
cf724bc9 13672 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13673 }
13674 }
13675
e4a62c65
TV
13676 struct symbol *res = new_symbol (die, NULL, cu, storage);
13677 struct attribute *abstract_origin
13678 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13679 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13680 if (res == NULL && loc && abstract_origin)
13681 {
13682 /* We have a variable without a name, but with a location and an abstract
13683 origin. This may be a concrete instance of an abstract variable
13684 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13685 later. */
13686 struct dwarf2_cu *origin_cu = cu;
13687 struct die_info *origin_die
13688 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13689 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13690 per_objfile->per_bfd->abstract_to_concrete
13691 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13692 }
71a3c369
TT
13693}
13694
43988095
JK
13695/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13696 reading .debug_rnglists.
13697 Callback's type should be:
13698 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13699 Return true if the attributes are present and valid, otherwise,
13700 return false. */
13701
13702template <typename Callback>
13703static bool
13704dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13705 Callback &&callback)
13706{
5e22e966 13707 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13708 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13709 bfd *obfd = objfile->obfd;
43988095 13710 /* Base address selection entry. */
2b24b6e4 13711 gdb::optional<CORE_ADDR> base;
43988095 13712 const gdb_byte *buffer;
43988095
JK
13713 CORE_ADDR baseaddr;
13714 bool overflow = false;
13715
43988095
JK
13716 base = cu->base_address;
13717
5989a64e
SM
13718 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13719 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13720 {
b98664d3 13721 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13722 offset);
13723 return false;
13724 }
5989a64e 13725 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13726
b3b3bada 13727 baseaddr = objfile->text_section_offset ();
43988095
JK
13728
13729 while (1)
13730 {
7814882a
JK
13731 /* Initialize it due to a false compiler warning. */
13732 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13733 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13734 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13735 unsigned int bytes_read;
13736
13737 if (buffer == buf_end)
13738 {
13739 overflow = true;
13740 break;
13741 }
13742 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13743 switch (rlet)
13744 {
13745 case DW_RLE_end_of_list:
13746 break;
13747 case DW_RLE_base_address:
13748 if (buffer + cu->header.addr_size > buf_end)
13749 {
13750 overflow = true;
13751 break;
13752 }
c8a7a66f 13753 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13754 buffer += bytes_read;
13755 break;
13756 case DW_RLE_start_length:
13757 if (buffer + cu->header.addr_size > buf_end)
13758 {
13759 overflow = true;
13760 break;
13761 }
c8a7a66f
TT
13762 range_beginning = cu->header.read_address (obfd, buffer,
13763 &bytes_read);
43988095
JK
13764 buffer += bytes_read;
13765 range_end = (range_beginning
13766 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13767 buffer += bytes_read;
13768 if (buffer > buf_end)
13769 {
13770 overflow = true;
13771 break;
13772 }
13773 break;
13774 case DW_RLE_offset_pair:
13775 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13776 buffer += bytes_read;
13777 if (buffer > buf_end)
13778 {
13779 overflow = true;
13780 break;
13781 }
13782 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13783 buffer += bytes_read;
13784 if (buffer > buf_end)
13785 {
13786 overflow = true;
13787 break;
13788 }
13789 break;
13790 case DW_RLE_start_end:
13791 if (buffer + 2 * cu->header.addr_size > buf_end)
13792 {
13793 overflow = true;
13794 break;
13795 }
c8a7a66f
TT
13796 range_beginning = cu->header.read_address (obfd, buffer,
13797 &bytes_read);
43988095 13798 buffer += bytes_read;
c8a7a66f 13799 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13800 buffer += bytes_read;
13801 break;
13802 default:
b98664d3 13803 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13804 return false;
13805 }
13806 if (rlet == DW_RLE_end_of_list || overflow)
13807 break;
13808 if (rlet == DW_RLE_base_address)
13809 continue;
13810
2b24b6e4 13811 if (!base.has_value ())
43988095
JK
13812 {
13813 /* We have no valid base address for the ranges
13814 data. */
b98664d3 13815 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13816 return false;
13817 }
13818
13819 if (range_beginning > range_end)
13820 {
13821 /* Inverted range entries are invalid. */
b98664d3 13822 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13823 return false;
13824 }
13825
13826 /* Empty range entries have no effect. */
13827 if (range_beginning == range_end)
13828 continue;
13829
2b24b6e4
TT
13830 range_beginning += *base;
13831 range_end += *base;
43988095
JK
13832
13833 /* A not-uncommon case of bad debug info.
13834 Don't pollute the addrmap with bad data. */
13835 if (range_beginning + baseaddr == 0
5989a64e 13836 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13837 {
b98664d3 13838 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13839 " [in module %s]"), objfile_name (objfile));
13840 continue;
13841 }
13842
13843 callback (range_beginning, range_end);
13844 }
13845
13846 if (overflow)
13847 {
b98664d3 13848 complaint (_("Offset %d is not terminated "
43988095
JK
13849 "for DW_AT_ranges attribute"),
13850 offset);
13851 return false;
13852 }
13853
13854 return true;
13855}
13856
13857/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13858 Callback's type should be:
13859 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13860 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13861
43988095 13862template <typename Callback>
43039443 13863static int
5f46c5a5 13864dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13865 Callback &&callback)
43039443 13866{
5e22e966
SM
13867 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13868 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13869 struct comp_unit_head *cu_header = &cu->header;
13870 bfd *obfd = objfile->obfd;
13871 unsigned int addr_size = cu_header->addr_size;
13872 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13873 /* Base address selection entry. */
2b24b6e4 13874 gdb::optional<CORE_ADDR> base;
43039443 13875 unsigned int dummy;
d521ce57 13876 const gdb_byte *buffer;
ff013f42 13877 CORE_ADDR baseaddr;
43039443 13878
43988095
JK
13879 if (cu_header->version >= 5)
13880 return dwarf2_rnglists_process (offset, cu, callback);
13881
d00adf39 13882 base = cu->base_address;
43039443 13883
5e22e966
SM
13884 per_objfile->per_bfd->ranges.read (objfile);
13885 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13886 {
b98664d3 13887 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13888 offset);
13889 return 0;
13890 }
5e22e966 13891 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13892
b3b3bada 13893 baseaddr = objfile->text_section_offset ();
ff013f42 13894
43039443
JK
13895 while (1)
13896 {
13897 CORE_ADDR range_beginning, range_end;
13898
c8a7a66f 13899 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13900 buffer += addr_size;
c8a7a66f 13901 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13902 buffer += addr_size;
13903 offset += 2 * addr_size;
13904
13905 /* An end of list marker is a pair of zero addresses. */
13906 if (range_beginning == 0 && range_end == 0)
13907 /* Found the end of list entry. */
13908 break;
13909
13910 /* Each base address selection entry is a pair of 2 values.
13911 The first is the largest possible address, the second is
13912 the base address. Check for a base address here. */
13913 if ((range_beginning & mask) == mask)
13914 {
28d2bfb9
AB
13915 /* If we found the largest possible address, then we already
13916 have the base address in range_end. */
13917 base = range_end;
43039443
JK
13918 continue;
13919 }
13920
2b24b6e4 13921 if (!base.has_value ())
43039443
JK
13922 {
13923 /* We have no valid base address for the ranges
13924 data. */
b98664d3 13925 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13926 return 0;
13927 }
13928
9277c30c
UW
13929 if (range_beginning > range_end)
13930 {
13931 /* Inverted range entries are invalid. */
b98664d3 13932 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13933 return 0;
13934 }
13935
13936 /* Empty range entries have no effect. */
13937 if (range_beginning == range_end)
13938 continue;
13939
2b24b6e4
TT
13940 range_beginning += *base;
13941 range_end += *base;
43039443 13942
01093045
DE
13943 /* A not-uncommon case of bad debug info.
13944 Don't pollute the addrmap with bad data. */
13945 if (range_beginning + baseaddr == 0
5e22e966 13946 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13947 {
b98664d3 13948 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13949 " [in module %s]"), objfile_name (objfile));
01093045
DE
13950 continue;
13951 }
13952
5f46c5a5
JK
13953 callback (range_beginning, range_end);
13954 }
13955
13956 return 1;
13957}
13958
13959/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13960 Return 1 if the attributes are present and valid, otherwise, return 0.
13961 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13962
13963static int
13964dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13965 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13966 dwarf2_psymtab *ranges_pst)
5f46c5a5 13967{
5e22e966 13968 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13969 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13970 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13971 int low_set = 0;
13972 CORE_ADDR low = 0;
13973 CORE_ADDR high = 0;
13974 int retval;
13975
13976 retval = dwarf2_ranges_process (offset, cu,
13977 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13978 {
9277c30c 13979 if (ranges_pst != NULL)
3e29f34a
MR
13980 {
13981 CORE_ADDR lowpc;
13982 CORE_ADDR highpc;
13983
79748972
TT
13984 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13985 range_beginning + baseaddr)
13986 - baseaddr);
13987 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13988 range_end + baseaddr)
13989 - baseaddr);
d320c2b5
TT
13990 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13991 lowpc, highpc - 1, ranges_pst);
3e29f34a 13992 }
ff013f42 13993
43039443
JK
13994 /* FIXME: This is recording everything as a low-high
13995 segment of consecutive addresses. We should have a
13996 data structure for discontiguous block ranges
13997 instead. */
13998 if (! low_set)
13999 {
14000 low = range_beginning;
14001 high = range_end;
14002 low_set = 1;
14003 }
14004 else
14005 {
14006 if (range_beginning < low)
14007 low = range_beginning;
14008 if (range_end > high)
14009 high = range_end;
14010 }
5f46c5a5
JK
14011 });
14012 if (!retval)
14013 return 0;
43039443
JK
14014
14015 if (! low_set)
14016 /* If the first entry is an end-of-list marker, the range
14017 describes an empty scope, i.e. no instructions. */
14018 return 0;
14019
14020 if (low_return)
14021 *low_return = low;
14022 if (high_return)
14023 *high_return = high;
14024 return 1;
14025}
14026
3a2b436a
JK
14027/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14028 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14029 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14030
3a2b436a 14031static enum pc_bounds_kind
af34e669 14032dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14033 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14034 dwarf2_psymtab *pst)
c906108c 14035{
5e22e966 14036 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14037 struct attribute *attr;
91da1414 14038 struct attribute *attr_high;
af34e669
DJ
14039 CORE_ADDR low = 0;
14040 CORE_ADDR high = 0;
e385593e 14041 enum pc_bounds_kind ret;
c906108c 14042
91da1414
MW
14043 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14044 if (attr_high)
af34e669 14045 {
e142c38c 14046 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14047 if (attr != nullptr)
91da1414 14048 {
cd6c91b4
TT
14049 low = attr->value_as_address ();
14050 high = attr_high->value_as_address ();
14051 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14052 high += low;
91da1414 14053 }
af34e669
DJ
14054 else
14055 /* Found high w/o low attribute. */
e385593e 14056 return PC_BOUNDS_INVALID;
af34e669
DJ
14057
14058 /* Found consecutive range of addresses. */
3a2b436a 14059 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14060 }
c906108c 14061 else
af34e669 14062 {
e142c38c 14063 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14064 if (attr != NULL)
14065 {
18a8505e 14066 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14067 We take advantage of the fact that DW_AT_ranges does not appear
14068 in DW_TAG_compile_unit of DWO files. */
14069 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14070 unsigned int ranges_offset = (DW_UNSND (attr)
14071 + (need_ranges_base
14072 ? cu->ranges_base
14073 : 0));
2e3cf129 14074
af34e669 14075 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14076 .debug_ranges section. */
2e3cf129 14077 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14078 return PC_BOUNDS_INVALID;
43039443 14079 /* Found discontinuous range of addresses. */
3a2b436a 14080 ret = PC_BOUNDS_RANGES;
af34e669 14081 }
e385593e
JK
14082 else
14083 return PC_BOUNDS_NOT_PRESENT;
af34e669 14084 }
c906108c 14085
48fbe735 14086 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14087 if (high <= low)
e385593e 14088 return PC_BOUNDS_INVALID;
c906108c
SS
14089
14090 /* When using the GNU linker, .gnu.linkonce. sections are used to
14091 eliminate duplicate copies of functions and vtables and such.
14092 The linker will arbitrarily choose one and discard the others.
14093 The AT_*_pc values for such functions refer to local labels in
14094 these sections. If the section from that file was discarded, the
14095 labels are not in the output, so the relocs get a value of 0.
14096 If this is a discarded function, mark the pc bounds as invalid,
14097 so that GDB will ignore it. */
5989a64e 14098 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14099 return PC_BOUNDS_INVALID;
c906108c
SS
14100
14101 *lowpc = low;
96408a79
SA
14102 if (highpc)
14103 *highpc = high;
af34e669 14104 return ret;
c906108c
SS
14105}
14106
b084d499
JB
14107/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14108 its low and high PC addresses. Do nothing if these addresses could not
14109 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14110 and HIGHPC to the high address if greater than HIGHPC. */
14111
14112static void
14113dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14114 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14115 struct dwarf2_cu *cu)
14116{
14117 CORE_ADDR low, high;
14118 struct die_info *child = die->child;
14119
e385593e 14120 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14121 {
325fac50
PA
14122 *lowpc = std::min (*lowpc, low);
14123 *highpc = std::max (*highpc, high);
b084d499
JB
14124 }
14125
14126 /* If the language does not allow nested subprograms (either inside
14127 subprograms or lexical blocks), we're done. */
14128 if (cu->language != language_ada)
14129 return;
6e70227d 14130
b084d499
JB
14131 /* Check all the children of the given DIE. If it contains nested
14132 subprograms, then check their pc bounds. Likewise, we need to
14133 check lexical blocks as well, as they may also contain subprogram
14134 definitions. */
14135 while (child && child->tag)
14136 {
14137 if (child->tag == DW_TAG_subprogram
14138 || child->tag == DW_TAG_lexical_block)
14139 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14140 child = child->sibling;
b084d499
JB
14141 }
14142}
14143
fae299cd
DC
14144/* Get the low and high pc's represented by the scope DIE, and store
14145 them in *LOWPC and *HIGHPC. If the correct values can't be
14146 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14147
14148static void
14149get_scope_pc_bounds (struct die_info *die,
14150 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14151 struct dwarf2_cu *cu)
14152{
14153 CORE_ADDR best_low = (CORE_ADDR) -1;
14154 CORE_ADDR best_high = (CORE_ADDR) 0;
14155 CORE_ADDR current_low, current_high;
14156
3a2b436a 14157 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14158 >= PC_BOUNDS_RANGES)
fae299cd
DC
14159 {
14160 best_low = current_low;
14161 best_high = current_high;
14162 }
14163 else
14164 {
14165 struct die_info *child = die->child;
14166
14167 while (child && child->tag)
14168 {
14169 switch (child->tag) {
14170 case DW_TAG_subprogram:
b084d499 14171 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14172 break;
14173 case DW_TAG_namespace:
f55ee35c 14174 case DW_TAG_module:
fae299cd
DC
14175 /* FIXME: carlton/2004-01-16: Should we do this for
14176 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14177 that current GCC's always emit the DIEs corresponding
14178 to definitions of methods of classes as children of a
14179 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14180 the DIEs giving the declarations, which could be
14181 anywhere). But I don't see any reason why the
14182 standards says that they have to be there. */
14183 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14184
14185 if (current_low != ((CORE_ADDR) -1))
14186 {
325fac50
PA
14187 best_low = std::min (best_low, current_low);
14188 best_high = std::max (best_high, current_high);
fae299cd
DC
14189 }
14190 break;
14191 default:
0963b4bd 14192 /* Ignore. */
fae299cd
DC
14193 break;
14194 }
14195
436c571c 14196 child = child->sibling;
fae299cd
DC
14197 }
14198 }
14199
14200 *lowpc = best_low;
14201 *highpc = best_high;
14202}
14203
801e3a5b
JB
14204/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14205 in DIE. */
380bca97 14206
801e3a5b
JB
14207static void
14208dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14209 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14210{
5e22e966 14211 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14212 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14213 struct attribute *attr;
91da1414 14214 struct attribute *attr_high;
801e3a5b 14215
91da1414
MW
14216 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14217 if (attr_high)
801e3a5b 14218 {
801e3a5b 14219 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14220 if (attr != nullptr)
801e3a5b 14221 {
cd6c91b4
TT
14222 CORE_ADDR low = attr->value_as_address ();
14223 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14224
cd6c91b4 14225 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14226 high += low;
9a619af0 14227
3e29f34a
MR
14228 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14229 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14230 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14231 }
14232 }
14233
14234 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14235 if (attr != nullptr)
801e3a5b 14236 {
18a8505e 14237 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14238 We take advantage of the fact that DW_AT_ranges does not appear
14239 in DW_TAG_compile_unit of DWO files. */
14240 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14241
14242 /* The value of the DW_AT_ranges attribute is the offset of the
14243 address range list in the .debug_ranges section. */
ab435259
DE
14244 unsigned long offset = (DW_UNSND (attr)
14245 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14246
2d5f09ec 14247 std::vector<blockrange> blockvec;
5f46c5a5
JK
14248 dwarf2_ranges_process (offset, cu,
14249 [&] (CORE_ADDR start, CORE_ADDR end)
14250 {
58fdfd2c
JK
14251 start += baseaddr;
14252 end += baseaddr;
5f46c5a5
JK
14253 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14254 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14255 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14256 blockvec.emplace_back (start, end);
5f46c5a5 14257 });
2d5f09ec
KB
14258
14259 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14260 }
14261}
14262
685b1105
JK
14263/* Check whether the producer field indicates either of GCC < 4.6, or the
14264 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14265
685b1105
JK
14266static void
14267check_producer (struct dwarf2_cu *cu)
60d5a603 14268{
38360086 14269 int major, minor;
60d5a603
JK
14270
14271 if (cu->producer == NULL)
14272 {
14273 /* For unknown compilers expect their behavior is DWARF version
14274 compliant.
14275
14276 GCC started to support .debug_types sections by -gdwarf-4 since
14277 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14278 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14279 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14280 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14281 }
b1ffba5a 14282 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14283 {
38360086
MW
14284 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14285 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14286 }
5230b05a 14287 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14288 {
14289 cu->producer_is_icc = true;
14290 cu->producer_is_icc_lt_14 = major < 14;
14291 }
c258c396
JD
14292 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14293 cu->producer_is_codewarrior = true;
685b1105
JK
14294 else
14295 {
14296 /* For other non-GCC compilers, expect their behavior is DWARF version
14297 compliant. */
60d5a603
JK
14298 }
14299
9068261f 14300 cu->checked_producer = true;
685b1105 14301}
ba919b58 14302
685b1105
JK
14303/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14304 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14305 during 4.6.0 experimental. */
14306
9068261f 14307static bool
685b1105
JK
14308producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14309{
14310 if (!cu->checked_producer)
14311 check_producer (cu);
14312
14313 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14314}
14315
c258c396
JD
14316
14317/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14318 with incorrect is_stmt attributes. */
14319
14320static bool
14321producer_is_codewarrior (struct dwarf2_cu *cu)
14322{
14323 if (!cu->checked_producer)
14324 check_producer (cu);
14325
14326 return cu->producer_is_codewarrior;
14327}
14328
405feb71 14329/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14330 DW_AT_accessibility. */
14331
14332static enum dwarf_access_attribute
14333dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14334{
14335 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14336 {
14337 /* The default DWARF 2 accessibility for members is public, the default
14338 accessibility for inheritance is private. */
14339
14340 if (die->tag != DW_TAG_inheritance)
14341 return DW_ACCESS_public;
14342 else
14343 return DW_ACCESS_private;
14344 }
14345 else
14346 {
14347 /* DWARF 3+ defines the default accessibility a different way. The same
14348 rules apply now for DW_TAG_inheritance as for the members and it only
14349 depends on the container kind. */
14350
14351 if (die->parent->tag == DW_TAG_class_type)
14352 return DW_ACCESS_private;
14353 else
14354 return DW_ACCESS_public;
14355 }
14356}
14357
74ac6d43
TT
14358/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14359 offset. If the attribute was not found return 0, otherwise return
14360 1. If it was found but could not properly be handled, set *OFFSET
14361 to 0. */
14362
14363static int
14364handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14365 LONGEST *offset)
14366{
14367 struct attribute *attr;
14368
14369 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14370 if (attr != NULL)
14371 {
14372 *offset = 0;
14373
14374 /* Note that we do not check for a section offset first here.
14375 This is because DW_AT_data_member_location is new in DWARF 4,
14376 so if we see it, we can assume that a constant form is really
14377 a constant and not a section offset. */
cd6c91b4 14378 if (attr->form_is_constant ())
0826b30a 14379 *offset = attr->constant_value (0);
cd6c91b4 14380 else if (attr->form_is_section_offset ())
74ac6d43 14381 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14382 else if (attr->form_is_block ())
74ac6d43
TT
14383 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14384 else
14385 dwarf2_complex_location_expr_complaint ();
14386
14387 return 1;
14388 }
14389
14390 return 0;
14391}
14392
7d79de9a
TT
14393/* Look for DW_AT_data_member_location and store the results in FIELD. */
14394
14395static void
14396handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14397 struct field *field)
14398{
14399 struct attribute *attr;
14400
14401 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14402 if (attr != NULL)
14403 {
14404 if (attr->form_is_constant ())
14405 {
14406 LONGEST offset = attr->constant_value (0);
14407 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14408 }
14409 else if (attr->form_is_section_offset ())
14410 dwarf2_complex_location_expr_complaint ();
14411 else if (attr->form_is_block ())
14412 {
14413 bool handled;
14414 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14415 if (handled)
14416 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14417 else
14418 {
5e22e966
SM
14419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14420 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14421 struct dwarf2_locexpr_baton *dlbaton
14422 = XOBNEW (&objfile->objfile_obstack,
14423 struct dwarf2_locexpr_baton);
14424 dlbaton->data = DW_BLOCK (attr)->data;
14425 dlbaton->size = DW_BLOCK (attr)->size;
14426 /* When using this baton, we want to compute the address
14427 of the field, not the value. This is why
14428 is_reference is set to false here. */
14429 dlbaton->is_reference = false;
5e22e966 14430 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14431 dlbaton->per_cu = cu->per_cu;
14432
14433 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14434 }
14435 }
14436 else
14437 dwarf2_complex_location_expr_complaint ();
14438 }
14439}
14440
c906108c
SS
14441/* Add an aggregate field to the field list. */
14442
14443static void
107d2387 14444dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14445 struct dwarf2_cu *cu)
6e70227d 14446{
5e22e966 14447 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14448 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14449 struct nextfield *new_field;
14450 struct attribute *attr;
14451 struct field *fp;
15d034d0 14452 const char *fieldname = "";
c906108c 14453
7d0ccb61
DJ
14454 if (die->tag == DW_TAG_inheritance)
14455 {
be2daae6
TT
14456 fip->baseclasses.emplace_back ();
14457 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14458 }
14459 else
14460 {
be2daae6
TT
14461 fip->fields.emplace_back ();
14462 new_field = &fip->fields.back ();
7d0ccb61 14463 }
be2daae6 14464
9c6a1327
TT
14465 new_field->offset = die->sect_off;
14466
e142c38c 14467 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14468 if (attr != nullptr)
c906108c 14469 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14470 else
14471 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14472 if (new_field->accessibility != DW_ACCESS_public)
14473 fip->non_public_fields = 1;
60d5a603 14474
e142c38c 14475 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14476 if (attr != nullptr)
c906108c 14477 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14478 else
14479 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14480
14481 fp = &new_field->field;
a9a9bd0f 14482
e142c38c 14483 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14484 {
a9a9bd0f 14485 /* Data member other than a C++ static data member. */
6e70227d 14486
c906108c 14487 /* Get type of field. */
e7c27a73 14488 fp->type = die_type (die, cu);
c906108c 14489
d6a843b5 14490 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14491
c906108c 14492 /* Get bit size of field (zero if none). */
e142c38c 14493 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14494 if (attr != nullptr)
c906108c
SS
14495 {
14496 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14497 }
14498 else
14499 {
14500 FIELD_BITSIZE (*fp) = 0;
14501 }
14502
14503 /* Get bit offset of field. */
7d79de9a 14504 handle_data_member_location (die, cu, fp);
e142c38c 14505 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14506 if (attr != nullptr)
c906108c 14507 {
d5a22e77 14508 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14509 {
14510 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14511 additional bit offset from the MSB of the containing
14512 anonymous object to the MSB of the field. We don't
14513 have to do anything special since we don't need to
14514 know the size of the anonymous object. */
f41f5e61 14515 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14516 }
14517 else
14518 {
14519 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14520 MSB of the anonymous object, subtract off the number of
14521 bits from the MSB of the field to the MSB of the
14522 object, and then subtract off the number of bits of
14523 the field itself. The result is the bit offset of
14524 the LSB of the field. */
c906108c
SS
14525 int anonymous_size;
14526 int bit_offset = DW_UNSND (attr);
14527
e142c38c 14528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14529 if (attr != nullptr)
c906108c
SS
14530 {
14531 /* The size of the anonymous object containing
14532 the bit field is explicit, so use the
14533 indicated size (in bytes). */
14534 anonymous_size = DW_UNSND (attr);
14535 }
14536 else
14537 {
14538 /* The size of the anonymous object containing
14539 the bit field must be inferred from the type
14540 attribute of the data member containing the
14541 bit field. */
14542 anonymous_size = TYPE_LENGTH (fp->type);
14543 }
f41f5e61
PA
14544 SET_FIELD_BITPOS (*fp,
14545 (FIELD_BITPOS (*fp)
14546 + anonymous_size * bits_per_byte
14547 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14548 }
14549 }
da5b30da
AA
14550 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14551 if (attr != NULL)
14552 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14553 + attr->constant_value (0)));
c906108c
SS
14554
14555 /* Get name of field. */
39cbfefa
DJ
14556 fieldname = dwarf2_name (die, cu);
14557 if (fieldname == NULL)
14558 fieldname = "";
d8151005
DJ
14559
14560 /* The name is already allocated along with this objfile, so we don't
14561 need to duplicate it for the type. */
14562 fp->name = fieldname;
c906108c
SS
14563
14564 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14565 pointer or virtual base class pointer) to private. */
e142c38c 14566 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14567 {
d48cc9dd 14568 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14569 new_field->accessibility = DW_ACCESS_private;
14570 fip->non_public_fields = 1;
14571 }
14572 }
a9a9bd0f 14573 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14574 {
a9a9bd0f
DC
14575 /* C++ static member. */
14576
14577 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14578 is a declaration, but all versions of G++ as of this writing
14579 (so through at least 3.2.1) incorrectly generate
14580 DW_TAG_variable tags. */
6e70227d 14581
ff355380 14582 const char *physname;
c906108c 14583
a9a9bd0f 14584 /* Get name of field. */
39cbfefa
DJ
14585 fieldname = dwarf2_name (die, cu);
14586 if (fieldname == NULL)
c906108c
SS
14587 return;
14588
254e6b9e 14589 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14590 if (attr
14591 /* Only create a symbol if this is an external value.
14592 new_symbol checks this and puts the value in the global symbol
14593 table, which we want. If it is not external, new_symbol
14594 will try to put the value in cu->list_in_scope which is wrong. */
14595 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14596 {
14597 /* A static const member, not much different than an enum as far as
14598 we're concerned, except that we can support more types. */
14599 new_symbol (die, NULL, cu);
14600 }
14601
2df3850c 14602 /* Get physical name. */
ff355380 14603 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14604
d8151005
DJ
14605 /* The name is already allocated along with this objfile, so we don't
14606 need to duplicate it for the type. */
14607 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14608 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14609 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14610 }
14611 else if (die->tag == DW_TAG_inheritance)
14612 {
74ac6d43 14613 /* C++ base class field. */
7d79de9a 14614 handle_data_member_location (die, cu, fp);
c906108c 14615 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14616 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14617 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14618 }
2ddeaf8a
TT
14619 else
14620 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14621}
14622
883fd55a
KS
14623/* Can the type given by DIE define another type? */
14624
14625static bool
14626type_can_define_types (const struct die_info *die)
14627{
14628 switch (die->tag)
14629 {
14630 case DW_TAG_typedef:
14631 case DW_TAG_class_type:
14632 case DW_TAG_structure_type:
14633 case DW_TAG_union_type:
14634 case DW_TAG_enumeration_type:
14635 return true;
14636
14637 default:
14638 return false;
14639 }
14640}
14641
14642/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14643
14644static void
883fd55a
KS
14645dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14646 struct dwarf2_cu *cu)
6e70227d 14647{
be2daae6
TT
14648 struct decl_field fp;
14649 memset (&fp, 0, sizeof (fp));
98751a41 14650
883fd55a 14651 gdb_assert (type_can_define_types (die));
98751a41 14652
883fd55a 14653 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14654 fp.name = dwarf2_name (die, cu);
14655 fp.type = read_type_die (die, cu);
98751a41 14656
c191a687
KS
14657 /* Save accessibility. */
14658 enum dwarf_access_attribute accessibility;
14659 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14660 if (attr != NULL)
14661 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14662 else
14663 accessibility = dwarf2_default_access_attribute (die, cu);
14664 switch (accessibility)
14665 {
14666 case DW_ACCESS_public:
14667 /* The assumed value if neither private nor protected. */
14668 break;
14669 case DW_ACCESS_private:
be2daae6 14670 fp.is_private = 1;
c191a687
KS
14671 break;
14672 case DW_ACCESS_protected:
be2daae6 14673 fp.is_protected = 1;
c191a687
KS
14674 break;
14675 default:
b98664d3 14676 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14677 }
14678
883fd55a 14679 if (die->tag == DW_TAG_typedef)
be2daae6 14680 fip->typedef_field_list.push_back (fp);
883fd55a 14681 else
be2daae6 14682 fip->nested_types_list.push_back (fp);
98751a41
JK
14683}
14684
9c6a1327
TT
14685/* A convenience typedef that's used when finding the discriminant
14686 field for a variant part. */
1b95cdb7
SM
14687typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14688 offset_map_type;
9c6a1327
TT
14689
14690/* Compute the discriminant range for a given variant. OBSTACK is
14691 where the results will be stored. VARIANT is the variant to
14692 process. IS_UNSIGNED indicates whether the discriminant is signed
14693 or unsigned. */
14694
14695static const gdb::array_view<discriminant_range>
14696convert_variant_range (struct obstack *obstack, const variant_field &variant,
14697 bool is_unsigned)
14698{
14699 std::vector<discriminant_range> ranges;
14700
14701 if (variant.default_branch)
14702 return {};
14703
14704 if (variant.discr_list_data == nullptr)
14705 {
14706 discriminant_range r
14707 = {variant.discriminant_value, variant.discriminant_value};
14708 ranges.push_back (r);
14709 }
14710 else
14711 {
14712 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14713 variant.discr_list_data->size);
14714 while (!data.empty ())
14715 {
14716 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14717 {
14718 complaint (_("invalid discriminant marker: %d"), data[0]);
14719 break;
14720 }
14721 bool is_range = data[0] == DW_DSC_range;
14722 data = data.slice (1);
14723
14724 ULONGEST low, high;
14725 unsigned int bytes_read;
14726
14727 if (data.empty ())
14728 {
14729 complaint (_("DW_AT_discr_list missing low value"));
14730 break;
14731 }
14732 if (is_unsigned)
14733 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14734 else
14735 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14736 &bytes_read);
14737 data = data.slice (bytes_read);
14738
14739 if (is_range)
14740 {
14741 if (data.empty ())
14742 {
14743 complaint (_("DW_AT_discr_list missing high value"));
14744 break;
14745 }
14746 if (is_unsigned)
14747 high = read_unsigned_leb128 (nullptr, data.data (),
14748 &bytes_read);
14749 else
14750 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14751 &bytes_read);
14752 data = data.slice (bytes_read);
14753 }
14754 else
14755 high = low;
14756
14757 ranges.push_back ({ low, high });
14758 }
14759 }
14760
14761 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14762 ranges.size ());
14763 std::copy (ranges.begin (), ranges.end (), result);
14764 return gdb::array_view<discriminant_range> (result, ranges.size ());
14765}
14766
14767static const gdb::array_view<variant_part> create_variant_parts
14768 (struct obstack *obstack,
14769 const offset_map_type &offset_map,
14770 struct field_info *fi,
14771 const std::vector<variant_part_builder> &variant_parts);
14772
14773/* Fill in a "struct variant" for a given variant field. RESULT is
14774 the variant to fill in. OBSTACK is where any needed allocations
14775 will be done. OFFSET_MAP holds the mapping from section offsets to
14776 fields for the type. FI describes the fields of the type we're
14777 processing. FIELD is the variant field we're converting. */
14778
14779static void
14780create_one_variant (variant &result, struct obstack *obstack,
14781 const offset_map_type &offset_map,
14782 struct field_info *fi, const variant_field &field)
14783{
14784 result.discriminants = convert_variant_range (obstack, field, false);
14785 result.first_field = field.first_field + fi->baseclasses.size ();
14786 result.last_field = field.last_field + fi->baseclasses.size ();
14787 result.parts = create_variant_parts (obstack, offset_map, fi,
14788 field.variant_parts);
14789}
14790
14791/* Fill in a "struct variant_part" for a given variant part. RESULT
14792 is the variant part to fill in. OBSTACK is where any needed
14793 allocations will be done. OFFSET_MAP holds the mapping from
14794 section offsets to fields for the type. FI describes the fields of
14795 the type we're processing. BUILDER is the variant part to be
14796 converted. */
14797
14798static void
14799create_one_variant_part (variant_part &result,
14800 struct obstack *obstack,
14801 const offset_map_type &offset_map,
14802 struct field_info *fi,
14803 const variant_part_builder &builder)
14804{
14805 auto iter = offset_map.find (builder.discriminant_offset);
14806 if (iter == offset_map.end ())
14807 {
14808 result.discriminant_index = -1;
14809 /* Doesn't matter. */
14810 result.is_unsigned = false;
14811 }
14812 else
14813 {
14814 result.discriminant_index = iter->second;
14815 result.is_unsigned
14816 = TYPE_UNSIGNED (FIELD_TYPE
14817 (fi->fields[result.discriminant_index].field));
14818 }
14819
14820 size_t n = builder.variants.size ();
14821 variant *output = new (obstack) variant[n];
14822 for (size_t i = 0; i < n; ++i)
14823 create_one_variant (output[i], obstack, offset_map, fi,
14824 builder.variants[i]);
14825
14826 result.variants = gdb::array_view<variant> (output, n);
14827}
14828
14829/* Create a vector of variant parts that can be attached to a type.
14830 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14831 holds the mapping from section offsets to fields for the type. FI
14832 describes the fields of the type we're processing. VARIANT_PARTS
14833 is the vector to convert. */
14834
14835static const gdb::array_view<variant_part>
14836create_variant_parts (struct obstack *obstack,
14837 const offset_map_type &offset_map,
14838 struct field_info *fi,
14839 const std::vector<variant_part_builder> &variant_parts)
14840{
14841 if (variant_parts.empty ())
14842 return {};
14843
14844 size_t n = variant_parts.size ();
14845 variant_part *result = new (obstack) variant_part[n];
14846 for (size_t i = 0; i < n; ++i)
14847 create_one_variant_part (result[i], obstack, offset_map, fi,
14848 variant_parts[i]);
14849
14850 return gdb::array_view<variant_part> (result, n);
14851}
14852
14853/* Compute the variant part vector for FIP, attaching it to TYPE when
14854 done. */
14855
14856static void
14857add_variant_property (struct field_info *fip, struct type *type,
14858 struct dwarf2_cu *cu)
14859{
14860 /* Map section offsets of fields to their field index. Note the
14861 field index here does not take the number of baseclasses into
14862 account. */
14863 offset_map_type offset_map;
14864 for (int i = 0; i < fip->fields.size (); ++i)
14865 offset_map[fip->fields[i].offset] = i;
14866
5e22e966 14867 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14868 gdb::array_view<variant_part> parts
14869 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14870 fip->variant_parts);
14871
14872 struct dynamic_prop prop;
14873 prop.kind = PROP_VARIANT_PARTS;
14874 prop.data.variant_parts
14875 = ((gdb::array_view<variant_part> *)
14876 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14877
5c54719c 14878 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14879}
14880
c906108c
SS
14881/* Create the vector of fields, and attach it to the type. */
14882
14883static void
fba45db2 14884dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14885 struct dwarf2_cu *cu)
c906108c 14886{
317f7127 14887 int nfields = fip->nfields ();
c906108c
SS
14888
14889 /* Record the field count, allocate space for the array of fields,
14890 and create blank accessibility bitfields if necessary. */
5e33d5f4 14891 type->set_num_fields (nfields);
3cabb6b0
SM
14892 type->set_fields
14893 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14894
b4ba55a1 14895 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14896 {
14897 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14898
14899 TYPE_FIELD_PRIVATE_BITS (type) =
14900 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14901 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14902
14903 TYPE_FIELD_PROTECTED_BITS (type) =
14904 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14905 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14906
774b6a14
TT
14907 TYPE_FIELD_IGNORE_BITS (type) =
14908 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14909 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14910 }
14911
14912 /* If the type has baseclasses, allocate and clear a bit vector for
14913 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14914 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14915 {
be2daae6 14916 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14917 unsigned char *pointer;
c906108c
SS
14918
14919 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14920 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14921 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14922 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14923 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14924 }
14925
9c6a1327
TT
14926 if (!fip->variant_parts.empty ())
14927 add_variant_property (fip, type, cu);
2ddeaf8a 14928
be2daae6
TT
14929 /* Copy the saved-up fields into the field vector. */
14930 for (int i = 0; i < nfields; ++i)
c906108c 14931 {
be2daae6
TT
14932 struct nextfield &field
14933 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14934 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14935
ceacbf6e 14936 type->field (i) = field.field;
be2daae6 14937 switch (field.accessibility)
c906108c 14938 {
c5aa993b 14939 case DW_ACCESS_private:
b4ba55a1 14940 if (cu->language != language_ada)
be2daae6 14941 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14942 break;
c906108c 14943
c5aa993b 14944 case DW_ACCESS_protected:
b4ba55a1 14945 if (cu->language != language_ada)
be2daae6 14946 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14947 break;
c906108c 14948
c5aa993b
JM
14949 case DW_ACCESS_public:
14950 break;
c906108c 14951
c5aa993b
JM
14952 default:
14953 /* Unknown accessibility. Complain and treat it as public. */
14954 {
b98664d3 14955 complaint (_("unsupported accessibility %d"),
be2daae6 14956 field.accessibility);
c5aa993b
JM
14957 }
14958 break;
c906108c 14959 }
be2daae6 14960 if (i < fip->baseclasses.size ())
c906108c 14961 {
be2daae6 14962 switch (field.virtuality)
c906108c 14963 {
c5aa993b
JM
14964 case DW_VIRTUALITY_virtual:
14965 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14966 if (cu->language == language_ada)
a73c6dcd 14967 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14968 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14969 break;
c906108c
SS
14970 }
14971 }
c906108c
SS
14972 }
14973}
14974
7d27a96d
TT
14975/* Return true if this member function is a constructor, false
14976 otherwise. */
14977
14978static int
14979dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14980{
14981 const char *fieldname;
fe978cb0 14982 const char *type_name;
7d27a96d
TT
14983 int len;
14984
14985 if (die->parent == NULL)
14986 return 0;
14987
14988 if (die->parent->tag != DW_TAG_structure_type
14989 && die->parent->tag != DW_TAG_union_type
14990 && die->parent->tag != DW_TAG_class_type)
14991 return 0;
14992
14993 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14994 type_name = dwarf2_name (die->parent, cu);
14995 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14996 return 0;
14997
14998 len = strlen (fieldname);
fe978cb0
PA
14999 return (strncmp (fieldname, type_name, len) == 0
15000 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15001}
15002
e35000a7
TBA
15003/* Check if the given VALUE is a recognized enum
15004 dwarf_defaulted_attribute constant according to DWARF5 spec,
15005 Table 7.24. */
15006
15007static bool
15008is_valid_DW_AT_defaulted (ULONGEST value)
15009{
15010 switch (value)
15011 {
15012 case DW_DEFAULTED_no:
15013 case DW_DEFAULTED_in_class:
15014 case DW_DEFAULTED_out_of_class:
15015 return true;
15016 }
15017
3142e908 15018 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15019 return false;
15020}
15021
c906108c
SS
15022/* Add a member function to the proper fieldlist. */
15023
15024static void
107d2387 15025dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15026 struct type *type, struct dwarf2_cu *cu)
c906108c 15027{
5e22e966 15028 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15029 struct attribute *attr;
c906108c 15030 int i;
be2daae6 15031 struct fnfieldlist *flp = nullptr;
c906108c 15032 struct fn_field *fnp;
15d034d0 15033 const char *fieldname;
f792889a 15034 struct type *this_type;
60d5a603 15035 enum dwarf_access_attribute accessibility;
c906108c 15036
b4ba55a1 15037 if (cu->language == language_ada)
a73c6dcd 15038 error (_("unexpected member function in Ada type"));
b4ba55a1 15039
2df3850c 15040 /* Get name of member function. */
39cbfefa
DJ
15041 fieldname = dwarf2_name (die, cu);
15042 if (fieldname == NULL)
2df3850c 15043 return;
c906108c 15044
c906108c 15045 /* Look up member function name in fieldlist. */
be2daae6 15046 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15047 {
27bfe10e 15048 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15049 {
15050 flp = &fip->fnfieldlists[i];
15051 break;
15052 }
c906108c
SS
15053 }
15054
be2daae6
TT
15055 /* Create a new fnfieldlist if necessary. */
15056 if (flp == nullptr)
c906108c 15057 {
be2daae6
TT
15058 fip->fnfieldlists.emplace_back ();
15059 flp = &fip->fnfieldlists.back ();
c906108c 15060 flp->name = fieldname;
be2daae6 15061 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15062 }
15063
be2daae6
TT
15064 /* Create a new member function field and add it to the vector of
15065 fnfieldlists. */
15066 flp->fnfields.emplace_back ();
15067 fnp = &flp->fnfields.back ();
3da10d80
KS
15068
15069 /* Delay processing of the physname until later. */
9c37b5ae 15070 if (cu->language == language_cplus)
be2daae6
TT
15071 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15072 die, cu);
3da10d80
KS
15073 else
15074 {
1d06ead6 15075 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15076 fnp->physname = physname ? physname : "";
15077 }
15078
c906108c 15079 fnp->type = alloc_type (objfile);
f792889a 15080 this_type = read_type_die (die, cu);
78134374 15081 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15082 {
1f704f76 15083 int nparams = this_type->num_fields ();
c906108c 15084
f792889a 15085 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15086 of the method itself (TYPE_CODE_METHOD). */
15087 smash_to_method_type (fnp->type, type,
f792889a 15088 TYPE_TARGET_TYPE (this_type),
80fc5e77 15089 this_type->fields (),
1f704f76 15090 this_type->num_fields (),
f792889a 15091 TYPE_VARARGS (this_type));
c906108c
SS
15092
15093 /* Handle static member functions.
c5aa993b 15094 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15095 member functions. G++ helps GDB by marking the first
15096 parameter for non-static member functions (which is the this
15097 pointer) as artificial. We obtain this information from
15098 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15099 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15100 fnp->voffset = VOFFSET_STATIC;
15101 }
15102 else
b98664d3 15103 complaint (_("member function type missing for '%s'"),
3da10d80 15104 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15105
15106 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15107 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15108 fnp->fcontext = die_containing_type (die, cu);
c906108c 15109
3e43a32a
MS
15110 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15111 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15112
15113 /* Get accessibility. */
e142c38c 15114 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15115 if (attr != nullptr)
aead7601 15116 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15117 else
15118 accessibility = dwarf2_default_access_attribute (die, cu);
15119 switch (accessibility)
c906108c 15120 {
60d5a603
JK
15121 case DW_ACCESS_private:
15122 fnp->is_private = 1;
15123 break;
15124 case DW_ACCESS_protected:
15125 fnp->is_protected = 1;
15126 break;
c906108c
SS
15127 }
15128
b02dede2 15129 /* Check for artificial methods. */
e142c38c 15130 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15131 if (attr && DW_UNSND (attr) != 0)
15132 fnp->is_artificial = 1;
15133
e35000a7
TBA
15134 /* Check for defaulted methods. */
15135 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15136 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15137 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15138
15139 /* Check for deleted methods. */
15140 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15141 if (attr != nullptr && DW_UNSND (attr) != 0)
15142 fnp->is_deleted = 1;
15143
7d27a96d
TT
15144 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15145
0d564a31 15146 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15147 function. For older versions of GCC, this is an offset in the
15148 appropriate virtual table, as specified by DW_AT_containing_type.
15149 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15150 to the object address. */
15151
e142c38c 15152 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15153 if (attr != nullptr)
8e19ed76 15154 {
4fc6c0d5 15155 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15156 {
aec5aa8b
TT
15157 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15158 {
15159 /* Old-style GCC. */
15160 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15161 }
15162 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15163 || (DW_BLOCK (attr)->size > 1
15164 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15165 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15166 {
aec5aa8b
TT
15167 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15168 if ((fnp->voffset % cu->header.addr_size) != 0)
15169 dwarf2_complex_location_expr_complaint ();
15170 else
15171 fnp->voffset /= cu->header.addr_size;
15172 fnp->voffset += 2;
15173 }
15174 else
15175 dwarf2_complex_location_expr_complaint ();
15176
15177 if (!fnp->fcontext)
7e993ebf
KS
15178 {
15179 /* If there is no `this' field and no DW_AT_containing_type,
15180 we cannot actually find a base class context for the
15181 vtable! */
1f704f76 15182 if (this_type->num_fields () == 0
7e993ebf
KS
15183 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15184 {
b98664d3 15185 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15186 "function \"%s\" (offset %s)"),
15187 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15188 }
15189 else
15190 {
15191 fnp->fcontext
15192 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15193 }
15194 }
aec5aa8b 15195 }
cd6c91b4 15196 else if (attr->form_is_section_offset ())
8e19ed76 15197 {
4d3c2250 15198 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15199 }
15200 else
15201 {
4d3c2250
KB
15202 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15203 fieldname);
8e19ed76 15204 }
0d564a31 15205 }
d48cc9dd
DJ
15206 else
15207 {
15208 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15209 if (attr && DW_UNSND (attr))
15210 {
15211 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15212 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15213 "but the vtable offset is not specified"),
9d8780f0 15214 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15215 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15216 TYPE_CPLUS_DYNAMIC (type) = 1;
15217 }
15218 }
c906108c
SS
15219}
15220
15221/* Create the vector of member function fields, and attach it to the type. */
15222
15223static void
fba45db2 15224dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15225 struct dwarf2_cu *cu)
c906108c 15226{
b4ba55a1 15227 if (cu->language == language_ada)
a73c6dcd 15228 error (_("unexpected member functions in Ada type"));
b4ba55a1 15229
c906108c
SS
15230 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15231 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15232 TYPE_ALLOC (type,
15233 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15234
be2daae6 15235 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15236 {
be2daae6 15237 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15238 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15239
be2daae6
TT
15240 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15241 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15242 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15243 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15244
15245 for (int k = 0; k < nf.fnfields.size (); ++k)
15246 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15247 }
15248
be2daae6 15249 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15250}
15251
1168df01
JB
15252/* Returns non-zero if NAME is the name of a vtable member in CU's
15253 language, zero otherwise. */
15254static int
15255is_vtable_name (const char *name, struct dwarf2_cu *cu)
15256{
15257 static const char vptr[] = "_vptr";
15258
9c37b5ae
TT
15259 /* Look for the C++ form of the vtable. */
15260 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15261 return 1;
15262
15263 return 0;
15264}
15265
c0dd20ea 15266/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15267 functions, with the ABI-specified layout. If TYPE describes
15268 such a structure, smash it into a member function type.
61049d3b
DJ
15269
15270 GCC shouldn't do this; it should just output pointer to member DIEs.
15271 This is GCC PR debug/28767. */
c0dd20ea 15272
0b92b5bb
TT
15273static void
15274quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15275{
09e2d7c7 15276 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15277
15278 /* Check for a structure with no name and two children. */
1f704f76 15279 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15280 return;
c0dd20ea
DJ
15281
15282 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15283 if (TYPE_FIELD_NAME (type, 0) == NULL
15284 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15285 || TYPE_FIELD_NAME (type, 1) == NULL
15286 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15287 return;
c0dd20ea
DJ
15288
15289 /* Find the type of the method. */
0b92b5bb 15290 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15291 if (pfn_type == NULL
78134374
SM
15292 || pfn_type->code () != TYPE_CODE_PTR
15293 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15294 return;
c0dd20ea
DJ
15295
15296 /* Look for the "this" argument. */
15297 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15298 if (pfn_type->num_fields () == 0
0b92b5bb 15299 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15300 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15301 return;
c0dd20ea 15302
09e2d7c7 15303 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15304 new_type = alloc_type (objfile);
09e2d7c7 15305 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15306 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15307 TYPE_VARARGS (pfn_type));
0b92b5bb 15308 smash_to_methodptr_type (type, new_type);
c0dd20ea 15309}
1168df01 15310
2b4424c3
TT
15311/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15312 appropriate error checking and issuing complaints if there is a
15313 problem. */
15314
15315static ULONGEST
15316get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15317{
15318 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15319
15320 if (attr == nullptr)
15321 return 0;
15322
cd6c91b4 15323 if (!attr->form_is_constant ())
2b4424c3 15324 {
b98664d3 15325 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15326 " - DIE at %s [in module %s]"),
15327 sect_offset_str (die->sect_off),
5e22e966 15328 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15329 return 0;
15330 }
15331
15332 ULONGEST align;
15333 if (attr->form == DW_FORM_sdata)
15334 {
15335 LONGEST val = DW_SND (attr);
15336 if (val < 0)
15337 {
b98664d3 15338 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15339 " - DIE at %s [in module %s]"),
15340 sect_offset_str (die->sect_off),
5e22e966 15341 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15342 return 0;
15343 }
15344 align = val;
15345 }
15346 else
15347 align = DW_UNSND (attr);
15348
15349 if (align == 0)
15350 {
b98664d3 15351 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15352 " - DIE at %s [in module %s]"),
15353 sect_offset_str (die->sect_off),
5e22e966 15354 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15355 return 0;
15356 }
15357 if ((align & (align - 1)) != 0)
15358 {
b98664d3 15359 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15360 " - DIE at %s [in module %s]"),
15361 sect_offset_str (die->sect_off),
5e22e966 15362 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15363 return 0;
15364 }
15365
15366 return align;
15367}
15368
15369/* If the DIE has a DW_AT_alignment attribute, use its value to set
15370 the alignment for TYPE. */
15371
15372static void
15373maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15374 struct type *type)
15375{
15376 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15377 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15378 " - DIE at %s [in module %s]"),
15379 sect_offset_str (die->sect_off),
5e22e966 15380 objfile_name (cu->per_objfile->objfile));
2b4424c3 15381}
685b1105 15382
e35000a7
TBA
15383/* Check if the given VALUE is a valid enum dwarf_calling_convention
15384 constant for a type, according to DWARF5 spec, Table 5.5. */
15385
15386static bool
15387is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15388{
15389 switch (value)
15390 {
15391 case DW_CC_normal:
15392 case DW_CC_pass_by_reference:
15393 case DW_CC_pass_by_value:
15394 return true;
15395
15396 default:
15397 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15398 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15399 return false;
15400 }
15401}
15402
d0922fcf
TBA
15403/* Check if the given VALUE is a valid enum dwarf_calling_convention
15404 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15405 also according to GNU-specific values (see include/dwarf2.h). */
15406
15407static bool
15408is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15409{
15410 switch (value)
15411 {
15412 case DW_CC_normal:
15413 case DW_CC_program:
15414 case DW_CC_nocall:
15415 return true;
15416
15417 case DW_CC_GNU_renesas_sh:
15418 case DW_CC_GNU_borland_fastcall_i386:
15419 case DW_CC_GDB_IBM_OpenCL:
15420 return true;
15421
15422 default:
15423 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15424 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15425 return false;
15426 }
15427}
15428
c906108c 15429/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15430 (definition) to create a type for the structure or union. Fill in
15431 the type's name and general properties; the members will not be
83655187
DE
15432 processed until process_structure_scope. A symbol table entry for
15433 the type will also not be done until process_structure_scope (assuming
15434 the type has a name).
c906108c 15435
c767944b
DJ
15436 NOTE: we need to call these functions regardless of whether or not the
15437 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15438 structure or union. This gets the type entered into our set of
83655187 15439 user defined types. */
c906108c 15440
f792889a 15441static struct type *
134d01f1 15442read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15443{
5e22e966 15444 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15445 struct type *type;
15446 struct attribute *attr;
15d034d0 15447 const char *name;
c906108c 15448
348e048f
DE
15449 /* If the definition of this type lives in .debug_types, read that type.
15450 Don't follow DW_AT_specification though, that will take us back up
15451 the chain and we want to go down. */
052c8bb8 15452 attr = die->attr (DW_AT_signature);
435d3d88 15453 if (attr != nullptr)
348e048f 15454 {
ac9ec31b 15455 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15456
ac9ec31b 15457 /* The type's CU may not be the same as CU.
02142a6c 15458 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15459 return set_die_type (die, type, cu);
15460 }
15461
c0dd20ea 15462 type = alloc_type (objfile);
c906108c 15463 INIT_CPLUS_SPECIFIC (type);
93311388 15464
39cbfefa
DJ
15465 name = dwarf2_name (die, cu);
15466 if (name != NULL)
c906108c 15467 {
987504bb 15468 if (cu->language == language_cplus
c44af4eb
TT
15469 || cu->language == language_d
15470 || cu->language == language_rust)
63d06c5c 15471 {
15d034d0 15472 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15473
15474 /* dwarf2_full_name might have already finished building the DIE's
15475 type. If so, there is no need to continue. */
15476 if (get_die_type (die, cu) != NULL)
15477 return get_die_type (die, cu);
15478
d0e39ea2 15479 type->set_name (full_name);
63d06c5c
DC
15480 }
15481 else
15482 {
d8151005
DJ
15483 /* The name is already allocated along with this objfile, so
15484 we don't need to duplicate it for the type. */
d0e39ea2 15485 type->set_name (name);
63d06c5c 15486 }
c906108c
SS
15487 }
15488
15489 if (die->tag == DW_TAG_structure_type)
15490 {
67607e24 15491 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15492 }
15493 else if (die->tag == DW_TAG_union_type)
15494 {
67607e24 15495 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15496 }
15497 else
15498 {
67607e24 15499 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15500 }
15501
0cc2414c
TT
15502 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15503 TYPE_DECLARED_CLASS (type) = 1;
15504
e35000a7
TBA
15505 /* Store the calling convention in the type if it's available in
15506 the die. Otherwise the calling convention remains set to
15507 the default value DW_CC_normal. */
15508 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15509 if (attr != nullptr
15510 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15511 {
15512 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15513 TYPE_CPLUS_CALLING_CONVENTION (type)
15514 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15515 }
15516
e142c38c 15517 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15518 if (attr != nullptr)
c906108c 15519 {
cd6c91b4 15520 if (attr->form_is_constant ())
155bfbd3
JB
15521 TYPE_LENGTH (type) = DW_UNSND (attr);
15522 else
15523 {
f8e89861 15524 struct dynamic_prop prop;
293e7e51 15525 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15526 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15527 TYPE_LENGTH (type) = 0;
15528 }
c906108c
SS
15529 }
15530 else
15531 {
15532 TYPE_LENGTH (type) = 0;
15533 }
15534
2b4424c3
TT
15535 maybe_set_alignment (cu, die, type);
15536
5230b05a 15537 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15538 {
5230b05a
WT
15539 /* ICC<14 does not output the required DW_AT_declaration on
15540 incomplete types, but gives them a size of zero. */
422b1cb0 15541 TYPE_STUB (type) = 1;
685b1105
JK
15542 }
15543 else
15544 TYPE_STUB_SUPPORTED (type) = 1;
15545
dc718098 15546 if (die_is_declaration (die, cu))
876cecd0 15547 TYPE_STUB (type) = 1;
a6c727b2
DJ
15548 else if (attr == NULL && die->child == NULL
15549 && producer_is_realview (cu->producer))
15550 /* RealView does not output the required DW_AT_declaration
15551 on incomplete types. */
15552 TYPE_STUB (type) = 1;
dc718098 15553
c906108c
SS
15554 /* We need to add the type field to the die immediately so we don't
15555 infinitely recurse when dealing with pointers to the structure
0963b4bd 15556 type within the structure itself. */
1c379e20 15557 set_die_type (die, type, cu);
c906108c 15558
7e314c57
JK
15559 /* set_die_type should be already done. */
15560 set_descriptive_type (type, die, cu);
15561
c767944b
DJ
15562 return type;
15563}
15564
9c6a1327
TT
15565static void handle_struct_member_die
15566 (struct die_info *child_die,
15567 struct type *type,
15568 struct field_info *fi,
15569 std::vector<struct symbol *> *template_args,
15570 struct dwarf2_cu *cu);
15571
15572/* A helper for handle_struct_member_die that handles
15573 DW_TAG_variant_part. */
15574
15575static void
15576handle_variant_part (struct die_info *die, struct type *type,
15577 struct field_info *fi,
15578 std::vector<struct symbol *> *template_args,
15579 struct dwarf2_cu *cu)
15580{
15581 variant_part_builder *new_part;
15582 if (fi->current_variant_part == nullptr)
15583 {
15584 fi->variant_parts.emplace_back ();
15585 new_part = &fi->variant_parts.back ();
15586 }
15587 else if (!fi->current_variant_part->processing_variant)
15588 {
15589 complaint (_("nested DW_TAG_variant_part seen "
15590 "- DIE at %s [in module %s]"),
15591 sect_offset_str (die->sect_off),
5e22e966 15592 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15593 return;
15594 }
15595 else
15596 {
15597 variant_field &current = fi->current_variant_part->variants.back ();
15598 current.variant_parts.emplace_back ();
15599 new_part = &current.variant_parts.back ();
15600 }
15601
15602 /* When we recurse, we want callees to add to this new variant
15603 part. */
15604 scoped_restore save_current_variant_part
15605 = make_scoped_restore (&fi->current_variant_part, new_part);
15606
15607 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15608 if (discr == NULL)
15609 {
15610 /* It's a univariant form, an extension we support. */
15611 }
15612 else if (discr->form_is_ref ())
15613 {
15614 struct dwarf2_cu *target_cu = cu;
15615 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15616
15617 new_part->discriminant_offset = target_die->sect_off;
15618 }
15619 else
15620 {
15621 complaint (_("DW_AT_discr does not have DIE reference form"
15622 " - DIE at %s [in module %s]"),
15623 sect_offset_str (die->sect_off),
5e22e966 15624 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15625 }
15626
15627 for (die_info *child_die = die->child;
15628 child_die != NULL;
15629 child_die = child_die->sibling)
15630 handle_struct_member_die (child_die, type, fi, template_args, cu);
15631}
15632
15633/* A helper for handle_struct_member_die that handles
15634 DW_TAG_variant. */
15635
15636static void
15637handle_variant (struct die_info *die, struct type *type,
15638 struct field_info *fi,
15639 std::vector<struct symbol *> *template_args,
15640 struct dwarf2_cu *cu)
15641{
15642 if (fi->current_variant_part == nullptr)
15643 {
15644 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15645 "- DIE at %s [in module %s]"),
15646 sect_offset_str (die->sect_off),
5e22e966 15647 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15648 return;
15649 }
15650 if (fi->current_variant_part->processing_variant)
15651 {
15652 complaint (_("nested DW_TAG_variant seen "
15653 "- DIE at %s [in module %s]"),
15654 sect_offset_str (die->sect_off),
5e22e966 15655 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15656 return;
15657 }
15658
15659 scoped_restore save_processing_variant
15660 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15661 true);
15662
15663 fi->current_variant_part->variants.emplace_back ();
15664 variant_field &variant = fi->current_variant_part->variants.back ();
15665 variant.first_field = fi->fields.size ();
15666
15667 /* In a variant we want to get the discriminant and also add a
15668 field for our sole member child. */
15669 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15670 if (discr == nullptr)
15671 {
15672 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15673 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15674 variant.default_branch = true;
15675 else
15676 variant.discr_list_data = DW_BLOCK (discr);
15677 }
15678 else
15679 variant.discriminant_value = DW_UNSND (discr);
15680
15681 for (die_info *variant_child = die->child;
15682 variant_child != NULL;
15683 variant_child = variant_child->sibling)
15684 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15685
15686 variant.last_field = fi->fields.size ();
15687}
15688
2ddeaf8a
TT
15689/* A helper for process_structure_scope that handles a single member
15690 DIE. */
15691
15692static void
15693handle_struct_member_die (struct die_info *child_die, struct type *type,
15694 struct field_info *fi,
15695 std::vector<struct symbol *> *template_args,
15696 struct dwarf2_cu *cu)
15697{
15698 if (child_die->tag == DW_TAG_member
9c6a1327 15699 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15700 {
15701 /* NOTE: carlton/2002-11-05: A C++ static data member
15702 should be a DW_TAG_member that is a declaration, but
15703 all versions of G++ as of this writing (so through at
15704 least 3.2.1) incorrectly generate DW_TAG_variable
15705 tags for them instead. */
15706 dwarf2_add_field (fi, child_die, cu);
15707 }
15708 else if (child_die->tag == DW_TAG_subprogram)
15709 {
15710 /* Rust doesn't have member functions in the C++ sense.
15711 However, it does emit ordinary functions as children
15712 of a struct DIE. */
15713 if (cu->language == language_rust)
15714 read_func_scope (child_die, cu);
15715 else
15716 {
15717 /* C++ member function. */
15718 dwarf2_add_member_fn (fi, child_die, type, cu);
15719 }
15720 }
15721 else if (child_die->tag == DW_TAG_inheritance)
15722 {
15723 /* C++ base class field. */
15724 dwarf2_add_field (fi, child_die, cu);
15725 }
15726 else if (type_can_define_types (child_die))
15727 dwarf2_add_type_defn (fi, child_die, cu);
15728 else if (child_die->tag == DW_TAG_template_type_param
15729 || child_die->tag == DW_TAG_template_value_param)
15730 {
15731 struct symbol *arg = new_symbol (child_die, NULL, cu);
15732
15733 if (arg != NULL)
15734 template_args->push_back (arg);
15735 }
9c6a1327
TT
15736 else if (child_die->tag == DW_TAG_variant_part)
15737 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15738 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15739 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15740}
15741
c767944b
DJ
15742/* Finish creating a structure or union type, including filling in
15743 its members and creating a symbol for it. */
15744
15745static void
15746process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15747{
5e22e966 15748 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15749 struct die_info *child_die;
c767944b
DJ
15750 struct type *type;
15751
15752 type = get_die_type (die, cu);
15753 if (type == NULL)
15754 type = read_structure_type (die, cu);
15755
3e1d3d8c 15756 bool has_template_parameters = false;
e142c38c 15757 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15758 {
15759 struct field_info fi;
2f4732b0 15760 std::vector<struct symbol *> template_args;
c906108c 15761
639d11d3 15762 child_die = die->child;
c906108c
SS
15763
15764 while (child_die && child_die->tag)
15765 {
2ddeaf8a 15766 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15767 child_die = child_die->sibling;
c906108c
SS
15768 }
15769
34eaf542 15770 /* Attach template arguments to type. */
2f4732b0 15771 if (!template_args.empty ())
34eaf542 15772 {
3e1d3d8c 15773 has_template_parameters = true;
34eaf542 15774 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15775 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15776 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15777 = XOBNEWVEC (&objfile->objfile_obstack,
15778 struct symbol *,
15779 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15780 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15781 template_args.data (),
34eaf542
TT
15782 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15783 * sizeof (struct symbol *)));
34eaf542
TT
15784 }
15785
c906108c 15786 /* Attach fields and member functions to the type. */
317f7127 15787 if (fi.nfields () > 0)
e7c27a73 15788 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15789 if (!fi.fnfieldlists.empty ())
c906108c 15790 {
e7c27a73 15791 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15792
c5aa993b 15793 /* Get the type which refers to the base class (possibly this
c906108c 15794 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15795 class from the DW_AT_containing_type attribute. This use of
15796 DW_AT_containing_type is a GNU extension. */
c906108c 15797
e142c38c 15798 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15799 {
e7c27a73 15800 struct type *t = die_containing_type (die, cu);
c906108c 15801
ae6ae975 15802 set_type_vptr_basetype (type, t);
c906108c
SS
15803 if (type == t)
15804 {
c906108c
SS
15805 int i;
15806
15807 /* Our own class provides vtbl ptr. */
1f704f76 15808 for (i = t->num_fields () - 1;
c906108c
SS
15809 i >= TYPE_N_BASECLASSES (t);
15810 --i)
15811 {
0d5cff50 15812 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15813
1168df01 15814 if (is_vtable_name (fieldname, cu))
c906108c 15815 {
ae6ae975 15816 set_type_vptr_fieldno (type, i);
c906108c
SS
15817 break;
15818 }
15819 }
15820
15821 /* Complain if virtual function table field not found. */
15822 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15823 complaint (_("virtual function table pointer "
3e43a32a 15824 "not found when defining class '%s'"),
7d93a1e0 15825 type->name () ? type->name () : "");
c906108c
SS
15826 }
15827 else
15828 {
ae6ae975 15829 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15830 }
15831 }
f6235d4c 15832 else if (cu->producer
61012eef 15833 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15834 {
15835 /* The IBM XLC compiler does not provide direct indication
15836 of the containing type, but the vtable pointer is
15837 always named __vfp. */
15838
15839 int i;
15840
1f704f76 15841 for (i = type->num_fields () - 1;
f6235d4c
EZ
15842 i >= TYPE_N_BASECLASSES (type);
15843 --i)
15844 {
15845 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15846 {
ae6ae975
DE
15847 set_type_vptr_fieldno (type, i);
15848 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15849 break;
15850 }
15851 }
15852 }
c906108c 15853 }
98751a41
JK
15854
15855 /* Copy fi.typedef_field_list linked list elements content into the
15856 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15857 if (!fi.typedef_field_list.empty ())
98751a41 15858 {
be2daae6 15859 int count = fi.typedef_field_list.size ();
98751a41 15860
a0d7a4ff 15861 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15862 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15863 = ((struct decl_field *)
be2daae6
TT
15864 TYPE_ALLOC (type,
15865 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15866 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15867
be2daae6
TT
15868 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15869 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15870 }
c767944b 15871
883fd55a
KS
15872 /* Copy fi.nested_types_list linked list elements content into the
15873 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15874 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15875 {
be2daae6 15876 int count = fi.nested_types_list.size ();
883fd55a
KS
15877
15878 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15879 TYPE_NESTED_TYPES_ARRAY (type)
15880 = ((struct decl_field *)
be2daae6
TT
15881 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15882 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15883
be2daae6
TT
15884 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15885 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15886 }
c906108c 15887 }
63d06c5c 15888
bb5ed363 15889 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15890 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15891 cu->rust_unions.push_back (type);
0b92b5bb 15892
90aeadfc
DC
15893 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15894 snapshots) has been known to create a die giving a declaration
15895 for a class that has, as a child, a die giving a definition for a
15896 nested class. So we have to process our children even if the
15897 current die is a declaration. Normally, of course, a declaration
15898 won't have any children at all. */
134d01f1 15899
ca040673
DE
15900 child_die = die->child;
15901
90aeadfc
DC
15902 while (child_die != NULL && child_die->tag)
15903 {
15904 if (child_die->tag == DW_TAG_member
15905 || child_die->tag == DW_TAG_variable
34eaf542
TT
15906 || child_die->tag == DW_TAG_inheritance
15907 || child_die->tag == DW_TAG_template_value_param
15908 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15909 {
90aeadfc 15910 /* Do nothing. */
134d01f1 15911 }
90aeadfc
DC
15912 else
15913 process_die (child_die, cu);
134d01f1 15914
436c571c 15915 child_die = child_die->sibling;
134d01f1
DJ
15916 }
15917
fa4028e9
JB
15918 /* Do not consider external references. According to the DWARF standard,
15919 these DIEs are identified by the fact that they have no byte_size
15920 attribute, and a declaration attribute. */
15921 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15922 || !die_is_declaration (die, cu)
15923 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15924 {
15925 struct symbol *sym = new_symbol (die, type, cu);
15926
15927 if (has_template_parameters)
15928 {
a776957c
TT
15929 struct symtab *symtab;
15930 if (sym != nullptr)
15931 symtab = symbol_symtab (sym);
15932 else if (cu->line_header != nullptr)
15933 {
15934 /* Any related symtab will do. */
15935 symtab
7ba99d21 15936 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15937 }
15938 else
15939 {
15940 symtab = nullptr;
15941 complaint (_("could not find suitable "
15942 "symtab for template parameter"
15943 " - DIE at %s [in module %s]"),
15944 sect_offset_str (die->sect_off),
15945 objfile_name (objfile));
15946 }
15947
15948 if (symtab != nullptr)
15949 {
15950 /* Make sure that the symtab is set on the new symbols.
15951 Even though they don't appear in this symtab directly,
15952 other parts of gdb assume that symbols do, and this is
15953 reasonably true. */
15954 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15955 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15956 }
3e1d3d8c
TT
15957 }
15958 }
134d01f1
DJ
15959}
15960
ed6acedd
TT
15961/* Assuming DIE is an enumeration type, and TYPE is its associated
15962 type, update TYPE using some information only available in DIE's
15963 children. In particular, the fields are computed. */
55426c9d
JB
15964
15965static void
15966update_enumeration_type_from_children (struct die_info *die,
15967 struct type *type,
15968 struct dwarf2_cu *cu)
15969{
60f7655a 15970 struct die_info *child_die;
55426c9d
JB
15971 int unsigned_enum = 1;
15972 int flag_enum = 1;
55426c9d 15973
8268c778 15974 auto_obstack obstack;
ed6acedd 15975 std::vector<struct field> fields;
55426c9d 15976
60f7655a
DE
15977 for (child_die = die->child;
15978 child_die != NULL && child_die->tag;
436c571c 15979 child_die = child_die->sibling)
55426c9d
JB
15980 {
15981 struct attribute *attr;
15982 LONGEST value;
15983 const gdb_byte *bytes;
15984 struct dwarf2_locexpr_baton *baton;
15985 const char *name;
60f7655a 15986
55426c9d
JB
15987 if (child_die->tag != DW_TAG_enumerator)
15988 continue;
15989
15990 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15991 if (attr == NULL)
15992 continue;
15993
15994 name = dwarf2_name (child_die, cu);
15995 if (name == NULL)
15996 name = "<anonymous enumerator>";
15997
15998 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15999 &value, &bytes, &baton);
16000 if (value < 0)
16001 {
16002 unsigned_enum = 0;
16003 flag_enum = 0;
16004 }
55426c9d 16005 else
edd45eb0
SM
16006 {
16007 if (count_one_bits_ll (value) >= 2)
16008 flag_enum = 0;
edd45eb0 16009 }
55426c9d 16010
ed6acedd
TT
16011 fields.emplace_back ();
16012 struct field &field = fields.back ();
16013 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16014 SET_FIELD_ENUMVAL (field, value);
16015 }
16016
16017 if (!fields.empty ())
16018 {
5e33d5f4 16019 type->set_num_fields (fields.size ());
3cabb6b0
SM
16020 type->set_fields
16021 ((struct field *)
16022 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16023 memcpy (type->fields (), fields.data (),
ed6acedd 16024 sizeof (struct field) * fields.size ());
55426c9d
JB
16025 }
16026
16027 if (unsigned_enum)
16028 TYPE_UNSIGNED (type) = 1;
16029 if (flag_enum)
16030 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16031}
16032
134d01f1
DJ
16033/* Given a DW_AT_enumeration_type die, set its type. We do not
16034 complete the type's fields yet, or create any symbols. */
c906108c 16035
f792889a 16036static struct type *
134d01f1 16037read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16038{
5e22e966 16039 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16040 struct type *type;
c906108c 16041 struct attribute *attr;
0114d602 16042 const char *name;
134d01f1 16043
348e048f
DE
16044 /* If the definition of this type lives in .debug_types, read that type.
16045 Don't follow DW_AT_specification though, that will take us back up
16046 the chain and we want to go down. */
052c8bb8 16047 attr = die->attr (DW_AT_signature);
435d3d88 16048 if (attr != nullptr)
348e048f 16049 {
ac9ec31b 16050 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16051
ac9ec31b 16052 /* The type's CU may not be the same as CU.
02142a6c 16053 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16054 return set_die_type (die, type, cu);
16055 }
16056
c906108c
SS
16057 type = alloc_type (objfile);
16058
67607e24 16059 type->set_code (TYPE_CODE_ENUM);
94af9270 16060 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16061 if (name != NULL)
d0e39ea2 16062 type->set_name (name);
c906108c 16063
0626fc76
TT
16064 attr = dwarf2_attr (die, DW_AT_type, cu);
16065 if (attr != NULL)
16066 {
16067 struct type *underlying_type = die_type (die, cu);
16068
16069 TYPE_TARGET_TYPE (type) = underlying_type;
16070 }
16071
e142c38c 16072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16073 if (attr != nullptr)
c906108c
SS
16074 {
16075 TYPE_LENGTH (type) = DW_UNSND (attr);
16076 }
16077 else
16078 {
16079 TYPE_LENGTH (type) = 0;
16080 }
16081
2b4424c3
TT
16082 maybe_set_alignment (cu, die, type);
16083
137033e9
JB
16084 /* The enumeration DIE can be incomplete. In Ada, any type can be
16085 declared as private in the package spec, and then defined only
16086 inside the package body. Such types are known as Taft Amendment
16087 Types. When another package uses such a type, an incomplete DIE
16088 may be generated by the compiler. */
02eb380e 16089 if (die_is_declaration (die, cu))
876cecd0 16090 TYPE_STUB (type) = 1;
02eb380e 16091
0626fc76
TT
16092 /* If this type has an underlying type that is not a stub, then we
16093 may use its attributes. We always use the "unsigned" attribute
16094 in this situation, because ordinarily we guess whether the type
16095 is unsigned -- but the guess can be wrong and the underlying type
16096 can tell us the reality. However, we defer to a local size
16097 attribute if one exists, because this lets the compiler override
16098 the underlying type if needed. */
16099 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16100 {
9e7c9a03
HD
16101 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16102 underlying_type = check_typedef (underlying_type);
16103 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16104 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16105 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16106 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16107 && TYPE_RAW_ALIGN (underlying_type) != 0)
16108 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16109 }
16110
3d567982
TT
16111 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16112
ed6acedd
TT
16113 set_die_type (die, type, cu);
16114
16115 /* Finish the creation of this type by using the enum's children.
16116 Note that, as usual, this must come after set_die_type to avoid
16117 infinite recursion when trying to compute the names of the
16118 enumerators. */
16119 update_enumeration_type_from_children (die, type, cu);
16120
16121 return type;
134d01f1
DJ
16122}
16123
16124/* Given a pointer to a die which begins an enumeration, process all
16125 the dies that define the members of the enumeration, and create the
16126 symbol for the enumeration type.
16127
16128 NOTE: We reverse the order of the element list. */
16129
16130static void
16131process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16132{
f792889a 16133 struct type *this_type;
134d01f1 16134
f792889a
DJ
16135 this_type = get_die_type (die, cu);
16136 if (this_type == NULL)
16137 this_type = read_enumeration_type (die, cu);
9dc481d3 16138
639d11d3 16139 if (die->child != NULL)
c906108c 16140 {
9dc481d3 16141 struct die_info *child_die;
15d034d0 16142 const char *name;
9dc481d3 16143
639d11d3 16144 child_die = die->child;
c906108c
SS
16145 while (child_die && child_die->tag)
16146 {
16147 if (child_die->tag != DW_TAG_enumerator)
16148 {
e7c27a73 16149 process_die (child_die, cu);
c906108c
SS
16150 }
16151 else
16152 {
39cbfefa
DJ
16153 name = dwarf2_name (child_die, cu);
16154 if (name)
ed6acedd 16155 new_symbol (child_die, this_type, cu);
c906108c
SS
16156 }
16157
436c571c 16158 child_die = child_die->sibling;
c906108c 16159 }
c906108c 16160 }
134d01f1 16161
6c83ed52
TT
16162 /* If we are reading an enum from a .debug_types unit, and the enum
16163 is a declaration, and the enum is not the signatured type in the
16164 unit, then we do not want to add a symbol for it. Adding a
16165 symbol would in some cases obscure the true definition of the
16166 enum, giving users an incomplete type when the definition is
16167 actually available. Note that we do not want to do this for all
16168 enums which are just declarations, because C++0x allows forward
16169 enum declarations. */
3019eac3 16170 if (cu->per_cu->is_debug_types
6c83ed52
TT
16171 && die_is_declaration (die, cu))
16172 {
52dc124a 16173 struct signatured_type *sig_type;
6c83ed52 16174
c0f78cd4 16175 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16176 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16177 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16178 return;
16179 }
16180
f792889a 16181 new_symbol (die, this_type, cu);
c906108c
SS
16182}
16183
16184/* Extract all information from a DW_TAG_array_type DIE and put it in
16185 the DIE's type field. For now, this only handles one dimensional
16186 arrays. */
16187
f792889a 16188static struct type *
e7c27a73 16189read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16190{
5e22e966 16191 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16192 struct die_info *child_die;
7e314c57 16193 struct type *type;
c906108c 16194 struct type *element_type, *range_type, *index_type;
c906108c 16195 struct attribute *attr;
15d034d0 16196 const char *name;
a405673c 16197 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16198 unsigned int bit_stride = 0;
c906108c 16199
e7c27a73 16200 element_type = die_type (die, cu);
c906108c 16201
7e314c57
JK
16202 /* The die_type call above may have already set the type for this DIE. */
16203 type = get_die_type (die, cu);
16204 if (type)
16205 return type;
16206
dc53a7ad
JB
16207 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16208 if (attr != NULL)
a405673c
JB
16209 {
16210 int stride_ok;
293e7e51 16211 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16212
16213 byte_stride_prop
16214 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16215 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16216 prop_type);
a405673c
JB
16217 if (!stride_ok)
16218 {
b98664d3 16219 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16220 " - DIE at %s [in module %s]"),
16221 sect_offset_str (die->sect_off),
5e22e966 16222 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16223 /* Ignore this attribute. We will likely not be able to print
16224 arrays of this type correctly, but there is little we can do
16225 to help if we cannot read the attribute's value. */
16226 byte_stride_prop = NULL;
16227 }
16228 }
dc53a7ad
JB
16229
16230 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16231 if (attr != NULL)
16232 bit_stride = DW_UNSND (attr);
16233
c906108c
SS
16234 /* Irix 6.2 native cc creates array types without children for
16235 arrays with unspecified length. */
639d11d3 16236 if (die->child == NULL)
c906108c 16237 {
46bf5051 16238 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16239 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16240 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16241 byte_stride_prop, bit_stride);
f792889a 16242 return set_die_type (die, type, cu);
c906108c
SS
16243 }
16244
791afaa2 16245 std::vector<struct type *> range_types;
639d11d3 16246 child_die = die->child;
c906108c
SS
16247 while (child_die && child_die->tag)
16248 {
16249 if (child_die->tag == DW_TAG_subrange_type)
16250 {
f792889a 16251 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16252
f792889a 16253 if (child_type != NULL)
a02abb62 16254 {
0963b4bd
MS
16255 /* The range type was succesfully read. Save it for the
16256 array type creation. */
791afaa2 16257 range_types.push_back (child_type);
a02abb62 16258 }
c906108c 16259 }
436c571c 16260 child_die = child_die->sibling;
c906108c
SS
16261 }
16262
16263 /* Dwarf2 dimensions are output from left to right, create the
16264 necessary array types in backwards order. */
7ca2d3a3 16265
c906108c 16266 type = element_type;
7ca2d3a3
DL
16267
16268 if (read_array_order (die, cu) == DW_ORD_col_major)
16269 {
16270 int i = 0;
9a619af0 16271
791afaa2 16272 while (i < range_types.size ())
dc53a7ad 16273 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16274 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16275 }
16276 else
16277 {
791afaa2 16278 size_t ndim = range_types.size ();
7ca2d3a3 16279 while (ndim-- > 0)
dc53a7ad 16280 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16281 byte_stride_prop, bit_stride);
7ca2d3a3 16282 }
c906108c 16283
f5f8a009
EZ
16284 /* Understand Dwarf2 support for vector types (like they occur on
16285 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16286 array type. This is not part of the Dwarf2/3 standard yet, but a
16287 custom vendor extension. The main difference between a regular
16288 array and the vector variant is that vectors are passed by value
16289 to functions. */
e142c38c 16290 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16291 if (attr != nullptr)
ea37ba09 16292 make_vector_type (type);
f5f8a009 16293
dbc98a8b
KW
16294 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16295 implementation may choose to implement triple vectors using this
16296 attribute. */
16297 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16298 if (attr != nullptr)
dbc98a8b
KW
16299 {
16300 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16301 TYPE_LENGTH (type) = DW_UNSND (attr);
16302 else
b98664d3 16303 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16304 "than the total size of elements"));
dbc98a8b
KW
16305 }
16306
39cbfefa
DJ
16307 name = dwarf2_name (die, cu);
16308 if (name)
d0e39ea2 16309 type->set_name (name);
6e70227d 16310
2b4424c3
TT
16311 maybe_set_alignment (cu, die, type);
16312
0963b4bd 16313 /* Install the type in the die. */
7e314c57
JK
16314 set_die_type (die, type, cu);
16315
16316 /* set_die_type should be already done. */
b4ba55a1
JB
16317 set_descriptive_type (type, die, cu);
16318
7e314c57 16319 return type;
c906108c
SS
16320}
16321
7ca2d3a3 16322static enum dwarf_array_dim_ordering
6e70227d 16323read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16324{
16325 struct attribute *attr;
16326
16327 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16328
435d3d88 16329 if (attr != nullptr)
aead7601 16330 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16331
0963b4bd
MS
16332 /* GNU F77 is a special case, as at 08/2004 array type info is the
16333 opposite order to the dwarf2 specification, but data is still
16334 laid out as per normal fortran.
7ca2d3a3 16335
0963b4bd
MS
16336 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16337 version checking. */
7ca2d3a3 16338
905e0470
PM
16339 if (cu->language == language_fortran
16340 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16341 {
16342 return DW_ORD_row_major;
16343 }
16344
6e70227d 16345 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16346 {
16347 case array_column_major:
16348 return DW_ORD_col_major;
16349 case array_row_major:
16350 default:
16351 return DW_ORD_row_major;
16352 };
16353}
16354
72019c9c 16355/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16356 the DIE's type field. */
72019c9c 16357
f792889a 16358static struct type *
72019c9c
GM
16359read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16360{
7e314c57
JK
16361 struct type *domain_type, *set_type;
16362 struct attribute *attr;
f792889a 16363
7e314c57
JK
16364 domain_type = die_type (die, cu);
16365
16366 /* The die_type call above may have already set the type for this DIE. */
16367 set_type = get_die_type (die, cu);
16368 if (set_type)
16369 return set_type;
16370
16371 set_type = create_set_type (NULL, domain_type);
16372
16373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16374 if (attr != nullptr)
d09039dd 16375 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16376
2b4424c3
TT
16377 maybe_set_alignment (cu, die, set_type);
16378
f792889a 16379 return set_die_type (die, set_type, cu);
72019c9c 16380}
7ca2d3a3 16381
0971de02
TT
16382/* A helper for read_common_block that creates a locexpr baton.
16383 SYM is the symbol which we are marking as computed.
16384 COMMON_DIE is the DIE for the common block.
16385 COMMON_LOC is the location expression attribute for the common
16386 block itself.
16387 MEMBER_LOC is the location expression attribute for the particular
16388 member of the common block that we are processing.
16389 CU is the CU from which the above come. */
16390
16391static void
16392mark_common_block_symbol_computed (struct symbol *sym,
16393 struct die_info *common_die,
16394 struct attribute *common_loc,
16395 struct attribute *member_loc,
16396 struct dwarf2_cu *cu)
16397{
5e22e966 16398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16399 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16400 struct dwarf2_locexpr_baton *baton;
16401 gdb_byte *ptr;
16402 unsigned int cu_off;
08feed99 16403 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16404 LONGEST offset = 0;
16405
16406 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16407 gdb_assert (common_loc->form_is_block ());
16408 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16409 || member_loc->form_is_constant ());
0971de02 16410
8d749320 16411 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16412 baton->per_objfile = per_objfile;
0971de02
TT
16413 baton->per_cu = cu->per_cu;
16414 gdb_assert (baton->per_cu);
16415
16416 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16417
cd6c91b4 16418 if (member_loc->form_is_constant ())
0971de02 16419 {
0826b30a 16420 offset = member_loc->constant_value (0);
0971de02
TT
16421 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16422 }
16423 else
16424 baton->size += DW_BLOCK (member_loc)->size;
16425
224c3ddb 16426 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16427 baton->data = ptr;
16428
16429 *ptr++ = DW_OP_call4;
9c541725 16430 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16431 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16432 ptr += 4;
16433
cd6c91b4 16434 if (member_loc->form_is_constant ())
0971de02
TT
16435 {
16436 *ptr++ = DW_OP_addr;
16437 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16438 ptr += cu->header.addr_size;
16439 }
16440 else
16441 {
16442 /* We have to copy the data here, because DW_OP_call4 will only
16443 use a DW_AT_location attribute. */
16444 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16445 ptr += DW_BLOCK (member_loc)->size;
16446 }
16447
16448 *ptr++ = DW_OP_plus;
16449 gdb_assert (ptr - baton->data == baton->size);
16450
0971de02 16451 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16452 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16453}
16454
4357ac6c
TT
16455/* Create appropriate locally-scoped variables for all the
16456 DW_TAG_common_block entries. Also create a struct common_block
16457 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16458 is used to separate the common blocks name namespace from regular
4357ac6c 16459 variable names. */
c906108c
SS
16460
16461static void
e7c27a73 16462read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16463{
0971de02
TT
16464 struct attribute *attr;
16465
16466 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16467 if (attr != nullptr)
0971de02
TT
16468 {
16469 /* Support the .debug_loc offsets. */
4fc6c0d5 16470 if (attr->form_is_block ())
0971de02
TT
16471 {
16472 /* Ok. */
16473 }
cd6c91b4 16474 else if (attr->form_is_section_offset ())
0971de02
TT
16475 {
16476 dwarf2_complex_location_expr_complaint ();
16477 attr = NULL;
16478 }
16479 else
16480 {
16481 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16482 "common block member");
16483 attr = NULL;
16484 }
16485 }
16486
639d11d3 16487 if (die->child != NULL)
c906108c 16488 {
5e22e966 16489 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16490 struct die_info *child_die;
16491 size_t n_entries = 0, size;
16492 struct common_block *common_block;
16493 struct symbol *sym;
74ac6d43 16494
4357ac6c
TT
16495 for (child_die = die->child;
16496 child_die && child_die->tag;
436c571c 16497 child_die = child_die->sibling)
4357ac6c
TT
16498 ++n_entries;
16499
16500 size = (sizeof (struct common_block)
16501 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16502 common_block
16503 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16504 size);
4357ac6c
TT
16505 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16506 common_block->n_entries = 0;
16507
16508 for (child_die = die->child;
16509 child_die && child_die->tag;
436c571c 16510 child_die = child_die->sibling)
4357ac6c
TT
16511 {
16512 /* Create the symbol in the DW_TAG_common_block block in the current
16513 symbol scope. */
e7c27a73 16514 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16515 if (sym != NULL)
16516 {
16517 struct attribute *member_loc;
16518
16519 common_block->contents[common_block->n_entries++] = sym;
16520
16521 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16522 cu);
16523 if (member_loc)
16524 {
16525 /* GDB has handled this for a long time, but it is
16526 not specified by DWARF. It seems to have been
16527 emitted by gfortran at least as recently as:
16528 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16529 complaint (_("Variable in common block has "
0971de02 16530 "DW_AT_data_member_location "
9d8780f0
SM
16531 "- DIE at %s [in module %s]"),
16532 sect_offset_str (child_die->sect_off),
518817b3 16533 objfile_name (objfile));
0971de02 16534
cd6c91b4 16535 if (member_loc->form_is_section_offset ())
0971de02 16536 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16537 else if (member_loc->form_is_constant ()
4fc6c0d5 16538 || member_loc->form_is_block ())
0971de02 16539 {
435d3d88 16540 if (attr != nullptr)
0971de02
TT
16541 mark_common_block_symbol_computed (sym, die, attr,
16542 member_loc, cu);
16543 }
16544 else
16545 dwarf2_complex_location_expr_complaint ();
16546 }
16547 }
c906108c 16548 }
4357ac6c
TT
16549
16550 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16551 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16552 }
16553}
16554
0114d602 16555/* Create a type for a C++ namespace. */
d9fa45fe 16556
0114d602
DJ
16557static struct type *
16558read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16559{
5e22e966 16560 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16561 const char *previous_prefix, *name;
9219021c 16562 int is_anonymous;
0114d602
DJ
16563 struct type *type;
16564
16565 /* For extensions, reuse the type of the original namespace. */
16566 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16567 {
16568 struct die_info *ext_die;
16569 struct dwarf2_cu *ext_cu = cu;
9a619af0 16570
0114d602
DJ
16571 ext_die = dwarf2_extension (die, &ext_cu);
16572 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16573
16574 /* EXT_CU may not be the same as CU.
02142a6c 16575 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16576 return set_die_type (die, type, cu);
16577 }
9219021c 16578
e142c38c 16579 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16580
16581 /* Now build the name of the current namespace. */
16582
0114d602
DJ
16583 previous_prefix = determine_prefix (die, cu);
16584 if (previous_prefix[0] != '\0')
16585 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16586 previous_prefix, name, 0, cu);
0114d602
DJ
16587
16588 /* Create the type. */
19f392bc 16589 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16590
60531b24 16591 return set_die_type (die, type, cu);
0114d602
DJ
16592}
16593
22cee43f 16594/* Read a namespace scope. */
0114d602
DJ
16595
16596static void
16597read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16598{
5e22e966 16599 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16600 int is_anonymous;
9219021c 16601
5c4e30ca
DC
16602 /* Add a symbol associated to this if we haven't seen the namespace
16603 before. Also, add a using directive if it's an anonymous
16604 namespace. */
9219021c 16605
f2f0e013 16606 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16607 {
16608 struct type *type;
16609
0114d602 16610 type = read_type_die (die, cu);
e7c27a73 16611 new_symbol (die, type, cu);
5c4e30ca 16612
e8e80198 16613 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16614 if (is_anonymous)
0114d602
DJ
16615 {
16616 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16617
eb1e02fd 16618 std::vector<const char *> excludes;
804d2729 16619 add_using_directive (using_directives (cu),
7d93a1e0 16620 previous_prefix, type->name (), NULL,
eb1e02fd 16621 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16622 }
5c4e30ca 16623 }
9219021c 16624
639d11d3 16625 if (die->child != NULL)
d9fa45fe 16626 {
639d11d3 16627 struct die_info *child_die = die->child;
6e70227d 16628
d9fa45fe
DC
16629 while (child_die && child_die->tag)
16630 {
e7c27a73 16631 process_die (child_die, cu);
436c571c 16632 child_die = child_die->sibling;
d9fa45fe
DC
16633 }
16634 }
38d518c9
EZ
16635}
16636
f55ee35c
JK
16637/* Read a Fortran module as type. This DIE can be only a declaration used for
16638 imported module. Still we need that type as local Fortran "use ... only"
16639 declaration imports depend on the created type in determine_prefix. */
16640
16641static struct type *
16642read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16643{
5e22e966 16644 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16645 const char *module_name;
f55ee35c
JK
16646 struct type *type;
16647
16648 module_name = dwarf2_name (die, cu);
19f392bc 16649 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16650
f55ee35c
JK
16651 return set_die_type (die, type, cu);
16652}
16653
5d7cb8df
JK
16654/* Read a Fortran module. */
16655
16656static void
16657read_module (struct die_info *die, struct dwarf2_cu *cu)
16658{
16659 struct die_info *child_die = die->child;
530e8392
KB
16660 struct type *type;
16661
16662 type = read_type_die (die, cu);
16663 new_symbol (die, type, cu);
5d7cb8df 16664
5d7cb8df
JK
16665 while (child_die && child_die->tag)
16666 {
16667 process_die (child_die, cu);
436c571c 16668 child_die = child_die->sibling;
5d7cb8df
JK
16669 }
16670}
16671
38d518c9
EZ
16672/* Return the name of the namespace represented by DIE. Set
16673 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16674 namespace. */
16675
16676static const char *
e142c38c 16677namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16678{
16679 struct die_info *current_die;
16680 const char *name = NULL;
16681
16682 /* Loop through the extensions until we find a name. */
16683
16684 for (current_die = die;
16685 current_die != NULL;
f2f0e013 16686 current_die = dwarf2_extension (die, &cu))
38d518c9 16687 {
96553a0c
DE
16688 /* We don't use dwarf2_name here so that we can detect the absence
16689 of a name -> anonymous namespace. */
7d45c7c3 16690 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16691
38d518c9
EZ
16692 if (name != NULL)
16693 break;
16694 }
16695
16696 /* Is it an anonymous namespace? */
16697
16698 *is_anonymous = (name == NULL);
16699 if (*is_anonymous)
2b1dbab0 16700 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16701
16702 return name;
d9fa45fe
DC
16703}
16704
c906108c
SS
16705/* Extract all information from a DW_TAG_pointer_type DIE and add to
16706 the user defined type vector. */
16707
f792889a 16708static struct type *
e7c27a73 16709read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16710{
5e22e966 16711 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16712 struct comp_unit_head *cu_header = &cu->header;
c906108c 16713 struct type *type;
8b2dbe47
KB
16714 struct attribute *attr_byte_size;
16715 struct attribute *attr_address_class;
16716 int byte_size, addr_class;
7e314c57
JK
16717 struct type *target_type;
16718
16719 target_type = die_type (die, cu);
c906108c 16720
7e314c57
JK
16721 /* The die_type call above may have already set the type for this DIE. */
16722 type = get_die_type (die, cu);
16723 if (type)
16724 return type;
16725
16726 type = lookup_pointer_type (target_type);
8b2dbe47 16727
e142c38c 16728 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16729 if (attr_byte_size)
16730 byte_size = DW_UNSND (attr_byte_size);
c906108c 16731 else
8b2dbe47
KB
16732 byte_size = cu_header->addr_size;
16733
e142c38c 16734 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16735 if (attr_address_class)
16736 addr_class = DW_UNSND (attr_address_class);
16737 else
16738 addr_class = DW_ADDR_none;
16739
2b4424c3
TT
16740 ULONGEST alignment = get_alignment (cu, die);
16741
16742 /* If the pointer size, alignment, or address class is different
16743 than the default, create a type variant marked as such and set
16744 the length accordingly. */
16745 if (TYPE_LENGTH (type) != byte_size
16746 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16747 && alignment != TYPE_RAW_ALIGN (type))
16748 || addr_class != DW_ADDR_none)
c906108c 16749 {
5e2b427d 16750 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16751 {
16752 int type_flags;
16753
849957d9 16754 type_flags = gdbarch_address_class_type_flags
5e2b427d 16755 (gdbarch, byte_size, addr_class);
876cecd0
TT
16756 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16757 == 0);
8b2dbe47
KB
16758 type = make_type_with_address_space (type, type_flags);
16759 }
16760 else if (TYPE_LENGTH (type) != byte_size)
16761 {
b98664d3 16762 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16763 }
2b4424c3
TT
16764 else if (TYPE_RAW_ALIGN (type) != alignment)
16765 {
b98664d3 16766 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16767 " - DIE at %s [in module %s]"),
16768 sect_offset_str (die->sect_off),
5e22e966 16769 objfile_name (cu->per_objfile->objfile));
2b4424c3 16770 }
6e70227d 16771 else
9a619af0
MS
16772 {
16773 /* Should we also complain about unhandled address classes? */
16774 }
c906108c 16775 }
8b2dbe47
KB
16776
16777 TYPE_LENGTH (type) = byte_size;
2b4424c3 16778 set_type_align (type, alignment);
f792889a 16779 return set_die_type (die, type, cu);
c906108c
SS
16780}
16781
16782/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16783 the user defined type vector. */
16784
f792889a 16785static struct type *
e7c27a73 16786read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16787{
16788 struct type *type;
16789 struct type *to_type;
16790 struct type *domain;
16791
e7c27a73
DJ
16792 to_type = die_type (die, cu);
16793 domain = die_containing_type (die, cu);
0d5de010 16794
7e314c57
JK
16795 /* The calls above may have already set the type for this DIE. */
16796 type = get_die_type (die, cu);
16797 if (type)
16798 return type;
16799
78134374 16800 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16801 type = lookup_methodptr_type (to_type);
78134374 16802 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16803 {
5e22e966 16804 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16805
16806 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16807 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16808 TYPE_VARARGS (to_type));
16809 type = lookup_methodptr_type (new_type);
16810 }
0d5de010
DJ
16811 else
16812 type = lookup_memberptr_type (to_type, domain);
c906108c 16813
f792889a 16814 return set_die_type (die, type, cu);
c906108c
SS
16815}
16816
4297a3f0 16817/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16818 the user defined type vector. */
16819
f792889a 16820static struct type *
4297a3f0
AV
16821read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16822 enum type_code refcode)
c906108c 16823{
e7c27a73 16824 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16825 struct type *type, *target_type;
c906108c
SS
16826 struct attribute *attr;
16827
4297a3f0
AV
16828 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16829
7e314c57
JK
16830 target_type = die_type (die, cu);
16831
16832 /* The die_type call above may have already set the type for this DIE. */
16833 type = get_die_type (die, cu);
16834 if (type)
16835 return type;
16836
4297a3f0 16837 type = lookup_reference_type (target_type, refcode);
e142c38c 16838 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16839 if (attr != nullptr)
c906108c
SS
16840 {
16841 TYPE_LENGTH (type) = DW_UNSND (attr);
16842 }
16843 else
16844 {
107d2387 16845 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16846 }
2b4424c3 16847 maybe_set_alignment (cu, die, type);
f792889a 16848 return set_die_type (die, type, cu);
c906108c
SS
16849}
16850
cf363f18
MW
16851/* Add the given cv-qualifiers to the element type of the array. GCC
16852 outputs DWARF type qualifiers that apply to an array, not the
16853 element type. But GDB relies on the array element type to carry
16854 the cv-qualifiers. This mimics section 6.7.3 of the C99
16855 specification. */
16856
16857static struct type *
16858add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16859 struct type *base_type, int cnst, int voltl)
16860{
16861 struct type *el_type, *inner_array;
16862
16863 base_type = copy_type (base_type);
16864 inner_array = base_type;
16865
78134374 16866 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16867 {
16868 TYPE_TARGET_TYPE (inner_array) =
16869 copy_type (TYPE_TARGET_TYPE (inner_array));
16870 inner_array = TYPE_TARGET_TYPE (inner_array);
16871 }
16872
16873 el_type = TYPE_TARGET_TYPE (inner_array);
16874 cnst |= TYPE_CONST (el_type);
16875 voltl |= TYPE_VOLATILE (el_type);
16876 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16877
16878 return set_die_type (die, base_type, cu);
16879}
16880
f792889a 16881static struct type *
e7c27a73 16882read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16883{
f792889a 16884 struct type *base_type, *cv_type;
c906108c 16885
e7c27a73 16886 base_type = die_type (die, cu);
7e314c57
JK
16887
16888 /* The die_type call above may have already set the type for this DIE. */
16889 cv_type = get_die_type (die, cu);
16890 if (cv_type)
16891 return cv_type;
16892
2f608a3a
KW
16893 /* In case the const qualifier is applied to an array type, the element type
16894 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16895 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16896 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16897
f792889a
DJ
16898 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16899 return set_die_type (die, cv_type, cu);
c906108c
SS
16900}
16901
f792889a 16902static struct type *
e7c27a73 16903read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16904{
f792889a 16905 struct type *base_type, *cv_type;
c906108c 16906
e7c27a73 16907 base_type = die_type (die, cu);
7e314c57
JK
16908
16909 /* The die_type call above may have already set the type for this DIE. */
16910 cv_type = get_die_type (die, cu);
16911 if (cv_type)
16912 return cv_type;
16913
cf363f18
MW
16914 /* In case the volatile qualifier is applied to an array type, the
16915 element type is so qualified, not the array type (section 6.7.3
16916 of C99). */
78134374 16917 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16918 return add_array_cv_type (die, cu, base_type, 0, 1);
16919
f792889a
DJ
16920 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16921 return set_die_type (die, cv_type, cu);
c906108c
SS
16922}
16923
06d66ee9
TT
16924/* Handle DW_TAG_restrict_type. */
16925
16926static struct type *
16927read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16928{
16929 struct type *base_type, *cv_type;
16930
16931 base_type = die_type (die, cu);
16932
16933 /* The die_type call above may have already set the type for this DIE. */
16934 cv_type = get_die_type (die, cu);
16935 if (cv_type)
16936 return cv_type;
16937
16938 cv_type = make_restrict_type (base_type);
16939 return set_die_type (die, cv_type, cu);
16940}
16941
a2c2acaf
MW
16942/* Handle DW_TAG_atomic_type. */
16943
16944static struct type *
16945read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16946{
16947 struct type *base_type, *cv_type;
16948
16949 base_type = die_type (die, cu);
16950
16951 /* The die_type call above may have already set the type for this DIE. */
16952 cv_type = get_die_type (die, cu);
16953 if (cv_type)
16954 return cv_type;
16955
16956 cv_type = make_atomic_type (base_type);
16957 return set_die_type (die, cv_type, cu);
16958}
16959
c906108c
SS
16960/* Extract all information from a DW_TAG_string_type DIE and add to
16961 the user defined type vector. It isn't really a user defined type,
16962 but it behaves like one, with other DIE's using an AT_user_def_type
16963 attribute to reference it. */
16964
f792889a 16965static struct type *
e7c27a73 16966read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16967{
5e22e966 16968 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16969 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16970 struct type *type, *range_type, *index_type, *char_type;
16971 struct attribute *attr;
216a7e6b
AB
16972 struct dynamic_prop prop;
16973 bool length_is_constant = true;
16974 LONGEST length;
16975
16976 /* There are a couple of places where bit sizes might be made use of
16977 when parsing a DW_TAG_string_type, however, no producer that we know
16978 of make use of these. Handling bit sizes that are a multiple of the
16979 byte size is easy enough, but what about other bit sizes? Lets deal
16980 with that problem when we have to. Warn about these attributes being
16981 unsupported, then parse the type and ignore them like we always
16982 have. */
16983 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16984 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16985 {
16986 static bool warning_printed = false;
16987 if (!warning_printed)
16988 {
16989 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16990 "currently supported on DW_TAG_string_type."));
16991 warning_printed = true;
16992 }
16993 }
c906108c 16994
e142c38c 16995 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16996 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16997 {
16998 /* The string length describes the location at which the length of
16999 the string can be found. The size of the length field can be
17000 specified with one of the attributes below. */
17001 struct type *prop_type;
17002 struct attribute *len
17003 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17004 if (len == nullptr)
17005 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17006 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17007 {
17008 /* Pass 0 as the default as we know this attribute is constant
17009 and the default value will not be returned. */
0826b30a 17010 LONGEST sz = len->constant_value (0);
293e7e51 17011 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17012 }
17013 else
17014 {
17015 /* If the size is not specified then we assume it is the size of
17016 an address on this target. */
293e7e51 17017 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17018 }
17019
17020 /* Convert the attribute into a dynamic property. */
17021 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17022 length = 1;
17023 else
17024 length_is_constant = false;
17025 }
17026 else if (attr != nullptr)
17027 {
17028 /* This DW_AT_string_length just contains the length with no
17029 indirection. There's no need to create a dynamic property in this
17030 case. Pass 0 for the default value as we know it will not be
17031 returned in this case. */
0826b30a 17032 length = attr->constant_value (0);
216a7e6b
AB
17033 }
17034 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17035 {
216a7e6b 17036 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17037 length = attr->constant_value (1);
c906108c
SS
17038 }
17039 else
17040 {
216a7e6b
AB
17041 /* Use 1 as a fallback length if we have nothing else. */
17042 length = 1;
c906108c 17043 }
6ccb9162 17044
46bf5051 17045 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17046 if (length_is_constant)
17047 range_type = create_static_range_type (NULL, index_type, 1, length);
17048 else
17049 {
17050 struct dynamic_prop low_bound;
17051
17052 low_bound.kind = PROP_CONST;
17053 low_bound.data.const_val = 1;
17054 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17055 }
3b7538c0
UW
17056 char_type = language_string_char_type (cu->language_defn, gdbarch);
17057 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17058
f792889a 17059 return set_die_type (die, type, cu);
c906108c
SS
17060}
17061
4d804846
JB
17062/* Assuming that DIE corresponds to a function, returns nonzero
17063 if the function is prototyped. */
17064
17065static int
17066prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17067{
17068 struct attribute *attr;
17069
17070 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17071 if (attr && (DW_UNSND (attr) != 0))
17072 return 1;
17073
17074 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17075 is only meaningful for C, but the concept also extends to other
4d804846
JB
17076 languages that allow unprototyped functions (Eg: Objective C).
17077 For all other languages, assume that functions are always
17078 prototyped. */
17079 if (cu->language != language_c
17080 && cu->language != language_objc
17081 && cu->language != language_opencl)
17082 return 1;
17083
17084 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17085 prototyped and unprototyped functions; default to prototyped,
17086 since that is more common in modern code (and RealView warns
17087 about unprototyped functions). */
17088 if (producer_is_realview (cu->producer))
17089 return 1;
17090
17091 return 0;
17092}
17093
c906108c
SS
17094/* Handle DIES due to C code like:
17095
17096 struct foo
c5aa993b
JM
17097 {
17098 int (*funcp)(int a, long l);
17099 int b;
17100 };
c906108c 17101
0963b4bd 17102 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17103
f792889a 17104static struct type *
e7c27a73 17105read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17106{
5e22e966 17107 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17108 struct type *type; /* Type that this function returns. */
17109 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17110 struct attribute *attr;
17111
e7c27a73 17112 type = die_type (die, cu);
7e314c57
JK
17113
17114 /* The die_type call above may have already set the type for this DIE. */
17115 ftype = get_die_type (die, cu);
17116 if (ftype)
17117 return ftype;
17118
0c8b41f1 17119 ftype = lookup_function_type (type);
c906108c 17120
4d804846 17121 if (prototyped_function_p (die, cu))
a6c727b2 17122 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17123
c055b101
CV
17124 /* Store the calling convention in the type if it's available in
17125 the subroutine die. Otherwise set the calling convention to
17126 the default value DW_CC_normal. */
17127 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17128 if (attr != nullptr
17129 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17130 TYPE_CALLING_CONVENTION (ftype)
17131 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17132 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17133 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17134 else
17135 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17136
743649fd
MW
17137 /* Record whether the function returns normally to its caller or not
17138 if the DWARF producer set that information. */
17139 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17140 if (attr && (DW_UNSND (attr) != 0))
17141 TYPE_NO_RETURN (ftype) = 1;
17142
76c10ea2
GM
17143 /* We need to add the subroutine type to the die immediately so
17144 we don't infinitely recurse when dealing with parameters
0963b4bd 17145 declared as the same subroutine type. */
76c10ea2 17146 set_die_type (die, ftype, cu);
6e70227d 17147
639d11d3 17148 if (die->child != NULL)
c906108c 17149 {
bb5ed363 17150 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17151 struct die_info *child_die;
8072405b 17152 int nparams, iparams;
c906108c
SS
17153
17154 /* Count the number of parameters.
17155 FIXME: GDB currently ignores vararg functions, but knows about
17156 vararg member functions. */
8072405b 17157 nparams = 0;
639d11d3 17158 child_die = die->child;
c906108c
SS
17159 while (child_die && child_die->tag)
17160 {
17161 if (child_die->tag == DW_TAG_formal_parameter)
17162 nparams++;
17163 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17164 TYPE_VARARGS (ftype) = 1;
436c571c 17165 child_die = child_die->sibling;
c906108c
SS
17166 }
17167
17168 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17169 ftype->set_num_fields (nparams);
3cabb6b0
SM
17170 ftype->set_fields
17171 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17172
8072405b
JK
17173 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17174 even if we error out during the parameters reading below. */
17175 for (iparams = 0; iparams < nparams; iparams++)
17176 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17177
17178 iparams = 0;
639d11d3 17179 child_die = die->child;
c906108c
SS
17180 while (child_die && child_die->tag)
17181 {
17182 if (child_die->tag == DW_TAG_formal_parameter)
17183 {
3ce3b1ba
PA
17184 struct type *arg_type;
17185
17186 /* DWARF version 2 has no clean way to discern C++
17187 static and non-static member functions. G++ helps
17188 GDB by marking the first parameter for non-static
17189 member functions (which is the this pointer) as
17190 artificial. We pass this information to
17191 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17192
17193 DWARF version 3 added DW_AT_object_pointer, which GCC
17194 4.5 does not yet generate. */
e142c38c 17195 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17196 if (attr != nullptr)
c906108c
SS
17197 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17198 else
9c37b5ae 17199 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17200 arg_type = die_type (child_die, cu);
17201
17202 /* RealView does not mark THIS as const, which the testsuite
17203 expects. GCC marks THIS as const in method definitions,
17204 but not in the class specifications (GCC PR 43053). */
17205 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17206 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17207 {
17208 int is_this = 0;
17209 struct dwarf2_cu *arg_cu = cu;
17210 const char *name = dwarf2_name (child_die, cu);
17211
17212 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17213 if (attr != nullptr)
3ce3b1ba
PA
17214 {
17215 /* If the compiler emits this, use it. */
17216 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17217 is_this = 1;
17218 }
17219 else if (name && strcmp (name, "this") == 0)
17220 /* Function definitions will have the argument names. */
17221 is_this = 1;
17222 else if (name == NULL && iparams == 0)
17223 /* Declarations may not have the names, so like
17224 elsewhere in GDB, assume an artificial first
17225 argument is "this". */
17226 is_this = 1;
17227
17228 if (is_this)
17229 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17230 arg_type, 0);
17231 }
17232
17233 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17234 iparams++;
17235 }
436c571c 17236 child_die = child_die->sibling;
c906108c
SS
17237 }
17238 }
17239
76c10ea2 17240 return ftype;
c906108c
SS
17241}
17242
f792889a 17243static struct type *
e7c27a73 17244read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17245{
5e22e966 17246 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17247 const char *name = NULL;
3c8e0968 17248 struct type *this_type, *target_type;
c906108c 17249
94af9270 17250 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17251 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17252 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17253 set_die_type (die, this_type, cu);
3c8e0968
DE
17254 target_type = die_type (die, cu);
17255 if (target_type != this_type)
17256 TYPE_TARGET_TYPE (this_type) = target_type;
17257 else
17258 {
17259 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17260 spec and cause infinite loops in GDB. */
b98664d3 17261 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17262 "- DIE at %s [in module %s]"),
17263 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17264 TYPE_TARGET_TYPE (this_type) = NULL;
17265 }
e4003a34
TV
17266 if (name == NULL)
17267 {
17268 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17269 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17270 Handle these by just returning the target type, rather than
17271 constructing an anonymous typedef type and trying to handle this
17272 elsewhere. */
17273 set_die_type (die, target_type, cu);
17274 return target_type;
17275 }
f792889a 17276 return this_type;
c906108c
SS
17277}
17278
9b790ce7
UW
17279/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17280 (which may be different from NAME) to the architecture back-end to allow
17281 it to guess the correct format if necessary. */
17282
17283static struct type *
17284dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17285 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17286{
08feed99 17287 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17288 const struct floatformat **format;
17289 struct type *type;
17290
17291 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17292 if (format)
103a685e 17293 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17294 else
77b7c781 17295 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17296
17297 return type;
17298}
17299
eb77c9df
AB
17300/* Allocate an integer type of size BITS and name NAME. */
17301
17302static struct type *
17303dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17304 int bits, int unsigned_p, const char *name)
17305{
17306 struct type *type;
17307
17308 /* Versions of Intel's C Compiler generate an integer type called "void"
17309 instead of using DW_TAG_unspecified_type. This has been seen on
17310 at least versions 14, 17, and 18. */
35ee2dc2
AB
17311 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17312 && strcmp (name, "void") == 0)
eb77c9df
AB
17313 type = objfile_type (objfile)->builtin_void;
17314 else
17315 type = init_integer_type (objfile, bits, unsigned_p, name);
17316
17317 return type;
17318}
17319
8bdc1658
AB
17320/* Initialise and return a floating point type of size BITS suitable for
17321 use as a component of a complex number. The NAME_HINT is passed through
17322 when initialising the floating point type and is the name of the complex
17323 type.
17324
17325 As DWARF doesn't currently provide an explicit name for the components
17326 of a complex number, but it can be helpful to have these components
17327 named, we try to select a suitable name based on the size of the
17328 component. */
17329static struct type *
17330dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17331 struct objfile *objfile,
103a685e
TT
17332 int bits, const char *name_hint,
17333 enum bfd_endian byte_order)
8bdc1658 17334{
08feed99 17335 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17336 struct type *tt = nullptr;
17337
35add35e
AB
17338 /* Try to find a suitable floating point builtin type of size BITS.
17339 We're going to use the name of this type as the name for the complex
17340 target type that we are about to create. */
1db455a7 17341 switch (cu->language)
8bdc1658 17342 {
1db455a7
AB
17343 case language_fortran:
17344 switch (bits)
17345 {
17346 case 32:
17347 tt = builtin_f_type (gdbarch)->builtin_real;
17348 break;
17349 case 64:
17350 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17351 break;
17352 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17353 case 128:
17354 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17355 break;
17356 }
8bdc1658 17357 break;
1db455a7
AB
17358 default:
17359 switch (bits)
17360 {
17361 case 32:
17362 tt = builtin_type (gdbarch)->builtin_float;
17363 break;
17364 case 64:
17365 tt = builtin_type (gdbarch)->builtin_double;
17366 break;
17367 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17368 case 128:
17369 tt = builtin_type (gdbarch)->builtin_long_double;
17370 break;
17371 }
8bdc1658
AB
17372 break;
17373 }
17374
35add35e
AB
17375 /* If the type we found doesn't match the size we were looking for, then
17376 pretend we didn't find a type at all, the complex target type we
17377 create will then be nameless. */
a12e5744 17378 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17379 tt = nullptr;
17380
7d93a1e0 17381 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17382 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17383}
17384
c906108c
SS
17385/* Find a representation of a given base type and install
17386 it in the TYPE field of the die. */
17387
f792889a 17388static struct type *
e7c27a73 17389read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17390{
5e22e966 17391 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17392 struct type *type;
17393 struct attribute *attr;
19f392bc 17394 int encoding = 0, bits = 0;
15d034d0 17395 const char *name;
34877895 17396 gdbarch *arch;
c906108c 17397
e142c38c 17398 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17399 if (attr != nullptr)
34877895 17400 encoding = DW_UNSND (attr);
e142c38c 17401 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17402 if (attr != nullptr)
34877895 17403 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17404 name = dwarf2_name (die, cu);
6ccb9162 17405 if (!name)
34877895 17406 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17407
08feed99 17408 arch = objfile->arch ();
103a685e
TT
17409 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17410
34877895
PJ
17411 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17412 if (attr)
103a685e
TT
17413 {
17414 int endianity = DW_UNSND (attr);
17415
17416 switch (endianity)
17417 {
17418 case DW_END_big:
17419 byte_order = BFD_ENDIAN_BIG;
17420 break;
17421 case DW_END_little:
17422 byte_order = BFD_ENDIAN_LITTLE;
17423 break;
17424 default:
17425 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17426 break;
17427 }
17428 }
6ccb9162
UW
17429
17430 switch (encoding)
c906108c 17431 {
6ccb9162
UW
17432 case DW_ATE_address:
17433 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17434 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17435 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17436 break;
17437 case DW_ATE_boolean:
19f392bc 17438 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17439 break;
17440 case DW_ATE_complex_float:
103a685e
TT
17441 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17442 byte_order);
78134374 17443 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17444 {
17445 if (name == nullptr)
17446 {
17447 struct obstack *obstack
5e22e966 17448 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17449 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17450 nullptr);
17451 }
17452 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17453 }
17454 else
17455 type = init_complex_type (name, type);
6ccb9162
UW
17456 break;
17457 case DW_ATE_decimal_float:
19f392bc 17458 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17459 break;
17460 case DW_ATE_float:
103a685e 17461 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17462 break;
17463 case DW_ATE_signed:
eb77c9df 17464 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17465 break;
17466 case DW_ATE_unsigned:
3b2b8fea
TT
17467 if (cu->language == language_fortran
17468 && name
61012eef 17469 && startswith (name, "character("))
19f392bc
UW
17470 type = init_character_type (objfile, bits, 1, name);
17471 else
eb77c9df 17472 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17473 break;
17474 case DW_ATE_signed_char:
6e70227d 17475 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17476 || cu->language == language_pascal
17477 || cu->language == language_fortran)
19f392bc
UW
17478 type = init_character_type (objfile, bits, 0, name);
17479 else
eb77c9df 17480 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17481 break;
17482 case DW_ATE_unsigned_char:
868a0084 17483 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17484 || cu->language == language_pascal
c44af4eb
TT
17485 || cu->language == language_fortran
17486 || cu->language == language_rust)
19f392bc
UW
17487 type = init_character_type (objfile, bits, 1, name);
17488 else
eb77c9df 17489 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17490 break;
75079b2b 17491 case DW_ATE_UTF:
53e710ac 17492 {
53e710ac
PA
17493 if (bits == 16)
17494 type = builtin_type (arch)->builtin_char16;
17495 else if (bits == 32)
17496 type = builtin_type (arch)->builtin_char32;
17497 else
17498 {
b98664d3 17499 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17500 bits);
eb77c9df 17501 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17502 }
17503 return set_die_type (die, type, cu);
17504 }
75079b2b
TT
17505 break;
17506
6ccb9162 17507 default:
b98664d3 17508 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17509 dwarf_type_encoding_name (encoding));
77b7c781 17510 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17511 break;
c906108c 17512 }
6ccb9162 17513
0114d602 17514 if (name && strcmp (name, "char") == 0)
876cecd0 17515 TYPE_NOSIGN (type) = 1;
0114d602 17516
2b4424c3
TT
17517 maybe_set_alignment (cu, die, type);
17518
103a685e 17519 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17520
f792889a 17521 return set_die_type (die, type, cu);
c906108c
SS
17522}
17523
80180f79
SA
17524/* Parse dwarf attribute if it's a block, reference or constant and put the
17525 resulting value of the attribute into struct bound_prop.
17526 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17527
17528static int
17529attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17530 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17531 struct type *default_type)
80180f79
SA
17532{
17533 struct dwarf2_property_baton *baton;
5e22e966 17534 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17535 struct objfile *objfile = per_objfile->objfile;
17536 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17537
9a49df9d
AB
17538 gdb_assert (default_type != NULL);
17539
80180f79
SA
17540 if (attr == NULL || prop == NULL)
17541 return 0;
17542
4fc6c0d5 17543 if (attr->form_is_block ())
80180f79 17544 {
8d749320 17545 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17546 baton->property_type = default_type;
80180f79 17547 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17548 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17549 baton->locexpr.size = DW_BLOCK (attr)->size;
17550 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17551 switch (attr->name)
17552 {
17553 case DW_AT_string_length:
17554 baton->locexpr.is_reference = true;
17555 break;
17556 default:
17557 baton->locexpr.is_reference = false;
17558 break;
17559 }
80180f79
SA
17560 prop->data.baton = baton;
17561 prop->kind = PROP_LOCEXPR;
17562 gdb_assert (prop->data.baton != NULL);
17563 }
cd6c91b4 17564 else if (attr->form_is_ref ())
80180f79
SA
17565 {
17566 struct dwarf2_cu *target_cu = cu;
17567 struct die_info *target_die;
17568 struct attribute *target_attr;
17569
17570 target_die = follow_die_ref (die, attr, &target_cu);
17571 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17572 if (target_attr == NULL)
17573 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17574 target_cu);
80180f79
SA
17575 if (target_attr == NULL)
17576 return 0;
17577
df25ebbd 17578 switch (target_attr->name)
80180f79 17579 {
df25ebbd 17580 case DW_AT_location:
cd6c91b4 17581 if (target_attr->form_is_section_offset ())
df25ebbd 17582 {
8d749320 17583 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17584 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17585 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17586 prop->data.baton = baton;
17587 prop->kind = PROP_LOCLIST;
17588 gdb_assert (prop->data.baton != NULL);
17589 }
4fc6c0d5 17590 else if (target_attr->form_is_block ())
df25ebbd 17591 {
8d749320 17592 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17593 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17594 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17595 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17596 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17597 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17598 baton->locexpr.is_reference = true;
df25ebbd
JB
17599 prop->data.baton = baton;
17600 prop->kind = PROP_LOCEXPR;
17601 gdb_assert (prop->data.baton != NULL);
17602 }
17603 else
17604 {
17605 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17606 "dynamic property");
17607 return 0;
17608 }
17609 break;
17610 case DW_AT_data_member_location:
17611 {
17612 LONGEST offset;
17613
17614 if (!handle_data_member_location (target_die, target_cu,
17615 &offset))
17616 return 0;
17617
8d749320 17618 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17619 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17620 target_cu);
df25ebbd
JB
17621 baton->offset_info.offset = offset;
17622 baton->offset_info.type = die_type (target_die, target_cu);
17623 prop->data.baton = baton;
17624 prop->kind = PROP_ADDR_OFFSET;
17625 break;
17626 }
80180f79
SA
17627 }
17628 }
cd6c91b4 17629 else if (attr->form_is_constant ())
80180f79 17630 {
0826b30a 17631 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17632 prop->kind = PROP_CONST;
17633 }
17634 else
17635 {
17636 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17637 dwarf2_name (die, cu));
17638 return 0;
17639 }
17640
17641 return 1;
17642}
17643
09ba997f 17644/* See read.h. */
9a49df9d 17645
09ba997f 17646struct type *
293e7e51 17647dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17648{
9a49df9d
AB
17649 struct type *int_type;
17650
17651 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17652#define TRY_TYPE(F) \
17653 int_type = (unsigned_p \
17654 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17655 : objfile_type (objfile)->builtin_ ## F); \
17656 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17657 return int_type
17658
17659 TRY_TYPE (char);
17660 TRY_TYPE (short);
17661 TRY_TYPE (int);
17662 TRY_TYPE (long);
17663 TRY_TYPE (long_long);
17664
17665#undef TRY_TYPE
17666
17667 gdb_assert_not_reached ("unable to find suitable integer type");
17668}
17669
09ba997f 17670/* See read.h. */
11a8b164 17671
09ba997f 17672struct type *
293e7e51 17673dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17674{
293e7e51
SM
17675 int addr_size = this->per_cu->addr_size ();
17676 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17677}
17678
b86352cf
AB
17679/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17680 present (which is valid) then compute the default type based on the
17681 compilation units address size. */
17682
17683static struct type *
17684read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17685{
17686 struct type *index_type = die_type (die, cu);
17687
17688 /* Dwarf-2 specifications explicitly allows to create subrange types
17689 without specifying a base type.
17690 In that case, the base type must be set to the type of
17691 the lower bound, upper bound or count, in that order, if any of these
17692 three attributes references an object that has a type.
17693 If no base type is found, the Dwarf-2 specifications say that
17694 a signed integer type of size equal to the size of an address should
17695 be used.
17696 For the following C code: `extern char gdb_int [];'
17697 GCC produces an empty range DIE.
17698 FIXME: muller/2010-05-28: Possible references to object for low bound,
17699 high bound or count are not yet handled by this code. */
78134374 17700 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17701 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17702
17703 return index_type;
17704}
17705
a02abb62
JB
17706/* Read the given DW_AT_subrange DIE. */
17707
f792889a 17708static struct type *
a02abb62
JB
17709read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17710{
4c9ad8c2 17711 struct type *base_type, *orig_base_type;
a02abb62
JB
17712 struct type *range_type;
17713 struct attribute *attr;
729efb13 17714 struct dynamic_prop low, high;
4fae6e18 17715 int low_default_is_valid;
c451ebe5 17716 int high_bound_is_count = 0;
15d034d0 17717 const char *name;
d359392f 17718 ULONGEST negative_mask;
e77813c8 17719
b86352cf
AB
17720 orig_base_type = read_subrange_index_type (die, cu);
17721
4c9ad8c2
TT
17722 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17723 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17724 creating the range type, but we use the result of check_typedef
17725 when examining properties of the type. */
17726 base_type = check_typedef (orig_base_type);
a02abb62 17727
7e314c57
JK
17728 /* The die_type call above may have already set the type for this DIE. */
17729 range_type = get_die_type (die, cu);
17730 if (range_type)
17731 return range_type;
17732
729efb13
SA
17733 low.kind = PROP_CONST;
17734 high.kind = PROP_CONST;
17735 high.data.const_val = 0;
17736
4fae6e18
JK
17737 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17738 omitting DW_AT_lower_bound. */
17739 switch (cu->language)
6e70227d 17740 {
4fae6e18
JK
17741 case language_c:
17742 case language_cplus:
729efb13 17743 low.data.const_val = 0;
4fae6e18
JK
17744 low_default_is_valid = 1;
17745 break;
17746 case language_fortran:
729efb13 17747 low.data.const_val = 1;
4fae6e18
JK
17748 low_default_is_valid = 1;
17749 break;
17750 case language_d:
4fae6e18 17751 case language_objc:
c44af4eb 17752 case language_rust:
729efb13 17753 low.data.const_val = 0;
4fae6e18
JK
17754 low_default_is_valid = (cu->header.version >= 4);
17755 break;
17756 case language_ada:
17757 case language_m2:
17758 case language_pascal:
729efb13 17759 low.data.const_val = 1;
4fae6e18
JK
17760 low_default_is_valid = (cu->header.version >= 4);
17761 break;
17762 default:
729efb13 17763 low.data.const_val = 0;
4fae6e18
JK
17764 low_default_is_valid = 0;
17765 break;
a02abb62
JB
17766 }
17767
e142c38c 17768 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17769 if (attr != nullptr)
9a49df9d 17770 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17771 else if (!low_default_is_valid)
b98664d3 17772 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17773 "- DIE at %s [in module %s]"),
17774 sect_offset_str (die->sect_off),
5e22e966 17775 objfile_name (cu->per_objfile->objfile));
a02abb62 17776
506f5c41
TV
17777 struct attribute *attr_ub, *attr_count;
17778 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17779 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17780 {
506f5c41 17781 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17782 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17783 {
c451ebe5
SA
17784 /* If bounds are constant do the final calculation here. */
17785 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17786 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17787 else
17788 high_bound_is_count = 1;
c2ff108b 17789 }
506f5c41
TV
17790 else
17791 {
17792 if (attr_ub != NULL)
17793 complaint (_("Unresolved DW_AT_upper_bound "
17794 "- DIE at %s [in module %s]"),
17795 sect_offset_str (die->sect_off),
5e22e966 17796 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17797 if (attr_count != NULL)
17798 complaint (_("Unresolved DW_AT_count "
17799 "- DIE at %s [in module %s]"),
17800 sect_offset_str (die->sect_off),
5e22e966 17801 objfile_name (cu->per_objfile->objfile));
506f5c41 17802 }
e77813c8 17803 }
a02abb62 17804
4e962e74
TT
17805 LONGEST bias = 0;
17806 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17807 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17808 bias = bias_attr->constant_value (0);
4e962e74 17809
dbb9c2b1
JB
17810 /* Normally, the DWARF producers are expected to use a signed
17811 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17812 But this is unfortunately not always the case, as witnessed
17813 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17814 is used instead. To work around that ambiguity, we treat
17815 the bounds as signed, and thus sign-extend their values, when
17816 the base type is signed. */
6e70227d 17817 negative_mask =
d359392f 17818 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17819 if (low.kind == PROP_CONST
17820 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17821 low.data.const_val |= negative_mask;
17822 if (high.kind == PROP_CONST
17823 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17824 high.data.const_val |= negative_mask;
43bbcdc2 17825
5bbd8269
AB
17826 /* Check for bit and byte strides. */
17827 struct dynamic_prop byte_stride_prop;
17828 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17829 if (attr_byte_stride != nullptr)
17830 {
293e7e51 17831 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17832 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17833 prop_type);
17834 }
17835
17836 struct dynamic_prop bit_stride_prop;
17837 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17838 if (attr_bit_stride != nullptr)
17839 {
17840 /* It only makes sense to have either a bit or byte stride. */
17841 if (attr_byte_stride != nullptr)
17842 {
17843 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17844 "- DIE at %s [in module %s]"),
17845 sect_offset_str (die->sect_off),
5e22e966 17846 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17847 attr_bit_stride = nullptr;
17848 }
17849 else
17850 {
293e7e51 17851 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17852 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17853 prop_type);
17854 }
17855 }
17856
17857 if (attr_byte_stride != nullptr
17858 || attr_bit_stride != nullptr)
17859 {
17860 bool byte_stride_p = (attr_byte_stride != nullptr);
17861 struct dynamic_prop *stride
17862 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17863
17864 range_type
17865 = create_range_type_with_stride (NULL, orig_base_type, &low,
17866 &high, bias, stride, byte_stride_p);
17867 }
17868 else
17869 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17870
c451ebe5
SA
17871 if (high_bound_is_count)
17872 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17873
c2ff108b
JK
17874 /* Ada expects an empty array on no boundary attributes. */
17875 if (attr == NULL && cu->language != language_ada)
729efb13 17876 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17877
39cbfefa
DJ
17878 name = dwarf2_name (die, cu);
17879 if (name)
d0e39ea2 17880 range_type->set_name (name);
6e70227d 17881
e142c38c 17882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17883 if (attr != nullptr)
a02abb62
JB
17884 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17885
2b4424c3
TT
17886 maybe_set_alignment (cu, die, range_type);
17887
7e314c57
JK
17888 set_die_type (die, range_type, cu);
17889
17890 /* set_die_type should be already done. */
b4ba55a1
JB
17891 set_descriptive_type (range_type, die, cu);
17892
7e314c57 17893 return range_type;
a02abb62 17894}
6e70227d 17895
f792889a 17896static struct type *
81a17f79
JB
17897read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17898{
17899 struct type *type;
81a17f79 17900
5e22e966 17901 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17902 type->set_name (dwarf2_name (die, cu));
81a17f79 17903
74a2f8ff 17904 /* In Ada, an unspecified type is typically used when the description
85102364 17905 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17906 such a type, we treat it as a stub, and try to resolve it later on,
17907 when needed. */
17908 if (cu->language == language_ada)
17909 TYPE_STUB (type) = 1;
17910
f792889a 17911 return set_die_type (die, type, cu);
81a17f79 17912}
a02abb62 17913
639d11d3
DC
17914/* Read a single die and all its descendents. Set the die's sibling
17915 field to NULL; set other fields in the die correctly, and set all
17916 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17917 location of the info_ptr after reading all of those dies. PARENT
17918 is the parent of the die in question. */
17919
17920static struct die_info *
dee91e82 17921read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17922 const gdb_byte *info_ptr,
17923 const gdb_byte **new_info_ptr,
dee91e82 17924 struct die_info *parent)
639d11d3
DC
17925{
17926 struct die_info *die;
d521ce57 17927 const gdb_byte *cur_ptr;
639d11d3 17928
3e225074 17929 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17930 if (die == NULL)
17931 {
17932 *new_info_ptr = cur_ptr;
17933 return NULL;
17934 }
93311388 17935 store_in_ref_table (die, reader->cu);
639d11d3 17936
3e225074 17937 if (die->has_children)
bf6af496 17938 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17939 else
17940 {
17941 die->child = NULL;
17942 *new_info_ptr = cur_ptr;
17943 }
17944
17945 die->sibling = NULL;
17946 die->parent = parent;
17947 return die;
17948}
17949
17950/* Read a die, all of its descendents, and all of its siblings; set
17951 all of the fields of all of the dies correctly. Arguments are as
17952 in read_die_and_children. */
17953
17954static struct die_info *
bf6af496 17955read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17956 const gdb_byte *info_ptr,
17957 const gdb_byte **new_info_ptr,
bf6af496 17958 struct die_info *parent)
639d11d3
DC
17959{
17960 struct die_info *first_die, *last_sibling;
d521ce57 17961 const gdb_byte *cur_ptr;
639d11d3 17962
c906108c 17963 cur_ptr = info_ptr;
639d11d3
DC
17964 first_die = last_sibling = NULL;
17965
17966 while (1)
c906108c 17967 {
639d11d3 17968 struct die_info *die
dee91e82 17969 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17970
1d325ec1 17971 if (die == NULL)
c906108c 17972 {
639d11d3
DC
17973 *new_info_ptr = cur_ptr;
17974 return first_die;
c906108c 17975 }
1d325ec1
DJ
17976
17977 if (!first_die)
17978 first_die = die;
c906108c 17979 else
1d325ec1
DJ
17980 last_sibling->sibling = die;
17981
17982 last_sibling = die;
c906108c 17983 }
c906108c
SS
17984}
17985
bf6af496
DE
17986/* Read a die, all of its descendents, and all of its siblings; set
17987 all of the fields of all of the dies correctly. Arguments are as
17988 in read_die_and_children.
17989 This the main entry point for reading a DIE and all its children. */
17990
17991static struct die_info *
17992read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17993 const gdb_byte *info_ptr,
17994 const gdb_byte **new_info_ptr,
bf6af496
DE
17995 struct die_info *parent)
17996{
17997 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17998 new_info_ptr, parent);
17999
b4f54984 18000 if (dwarf_die_debug)
bf6af496
DE
18001 {
18002 fprintf_unfiltered (gdb_stdlog,
18003 "Read die from %s@0x%x of %s:\n",
96b79293 18004 reader->die_section->get_name (),
bf6af496
DE
18005 (unsigned) (info_ptr - reader->die_section->buffer),
18006 bfd_get_filename (reader->abfd));
b4f54984 18007 dump_die (die, dwarf_die_debug);
bf6af496
DE
18008 }
18009
18010 return die;
18011}
18012
3019eac3
DE
18013/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18014 attributes.
18015 The caller is responsible for filling in the extra attributes
18016 and updating (*DIEP)->num_attrs.
18017 Set DIEP to point to a newly allocated die with its information,
3e225074 18018 except for its child, sibling, and parent fields. */
93311388 18019
d521ce57 18020static const gdb_byte *
3019eac3 18021read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18022 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18023 int num_extra_attrs)
93311388 18024{
b64f50a1 18025 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18026 struct abbrev_info *abbrev;
18027 struct die_info *die;
18028 struct dwarf2_cu *cu = reader->cu;
18029 bfd *abfd = reader->abfd;
18030
9c541725 18031 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18032 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18033 info_ptr += bytes_read;
18034 if (!abbrev_number)
18035 {
18036 *diep = NULL;
93311388
DE
18037 return info_ptr;
18038 }
18039
685af9cd 18040 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18041 if (!abbrev)
348e048f
DE
18042 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18043 abbrev_number,
18044 bfd_get_filename (abfd));
18045
3019eac3 18046 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18047 die->sect_off = sect_off;
93311388
DE
18048 die->tag = abbrev->tag;
18049 die->abbrev = abbrev_number;
3e225074 18050 die->has_children = abbrev->has_children;
93311388 18051
3019eac3
DE
18052 /* Make the result usable.
18053 The caller needs to update num_attrs after adding the extra
18054 attributes. */
93311388
DE
18055 die->num_attrs = abbrev->num_attrs;
18056
18a8505e 18057 std::vector<int> indexes_that_need_reprocess;
93311388 18058 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18059 {
18060 bool need_reprocess;
18061 info_ptr =
18062 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18063 info_ptr, &need_reprocess);
18064 if (need_reprocess)
18065 indexes_that_need_reprocess.push_back (i);
18066 }
18067
052c8bb8 18068 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18069 if (attr != nullptr)
18070 cu->str_offsets_base = DW_UNSND (attr);
93311388 18071
41144253 18072 attr = die->attr (DW_AT_loclists_base);
18073 if (attr != nullptr)
18074 cu->loclist_base = DW_UNSND (attr);
18075
a39fdb41 18076 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18077 if (maybe_addr_base.has_value ())
18078 cu->addr_base = *maybe_addr_base;
18079 for (int index : indexes_that_need_reprocess)
18080 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18081 *diep = die;
93311388
DE
18082 return info_ptr;
18083}
18084
3019eac3
DE
18085/* Read a die and all its attributes.
18086 Set DIEP to point to a newly allocated die with its information,
3e225074 18087 except for its child, sibling, and parent fields. */
3019eac3 18088
d521ce57 18089static const gdb_byte *
3019eac3 18090read_full_die (const struct die_reader_specs *reader,
3e225074 18091 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18092{
d521ce57 18093 const gdb_byte *result;
bf6af496 18094
3e225074 18095 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18096
b4f54984 18097 if (dwarf_die_debug)
bf6af496
DE
18098 {
18099 fprintf_unfiltered (gdb_stdlog,
18100 "Read die from %s@0x%x of %s:\n",
96b79293 18101 reader->die_section->get_name (),
bf6af496
DE
18102 (unsigned) (info_ptr - reader->die_section->buffer),
18103 bfd_get_filename (reader->abfd));
b4f54984 18104 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18105 }
18106
18107 return result;
3019eac3 18108}
433df2d4 18109\f
c906108c 18110
72bf9492
DJ
18111/* Returns nonzero if TAG represents a type that we might generate a partial
18112 symbol for. */
18113
18114static int
18115is_type_tag_for_partial (int tag)
18116{
18117 switch (tag)
18118 {
18119#if 0
18120 /* Some types that would be reasonable to generate partial symbols for,
18121 that we don't at present. */
18122 case DW_TAG_array_type:
18123 case DW_TAG_file_type:
18124 case DW_TAG_ptr_to_member_type:
18125 case DW_TAG_set_type:
18126 case DW_TAG_string_type:
18127 case DW_TAG_subroutine_type:
18128#endif
18129 case DW_TAG_base_type:
18130 case DW_TAG_class_type:
680b30c7 18131 case DW_TAG_interface_type:
72bf9492
DJ
18132 case DW_TAG_enumeration_type:
18133 case DW_TAG_structure_type:
18134 case DW_TAG_subrange_type:
18135 case DW_TAG_typedef:
18136 case DW_TAG_union_type:
18137 return 1;
18138 default:
18139 return 0;
18140 }
18141}
18142
18143/* Load all DIEs that are interesting for partial symbols into memory. */
18144
18145static struct partial_die_info *
dee91e82 18146load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18147 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18148{
dee91e82 18149 struct dwarf2_cu *cu = reader->cu;
5e22e966 18150 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18151 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18152 unsigned int bytes_read;
5afb4e99 18153 unsigned int load_all = 0;
72bf9492
DJ
18154 int nesting_level = 1;
18155
18156 parent_die = NULL;
18157 last_die = NULL;
18158
7adf1e79
DE
18159 gdb_assert (cu->per_cu != NULL);
18160 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18161 load_all = 1;
18162
72bf9492
DJ
18163 cu->partial_dies
18164 = htab_create_alloc_ex (cu->header.length / 12,
18165 partial_die_hash,
18166 partial_die_eq,
18167 NULL,
18168 &cu->comp_unit_obstack,
18169 hashtab_obstack_allocate,
18170 dummy_obstack_deallocate);
18171
72bf9492
DJ
18172 while (1)
18173 {
685af9cd 18174 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18175
18176 /* A NULL abbrev means the end of a series of children. */
18177 if (abbrev == NULL)
18178 {
18179 if (--nesting_level == 0)
cd9983dd
YQ
18180 return first_die;
18181
72bf9492
DJ
18182 info_ptr += bytes_read;
18183 last_die = parent_die;
18184 parent_die = parent_die->die_parent;
18185 continue;
18186 }
18187
98bfdba5
PA
18188 /* Check for template arguments. We never save these; if
18189 they're seen, we just mark the parent, and go on our way. */
18190 if (parent_die != NULL
18191 && cu->language == language_cplus
18192 && (abbrev->tag == DW_TAG_template_type_param
18193 || abbrev->tag == DW_TAG_template_value_param))
18194 {
18195 parent_die->has_template_arguments = 1;
18196
18197 if (!load_all)
18198 {
18199 /* We don't need a partial DIE for the template argument. */
dee91e82 18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18201 continue;
18202 }
18203 }
18204
0d99eb77 18205 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18206 Skip their other children. */
18207 if (!load_all
18208 && cu->language == language_cplus
18209 && parent_die != NULL
18210 && parent_die->tag == DW_TAG_subprogram)
18211 {
dee91e82 18212 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18213 continue;
18214 }
18215
5afb4e99
DJ
18216 /* Check whether this DIE is interesting enough to save. Normally
18217 we would not be interested in members here, but there may be
18218 later variables referencing them via DW_AT_specification (for
18219 static members). */
18220 if (!load_all
18221 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18222 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18223 && abbrev->tag != DW_TAG_enumerator
18224 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18225 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18226 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18227 && abbrev->tag != DW_TAG_variable
5afb4e99 18228 && abbrev->tag != DW_TAG_namespace
f55ee35c 18229 && abbrev->tag != DW_TAG_module
95554aad 18230 && abbrev->tag != DW_TAG_member
74921315
KS
18231 && abbrev->tag != DW_TAG_imported_unit
18232 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18233 {
18234 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18235 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18236 continue;
18237 }
18238
6f06d47b
YQ
18239 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18240 abbrev);
cd9983dd 18241
48fbe735 18242 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18243
18244 /* This two-pass algorithm for processing partial symbols has a
18245 high cost in cache pressure. Thus, handle some simple cases
18246 here which cover the majority of C partial symbols. DIEs
18247 which neither have specification tags in them, nor could have
18248 specification tags elsewhere pointing at them, can simply be
18249 processed and discarded.
18250
18251 This segment is also optional; scan_partial_symbols and
18252 add_partial_symbol will handle these DIEs if we chain
18253 them in normally. When compilers which do not emit large
18254 quantities of duplicate debug information are more common,
18255 this code can probably be removed. */
18256
18257 /* Any complete simple types at the top level (pretty much all
18258 of them, for a language without namespaces), can be processed
18259 directly. */
18260 if (parent_die == NULL
cd9983dd
YQ
18261 && pdi.has_specification == 0
18262 && pdi.is_declaration == 0
18263 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18264 || pdi.tag == DW_TAG_base_type
18265 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18266 {
cd9983dd 18267 if (building_psymtab && pdi.name != NULL)
31edb802 18268 add_psymbol_to_list (pdi.name, false,
79748972 18269 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18270 psymbol_placement::STATIC,
1762568f 18271 0, cu->language, objfile);
cd9983dd 18272 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18273 continue;
18274 }
18275
d8228535
JK
18276 /* The exception for DW_TAG_typedef with has_children above is
18277 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18278 type_name_or_error will error on such types later.
d8228535
JK
18279
18280 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18281 it could not find the child DIEs referenced later, this is checked
18282 above. In correct DWARF DW_TAG_typedef should have no children. */
18283
cd9983dd 18284 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18285 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18286 "- DIE at %s [in module %s]"),
cd9983dd 18287 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18288
72bf9492
DJ
18289 /* If we're at the second level, and we're an enumerator, and
18290 our parent has no specification (meaning possibly lives in a
18291 namespace elsewhere), then we can add the partial symbol now
18292 instead of queueing it. */
cd9983dd 18293 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18294 && parent_die != NULL
18295 && parent_die->die_parent == NULL
18296 && parent_die->tag == DW_TAG_enumeration_type
18297 && parent_die->has_specification == 0)
18298 {
cd9983dd 18299 if (pdi.name == NULL)
b98664d3 18300 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18301 else if (building_psymtab)
31edb802 18302 add_psymbol_to_list (pdi.name, false,
79748972 18303 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18304 cu->language == language_cplus
75aedd27
TT
18305 ? psymbol_placement::GLOBAL
18306 : psymbol_placement::STATIC,
1762568f 18307 0, cu->language, objfile);
72bf9492 18308
cd9983dd 18309 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18310 continue;
18311 }
18312
cd9983dd 18313 struct partial_die_info *part_die
6f06d47b 18314 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18315
72bf9492
DJ
18316 /* We'll save this DIE so link it in. */
18317 part_die->die_parent = parent_die;
18318 part_die->die_sibling = NULL;
18319 part_die->die_child = NULL;
18320
18321 if (last_die && last_die == parent_die)
18322 last_die->die_child = part_die;
18323 else if (last_die)
18324 last_die->die_sibling = part_die;
18325
18326 last_die = part_die;
18327
18328 if (first_die == NULL)
18329 first_die = part_die;
18330
18331 /* Maybe add the DIE to the hash table. Not all DIEs that we
18332 find interesting need to be in the hash table, because we
18333 also have the parent/sibling/child chains; only those that we
18334 might refer to by offset later during partial symbol reading.
18335
18336 For now this means things that might have be the target of a
18337 DW_AT_specification, DW_AT_abstract_origin, or
18338 DW_AT_extension. DW_AT_extension will refer only to
18339 namespaces; DW_AT_abstract_origin refers to functions (and
18340 many things under the function DIE, but we do not recurse
18341 into function DIEs during partial symbol reading) and
18342 possibly variables as well; DW_AT_specification refers to
18343 declarations. Declarations ought to have the DW_AT_declaration
18344 flag. It happens that GCC forgets to put it in sometimes, but
18345 only for functions, not for types.
18346
18347 Adding more things than necessary to the hash table is harmless
18348 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18349 wasted time in find_partial_die, when we reread the compilation
18350 unit with load_all_dies set. */
72bf9492 18351
5afb4e99 18352 if (load_all
72929c62 18353 || abbrev->tag == DW_TAG_constant
5afb4e99 18354 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18355 || abbrev->tag == DW_TAG_variable
18356 || abbrev->tag == DW_TAG_namespace
18357 || part_die->is_declaration)
18358 {
18359 void **slot;
18360
18361 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18362 to_underlying (part_die->sect_off),
18363 INSERT);
72bf9492
DJ
18364 *slot = part_die;
18365 }
18366
72bf9492 18367 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18368 we have no reason to follow the children of structures; for other
98bfdba5
PA
18369 languages we have to, so that we can get at method physnames
18370 to infer fully qualified class names, for DW_AT_specification,
18371 and for C++ template arguments. For C++, we also look one level
18372 inside functions to find template arguments (if the name of the
18373 function does not already contain the template arguments).
bc30ff58 18374
0a4b0913
AB
18375 For Ada and Fortran, we need to scan the children of subprograms
18376 and lexical blocks as well because these languages allow the
18377 definition of nested entities that could be interesting for the
18378 debugger, such as nested subprograms for instance. */
72bf9492 18379 if (last_die->has_children
5afb4e99
DJ
18380 && (load_all
18381 || last_die->tag == DW_TAG_namespace
f55ee35c 18382 || last_die->tag == DW_TAG_module
72bf9492 18383 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18384 || (cu->language == language_cplus
18385 && last_die->tag == DW_TAG_subprogram
18386 && (last_die->name == NULL
18387 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18388 || (cu->language != language_c
18389 && (last_die->tag == DW_TAG_class_type
680b30c7 18390 || last_die->tag == DW_TAG_interface_type
72bf9492 18391 || last_die->tag == DW_TAG_structure_type
bc30ff58 18392 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18393 || ((cu->language == language_ada
18394 || cu->language == language_fortran)
bc30ff58
JB
18395 && (last_die->tag == DW_TAG_subprogram
18396 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18397 {
18398 nesting_level++;
18399 parent_die = last_die;
18400 continue;
18401 }
18402
18403 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18404 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18405
18406 /* Back to the top, do it again. */
18407 }
18408}
18409
6f06d47b
YQ
18410partial_die_info::partial_die_info (sect_offset sect_off_,
18411 struct abbrev_info *abbrev)
18412 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18413{
18414}
18415
35cc7ed7
YQ
18416/* Read a minimal amount of information into the minimal die structure.
18417 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18418
48fbe735
YQ
18419const gdb_byte *
18420partial_die_info::read (const struct die_reader_specs *reader,
18421 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18422{
dee91e82 18423 struct dwarf2_cu *cu = reader->cu;
5e22e966 18424 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18425 unsigned int i;
c5aa993b 18426 int has_low_pc_attr = 0;
c906108c 18427 int has_high_pc_attr = 0;
91da1414 18428 int high_pc_relative = 0;
c906108c 18429
fd0a254f 18430 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18431 {
e7da7f8f 18432 attribute attr;
18a8505e 18433 bool need_reprocess;
e7da7f8f 18434 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18435 info_ptr, &need_reprocess);
18436 /* String and address offsets that need to do the reprocessing have
18437 already been read at this point, so there is no need to wait until
18438 the loop terminates to do the reprocessing. */
18439 if (need_reprocess)
e7da7f8f 18440 read_attribute_reprocess (reader, &attr);
c906108c 18441 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18442 partial symbol table. */
c906108c
SS
18443 switch (attr.name)
18444 {
18445 case DW_AT_name:
48fbe735 18446 switch (tag)
71c25dea
TT
18447 {
18448 case DW_TAG_compile_unit:
95554aad 18449 case DW_TAG_partial_unit:
348e048f 18450 case DW_TAG_type_unit:
71c25dea
TT
18451 /* Compilation units have a DW_AT_name that is a filename, not
18452 a source language identifier. */
18453 case DW_TAG_enumeration_type:
18454 case DW_TAG_enumerator:
18455 /* These tags always have simple identifiers already; no need
18456 to canonicalize them. */
48fbe735 18457 name = DW_STRING (&attr);
71c25dea
TT
18458 break;
18459 default:
48fbe735
YQ
18460 {
18461 struct objfile *objfile = dwarf2_per_objfile->objfile;
18462
18463 name
be1e3d3e 18464 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18465 }
71c25dea
TT
18466 break;
18467 }
c906108c 18468 break;
31ef98ae 18469 case DW_AT_linkage_name:
c906108c 18470 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18471 /* Note that both forms of linkage name might appear. We
18472 assume they will be the same, and we only store the last
18473 one we see. */
e61108c9 18474 linkage_name = attr.value_as_string ();
787de330
TT
18475 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18476 See https://github.com/rust-lang/rust/issues/32925. */
18477 if (cu->language == language_rust && linkage_name != NULL
18478 && strchr (linkage_name, '{') != NULL)
18479 linkage_name = NULL;
c906108c
SS
18480 break;
18481 case DW_AT_low_pc:
18482 has_low_pc_attr = 1;
cd6c91b4 18483 lowpc = attr.value_as_address ();
c906108c
SS
18484 break;
18485 case DW_AT_high_pc:
18486 has_high_pc_attr = 1;
cd6c91b4
TT
18487 highpc = attr.value_as_address ();
18488 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18489 high_pc_relative = 1;
c906108c
SS
18490 break;
18491 case DW_AT_location:
0963b4bd 18492 /* Support the .debug_loc offsets. */
4fc6c0d5 18493 if (attr.form_is_block ())
8e19ed76 18494 {
48fbe735 18495 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18496 }
cd6c91b4 18497 else if (attr.form_is_section_offset ())
8e19ed76 18498 {
4d3c2250 18499 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18500 }
18501 else
18502 {
4d3c2250
KB
18503 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18504 "partial symbol information");
8e19ed76 18505 }
c906108c 18506 break;
c906108c 18507 case DW_AT_external:
48fbe735 18508 is_external = DW_UNSND (&attr);
c906108c
SS
18509 break;
18510 case DW_AT_declaration:
48fbe735 18511 is_declaration = DW_UNSND (&attr);
c906108c
SS
18512 break;
18513 case DW_AT_type:
48fbe735 18514 has_type = 1;
c906108c
SS
18515 break;
18516 case DW_AT_abstract_origin:
18517 case DW_AT_specification:
72bf9492 18518 case DW_AT_extension:
48fbe735 18519 has_specification = 1;
0826b30a 18520 spec_offset = attr.get_ref_die_offset ();
48fbe735 18521 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18522 || cu->per_cu->is_dwz);
c906108c
SS
18523 break;
18524 case DW_AT_sibling:
18525 /* Ignore absolute siblings, they might point outside of
18526 the current compile unit. */
18527 if (attr.form == DW_FORM_ref_addr)
b98664d3 18528 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18529 else
b9502d3f 18530 {
48fbe735 18531 const gdb_byte *buffer = reader->buffer;
0826b30a 18532 sect_offset off = attr.get_ref_die_offset ();
9c541725 18533 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18534
18535 if (sibling_ptr < info_ptr)
b98664d3 18536 complaint (_("DW_AT_sibling points backwards"));
22869d73 18537 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18538 reader->die_section->overflow_complaint ();
b9502d3f 18539 else
48fbe735 18540 sibling = sibling_ptr;
b9502d3f 18541 }
c906108c 18542 break;
fa4028e9 18543 case DW_AT_byte_size:
48fbe735 18544 has_byte_size = 1;
fa4028e9 18545 break;
ff908ebf 18546 case DW_AT_const_value:
48fbe735 18547 has_const_value = 1;
ff908ebf 18548 break;
68511cec
CES
18549 case DW_AT_calling_convention:
18550 /* DWARF doesn't provide a way to identify a program's source-level
18551 entry point. DW_AT_calling_convention attributes are only meant
18552 to describe functions' calling conventions.
18553
18554 However, because it's a necessary piece of information in
0c1b455e
TT
18555 Fortran, and before DWARF 4 DW_CC_program was the only
18556 piece of debugging information whose definition refers to
18557 a 'main program' at all, several compilers marked Fortran
18558 main programs with DW_CC_program --- even when those
18559 functions use the standard calling conventions.
18560
18561 Although DWARF now specifies a way to provide this
18562 information, we support this practice for backward
18563 compatibility. */
68511cec 18564 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18565 && cu->language == language_fortran)
48fbe735 18566 main_subprogram = 1;
68511cec 18567 break;
481860b3
GB
18568 case DW_AT_inline:
18569 if (DW_UNSND (&attr) == DW_INL_inlined
18570 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18571 may_be_inlined = 1;
481860b3 18572 break;
95554aad
TT
18573
18574 case DW_AT_import:
48fbe735 18575 if (tag == DW_TAG_imported_unit)
36586728 18576 {
0826b30a 18577 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18578 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18579 || cu->per_cu->is_dwz);
18580 }
95554aad
TT
18581 break;
18582
0c1b455e 18583 case DW_AT_main_subprogram:
48fbe735 18584 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18585 break;
18586
05caa1d2
TT
18587 case DW_AT_ranges:
18588 {
18589 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18590 but that requires a full DIE, so instead we just
18591 reimplement it. */
18592 int need_ranges_base = tag != DW_TAG_compile_unit;
18593 unsigned int ranges_offset = (DW_UNSND (&attr)
18594 + (need_ranges_base
18595 ? cu->ranges_base
18596 : 0));
18597
18598 /* Value of the DW_AT_ranges attribute is the offset in the
18599 .debug_ranges section. */
18600 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18601 nullptr))
18602 has_pc_info = 1;
18603 }
18604 break;
18605
c906108c
SS
18606 default:
18607 break;
18608 }
18609 }
18610
10d06d82
TT
18611 /* For Ada, if both the name and the linkage name appear, we prefer
18612 the latter. This lets "catch exception" work better, regardless
18613 of the order in which the name and linkage name were emitted.
18614 Really, though, this is just a workaround for the fact that gdb
18615 doesn't store both the name and the linkage name. */
18616 if (cu->language == language_ada && linkage_name != nullptr)
18617 name = linkage_name;
18618
91da1414 18619 if (high_pc_relative)
48fbe735 18620 highpc += lowpc;
91da1414 18621
9373cf26
JK
18622 if (has_low_pc_attr && has_high_pc_attr)
18623 {
18624 /* When using the GNU linker, .gnu.linkonce. sections are used to
18625 eliminate duplicate copies of functions and vtables and such.
18626 The linker will arbitrarily choose one and discard the others.
18627 The AT_*_pc values for such functions refer to local labels in
18628 these sections. If the section from that file was discarded, the
18629 labels are not in the output, so the relocs get a value of 0.
18630 If this is a discarded function, mark the pc bounds as invalid,
18631 so that GDB will ignore it. */
5989a64e 18632 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18633 {
48fbe735 18634 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18635 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18636
b98664d3 18637 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18638 "for DIE at %s [in module %s]"),
48fbe735
YQ
18639 paddress (gdbarch, lowpc),
18640 sect_offset_str (sect_off),
9d8780f0 18641 objfile_name (objfile));
9373cf26
JK
18642 }
18643 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18644 else if (lowpc >= highpc)
9373cf26 18645 {
48fbe735 18646 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18647 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18648
b98664d3 18649 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18650 "for DIE at %s [in module %s]"),
48fbe735
YQ
18651 paddress (gdbarch, lowpc),
18652 paddress (gdbarch, highpc),
18653 sect_offset_str (sect_off),
9c541725 18654 objfile_name (objfile));
9373cf26
JK
18655 }
18656 else
48fbe735 18657 has_pc_info = 1;
9373cf26 18658 }
85cbf3d3 18659
c906108c
SS
18660 return info_ptr;
18661}
18662
72bf9492
DJ
18663/* Find a cached partial DIE at OFFSET in CU. */
18664
d590ff25
YQ
18665struct partial_die_info *
18666dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18667{
18668 struct partial_die_info *lookup_die = NULL;
6f06d47b 18669 struct partial_die_info part_die (sect_off);
72bf9492 18670
9a3c8263 18671 lookup_die = ((struct partial_die_info *)
d590ff25 18672 htab_find_with_hash (partial_dies, &part_die,
9c541725 18673 to_underlying (sect_off)));
72bf9492 18674
72bf9492
DJ
18675 return lookup_die;
18676}
18677
348e048f
DE
18678/* Find a partial DIE at OFFSET, which may or may not be in CU,
18679 except in the case of .debug_types DIEs which do not reference
18680 outside their CU (they do however referencing other types via
55f1336d 18681 DW_FORM_ref_sig8). */
72bf9492 18682
122cf0f2 18683static const struct cu_partial_die_info
9c541725 18684find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18685{
5e22e966 18686 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18687 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18688 struct dwarf2_per_cu_data *per_cu = NULL;
18689 struct partial_die_info *pd = NULL;
72bf9492 18690
36586728 18691 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18692 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18693 {
d590ff25 18694 pd = cu->find_partial_die (sect_off);
5afb4e99 18695 if (pd != NULL)
fb816e8b 18696 return { cu, pd };
0d99eb77
DE
18697 /* We missed recording what we needed.
18698 Load all dies and try again. */
18699 per_cu = cu->per_cu;
5afb4e99 18700 }
0d99eb77
DE
18701 else
18702 {
18703 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18704 if (cu->per_cu->is_debug_types)
0d99eb77 18705 {
9d8780f0
SM
18706 error (_("Dwarf Error: Type Unit at offset %s contains"
18707 " external reference to offset %s [in module %s].\n"),
18708 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18709 bfd_get_filename (objfile->obfd));
18710 }
9c541725 18711 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18712 dwarf2_per_objfile);
72bf9492 18713
0d99eb77 18714 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18715 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18716
0d99eb77 18717 per_cu->cu->last_used = 0;
d590ff25 18718 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18719 }
5afb4e99 18720
dee91e82
DE
18721 /* If we didn't find it, and not all dies have been loaded,
18722 load them all and try again. */
18723
5afb4e99
DJ
18724 if (pd == NULL && per_cu->load_all_dies == 0)
18725 {
5afb4e99 18726 per_cu->load_all_dies = 1;
fd820528
DE
18727
18728 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18729 THIS_CU->cu may already be in use. So we can't just free it and
18730 replace its DIEs with the ones we read in. Instead, we leave those
18731 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18732 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18733 set. */
ab432490 18734 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18735
d590ff25 18736 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18737 }
18738
18739 if (pd == NULL)
18740 internal_error (__FILE__, __LINE__,
9d8780f0 18741 _("could not find partial DIE %s "
3e43a32a 18742 "in cache [from module %s]\n"),
9d8780f0 18743 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18744 return { per_cu->cu, pd };
72bf9492
DJ
18745}
18746
abc72ce4
DE
18747/* See if we can figure out if the class lives in a namespace. We do
18748 this by looking for a member function; its demangled name will
18749 contain namespace info, if there is any. */
18750
18751static void
18752guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18753 struct dwarf2_cu *cu)
18754{
18755 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18756 what template types look like, because the demangler
18757 frequently doesn't give the same name as the debug info. We
18758 could fix this by only using the demangled name to get the
18759 prefix (but see comment in read_structure_type). */
18760
18761 struct partial_die_info *real_pdi;
18762 struct partial_die_info *child_pdi;
18763
18764 /* If this DIE (this DIE's specification, if any) has a parent, then
18765 we should not do this. We'll prepend the parent's fully qualified
18766 name when we create the partial symbol. */
18767
18768 real_pdi = struct_pdi;
18769 while (real_pdi->has_specification)
fb816e8b 18770 {
122cf0f2
AB
18771 auto res = find_partial_die (real_pdi->spec_offset,
18772 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18773 real_pdi = res.pdi;
18774 cu = res.cu;
18775 }
abc72ce4
DE
18776
18777 if (real_pdi->die_parent != NULL)
18778 return;
18779
18780 for (child_pdi = struct_pdi->die_child;
18781 child_pdi != NULL;
18782 child_pdi = child_pdi->die_sibling)
18783 {
18784 if (child_pdi->tag == DW_TAG_subprogram
18785 && child_pdi->linkage_name != NULL)
18786 {
43816ebc
TT
18787 gdb::unique_xmalloc_ptr<char> actual_class_name
18788 (language_class_name_from_physname (cu->language_defn,
18789 child_pdi->linkage_name));
abc72ce4
DE
18790 if (actual_class_name != NULL)
18791 {
5e22e966 18792 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18793 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18794 }
18795 break;
18796 }
18797 }
18798}
18799
25c11aca
TV
18800/* Return true if a DIE with TAG may have the DW_AT_const_value
18801 attribute. */
18802
18803static bool
18804can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18805{
18806 switch (tag)
18807 {
18808 case DW_TAG_constant:
18809 case DW_TAG_enumerator:
18810 case DW_TAG_formal_parameter:
18811 case DW_TAG_template_value_param:
18812 case DW_TAG_variable:
18813 return true;
18814 }
18815
18816 return false;
18817}
18818
52356b79
YQ
18819void
18820partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18821{
abc72ce4
DE
18822 /* Once we've fixed up a die, there's no point in doing so again.
18823 This also avoids a memory leak if we were to call
18824 guess_partial_die_structure_name multiple times. */
52356b79 18825 if (fixup_called)
abc72ce4
DE
18826 return;
18827
72bf9492
DJ
18828 /* If we found a reference attribute and the DIE has no name, try
18829 to find a name in the referred to DIE. */
18830
52356b79 18831 if (name == NULL && has_specification)
72bf9492
DJ
18832 {
18833 struct partial_die_info *spec_die;
72bf9492 18834
122cf0f2 18835 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18836 spec_die = res.pdi;
18837 cu = res.cu;
72bf9492 18838
52356b79 18839 spec_die->fixup (cu);
72bf9492
DJ
18840
18841 if (spec_die->name)
18842 {
52356b79 18843 name = spec_die->name;
72bf9492
DJ
18844
18845 /* Copy DW_AT_external attribute if it is set. */
18846 if (spec_die->is_external)
52356b79 18847 is_external = spec_die->is_external;
72bf9492
DJ
18848 }
18849 }
18850
25c11aca
TV
18851 if (!has_const_value && has_specification
18852 && can_have_DW_AT_const_value_p (tag))
18853 {
18854 struct partial_die_info *spec_die;
18855
18856 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18857 spec_die = res.pdi;
18858 cu = res.cu;
18859
18860 spec_die->fixup (cu);
18861
18862 if (spec_die->has_const_value)
18863 {
18864 /* Copy DW_AT_const_value attribute if it is set. */
18865 has_const_value = spec_die->has_const_value;
18866 }
18867 }
18868
72bf9492 18869 /* Set default names for some unnamed DIEs. */
72bf9492 18870
52356b79
YQ
18871 if (name == NULL && tag == DW_TAG_namespace)
18872 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18873
abc72ce4
DE
18874 /* If there is no parent die to provide a namespace, and there are
18875 children, see if we can determine the namespace from their linkage
122d1940 18876 name. */
abc72ce4 18877 if (cu->language == language_cplus
5e22e966 18878 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18879 && die_parent == NULL
18880 && has_children
18881 && (tag == DW_TAG_class_type
18882 || tag == DW_TAG_structure_type
18883 || tag == DW_TAG_union_type))
18884 guess_partial_die_structure_name (this, cu);
abc72ce4 18885
53832f31
TT
18886 /* GCC might emit a nameless struct or union that has a linkage
18887 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18888 if (name == NULL
18889 && (tag == DW_TAG_class_type
18890 || tag == DW_TAG_interface_type
18891 || tag == DW_TAG_structure_type
18892 || tag == DW_TAG_union_type)
18893 && linkage_name != NULL)
53832f31 18894 {
43816ebc
TT
18895 gdb::unique_xmalloc_ptr<char> demangled
18896 (gdb_demangle (linkage_name, DMGL_TYPES));
18897 if (demangled != nullptr)
53832f31 18898 {
96408a79
SA
18899 const char *base;
18900
18901 /* Strip any leading namespaces/classes, keep only the base name.
18902 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18903 base = strrchr (demangled.get (), ':');
18904 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18905 base++;
18906 else
43816ebc 18907 base = demangled.get ();
96408a79 18908
5e22e966 18909 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18910 name = objfile->intern (base);
53832f31
TT
18911 }
18912 }
18913
52356b79 18914 fixup_called = 1;
72bf9492
DJ
18915}
18916
41144253 18917/* Read the .debug_loclists header contents from the given SECTION in the
18918 HEADER. */
18919static void
18920read_loclist_header (struct loclist_header *header,
18921 struct dwarf2_section_info *section)
18922{
18923 unsigned int bytes_read;
18924 bfd *abfd = section->get_bfd_owner ();
18925 const gdb_byte *info_ptr = section->buffer;
18926 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18927 info_ptr += bytes_read;
18928 header->version = read_2_bytes (abfd, info_ptr);
18929 info_ptr += 2;
18930 header->addr_size = read_1_byte (abfd, info_ptr);
18931 info_ptr += 1;
18932 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18933 info_ptr += 1;
18934 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18935}
18936
18937/* Return the DW_AT_loclists_base value for the CU. */
18938static ULONGEST
18939lookup_loclist_base (struct dwarf2_cu *cu)
18940{
18941 /* For the .dwo unit, the loclist_base points to the first offset following
18942 the header. The header consists of the following entities-
18943 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18944 bit format)
18945 2. version (2 bytes)
18946 3. address size (1 byte)
18947 4. segment selector size (1 byte)
18948 5. offset entry count (4 bytes)
18949 These sizes are derived as per the DWARFv5 standard. */
18950 if (cu->dwo_unit != nullptr)
18951 {
18952 if (cu->header.initial_length_size == 4)
18953 return LOCLIST_HEADER_SIZE32;
18954 return LOCLIST_HEADER_SIZE64;
18955 }
18956 return cu->loclist_base;
18957}
18958
18959/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18960 array of offsets in the .debug_loclists section. */
18961static CORE_ADDR
18962read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18963{
5e22e966 18964 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18965 struct objfile *objfile = dwarf2_per_objfile->objfile;
18966 bfd *abfd = objfile->obfd;
18967 ULONGEST loclist_base = lookup_loclist_base (cu);
18968 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18969
18970 section->read (objfile);
18971 if (section->buffer == NULL)
18972 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18973 "section [in module %s]"), objfile_name (objfile));
18974 struct loclist_header header;
18975 read_loclist_header (&header, section);
18976 if (loclist_index >= header.offset_entry_count)
18977 complaint (_("DW_FORM_loclistx pointing outside of "
18978 ".debug_loclists offset array [in module %s]"),
18979 objfile_name (objfile));
18980 if (loclist_base + loclist_index * cu->header.offset_size
18981 >= section->size)
18982 complaint (_("DW_FORM_loclistx pointing outside of "
18983 ".debug_loclists section [in module %s]"),
18984 objfile_name (objfile));
18985 const gdb_byte *info_ptr
18986 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18987
18988 if (cu->header.offset_size == 4)
18989 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18990 else
18991 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18992}
18993
18a8505e
AT
18994/* Process the attributes that had to be skipped in the first round. These
18995 attributes are the ones that need str_offsets_base or addr_base attributes.
18996 They could not have been processed in the first round, because at the time
18997 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18998static void
18999read_attribute_reprocess (const struct die_reader_specs *reader,
19000 struct attribute *attr)
18a8505e
AT
19001{
19002 struct dwarf2_cu *cu = reader->cu;
19003 switch (attr->form)
19004 {
19005 case DW_FORM_addrx:
19006 case DW_FORM_GNU_addr_index:
19007 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19008 break;
41144253 19009 case DW_FORM_loclistx:
19010 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19011 break;
18a8505e
AT
19012 case DW_FORM_strx:
19013 case DW_FORM_strx1:
19014 case DW_FORM_strx2:
19015 case DW_FORM_strx3:
19016 case DW_FORM_strx4:
19017 case DW_FORM_GNU_str_index:
19018 {
19019 unsigned int str_index = DW_UNSND (attr);
19020 if (reader->dwo_file != NULL)
19021 {
19022 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19023 DW_STRING_IS_CANONICAL (attr) = 0;
19024 }
19025 else
19026 {
19027 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19028 DW_STRING_IS_CANONICAL (attr) = 0;
19029 }
19030 break;
19031 }
19032 default:
19033 gdb_assert_not_reached (_("Unexpected DWARF form."));
19034 }
19035}
19036
a8329558 19037/* Read an attribute value described by an attribute form. */
c906108c 19038
d521ce57 19039static const gdb_byte *
dee91e82
DE
19040read_attribute_value (const struct die_reader_specs *reader,
19041 struct attribute *attr, unsigned form,
18a8505e
AT
19042 LONGEST implicit_const, const gdb_byte *info_ptr,
19043 bool *need_reprocess)
c906108c 19044{
dee91e82 19045 struct dwarf2_cu *cu = reader->cu;
5e22e966 19046 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19047 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19048 bfd *abfd = reader->abfd;
e7c27a73 19049 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19050 unsigned int bytes_read;
19051 struct dwarf_block *blk;
18a8505e 19052 *need_reprocess = false;
c906108c 19053
aead7601 19054 attr->form = (enum dwarf_form) form;
a8329558 19055 switch (form)
c906108c 19056 {
c906108c 19057 case DW_FORM_ref_addr:
ae411497 19058 if (cu->header.version == 2)
c8a7a66f
TT
19059 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19060 &bytes_read);
ae411497 19061 else
8266302d
TT
19062 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19063 &bytes_read);
ae411497
TT
19064 info_ptr += bytes_read;
19065 break;
36586728 19066 case DW_FORM_GNU_ref_alt:
8266302d 19067 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19068 info_ptr += bytes_read;
19069 break;
ae411497 19070 case DW_FORM_addr:
08feed99
TT
19071 {
19072 struct gdbarch *gdbarch = objfile->arch ();
19073 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19074 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19075 info_ptr += bytes_read;
19076 }
c906108c
SS
19077 break;
19078 case DW_FORM_block2:
7b5a2f43 19079 blk = dwarf_alloc_block (cu);
c906108c
SS
19080 blk->size = read_2_bytes (abfd, info_ptr);
19081 info_ptr += 2;
19082 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19083 info_ptr += blk->size;
19084 DW_BLOCK (attr) = blk;
19085 break;
19086 case DW_FORM_block4:
7b5a2f43 19087 blk = dwarf_alloc_block (cu);
c906108c
SS
19088 blk->size = read_4_bytes (abfd, info_ptr);
19089 info_ptr += 4;
19090 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19091 info_ptr += blk->size;
19092 DW_BLOCK (attr) = blk;
19093 break;
19094 case DW_FORM_data2:
19095 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19096 info_ptr += 2;
19097 break;
19098 case DW_FORM_data4:
19099 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19100 info_ptr += 4;
19101 break;
19102 case DW_FORM_data8:
19103 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19104 info_ptr += 8;
19105 break;
0224619f
JK
19106 case DW_FORM_data16:
19107 blk = dwarf_alloc_block (cu);
19108 blk->size = 16;
19109 blk->data = read_n_bytes (abfd, info_ptr, 16);
19110 info_ptr += 16;
19111 DW_BLOCK (attr) = blk;
19112 break;
2dc7f7b3 19113 case DW_FORM_sec_offset:
8266302d 19114 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19115 info_ptr += bytes_read;
19116 break;
41144253 19117 case DW_FORM_loclistx:
19118 {
19119 *need_reprocess = true;
19120 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19121 info_ptr += bytes_read;
19122 }
19123 break;
c906108c 19124 case DW_FORM_string:
9b1c24c8 19125 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19126 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19127 info_ptr += bytes_read;
19128 break;
4bdf3d34 19129 case DW_FORM_strp:
36586728
TT
19130 if (!cu->per_cu->is_dwz)
19131 {
ed2dc618
SM
19132 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19133 abfd, info_ptr, cu_header,
36586728
TT
19134 &bytes_read);
19135 DW_STRING_IS_CANONICAL (attr) = 0;
19136 info_ptr += bytes_read;
19137 break;
19138 }
19139 /* FALLTHROUGH */
43988095
JK
19140 case DW_FORM_line_strp:
19141 if (!cu->per_cu->is_dwz)
19142 {
86c0bb4c
TT
19143 DW_STRING (attr)
19144 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19145 &bytes_read);
43988095
JK
19146 DW_STRING_IS_CANONICAL (attr) = 0;
19147 info_ptr += bytes_read;
19148 break;
19149 }
19150 /* FALLTHROUGH */
36586728
TT
19151 case DW_FORM_GNU_strp_alt:
19152 {
c3699833 19153 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19154 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19155 &bytes_read);
36586728 19156
0314b390 19157 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19158 DW_STRING_IS_CANONICAL (attr) = 0;
19159 info_ptr += bytes_read;
19160 }
4bdf3d34 19161 break;
2dc7f7b3 19162 case DW_FORM_exprloc:
c906108c 19163 case DW_FORM_block:
7b5a2f43 19164 blk = dwarf_alloc_block (cu);
c906108c
SS
19165 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19166 info_ptr += bytes_read;
19167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19168 info_ptr += blk->size;
19169 DW_BLOCK (attr) = blk;
19170 break;
19171 case DW_FORM_block1:
7b5a2f43 19172 blk = dwarf_alloc_block (cu);
c906108c
SS
19173 blk->size = read_1_byte (abfd, info_ptr);
19174 info_ptr += 1;
19175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19176 info_ptr += blk->size;
19177 DW_BLOCK (attr) = blk;
19178 break;
19179 case DW_FORM_data1:
19180 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19181 info_ptr += 1;
19182 break;
19183 case DW_FORM_flag:
19184 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19185 info_ptr += 1;
19186 break;
2dc7f7b3
TT
19187 case DW_FORM_flag_present:
19188 DW_UNSND (attr) = 1;
19189 break;
c906108c
SS
19190 case DW_FORM_sdata:
19191 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19192 info_ptr += bytes_read;
19193 break;
19194 case DW_FORM_udata:
18a8505e 19195 case DW_FORM_rnglistx:
c906108c
SS
19196 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19197 info_ptr += bytes_read;
19198 break;
19199 case DW_FORM_ref1:
9c541725 19200 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19201 + read_1_byte (abfd, info_ptr));
c906108c
SS
19202 info_ptr += 1;
19203 break;
19204 case DW_FORM_ref2:
9c541725 19205 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19206 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19207 info_ptr += 2;
19208 break;
19209 case DW_FORM_ref4:
9c541725 19210 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19211 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19212 info_ptr += 4;
19213 break;
613e1657 19214 case DW_FORM_ref8:
9c541725 19215 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19216 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19217 info_ptr += 8;
19218 break;
55f1336d 19219 case DW_FORM_ref_sig8:
ac9ec31b 19220 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19221 info_ptr += 8;
19222 break;
c906108c 19223 case DW_FORM_ref_udata:
9c541725 19224 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19225 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19226 info_ptr += bytes_read;
19227 break;
c906108c 19228 case DW_FORM_indirect:
a8329558
KW
19229 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19230 info_ptr += bytes_read;
43988095
JK
19231 if (form == DW_FORM_implicit_const)
19232 {
19233 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19234 info_ptr += bytes_read;
19235 }
19236 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19237 info_ptr, need_reprocess);
43988095
JK
19238 break;
19239 case DW_FORM_implicit_const:
19240 DW_SND (attr) = implicit_const;
a8329558 19241 break;
336d760d 19242 case DW_FORM_addrx:
3019eac3 19243 case DW_FORM_GNU_addr_index:
18a8505e
AT
19244 *need_reprocess = true;
19245 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19246 info_ptr += bytes_read;
19247 break;
cf532bd1 19248 case DW_FORM_strx:
15f18d14
AT
19249 case DW_FORM_strx1:
19250 case DW_FORM_strx2:
19251 case DW_FORM_strx3:
19252 case DW_FORM_strx4:
3019eac3 19253 case DW_FORM_GNU_str_index:
3019eac3 19254 {
15f18d14
AT
19255 ULONGEST str_index;
19256 if (form == DW_FORM_strx1)
19257 {
19258 str_index = read_1_byte (abfd, info_ptr);
19259 info_ptr += 1;
19260 }
19261 else if (form == DW_FORM_strx2)
19262 {
19263 str_index = read_2_bytes (abfd, info_ptr);
19264 info_ptr += 2;
19265 }
19266 else if (form == DW_FORM_strx3)
19267 {
19268 str_index = read_3_bytes (abfd, info_ptr);
19269 info_ptr += 3;
19270 }
19271 else if (form == DW_FORM_strx4)
19272 {
19273 str_index = read_4_bytes (abfd, info_ptr);
19274 info_ptr += 4;
19275 }
19276 else
19277 {
19278 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19279 info_ptr += bytes_read;
19280 }
18a8505e
AT
19281 *need_reprocess = true;
19282 DW_UNSND (attr) = str_index;
19283 }
3019eac3 19284 break;
c906108c 19285 default:
8a3fe4f8 19286 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19287 dwarf_form_name (form),
19288 bfd_get_filename (abfd));
c906108c 19289 }
28e94949 19290
36586728 19291 /* Super hack. */
cd6c91b4 19292 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19293 attr->form = DW_FORM_GNU_ref_alt;
19294
28e94949
JB
19295 /* We have seen instances where the compiler tried to emit a byte
19296 size attribute of -1 which ended up being encoded as an unsigned
19297 0xffffffff. Although 0xffffffff is technically a valid size value,
19298 an object of this size seems pretty unlikely so we can relatively
19299 safely treat these cases as if the size attribute was invalid and
19300 treat them as zero by default. */
19301 if (attr->name == DW_AT_byte_size
19302 && form == DW_FORM_data4
19303 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19304 {
19305 complaint
b98664d3 19306 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19307 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19308 DW_UNSND (attr) = 0;
19309 }
28e94949 19310
c906108c
SS
19311 return info_ptr;
19312}
19313
a8329558
KW
19314/* Read an attribute described by an abbreviated attribute. */
19315
d521ce57 19316static const gdb_byte *
dee91e82
DE
19317read_attribute (const struct die_reader_specs *reader,
19318 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19319 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19320{
19321 attr->name = abbrev->name;
43988095 19322 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19323 abbrev->implicit_const, info_ptr,
19324 need_reprocess);
a8329558
KW
19325}
19326
43988095
JK
19327/* Return pointer to string at .debug_str offset STR_OFFSET. */
19328
19329static const char *
ed2dc618 19330read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19331 LONGEST str_offset)
43988095 19332{
5989a64e
SM
19333 return dwarf2_per_objfile->per_bfd->str.read_string
19334 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19335}
19336
43988095
JK
19337/* Return pointer to string at .debug_str offset as read from BUF.
19338 BUF is assumed to be in a compilation unit described by CU_HEADER.
19339 Return *BYTES_READ_PTR count of bytes read from BUF. */
19340
d521ce57 19341static const char *
ed2dc618
SM
19342read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19343 const gdb_byte *buf,
cf2c3c16
TT
19344 const struct comp_unit_head *cu_header,
19345 unsigned int *bytes_read_ptr)
19346{
8266302d 19347 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19348
4f44ae6c 19349 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19350}
19351
86c0bb4c 19352/* See read.h. */
43988095 19353
86c0bb4c
TT
19354const char *
19355dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19356 const struct comp_unit_head *cu_header,
19357 unsigned int *bytes_read_ptr)
43988095 19358{
86c0bb4c 19359 bfd *abfd = objfile->obfd;
8266302d 19360 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19361
5989a64e 19362 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19363}
19364
3019eac3 19365/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19366 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19367 ADDR_SIZE is the size of addresses from the CU header. */
19368
19369static CORE_ADDR
ed2dc618 19370read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19371 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19372 int addr_size)
3019eac3
DE
19373{
19374 struct objfile *objfile = dwarf2_per_objfile->objfile;
19375 bfd *abfd = objfile->obfd;
19376 const gdb_byte *info_ptr;
18a8505e 19377 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19378
5989a64e
SM
19379 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19380 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19381 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19382 objfile_name (objfile));
18a8505e 19383 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19384 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19385 error (_("DW_FORM_addr_index pointing outside of "
19386 ".debug_addr section [in module %s]"),
4262abfb 19387 objfile_name (objfile));
5989a64e 19388 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19389 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19390 if (addr_size == 4)
19391 return bfd_get_32 (abfd, info_ptr);
19392 else
19393 return bfd_get_64 (abfd, info_ptr);
19394}
19395
19396/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19397
19398static CORE_ADDR
19399read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19400{
5e22e966 19401 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19402 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19403}
19404
19405/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19406
19407static CORE_ADDR
d521ce57 19408read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19409 unsigned int *bytes_read)
19410{
5e22e966 19411 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19412 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19413
19414 return read_addr_index (cu, addr_index);
19415}
19416
450a1bfc 19417/* See read.h. */
3019eac3
DE
19418
19419CORE_ADDR
82ca3f51
SM
19420dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19421 dwarf2_per_objfile *dwarf2_per_objfile,
19422 unsigned int addr_index)
3019eac3 19423{
3019eac3 19424 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19425 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19426 int addr_size;
19427
3019eac3
DE
19428 /* We need addr_base and addr_size.
19429 If we don't have PER_CU->cu, we have to get it.
19430 Nasty, but the alternative is storing the needed info in PER_CU,
19431 which at this point doesn't seem justified: it's not clear how frequently
19432 it would get used and it would increase the size of every PER_CU.
19433 Entry points like dwarf2_per_cu_addr_size do a similar thing
19434 so we're not in uncharted territory here.
19435 Alas we need to be a bit more complicated as addr_base is contained
19436 in the DIE.
19437
19438 We don't need to read the entire CU(/TU).
19439 We just need the header and top level die.
a1b64ce1 19440
3019eac3 19441 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19442 For now we skip this optimization. */
3019eac3
DE
19443
19444 if (cu != NULL)
19445 {
19446 addr_base = cu->addr_base;
19447 addr_size = cu->header.addr_size;
19448 }
19449 else
19450 {
ab432490 19451 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19452 addr_base = reader.cu->addr_base;
19453 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19454 }
19455
ed2dc618
SM
19456 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19457 addr_size);
3019eac3
DE
19458}
19459
18a8505e
AT
19460/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19461 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19462 DWO file. */
3019eac3 19463
d521ce57 19464static const char *
18a8505e
AT
19465read_str_index (struct dwarf2_cu *cu,
19466 struct dwarf2_section_info *str_section,
19467 struct dwarf2_section_info *str_offsets_section,
19468 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19469{
5e22e966 19470 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19471 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19472 const char *objf_name = objfile_name (objfile);
3019eac3 19473 bfd *abfd = objfile->obfd;
d521ce57 19474 const gdb_byte *info_ptr;
3019eac3 19475 ULONGEST str_offset;
cf532bd1 19476 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19477
96b79293
TT
19478 str_section->read (objfile);
19479 str_offsets_section->read (objfile);
73869dc2 19480 if (str_section->buffer == NULL)
18a8505e 19481 error (_("%s used without %s section"
9d8780f0 19482 " in CU at offset %s [in module %s]"),
96b79293 19483 form_name, str_section->get_name (),
18a8505e 19484 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19485 if (str_offsets_section->buffer == NULL)
18a8505e 19486 error (_("%s used without %s section"
9d8780f0 19487 " in CU at offset %s [in module %s]"),
96b79293 19488 form_name, str_section->get_name (),
18a8505e 19489 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19490 info_ptr = (str_offsets_section->buffer
18a8505e 19491 + str_offsets_base
3019eac3
DE
19492 + str_index * cu->header.offset_size);
19493 if (cu->header.offset_size == 4)
19494 str_offset = bfd_get_32 (abfd, info_ptr);
19495 else
19496 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19497 if (str_offset >= str_section->size)
57d63ce2 19498 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19499 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19500 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19501 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19502}
19503
18a8505e
AT
19504/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19505
19506static const char *
19507read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19508{
19509 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19510 ? reader->cu->header.addr_size : 0;
19511 return read_str_index (reader->cu,
19512 &reader->dwo_file->sections.str,
19513 &reader->dwo_file->sections.str_offsets,
19514 str_offsets_base, str_index);
19515}
19516
19517/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19518
19519static const char *
19520read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19521{
5e22e966 19522 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19523 const char *objf_name = objfile_name (objfile);
19524 static const char form_name[] = "DW_FORM_GNU_str_index";
19525 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19526
19527 if (!cu->str_offsets_base.has_value ())
19528 error (_("%s used in Fission stub without %s"
19529 " in CU at offset 0x%lx [in module %s]"),
19530 form_name, str_offsets_attr_name,
19531 (long) cu->header.offset_size, objf_name);
19532
19533 return read_str_index (cu,
5e22e966
SM
19534 &cu->per_objfile->per_bfd->str,
19535 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19536 *cu->str_offsets_base, str_index);
19537}
19538
3019eac3
DE
19539/* Return the length of an LEB128 number in BUF. */
19540
19541static int
19542leb128_size (const gdb_byte *buf)
19543{
19544 const gdb_byte *begin = buf;
19545 gdb_byte byte;
19546
19547 while (1)
19548 {
19549 byte = *buf++;
19550 if ((byte & 128) == 0)
19551 return buf - begin;
19552 }
19553}
19554
c906108c 19555static void
e142c38c 19556set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19557{
19558 switch (lang)
19559 {
19560 case DW_LANG_C89:
76bee0cc 19561 case DW_LANG_C99:
0cfd832f 19562 case DW_LANG_C11:
c906108c 19563 case DW_LANG_C:
d1be3247 19564 case DW_LANG_UPC:
e142c38c 19565 cu->language = language_c;
c906108c 19566 break;
9c37b5ae 19567 case DW_LANG_Java:
c906108c 19568 case DW_LANG_C_plus_plus:
0cfd832f
MW
19569 case DW_LANG_C_plus_plus_11:
19570 case DW_LANG_C_plus_plus_14:
e142c38c 19571 cu->language = language_cplus;
c906108c 19572 break;
6aecb9c2
JB
19573 case DW_LANG_D:
19574 cu->language = language_d;
19575 break;
c906108c
SS
19576 case DW_LANG_Fortran77:
19577 case DW_LANG_Fortran90:
b21b22e0 19578 case DW_LANG_Fortran95:
f7de9aab
MW
19579 case DW_LANG_Fortran03:
19580 case DW_LANG_Fortran08:
e142c38c 19581 cu->language = language_fortran;
c906108c 19582 break;
a766d390
DE
19583 case DW_LANG_Go:
19584 cu->language = language_go;
19585 break;
c906108c 19586 case DW_LANG_Mips_Assembler:
e142c38c 19587 cu->language = language_asm;
c906108c
SS
19588 break;
19589 case DW_LANG_Ada83:
8aaf0b47 19590 case DW_LANG_Ada95:
bc5f45f8
JB
19591 cu->language = language_ada;
19592 break;
72019c9c
GM
19593 case DW_LANG_Modula2:
19594 cu->language = language_m2;
19595 break;
fe8e67fd
PM
19596 case DW_LANG_Pascal83:
19597 cu->language = language_pascal;
19598 break;
22566fbd
DJ
19599 case DW_LANG_ObjC:
19600 cu->language = language_objc;
19601 break;
c44af4eb
TT
19602 case DW_LANG_Rust:
19603 case DW_LANG_Rust_old:
19604 cu->language = language_rust;
19605 break;
c906108c
SS
19606 case DW_LANG_Cobol74:
19607 case DW_LANG_Cobol85:
c906108c 19608 default:
e142c38c 19609 cu->language = language_minimal;
c906108c
SS
19610 break;
19611 }
e142c38c 19612 cu->language_defn = language_def (cu->language);
c906108c
SS
19613}
19614
19615/* Return the named attribute or NULL if not there. */
19616
19617static struct attribute *
e142c38c 19618dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19619{
a48e046c 19620 for (;;)
c906108c 19621 {
a48e046c
TT
19622 unsigned int i;
19623 struct attribute *spec = NULL;
19624
19625 for (i = 0; i < die->num_attrs; ++i)
19626 {
19627 if (die->attrs[i].name == name)
19628 return &die->attrs[i];
19629 if (die->attrs[i].name == DW_AT_specification
19630 || die->attrs[i].name == DW_AT_abstract_origin)
19631 spec = &die->attrs[i];
19632 }
19633
19634 if (!spec)
19635 break;
c906108c 19636
f2f0e013 19637 die = follow_die_ref (die, spec, &cu);
f2f0e013 19638 }
c5aa993b 19639
c906108c
SS
19640 return NULL;
19641}
19642
7d45c7c3
KB
19643/* Return the string associated with a string-typed attribute, or NULL if it
19644 is either not found or is of an incorrect type. */
19645
19646static const char *
19647dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19648{
19649 struct attribute *attr;
19650 const char *str = NULL;
19651
19652 attr = dwarf2_attr (die, name, cu);
19653
19654 if (attr != NULL)
19655 {
e61108c9
TT
19656 str = attr->value_as_string ();
19657 if (str == nullptr)
b98664d3 19658 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19659 "DIE at %s in module %s"),
19660 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19661 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19662 }
19663
19664 return str;
19665}
19666
a084a2a6 19667/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19668 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19669static const char *
19670dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19671{
19672 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19673 if (dwo_name == nullptr)
19674 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19675 return dwo_name;
19676}
19677
05cf31d1
JB
19678/* Return non-zero iff the attribute NAME is defined for the given DIE,
19679 and holds a non-zero value. This function should only be used for
2dc7f7b3 19680 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19681
19682static int
19683dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19684{
19685 struct attribute *attr = dwarf2_attr (die, name, cu);
19686
19687 return (attr && DW_UNSND (attr));
19688}
19689
3ca72b44 19690static int
e142c38c 19691die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19692{
05cf31d1
JB
19693 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19694 which value is non-zero. However, we have to be careful with
19695 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19696 (via dwarf2_flag_true_p) follows this attribute. So we may
19697 end up accidently finding a declaration attribute that belongs
19698 to a different DIE referenced by the specification attribute,
19699 even though the given DIE does not have a declaration attribute. */
19700 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19701 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19702}
19703
63d06c5c 19704/* Return the die giving the specification for DIE, if there is
f2f0e013 19705 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19706 containing the return value on output. If there is no
19707 specification, but there is an abstract origin, that is
19708 returned. */
63d06c5c
DC
19709
19710static struct die_info *
f2f0e013 19711die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19712{
f2f0e013
DJ
19713 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19714 *spec_cu);
63d06c5c 19715
edb3359d
DJ
19716 if (spec_attr == NULL)
19717 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19718
63d06c5c
DC
19719 if (spec_attr == NULL)
19720 return NULL;
19721 else
f2f0e013 19722 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19723}
c906108c 19724
527f3840
JK
19725/* Stub for free_line_header to match void * callback types. */
19726
19727static void
19728free_line_header_voidp (void *arg)
19729{
9a3c8263 19730 struct line_header *lh = (struct line_header *) arg;
527f3840 19731
fff8551c 19732 delete lh;
527f3840
JK
19733}
19734
83769d0b 19735/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19736
19737static struct dwarf2_section_info *
19738get_debug_line_section (struct dwarf2_cu *cu)
19739{
19740 struct dwarf2_section_info *section;
5e22e966 19741 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19742
19743 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19744 DWO file. */
19745 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19746 section = &cu->dwo_unit->dwo_file->sections.line;
19747 else if (cu->per_cu->is_dwz)
19748 {
c3699833 19749 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19750
19751 section = &dwz->line;
19752 }
19753 else
5989a64e 19754 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19755
19756 return section;
19757}
19758
debd256d 19759/* Read the statement program header starting at OFFSET in
3019eac3 19760 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19761 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19762 Returns NULL if there is a problem reading the header, e.g., if it
19763 has a version we don't understand.
debd256d
JB
19764
19765 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19766 the returned object point into the dwarf line section buffer,
19767 and must not be freed. */
ae2de4f8 19768
fff8551c 19769static line_header_up
9c541725 19770dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19771{
3019eac3 19772 struct dwarf2_section_info *section;
5e22e966 19773 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19774
36586728 19775 section = get_debug_line_section (cu);
96b79293 19776 section->read (dwarf2_per_objfile->objfile);
3019eac3 19777 if (section->buffer == NULL)
debd256d 19778 {
3019eac3 19779 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19780 complaint (_("missing .debug_line.dwo section"));
3019eac3 19781 else
b98664d3 19782 complaint (_("missing .debug_line section"));
debd256d
JB
19783 return 0;
19784 }
19785
0df7ad3a
TT
19786 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19787 dwarf2_per_objfile, section,
19788 &cu->header);
debd256d 19789}
c906108c 19790
c6da4cef 19791/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19792 Return the file name of the psymtab for the given file_entry.
c6da4cef 19793 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19794 If space for the result is malloc'd, *NAME_HOLDER will be set.
19795 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19796
d521ce57 19797static const char *
7ba99d21 19798psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19799 const dwarf2_psymtab *pst,
c89b44cd
TT
19800 const char *comp_dir,
19801 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19802{
d521ce57
TT
19803 const char *include_name = fe.name;
19804 const char *include_name_to_compare = include_name;
72b9f47f 19805 const char *pst_filename;
c6da4cef
DE
19806 int file_is_pst;
19807
8c43009f 19808 const char *dir_name = fe.include_dir (lh);
c6da4cef 19809
c89b44cd 19810 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19811 if (!IS_ABSOLUTE_PATH (include_name)
19812 && (dir_name != NULL || comp_dir != NULL))
19813 {
19814 /* Avoid creating a duplicate psymtab for PST.
19815 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19816 Before we do the comparison, however, we need to account
19817 for DIR_NAME and COMP_DIR.
19818 First prepend dir_name (if non-NULL). If we still don't
19819 have an absolute path prepend comp_dir (if non-NULL).
19820 However, the directory we record in the include-file's
19821 psymtab does not contain COMP_DIR (to match the
19822 corresponding symtab(s)).
19823
19824 Example:
19825
19826 bash$ cd /tmp
19827 bash$ gcc -g ./hello.c
19828 include_name = "hello.c"
19829 dir_name = "."
19830 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19831 DW_AT_name = "./hello.c"
19832
19833 */
c6da4cef
DE
19834
19835 if (dir_name != NULL)
19836 {
c89b44cd
TT
19837 name_holder->reset (concat (dir_name, SLASH_STRING,
19838 include_name, (char *) NULL));
19839 include_name = name_holder->get ();
c6da4cef 19840 include_name_to_compare = include_name;
c6da4cef
DE
19841 }
19842 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19843 {
c89b44cd
TT
19844 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19845 include_name, (char *) NULL));
19846 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19847 }
19848 }
19849
19850 pst_filename = pst->filename;
c89b44cd 19851 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19852 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19853 {
c89b44cd
TT
19854 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19855 pst_filename, (char *) NULL));
19856 pst_filename = copied_name.get ();
c6da4cef
DE
19857 }
19858
1e3fad37 19859 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19860
c6da4cef
DE
19861 if (file_is_pst)
19862 return NULL;
19863 return include_name;
19864}
19865
d9b3de22
DE
19866/* State machine to track the state of the line number program. */
19867
6f77053d 19868class lnp_state_machine
d9b3de22 19869{
6f77053d
PA
19870public:
19871 /* Initialize a machine state for the start of a line number
19872 program. */
804d2729
TT
19873 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19874 bool record_lines_p);
6f77053d 19875
8c43009f
PA
19876 file_entry *current_file ()
19877 {
19878 /* lh->file_names is 0-based, but the file name numbers in the
19879 statement program are 1-based. */
6f77053d
PA
19880 return m_line_header->file_name_at (m_file);
19881 }
19882
19883 /* Record the line in the state machine. END_SEQUENCE is true if
19884 we're processing the end of a sequence. */
19885 void record_line (bool end_sequence);
19886
7ab6656f
OJ
19887 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19888 nop-out rest of the lines in this sequence. */
6f77053d
PA
19889 void check_line_address (struct dwarf2_cu *cu,
19890 const gdb_byte *line_ptr,
7ab6656f 19891 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19892
19893 void handle_set_discriminator (unsigned int discriminator)
19894 {
19895 m_discriminator = discriminator;
19896 m_line_has_non_zero_discriminator |= discriminator != 0;
19897 }
19898
19899 /* Handle DW_LNE_set_address. */
19900 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19901 {
19902 m_op_index = 0;
19903 address += baseaddr;
19904 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19905 }
19906
19907 /* Handle DW_LNS_advance_pc. */
19908 void handle_advance_pc (CORE_ADDR adjust);
19909
19910 /* Handle a special opcode. */
19911 void handle_special_opcode (unsigned char op_code);
19912
19913 /* Handle DW_LNS_advance_line. */
19914 void handle_advance_line (int line_delta)
19915 {
19916 advance_line (line_delta);
19917 }
19918
19919 /* Handle DW_LNS_set_file. */
19920 void handle_set_file (file_name_index file);
19921
19922 /* Handle DW_LNS_negate_stmt. */
19923 void handle_negate_stmt ()
19924 {
19925 m_is_stmt = !m_is_stmt;
19926 }
19927
19928 /* Handle DW_LNS_const_add_pc. */
19929 void handle_const_add_pc ();
19930
19931 /* Handle DW_LNS_fixed_advance_pc. */
19932 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19933 {
19934 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19935 m_op_index = 0;
19936 }
19937
19938 /* Handle DW_LNS_copy. */
19939 void handle_copy ()
19940 {
19941 record_line (false);
19942 m_discriminator = 0;
19943 }
19944
19945 /* Handle DW_LNE_end_sequence. */
19946 void handle_end_sequence ()
19947 {
804d2729 19948 m_currently_recording_lines = true;
6f77053d
PA
19949 }
19950
19951private:
19952 /* Advance the line by LINE_DELTA. */
19953 void advance_line (int line_delta)
19954 {
19955 m_line += line_delta;
19956
19957 if (line_delta != 0)
19958 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19959 }
19960
804d2729
TT
19961 struct dwarf2_cu *m_cu;
19962
6f77053d
PA
19963 gdbarch *m_gdbarch;
19964
19965 /* True if we're recording lines.
19966 Otherwise we're building partial symtabs and are just interested in
19967 finding include files mentioned by the line number program. */
19968 bool m_record_lines_p;
19969
8c43009f 19970 /* The line number header. */
6f77053d 19971 line_header *m_line_header;
8c43009f 19972
6f77053d
PA
19973 /* These are part of the standard DWARF line number state machine,
19974 and initialized according to the DWARF spec. */
d9b3de22 19975
6f77053d 19976 unsigned char m_op_index = 0;
7ba99d21
AT
19977 /* The line table index of the current file. */
19978 file_name_index m_file = 1;
6f77053d
PA
19979 unsigned int m_line = 1;
19980
19981 /* These are initialized in the constructor. */
19982
19983 CORE_ADDR m_address;
19984 bool m_is_stmt;
19985 unsigned int m_discriminator;
d9b3de22
DE
19986
19987 /* Additional bits of state we need to track. */
19988
19989 /* The last file that we called dwarf2_start_subfile for.
19990 This is only used for TLLs. */
6f77053d 19991 unsigned int m_last_file = 0;
d9b3de22 19992 /* The last file a line number was recorded for. */
6f77053d 19993 struct subfile *m_last_subfile = NULL;
d9b3de22 19994
804d2729
TT
19995 /* When true, record the lines we decode. */
19996 bool m_currently_recording_lines = false;
d9b3de22
DE
19997
19998 /* The last line number that was recorded, used to coalesce
19999 consecutive entries for the same line. This can happen, for
20000 example, when discriminators are present. PR 17276. */
6f77053d
PA
20001 unsigned int m_last_line = 0;
20002 bool m_line_has_non_zero_discriminator = false;
8c43009f 20003};
d9b3de22 20004
6f77053d
PA
20005void
20006lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20007{
20008 CORE_ADDR addr_adj = (((m_op_index + adjust)
20009 / m_line_header->maximum_ops_per_instruction)
20010 * m_line_header->minimum_instruction_length);
20011 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20012 m_op_index = ((m_op_index + adjust)
20013 % m_line_header->maximum_ops_per_instruction);
20014}
d9b3de22 20015
6f77053d
PA
20016void
20017lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20018{
6f77053d 20019 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20020 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20021 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20022 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20023 / m_line_header->maximum_ops_per_instruction)
20024 * m_line_header->minimum_instruction_length);
20025 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20026 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20027 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20028
258bf0ee 20029 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20030 advance_line (line_delta);
20031 record_line (false);
20032 m_discriminator = 0;
20033}
d9b3de22 20034
6f77053d
PA
20035void
20036lnp_state_machine::handle_set_file (file_name_index file)
20037{
20038 m_file = file;
20039
20040 const file_entry *fe = current_file ();
20041 if (fe == NULL)
20042 dwarf2_debug_line_missing_file_complaint ();
20043 else if (m_record_lines_p)
20044 {
20045 const char *dir = fe->include_dir (m_line_header);
20046
c24bdb02 20047 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20048 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20049 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20050 }
20051}
20052
20053void
20054lnp_state_machine::handle_const_add_pc ()
20055{
20056 CORE_ADDR adjust
20057 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20058
20059 CORE_ADDR addr_adj
20060 = (((m_op_index + adjust)
20061 / m_line_header->maximum_ops_per_instruction)
20062 * m_line_header->minimum_instruction_length);
20063
20064 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20065 m_op_index = ((m_op_index + adjust)
20066 % m_line_header->maximum_ops_per_instruction);
20067}
d9b3de22 20068
a05a36a5
DE
20069/* Return non-zero if we should add LINE to the line number table.
20070 LINE is the line to add, LAST_LINE is the last line that was added,
20071 LAST_SUBFILE is the subfile for LAST_LINE.
20072 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20073 had a non-zero discriminator.
20074
20075 We have to be careful in the presence of discriminators.
20076 E.g., for this line:
20077
20078 for (i = 0; i < 100000; i++);
20079
20080 clang can emit four line number entries for that one line,
20081 each with a different discriminator.
20082 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20083
20084 However, we want gdb to coalesce all four entries into one.
20085 Otherwise the user could stepi into the middle of the line and
20086 gdb would get confused about whether the pc really was in the
20087 middle of the line.
20088
20089 Things are further complicated by the fact that two consecutive
20090 line number entries for the same line is a heuristic used by gcc
20091 to denote the end of the prologue. So we can't just discard duplicate
20092 entries, we have to be selective about it. The heuristic we use is
20093 that we only collapse consecutive entries for the same line if at least
20094 one of those entries has a non-zero discriminator. PR 17276.
20095
20096 Note: Addresses in the line number state machine can never go backwards
20097 within one sequence, thus this coalescing is ok. */
20098
20099static int
804d2729
TT
20100dwarf_record_line_p (struct dwarf2_cu *cu,
20101 unsigned int line, unsigned int last_line,
a05a36a5
DE
20102 int line_has_non_zero_discriminator,
20103 struct subfile *last_subfile)
20104{
c24bdb02 20105 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20106 return 1;
20107 if (line != last_line)
20108 return 1;
20109 /* Same line for the same file that we've seen already.
20110 As a last check, for pr 17276, only record the line if the line
20111 has never had a non-zero discriminator. */
20112 if (!line_has_non_zero_discriminator)
20113 return 1;
20114 return 0;
20115}
20116
804d2729
TT
20117/* Use the CU's builder to record line number LINE beginning at
20118 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20119
20120static void
d9b3de22 20121dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20122 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20123 struct dwarf2_cu *cu)
252a6764
DE
20124{
20125 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20126
27e0867f
DE
20127 if (dwarf_line_debug)
20128 {
20129 fprintf_unfiltered (gdb_stdlog,
20130 "Recording line %u, file %s, address %s\n",
20131 line, lbasename (subfile->name),
20132 paddress (gdbarch, address));
20133 }
20134
804d2729 20135 if (cu != nullptr)
8c95582d 20136 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20137}
20138
20139/* Subroutine of dwarf_decode_lines_1 to simplify it.
20140 Mark the end of a set of line number records.
d9b3de22 20141 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20142 If SUBFILE is NULL the request is ignored. */
20143
20144static void
20145dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20146 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20147{
27e0867f
DE
20148 if (subfile == NULL)
20149 return;
20150
20151 if (dwarf_line_debug)
20152 {
20153 fprintf_unfiltered (gdb_stdlog,
20154 "Finishing current line, file %s, address %s\n",
20155 lbasename (subfile->name),
20156 paddress (gdbarch, address));
20157 }
20158
8c95582d 20159 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20160}
20161
6f77053d
PA
20162void
20163lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20164{
d9b3de22
DE
20165 if (dwarf_line_debug)
20166 {
20167 fprintf_unfiltered (gdb_stdlog,
20168 "Processing actual line %u: file %u,"
94a72be7 20169 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20170 m_line, m_file,
6f77053d 20171 paddress (m_gdbarch, m_address),
94a72be7
AB
20172 m_is_stmt, m_discriminator,
20173 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20174 }
20175
6f77053d 20176 file_entry *fe = current_file ();
8c43009f
PA
20177
20178 if (fe == NULL)
d9b3de22
DE
20179 dwarf2_debug_line_missing_file_complaint ();
20180 /* For now we ignore lines not starting on an instruction boundary.
20181 But not when processing end_sequence for compatibility with the
20182 previous version of the code. */
6f77053d 20183 else if (m_op_index == 0 || end_sequence)
d9b3de22 20184 {
8c43009f 20185 fe->included_p = 1;
8c95582d 20186 if (m_record_lines_p)
d9b3de22 20187 {
c24bdb02 20188 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20189 || end_sequence)
d9b3de22 20190 {
804d2729
TT
20191 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20192 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20193 }
20194
20195 if (!end_sequence)
20196 {
8c95582d
AB
20197 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20198
804d2729 20199 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20200 m_line_has_non_zero_discriminator,
20201 m_last_subfile))
d9b3de22 20202 {
c24bdb02 20203 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20204 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20205 builder->get_current_subfile (),
8c95582d 20206 m_line, m_address, is_stmt,
804d2729 20207 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20208 }
c24bdb02 20209 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20210 m_last_line = m_line;
d9b3de22
DE
20211 }
20212 }
20213 }
20214}
20215
804d2729
TT
20216lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20217 line_header *lh, bool record_lines_p)
d9b3de22 20218{
804d2729 20219 m_cu = cu;
6f77053d
PA
20220 m_gdbarch = arch;
20221 m_record_lines_p = record_lines_p;
20222 m_line_header = lh;
d9b3de22 20223
804d2729 20224 m_currently_recording_lines = true;
d9b3de22 20225
d9b3de22
DE
20226 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20227 was a line entry for it so that the backend has a chance to adjust it
20228 and also record it in case it needs it. This is currently used by MIPS
20229 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20230 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20231 m_is_stmt = lh->default_is_stmt;
20232 m_discriminator = 0;
252a6764
DE
20233}
20234
6f77053d
PA
20235void
20236lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20237 const gdb_byte *line_ptr,
7ab6656f 20238 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20239{
7ab6656f
OJ
20240 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20241 the pc range of the CU. However, we restrict the test to only ADDRESS
20242 values of zero to preserve GDB's previous behaviour which is to handle
20243 the specific case of a function being GC'd by the linker. */
924c2928 20244
7ab6656f 20245 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20246 {
20247 /* This line table is for a function which has been
20248 GCd by the linker. Ignore it. PR gdb/12528 */
20249
5e22e966 20250 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20251 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20252
b98664d3 20253 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20254 line_offset, objfile_name (objfile));
804d2729
TT
20255 m_currently_recording_lines = false;
20256 /* Note: m_currently_recording_lines is left as false until we see
20257 DW_LNE_end_sequence. */
924c2928
DE
20258 }
20259}
20260
f3f5162e 20261/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20262 Process the line number information in LH.
20263 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20264 program in order to set included_p for every referenced header. */
debd256d 20265
c906108c 20266static void
43f3e411
DE
20267dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20268 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20269{
d521ce57
TT
20270 const gdb_byte *line_ptr, *extended_end;
20271 const gdb_byte *line_end;
a8c50c1f 20272 unsigned int bytes_read, extended_len;
699ca60a 20273 unsigned char op_code, extended_op;
e142c38c 20274 CORE_ADDR baseaddr;
5e22e966 20275 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20276 bfd *abfd = objfile->obfd;
08feed99 20277 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20278 /* True if we're recording line info (as opposed to building partial
20279 symtabs and just interested in finding include files mentioned by
20280 the line number program). */
20281 bool record_lines_p = !decode_for_pst_p;
e142c38c 20282
b3b3bada 20283 baseaddr = objfile->text_section_offset ();
c906108c 20284
debd256d
JB
20285 line_ptr = lh->statement_program_start;
20286 line_end = lh->statement_program_end;
c906108c
SS
20287
20288 /* Read the statement sequences until there's nothing left. */
20289 while (line_ptr < line_end)
20290 {
6f77053d
PA
20291 /* The DWARF line number program state machine. Reset the state
20292 machine at the start of each sequence. */
804d2729 20293 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20294 bool end_sequence = false;
d9b3de22 20295
8c43009f 20296 if (record_lines_p)
c906108c 20297 {
8c43009f
PA
20298 /* Start a subfile for the current file of the state
20299 machine. */
20300 const file_entry *fe = state_machine.current_file ();
20301
20302 if (fe != NULL)
804d2729 20303 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20304 }
20305
a738430d 20306 /* Decode the table. */
d9b3de22 20307 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20308 {
20309 op_code = read_1_byte (abfd, line_ptr);
20310 line_ptr += 1;
9aa1fe7e 20311
debd256d 20312 if (op_code >= lh->opcode_base)
6e70227d 20313 {
8e07a239 20314 /* Special opcode. */
6f77053d 20315 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20316 }
20317 else switch (op_code)
c906108c
SS
20318 {
20319 case DW_LNS_extended_op:
3e43a32a
MS
20320 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20321 &bytes_read);
473b7be6 20322 line_ptr += bytes_read;
a8c50c1f 20323 extended_end = line_ptr + extended_len;
c906108c
SS
20324 extended_op = read_1_byte (abfd, line_ptr);
20325 line_ptr += 1;
20326 switch (extended_op)
20327 {
20328 case DW_LNE_end_sequence:
6f77053d
PA
20329 state_machine.handle_end_sequence ();
20330 end_sequence = true;
c906108c
SS
20331 break;
20332 case DW_LNE_set_address:
d9b3de22
DE
20333 {
20334 CORE_ADDR address
c8a7a66f 20335 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20336 line_ptr += bytes_read;
6f77053d
PA
20337
20338 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20339 lowpc - baseaddr, address);
6f77053d 20340 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20341 }
c906108c
SS
20342 break;
20343 case DW_LNE_define_file:
debd256d 20344 {
d521ce57 20345 const char *cur_file;
ecfb656c
PA
20346 unsigned int mod_time, length;
20347 dir_index dindex;
6e70227d 20348
3e43a32a
MS
20349 cur_file = read_direct_string (abfd, line_ptr,
20350 &bytes_read);
debd256d 20351 line_ptr += bytes_read;
ecfb656c 20352 dindex = (dir_index)
debd256d
JB
20353 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20354 line_ptr += bytes_read;
20355 mod_time =
20356 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20357 line_ptr += bytes_read;
20358 length =
20359 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20360 line_ptr += bytes_read;
ecfb656c 20361 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20362 }
c906108c 20363 break;
d0c6ba3d 20364 case DW_LNE_set_discriminator:
6f77053d
PA
20365 {
20366 /* The discriminator is not interesting to the
20367 debugger; just ignore it. We still need to
20368 check its value though:
20369 if there are consecutive entries for the same
20370 (non-prologue) line we want to coalesce them.
20371 PR 17276. */
20372 unsigned int discr
20373 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20374 line_ptr += bytes_read;
20375
20376 state_machine.handle_set_discriminator (discr);
20377 }
d0c6ba3d 20378 break;
c906108c 20379 default:
b98664d3 20380 complaint (_("mangled .debug_line section"));
debd256d 20381 return;
c906108c 20382 }
a8c50c1f
DJ
20383 /* Make sure that we parsed the extended op correctly. If e.g.
20384 we expected a different address size than the producer used,
20385 we may have read the wrong number of bytes. */
20386 if (line_ptr != extended_end)
20387 {
b98664d3 20388 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20389 return;
20390 }
c906108c
SS
20391 break;
20392 case DW_LNS_copy:
6f77053d 20393 state_machine.handle_copy ();
c906108c
SS
20394 break;
20395 case DW_LNS_advance_pc:
2dc7f7b3
TT
20396 {
20397 CORE_ADDR adjust
20398 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20399 line_ptr += bytes_read;
6f77053d
PA
20400
20401 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20402 }
c906108c
SS
20403 break;
20404 case DW_LNS_advance_line:
a05a36a5
DE
20405 {
20406 int line_delta
20407 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20408 line_ptr += bytes_read;
6f77053d
PA
20409
20410 state_machine.handle_advance_line (line_delta);
a05a36a5 20411 }
c906108c
SS
20412 break;
20413 case DW_LNS_set_file:
d9b3de22 20414 {
6f77053d 20415 file_name_index file
ecfb656c
PA
20416 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20417 &bytes_read);
d9b3de22 20418 line_ptr += bytes_read;
8c43009f 20419
6f77053d 20420 state_machine.handle_set_file (file);
d9b3de22 20421 }
c906108c
SS
20422 break;
20423 case DW_LNS_set_column:
0ad93d4f 20424 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20425 line_ptr += bytes_read;
20426 break;
20427 case DW_LNS_negate_stmt:
6f77053d 20428 state_machine.handle_negate_stmt ();
c906108c
SS
20429 break;
20430 case DW_LNS_set_basic_block:
c906108c 20431 break;
c2c6d25f
JM
20432 /* Add to the address register of the state machine the
20433 address increment value corresponding to special opcode
a738430d
MK
20434 255. I.e., this value is scaled by the minimum
20435 instruction length since special opcode 255 would have
b021a221 20436 scaled the increment. */
c906108c 20437 case DW_LNS_const_add_pc:
6f77053d 20438 state_machine.handle_const_add_pc ();
c906108c
SS
20439 break;
20440 case DW_LNS_fixed_advance_pc:
3e29f34a 20441 {
6f77053d 20442 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20443 line_ptr += 2;
6f77053d
PA
20444
20445 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20446 }
c906108c 20447 break;
9aa1fe7e 20448 default:
a738430d
MK
20449 {
20450 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20451 int i;
a738430d 20452
debd256d 20453 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20454 {
20455 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20456 line_ptr += bytes_read;
20457 }
20458 }
c906108c
SS
20459 }
20460 }
d9b3de22
DE
20461
20462 if (!end_sequence)
20463 dwarf2_debug_line_missing_end_sequence_complaint ();
20464
20465 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20466 in which case we still finish recording the last line). */
6f77053d 20467 state_machine.record_line (true);
c906108c 20468 }
f3f5162e
DE
20469}
20470
20471/* Decode the Line Number Program (LNP) for the given line_header
20472 structure and CU. The actual information extracted and the type
20473 of structures created from the LNP depends on the value of PST.
20474
20475 1. If PST is NULL, then this procedure uses the data from the program
20476 to create all necessary symbol tables, and their linetables.
20477
20478 2. If PST is not NULL, this procedure reads the program to determine
20479 the list of files included by the unit represented by PST, and
20480 builds all the associated partial symbol tables.
20481
20482 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20483 It is used for relative paths in the line table.
20484 NOTE: When processing partial symtabs (pst != NULL),
20485 comp_dir == pst->dirname.
20486
20487 NOTE: It is important that psymtabs have the same file name (via strcmp)
20488 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20489 symtab we don't use it in the name of the psymtabs we create.
20490 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20491 A good testcase for this is mb-inline.exp.
20492
527f3840
JK
20493 LOWPC is the lowest address in CU (or 0 if not known).
20494
20495 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20496 for its PC<->lines mapping information. Otherwise only the filename
20497 table is read in. */
f3f5162e
DE
20498
20499static void
20500dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20501 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20502 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20503{
5e22e966 20504 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20505 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20506
527f3840
JK
20507 if (decode_mapping)
20508 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20509
20510 if (decode_for_pst_p)
20511 {
aaa75496
JB
20512 /* Now that we're done scanning the Line Header Program, we can
20513 create the psymtab of each included file. */
7ba99d21
AT
20514 for (auto &file_entry : lh->file_names ())
20515 if (file_entry.included_p == 1)
aaa75496 20516 {
c89b44cd 20517 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20518 const char *include_name =
7ba99d21
AT
20519 psymtab_include_file_name (lh, file_entry, pst,
20520 comp_dir, &name_holder);
c6da4cef 20521 if (include_name != NULL)
aaa75496
JB
20522 dwarf2_create_include_psymtab (include_name, pst, objfile);
20523 }
20524 }
cb1df416
DJ
20525 else
20526 {
20527 /* Make sure a symtab is created for every file, even files
20528 which contain only variables (i.e. no code with associated
20529 line numbers). */
c24bdb02
KS
20530 buildsym_compunit *builder = cu->get_builder ();
20531 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20532
7ba99d21 20533 for (auto &fe : lh->file_names ())
cb1df416 20534 {
804d2729 20535 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20536 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20537 {
c24bdb02 20538 builder->get_current_subfile ()->symtab
804d2729 20539 = allocate_symtab (cust,
c24bdb02 20540 builder->get_current_subfile ()->name);
43f3e411 20541 }
c24bdb02 20542 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20543 }
20544 }
c906108c
SS
20545}
20546
20547/* Start a subfile for DWARF. FILENAME is the name of the file and
20548 DIRNAME the name of the source directory which contains FILENAME
4d663531 20549 or NULL if not known.
c906108c
SS
20550 This routine tries to keep line numbers from identical absolute and
20551 relative file names in a common subfile.
20552
20553 Using the `list' example from the GDB testsuite, which resides in
20554 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20555 of /srcdir/list0.c yields the following debugging information for list0.c:
20556
c5aa993b 20557 DW_AT_name: /srcdir/list0.c
4d663531 20558 DW_AT_comp_dir: /compdir
357e46e7 20559 files.files[0].name: list0.h
c5aa993b 20560 files.files[0].dir: /srcdir
357e46e7 20561 files.files[1].name: list0.c
c5aa993b 20562 files.files[1].dir: /srcdir
c906108c
SS
20563
20564 The line number information for list0.c has to end up in a single
4f1520fb
FR
20565 subfile, so that `break /srcdir/list0.c:1' works as expected.
20566 start_subfile will ensure that this happens provided that we pass the
20567 concatenation of files.files[1].dir and files.files[1].name as the
20568 subfile's name. */
c906108c
SS
20569
20570static void
804d2729
TT
20571dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20572 const char *dirname)
c906108c 20573{
43816ebc 20574 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20575
4d663531 20576 /* In order not to lose the line information directory,
4f1520fb
FR
20577 we concatenate it to the filename when it makes sense.
20578 Note that the Dwarf3 standard says (speaking of filenames in line
20579 information): ``The directory index is ignored for file names
20580 that represent full path names''. Thus ignoring dirname in the
20581 `else' branch below isn't an issue. */
c906108c 20582
d5166ae1 20583 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20584 {
43816ebc
TT
20585 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20586 filename = copy.get ();
d521ce57 20587 }
c906108c 20588
c24bdb02 20589 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20590}
20591
804d2729
TT
20592/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20593 buildsym_compunit constructor. */
f4dc4d17 20594
c24bdb02
KS
20595struct compunit_symtab *
20596dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20597 CORE_ADDR low_pc)
f4dc4d17 20598{
c24bdb02 20599 gdb_assert (m_builder == nullptr);
43f3e411 20600
c24bdb02 20601 m_builder.reset (new struct buildsym_compunit
f6e649dd 20602 (this->per_objfile->objfile,
c24bdb02 20603 name, comp_dir, language, low_pc));
93b8bea4 20604
c24bdb02 20605 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20606
c24bdb02
KS
20607 get_builder ()->record_debugformat ("DWARF 2");
20608 get_builder ()->record_producer (producer);
f4dc4d17 20609
c24bdb02 20610 processing_has_namespace_info = false;
43f3e411 20611
c24bdb02 20612 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20613}
20614
4c2df51b
DJ
20615static void
20616var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20617 struct dwarf2_cu *cu)
4c2df51b 20618{
5e22e966 20619 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20620 struct comp_unit_head *cu_header = &cu->header;
20621
4c2df51b
DJ
20622 /* NOTE drow/2003-01-30: There used to be a comment and some special
20623 code here to turn a symbol with DW_AT_external and a
20624 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20625 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20626 with some versions of binutils) where shared libraries could have
20627 relocations against symbols in their debug information - the
20628 minimal symbol would have the right address, but the debug info
20629 would not. It's no longer necessary, because we will explicitly
20630 apply relocations when we read in the debug information now. */
20631
20632 /* A DW_AT_location attribute with no contents indicates that a
20633 variable has been optimized away. */
4fc6c0d5 20634 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20635 {
f1e6e072 20636 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20637 return;
20638 }
20639
20640 /* Handle one degenerate form of location expression specially, to
20641 preserve GDB's previous behavior when section offsets are
336d760d
AT
20642 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20643 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20644
4fc6c0d5 20645 if (attr->form_is_block ()
3019eac3
DE
20646 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20647 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20648 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20649 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20650 && (DW_BLOCK (attr)->size
20651 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20652 {
891d2f0b 20653 unsigned int dummy;
4c2df51b 20654
3019eac3 20655 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20656 SET_SYMBOL_VALUE_ADDRESS
20657 (sym, cu->header.read_address (objfile->obfd,
20658 DW_BLOCK (attr)->data + 1,
20659 &dummy));
3019eac3 20660 else
38583298
TT
20661 SET_SYMBOL_VALUE_ADDRESS
20662 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20663 &dummy));
f1e6e072 20664 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20665 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20666 SET_SYMBOL_VALUE_ADDRESS
20667 (sym,
20668 SYMBOL_VALUE_ADDRESS (sym)
20669 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20670 return;
20671 }
20672
20673 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20674 expression evaluator, and use LOC_COMPUTED only when necessary
20675 (i.e. when the value of a register or memory location is
20676 referenced, or a thread-local block, etc.). Then again, it might
20677 not be worthwhile. I'm assuming that it isn't unless performance
20678 or memory numbers show me otherwise. */
20679
f1e6e072 20680 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20681
f1e6e072 20682 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20683 cu->has_loclist = true;
4c2df51b
DJ
20684}
20685
c906108c
SS
20686/* Given a pointer to a DWARF information entry, figure out if we need
20687 to make a symbol table entry for it, and if so, create a new entry
20688 and return a pointer to it.
20689 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20690 used the passed type.
20691 If SPACE is not NULL, use it to hold the new symbol. If it is
20692 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20693
20694static struct symbol *
5e2db402
TT
20695new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20696 struct symbol *space)
c906108c 20697{
5e22e966 20698 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20699 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20700 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20701 struct symbol *sym = NULL;
15d034d0 20702 const char *name;
c906108c
SS
20703 struct attribute *attr = NULL;
20704 struct attribute *attr2 = NULL;
e142c38c 20705 CORE_ADDR baseaddr;
e37fd15a
SW
20706 struct pending **list_to_add = NULL;
20707
edb3359d 20708 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20709
b3b3bada 20710 baseaddr = objfile->text_section_offset ();
c906108c 20711
94af9270 20712 name = dwarf2_name (die, cu);
c906108c
SS
20713 if (name)
20714 {
34eaf542 20715 int suppress_add = 0;
94af9270 20716
34eaf542
TT
20717 if (space)
20718 sym = space;
20719 else
8c14c3a3 20720 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20721 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20722
20723 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20724 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20725 /* Fortran does not have mangling standard and the mangling does differ
20726 between gfortran, iFort etc. */
bcfe6157
TT
20727 const char *physname
20728 = (cu->language == language_fortran
20729 ? dwarf2_full_name (name, die, cu)
20730 : dwarf2_physname (name, die, cu));
20731 const char *linkagename = dw2_linkage_name (die, cu);
20732
20733 if (linkagename == nullptr || cu->language == language_ada)
20734 sym->set_linkage_name (physname);
20735 else
20736 {
20737 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20738 sym->set_linkage_name (linkagename);
20739 }
f55ee35c 20740
c906108c 20741 /* Default assumptions.
c5aa993b 20742 Use the passed type or decode it from the die. */
176620f1 20743 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20744 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20745 if (type != NULL)
20746 SYMBOL_TYPE (sym) = type;
20747 else
e7c27a73 20748 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20749 attr = dwarf2_attr (die,
20750 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20751 cu);
435d3d88 20752 if (attr != nullptr)
c906108c
SS
20753 {
20754 SYMBOL_LINE (sym) = DW_UNSND (attr);
20755 }
cb1df416 20756
edb3359d
DJ
20757 attr = dwarf2_attr (die,
20758 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20759 cu);
435d3d88 20760 if (attr != nullptr)
cb1df416 20761 {
ecfb656c 20762 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20763 struct file_entry *fe;
9a619af0 20764
ecfb656c
PA
20765 if (cu->line_header != NULL)
20766 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20767 else
20768 fe = NULL;
20769
20770 if (fe == NULL)
b98664d3 20771 complaint (_("file index out of range"));
8c43009f
PA
20772 else
20773 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20774 }
20775
c906108c
SS
20776 switch (die->tag)
20777 {
20778 case DW_TAG_label:
e142c38c 20779 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20780 if (attr != nullptr)
3e29f34a
MR
20781 {
20782 CORE_ADDR addr;
20783
cd6c91b4 20784 addr = attr->value_as_address ();
3e29f34a 20785 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20786 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20787 }
0f5238ed
TT
20788 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20789 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20790 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20791 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20792 break;
20793 case DW_TAG_subprogram:
20794 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20795 finish_block. */
f1e6e072 20796 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20797 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20798 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20799 || cu->language == language_ada
20800 || cu->language == language_fortran)
c906108c 20801 {
2cfa0c8d 20802 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20803 Ada and Fortran subprograms, whether marked external or
20804 not, are always stored as a global symbol, because we want
20805 to be able to access them globally. For instance, we want
20806 to be able to break on a nested subprogram without having
20807 to specify the context. */
c24bdb02 20808 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20809 }
20810 else
20811 {
e37fd15a 20812 list_to_add = cu->list_in_scope;
c906108c
SS
20813 }
20814 break;
edb3359d
DJ
20815 case DW_TAG_inlined_subroutine:
20816 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20817 finish_block. */
f1e6e072 20818 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20819 SYMBOL_INLINED (sym) = 1;
481860b3 20820 list_to_add = cu->list_in_scope;
edb3359d 20821 break;
34eaf542
TT
20822 case DW_TAG_template_value_param:
20823 suppress_add = 1;
20824 /* Fall through. */
72929c62 20825 case DW_TAG_constant:
c906108c 20826 case DW_TAG_variable:
254e6b9e 20827 case DW_TAG_member:
0963b4bd
MS
20828 /* Compilation with minimal debug info may result in
20829 variables with missing type entries. Change the
20830 misleading `void' type to something sensible. */
78134374 20831 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20832 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20833
e142c38c 20834 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20835 /* In the case of DW_TAG_member, we should only be called for
20836 static const members. */
20837 if (die->tag == DW_TAG_member)
20838 {
3863f96c
DE
20839 /* dwarf2_add_field uses die_is_declaration,
20840 so we do the same. */
254e6b9e
DE
20841 gdb_assert (die_is_declaration (die, cu));
20842 gdb_assert (attr);
20843 }
435d3d88 20844 if (attr != nullptr)
c906108c 20845 {
e7c27a73 20846 dwarf2_const_value (attr, sym, cu);
e142c38c 20847 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20848 if (!suppress_add)
34eaf542
TT
20849 {
20850 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20851 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20852 else
e37fd15a 20853 list_to_add = cu->list_in_scope;
34eaf542 20854 }
c906108c
SS
20855 break;
20856 }
e142c38c 20857 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20858 if (attr != nullptr)
c906108c 20859 {
e7c27a73 20860 var_decode_location (attr, sym, cu);
e142c38c 20861 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20862
20863 /* Fortran explicitly imports any global symbols to the local
20864 scope by DW_TAG_common_block. */
20865 if (cu->language == language_fortran && die->parent
20866 && die->parent->tag == DW_TAG_common_block)
20867 attr2 = NULL;
20868
caac4577
JG
20869 if (SYMBOL_CLASS (sym) == LOC_STATIC
20870 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20871 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20872 {
20873 /* When a static variable is eliminated by the linker,
20874 the corresponding debug information is not stripped
20875 out, but the variable address is set to null;
20876 do not add such variables into symbol table. */
20877 }
20878 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20879 {
4b610737
TT
20880 if (SYMBOL_CLASS (sym) == LOC_STATIC
20881 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20882 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20883 {
20884 /* A global static variable might be subject to
20885 copy relocation. We first check for a local
20886 minsym, though, because maybe the symbol was
20887 marked hidden, in which case this would not
20888 apply. */
20889 bound_minimal_symbol found
20890 = (lookup_minimal_symbol_linkage
987012b8 20891 (sym->linkage_name (), objfile));
4b610737
TT
20892 if (found.minsym != nullptr)
20893 sym->maybe_copied = 1;
20894 }
f55ee35c 20895
1c809c68
TT
20896 /* A variable with DW_AT_external is never static,
20897 but it may be block-scoped. */
804d2729 20898 list_to_add
c24bdb02
KS
20899 = ((cu->list_in_scope
20900 == cu->get_builder ()->get_file_symbols ())
20901 ? cu->get_builder ()->get_global_symbols ()
804d2729 20902 : cu->list_in_scope);
1c809c68 20903 }
c906108c 20904 else
e37fd15a 20905 list_to_add = cu->list_in_scope;
c906108c
SS
20906 }
20907 else
20908 {
20909 /* We do not know the address of this symbol.
c5aa993b
JM
20910 If it is an external symbol and we have type information
20911 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20912 The address of the variable will then be determined from
20913 the minimal symbol table whenever the variable is
20914 referenced. */
e142c38c 20915 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20916
20917 /* Fortran explicitly imports any global symbols to the local
20918 scope by DW_TAG_common_block. */
20919 if (cu->language == language_fortran && die->parent
20920 && die->parent->tag == DW_TAG_common_block)
20921 {
20922 /* SYMBOL_CLASS doesn't matter here because
20923 read_common_block is going to reset it. */
20924 if (!suppress_add)
20925 list_to_add = cu->list_in_scope;
20926 }
20927 else if (attr2 && (DW_UNSND (attr2) != 0)
20928 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20929 {
0fe7935b
DJ
20930 /* A variable with DW_AT_external is never static, but it
20931 may be block-scoped. */
804d2729 20932 list_to_add
c24bdb02
KS
20933 = ((cu->list_in_scope
20934 == cu->get_builder ()->get_file_symbols ())
20935 ? cu->get_builder ()->get_global_symbols ()
804d2729 20936 : cu->list_in_scope);
0fe7935b 20937
f1e6e072 20938 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20939 }
442ddf59
JK
20940 else if (!die_is_declaration (die, cu))
20941 {
20942 /* Use the default LOC_OPTIMIZED_OUT class. */
20943 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20944 if (!suppress_add)
20945 list_to_add = cu->list_in_scope;
442ddf59 20946 }
c906108c
SS
20947 }
20948 break;
20949 case DW_TAG_formal_parameter:
a60f3166
TT
20950 {
20951 /* If we are inside a function, mark this as an argument. If
20952 not, we might be looking at an argument to an inlined function
20953 when we do not have enough information to show inlined frames;
20954 pretend it's a local variable in that case so that the user can
20955 still see it. */
804d2729 20956 struct context_stack *curr
c24bdb02 20957 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20958 if (curr != nullptr && curr->name != nullptr)
20959 SYMBOL_IS_ARGUMENT (sym) = 1;
20960 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20961 if (attr != nullptr)
a60f3166
TT
20962 {
20963 var_decode_location (attr, sym, cu);
20964 }
20965 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20966 if (attr != nullptr)
a60f3166
TT
20967 {
20968 dwarf2_const_value (attr, sym, cu);
20969 }
f346a30d 20970
a60f3166
TT
20971 list_to_add = cu->list_in_scope;
20972 }
c906108c
SS
20973 break;
20974 case DW_TAG_unspecified_parameters:
20975 /* From varargs functions; gdb doesn't seem to have any
20976 interest in this information, so just ignore it for now.
20977 (FIXME?) */
20978 break;
34eaf542
TT
20979 case DW_TAG_template_type_param:
20980 suppress_add = 1;
20981 /* Fall through. */
c906108c 20982 case DW_TAG_class_type:
680b30c7 20983 case DW_TAG_interface_type:
c906108c
SS
20984 case DW_TAG_structure_type:
20985 case DW_TAG_union_type:
72019c9c 20986 case DW_TAG_set_type:
c906108c 20987 case DW_TAG_enumeration_type:
f1e6e072 20988 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20989 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20990
63d06c5c 20991 {
9c37b5ae 20992 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20993 really ever be static objects: otherwise, if you try
20994 to, say, break of a class's method and you're in a file
20995 which doesn't mention that class, it won't work unless
20996 the check for all static symbols in lookup_symbol_aux
20997 saves you. See the OtherFileClass tests in
20998 gdb.c++/namespace.exp. */
20999
e37fd15a 21000 if (!suppress_add)
34eaf542 21001 {
c24bdb02 21002 buildsym_compunit *builder = cu->get_builder ();
804d2729 21003 list_to_add
c24bdb02 21004 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21005 && cu->language == language_cplus
c24bdb02 21006 ? builder->get_global_symbols ()
804d2729 21007 : cu->list_in_scope);
63d06c5c 21008
64382290 21009 /* The semantics of C++ state that "struct foo {
9c37b5ae 21010 ... }" also defines a typedef for "foo". */
64382290 21011 if (cu->language == language_cplus
45280282 21012 || cu->language == language_ada
c44af4eb
TT
21013 || cu->language == language_d
21014 || cu->language == language_rust)
64382290
TT
21015 {
21016 /* The symbol's name is already allocated along
21017 with this objfile, so we don't need to
21018 duplicate it for the type. */
7d93a1e0 21019 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21020 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21021 }
63d06c5c
DC
21022 }
21023 }
c906108c
SS
21024 break;
21025 case DW_TAG_typedef:
f1e6e072 21026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21027 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21028 list_to_add = cu->list_in_scope;
63d06c5c 21029 break;
c906108c 21030 case DW_TAG_base_type:
a02abb62 21031 case DW_TAG_subrange_type:
f1e6e072 21032 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21033 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21034 list_to_add = cu->list_in_scope;
c906108c
SS
21035 break;
21036 case DW_TAG_enumerator:
e142c38c 21037 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21038 if (attr != nullptr)
c906108c 21039 {
e7c27a73 21040 dwarf2_const_value (attr, sym, cu);
c906108c 21041 }
63d06c5c
DC
21042 {
21043 /* NOTE: carlton/2003-11-10: See comment above in the
21044 DW_TAG_class_type, etc. block. */
21045
804d2729 21046 list_to_add
c24bdb02 21047 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21048 && cu->language == language_cplus
c24bdb02 21049 ? cu->get_builder ()->get_global_symbols ()
804d2729 21050 : cu->list_in_scope);
63d06c5c 21051 }
c906108c 21052 break;
74921315 21053 case DW_TAG_imported_declaration:
5c4e30ca 21054 case DW_TAG_namespace:
f1e6e072 21055 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21056 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21057 break;
530e8392
KB
21058 case DW_TAG_module:
21059 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21060 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21061 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21062 break;
4357ac6c 21063 case DW_TAG_common_block:
f1e6e072 21064 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21065 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21066 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21067 break;
c906108c
SS
21068 default:
21069 /* Not a tag we recognize. Hopefully we aren't processing
21070 trash data, but since we must specifically ignore things
21071 we don't recognize, there is nothing else we should do at
0963b4bd 21072 this point. */
b98664d3 21073 complaint (_("unsupported tag: '%s'"),
4d3c2250 21074 dwarf_tag_name (die->tag));
c906108c
SS
21075 break;
21076 }
df8a16a1 21077
e37fd15a
SW
21078 if (suppress_add)
21079 {
21080 sym->hash_next = objfile->template_symbols;
21081 objfile->template_symbols = sym;
21082 list_to_add = NULL;
21083 }
21084
21085 if (list_to_add != NULL)
d3cb6808 21086 add_symbol_to_list (sym, list_to_add);
e37fd15a 21087
df8a16a1
DJ
21088 /* For the benefit of old versions of GCC, check for anonymous
21089 namespaces based on the demangled name. */
4d4ec4e5 21090 if (!cu->processing_has_namespace_info
94af9270 21091 && cu->language == language_cplus)
c24bdb02 21092 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21093 }
21094 return (sym);
21095}
21096
98bfdba5
PA
21097/* Given an attr with a DW_FORM_dataN value in host byte order,
21098 zero-extend it as appropriate for the symbol's type. The DWARF
21099 standard (v4) is not entirely clear about the meaning of using
21100 DW_FORM_dataN for a constant with a signed type, where the type is
21101 wider than the data. The conclusion of a discussion on the DWARF
21102 list was that this is unspecified. We choose to always zero-extend
21103 because that is the interpretation long in use by GCC. */
c906108c 21104
98bfdba5 21105static gdb_byte *
ff39bb5e 21106dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21107 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21108{
5e22e966 21109 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21110 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21111 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21112 LONGEST l = DW_UNSND (attr);
21113
21114 if (bits < sizeof (*value) * 8)
21115 {
21116 l &= ((LONGEST) 1 << bits) - 1;
21117 *value = l;
21118 }
21119 else if (bits == sizeof (*value) * 8)
21120 *value = l;
21121 else
21122 {
224c3ddb 21123 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21124 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21125 return bytes;
21126 }
21127
21128 return NULL;
21129}
21130
21131/* Read a constant value from an attribute. Either set *VALUE, or if
21132 the value does not fit in *VALUE, set *BYTES - either already
21133 allocated on the objfile obstack, or newly allocated on OBSTACK,
21134 or, set *BATON, if we translated the constant to a location
21135 expression. */
21136
21137static void
ff39bb5e 21138dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21139 const char *name, struct obstack *obstack,
21140 struct dwarf2_cu *cu,
d521ce57 21141 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21142 struct dwarf2_locexpr_baton **baton)
21143{
5e22e966 21144 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21145 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21146 struct comp_unit_head *cu_header = &cu->header;
c906108c 21147 struct dwarf_block *blk;
98bfdba5
PA
21148 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21149 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21150
21151 *value = 0;
21152 *bytes = NULL;
21153 *baton = NULL;
c906108c
SS
21154
21155 switch (attr->form)
21156 {
21157 case DW_FORM_addr:
336d760d 21158 case DW_FORM_addrx:
3019eac3 21159 case DW_FORM_GNU_addr_index:
ac56253d 21160 {
ac56253d
TT
21161 gdb_byte *data;
21162
98bfdba5
PA
21163 if (TYPE_LENGTH (type) != cu_header->addr_size)
21164 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21165 cu_header->addr_size,
98bfdba5 21166 TYPE_LENGTH (type));
ac56253d
TT
21167 /* Symbols of this form are reasonably rare, so we just
21168 piggyback on the existing location code rather than writing
21169 a new implementation of symbol_computed_ops. */
8d749320 21170 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21171 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21172 (*baton)->per_cu = cu->per_cu;
21173 gdb_assert ((*baton)->per_cu);
ac56253d 21174
98bfdba5 21175 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21176 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21177 (*baton)->data = data;
ac56253d
TT
21178
21179 data[0] = DW_OP_addr;
21180 store_unsigned_integer (&data[1], cu_header->addr_size,
21181 byte_order, DW_ADDR (attr));
21182 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21183 }
c906108c 21184 break;
4ac36638 21185 case DW_FORM_string:
93b5768b 21186 case DW_FORM_strp:
cf532bd1 21187 case DW_FORM_strx:
3019eac3 21188 case DW_FORM_GNU_str_index:
36586728 21189 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21190 /* DW_STRING is already allocated on the objfile obstack, point
21191 directly to it. */
d521ce57 21192 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21193 break;
c906108c
SS
21194 case DW_FORM_block1:
21195 case DW_FORM_block2:
21196 case DW_FORM_block4:
21197 case DW_FORM_block:
2dc7f7b3 21198 case DW_FORM_exprloc:
0224619f 21199 case DW_FORM_data16:
c906108c 21200 blk = DW_BLOCK (attr);
98bfdba5
PA
21201 if (TYPE_LENGTH (type) != blk->size)
21202 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21203 TYPE_LENGTH (type));
21204 *bytes = blk->data;
c906108c 21205 break;
2df3850c
JM
21206
21207 /* The DW_AT_const_value attributes are supposed to carry the
21208 symbol's value "represented as it would be on the target
21209 architecture." By the time we get here, it's already been
21210 converted to host endianness, so we just need to sign- or
21211 zero-extend it as appropriate. */
21212 case DW_FORM_data1:
3aef2284 21213 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21214 break;
c906108c 21215 case DW_FORM_data2:
3aef2284 21216 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21217 break;
c906108c 21218 case DW_FORM_data4:
3aef2284 21219 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21220 break;
c906108c 21221 case DW_FORM_data8:
3aef2284 21222 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21223 break;
21224
c906108c 21225 case DW_FORM_sdata:
663c44ac 21226 case DW_FORM_implicit_const:
98bfdba5 21227 *value = DW_SND (attr);
2df3850c
JM
21228 break;
21229
c906108c 21230 case DW_FORM_udata:
98bfdba5 21231 *value = DW_UNSND (attr);
c906108c 21232 break;
2df3850c 21233
c906108c 21234 default:
b98664d3 21235 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21236 dwarf_form_name (attr->form));
98bfdba5 21237 *value = 0;
c906108c
SS
21238 break;
21239 }
21240}
21241
2df3850c 21242
98bfdba5
PA
21243/* Copy constant value from an attribute to a symbol. */
21244
2df3850c 21245static void
ff39bb5e 21246dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21247 struct dwarf2_cu *cu)
2df3850c 21248{
5e22e966 21249 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21250 LONGEST value;
d521ce57 21251 const gdb_byte *bytes;
98bfdba5 21252 struct dwarf2_locexpr_baton *baton;
2df3850c 21253
98bfdba5 21254 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21255 sym->print_name (),
98bfdba5
PA
21256 &objfile->objfile_obstack, cu,
21257 &value, &bytes, &baton);
2df3850c 21258
98bfdba5
PA
21259 if (baton != NULL)
21260 {
98bfdba5 21261 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21262 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21263 }
21264 else if (bytes != NULL)
21265 {
21266 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21267 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21268 }
21269 else
21270 {
21271 SYMBOL_VALUE (sym) = value;
f1e6e072 21272 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21273 }
2df3850c
JM
21274}
21275
c906108c
SS
21276/* Return the type of the die in question using its DW_AT_type attribute. */
21277
21278static struct type *
e7c27a73 21279die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21280{
c906108c 21281 struct attribute *type_attr;
c906108c 21282
e142c38c 21283 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21284 if (!type_attr)
21285 {
5e22e966 21286 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21287 /* A missing DW_AT_type represents a void type. */
518817b3 21288 return objfile_type (objfile)->builtin_void;
c906108c 21289 }
348e048f 21290
673bfd45 21291 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21292}
21293
b4ba55a1
JB
21294/* True iff CU's producer generates GNAT Ada auxiliary information
21295 that allows to find parallel types through that information instead
21296 of having to do expensive parallel lookups by type name. */
21297
21298static int
21299need_gnat_info (struct dwarf2_cu *cu)
21300{
de4cb04a
JB
21301 /* Assume that the Ada compiler was GNAT, which always produces
21302 the auxiliary information. */
21303 return (cu->language == language_ada);
b4ba55a1
JB
21304}
21305
b4ba55a1
JB
21306/* Return the auxiliary type of the die in question using its
21307 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21308 attribute is not present. */
21309
21310static struct type *
21311die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21312{
b4ba55a1 21313 struct attribute *type_attr;
b4ba55a1
JB
21314
21315 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21316 if (!type_attr)
21317 return NULL;
21318
673bfd45 21319 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21320}
21321
21322/* If DIE has a descriptive_type attribute, then set the TYPE's
21323 descriptive type accordingly. */
21324
21325static void
21326set_descriptive_type (struct type *type, struct die_info *die,
21327 struct dwarf2_cu *cu)
21328{
21329 struct type *descriptive_type = die_descriptive_type (die, cu);
21330
21331 if (descriptive_type)
21332 {
21333 ALLOCATE_GNAT_AUX_TYPE (type);
21334 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21335 }
21336}
21337
c906108c
SS
21338/* Return the containing type of the die in question using its
21339 DW_AT_containing_type attribute. */
21340
21341static struct type *
e7c27a73 21342die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21343{
c906108c 21344 struct attribute *type_attr;
5e22e966 21345 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21346
e142c38c 21347 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21348 if (!type_attr)
21349 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21350 "[in module %s]"), objfile_name (objfile));
33ac96f0 21351
673bfd45 21352 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21353}
21354
ac9ec31b
DE
21355/* Return an error marker type to use for the ill formed type in DIE/CU. */
21356
21357static struct type *
21358build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21359{
5e22e966 21360 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21361 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21362 char *saved;
ac9ec31b 21363
528e1572
SM
21364 std::string message
21365 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21366 objfile_name (objfile),
21367 sect_offset_str (cu->header.sect_off),
21368 sect_offset_str (die->sect_off));
efba19b0 21369 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21370
19f392bc 21371 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21372}
21373
673bfd45 21374/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21375 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21376 DW_AT_containing_type.
673bfd45
DE
21377 If there is no type substitute an error marker. */
21378
c906108c 21379static struct type *
ff39bb5e 21380lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21381 struct dwarf2_cu *cu)
c906108c 21382{
5e22e966 21383 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21384 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21385 struct type *this_type;
21386
ac9ec31b
DE
21387 gdb_assert (attr->name == DW_AT_type
21388 || attr->name == DW_AT_GNAT_descriptive_type
21389 || attr->name == DW_AT_containing_type);
21390
673bfd45
DE
21391 /* First see if we have it cached. */
21392
36586728
TT
21393 if (attr->form == DW_FORM_GNU_ref_alt)
21394 {
21395 struct dwarf2_per_cu_data *per_cu;
0826b30a 21396 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21397
ed2dc618
SM
21398 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21399 dwarf2_per_objfile);
aa66c379 21400 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21401 }
cd6c91b4 21402 else if (attr->form_is_ref ())
673bfd45 21403 {
0826b30a 21404 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21405
aa66c379
SM
21406 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21407 dwarf2_per_objfile);
673bfd45 21408 }
55f1336d 21409 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21410 {
ac9ec31b 21411 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21412
ac9ec31b 21413 return get_signatured_type (die, signature, cu);
673bfd45
DE
21414 }
21415 else
21416 {
b98664d3 21417 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21418 " at %s [in module %s]"),
21419 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21420 objfile_name (objfile));
ac9ec31b 21421 return build_error_marker_type (cu, die);
673bfd45
DE
21422 }
21423
21424 /* If not cached we need to read it in. */
21425
21426 if (this_type == NULL)
21427 {
ac9ec31b 21428 struct die_info *type_die = NULL;
673bfd45
DE
21429 struct dwarf2_cu *type_cu = cu;
21430
cd6c91b4 21431 if (attr->form_is_ref ())
ac9ec31b
DE
21432 type_die = follow_die_ref (die, attr, &type_cu);
21433 if (type_die == NULL)
21434 return build_error_marker_type (cu, die);
21435 /* If we find the type now, it's probably because the type came
3019eac3
DE
21436 from an inter-CU reference and the type's CU got expanded before
21437 ours. */
ac9ec31b 21438 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21439 }
21440
21441 /* If we still don't have a type use an error marker. */
21442
21443 if (this_type == NULL)
ac9ec31b 21444 return build_error_marker_type (cu, die);
673bfd45 21445
f792889a 21446 return this_type;
c906108c
SS
21447}
21448
673bfd45
DE
21449/* Return the type in DIE, CU.
21450 Returns NULL for invalid types.
21451
02142a6c 21452 This first does a lookup in die_type_hash,
673bfd45
DE
21453 and only reads the die in if necessary.
21454
21455 NOTE: This can be called when reading in partial or full symbols. */
21456
f792889a 21457static struct type *
e7c27a73 21458read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21459{
f792889a
DJ
21460 struct type *this_type;
21461
21462 this_type = get_die_type (die, cu);
21463 if (this_type)
21464 return this_type;
21465
673bfd45
DE
21466 return read_type_die_1 (die, cu);
21467}
21468
21469/* Read the type in DIE, CU.
21470 Returns NULL for invalid types. */
21471
21472static struct type *
21473read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21474{
21475 struct type *this_type = NULL;
21476
c906108c
SS
21477 switch (die->tag)
21478 {
21479 case DW_TAG_class_type:
680b30c7 21480 case DW_TAG_interface_type:
c906108c
SS
21481 case DW_TAG_structure_type:
21482 case DW_TAG_union_type:
f792889a 21483 this_type = read_structure_type (die, cu);
c906108c
SS
21484 break;
21485 case DW_TAG_enumeration_type:
f792889a 21486 this_type = read_enumeration_type (die, cu);
c906108c
SS
21487 break;
21488 case DW_TAG_subprogram:
21489 case DW_TAG_subroutine_type:
edb3359d 21490 case DW_TAG_inlined_subroutine:
f792889a 21491 this_type = read_subroutine_type (die, cu);
c906108c
SS
21492 break;
21493 case DW_TAG_array_type:
f792889a 21494 this_type = read_array_type (die, cu);
c906108c 21495 break;
72019c9c 21496 case DW_TAG_set_type:
f792889a 21497 this_type = read_set_type (die, cu);
72019c9c 21498 break;
c906108c 21499 case DW_TAG_pointer_type:
f792889a 21500 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21501 break;
21502 case DW_TAG_ptr_to_member_type:
f792889a 21503 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21504 break;
21505 case DW_TAG_reference_type:
4297a3f0
AV
21506 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21507 break;
21508 case DW_TAG_rvalue_reference_type:
21509 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21510 break;
21511 case DW_TAG_const_type:
f792889a 21512 this_type = read_tag_const_type (die, cu);
c906108c
SS
21513 break;
21514 case DW_TAG_volatile_type:
f792889a 21515 this_type = read_tag_volatile_type (die, cu);
c906108c 21516 break;
06d66ee9
TT
21517 case DW_TAG_restrict_type:
21518 this_type = read_tag_restrict_type (die, cu);
21519 break;
c906108c 21520 case DW_TAG_string_type:
f792889a 21521 this_type = read_tag_string_type (die, cu);
c906108c
SS
21522 break;
21523 case DW_TAG_typedef:
f792889a 21524 this_type = read_typedef (die, cu);
c906108c 21525 break;
a02abb62 21526 case DW_TAG_subrange_type:
f792889a 21527 this_type = read_subrange_type (die, cu);
a02abb62 21528 break;
c906108c 21529 case DW_TAG_base_type:
f792889a 21530 this_type = read_base_type (die, cu);
c906108c 21531 break;
81a17f79 21532 case DW_TAG_unspecified_type:
f792889a 21533 this_type = read_unspecified_type (die, cu);
81a17f79 21534 break;
0114d602
DJ
21535 case DW_TAG_namespace:
21536 this_type = read_namespace_type (die, cu);
21537 break;
f55ee35c
JK
21538 case DW_TAG_module:
21539 this_type = read_module_type (die, cu);
21540 break;
a2c2acaf
MW
21541 case DW_TAG_atomic_type:
21542 this_type = read_tag_atomic_type (die, cu);
21543 break;
c906108c 21544 default:
b98664d3 21545 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21546 dwarf_tag_name (die->tag));
c906108c
SS
21547 break;
21548 }
63d06c5c 21549
f792889a 21550 return this_type;
63d06c5c
DC
21551}
21552
abc72ce4
DE
21553/* See if we can figure out if the class lives in a namespace. We do
21554 this by looking for a member function; its demangled name will
21555 contain namespace info, if there is any.
21556 Return the computed name or NULL.
21557 Space for the result is allocated on the objfile's obstack.
21558 This is the full-die version of guess_partial_die_structure_name.
21559 In this case we know DIE has no useful parent. */
21560
43816ebc 21561static const char *
abc72ce4
DE
21562guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21563{
21564 struct die_info *spec_die;
21565 struct dwarf2_cu *spec_cu;
21566 struct die_info *child;
5e22e966 21567 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21568
21569 spec_cu = cu;
21570 spec_die = die_specification (die, &spec_cu);
21571 if (spec_die != NULL)
21572 {
21573 die = spec_die;
21574 cu = spec_cu;
21575 }
21576
21577 for (child = die->child;
21578 child != NULL;
21579 child = child->sibling)
21580 {
21581 if (child->tag == DW_TAG_subprogram)
21582 {
73b9be8b 21583 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21584
7d45c7c3 21585 if (linkage_name != NULL)
abc72ce4 21586 {
43816ebc
TT
21587 gdb::unique_xmalloc_ptr<char> actual_name
21588 (language_class_name_from_physname (cu->language_defn,
21589 linkage_name));
21590 const char *name = NULL;
abc72ce4
DE
21591
21592 if (actual_name != NULL)
21593 {
15d034d0 21594 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21595
21596 if (die_name != NULL
43816ebc 21597 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21598 {
21599 /* Strip off the class name from the full name.
21600 We want the prefix. */
21601 int die_name_len = strlen (die_name);
43816ebc
TT
21602 int actual_name_len = strlen (actual_name.get ());
21603 const char *ptr = actual_name.get ();
abc72ce4
DE
21604
21605 /* Test for '::' as a sanity check. */
21606 if (actual_name_len > die_name_len + 2
43816ebc 21607 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21608 name = obstack_strndup (
e3b94546 21609 &objfile->per_bfd->storage_obstack,
43816ebc 21610 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21611 }
21612 }
abc72ce4
DE
21613 return name;
21614 }
21615 }
21616 }
21617
21618 return NULL;
21619}
21620
96408a79
SA
21621/* GCC might emit a nameless typedef that has a linkage name. Determine the
21622 prefix part in such case. See
21623 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21624
a121b7c1 21625static const char *
96408a79
SA
21626anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21627{
21628 struct attribute *attr;
e6a959d6 21629 const char *base;
96408a79
SA
21630
21631 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21632 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21633 return NULL;
21634
7d45c7c3 21635 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21636 return NULL;
21637
73b9be8b 21638 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21639 if (attr == NULL || DW_STRING (attr) == NULL)
21640 return NULL;
21641
21642 /* dwarf2_name had to be already called. */
21643 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21644
21645 /* Strip the base name, keep any leading namespaces/classes. */
21646 base = strrchr (DW_STRING (attr), ':');
21647 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21648 return "";
21649
5e22e966 21650 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21651 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21652 DW_STRING (attr),
21653 &base[-1] - DW_STRING (attr));
96408a79
SA
21654}
21655
fdde2d81 21656/* Return the name of the namespace/class that DIE is defined within,
0114d602 21657 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21658
0114d602
DJ
21659 For example, if we're within the method foo() in the following
21660 code:
21661
21662 namespace N {
21663 class C {
21664 void foo () {
21665 }
21666 };
21667 }
21668
21669 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21670
0d5cff50 21671static const char *
e142c38c 21672determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21673{
5e22e966 21674 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21675 struct die_info *parent, *spec_die;
21676 struct dwarf2_cu *spec_cu;
21677 struct type *parent_type;
a121b7c1 21678 const char *retval;
63d06c5c 21679
9c37b5ae 21680 if (cu->language != language_cplus
c44af4eb
TT
21681 && cu->language != language_fortran && cu->language != language_d
21682 && cu->language != language_rust)
0114d602
DJ
21683 return "";
21684
96408a79
SA
21685 retval = anonymous_struct_prefix (die, cu);
21686 if (retval)
21687 return retval;
21688
0114d602
DJ
21689 /* We have to be careful in the presence of DW_AT_specification.
21690 For example, with GCC 3.4, given the code
21691
21692 namespace N {
21693 void foo() {
21694 // Definition of N::foo.
21695 }
21696 }
21697
21698 then we'll have a tree of DIEs like this:
21699
21700 1: DW_TAG_compile_unit
21701 2: DW_TAG_namespace // N
21702 3: DW_TAG_subprogram // declaration of N::foo
21703 4: DW_TAG_subprogram // definition of N::foo
21704 DW_AT_specification // refers to die #3
21705
21706 Thus, when processing die #4, we have to pretend that we're in
21707 the context of its DW_AT_specification, namely the contex of die
21708 #3. */
21709 spec_cu = cu;
21710 spec_die = die_specification (die, &spec_cu);
21711 if (spec_die == NULL)
21712 parent = die->parent;
21713 else
63d06c5c 21714 {
0114d602
DJ
21715 parent = spec_die->parent;
21716 cu = spec_cu;
63d06c5c 21717 }
0114d602
DJ
21718
21719 if (parent == NULL)
21720 return "";
98bfdba5
PA
21721 else if (parent->building_fullname)
21722 {
21723 const char *name;
21724 const char *parent_name;
21725
21726 /* It has been seen on RealView 2.2 built binaries,
21727 DW_TAG_template_type_param types actually _defined_ as
21728 children of the parent class:
21729
21730 enum E {};
21731 template class <class Enum> Class{};
21732 Class<enum E> class_e;
21733
21734 1: DW_TAG_class_type (Class)
21735 2: DW_TAG_enumeration_type (E)
21736 3: DW_TAG_enumerator (enum1:0)
21737 3: DW_TAG_enumerator (enum2:1)
21738 ...
21739 2: DW_TAG_template_type_param
21740 DW_AT_type DW_FORM_ref_udata (E)
21741
21742 Besides being broken debug info, it can put GDB into an
21743 infinite loop. Consider:
21744
21745 When we're building the full name for Class<E>, we'll start
21746 at Class, and go look over its template type parameters,
21747 finding E. We'll then try to build the full name of E, and
21748 reach here. We're now trying to build the full name of E,
21749 and look over the parent DIE for containing scope. In the
21750 broken case, if we followed the parent DIE of E, we'd again
21751 find Class, and once again go look at its template type
21752 arguments, etc., etc. Simply don't consider such parent die
21753 as source-level parent of this die (it can't be, the language
21754 doesn't allow it), and break the loop here. */
21755 name = dwarf2_name (die, cu);
21756 parent_name = dwarf2_name (parent, cu);
b98664d3 21757 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21758 name ? name : "<unknown>",
21759 parent_name ? parent_name : "<unknown>");
21760 return "";
21761 }
63d06c5c 21762 else
0114d602
DJ
21763 switch (parent->tag)
21764 {
63d06c5c 21765 case DW_TAG_namespace:
0114d602 21766 parent_type = read_type_die (parent, cu);
acebe513
UW
21767 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21768 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21769 Work around this problem here. */
21770 if (cu->language == language_cplus
7d93a1e0 21771 && strcmp (parent_type->name (), "::") == 0)
acebe513 21772 return "";
0114d602 21773 /* We give a name to even anonymous namespaces. */
7d93a1e0 21774 return parent_type->name ();
63d06c5c 21775 case DW_TAG_class_type:
680b30c7 21776 case DW_TAG_interface_type:
63d06c5c 21777 case DW_TAG_structure_type:
0114d602 21778 case DW_TAG_union_type:
f55ee35c 21779 case DW_TAG_module:
0114d602 21780 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21781 if (parent_type->name () != NULL)
21782 return parent_type->name ();
0114d602
DJ
21783 else
21784 /* An anonymous structure is only allowed non-static data
21785 members; no typedefs, no member functions, et cetera.
21786 So it does not need a prefix. */
21787 return "";
abc72ce4 21788 case DW_TAG_compile_unit:
95554aad 21789 case DW_TAG_partial_unit:
abc72ce4
DE
21790 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21791 if (cu->language == language_cplus
5989a64e 21792 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21793 && die->child != NULL
21794 && (die->tag == DW_TAG_class_type
21795 || die->tag == DW_TAG_structure_type
21796 || die->tag == DW_TAG_union_type))
21797 {
43816ebc 21798 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21799 if (name != NULL)
21800 return name;
21801 }
21802 return "";
0a4b0913
AB
21803 case DW_TAG_subprogram:
21804 /* Nested subroutines in Fortran get a prefix with the name
21805 of the parent's subroutine. */
21806 if (cu->language == language_fortran)
21807 {
21808 if ((die->tag == DW_TAG_subprogram)
21809 && (dwarf2_name (parent, cu) != NULL))
21810 return dwarf2_name (parent, cu);
21811 }
21812 return determine_prefix (parent, cu);
3d567982
TT
21813 case DW_TAG_enumeration_type:
21814 parent_type = read_type_die (parent, cu);
21815 if (TYPE_DECLARED_CLASS (parent_type))
21816 {
7d93a1e0
SM
21817 if (parent_type->name () != NULL)
21818 return parent_type->name ();
3d567982
TT
21819 return "";
21820 }
21821 /* Fall through. */
63d06c5c 21822 default:
8176b9b8 21823 return determine_prefix (parent, cu);
63d06c5c 21824 }
63d06c5c
DC
21825}
21826
3e43a32a
MS
21827/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21828 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21829 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21830 an obconcat, otherwise allocate storage for the result. The CU argument is
21831 used to determine the language and hence, the appropriate separator. */
987504bb 21832
f55ee35c 21833#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21834
21835static char *
f55ee35c
JK
21836typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21837 int physname, struct dwarf2_cu *cu)
63d06c5c 21838{
f55ee35c 21839 const char *lead = "";
5c315b68 21840 const char *sep;
63d06c5c 21841
3e43a32a
MS
21842 if (suffix == NULL || suffix[0] == '\0'
21843 || prefix == NULL || prefix[0] == '\0')
987504bb 21844 sep = "";
45280282
IB
21845 else if (cu->language == language_d)
21846 {
21847 /* For D, the 'main' function could be defined in any module, but it
21848 should never be prefixed. */
21849 if (strcmp (suffix, "D main") == 0)
21850 {
21851 prefix = "";
21852 sep = "";
21853 }
21854 else
21855 sep = ".";
21856 }
f55ee35c
JK
21857 else if (cu->language == language_fortran && physname)
21858 {
21859 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21860 DW_AT_MIPS_linkage_name is preferred and used instead. */
21861
21862 lead = "__";
21863 sep = "_MOD_";
21864 }
987504bb
JJ
21865 else
21866 sep = "::";
63d06c5c 21867
6dd47d34
DE
21868 if (prefix == NULL)
21869 prefix = "";
21870 if (suffix == NULL)
21871 suffix = "";
21872
987504bb
JJ
21873 if (obs == NULL)
21874 {
3e43a32a 21875 char *retval
224c3ddb
SM
21876 = ((char *)
21877 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21878
f55ee35c
JK
21879 strcpy (retval, lead);
21880 strcat (retval, prefix);
6dd47d34
DE
21881 strcat (retval, sep);
21882 strcat (retval, suffix);
63d06c5c
DC
21883 return retval;
21884 }
987504bb
JJ
21885 else
21886 {
21887 /* We have an obstack. */
f55ee35c 21888 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21889 }
63d06c5c
DC
21890}
21891
71c25dea
TT
21892/* Get name of a die, return NULL if not found. */
21893
15d034d0
TT
21894static const char *
21895dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21896 struct objfile *objfile)
71c25dea
TT
21897{
21898 if (name && cu->language == language_cplus)
21899 {
596dc4ad
TT
21900 gdb::unique_xmalloc_ptr<char> canon_name
21901 = cp_canonicalize_string (name);
71c25dea 21902
596dc4ad
TT
21903 if (canon_name != nullptr)
21904 name = objfile->intern (canon_name.get ());
71c25dea
TT
21905 }
21906
21907 return name;
c906108c
SS
21908}
21909
96553a0c
DE
21910/* Get name of a die, return NULL if not found.
21911 Anonymous namespaces are converted to their magic string. */
9219021c 21912
15d034d0 21913static const char *
e142c38c 21914dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21915{
21916 struct attribute *attr;
5e22e966 21917 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21918
e142c38c 21919 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21920 if ((!attr || !DW_STRING (attr))
96553a0c 21921 && die->tag != DW_TAG_namespace
53832f31
TT
21922 && die->tag != DW_TAG_class_type
21923 && die->tag != DW_TAG_interface_type
21924 && die->tag != DW_TAG_structure_type
21925 && die->tag != DW_TAG_union_type)
71c25dea
TT
21926 return NULL;
21927
21928 switch (die->tag)
21929 {
21930 case DW_TAG_compile_unit:
95554aad 21931 case DW_TAG_partial_unit:
71c25dea
TT
21932 /* Compilation units have a DW_AT_name that is a filename, not
21933 a source language identifier. */
21934 case DW_TAG_enumeration_type:
21935 case DW_TAG_enumerator:
21936 /* These tags always have simple identifiers already; no need
21937 to canonicalize them. */
21938 return DW_STRING (attr);
907af001 21939
96553a0c
DE
21940 case DW_TAG_namespace:
21941 if (attr != NULL && DW_STRING (attr) != NULL)
21942 return DW_STRING (attr);
21943 return CP_ANONYMOUS_NAMESPACE_STR;
21944
907af001
UW
21945 case DW_TAG_class_type:
21946 case DW_TAG_interface_type:
21947 case DW_TAG_structure_type:
21948 case DW_TAG_union_type:
21949 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21950 structures or unions. These were of the form "._%d" in GCC 4.1,
21951 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21952 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21953 if (attr && DW_STRING (attr)
61012eef
GB
21954 && (startswith (DW_STRING (attr), "._")
21955 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21956 return NULL;
53832f31
TT
21957
21958 /* GCC might emit a nameless typedef that has a linkage name. See
21959 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21960 if (!attr || DW_STRING (attr) == NULL)
21961 {
73b9be8b 21962 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21963 if (attr == NULL || DW_STRING (attr) == NULL)
21964 return NULL;
21965
df5c6c50
JK
21966 /* Avoid demangling DW_STRING (attr) the second time on a second
21967 call for the same DIE. */
21968 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21969 {
43816ebc
TT
21970 gdb::unique_xmalloc_ptr<char> demangled
21971 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21972 if (demangled == nullptr)
21973 return nullptr;
43816ebc 21974
be1e3d3e 21975 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21976 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21977 }
67430cd0
TT
21978
21979 /* Strip any leading namespaces/classes, keep only the base name.
21980 DW_AT_name for named DIEs does not contain the prefixes. */
21981 const char *base = strrchr (DW_STRING (attr), ':');
21982 if (base && base > DW_STRING (attr) && base[-1] == ':')
21983 return &base[1];
21984 else
21985 return DW_STRING (attr);
53832f31 21986 }
907af001
UW
21987 break;
21988
71c25dea 21989 default:
907af001
UW
21990 break;
21991 }
21992
21993 if (!DW_STRING_IS_CANONICAL (attr))
21994 {
be1e3d3e
TT
21995 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21996 objfile);
907af001 21997 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21998 }
907af001 21999 return DW_STRING (attr);
9219021c
DC
22000}
22001
22002/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22003 is none. *EXT_CU is the CU containing DIE on input, and the CU
22004 containing the return value on output. */
9219021c
DC
22005
22006static struct die_info *
f2f0e013 22007dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22008{
22009 struct attribute *attr;
9219021c 22010
f2f0e013 22011 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22012 if (attr == NULL)
22013 return NULL;
22014
f2f0e013 22015 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22016}
22017
f9aca02d 22018static void
d97bc12b 22019dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22020{
22021 unsigned int i;
22022
d97bc12b 22023 print_spaces (indent, f);
9d8780f0 22024 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22025 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22026 sect_offset_str (die->sect_off));
d97bc12b
DE
22027
22028 if (die->parent != NULL)
22029 {
22030 print_spaces (indent, f);
9d8780f0
SM
22031 fprintf_unfiltered (f, " parent at offset: %s\n",
22032 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22033 }
22034
22035 print_spaces (indent, f);
22036 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22037 dwarf_bool_name (die->child != NULL));
c906108c 22038
d97bc12b
DE
22039 print_spaces (indent, f);
22040 fprintf_unfiltered (f, " attributes:\n");
22041
c906108c
SS
22042 for (i = 0; i < die->num_attrs; ++i)
22043 {
d97bc12b
DE
22044 print_spaces (indent, f);
22045 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22046 dwarf_attr_name (die->attrs[i].name),
22047 dwarf_form_name (die->attrs[i].form));
d97bc12b 22048
c906108c
SS
22049 switch (die->attrs[i].form)
22050 {
c906108c 22051 case DW_FORM_addr:
336d760d 22052 case DW_FORM_addrx:
3019eac3 22053 case DW_FORM_GNU_addr_index:
d97bc12b 22054 fprintf_unfiltered (f, "address: ");
5af949e3 22055 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22056 break;
22057 case DW_FORM_block2:
22058 case DW_FORM_block4:
22059 case DW_FORM_block:
22060 case DW_FORM_block1:
56eb65bd
SP
22061 fprintf_unfiltered (f, "block: size %s",
22062 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22063 break;
2dc7f7b3 22064 case DW_FORM_exprloc:
56eb65bd
SP
22065 fprintf_unfiltered (f, "expression: size %s",
22066 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22067 break;
0224619f
JK
22068 case DW_FORM_data16:
22069 fprintf_unfiltered (f, "constant of 16 bytes");
22070 break;
4568ecf9
DE
22071 case DW_FORM_ref_addr:
22072 fprintf_unfiltered (f, "ref address: ");
22073 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22074 break;
36586728
TT
22075 case DW_FORM_GNU_ref_alt:
22076 fprintf_unfiltered (f, "alt ref address: ");
22077 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22078 break;
10b3939b
DJ
22079 case DW_FORM_ref1:
22080 case DW_FORM_ref2:
22081 case DW_FORM_ref4:
4568ecf9
DE
22082 case DW_FORM_ref8:
22083 case DW_FORM_ref_udata:
d97bc12b 22084 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22085 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22086 break;
c906108c
SS
22087 case DW_FORM_data1:
22088 case DW_FORM_data2:
22089 case DW_FORM_data4:
ce5d95e1 22090 case DW_FORM_data8:
c906108c
SS
22091 case DW_FORM_udata:
22092 case DW_FORM_sdata:
43bbcdc2
PH
22093 fprintf_unfiltered (f, "constant: %s",
22094 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22095 break;
2dc7f7b3
TT
22096 case DW_FORM_sec_offset:
22097 fprintf_unfiltered (f, "section offset: %s",
22098 pulongest (DW_UNSND (&die->attrs[i])));
22099 break;
55f1336d 22100 case DW_FORM_ref_sig8:
ac9ec31b
DE
22101 fprintf_unfiltered (f, "signature: %s",
22102 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22103 break;
c906108c 22104 case DW_FORM_string:
4bdf3d34 22105 case DW_FORM_strp:
43988095 22106 case DW_FORM_line_strp:
cf532bd1 22107 case DW_FORM_strx:
3019eac3 22108 case DW_FORM_GNU_str_index:
36586728 22109 case DW_FORM_GNU_strp_alt:
8285870a 22110 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22111 DW_STRING (&die->attrs[i])
8285870a
JK
22112 ? DW_STRING (&die->attrs[i]) : "",
22113 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22114 break;
22115 case DW_FORM_flag:
22116 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22117 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22118 else
d97bc12b 22119 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22120 break;
2dc7f7b3
TT
22121 case DW_FORM_flag_present:
22122 fprintf_unfiltered (f, "flag: TRUE");
22123 break;
a8329558 22124 case DW_FORM_indirect:
0963b4bd
MS
22125 /* The reader will have reduced the indirect form to
22126 the "base form" so this form should not occur. */
5f48f8f3 22127 fprintf_unfiltered (f,
3e43a32a 22128 "unexpected attribute form: DW_FORM_indirect");
a8329558 22129 break;
663c44ac
JK
22130 case DW_FORM_implicit_const:
22131 fprintf_unfiltered (f, "constant: %s",
22132 plongest (DW_SND (&die->attrs[i])));
22133 break;
c906108c 22134 default:
d97bc12b 22135 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22136 die->attrs[i].form);
d97bc12b 22137 break;
c906108c 22138 }
d97bc12b 22139 fprintf_unfiltered (f, "\n");
c906108c
SS
22140 }
22141}
22142
f9aca02d 22143static void
d97bc12b 22144dump_die_for_error (struct die_info *die)
c906108c 22145{
d97bc12b
DE
22146 dump_die_shallow (gdb_stderr, 0, die);
22147}
22148
22149static void
22150dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22151{
22152 int indent = level * 4;
22153
22154 gdb_assert (die != NULL);
22155
22156 if (level >= max_level)
22157 return;
22158
22159 dump_die_shallow (f, indent, die);
22160
22161 if (die->child != NULL)
c906108c 22162 {
d97bc12b
DE
22163 print_spaces (indent, f);
22164 fprintf_unfiltered (f, " Children:");
22165 if (level + 1 < max_level)
22166 {
22167 fprintf_unfiltered (f, "\n");
22168 dump_die_1 (f, level + 1, max_level, die->child);
22169 }
22170 else
22171 {
3e43a32a
MS
22172 fprintf_unfiltered (f,
22173 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22174 }
22175 }
22176
22177 if (die->sibling != NULL && level > 0)
22178 {
22179 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22180 }
22181}
22182
d97bc12b
DE
22183/* This is called from the pdie macro in gdbinit.in.
22184 It's not static so gcc will keep a copy callable from gdb. */
22185
22186void
22187dump_die (struct die_info *die, int max_level)
22188{
22189 dump_die_1 (gdb_stdlog, 0, max_level, die);
22190}
22191
f9aca02d 22192static void
51545339 22193store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22194{
51545339 22195 void **slot;
c906108c 22196
9c541725
PA
22197 slot = htab_find_slot_with_hash (cu->die_hash, die,
22198 to_underlying (die->sect_off),
b64f50a1 22199 INSERT);
51545339
DJ
22200
22201 *slot = die;
c906108c
SS
22202}
22203
348e048f
DE
22204/* Follow reference or signature attribute ATTR of SRC_DIE.
22205 On entry *REF_CU is the CU of SRC_DIE.
22206 On exit *REF_CU is the CU of the result. */
22207
22208static struct die_info *
ff39bb5e 22209follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22210 struct dwarf2_cu **ref_cu)
22211{
22212 struct die_info *die;
22213
cd6c91b4 22214 if (attr->form_is_ref ())
348e048f 22215 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22216 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22217 die = follow_die_sig (src_die, attr, ref_cu);
22218 else
22219 {
22220 dump_die_for_error (src_die);
22221 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22222 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22223 }
22224
22225 return die;
03dd20cc
DJ
22226}
22227
5c631832 22228/* Follow reference OFFSET.
673bfd45
DE
22229 On entry *REF_CU is the CU of the source die referencing OFFSET.
22230 On exit *REF_CU is the CU of the result.
22231 Returns NULL if OFFSET is invalid. */
f504f079 22232
f9aca02d 22233static struct die_info *
9c541725 22234follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22235 struct dwarf2_cu **ref_cu)
c906108c 22236{
10b3939b 22237 struct die_info temp_die;
f2f0e013 22238 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22239 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22240
348e048f
DE
22241 gdb_assert (cu->per_cu != NULL);
22242
98bfdba5
PA
22243 target_cu = cu;
22244
3019eac3 22245 if (cu->per_cu->is_debug_types)
348e048f
DE
22246 {
22247 /* .debug_types CUs cannot reference anything outside their CU.
22248 If they need to, they have to reference a signatured type via
55f1336d 22249 DW_FORM_ref_sig8. */
4057dfde 22250 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22251 return NULL;
348e048f 22252 }
36586728 22253 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22254 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22255 {
22256 struct dwarf2_per_cu_data *per_cu;
9a619af0 22257
9c541725 22258 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22259 dwarf2_per_objfile);
03dd20cc
DJ
22260
22261 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22262 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22263 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22264
10b3939b
DJ
22265 target_cu = per_cu->cu;
22266 }
98bfdba5
PA
22267 else if (cu->dies == NULL)
22268 {
22269 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22270 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22271 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22272 language_minimal);
98bfdba5 22273 }
c906108c 22274
f2f0e013 22275 *ref_cu = target_cu;
9c541725 22276 temp_die.sect_off = sect_off;
c24bdb02
KS
22277
22278 if (target_cu != cu)
22279 target_cu->ancestor = cu;
22280
9a3c8263 22281 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22282 &temp_die,
22283 to_underlying (sect_off));
5c631832 22284}
10b3939b 22285
5c631832
JK
22286/* Follow reference attribute ATTR of SRC_DIE.
22287 On entry *REF_CU is the CU of SRC_DIE.
22288 On exit *REF_CU is the CU of the result. */
22289
22290static struct die_info *
ff39bb5e 22291follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22292 struct dwarf2_cu **ref_cu)
22293{
0826b30a 22294 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22295 struct dwarf2_cu *cu = *ref_cu;
22296 struct die_info *die;
22297
9c541725 22298 die = follow_die_offset (sect_off,
36586728
TT
22299 (attr->form == DW_FORM_GNU_ref_alt
22300 || cu->per_cu->is_dwz),
22301 ref_cu);
5c631832 22302 if (!die)
9d8780f0
SM
22303 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22304 "at %s [in module %s]"),
22305 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22306 objfile_name (cu->per_objfile->objfile));
348e048f 22307
5c631832
JK
22308 return die;
22309}
22310
d4c9a4f8 22311/* See read.h. */
5c631832
JK
22312
22313struct dwarf2_locexpr_baton
9c541725 22314dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22315 dwarf2_per_cu_data *per_cu,
14095eb3 22316 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22317 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22318 void *baton, bool resolve_abstract_p)
5c631832 22319{
918dd910 22320 struct dwarf2_cu *cu;
5c631832
JK
22321 struct die_info *die;
22322 struct attribute *attr;
22323 struct dwarf2_locexpr_baton retval;
12359b5e 22324 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22325
918dd910 22326 if (per_cu->cu == NULL)
ab432490 22327 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22328 cu = per_cu->cu;
cc12ce38
DE
22329 if (cu == NULL)
22330 {
22331 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22332 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22333 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22334 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22335 }
918dd910 22336
9c541725 22337 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22338 if (!die)
9d8780f0
SM
22339 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22340 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22341
22342 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22343 if (!attr && resolve_abstract_p
5989a64e
SM
22344 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22345 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22346 {
22347 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22348 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22349 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22350
3360b6e7 22351 for (const auto &cand_off
5989a64e 22352 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22353 {
3360b6e7
TV
22354 struct dwarf2_cu *cand_cu = cu;
22355 struct die_info *cand
22356 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22357 if (!cand
22358 || !cand->parent
e4a62c65
TV
22359 || cand->parent->tag != DW_TAG_subprogram)
22360 continue;
22361
22362 CORE_ADDR pc_low, pc_high;
22363 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22364 if (pc_low == ((CORE_ADDR) -1))
22365 continue;
22366 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22367 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22368 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22369 continue;
22370
22371 die = cand;
22372 attr = dwarf2_attr (die, DW_AT_location, cu);
22373 break;
22374 }
22375 }
22376
5c631832
JK
22377 if (!attr)
22378 {
e103e986
JK
22379 /* DWARF: "If there is no such attribute, then there is no effect.".
22380 DATA is ignored if SIZE is 0. */
5c631832 22381
e103e986 22382 retval.data = NULL;
5c631832
JK
22383 retval.size = 0;
22384 }
cd6c91b4 22385 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22386 {
22387 struct dwarf2_loclist_baton loclist_baton;
22388 CORE_ADDR pc = (*get_frame_pc) (baton);
22389 size_t size;
22390
22391 fill_in_loclist_baton (cu, &loclist_baton, attr);
22392
22393 retval.data = dwarf2_find_location_expression (&loclist_baton,
22394 &size, pc);
22395 retval.size = size;
22396 }
5c631832
JK
22397 else
22398 {
4fc6c0d5 22399 if (!attr->form_is_block ())
9d8780f0 22400 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22401 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22402 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22403
22404 retval.data = DW_BLOCK (attr)->data;
22405 retval.size = DW_BLOCK (attr)->size;
22406 }
a50264ba 22407 retval.per_objfile = dwarf2_per_objfile;
5c631832 22408 retval.per_cu = cu->per_cu;
918dd910 22409
ed2dc618 22410 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22411
5c631832 22412 return retval;
348e048f
DE
22413}
22414
d4c9a4f8 22415/* See read.h. */
8b9737bf
TT
22416
22417struct dwarf2_locexpr_baton
22418dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22419 dwarf2_per_cu_data *per_cu,
14095eb3 22420 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22421 CORE_ADDR (*get_frame_pc) (void *baton),
22422 void *baton)
22423{
9c541725 22424 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22425
14095eb3
SM
22426 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22427 get_frame_pc, baton);
8b9737bf
TT
22428}
22429
b6807d98
TT
22430/* Write a constant of a given type as target-ordered bytes into
22431 OBSTACK. */
22432
22433static const gdb_byte *
22434write_constant_as_bytes (struct obstack *obstack,
22435 enum bfd_endian byte_order,
22436 struct type *type,
22437 ULONGEST value,
22438 LONGEST *len)
22439{
22440 gdb_byte *result;
22441
22442 *len = TYPE_LENGTH (type);
224c3ddb 22443 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22444 store_unsigned_integer (result, *len, byte_order, value);
22445
22446 return result;
22447}
22448
d4c9a4f8 22449/* See read.h. */
b6807d98
TT
22450
22451const gdb_byte *
9c541725 22452dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22453 dwarf2_per_cu_data *per_cu,
14095eb3 22454 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22455 obstack *obstack,
b6807d98
TT
22456 LONGEST *len)
22457{
22458 struct dwarf2_cu *cu;
22459 struct die_info *die;
22460 struct attribute *attr;
22461 const gdb_byte *result = NULL;
22462 struct type *type;
22463 LONGEST value;
22464 enum bfd_endian byte_order;
14095eb3 22465 struct objfile *objfile = per_objfile->objfile;
b6807d98 22466
b6807d98 22467 if (per_cu->cu == NULL)
14095eb3 22468 load_cu (per_cu, per_objfile, false);
b6807d98 22469 cu = per_cu->cu;
cc12ce38
DE
22470 if (cu == NULL)
22471 {
22472 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22473 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22474 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22475 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22476 }
b6807d98 22477
9c541725 22478 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22479 if (!die)
9d8780f0
SM
22480 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22481 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22482
22483 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22484 if (attr == NULL)
22485 return NULL;
22486
e3b94546 22487 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22488 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22489
22490 switch (attr->form)
22491 {
22492 case DW_FORM_addr:
336d760d 22493 case DW_FORM_addrx:
b6807d98
TT
22494 case DW_FORM_GNU_addr_index:
22495 {
22496 gdb_byte *tem;
22497
22498 *len = cu->header.addr_size;
224c3ddb 22499 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22500 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22501 result = tem;
22502 }
22503 break;
22504 case DW_FORM_string:
22505 case DW_FORM_strp:
cf532bd1 22506 case DW_FORM_strx:
b6807d98
TT
22507 case DW_FORM_GNU_str_index:
22508 case DW_FORM_GNU_strp_alt:
22509 /* DW_STRING is already allocated on the objfile obstack, point
22510 directly to it. */
22511 result = (const gdb_byte *) DW_STRING (attr);
22512 *len = strlen (DW_STRING (attr));
22513 break;
22514 case DW_FORM_block1:
22515 case DW_FORM_block2:
22516 case DW_FORM_block4:
22517 case DW_FORM_block:
22518 case DW_FORM_exprloc:
0224619f 22519 case DW_FORM_data16:
b6807d98
TT
22520 result = DW_BLOCK (attr)->data;
22521 *len = DW_BLOCK (attr)->size;
22522 break;
22523
22524 /* The DW_AT_const_value attributes are supposed to carry the
22525 symbol's value "represented as it would be on the target
22526 architecture." By the time we get here, it's already been
22527 converted to host endianness, so we just need to sign- or
22528 zero-extend it as appropriate. */
22529 case DW_FORM_data1:
22530 type = die_type (die, cu);
22531 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22532 if (result == NULL)
22533 result = write_constant_as_bytes (obstack, byte_order,
22534 type, value, len);
22535 break;
22536 case DW_FORM_data2:
22537 type = die_type (die, cu);
22538 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22539 if (result == NULL)
22540 result = write_constant_as_bytes (obstack, byte_order,
22541 type, value, len);
22542 break;
22543 case DW_FORM_data4:
22544 type = die_type (die, cu);
22545 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22546 if (result == NULL)
22547 result = write_constant_as_bytes (obstack, byte_order,
22548 type, value, len);
22549 break;
22550 case DW_FORM_data8:
22551 type = die_type (die, cu);
22552 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22553 if (result == NULL)
22554 result = write_constant_as_bytes (obstack, byte_order,
22555 type, value, len);
22556 break;
22557
22558 case DW_FORM_sdata:
663c44ac 22559 case DW_FORM_implicit_const:
b6807d98
TT
22560 type = die_type (die, cu);
22561 result = write_constant_as_bytes (obstack, byte_order,
22562 type, DW_SND (attr), len);
22563 break;
22564
22565 case DW_FORM_udata:
22566 type = die_type (die, cu);
22567 result = write_constant_as_bytes (obstack, byte_order,
22568 type, DW_UNSND (attr), len);
22569 break;
22570
22571 default:
b98664d3 22572 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22573 dwarf_form_name (attr->form));
22574 break;
22575 }
22576
22577 return result;
22578}
22579
d4c9a4f8 22580/* See read.h. */
7942e96e
AA
22581
22582struct type *
9c541725 22583dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22584 dwarf2_per_cu_data *per_cu,
22585 dwarf2_per_objfile *per_objfile)
7942e96e
AA
22586{
22587 struct dwarf2_cu *cu;
22588 struct die_info *die;
22589
7942e96e 22590 if (per_cu->cu == NULL)
14095eb3 22591 load_cu (per_cu, per_objfile, false);
7942e96e
AA
22592 cu = per_cu->cu;
22593 if (!cu)
22594 return NULL;
22595
9c541725 22596 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22597 if (!die)
22598 return NULL;
22599
22600 return die_type (die, cu);
22601}
22602
8cb5117c 22603/* See read.h. */
8a9b8146
TT
22604
22605struct type *
b64f50a1 22606dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22607 dwarf2_per_cu_data *per_cu,
22608 dwarf2_per_objfile *per_objfile)
8a9b8146 22609{
9c541725 22610 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22611 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22612}
22613
ac9ec31b 22614/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22615 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22616 On exit *REF_CU is the CU of the result.
22617 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22618
22619static struct die_info *
ac9ec31b
DE
22620follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22621 struct dwarf2_cu **ref_cu)
348e048f 22622{
348e048f 22623 struct die_info temp_die;
c24bdb02 22624 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22625 struct die_info *die;
120ce1b5
SM
22626 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22627
348e048f 22628
ac9ec31b
DE
22629 /* While it might be nice to assert sig_type->type == NULL here,
22630 we can get here for DW_AT_imported_declaration where we need
22631 the DIE not the type. */
348e048f
DE
22632
22633 /* If necessary, add it to the queue and load its DIEs. */
22634
120ce1b5
SM
22635 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22636 language_minimal))
22637 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22638
348e048f 22639 sig_cu = sig_type->per_cu.cu;
69d751e3 22640 gdb_assert (sig_cu != NULL);
9c541725
PA
22641 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22642 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22643 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22644 to_underlying (temp_die.sect_off));
348e048f
DE
22645 if (die)
22646 {
796a7ff8
DE
22647 /* For .gdb_index version 7 keep track of included TUs.
22648 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22649 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22650 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22651 {
ae640021 22652 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22653 }
22654
348e048f 22655 *ref_cu = sig_cu;
c24bdb02
KS
22656 if (sig_cu != cu)
22657 sig_cu->ancestor = cu;
22658
348e048f
DE
22659 return die;
22660 }
22661
ac9ec31b
DE
22662 return NULL;
22663}
22664
22665/* Follow signatured type referenced by ATTR in SRC_DIE.
22666 On entry *REF_CU is the CU of SRC_DIE.
22667 On exit *REF_CU is the CU of the result.
22668 The result is the DIE of the type.
22669 If the referenced type cannot be found an error is thrown. */
22670
22671static struct die_info *
ff39bb5e 22672follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22673 struct dwarf2_cu **ref_cu)
22674{
22675 ULONGEST signature = DW_SIGNATURE (attr);
22676 struct signatured_type *sig_type;
22677 struct die_info *die;
22678
22679 gdb_assert (attr->form == DW_FORM_ref_sig8);
22680
a2ce51a0 22681 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22682 /* sig_type will be NULL if the signatured type is missing from
22683 the debug info. */
22684 if (sig_type == NULL)
22685 {
22686 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22687 " from DIE at %s [in module %s]"),
22688 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22689 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22690 }
22691
22692 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22693 if (die == NULL)
22694 {
22695 dump_die_for_error (src_die);
22696 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22697 " from DIE at %s [in module %s]"),
22698 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22699 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22700 }
22701
22702 return die;
22703}
22704
22705/* Get the type specified by SIGNATURE referenced in DIE/CU,
22706 reading in and processing the type unit if necessary. */
22707
22708static struct type *
22709get_signatured_type (struct die_info *die, ULONGEST signature,
22710 struct dwarf2_cu *cu)
22711{
5e22e966 22712 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22713 struct signatured_type *sig_type;
22714 struct dwarf2_cu *type_cu;
22715 struct die_info *type_die;
22716 struct type *type;
22717
a2ce51a0 22718 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22719 /* sig_type will be NULL if the signatured type is missing from
22720 the debug info. */
22721 if (sig_type == NULL)
22722 {
b98664d3 22723 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22724 " from DIE at %s [in module %s]"),
22725 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22726 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22727 return build_error_marker_type (cu, die);
22728 }
22729
22730 /* If we already know the type we're done. */
e286671b
TT
22731 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22732 if (type != nullptr)
22733 return type;
ac9ec31b
DE
22734
22735 type_cu = cu;
22736 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22737 if (type_die != NULL)
22738 {
22739 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22740 is created. This is important, for example, because for c++ classes
22741 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22742 type = read_type_die (type_die, type_cu);
22743 if (type == NULL)
22744 {
b98664d3 22745 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22746 " referenced from DIE at %s [in module %s]"),
22747 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22748 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22749 type = build_error_marker_type (cu, die);
22750 }
22751 }
22752 else
22753 {
b98664d3 22754 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22755 " from DIE at %s [in module %s]"),
22756 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22757 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22758 type = build_error_marker_type (cu, die);
22759 }
e286671b
TT
22760
22761 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22762
22763 return type;
22764}
22765
22766/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22767 reading in and processing the type unit if necessary. */
22768
22769static struct type *
ff39bb5e 22770get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22771 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22772{
22773 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22774 if (attr->form_is_ref ())
ac9ec31b
DE
22775 {
22776 struct dwarf2_cu *type_cu = cu;
22777 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22778
22779 return read_type_die (type_die, type_cu);
22780 }
22781 else if (attr->form == DW_FORM_ref_sig8)
22782 {
22783 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22784 }
22785 else
22786 {
5e22e966 22787 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22788
b98664d3 22789 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22790 " at %s [in module %s]"),
22791 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22792 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22793 return build_error_marker_type (cu, die);
22794 }
348e048f
DE
22795}
22796
e5fe5e75 22797/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22798
22799static void
ab432490
SM
22800load_full_type_unit (dwarf2_per_cu_data *per_cu,
22801 dwarf2_per_objfile *per_objfile)
348e048f 22802{
52dc124a 22803 struct signatured_type *sig_type;
348e048f 22804
f4dc4d17 22805 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22806 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22807
6721b2ec
DE
22808 /* We have the per_cu, but we need the signatured_type.
22809 Fortunately this is an easy translation. */
22810 gdb_assert (per_cu->is_debug_types);
22811 sig_type = (struct signatured_type *) per_cu;
348e048f 22812
6721b2ec 22813 gdb_assert (per_cu->cu == NULL);
348e048f 22814
ab432490 22815 read_signatured_type (sig_type, per_objfile);
348e048f 22816
6721b2ec 22817 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22818}
22819
3019eac3
DE
22820/* Read in a signatured type and build its CU and DIEs.
22821 If the type is a stub for the real type in a DWO file,
22822 read in the real type from the DWO file as well. */
dee91e82
DE
22823
22824static void
ab432490
SM
22825read_signatured_type (signatured_type *sig_type,
22826 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22827{
22828 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22829
3019eac3 22830 gdb_assert (per_cu->is_debug_types);
dee91e82 22831 gdb_assert (per_cu->cu == NULL);
348e048f 22832
ab432490 22833 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22834
22835 if (!reader.dummy_p)
22836 {
22837 struct dwarf2_cu *cu = reader.cu;
22838 const gdb_byte *info_ptr = reader.info_ptr;
22839
22840 gdb_assert (cu->die_hash == NULL);
22841 cu->die_hash =
22842 htab_create_alloc_ex (cu->header.length / 12,
22843 die_hash,
22844 die_eq,
22845 NULL,
22846 &cu->comp_unit_obstack,
22847 hashtab_obstack_allocate,
22848 dummy_obstack_deallocate);
22849
3e225074 22850 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22851 reader.comp_unit_die->child
22852 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22853 reader.comp_unit_die);
22854 cu->dies = reader.comp_unit_die;
22855 /* comp_unit_die is not stored in die_hash, no need. */
22856
22857 /* We try not to read any attributes in this function, because
22858 not all CUs needed for references have been loaded yet, and
22859 symbol table processing isn't initialized. But we have to
22860 set the CU language, or we won't be able to build types
22861 correctly. Similarly, if we do not read the producer, we can
22862 not apply producer-specific interpretation. */
22863 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22864
22865 reader.keep ();
c0ab21c2
TT
22866 }
22867
7ee85ab1 22868 sig_type->per_cu.tu_read = 1;
c906108c
SS
22869}
22870
c906108c
SS
22871/* Decode simple location descriptions.
22872 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22873 the location and return the value. If COMPUTED is non-null, it is
22874 set to true to indicate that decoding was successful, and false
22875 otherwise. If COMPUTED is null, then this function may emit a
22876 complaint. */
c906108c
SS
22877
22878static CORE_ADDR
7d79de9a 22879decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22880{
5e22e966 22881 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22882 size_t i;
22883 size_t size = blk->size;
d521ce57 22884 const gdb_byte *data = blk->data;
21ae7a4d
JK
22885 CORE_ADDR stack[64];
22886 int stacki;
22887 unsigned int bytes_read, unsnd;
22888 gdb_byte op;
c906108c 22889
7d79de9a
TT
22890 if (computed != nullptr)
22891 *computed = false;
22892
21ae7a4d
JK
22893 i = 0;
22894 stacki = 0;
22895 stack[stacki] = 0;
22896 stack[++stacki] = 0;
22897
22898 while (i < size)
22899 {
22900 op = data[i++];
22901 switch (op)
22902 {
22903 case DW_OP_lit0:
22904 case DW_OP_lit1:
22905 case DW_OP_lit2:
22906 case DW_OP_lit3:
22907 case DW_OP_lit4:
22908 case DW_OP_lit5:
22909 case DW_OP_lit6:
22910 case DW_OP_lit7:
22911 case DW_OP_lit8:
22912 case DW_OP_lit9:
22913 case DW_OP_lit10:
22914 case DW_OP_lit11:
22915 case DW_OP_lit12:
22916 case DW_OP_lit13:
22917 case DW_OP_lit14:
22918 case DW_OP_lit15:
22919 case DW_OP_lit16:
22920 case DW_OP_lit17:
22921 case DW_OP_lit18:
22922 case DW_OP_lit19:
22923 case DW_OP_lit20:
22924 case DW_OP_lit21:
22925 case DW_OP_lit22:
22926 case DW_OP_lit23:
22927 case DW_OP_lit24:
22928 case DW_OP_lit25:
22929 case DW_OP_lit26:
22930 case DW_OP_lit27:
22931 case DW_OP_lit28:
22932 case DW_OP_lit29:
22933 case DW_OP_lit30:
22934 case DW_OP_lit31:
22935 stack[++stacki] = op - DW_OP_lit0;
22936 break;
f1bea926 22937
21ae7a4d
JK
22938 case DW_OP_reg0:
22939 case DW_OP_reg1:
22940 case DW_OP_reg2:
22941 case DW_OP_reg3:
22942 case DW_OP_reg4:
22943 case DW_OP_reg5:
22944 case DW_OP_reg6:
22945 case DW_OP_reg7:
22946 case DW_OP_reg8:
22947 case DW_OP_reg9:
22948 case DW_OP_reg10:
22949 case DW_OP_reg11:
22950 case DW_OP_reg12:
22951 case DW_OP_reg13:
22952 case DW_OP_reg14:
22953 case DW_OP_reg15:
22954 case DW_OP_reg16:
22955 case DW_OP_reg17:
22956 case DW_OP_reg18:
22957 case DW_OP_reg19:
22958 case DW_OP_reg20:
22959 case DW_OP_reg21:
22960 case DW_OP_reg22:
22961 case DW_OP_reg23:
22962 case DW_OP_reg24:
22963 case DW_OP_reg25:
22964 case DW_OP_reg26:
22965 case DW_OP_reg27:
22966 case DW_OP_reg28:
22967 case DW_OP_reg29:
22968 case DW_OP_reg30:
22969 case DW_OP_reg31:
22970 stack[++stacki] = op - DW_OP_reg0;
22971 if (i < size)
7d79de9a
TT
22972 {
22973 if (computed == nullptr)
22974 dwarf2_complex_location_expr_complaint ();
22975 else
22976 return 0;
22977 }
21ae7a4d 22978 break;
c906108c 22979
21ae7a4d
JK
22980 case DW_OP_regx:
22981 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22982 i += bytes_read;
22983 stack[++stacki] = unsnd;
22984 if (i < size)
7d79de9a
TT
22985 {
22986 if (computed == nullptr)
22987 dwarf2_complex_location_expr_complaint ();
22988 else
22989 return 0;
22990 }
21ae7a4d 22991 break;
c906108c 22992
21ae7a4d 22993 case DW_OP_addr:
c8a7a66f
TT
22994 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22995 &bytes_read);
21ae7a4d
JK
22996 i += bytes_read;
22997 break;
d53d4ac5 22998
21ae7a4d
JK
22999 case DW_OP_const1u:
23000 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23001 i += 1;
23002 break;
23003
23004 case DW_OP_const1s:
23005 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23006 i += 1;
23007 break;
23008
23009 case DW_OP_const2u:
23010 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23011 i += 2;
23012 break;
23013
23014 case DW_OP_const2s:
23015 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23016 i += 2;
23017 break;
d53d4ac5 23018
21ae7a4d
JK
23019 case DW_OP_const4u:
23020 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23021 i += 4;
23022 break;
23023
23024 case DW_OP_const4s:
23025 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23026 i += 4;
23027 break;
23028
585861ea
JK
23029 case DW_OP_const8u:
23030 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23031 i += 8;
23032 break;
23033
21ae7a4d
JK
23034 case DW_OP_constu:
23035 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23036 &bytes_read);
23037 i += bytes_read;
23038 break;
23039
23040 case DW_OP_consts:
23041 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23042 i += bytes_read;
23043 break;
23044
23045 case DW_OP_dup:
23046 stack[stacki + 1] = stack[stacki];
23047 stacki++;
23048 break;
23049
23050 case DW_OP_plus:
23051 stack[stacki - 1] += stack[stacki];
23052 stacki--;
23053 break;
23054
23055 case DW_OP_plus_uconst:
23056 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23057 &bytes_read);
23058 i += bytes_read;
23059 break;
23060
23061 case DW_OP_minus:
23062 stack[stacki - 1] -= stack[stacki];
23063 stacki--;
23064 break;
23065
23066 case DW_OP_deref:
23067 /* If we're not the last op, then we definitely can't encode
23068 this using GDB's address_class enum. This is valid for partial
23069 global symbols, although the variable's address will be bogus
23070 in the psymtab. */
23071 if (i < size)
7d79de9a
TT
23072 {
23073 if (computed == nullptr)
23074 dwarf2_complex_location_expr_complaint ();
23075 else
23076 return 0;
23077 }
21ae7a4d
JK
23078 break;
23079
23080 case DW_OP_GNU_push_tls_address:
4aa4e28b 23081 case DW_OP_form_tls_address:
21ae7a4d
JK
23082 /* The top of the stack has the offset from the beginning
23083 of the thread control block at which the variable is located. */
23084 /* Nothing should follow this operator, so the top of stack would
23085 be returned. */
23086 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23087 address will be bogus in the psymtab. Make it always at least
23088 non-zero to not look as a variable garbage collected by linker
23089 which have DW_OP_addr 0. */
21ae7a4d 23090 if (i < size)
7d79de9a
TT
23091 {
23092 if (computed == nullptr)
23093 dwarf2_complex_location_expr_complaint ();
23094 else
23095 return 0;
23096 }
585861ea 23097 stack[stacki]++;
21ae7a4d
JK
23098 break;
23099
23100 case DW_OP_GNU_uninit:
7d79de9a
TT
23101 if (computed != nullptr)
23102 return 0;
21ae7a4d
JK
23103 break;
23104
336d760d 23105 case DW_OP_addrx:
3019eac3 23106 case DW_OP_GNU_addr_index:
49f6c839 23107 case DW_OP_GNU_const_index:
3019eac3
DE
23108 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23109 &bytes_read);
23110 i += bytes_read;
23111 break;
23112
21ae7a4d 23113 default:
7d79de9a
TT
23114 if (computed == nullptr)
23115 {
23116 const char *name = get_DW_OP_name (op);
21ae7a4d 23117
7d79de9a
TT
23118 if (name)
23119 complaint (_("unsupported stack op: '%s'"),
23120 name);
23121 else
23122 complaint (_("unsupported stack op: '%02x'"),
23123 op);
23124 }
21ae7a4d
JK
23125
23126 return (stack[stacki]);
d53d4ac5 23127 }
3c6e0cb3 23128
21ae7a4d
JK
23129 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23130 outside of the allocated space. Also enforce minimum>0. */
23131 if (stacki >= ARRAY_SIZE (stack) - 1)
23132 {
7d79de9a
TT
23133 if (computed == nullptr)
23134 complaint (_("location description stack overflow"));
21ae7a4d
JK
23135 return 0;
23136 }
23137
23138 if (stacki <= 0)
23139 {
7d79de9a
TT
23140 if (computed == nullptr)
23141 complaint (_("location description stack underflow"));
21ae7a4d
JK
23142 return 0;
23143 }
23144 }
7d79de9a
TT
23145
23146 if (computed != nullptr)
23147 *computed = true;
21ae7a4d 23148 return (stack[stacki]);
c906108c
SS
23149}
23150
23151/* memory allocation interface */
23152
c906108c 23153static struct dwarf_block *
7b5a2f43 23154dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23155{
8d749320 23156 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23157}
23158
c906108c 23159static struct die_info *
b60c80d6 23160dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23161{
23162 struct die_info *die;
b60c80d6
DJ
23163 size_t size = sizeof (struct die_info);
23164
23165 if (num_attrs > 1)
23166 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23167
b60c80d6 23168 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23169 memset (die, 0, sizeof (struct die_info));
23170 return (die);
23171}
2e276125
JB
23172
23173\f
a036ba48 23174
c90ec28a 23175/* Macro support. */
cf2c3c16 23176
9eac9650
TT
23177/* An overload of dwarf_decode_macros that finds the correct section
23178 and ensures it is read in before calling the other overload. */
23179
23180static void
23181dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23182 int section_is_gnu)
23183{
5e22e966 23184 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23185 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23186 const struct line_header *lh = cu->line_header;
9eac9650
TT
23187 unsigned int offset_size = cu->header.offset_size;
23188 struct dwarf2_section_info *section;
23189 const char *section_name;
23190
23191 if (cu->dwo_unit != nullptr)
23192 {
23193 if (section_is_gnu)
23194 {
23195 section = &cu->dwo_unit->dwo_file->sections.macro;
23196 section_name = ".debug_macro.dwo";
23197 }
23198 else
23199 {
23200 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23201 section_name = ".debug_macinfo.dwo";
23202 }
23203 }
23204 else
23205 {
23206 if (section_is_gnu)
23207 {
5989a64e 23208 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23209 section_name = ".debug_macro";
23210 }
23211 else
23212 {
5989a64e 23213 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23214 section_name = ".debug_macinfo";
23215 }
23216 }
23217
23218 section->read (objfile);
23219 if (section->buffer == nullptr)
23220 {
23221 complaint (_("missing %s section"), section_name);
23222 return;
23223 }
23224
23225 buildsym_compunit *builder = cu->get_builder ();
23226
23227 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23228 offset_size, offset, section_is_gnu);
23229}
23230
3019eac3
DE
23231/* Return the .debug_loc section to use for CU.
23232 For DWO files use .debug_loc.dwo. */
23233
23234static struct dwarf2_section_info *
23235cu_debug_loc_section (struct dwarf2_cu *cu)
23236{
5e22e966 23237 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23238
3019eac3 23239 if (cu->dwo_unit)
43988095
JK
23240 {
23241 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23242
43988095
JK
23243 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23244 }
5989a64e
SM
23245 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23246 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23247}
23248
8cf6f0b1
TT
23249/* A helper function that fills in a dwarf2_loclist_baton. */
23250
23251static void
23252fill_in_loclist_baton (struct dwarf2_cu *cu,
23253 struct dwarf2_loclist_baton *baton,
ff39bb5e 23254 const struct attribute *attr)
8cf6f0b1 23255{
5e22e966 23256 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23257 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23258
96b79293 23259 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23260
a50264ba 23261 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23262 baton->per_cu = cu->per_cu;
23263 gdb_assert (baton->per_cu);
23264 /* We don't know how long the location list is, but make sure we
23265 don't run off the edge of the section. */
3019eac3
DE
23266 baton->size = section->size - DW_UNSND (attr);
23267 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23268 if (cu->base_address.has_value ())
23269 baton->base_address = *cu->base_address;
23270 else
23271 baton->base_address = 0;
f664829e 23272 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23273}
23274
4c2df51b 23275static void
ff39bb5e 23276dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23277 struct dwarf2_cu *cu, int is_block)
4c2df51b 23278{
5e22e966 23279 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23280 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23281 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23282
cd6c91b4 23283 if (attr->form_is_section_offset ()
3019eac3 23284 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23285 the section. If so, fall through to the complaint in the
23286 other branch. */
2c7d5afc 23287 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23288 {
0d53c4c4 23289 struct dwarf2_loclist_baton *baton;
4c2df51b 23290
8d749320 23291 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23292
8cf6f0b1 23293 fill_in_loclist_baton (cu, baton, attr);
be391dca 23294
2b24b6e4 23295 if (!cu->base_address.has_value ())
b98664d3 23296 complaint (_("Location list used without "
3e43a32a 23297 "specifying the CU base address."));
4c2df51b 23298
f1e6e072
TT
23299 SYMBOL_ACLASS_INDEX (sym) = (is_block
23300 ? dwarf2_loclist_block_index
23301 : dwarf2_loclist_index);
0d53c4c4
DJ
23302 SYMBOL_LOCATION_BATON (sym) = baton;
23303 }
23304 else
23305 {
23306 struct dwarf2_locexpr_baton *baton;
23307
8d749320 23308 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23309 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23310 baton->per_cu = cu->per_cu;
23311 gdb_assert (baton->per_cu);
0d53c4c4 23312
4fc6c0d5 23313 if (attr->form_is_block ())
0d53c4c4
DJ
23314 {
23315 /* Note that we're just copying the block's data pointer
23316 here, not the actual data. We're still pointing into the
6502dd73
DJ
23317 info_buffer for SYM's objfile; right now we never release
23318 that buffer, but when we do clean up properly this may
23319 need to change. */
0d53c4c4
DJ
23320 baton->size = DW_BLOCK (attr)->size;
23321 baton->data = DW_BLOCK (attr)->data;
23322 }
23323 else
23324 {
23325 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23326 sym->natural_name ());
0d53c4c4 23327 baton->size = 0;
0d53c4c4 23328 }
6e70227d 23329
f1e6e072
TT
23330 SYMBOL_ACLASS_INDEX (sym) = (is_block
23331 ? dwarf2_locexpr_block_index
23332 : dwarf2_locexpr_index);
0d53c4c4
DJ
23333 SYMBOL_LOCATION_BATON (sym) = baton;
23334 }
4c2df51b 23335}
6502dd73 23336
96408a79
SA
23337/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23338 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23339 CU_HEADERP first. */
23340
23341static const struct comp_unit_head *
23342per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23343 const struct dwarf2_per_cu_data *per_cu)
96408a79 23344{
d521ce57 23345 const gdb_byte *info_ptr;
96408a79
SA
23346
23347 if (per_cu->cu)
23348 return &per_cu->cu->header;
23349
9c541725 23350 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23351
23352 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23353 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23354 rcuh_kind::COMPILE);
96408a79
SA
23355
23356 return cu_headerp;
23357}
23358
09ba997f 23359/* See read.h. */
ae0d2f24 23360
98714339 23361int
09ba997f 23362dwarf2_per_cu_data::addr_size () const
ae0d2f24 23363{
96408a79
SA
23364 struct comp_unit_head cu_header_local;
23365 const struct comp_unit_head *cu_headerp;
c471e790 23366
09ba997f 23367 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23368
23369 return cu_headerp->addr_size;
ae0d2f24
UW
23370}
23371
09ba997f 23372/* See read.h. */
9eae7c52
TT
23373
23374int
09ba997f 23375dwarf2_per_cu_data::offset_size () const
9eae7c52 23376{
96408a79
SA
23377 struct comp_unit_head cu_header_local;
23378 const struct comp_unit_head *cu_headerp;
9c6c53f7 23379
09ba997f 23380 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23381
23382 return cu_headerp->offset_size;
23383}
23384
09ba997f 23385/* See read.h. */
96408a79
SA
23386
23387int
09ba997f 23388dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23389{
23390 struct comp_unit_head cu_header_local;
23391 const struct comp_unit_head *cu_headerp;
23392
09ba997f 23393 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23394
23395 if (cu_headerp->version == 2)
23396 return cu_headerp->addr_size;
23397 else
23398 return cu_headerp->offset_size;
181cebd4
JK
23399}
23400
09ba997f 23401/* See read.h. */
9aa1f1e3 23402
09ba997f 23403struct type *
293e7e51 23404dwarf2_cu::addr_type () const
9a49df9d 23405{
293e7e51 23406 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23407 struct type *void_type = objfile_type (objfile)->builtin_void;
23408 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23409 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23410
23411 if (TYPE_LENGTH (addr_type) == addr_size)
23412 return addr_type;
23413
09ba997f 23414 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23415 return addr_type;
23416}
23417
22b6cd70
TT
23418/* A helper function for dwarf2_find_containing_comp_unit that returns
23419 the index of the result, and that searches a vector. It will
23420 return a result even if the offset in question does not actually
23421 occur in any CU. This is separate so that it can be unit
23422 tested. */
ae038cb0 23423
22b6cd70
TT
23424static int
23425dwarf2_find_containing_comp_unit
23426 (sect_offset sect_off,
23427 unsigned int offset_in_dwz,
23428 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23429{
ae038cb0
DJ
23430 int low, high;
23431
ae038cb0 23432 low = 0;
22b6cd70 23433 high = all_comp_units.size () - 1;
ae038cb0
DJ
23434 while (high > low)
23435 {
36586728 23436 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23437 int mid = low + (high - low) / 2;
9a619af0 23438
22b6cd70 23439 mid_cu = all_comp_units[mid];
36586728 23440 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23441 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23442 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23443 high = mid;
23444 else
23445 low = mid + 1;
23446 }
23447 gdb_assert (low == high);
22b6cd70
TT
23448 return low;
23449}
23450
23451/* Locate the .debug_info compilation unit from CU's objfile which contains
23452 the DIE at OFFSET. Raises an error on failure. */
23453
23454static struct dwarf2_per_cu_data *
23455dwarf2_find_containing_comp_unit (sect_offset sect_off,
23456 unsigned int offset_in_dwz,
23457 struct dwarf2_per_objfile *dwarf2_per_objfile)
23458{
23459 int low
23460 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23461 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23462 struct dwarf2_per_cu_data *this_cu
5989a64e 23463 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23464
45b8ae0c 23465 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23466 {
36586728 23467 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23468 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23469 "offset %s [in module %s]"),
23470 sect_offset_str (sect_off),
ed2dc618 23471 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23472
5989a64e 23473 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23474 <= sect_off);
5989a64e 23475 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23476 }
23477 else
23478 {
5989a64e 23479 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23480 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23481 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23482 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23483 return this_cu;
23484 }
23485}
23486
22b6cd70
TT
23487#if GDB_SELF_TEST
23488
23489namespace selftests {
23490namespace find_containing_comp_unit {
23491
23492static void
23493run_test ()
23494{
23495 struct dwarf2_per_cu_data one {};
23496 struct dwarf2_per_cu_data two {};
23497 struct dwarf2_per_cu_data three {};
23498 struct dwarf2_per_cu_data four {};
23499
23500 one.length = 5;
23501 two.sect_off = sect_offset (one.length);
23502 two.length = 7;
23503
23504 three.length = 5;
23505 three.is_dwz = 1;
23506 four.sect_off = sect_offset (three.length);
23507 four.length = 7;
23508 four.is_dwz = 1;
23509
23510 std::vector<dwarf2_per_cu_data *> units;
23511 units.push_back (&one);
23512 units.push_back (&two);
23513 units.push_back (&three);
23514 units.push_back (&four);
23515
23516 int result;
23517
23518 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23519 SELF_CHECK (units[result] == &one);
23520 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23521 SELF_CHECK (units[result] == &one);
23522 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23523 SELF_CHECK (units[result] == &two);
23524
23525 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23526 SELF_CHECK (units[result] == &three);
23527 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23528 SELF_CHECK (units[result] == &three);
23529 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23530 SELF_CHECK (units[result] == &four);
23531}
23532
23533}
23534}
23535
23536#endif /* GDB_SELF_TEST */
23537
9e021579 23538/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23539
9e021579
SM
23540dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23541 dwarf2_per_objfile *per_objfile)
23542 : per_cu (per_cu),
23543 per_objfile (per_objfile),
9068261f
AB
23544 mark (false),
23545 has_loclist (false),
23546 checked_producer (false),
23547 producer_is_gxx_lt_4_6 (false),
23548 producer_is_gcc_lt_4_3 (false),
eb77c9df 23549 producer_is_icc (false),
9068261f 23550 producer_is_icc_lt_14 (false),
c258c396 23551 producer_is_codewarrior (false),
9068261f 23552 processing_has_namespace_info (false)
93311388 23553{
fcd3b13d
SM
23554 per_cu->cu = this;
23555}
23556
23557/* Destroy a dwarf2_cu. */
23558
23559dwarf2_cu::~dwarf2_cu ()
23560{
23561 per_cu->cu = NULL;
9816fde3
JK
23562}
23563
23564/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23565
23566static void
95554aad
TT
23567prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23568 enum language pretend_language)
9816fde3
JK
23569{
23570 struct attribute *attr;
23571
23572 /* Set the language we're debugging. */
23573 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23574 if (attr != nullptr)
9816fde3
JK
23575 set_cu_language (DW_UNSND (attr), cu);
23576 else
9cded63f 23577 {
95554aad 23578 cu->language = pretend_language;
9cded63f
TT
23579 cu->language_defn = language_def (cu->language);
23580 }
dee91e82 23581
7d45c7c3 23582 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23583}
23584
ae038cb0
DJ
23585/* Increase the age counter on each cached compilation unit, and free
23586 any that are too old. */
23587
23588static void
ed2dc618 23589age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23590{
23591 struct dwarf2_per_cu_data *per_cu, **last_chain;
23592
5989a64e
SM
23593 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23594 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23595 while (per_cu != NULL)
23596 {
23597 per_cu->cu->last_used ++;
b4f54984 23598 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23599 dwarf2_mark (per_cu->cu);
23600 per_cu = per_cu->cu->read_in_chain;
23601 }
23602
5989a64e
SM
23603 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23604 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23605 while (per_cu != NULL)
23606 {
23607 struct dwarf2_per_cu_data *next_cu;
23608
23609 next_cu = per_cu->cu->read_in_chain;
23610
23611 if (!per_cu->cu->mark)
23612 {
fcd3b13d 23613 delete per_cu->cu;
ae038cb0
DJ
23614 *last_chain = next_cu;
23615 }
23616 else
23617 last_chain = &per_cu->cu->read_in_chain;
23618
23619 per_cu = next_cu;
23620 }
23621}
23622
23623/* Remove a single compilation unit from the cache. */
23624
23625static void
120ce1b5
SM
23626free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23627 dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23628{
23629 struct dwarf2_per_cu_data *per_cu, **last_chain;
23630
5989a64e
SM
23631 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23632 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23633 while (per_cu != NULL)
23634 {
23635 struct dwarf2_per_cu_data *next_cu;
23636
23637 next_cu = per_cu->cu->read_in_chain;
23638
dee91e82 23639 if (per_cu == target_per_cu)
ae038cb0 23640 {
fcd3b13d 23641 delete per_cu->cu;
dee91e82 23642 per_cu->cu = NULL;
ae038cb0
DJ
23643 *last_chain = next_cu;
23644 break;
23645 }
23646 else
23647 last_chain = &per_cu->cu->read_in_chain;
23648
23649 per_cu = next_cu;
23650 }
23651}
23652
dee91e82
DE
23653/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23654 We store these in a hash table separate from the DIEs, and preserve them
23655 when the DIEs are flushed out of cache.
23656
23657 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23658 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23659 or the type may come from a DWO file. Furthermore, while it's more logical
23660 to use per_cu->section+offset, with Fission the section with the data is in
23661 the DWO file but we don't know that section at the point we need it.
23662 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23663 because we can enter the lookup routine, get_die_type_at_offset, from
23664 outside this file, and thus won't necessarily have PER_CU->cu.
23665 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23666
dee91e82 23667struct dwarf2_per_cu_offset_and_type
1c379e20 23668{
dee91e82 23669 const struct dwarf2_per_cu_data *per_cu;
9c541725 23670 sect_offset sect_off;
1c379e20
DJ
23671 struct type *type;
23672};
23673
dee91e82 23674/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23675
23676static hashval_t
dee91e82 23677per_cu_offset_and_type_hash (const void *item)
1c379e20 23678{
9a3c8263
SM
23679 const struct dwarf2_per_cu_offset_and_type *ofs
23680 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23681
9c541725 23682 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23683}
23684
dee91e82 23685/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23686
23687static int
dee91e82 23688per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23689{
9a3c8263
SM
23690 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23691 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23692 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23693 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23694
dee91e82 23695 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23696 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23697}
23698
23699/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23700 table if necessary. For convenience, return TYPE.
23701
23702 The DIEs reading must have careful ordering to:
85102364 23703 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23704 reading current DIE.
23705 * Not trying to dereference contents of still incompletely read in types
23706 while reading in other DIEs.
23707 * Enable referencing still incompletely read in types just by a pointer to
23708 the type without accessing its fields.
23709
23710 Therefore caller should follow these rules:
23711 * Try to fetch any prerequisite types we may need to build this DIE type
23712 before building the type and calling set_die_type.
e71ec853 23713 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23714 possible before fetching more types to complete the current type.
23715 * Make the type as complete as possible before fetching more types. */
1c379e20 23716
f792889a 23717static struct type *
1c379e20
DJ
23718set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23719{
5e22e966 23720 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23721 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23722 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23723 struct attribute *attr;
23724 struct dynamic_prop prop;
1c379e20 23725
b4ba55a1
JB
23726 /* For Ada types, make sure that the gnat-specific data is always
23727 initialized (if not already set). There are a few types where
23728 we should not be doing so, because the type-specific area is
23729 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23730 where the type-specific area is used to store the floatformat).
23731 But this is not a problem, because the gnat-specific information
23732 is actually not needed for these types. */
23733 if (need_gnat_info (cu)
78134374
SM
23734 && type->code () != TYPE_CODE_FUNC
23735 && type->code () != TYPE_CODE_FLT
23736 && type->code () != TYPE_CODE_METHODPTR
23737 && type->code () != TYPE_CODE_MEMBERPTR
23738 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23739 && !HAVE_GNAT_AUX_INFO (type))
23740 INIT_GNAT_SPECIFIC (type);
23741
3f2f83dd
KB
23742 /* Read DW_AT_allocated and set in type. */
23743 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23744 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23745 {
293e7e51 23746 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23747 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23748 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23749 }
23750 else if (attr != NULL)
23751 {
b98664d3 23752 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23753 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23754 sect_offset_str (die->sect_off));
3f2f83dd
KB
23755 }
23756
23757 /* Read DW_AT_associated and set in type. */
23758 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23759 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23760 {
293e7e51 23761 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23762 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23763 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23764 }
23765 else if (attr != NULL)
23766 {
b98664d3 23767 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23768 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23769 sect_offset_str (die->sect_off));
3f2f83dd
KB
23770 }
23771
3cdcd0ce
JB
23772 /* Read DW_AT_data_location and set in type. */
23773 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23774 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23775 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23776
ae090bdb
SM
23777 if (dwarf2_per_objfile->die_type_hash == NULL)
23778 dwarf2_per_objfile->die_type_hash
0335378b
TT
23779 = htab_up (htab_create_alloc (127,
23780 per_cu_offset_and_type_hash,
23781 per_cu_offset_and_type_eq,
23782 NULL, xcalloc, xfree));
1c379e20 23783
dee91e82 23784 ofs.per_cu = cu->per_cu;
9c541725 23785 ofs.sect_off = die->sect_off;
1c379e20 23786 ofs.type = type;
dee91e82 23787 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23788 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23789 if (*slot)
b98664d3 23790 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23791 sect_offset_str (die->sect_off));
8d749320
SM
23792 *slot = XOBNEW (&objfile->objfile_obstack,
23793 struct dwarf2_per_cu_offset_and_type);
1c379e20 23794 **slot = ofs;
f792889a 23795 return type;
1c379e20
DJ
23796}
23797
9c541725 23798/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23799 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23800
23801static struct type *
9c541725 23802get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23803 dwarf2_per_cu_data *per_cu,
23804 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23805{
dee91e82 23806 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23807
ae090bdb 23808 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23809 return NULL;
1c379e20 23810
dee91e82 23811 ofs.per_cu = per_cu;
9c541725 23812 ofs.sect_off = sect_off;
9a3c8263 23813 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23814 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23815 if (slot)
23816 return slot->type;
23817 else
23818 return NULL;
23819}
23820
02142a6c 23821/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23822 or return NULL if DIE does not have a saved type. */
23823
23824static struct type *
23825get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23826{
aa66c379 23827 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23828}
23829
10b3939b
DJ
23830/* Add a dependence relationship from CU to REF_PER_CU. */
23831
23832static void
23833dwarf2_add_dependence (struct dwarf2_cu *cu,
23834 struct dwarf2_per_cu_data *ref_per_cu)
23835{
23836 void **slot;
23837
23838 if (cu->dependencies == NULL)
23839 cu->dependencies
23840 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23841 NULL, &cu->comp_unit_obstack,
23842 hashtab_obstack_allocate,
23843 dummy_obstack_deallocate);
23844
23845 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23846 if (*slot == NULL)
23847 *slot = ref_per_cu;
23848}
1c379e20 23849
f504f079
DE
23850/* Subroutine of dwarf2_mark to pass to htab_traverse.
23851 Set the mark field in every compilation unit in the
ae038cb0
DJ
23852 cache that we must keep because we are keeping CU. */
23853
10b3939b
DJ
23854static int
23855dwarf2_mark_helper (void **slot, void *data)
23856{
23857 struct dwarf2_per_cu_data *per_cu;
23858
23859 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23860
23861 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23862 reading of the chain. As such dependencies remain valid it is not much
23863 useful to track and undo them during QUIT cleanups. */
23864 if (per_cu->cu == NULL)
23865 return 1;
23866
10b3939b
DJ
23867 if (per_cu->cu->mark)
23868 return 1;
9068261f 23869 per_cu->cu->mark = true;
10b3939b
DJ
23870
23871 if (per_cu->cu->dependencies != NULL)
23872 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23873
23874 return 1;
23875}
23876
f504f079
DE
23877/* Set the mark field in CU and in every other compilation unit in the
23878 cache that we must keep because we are keeping CU. */
23879
ae038cb0
DJ
23880static void
23881dwarf2_mark (struct dwarf2_cu *cu)
23882{
23883 if (cu->mark)
23884 return;
9068261f 23885 cu->mark = true;
10b3939b
DJ
23886 if (cu->dependencies != NULL)
23887 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23888}
23889
23890static void
23891dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23892{
23893 while (per_cu)
23894 {
9068261f 23895 per_cu->cu->mark = false;
ae038cb0
DJ
23896 per_cu = per_cu->cu->read_in_chain;
23897 }
72bf9492
DJ
23898}
23899
72bf9492
DJ
23900/* Trivial hash function for partial_die_info: the hash value of a DIE
23901 is its offset in .debug_info for this objfile. */
23902
23903static hashval_t
23904partial_die_hash (const void *item)
23905{
9a3c8263
SM
23906 const struct partial_die_info *part_die
23907 = (const struct partial_die_info *) item;
9a619af0 23908
9c541725 23909 return to_underlying (part_die->sect_off);
72bf9492
DJ
23910}
23911
23912/* Trivial comparison function for partial_die_info structures: two DIEs
23913 are equal if they have the same offset. */
23914
23915static int
23916partial_die_eq (const void *item_lhs, const void *item_rhs)
23917{
9a3c8263
SM
23918 const struct partial_die_info *part_die_lhs
23919 = (const struct partial_die_info *) item_lhs;
23920 const struct partial_die_info *part_die_rhs
23921 = (const struct partial_die_info *) item_rhs;
9a619af0 23922
9c541725 23923 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23924}
23925
3c3bb058
AB
23926struct cmd_list_element *set_dwarf_cmdlist;
23927struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23928
9291a0cd 23929static void
cd4fb1b2
SM
23930show_check_physname (struct ui_file *file, int from_tty,
23931 struct cmd_list_element *c, const char *value)
9291a0cd 23932{
cd4fb1b2
SM
23933 fprintf_filtered (file,
23934 _("Whether to check \"physname\" is %s.\n"),
23935 value);
9291a0cd
TT
23936}
23937
6c265988 23938void _initialize_dwarf2_read ();
cd4fb1b2 23939void
6c265988 23940_initialize_dwarf2_read ()
9291a0cd 23941{
0743fc83 23942 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23943Set DWARF specific variables.\n\
590042fc 23944Configure DWARF variables such as the cache size."),
0743fc83
TT
23945 &set_dwarf_cmdlist, "maintenance set dwarf ",
23946 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23947
0743fc83 23948 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23949Show DWARF specific variables.\n\
23950Show DWARF variables such as the cache size."),
0743fc83
TT
23951 &show_dwarf_cmdlist, "maintenance show dwarf ",
23952 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23953
cd4fb1b2
SM
23954 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23955 &dwarf_max_cache_age, _("\
23956Set the upper bound on the age of cached DWARF compilation units."), _("\
23957Show the upper bound on the age of cached DWARF compilation units."), _("\
23958A higher limit means that cached compilation units will be stored\n\
23959in memory longer, and more total memory will be used. Zero disables\n\
23960caching, which can slow down startup."),
23961 NULL,
23962 show_dwarf_max_cache_age,
23963 &set_dwarf_cmdlist,
23964 &show_dwarf_cmdlist);
156942c7 23965
cd4fb1b2
SM
23966 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23967Set debugging of the DWARF reader."), _("\
23968Show debugging of the DWARF reader."), _("\
23969When enabled (non-zero), debugging messages are printed during DWARF\n\
23970reading and symtab expansion. A value of 1 (one) provides basic\n\
23971information. A value greater than 1 provides more verbose information."),
23972 NULL,
23973 NULL,
23974 &setdebuglist, &showdebuglist);
9291a0cd 23975
cd4fb1b2
SM
23976 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23977Set debugging of the DWARF DIE reader."), _("\
23978Show debugging of the DWARF DIE reader."), _("\
23979When enabled (non-zero), DIEs are dumped after they are read in.\n\
23980The value is the maximum depth to print."),
23981 NULL,
23982 NULL,
23983 &setdebuglist, &showdebuglist);
9291a0cd 23984
cd4fb1b2
SM
23985 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23986Set debugging of the dwarf line reader."), _("\
23987Show debugging of the dwarf line reader."), _("\
23988When enabled (non-zero), line number entries are dumped as they are read in.\n\
23989A value of 1 (one) provides basic information.\n\
23990A value greater than 1 provides more verbose information."),
23991 NULL,
23992 NULL,
23993 &setdebuglist, &showdebuglist);
437afbb8 23994
cd4fb1b2
SM
23995 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23996Set cross-checking of \"physname\" code against demangler."), _("\
23997Show cross-checking of \"physname\" code against demangler."), _("\
23998When enabled, GDB's internal \"physname\" code is checked against\n\
23999the demangler."),
24000 NULL, show_check_physname,
24001 &setdebuglist, &showdebuglist);
900e11f9 24002
e615022a
DE
24003 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24004 no_class, &use_deprecated_index_sections, _("\
24005Set whether to use deprecated gdb_index sections."), _("\
24006Show whether to use deprecated gdb_index sections."), _("\
24007When enabled, deprecated .gdb_index sections are used anyway.\n\
24008Normally they are ignored either because of a missing feature or\n\
24009performance issue.\n\
24010Warning: This option must be enabled before gdb reads the file."),
24011 NULL,
24012 NULL,
24013 &setlist, &showlist);
24014
f1e6e072
TT
24015 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24016 &dwarf2_locexpr_funcs);
24017 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24018 &dwarf2_loclist_funcs);
24019
24020 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24021 &dwarf2_block_frame_base_locexpr_funcs);
24022 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24023 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24024
24025#if GDB_SELF_TEST
24026 selftests::register_test ("dw2_expand_symtabs_matching",
24027 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24028 selftests::register_test ("dwarf2_find_containing_comp_unit",
24029 selftests::find_containing_comp_unit::run_test);
c62446b1 24030#endif
6502dd73 24031}
This page took 5.40608 seconds and 4 git commands to generate.