Move signatured_type::type to unshareable object
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
c24bdb02
KS
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
293e7e51
SM
424 /* Return a type that is a generic pointer type, the size of which
425 matches the address size given in the compilation unit header for
426 this CU. */
427 struct type *addr_type () const;
428
429 /* Find an integer type the same size as the address size given in
430 the compilation unit header for this CU. UNSIGNED_P controls if
431 the integer is unsigned or not. */
432 struct type *addr_sized_int_type (bool unsigned_p) const;
433
d00adf39 434 /* The header of the compilation unit. */
fcd3b13d 435 struct comp_unit_head header {};
e142c38c 436
d00adf39 437 /* Base address of this compilation unit. */
2b24b6e4 438 gdb::optional<CORE_ADDR> base_address;
d00adf39 439
e142c38c 440 /* The language we are debugging. */
fcd3b13d
SM
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
e142c38c 443
fcd3b13d 444 const char *producer = nullptr;
b0f35d58 445
c24bdb02 446private:
804d2729
TT
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
c24bdb02 449 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 450
c24bdb02 451public:
e142c38c
DJ
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
fcd3b13d 461 struct pending **list_in_scope = nullptr;
e142c38c 462
b64f50a1
JK
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 465 htab_t partial_dies = nullptr;
72bf9492
DJ
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
fcd3b13d 469 auto_obstack comp_unit_obstack;
72bf9492 470
ae038cb0
DJ
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
fcd3b13d 475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 476
69d751e3 477 /* Backlink to our per_cu entry. */
ae038cb0
DJ
478 struct dwarf2_per_cu_data *per_cu;
479
9e021579
SM
480 /* The dwarf2_per_objfile that owns this. */
481 struct dwarf2_per_objfile *per_objfile;
482
ae038cb0 483 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 484 int last_used = 0;
ae038cb0 485
b64f50a1
JK
486 /* A hash table of DIE cu_offset for following references with
487 die_info->offset.sect_off as hash. */
fcd3b13d 488 htab_t die_hash = nullptr;
10b3939b
DJ
489
490 /* Full DIEs if read in. */
fcd3b13d 491 struct die_info *dies = nullptr;
10b3939b
DJ
492
493 /* A set of pointers to dwarf2_per_cu_data objects for compilation
494 units referenced by this one. Only set during full symbol processing;
495 partial symbol tables do not have dependencies. */
fcd3b13d 496 htab_t dependencies = nullptr;
10b3939b 497
cb1df416 498 /* Header data from the line table, during full symbol processing. */
fcd3b13d 499 struct line_header *line_header = nullptr;
4c8aa72d 500 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 501 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
502 this is the DW_TAG_compile_unit die for this CU. We'll hold on
503 to the line header as long as this DIE is being processed. See
504 process_die_scope. */
fcd3b13d 505 die_info *line_header_die_owner = nullptr;
cb1df416 506
3da10d80
KS
507 /* A list of methods which need to have physnames computed
508 after all type information has been read. */
c89b44cd 509 std::vector<delayed_method_info> method_list;
3da10d80 510
96408a79 511 /* To be copied to symtab->call_site_htab. */
fcd3b13d 512 htab_t call_site_htab = nullptr;
96408a79 513
034e5797
DE
514 /* Non-NULL if this CU came from a DWO file.
515 There is an invariant here that is important to remember:
516 Except for attributes copied from the top level DIE in the "main"
517 (or "stub") file in preparation for reading the DWO file
18a8505e 518 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
519 Either there isn't a DWO file (in which case this is NULL and the point
520 is moot), or there is and either we're not going to read it (in which
521 case this is NULL) or there is and we are reading it (in which case this
522 is non-NULL). */
fcd3b13d 523 struct dwo_unit *dwo_unit = nullptr;
3019eac3 524
18a8505e 525 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 526 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 527 gdb::optional<ULONGEST> addr_base;
3019eac3 528
18a8505e 529 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 530 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 531 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
532 be used without needing to know whether DWO files are in use or not.
533 N.B. This does not apply to DW_AT_ranges appearing in
534 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
535 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 536 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 537 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 538 ULONGEST ranges_base = 0;
2e3cf129 539
41144253 540 /* The DW_AT_loclists_base attribute if present. */
541 ULONGEST loclist_base = 0;
542
c9317f21
TT
543 /* When reading debug info generated by older versions of rustc, we
544 have to rewrite some union types to be struct types with a
545 variant part. This rewriting must be done after the CU is fully
546 read in, because otherwise at the point of rewriting some struct
547 type might not have been fully processed. So, we keep a list of
548 all such types here and process them after expansion. */
549 std::vector<struct type *> rust_unions;
550
18a8505e
AT
551 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
552 files, the value is implicitly zero. For DWARF 5 version DWO files, the
553 value is often implicit and is the size of the header of
554 .debug_str_offsets section (8 or 4, depending on the address size). */
555 gdb::optional<ULONGEST> str_offsets_base;
556
ae038cb0 557 /* Mark used when releasing cached dies. */
9068261f 558 bool mark : 1;
ae038cb0 559
8be455d7
JK
560 /* This CU references .debug_loc. See the symtab->locations_valid field.
561 This test is imperfect as there may exist optimized debug code not using
562 any location list and still facing inlining issues if handled as
563 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 564 bool has_loclist : 1;
ba919b58 565
9068261f 566 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
567 if all the producer_is_* fields are valid. This information is cached
568 because profiling CU expansion showed excessive time spent in
569 producer_is_gxx_lt_4_6. */
9068261f
AB
570 bool checked_producer : 1;
571 bool producer_is_gxx_lt_4_6 : 1;
572 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 573 bool producer_is_icc : 1;
9068261f 574 bool producer_is_icc_lt_14 : 1;
c258c396 575 bool producer_is_codewarrior : 1;
4d4ec4e5 576
9068261f 577 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
578 debugging info for C++ namespaces. GCC 3.3.x did not produce
579 this information, but later versions do. */
580
9068261f 581 bool processing_has_namespace_info : 1;
d590ff25
YQ
582
583 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
584
585 /* If this CU was inherited by another CU (via specification,
586 abstract_origin, etc), this is the ancestor CU. */
587 dwarf2_cu *ancestor;
588
589 /* Get the buildsym_compunit for this CU. */
590 buildsym_compunit *get_builder ()
591 {
592 /* If this CU has a builder associated with it, use that. */
593 if (m_builder != nullptr)
594 return m_builder.get ();
595
596 /* Otherwise, search ancestors for a valid builder. */
597 if (ancestor != nullptr)
598 return ancestor->get_builder ();
599
600 return nullptr;
601 }
e7c27a73
DJ
602};
603
094b34ac
DE
604/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
605 This includes type_unit_group and quick_file_names. */
606
607struct stmt_list_hash
608{
609 /* The DWO unit this table is from or NULL if there is none. */
610 struct dwo_unit *dwo_unit;
611
612 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 613 sect_offset line_sect_off;
094b34ac
DE
614};
615
5989a64e 616/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
617 an object of this type. This contains elements of type unit groups
618 that can be shared across objfiles. The non-shareable parts are in
619 type_unit_group_unshareable. */
f4dc4d17
DE
620
621struct type_unit_group
622{
0186c6a7 623 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
624 To simplify things we create an artificial CU that "includes" all the
625 type units using this stmt_list so that the rest of the code still has
197400e8 626 a "per_cu" handle on the symtab. */
094b34ac
DE
627 struct dwarf2_per_cu_data per_cu;
628
0186c6a7
DE
629 /* The TUs that share this DW_AT_stmt_list entry.
630 This is added to while parsing type units to build partial symtabs,
631 and is deleted afterwards and not used again. */
a8b3b8e9 632 std::vector<signatured_type *> *tus;
f4dc4d17 633
094b34ac
DE
634 /* The data used to construct the hash key. */
635 struct stmt_list_hash hash;
f4dc4d17
DE
636};
637
73869dc2 638/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
639
640struct dwo_sections
641{
642 struct dwarf2_section_info abbrev;
3019eac3
DE
643 struct dwarf2_section_info line;
644 struct dwarf2_section_info loc;
43988095 645 struct dwarf2_section_info loclists;
09262596
DE
646 struct dwarf2_section_info macinfo;
647 struct dwarf2_section_info macro;
3019eac3
DE
648 struct dwarf2_section_info str;
649 struct dwarf2_section_info str_offsets;
80626a55
DE
650 /* In the case of a virtual DWO file, these two are unused. */
651 struct dwarf2_section_info info;
fd5866f6 652 std::vector<dwarf2_section_info> types;
3019eac3
DE
653};
654
c88ee1f0 655/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
656
657struct dwo_unit
658{
659 /* Backlink to the containing struct dwo_file. */
660 struct dwo_file *dwo_file;
661
662 /* The "id" that distinguishes this CU/TU.
663 .debug_info calls this "dwo_id", .debug_types calls this "signature".
664 Since signatures came first, we stick with it for consistency. */
665 ULONGEST signature;
666
667 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 668 struct dwarf2_section_info *section;
3019eac3 669
9c541725
PA
670 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
671 sect_offset sect_off;
3019eac3
DE
672 unsigned int length;
673
674 /* For types, offset in the type's DIE of the type defined by this TU. */
675 cu_offset type_offset_in_tu;
676};
677
73869dc2
DE
678/* include/dwarf2.h defines the DWP section codes.
679 It defines a max value but it doesn't define a min value, which we
680 use for error checking, so provide one. */
681
682enum dwp_v2_section_ids
683{
684 DW_SECT_MIN = 1
685};
686
80626a55 687/* Data for one DWO file.
57d63ce2
DE
688
689 This includes virtual DWO files (a virtual DWO file is a DWO file as it
690 appears in a DWP file). DWP files don't really have DWO files per se -
691 comdat folding of types "loses" the DWO file they came from, and from
692 a high level view DWP files appear to contain a mass of random types.
693 However, to maintain consistency with the non-DWP case we pretend DWP
694 files contain virtual DWO files, and we assign each TU with one virtual
695 DWO file (generally based on the line and abbrev section offsets -
696 a heuristic that seems to work in practice). */
3019eac3
DE
697
698struct dwo_file
699{
51ac9db5
SM
700 dwo_file () = default;
701 DISABLE_COPY_AND_ASSIGN (dwo_file);
702
18a8505e 703 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
704 For virtual DWO files the name is constructed from the section offsets
705 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
706 from related CU+TUs. */
51ac9db5 707 const char *dwo_name = nullptr;
0ac5b59e
DE
708
709 /* The DW_AT_comp_dir attribute. */
51ac9db5 710 const char *comp_dir = nullptr;
3019eac3 711
80626a55
DE
712 /* The bfd, when the file is open. Otherwise this is NULL.
713 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 714 gdb_bfd_ref_ptr dbfd;
3019eac3 715
73869dc2
DE
716 /* The sections that make up this DWO file.
717 Remember that for virtual DWO files in DWP V2, these are virtual
718 sections (for lack of a better name). */
51ac9db5 719 struct dwo_sections sections {};
3019eac3 720
33c5cd75
DB
721 /* The CUs in the file.
722 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
723 an extension to handle LLVM's Link Time Optimization output (where
724 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 725 htab_up cus;
3019eac3
DE
726
727 /* Table of TUs in the file.
728 Each element is a struct dwo_unit. */
b0b6a987 729 htab_up tus;
3019eac3
DE
730};
731
80626a55
DE
732/* These sections are what may appear in a DWP file. */
733
734struct dwp_sections
735{
73869dc2 736 /* These are used by both DWP version 1 and 2. */
80626a55
DE
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info cu_index;
739 struct dwarf2_section_info tu_index;
73869dc2
DE
740
741 /* These are only used by DWP version 2 files.
742 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
743 sections are referenced by section number, and are not recorded here.
744 In DWP version 2 there is at most one copy of all these sections, each
745 section being (effectively) comprised of the concatenation of all of the
746 individual sections that exist in the version 1 format.
747 To keep the code simple we treat each of these concatenated pieces as a
748 section itself (a virtual section?). */
749 struct dwarf2_section_info abbrev;
750 struct dwarf2_section_info info;
751 struct dwarf2_section_info line;
752 struct dwarf2_section_info loc;
753 struct dwarf2_section_info macinfo;
754 struct dwarf2_section_info macro;
755 struct dwarf2_section_info str_offsets;
756 struct dwarf2_section_info types;
80626a55
DE
757};
758
73869dc2
DE
759/* These sections are what may appear in a virtual DWO file in DWP version 1.
760 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 761
73869dc2 762struct virtual_v1_dwo_sections
80626a55
DE
763{
764 struct dwarf2_section_info abbrev;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
767 struct dwarf2_section_info macinfo;
768 struct dwarf2_section_info macro;
769 struct dwarf2_section_info str_offsets;
770 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 771 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
772 struct dwarf2_section_info info_or_types;
773};
774
73869dc2
DE
775/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
776 In version 2, the sections of the DWO files are concatenated together
777 and stored in one section of that name. Thus each ELF section contains
778 several "virtual" sections. */
779
780struct virtual_v2_dwo_sections
781{
782 bfd_size_type abbrev_offset;
783 bfd_size_type abbrev_size;
784
785 bfd_size_type line_offset;
786 bfd_size_type line_size;
787
788 bfd_size_type loc_offset;
789 bfd_size_type loc_size;
790
791 bfd_size_type macinfo_offset;
792 bfd_size_type macinfo_size;
793
794 bfd_size_type macro_offset;
795 bfd_size_type macro_size;
796
797 bfd_size_type str_offsets_offset;
798 bfd_size_type str_offsets_size;
799
800 /* Each DWP hash table entry records one CU or one TU.
801 That is recorded here, and copied to dwo_unit.section. */
802 bfd_size_type info_or_types_offset;
803 bfd_size_type info_or_types_size;
804};
805
80626a55
DE
806/* Contents of DWP hash tables. */
807
808struct dwp_hash_table
809{
73869dc2 810 uint32_t version, nr_columns;
80626a55 811 uint32_t nr_units, nr_slots;
73869dc2
DE
812 const gdb_byte *hash_table, *unit_table;
813 union
814 {
815 struct
816 {
817 const gdb_byte *indices;
818 } v1;
819 struct
820 {
821 /* This is indexed by column number and gives the id of the section
822 in that column. */
823#define MAX_NR_V2_DWO_SECTIONS \
824 (1 /* .debug_info or .debug_types */ \
825 + 1 /* .debug_abbrev */ \
826 + 1 /* .debug_line */ \
827 + 1 /* .debug_loc */ \
828 + 1 /* .debug_str_offsets */ \
829 + 1 /* .debug_macro or .debug_macinfo */)
830 int section_ids[MAX_NR_V2_DWO_SECTIONS];
831 const gdb_byte *offsets;
832 const gdb_byte *sizes;
833 } v2;
834 } section_pool;
80626a55
DE
835};
836
837/* Data for one DWP file. */
838
839struct dwp_file
840{
400174b1
TT
841 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
842 : name (name_),
843 dbfd (std::move (abfd))
844 {
845 }
846
80626a55
DE
847 /* Name of the file. */
848 const char *name;
849
73869dc2 850 /* File format version. */
400174b1 851 int version = 0;
73869dc2 852
93417882 853 /* The bfd. */
400174b1 854 gdb_bfd_ref_ptr dbfd;
80626a55
DE
855
856 /* Section info for this file. */
400174b1 857 struct dwp_sections sections {};
80626a55 858
57d63ce2 859 /* Table of CUs in the file. */
400174b1 860 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
861
862 /* Table of TUs in the file. */
400174b1 863 const struct dwp_hash_table *tus = nullptr;
80626a55 864
19ac8c2e 865 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
866 htab_up loaded_cus;
867 htab_up loaded_tus;
80626a55 868
73869dc2
DE
869 /* Table to map ELF section numbers to their sections.
870 This is only needed for the DWP V1 file format. */
400174b1
TT
871 unsigned int num_sections = 0;
872 asection **elf_sections = nullptr;
80626a55
DE
873};
874
0963b4bd
MS
875/* Struct used to pass misc. parameters to read_die_and_children, et
876 al. which are used for both .debug_info and .debug_types dies.
877 All parameters here are unchanging for the life of the call. This
dee91e82 878 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
879
880struct die_reader_specs
881{
a32a8923 882 /* The bfd of die_section. */
93311388
DE
883 bfd* abfd;
884
885 /* The CU of the DIE we are parsing. */
886 struct dwarf2_cu *cu;
887
80626a55 888 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
889 struct dwo_file *dwo_file;
890
dee91e82 891 /* The section the die comes from.
3019eac3 892 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
893 struct dwarf2_section_info *die_section;
894
895 /* die_section->buffer. */
d521ce57 896 const gdb_byte *buffer;
f664829e
DE
897
898 /* The end of the buffer. */
899 const gdb_byte *buffer_end;
a2ce51a0 900
685af9cd
TT
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
c0ab21c2
TT
905/* A subclass of die_reader_specs that holds storage and has complex
906 constructor and destructor behavior. */
907
908class cutu_reader : public die_reader_specs
909{
910public:
911
ab432490
SM
912 cutu_reader (dwarf2_per_cu_data *this_cu,
913 dwarf2_per_objfile *per_objfile,
c0ab21c2 914 struct abbrev_table *abbrev_table,
6751ebae 915 int use_existing_cu,
c0ab21c2
TT
916 bool skip_partial);
917
918 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 919 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
920 struct dwarf2_cu *parent_cu = nullptr,
921 struct dwo_file *dwo_file = nullptr);
922
c0ab21c2
TT
923 DISABLE_COPY_AND_ASSIGN (cutu_reader);
924
925 const gdb_byte *info_ptr = nullptr;
926 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
927 bool dummy_p = false;
928
6751ebae
TT
929 /* Release the new CU, putting it on the chain. This cannot be done
930 for dummy CUs. */
931 void keep ();
932
c0ab21c2 933private:
9e021579
SM
934 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
935 dwarf2_per_objfile *per_objfile,
6751ebae 936 int use_existing_cu);
c0ab21c2
TT
937
938 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
939 std::unique_ptr<dwarf2_cu> m_new_cu;
940
941 /* The ordinary abbreviation table. */
942 abbrev_table_up m_abbrev_table_holder;
943
944 /* The DWO abbreviation table. */
945 abbrev_table_up m_dwo_abbrev_table;
946};
dee91e82 947
c906108c 948/* When we construct a partial symbol table entry we only
0963b4bd 949 need this much information. */
6f06d47b 950struct partial_die_info : public allocate_on_obstack
c906108c 951 {
6f06d47b
YQ
952 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
953
954 /* Disable assign but still keep copy ctor, which is needed
955 load_partial_dies. */
956 partial_die_info& operator=(const partial_die_info& rhs) = delete;
957
52356b79
YQ
958 /* Adjust the partial die before generating a symbol for it. This
959 function may set the is_external flag or change the DIE's
960 name. */
961 void fixup (struct dwarf2_cu *cu);
962
48fbe735
YQ
963 /* Read a minimal amount of information into the minimal die
964 structure. */
965 const gdb_byte *read (const struct die_reader_specs *reader,
966 const struct abbrev_info &abbrev,
967 const gdb_byte *info_ptr);
968
72bf9492 969 /* Offset of this DIE. */
6f06d47b 970 const sect_offset sect_off;
72bf9492
DJ
971
972 /* DWARF-2 tag for this DIE. */
6f06d47b 973 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 974
72bf9492 975 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
976 const unsigned int has_children : 1;
977
72bf9492
DJ
978 unsigned int is_external : 1;
979 unsigned int is_declaration : 1;
980 unsigned int has_type : 1;
981 unsigned int has_specification : 1;
982 unsigned int has_pc_info : 1;
481860b3 983 unsigned int may_be_inlined : 1;
72bf9492 984
0c1b455e
TT
985 /* This DIE has been marked DW_AT_main_subprogram. */
986 unsigned int main_subprogram : 1;
987
72bf9492
DJ
988 /* Flag set if the SCOPE field of this structure has been
989 computed. */
990 unsigned int scope_set : 1;
991
fa4028e9
JB
992 /* Flag set if the DIE has a byte_size attribute. */
993 unsigned int has_byte_size : 1;
994
ff908ebf
AW
995 /* Flag set if the DIE has a DW_AT_const_value attribute. */
996 unsigned int has_const_value : 1;
997
98bfdba5
PA
998 /* Flag set if any of the DIE's children are template arguments. */
999 unsigned int has_template_arguments : 1;
1000
52356b79 1001 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1002 unsigned int fixup_called : 1;
1003
36586728
TT
1004 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1005 unsigned int is_dwz : 1;
1006
1007 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1008 unsigned int spec_is_dwz : 1;
1009
72bf9492 1010 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1011 sometimes a default name for unnamed DIEs. */
6f06d47b 1012 const char *name = nullptr;
72bf9492 1013
abc72ce4 1014 /* The linkage name, if present. */
6f06d47b 1015 const char *linkage_name = nullptr;
abc72ce4 1016
72bf9492
DJ
1017 /* The scope to prepend to our children. This is generally
1018 allocated on the comp_unit_obstack, so will disappear
1019 when this compilation unit leaves the cache. */
6f06d47b 1020 const char *scope = nullptr;
72bf9492 1021
95554aad
TT
1022 /* Some data associated with the partial DIE. The tag determines
1023 which field is live. */
1024 union
1025 {
1026 /* The location description associated with this DIE, if any. */
1027 struct dwarf_block *locdesc;
1028 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1029 sect_offset sect_off;
6f06d47b 1030 } d {};
72bf9492
DJ
1031
1032 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1033 CORE_ADDR lowpc = 0;
1034 CORE_ADDR highpc = 0;
72bf9492 1035
93311388 1036 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1037 DW_AT_sibling, if any. */
48fbe735
YQ
1038 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1039 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1040 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1041
1042 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1043 DW_AT_specification (or DW_AT_abstract_origin or
1044 DW_AT_extension). */
6f06d47b 1045 sect_offset spec_offset {};
72bf9492
DJ
1046
1047 /* Pointers to this DIE's parent, first child, and next sibling,
1048 if any. */
6f06d47b
YQ
1049 struct partial_die_info *die_parent = nullptr;
1050 struct partial_die_info *die_child = nullptr;
1051 struct partial_die_info *die_sibling = nullptr;
1052
1053 friend struct partial_die_info *
1054 dwarf2_cu::find_partial_die (sect_offset sect_off);
1055
1056 private:
1057 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1058 partial_die_info (sect_offset sect_off)
1059 : partial_die_info (sect_off, DW_TAG_padding, 0)
1060 {
1061 }
1062
1063 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1064 int has_children_)
1065 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1066 {
1067 is_external = 0;
1068 is_declaration = 0;
1069 has_type = 0;
1070 has_specification = 0;
1071 has_pc_info = 0;
1072 may_be_inlined = 0;
1073 main_subprogram = 0;
1074 scope_set = 0;
1075 has_byte_size = 0;
1076 has_const_value = 0;
1077 has_template_arguments = 0;
1078 fixup_called = 0;
1079 is_dwz = 0;
1080 spec_is_dwz = 0;
1081 }
c906108c
SS
1082 };
1083
c906108c
SS
1084/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1085 but this would require a corresponding change in unpack_field_as_long
1086 and friends. */
1087static int bits_per_byte = 8;
1088
9c6a1327
TT
1089struct variant_part_builder;
1090
1091/* When reading a variant, we track a bit more information about the
1092 field, and store it in an object of this type. */
2ddeaf8a
TT
1093
1094struct variant_field
1095{
9c6a1327
TT
1096 int first_field = -1;
1097 int last_field = -1;
1098
1099 /* A variant can contain other variant parts. */
1100 std::vector<variant_part_builder> variant_parts;
1101
2ddeaf8a
TT
1102 /* If we see a DW_TAG_variant, then this will be set if this is the
1103 default branch. */
9c6a1327
TT
1104 bool default_branch = false;
1105 /* If we see a DW_AT_discr_value, then this will be the discriminant
1106 value. */
1107 ULONGEST discriminant_value = 0;
1108 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1109 data. */
1110 struct dwarf_block *discr_list_data = nullptr;
1111};
1112
1113/* This represents a DW_TAG_variant_part. */
1114
1115struct variant_part_builder
1116{
1117 /* The offset of the discriminant field. */
1118 sect_offset discriminant_offset {};
1119
1120 /* Variants that are direct children of this variant part. */
1121 std::vector<variant_field> variants;
1122
1123 /* True if we're currently reading a variant. */
1124 bool processing_variant = false;
2ddeaf8a
TT
1125};
1126
52059ffd
TT
1127struct nextfield
1128{
be2daae6
TT
1129 int accessibility = 0;
1130 int virtuality = 0;
9c6a1327
TT
1131 /* Variant parts need to find the discriminant, which is a DIE
1132 reference. We track the section offset of each field to make
1133 this link. */
1134 sect_offset offset;
be2daae6 1135 struct field field {};
52059ffd
TT
1136};
1137
1138struct fnfieldlist
1139{
be2daae6
TT
1140 const char *name = nullptr;
1141 std::vector<struct fn_field> fnfields;
52059ffd
TT
1142};
1143
c906108c
SS
1144/* The routines that read and process dies for a C struct or C++ class
1145 pass lists of data member fields and lists of member function fields
1146 in an instance of a field_info structure, as defined below. */
1147struct field_info
c5aa993b 1148 {
0963b4bd 1149 /* List of data member and baseclasses fields. */
be2daae6
TT
1150 std::vector<struct nextfield> fields;
1151 std::vector<struct nextfield> baseclasses;
c906108c 1152
85102364 1153 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1154 int non_public_fields = 0;
c906108c 1155
c5aa993b
JM
1156 /* Member function fieldlist array, contains name of possibly overloaded
1157 member function, number of overloaded member functions and a pointer
1158 to the head of the member function field chain. */
be2daae6 1159 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1160
1161 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1162 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1163 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1164
1165 /* Nested types defined by this class and the number of elements in this
1166 list. */
be2daae6 1167 std::vector<struct decl_field> nested_types_list;
317f7127 1168
9c6a1327
TT
1169 /* If non-null, this is the variant part we are currently
1170 reading. */
1171 variant_part_builder *current_variant_part = nullptr;
1172 /* This holds all the top-level variant parts attached to the type
1173 we're reading. */
1174 std::vector<variant_part_builder> variant_parts;
1175
317f7127
TT
1176 /* Return the total number of fields (including baseclasses). */
1177 int nfields () const
1178 {
1179 return fields.size () + baseclasses.size ();
1180 }
c5aa993b 1181 };
c906108c 1182
ae038cb0
DJ
1183/* Loaded secondary compilation units are kept in memory until they
1184 have not been referenced for the processing of this many
1185 compilation units. Set this to zero to disable caching. Cache
1186 sizes of up to at least twenty will improve startup time for
1187 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1188static int dwarf_max_cache_age = 5;
920d2a44 1189static void
b4f54984
DE
1190show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1191 struct cmd_list_element *c, const char *value)
920d2a44 1192{
3e43a32a 1193 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1194 "DWARF compilation units is %s.\n"),
920d2a44
AC
1195 value);
1196}
4390d890 1197\f
c906108c
SS
1198/* local function prototypes */
1199
918dd910
JK
1200static void dwarf2_find_base_address (struct die_info *die,
1201 struct dwarf2_cu *cu);
1202
891813be 1203static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1204 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1205 const char *name);
0018ea6f 1206
f1902523
JK
1207static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1208 const gdb_byte *info_ptr,
3e225074 1209 struct die_info *type_unit_die);
f1902523 1210
ed2dc618
SM
1211static void dwarf2_build_psymtabs_hard
1212 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1213
72bf9492
DJ
1214static void scan_partial_symbols (struct partial_die_info *,
1215 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1216 int, struct dwarf2_cu *);
c906108c 1217
72bf9492
DJ
1218static void add_partial_symbol (struct partial_die_info *,
1219 struct dwarf2_cu *);
63d06c5c 1220
72bf9492
DJ
1221static void add_partial_namespace (struct partial_die_info *pdi,
1222 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1223 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1224
5d7cb8df 1225static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1226 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1227 struct dwarf2_cu *cu);
1228
72bf9492
DJ
1229static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1230 struct dwarf2_cu *cu);
91c24f0a 1231
bc30ff58
JB
1232static void add_partial_subprogram (struct partial_die_info *pdi,
1233 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1234 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1235
d521ce57 1236static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1237
dee91e82 1238static struct partial_die_info *load_partial_dies
d521ce57 1239 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1240
fb816e8b
TV
1241/* A pair of partial_die_info and compilation unit. */
1242struct cu_partial_die_info
1243{
1244 /* The compilation unit of the partial_die_info. */
1245 struct dwarf2_cu *cu;
1246 /* A partial_die_info. */
1247 struct partial_die_info *pdi;
122cf0f2
AB
1248
1249 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1250 : cu (cu),
1251 pdi (pdi)
405feb71 1252 { /* Nothing. */ }
122cf0f2
AB
1253
1254private:
1255 cu_partial_die_info () = delete;
fb816e8b
TV
1256};
1257
122cf0f2
AB
1258static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1259 struct dwarf2_cu *);
72bf9492 1260
d521ce57
TT
1261static const gdb_byte *read_attribute (const struct die_reader_specs *,
1262 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1263 const gdb_byte *, bool *need_reprocess);
1264
1265static void read_attribute_reprocess (const struct die_reader_specs *reader,
1266 struct attribute *attr);
1267
1268static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1269
ed2dc618
SM
1270static sect_offset read_abbrev_offset
1271 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1272 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1273
ed2dc618
SM
1274static const char *read_indirect_string
1275 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1276 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1277
ed2dc618 1278static const char *read_indirect_string_at_offset
4f44ae6c 1279 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1280
d521ce57
TT
1281static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1282 const gdb_byte *,
3019eac3
DE
1283 unsigned int *);
1284
18a8505e
AT
1285static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1286 ULONGEST str_index);
1287
1288static const char *read_stub_str_index (struct dwarf2_cu *cu,
1289 ULONGEST str_index);
3019eac3 1290
e142c38c 1291static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1292
e142c38c
DJ
1293static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1294 struct dwarf2_cu *);
c906108c 1295
7d45c7c3
KB
1296static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1297 struct dwarf2_cu *cu);
1298
a084a2a6
AT
1299static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1300
05cf31d1
JB
1301static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1302 struct dwarf2_cu *cu);
1303
e142c38c 1304static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1305
e142c38c 1306static struct die_info *die_specification (struct die_info *die,
f2f0e013 1307 struct dwarf2_cu **);
63d06c5c 1308
9c541725 1309static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1310 struct dwarf2_cu *cu);
debd256d 1311
f3f5162e 1312static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1313 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1314 CORE_ADDR, int decode_mapping);
c906108c 1315
804d2729
TT
1316static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1317 const char *);
c906108c 1318
a14ed312 1319static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1320 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1321
ff39bb5e 1322static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1323 struct dwarf2_cu *);
c906108c 1324
ff39bb5e 1325static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1326 struct type *type,
1327 const char *name,
1328 struct obstack *obstack,
12df843f 1329 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1330 const gdb_byte **bytes,
98bfdba5 1331 struct dwarf2_locexpr_baton **baton);
2df3850c 1332
e7c27a73 1333static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1334
b4ba55a1
JB
1335static int need_gnat_info (struct dwarf2_cu *);
1336
3e43a32a
MS
1337static struct type *die_descriptive_type (struct die_info *,
1338 struct dwarf2_cu *);
b4ba55a1
JB
1339
1340static void set_descriptive_type (struct type *, struct die_info *,
1341 struct dwarf2_cu *);
1342
e7c27a73
DJ
1343static struct type *die_containing_type (struct die_info *,
1344 struct dwarf2_cu *);
c906108c 1345
ff39bb5e 1346static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1347 struct dwarf2_cu *);
c906108c 1348
f792889a 1349static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1350
673bfd45
DE
1351static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1352
0d5cff50 1353static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1354
6e70227d 1355static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1356 const char *suffix, int physname,
1357 struct dwarf2_cu *cu);
63d06c5c 1358
e7c27a73 1359static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1360
348e048f
DE
1361static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1362
e7c27a73 1363static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1364
e7c27a73 1365static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1366
96408a79
SA
1367static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1368
71a3c369
TT
1369static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1370
ff013f42 1371static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1372 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1373
41144253 1374/* Return the .debug_loclists section to use for cu. */
1375static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1376
3a2b436a 1377/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1378 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1379enum pc_bounds_kind
1380{
e385593e 1381 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1382 PC_BOUNDS_NOT_PRESENT,
1383
e385593e
JK
1384 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1385 were present but they do not form a valid range of PC addresses. */
1386 PC_BOUNDS_INVALID,
1387
3a2b436a
JK
1388 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1389 PC_BOUNDS_RANGES,
1390
1391 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1392 PC_BOUNDS_HIGH_LOW,
1393};
1394
1395static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *,
891813be 1398 dwarf2_psymtab *);
c906108c 1399
fae299cd
DC
1400static void get_scope_pc_bounds (struct die_info *,
1401 CORE_ADDR *, CORE_ADDR *,
1402 struct dwarf2_cu *);
1403
801e3a5b
JB
1404static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1405 CORE_ADDR, struct dwarf2_cu *);
1406
a14ed312 1407static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
a14ed312 1410static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1411 struct type *, struct dwarf2_cu *);
c906108c 1412
a14ed312 1413static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1414 struct die_info *, struct type *,
e7c27a73 1415 struct dwarf2_cu *);
c906108c 1416
a14ed312 1417static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1418 struct type *,
1419 struct dwarf2_cu *);
c906108c 1420
134d01f1 1421static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1424
e7c27a73 1425static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1426
5d7cb8df
JK
1427static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1428
804d2729 1429static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1430
27aa8d6a
SW
1431static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1432
74921315
KS
1433static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1434
f55ee35c
JK
1435static struct type *read_module_type (struct die_info *die,
1436 struct dwarf2_cu *cu);
1437
38d518c9 1438static const char *namespace_name (struct die_info *die,
e142c38c 1439 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1440
134d01f1 1441static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1442
7d79de9a
TT
1443static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1444 bool * = nullptr);
c906108c 1445
6e70227d 1446static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1447 struct dwarf2_cu *);
1448
bf6af496 1449static struct die_info *read_die_and_siblings_1
d521ce57 1450 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1451 struct die_info *);
639d11d3 1452
dee91e82 1453static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1454 const gdb_byte *info_ptr,
1455 const gdb_byte **new_info_ptr,
639d11d3
DC
1456 struct die_info *parent);
1457
d521ce57
TT
1458static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1459 struct die_info **, const gdb_byte *,
3e225074 1460 int);
3019eac3 1461
d521ce57 1462static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1463 struct die_info **, const gdb_byte *);
93311388 1464
e7c27a73 1465static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1466
15d034d0 1467static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1468 struct objfile *);
71c25dea 1469
15d034d0 1470static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1471
15d034d0 1472static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1473 struct die_info *die,
1474 struct dwarf2_cu *cu);
1475
ca69b9e6
DE
1476static const char *dwarf2_physname (const char *name, struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
e142c38c 1479static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1480 struct dwarf2_cu **);
9219021c 1481
d97bc12b
DE
1482static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1483
1484static void dump_die_for_error (struct die_info *);
1485
1486static void dump_die_1 (struct ui_file *, int level, int max_level,
1487 struct die_info *);
c906108c 1488
d97bc12b 1489/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1490
51545339 1491static void store_in_ref_table (struct die_info *,
10b3939b 1492 struct dwarf2_cu *);
c906108c 1493
348e048f 1494static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1495 const struct attribute *,
348e048f
DE
1496 struct dwarf2_cu **);
1497
10b3939b 1498static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1499 const struct attribute *,
f2f0e013 1500 struct dwarf2_cu **);
c906108c 1501
348e048f 1502static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1503 const struct attribute *,
348e048f
DE
1504 struct dwarf2_cu **);
1505
ac9ec31b
DE
1506static struct type *get_signatured_type (struct die_info *, ULONGEST,
1507 struct dwarf2_cu *);
1508
1509static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1510 const struct attribute *,
ac9ec31b
DE
1511 struct dwarf2_cu *);
1512
ab432490
SM
1513static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1514 dwarf2_per_objfile *per_objfile);
348e048f 1515
ab432490
SM
1516static void read_signatured_type (signatured_type *sig_type,
1517 dwarf2_per_objfile *per_objfile);
348e048f 1518
63e43d3a
PMR
1519static int attr_to_dynamic_prop (const struct attribute *attr,
1520 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1521 struct dynamic_prop *prop, struct type *type);
63e43d3a 1522
c906108c
SS
1523/* memory allocation interface */
1524
7b5a2f43 1525static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1526
b60c80d6 1527static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1528
43f3e411 1529static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1530
8cf6f0b1
TT
1531static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1532 struct dwarf2_loclist_baton *baton,
ff39bb5e 1533 const struct attribute *attr);
8cf6f0b1 1534
ff39bb5e 1535static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1536 struct symbol *sym,
f1e6e072
TT
1537 struct dwarf2_cu *cu,
1538 int is_block);
4c2df51b 1539
d521ce57
TT
1540static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1541 const gdb_byte *info_ptr,
1542 struct abbrev_info *abbrev);
4bb7a0a7 1543
72bf9492
DJ
1544static hashval_t partial_die_hash (const void *item);
1545
1546static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1547
ae038cb0 1548static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1549 (sect_offset sect_off, unsigned int offset_in_dwz,
1550 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1551
9816fde3 1552static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1553 struct die_info *comp_unit_die,
1554 enum language pretend_language);
93311388 1555
ed2dc618 1556static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
120ce1b5
SM
1558static void free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
1559 dwarf2_per_objfile *per_objfile);
ae038cb0 1560
f792889a
DJ
1561static struct type *set_die_type (struct die_info *, struct type *,
1562 struct dwarf2_cu *);
1c379e20 1563
ed2dc618 1564static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1565
ed2dc618 1566static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1567
ab432490
SM
1568static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1569 dwarf2_per_objfile *per_objfile,
1570 bool skip_partial,
1571 enum language pretend_language);
10b3939b 1572
47b14e86
SM
1573static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile,
1575 enum language pretend_language);
10b3939b 1576
47b14e86
SM
1577static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1578 dwarf2_per_objfile *per_objfile,
1579 enum language pretend_language);
f4dc4d17 1580
10b3939b
DJ
1581static void dwarf2_add_dependence (struct dwarf2_cu *,
1582 struct dwarf2_per_cu_data *);
1583
ae038cb0
DJ
1584static void dwarf2_mark (struct dwarf2_cu *);
1585
1586static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1587
b64f50a1 1588static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1589 dwarf2_per_cu_data *per_cu,
1590 dwarf2_per_objfile *per_objfile);
673bfd45 1591
f792889a 1592static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1593
120ce1b5
SM
1594static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1595 dwarf2_per_objfile *per_objfile,
95554aad
TT
1596 enum language pretend_language);
1597
ed2dc618 1598static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1599
b303c6f6
AB
1600/* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605class dwarf2_queue_guard
1606{
1607public:
39856def
TT
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
b303c6f6
AB
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
39856def
TT
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
5989a64e 1619 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1620 }
b303c6f6 1621
39856def 1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1623
39856def
TT
1624private:
1625 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1626};
1627
39856def
TT
1628dwarf2_queue_item::~dwarf2_queue_item ()
1629{
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
120ce1b5 1635 free_one_cached_comp_unit (per_cu, per_objfile);
39856def
TT
1636 per_cu->queued = 0;
1637 }
1638}
1639
d721ba37
PA
1640/* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643struct file_and_directory
1644{
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657};
1658
1659static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
9291a0cd 1661
298e9637 1662static htab_up allocate_signatured_type_table ();
1fd400ff 1663
298e9637 1664static htab_up allocate_dwo_unit_table ();
3019eac3 1665
57d63ce2 1666static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1669 ULONGEST signature, int is_debug_types);
a2ce51a0 1670
ed2dc618
SM
1671static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1673
3019eac3 1674static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1675 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1676 ULONGEST signature);
3019eac3
DE
1677
1678static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1679 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1680
89e63ee4
DE
1681static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1682
263db9a1
TT
1683/* A unique pointer to a dwo_file. */
1684
51ac9db5 1685typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1686
ed2dc618 1687static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1688
1b80a9fa 1689static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1690
1691static void free_line_header_voidp (void *arg);
4390d890
DE
1692\f
1693/* Various complaints about symbol reading that don't abort the process. */
1694
4390d890
DE
1695static void
1696dwarf2_debug_line_missing_file_complaint (void)
1697{
b98664d3 1698 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1699}
1700
1701static void
1702dwarf2_debug_line_missing_end_sequence_complaint (void)
1703{
b98664d3 1704 complaint (_(".debug_line section has line "
4390d890
DE
1705 "program sequence without an end"));
1706}
1707
1708static void
1709dwarf2_complex_location_expr_complaint (void)
1710{
b98664d3 1711 complaint (_("location expression too complex"));
4390d890
DE
1712}
1713
1714static void
1715dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1716 int arg3)
1717{
b98664d3 1718 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1719 arg1, arg2, arg3);
1720}
1721
4390d890
DE
1722static void
1723dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1724{
b98664d3 1725 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1726 arg1, arg2);
1727}
527f3840
JK
1728
1729/* Hash function for line_header_hash. */
1730
1731static hashval_t
1732line_header_hash (const struct line_header *ofs)
1733{
9c541725 1734 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1735}
1736
1737/* Hash function for htab_create_alloc_ex for line_header_hash. */
1738
1739static hashval_t
1740line_header_hash_voidp (const void *item)
1741{
9a3c8263 1742 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1743
1744 return line_header_hash (ofs);
1745}
1746
1747/* Equality function for line_header_hash. */
1748
1749static int
1750line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1751{
9a3c8263
SM
1752 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1753 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1754
9c541725 1755 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1756 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1757}
1758
4390d890 1759\f
9291a0cd 1760
330cdd98
PA
1761/* See declaration. */
1762
5989a64e
SM
1763dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1764 bool can_copy_)
c3699833
SM
1765 : obfd (obfd),
1766 can_copy (can_copy_)
330cdd98
PA
1767{
1768 if (names == NULL)
1769 names = &dwarf2_elf_names;
1770
330cdd98
PA
1771 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1772 locate_sections (obfd, sec, *names);
1773}
1774
5989a64e 1775dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98
PA
1776{
1777 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1778 free_cached_comp_units ();
1779
b76e467d 1780 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1781 per_cu->imported_symtabs_free ();
fc8e7e75 1782
b2bdb8cf 1783 for (signatured_type *sig_type : all_type_units)
ae640021 1784 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1785
5989a64e 1786 /* Everything else should be on this->obstack. */
330cdd98
PA
1787}
1788
1789/* See declaration. */
1790
1791void
5989a64e 1792dwarf2_per_bfd::free_cached_comp_units ()
330cdd98
PA
1793{
1794 dwarf2_per_cu_data *per_cu = read_in_chain;
1795 dwarf2_per_cu_data **last_chain = &read_in_chain;
1796 while (per_cu != NULL)
1797 {
1798 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1799
fcd3b13d 1800 delete per_cu->cu;
330cdd98
PA
1801 *last_chain = next_cu;
1802 per_cu = next_cu;
1803 }
1804}
1805
11ed8cad
TT
1806/* A helper class that calls free_cached_comp_units on
1807 destruction. */
1808
1809class free_cached_comp_units
1810{
1811public:
1812
1813 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1814 : m_per_objfile (per_objfile)
1815 {
1816 }
1817
1818 ~free_cached_comp_units ()
1819 {
5989a64e 1820 m_per_objfile->per_bfd->free_cached_comp_units ();
11ed8cad
TT
1821 }
1822
1823 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1824
1825private:
1826
1827 dwarf2_per_objfile *m_per_objfile;
1828};
1829
af758d11
SM
1830/* See read.h. */
1831
1832bool
1833dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1834{
1835 gdb_assert (per_cu->index < this->m_symtabs.size ());
1836
1837 return this->m_symtabs[per_cu->index] != nullptr;
1838}
1839
1840/* See read.h. */
1841
1842compunit_symtab *
1843dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1844{
1845 gdb_assert (per_cu->index < this->m_symtabs.size ());
1846
1847 return this->m_symtabs[per_cu->index];
1848}
1849
1850/* See read.h. */
1851
1852void
1853dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1854 compunit_symtab *symtab)
1855{
1856 gdb_assert (per_cu->index < this->m_symtabs.size ());
1857 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1858
1859 this->m_symtabs[per_cu->index] = symtab;
1860}
1861
c906108c 1862/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1863 information and return true if we have enough to do something.
1864 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1865 ELF names are used. CAN_COPY is true for formats where symbol
1866 interposition is possible and so symbol values must follow copy
1867 relocation rules. */
c906108c
SS
1868
1869int
251d32d9 1870dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1871 const struct dwarf2_debug_sections *names,
1872 bool can_copy)
c906108c 1873{
97cbe998
SDJ
1874 if (objfile->flags & OBJF_READNEVER)
1875 return 0;
1876
ed2dc618
SM
1877 struct dwarf2_per_objfile *dwarf2_per_objfile
1878 = get_dwarf2_per_objfile (objfile);
1879
1880 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1881 {
1882 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1883 sharing yet). */
1884 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1885
1886 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1887 }
5bfd760d 1888
5989a64e
SM
1889 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1890 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1891 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1892 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1893}
1894
251d32d9
TG
1895/* When loading sections, we look either for uncompressed section or for
1896 compressed section names. */
233a11ab
CS
1897
1898static int
251d32d9
TG
1899section_is_p (const char *section_name,
1900 const struct dwarf2_section_names *names)
233a11ab 1901{
251d32d9
TG
1902 if (names->normal != NULL
1903 && strcmp (section_name, names->normal) == 0)
1904 return 1;
1905 if (names->compressed != NULL
1906 && strcmp (section_name, names->compressed) == 0)
1907 return 1;
1908 return 0;
233a11ab
CS
1909}
1910
330cdd98 1911/* See declaration. */
c906108c 1912
330cdd98 1913void
5989a64e
SM
1914dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1915 const dwarf2_debug_sections &names)
c906108c 1916{
fd361982 1917 flagword aflag = bfd_section_flags (sectp);
251d32d9 1918
dc7650b8
JK
1919 if ((aflag & SEC_HAS_CONTENTS) == 0)
1920 {
1921 }
950b7495
KS
1922 else if (elf_section_data (sectp)->this_hdr.sh_size
1923 > bfd_get_file_size (abfd))
1924 {
1925 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1926 warning (_("Discarding section %s which has a section size (%s"
1927 ") larger than the file size [in module %s]"),
1928 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1929 bfd_get_filename (abfd));
1930 }
330cdd98 1931 else if (section_is_p (sectp->name, &names.info))
c906108c 1932 {
330cdd98 1933 this->info.s.section = sectp;
fd361982 1934 this->info.size = bfd_section_size (sectp);
c906108c 1935 }
330cdd98 1936 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1937 {
330cdd98 1938 this->abbrev.s.section = sectp;
fd361982 1939 this->abbrev.size = bfd_section_size (sectp);
c906108c 1940 }
330cdd98 1941 else if (section_is_p (sectp->name, &names.line))
c906108c 1942 {
330cdd98 1943 this->line.s.section = sectp;
fd361982 1944 this->line.size = bfd_section_size (sectp);
c906108c 1945 }
330cdd98 1946 else if (section_is_p (sectp->name, &names.loc))
c906108c 1947 {
330cdd98 1948 this->loc.s.section = sectp;
fd361982 1949 this->loc.size = bfd_section_size (sectp);
c906108c 1950 }
330cdd98 1951 else if (section_is_p (sectp->name, &names.loclists))
43988095 1952 {
330cdd98 1953 this->loclists.s.section = sectp;
fd361982 1954 this->loclists.size = bfd_section_size (sectp);
43988095 1955 }
330cdd98 1956 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1957 {
330cdd98 1958 this->macinfo.s.section = sectp;
fd361982 1959 this->macinfo.size = bfd_section_size (sectp);
c906108c 1960 }
330cdd98 1961 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1962 {
330cdd98 1963 this->macro.s.section = sectp;
fd361982 1964 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1965 }
330cdd98 1966 else if (section_is_p (sectp->name, &names.str))
c906108c 1967 {
330cdd98 1968 this->str.s.section = sectp;
fd361982 1969 this->str.size = bfd_section_size (sectp);
c906108c 1970 }
18a8505e
AT
1971 else if (section_is_p (sectp->name, &names.str_offsets))
1972 {
1973 this->str_offsets.s.section = sectp;
1974 this->str_offsets.size = bfd_section_size (sectp);
1975 }
330cdd98 1976 else if (section_is_p (sectp->name, &names.line_str))
43988095 1977 {
330cdd98 1978 this->line_str.s.section = sectp;
fd361982 1979 this->line_str.size = bfd_section_size (sectp);
43988095 1980 }
330cdd98 1981 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1982 {
330cdd98 1983 this->addr.s.section = sectp;
fd361982 1984 this->addr.size = bfd_section_size (sectp);
3019eac3 1985 }
330cdd98 1986 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1987 {
330cdd98 1988 this->frame.s.section = sectp;
fd361982 1989 this->frame.size = bfd_section_size (sectp);
b6af0555 1990 }
330cdd98 1991 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1992 {
330cdd98 1993 this->eh_frame.s.section = sectp;
fd361982 1994 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1995 }
330cdd98 1996 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1997 {
330cdd98 1998 this->ranges.s.section = sectp;
fd361982 1999 this->ranges.size = bfd_section_size (sectp);
af34e669 2000 }
330cdd98 2001 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2002 {
330cdd98 2003 this->rnglists.s.section = sectp;
fd361982 2004 this->rnglists.size = bfd_section_size (sectp);
43988095 2005 }
330cdd98 2006 else if (section_is_p (sectp->name, &names.types))
348e048f 2007 {
8b70b953
TT
2008 struct dwarf2_section_info type_section;
2009
2010 memset (&type_section, 0, sizeof (type_section));
049412e3 2011 type_section.s.section = sectp;
fd361982 2012 type_section.size = bfd_section_size (sectp);
8b70b953 2013
fd5866f6 2014 this->types.push_back (type_section);
348e048f 2015 }
330cdd98 2016 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2017 {
330cdd98 2018 this->gdb_index.s.section = sectp;
fd361982 2019 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2020 }
927aa2e7
JK
2021 else if (section_is_p (sectp->name, &names.debug_names))
2022 {
2023 this->debug_names.s.section = sectp;
fd361982 2024 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2025 }
2026 else if (section_is_p (sectp->name, &names.debug_aranges))
2027 {
2028 this->debug_aranges.s.section = sectp;
fd361982 2029 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2030 }
dce234bc 2031
fd361982
AM
2032 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2033 && bfd_section_vma (sectp) == 0)
330cdd98 2034 this->has_section_at_zero = true;
c906108c
SS
2035}
2036
dce234bc 2037/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2038 SECTION_NAME. */
af34e669 2039
dce234bc 2040void
3017a003
TG
2041dwarf2_get_section_info (struct objfile *objfile,
2042 enum dwarf2_section_enum sect,
d521ce57 2043 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2044 bfd_size_type *sizep)
2045{
5bfd760d 2046 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2047 struct dwarf2_section_info *info;
a3b2a86b
TT
2048
2049 /* We may see an objfile without any DWARF, in which case we just
2050 return nothing. */
2051 if (data == NULL)
2052 {
2053 *sectp = NULL;
2054 *bufp = NULL;
2055 *sizep = 0;
2056 return;
2057 }
3017a003
TG
2058 switch (sect)
2059 {
2060 case DWARF2_DEBUG_FRAME:
5989a64e 2061 info = &data->per_bfd->frame;
3017a003
TG
2062 break;
2063 case DWARF2_EH_FRAME:
5989a64e 2064 info = &data->per_bfd->eh_frame;
3017a003
TG
2065 break;
2066 default:
2067 gdb_assert_not_reached ("unexpected section");
2068 }
dce234bc 2069
96b79293 2070 info->read (objfile);
dce234bc 2071
96b79293 2072 *sectp = info->get_bfd_section ();
dce234bc
PP
2073 *bufp = info->buffer;
2074 *sizep = info->size;
2075}
2076
36586728
TT
2077/* A helper function to find the sections for a .dwz file. */
2078
2079static void
2080locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2081{
9a3c8263 2082 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2083
2084 /* Note that we only support the standard ELF names, because .dwz
2085 is ELF-only (at the time of writing). */
2086 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2087 {
049412e3 2088 dwz_file->abbrev.s.section = sectp;
fd361982 2089 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2090 }
2091 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2092 {
049412e3 2093 dwz_file->info.s.section = sectp;
fd361982 2094 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2095 }
2096 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2097 {
049412e3 2098 dwz_file->str.s.section = sectp;
fd361982 2099 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2100 }
2101 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2102 {
049412e3 2103 dwz_file->line.s.section = sectp;
fd361982 2104 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2105 }
2106 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2107 {
049412e3 2108 dwz_file->macro.s.section = sectp;
fd361982 2109 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2110 }
2ec9a5e0
TT
2111 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2112 {
049412e3 2113 dwz_file->gdb_index.s.section = sectp;
fd361982 2114 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2115 }
927aa2e7
JK
2116 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2117 {
2118 dwz_file->debug_names.s.section = sectp;
fd361982 2119 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2120 }
36586728
TT
2121}
2122
c4973306 2123/* See dwarf2read.h. */
36586728 2124
c4973306 2125struct dwz_file *
c3699833 2126dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2127{
36586728 2128 const char *filename;
acd13123 2129 bfd_size_type buildid_len_arg;
dc294be5
TT
2130 size_t buildid_len;
2131 bfd_byte *buildid;
36586728 2132
c3699833
SM
2133 if (per_bfd->dwz_file != NULL)
2134 return per_bfd->dwz_file.get ();
36586728 2135
4db1a1dc 2136 bfd_set_error (bfd_error_no_error);
791afaa2 2137 gdb::unique_xmalloc_ptr<char> data
c3699833 2138 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2139 &buildid_len_arg, &buildid));
4db1a1dc
TT
2140 if (data == NULL)
2141 {
2142 if (bfd_get_error () == bfd_error_no_error)
2143 return NULL;
2144 error (_("could not read '.gnu_debugaltlink' section: %s"),
2145 bfd_errmsg (bfd_get_error ()));
2146 }
791afaa2
TT
2147
2148 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2149
acd13123
TT
2150 buildid_len = (size_t) buildid_len_arg;
2151
791afaa2 2152 filename = data.get ();
d721ba37
PA
2153
2154 std::string abs_storage;
36586728
TT
2155 if (!IS_ABSOLUTE_PATH (filename))
2156 {
14278e1f 2157 gdb::unique_xmalloc_ptr<char> abs
c3699833 2158 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2159
14278e1f 2160 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2161 filename = abs_storage.c_str ();
36586728
TT
2162 }
2163
dc294be5
TT
2164 /* First try the file name given in the section. If that doesn't
2165 work, try to use the build-id instead. */
ad80db5b 2166 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2167 if (dwz_bfd != NULL)
36586728 2168 {
192b62ce 2169 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2170 dwz_bfd.reset (nullptr);
36586728
TT
2171 }
2172
dc294be5
TT
2173 if (dwz_bfd == NULL)
2174 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2175
0d79cdc4
AM
2176 if (dwz_bfd == nullptr)
2177 {
2178 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2179 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2180
2181 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2182 buildid_len,
2183 origname,
2184 &alt_filename));
2185
2186 if (fd.get () >= 0)
2187 {
2188 /* File successfully retrieved from server. */
ad80db5b 2189 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2190
2191 if (dwz_bfd == nullptr)
2192 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2193 alt_filename.get ());
2194 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2195 dwz_bfd.reset (nullptr);
2196 }
2197 }
2198
dc294be5
TT
2199 if (dwz_bfd == NULL)
2200 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2201 bfd_get_filename (per_bfd->obfd));
dc294be5 2202
7ff8cb8c
TT
2203 std::unique_ptr<struct dwz_file> result
2204 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2205
7ff8cb8c
TT
2206 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2207 result.get ());
36586728 2208
c3699833
SM
2209 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2210 per_bfd->dwz_file = std::move (result);
2211 return per_bfd->dwz_file.get ();
36586728 2212}
9291a0cd 2213\f
7b9f3c50
DE
2214/* DWARF quick_symbols_functions support. */
2215
2216/* TUs can share .debug_line entries, and there can be a lot more TUs than
2217 unique line tables, so we maintain a separate table of all .debug_line
2218 derived entries to support the sharing.
2219 All the quick functions need is the list of file names. We discard the
2220 line_header when we're done and don't need to record it here. */
2221struct quick_file_names
2222{
094b34ac
DE
2223 /* The data used to construct the hash key. */
2224 struct stmt_list_hash hash;
7b9f3c50
DE
2225
2226 /* The number of entries in file_names, real_names. */
2227 unsigned int num_file_names;
2228
2229 /* The file names from the line table, after being run through
2230 file_full_name. */
2231 const char **file_names;
2232
2233 /* The file names from the line table after being run through
2234 gdb_realpath. These are computed lazily. */
2235 const char **real_names;
2236};
2237
2238/* When using the index (and thus not using psymtabs), each CU has an
2239 object of this type. This is used to hold information needed by
2240 the various "quick" methods. */
2241struct dwarf2_per_cu_quick_data
2242{
2243 /* The file table. This can be NULL if there was no file table
2244 or it's currently not read in.
5989a64e 2245 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2246 struct quick_file_names *file_names;
2247
7b9f3c50
DE
2248 /* A temporary mark bit used when iterating over all CUs in
2249 expand_symtabs_matching. */
2250 unsigned int mark : 1;
2251
2252 /* True if we've tried to read the file table and found there isn't one.
2253 There will be no point in trying to read it again next time. */
2254 unsigned int no_file_data : 1;
2255};
2256
094b34ac
DE
2257/* Utility hash function for a stmt_list_hash. */
2258
2259static hashval_t
2260hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2261{
2262 hashval_t v = 0;
2263
2264 if (stmt_list_hash->dwo_unit != NULL)
2265 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2266 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2267 return v;
2268}
2269
2270/* Utility equality function for a stmt_list_hash. */
2271
2272static int
2273eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2274 const struct stmt_list_hash *rhs)
2275{
2276 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2277 return 0;
2278 if (lhs->dwo_unit != NULL
2279 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2280 return 0;
2281
9c541725 2282 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2283}
2284
7b9f3c50
DE
2285/* Hash function for a quick_file_names. */
2286
2287static hashval_t
2288hash_file_name_entry (const void *e)
2289{
9a3c8263
SM
2290 const struct quick_file_names *file_data
2291 = (const struct quick_file_names *) e;
7b9f3c50 2292
094b34ac 2293 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2294}
2295
2296/* Equality function for a quick_file_names. */
2297
2298static int
2299eq_file_name_entry (const void *a, const void *b)
2300{
9a3c8263
SM
2301 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2302 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2303
094b34ac 2304 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2305}
2306
2307/* Delete function for a quick_file_names. */
2308
2309static void
2310delete_file_name_entry (void *e)
2311{
9a3c8263 2312 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2313 int i;
2314
2315 for (i = 0; i < file_data->num_file_names; ++i)
2316 {
2317 xfree ((void*) file_data->file_names[i]);
2318 if (file_data->real_names)
2319 xfree ((void*) file_data->real_names[i]);
2320 }
2321
45940949
TT
2322 /* The space for the struct itself lives on the obstack, so we don't
2323 free it here. */
7b9f3c50
DE
2324}
2325
2326/* Create a quick_file_names hash table. */
2327
5895093f 2328static htab_up
7b9f3c50
DE
2329create_quick_file_names_table (unsigned int nr_initial_entries)
2330{
5895093f
TT
2331 return htab_up (htab_create_alloc (nr_initial_entries,
2332 hash_file_name_entry, eq_file_name_entry,
2333 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2334}
9291a0cd 2335
ab432490
SM
2336/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2337 function is unrelated to symtabs, symtab would have to be created afterwards.
2338 You should call age_cached_comp_units after processing the CU. */
918dd910
JK
2339
2340static void
ab432490
SM
2341load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2342 bool skip_partial)
918dd910 2343{
3019eac3 2344 if (per_cu->is_debug_types)
ab432490 2345 load_full_type_unit (per_cu, per_objfile);
918dd910 2346 else
ab432490 2347 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2348
cc12ce38
DE
2349 if (per_cu->cu == NULL)
2350 return; /* Dummy CU. */
2dc860c0
DE
2351
2352 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2353}
2354
97a1449a 2355/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2356
9291a0cd 2357static void
97a1449a
SM
2358dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2359 dwarf2_per_objfile *dwarf2_per_objfile,
2360 bool skip_partial)
9291a0cd 2361{
f4dc4d17
DE
2362 /* Skip type_unit_groups, reading the type units they contain
2363 is handled elsewhere. */
197400e8 2364 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2365 return;
2366
b303c6f6
AB
2367 /* The destructor of dwarf2_queue_guard frees any entries left on
2368 the queue. After this point we're guaranteed to leave this function
2369 with the dwarf queue empty. */
39856def 2370 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2371
af758d11 2372 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2373 {
120ce1b5 2374 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
ab432490 2375 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2376
2377 /* If we just loaded a CU from a DWO, and we're working with an index
2378 that may badly handle TUs, load all the TUs in that DWO as well.
2379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2380 if (!per_cu->is_debug_types
cc12ce38 2381 && per_cu->cu != NULL
89e63ee4 2382 && per_cu->cu->dwo_unit != NULL
5989a64e
SM
2383 && dwarf2_per_objfile->per_bfd->index_table != NULL
2384 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2385 /* DWP files aren't supported yet. */
ed2dc618 2386 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2387 queue_and_load_all_dwo_tus (per_cu);
95554aad 2388 }
9291a0cd 2389
ed2dc618 2390 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2391
2392 /* Age the cache, releasing compilation units that have not
2393 been used recently. */
ed2dc618 2394 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2395}
2396
97a1449a
SM
2397/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2398 the per-objfile for which this symtab is instantiated.
2399
2400 Returns the resulting symbol table. */
2fdf6df6 2401
43f3e411 2402static struct compunit_symtab *
97a1449a
SM
2403dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2404 dwarf2_per_objfile *dwarf2_per_objfile,
2405 bool skip_partial)
9291a0cd 2406{
5989a64e 2407 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2408
2409 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2410 {
11ed8cad 2411 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2412 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2413 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2414 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2415 }
f194fefb 2416
af758d11 2417 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2418}
2419
ff4c9fec 2420/* See declaration. */
f4dc4d17 2421
ff4c9fec 2422dwarf2_per_cu_data *
5989a64e 2423dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2424{
b76e467d 2425 if (index >= this->all_comp_units.size ())
ff4c9fec 2426 {
b76e467d 2427 index -= this->all_comp_units.size ();
b2bdb8cf 2428 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2429 return &this->all_type_units[index]->per_cu;
2430 }
f4dc4d17 2431
ff4c9fec
SM
2432 return this->all_comp_units[index];
2433}
f4dc4d17 2434
ff4c9fec 2435/* See declaration. */
2fdf6df6 2436
ff4c9fec 2437dwarf2_per_cu_data *
5989a64e 2438dwarf2_per_bfd::get_cu (int index)
1fd400ff 2439{
b76e467d 2440 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2441
ff4c9fec 2442 return this->all_comp_units[index];
f4dc4d17
DE
2443}
2444
ff4c9fec 2445/* See declaration. */
f4dc4d17 2446
ff4c9fec 2447signatured_type *
5989a64e 2448dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2449{
b2bdb8cf 2450 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2451
ff4c9fec 2452 return this->all_type_units[index];
1fd400ff
TT
2453}
2454
d3473f0c
TT
2455/* See read.h. */
2456
2457dwarf2_per_cu_data *
5989a64e 2458dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2459{
2460 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2461 result->per_bfd = this;
d3473f0c
TT
2462 result->index = m_num_psymtabs++;
2463 return result;
2464}
2465
2466/* See read.h. */
2467
2468signatured_type *
5989a64e 2469dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2470{
2471 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2472 result->per_cu.per_bfd = this;
d3473f0c
TT
2473 result->per_cu.index = m_num_psymtabs++;
2474 return result;
2475}
2476
45940949
TT
2477/* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2478 obstack, and constructed with the specified field values. */
4b514bc8
JK
2479
2480static dwarf2_per_cu_data *
ed2dc618 2481create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2482 struct dwarf2_section_info *section,
2483 int is_dwz,
2484 sect_offset sect_off, ULONGEST length)
2485{
5989a64e 2486 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
4b514bc8
JK
2487 the_cu->sect_off = sect_off;
2488 the_cu->length = length;
4b514bc8 2489 the_cu->section = section;
5989a64e 2490 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
45940949 2491 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494}
2495
2ec9a5e0
TT
2496/* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2fdf6df6 2498
74a0d9f6 2499static void
12359b5e 2500create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
b76e467d 2503 int is_dwz)
9291a0cd 2504{
12359b5e 2505 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2506 {
74a0d9f6 2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2512 cu_list += 2 * 8;
2513
b76e467d 2514 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2515 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2516 sect_off, length);
5989a64e 2517 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2518 }
9291a0cd
TT
2519}
2520
2ec9a5e0 2521/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2522 the CU objects for this objfile. */
2ec9a5e0 2523
74a0d9f6 2524static void
12359b5e 2525create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528{
5989a64e
SM
2529 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2530 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
b76e467d 2531 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2532
12359b5e 2533 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
5989a64e 2534 &dwarf2_per_objfile->per_bfd->info, 0);
2ec9a5e0
TT
2535
2536 if (dwz_elements == 0)
74a0d9f6 2537 return;
2ec9a5e0 2538
c3699833 2539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
12359b5e 2540 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2541 &dwz->info, 1);
2ec9a5e0
TT
2542}
2543
1fd400ff 2544/* Create the signatured type hash table from the index. */
673bfd45 2545
74a0d9f6 2546static void
12359b5e
SM
2547create_signatured_type_table_from_index
2548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct dwarf2_section_info *section,
2550 const gdb_byte *bytes,
2551 offset_type elements)
1fd400ff 2552{
5989a64e
SM
2553 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2554 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2555
298e9637 2556 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2557
12359b5e 2558 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2559 {
52dc124a 2560 struct signatured_type *sig_type;
9c541725 2561 ULONGEST signature;
1fd400ff 2562 void **slot;
9c541725 2563 cu_offset type_offset_in_tu;
1fd400ff 2564
74a0d9f6 2565 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2568 type_offset_in_tu
2569 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2570 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2571 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2572 bytes += 3 * 8;
2573
5989a64e 2574 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
52dc124a 2575 sig_type->signature = signature;
9c541725 2576 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2577 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2578 sig_type->per_cu.section = section;
9c541725 2579 sig_type->per_cu.sect_off = sect_off;
52dc124a 2580 sig_type->per_cu.v.quick
5989a64e 2581 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
1fd400ff
TT
2582 struct dwarf2_per_cu_quick_data);
2583
b0b6a987 2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2585 *slot = sig_type;
1fd400ff 2586
5989a64e 2587 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2588 }
2589
5989a64e 2590 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2591}
2592
927aa2e7
JK
2593/* Create the signatured type hash table from .debug_names. */
2594
2595static void
2596create_signatured_type_table_from_debug_names
ed2dc618 2597 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2598 const mapped_debug_names &map,
2599 struct dwarf2_section_info *section,
2600 struct dwarf2_section_info *abbrev_section)
2601{
ed2dc618
SM
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603
96b79293
TT
2604 section->read (objfile);
2605 abbrev_section->read (objfile);
927aa2e7 2606
5989a64e
SM
2607 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2608 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2609
298e9637 2610 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2611
2612 for (uint32_t i = 0; i < map.tu_count; ++i)
2613 {
2614 struct signatured_type *sig_type;
927aa2e7 2615 void **slot;
927aa2e7
JK
2616
2617 sect_offset sect_off
2618 = (sect_offset) (extract_unsigned_integer
2619 (map.tu_table_reordered + i * map.offset_size,
2620 map.offset_size,
2621 map.dwarf5_byte_order));
2622
2623 comp_unit_head cu_header;
ed2dc618
SM
2624 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2625 abbrev_section,
927aa2e7
JK
2626 section->buffer + to_underlying (sect_off),
2627 rcuh_kind::TYPE);
2628
5989a64e 2629 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2630 sig_type->signature = cu_header.signature;
2631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2632 sig_type->per_cu.is_debug_types = 1;
2633 sig_type->per_cu.section = section;
2634 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2635 sig_type->per_cu.v.quick
5989a64e 2636 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2637 struct dwarf2_per_cu_quick_data);
2638
b0b6a987 2639 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2640 *slot = sig_type;
2641
5989a64e 2642 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2643 }
2644
5989a64e 2645 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2646}
2647
9291a0cd
TT
2648/* Read the address map data from the mapped index, and use it to
2649 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2650
9291a0cd 2651static void
ed2dc618
SM
2652create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2653 struct mapped_index *index)
9291a0cd 2654{
ed2dc618 2655 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2656 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2657 const gdb_byte *iter, *end;
9291a0cd 2658 struct addrmap *mutable_map;
9291a0cd
TT
2659 CORE_ADDR baseaddr;
2660
8268c778
PA
2661 auto_obstack temp_obstack;
2662
9291a0cd
TT
2663 mutable_map = addrmap_create_mutable (&temp_obstack);
2664
f00a2de2
PA
2665 iter = index->address_table.data ();
2666 end = iter + index->address_table.size ();
9291a0cd 2667
b3b3bada 2668 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2669
2670 while (iter < end)
2671 {
2672 ULONGEST hi, lo, cu_index;
2673 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2674 iter += 8;
2675 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2678 iter += 4;
f652bce2 2679
24a55014 2680 if (lo > hi)
f652bce2 2681 {
b98664d3 2682 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2683 hex_string (lo), hex_string (hi));
24a55014 2684 continue;
f652bce2 2685 }
24a55014 2686
5989a64e 2687 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2688 {
b98664d3 2689 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2690 (unsigned) cu_index);
24a55014 2691 continue;
f652bce2 2692 }
24a55014 2693
79748972
TT
2694 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2695 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2696 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2697 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2698 }
2699
d320c2b5 2700 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2701 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2702}
2703
927aa2e7
JK
2704/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2705 populate the objfile's psymtabs_addrmap. */
2706
2707static void
ed2dc618 2708create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2709 struct dwarf2_section_info *section)
2710{
ed2dc618 2711 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2712 bfd *abfd = objfile->obfd;
08feed99 2713 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2714 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2715
2716 auto_obstack temp_obstack;
2717 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2718
2719 std::unordered_map<sect_offset,
2720 dwarf2_per_cu_data *,
2721 gdb::hash_enum<sect_offset>>
2722 debug_info_offset_to_per_cu;
5989a64e 2723 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2724 {
927aa2e7
JK
2725 const auto insertpair
2726 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2727 if (!insertpair.second)
2728 {
2729 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2730 "debug_info_offset %s, ignoring .debug_aranges."),
2731 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2732 return;
2733 }
2734 }
2735
96b79293 2736 section->read (objfile);
927aa2e7
JK
2737
2738 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2739
2740 const gdb_byte *addr = section->buffer;
2741
2742 while (addr < section->buffer + section->size)
2743 {
2744 const gdb_byte *const entry_addr = addr;
2745 unsigned int bytes_read;
2746
2747 const LONGEST entry_length = read_initial_length (abfd, addr,
2748 &bytes_read);
2749 addr += bytes_read;
2750
2751 const gdb_byte *const entry_end = addr + entry_length;
2752 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2753 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2754 if (addr + entry_length > section->buffer + section->size)
2755 {
47e3f474 2756 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2757 "length %s exceeds section length %s, "
2758 "ignoring .debug_aranges."),
47e3f474
TV
2759 objfile_name (objfile),
2760 plongest (entry_addr - section->buffer),
927aa2e7
JK
2761 plongest (bytes_read + entry_length),
2762 pulongest (section->size));
2763 return;
2764 }
2765
2766 /* The version number. */
2767 const uint16_t version = read_2_bytes (abfd, addr);
2768 addr += 2;
2769 if (version != 2)
2770 {
47e3f474 2771 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2772 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2773 objfile_name (objfile),
2774 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2775 return;
2776 }
2777
2778 const uint64_t debug_info_offset
2779 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2780 addr += offset_size;
2781 const auto per_cu_it
2782 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2783 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2784 {
47e3f474 2785 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2786 "debug_info_offset %s does not exists, "
2787 "ignoring .debug_aranges."),
47e3f474
TV
2788 objfile_name (objfile),
2789 plongest (entry_addr - section->buffer),
927aa2e7
JK
2790 pulongest (debug_info_offset));
2791 return;
2792 }
2793 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2794
2795 const uint8_t address_size = *addr++;
2796 if (address_size < 1 || address_size > 8)
2797 {
47e3f474 2798 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2799 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2800 objfile_name (objfile),
2801 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2802 return;
2803 }
2804
2805 const uint8_t segment_selector_size = *addr++;
2806 if (segment_selector_size != 0)
2807 {
47e3f474 2808 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2809 "segment_selector_size %u is not supported, "
2810 "ignoring .debug_aranges."),
47e3f474
TV
2811 objfile_name (objfile),
2812 plongest (entry_addr - section->buffer),
927aa2e7
JK
2813 segment_selector_size);
2814 return;
2815 }
2816
2817 /* Must pad to an alignment boundary that is twice the address
2818 size. It is undocumented by the DWARF standard but GCC does
2819 use it. */
2820 for (size_t padding = ((-(addr - section->buffer))
2821 & (2 * address_size - 1));
2822 padding > 0; padding--)
2823 if (*addr++ != 0)
2824 {
47e3f474 2825 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2826 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2827 objfile_name (objfile),
2828 plongest (entry_addr - section->buffer));
927aa2e7
JK
2829 return;
2830 }
2831
2832 for (;;)
2833 {
2834 if (addr + 2 * address_size > entry_end)
2835 {
47e3f474 2836 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2837 "address list is not properly terminated, "
2838 "ignoring .debug_aranges."),
47e3f474
TV
2839 objfile_name (objfile),
2840 plongest (entry_addr - section->buffer));
927aa2e7
JK
2841 return;
2842 }
2843 ULONGEST start = extract_unsigned_integer (addr, address_size,
2844 dwarf5_byte_order);
2845 addr += address_size;
2846 ULONGEST length = extract_unsigned_integer (addr, address_size,
2847 dwarf5_byte_order);
2848 addr += address_size;
2849 if (start == 0 && length == 0)
2850 break;
5989a64e 2851 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2852 {
2853 /* Symbol was eliminated due to a COMDAT group. */
2854 continue;
2855 }
2856 ULONGEST end = start + length;
79748972
TT
2857 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2858 - baseaddr);
2859 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2860 - baseaddr);
927aa2e7
JK
2861 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2862 }
2863 }
2864
d320c2b5 2865 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2866 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2867}
2868
9291a0cd
TT
2869/* Find a slot in the mapped index INDEX for the object named NAME.
2870 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2871 constant pool and return true. If NAME cannot be found, return
2872 false. */
2fdf6df6 2873
109483d9 2874static bool
9291a0cd
TT
2875find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2876 offset_type **vec_out)
2877{
0cf03b49 2878 offset_type hash;
9291a0cd 2879 offset_type slot, step;
559a7a62 2880 int (*cmp) (const char *, const char *);
9291a0cd 2881
791afaa2 2882 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2883 if (current_language->la_language == language_cplus
45280282
IB
2884 || current_language->la_language == language_fortran
2885 || current_language->la_language == language_d)
0cf03b49
JK
2886 {
2887 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2888 not contain any. */
a8719064 2889
72998fb3 2890 if (strchr (name, '(') != NULL)
0cf03b49 2891 {
109483d9 2892 without_params = cp_remove_params (name);
0cf03b49 2893
72998fb3 2894 if (without_params != NULL)
791afaa2 2895 name = without_params.get ();
0cf03b49
JK
2896 }
2897 }
2898
559a7a62 2899 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2900 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2901 simulate our NAME being searched is also lowercased. */
2902 hash = mapped_index_string_hash ((index->version == 4
2903 && case_sensitivity == case_sensitive_off
2904 ? 5 : index->version),
2905 name);
2906
f00a2de2
PA
2907 slot = hash & (index->symbol_table.size () - 1);
2908 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2909 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2910
2911 for (;;)
2912 {
9291a0cd 2913 const char *str;
f00a2de2
PA
2914
2915 const auto &bucket = index->symbol_table[slot];
2916 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2917 return false;
9291a0cd 2918
f00a2de2 2919 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2920 if (!cmp (name, str))
9291a0cd
TT
2921 {
2922 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2923 + MAYBE_SWAP (bucket.vec));
109483d9 2924 return true;
9291a0cd
TT
2925 }
2926
f00a2de2 2927 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2928 }
2929}
2930
4485a1c1
SM
2931/* A helper function that reads the .gdb_index from BUFFER and fills
2932 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2933 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2934 ok to use deprecated sections.
2935
2936 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2937 out parameters that are filled in with information about the CU and
2938 TU lists in the section.
2939
4485a1c1 2940 Returns true if all went well, false otherwise. */
2fdf6df6 2941
d33bc52e 2942static bool
3810f182 2943read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2944 bool deprecated_ok,
2945 gdb::array_view<const gdb_byte> buffer,
2946 struct mapped_index *map,
2947 const gdb_byte **cu_list,
2948 offset_type *cu_list_elements,
2949 const gdb_byte **types_list,
2950 offset_type *types_list_elements)
2951{
2952 const gdb_byte *addr = &buffer[0];
82430852 2953
9291a0cd 2954 /* Version check. */
4485a1c1 2955 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2956 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2957 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2958 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2959 indices. */
831adc1f 2960 if (version < 4)
481860b3
GB
2961 {
2962 static int warning_printed = 0;
2963 if (!warning_printed)
2964 {
2965 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2966 filename);
481860b3
GB
2967 warning_printed = 1;
2968 }
2969 return 0;
2970 }
2971 /* Index version 4 uses a different hash function than index version
2972 5 and later.
2973
2974 Versions earlier than 6 did not emit psymbols for inlined
2975 functions. Using these files will cause GDB not to be able to
2976 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2977 indices unless the user has done
2978 "set use-deprecated-index-sections on". */
2ec9a5e0 2979 if (version < 6 && !deprecated_ok)
481860b3
GB
2980 {
2981 static int warning_printed = 0;
2982 if (!warning_printed)
2983 {
e615022a
DE
2984 warning (_("\
2985Skipping deprecated .gdb_index section in %s.\n\
2986Do \"set use-deprecated-index-sections on\" before the file is read\n\
2987to use the section anyway."),
2ec9a5e0 2988 filename);
481860b3
GB
2989 warning_printed = 1;
2990 }
2991 return 0;
2992 }
796a7ff8 2993 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2994 of the TU (for symbols coming from TUs),
2995 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2996 Plus gold-generated indices can have duplicate entries for global symbols,
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2998 These are just performance bugs, and we can't distinguish gdb-generated
2999 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3000
481860b3 3001 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3002 longer backward compatible. */
796a7ff8 3003 if (version > 8)
594e8718 3004 return 0;
9291a0cd 3005
559a7a62 3006 map->version = version;
9291a0cd 3007
4485a1c1 3008 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3009
4485a1c1 3010 int i = 0;
2ec9a5e0
TT
3011 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3012 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3013 / 8);
1fd400ff
TT
3014 ++i;
3015
2ec9a5e0
TT
3016 *types_list = addr + MAYBE_SWAP (metadata[i]);
3017 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3018 - MAYBE_SWAP (metadata[i]))
3019 / 8);
987d643c 3020 ++i;
1fd400ff 3021
f00a2de2
PA
3022 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3023 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3024 map->address_table
3025 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3026 ++i;
3027
f00a2de2
PA
3028 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3029 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3030 map->symbol_table
3031 = gdb::array_view<mapped_index::symbol_table_slot>
3032 ((mapped_index::symbol_table_slot *) symbol_table,
3033 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3034
f00a2de2 3035 ++i;
f9d83a0b 3036 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3037
2ec9a5e0
TT
3038 return 1;
3039}
3040
4485a1c1
SM
3041/* Callback types for dwarf2_read_gdb_index. */
3042
3043typedef gdb::function_view
5989a64e 3044 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3045 get_gdb_index_contents_ftype;
3046typedef gdb::function_view
3047 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3048 get_gdb_index_contents_dwz_ftype;
3049
927aa2e7 3050/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3051 elements of all the CUs and return 1. Otherwise, return 0. */
3052
3053static int
4485a1c1
SM
3054dwarf2_read_gdb_index
3055 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3056 get_gdb_index_contents_ftype get_gdb_index_contents,
3057 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3058{
2ec9a5e0
TT
3059 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3060 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3061 struct dwz_file *dwz;
12359b5e 3062 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3063
4485a1c1 3064 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3065 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3066
3067 if (main_index_contents.empty ())
3068 return 0;
3069
3063847f 3070 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3071 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3072 use_deprecated_index_sections,
3073 main_index_contents, map.get (), &cu_list,
3074 &cu_list_elements, &types_list,
3075 &types_list_elements))
2ec9a5e0
TT
3076 return 0;
3077
0fefef59 3078 /* Don't use the index if it's empty. */
3063847f 3079 if (map->symbol_table.empty ())
0fefef59
DE
3080 return 0;
3081
2ec9a5e0
TT
3082 /* If there is a .dwz file, read it so we can get its CU list as
3083 well. */
c3699833 3084 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3085 if (dwz != NULL)
2ec9a5e0 3086 {
2ec9a5e0
TT
3087 struct mapped_index dwz_map;
3088 const gdb_byte *dwz_types_ignore;
3089 offset_type dwz_types_elements_ignore;
3090
4485a1c1
SM
3091 gdb::array_view<const gdb_byte> dwz_index_content
3092 = get_gdb_index_contents_dwz (objfile, dwz);
3093
3094 if (dwz_index_content.empty ())
3095 return 0;
3096
3810f182 3097 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3098 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3099 &dwz_list, &dwz_list_elements,
3100 &dwz_types_ignore,
3101 &dwz_types_elements_ignore))
2ec9a5e0
TT
3102 {
3103 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3104 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3105 return 0;
3106 }
3107 }
3108
12359b5e
SM
3109 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3110 dwz_list, dwz_list_elements);
1fd400ff 3111
8b70b953
TT
3112 if (types_list_elements)
3113 {
8b70b953
TT
3114 /* We can only handle a single .debug_types when we have an
3115 index. */
5989a64e 3116 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3117 return 0;
3118
5989a64e 3119 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3120
12359b5e
SM
3121 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3122 types_list, types_list_elements);
8b70b953 3123 }
9291a0cd 3124
3063847f 3125 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3126
5989a64e
SM
3127 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3128 dwarf2_per_objfile->per_bfd->using_index = 1;
3129 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3130 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3131
3132 return 1;
3133}
3134
dee91e82 3135/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3136
dee91e82
DE
3137static void
3138dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3139 const gdb_byte *info_ptr,
3e225074 3140 struct die_info *comp_unit_die)
9291a0cd 3141{
dee91e82 3142 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3143 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3144 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3145 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3146 struct attribute *attr;
7b9f3c50
DE
3147 void **slot;
3148 struct quick_file_names *qfn;
9291a0cd 3149
0186c6a7
DE
3150 gdb_assert (! this_cu->is_debug_types);
3151
07261596
TT
3152 /* Our callers never want to match partial units -- instead they
3153 will match the enclosing full CU. */
3154 if (comp_unit_die->tag == DW_TAG_partial_unit)
3155 {
3156 this_cu->v.quick->no_file_data = 1;
3157 return;
3158 }
3159
0186c6a7 3160 lh_cu = this_cu;
7b9f3c50 3161 slot = NULL;
dee91e82 3162
fff8551c 3163 line_header_up lh;
9c541725 3164 sect_offset line_offset {};
fff8551c 3165
dee91e82 3166 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3167 if (attr != nullptr)
9291a0cd 3168 {
7b9f3c50
DE
3169 struct quick_file_names find_entry;
3170
9c541725 3171 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3172
3173 /* We may have already read in this line header (TU line header sharing).
3174 If we have we're done. */
094b34ac 3175 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3176 find_entry.hash.line_sect_off = line_offset;
5989a64e 3177 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3178 &find_entry, INSERT);
3179 if (*slot != NULL)
3180 {
9a3c8263 3181 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3182 return;
7b9f3c50
DE
3183 }
3184
3019eac3 3185 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3186 }
3187 if (lh == NULL)
3188 {
094b34ac 3189 lh_cu->v.quick->no_file_data = 1;
dee91e82 3190 return;
9291a0cd
TT
3191 }
3192
5989a64e 3193 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3194 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3195 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3196 gdb_assert (slot != NULL);
3197 *slot = qfn;
9291a0cd 3198
d721ba37 3199 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3200
aa391654
TT
3201 int offset = 0;
3202 if (strcmp (fnd.name, "<unknown>") != 0)
3203 ++offset;
3204
7ba99d21 3205 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3206 qfn->file_names =
5989a64e 3207 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3208 qfn->num_file_names);
aa391654
TT
3209 if (offset != 0)
3210 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3211 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3212 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3213 fnd.comp_dir).release ();
7b9f3c50 3214 qfn->real_names = NULL;
9291a0cd 3215
094b34ac 3216 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3217}
3218
3219/* A helper for the "quick" functions which attempts to read the line
3220 table for THIS_CU. */
3221
3222static struct quick_file_names *
ab432490
SM
3223dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3224 dwarf2_per_objfile *per_objfile)
dee91e82 3225{
0186c6a7
DE
3226 /* This should never be called for TUs. */
3227 gdb_assert (! this_cu->is_debug_types);
3228 /* Nor type unit groups. */
197400e8 3229 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3230
dee91e82
DE
3231 if (this_cu->v.quick->file_names != NULL)
3232 return this_cu->v.quick->file_names;
3233 /* If we know there is no line data, no point in looking again. */
3234 if (this_cu->v.quick->no_file_data)
3235 return NULL;
3236
ab432490 3237 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3238 if (!reader.dummy_p)
3e225074 3239 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3240
3241 if (this_cu->v.quick->no_file_data)
3242 return NULL;
3243 return this_cu->v.quick->file_names;
9291a0cd
TT
3244}
3245
3246/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3247 real path for a given file name from the line table. */
2fdf6df6 3248
9291a0cd 3249static const char *
45940949 3250dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3251 struct quick_file_names *qfn, int index)
9291a0cd 3252{
7b9f3c50 3253 if (qfn->real_names == NULL)
5989a64e 3254 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3255 qfn->num_file_names, const char *);
9291a0cd 3256
7b9f3c50 3257 if (qfn->real_names[index] == NULL)
14278e1f 3258 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3259
7b9f3c50 3260 return qfn->real_names[index];
9291a0cd
TT
3261}
3262
3263static struct symtab *
3264dw2_find_last_source_symtab (struct objfile *objfile)
3265{
ed2dc618
SM
3266 struct dwarf2_per_objfile *dwarf2_per_objfile
3267 = get_dwarf2_per_objfile (objfile);
5989a64e 3268 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3269 compunit_symtab *cust
3270 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3271
43f3e411
DE
3272 if (cust == NULL)
3273 return NULL;
ed2dc618 3274
43f3e411 3275 return compunit_primary_filetab (cust);
9291a0cd
TT
3276}
3277
7b9f3c50
DE
3278/* Traversal function for dw2_forget_cached_source_info. */
3279
3280static int
3281dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3282{
7b9f3c50 3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3284
7b9f3c50 3285 if (file_data->real_names)
9291a0cd 3286 {
7b9f3c50 3287 int i;
9291a0cd 3288
7b9f3c50 3289 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3290 {
7b9f3c50
DE
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
9291a0cd
TT
3293 }
3294 }
7b9f3c50
DE
3295
3296 return 1;
3297}
3298
3299static void
3300dw2_forget_cached_source_info (struct objfile *objfile)
3301{
ed2dc618
SM
3302 struct dwarf2_per_objfile *dwarf2_per_objfile
3303 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3304
5989a64e 3305 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3306 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3307}
3308
f8eba3c6
TT
3309/* Helper function for dw2_map_symtabs_matching_filename that expands
3310 the symtabs and calls the iterator. */
3311
3312static int
3313dw2_map_expand_apply (struct objfile *objfile,
3314 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3315 const char *name, const char *real_path,
14bc53a8 3316 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3317{
43f3e411 3318 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3319
3320 /* Don't visit already-expanded CUs. */
af758d11
SM
3321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3322 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3323 return 0;
3324
3325 /* This may expand more than one symtab, and we want to iterate over
3326 all of them. */
97a1449a 3327 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3328
14bc53a8
PA
3329 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3330 last_made, callback);
f8eba3c6
TT
3331}
3332
3333/* Implementation of the map_symtabs_matching_filename method. */
3334
14bc53a8
PA
3335static bool
3336dw2_map_symtabs_matching_filename
3337 (struct objfile *objfile, const char *name, const char *real_path,
3338 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3339{
c011a4f4 3340 const char *name_basename = lbasename (name);
ed2dc618
SM
3341 struct dwarf2_per_objfile *dwarf2_per_objfile
3342 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3343
848e3e78
DE
3344 /* The rule is CUs specify all the files, including those used by
3345 any TU, so there's no need to scan TUs here. */
f4dc4d17 3346
5989a64e 3347 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3348 {
3d7bb9d9 3349 /* We only need to look at symtabs not already expanded. */
af758d11 3350 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3351 continue;
3352
ab432490
SM
3353 quick_file_names *file_data
3354 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3355 if (file_data == NULL)
9291a0cd
TT
3356 continue;
3357
b76e467d 3358 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3359 {
7b9f3c50 3360 const char *this_name = file_data->file_names[j];
da235a7c 3361 const char *this_real_name;
9291a0cd 3362
af529f8f 3363 if (compare_filenames_for_search (this_name, name))
9291a0cd 3364 {
f5b95b50 3365 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3366 callback))
3367 return true;
288e77a7 3368 continue;
4aac40c8 3369 }
9291a0cd 3370
c011a4f4
DE
3371 /* Before we invoke realpath, which can get expensive when many
3372 files are involved, do a quick comparison of the basenames. */
3373 if (! basenames_may_differ
3374 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3375 continue;
3376
45940949
TT
3377 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3378 file_data, j);
da235a7c 3379 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3380 {
da235a7c 3381 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3382 callback))
3383 return true;
288e77a7 3384 continue;
da235a7c 3385 }
9291a0cd 3386
da235a7c
JK
3387 if (real_path != NULL)
3388 {
af529f8f
JK
3389 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3390 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3391 if (this_real_name != NULL
af529f8f 3392 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3393 {
f5b95b50 3394 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3395 callback))
3396 return true;
288e77a7 3397 continue;
9291a0cd
TT
3398 }
3399 }
3400 }
3401 }
3402
14bc53a8 3403 return false;
9291a0cd
TT
3404}
3405
da51c347
DE
3406/* Struct used to manage iterating over all CUs looking for a symbol. */
3407
3408struct dw2_symtab_iterator
9291a0cd 3409{
ed2dc618
SM
3410 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3411 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3412 /* If set, only look for symbols that match that block. Valid values are
3413 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3414 gdb::optional<block_enum> block_index;
da51c347
DE
3415 /* The kind of symbol we're looking for. */
3416 domain_enum domain;
3417 /* The list of CUs from the index entry of the symbol,
3418 or NULL if not found. */
3419 offset_type *vec;
3420 /* The next element in VEC to look at. */
3421 int next;
3422 /* The number of elements in VEC, or zero if there is no match. */
3423 int length;
8943b874
DE
3424 /* Have we seen a global version of the symbol?
3425 If so we can ignore all further global instances.
3426 This is to work around gold/15646, inefficient gold-generated
3427 indices. */
3428 int global_seen;
da51c347 3429};
9291a0cd 3430
2b79f376 3431/* Initialize the index symtab iterator ITER. */
2fdf6df6 3432
9291a0cd 3433static void
da51c347 3434dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3435 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3436 gdb::optional<block_enum> block_index,
da51c347
DE
3437 domain_enum domain,
3438 const char *name)
3439{
ed2dc618 3440 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3441 iter->block_index = block_index;
3442 iter->domain = domain;
3443 iter->next = 0;
8943b874 3444 iter->global_seen = 0;
da51c347 3445
5989a64e 3446 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3447
3448 /* index is NULL if OBJF_READNOW. */
3449 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3450 iter->length = MAYBE_SWAP (*iter->vec);
3451 else
3452 {
3453 iter->vec = NULL;
3454 iter->length = 0;
3455 }
3456}
3457
3458/* Return the next matching CU or NULL if there are no more. */
3459
3460static struct dwarf2_per_cu_data *
3461dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3462{
ed2dc618
SM
3463 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3464
da51c347
DE
3465 for ( ; iter->next < iter->length; ++iter->next)
3466 {
3467 offset_type cu_index_and_attrs =
3468 MAYBE_SWAP (iter->vec[iter->next + 1]);
3469 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3470 gdb_index_symbol_kind symbol_kind =
3471 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3472 /* Only check the symbol attributes if they're present.
3473 Indices prior to version 7 don't record them,
3474 and indices >= 7 may elide them for certain symbols
3475 (gold does this). */
3476 int attrs_valid =
5989a64e 3477 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3478 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3479
3190f0c6 3480 /* Don't crash on bad data. */
5989a64e
SM
3481 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3482 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3483 {
b98664d3 3484 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3485 " [in module %s]"),
3486 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3487 continue;
3488 }
3489
5989a64e 3490 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3491
da51c347 3492 /* Skip if already read in. */
af758d11 3493 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3494 continue;
3495
8943b874
DE
3496 /* Check static vs global. */
3497 if (attrs_valid)
3498 {
2b79f376
SM
3499 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3500
3501 if (iter->block_index.has_value ())
3502 {
3503 bool want_static = *iter->block_index == STATIC_BLOCK;
3504
3505 if (is_static != want_static)
3506 continue;
3507 }
3508
8943b874
DE
3509 /* Work around gold/15646. */
3510 if (!is_static && iter->global_seen)
3511 continue;
3512 if (!is_static)
3513 iter->global_seen = 1;
3514 }
da51c347
DE
3515
3516 /* Only check the symbol's kind if it has one. */
3517 if (attrs_valid)
3518 {
3519 switch (iter->domain)
3520 {
3521 case VAR_DOMAIN:
3522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3523 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3524 /* Some types are also in VAR_DOMAIN. */
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3526 continue;
3527 break;
3528 case STRUCT_DOMAIN:
3529 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3530 continue;
3531 break;
3532 case LABEL_DOMAIN:
3533 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3534 continue;
3535 break;
59c35742
AB
3536 case MODULE_DOMAIN:
3537 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3538 continue;
3539 break;
da51c347
DE
3540 default:
3541 break;
3542 }
3543 }
3544
3545 ++iter->next;
3546 return per_cu;
3547 }
3548
3549 return NULL;
3550}
3551
43f3e411 3552static struct compunit_symtab *
c7f839cb 3553dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3554 const char *name, domain_enum domain)
9291a0cd 3555{
43f3e411 3556 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3557 struct dwarf2_per_objfile *dwarf2_per_objfile
3558 = get_dwarf2_per_objfile (objfile);
9291a0cd 3559
b5ec771e
PA
3560 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3561
ed2dc618
SM
3562 struct dw2_symtab_iterator iter;
3563 struct dwarf2_per_cu_data *per_cu;
da51c347 3564
2b79f376 3565 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3566
ed2dc618
SM
3567 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3568 {
3569 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3570 struct compunit_symtab *stab
3571 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3572 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3573 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3574
ed2dc618
SM
3575 sym = block_find_symbol (block, name, domain,
3576 block_find_non_opaque_type_preferred,
3577 &with_opaque);
b2e2f908 3578
ed2dc618
SM
3579 /* Some caution must be observed with overloaded functions
3580 and methods, since the index will not contain any overload
3581 information (but NAME might contain it). */
da51c347 3582
ed2dc618
SM
3583 if (sym != NULL
3584 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3585 return stab;
3586 if (with_opaque != NULL
3587 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3588 stab_best = stab;
da51c347 3589
ed2dc618 3590 /* Keep looking through other CUs. */
9291a0cd 3591 }
9291a0cd 3592
da51c347 3593 return stab_best;
9291a0cd
TT
3594}
3595
3596static void
3597dw2_print_stats (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3601 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3602 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3603 int count = 0;
9291a0cd 3604
ed2dc618 3605 for (int i = 0; i < total; ++i)
9291a0cd 3606 {
5989a64e 3607 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3608
af758d11 3609 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3610 ++count;
3611 }
e4a48d9d 3612 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3613 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3614}
3615
779bd270
DE
3616/* This dumps minimal information about the index.
3617 It is called via "mt print objfiles".
3618 One use is to verify .gdb_index has been loaded by the
3619 gdb.dwarf2/gdb-index.exp testcase. */
3620
9291a0cd
TT
3621static void
3622dw2_dump (struct objfile *objfile)
3623{
ed2dc618
SM
3624 struct dwarf2_per_objfile *dwarf2_per_objfile
3625 = get_dwarf2_per_objfile (objfile);
3626
5989a64e 3627 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3628 printf_filtered (".gdb_index:");
5989a64e 3629 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3630 {
3631 printf_filtered (" version %d\n",
5989a64e 3632 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3633 }
3634 else
3635 printf_filtered (" faked for \"readnow\"\n");
3636 printf_filtered ("\n");
9291a0cd
TT
3637}
3638
9291a0cd
TT
3639static void
3640dw2_expand_symtabs_for_function (struct objfile *objfile,
3641 const char *func_name)
3642{
ed2dc618
SM
3643 struct dwarf2_per_objfile *dwarf2_per_objfile
3644 = get_dwarf2_per_objfile (objfile);
da51c347 3645
ed2dc618
SM
3646 struct dw2_symtab_iterator iter;
3647 struct dwarf2_per_cu_data *per_cu;
da51c347 3648
2b79f376 3649 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3650
ed2dc618 3651 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3652 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3653
9291a0cd
TT
3654}
3655
3656static void
3657dw2_expand_all_symtabs (struct objfile *objfile)
3658{
ed2dc618
SM
3659 struct dwarf2_per_objfile *dwarf2_per_objfile
3660 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3661 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3662 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3663
ed2dc618 3664 for (int i = 0; i < total_units; ++i)
9291a0cd 3665 {
5989a64e 3666 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3667
58f0c718
TT
3668 /* We don't want to directly expand a partial CU, because if we
3669 read it with the wrong language, then assertion failures can
3670 be triggered later on. See PR symtab/23010. So, tell
3671 dw2_instantiate_symtab to skip partial CUs -- any important
3672 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3673 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3674 }
3675}
3676
3677static void
652a8996
JK
3678dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3679 const char *fullname)
9291a0cd 3680{
ed2dc618
SM
3681 struct dwarf2_per_objfile *dwarf2_per_objfile
3682 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3683
3684 /* We don't need to consider type units here.
3685 This is only called for examining code, e.g. expand_line_sal.
3686 There can be an order of magnitude (or more) more type units
3687 than comp units, and we avoid them if we can. */
3688
5989a64e 3689 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3690 {
3d7bb9d9 3691 /* We only need to look at symtabs not already expanded. */
af758d11 3692 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3693 continue;
3694
ab432490
SM
3695 quick_file_names *file_data
3696 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3697 if (file_data == NULL)
9291a0cd
TT
3698 continue;
3699
b76e467d 3700 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3701 {
652a8996
JK
3702 const char *this_fullname = file_data->file_names[j];
3703
3704 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3705 {
97a1449a 3706 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3707 break;
3708 }
3709 }
3710 }
3711}
3712
9a0bacfb
TV
3713static void
3714dw2_expand_symtabs_matching_symbol
3715 (mapped_index_base &index,
3716 const lookup_name_info &lookup_name_in,
3717 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3718 enum search_domain kind,
3719 gdb::function_view<bool (offset_type)> match_callback);
3720
3721static void
3722dw2_expand_symtabs_matching_one
97a1449a
SM
3723 (dwarf2_per_cu_data *per_cu,
3724 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3725 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3726 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3727
9291a0cd 3728static void
199b4314
TT
3729dw2_map_matching_symbols
3730 (struct objfile *objfile,
b054970d 3731 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3732 int global,
3733 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3734 symbol_compare_ftype *ordered_compare)
9291a0cd 3735{
1aa98955
TV
3736 /* Used for Ada. */
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
3739
9a0bacfb
TV
3740 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3741
5989a64e 3742 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3743 {
3744 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3745 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3746 using GNU index. */
5989a64e 3747 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3748
9a0bacfb
TV
3749 const char *match_name = name.ada ().lookup_name ().c_str ();
3750 auto matcher = [&] (const char *symname)
3751 {
3752 if (ordered_compare == nullptr)
3753 return true;
3754 return ordered_compare (symname, match_name) == 0;
3755 };
3756
3757 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3758 [&] (offset_type namei)
3759 {
3760 struct dw2_symtab_iterator iter;
3761 struct dwarf2_per_cu_data *per_cu;
3762
3763 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3764 match_name);
3765 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3766 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3767 nullptr);
9a0bacfb
TV
3768 return true;
3769 });
3770 }
3771 else
3772 {
3773 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3774 proceed assuming all symtabs have been read in. */
3775 }
1aa98955
TV
3776
3777 for (compunit_symtab *cust : objfile->compunits ())
3778 {
3779 const struct block *block;
3780
3781 if (cust == NULL)
3782 continue;
3783 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3784 if (!iterate_over_symbols_terminated (block, name,
3785 domain, callback))
3786 return;
3787 }
9291a0cd
TT
3788}
3789
e1ef7d7a
PA
3790/* Starting from a search name, return the string that finds the upper
3791 bound of all strings that start with SEARCH_NAME in a sorted name
3792 list. Returns the empty string to indicate that the upper bound is
3793 the end of the list. */
3794
3795static std::string
3796make_sort_after_prefix_name (const char *search_name)
3797{
3798 /* When looking to complete "func", we find the upper bound of all
3799 symbols that start with "func" by looking for where we'd insert
3800 the closest string that would follow "func" in lexicographical
3801 order. Usually, that's "func"-with-last-character-incremented,
3802 i.e. "fund". Mind non-ASCII characters, though. Usually those
3803 will be UTF-8 multi-byte sequences, but we can't be certain.
3804 Especially mind the 0xff character, which is a valid character in
3805 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3806 rule out compilers allowing it in identifiers. Note that
3807 conveniently, strcmp/strcasecmp are specified to compare
3808 characters interpreted as unsigned char. So what we do is treat
3809 the whole string as a base 256 number composed of a sequence of
3810 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3811 to 0, and carries 1 to the following more-significant position.
3812 If the very first character in SEARCH_NAME ends up incremented
3813 and carries/overflows, then the upper bound is the end of the
3814 list. The string after the empty string is also the empty
3815 string.
3816
3817 Some examples of this operation:
3818
3819 SEARCH_NAME => "+1" RESULT
3820
3821 "abc" => "abd"
3822 "ab\xff" => "ac"
3823 "\xff" "a" "\xff" => "\xff" "b"
3824 "\xff" => ""
3825 "\xff\xff" => ""
3826 "" => ""
3827
3828 Then, with these symbols for example:
3829
3830 func
3831 func1
3832 fund
3833
3834 completing "func" looks for symbols between "func" and
3835 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3836 which finds "func" and "func1", but not "fund".
3837
3838 And with:
3839
3840 funcÿ (Latin1 'ÿ' [0xff])
3841 funcÿ1
3842 fund
3843
3844 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3845 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3846
3847 And with:
3848
3849 ÿÿ (Latin1 'ÿ' [0xff])
3850 ÿÿ1
3851
3852 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3853 the end of the list.
3854 */
3855 std::string after = search_name;
3856 while (!after.empty () && (unsigned char) after.back () == 0xff)
3857 after.pop_back ();
3858 if (!after.empty ())
3859 after.back () = (unsigned char) after.back () + 1;
3860 return after;
3861}
3862
5c58de74 3863/* See declaration. */
61d96d7e 3864
5c58de74
PA
3865std::pair<std::vector<name_component>::const_iterator,
3866 std::vector<name_component>::const_iterator>
44ed8f3e 3867mapped_index_base::find_name_components_bounds
3b00ef10 3868 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3869{
5c58de74
PA
3870 auto *name_cmp
3871 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3872
3b00ef10 3873 const char *lang_name
e0802d59 3874 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3875
3f563c84
PA
3876 /* Comparison function object for lower_bound that matches against a
3877 given symbol name. */
3878 auto lookup_compare_lower = [&] (const name_component &elem,
3879 const char *name)
3880 {
5c58de74 3881 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3882 const char *elem_name = elem_qualified + elem.name_offset;
3883 return name_cmp (elem_name, name) < 0;
3884 };
3885
3886 /* Comparison function object for upper_bound that matches against a
3887 given symbol name. */
3888 auto lookup_compare_upper = [&] (const char *name,
3889 const name_component &elem)
3890 {
5c58de74 3891 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3892 const char *elem_name = elem_qualified + elem.name_offset;
3893 return name_cmp (name, elem_name) < 0;
3894 };
3895
5c58de74
PA
3896 auto begin = this->name_components.begin ();
3897 auto end = this->name_components.end ();
3f563c84
PA
3898
3899 /* Find the lower bound. */
3900 auto lower = [&] ()
3901 {
3b00ef10 3902 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3903 return begin;
3904 else
3b00ef10 3905 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3906 } ();
3907
3908 /* Find the upper bound. */
3909 auto upper = [&] ()
3910 {
5c58de74 3911 if (lookup_name_without_params.completion_mode ())
3f563c84 3912 {
e1ef7d7a
PA
3913 /* In completion mode, we want UPPER to point past all
3914 symbols names that have the same prefix. I.e., with
3915 these symbols, and completing "func":
3916
3917 function << lower bound
3918 function1
3919 other_function << upper bound
3920
3921 We find the upper bound by looking for the insertion
3922 point of "func"-with-last-character-incremented,
3923 i.e. "fund". */
3b00ef10 3924 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3925 if (after.empty ())
3f563c84 3926 return end;
e6b2f5ef
PA
3927 return std::lower_bound (lower, end, after.c_str (),
3928 lookup_compare_lower);
3f563c84
PA
3929 }
3930 else
3b00ef10 3931 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3932 } ();
3933
5c58de74
PA
3934 return {lower, upper};
3935}
3936
3937/* See declaration. */
3938
3939void
44ed8f3e 3940mapped_index_base::build_name_components ()
5c58de74
PA
3941{
3942 if (!this->name_components.empty ())
3943 return;
3944
3945 this->name_components_casing = case_sensitivity;
3946 auto *name_cmp
3947 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3948
3949 /* The code below only knows how to break apart components of C++
3950 symbol names (and other languages that use '::' as
3b00ef10 3951 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3952 auto count = this->symbol_name_count ();
3953 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3954 {
44ed8f3e 3955 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3956 continue;
3957
3958 const char *name = this->symbol_name_at (idx);
3959
3960 /* Add each name component to the name component table. */
3961 unsigned int previous_len = 0;
3b00ef10
TT
3962
3963 if (strstr (name, "::") != nullptr)
3964 {
3965 for (unsigned int current_len = cp_find_first_component (name);
3966 name[current_len] != '\0';
3967 current_len += cp_find_first_component (name + current_len))
3968 {
3969 gdb_assert (name[current_len] == ':');
3970 this->name_components.push_back ({previous_len, idx});
3971 /* Skip the '::'. */
3972 current_len += 2;
3973 previous_len = current_len;
3974 }
3975 }
3976 else
5c58de74 3977 {
3b00ef10
TT
3978 /* Handle the Ada encoded (aka mangled) form here. */
3979 for (const char *iter = strstr (name, "__");
3980 iter != nullptr;
3981 iter = strstr (iter, "__"))
3982 {
3983 this->name_components.push_back ({previous_len, idx});
3984 iter += 2;
3985 previous_len = iter - name;
3986 }
5c58de74 3987 }
3b00ef10 3988
5c58de74
PA
3989 this->name_components.push_back ({previous_len, idx});
3990 }
3991
3992 /* Sort name_components elements by name. */
3993 auto name_comp_compare = [&] (const name_component &left,
3994 const name_component &right)
3995 {
3996 const char *left_qualified = this->symbol_name_at (left.idx);
3997 const char *right_qualified = this->symbol_name_at (right.idx);
3998
3999 const char *left_name = left_qualified + left.name_offset;
4000 const char *right_name = right_qualified + right.name_offset;
4001
4002 return name_cmp (left_name, right_name) < 0;
4003 };
4004
4005 std::sort (this->name_components.begin (),
4006 this->name_components.end (),
4007 name_comp_compare);
4008}
4009
4010/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4011 mapped_index_base instead of the containing objfile. This is split
4012 to a separate function in order to be able to unit test the
4013 name_components matching using a mock mapped_index_base. For each
5c58de74 4014 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4015 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4016
4017static void
4018dw2_expand_symtabs_matching_symbol
44ed8f3e 4019 (mapped_index_base &index,
5c58de74
PA
4020 const lookup_name_info &lookup_name_in,
4021 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4022 enum search_domain kind,
3b00ef10 4023 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4024{
4025 lookup_name_info lookup_name_without_params
4026 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4027
4028 /* Build the symbol name component sorted vector, if we haven't
4029 yet. */
4030 index.build_name_components ();
4031
3f563c84
PA
4032 /* The same symbol may appear more than once in the range though.
4033 E.g., if we're looking for symbols that complete "w", and we have
4034 a symbol named "w1::w2", we'll find the two name components for
4035 that same symbol in the range. To be sure we only call the
4036 callback once per symbol, we first collect the symbol name
4037 indexes that matched in a temporary vector and ignore
4038 duplicates. */
4039 std::vector<offset_type> matches;
3f563c84 4040
3b00ef10
TT
4041 struct name_and_matcher
4042 {
4043 symbol_name_matcher_ftype *matcher;
ecc6c606 4044 const char *name;
3b00ef10
TT
4045
4046 bool operator== (const name_and_matcher &other) const
3f563c84 4047 {
ecc6c606 4048 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4049 }
4050 };
4051
4052 /* A vector holding all the different symbol name matchers, for all
4053 languages. */
4054 std::vector<name_and_matcher> matchers;
4055
4056 for (int i = 0; i < nr_languages; i++)
4057 {
4058 enum language lang_e = (enum language) i;
4059
4060 const language_defn *lang = language_def (lang_e);
4061 symbol_name_matcher_ftype *name_matcher
4062 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4063
3b00ef10
TT
4064 name_and_matcher key {
4065 name_matcher,
4066 lookup_name_without_params.language_lookup_name (lang_e)
4067 };
4068
4069 /* Don't insert the same comparison routine more than once.
4070 Note that we do this linear walk. This is not a problem in
4071 practice because the number of supported languages is
4072 low. */
4073 if (std::find (matchers.begin (), matchers.end (), key)
4074 != matchers.end ())
9291a0cd 4075 continue;
3b00ef10
TT
4076 matchers.push_back (std::move (key));
4077
4078 auto bounds
4079 = index.find_name_components_bounds (lookup_name_without_params,
4080 lang_e);
4081
4082 /* Now for each symbol name in range, check to see if we have a name
4083 match, and if so, call the MATCH_CALLBACK callback. */
4084
4085 for (; bounds.first != bounds.second; ++bounds.first)
4086 {
4087 const char *qualified = index.symbol_name_at (bounds.first->idx);
4088
4089 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4090 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4091 continue;
9291a0cd 4092
3b00ef10
TT
4093 matches.push_back (bounds.first->idx);
4094 }
3f563c84
PA
4095 }
4096
4097 std::sort (matches.begin (), matches.end ());
4098
4099 /* Finally call the callback, once per match. */
4100 ULONGEST prev = -1;
4101 for (offset_type idx : matches)
4102 {
4103 if (prev != idx)
4104 {
3b00ef10
TT
4105 if (!match_callback (idx))
4106 break;
3f563c84
PA
4107 prev = idx;
4108 }
4109 }
4110
4111 /* Above we use a type wider than idx's for 'prev', since 0 and
4112 (offset_type)-1 are both possible values. */
4113 static_assert (sizeof (prev) > sizeof (offset_type), "");
4114}
4115
c62446b1
PA
4116#if GDB_SELF_TEST
4117
4118namespace selftests { namespace dw2_expand_symtabs_matching {
4119
a3c5fafd
PA
4120/* A mock .gdb_index/.debug_names-like name index table, enough to
4121 exercise dw2_expand_symtabs_matching_symbol, which works with the
4122 mapped_index_base interface. Builds an index from the symbol list
4123 passed as parameter to the constructor. */
4124class mock_mapped_index : public mapped_index_base
c62446b1
PA
4125{
4126public:
a3c5fafd
PA
4127 mock_mapped_index (gdb::array_view<const char *> symbols)
4128 : m_symbol_table (symbols)
c62446b1
PA
4129 {}
4130
a3c5fafd 4131 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4132
a3c5fafd 4133 /* Return the number of names in the symbol table. */
632e107b 4134 size_t symbol_name_count () const override
c62446b1 4135 {
a3c5fafd 4136 return m_symbol_table.size ();
c62446b1
PA
4137 }
4138
a3c5fafd 4139 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4140 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4141 {
4142 return m_symbol_table[idx];
4143 }
c62446b1 4144
a3c5fafd
PA
4145private:
4146 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4147};
4148
4149/* Convenience function that converts a NULL pointer to a "<null>"
4150 string, to pass to print routines. */
4151
4152static const char *
4153string_or_null (const char *str)
4154{
4155 return str != NULL ? str : "<null>";
4156}
4157
4158/* Check if a lookup_name_info built from
4159 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4160 index. EXPECTED_LIST is the list of expected matches, in expected
4161 matching order. If no match expected, then an empty list is
4162 specified. Returns true on success. On failure prints a warning
4163 indicating the file:line that failed, and returns false. */
4164
4165static bool
4166check_match (const char *file, int line,
4167 mock_mapped_index &mock_index,
4168 const char *name, symbol_name_match_type match_type,
4169 bool completion_mode,
4170 std::initializer_list<const char *> expected_list)
4171{
4172 lookup_name_info lookup_name (name, match_type, completion_mode);
4173
4174 bool matched = true;
4175
4176 auto mismatch = [&] (const char *expected_str,
4177 const char *got)
4178 {
4179 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4180 "expected=\"%s\", got=\"%s\"\n"),
4181 file, line,
4182 (match_type == symbol_name_match_type::FULL
4183 ? "FULL" : "WILD"),
4184 name, string_or_null (expected_str), string_or_null (got));
4185 matched = false;
4186 };
4187
4188 auto expected_it = expected_list.begin ();
4189 auto expected_end = expected_list.end ();
4190
a3c5fafd 4191 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4192 NULL, ALL_DOMAIN,
4193 [&] (offset_type idx)
4194 {
a3c5fafd 4195 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4196 const char *expected_str
4197 = expected_it == expected_end ? NULL : *expected_it++;
4198
4199 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4200 mismatch (expected_str, matched_name);
3b00ef10 4201 return true;
c62446b1
PA
4202 });
4203
4204 const char *expected_str
4205 = expected_it == expected_end ? NULL : *expected_it++;
4206 if (expected_str != NULL)
4207 mismatch (expected_str, NULL);
4208
4209 return matched;
4210}
4211
4212/* The symbols added to the mock mapped_index for testing (in
4213 canonical form). */
4214static const char *test_symbols[] = {
4215 "function",
4216 "std::bar",
4217 "std::zfunction",
4218 "std::zfunction2",
4219 "w1::w2",
4220 "ns::foo<char*>",
4221 "ns::foo<int>",
4222 "ns::foo<long>",
a20714ff
PA
4223 "ns2::tmpl<int>::foo2",
4224 "(anonymous namespace)::A::B::C",
c62446b1 4225
e1ef7d7a
PA
4226 /* These are used to check that the increment-last-char in the
4227 matching algorithm for completion doesn't match "t1_fund" when
4228 completing "t1_func". */
4229 "t1_func",
4230 "t1_func1",
4231 "t1_fund",
4232 "t1_fund1",
4233
4234 /* A UTF-8 name with multi-byte sequences to make sure that
4235 cp-name-parser understands this as a single identifier ("função"
4236 is "function" in PT). */
4237 u8"u8função",
4238
4239 /* \377 (0xff) is Latin1 'ÿ'. */
4240 "yfunc\377",
4241
4242 /* \377 (0xff) is Latin1 'ÿ'. */
4243 "\377",
4244 "\377\377123",
4245
c62446b1
PA
4246 /* A name with all sorts of complications. Starts with "z" to make
4247 it easier for the completion tests below. */
4248#define Z_SYM_NAME \
4249 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4250 "::tuple<(anonymous namespace)::ui*, " \
4251 "std::default_delete<(anonymous namespace)::ui>, void>"
4252
4253 Z_SYM_NAME
4254};
4255
a3c5fafd
PA
4256/* Returns true if the mapped_index_base::find_name_component_bounds
4257 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4258 in completion mode. */
5c58de74
PA
4259
4260static bool
a3c5fafd 4261check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4262 const char *search_name,
4263 gdb::array_view<const char *> expected_syms)
4264{
4265 lookup_name_info lookup_name (search_name,
4266 symbol_name_match_type::FULL, true);
4267
3b00ef10
TT
4268 auto bounds = index.find_name_components_bounds (lookup_name,
4269 language_cplus);
5c58de74
PA
4270
4271 size_t distance = std::distance (bounds.first, bounds.second);
4272 if (distance != expected_syms.size ())
4273 return false;
4274
4275 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4276 {
4277 auto nc_elem = bounds.first + exp_elem;
4278 const char *qualified = index.symbol_name_at (nc_elem->idx);
4279 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4280 return false;
4281 }
4282
4283 return true;
4284}
4285
4286/* Test the lower-level mapped_index::find_name_component_bounds
4287 method. */
4288
c62446b1 4289static void
5c58de74
PA
4290test_mapped_index_find_name_component_bounds ()
4291{
4292 mock_mapped_index mock_index (test_symbols);
4293
a3c5fafd 4294 mock_index.build_name_components ();
5c58de74
PA
4295
4296 /* Test the lower-level mapped_index::find_name_component_bounds
4297 method in completion mode. */
4298 {
4299 static const char *expected_syms[] = {
4300 "t1_func",
4301 "t1_func1",
5c58de74
PA
4302 };
4303
a3c5fafd 4304 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4305 "t1_func", expected_syms));
4306 }
4307
4308 /* Check that the increment-last-char in the name matching algorithm
4309 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4310 {
4311 static const char *expected_syms1[] = {
4312 "\377",
4313 "\377\377123",
4314 };
a3c5fafd 4315 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4316 "\377", expected_syms1));
4317
4318 static const char *expected_syms2[] = {
4319 "\377\377123",
4320 };
a3c5fafd 4321 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4322 "\377\377", expected_syms2));
4323 }
4324}
4325
4326/* Test dw2_expand_symtabs_matching_symbol. */
4327
4328static void
4329test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4330{
4331 mock_mapped_index mock_index (test_symbols);
4332
4333 /* We let all tests run until the end even if some fails, for debug
4334 convenience. */
4335 bool any_mismatch = false;
4336
4337 /* Create the expected symbols list (an initializer_list). Needed
4338 because lists have commas, and we need to pass them to CHECK,
4339 which is a macro. */
4340#define EXPECT(...) { __VA_ARGS__ }
4341
4342 /* Wrapper for check_match that passes down the current
4343 __FILE__/__LINE__. */
4344#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4345 any_mismatch |= !check_match (__FILE__, __LINE__, \
4346 mock_index, \
4347 NAME, MATCH_TYPE, COMPLETION_MODE, \
4348 EXPECTED_LIST)
4349
4350 /* Identity checks. */
4351 for (const char *sym : test_symbols)
4352 {
4353 /* Should be able to match all existing symbols. */
4354 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4355 EXPECT (sym));
4356
4357 /* Should be able to match all existing symbols with
4358 parameters. */
4359 std::string with_params = std::string (sym) + "(int)";
4360 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4361 EXPECT (sym));
4362
4363 /* Should be able to match all existing symbols with
4364 parameters and qualifiers. */
4365 with_params = std::string (sym) + " ( int ) const";
4366 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4367 EXPECT (sym));
4368
4369 /* This should really find sym, but cp-name-parser.y doesn't
4370 know about lvalue/rvalue qualifiers yet. */
4371 with_params = std::string (sym) + " ( int ) &&";
4372 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4373 {});
4374 }
4375
e1ef7d7a
PA
4376 /* Check that the name matching algorithm for completion doesn't get
4377 confused with Latin1 'ÿ' / 0xff. */
4378 {
4379 static const char str[] = "\377";
4380 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4381 EXPECT ("\377", "\377\377123"));
4382 }
4383
4384 /* Check that the increment-last-char in the matching algorithm for
4385 completion doesn't match "t1_fund" when completing "t1_func". */
4386 {
4387 static const char str[] = "t1_func";
4388 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4389 EXPECT ("t1_func", "t1_func1"));
4390 }
4391
c62446b1
PA
4392 /* Check that completion mode works at each prefix of the expected
4393 symbol name. */
4394 {
4395 static const char str[] = "function(int)";
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT ("function"));
4404 }
4405 }
4406
4407 /* While "w" is a prefix of both components, the match function
4408 should still only be called once. */
4409 {
4410 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4411 EXPECT ("w1::w2"));
a20714ff
PA
4412 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4413 EXPECT ("w1::w2"));
c62446b1
PA
4414 }
4415
4416 /* Same, with a "complicated" symbol. */
4417 {
4418 static const char str[] = Z_SYM_NAME;
4419 size_t len = strlen (str);
4420 std::string lookup;
4421
4422 for (size_t i = 1; i < len; i++)
4423 {
4424 lookup.assign (str, i);
4425 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4426 EXPECT (Z_SYM_NAME));
4427 }
4428 }
4429
4430 /* In FULL mode, an incomplete symbol doesn't match. */
4431 {
4432 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4433 {});
4434 }
4435
4436 /* A complete symbol with parameters matches any overload, since the
4437 index has no overload info. */
4438 {
4439 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4440 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4441 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4445 }
4446
4447 /* Check that whitespace is ignored appropriately. A symbol with a
4448 template argument list. */
4449 {
4450 static const char expected[] = "ns::foo<int>";
4451 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4452 EXPECT (expected));
a20714ff
PA
4453 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4454 EXPECT (expected));
c62446b1
PA
4455 }
4456
4457 /* Check that whitespace is ignored appropriately. A symbol with a
4458 template argument list that includes a pointer. */
4459 {
4460 static const char expected[] = "ns::foo<char*>";
4461 /* Try both completion and non-completion modes. */
4462 static const bool completion_mode[2] = {false, true};
4463 for (size_t i = 0; i < 2; i++)
4464 {
4465 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4466 completion_mode[i], EXPECT (expected));
a20714ff
PA
4467 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4468 completion_mode[i], EXPECT (expected));
c62446b1
PA
4469
4470 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4471 completion_mode[i], EXPECT (expected));
a20714ff
PA
4472 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4473 completion_mode[i], EXPECT (expected));
c62446b1
PA
4474 }
4475 }
4476
4477 {
4478 /* Check method qualifiers are ignored. */
4479 static const char expected[] = "ns::foo<char*>";
4480 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4481 symbol_name_match_type::FULL, true, EXPECT (expected));
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4484 CHECK_MATCH ("foo < char * > ( int ) const",
4485 symbol_name_match_type::WILD, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) &&",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4488 }
4489
4490 /* Test lookup names that don't match anything. */
4491 {
a20714ff
PA
4492 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4493 {});
4494
c62446b1
PA
4495 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4496 {});
4497 }
4498
a20714ff
PA
4499 /* Some wild matching tests, exercising "(anonymous namespace)",
4500 which should not be confused with a parameter list. */
4501 {
4502 static const char *syms[] = {
4503 "A::B::C",
4504 "B::C",
4505 "C",
4506 "A :: B :: C ( int )",
4507 "B :: C ( int )",
4508 "C ( int )",
4509 };
4510
4511 for (const char *s : syms)
4512 {
4513 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4514 EXPECT ("(anonymous namespace)::A::B::C"));
4515 }
4516 }
4517
4518 {
4519 static const char expected[] = "ns2::tmpl<int>::foo2";
4520 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4521 EXPECT (expected));
4522 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 }
4525
c62446b1
PA
4526 SELF_CHECK (!any_mismatch);
4527
4528#undef EXPECT
4529#undef CHECK_MATCH
4530}
4531
5c58de74
PA
4532static void
4533run_test ()
4534{
4535 test_mapped_index_find_name_component_bounds ();
4536 test_dw2_expand_symtabs_matching_symbol ();
4537}
4538
c62446b1
PA
4539}} // namespace selftests::dw2_expand_symtabs_matching
4540
4541#endif /* GDB_SELF_TEST */
4542
4b514bc8
JK
4543/* If FILE_MATCHER is NULL or if PER_CU has
4544 dwarf2_per_cu_quick_data::MARK set (see
4545 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4546 EXPANSION_NOTIFY on it. */
4547
4548static void
4549dw2_expand_symtabs_matching_one
97a1449a
SM
4550 (dwarf2_per_cu_data *per_cu,
4551 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4554{
4555 if (file_matcher == NULL || per_cu->v.quick->mark)
4556 {
af758d11 4557 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4558
97a1449a
SM
4559 compunit_symtab *symtab
4560 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4561 gdb_assert (symtab != nullptr);
4b514bc8 4562
af758d11
SM
4563 if (expansion_notify != NULL && symtab_was_null)
4564 expansion_notify (symtab);
4b514bc8
JK
4565 }
4566}
4567
3f563c84
PA
4568/* Helper for dw2_expand_matching symtabs. Called on each symbol
4569 matched, to expand corresponding CUs that were marked. IDX is the
4570 index of the symbol name that matched. */
4571
4572static void
4573dw2_expand_marked_cus
97a1449a 4574 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4575 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4576 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4577 search_domain kind)
4578{
3f563c84
PA
4579 offset_type *vec, vec_len, vec_idx;
4580 bool global_seen = false;
5989a64e 4581 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4582
61920122 4583 vec = (offset_type *) (index.constant_pool
f00a2de2 4584 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4585 vec_len = MAYBE_SWAP (vec[0]);
4586 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4587 {
61920122
PA
4588 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4589 /* This value is only valid for index versions >= 7. */
4590 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4591 gdb_index_symbol_kind symbol_kind =
4592 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4593 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4594 /* Only check the symbol attributes if they're present.
4595 Indices prior to version 7 don't record them,
4596 and indices >= 7 may elide them for certain symbols
4597 (gold does this). */
4598 int attrs_valid =
4599 (index.version >= 7
4600 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4601
4602 /* Work around gold/15646. */
4603 if (attrs_valid)
9291a0cd 4604 {
61920122
PA
4605 if (!is_static && global_seen)
4606 continue;
4607 if (!is_static)
4608 global_seen = true;
4609 }
3190f0c6 4610
61920122
PA
4611 /* Only check the symbol's kind if it has one. */
4612 if (attrs_valid)
4613 {
4614 switch (kind)
8943b874 4615 {
61920122
PA
4616 case VARIABLES_DOMAIN:
4617 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4618 continue;
4619 break;
4620 case FUNCTIONS_DOMAIN:
4621 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4622 continue;
61920122
PA
4623 break;
4624 case TYPES_DOMAIN:
4625 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4626 continue;
4627 break;
59c35742
AB
4628 case MODULES_DOMAIN:
4629 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4630 continue;
4631 break;
61920122
PA
4632 default:
4633 break;
8943b874 4634 }
61920122 4635 }
8943b874 4636
61920122 4637 /* Don't crash on bad data. */
5989a64e
SM
4638 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4639 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4640 {
b98664d3 4641 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4642 " [in module %s]"),
4643 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4644 continue;
4645 }
4646
5989a64e 4647 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4648 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4649 expansion_notify);
61920122
PA
4650 }
4651}
4652
4b514bc8
JK
4653/* If FILE_MATCHER is non-NULL, set all the
4654 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4655 that match FILE_MATCHER. */
4656
61920122 4657static void
4b514bc8 4658dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4659 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4660 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4661{
4b514bc8 4662 if (file_matcher == NULL)
61920122
PA
4663 return;
4664
4b514bc8
JK
4665 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4666 htab_eq_pointer,
4667 NULL, xcalloc, xfree));
4668 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4669 htab_eq_pointer,
4670 NULL, xcalloc, xfree));
61920122 4671
4b514bc8
JK
4672 /* The rule is CUs specify all the files, including those used by
4673 any TU, so there's no need to scan TUs here. */
61920122 4674
5989a64e 4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4676 {
927aa2e7
JK
4677 QUIT;
4678
4679 per_cu->v.quick->mark = 0;
4680
4681 /* We only need to look at symtabs not already expanded. */
af758d11 4682 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4683 continue;
4684
ab432490
SM
4685 quick_file_names *file_data
4686 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4687 if (file_data == NULL)
4688 continue;
4689
4690 if (htab_find (visited_not_found.get (), file_data) != NULL)
4691 continue;
4692 else if (htab_find (visited_found.get (), file_data) != NULL)
4693 {
4694 per_cu->v.quick->mark = 1;
4695 continue;
4696 }
4697
b76e467d 4698 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4699 {
4700 const char *this_real_name;
4701
4702 if (file_matcher (file_data->file_names[j], false))
4703 {
4704 per_cu->v.quick->mark = 1;
4705 break;
4706 }
4707
4708 /* Before we invoke realpath, which can get expensive when many
4709 files are involved, do a quick comparison of the basenames. */
4710 if (!basenames_may_differ
4711 && !file_matcher (lbasename (file_data->file_names[j]),
4712 true))
4713 continue;
4714
45940949
TT
4715 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4716 file_data, j);
927aa2e7
JK
4717 if (file_matcher (this_real_name, false))
4718 {
4719 per_cu->v.quick->mark = 1;
4720 break;
4721 }
4722 }
4723
b76e467d
SM
4724 void **slot = htab_find_slot (per_cu->v.quick->mark
4725 ? visited_found.get ()
4726 : visited_not_found.get (),
4727 file_data, INSERT);
927aa2e7
JK
4728 *slot = file_data;
4729 }
4730}
4731
4732static void
4733dw2_expand_symtabs_matching
4734 (struct objfile *objfile,
4735 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4736 const lookup_name_info *lookup_name,
927aa2e7
JK
4737 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4738 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4739 enum search_domain kind)
4740{
ed2dc618
SM
4741 struct dwarf2_per_objfile *dwarf2_per_objfile
4742 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4743
4744 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4745 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4746 return;
4747
ed2dc618 4748 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4749
c1a66c06
TV
4750 if (symbol_matcher == NULL && lookup_name == NULL)
4751 {
5989a64e 4752 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4753 {
4754 QUIT;
4755
97a1449a
SM
4756 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4757 file_matcher, expansion_notify);
c1a66c06
TV
4758 }
4759 return;
4760 }
4761
5989a64e 4762 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4763
c1a66c06 4764 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4765 symbol_matcher,
4766 kind, [&] (offset_type idx)
4767 {
ed2dc618 4768 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4769 expansion_notify, kind);
3b00ef10 4770 return true;
927aa2e7
JK
4771 });
4772}
4773
4774/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4775 symtab. */
4776
4777static struct compunit_symtab *
4778recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4779 CORE_ADDR pc)
4780{
4781 int i;
4782
4783 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4784 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4785 return cust;
4786
4787 if (cust->includes == NULL)
4788 return NULL;
4789
4790 for (i = 0; cust->includes[i]; ++i)
4791 {
4792 struct compunit_symtab *s = cust->includes[i];
4793
4794 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4795 if (s != NULL)
4796 return s;
4797 }
4798
4799 return NULL;
4800}
4801
4802static struct compunit_symtab *
4803dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4804 struct bound_minimal_symbol msymbol,
4805 CORE_ADDR pc,
4806 struct obj_section *section,
4807 int warn_if_readin)
4808{
4809 struct dwarf2_per_cu_data *data;
4810 struct compunit_symtab *result;
4811
d320c2b5 4812 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4813 return NULL;
4814
b3b3bada 4815 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4816 data = (struct dwarf2_per_cu_data *) addrmap_find
4817 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4818 if (!data)
4819 return NULL;
4820
af758d11
SM
4821 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4822 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4823 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4824 paddress (objfile->arch (), pc));
927aa2e7 4825
97a1449a
SM
4826 result = recursively_find_pc_sect_compunit_symtab
4827 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4828
927aa2e7
JK
4829 gdb_assert (result != NULL);
4830 return result;
4831}
4832
4833static void
4834dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4835 void *data, int need_fullname)
4836{
ed2dc618
SM
4837 struct dwarf2_per_objfile *dwarf2_per_objfile
4838 = get_dwarf2_per_objfile (objfile);
927aa2e7 4839
5989a64e 4840 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4841 {
5989a64e 4842 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4843
4844 htab_up visited (htab_create_alloc (10,
4845 htab_hash_pointer, htab_eq_pointer,
4846 NULL, xcalloc, xfree));
4847
4848 /* The rule is CUs specify all the files, including those used
4849 by any TU, so there's no need to scan TUs here. We can
4850 ignore file names coming from already-expanded CUs. */
4851
5989a64e 4852 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4853 {
af758d11 4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4855 {
4856 void **slot = htab_find_slot (visited.get (),
4857 per_cu->v.quick->file_names,
4858 INSERT);
4859
4860 *slot = per_cu->v.quick->file_names;
4861 }
4862 }
4863
5989a64e 4864 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4865 {
927aa2e7 4866 /* We only need to look at symtabs not already expanded. */
af758d11 4867 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4868 continue;
4869
ab432490
SM
4870 quick_file_names *file_data
4871 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4872 if (file_data == NULL)
4873 continue;
4874
b76e467d 4875 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4876 if (*slot)
4877 {
4878 /* Already visited. */
4879 continue;
4880 }
4881 *slot = file_data;
4882
4883 for (int j = 0; j < file_data->num_file_names; ++j)
4884 {
4885 const char *filename = file_data->file_names[j];
5989a64e 4886 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4887 }
4888 }
4889 }
4890
5989a64e 4891 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4892 {
4893 gdb::unique_xmalloc_ptr<char> this_real_name;
4894
4895 if (need_fullname)
4896 this_real_name = gdb_realpath (filename);
4897 (*fun) (filename, this_real_name.get (), data);
4898 });
4899}
4900
4901static int
4902dw2_has_symbols (struct objfile *objfile)
4903{
4904 return 1;
4905}
4906
4907const struct quick_symbol_functions dwarf2_gdb_index_functions =
4908{
4909 dw2_has_symbols,
4910 dw2_find_last_source_symtab,
4911 dw2_forget_cached_source_info,
4912 dw2_map_symtabs_matching_filename,
4913 dw2_lookup_symbol,
d3214198 4914 NULL,
927aa2e7
JK
4915 dw2_print_stats,
4916 dw2_dump,
927aa2e7
JK
4917 dw2_expand_symtabs_for_function,
4918 dw2_expand_all_symtabs,
4919 dw2_expand_symtabs_with_fullname,
4920 dw2_map_matching_symbols,
4921 dw2_expand_symtabs_matching,
4922 dw2_find_pc_sect_compunit_symtab,
4923 NULL,
4924 dw2_map_symbol_filenames
4925};
4926
4927/* DWARF-5 debug_names reader. */
4928
4929/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4930static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4931
4932/* A helper function that reads the .debug_names section in SECTION
4933 and fills in MAP. FILENAME is the name of the file containing the
4934 section; it is used for error reporting.
4935
4936 Returns true if all went well, false otherwise. */
4937
4938static bool
4939read_debug_names_from_section (struct objfile *objfile,
4940 const char *filename,
4941 struct dwarf2_section_info *section,
4942 mapped_debug_names &map)
4943{
96b79293 4944 if (section->empty ())
927aa2e7
JK
4945 return false;
4946
4947 /* Older elfutils strip versions could keep the section in the main
4948 executable while splitting it for the separate debug info file. */
96b79293 4949 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4950 return false;
4951
96b79293 4952 section->read (objfile);
927aa2e7 4953
08feed99 4954 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4955
4956 const gdb_byte *addr = section->buffer;
4957
96b79293 4958 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4959
4960 unsigned int bytes_read;
4961 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4962 addr += bytes_read;
4963
4964 map.dwarf5_is_dwarf64 = bytes_read != 4;
4965 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4966 if (bytes_read + length != section->size)
4967 {
4968 /* There may be multiple per-CU indices. */
4969 warning (_("Section .debug_names in %s length %s does not match "
4970 "section length %s, ignoring .debug_names."),
4971 filename, plongest (bytes_read + length),
4972 pulongest (section->size));
4973 return false;
4974 }
4975
4976 /* The version number. */
4977 uint16_t version = read_2_bytes (abfd, addr);
4978 addr += 2;
4979 if (version != 5)
4980 {
4981 warning (_("Section .debug_names in %s has unsupported version %d, "
4982 "ignoring .debug_names."),
4983 filename, version);
4984 return false;
4985 }
4986
4987 /* Padding. */
4988 uint16_t padding = read_2_bytes (abfd, addr);
4989 addr += 2;
4990 if (padding != 0)
4991 {
4992 warning (_("Section .debug_names in %s has unsupported padding %d, "
4993 "ignoring .debug_names."),
4994 filename, padding);
4995 return false;
4996 }
4997
4998 /* comp_unit_count - The number of CUs in the CU list. */
4999 map.cu_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* local_type_unit_count - The number of TUs in the local TU
5003 list. */
5004 map.tu_count = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* foreign_type_unit_count - The number of TUs in the foreign TU
5008 list. */
5009 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 if (foreign_tu_count != 0)
5012 {
5013 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5014 "ignoring .debug_names."),
5015 filename, static_cast<unsigned long> (foreign_tu_count));
5016 return false;
5017 }
5018
5019 /* bucket_count - The number of hash buckets in the hash lookup
5020 table. */
5021 map.bucket_count = read_4_bytes (abfd, addr);
5022 addr += 4;
5023
5024 /* name_count - The number of unique names in the index. */
5025 map.name_count = read_4_bytes (abfd, addr);
5026 addr += 4;
5027
5028 /* abbrev_table_size - The size in bytes of the abbreviations
5029 table. */
5030 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5031 addr += 4;
5032
5033 /* augmentation_string_size - The size in bytes of the augmentation
5034 string. This value is rounded up to a multiple of 4. */
5035 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5036 addr += 4;
5037 map.augmentation_is_gdb = ((augmentation_string_size
5038 == sizeof (dwarf5_augmentation))
5039 && memcmp (addr, dwarf5_augmentation,
5040 sizeof (dwarf5_augmentation)) == 0);
5041 augmentation_string_size += (-augmentation_string_size) & 3;
5042 addr += augmentation_string_size;
5043
5044 /* List of CUs */
5045 map.cu_table_reordered = addr;
5046 addr += map.cu_count * map.offset_size;
5047
5048 /* List of Local TUs */
5049 map.tu_table_reordered = addr;
5050 addr += map.tu_count * map.offset_size;
5051
5052 /* Hash Lookup Table */
5053 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5054 addr += map.bucket_count * 4;
5055 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.name_count * 4;
5057
5058 /* Name Table */
5059 map.name_table_string_offs_reordered = addr;
5060 addr += map.name_count * map.offset_size;
5061 map.name_table_entry_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063
5064 const gdb_byte *abbrev_table_start = addr;
5065 for (;;)
5066 {
927aa2e7
JK
5067 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5068 addr += bytes_read;
5069 if (index_num == 0)
5070 break;
5071
5072 const auto insertpair
5073 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5074 if (!insertpair.second)
5075 {
5076 warning (_("Section .debug_names in %s has duplicate index %s, "
5077 "ignoring .debug_names."),
5078 filename, pulongest (index_num));
5079 return false;
5080 }
5081 mapped_debug_names::index_val &indexval = insertpair.first->second;
5082 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5083 addr += bytes_read;
5084
5085 for (;;)
5086 {
5087 mapped_debug_names::index_val::attr attr;
5088 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5089 addr += bytes_read;
5090 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 if (attr.form == DW_FORM_implicit_const)
5093 {
5094 attr.implicit_const = read_signed_leb128 (abfd, addr,
5095 &bytes_read);
5096 addr += bytes_read;
5097 }
5098 if (attr.dw_idx == 0 && attr.form == 0)
5099 break;
5100 indexval.attr_vec.push_back (std::move (attr));
5101 }
5102 }
5103 if (addr != abbrev_table_start + abbrev_table_size)
5104 {
5105 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5106 "of size %s vs. written as %u, ignoring .debug_names."),
5107 filename, plongest (addr - abbrev_table_start),
5108 abbrev_table_size);
927aa2e7
JK
5109 return false;
5110 }
5111 map.entry_pool = addr;
5112
5113 return true;
5114}
5115
5116/* A helper for create_cus_from_debug_names that handles the MAP's CU
5117 list. */
5118
5119static void
ed2dc618 5120create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5121 const mapped_debug_names &map,
5122 dwarf2_section_info &section,
b76e467d 5123 bool is_dwz)
927aa2e7 5124{
3ee6bb11
TV
5125 if (!map.augmentation_is_gdb)
5126 {
5127 for (uint32_t i = 0; i < map.cu_count; ++i)
5128 {
5129 sect_offset sect_off
5130 = (sect_offset) (extract_unsigned_integer
5131 (map.cu_table_reordered + i * map.offset_size,
5132 map.offset_size,
5133 map.dwarf5_byte_order));
5134 /* We don't know the length of the CU, because the CU list in a
5135 .debug_names index can be incomplete, so we can't use the start of
5136 the next CU as end of this CU. We create the CUs here with length 0,
5137 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5138 dwarf2_per_cu_data *per_cu
5139 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5140 sect_off, 0);
5989a64e 5141 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5142 }
5143 }
5144
927aa2e7
JK
5145 sect_offset sect_off_prev;
5146 for (uint32_t i = 0; i <= map.cu_count; ++i)
5147 {
5148 sect_offset sect_off_next;
5149 if (i < map.cu_count)
5150 {
5151 sect_off_next
5152 = (sect_offset) (extract_unsigned_integer
5153 (map.cu_table_reordered + i * map.offset_size,
5154 map.offset_size,
5155 map.dwarf5_byte_order));
5156 }
5157 else
5158 sect_off_next = (sect_offset) section.size;
5159 if (i >= 1)
5160 {
5161 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5162 dwarf2_per_cu_data *per_cu
ed2dc618 5163 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5164 sect_off_prev, length);
5989a64e 5165 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5166 }
5167 sect_off_prev = sect_off_next;
5168 }
5169}
5170
5171/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5172 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5173
5174static void
ed2dc618 5175create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5176 const mapped_debug_names &map,
5177 const mapped_debug_names &dwz_map)
5178{
5989a64e
SM
5179 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5180 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5181
ed2dc618 5182 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5989a64e 5183 dwarf2_per_objfile->per_bfd->info,
b76e467d 5184 false /* is_dwz */);
927aa2e7
JK
5185
5186 if (dwz_map.cu_count == 0)
5187 return;
5188
c3699833 5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
ed2dc618 5190 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5191 true /* is_dwz */);
927aa2e7
JK
5192}
5193
5194/* Read .debug_names. If everything went ok, initialize the "quick"
5195 elements of all the CUs and return true. Otherwise, return false. */
5196
5197static bool
ed2dc618 5198dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5199{
22ca247e
TT
5200 std::unique_ptr<mapped_debug_names> map
5201 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5202 mapped_debug_names dwz_map (dwarf2_per_objfile);
5203 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5204
5205 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5206 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5207 *map))
927aa2e7
JK
5208 return false;
5209
5210 /* Don't use the index if it's empty. */
22ca247e 5211 if (map->name_count == 0)
927aa2e7
JK
5212 return false;
5213
5214 /* If there is a .dwz file, read it so we can get its CU list as
5215 well. */
c3699833 5216 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5217 if (dwz != NULL)
5218 {
5219 if (!read_debug_names_from_section (objfile,
00f93c44 5220 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5221 &dwz->debug_names, dwz_map))
5222 {
5223 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5224 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5225 return false;
5226 }
5227 }
5228
22ca247e 5229 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5230
22ca247e 5231 if (map->tu_count != 0)
927aa2e7
JK
5232 {
5233 /* We can only handle a single .debug_types when we have an
5234 index. */
5989a64e 5235 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5236 return false;
5237
5989a64e 5238 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5239
5240 create_signatured_type_table_from_debug_names
5989a64e 5241 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5242 }
5243
ed2dc618 5244 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5245 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5246
5989a64e
SM
5247 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5248 dwarf2_per_objfile->per_bfd->using_index = 1;
5249 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5250 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5251
5252 return true;
5253}
5254
927aa2e7
JK
5255/* Type used to manage iterating over all CUs looking for a symbol for
5256 .debug_names. */
5257
5258class dw2_debug_names_iterator
5259{
5260public:
927aa2e7 5261 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5262 gdb::optional<block_enum> block_index,
5263 domain_enum domain,
927aa2e7 5264 const char *name)
2b79f376 5265 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5266 m_addr (find_vec_in_debug_names (map, name))
5267 {}
5268
5269 dw2_debug_names_iterator (const mapped_debug_names &map,
5270 search_domain search, uint32_t namei)
5271 : m_map (map),
5272 m_search (search),
5273 m_addr (find_vec_in_debug_names (map, namei))
5274 {}
5275
3b00ef10
TT
5276 dw2_debug_names_iterator (const mapped_debug_names &map,
5277 block_enum block_index, domain_enum domain,
5278 uint32_t namei)
5279 : m_map (map), m_block_index (block_index), m_domain (domain),
5280 m_addr (find_vec_in_debug_names (map, namei))
5281 {}
5282
927aa2e7
JK
5283 /* Return the next matching CU or NULL if there are no more. */
5284 dwarf2_per_cu_data *next ();
5285
5286private:
5287 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5288 const char *name);
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 uint32_t namei);
5291
5292 /* The internalized form of .debug_names. */
5293 const mapped_debug_names &m_map;
5294
2b79f376
SM
5295 /* If set, only look for symbols that match that block. Valid values are
5296 GLOBAL_BLOCK and STATIC_BLOCK. */
5297 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5298
5299 /* The kind of symbol we're looking for. */
5300 const domain_enum m_domain = UNDEF_DOMAIN;
5301 const search_domain m_search = ALL_DOMAIN;
5302
5303 /* The list of CUs from the index entry of the symbol, or NULL if
5304 not found. */
5305 const gdb_byte *m_addr;
5306};
5307
5308const char *
5309mapped_debug_names::namei_to_name (uint32_t namei) const
5310{
5311 const ULONGEST namei_string_offs
5312 = extract_unsigned_integer ((name_table_string_offs_reordered
5313 + namei * offset_size),
5314 offset_size,
5315 dwarf5_byte_order);
4f44ae6c
TT
5316 return read_indirect_string_at_offset (dwarf2_per_objfile,
5317 namei_string_offs);
927aa2e7
JK
5318}
5319
5320/* Find a slot in .debug_names for the object named NAME. If NAME is
5321 found, return pointer to its pool data. If NAME cannot be found,
5322 return NULL. */
5323
5324const gdb_byte *
5325dw2_debug_names_iterator::find_vec_in_debug_names
5326 (const mapped_debug_names &map, const char *name)
5327{
5328 int (*cmp) (const char *, const char *);
5329
54ee4252 5330 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5331 if (current_language->la_language == language_cplus
5332 || current_language->la_language == language_fortran
5333 || current_language->la_language == language_d)
5334 {
5335 /* NAME is already canonical. Drop any qualifiers as
5336 .debug_names does not contain any. */
5337
5338 if (strchr (name, '(') != NULL)
5339 {
54ee4252 5340 without_params = cp_remove_params (name);
927aa2e7 5341 if (without_params != NULL)
54ee4252 5342 name = without_params.get ();
927aa2e7
JK
5343 }
5344 }
5345
5346 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5347
5348 const uint32_t full_hash = dwarf5_djb_hash (name);
5349 uint32_t namei
5350 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5351 (map.bucket_table_reordered
5352 + (full_hash % map.bucket_count)), 4,
5353 map.dwarf5_byte_order);
5354 if (namei == 0)
5355 return NULL;
5356 --namei;
5357 if (namei >= map.name_count)
5358 {
b98664d3 5359 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5360 "[in module %s]"),
5361 namei, map.name_count,
ed2dc618 5362 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5363 return NULL;
5364 }
5365
5366 for (;;)
5367 {
5368 const uint32_t namei_full_hash
5369 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5370 (map.hash_table_reordered + namei), 4,
5371 map.dwarf5_byte_order);
5372 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5373 return NULL;
5374
5375 if (full_hash == namei_full_hash)
5376 {
5377 const char *const namei_string = map.namei_to_name (namei);
5378
5379#if 0 /* An expensive sanity check. */
5380 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5381 {
b98664d3 5382 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5383 "[in module %s]"),
5384 namei, objfile_name (dwarf2_per_objfile->objfile));
5385 return NULL;
5386 }
5387#endif
5388
5389 if (cmp (namei_string, name) == 0)
5390 {
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396 }
5397 }
5398
5399 ++namei;
5400 if (namei >= map.name_count)
5401 return NULL;
5402 }
5403}
5404
5405const gdb_byte *
5406dw2_debug_names_iterator::find_vec_in_debug_names
5407 (const mapped_debug_names &map, uint32_t namei)
5408{
5409 if (namei >= map.name_count)
5410 {
b98664d3 5411 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5412 "[in module %s]"),
5413 namei, map.name_count,
ed2dc618 5414 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5415 return NULL;
5416 }
5417
5418 const ULONGEST namei_entry_offs
5419 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5420 + namei * map.offset_size),
5421 map.offset_size, map.dwarf5_byte_order);
5422 return map.entry_pool + namei_entry_offs;
5423}
5424
5425/* See dw2_debug_names_iterator. */
5426
5427dwarf2_per_cu_data *
5428dw2_debug_names_iterator::next ()
5429{
5430 if (m_addr == NULL)
5431 return NULL;
5432
ed2dc618
SM
5433 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5434 struct objfile *objfile = dwarf2_per_objfile->objfile;
5435 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5436
5437 again:
5438
5439 unsigned int bytes_read;
5440 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5441 m_addr += bytes_read;
5442 if (abbrev == 0)
5443 return NULL;
5444
5445 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5446 if (indexval_it == m_map.abbrev_map.cend ())
5447 {
b98664d3 5448 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5449 "[in module %s]"),
ed2dc618 5450 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5451 return NULL;
5452 }
5453 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5454 enum class symbol_linkage {
5455 unknown,
5456 static_,
5457 extern_,
23c13d42 5458 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5459 dwarf2_per_cu_data *per_cu = NULL;
5460 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5461 {
5462 ULONGEST ull;
5463 switch (attr.form)
5464 {
5465 case DW_FORM_implicit_const:
5466 ull = attr.implicit_const;
5467 break;
5468 case DW_FORM_flag_present:
5469 ull = 1;
5470 break;
5471 case DW_FORM_udata:
5472 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5473 m_addr += bytes_read;
5474 break;
6dc55ce9 5475 case DW_FORM_ref4:
5476 ull = read_4_bytes (abfd, m_addr);
5477 m_addr += 4;
5478 break;
5479 case DW_FORM_ref8:
5480 ull = read_8_bytes (abfd, m_addr);
5481 m_addr += 8;
5482 break;
5483 case DW_FORM_ref_sig8:
5484 ull = read_8_bytes (abfd, m_addr);
5485 m_addr += 8;
5486 break;
927aa2e7 5487 default:
b98664d3 5488 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5489 dwarf_form_name (attr.form),
ed2dc618 5490 objfile_name (objfile));
927aa2e7
JK
5491 return NULL;
5492 }
5493 switch (attr.dw_idx)
5494 {
5495 case DW_IDX_compile_unit:
5496 /* Don't crash on bad data. */
5989a64e 5497 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5498 {
b98664d3 5499 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5500 " [in module %s]"),
5501 pulongest (ull),
5502 objfile_name (dwarf2_per_objfile->objfile));
5503 continue;
5504 }
5989a64e 5505 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5506 break;
8af5c486
JK
5507 case DW_IDX_type_unit:
5508 /* Don't crash on bad data. */
5989a64e 5509 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5510 {
b98664d3 5511 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5512 " [in module %s]"),
5513 pulongest (ull),
5514 objfile_name (dwarf2_per_objfile->objfile));
5515 continue;
5516 }
5989a64e 5517 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5518 break;
6dc55ce9 5519 case DW_IDX_die_offset:
5520 /* In a per-CU index (as opposed to a per-module index), index
5521 entries without CU attribute implicitly refer to the single CU. */
5522 if (per_cu == NULL)
5989a64e 5523 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5524 break;
927aa2e7
JK
5525 case DW_IDX_GNU_internal:
5526 if (!m_map.augmentation_is_gdb)
5527 break;
23c13d42 5528 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5529 break;
5530 case DW_IDX_GNU_external:
5531 if (!m_map.augmentation_is_gdb)
5532 break;
23c13d42 5533 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5534 break;
5535 }
5536 }
5537
5538 /* Skip if already read in. */
af758d11 5539 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5540 goto again;
5541
5542 /* Check static vs global. */
23c13d42 5543 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5544 {
2b79f376 5545 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5546 const bool symbol_is_static =
5547 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5548 if (want_static != symbol_is_static)
2b79f376 5549 goto again;
927aa2e7
JK
5550 }
5551
5552 /* Match dw2_symtab_iter_next, symbol_kind
5553 and debug_names::psymbol_tag. */
5554 switch (m_domain)
5555 {
5556 case VAR_DOMAIN:
5557 switch (indexval.dwarf_tag)
5558 {
5559 case DW_TAG_variable:
5560 case DW_TAG_subprogram:
5561 /* Some types are also in VAR_DOMAIN. */
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case STRUCT_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case DW_TAG_typedef:
5573 case DW_TAG_structure_type:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case LABEL_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case 0:
5583 case DW_TAG_variable:
5584 break;
5585 default:
5586 goto again;
5587 }
5588 break;
59c35742
AB
5589 case MODULE_DOMAIN:
5590 switch (indexval.dwarf_tag)
5591 {
5592 case DW_TAG_module:
5593 break;
5594 default:
5595 goto again;
5596 }
5597 break;
927aa2e7
JK
5598 default:
5599 break;
5600 }
5601
5602 /* Match dw2_expand_symtabs_matching, symbol_kind and
5603 debug_names::psymbol_tag. */
5604 switch (m_search)
4b514bc8 5605 {
927aa2e7
JK
5606 case VARIABLES_DOMAIN:
5607 switch (indexval.dwarf_tag)
4b514bc8 5608 {
927aa2e7
JK
5609 case DW_TAG_variable:
5610 break;
5611 default:
5612 goto again;
4b514bc8 5613 }
927aa2e7
JK
5614 break;
5615 case FUNCTIONS_DOMAIN:
5616 switch (indexval.dwarf_tag)
4b514bc8 5617 {
927aa2e7
JK
5618 case DW_TAG_subprogram:
5619 break;
5620 default:
5621 goto again;
4b514bc8 5622 }
927aa2e7
JK
5623 break;
5624 case TYPES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_typedef:
5628 case DW_TAG_structure_type:
5629 break;
5630 default:
5631 goto again;
5632 }
5633 break;
59c35742
AB
5634 case MODULES_DOMAIN:
5635 switch (indexval.dwarf_tag)
5636 {
5637 case DW_TAG_module:
5638 break;
5639 default:
5640 goto again;
5641 }
927aa2e7
JK
5642 default:
5643 break;
4b514bc8 5644 }
927aa2e7
JK
5645
5646 return per_cu;
4b514bc8 5647}
61920122 5648
927aa2e7 5649static struct compunit_symtab *
c7f839cb 5650dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5651 const char *name, domain_enum domain)
4b514bc8 5652{
ed2dc618
SM
5653 struct dwarf2_per_objfile *dwarf2_per_objfile
5654 = get_dwarf2_per_objfile (objfile);
61920122 5655
5989a64e 5656 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5657 if (!mapp)
61920122 5658 {
927aa2e7
JK
5659 /* index is NULL if OBJF_READNOW. */
5660 return NULL;
5661 }
5662 const auto &map = *mapp;
9291a0cd 5663
2b79f376 5664 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5665
927aa2e7
JK
5666 struct compunit_symtab *stab_best = NULL;
5667 struct dwarf2_per_cu_data *per_cu;
5668 while ((per_cu = iter.next ()) != NULL)
5669 {
5670 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5671 compunit_symtab *stab
5672 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5673 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5674 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5675
927aa2e7
JK
5676 sym = block_find_symbol (block, name, domain,
5677 block_find_non_opaque_type_preferred,
5678 &with_opaque);
9703b513 5679
927aa2e7
JK
5680 /* Some caution must be observed with overloaded functions and
5681 methods, since the index will not contain any overload
5682 information (but NAME might contain it). */
a3ec0bb1 5683
927aa2e7 5684 if (sym != NULL
987012b8 5685 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5686 return stab;
5687 if (with_opaque != NULL
987012b8 5688 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5689 stab_best = stab;
9703b513 5690
927aa2e7 5691 /* Keep looking through other CUs. */
9703b513
TT
5692 }
5693
927aa2e7 5694 return stab_best;
9703b513
TT
5695}
5696
927aa2e7
JK
5697/* This dumps minimal information about .debug_names. It is called
5698 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5699 uses this to verify that .debug_names has been loaded. */
9291a0cd 5700
927aa2e7
JK
5701static void
5702dw2_debug_names_dump (struct objfile *objfile)
5703{
ed2dc618
SM
5704 struct dwarf2_per_objfile *dwarf2_per_objfile
5705 = get_dwarf2_per_objfile (objfile);
5706
5989a64e 5707 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5708 printf_filtered (".debug_names:");
5989a64e 5709 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5710 printf_filtered (" exists\n");
5711 else
5712 printf_filtered (" faked for \"readnow\"\n");
5713 printf_filtered ("\n");
9291a0cd
TT
5714}
5715
9291a0cd 5716static void
927aa2e7
JK
5717dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5718 const char *func_name)
9291a0cd 5719{
ed2dc618
SM
5720 struct dwarf2_per_objfile *dwarf2_per_objfile
5721 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5722
5989a64e
SM
5723 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5724 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5725 {
5989a64e 5726 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5727
2b79f376 5728 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5729
927aa2e7
JK
5730 struct dwarf2_per_cu_data *per_cu;
5731 while ((per_cu = iter.next ()) != NULL)
97a1449a 5732 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5733 }
5734}
24c79950 5735
3b00ef10
TT
5736static void
5737dw2_debug_names_map_matching_symbols
5738 (struct objfile *objfile,
5739 const lookup_name_info &name, domain_enum domain,
5740 int global,
5741 gdb::function_view<symbol_found_callback_ftype> callback,
5742 symbol_compare_ftype *ordered_compare)
5743{
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5748 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5749 return;
5750
5989a64e 5751 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5752 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5753
5754 const char *match_name = name.ada ().lookup_name ().c_str ();
5755 auto matcher = [&] (const char *symname)
5756 {
5757 if (ordered_compare == nullptr)
5758 return true;
5759 return ordered_compare (symname, match_name) == 0;
5760 };
5761
5762 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5763 [&] (offset_type namei)
5764 {
5765 /* The name was matched, now expand corresponding CUs that were
5766 marked. */
5767 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5768
5769 struct dwarf2_per_cu_data *per_cu;
5770 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5771 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5772 nullptr);
3b00ef10
TT
5773 return true;
5774 });
5775
5776 /* It's a shame we couldn't do this inside the
5777 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5778 that have already been expanded. Instead, this loop matches what
5779 the psymtab code does. */
5989a64e 5780 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5781 {
af758d11
SM
5782 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5783 if (symtab != nullptr)
3b00ef10
TT
5784 {
5785 const struct block *block
af758d11 5786 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5787 if (!iterate_over_symbols_terminated (block, name,
5788 domain, callback))
5789 break;
5790 }
5791 }
5792}
5793
927aa2e7
JK
5794static void
5795dw2_debug_names_expand_symtabs_matching
5796 (struct objfile *objfile,
5797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5798 const lookup_name_info *lookup_name,
927aa2e7
JK
5799 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5800 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5801 enum search_domain kind)
5802{
ed2dc618
SM
5803 struct dwarf2_per_objfile *dwarf2_per_objfile
5804 = get_dwarf2_per_objfile (objfile);
9291a0cd 5805
927aa2e7 5806 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5807 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5808 return;
9291a0cd 5809
ed2dc618 5810 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5811
c1a66c06
TV
5812 if (symbol_matcher == NULL && lookup_name == NULL)
5813 {
5989a64e 5814 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5815 {
5816 QUIT;
5817
97a1449a
SM
5818 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5819 file_matcher, expansion_notify);
c1a66c06
TV
5820 }
5821 return;
5822 }
5823
5989a64e 5824 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5825
c1a66c06 5826 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5827 symbol_matcher,
5828 kind, [&] (offset_type namei)
927aa2e7 5829 {
927aa2e7
JK
5830 /* The name was matched, now expand corresponding CUs that were
5831 marked. */
5832 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5833
927aa2e7
JK
5834 struct dwarf2_per_cu_data *per_cu;
5835 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5836 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5837 file_matcher, expansion_notify);
3b00ef10 5838 return true;
44ed8f3e 5839 });
9291a0cd
TT
5840}
5841
927aa2e7 5842const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5843{
5844 dw2_has_symbols,
5845 dw2_find_last_source_symtab,
5846 dw2_forget_cached_source_info,
f8eba3c6 5847 dw2_map_symtabs_matching_filename,
927aa2e7 5848 dw2_debug_names_lookup_symbol,
d3214198 5849 NULL,
9291a0cd 5850 dw2_print_stats,
927aa2e7 5851 dw2_debug_names_dump,
927aa2e7 5852 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5853 dw2_expand_all_symtabs,
652a8996 5854 dw2_expand_symtabs_with_fullname,
3b00ef10 5855 dw2_debug_names_map_matching_symbols,
927aa2e7 5856 dw2_debug_names_expand_symtabs_matching,
43f3e411 5857 dw2_find_pc_sect_compunit_symtab,
71a3c369 5858 NULL,
9291a0cd
TT
5859 dw2_map_symbol_filenames
5860};
5861
4485a1c1 5862/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5863 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5864
5865template <typename T>
5866static gdb::array_view<const gdb_byte>
5867get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5868{
5869 dwarf2_section_info *section = &section_owner->gdb_index;
5870
96b79293 5871 if (section->empty ())
4485a1c1
SM
5872 return {};
5873
5874 /* Older elfutils strip versions could keep the section in the main
5875 executable while splitting it for the separate debug info file. */
96b79293 5876 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5877 return {};
5878
96b79293 5879 section->read (obj);
4485a1c1 5880
8bebfcda
PA
5881 /* dwarf2_section_info::size is a bfd_size_type, while
5882 gdb::array_view works with size_t. On 32-bit hosts, with
5883 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5884 is 32-bit. So we need an explicit narrowing conversion here.
5885 This is fine, because it's impossible to allocate or mmap an
5886 array/buffer larger than what size_t can represent. */
5887 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5888}
5889
87d6a7aa
SM
5890/* Lookup the index cache for the contents of the index associated to
5891 DWARF2_OBJ. */
5892
5893static gdb::array_view<const gdb_byte>
5989a64e 5894get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5895{
5896 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5897 if (build_id == nullptr)
5898 return {};
5899
5900 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5901 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5902}
5903
5904/* Same as the above, but for DWZ. */
5905
5906static gdb::array_view<const gdb_byte>
5907get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5908{
5909 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5910 if (build_id == nullptr)
5911 return {};
5912
5913 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5914}
5915
3c0aa29a 5916/* See symfile.h. */
9291a0cd 5917
3c0aa29a
PA
5918bool
5919dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5920{
ed2dc618
SM
5921 struct dwarf2_per_objfile *dwarf2_per_objfile
5922 = get_dwarf2_per_objfile (objfile);
5923
9291a0cd
TT
5924 /* If we're about to read full symbols, don't bother with the
5925 indices. In this case we also don't care if some other debug
5926 format is making psymtabs, because they are all about to be
5927 expanded anyway. */
5928 if ((objfile->flags & OBJF_READNOW))
5929 {
5989a64e 5930 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5931 create_all_comp_units (dwarf2_per_objfile);
5932 create_all_type_units (dwarf2_per_objfile);
5989a64e 5933 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5934 = create_quick_file_names_table
5989a64e 5935 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5936 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5937
5989a64e
SM
5938 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5939 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5940 {
5989a64e 5941 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5942
5989a64e 5943 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5944 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5945 }
5946
5947 /* Return 1 so that gdb sees the "quick" functions. However,
5948 these functions will be no-ops because we will have expanded
5949 all symtabs. */
3c0aa29a
PA
5950 *index_kind = dw_index_kind::GDB_INDEX;
5951 return true;
9291a0cd
TT
5952 }
5953
ed2dc618 5954 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5955 {
5956 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5957 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5958 return true;
5959 }
927aa2e7 5960
4485a1c1 5961 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5962 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5963 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5964 {
5965 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5966 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5967 return true;
5968 }
9291a0cd 5969
87d6a7aa
SM
5970 /* ... otherwise, try to find the index in the index cache. */
5971 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5972 get_gdb_index_contents_from_cache,
5973 get_gdb_index_contents_from_cache_dwz))
5974 {
5975 global_index_cache.hit ();
5976 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5977 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5978 return true;
5979 }
5980
5981 global_index_cache.miss ();
3c0aa29a 5982 return false;
9291a0cd
TT
5983}
5984
5985\f
5986
dce234bc
PP
5987/* Build a partial symbol table. */
5988
5989void
f29dff0a 5990dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5991{
ed2dc618
SM
5992 struct dwarf2_per_objfile *dwarf2_per_objfile
5993 = get_dwarf2_per_objfile (objfile);
c9bf0622 5994
6eee24ce 5995 init_psymbol_list (objfile, 1024);
c906108c 5996
a70b8144 5997 try
c9bf0622
TT
5998 {
5999 /* This isn't really ideal: all the data we allocate on the
6000 objfile's obstack is still uselessly kept around. However,
6001 freeing it seems unsafe. */
906768f9 6002 psymtab_discarder psymtabs (objfile);
ed2dc618 6003 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6004 psymtabs.keep ();
87d6a7aa 6005
af758d11
SM
6006 dwarf2_per_objfile->resize_symtabs ();
6007
87d6a7aa
SM
6008 /* (maybe) store an index in the cache. */
6009 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6010 }
230d2906 6011 catch (const gdb_exception_error &except)
492d29ea
PA
6012 {
6013 exception_print (gdb_stderr, except);
6014 }
c906108c 6015}
c906108c 6016
3b80fe9b
DE
6017/* Find the base address of the compilation unit for range lists and
6018 location lists. It will normally be specified by DW_AT_low_pc.
6019 In DWARF-3 draft 4, the base address could be overridden by
6020 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6021 compilation units with discontinuous ranges. */
6022
6023static void
6024dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6025{
6026 struct attribute *attr;
6027
2b24b6e4 6028 cu->base_address.reset ();
3b80fe9b
DE
6029
6030 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6031 if (attr != nullptr)
2b24b6e4 6032 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6033 else
6034 {
6035 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6036 if (attr != nullptr)
2b24b6e4 6037 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6038 }
6039}
6040
36586728
TT
6041/* Helper function that returns the proper abbrev section for
6042 THIS_CU. */
6043
6044static struct dwarf2_section_info *
6045get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6046{
6047 struct dwarf2_section_info *abbrev;
c3699833 6048 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6049
6050 if (this_cu->is_dwz)
c3699833 6051 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6052 else
c3699833 6053 abbrev = &per_bfd->abbrev;
36586728
TT
6054
6055 return abbrev;
6056}
6057
f4dc4d17
DE
6058/* Fetch the abbreviation table offset from a comp or type unit header. */
6059
6060static sect_offset
ed2dc618
SM
6061read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6062 struct dwarf2_section_info *section,
9c541725 6063 sect_offset sect_off)
f4dc4d17 6064{
96b79293 6065 bfd *abfd = section->get_bfd_owner ();
d521ce57 6066 const gdb_byte *info_ptr;
ac298888 6067 unsigned int initial_length_size, offset_size;
43988095 6068 uint16_t version;
f4dc4d17 6069
96b79293 6070 section->read (dwarf2_per_objfile->objfile);
9c541725 6071 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6072 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6073 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6074 info_ptr += initial_length_size;
6075
6076 version = read_2_bytes (abfd, info_ptr);
6077 info_ptr += 2;
6078 if (version >= 5)
6079 {
6080 /* Skip unit type and address size. */
6081 info_ptr += 2;
6082 }
6083
24aa364d 6084 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6085}
6086
b83470bf
TT
6087/* A partial symtab that is used only for include files. */
6088struct dwarf2_include_psymtab : public partial_symtab
6089{
6090 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6091 : partial_symtab (filename, objfile)
6092 {
6093 }
6094
6095 void read_symtab (struct objfile *objfile) override
6096 {
194d088f
TV
6097 /* It's an include file, no symbols to read for it.
6098 Everything is in the includer symtab. */
6099
6100 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6101 expansion of the includer psymtab. We use the dependencies[0] field to
6102 model the includer. But if we go the regular route of calling
6103 expand_psymtab here, and having expand_psymtab call expand_dependencies
6104 to expand the includer, we'll only use expand_psymtab on the includer
6105 (making it a non-toplevel psymtab), while if we expand the includer via
6106 another path, we'll use read_symtab (making it a toplevel psymtab).
6107 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6108 psymtab, and trigger read_symtab on the includer here directly. */
6109 includer ()->read_symtab (objfile);
b83470bf
TT
6110 }
6111
6112 void expand_psymtab (struct objfile *objfile) override
6113 {
194d088f
TV
6114 /* This is not called by read_symtab, and should not be called by any
6115 expand_dependencies. */
6116 gdb_assert (false);
b83470bf
TT
6117 }
6118
5717c425 6119 bool readin_p (struct objfile *objfile) const override
b83470bf 6120 {
5717c425 6121 return includer ()->readin_p (objfile);
b83470bf
TT
6122 }
6123
5717c425 6124 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6125 {
6126 return nullptr;
6127 }
6128
6129private:
194d088f
TV
6130 partial_symtab *includer () const
6131 {
6132 /* An include psymtab has exactly one dependency: the psymtab that
6133 includes it. */
6134 gdb_assert (this->number_of_dependencies == 1);
6135 return this->dependencies[0];
6136 }
b83470bf
TT
6137};
6138
aaa75496
JB
6139/* Allocate a new partial symtab for file named NAME and mark this new
6140 partial symtab as being an include of PST. */
6141
6142static void
891813be 6143dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6144 struct objfile *objfile)
6145{
b83470bf 6146 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6147
fbd9ab74 6148 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6149 subpst->dirname = pst->dirname;
fbd9ab74 6150
a9342b62 6151 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6152 subpst->dependencies[0] = pst;
6153 subpst->number_of_dependencies = 1;
aaa75496
JB
6154}
6155
6156/* Read the Line Number Program data and extract the list of files
6157 included by the source file represented by PST. Build an include
d85a05f0 6158 partial symtab for each of these included files. */
aaa75496
JB
6159
6160static void
6161dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6162 struct die_info *die,
891813be 6163 dwarf2_psymtab *pst)
aaa75496 6164{
fff8551c 6165 line_header_up lh;
d85a05f0 6166 struct attribute *attr;
aaa75496 6167
d85a05f0 6168 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6169 if (attr != nullptr)
9c541725 6170 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6171 if (lh == NULL)
6172 return; /* No linetable, so no includes. */
6173
79748972
TT
6174 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6175 that we pass in the raw text_low here; that is ok because we're
6176 only decoding the line table to make include partial symtabs, and
6177 so the addresses aren't really used. */
4ae976d1 6178 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6179 pst->raw_text_low (), 1);
aaa75496
JB
6180}
6181
348e048f 6182static hashval_t
52dc124a 6183hash_signatured_type (const void *item)
348e048f 6184{
9a3c8263
SM
6185 const struct signatured_type *sig_type
6186 = (const struct signatured_type *) item;
9a619af0 6187
348e048f 6188 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6189 return sig_type->signature;
348e048f
DE
6190}
6191
6192static int
52dc124a 6193eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6194{
9a3c8263
SM
6195 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6196 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6197
348e048f
DE
6198 return lhs->signature == rhs->signature;
6199}
6200
1fd400ff
TT
6201/* Allocate a hash table for signatured types. */
6202
b0b6a987 6203static htab_up
298e9637 6204allocate_signatured_type_table ()
1fd400ff 6205{
b0b6a987
TT
6206 return htab_up (htab_create_alloc (41,
6207 hash_signatured_type,
6208 eq_signatured_type,
6209 NULL, xcalloc, xfree));
1fd400ff
TT
6210}
6211
d467dd73 6212/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6213
6214static int
d467dd73 6215add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6216{
9a3c8263 6217 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6218 std::vector<signatured_type *> *all_type_units
6219 = (std::vector<signatured_type *> *) datum;
1fd400ff 6220
b2bdb8cf 6221 all_type_units->push_back (sigt);
1fd400ff
TT
6222
6223 return 1;
6224}
6225
78d4d2c5 6226/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6227 and fill them into TYPES_HTAB. It will process only type units,
6228 therefore DW_UT_type. */
c88ee1f0 6229
78d4d2c5 6230static void
ed2dc618
SM
6231create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6232 struct dwo_file *dwo_file,
b0b6a987 6233 dwarf2_section_info *section, htab_up &types_htab,
43988095 6234 rcuh_kind section_kind)
348e048f 6235{
3019eac3 6236 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6237 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6238 bfd *abfd;
6239 const gdb_byte *info_ptr, *end_ptr;
348e048f 6240
4bdcc0c1
DE
6241 abbrev_section = (dwo_file != NULL
6242 ? &dwo_file->sections.abbrev
5989a64e 6243 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6244
b4f54984 6245 if (dwarf_read_debug)
43988095 6246 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6247 section->get_name (),
6248 abbrev_section->get_file_name ());
09406207 6249
96b79293 6250 section->read (objfile);
78d4d2c5 6251 info_ptr = section->buffer;
348e048f 6252
78d4d2c5
JK
6253 if (info_ptr == NULL)
6254 return;
348e048f 6255
78d4d2c5
JK
6256 /* We can't set abfd until now because the section may be empty or
6257 not present, in which case the bfd is unknown. */
96b79293 6258 abfd = section->get_bfd_owner ();
348e048f 6259
c0ab21c2
TT
6260 /* We don't use cutu_reader here because we don't need to read
6261 any dies: the signature is in the header. */
3019eac3 6262
78d4d2c5
JK
6263 end_ptr = info_ptr + section->size;
6264 while (info_ptr < end_ptr)
6265 {
78d4d2c5
JK
6266 struct signatured_type *sig_type;
6267 struct dwo_unit *dwo_tu;
6268 void **slot;
6269 const gdb_byte *ptr = info_ptr;
6270 struct comp_unit_head header;
6271 unsigned int length;
8b70b953 6272
9c541725 6273 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6274
a49dd8dd
JK
6275 /* Initialize it due to a false compiler warning. */
6276 header.signature = -1;
9c541725 6277 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6278
78d4d2c5
JK
6279 /* We need to read the type's signature in order to build the hash
6280 table, but we don't need anything else just yet. */
348e048f 6281
ed2dc618 6282 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6283 abbrev_section, ptr, section_kind);
348e048f 6284
4057dfde 6285 length = header.get_length ();
6caca83c 6286
78d4d2c5
JK
6287 /* Skip dummy type units. */
6288 if (ptr >= info_ptr + length
43988095
JK
6289 || peek_abbrev_code (abfd, ptr) == 0
6290 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6291 {
6292 info_ptr += length;
6293 continue;
6294 }
dee91e82 6295
78d4d2c5
JK
6296 if (types_htab == NULL)
6297 {
6298 if (dwo_file)
298e9637 6299 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6300 else
298e9637 6301 types_htab = allocate_signatured_type_table ();
78d4d2c5 6302 }
8b70b953 6303
78d4d2c5
JK
6304 if (dwo_file)
6305 {
6306 sig_type = NULL;
5989a64e 6307 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6308 struct dwo_unit);
6309 dwo_tu->dwo_file = dwo_file;
43988095 6310 dwo_tu->signature = header.signature;
9c541725 6311 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6312 dwo_tu->section = section;
9c541725 6313 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6314 dwo_tu->length = length;
6315 }
6316 else
6317 {
6318 /* N.B.: type_offset is not usable if this type uses a DWO file.
6319 The real type_offset is in the DWO file. */
6320 dwo_tu = NULL;
5989a64e 6321 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6322 sig_type->signature = header.signature;
9c541725 6323 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6324 sig_type->per_cu.is_debug_types = 1;
6325 sig_type->per_cu.section = section;
9c541725 6326 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6327 sig_type->per_cu.length = length;
6328 }
6329
b0b6a987 6330 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6331 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6332 INSERT);
6333 gdb_assert (slot != NULL);
6334 if (*slot != NULL)
6335 {
9c541725 6336 sect_offset dup_sect_off;
0349ea22 6337
3019eac3
DE
6338 if (dwo_file)
6339 {
78d4d2c5
JK
6340 const struct dwo_unit *dup_tu
6341 = (const struct dwo_unit *) *slot;
6342
9c541725 6343 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6344 }
6345 else
6346 {
78d4d2c5
JK
6347 const struct signatured_type *dup_tu
6348 = (const struct signatured_type *) *slot;
6349
9c541725 6350 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6351 }
8b70b953 6352
b98664d3 6353 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6354 " the entry at offset %s, signature %s"),
6355 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6356 hex_string (header.signature));
78d4d2c5
JK
6357 }
6358 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6359
78d4d2c5 6360 if (dwarf_read_debug > 1)
9d8780f0
SM
6361 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6362 sect_offset_str (sect_off),
43988095 6363 hex_string (header.signature));
3019eac3 6364
78d4d2c5
JK
6365 info_ptr += length;
6366 }
6367}
3019eac3 6368
78d4d2c5
JK
6369/* Create the hash table of all entries in the .debug_types
6370 (or .debug_types.dwo) section(s).
6371 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6372 otherwise it is NULL.
b3c8eb43 6373
78d4d2c5 6374 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6375
78d4d2c5 6376 Note: This function processes DWO files only, not DWP files. */
348e048f 6377
78d4d2c5 6378static void
ed2dc618
SM
6379create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6380 struct dwo_file *dwo_file,
fd5866f6 6381 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6382 htab_up &types_htab)
78d4d2c5 6383{
fd5866f6
SM
6384 for (dwarf2_section_info &section : type_sections)
6385 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6386 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6387}
6388
6389/* Create the hash table of all entries in the .debug_types section,
6390 and initialize all_type_units.
6391 The result is zero if there is an error (e.g. missing .debug_types section),
6392 otherwise non-zero. */
6393
6394static int
ed2dc618 6395create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6396{
b0b6a987 6397 htab_up types_htab;
3019eac3 6398
ed2dc618 6399 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6400 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6401 rcuh_kind::COMPILE);
ed2dc618 6402 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6403 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6404 if (types_htab == NULL)
6405 {
5989a64e 6406 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6407 return 0;
6408 }
6409
5989a64e 6410 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6411
5989a64e
SM
6412 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6413 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6414 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6415
5989a64e 6416 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6417 add_signatured_type_cu_to_table,
5989a64e 6418 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6419
348e048f
DE
6420 return 1;
6421}
6422
5989a64e 6423/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6424 If SLOT is non-NULL, it is the entry to use in the hash table.
6425 Otherwise we find one. */
6426
6427static struct signatured_type *
ed2dc618
SM
6428add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6429 void **slot)
6aa5f3a6 6430{
5989a64e
SM
6431 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6432 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6433 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6434
5989a64e 6435 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6436
af758d11
SM
6437 dwarf2_per_objfile->resize_symtabs ();
6438
5989a64e 6439 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6440 sig_type->signature = sig;
6441 sig_type->per_cu.is_debug_types = 1;
5989a64e 6442 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6443 {
6444 sig_type->per_cu.v.quick =
5989a64e 6445 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6446 struct dwarf2_per_cu_quick_data);
6447 }
6448
6449 if (slot == NULL)
6450 {
5989a64e 6451 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6452 sig_type, INSERT);
6453 }
6454 gdb_assert (*slot == NULL);
6455 *slot = sig_type;
6456 /* The rest of sig_type must be filled in by the caller. */
6457 return sig_type;
6458}
6459
a2ce51a0
DE
6460/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6461 Fill in SIG_ENTRY with DWO_ENTRY. */
6462
6463static void
ed2dc618 6464fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6465 struct signatured_type *sig_entry,
6466 struct dwo_unit *dwo_entry)
6467{
1859c670
SM
6468 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6469
7ee85ab1 6470 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6471 gdb_assert (! sig_entry->per_cu.queued);
6472 gdb_assert (sig_entry->per_cu.cu == NULL);
1859c670 6473 if (per_bfd->using_index)
6aa5f3a6
DE
6474 {
6475 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6476 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6477 }
6478 else
6479 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6480 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6481 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6482 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6483 gdb_assert (sig_entry->dwo_unit == NULL);
6484
6485 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6486 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6487 sig_entry->per_cu.length = dwo_entry->length;
6488 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6489 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6490 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6491 sig_entry->dwo_unit = dwo_entry;
6492}
6493
6494/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6495 If we haven't read the TU yet, create the signatured_type data structure
6496 for a TU to be read in directly from a DWO file, bypassing the stub.
6497 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6498 using .gdb_index, then when reading a CU we want to stay in the DWO file
6499 containing that CU. Otherwise we could end up reading several other DWO
6500 files (due to comdat folding) to process the transitive closure of all the
6501 mentioned TUs, and that can be slow. The current DWO file will have every
6502 type signature that it needs.
a2ce51a0
DE
6503 We only do this for .gdb_index because in the psymtab case we already have
6504 to read all the DWOs to build the type unit groups. */
6505
6506static struct signatured_type *
6507lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6508{
5e22e966 6509 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6510 struct dwo_file *dwo_file;
6511 struct dwo_unit find_dwo_entry, *dwo_entry;
6512 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6513 void **slot;
a2ce51a0 6514
5989a64e 6515 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6516
6aa5f3a6
DE
6517 /* If TU skeletons have been removed then we may not have read in any
6518 TUs yet. */
5989a64e
SM
6519 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6520 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6521
6522 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6523 Use the global signatured_types array to do our own comdat-folding
6524 of types. If this is the first time we're reading this TU, and
6525 the TU has an entry in .gdb_index, replace the recorded data from
6526 .gdb_index with this TU. */
a2ce51a0 6527
a2ce51a0 6528 find_sig_entry.signature = sig;
5989a64e 6529 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6530 &find_sig_entry, INSERT);
9a3c8263 6531 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6532
6533 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6534 read. Don't reassign the global entry to point to this DWO if that's
6535 the case. Also note that if the TU is already being read, it may not
6536 have come from a DWO, the program may be a mix of Fission-compiled
6537 code and non-Fission-compiled code. */
6538
6539 /* Have we already tried to read this TU?
6540 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6541 needn't exist in the global table yet). */
6542 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6543 return sig_entry;
6544
6aa5f3a6
DE
6545 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6546 dwo_unit of the TU itself. */
6547 dwo_file = cu->dwo_unit->dwo_file;
6548
a2ce51a0
DE
6549 /* Ok, this is the first time we're reading this TU. */
6550 if (dwo_file->tus == NULL)
6551 return NULL;
6552 find_dwo_entry.signature = sig;
b0b6a987
TT
6553 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6554 &find_dwo_entry);
a2ce51a0
DE
6555 if (dwo_entry == NULL)
6556 return NULL;
6557
6aa5f3a6
DE
6558 /* If the global table doesn't have an entry for this TU, add one. */
6559 if (sig_entry == NULL)
ed2dc618 6560 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6561
ed2dc618 6562 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6563 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6564 return sig_entry;
6565}
6566
a2ce51a0
DE
6567/* Subroutine of lookup_signatured_type.
6568 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6569 then try the DWP file. If the TU stub (skeleton) has been removed then
6570 it won't be in .gdb_index. */
a2ce51a0
DE
6571
6572static struct signatured_type *
6573lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6574{
5e22e966 6575 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6576 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6577 struct dwo_unit *dwo_entry;
6578 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6579 void **slot;
a2ce51a0 6580
5989a64e 6581 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6582 gdb_assert (dwp_file != NULL);
6583
6aa5f3a6
DE
6584 /* If TU skeletons have been removed then we may not have read in any
6585 TUs yet. */
5989a64e
SM
6586 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6587 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6588
6aa5f3a6 6589 find_sig_entry.signature = sig;
5989a64e 6590 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6591 &find_sig_entry, INSERT);
9a3c8263 6592 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6593
6594 /* Have we already tried to read this TU?
6595 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6596 needn't exist in the global table yet). */
6597 if (sig_entry != NULL)
6598 return sig_entry;
6599
a2ce51a0
DE
6600 if (dwp_file->tus == NULL)
6601 return NULL;
ed2dc618 6602 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6603 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6604 if (dwo_entry == NULL)
6605 return NULL;
6606
ed2dc618
SM
6607 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6608 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6609
a2ce51a0
DE
6610 return sig_entry;
6611}
6612
380bca97 6613/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6614 Returns NULL if signature SIG is not present in the table.
6615 It is up to the caller to complain about this. */
348e048f
DE
6616
6617static struct signatured_type *
a2ce51a0 6618lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6619{
5e22e966 6620 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6621
a2ce51a0 6622 if (cu->dwo_unit
5989a64e 6623 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6624 {
6625 /* We're in a DWO/DWP file, and we're using .gdb_index.
6626 These cases require special processing. */
ed2dc618 6627 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6628 return lookup_dwo_signatured_type (cu, sig);
6629 else
6630 return lookup_dwp_signatured_type (cu, sig);
6631 }
6632 else
6633 {
6634 struct signatured_type find_entry, *entry;
348e048f 6635
5989a64e 6636 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6637 return NULL;
6638 find_entry.signature = sig;
9a3c8263 6639 entry = ((struct signatured_type *)
5989a64e 6640 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6641 &find_entry));
a2ce51a0
DE
6642 return entry;
6643 }
348e048f 6644}
18a8505e 6645
42e7ad6c 6646/* Low level DIE reading support. */
348e048f 6647
d85a05f0
DJ
6648/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6649
6650static void
6651init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6652 struct dwarf2_cu *cu,
3019eac3 6653 struct dwarf2_section_info *section,
685af9cd
TT
6654 struct dwo_file *dwo_file,
6655 struct abbrev_table *abbrev_table)
d85a05f0 6656{
fceca515 6657 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6658 reader->abfd = section->get_bfd_owner ();
d85a05f0 6659 reader->cu = cu;
3019eac3 6660 reader->dwo_file = dwo_file;
dee91e82
DE
6661 reader->die_section = section;
6662 reader->buffer = section->buffer;
f664829e 6663 reader->buffer_end = section->buffer + section->size;
685af9cd 6664 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6665}
6666
c0ab21c2 6667/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6668 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6669 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6670 already.
6671
6672 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6673 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6674 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6675 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6676 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6677 STUB_COMP_DIR may be non-NULL.
3e225074 6678 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6679 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6680 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6681 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6682 kept around for at least as long as *RESULT_READER.
6683
b0c7bfa9
DE
6684 The result is non-zero if a valid (non-dummy) DIE was found. */
6685
6686static int
4ab09049 6687read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6688 struct dwo_unit *dwo_unit,
b0c7bfa9 6689 struct die_info *stub_comp_unit_die,
a2ce51a0 6690 const char *stub_comp_dir,
b0c7bfa9 6691 struct die_reader_specs *result_reader,
d521ce57 6692 const gdb_byte **result_info_ptr,
b0c7bfa9 6693 struct die_info **result_comp_unit_die,
685af9cd 6694 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6695{
4ab09049
SM
6696 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6697 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6698 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6699 bfd *abfd;
d521ce57 6700 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6701 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6702 int i,num_extra_attrs;
6703 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6704 struct die_info *comp_unit_die;
6705
b0aeadb3
DE
6706 /* At most one of these may be provided. */
6707 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6708
b0c7bfa9
DE
6709 /* These attributes aren't processed until later:
6710 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6711 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6712 referenced later. However, these attributes are found in the stub
6713 which we won't have later. In order to not impose this complication
6714 on the rest of the code, we read them here and copy them to the
6715 DWO CU/TU die. */
b0c7bfa9
DE
6716
6717 stmt_list = NULL;
6718 low_pc = NULL;
6719 high_pc = NULL;
6720 ranges = NULL;
6721 comp_dir = NULL;
6722
6723 if (stub_comp_unit_die != NULL)
6724 {
6725 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6726 DWO file. */
4ab09049 6727 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6728 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6729 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6730 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6731 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6732 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6733
a39fdb41 6734 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6735
18a8505e
AT
6736 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6737 here (if needed). We need the value before we can process
6738 DW_AT_ranges. */
a39fdb41 6739 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6740 }
a2ce51a0
DE
6741 else if (stub_comp_dir != NULL)
6742 {
6743 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6744 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6745 comp_dir->name = DW_AT_comp_dir;
6746 comp_dir->form = DW_FORM_string;
6747 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6748 DW_STRING (comp_dir) = stub_comp_dir;
6749 }
b0c7bfa9
DE
6750
6751 /* Set up for reading the DWO CU/TU. */
6752 cu->dwo_unit = dwo_unit;
685af9cd 6753 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6754 section->read (objfile);
6755 abfd = section->get_bfd_owner ();
9c541725
PA
6756 begin_info_ptr = info_ptr = (section->buffer
6757 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6758 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6759
4ab09049 6760 if (per_cu->is_debug_types)
b0c7bfa9 6761 {
4ab09049 6762 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6763
ed2dc618
SM
6764 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6765 &cu->header, section,
b0c7bfa9 6766 dwo_abbrev_section,
43988095 6767 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6768 /* This is not an assert because it can be caused by bad debug info. */
43988095 6769 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6770 {
6771 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6772 " TU at offset %s [in module %s]"),
a2ce51a0 6773 hex_string (sig_type->signature),
43988095 6774 hex_string (cu->header.signature),
9d8780f0 6775 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6776 bfd_get_filename (abfd));
6777 }
9c541725 6778 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6779 /* For DWOs coming from DWP files, we don't know the CU length
6780 nor the type's offset in the TU until now. */
4057dfde 6781 dwo_unit->length = cu->header.get_length ();
9c541725 6782 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6783
6784 /* Establish the type offset that can be used to lookup the type.
6785 For DWO files, we don't know it until now. */
9c541725
PA
6786 sig_type->type_offset_in_section
6787 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6788 }
6789 else
6790 {
ed2dc618
SM
6791 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6792 &cu->header, section,
b0c7bfa9 6793 dwo_abbrev_section,
43988095 6794 info_ptr, rcuh_kind::COMPILE);
9c541725 6795 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6796 /* For DWOs coming from DWP files, we don't know the CU length
6797 until now. */
4057dfde 6798 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6799 }
6800
685af9cd 6801 *result_dwo_abbrev_table
86de1d91
TT
6802 = abbrev_table::read (objfile, dwo_abbrev_section,
6803 cu->header.abbrev_sect_off);
685af9cd
TT
6804 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6805 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6806
6807 /* Read in the die, but leave space to copy over the attributes
6808 from the stub. This has the benefit of simplifying the rest of
6809 the code - all the work to maintain the illusion of a single
6810 DW_TAG_{compile,type}_unit DIE is done here. */
6811 num_extra_attrs = ((stmt_list != NULL)
6812 + (low_pc != NULL)
6813 + (high_pc != NULL)
6814 + (ranges != NULL)
6815 + (comp_dir != NULL));
6816 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6817 num_extra_attrs);
b0c7bfa9
DE
6818
6819 /* Copy over the attributes from the stub to the DIE we just read in. */
6820 comp_unit_die = *result_comp_unit_die;
6821 i = comp_unit_die->num_attrs;
6822 if (stmt_list != NULL)
6823 comp_unit_die->attrs[i++] = *stmt_list;
6824 if (low_pc != NULL)
6825 comp_unit_die->attrs[i++] = *low_pc;
6826 if (high_pc != NULL)
6827 comp_unit_die->attrs[i++] = *high_pc;
6828 if (ranges != NULL)
6829 comp_unit_die->attrs[i++] = *ranges;
6830 if (comp_dir != NULL)
6831 comp_unit_die->attrs[i++] = *comp_dir;
6832 comp_unit_die->num_attrs += num_extra_attrs;
6833
b4f54984 6834 if (dwarf_die_debug)
bf6af496
DE
6835 {
6836 fprintf_unfiltered (gdb_stdlog,
6837 "Read die from %s@0x%x of %s:\n",
96b79293 6838 section->get_name (),
bf6af496
DE
6839 (unsigned) (begin_info_ptr - section->buffer),
6840 bfd_get_filename (abfd));
b4f54984 6841 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6842 }
6843
b0c7bfa9
DE
6844 /* Skip dummy compilation units. */
6845 if (info_ptr >= begin_info_ptr + dwo_unit->length
6846 || peek_abbrev_code (abfd, info_ptr) == 0)
6847 return 0;
6848
6849 *result_info_ptr = info_ptr;
6850 return 1;
6851}
6852
a084a2a6
AT
6853/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6854 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6855 signature is part of the header. */
6856static gdb::optional<ULONGEST>
6857lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6858{
6859 if (cu->header.version >= 5)
6860 return cu->header.signature;
6861 struct attribute *attr;
6862 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6863 if (attr == nullptr)
6864 return gdb::optional<ULONGEST> ();
6865 return DW_UNSND (attr);
6866}
6867
c0ab21c2 6868/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6869 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6870 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6871
6872static struct dwo_unit *
4ab09049 6873lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6874{
4ab09049 6875 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6876 struct dwo_unit *dwo_unit;
c0ab21c2 6877 const char *comp_dir;
b0c7bfa9 6878
a2ce51a0
DE
6879 gdb_assert (cu != NULL);
6880
b0c7bfa9 6881 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6882 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6883 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6884
4ab09049
SM
6885 if (per_cu->is_debug_types)
6886 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6887 else
6888 {
a084a2a6 6889 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6890
a084a2a6 6891 if (!signature.has_value ())
b0c7bfa9
DE
6892 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6893 " [in module %s]"),
4ab09049
SM
6894 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6895
6896 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6897 }
6898
b0c7bfa9
DE
6899 return dwo_unit;
6900}
6901
c0ab21c2 6902/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6903 See it for a description of the parameters.
fcd3b13d 6904 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6905
c0ab21c2 6906void
9e021579
SM
6907cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6908 dwarf2_per_objfile *per_objfile,
6751ebae 6909 int use_existing_cu)
a2ce51a0 6910{
a2ce51a0 6911 struct signatured_type *sig_type;
a2ce51a0
DE
6912
6913 /* Verify we can do the following downcast, and that we have the
6914 data we need. */
6915 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6916 sig_type = (struct signatured_type *) this_cu;
6917 gdb_assert (sig_type->dwo_unit != NULL);
6918
6aa5f3a6
DE
6919 if (use_existing_cu && this_cu->cu != NULL)
6920 {
6921 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6922 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6923 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6924 }
6925 else
6926 {
6927 /* If !use_existing_cu, this_cu->cu must be NULL. */
6928 gdb_assert (this_cu->cu == NULL);
9e021579 6929 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6aa5f3a6
DE
6930 }
6931
6932 /* A future optimization, if needed, would be to use an existing
6933 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6934 could share abbrev tables. */
a2ce51a0 6935
4ab09049 6936 if (read_cutu_die_from_dwo (this_cu->cu, sig_type->dwo_unit,
a2ce51a0
DE
6937 NULL /* stub_comp_unit_die */,
6938 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6939 this, &info_ptr,
3e225074 6940 &comp_unit_die,
c0ab21c2 6941 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6942 {
6943 /* Dummy die. */
c0ab21c2 6944 dummy_p = true;
a2ce51a0 6945 }
a2ce51a0
DE
6946}
6947
fd820528 6948/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6949 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6950
f4dc4d17
DE
6951 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6952 Otherwise the table specified in the comp unit header is read in and used.
6953 This is an optimization for when we already have the abbrev table.
6954
dee91e82 6955 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6956 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6957
ab432490
SM
6958cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6959 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6960 struct abbrev_table *abbrev_table,
6751ebae 6961 int use_existing_cu,
c0ab21c2
TT
6962 bool skip_partial)
6963 : die_reader_specs {},
6751ebae 6964 m_this_cu (this_cu)
c906108c 6965{
dee91e82 6966 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6967 struct dwarf2_section_info *section = this_cu->section;
96b79293 6968 bfd *abfd = section->get_bfd_owner ();
dee91e82 6969 struct dwarf2_cu *cu;
c0ab21c2 6970 const gdb_byte *begin_info_ptr;
dee91e82 6971 struct signatured_type *sig_type = NULL;
4bdcc0c1 6972 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6973 /* Non-zero if CU currently points to a DWO file and we need to
6974 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6975 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6976 int rereading_dwo_cu = 0;
c906108c 6977
b4f54984 6978 if (dwarf_die_debug)
9d8780f0 6979 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6980 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6981 sect_offset_str (this_cu->sect_off));
09406207 6982
a2ce51a0
DE
6983 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6984 file (instead of going through the stub), short-circuit all of this. */
6985 if (this_cu->reading_dwo_directly)
6986 {
6987 /* Narrow down the scope of possibilities to have to understand. */
6988 gdb_assert (this_cu->is_debug_types);
6989 gdb_assert (abbrev_table == NULL);
9e021579 6990 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
a2ce51a0
DE
6991 return;
6992 }
6993
dee91e82 6994 /* This is cheap if the section is already read in. */
96b79293 6995 section->read (objfile);
dee91e82 6996
9c541725 6997 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6998
6999 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
7000
7001 if (use_existing_cu && this_cu->cu != NULL)
7002 {
7003 cu = this_cu->cu;
42e7ad6c
DE
7004 /* If this CU is from a DWO file we need to start over, we need to
7005 refetch the attributes from the skeleton CU.
7006 This could be optimized by retrieving those attributes from when we
7007 were here the first time: the previous comp_unit_die was stored in
7008 comp_unit_obstack. But there's no data yet that we need this
7009 optimization. */
7010 if (cu->dwo_unit != NULL)
7011 rereading_dwo_cu = 1;
dee91e82
DE
7012 }
7013 else
7014 {
7015 /* If !use_existing_cu, this_cu->cu must be NULL. */
7016 gdb_assert (this_cu->cu == NULL);
9e021579 7017 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 7018 cu = m_new_cu.get ();
42e7ad6c 7019 }
dee91e82 7020
b0c7bfa9 7021 /* Get the header. */
9c541725 7022 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7023 {
7024 /* We already have the header, there's no need to read it in again. */
9c541725 7025 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7026 }
7027 else
7028 {
3019eac3 7029 if (this_cu->is_debug_types)
dee91e82 7030 {
ed2dc618
SM
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
4bdcc0c1 7033 abbrev_section, info_ptr,
43988095 7034 rcuh_kind::TYPE);
dee91e82 7035
42e7ad6c
DE
7036 /* Since per_cu is the first member of struct signatured_type,
7037 we can go from a pointer to one to a pointer to the other. */
7038 sig_type = (struct signatured_type *) this_cu;
43988095 7039 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7040 gdb_assert (sig_type->type_offset_in_tu
7041 == cu->header.type_cu_offset_in_tu);
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7043
42e7ad6c
DE
7044 /* LENGTH has not been set yet for type units if we're
7045 using .gdb_index. */
4057dfde 7046 this_cu->length = cu->header.get_length ();
3019eac3
DE
7047
7048 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7049 sig_type->type_offset_in_section =
7050 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7051
7052 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7053 }
7054 else
7055 {
ed2dc618
SM
7056 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7057 &cu->header, section,
4bdcc0c1 7058 abbrev_section,
43988095
JK
7059 info_ptr,
7060 rcuh_kind::COMPILE);
dee91e82 7061
9c541725 7062 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7063 if (this_cu->length == 0)
7064 this_cu->length = cu->header.get_length ();
7065 else
7066 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7067 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7068 }
7069 }
10b3939b 7070
6caca83c 7071 /* Skip dummy compilation units. */
dee91e82 7072 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7073 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7074 {
7075 dummy_p = true;
7076 return;
7077 }
6caca83c 7078
433df2d4
DE
7079 /* If we don't have them yet, read the abbrevs for this compilation unit.
7080 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7081 done. */
f4dc4d17 7082 if (abbrev_table != NULL)
685af9cd
TT
7083 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7084 else
f4dc4d17 7085 {
c0ab21c2 7086 m_abbrev_table_holder
86de1d91
TT
7087 = abbrev_table::read (objfile, abbrev_section,
7088 cu->header.abbrev_sect_off);
c0ab21c2 7089 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7090 }
af703f96 7091
dee91e82 7092 /* Read the top level CU/TU die. */
c0ab21c2 7093 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7095
58f0c718 7096 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7097 {
7098 dummy_p = true;
7099 return;
7100 }
58f0c718 7101
b0c7bfa9 7102 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7103 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7104 table from the DWO file and pass the ownership over to us. It will be
7105 referenced from READER, so we must make sure to free it after we're done
7106 with READER.
7107
b0c7bfa9
DE
7108 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7109 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7110 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7111 if (dwo_name != nullptr)
3019eac3 7112 {
3019eac3 7113 struct dwo_unit *dwo_unit;
b0c7bfa9 7114 struct die_info *dwo_comp_unit_die;
3019eac3 7115
3e225074 7116 if (comp_unit_die->has_children)
6a506a2d 7117 {
b98664d3 7118 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7119 " has children (offset %s) [in module %s]"),
7120 sect_offset_str (this_cu->sect_off),
7121 bfd_get_filename (abfd));
6a506a2d 7122 }
4ab09049 7123 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7124 if (dwo_unit != NULL)
3019eac3 7125 {
4ab09049 7126 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7127 comp_unit_die, NULL,
c0ab21c2 7128 this, &info_ptr,
3e225074 7129 &dwo_comp_unit_die,
c0ab21c2 7130 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7131 {
7132 /* Dummy die. */
c0ab21c2 7133 dummy_p = true;
6a506a2d
DE
7134 return;
7135 }
7136 comp_unit_die = dwo_comp_unit_die;
7137 }
7138 else
7139 {
7140 /* Yikes, we couldn't find the rest of the DIE, we only have
7141 the stub. A complaint has already been logged. There's
7142 not much more we can do except pass on the stub DIE to
7143 die_reader_func. We don't want to throw an error on bad
7144 debug info. */
3019eac3
DE
7145 }
7146 }
c0ab21c2 7147}
3019eac3 7148
6751ebae
TT
7149void
7150cutu_reader::keep ()
c0ab21c2 7151{
b0c7bfa9 7152 /* Done, clean up. */
6751ebae
TT
7153 gdb_assert (!dummy_p);
7154 if (m_new_cu != NULL)
348e048f 7155 {
e3beb21d
SM
7156 /* We know that m_this_cu->cu is set, since we are in the process of
7157 parsing the CU. */
7158 gdb_assert (m_this_cu->cu != nullptr);
7159 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7160
fcd3b13d 7161 /* Link this CU into read_in_chain. */
5989a64e
SM
7162 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7163 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
fcd3b13d 7164 /* The chain owns it now. */
c0ab21c2 7165 m_new_cu.release ();
348e048f 7166 }
dee91e82
DE
7167}
7168
18a8505e
AT
7169/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7170 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7171 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7172
7173 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7174 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7175
7176 We fill in THIS_CU->length.
7177
dee91e82 7178 THIS_CU->cu is always freed when done.
3019eac3 7179 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7180 to care whether it refers to the "main" CU or the DWO CU.
7181
7182 When parent_cu is passed, it is used to provide a default value for
7183 str_offsets_base and addr_base from the parent. */
dee91e82 7184
ab432490
SM
7185cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7186 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7187 struct dwarf2_cu *parent_cu,
7188 struct dwo_file *dwo_file)
7189 : die_reader_specs {},
7190 m_this_cu (this_cu)
dee91e82
DE
7191{
7192 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7193 struct dwarf2_section_info *section = this_cu->section;
96b79293 7194 bfd *abfd = section->get_bfd_owner ();
33e80786 7195 struct dwarf2_section_info *abbrev_section;
d521ce57 7196 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7197
b4f54984 7198 if (dwarf_die_debug)
9d8780f0 7199 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7200 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7201 sect_offset_str (this_cu->sect_off));
09406207 7202
dee91e82
DE
7203 gdb_assert (this_cu->cu == NULL);
7204
33e80786
DE
7205 abbrev_section = (dwo_file != NULL
7206 ? &dwo_file->sections.abbrev
7207 : get_abbrev_section_for_cu (this_cu));
7208
dee91e82 7209 /* This is cheap if the section is already read in. */
96b79293 7210 section->read (objfile);
dee91e82 7211
9e021579 7212 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7213
9c541725 7214 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7215 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7216 &m_new_cu->header, section,
4bdcc0c1 7217 abbrev_section, info_ptr,
43988095
JK
7218 (this_cu->is_debug_types
7219 ? rcuh_kind::TYPE
7220 : rcuh_kind::COMPILE));
dee91e82 7221
18a8505e
AT
7222 if (parent_cu != nullptr)
7223 {
c0ab21c2
TT
7224 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7225 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7226 }
4057dfde 7227 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7228
7229 /* Skip dummy compilation units. */
7230 if (info_ptr >= begin_info_ptr + this_cu->length
7231 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7232 {
7233 dummy_p = true;
7234 return;
7235 }
72bf9492 7236
c0ab21c2 7237 m_abbrev_table_holder
86de1d91
TT
7238 = abbrev_table::read (objfile, abbrev_section,
7239 m_new_cu->header.abbrev_sect_off);
dee91e82 7240
c0ab21c2
TT
7241 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7242 m_abbrev_table_holder.get ());
3e225074 7243 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7244}
7245
0018ea6f
DE
7246\f
7247/* Type Unit Groups.
dee91e82 7248
0018ea6f
DE
7249 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7250 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7251 so that all types coming from the same compilation (.o file) are grouped
7252 together. A future step could be to put the types in the same symtab as
7253 the CU the types ultimately came from. */
ff013f42 7254
f4dc4d17
DE
7255static hashval_t
7256hash_type_unit_group (const void *item)
7257{
9a3c8263
SM
7258 const struct type_unit_group *tu_group
7259 = (const struct type_unit_group *) item;
f4dc4d17 7260
094b34ac 7261 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7262}
348e048f
DE
7263
7264static int
f4dc4d17 7265eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7266{
9a3c8263
SM
7267 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7268 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7269
094b34ac 7270 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7271}
348e048f 7272
f4dc4d17
DE
7273/* Allocate a hash table for type unit groups. */
7274
eaa5fa8b 7275static htab_up
298e9637 7276allocate_type_unit_groups_table ()
f4dc4d17 7277{
eaa5fa8b
TT
7278 return htab_up (htab_create_alloc (3,
7279 hash_type_unit_group,
7280 eq_type_unit_group,
7281 NULL, xcalloc, xfree));
f4dc4d17 7282}
dee91e82 7283
f4dc4d17
DE
7284/* Type units that don't have DW_AT_stmt_list are grouped into their own
7285 partial symtabs. We combine several TUs per psymtab to not let the size
7286 of any one psymtab grow too big. */
7287#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7288#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7289
094b34ac 7290/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7291 Create the type_unit_group object used to hold one or more TUs. */
7292
7293static struct type_unit_group *
094b34ac 7294create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7295{
5e22e966 7296 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7297 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7298 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7299 struct type_unit_group *tu_group;
f4dc4d17 7300
5989a64e 7301 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7302 struct type_unit_group);
094b34ac 7303 per_cu = &tu_group->per_cu;
1859c670 7304 per_cu->per_bfd = per_bfd;
f4dc4d17 7305
1859c670 7306 if (per_bfd->using_index)
094b34ac 7307 {
1859c670 7308 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7309 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7310 }
7311 else
7312 {
9c541725 7313 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7314 dwarf2_psymtab *pst;
528e1572 7315 std::string name;
094b34ac
DE
7316
7317 /* Give the symtab a useful name for debug purposes. */
7318 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7319 name = string_printf ("<type_units_%d>",
7320 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7321 else
528e1572 7322 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7323
7aa104c4 7324 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7325 pst->anonymous = true;
094b34ac 7326 }
f4dc4d17 7327
094b34ac 7328 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7329 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7330
7331 return tu_group;
7332}
7333
094b34ac
DE
7334/* Look up the type_unit_group for type unit CU, and create it if necessary.
7335 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7336
7337static struct type_unit_group *
ff39bb5e 7338get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7339{
5e22e966 7340 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7341 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7342 struct type_unit_group *tu_group;
7343 void **slot;
7344 unsigned int line_offset;
7345 struct type_unit_group type_unit_group_for_lookup;
7346
5989a64e
SM
7347 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7348 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7349
7350 /* Do we need to create a new group, or can we use an existing one? */
7351
7352 if (stmt_list)
7353 {
7354 line_offset = DW_UNSND (stmt_list);
7355 ++tu_stats->nr_symtab_sharers;
7356 }
7357 else
7358 {
7359 /* Ugh, no stmt_list. Rare, but we have to handle it.
7360 We can do various things here like create one group per TU or
7361 spread them over multiple groups to split up the expansion work.
7362 To avoid worst case scenarios (too many groups or too large groups)
7363 we, umm, group them in bunches. */
7364 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7365 | (tu_stats->nr_stmt_less_type_units
7366 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7367 ++tu_stats->nr_stmt_less_type_units;
7368 }
7369
094b34ac 7370 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7371 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7372 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7373 &type_unit_group_for_lookup, INSERT);
7374 if (*slot != NULL)
7375 {
9a3c8263 7376 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7377 gdb_assert (tu_group != NULL);
7378 }
7379 else
7380 {
9c541725 7381 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7382 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7383 *slot = tu_group;
7384 ++tu_stats->nr_symtabs;
7385 }
7386
7387 return tu_group;
7388}
0018ea6f
DE
7389\f
7390/* Partial symbol tables. */
7391
7392/* Create a psymtab named NAME and assign it to PER_CU.
7393
7394 The caller must fill in the following details:
7395 dirname, textlow, texthigh. */
7396
891813be 7397static dwarf2_psymtab *
7aa104c4
SM
7398create_partial_symtab (dwarf2_per_cu_data *per_cu,
7399 dwarf2_per_objfile *per_objfile,
7400 const char *name)
0018ea6f 7401{
7aa104c4 7402 struct objfile *objfile = per_objfile->objfile;
891813be 7403 dwarf2_psymtab *pst;
0018ea6f 7404
9f4e76a4 7405 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7406
6d94535f 7407 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7408
7409 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7410 per_cu->v.psymtab = pst;
7411
7412 return pst;
7413}
7414
c0ab21c2 7415/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7416
7417static void
7418process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7419 const gdb_byte *info_ptr,
0018ea6f 7420 struct die_info *comp_unit_die,
c0ab21c2 7421 enum language pretend_language)
0018ea6f
DE
7422{
7423 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7424 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7425 struct objfile *objfile = per_objfile->objfile;
08feed99 7426 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7427 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7428 CORE_ADDR baseaddr;
7429 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7430 dwarf2_psymtab *pst;
3a2b436a 7431 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7432 const char *filename;
0018ea6f 7433
0018ea6f
DE
7434 gdb_assert (! per_cu->is_debug_types);
7435
c0ab21c2 7436 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7437
0018ea6f 7438 /* Allocate a new partial symbol table structure. */
2e927613
TV
7439 gdb::unique_xmalloc_ptr<char> debug_filename;
7440 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7441 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7442 if (filename == NULL)
0018ea6f 7443 filename = "";
2e927613
TV
7444 else if (strcmp (filename, artificial) == 0)
7445 {
7446 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7447 sect_offset_str (per_cu->sect_off),
7448 (char *) NULL));
2e927613
TV
7449 filename = debug_filename.get ();
7450 }
0018ea6f 7451
7aa104c4 7452 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7453
7454 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7455 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7456
b3b3bada 7457 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7458
7459 dwarf2_find_base_address (comp_unit_die, cu);
7460
7461 /* Possibly set the default values of LOWPC and HIGHPC from
7462 `DW_AT_ranges'. */
3a2b436a
JK
7463 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7464 &best_highpc, cu, pst);
7465 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7466 {
7467 CORE_ADDR low
7468 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7469 - baseaddr);
7470 CORE_ADDR high
7471 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7472 - baseaddr - 1);
7473 /* Store the contiguous range if it is not empty; it can be
7474 empty for CUs with no code. */
d320c2b5
TT
7475 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7476 low, high, pst);
79748972 7477 }
0018ea6f
DE
7478
7479 /* Check if comp unit has_children.
7480 If so, read the rest of the partial symbols from this comp unit.
7481 If not, there's no more debug_info for this comp unit. */
3e225074 7482 if (comp_unit_die->has_children)
0018ea6f
DE
7483 {
7484 struct partial_die_info *first_die;
7485 CORE_ADDR lowpc, highpc;
7486
7487 lowpc = ((CORE_ADDR) -1);
7488 highpc = ((CORE_ADDR) 0);
7489
7490 first_die = load_partial_dies (reader, info_ptr, 1);
7491
7492 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7493 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7494
7495 /* If we didn't find a lowpc, set it to highpc to avoid
7496 complaints from `maint check'. */
7497 if (lowpc == ((CORE_ADDR) -1))
7498 lowpc = highpc;
7499
7500 /* If the compilation unit didn't have an explicit address range,
7501 then use the information extracted from its child dies. */
e385593e 7502 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7503 {
7504 best_lowpc = lowpc;
7505 best_highpc = highpc;
7506 }
7507 }
4ae976d1 7508 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7509 best_lowpc + baseaddr)
7510 - baseaddr);
4ae976d1 7511 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7512 best_highpc + baseaddr)
7513 - baseaddr);
0018ea6f 7514
8763cede 7515 end_psymtab_common (objfile, pst);
0018ea6f 7516
ae640021 7517 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7518 {
7519 int i;
ae640021 7520 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7521
7522 /* Fill in 'dependencies' here; we fill in 'users' in a
7523 post-pass. */
7524 pst->number_of_dependencies = len;
a9342b62
TT
7525 pst->dependencies
7526 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7527 for (i = 0; i < len; ++i)
7528 {
7529 pst->dependencies[i]
7530 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7531 }
0018ea6f 7532
ae640021 7533 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7534 }
7535
7536 /* Get the list of files included in the current compilation unit,
7537 and build a psymtab for each of them. */
7538 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7539
b4f54984 7540 if (dwarf_read_debug)
b926417a
TT
7541 fprintf_unfiltered (gdb_stdlog,
7542 "Psymtab for %s unit @%s: %s - %s"
7543 ", %d global, %d static syms\n",
7544 per_cu->is_debug_types ? "type" : "comp",
7545 sect_offset_str (per_cu->sect_off),
7546 paddress (gdbarch, pst->text_low (objfile)),
7547 paddress (gdbarch, pst->text_high (objfile)),
7548 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7549}
7550
7551/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7552 Process compilation unit THIS_CU for a psymtab. */
7553
7554static void
ab432490
SM
7555process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7556 dwarf2_per_objfile *per_objfile,
135f5437 7557 bool want_partial_unit,
b93601f3 7558 enum language pretend_language)
0018ea6f
DE
7559{
7560 /* If this compilation unit was already read in, free the
7561 cached copy in order to read it in again. This is
7562 necessary because we skipped some symbols when we first
7563 read in the compilation unit (see load_partial_dies).
7564 This problem could be avoided, but the benefit is unclear. */
7565 if (this_cu->cu != NULL)
120ce1b5 7566 free_one_cached_comp_unit (this_cu, per_objfile);
0018ea6f 7567
ab432490 7568 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
c0ab21c2 7569
58990295
TV
7570 switch (reader.comp_unit_die->tag)
7571 {
7572 case DW_TAG_compile_unit:
7573 this_cu->unit_type = DW_UT_compile;
7574 break;
7575 case DW_TAG_partial_unit:
7576 this_cu->unit_type = DW_UT_partial;
7577 break;
7578 default:
7579 abort ();
7580 }
7581
c0ab21c2 7582 if (reader.dummy_p)
f1902523 7583 {
c0ab21c2 7584 /* Nothing. */
f1902523 7585 }
c0ab21c2 7586 else if (this_cu->is_debug_types)
3e225074
TT
7587 build_type_psymtabs_reader (&reader, reader.info_ptr,
7588 reader.comp_unit_die);
135f5437
TT
7589 else if (want_partial_unit
7590 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7591 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7592 reader.comp_unit_die,
c0ab21c2 7593 pretend_language);
0018ea6f 7594
58990295
TV
7595 this_cu->lang = this_cu->cu->language;
7596
0018ea6f 7597 /* Age out any secondary CUs. */
f6e649dd 7598 age_cached_comp_units (per_objfile);
0018ea6f 7599}
f4dc4d17
DE
7600
7601/* Reader function for build_type_psymtabs. */
7602
7603static void
7604build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7605 const gdb_byte *info_ptr,
3e225074 7606 struct die_info *type_unit_die)
f4dc4d17 7607{
5e22e966 7608 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7609 struct objfile *objfile = dwarf2_per_objfile->objfile;
7610 struct dwarf2_cu *cu = reader->cu;
7611 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7612 struct signatured_type *sig_type;
f4dc4d17
DE
7613 struct type_unit_group *tu_group;
7614 struct attribute *attr;
7615 struct partial_die_info *first_die;
7616 CORE_ADDR lowpc, highpc;
891813be 7617 dwarf2_psymtab *pst;
f4dc4d17 7618
0186c6a7
DE
7619 gdb_assert (per_cu->is_debug_types);
7620 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7621
3e225074 7622 if (! type_unit_die->has_children)
f4dc4d17
DE
7623 return;
7624
052c8bb8 7625 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7626 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7627
df07e2c7 7628 if (tu_group->tus == nullptr)
a8b3b8e9 7629 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7630 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7631
7632 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7633 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7634 pst->anonymous = true;
f4dc4d17
DE
7635
7636 first_die = load_partial_dies (reader, info_ptr, 1);
7637
7638 lowpc = (CORE_ADDR) -1;
7639 highpc = (CORE_ADDR) 0;
7640 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7641
8763cede 7642 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7643}
7644
73051182
DE
7645/* Struct used to sort TUs by their abbreviation table offset. */
7646
7647struct tu_abbrev_offset
7648{
b2bdb8cf
SM
7649 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7650 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7651 {}
7652
7653 signatured_type *sig_type;
73051182
DE
7654 sect_offset abbrev_offset;
7655};
7656
484cf504 7657/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7658
484cf504
TT
7659static bool
7660sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7661 const struct tu_abbrev_offset &b)
73051182 7662{
484cf504 7663 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7664}
7665
7666/* Efficiently read all the type units.
7667 This does the bulk of the work for build_type_psymtabs.
7668
7669 The efficiency is because we sort TUs by the abbrev table they use and
7670 only read each abbrev table once. In one program there are 200K TUs
7671 sharing 8K abbrev tables.
7672
7673 The main purpose of this function is to support building the
5989a64e 7674 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7675 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7676 can collapse the search space by grouping them by stmt_list.
7677 The savings can be significant, in the same program from above the 200K TUs
7678 share 8K stmt_list tables.
7679
7680 FUNC is expected to call get_type_unit_group, which will create the
7681 struct type_unit_group if necessary and add it to
5989a64e 7682 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7683
7684static void
ed2dc618 7685build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7686{
5989a64e 7687 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7688 abbrev_table_up abbrev_table;
73051182 7689 sect_offset abbrev_offset;
73051182
DE
7690
7691 /* It's up to the caller to not call us multiple times. */
5989a64e 7692 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7693
5989a64e 7694 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7695 return;
7696
7697 /* TUs typically share abbrev tables, and there can be way more TUs than
7698 abbrev tables. Sort by abbrev table to reduce the number of times we
7699 read each abbrev table in.
7700 Alternatives are to punt or to maintain a cache of abbrev tables.
7701 This is simpler and efficient enough for now.
7702
7703 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7704 symtab to use). Typically TUs with the same abbrev offset have the same
7705 stmt_list value too so in practice this should work well.
7706
7707 The basic algorithm here is:
7708
7709 sort TUs by abbrev table
7710 for each TU with same abbrev table:
7711 read abbrev table if first user
7712 read TU top level DIE
7713 [IWBN if DWO skeletons had DW_AT_stmt_list]
7714 call FUNC */
7715
b4f54984 7716 if (dwarf_read_debug)
73051182
DE
7717 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7718
7719 /* Sort in a separate table to maintain the order of all_type_units
7720 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7721 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7722 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7723
5989a64e 7724 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7725 sorted_by_abbrev.emplace_back
7726 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7727 sig_type->per_cu.section,
7728 sig_type->per_cu.sect_off));
73051182 7729
484cf504
TT
7730 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7731 sort_tu_by_abbrev_offset);
73051182 7732
9c541725 7733 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7734
b2bdb8cf 7735 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7736 {
73051182
DE
7737 /* Switch to the next abbrev table if necessary. */
7738 if (abbrev_table == NULL
b2bdb8cf 7739 || tu.abbrev_offset != abbrev_offset)
73051182 7740 {
b2bdb8cf 7741 abbrev_offset = tu.abbrev_offset;
73051182 7742 abbrev_table =
86de1d91 7743 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7744 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7745 abbrev_offset);
73051182
DE
7746 ++tu_stats->nr_uniq_abbrev_tables;
7747 }
7748
ab432490
SM
7749 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7750 abbrev_table.get (), 0, false);
c0ab21c2
TT
7751 if (!reader.dummy_p)
7752 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7753 reader.comp_unit_die);
73051182 7754 }
6aa5f3a6 7755}
73051182 7756
6aa5f3a6
DE
7757/* Print collected type unit statistics. */
7758
7759static void
ed2dc618 7760print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7761{
5989a64e 7762 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7763
7764 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7765 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7766 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7767 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7768 tu_stats->nr_uniq_abbrev_tables);
7769 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7770 tu_stats->nr_symtabs);
7771 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7772 tu_stats->nr_symtab_sharers);
7773 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7774 tu_stats->nr_stmt_less_type_units);
7775 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7776 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7777}
7778
f4dc4d17
DE
7779/* Traversal function for build_type_psymtabs. */
7780
7781static int
7782build_type_psymtab_dependencies (void **slot, void *info)
7783{
ed2dc618
SM
7784 struct dwarf2_per_objfile *dwarf2_per_objfile
7785 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7786 struct objfile *objfile = dwarf2_per_objfile->objfile;
7787 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7788 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7789 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7790 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7791 int i;
7792
7793 gdb_assert (len > 0);
197400e8 7794 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7795
7796 pst->number_of_dependencies = len;
a9342b62 7797 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7798 for (i = 0; i < len; ++i)
f4dc4d17 7799 {
df07e2c7 7800 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7801 gdb_assert (iter->per_cu.is_debug_types);
7802 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7803 iter->type_unit_group = tu_group;
f4dc4d17
DE
7804 }
7805
df07e2c7
AB
7806 delete tu_group->tus;
7807 tu_group->tus = nullptr;
348e048f
DE
7808
7809 return 1;
7810}
7811
7812/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7813 Build partial symbol tables for the .debug_types comp-units. */
7814
7815static void
ed2dc618 7816build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7817{
ed2dc618 7818 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7819 return;
7820
ed2dc618 7821 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7822}
f4dc4d17 7823
6aa5f3a6
DE
7824/* Traversal function for process_skeletonless_type_unit.
7825 Read a TU in a DWO file and build partial symbols for it. */
7826
7827static int
7828process_skeletonless_type_unit (void **slot, void *info)
7829{
7830 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7831 struct dwarf2_per_objfile *dwarf2_per_objfile
7832 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7833 struct signatured_type find_entry, *entry;
7834
7835 /* If this TU doesn't exist in the global table, add it and read it in. */
7836
5989a64e
SM
7837 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7838 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7839
7840 find_entry.signature = dwo_unit->signature;
5989a64e 7841 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7842 &find_entry, INSERT);
6aa5f3a6
DE
7843 /* If we've already seen this type there's nothing to do. What's happening
7844 is we're doing our own version of comdat-folding here. */
7845 if (*slot != NULL)
7846 return 1;
7847
7848 /* This does the job that create_all_type_units would have done for
7849 this TU. */
ed2dc618
SM
7850 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7851 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7852 *slot = entry;
7853
7854 /* This does the job that build_type_psymtabs_1 would have done. */
ab432490 7855 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
7856 if (!reader.dummy_p)
7857 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7858 reader.comp_unit_die);
6aa5f3a6
DE
7859
7860 return 1;
7861}
7862
7863/* Traversal function for process_skeletonless_type_units. */
7864
7865static int
7866process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7867{
7868 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7869
7870 if (dwo_file->tus != NULL)
b0b6a987
TT
7871 htab_traverse_noresize (dwo_file->tus.get (),
7872 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7873
7874 return 1;
7875}
7876
7877/* Scan all TUs of DWO files, verifying we've processed them.
7878 This is needed in case a TU was emitted without its skeleton.
7879 Note: This can't be done until we know what all the DWO files are. */
7880
7881static void
ed2dc618 7882process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7883{
7884 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7885 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7886 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7887 {
5989a64e 7888 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7889 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7890 dwarf2_per_objfile);
6aa5f3a6 7891 }
348e048f
DE
7892}
7893
ed2dc618 7894/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7895
7896static void
ed2dc618 7897set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7898{
5989a64e 7899 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7900 {
891813be 7901 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7902
36586728
TT
7903 if (pst == NULL)
7904 continue;
7905
b76e467d 7906 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7907 {
7908 /* Set the 'user' field only if it is not already set. */
7909 if (pst->dependencies[j]->user == NULL)
7910 pst->dependencies[j]->user = pst;
7911 }
7912 }
7913}
7914
93311388
DE
7915/* Build the partial symbol table by doing a quick pass through the
7916 .debug_info and .debug_abbrev sections. */
72bf9492 7917
93311388 7918static void
ed2dc618 7919dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7920{
ed2dc618 7921 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7922
b4f54984 7923 if (dwarf_read_debug)
45cfd468
DE
7924 {
7925 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7926 objfile_name (objfile));
45cfd468
DE
7927 }
7928
76935768 7929 scoped_restore restore_reading_psyms
5989a64e 7930 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7931 true);
98bfdba5 7932
5989a64e 7933 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7934
93311388
DE
7935 /* Any cached compilation units will be linked by the per-objfile
7936 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7937 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7938
ed2dc618 7939 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7940
ed2dc618 7941 create_all_comp_units (dwarf2_per_objfile);
c906108c 7942
60606b2c
TT
7943 /* Create a temporary address map on a temporary obstack. We later
7944 copy this to the final obstack. */
8268c778 7945 auto_obstack temp_obstack;
791afaa2
TT
7946
7947 scoped_restore save_psymtabs_addrmap
d320c2b5 7948 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7949 addrmap_create_mutable (&temp_obstack));
72bf9492 7950
5989a64e 7951 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7952 {
7953 if (per_cu->v.psymtab != NULL)
7954 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7955 continue;
ab432490
SM
7956 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7957 language_minimal);
3d5afab3 7958 }
ff013f42 7959
6aa5f3a6 7960 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7961 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7962
7963 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7964 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7965 {
5989a64e 7966 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7967 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7968 }
7969
b4f54984 7970 if (dwarf_read_debug)
ed2dc618 7971 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7972
ed2dc618 7973 set_partial_user (dwarf2_per_objfile);
95554aad 7974
d320c2b5
TT
7975 objfile->partial_symtabs->psymtabs_addrmap
7976 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7977 objfile->partial_symtabs->obstack ());
791afaa2
TT
7978 /* At this point we want to keep the address map. */
7979 save_psymtabs_addrmap.release ();
ff013f42 7980
b4f54984 7981 if (dwarf_read_debug)
45cfd468 7982 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7983 objfile_name (objfile));
ae038cb0
DJ
7984}
7985
dee91e82
DE
7986/* Load the partial DIEs for a secondary CU into memory.
7987 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7988
dee91e82 7989static void
ab432490
SM
7990load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7991 dwarf2_per_objfile *per_objfile)
dee91e82 7992{
ab432490 7993 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
c0ab21c2
TT
7994
7995 if (!reader.dummy_p)
7996 {
7997 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7998 language_minimal);
7999
8000 /* Check if comp unit has_children.
8001 If so, read the rest of the partial symbols from this comp unit.
8002 If not, there's no more debug_info for this comp unit. */
3e225074 8003 if (reader.comp_unit_die->has_children)
c0ab21c2 8004 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8005
8006 reader.keep ();
c0ab21c2 8007 }
ae038cb0
DJ
8008}
8009
ae038cb0 8010static void
ed2dc618 8011read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8012 struct dwarf2_section_info *section,
f1902523 8013 struct dwarf2_section_info *abbrev_section,
b76e467d 8014 unsigned int is_dwz)
ae038cb0 8015{
d521ce57 8016 const gdb_byte *info_ptr;
ed2dc618 8017 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8018
b4f54984 8019 if (dwarf_read_debug)
bf6af496 8020 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8021 section->get_name (),
8022 section->get_file_name ());
bf6af496 8023
96b79293 8024 section->read (objfile);
ae038cb0 8025
36586728 8026 info_ptr = section->buffer;
6e70227d 8027
36586728 8028 while (info_ptr < section->buffer + section->size)
ae038cb0 8029 {
ae038cb0 8030 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8031
9c541725 8032 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8033
f1902523 8034 comp_unit_head cu_header;
ed2dc618
SM
8035 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8036 abbrev_section, info_ptr,
8037 rcuh_kind::COMPILE);
ae038cb0
DJ
8038
8039 /* Save the compilation unit for later lookup. */
f1902523 8040 if (cu_header.unit_type != DW_UT_type)
5989a64e 8041 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8042 else
8043 {
5989a64e 8044 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8045 sig_type->signature = cu_header.signature;
8046 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8047 this_cu = &sig_type->per_cu;
8048 }
8049 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8050 this_cu->sect_off = sect_off;
f1902523 8051 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8052 this_cu->is_dwz = is_dwz;
8a0459fd 8053 this_cu->section = section;
ae038cb0 8054
5989a64e 8055 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8056
8057 info_ptr = info_ptr + this_cu->length;
8058 }
36586728
TT
8059}
8060
8061/* Create a list of all compilation units in OBJFILE.
8062 This is only done for -readnow and building partial symtabs. */
8063
8064static void
ed2dc618 8065create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8066{
5989a64e
SM
8067 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8068 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8069 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8070
c3699833 8071 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8072 if (dwz != NULL)
ed2dc618 8073 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8074 1);
c906108c
SS
8075}
8076
5734ee8b 8077/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8078 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8079 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8080 DW_AT_ranges). See the comments of add_partial_subprogram on how
8081 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8082
72bf9492
DJ
8083static void
8084scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8085 CORE_ADDR *highpc, int set_addrmap,
8086 struct dwarf2_cu *cu)
c906108c 8087{
72bf9492 8088 struct partial_die_info *pdi;
c906108c 8089
91c24f0a
DC
8090 /* Now, march along the PDI's, descending into ones which have
8091 interesting children but skipping the children of the other ones,
8092 until we reach the end of the compilation unit. */
c906108c 8093
72bf9492 8094 pdi = first_die;
91c24f0a 8095
72bf9492
DJ
8096 while (pdi != NULL)
8097 {
52356b79 8098 pdi->fixup (cu);
c906108c 8099
f55ee35c 8100 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8101 children, so we need to look at them. Ditto for anonymous
8102 enums. */
933c6fe4 8103
72bf9492 8104 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8105 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8106 || pdi->tag == DW_TAG_imported_unit
8107 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8108 {
72bf9492 8109 switch (pdi->tag)
c906108c
SS
8110 {
8111 case DW_TAG_subprogram:
b1dc1806 8112 case DW_TAG_inlined_subroutine:
cdc07690 8113 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8114 break;
72929c62 8115 case DW_TAG_constant:
c906108c
SS
8116 case DW_TAG_variable:
8117 case DW_TAG_typedef:
91c24f0a 8118 case DW_TAG_union_type:
317d2668
TV
8119 if (!pdi->is_declaration
8120 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8121 {
72bf9492 8122 add_partial_symbol (pdi, cu);
63d06c5c
DC
8123 }
8124 break;
c906108c 8125 case DW_TAG_class_type:
680b30c7 8126 case DW_TAG_interface_type:
c906108c 8127 case DW_TAG_structure_type:
72bf9492 8128 if (!pdi->is_declaration)
c906108c 8129 {
72bf9492 8130 add_partial_symbol (pdi, cu);
c906108c 8131 }
b7fee5a3
KS
8132 if ((cu->language == language_rust
8133 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8134 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8135 set_addrmap, cu);
c906108c 8136 break;
91c24f0a 8137 case DW_TAG_enumeration_type:
72bf9492
DJ
8138 if (!pdi->is_declaration)
8139 add_partial_enumeration (pdi, cu);
c906108c
SS
8140 break;
8141 case DW_TAG_base_type:
a02abb62 8142 case DW_TAG_subrange_type:
c906108c 8143 /* File scope base type definitions are added to the partial
c5aa993b 8144 symbol table. */
72bf9492 8145 add_partial_symbol (pdi, cu);
c906108c 8146 break;
d9fa45fe 8147 case DW_TAG_namespace:
cdc07690 8148 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8149 break;
5d7cb8df 8150 case DW_TAG_module:
59c35742
AB
8151 if (!pdi->is_declaration)
8152 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8153 break;
95554aad
TT
8154 case DW_TAG_imported_unit:
8155 {
8156 struct dwarf2_per_cu_data *per_cu;
8157
f4dc4d17
DE
8158 /* For now we don't handle imported units in type units. */
8159 if (cu->per_cu->is_debug_types)
8160 {
8161 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8162 " supported in type units [in module %s]"),
5e22e966 8163 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8164 }
8165
e3b94546 8166 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8167 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8168
8169 /* Go read the partial unit, if needed. */
8170 if (per_cu->v.psymtab == NULL)
ab432490
SM
8171 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8172 cu->language);
95554aad 8173
ae640021 8174 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8175 }
8176 break;
74921315
KS
8177 case DW_TAG_imported_declaration:
8178 add_partial_symbol (pdi, cu);
8179 break;
c906108c
SS
8180 default:
8181 break;
8182 }
8183 }
8184
72bf9492
DJ
8185 /* If the die has a sibling, skip to the sibling. */
8186
8187 pdi = pdi->die_sibling;
8188 }
8189}
8190
8191/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8192
72bf9492 8193 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8194 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8195 Enumerators are an exception; they use the scope of their parent
8196 enumeration type, i.e. the name of the enumeration type is not
8197 prepended to the enumerator.
91c24f0a 8198
72bf9492
DJ
8199 There are two complexities. One is DW_AT_specification; in this
8200 case "parent" means the parent of the target of the specification,
8201 instead of the direct parent of the DIE. The other is compilers
8202 which do not emit DW_TAG_namespace; in this case we try to guess
8203 the fully qualified name of structure types from their members'
8204 linkage names. This must be done using the DIE's children rather
8205 than the children of any DW_AT_specification target. We only need
8206 to do this for structures at the top level, i.e. if the target of
8207 any DW_AT_specification (if any; otherwise the DIE itself) does not
8208 have a parent. */
8209
8210/* Compute the scope prefix associated with PDI's parent, in
8211 compilation unit CU. The result will be allocated on CU's
8212 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8213 field. NULL is returned if no prefix is necessary. */
15d034d0 8214static const char *
72bf9492
DJ
8215partial_die_parent_scope (struct partial_die_info *pdi,
8216 struct dwarf2_cu *cu)
8217{
15d034d0 8218 const char *grandparent_scope;
72bf9492 8219 struct partial_die_info *parent, *real_pdi;
91c24f0a 8220
72bf9492
DJ
8221 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8222 then this means the parent of the specification DIE. */
8223
8224 real_pdi = pdi;
72bf9492 8225 while (real_pdi->has_specification)
fb816e8b 8226 {
122cf0f2
AB
8227 auto res = find_partial_die (real_pdi->spec_offset,
8228 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8229 real_pdi = res.pdi;
8230 cu = res.cu;
8231 }
72bf9492
DJ
8232
8233 parent = real_pdi->die_parent;
8234 if (parent == NULL)
8235 return NULL;
8236
8237 if (parent->scope_set)
8238 return parent->scope;
8239
52356b79 8240 parent->fixup (cu);
72bf9492 8241
10b3939b 8242 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8243
acebe513
UW
8244 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8245 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8246 Work around this problem here. */
8247 if (cu->language == language_cplus
6e70227d 8248 && parent->tag == DW_TAG_namespace
acebe513
UW
8249 && strcmp (parent->name, "::") == 0
8250 && grandparent_scope == NULL)
8251 {
8252 parent->scope = NULL;
8253 parent->scope_set = 1;
8254 return NULL;
8255 }
8256
0a4b0913 8257 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8258 if (pdi->tag == DW_TAG_enumerator)
8259 /* Enumerators should not get the name of the enumeration as a prefix. */
8260 parent->scope = grandparent_scope;
8261 else if (parent->tag == DW_TAG_namespace
f55ee35c 8262 || parent->tag == DW_TAG_module
72bf9492
DJ
8263 || parent->tag == DW_TAG_structure_type
8264 || parent->tag == DW_TAG_class_type
680b30c7 8265 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8266 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8267 || parent->tag == DW_TAG_enumeration_type
8268 || (cu->language == language_fortran
8269 && parent->tag == DW_TAG_subprogram
8270 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8271 {
8272 if (grandparent_scope == NULL)
8273 parent->scope = parent->name;
8274 else
3e43a32a
MS
8275 parent->scope = typename_concat (&cu->comp_unit_obstack,
8276 grandparent_scope,
f55ee35c 8277 parent->name, 0, cu);
72bf9492 8278 }
72bf9492
DJ
8279 else
8280 {
8281 /* FIXME drow/2004-04-01: What should we be doing with
8282 function-local names? For partial symbols, we should probably be
8283 ignoring them. */
fa9c3fa0
TT
8284 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8285 dwarf_tag_name (parent->tag),
8286 sect_offset_str (pdi->sect_off));
72bf9492 8287 parent->scope = grandparent_scope;
c906108c
SS
8288 }
8289
72bf9492
DJ
8290 parent->scope_set = 1;
8291 return parent->scope;
8292}
8293
8294/* Return the fully scoped name associated with PDI, from compilation unit
8295 CU. The result will be allocated with malloc. */
4568ecf9 8296
43816ebc 8297static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8298partial_die_full_name (struct partial_die_info *pdi,
8299 struct dwarf2_cu *cu)
8300{
15d034d0 8301 const char *parent_scope;
72bf9492 8302
98bfdba5
PA
8303 /* If this is a template instantiation, we can not work out the
8304 template arguments from partial DIEs. So, unfortunately, we have
8305 to go through the full DIEs. At least any work we do building
8306 types here will be reused if full symbols are loaded later. */
8307 if (pdi->has_template_arguments)
8308 {
52356b79 8309 pdi->fixup (cu);
98bfdba5
PA
8310
8311 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8312 {
8313 struct die_info *die;
8314 struct attribute attr;
8315 struct dwarf2_cu *ref_cu = cu;
8316
b64f50a1 8317 /* DW_FORM_ref_addr is using section offset. */
b4069958 8318 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8319 attr.form = DW_FORM_ref_addr;
9c541725 8320 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8321 die = follow_die_ref (NULL, &attr, &ref_cu);
8322
43816ebc 8323 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8324 }
8325 }
8326
72bf9492
DJ
8327 parent_scope = partial_die_parent_scope (pdi, cu);
8328 if (parent_scope == NULL)
8329 return NULL;
8330 else
43816ebc
TT
8331 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8332 pdi->name, 0, cu));
c906108c
SS
8333}
8334
8335static void
72bf9492 8336add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8337{
5e22e966 8338 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8339 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8340 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8341 CORE_ADDR addr = 0;
15d034d0 8342 const char *actual_name = NULL;
e142c38c
DJ
8343 CORE_ADDR baseaddr;
8344
b3b3bada 8345 baseaddr = objfile->text_section_offset ();
c906108c 8346
43816ebc
TT
8347 gdb::unique_xmalloc_ptr<char> built_actual_name
8348 = partial_die_full_name (pdi, cu);
15d034d0 8349 if (built_actual_name != NULL)
43816ebc 8350 actual_name = built_actual_name.get ();
63d06c5c 8351
72bf9492
DJ
8352 if (actual_name == NULL)
8353 actual_name = pdi->name;
8354
76e288d1
TT
8355 partial_symbol psymbol;
8356 memset (&psymbol, 0, sizeof (psymbol));
8357 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8358 psymbol.ginfo.section = -1;
8359
8360 /* The code below indicates that the psymbol should be installed by
8361 setting this. */
8362 gdb::optional<psymbol_placement> where;
8363
c906108c
SS
8364 switch (pdi->tag)
8365 {
b1dc1806 8366 case DW_TAG_inlined_subroutine:
c906108c 8367 case DW_TAG_subprogram:
79748972
TT
8368 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8369 - baseaddr);
0a4b0913
AB
8370 if (pdi->is_external
8371 || cu->language == language_ada
8372 || (cu->language == language_fortran
8373 && pdi->die_parent != NULL
8374 && pdi->die_parent->tag == DW_TAG_subprogram))
8375 {
8376 /* Normally, only "external" DIEs are part of the global scope.
8377 But in Ada and Fortran, we want to be able to access nested
8378 procedures globally. So all Ada and Fortran subprograms are
8379 stored in the global scope. */
76e288d1 8380 where = psymbol_placement::GLOBAL;
c906108c
SS
8381 }
8382 else
76e288d1
TT
8383 where = psymbol_placement::STATIC;
8384
8385 psymbol.domain = VAR_DOMAIN;
8386 psymbol.aclass = LOC_BLOCK;
8387 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8388 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8389
8390 if (pdi->main_subprogram && actual_name != NULL)
8391 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8392 break;
72929c62 8393 case DW_TAG_constant:
76e288d1
TT
8394 psymbol.domain = VAR_DOMAIN;
8395 psymbol.aclass = LOC_STATIC;
8396 where = (pdi->is_external
8397 ? psymbol_placement::GLOBAL
8398 : psymbol_placement::STATIC);
72929c62 8399 break;
c906108c 8400 case DW_TAG_variable:
95554aad
TT
8401 if (pdi->d.locdesc)
8402 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8403
95554aad 8404 if (pdi->d.locdesc
caac4577 8405 && addr == 0
5989a64e 8406 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8407 {
8408 /* A global or static variable may also have been stripped
8409 out by the linker if unused, in which case its address
8410 will be nullified; do not add such variables into partial
8411 symbol table then. */
8412 }
8413 else if (pdi->is_external)
c906108c
SS
8414 {
8415 /* Global Variable.
8416 Don't enter into the minimal symbol tables as there is
8417 a minimal symbol table entry from the ELF symbols already.
8418 Enter into partial symbol table if it has a location
8419 descriptor or a type.
8420 If the location descriptor is missing, new_symbol will create
8421 a LOC_UNRESOLVED symbol, the address of the variable will then
8422 be determined from the minimal symbol table whenever the variable
8423 is referenced.
8424 The address for the partial symbol table entry is not
8425 used by GDB, but it comes in handy for debugging partial symbol
8426 table building. */
8427
95554aad 8428 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8429 {
8430 psymbol.domain = VAR_DOMAIN;
8431 psymbol.aclass = LOC_STATIC;
8432 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8433 psymbol.ginfo.value.address = addr;
8434 where = psymbol_placement::GLOBAL;
8435 }
c906108c
SS
8436 }
8437 else
8438 {
ff908ebf
AW
8439 int has_loc = pdi->d.locdesc != NULL;
8440
8441 /* Static Variable. Skip symbols whose value we cannot know (those
8442 without location descriptors or constant values). */
8443 if (!has_loc && !pdi->has_const_value)
43816ebc 8444 return;
ff908ebf 8445
76e288d1
TT
8446 psymbol.domain = VAR_DOMAIN;
8447 psymbol.aclass = LOC_STATIC;
8448 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8449 if (has_loc)
8450 psymbol.ginfo.value.address = addr;
8451 where = psymbol_placement::STATIC;
c906108c
SS
8452 }
8453 break;
8454 case DW_TAG_typedef:
8455 case DW_TAG_base_type:
a02abb62 8456 case DW_TAG_subrange_type:
76e288d1
TT
8457 psymbol.domain = VAR_DOMAIN;
8458 psymbol.aclass = LOC_TYPEDEF;
8459 where = psymbol_placement::STATIC;
c906108c 8460 break;
74921315 8461 case DW_TAG_imported_declaration:
72bf9492 8462 case DW_TAG_namespace:
76e288d1
TT
8463 psymbol.domain = VAR_DOMAIN;
8464 psymbol.aclass = LOC_TYPEDEF;
8465 where = psymbol_placement::GLOBAL;
72bf9492 8466 break;
530e8392 8467 case DW_TAG_module:
a5fd13a9
BH
8468 /* With Fortran 77 there might be a "BLOCK DATA" module
8469 available without any name. If so, we skip the module as it
8470 doesn't bring any value. */
8471 if (actual_name != nullptr)
76e288d1
TT
8472 {
8473 psymbol.domain = MODULE_DOMAIN;
8474 psymbol.aclass = LOC_TYPEDEF;
8475 where = psymbol_placement::GLOBAL;
8476 }
530e8392 8477 break;
c906108c 8478 case DW_TAG_class_type:
680b30c7 8479 case DW_TAG_interface_type:
c906108c
SS
8480 case DW_TAG_structure_type:
8481 case DW_TAG_union_type:
8482 case DW_TAG_enumeration_type:
fa4028e9
JB
8483 /* Skip external references. The DWARF standard says in the section
8484 about "Structure, Union, and Class Type Entries": "An incomplete
8485 structure, union or class type is represented by a structure,
8486 union or class entry that does not have a byte size attribute
8487 and that has a DW_AT_declaration attribute." */
8488 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8489 return;
fa4028e9 8490
63d06c5c
DC
8491 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8492 static vs. global. */
76e288d1
TT
8493 psymbol.domain = STRUCT_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = (cu->language == language_cplus
8496 ? psymbol_placement::GLOBAL
8497 : psymbol_placement::STATIC);
c906108c
SS
8498 break;
8499 case DW_TAG_enumerator:
76e288d1
TT
8500 psymbol.domain = VAR_DOMAIN;
8501 psymbol.aclass = LOC_CONST;
8502 where = (cu->language == language_cplus
8503 ? psymbol_placement::GLOBAL
8504 : psymbol_placement::STATIC);
c906108c
SS
8505 break;
8506 default:
8507 break;
8508 }
76e288d1
TT
8509
8510 if (where.has_value ())
8511 {
f049a313
TT
8512 if (built_actual_name != nullptr)
8513 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8514 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8515 psymbol.ginfo.set_linkage_name (actual_name);
8516 else
8517 {
8518 psymbol.ginfo.set_demangled_name (actual_name,
8519 &objfile->objfile_obstack);
8520 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8521 }
76e288d1
TT
8522 add_psymbol_to_list (psymbol, *where, objfile);
8523 }
c906108c
SS
8524}
8525
5c4e30ca
DC
8526/* Read a partial die corresponding to a namespace; also, add a symbol
8527 corresponding to that namespace to the symbol table. NAMESPACE is
8528 the name of the enclosing namespace. */
91c24f0a 8529
72bf9492
DJ
8530static void
8531add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8532 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8533 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8534{
72bf9492 8535 /* Add a symbol for the namespace. */
e7c27a73 8536
72bf9492 8537 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8538
8539 /* Now scan partial symbols in that namespace. */
8540
91c24f0a 8541 if (pdi->has_children)
cdc07690 8542 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8543}
8544
5d7cb8df
JK
8545/* Read a partial die corresponding to a Fortran module. */
8546
8547static void
8548add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8549 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8550{
530e8392
KB
8551 /* Add a symbol for the namespace. */
8552
8553 add_partial_symbol (pdi, cu);
8554
f55ee35c 8555 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8556
8557 if (pdi->has_children)
cdc07690 8558 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8559}
8560
b1dc1806
XR
8561/* Read a partial die corresponding to a subprogram or an inlined
8562 subprogram and create a partial symbol for that subprogram.
8563 When the CU language allows it, this routine also defines a partial
8564 symbol for each nested subprogram that this subprogram contains.
8565 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8566 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8567
cdc07690
YQ
8568 PDI may also be a lexical block, in which case we simply search
8569 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8570 Again, this is only performed when the CU language allows this
8571 type of definitions. */
8572
8573static void
8574add_partial_subprogram (struct partial_die_info *pdi,
8575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8576 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8577{
b1dc1806 8578 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8579 {
8580 if (pdi->has_pc_info)
8581 {
8582 if (pdi->lowpc < *lowpc)
8583 *lowpc = pdi->lowpc;
8584 if (pdi->highpc > *highpc)
8585 *highpc = pdi->highpc;
cdc07690 8586 if (set_addrmap)
5734ee8b 8587 {
5e22e966 8588 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8589 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8590 CORE_ADDR baseaddr;
b926417a
TT
8591 CORE_ADDR this_highpc;
8592 CORE_ADDR this_lowpc;
5734ee8b 8593
b3b3bada 8594 baseaddr = objfile->text_section_offset ();
b926417a
TT
8595 this_lowpc
8596 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8597 pdi->lowpc + baseaddr)
8598 - baseaddr);
8599 this_highpc
8600 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8601 pdi->highpc + baseaddr)
8602 - baseaddr);
d320c2b5 8603 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8604 this_lowpc, this_highpc - 1,
9291a0cd 8605 cu->per_cu->v.psymtab);
5734ee8b 8606 }
481860b3
GB
8607 }
8608
8609 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8610 {
bc30ff58 8611 if (!pdi->is_declaration)
e8d05480
JB
8612 /* Ignore subprogram DIEs that do not have a name, they are
8613 illegal. Do not emit a complaint at this point, we will
8614 do so when we convert this psymtab into a symtab. */
8615 if (pdi->name)
8616 add_partial_symbol (pdi, cu);
bc30ff58
JB
8617 }
8618 }
6e70227d 8619
bc30ff58
JB
8620 if (! pdi->has_children)
8621 return;
8622
0a4b0913 8623 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8624 {
8625 pdi = pdi->die_child;
8626 while (pdi != NULL)
8627 {
52356b79 8628 pdi->fixup (cu);
bc30ff58 8629 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8630 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8631 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8632 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8633 pdi = pdi->die_sibling;
8634 }
8635 }
8636}
8637
91c24f0a
DC
8638/* Read a partial die corresponding to an enumeration type. */
8639
72bf9492
DJ
8640static void
8641add_partial_enumeration (struct partial_die_info *enum_pdi,
8642 struct dwarf2_cu *cu)
91c24f0a 8643{
72bf9492 8644 struct partial_die_info *pdi;
91c24f0a
DC
8645
8646 if (enum_pdi->name != NULL)
72bf9492
DJ
8647 add_partial_symbol (enum_pdi, cu);
8648
8649 pdi = enum_pdi->die_child;
8650 while (pdi)
91c24f0a 8651 {
72bf9492 8652 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8653 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8654 else
72bf9492
DJ
8655 add_partial_symbol (pdi, cu);
8656 pdi = pdi->die_sibling;
91c24f0a 8657 }
91c24f0a
DC
8658}
8659
6caca83c
CC
8660/* Return the initial uleb128 in the die at INFO_PTR. */
8661
8662static unsigned int
d521ce57 8663peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8664{
8665 unsigned int bytes_read;
8666
8667 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8668}
8669
685af9cd
TT
8670/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8671 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8672
4bb7a0a7
DJ
8673 Return the corresponding abbrev, or NULL if the number is zero (indicating
8674 an empty DIE). In either case *BYTES_READ will be set to the length of
8675 the initial number. */
8676
8677static struct abbrev_info *
685af9cd
TT
8678peek_die_abbrev (const die_reader_specs &reader,
8679 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8680{
685af9cd 8681 dwarf2_cu *cu = reader.cu;
5e22e966 8682 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8683 unsigned int abbrev_number
8684 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8685
8686 if (abbrev_number == 0)
8687 return NULL;
8688
685af9cd 8689 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8690 if (!abbrev)
8691 {
422b9917 8692 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8693 " at offset %s [in module %s]"),
422b9917 8694 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8695 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8696 }
8697
8698 return abbrev;
8699}
8700
93311388
DE
8701/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8702 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8703 DIE. Any children of the skipped DIEs will also be skipped. */
8704
d521ce57
TT
8705static const gdb_byte *
8706skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8707{
4bb7a0a7
DJ
8708 while (1)
8709 {
685af9cd
TT
8710 unsigned int bytes_read;
8711 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8712
4bb7a0a7
DJ
8713 if (abbrev == NULL)
8714 return info_ptr + bytes_read;
8715 else
dee91e82 8716 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8717 }
8718}
8719
93311388
DE
8720/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8721 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8722 abbrev corresponding to that skipped uleb128 should be passed in
8723 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8724 children. */
8725
d521ce57
TT
8726static const gdb_byte *
8727skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8728 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8729{
8730 unsigned int bytes_read;
8731 struct attribute attr;
dee91e82
DE
8732 bfd *abfd = reader->abfd;
8733 struct dwarf2_cu *cu = reader->cu;
d521ce57 8734 const gdb_byte *buffer = reader->buffer;
f664829e 8735 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8736 unsigned int form, i;
8737
8738 for (i = 0; i < abbrev->num_attrs; i++)
8739 {
8740 /* The only abbrev we care about is DW_AT_sibling. */
8741 if (abbrev->attrs[i].name == DW_AT_sibling)
8742 {
18a8505e
AT
8743 bool ignored;
8744 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8745 &ignored);
4bb7a0a7 8746 if (attr.form == DW_FORM_ref_addr)
b98664d3 8747 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8748 else
b9502d3f 8749 {
0826b30a 8750 sect_offset off = attr.get_ref_die_offset ();
9c541725 8751 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8752
8753 if (sibling_ptr < info_ptr)
b98664d3 8754 complaint (_("DW_AT_sibling points backwards"));
22869d73 8755 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8756 reader->die_section->overflow_complaint ();
b9502d3f
WN
8757 else
8758 return sibling_ptr;
8759 }
4bb7a0a7
DJ
8760 }
8761
8762 /* If it isn't DW_AT_sibling, skip this attribute. */
8763 form = abbrev->attrs[i].form;
8764 skip_attribute:
8765 switch (form)
8766 {
4bb7a0a7 8767 case DW_FORM_ref_addr:
ae411497
TT
8768 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8769 and later it is offset sized. */
8770 if (cu->header.version == 2)
8771 info_ptr += cu->header.addr_size;
8772 else
8773 info_ptr += cu->header.offset_size;
8774 break;
36586728
TT
8775 case DW_FORM_GNU_ref_alt:
8776 info_ptr += cu->header.offset_size;
8777 break;
ae411497 8778 case DW_FORM_addr:
4bb7a0a7
DJ
8779 info_ptr += cu->header.addr_size;
8780 break;
8781 case DW_FORM_data1:
8782 case DW_FORM_ref1:
8783 case DW_FORM_flag:
8fe0f950 8784 case DW_FORM_strx1:
4bb7a0a7
DJ
8785 info_ptr += 1;
8786 break;
2dc7f7b3 8787 case DW_FORM_flag_present:
43988095 8788 case DW_FORM_implicit_const:
2dc7f7b3 8789 break;
4bb7a0a7
DJ
8790 case DW_FORM_data2:
8791 case DW_FORM_ref2:
8fe0f950 8792 case DW_FORM_strx2:
4bb7a0a7
DJ
8793 info_ptr += 2;
8794 break;
8fe0f950
AT
8795 case DW_FORM_strx3:
8796 info_ptr += 3;
8797 break;
4bb7a0a7
DJ
8798 case DW_FORM_data4:
8799 case DW_FORM_ref4:
8fe0f950 8800 case DW_FORM_strx4:
4bb7a0a7
DJ
8801 info_ptr += 4;
8802 break;
8803 case DW_FORM_data8:
8804 case DW_FORM_ref8:
55f1336d 8805 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8806 info_ptr += 8;
8807 break;
0224619f
JK
8808 case DW_FORM_data16:
8809 info_ptr += 16;
8810 break;
4bb7a0a7 8811 case DW_FORM_string:
9b1c24c8 8812 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8813 info_ptr += bytes_read;
8814 break;
2dc7f7b3 8815 case DW_FORM_sec_offset:
4bb7a0a7 8816 case DW_FORM_strp:
36586728 8817 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8818 info_ptr += cu->header.offset_size;
8819 break;
2dc7f7b3 8820 case DW_FORM_exprloc:
4bb7a0a7
DJ
8821 case DW_FORM_block:
8822 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8823 info_ptr += bytes_read;
8824 break;
8825 case DW_FORM_block1:
8826 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8827 break;
8828 case DW_FORM_block2:
8829 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8830 break;
8831 case DW_FORM_block4:
8832 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8833 break;
336d760d 8834 case DW_FORM_addrx:
cf532bd1 8835 case DW_FORM_strx:
4bb7a0a7
DJ
8836 case DW_FORM_sdata:
8837 case DW_FORM_udata:
8838 case DW_FORM_ref_udata:
3019eac3
DE
8839 case DW_FORM_GNU_addr_index:
8840 case DW_FORM_GNU_str_index:
18a8505e 8841 case DW_FORM_rnglistx:
41144253 8842 case DW_FORM_loclistx:
d521ce57 8843 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8844 break;
8845 case DW_FORM_indirect:
8846 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8847 info_ptr += bytes_read;
8848 /* We need to continue parsing from here, so just go back to
8849 the top. */
8850 goto skip_attribute;
8851
8852 default:
3e43a32a
MS
8853 error (_("Dwarf Error: Cannot handle %s "
8854 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8855 dwarf_form_name (form),
8856 bfd_get_filename (abfd));
8857 }
8858 }
8859
8860 if (abbrev->has_children)
dee91e82 8861 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8862 else
8863 return info_ptr;
8864}
8865
93311388 8866/* Locate ORIG_PDI's sibling.
dee91e82 8867 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8868
d521ce57 8869static const gdb_byte *
dee91e82
DE
8870locate_pdi_sibling (const struct die_reader_specs *reader,
8871 struct partial_die_info *orig_pdi,
d521ce57 8872 const gdb_byte *info_ptr)
91c24f0a
DC
8873{
8874 /* Do we know the sibling already? */
72bf9492 8875
91c24f0a
DC
8876 if (orig_pdi->sibling)
8877 return orig_pdi->sibling;
8878
8879 /* Are there any children to deal with? */
8880
8881 if (!orig_pdi->has_children)
8882 return info_ptr;
8883
4bb7a0a7 8884 /* Skip the children the long way. */
91c24f0a 8885
dee91e82 8886 return skip_children (reader, info_ptr);
91c24f0a
DC
8887}
8888
257e7a09 8889/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8890 not NULL. */
c906108c 8891
891813be
TT
8892void
8893dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8894{
ed2dc618
SM
8895 struct dwarf2_per_objfile *dwarf2_per_objfile
8896 = get_dwarf2_per_objfile (objfile);
8897
af758d11
SM
8898 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8899
077cbab2
TT
8900 /* If this psymtab is constructed from a debug-only objfile, the
8901 has_section_at_zero flag will not necessarily be correct. We
8902 can get the correct value for this flag by looking at the data
8903 associated with the (presumably stripped) associated objfile. */
8904 if (objfile->separate_debug_objfile_backlink)
c906108c 8905 {
077cbab2
TT
8906 struct dwarf2_per_objfile *dpo_backlink
8907 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8908
5989a64e
SM
8909 dwarf2_per_objfile->per_bfd->has_section_at_zero
8910 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8911 }
98bfdba5 8912
8566b89b 8913 expand_psymtab (objfile);
95554aad 8914
ed2dc618 8915 process_cu_includes (dwarf2_per_objfile);
c906108c 8916}
9cdd5dbd
DE
8917\f
8918/* Reading in full CUs. */
c906108c 8919
10b3939b
DJ
8920/* Add PER_CU to the queue. */
8921
8922static void
120ce1b5
SM
8923queue_comp_unit (dwarf2_per_cu_data *per_cu,
8924 dwarf2_per_objfile *per_objfile,
95554aad 8925 enum language pretend_language)
10b3939b 8926{
10b3939b 8927 per_cu->queued = 1;
120ce1b5 8928 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8929}
8930
89e63ee4
DE
8931/* If PER_CU is not yet queued, add it to the queue.
8932 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8933 dependency.
0907af0c 8934 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8935 meaning either PER_CU is already queued or it is already loaded.
8936
8937 N.B. There is an invariant here that if a CU is queued then it is loaded.
8938 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8939
8940static int
89e63ee4 8941maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8942 dwarf2_per_cu_data *per_cu,
8943 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8944 enum language pretend_language)
8945{
8946 /* We may arrive here during partial symbol reading, if we need full
8947 DIEs to process an unusual case (e.g. template arguments). Do
8948 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8949 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c
DE
8950 {
8951 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8952 return 1;
8953 return 0;
8954 }
8955
8956 /* Mark the dependence relation so that we don't flush PER_CU
8957 too early. */
89e63ee4
DE
8958 if (dependent_cu != NULL)
8959 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8960
8961 /* If it's already on the queue, we have nothing to do. */
8962 if (per_cu->queued)
8963 return 0;
8964
8965 /* If the compilation unit is already loaded, just mark it as
8966 used. */
8967 if (per_cu->cu != NULL)
8968 {
8969 per_cu->cu->last_used = 0;
8970 return 0;
8971 }
8972
8973 /* Add it to the queue. */
120ce1b5 8974 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
8975
8976 return 1;
8977}
8978
10b3939b
DJ
8979/* Process the queue. */
8980
8981static void
ed2dc618 8982process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8983{
b4f54984 8984 if (dwarf_read_debug)
45cfd468
DE
8985 {
8986 fprintf_unfiltered (gdb_stdlog,
8987 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8988 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8989 }
8990
03dd20cc
DJ
8991 /* The queue starts out with one item, but following a DIE reference
8992 may load a new CU, adding it to the end of the queue. */
5989a64e 8993 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8994 {
5989a64e 8995 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
39856def 8996
af758d11 8997 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
cc12ce38 8998 /* Skip dummy CUs. */
39856def 8999 && item.per_cu->cu != NULL)
f4dc4d17 9000 {
39856def 9001 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 9002 unsigned int debug_print_threshold;
247f5c4f 9003 char buf[100];
f4dc4d17 9004
247f5c4f 9005 if (per_cu->is_debug_types)
f4dc4d17 9006 {
247f5c4f
DE
9007 struct signatured_type *sig_type =
9008 (struct signatured_type *) per_cu;
9009
9d8780f0 9010 sprintf (buf, "TU %s at offset %s",
73be47f5 9011 hex_string (sig_type->signature),
9d8780f0 9012 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9013 /* There can be 100s of TUs.
9014 Only print them in verbose mode. */
9015 debug_print_threshold = 2;
f4dc4d17 9016 }
247f5c4f 9017 else
73be47f5 9018 {
9d8780f0
SM
9019 sprintf (buf, "CU at offset %s",
9020 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9021 debug_print_threshold = 1;
9022 }
247f5c4f 9023
b4f54984 9024 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9025 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9026
9027 if (per_cu->is_debug_types)
47b14e86
SM
9028 process_full_type_unit (per_cu, dwarf2_per_objfile,
9029 item.pretend_language);
f4dc4d17 9030 else
47b14e86
SM
9031 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9032 item.pretend_language);
f4dc4d17 9033
b4f54984 9034 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9035 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9036 }
10b3939b 9037
39856def 9038 item.per_cu->queued = 0;
5989a64e 9039 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9040 }
9041
b4f54984 9042 if (dwarf_read_debug)
45cfd468
DE
9043 {
9044 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9045 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9046 }
10b3939b
DJ
9047}
9048
10b3939b
DJ
9049/* Read in full symbols for PST, and anything it depends on. */
9050
8566b89b
TT
9051void
9052dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9053{
af758d11 9054 gdb_assert (!readin_p (objfile));
95554aad 9055
48993951 9056 expand_dependencies (objfile);
aaa75496 9057
97a1449a
SM
9058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9059 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9060 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9061}
9062
af758d11
SM
9063/* See psympriv.h. */
9064
9065bool
9066dwarf2_psymtab::readin_p (struct objfile *objfile) const
9067{
9068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9069 return per_objfile->symtab_set_p (per_cu_data);
9070}
9071
9072/* See psympriv.h. */
9073
9074compunit_symtab *
9075dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9076{
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 return per_objfile->get_symtab (per_cu_data);
9079}
9080
dee91e82
DE
9081/* Trivial hash function for die_info: the hash value of a DIE
9082 is its offset in .debug_info for this objfile. */
10b3939b 9083
dee91e82
DE
9084static hashval_t
9085die_hash (const void *item)
10b3939b 9086{
9a3c8263 9087 const struct die_info *die = (const struct die_info *) item;
6502dd73 9088
9c541725 9089 return to_underlying (die->sect_off);
dee91e82 9090}
63d06c5c 9091
dee91e82
DE
9092/* Trivial comparison function for die_info structures: two DIEs
9093 are equal if they have the same offset. */
98bfdba5 9094
dee91e82
DE
9095static int
9096die_eq (const void *item_lhs, const void *item_rhs)
9097{
9a3c8263
SM
9098 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9099 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9100
9c541725 9101 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9102}
c906108c 9103
c0ab21c2 9104/* Load the DIEs associated with PER_CU into memory. */
c906108c 9105
dee91e82 9106static void
ab432490
SM
9107load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9108 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9109 bool skip_partial,
9110 enum language pretend_language)
dee91e82 9111{
c0ab21c2
TT
9112 gdb_assert (! this_cu->is_debug_types);
9113
ab432490 9114 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
c0ab21c2
TT
9115 if (reader.dummy_p)
9116 return;
9117
9118 struct dwarf2_cu *cu = reader.cu;
9119 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9120
dee91e82
DE
9121 gdb_assert (cu->die_hash == NULL);
9122 cu->die_hash =
9123 htab_create_alloc_ex (cu->header.length / 12,
9124 die_hash,
9125 die_eq,
9126 NULL,
9127 &cu->comp_unit_obstack,
9128 hashtab_obstack_allocate,
9129 dummy_obstack_deallocate);
e142c38c 9130
3e225074 9131 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9132 reader.comp_unit_die->child
9133 = read_die_and_siblings (&reader, reader.info_ptr,
9134 &info_ptr, reader.comp_unit_die);
9135 cu->dies = reader.comp_unit_die;
dee91e82 9136 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9137
9138 /* We try not to read any attributes in this function, because not
9cdd5dbd 9139 all CUs needed for references have been loaded yet, and symbol
10b3939b 9140 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9141 or we won't be able to build types correctly.
9142 Similarly, if we do not read the producer, we can not apply
9143 producer-specific interpretation. */
c0ab21c2 9144 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9145
9146 reader.keep ();
10b3939b
DJ
9147}
9148
3da10d80
KS
9149/* Add a DIE to the delayed physname list. */
9150
9151static void
9152add_to_method_list (struct type *type, int fnfield_index, int index,
9153 const char *name, struct die_info *die,
9154 struct dwarf2_cu *cu)
9155{
9156 struct delayed_method_info mi;
9157 mi.type = type;
9158 mi.fnfield_index = fnfield_index;
9159 mi.index = index;
9160 mi.name = name;
9161 mi.die = die;
c89b44cd 9162 cu->method_list.push_back (mi);
3da10d80
KS
9163}
9164
3693fdb3
PA
9165/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9166 "const" / "volatile". If so, decrements LEN by the length of the
9167 modifier and return true. Otherwise return false. */
9168
9169template<size_t N>
9170static bool
9171check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9172{
9173 size_t mod_len = sizeof (mod) - 1;
9174 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9175 {
9176 len -= mod_len;
9177 return true;
9178 }
9179 return false;
9180}
9181
3da10d80
KS
9182/* Compute the physnames of any methods on the CU's method list.
9183
9184 The computation of method physnames is delayed in order to avoid the
9185 (bad) condition that one of the method's formal parameters is of an as yet
9186 incomplete type. */
9187
9188static void
9189compute_delayed_physnames (struct dwarf2_cu *cu)
9190{
3693fdb3 9191 /* Only C++ delays computing physnames. */
c89b44cd 9192 if (cu->method_list.empty ())
3693fdb3
PA
9193 return;
9194 gdb_assert (cu->language == language_cplus);
9195
52941706 9196 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9197 {
1d06ead6 9198 const char *physname;
3da10d80 9199 struct fn_fieldlist *fn_flp
c89b44cd
TT
9200 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9201 physname = dwarf2_physname (mi.name, mi.die, cu);
9202 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9203 = physname ? physname : "";
3693fdb3
PA
9204
9205 /* Since there's no tag to indicate whether a method is a
9206 const/volatile overload, extract that information out of the
9207 demangled name. */
9208 if (physname != NULL)
9209 {
9210 size_t len = strlen (physname);
9211
9212 while (1)
9213 {
9214 if (physname[len] == ')') /* shortcut */
9215 break;
9216 else if (check_modifier (physname, len, " const"))
c89b44cd 9217 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9218 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9219 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9220 else
9221 break;
9222 }
9223 }
3da10d80 9224 }
c89b44cd
TT
9225
9226 /* The list is no longer needed. */
9227 cu->method_list.clear ();
3da10d80
KS
9228}
9229
a766d390
DE
9230/* Go objects should be embedded in a DW_TAG_module DIE,
9231 and it's not clear if/how imported objects will appear.
9232 To keep Go support simple until that's worked out,
9233 go back through what we've read and create something usable.
9234 We could do this while processing each DIE, and feels kinda cleaner,
9235 but that way is more invasive.
9236 This is to, for example, allow the user to type "p var" or "b main"
9237 without having to specify the package name, and allow lookups
9238 of module.object to work in contexts that use the expression
9239 parser. */
9240
9241static void
9242fixup_go_packaging (struct dwarf2_cu *cu)
9243{
421d1616 9244 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9245 struct pending *list;
9246 int i;
9247
c24bdb02 9248 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9249 list != NULL;
9250 list = list->next)
a766d390
DE
9251 {
9252 for (i = 0; i < list->nsyms; ++i)
9253 {
9254 struct symbol *sym = list->symbol[i];
9255
c1b5c1eb 9256 if (sym->language () == language_go
a766d390
DE
9257 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9258 {
421d1616
TT
9259 gdb::unique_xmalloc_ptr<char> this_package_name
9260 (go_symbol_package_name (sym));
a766d390
DE
9261
9262 if (this_package_name == NULL)
9263 continue;
9264 if (package_name == NULL)
421d1616 9265 package_name = std::move (this_package_name);
a766d390
DE
9266 else
9267 {
5e22e966 9268 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9269 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9270 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9271 (symbol_symtab (sym) != NULL
9272 ? symtab_to_filename_for_display
9273 (symbol_symtab (sym))
e3b94546 9274 : objfile_name (objfile)),
421d1616 9275 this_package_name.get (), package_name.get ());
a766d390
DE
9276 }
9277 }
9278 }
9279 }
9280
9281 if (package_name != NULL)
9282 {
5e22e966 9283 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9284 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9285 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9286 saved_package_name);
a766d390
DE
9287 struct symbol *sym;
9288
8c14c3a3 9289 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9290 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9291 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9292 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9293 e.g., "main" finds the "main" module and not C's main(). */
9294 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9295 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9296 SYMBOL_TYPE (sym) = type;
9297
c24bdb02 9298 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9299 }
9300}
9301
c9317f21
TT
9302/* Allocate a fully-qualified name consisting of the two parts on the
9303 obstack. */
9304
9305static const char *
9306rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9307{
9308 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9309}
9310
9c6a1327
TT
9311/* A helper that allocates a variant part to attach to a Rust enum
9312 type. OBSTACK is where the results should be allocated. TYPE is
9313 the type we're processing. DISCRIMINANT_INDEX is the index of the
9314 discriminant. It must be the index of one of the fields of TYPE.
9315 DEFAULT_INDEX is the index of the default field; or -1 if there is
9316 no default. RANGES is indexed by "effective" field number (the
9317 field index, but omitting the discriminant and default fields) and
9318 must hold the discriminant values used by the variants. Note that
9319 RANGES must have a lifetime at least as long as OBSTACK -- either
9320 already allocated on it, or static. */
c9317f21 9321
9c6a1327
TT
9322static void
9323alloc_rust_variant (struct obstack *obstack, struct type *type,
9324 int discriminant_index, int default_index,
9325 gdb::array_view<discriminant_range> ranges)
9326{
9327 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9328 must be handled by the caller. */
9329 gdb_assert (discriminant_index >= 0
1f704f76 9330 && discriminant_index < type->num_fields ());
c9317f21 9331 gdb_assert (default_index == -1
1f704f76 9332 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9333
9c6a1327 9334 /* We have one variant for each non-discriminant field. */
1f704f76 9335 int n_variants = type->num_fields () - 1;
c9317f21 9336
9c6a1327
TT
9337 variant *variants = new (obstack) variant[n_variants];
9338 int var_idx = 0;
9339 int range_idx = 0;
1f704f76 9340 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9341 {
9342 if (i == discriminant_index)
9343 continue;
c9317f21 9344
9c6a1327
TT
9345 variants[var_idx].first_field = i;
9346 variants[var_idx].last_field = i + 1;
9347
9348 /* The default field does not need a range, but other fields do.
9349 We skipped the discriminant above. */
9350 if (i != default_index)
9351 {
9352 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9353 ++range_idx;
9354 }
c9317f21 9355
9c6a1327
TT
9356 ++var_idx;
9357 }
9358
9359 gdb_assert (range_idx == ranges.size ());
9360 gdb_assert (var_idx == n_variants);
9361
9362 variant_part *part = new (obstack) variant_part;
9363 part->discriminant_index = discriminant_index;
9364 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9365 discriminant_index));
9366 part->variants = gdb::array_view<variant> (variants, n_variants);
9367
9368 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9369 gdb::array_view<variant_part> *prop_value
9370 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9371
9c6a1327
TT
9372 struct dynamic_prop prop;
9373 prop.kind = PROP_VARIANT_PARTS;
9374 prop.data.variant_parts = prop_value;
9375
5c54719c 9376 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9377}
9378
9379/* Some versions of rustc emitted enums in an unusual way.
9380
9381 Ordinary enums were emitted as unions. The first element of each
9382 structure in the union was named "RUST$ENUM$DISR". This element
9383 held the discriminant.
9384
9385 These versions of Rust also implemented the "non-zero"
9386 optimization. When the enum had two values, and one is empty and
9387 the other holds a pointer that cannot be zero, the pointer is used
9388 as the discriminant, with a zero value meaning the empty variant.
9389 Here, the union's first member is of the form
9390 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9391 where the fieldnos are the indices of the fields that should be
9392 traversed in order to find the field (which may be several fields deep)
9393 and the variantname is the name of the variant of the case when the
9394 field is zero.
9395
9396 This function recognizes whether TYPE is of one of these forms,
9397 and, if so, smashes it to be a variant type. */
9398
9399static void
9400quirk_rust_enum (struct type *type, struct objfile *objfile)
9401{
78134374 9402 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9403
9404 /* We don't need to deal with empty enums. */
1f704f76 9405 if (type->num_fields () == 0)
c9317f21
TT
9406 return;
9407
9408#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9409 if (type->num_fields () == 1
c9317f21
TT
9410 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9411 {
9412 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9413
9414 /* Decode the field name to find the offset of the
9415 discriminant. */
9416 ULONGEST bit_offset = 0;
9417 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9418 while (name[0] >= '0' && name[0] <= '9')
9419 {
9420 char *tail;
9421 unsigned long index = strtoul (name, &tail, 10);
9422 name = tail;
9423 if (*name != '$'
1f704f76 9424 || index >= field_type->num_fields ()
c9317f21
TT
9425 || (TYPE_FIELD_LOC_KIND (field_type, index)
9426 != FIELD_LOC_KIND_BITPOS))
9427 {
b98664d3 9428 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9429 "[in module %s]"),
9430 TYPE_FIELD_NAME (type, 0),
9431 objfile_name (objfile));
9432 return;
9433 }
9434 ++name;
9435
9436 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9437 field_type = TYPE_FIELD_TYPE (field_type, index);
9438 }
9439
9c6a1327
TT
9440 /* Smash this type to be a structure type. We have to do this
9441 because the type has already been recorded. */
67607e24 9442 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9443 type->set_num_fields (3);
9c6a1327 9444 /* Save the field we care about. */
ceacbf6e 9445 struct field saved_field = type->field (0);
3cabb6b0
SM
9446 type->set_fields
9447 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9448
9c6a1327
TT
9449 /* Put the discriminant at index 0. */
9450 TYPE_FIELD_TYPE (type, 0) = field_type;
9451 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9452 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9453 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9454
9455 /* The order of fields doesn't really matter, so put the real
9456 field at index 1 and the data-less field at index 2. */
ceacbf6e 9457 type->field (1) = saved_field;
9c6a1327 9458 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9459 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9460 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9461 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9462 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9463
9464 const char *dataless_name
7d93a1e0 9465 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9466 name);
9467 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9468 dataless_name);
9c6a1327 9469 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9470 /* NAME points into the original discriminant name, which
9471 already has the correct lifetime. */
9c6a1327 9472 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9473 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9474
9c6a1327
TT
9475 /* Indicate that this is a variant type. */
9476 static discriminant_range ranges[1] = { { 0, 0 } };
9477 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9478 }
77c2dba3
TT
9479 /* A union with a single anonymous field is probably an old-style
9480 univariant enum. */
1f704f76 9481 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9482 {
c9317f21
TT
9483 /* Smash this type to be a structure type. We have to do this
9484 because the type has already been recorded. */
67607e24 9485 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9486
9c6a1327 9487 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9488 const char *variant_name
7d93a1e0 9489 = rust_last_path_segment (field_type->name ());
9c6a1327 9490 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9491 field_type->set_name
9492 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9493 type->name (), variant_name));
c9317f21
TT
9494 }
9495 else
9496 {
9497 struct type *disr_type = nullptr;
1f704f76 9498 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9499 {
9500 disr_type = TYPE_FIELD_TYPE (type, i);
9501
78134374 9502 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9503 {
9504 /* All fields of a true enum will be structs. */
9505 return;
9506 }
1f704f76 9507 else if (disr_type->num_fields () == 0)
c9317f21
TT
9508 {
9509 /* Could be data-less variant, so keep going. */
a037790e 9510 disr_type = nullptr;
c9317f21
TT
9511 }
9512 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9513 "RUST$ENUM$DISR") != 0)
9514 {
9515 /* Not a Rust enum. */
9516 return;
9517 }
9518 else
9519 {
9520 /* Found one. */
9521 break;
9522 }
9523 }
9524
9525 /* If we got here without a discriminant, then it's probably
9526 just a union. */
9527 if (disr_type == nullptr)
9528 return;
9529
9530 /* Smash this type to be a structure type. We have to do this
9531 because the type has already been recorded. */
67607e24 9532 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9533
9c6a1327 9534 /* Make space for the discriminant field. */
ceacbf6e 9535 struct field *disr_field = &disr_type->field (0);
9c6a1327 9536 field *new_fields
1f704f76 9537 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9538 * sizeof (struct field)));
80fc5e77 9539 memcpy (new_fields + 1, type->fields (),
1f704f76 9540 type->num_fields () * sizeof (struct field));
3cabb6b0 9541 type->set_fields (new_fields);
1f704f76 9542 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9543
9544 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9545 type->field (0) = *disr_field;
9c6a1327
TT
9546 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9547 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9548
9549 /* We need a way to find the correct discriminant given a
9550 variant name. For convenience we build a map here. */
9551 struct type *enum_type = FIELD_TYPE (*disr_field);
9552 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9553 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9554 {
9555 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9556 {
9557 const char *name
9558 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9559 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9560 }
9561 }
9562
1f704f76 9563 int n_fields = type->num_fields ();
9c6a1327
TT
9564 /* We don't need a range entry for the discriminant, but we do
9565 need one for every other field, as there is no default
9566 variant. */
9567 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9568 discriminant_range,
9569 n_fields - 1);
c9317f21
TT
9570 /* Skip the discriminant here. */
9571 for (int i = 1; i < n_fields; ++i)
9572 {
9573 /* Find the final word in the name of this variant's type.
9574 That name can be used to look up the correct
9575 discriminant. */
9576 const char *variant_name
7d93a1e0 9577 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9578
9579 auto iter = discriminant_map.find (variant_name);
9580 if (iter != discriminant_map.end ())
9c6a1327
TT
9581 {
9582 ranges[i].low = iter->second;
9583 ranges[i].high = iter->second;
9584 }
c9317f21 9585
bedda9ac 9586 /* Remove the discriminant field, if it exists. */
9c6a1327 9587 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9588 if (sub_type->num_fields () > 0)
bedda9ac 9589 {
5e33d5f4 9590 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9591 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9592 }
9c6a1327 9593 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9594 sub_type->set_name
9595 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9596 type->name (), variant_name));
c9317f21 9597 }
9c6a1327
TT
9598
9599 /* Indicate that this is a variant type. */
9600 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9601 gdb::array_view<discriminant_range> (ranges,
9602 n_fields - 1));
c9317f21
TT
9603 }
9604}
9605
9606/* Rewrite some Rust unions to be structures with variants parts. */
9607
9608static void
9609rust_union_quirks (struct dwarf2_cu *cu)
9610{
9611 gdb_assert (cu->language == language_rust);
52941706 9612 for (type *type_ : cu->rust_unions)
5e22e966 9613 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9614 /* We don't need this any more. */
9615 cu->rust_unions.clear ();
c9317f21
TT
9616}
9617
8adb8487
TT
9618/* See read.h. */
9619
9620type_unit_group_unshareable *
9621dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9622{
9623 auto iter = this->m_type_units.find (tu_group);
9624 if (iter != this->m_type_units.end ())
9625 return iter->second.get ();
9626
9627 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9628 type_unit_group_unshareable *result = uniq.get ();
9629 this->m_type_units[tu_group] = std::move (uniq);
9630 return result;
9631}
9632
e286671b
TT
9633struct type *
9634dwarf2_per_objfile::get_type_for_signatured_type
9635 (signatured_type *sig_type) const
9636{
9637 auto iter = this->m_type_map.find (sig_type);
9638 if (iter == this->m_type_map.end ())
9639 return nullptr;
9640
9641 return iter->second;
9642}
9643
9644void dwarf2_per_objfile::set_type_for_signatured_type
9645 (signatured_type *sig_type, struct type *type)
9646{
9647 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9648
9649 this->m_type_map[sig_type] = type;
9650}
9651
95554aad
TT
9652/* A helper function for computing the list of all symbol tables
9653 included by PER_CU. */
9654
9655static void
4c39bc03 9656recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9657 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9658 dwarf2_per_cu_data *per_cu,
9659 dwarf2_per_objfile *per_objfile,
43f3e411 9660 struct compunit_symtab *immediate_parent)
95554aad 9661{
af758d11 9662 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9663 if (*slot != NULL)
9664 {
9665 /* This inclusion and its children have been processed. */
9666 return;
9667 }
9668
9669 *slot = per_cu;
af758d11 9670
95554aad 9671 /* Only add a CU if it has a symbol table. */
43182c09 9672 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9673 if (cust != NULL)
ec94af83
DE
9674 {
9675 /* If this is a type unit only add its symbol table if we haven't
9676 seen it yet (type unit per_cu's can share symtabs). */
9677 if (per_cu->is_debug_types)
9678 {
43f3e411 9679 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9680 if (*slot == NULL)
9681 {
43f3e411 9682 *slot = cust;
4c39bc03 9683 result->push_back (cust);
43f3e411
DE
9684 if (cust->user == NULL)
9685 cust->user = immediate_parent;
ec94af83
DE
9686 }
9687 }
9688 else
f9125b6c 9689 {
4c39bc03 9690 result->push_back (cust);
43f3e411
DE
9691 if (cust->user == NULL)
9692 cust->user = immediate_parent;
f9125b6c 9693 }
ec94af83 9694 }
95554aad 9695
ae640021
AB
9696 if (!per_cu->imported_symtabs_empty ())
9697 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9698 {
9699 recursively_compute_inclusions (result, all_children,
43182c09
SM
9700 all_type_symtabs, ptr, per_objfile,
9701 cust);
ae640021 9702 }
95554aad
TT
9703}
9704
43f3e411 9705/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9706 PER_CU. */
9707
9708static void
43182c09
SM
9709compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9710 dwarf2_per_objfile *per_objfile)
95554aad 9711{
f4dc4d17
DE
9712 gdb_assert (! per_cu->is_debug_types);
9713
ae640021 9714 if (!per_cu->imported_symtabs_empty ())
95554aad 9715 {
ae640021 9716 int len;
4c39bc03 9717 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9718 htab_t all_children, all_type_symtabs;
43182c09 9719 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9720
9721 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9722 if (cust == NULL)
95554aad
TT
9723 return;
9724
9725 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9726 NULL, xcalloc, xfree);
ec94af83
DE
9727 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9728 NULL, xcalloc, xfree);
95554aad 9729
ae640021 9730 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9731 {
9732 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9733 all_type_symtabs, ptr, per_objfile,
9734 cust);
ec94af83 9735 }
95554aad 9736
ec94af83 9737 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9738 len = result_symtabs.size ();
43f3e411 9739 cust->includes
f6e649dd 9740 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9741 struct compunit_symtab *, len + 1);
4c39bc03
TT
9742 memcpy (cust->includes, result_symtabs.data (),
9743 len * sizeof (compunit_symtab *));
43f3e411 9744 cust->includes[len] = NULL;
95554aad 9745
95554aad 9746 htab_delete (all_children);
ec94af83 9747 htab_delete (all_type_symtabs);
95554aad
TT
9748 }
9749}
9750
9751/* Compute the 'includes' field for the symtabs of all the CUs we just
9752 read. */
9753
9754static void
ed2dc618 9755process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9756{
5989a64e 9757 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9758 {
9759 if (! iter->is_debug_types)
43182c09 9760 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9761 }
95554aad 9762
5989a64e 9763 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9764}
9765
9cdd5dbd 9766/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9767 already been loaded into memory. */
9768
9769static void
47b14e86
SM
9770process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9771 dwarf2_per_objfile *dwarf2_per_objfile,
95554aad 9772 enum language pretend_language)
10b3939b 9773{
10b3939b 9774 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9775 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9776 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9777 CORE_ADDR lowpc, highpc;
43f3e411 9778 struct compunit_symtab *cust;
10b3939b 9779 CORE_ADDR baseaddr;
4359dff1 9780 struct block *static_block;
3e29f34a 9781 CORE_ADDR addr;
10b3939b 9782
b3b3bada 9783 baseaddr = objfile->text_section_offset ();
10b3939b 9784
c89b44cd
TT
9785 /* Clear the list here in case something was left over. */
9786 cu->method_list.clear ();
10b3939b 9787
95554aad
TT
9788 cu->language = pretend_language;
9789 cu->language_defn = language_def (cu->language);
9790
c906108c 9791 /* Do line number decoding in read_file_scope () */
10b3939b 9792 process_die (cu->dies, cu);
c906108c 9793
a766d390
DE
9794 /* For now fudge the Go package. */
9795 if (cu->language == language_go)
9796 fixup_go_packaging (cu);
9797
5f48f8f3 9798 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9799 should be complete, and it should now be safe to compute all of the
9800 physnames. */
9801 compute_delayed_physnames (cu);
3da10d80 9802
c9317f21
TT
9803 if (cu->language == language_rust)
9804 rust_union_quirks (cu);
9805
fae299cd
DC
9806 /* Some compilers don't define a DW_AT_high_pc attribute for the
9807 compilation unit. If the DW_AT_high_pc is missing, synthesize
9808 it, by scanning the DIE's below the compilation unit. */
10b3939b 9809 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9810
3e29f34a 9811 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9812 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9813
9814 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9815 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9816 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9817 addrmap to help ensure it has an accurate map of pc values belonging to
9818 this comp unit. */
9819 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9820
c24bdb02 9821 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9822 SECT_OFF_TEXT (objfile),
9823 0);
c906108c 9824
43f3e411 9825 if (cust != NULL)
c906108c 9826 {
df15bd07 9827 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9828
8be455d7
JK
9829 /* Set symtab language to language from DW_AT_language. If the
9830 compilation is from a C file generated by language preprocessors, do
9831 not set the language if it was already deduced by start_subfile. */
43f3e411 9832 if (!(cu->language == language_c
40e3ad0e 9833 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9834 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9835
9836 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9837 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9838 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9839 there were bugs in prologue debug info, fixed later in GCC-4.5
9840 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9841
9842 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9843 needed, it would be wrong due to missing DW_AT_producer there.
9844
9845 Still one can confuse GDB by using non-standard GCC compilation
9846 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9847 */
ab260dad 9848 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9849 cust->locations_valid = 1;
e0d00bc7
JK
9850
9851 if (gcc_4_minor >= 5)
43f3e411 9852 cust->epilogue_unwind_valid = 1;
96408a79 9853
43f3e411 9854 cust->call_site_htab = cu->call_site_htab;
c906108c 9855 }
9291a0cd 9856
af758d11 9857 dwarf2_per_objfile->set_symtab (per_cu, cust);
c906108c 9858
95554aad 9859 /* Push it for inclusion processing later. */
5989a64e 9860 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
804d2729
TT
9861
9862 /* Not needed any more. */
c24bdb02 9863 cu->reset_builder ();
f4dc4d17 9864}
45cfd468 9865
f4dc4d17
DE
9866/* Generate full symbol information for type unit PER_CU, whose DIEs have
9867 already been loaded into memory. */
9868
9869static void
47b14e86
SM
9870process_full_type_unit (dwarf2_per_cu_data *per_cu,
9871 dwarf2_per_objfile *dwarf2_per_objfile,
f4dc4d17
DE
9872 enum language pretend_language)
9873{
9874 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618 9875 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9876 struct compunit_symtab *cust;
0186c6a7
DE
9877 struct signatured_type *sig_type;
9878
9879 gdb_assert (per_cu->is_debug_types);
9880 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9881
c89b44cd
TT
9882 /* Clear the list here in case something was left over. */
9883 cu->method_list.clear ();
f4dc4d17 9884
f4dc4d17
DE
9885 cu->language = pretend_language;
9886 cu->language_defn = language_def (cu->language);
9887
9888 /* The symbol tables are set up in read_type_unit_scope. */
9889 process_die (cu->dies, cu);
9890
9891 /* For now fudge the Go package. */
9892 if (cu->language == language_go)
9893 fixup_go_packaging (cu);
9894
5f48f8f3 9895 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9896 should be complete, and it should now be safe to compute all of the
9897 physnames. */
9898 compute_delayed_physnames (cu);
f4dc4d17 9899
c9317f21
TT
9900 if (cu->language == language_rust)
9901 rust_union_quirks (cu);
9902
f4dc4d17
DE
9903 /* TUs share symbol tables.
9904 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9905 of it with end_expandable_symtab. Otherwise, complete the addition of
9906 this TU's symbols to the existing symtab. */
8adb8487
TT
9907 type_unit_group_unshareable *tug_unshare =
9908 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9909 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9910 {
c24bdb02
KS
9911 buildsym_compunit *builder = cu->get_builder ();
9912 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9913 tug_unshare->compunit_symtab = cust;
f4dc4d17 9914
43f3e411 9915 if (cust != NULL)
f4dc4d17
DE
9916 {
9917 /* Set symtab language to language from DW_AT_language. If the
9918 compilation is from a C file generated by language preprocessors,
9919 do not set the language if it was already deduced by
9920 start_subfile. */
43f3e411
DE
9921 if (!(cu->language == language_c
9922 && COMPUNIT_FILETABS (cust)->language != language_c))
9923 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9924 }
9925 }
9926 else
9927 {
c24bdb02 9928 cu->get_builder ()->augment_type_symtab ();
8adb8487 9929 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9930 }
9931
af758d11 9932 dwarf2_per_objfile->set_symtab (per_cu, cust);
804d2729
TT
9933
9934 /* Not needed any more. */
c24bdb02 9935 cu->reset_builder ();
c906108c
SS
9936}
9937
95554aad
TT
9938/* Process an imported unit DIE. */
9939
9940static void
9941process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9942{
9943 struct attribute *attr;
9944
f4dc4d17
DE
9945 /* For now we don't handle imported units in type units. */
9946 if (cu->per_cu->is_debug_types)
9947 {
9948 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9949 " supported in type units [in module %s]"),
5e22e966 9950 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9951 }
9952
95554aad
TT
9953 attr = dwarf2_attr (die, DW_AT_import, cu);
9954 if (attr != NULL)
9955 {
0826b30a 9956 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9957 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9958 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9959 dwarf2_per_cu_data *per_cu
ab432490 9960 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9961
58990295
TV
9962 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9963 into another compilation unit, at root level. Regard this as a hint,
9964 and ignore it. */
9965 if (die->parent && die->parent->parent == NULL
9966 && per_cu->unit_type == DW_UT_compile
9967 && per_cu->lang == language_cplus)
9968 return;
9969
69d751e3 9970 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9971 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 9972 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9973
ae640021 9974 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9975 }
9976}
9977
4c8aa72d
PA
9978/* RAII object that represents a process_die scope: i.e.,
9979 starts/finishes processing a DIE. */
9980class process_die_scope
adde2bff 9981{
4c8aa72d
PA
9982public:
9983 process_die_scope (die_info *die, dwarf2_cu *cu)
9984 : m_die (die), m_cu (cu)
9985 {
9986 /* We should only be processing DIEs not already in process. */
9987 gdb_assert (!m_die->in_process);
9988 m_die->in_process = true;
9989 }
8c3cb9fa 9990
4c8aa72d
PA
9991 ~process_die_scope ()
9992 {
9993 m_die->in_process = false;
9994
9995 /* If we're done processing the DIE for the CU that owns the line
9996 header, we don't need the line header anymore. */
9997 if (m_cu->line_header_die_owner == m_die)
9998 {
9999 delete m_cu->line_header;
10000 m_cu->line_header = NULL;
10001 m_cu->line_header_die_owner = NULL;
10002 }
10003 }
10004
10005private:
10006 die_info *m_die;
10007 dwarf2_cu *m_cu;
10008};
adde2bff 10009
c906108c
SS
10010/* Process a die and its children. */
10011
10012static void
e7c27a73 10013process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10014{
4c8aa72d 10015 process_die_scope scope (die, cu);
adde2bff 10016
c906108c
SS
10017 switch (die->tag)
10018 {
10019 case DW_TAG_padding:
10020 break;
10021 case DW_TAG_compile_unit:
95554aad 10022 case DW_TAG_partial_unit:
e7c27a73 10023 read_file_scope (die, cu);
c906108c 10024 break;
348e048f
DE
10025 case DW_TAG_type_unit:
10026 read_type_unit_scope (die, cu);
10027 break;
c906108c 10028 case DW_TAG_subprogram:
0a4b0913
AB
10029 /* Nested subprograms in Fortran get a prefix. */
10030 if (cu->language == language_fortran
10031 && die->parent != NULL
10032 && die->parent->tag == DW_TAG_subprogram)
10033 cu->processing_has_namespace_info = true;
10034 /* Fall through. */
c906108c 10035 case DW_TAG_inlined_subroutine:
edb3359d 10036 read_func_scope (die, cu);
c906108c
SS
10037 break;
10038 case DW_TAG_lexical_block:
14898363
L
10039 case DW_TAG_try_block:
10040 case DW_TAG_catch_block:
e7c27a73 10041 read_lexical_block_scope (die, cu);
c906108c 10042 break;
216f72a1 10043 case DW_TAG_call_site:
96408a79
SA
10044 case DW_TAG_GNU_call_site:
10045 read_call_site_scope (die, cu);
10046 break;
c906108c 10047 case DW_TAG_class_type:
680b30c7 10048 case DW_TAG_interface_type:
c906108c
SS
10049 case DW_TAG_structure_type:
10050 case DW_TAG_union_type:
134d01f1 10051 process_structure_scope (die, cu);
c906108c
SS
10052 break;
10053 case DW_TAG_enumeration_type:
134d01f1 10054 process_enumeration_scope (die, cu);
c906108c 10055 break;
134d01f1 10056
f792889a
DJ
10057 /* These dies have a type, but processing them does not create
10058 a symbol or recurse to process the children. Therefore we can
10059 read them on-demand through read_type_die. */
c906108c 10060 case DW_TAG_subroutine_type:
72019c9c 10061 case DW_TAG_set_type:
c906108c 10062 case DW_TAG_array_type:
c906108c 10063 case DW_TAG_pointer_type:
c906108c 10064 case DW_TAG_ptr_to_member_type:
c906108c 10065 case DW_TAG_reference_type:
4297a3f0 10066 case DW_TAG_rvalue_reference_type:
c906108c 10067 case DW_TAG_string_type:
c906108c 10068 break;
134d01f1 10069
c906108c 10070 case DW_TAG_base_type:
a02abb62 10071 case DW_TAG_subrange_type:
cb249c71 10072 case DW_TAG_typedef:
134d01f1
DJ
10073 /* Add a typedef symbol for the type definition, if it has a
10074 DW_AT_name. */
f792889a 10075 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10076 break;
c906108c 10077 case DW_TAG_common_block:
e7c27a73 10078 read_common_block (die, cu);
c906108c
SS
10079 break;
10080 case DW_TAG_common_inclusion:
10081 break;
d9fa45fe 10082 case DW_TAG_namespace:
9068261f 10083 cu->processing_has_namespace_info = true;
e7c27a73 10084 read_namespace (die, cu);
d9fa45fe 10085 break;
5d7cb8df 10086 case DW_TAG_module:
9068261f 10087 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10088 read_module (die, cu);
10089 break;
d9fa45fe 10090 case DW_TAG_imported_declaration:
9068261f 10091 cu->processing_has_namespace_info = true;
74921315
KS
10092 if (read_namespace_alias (die, cu))
10093 break;
86a73007
TT
10094 /* The declaration is not a global namespace alias. */
10095 /* Fall through. */
d9fa45fe 10096 case DW_TAG_imported_module:
9068261f 10097 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10098 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10099 || cu->language != language_fortran))
b98664d3 10100 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10101 dwarf_tag_name (die->tag));
10102 read_import_statement (die, cu);
d9fa45fe 10103 break;
95554aad
TT
10104
10105 case DW_TAG_imported_unit:
10106 process_imported_unit_die (die, cu);
10107 break;
10108
71a3c369
TT
10109 case DW_TAG_variable:
10110 read_variable (die, cu);
10111 break;
10112
c906108c 10113 default:
e7c27a73 10114 new_symbol (die, NULL, cu);
c906108c
SS
10115 break;
10116 }
10117}
ca69b9e6
DE
10118\f
10119/* DWARF name computation. */
c906108c 10120
94af9270
KS
10121/* A helper function for dwarf2_compute_name which determines whether DIE
10122 needs to have the name of the scope prepended to the name listed in the
10123 die. */
10124
10125static int
10126die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10127{
1c809c68
TT
10128 struct attribute *attr;
10129
94af9270
KS
10130 switch (die->tag)
10131 {
10132 case DW_TAG_namespace:
10133 case DW_TAG_typedef:
10134 case DW_TAG_class_type:
10135 case DW_TAG_interface_type:
10136 case DW_TAG_structure_type:
10137 case DW_TAG_union_type:
10138 case DW_TAG_enumeration_type:
10139 case DW_TAG_enumerator:
10140 case DW_TAG_subprogram:
08a76f8a 10141 case DW_TAG_inlined_subroutine:
94af9270 10142 case DW_TAG_member:
74921315 10143 case DW_TAG_imported_declaration:
94af9270
KS
10144 return 1;
10145
10146 case DW_TAG_variable:
c2b0a229 10147 case DW_TAG_constant:
94af9270
KS
10148 /* We only need to prefix "globally" visible variables. These include
10149 any variable marked with DW_AT_external or any variable that
10150 lives in a namespace. [Variables in anonymous namespaces
10151 require prefixing, but they are not DW_AT_external.] */
10152
10153 if (dwarf2_attr (die, DW_AT_specification, cu))
10154 {
10155 struct dwarf2_cu *spec_cu = cu;
9a619af0 10156
94af9270
KS
10157 return die_needs_namespace (die_specification (die, &spec_cu),
10158 spec_cu);
10159 }
10160
1c809c68 10161 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10162 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10163 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10164 return 0;
10165 /* A variable in a lexical block of some kind does not need a
10166 namespace, even though in C++ such variables may be external
10167 and have a mangled name. */
10168 if (die->parent->tag == DW_TAG_lexical_block
10169 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10170 || die->parent->tag == DW_TAG_catch_block
10171 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10172 return 0;
10173 return 1;
94af9270
KS
10174
10175 default:
10176 return 0;
10177 }
10178}
10179
73b9be8b
KS
10180/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10181 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10182 defined for the given DIE. */
10183
10184static struct attribute *
10185dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10186{
10187 struct attribute *attr;
10188
10189 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10190 if (attr == NULL)
10191 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10192
10193 return attr;
10194}
10195
10196/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10197 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10198 defined for the given DIE. */
10199
10200static const char *
10201dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10202{
10203 const char *linkage_name;
10204
10205 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10206 if (linkage_name == NULL)
10207 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10208
787de330
TT
10209 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10210 See https://github.com/rust-lang/rust/issues/32925. */
10211 if (cu->language == language_rust && linkage_name != NULL
10212 && strchr (linkage_name, '{') != NULL)
10213 linkage_name = NULL;
10214
73b9be8b
KS
10215 return linkage_name;
10216}
10217
94af9270 10218/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10219 compute the physname for the object, which include a method's:
9c37b5ae 10220 - formal parameters (C++),
a766d390 10221 - receiver type (Go),
a766d390
DE
10222
10223 The term "physname" is a bit confusing.
10224 For C++, for example, it is the demangled name.
10225 For Go, for example, it's the mangled name.
94af9270 10226
af6b7be1
JB
10227 For Ada, return the DIE's linkage name rather than the fully qualified
10228 name. PHYSNAME is ignored..
10229
5989a64e 10230 The result is allocated on the objfile->per_bfd's obstack and
45940949 10231 canonicalized. */
94af9270
KS
10232
10233static const char *
15d034d0
TT
10234dwarf2_compute_name (const char *name,
10235 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10236 int physname)
10237{
5e22e966 10238 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10239
94af9270
KS
10240 if (name == NULL)
10241 name = dwarf2_name (die, cu);
10242
2ee7123e
DE
10243 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10244 but otherwise compute it by typename_concat inside GDB.
10245 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10246 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10247 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10248 will set the demangled name to the result of dwarf2_full_name, and it is
10249 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10250 if (cu->language == language_ada
10251 || (cu->language == language_fortran && physname))
10252 {
10253 /* For Ada unit, we prefer the linkage name over the name, as
10254 the former contains the exported name, which the user expects
10255 to be able to reference. Ideally, we want the user to be able
10256 to reference this entity using either natural or linkage name,
10257 but we haven't started looking at this enhancement yet. */
73b9be8b 10258 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10259
2ee7123e
DE
10260 if (linkage_name != NULL)
10261 return linkage_name;
f55ee35c
JK
10262 }
10263
94af9270
KS
10264 /* These are the only languages we know how to qualify names in. */
10265 if (name != NULL
9c37b5ae 10266 && (cu->language == language_cplus
c44af4eb
TT
10267 || cu->language == language_fortran || cu->language == language_d
10268 || cu->language == language_rust))
94af9270
KS
10269 {
10270 if (die_needs_namespace (die, cu))
10271 {
0d5cff50 10272 const char *prefix;
34a68019 10273 const char *canonical_name = NULL;
94af9270 10274
d7e74731
PA
10275 string_file buf;
10276
94af9270 10277 prefix = determine_prefix (die, cu);
94af9270
KS
10278 if (*prefix != '\0')
10279 {
43816ebc
TT
10280 gdb::unique_xmalloc_ptr<char> prefixed_name
10281 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10282
43816ebc 10283 buf.puts (prefixed_name.get ());
94af9270
KS
10284 }
10285 else
d7e74731 10286 buf.puts (name);
94af9270 10287
98bfdba5
PA
10288 /* Template parameters may be specified in the DIE's DW_AT_name, or
10289 as children with DW_TAG_template_type_param or
10290 DW_TAG_value_type_param. If the latter, add them to the name
10291 here. If the name already has template parameters, then
10292 skip this step; some versions of GCC emit both, and
10293 it is more efficient to use the pre-computed name.
10294
10295 Something to keep in mind about this process: it is very
10296 unlikely, or in some cases downright impossible, to produce
10297 something that will match the mangled name of a function.
10298 If the definition of the function has the same debug info,
10299 we should be able to match up with it anyway. But fallbacks
10300 using the minimal symbol, for instance to find a method
10301 implemented in a stripped copy of libstdc++, will not work.
10302 If we do not have debug info for the definition, we will have to
10303 match them up some other way.
10304
10305 When we do name matching there is a related problem with function
10306 templates; two instantiated function templates are allowed to
10307 differ only by their return types, which we do not add here. */
10308
10309 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10310 {
10311 struct attribute *attr;
10312 struct die_info *child;
10313 int first = 1;
10314
10315 die->building_fullname = 1;
10316
10317 for (child = die->child; child != NULL; child = child->sibling)
10318 {
10319 struct type *type;
12df843f 10320 LONGEST value;
d521ce57 10321 const gdb_byte *bytes;
98bfdba5
PA
10322 struct dwarf2_locexpr_baton *baton;
10323 struct value *v;
10324
10325 if (child->tag != DW_TAG_template_type_param
10326 && child->tag != DW_TAG_template_value_param)
10327 continue;
10328
10329 if (first)
10330 {
d7e74731 10331 buf.puts ("<");
98bfdba5
PA
10332 first = 0;
10333 }
10334 else
d7e74731 10335 buf.puts (", ");
98bfdba5
PA
10336
10337 attr = dwarf2_attr (child, DW_AT_type, cu);
10338 if (attr == NULL)
10339 {
b98664d3 10340 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10341 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10342 continue;
10343 }
10344 type = die_type (child, cu);
10345
10346 if (child->tag == DW_TAG_template_type_param)
10347 {
c1ec8cea
TT
10348 c_print_type (type, "", &buf, -1, 0, cu->language,
10349 &type_print_raw_options);
98bfdba5
PA
10350 continue;
10351 }
10352
10353 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10354 if (attr == NULL)
10355 {
b98664d3 10356 complaint (_("template parameter missing "
3e43a32a 10357 "DW_AT_const_value"));
d7e74731 10358 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10359 continue;
10360 }
10361
10362 dwarf2_const_value_attr (attr, type, name,
10363 &cu->comp_unit_obstack, cu,
10364 &value, &bytes, &baton);
10365
10366 if (TYPE_NOSIGN (type))
10367 /* GDB prints characters as NUMBER 'CHAR'. If that's
10368 changed, this can use value_print instead. */
d7e74731 10369 c_printchar (value, type, &buf);
98bfdba5
PA
10370 else
10371 {
10372 struct value_print_options opts;
10373
10374 if (baton != NULL)
10375 v = dwarf2_evaluate_loc_desc (type, NULL,
10376 baton->data,
10377 baton->size,
9f47c707
SM
10378 baton->per_cu,
10379 baton->per_objfile);
98bfdba5
PA
10380 else if (bytes != NULL)
10381 {
10382 v = allocate_value (type);
10383 memcpy (value_contents_writeable (v), bytes,
10384 TYPE_LENGTH (type));
10385 }
10386 else
10387 v = value_from_longest (type, value);
10388
3e43a32a
MS
10389 /* Specify decimal so that we do not depend on
10390 the radix. */
98bfdba5
PA
10391 get_formatted_print_options (&opts, 'd');
10392 opts.raw = 1;
d7e74731 10393 value_print (v, &buf, &opts);
98bfdba5 10394 release_value (v);
98bfdba5
PA
10395 }
10396 }
10397
10398 die->building_fullname = 0;
10399
10400 if (!first)
10401 {
10402 /* Close the argument list, with a space if necessary
10403 (nested templates). */
d7e74731
PA
10404 if (!buf.empty () && buf.string ().back () == '>')
10405 buf.puts (" >");
98bfdba5 10406 else
d7e74731 10407 buf.puts (">");
98bfdba5
PA
10408 }
10409 }
10410
9c37b5ae 10411 /* For C++ methods, append formal parameter type
94af9270 10412 information, if PHYSNAME. */
6e70227d 10413
94af9270 10414 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10415 && cu->language == language_cplus)
94af9270
KS
10416 {
10417 struct type *type = read_type_die (die, cu);
10418
d7e74731 10419 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10420 &type_print_raw_options);
94af9270 10421
9c37b5ae 10422 if (cu->language == language_cplus)
94af9270 10423 {
60430eff
DJ
10424 /* Assume that an artificial first parameter is
10425 "this", but do not crash if it is not. RealView
10426 marks unnamed (and thus unused) parameters as
10427 artificial; there is no way to differentiate
10428 the two cases. */
1f704f76 10429 if (type->num_fields () > 0
94af9270 10430 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10431 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10432 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10433 0))))
d7e74731 10434 buf.puts (" const");
94af9270
KS
10435 }
10436 }
10437
d7e74731 10438 const std::string &intermediate_name = buf.string ();
94af9270
KS
10439
10440 if (cu->language == language_cplus)
34a68019 10441 canonical_name
322a8516 10442 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10443 objfile);
34a68019
TT
10444
10445 /* If we only computed INTERMEDIATE_NAME, or if
10446 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10447 intern it. */
322a8516 10448 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10449 name = objfile->intern (intermediate_name);
34a68019
TT
10450 else
10451 name = canonical_name;
94af9270
KS
10452 }
10453 }
10454
10455 return name;
10456}
10457
0114d602
DJ
10458/* Return the fully qualified name of DIE, based on its DW_AT_name.
10459 If scope qualifiers are appropriate they will be added. The result
34a68019 10460 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10461 not have a name. NAME may either be from a previous call to
10462 dwarf2_name or NULL.
10463
9c37b5ae 10464 The output string will be canonicalized (if C++). */
0114d602
DJ
10465
10466static const char *
15d034d0 10467dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10468{
94af9270
KS
10469 return dwarf2_compute_name (name, die, cu, 0);
10470}
0114d602 10471
94af9270
KS
10472/* Construct a physname for the given DIE in CU. NAME may either be
10473 from a previous call to dwarf2_name or NULL. The result will be
10474 allocated on the objfile_objstack or NULL if the DIE does not have a
10475 name.
0114d602 10476
9c37b5ae 10477 The output string will be canonicalized (if C++). */
0114d602 10478
94af9270 10479static const char *
15d034d0 10480dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10481{
5e22e966 10482 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10483 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10484 int need_copy = 1;
10485
10486 /* In this case dwarf2_compute_name is just a shortcut not building anything
10487 on its own. */
10488 if (!die_needs_namespace (die, cu))
10489 return dwarf2_compute_name (name, die, cu, 1);
10490
906bb4c5
TT
10491 if (cu->language != language_rust)
10492 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10493
10494 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10495 has computed. */
791afaa2 10496 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10497 if (mangled != NULL)
900e11f9 10498 {
900e11f9 10499
59cc4834
JB
10500 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10501 {
10502 /* Do nothing (do not demangle the symbol name). */
10503 }
10504 else if (cu->language == language_go)
a766d390 10505 {
5e2db402
TT
10506 /* This is a lie, but we already lie to the caller new_symbol.
10507 new_symbol assumes we return the mangled name.
a766d390 10508 This just undoes that lie until things are cleaned up. */
a766d390
DE
10509 }
10510 else
10511 {
0eb876f5
JB
10512 /* Use DMGL_RET_DROP for C++ template functions to suppress
10513 their return type. It is easier for GDB users to search
10514 for such functions as `name(params)' than `long name(params)'.
10515 In such case the minimal symbol names do not match the full
10516 symbol names but for template functions there is never a need
10517 to look up their definition from their declaration so
10518 the only disadvantage remains the minimal symbol variant
10519 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10520 demangled.reset (gdb_demangle (mangled,
10521 (DMGL_PARAMS | DMGL_ANSI
10522 | DMGL_RET_DROP)));
a766d390 10523 }
900e11f9 10524 if (demangled)
791afaa2 10525 canon = demangled.get ();
900e11f9
JK
10526 else
10527 {
10528 canon = mangled;
10529 need_copy = 0;
10530 }
10531 }
10532
10533 if (canon == NULL || check_physname)
10534 {
10535 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10536
10537 if (canon != NULL && strcmp (physname, canon) != 0)
10538 {
10539 /* It may not mean a bug in GDB. The compiler could also
10540 compute DW_AT_linkage_name incorrectly. But in such case
10541 GDB would need to be bug-to-bug compatible. */
10542
b98664d3 10543 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10544 "(from linkage <%s>) - DIE at %s [in module %s]"),
10545 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10546 objfile_name (objfile));
900e11f9
JK
10547
10548 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10549 is available here - over computed PHYSNAME. It is safer
10550 against both buggy GDB and buggy compilers. */
10551
10552 retval = canon;
10553 }
10554 else
10555 {
10556 retval = physname;
10557 need_copy = 0;
10558 }
10559 }
10560 else
10561 retval = canon;
10562
10563 if (need_copy)
be1e3d3e 10564 retval = objfile->intern (retval);
900e11f9 10565
900e11f9 10566 return retval;
0114d602
DJ
10567}
10568
74921315
KS
10569/* Inspect DIE in CU for a namespace alias. If one exists, record
10570 a new symbol for it.
10571
10572 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10573
10574static int
10575read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10576{
10577 struct attribute *attr;
10578
10579 /* If the die does not have a name, this is not a namespace
10580 alias. */
10581 attr = dwarf2_attr (die, DW_AT_name, cu);
10582 if (attr != NULL)
10583 {
10584 int num;
10585 struct die_info *d = die;
10586 struct dwarf2_cu *imported_cu = cu;
10587
10588 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10589 keep inspecting DIEs until we hit the underlying import. */
10590#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10591 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10592 {
10593 attr = dwarf2_attr (d, DW_AT_import, cu);
10594 if (attr == NULL)
10595 break;
10596
10597 d = follow_die_ref (d, attr, &imported_cu);
10598 if (d->tag != DW_TAG_imported_declaration)
10599 break;
10600 }
10601
10602 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10603 {
b98664d3 10604 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10605 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10606 return 0;
10607 }
10608
10609 if (attr != NULL)
10610 {
10611 struct type *type;
0826b30a 10612 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10613
aa66c379 10614 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10615 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10616 {
10617 /* This declaration is a global namespace alias. Add
10618 a symbol for it whose type is the aliased namespace. */
10619 new_symbol (die, type, cu);
10620 return 1;
10621 }
10622 }
10623 }
10624
10625 return 0;
10626}
10627
22cee43f 10628/* Return the using directives repository (global or local?) to use in the
804d2729 10629 current context for CU.
22cee43f
PMR
10630
10631 For Ada, imported declarations can materialize renamings, which *may* be
10632 global. However it is impossible (for now?) in DWARF to distinguish
10633 "external" imported declarations and "static" ones. As all imported
10634 declarations seem to be static in all other languages, make them all CU-wide
10635 global only in Ada. */
10636
10637static struct using_direct **
804d2729 10638using_directives (struct dwarf2_cu *cu)
22cee43f 10639{
c24bdb02
KS
10640 if (cu->language == language_ada
10641 && cu->get_builder ()->outermost_context_p ())
10642 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10643 else
c24bdb02 10644 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10645}
10646
27aa8d6a
SW
10647/* Read the import statement specified by the given die and record it. */
10648
10649static void
10650read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10651{
5e22e966 10652 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10653 struct attribute *import_attr;
32019081 10654 struct die_info *imported_die, *child_die;
de4affc9 10655 struct dwarf2_cu *imported_cu;
27aa8d6a 10656 const char *imported_name;
794684b6 10657 const char *imported_name_prefix;
13387711
SW
10658 const char *canonical_name;
10659 const char *import_alias;
10660 const char *imported_declaration = NULL;
794684b6 10661 const char *import_prefix;
eb1e02fd 10662 std::vector<const char *> excludes;
13387711 10663
27aa8d6a
SW
10664 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10665 if (import_attr == NULL)
10666 {
b98664d3 10667 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10668 dwarf_tag_name (die->tag));
10669 return;
10670 }
10671
de4affc9
CC
10672 imported_cu = cu;
10673 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10674 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10675 if (imported_name == NULL)
10676 {
10677 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10678
10679 The import in the following code:
10680 namespace A
10681 {
10682 typedef int B;
10683 }
10684
10685 int main ()
10686 {
10687 using A::B;
10688 B b;
10689 return b;
10690 }
10691
10692 ...
10693 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10694 <52> DW_AT_decl_file : 1
10695 <53> DW_AT_decl_line : 6
10696 <54> DW_AT_import : <0x75>
10697 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10698 <59> DW_AT_name : B
10699 <5b> DW_AT_decl_file : 1
10700 <5c> DW_AT_decl_line : 2
10701 <5d> DW_AT_type : <0x6e>
10702 ...
10703 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10704 <76> DW_AT_byte_size : 4
10705 <77> DW_AT_encoding : 5 (signed)
10706
10707 imports the wrong die ( 0x75 instead of 0x58 ).
10708 This case will be ignored until the gcc bug is fixed. */
10709 return;
10710 }
10711
82856980
SW
10712 /* Figure out the local name after import. */
10713 import_alias = dwarf2_name (die, cu);
27aa8d6a 10714
794684b6
SW
10715 /* Figure out where the statement is being imported to. */
10716 import_prefix = determine_prefix (die, cu);
10717
10718 /* Figure out what the scope of the imported die is and prepend it
10719 to the name of the imported die. */
de4affc9 10720 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10721
f55ee35c
JK
10722 if (imported_die->tag != DW_TAG_namespace
10723 && imported_die->tag != DW_TAG_module)
794684b6 10724 {
13387711
SW
10725 imported_declaration = imported_name;
10726 canonical_name = imported_name_prefix;
794684b6 10727 }
13387711 10728 else if (strlen (imported_name_prefix) > 0)
12aaed36 10729 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10730 imported_name_prefix,
10731 (cu->language == language_d ? "." : "::"),
10732 imported_name, (char *) NULL);
13387711
SW
10733 else
10734 canonical_name = imported_name;
794684b6 10735
32019081
JK
10736 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10737 for (child_die = die->child; child_die && child_die->tag;
436c571c 10738 child_die = child_die->sibling)
32019081
JK
10739 {
10740 /* DWARF-4: A Fortran use statement with a “rename list” may be
10741 represented by an imported module entry with an import attribute
10742 referring to the module and owned entries corresponding to those
10743 entities that are renamed as part of being imported. */
10744
10745 if (child_die->tag != DW_TAG_imported_declaration)
10746 {
b98664d3 10747 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10748 "- DIE at %s [in module %s]"),
10749 sect_offset_str (child_die->sect_off),
10750 objfile_name (objfile));
32019081
JK
10751 continue;
10752 }
10753
10754 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10755 if (import_attr == NULL)
10756 {
b98664d3 10757 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10758 dwarf_tag_name (child_die->tag));
10759 continue;
10760 }
10761
10762 imported_cu = cu;
10763 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10764 &imported_cu);
10765 imported_name = dwarf2_name (imported_die, imported_cu);
10766 if (imported_name == NULL)
10767 {
b98664d3 10768 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10769 "imported name - DIE at %s [in module %s]"),
10770 sect_offset_str (child_die->sect_off),
10771 objfile_name (objfile));
32019081
JK
10772 continue;
10773 }
10774
eb1e02fd 10775 excludes.push_back (imported_name);
32019081
JK
10776
10777 process_die (child_die, cu);
10778 }
10779
804d2729 10780 add_using_directive (using_directives (cu),
22cee43f
PMR
10781 import_prefix,
10782 canonical_name,
10783 import_alias,
10784 imported_declaration,
10785 excludes,
10786 0,
10787 &objfile->objfile_obstack);
27aa8d6a
SW
10788}
10789
5230b05a
WT
10790/* ICC<14 does not output the required DW_AT_declaration on incomplete
10791 types, but gives them a size of zero. Starting with version 14,
10792 ICC is compatible with GCC. */
10793
9068261f 10794static bool
5230b05a
WT
10795producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10796{
10797 if (!cu->checked_producer)
10798 check_producer (cu);
10799
10800 return cu->producer_is_icc_lt_14;
10801}
10802
eb77c9df
AB
10803/* ICC generates a DW_AT_type for C void functions. This was observed on
10804 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10805 which says that void functions should not have a DW_AT_type. */
10806
10807static bool
10808producer_is_icc (struct dwarf2_cu *cu)
10809{
10810 if (!cu->checked_producer)
10811 check_producer (cu);
10812
10813 return cu->producer_is_icc;
10814}
10815
1b80a9fa
JK
10816/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10817 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10818 this, it was first present in GCC release 4.3.0. */
10819
9068261f 10820static bool
1b80a9fa
JK
10821producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10822{
10823 if (!cu->checked_producer)
10824 check_producer (cu);
10825
10826 return cu->producer_is_gcc_lt_4_3;
10827}
10828
d721ba37
PA
10829static file_and_directory
10830find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10831{
d721ba37
PA
10832 file_and_directory res;
10833
9291a0cd
TT
10834 /* Find the filename. Do not use dwarf2_name here, since the filename
10835 is not a source language identifier. */
d721ba37
PA
10836 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10837 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10838
d721ba37
PA
10839 if (res.comp_dir == NULL
10840 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10841 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10842 {
d721ba37
PA
10843 res.comp_dir_storage = ldirname (res.name);
10844 if (!res.comp_dir_storage.empty ())
10845 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10846 }
d721ba37 10847 if (res.comp_dir != NULL)
9291a0cd
TT
10848 {
10849 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10850 directory, get rid of it. */
d721ba37 10851 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10852
d721ba37
PA
10853 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10854 res.comp_dir = cp + 1;
9291a0cd
TT
10855 }
10856
d721ba37
PA
10857 if (res.name == NULL)
10858 res.name = "<unknown>";
10859
10860 return res;
9291a0cd
TT
10861}
10862
f4dc4d17
DE
10863/* Handle DW_AT_stmt_list for a compilation unit.
10864 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10865 COMP_DIR is the compilation directory. LOWPC is passed to
10866 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10867
10868static void
10869handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10870 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10871{
5e22e966 10872 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10873 struct attribute *attr;
527f3840
JK
10874 struct line_header line_header_local;
10875 hashval_t line_header_local_hash;
527f3840
JK
10876 void **slot;
10877 int decode_mapping;
2ab95328 10878
f4dc4d17
DE
10879 gdb_assert (! cu->per_cu->is_debug_types);
10880
2ab95328 10881 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10882 if (attr == NULL)
10883 return;
10884
9c541725 10885 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10886
10887 /* The line header hash table is only created if needed (it exists to
10888 prevent redundant reading of the line table for partial_units).
10889 If we're given a partial_unit, we'll need it. If we're given a
10890 compile_unit, then use the line header hash table if it's already
10891 created, but don't create one just yet. */
10892
5989a64e 10893 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10894 && die->tag == DW_TAG_partial_unit)
2ab95328 10895 {
5989a64e 10896 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10897 .reset (htab_create_alloc (127, line_header_hash_voidp,
10898 line_header_eq_voidp,
10899 free_line_header_voidp,
10900 xcalloc, xfree));
527f3840 10901 }
2ab95328 10902
9c541725 10903 line_header_local.sect_off = line_offset;
527f3840
JK
10904 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10905 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10906 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10907 {
5989a64e 10908 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10909 &line_header_local,
10910 line_header_local_hash, NO_INSERT);
10911
10912 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10913 is not present in *SLOT (since if there is something in *SLOT then
10914 it will be for a partial_unit). */
10915 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10916 {
527f3840 10917 gdb_assert (*slot != NULL);
9a3c8263 10918 cu->line_header = (struct line_header *) *slot;
527f3840 10919 return;
dee91e82 10920 }
2ab95328 10921 }
527f3840
JK
10922
10923 /* dwarf_decode_line_header does not yet provide sufficient information.
10924 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10925 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10926 if (lh == NULL)
527f3840 10927 return;
4c8aa72d
PA
10928
10929 cu->line_header = lh.release ();
10930 cu->line_header_die_owner = die;
527f3840 10931
5989a64e 10932 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10933 slot = NULL;
10934 else
10935 {
5989a64e 10936 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10937 &line_header_local,
10938 line_header_local_hash, INSERT);
10939 gdb_assert (slot != NULL);
10940 }
10941 if (slot != NULL && *slot == NULL)
10942 {
10943 /* This newly decoded line number information unit will be owned
10944 by line_header_hash hash table. */
10945 *slot = cu->line_header;
4c8aa72d 10946 cu->line_header_die_owner = NULL;
527f3840
JK
10947 }
10948 else
10949 {
10950 /* We cannot free any current entry in (*slot) as that struct line_header
10951 may be already used by multiple CUs. Create only temporary decoded
10952 line_header for this CU - it may happen at most once for each line
10953 number information unit. And if we're not using line_header_hash
10954 then this is what we want as well. */
10955 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10956 }
10957 decode_mapping = (die->tag != DW_TAG_partial_unit);
10958 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10959 decode_mapping);
fff8551c 10960
2ab95328
TT
10961}
10962
95554aad 10963/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10964
c906108c 10965static void
e7c27a73 10966read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10967{
5e22e966 10968 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10969 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10970 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10971 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10972 CORE_ADDR highpc = ((CORE_ADDR) 0);
10973 struct attribute *attr;
c906108c 10974 struct die_info *child_die;
e142c38c 10975 CORE_ADDR baseaddr;
6e70227d 10976
380618d6 10977 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10978 baseaddr = objfile->text_section_offset ();
c906108c 10979
fae299cd 10980 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10981
10982 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10983 from finish_block. */
2acceee2 10984 if (lowpc == ((CORE_ADDR) -1))
c906108c 10985 lowpc = highpc;
3e29f34a 10986 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10987
d721ba37 10988 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10989
f4b8a18d
KW
10990 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10991 standardised yet. As a workaround for the language detection we fall
10992 back to the DW_AT_producer string. */
10993 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10994 cu->language = language_opencl;
10995
3019eac3
DE
10996 /* Similar hack for Go. */
10997 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10998 set_cu_language (DW_LANG_Go, cu);
10999
c24bdb02 11000 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11001
11002 /* Decode line number information if present. We do this before
11003 processing child DIEs, so that the line header table is available
11004 for DW_AT_decl_file. */
d721ba37 11005 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11006
11007 /* Process all dies in compilation unit. */
11008 if (die->child != NULL)
11009 {
11010 child_die = die->child;
11011 while (child_die && child_die->tag)
11012 {
11013 process_die (child_die, cu);
436c571c 11014 child_die = child_die->sibling;
3019eac3
DE
11015 }
11016 }
11017
11018 /* Decode macro information, if present. Dwarf 2 macro information
11019 refers to information in the line number info statement program
11020 header, so we can only read it if we've read the header
11021 successfully. */
0af92d60
JK
11022 attr = dwarf2_attr (die, DW_AT_macros, cu);
11023 if (attr == NULL)
11024 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11025 if (attr && cu->line_header)
11026 {
11027 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11028 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11029
43f3e411 11030 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11031 }
11032 else
11033 {
11034 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11035 if (attr && cu->line_header)
11036 {
11037 unsigned int macro_offset = DW_UNSND (attr);
11038
43f3e411 11039 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11040 }
11041 }
3019eac3
DE
11042}
11043
c24bdb02
KS
11044void
11045dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11046{
f4dc4d17
DE
11047 struct type_unit_group *tu_group;
11048 int first_time;
3019eac3 11049 struct attribute *attr;
9c541725 11050 unsigned int i;
0186c6a7 11051 struct signatured_type *sig_type;
3019eac3 11052
f4dc4d17 11053 gdb_assert (per_cu->is_debug_types);
0186c6a7 11054 sig_type = (struct signatured_type *) per_cu;
3019eac3 11055
c24bdb02 11056 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11057
f4dc4d17 11058 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11059 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11060 if (sig_type->type_unit_group == NULL)
c24bdb02 11061 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11062 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11063
11064 /* If we've already processed this stmt_list there's no real need to
11065 do it again, we could fake it and just recreate the part we need
11066 (file name,index -> symtab mapping). If data shows this optimization
11067 is useful we can do it then. */
8adb8487
TT
11068 type_unit_group_unshareable *tug_unshare
11069 = per_objfile->get_type_unit_group_unshareable (tu_group);
11070 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11071
11072 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11073 debug info. */
fff8551c 11074 line_header_up lh;
f4dc4d17 11075 if (attr != NULL)
3019eac3 11076 {
9c541725 11077 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11078 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11079 }
11080 if (lh == NULL)
11081 {
11082 if (first_time)
c24bdb02 11083 start_symtab ("", NULL, 0);
f4dc4d17
DE
11084 else
11085 {
8adb8487 11086 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11087 gdb_assert (m_builder == nullptr);
8adb8487 11088 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11089 m_builder.reset (new struct buildsym_compunit
11090 (COMPUNIT_OBJFILE (cust), "",
11091 COMPUNIT_DIRNAME (cust),
11092 compunit_language (cust),
11093 0, cust));
770479f2 11094 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11095 }
f4dc4d17 11096 return;
3019eac3
DE
11097 }
11098
c24bdb02
KS
11099 line_header = lh.release ();
11100 line_header_die_owner = die;
3019eac3 11101
f4dc4d17
DE
11102 if (first_time)
11103 {
c24bdb02 11104 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11105
1fd60fc0
DE
11106 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11107 still initializing it, and our caller (a few levels up)
11108 process_full_type_unit still needs to know if this is the first
11109 time. */
11110
8adb8487 11111 tug_unshare->symtabs
4ac93832
TT
11112 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11113 struct symtab *, line_header->file_names_size ());
3019eac3 11114
7ba99d21
AT
11115 auto &file_names = line_header->file_names ();
11116 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11117 {
7ba99d21 11118 file_entry &fe = file_names[i];
c24bdb02
KS
11119 dwarf2_start_subfile (this, fe.name,
11120 fe.include_dir (line_header));
11121 buildsym_compunit *b = get_builder ();
11122 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11123 {
4c8aa72d
PA
11124 /* NOTE: start_subfile will recognize when it's been
11125 passed a file it has already seen. So we can't
11126 assume there's a simple mapping from
11127 cu->line_header->file_names to subfiles, plus
11128 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11129 b->get_current_subfile ()->symtab
11130 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11131 }
11132
c24bdb02 11133 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11134 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11135 }
11136 }
11137 else
3019eac3 11138 {
c24bdb02 11139 gdb_assert (m_builder == nullptr);
8adb8487 11140 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11141 m_builder.reset (new struct buildsym_compunit
11142 (COMPUNIT_OBJFILE (cust), "",
11143 COMPUNIT_DIRNAME (cust),
11144 compunit_language (cust),
11145 0, cust));
770479f2 11146 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11147
7ba99d21
AT
11148 auto &file_names = line_header->file_names ();
11149 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11150 {
7ba99d21 11151 file_entry &fe = file_names[i];
8adb8487 11152 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11153 }
3019eac3
DE
11154 }
11155
f4dc4d17
DE
11156 /* The main symtab is allocated last. Type units don't have DW_AT_name
11157 so they don't have a "real" (so to speak) symtab anyway.
11158 There is later code that will assign the main symtab to all symbols
11159 that don't have one. We need to handle the case of a symbol with a
11160 missing symtab (DW_AT_decl_file) anyway. */
11161}
3019eac3 11162
f4dc4d17
DE
11163/* Process DW_TAG_type_unit.
11164 For TUs we want to skip the first top level sibling if it's not the
11165 actual type being defined by this TU. In this case the first top
11166 level sibling is there to provide context only. */
3019eac3 11167
f4dc4d17
DE
11168static void
11169read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11170{
11171 struct die_info *child_die;
3019eac3 11172
f4dc4d17
DE
11173 prepare_one_comp_unit (cu, die, language_minimal);
11174
11175 /* Initialize (or reinitialize) the machinery for building symtabs.
11176 We do this before processing child DIEs, so that the line header table
11177 is available for DW_AT_decl_file. */
c24bdb02 11178 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11179
11180 if (die->child != NULL)
11181 {
11182 child_die = die->child;
11183 while (child_die && child_die->tag)
11184 {
11185 process_die (child_die, cu);
436c571c 11186 child_die = child_die->sibling;
f4dc4d17
DE
11187 }
11188 }
3019eac3
DE
11189}
11190\f
80626a55
DE
11191/* DWO/DWP files.
11192
11193 http://gcc.gnu.org/wiki/DebugFission
11194 http://gcc.gnu.org/wiki/DebugFissionDWP
11195
11196 To simplify handling of both DWO files ("object" files with the DWARF info)
11197 and DWP files (a file with the DWOs packaged up into one file), we treat
11198 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11199
11200static hashval_t
11201hash_dwo_file (const void *item)
11202{
9a3c8263 11203 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11204 hashval_t hash;
3019eac3 11205
a2ce51a0
DE
11206 hash = htab_hash_string (dwo_file->dwo_name);
11207 if (dwo_file->comp_dir != NULL)
11208 hash += htab_hash_string (dwo_file->comp_dir);
11209 return hash;
3019eac3
DE
11210}
11211
11212static int
11213eq_dwo_file (const void *item_lhs, const void *item_rhs)
11214{
9a3c8263
SM
11215 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11216 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11217
a2ce51a0
DE
11218 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11219 return 0;
11220 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11221 return lhs->comp_dir == rhs->comp_dir;
11222 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11223}
11224
11225/* Allocate a hash table for DWO files. */
11226
51ac9db5 11227static htab_up
298e9637 11228allocate_dwo_file_hash_table ()
3019eac3 11229{
51ac9db5
SM
11230 auto delete_dwo_file = [] (void *item)
11231 {
11232 struct dwo_file *dwo_file = (struct dwo_file *) item;
11233
11234 delete dwo_file;
11235 };
11236
bc68fb19
TT
11237 return htab_up (htab_create_alloc (41,
11238 hash_dwo_file,
11239 eq_dwo_file,
11240 delete_dwo_file,
11241 xcalloc, xfree));
3019eac3
DE
11242}
11243
80626a55
DE
11244/* Lookup DWO file DWO_NAME. */
11245
11246static void **
ed2dc618
SM
11247lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11248 const char *dwo_name,
11249 const char *comp_dir)
80626a55
DE
11250{
11251 struct dwo_file find_entry;
11252 void **slot;
11253
5989a64e
SM
11254 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11255 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11256
0ac5b59e
DE
11257 find_entry.dwo_name = dwo_name;
11258 find_entry.comp_dir = comp_dir;
5989a64e 11259 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11260 INSERT);
80626a55
DE
11261
11262 return slot;
11263}
11264
3019eac3
DE
11265static hashval_t
11266hash_dwo_unit (const void *item)
11267{
9a3c8263 11268 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11269
11270 /* This drops the top 32 bits of the id, but is ok for a hash. */
11271 return dwo_unit->signature;
11272}
11273
11274static int
11275eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11276{
9a3c8263
SM
11277 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11278 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11279
11280 /* The signature is assumed to be unique within the DWO file.
11281 So while object file CU dwo_id's always have the value zero,
11282 that's OK, assuming each object file DWO file has only one CU,
11283 and that's the rule for now. */
11284 return lhs->signature == rhs->signature;
11285}
11286
11287/* Allocate a hash table for DWO CUs,TUs.
11288 There is one of these tables for each of CUs,TUs for each DWO file. */
11289
b0b6a987 11290static htab_up
298e9637 11291allocate_dwo_unit_table ()
3019eac3
DE
11292{
11293 /* Start out with a pretty small number.
11294 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11295 return htab_up (htab_create_alloc (3,
11296 hash_dwo_unit,
11297 eq_dwo_unit,
11298 NULL, xcalloc, xfree));
3019eac3
DE
11299}
11300
19c3d4c9 11301/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11302
11303static void
19c3d4c9
DE
11304create_dwo_cu_reader (const struct die_reader_specs *reader,
11305 const gdb_byte *info_ptr,
11306 struct die_info *comp_unit_die,
c0ab21c2
TT
11307 struct dwo_file *dwo_file,
11308 struct dwo_unit *dwo_unit)
3019eac3
DE
11309{
11310 struct dwarf2_cu *cu = reader->cu;
9c541725 11311 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11312 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11313
a084a2a6
AT
11314 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11315 if (!signature.has_value ())
3019eac3 11316 {
b98664d3 11317 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11318 " its dwo_id [in module %s]"),
9d8780f0 11319 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11320 return;
11321 }
11322
3019eac3 11323 dwo_unit->dwo_file = dwo_file;
a084a2a6 11324 dwo_unit->signature = *signature;
8a0459fd 11325 dwo_unit->section = section;
9c541725 11326 dwo_unit->sect_off = sect_off;
3019eac3
DE
11327 dwo_unit->length = cu->per_cu->length;
11328
b4f54984 11329 if (dwarf_read_debug)
9d8780f0
SM
11330 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11331 sect_offset_str (sect_off),
9c541725 11332 hex_string (dwo_unit->signature));
3019eac3
DE
11333}
11334
33c5cd75 11335/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11336 Note: This function processes DWO files only, not DWP files. */
3019eac3 11337
33c5cd75 11338static void
ed2dc618 11339create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11340 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11341 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11342{
11343 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11344 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11345 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11346
96b79293 11347 section.read (objfile);
33c5cd75 11348 info_ptr = section.buffer;
3019eac3
DE
11349
11350 if (info_ptr == NULL)
33c5cd75 11351 return;
3019eac3 11352
b4f54984 11353 if (dwarf_read_debug)
19c3d4c9
DE
11354 {
11355 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11356 section.get_name (),
11357 section.get_file_name ());
19c3d4c9 11358 }
3019eac3 11359
33c5cd75 11360 end_ptr = info_ptr + section.size;
3019eac3
DE
11361 while (info_ptr < end_ptr)
11362 {
11363 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11364 struct dwo_unit read_unit {};
33c5cd75
DB
11365 struct dwo_unit *dwo_unit;
11366 void **slot;
11367 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11368
11369 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11370 per_cu.per_bfd = per_bfd;
3019eac3 11371 per_cu.is_debug_types = 0;
33c5cd75
DB
11372 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11373 per_cu.section = &section;
11374
ab432490 11375 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11376 if (!reader.dummy_p)
11377 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11378 &dwo_file, &read_unit);
33c5cd75
DB
11379 info_ptr += per_cu.length;
11380
11381 // If the unit could not be parsed, skip it.
c0ab21c2 11382 if (read_unit.dwo_file == NULL)
33c5cd75 11383 continue;
3019eac3 11384
33c5cd75 11385 if (cus_htab == NULL)
298e9637 11386 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11387
1859c670 11388 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11389 struct dwo_unit);
c0ab21c2 11390 *dwo_unit = read_unit;
b0b6a987 11391 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11392 gdb_assert (slot != NULL);
11393 if (*slot != NULL)
19c3d4c9 11394 {
33c5cd75
DB
11395 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11396 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11397
b98664d3 11398 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11399 " the entry at offset %s, signature %s"),
11400 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11401 hex_string (dwo_unit->signature));
19c3d4c9 11402 }
33c5cd75 11403 *slot = (void *)dwo_unit;
3019eac3 11404 }
3019eac3
DE
11405}
11406
80626a55
DE
11407/* DWP file .debug_{cu,tu}_index section format:
11408 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11409
d2415c6c
DE
11410 DWP Version 1:
11411
80626a55
DE
11412 Both index sections have the same format, and serve to map a 64-bit
11413 signature to a set of section numbers. Each section begins with a header,
11414 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11415 indexes, and a pool of 32-bit section numbers. The index sections will be
11416 aligned at 8-byte boundaries in the file.
11417
d2415c6c
DE
11418 The index section header consists of:
11419
11420 V, 32 bit version number
11421 -, 32 bits unused
11422 N, 32 bit number of compilation units or type units in the index
11423 M, 32 bit number of slots in the hash table
80626a55 11424
d2415c6c 11425 Numbers are recorded using the byte order of the application binary.
80626a55 11426
d2415c6c
DE
11427 The hash table begins at offset 16 in the section, and consists of an array
11428 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11429 order of the application binary). Unused slots in the hash table are 0.
11430 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11431
d2415c6c
DE
11432 The parallel table begins immediately after the hash table
11433 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11434 array of 32-bit indexes (using the byte order of the application binary),
11435 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11436 table contains a 32-bit index into the pool of section numbers. For unused
11437 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11438
73869dc2
DE
11439 The pool of section numbers begins immediately following the hash table
11440 (at offset 16 + 12 * M from the beginning of the section). The pool of
11441 section numbers consists of an array of 32-bit words (using the byte order
11442 of the application binary). Each item in the array is indexed starting
11443 from 0. The hash table entry provides the index of the first section
11444 number in the set. Additional section numbers in the set follow, and the
11445 set is terminated by a 0 entry (section number 0 is not used in ELF).
11446
11447 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11448 section must be the first entry in the set, and the .debug_abbrev.dwo must
11449 be the second entry. Other members of the set may follow in any order.
11450
11451 ---
11452
11453 DWP Version 2:
11454
11455 DWP Version 2 combines all the .debug_info, etc. sections into one,
11456 and the entries in the index tables are now offsets into these sections.
11457 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11458 section.
11459
11460 Index Section Contents:
11461 Header
11462 Hash Table of Signatures dwp_hash_table.hash_table
11463 Parallel Table of Indices dwp_hash_table.unit_table
11464 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11465 Table of Section Sizes dwp_hash_table.v2.sizes
11466
11467 The index section header consists of:
11468
11469 V, 32 bit version number
11470 L, 32 bit number of columns in the table of section offsets
11471 N, 32 bit number of compilation units or type units in the index
11472 M, 32 bit number of slots in the hash table
11473
11474 Numbers are recorded using the byte order of the application binary.
11475
11476 The hash table has the same format as version 1.
11477 The parallel table of indices has the same format as version 1,
11478 except that the entries are origin-1 indices into the table of sections
11479 offsets and the table of section sizes.
11480
11481 The table of offsets begins immediately following the parallel table
11482 (at offset 16 + 12 * M from the beginning of the section). The table is
11483 a two-dimensional array of 32-bit words (using the byte order of the
11484 application binary), with L columns and N+1 rows, in row-major order.
11485 Each row in the array is indexed starting from 0. The first row provides
11486 a key to the remaining rows: each column in this row provides an identifier
11487 for a debug section, and the offsets in the same column of subsequent rows
11488 refer to that section. The section identifiers are:
11489
11490 DW_SECT_INFO 1 .debug_info.dwo
11491 DW_SECT_TYPES 2 .debug_types.dwo
11492 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11493 DW_SECT_LINE 4 .debug_line.dwo
11494 DW_SECT_LOC 5 .debug_loc.dwo
11495 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11496 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11497 DW_SECT_MACRO 8 .debug_macro.dwo
11498
11499 The offsets provided by the CU and TU index sections are the base offsets
11500 for the contributions made by each CU or TU to the corresponding section
11501 in the package file. Each CU and TU header contains an abbrev_offset
11502 field, used to find the abbreviations table for that CU or TU within the
11503 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11504 be interpreted as relative to the base offset given in the index section.
11505 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11506 should be interpreted as relative to the base offset for .debug_line.dwo,
11507 and offsets into other debug sections obtained from DWARF attributes should
11508 also be interpreted as relative to the corresponding base offset.
11509
11510 The table of sizes begins immediately following the table of offsets.
11511 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11512 with L columns and N rows, in row-major order. Each row in the array is
11513 indexed starting from 1 (row 0 is shared by the two tables).
11514
11515 ---
11516
11517 Hash table lookup is handled the same in version 1 and 2:
11518
11519 We assume that N and M will not exceed 2^32 - 1.
11520 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11521
d2415c6c
DE
11522 Given a 64-bit compilation unit signature or a type signature S, an entry
11523 in the hash table is located as follows:
80626a55 11524
d2415c6c
DE
11525 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11526 the low-order k bits all set to 1.
80626a55 11527
d2415c6c 11528 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11529
d2415c6c
DE
11530 3) If the hash table entry at index H matches the signature, use that
11531 entry. If the hash table entry at index H is unused (all zeroes),
11532 terminate the search: the signature is not present in the table.
80626a55 11533
d2415c6c 11534 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11535
d2415c6c 11536 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11537 to stop at an unused slot or find the match. */
80626a55
DE
11538
11539/* Create a hash table to map DWO IDs to their CU/TU entry in
11540 .debug_{info,types}.dwo in DWP_FILE.
11541 Returns NULL if there isn't one.
11542 Note: This function processes DWP files only, not DWO files. */
11543
11544static struct dwp_hash_table *
ed2dc618
SM
11545create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11546 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11547{
11548 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11549 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11550 const gdb_byte *index_ptr, *index_end;
80626a55 11551 struct dwarf2_section_info *index;
73869dc2 11552 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11553 struct dwp_hash_table *htab;
11554
11555 if (is_debug_types)
11556 index = &dwp_file->sections.tu_index;
11557 else
11558 index = &dwp_file->sections.cu_index;
11559
96b79293 11560 if (index->empty ())
80626a55 11561 return NULL;
96b79293 11562 index->read (objfile);
80626a55
DE
11563
11564 index_ptr = index->buffer;
11565 index_end = index_ptr + index->size;
11566
11567 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11568 index_ptr += 4;
11569 if (version == 2)
11570 nr_columns = read_4_bytes (dbfd, index_ptr);
11571 else
11572 nr_columns = 0;
11573 index_ptr += 4;
80626a55
DE
11574 nr_units = read_4_bytes (dbfd, index_ptr);
11575 index_ptr += 4;
11576 nr_slots = read_4_bytes (dbfd, index_ptr);
11577 index_ptr += 4;
11578
73869dc2 11579 if (version != 1 && version != 2)
80626a55 11580 {
21aa081e 11581 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11582 " [in module %s]"),
21aa081e 11583 pulongest (version), dwp_file->name);
80626a55
DE
11584 }
11585 if (nr_slots != (nr_slots & -nr_slots))
11586 {
21aa081e 11587 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11588 " is not power of 2 [in module %s]"),
21aa081e 11589 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11590 }
11591
5989a64e 11592 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11593 htab->version = version;
11594 htab->nr_columns = nr_columns;
80626a55
DE
11595 htab->nr_units = nr_units;
11596 htab->nr_slots = nr_slots;
11597 htab->hash_table = index_ptr;
11598 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11599
11600 /* Exit early if the table is empty. */
11601 if (nr_slots == 0 || nr_units == 0
11602 || (version == 2 && nr_columns == 0))
11603 {
11604 /* All must be zero. */
11605 if (nr_slots != 0 || nr_units != 0
11606 || (version == 2 && nr_columns != 0))
11607 {
b98664d3 11608 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11609 " all zero [in modules %s]"),
11610 dwp_file->name);
11611 }
11612 return htab;
11613 }
11614
11615 if (version == 1)
11616 {
11617 htab->section_pool.v1.indices =
11618 htab->unit_table + sizeof (uint32_t) * nr_slots;
11619 /* It's harder to decide whether the section is too small in v1.
11620 V1 is deprecated anyway so we punt. */
11621 }
11622 else
11623 {
11624 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11625 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11626 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11627 /* Reverse map for error checking. */
11628 int ids_seen[DW_SECT_MAX + 1];
11629 int i;
11630
11631 if (nr_columns < 2)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, too few columns"
11634 " in section table [in module %s]"),
11635 dwp_file->name);
11636 }
11637 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, too many columns"
11640 " in section table [in module %s]"),
11641 dwp_file->name);
11642 }
04fd5eed
GB
11643 memset (ids, 255, sizeof_ids);
11644 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11645 for (i = 0; i < nr_columns; ++i)
11646 {
11647 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11648
11649 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11652 " in section table [in module %s]"),
11653 id, dwp_file->name);
11654 }
11655 if (ids_seen[id] != -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11658 " id %d in section table [in module %s]"),
11659 id, dwp_file->name);
11660 }
11661 ids_seen[id] = i;
11662 ids[i] = id;
11663 }
11664 /* Must have exactly one info or types section. */
11665 if (((ids_seen[DW_SECT_INFO] != -1)
11666 + (ids_seen[DW_SECT_TYPES] != -1))
11667 != 1)
11668 {
11669 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11670 " DWO info/types section [in module %s]"),
11671 dwp_file->name);
11672 }
11673 /* Must have an abbrev section. */
11674 if (ids_seen[DW_SECT_ABBREV] == -1)
11675 {
11676 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11677 " section [in module %s]"),
11678 dwp_file->name);
11679 }
11680 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11681 htab->section_pool.v2.sizes =
11682 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11683 * nr_units * nr_columns);
11684 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11685 * nr_units * nr_columns))
11686 > index_end)
11687 {
11688 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11689 " [in module %s]"),
11690 dwp_file->name);
11691 }
11692 }
80626a55
DE
11693
11694 return htab;
11695}
11696
11697/* Update SECTIONS with the data from SECTP.
11698
11699 This function is like the other "locate" section routines that are
11700 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11701 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11702
11703 The result is non-zero for success, or zero if an error was found. */
11704
11705static int
73869dc2
DE
11706locate_v1_virtual_dwo_sections (asection *sectp,
11707 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11708{
11709 const struct dwop_section_names *names = &dwop_section_names;
11710
11711 if (section_is_p (sectp->name, &names->abbrev_dwo))
11712 {
11713 /* There can be only one. */
049412e3 11714 if (sections->abbrev.s.section != NULL)
80626a55 11715 return 0;
049412e3 11716 sections->abbrev.s.section = sectp;
fd361982 11717 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11718 }
11719 else if (section_is_p (sectp->name, &names->info_dwo)
11720 || section_is_p (sectp->name, &names->types_dwo))
11721 {
11722 /* There can be only one. */
049412e3 11723 if (sections->info_or_types.s.section != NULL)
80626a55 11724 return 0;
049412e3 11725 sections->info_or_types.s.section = sectp;
fd361982 11726 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11727 }
11728 else if (section_is_p (sectp->name, &names->line_dwo))
11729 {
11730 /* There can be only one. */
049412e3 11731 if (sections->line.s.section != NULL)
80626a55 11732 return 0;
049412e3 11733 sections->line.s.section = sectp;
fd361982 11734 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11735 }
11736 else if (section_is_p (sectp->name, &names->loc_dwo))
11737 {
11738 /* There can be only one. */
049412e3 11739 if (sections->loc.s.section != NULL)
80626a55 11740 return 0;
049412e3 11741 sections->loc.s.section = sectp;
fd361982 11742 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11743 }
11744 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11745 {
11746 /* There can be only one. */
049412e3 11747 if (sections->macinfo.s.section != NULL)
80626a55 11748 return 0;
049412e3 11749 sections->macinfo.s.section = sectp;
fd361982 11750 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11751 }
11752 else if (section_is_p (sectp->name, &names->macro_dwo))
11753 {
11754 /* There can be only one. */
049412e3 11755 if (sections->macro.s.section != NULL)
80626a55 11756 return 0;
049412e3 11757 sections->macro.s.section = sectp;
fd361982 11758 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11759 }
11760 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11761 {
11762 /* There can be only one. */
049412e3 11763 if (sections->str_offsets.s.section != NULL)
80626a55 11764 return 0;
049412e3 11765 sections->str_offsets.s.section = sectp;
fd361982 11766 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11767 }
11768 else
11769 {
11770 /* No other kind of section is valid. */
11771 return 0;
11772 }
11773
11774 return 1;
11775}
11776
73869dc2
DE
11777/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11778 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11779 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11780 This is for DWP version 1 files. */
80626a55
DE
11781
11782static struct dwo_unit *
ed2dc618
SM
11783create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11784 struct dwp_file *dwp_file,
73869dc2
DE
11785 uint32_t unit_index,
11786 const char *comp_dir,
11787 ULONGEST signature, int is_debug_types)
80626a55 11788{
73869dc2
DE
11789 const struct dwp_hash_table *dwp_htab =
11790 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11791 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11792 const char *kind = is_debug_types ? "TU" : "CU";
11793 struct dwo_file *dwo_file;
11794 struct dwo_unit *dwo_unit;
73869dc2 11795 struct virtual_v1_dwo_sections sections;
80626a55 11796 void **dwo_file_slot;
80626a55
DE
11797 int i;
11798
73869dc2
DE
11799 gdb_assert (dwp_file->version == 1);
11800
b4f54984 11801 if (dwarf_read_debug)
80626a55 11802 {
73869dc2 11803 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11804 kind,
73869dc2 11805 pulongest (unit_index), hex_string (signature),
80626a55
DE
11806 dwp_file->name);
11807 }
11808
19ac8c2e 11809 /* Fetch the sections of this DWO unit.
80626a55
DE
11810 Put a limit on the number of sections we look for so that bad data
11811 doesn't cause us to loop forever. */
11812
73869dc2 11813#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11814 (1 /* .debug_info or .debug_types */ \
11815 + 1 /* .debug_abbrev */ \
11816 + 1 /* .debug_line */ \
11817 + 1 /* .debug_loc */ \
11818 + 1 /* .debug_str_offsets */ \
19ac8c2e 11819 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11820 + 1 /* trailing zero */)
11821
11822 memset (&sections, 0, sizeof (sections));
80626a55 11823
73869dc2 11824 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11825 {
11826 asection *sectp;
11827 uint32_t section_nr =
11828 read_4_bytes (dbfd,
73869dc2
DE
11829 dwp_htab->section_pool.v1.indices
11830 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11831
11832 if (section_nr == 0)
11833 break;
11834 if (section_nr >= dwp_file->num_sections)
11835 {
11836 error (_("Dwarf Error: bad DWP hash table, section number too large"
11837 " [in module %s]"),
11838 dwp_file->name);
11839 }
11840
11841 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11842 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11843 {
11844 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11845 " [in module %s]"),
11846 dwp_file->name);
11847 }
11848 }
11849
11850 if (i < 2
96b79293
TT
11851 || sections.info_or_types.empty ()
11852 || sections.abbrev.empty ())
80626a55
DE
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11855 " [in module %s]"),
11856 dwp_file->name);
11857 }
73869dc2 11858 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11861 " [in module %s]"),
11862 dwp_file->name);
11863 }
11864
11865 /* It's easier for the rest of the code if we fake a struct dwo_file and
11866 have dwo_unit "live" in that. At least for now.
11867
11868 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11869 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11870 file, we can combine them back into a virtual DWO file to save space
11871 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11872 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11873
791afaa2
TT
11874 std::string virtual_dwo_name =
11875 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11876 sections.abbrev.get_id (),
11877 sections.line.get_id (),
11878 sections.loc.get_id (),
11879 sections.str_offsets.get_id ());
80626a55 11880 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11881 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11882 virtual_dwo_name.c_str (),
11883 comp_dir);
80626a55
DE
11884 /* Create one if necessary. */
11885 if (*dwo_file_slot == NULL)
11886 {
b4f54984 11887 if (dwarf_read_debug)
80626a55
DE
11888 {
11889 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11890 virtual_dwo_name.c_str ());
80626a55 11891 }
51ac9db5 11892 dwo_file = new struct dwo_file;
45940949 11893 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11894 dwo_file->comp_dir = comp_dir;
80626a55
DE
11895 dwo_file->sections.abbrev = sections.abbrev;
11896 dwo_file->sections.line = sections.line;
11897 dwo_file->sections.loc = sections.loc;
11898 dwo_file->sections.macinfo = sections.macinfo;
11899 dwo_file->sections.macro = sections.macro;
11900 dwo_file->sections.str_offsets = sections.str_offsets;
11901 /* The "str" section is global to the entire DWP file. */
11902 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11903 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11904 there's no need to record it in dwo_file.
11905 Also, we can't simply record type sections in dwo_file because
11906 we record a pointer into the vector in dwo_unit. As we collect more
11907 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11908 for it, invalidating all copies of pointers into the previous
11909 contents. */
80626a55
DE
11910 *dwo_file_slot = dwo_file;
11911 }
11912 else
11913 {
b4f54984 11914 if (dwarf_read_debug)
80626a55
DE
11915 {
11916 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11917 virtual_dwo_name.c_str ());
80626a55 11918 }
9a3c8263 11919 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11920 }
80626a55 11921
5989a64e 11922 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11923 dwo_unit->dwo_file = dwo_file;
11924 dwo_unit->signature = signature;
8d749320 11925 dwo_unit->section =
5989a64e 11926 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11927 *dwo_unit->section = sections.info_or_types;
57d63ce2 11928 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11929
11930 return dwo_unit;
11931}
11932
73869dc2
DE
11933/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11934 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11935 piece within that section used by a TU/CU, return a virtual section
11936 of just that piece. */
11937
11938static struct dwarf2_section_info
ed2dc618
SM
11939create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11940 struct dwarf2_section_info *section,
73869dc2
DE
11941 bfd_size_type offset, bfd_size_type size)
11942{
11943 struct dwarf2_section_info result;
11944 asection *sectp;
11945
11946 gdb_assert (section != NULL);
11947 gdb_assert (!section->is_virtual);
11948
11949 memset (&result, 0, sizeof (result));
11950 result.s.containing_section = section;
dc4ccb6f 11951 result.is_virtual = true;
73869dc2
DE
11952
11953 if (size == 0)
11954 return result;
11955
96b79293 11956 sectp = section->get_bfd_section ();
73869dc2
DE
11957
11958 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11959 bounds of the real section. This is a pretty-rare event, so just
11960 flag an error (easier) instead of a warning and trying to cope. */
11961 if (sectp == NULL
fd361982 11962 || offset + size > bfd_section_size (sectp))
73869dc2 11963 {
73869dc2
DE
11964 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11965 " in section %s [in module %s]"),
fd361982 11966 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11967 objfile_name (dwarf2_per_objfile->objfile));
11968 }
11969
11970 result.virtual_offset = offset;
11971 result.size = size;
11972 return result;
11973}
11974
11975/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11976 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11977 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11978 This is for DWP version 2 files. */
11979
11980static struct dwo_unit *
ed2dc618
SM
11981create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11982 struct dwp_file *dwp_file,
73869dc2
DE
11983 uint32_t unit_index,
11984 const char *comp_dir,
11985 ULONGEST signature, int is_debug_types)
11986{
73869dc2
DE
11987 const struct dwp_hash_table *dwp_htab =
11988 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11989 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11990 const char *kind = is_debug_types ? "TU" : "CU";
11991 struct dwo_file *dwo_file;
11992 struct dwo_unit *dwo_unit;
11993 struct virtual_v2_dwo_sections sections;
11994 void **dwo_file_slot;
73869dc2
DE
11995 int i;
11996
11997 gdb_assert (dwp_file->version == 2);
11998
b4f54984 11999 if (dwarf_read_debug)
73869dc2
DE
12000 {
12001 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12002 kind,
12003 pulongest (unit_index), hex_string (signature),
12004 dwp_file->name);
12005 }
12006
12007 /* Fetch the section offsets of this DWO unit. */
12008
12009 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12010
12011 for (i = 0; i < dwp_htab->nr_columns; ++i)
12012 {
12013 uint32_t offset = read_4_bytes (dbfd,
12014 dwp_htab->section_pool.v2.offsets
12015 + (((unit_index - 1) * dwp_htab->nr_columns
12016 + i)
12017 * sizeof (uint32_t)));
12018 uint32_t size = read_4_bytes (dbfd,
12019 dwp_htab->section_pool.v2.sizes
12020 + (((unit_index - 1) * dwp_htab->nr_columns
12021 + i)
12022 * sizeof (uint32_t)));
12023
12024 switch (dwp_htab->section_pool.v2.section_ids[i])
12025 {
12026 case DW_SECT_INFO:
12027 case DW_SECT_TYPES:
12028 sections.info_or_types_offset = offset;
12029 sections.info_or_types_size = size;
12030 break;
12031 case DW_SECT_ABBREV:
12032 sections.abbrev_offset = offset;
12033 sections.abbrev_size = size;
12034 break;
12035 case DW_SECT_LINE:
12036 sections.line_offset = offset;
12037 sections.line_size = size;
12038 break;
12039 case DW_SECT_LOC:
12040 sections.loc_offset = offset;
12041 sections.loc_size = size;
12042 break;
12043 case DW_SECT_STR_OFFSETS:
12044 sections.str_offsets_offset = offset;
12045 sections.str_offsets_size = size;
12046 break;
12047 case DW_SECT_MACINFO:
12048 sections.macinfo_offset = offset;
12049 sections.macinfo_size = size;
12050 break;
12051 case DW_SECT_MACRO:
12052 sections.macro_offset = offset;
12053 sections.macro_size = size;
12054 break;
12055 }
12056 }
12057
12058 /* It's easier for the rest of the code if we fake a struct dwo_file and
12059 have dwo_unit "live" in that. At least for now.
12060
12061 The DWP file can be made up of a random collection of CUs and TUs.
12062 However, for each CU + set of TUs that came from the same original DWO
12063 file, we can combine them back into a virtual DWO file to save space
12064 (fewer struct dwo_file objects to allocate). Remember that for really
12065 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12066
791afaa2
TT
12067 std::string virtual_dwo_name =
12068 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12069 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12070 (long) (sections.line_size ? sections.line_offset : 0),
12071 (long) (sections.loc_size ? sections.loc_offset : 0),
12072 (long) (sections.str_offsets_size
12073 ? sections.str_offsets_offset : 0));
73869dc2 12074 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12075 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12076 virtual_dwo_name.c_str (),
12077 comp_dir);
73869dc2
DE
12078 /* Create one if necessary. */
12079 if (*dwo_file_slot == NULL)
12080 {
b4f54984 12081 if (dwarf_read_debug)
73869dc2
DE
12082 {
12083 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12084 virtual_dwo_name.c_str ());
73869dc2 12085 }
51ac9db5 12086 dwo_file = new struct dwo_file;
45940949 12087 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12088 dwo_file->comp_dir = comp_dir;
12089 dwo_file->sections.abbrev =
ed2dc618 12090 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12091 sections.abbrev_offset, sections.abbrev_size);
12092 dwo_file->sections.line =
ed2dc618 12093 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12094 sections.line_offset, sections.line_size);
12095 dwo_file->sections.loc =
ed2dc618 12096 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12097 sections.loc_offset, sections.loc_size);
12098 dwo_file->sections.macinfo =
ed2dc618 12099 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12100 sections.macinfo_offset, sections.macinfo_size);
12101 dwo_file->sections.macro =
ed2dc618 12102 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12103 sections.macro_offset, sections.macro_size);
12104 dwo_file->sections.str_offsets =
ed2dc618
SM
12105 create_dwp_v2_section (dwarf2_per_objfile,
12106 &dwp_file->sections.str_offsets,
73869dc2
DE
12107 sections.str_offsets_offset,
12108 sections.str_offsets_size);
12109 /* The "str" section is global to the entire DWP file. */
12110 dwo_file->sections.str = dwp_file->sections.str;
12111 /* The info or types section is assigned below to dwo_unit,
12112 there's no need to record it in dwo_file.
12113 Also, we can't simply record type sections in dwo_file because
12114 we record a pointer into the vector in dwo_unit. As we collect more
12115 types we'll grow the vector and eventually have to reallocate space
12116 for it, invalidating all copies of pointers into the previous
12117 contents. */
12118 *dwo_file_slot = dwo_file;
12119 }
12120 else
12121 {
b4f54984 12122 if (dwarf_read_debug)
73869dc2
DE
12123 {
12124 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12125 virtual_dwo_name.c_str ());
73869dc2 12126 }
9a3c8263 12127 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12128 }
73869dc2 12129
5989a64e 12130 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12131 dwo_unit->dwo_file = dwo_file;
12132 dwo_unit->signature = signature;
8d749320 12133 dwo_unit->section =
5989a64e 12134 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12135 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12136 is_debug_types
73869dc2
DE
12137 ? &dwp_file->sections.types
12138 : &dwp_file->sections.info,
12139 sections.info_or_types_offset,
12140 sections.info_or_types_size);
12141 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12142
12143 return dwo_unit;
12144}
12145
57d63ce2
DE
12146/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12147 Returns NULL if the signature isn't found. */
80626a55
DE
12148
12149static struct dwo_unit *
ed2dc618
SM
12150lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12151 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12152 ULONGEST signature, int is_debug_types)
80626a55 12153{
57d63ce2
DE
12154 const struct dwp_hash_table *dwp_htab =
12155 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12156 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12157 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12158 uint32_t hash = signature & mask;
12159 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12160 unsigned int i;
12161 void **slot;
870f88f7 12162 struct dwo_unit find_dwo_cu;
80626a55
DE
12163
12164 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12165 find_dwo_cu.signature = signature;
19ac8c2e 12166 slot = htab_find_slot (is_debug_types
48b490f2
TT
12167 ? dwp_file->loaded_tus.get ()
12168 : dwp_file->loaded_cus.get (),
19ac8c2e 12169 &find_dwo_cu, INSERT);
80626a55
DE
12170
12171 if (*slot != NULL)
9a3c8263 12172 return (struct dwo_unit *) *slot;
80626a55
DE
12173
12174 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12175 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12176 {
12177 ULONGEST signature_in_table;
12178
12179 signature_in_table =
57d63ce2 12180 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12181 if (signature_in_table == signature)
12182 {
57d63ce2
DE
12183 uint32_t unit_index =
12184 read_4_bytes (dbfd,
12185 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12186
73869dc2
DE
12187 if (dwp_file->version == 1)
12188 {
ed2dc618
SM
12189 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12190 dwp_file, unit_index,
73869dc2
DE
12191 comp_dir, signature,
12192 is_debug_types);
12193 }
12194 else
12195 {
ed2dc618
SM
12196 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12197 dwp_file, unit_index,
73869dc2
DE
12198 comp_dir, signature,
12199 is_debug_types);
12200 }
9a3c8263 12201 return (struct dwo_unit *) *slot;
80626a55
DE
12202 }
12203 if (signature_in_table == 0)
12204 return NULL;
12205 hash = (hash + hash2) & mask;
12206 }
12207
12208 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12209 " [in module %s]"),
12210 dwp_file->name);
12211}
12212
ab5088bf 12213/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12214 Open the file specified by FILE_NAME and hand it off to BFD for
12215 preliminary analysis. Return a newly initialized bfd *, which
12216 includes a canonicalized copy of FILE_NAME.
80626a55 12217 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12218 SEARCH_CWD is true if the current directory is to be searched.
12219 It will be searched before debug-file-directory.
13aaf454
DE
12220 If successful, the file is added to the bfd include table of the
12221 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12222 If unable to find/open the file, return NULL.
3019eac3
DE
12223 NOTE: This function is derived from symfile_bfd_open. */
12224
192b62ce 12225static gdb_bfd_ref_ptr
ed2dc618
SM
12226try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12227 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12228{
24b9144d 12229 int desc;
9c02c129
DE
12230 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12231 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12232 to debug_file_directory. */
e0cc99a6 12233 const char *search_path;
9c02c129
DE
12234 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12235
e0cc99a6 12236 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12237 if (search_cwd)
12238 {
12239 if (*debug_file_directory != '\0')
e0cc99a6
TT
12240 {
12241 search_path_holder.reset (concat (".", dirname_separator_string,
12242 debug_file_directory,
12243 (char *) NULL));
12244 search_path = search_path_holder.get ();
12245 }
6ac97d4c 12246 else
e0cc99a6 12247 search_path = ".";
6ac97d4c 12248 }
9c02c129 12249 else
e0cc99a6 12250 search_path = debug_file_directory;
3019eac3 12251
24b9144d 12252 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12253 if (is_dwp)
12254 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12255
12256 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12257 desc = openp (search_path, flags, file_name,
3019eac3
DE
12258 O_RDONLY | O_BINARY, &absolute_name);
12259 if (desc < 0)
12260 return NULL;
12261
e0cc99a6
TT
12262 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12263 gnutarget, desc));
9c02c129
DE
12264 if (sym_bfd == NULL)
12265 return NULL;
192b62ce 12266 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12267
192b62ce
TT
12268 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12269 return NULL;
3019eac3 12270
13aaf454
DE
12271 /* Success. Record the bfd as having been included by the objfile's bfd.
12272 This is important because things like demangled_names_hash lives in the
12273 objfile's per_bfd space and may have references to things like symbol
12274 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12275 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12276
3019eac3
DE
12277 return sym_bfd;
12278}
12279
ab5088bf 12280/* Try to open DWO file FILE_NAME.
3019eac3
DE
12281 COMP_DIR is the DW_AT_comp_dir attribute.
12282 The result is the bfd handle of the file.
12283 If there is a problem finding or opening the file, return NULL.
12284 Upon success, the canonicalized path of the file is stored in the bfd,
12285 same as symfile_bfd_open. */
12286
192b62ce 12287static gdb_bfd_ref_ptr
ed2dc618
SM
12288open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12289 const char *file_name, const char *comp_dir)
3019eac3 12290{
80626a55 12291 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12292 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12293 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12294
12295 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12296
12297 if (comp_dir != NULL)
12298 {
43816ebc
TT
12299 gdb::unique_xmalloc_ptr<char> path_to_try
12300 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12301
12302 /* NOTE: If comp_dir is a relative path, this will also try the
12303 search path, which seems useful. */
ed2dc618 12304 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12305 path_to_try.get (),
ed2dc618 12306 0 /*is_dwp*/,
192b62ce 12307 1 /*search_cwd*/));
3019eac3
DE
12308 if (abfd != NULL)
12309 return abfd;
12310 }
12311
12312 /* That didn't work, try debug-file-directory, which, despite its name,
12313 is a list of paths. */
12314
12315 if (*debug_file_directory == '\0')
12316 return NULL;
12317
ed2dc618
SM
12318 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12319 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12320}
12321
80626a55
DE
12322/* This function is mapped across the sections and remembers the offset and
12323 size of each of the DWO debugging sections we are interested in. */
12324
12325static void
12326dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12327{
9a3c8263 12328 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12329 const struct dwop_section_names *names = &dwop_section_names;
12330
12331 if (section_is_p (sectp->name, &names->abbrev_dwo))
12332 {
049412e3 12333 dwo_sections->abbrev.s.section = sectp;
fd361982 12334 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12335 }
12336 else if (section_is_p (sectp->name, &names->info_dwo))
12337 {
049412e3 12338 dwo_sections->info.s.section = sectp;
fd361982 12339 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12340 }
12341 else if (section_is_p (sectp->name, &names->line_dwo))
12342 {
049412e3 12343 dwo_sections->line.s.section = sectp;
fd361982 12344 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12345 }
12346 else if (section_is_p (sectp->name, &names->loc_dwo))
12347 {
049412e3 12348 dwo_sections->loc.s.section = sectp;
fd361982 12349 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12350 }
41144253 12351 else if (section_is_p (sectp->name, &names->loclists_dwo))
12352 {
12353 dwo_sections->loclists.s.section = sectp;
12354 dwo_sections->loclists.size = bfd_section_size (sectp);
12355 }
80626a55
DE
12356 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12357 {
049412e3 12358 dwo_sections->macinfo.s.section = sectp;
fd361982 12359 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12360 }
12361 else if (section_is_p (sectp->name, &names->macro_dwo))
12362 {
049412e3 12363 dwo_sections->macro.s.section = sectp;
fd361982 12364 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12365 }
12366 else if (section_is_p (sectp->name, &names->str_dwo))
12367 {
049412e3 12368 dwo_sections->str.s.section = sectp;
fd361982 12369 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12370 }
12371 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12372 {
049412e3 12373 dwo_sections->str_offsets.s.section = sectp;
fd361982 12374 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12375 }
12376 else if (section_is_p (sectp->name, &names->types_dwo))
12377 {
12378 struct dwarf2_section_info type_section;
12379
12380 memset (&type_section, 0, sizeof (type_section));
049412e3 12381 type_section.s.section = sectp;
fd361982 12382 type_section.size = bfd_section_size (sectp);
fd5866f6 12383 dwo_sections->types.push_back (type_section);
80626a55
DE
12384 }
12385}
12386
ab5088bf 12387/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12388 by PER_CU. This is for the non-DWP case.
80626a55 12389 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12390
12391static struct dwo_file *
4ab09049
SM
12392open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12393 const char *comp_dir)
3019eac3 12394{
4ab09049 12395 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12396
fb1eb2f9 12397 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12398 if (dbfd == NULL)
12399 {
b4f54984 12400 if (dwarf_read_debug)
80626a55
DE
12401 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12402 return NULL;
12403 }
263db9a1 12404
51ac9db5 12405 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12406 dwo_file->dwo_name = dwo_name;
12407 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12408 dwo_file->dbfd = std::move (dbfd);
3019eac3 12409
fb1eb2f9 12410 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12411 &dwo_file->sections);
3019eac3 12412
4ab09049 12413 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12414 dwo_file->sections.info, dwo_file->cus);
3019eac3 12415
263db9a1 12416 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12417 dwo_file->sections.types, dwo_file->tus);
3019eac3 12418
b4f54984 12419 if (dwarf_read_debug)
80626a55
DE
12420 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12421
263db9a1 12422 return dwo_file.release ();
3019eac3
DE
12423}
12424
80626a55 12425/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12426 size of each of the DWP debugging sections common to version 1 and 2 that
12427 we are interested in. */
3019eac3 12428
80626a55 12429static void
73869dc2
DE
12430dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12431 void *dwp_file_ptr)
3019eac3 12432{
9a3c8263 12433 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12434 const struct dwop_section_names *names = &dwop_section_names;
12435 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12436
80626a55 12437 /* Record the ELF section number for later lookup: this is what the
73869dc2 12438 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12439 gdb_assert (elf_section_nr < dwp_file->num_sections);
12440 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12441
80626a55
DE
12442 /* Look for specific sections that we need. */
12443 if (section_is_p (sectp->name, &names->str_dwo))
12444 {
049412e3 12445 dwp_file->sections.str.s.section = sectp;
fd361982 12446 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12447 }
12448 else if (section_is_p (sectp->name, &names->cu_index))
12449 {
049412e3 12450 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12451 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12452 }
12453 else if (section_is_p (sectp->name, &names->tu_index))
12454 {
049412e3 12455 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12456 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12457 }
12458}
3019eac3 12459
73869dc2
DE
12460/* This function is mapped across the sections and remembers the offset and
12461 size of each of the DWP version 2 debugging sections that we are interested
12462 in. This is split into a separate function because we don't know if we
12463 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12464
12465static void
12466dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12467{
9a3c8263 12468 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12469 const struct dwop_section_names *names = &dwop_section_names;
12470 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12471
12472 /* Record the ELF section number for later lookup: this is what the
12473 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12474 gdb_assert (elf_section_nr < dwp_file->num_sections);
12475 dwp_file->elf_sections[elf_section_nr] = sectp;
12476
12477 /* Look for specific sections that we need. */
12478 if (section_is_p (sectp->name, &names->abbrev_dwo))
12479 {
049412e3 12480 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12481 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12482 }
12483 else if (section_is_p (sectp->name, &names->info_dwo))
12484 {
049412e3 12485 dwp_file->sections.info.s.section = sectp;
fd361982 12486 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12487 }
12488 else if (section_is_p (sectp->name, &names->line_dwo))
12489 {
049412e3 12490 dwp_file->sections.line.s.section = sectp;
fd361982 12491 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12492 }
12493 else if (section_is_p (sectp->name, &names->loc_dwo))
12494 {
049412e3 12495 dwp_file->sections.loc.s.section = sectp;
fd361982 12496 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12497 }
12498 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12499 {
049412e3 12500 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12501 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12502 }
12503 else if (section_is_p (sectp->name, &names->macro_dwo))
12504 {
049412e3 12505 dwp_file->sections.macro.s.section = sectp;
fd361982 12506 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12507 }
12508 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12509 {
049412e3 12510 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12511 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12512 }
12513 else if (section_is_p (sectp->name, &names->types_dwo))
12514 {
049412e3 12515 dwp_file->sections.types.s.section = sectp;
fd361982 12516 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12517 }
12518}
12519
80626a55 12520/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12521
80626a55
DE
12522static hashval_t
12523hash_dwp_loaded_cutus (const void *item)
12524{
9a3c8263 12525 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12526
80626a55
DE
12527 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12528 return dwo_unit->signature;
3019eac3
DE
12529}
12530
80626a55 12531/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12532
80626a55
DE
12533static int
12534eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12535{
9a3c8263
SM
12536 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12537 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12538
80626a55
DE
12539 return dua->signature == dub->signature;
12540}
3019eac3 12541
80626a55 12542/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12543
48b490f2 12544static htab_up
298e9637 12545allocate_dwp_loaded_cutus_table ()
80626a55 12546{
48b490f2
TT
12547 return htab_up (htab_create_alloc (3,
12548 hash_dwp_loaded_cutus,
12549 eq_dwp_loaded_cutus,
12550 NULL, xcalloc, xfree));
80626a55 12551}
3019eac3 12552
ab5088bf
DE
12553/* Try to open DWP file FILE_NAME.
12554 The result is the bfd handle of the file.
12555 If there is a problem finding or opening the file, return NULL.
12556 Upon success, the canonicalized path of the file is stored in the bfd,
12557 same as symfile_bfd_open. */
12558
192b62ce 12559static gdb_bfd_ref_ptr
ed2dc618
SM
12560open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12561 const char *file_name)
ab5088bf 12562{
ed2dc618
SM
12563 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12564 1 /*is_dwp*/,
192b62ce 12565 1 /*search_cwd*/));
6ac97d4c
DE
12566 if (abfd != NULL)
12567 return abfd;
12568
12569 /* Work around upstream bug 15652.
12570 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12571 [Whether that's a "bug" is debatable, but it is getting in our way.]
12572 We have no real idea where the dwp file is, because gdb's realpath-ing
12573 of the executable's path may have discarded the needed info.
12574 [IWBN if the dwp file name was recorded in the executable, akin to
12575 .gnu_debuglink, but that doesn't exist yet.]
12576 Strip the directory from FILE_NAME and search again. */
12577 if (*debug_file_directory != '\0')
12578 {
12579 /* Don't implicitly search the current directory here.
12580 If the user wants to search "." to handle this case,
12581 it must be added to debug-file-directory. */
ed2dc618
SM
12582 return try_open_dwop_file (dwarf2_per_objfile,
12583 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12584 0 /*search_cwd*/);
12585 }
12586
12587 return NULL;
ab5088bf
DE
12588}
12589
80626a55
DE
12590/* Initialize the use of the DWP file for the current objfile.
12591 By convention the name of the DWP file is ${objfile}.dwp.
12592 The result is NULL if it can't be found. */
a766d390 12593
400174b1 12594static std::unique_ptr<struct dwp_file>
ed2dc618 12595open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12596{
12597 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12598
82bf32bc
JK
12599 /* Try to find first .dwp for the binary file before any symbolic links
12600 resolving. */
6c447423
DE
12601
12602 /* If the objfile is a debug file, find the name of the real binary
12603 file and get the name of dwp file from there. */
d721ba37 12604 std::string dwp_name;
6c447423
DE
12605 if (objfile->separate_debug_objfile_backlink != NULL)
12606 {
12607 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12608 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12609
d721ba37 12610 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12611 }
12612 else
d721ba37
PA
12613 dwp_name = objfile->original_name;
12614
12615 dwp_name += ".dwp";
80626a55 12616
ed2dc618 12617 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12618 if (dbfd == NULL
12619 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12620 {
12621 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12622 dwp_name = objfile_name (objfile);
12623 dwp_name += ".dwp";
ed2dc618 12624 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12625 }
12626
80626a55
DE
12627 if (dbfd == NULL)
12628 {
b4f54984 12629 if (dwarf_read_debug)
d721ba37 12630 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12631 return std::unique_ptr<dwp_file> ();
3019eac3 12632 }
400174b1
TT
12633
12634 const char *name = bfd_get_filename (dbfd.get ());
12635 std::unique_ptr<struct dwp_file> dwp_file
12636 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12637
0a0f4c01 12638 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12639 dwp_file->elf_sections =
5989a64e 12640 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12641 dwp_file->num_sections, asection *);
12642
400174b1
TT
12643 bfd_map_over_sections (dwp_file->dbfd.get (),
12644 dwarf2_locate_common_dwp_sections,
12645 dwp_file.get ());
80626a55 12646
400174b1
TT
12647 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12648 0);
80626a55 12649
400174b1
TT
12650 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12651 1);
80626a55 12652
73869dc2 12653 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12654 if (dwp_file->cus && dwp_file->tus
12655 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12656 {
12657 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12658 pretty bizarre. We use pulongest here because that's the established
4d65956b 12659 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12660 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12661 " TU version %s [in DWP file %s]"),
12662 pulongest (dwp_file->cus->version),
d721ba37 12663 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12664 }
08302ed2
DE
12665
12666 if (dwp_file->cus)
12667 dwp_file->version = dwp_file->cus->version;
12668 else if (dwp_file->tus)
12669 dwp_file->version = dwp_file->tus->version;
12670 else
12671 dwp_file->version = 2;
73869dc2
DE
12672
12673 if (dwp_file->version == 2)
400174b1
TT
12674 bfd_map_over_sections (dwp_file->dbfd.get (),
12675 dwarf2_locate_v2_dwp_sections,
12676 dwp_file.get ());
73869dc2 12677
298e9637
SM
12678 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12679 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12680
b4f54984 12681 if (dwarf_read_debug)
80626a55
DE
12682 {
12683 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12684 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12685 " %s CUs, %s TUs\n",
12686 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12687 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12688 }
12689
12690 return dwp_file;
3019eac3 12691}
c906108c 12692
ab5088bf
DE
12693/* Wrapper around open_and_init_dwp_file, only open it once. */
12694
12695static struct dwp_file *
ed2dc618 12696get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12697{
5989a64e 12698 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12699 {
5989a64e 12700 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12701 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12702 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12703 }
5989a64e 12704 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12705}
12706
80626a55
DE
12707/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12708 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12709 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12710 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12711 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12712
12713 This is called, for example, when wanting to read a variable with a
12714 complex location. Therefore we don't want to do file i/o for every call.
12715 Therefore we don't want to look for a DWO file on every call.
12716 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12717 then we check if we've already seen DWO_NAME, and only THEN do we check
12718 for a DWO file.
12719
1c658ad5 12720 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12721 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12722
3019eac3 12723static struct dwo_unit *
4ab09049 12724lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12725 ULONGEST signature, int is_debug_types)
3019eac3 12726{
4ab09049 12727 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12728 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12729 const char *kind = is_debug_types ? "TU" : "CU";
12730 void **dwo_file_slot;
3019eac3 12731 struct dwo_file *dwo_file;
80626a55 12732 struct dwp_file *dwp_file;
cb1df416 12733
6a506a2d
DE
12734 /* First see if there's a DWP file.
12735 If we have a DWP file but didn't find the DWO inside it, don't
12736 look for the original DWO file. It makes gdb behave differently
12737 depending on whether one is debugging in the build tree. */
cf2c3c16 12738
ed2dc618 12739 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12740 if (dwp_file != NULL)
cf2c3c16 12741 {
80626a55
DE
12742 const struct dwp_hash_table *dwp_htab =
12743 is_debug_types ? dwp_file->tus : dwp_file->cus;
12744
12745 if (dwp_htab != NULL)
12746 {
12747 struct dwo_unit *dwo_cutu =
ed2dc618 12748 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12749 signature, is_debug_types);
80626a55
DE
12750
12751 if (dwo_cutu != NULL)
12752 {
b4f54984 12753 if (dwarf_read_debug)
80626a55
DE
12754 {
12755 fprintf_unfiltered (gdb_stdlog,
12756 "Virtual DWO %s %s found: @%s\n",
12757 kind, hex_string (signature),
12758 host_address_to_string (dwo_cutu));
12759 }
12760 return dwo_cutu;
12761 }
12762 }
12763 }
6a506a2d 12764 else
80626a55 12765 {
6a506a2d 12766 /* No DWP file, look for the DWO file. */
80626a55 12767
ed2dc618
SM
12768 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12769 dwo_name, comp_dir);
6a506a2d 12770 if (*dwo_file_slot == NULL)
80626a55 12771 {
6a506a2d 12772 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12773 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12774 }
6a506a2d 12775 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12776 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12777
6a506a2d 12778 if (dwo_file != NULL)
19c3d4c9 12779 {
6a506a2d
DE
12780 struct dwo_unit *dwo_cutu = NULL;
12781
12782 if (is_debug_types && dwo_file->tus)
12783 {
12784 struct dwo_unit find_dwo_cutu;
12785
12786 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12787 find_dwo_cutu.signature = signature;
9a3c8263 12788 dwo_cutu
b0b6a987
TT
12789 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12790 &find_dwo_cutu);
6a506a2d 12791 }
33c5cd75 12792 else if (!is_debug_types && dwo_file->cus)
80626a55 12793 {
33c5cd75
DB
12794 struct dwo_unit find_dwo_cutu;
12795
12796 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12797 find_dwo_cutu.signature = signature;
b0b6a987 12798 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12799 &find_dwo_cutu);
6a506a2d
DE
12800 }
12801
12802 if (dwo_cutu != NULL)
12803 {
b4f54984 12804 if (dwarf_read_debug)
6a506a2d
DE
12805 {
12806 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12807 kind, dwo_name, hex_string (signature),
12808 host_address_to_string (dwo_cutu));
12809 }
12810 return dwo_cutu;
80626a55
DE
12811 }
12812 }
2e276125 12813 }
9cdd5dbd 12814
80626a55
DE
12815 /* We didn't find it. This could mean a dwo_id mismatch, or
12816 someone deleted the DWO/DWP file, or the search path isn't set up
12817 correctly to find the file. */
12818
b4f54984 12819 if (dwarf_read_debug)
80626a55
DE
12820 {
12821 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12822 kind, dwo_name, hex_string (signature));
12823 }
3019eac3 12824
6656a72d
DE
12825 /* This is a warning and not a complaint because it can be caused by
12826 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12827 {
12828 /* Print the name of the DWP file if we looked there, helps the user
12829 better diagnose the problem. */
791afaa2 12830 std::string dwp_text;
43942612
DE
12831
12832 if (dwp_file != NULL)
791afaa2
TT
12833 dwp_text = string_printf (" [in DWP file %s]",
12834 lbasename (dwp_file->name));
43942612 12835
9d8780f0 12836 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12837 " [in module %s]"),
4ab09049
SM
12838 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12839 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12840 }
3019eac3 12841 return NULL;
5fb290d7
DJ
12842}
12843
80626a55
DE
12844/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12845 See lookup_dwo_cutu_unit for details. */
12846
12847static struct dwo_unit *
4ab09049 12848lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12849 ULONGEST signature)
12850{
4ab09049
SM
12851 gdb_assert (!cu->per_cu->is_debug_types);
12852
12853 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12854}
12855
12856/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12857 See lookup_dwo_cutu_unit for details. */
12858
12859static struct dwo_unit *
4ab09049 12860lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12861{
4ab09049
SM
12862 gdb_assert (cu->per_cu->is_debug_types);
12863
12864 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12865
12866 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12867}
12868
89e63ee4
DE
12869/* Traversal function for queue_and_load_all_dwo_tus. */
12870
12871static int
12872queue_and_load_dwo_tu (void **slot, void *info)
12873{
12874 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12875 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12876 ULONGEST signature = dwo_unit->signature;
d460f660 12877 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12878
12879 if (sig_type != NULL)
12880 {
12881 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12882
12883 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12884 a real dependency of PER_CU on SIG_TYPE. That is detected later
12885 while processing PER_CU. */
120ce1b5 12886 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12887 load_full_type_unit (sig_cu, cu->per_objfile);
12888 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12889 }
12890
12891 return 1;
12892}
12893
12894/* Queue all TUs contained in the DWO of PER_CU to be read in.
12895 The DWO may have the only definition of the type, though it may not be
12896 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12897 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12898
12899static void
12900queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12901{
12902 struct dwo_unit *dwo_unit;
12903 struct dwo_file *dwo_file;
12904
12905 gdb_assert (!per_cu->is_debug_types);
89e63ee4 12906 gdb_assert (per_cu->cu != NULL);
64874a40 12907 gdb_assert (get_dwp_file (per_cu->cu->per_objfile) == NULL);
89e63ee4
DE
12908
12909 dwo_unit = per_cu->cu->dwo_unit;
12910 gdb_assert (dwo_unit != NULL);
12911
12912 dwo_file = dwo_unit->dwo_file;
12913 if (dwo_file->tus != NULL)
b0b6a987 12914 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
d460f660 12915 per_cu->cu);
89e63ee4
DE
12916}
12917
3019eac3 12918/* Read in various DIEs. */
348e048f 12919
d389af10 12920/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12921 Inherit only the children of the DW_AT_abstract_origin DIE not being
12922 already referenced by DW_AT_abstract_origin from the children of the
12923 current DIE. */
d389af10
JK
12924
12925static void
12926inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12927{
12928 struct die_info *child_die;
791afaa2 12929 sect_offset *offsetp;
d389af10
JK
12930 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12931 struct die_info *origin_die;
12932 /* Iterator of the ORIGIN_DIE children. */
12933 struct die_info *origin_child_die;
d389af10 12934 struct attribute *attr;
cd02d79d
PA
12935 struct dwarf2_cu *origin_cu;
12936 struct pending **origin_previous_list_in_scope;
d389af10
JK
12937
12938 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12939 if (!attr)
12940 return;
12941
cd02d79d
PA
12942 /* Note that following die references may follow to a die in a
12943 different cu. */
12944
12945 origin_cu = cu;
12946 origin_die = follow_die_ref (die, attr, &origin_cu);
12947
12948 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12949 symbols in. */
12950 origin_previous_list_in_scope = origin_cu->list_in_scope;
12951 origin_cu->list_in_scope = cu->list_in_scope;
12952
edb3359d
DJ
12953 if (die->tag != origin_die->tag
12954 && !(die->tag == DW_TAG_inlined_subroutine
12955 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12956 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12957 sect_offset_str (die->sect_off),
12958 sect_offset_str (origin_die->sect_off));
d389af10 12959
791afaa2 12960 std::vector<sect_offset> offsets;
d389af10 12961
3ea89b92
PMR
12962 for (child_die = die->child;
12963 child_die && child_die->tag;
436c571c 12964 child_die = child_die->sibling)
3ea89b92
PMR
12965 {
12966 struct die_info *child_origin_die;
12967 struct dwarf2_cu *child_origin_cu;
12968
12969 /* We are trying to process concrete instance entries:
216f72a1 12970 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12971 it's not relevant to our analysis here. i.e. detecting DIEs that are
12972 present in the abstract instance but not referenced in the concrete
12973 one. */
216f72a1
JK
12974 if (child_die->tag == DW_TAG_call_site
12975 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12976 continue;
12977
c38f313d
DJ
12978 /* For each CHILD_DIE, find the corresponding child of
12979 ORIGIN_DIE. If there is more than one layer of
12980 DW_AT_abstract_origin, follow them all; there shouldn't be,
12981 but GCC versions at least through 4.4 generate this (GCC PR
12982 40573). */
3ea89b92
PMR
12983 child_origin_die = child_die;
12984 child_origin_cu = cu;
c38f313d
DJ
12985 while (1)
12986 {
cd02d79d
PA
12987 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12988 child_origin_cu);
c38f313d
DJ
12989 if (attr == NULL)
12990 break;
cd02d79d
PA
12991 child_origin_die = follow_die_ref (child_origin_die, attr,
12992 &child_origin_cu);
c38f313d
DJ
12993 }
12994
d389af10
JK
12995 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12996 counterpart may exist. */
c38f313d 12997 if (child_origin_die != child_die)
d389af10 12998 {
edb3359d
DJ
12999 if (child_die->tag != child_origin_die->tag
13000 && !(child_die->tag == DW_TAG_inlined_subroutine
13001 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13002 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13003 "different tags"),
9d8780f0
SM
13004 sect_offset_str (child_die->sect_off),
13005 sect_offset_str (child_origin_die->sect_off));
c38f313d 13006 if (child_origin_die->parent != origin_die)
b98664d3 13007 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13008 "different parents"),
9d8780f0
SM
13009 sect_offset_str (child_die->sect_off),
13010 sect_offset_str (child_origin_die->sect_off));
c38f313d 13011 else
791afaa2 13012 offsets.push_back (child_origin_die->sect_off);
d389af10 13013 }
d389af10 13014 }
791afaa2
TT
13015 std::sort (offsets.begin (), offsets.end ());
13016 sect_offset *offsets_end = offsets.data () + offsets.size ();
13017 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13018 if (offsetp[-1] == *offsetp)
b98664d3 13019 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13020 "to DIE %s as their abstract origin"),
13021 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13022
791afaa2 13023 offsetp = offsets.data ();
d389af10
JK
13024 origin_child_die = origin_die->child;
13025 while (origin_child_die && origin_child_die->tag)
13026 {
13027 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13028 while (offsetp < offsets_end
9c541725 13029 && *offsetp < origin_child_die->sect_off)
d389af10 13030 offsetp++;
b64f50a1 13031 if (offsetp >= offsets_end
9c541725 13032 || *offsetp > origin_child_die->sect_off)
d389af10 13033 {
adde2bff
DE
13034 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13035 Check whether we're already processing ORIGIN_CHILD_DIE.
13036 This can happen with mutually referenced abstract_origins.
13037 PR 16581. */
13038 if (!origin_child_die->in_process)
13039 process_die (origin_child_die, origin_cu);
d389af10 13040 }
436c571c 13041 origin_child_die = origin_child_die->sibling;
d389af10 13042 }
cd02d79d 13043 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13044
13045 if (cu != origin_cu)
13046 compute_delayed_physnames (origin_cu);
d389af10
JK
13047}
13048
c906108c 13049static void
e7c27a73 13050read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13051{
5e22e966 13052 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13053 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13054 struct context_stack *newobj;
c906108c
SS
13055 CORE_ADDR lowpc;
13056 CORE_ADDR highpc;
13057 struct die_info *child_die;
edb3359d 13058 struct attribute *attr, *call_line, *call_file;
15d034d0 13059 const char *name;
e142c38c 13060 CORE_ADDR baseaddr;
801e3a5b 13061 struct block *block;
edb3359d 13062 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13063 std::vector<struct symbol *> template_args;
34eaf542 13064 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13065
13066 if (inlined_func)
13067 {
13068 /* If we do not have call site information, we can't show the
13069 caller of this inlined function. That's too confusing, so
13070 only use the scope for local variables. */
13071 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13072 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13073 if (call_line == NULL || call_file == NULL)
13074 {
13075 read_lexical_block_scope (die, cu);
13076 return;
13077 }
13078 }
c906108c 13079
b3b3bada 13080 baseaddr = objfile->text_section_offset ();
e142c38c 13081
94af9270 13082 name = dwarf2_name (die, cu);
c906108c 13083
e8d05480
JB
13084 /* Ignore functions with missing or empty names. These are actually
13085 illegal according to the DWARF standard. */
13086 if (name == NULL)
13087 {
b98664d3 13088 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13089 sect_offset_str (die->sect_off));
e8d05480
JB
13090 return;
13091 }
13092
13093 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13094 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13095 <= PC_BOUNDS_INVALID)
e8d05480 13096 {
ae4d0c03
PM
13097 attr = dwarf2_attr (die, DW_AT_external, cu);
13098 if (!attr || !DW_UNSND (attr))
b98664d3 13099 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13100 "for subprogram DIE at %s"),
13101 sect_offset_str (die->sect_off));
e8d05480
JB
13102 return;
13103 }
c906108c 13104
3e29f34a
MR
13105 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13106 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13107
34eaf542
TT
13108 /* If we have any template arguments, then we must allocate a
13109 different sort of symbol. */
436c571c 13110 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13111 {
13112 if (child_die->tag == DW_TAG_template_type_param
13113 || child_die->tag == DW_TAG_template_value_param)
13114 {
8c14c3a3 13115 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13116 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13117 break;
13118 }
13119 }
13120
c24bdb02 13121 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13122 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13123 (struct symbol *) templ_func);
4c2df51b 13124
81873cc8 13125 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13126 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13127 cu->language);
13128
4cecd739
DJ
13129 /* If there is a location expression for DW_AT_frame_base, record
13130 it. */
e142c38c 13131 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13132 if (attr != nullptr)
fe978cb0 13133 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13134
63e43d3a
PMR
13135 /* If there is a location for the static link, record it. */
13136 newobj->static_link = NULL;
13137 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13138 if (attr != nullptr)
63e43d3a 13139 {
224c3ddb
SM
13140 newobj->static_link
13141 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13142 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13143 cu->addr_type ());
63e43d3a
PMR
13144 }
13145
c24bdb02 13146 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13147
639d11d3 13148 if (die->child != NULL)
c906108c 13149 {
639d11d3 13150 child_die = die->child;
c906108c
SS
13151 while (child_die && child_die->tag)
13152 {
34eaf542
TT
13153 if (child_die->tag == DW_TAG_template_type_param
13154 || child_die->tag == DW_TAG_template_value_param)
13155 {
13156 struct symbol *arg = new_symbol (child_die, NULL, cu);
13157
f1078f66 13158 if (arg != NULL)
2f4732b0 13159 template_args.push_back (arg);
34eaf542
TT
13160 }
13161 else
13162 process_die (child_die, cu);
436c571c 13163 child_die = child_die->sibling;
c906108c
SS
13164 }
13165 }
13166
d389af10
JK
13167 inherit_abstract_dies (die, cu);
13168
4a811a97
UW
13169 /* If we have a DW_AT_specification, we might need to import using
13170 directives from the context of the specification DIE. See the
13171 comment in determine_prefix. */
13172 if (cu->language == language_cplus
13173 && dwarf2_attr (die, DW_AT_specification, cu))
13174 {
13175 struct dwarf2_cu *spec_cu = cu;
13176 struct die_info *spec_die = die_specification (die, &spec_cu);
13177
13178 while (spec_die)
13179 {
13180 child_die = spec_die->child;
13181 while (child_die && child_die->tag)
13182 {
13183 if (child_die->tag == DW_TAG_imported_module)
13184 process_die (child_die, spec_cu);
436c571c 13185 child_die = child_die->sibling;
4a811a97
UW
13186 }
13187
13188 /* In some cases, GCC generates specification DIEs that
13189 themselves contain DW_AT_specification attributes. */
13190 spec_die = die_specification (spec_die, &spec_cu);
13191 }
13192 }
13193
c24bdb02 13194 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13195 /* Make a block for the local symbols within. */
c24bdb02 13196 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13197 cstk.static_link, lowpc, highpc);
801e3a5b 13198
df8a16a1 13199 /* For C++, set the block's scope. */
45280282
IB
13200 if ((cu->language == language_cplus
13201 || cu->language == language_fortran
c44af4eb
TT
13202 || cu->language == language_d
13203 || cu->language == language_rust)
4d4ec4e5 13204 && cu->processing_has_namespace_info)
195a3f6c
TT
13205 block_set_scope (block, determine_prefix (die, cu),
13206 &objfile->objfile_obstack);
df8a16a1 13207
801e3a5b
JB
13208 /* If we have address ranges, record them. */
13209 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13210
a60f3166 13211 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13212
34eaf542 13213 /* Attach template arguments to function. */
2f4732b0 13214 if (!template_args.empty ())
34eaf542
TT
13215 {
13216 gdb_assert (templ_func != NULL);
13217
2f4732b0 13218 templ_func->n_template_arguments = template_args.size ();
34eaf542 13219 templ_func->template_arguments
8d749320
SM
13220 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13221 templ_func->n_template_arguments);
34eaf542 13222 memcpy (templ_func->template_arguments,
2f4732b0 13223 template_args.data (),
34eaf542 13224 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13225
13226 /* Make sure that the symtab is set on the new symbols. Even
13227 though they don't appear in this symtab directly, other parts
13228 of gdb assume that symbols do, and this is reasonably
13229 true. */
8634679f 13230 for (symbol *sym : template_args)
3e1d3d8c 13231 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13232 }
13233
208d8187
JB
13234 /* In C++, we can have functions nested inside functions (e.g., when
13235 a function declares a class that has methods). This means that
13236 when we finish processing a function scope, we may need to go
13237 back to building a containing block's symbol lists. */
c24bdb02
KS
13238 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13239 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13240
921e78cf
JB
13241 /* If we've finished processing a top-level function, subsequent
13242 symbols go in the file symbol list. */
c24bdb02
KS
13243 if (cu->get_builder ()->outermost_context_p ())
13244 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13245}
13246
13247/* Process all the DIES contained within a lexical block scope. Start
13248 a new scope, process the dies, and then close the scope. */
13249
13250static void
e7c27a73 13251read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13252{
5e22e966 13253 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13254 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13255 CORE_ADDR lowpc, highpc;
13256 struct die_info *child_die;
e142c38c
DJ
13257 CORE_ADDR baseaddr;
13258
b3b3bada 13259 baseaddr = objfile->text_section_offset ();
c906108c
SS
13260
13261 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13262 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13263 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13264 be nasty. Might be easier to properly extend generic blocks to
af34e669 13265 describe ranges. */
e385593e
JK
13266 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13267 {
13268 case PC_BOUNDS_NOT_PRESENT:
13269 /* DW_TAG_lexical_block has no attributes, process its children as if
13270 there was no wrapping by that DW_TAG_lexical_block.
13271 GCC does no longer produces such DWARF since GCC r224161. */
13272 for (child_die = die->child;
13273 child_die != NULL && child_die->tag;
436c571c 13274 child_die = child_die->sibling)
4f7bc5ed
TT
13275 {
13276 /* We might already be processing this DIE. This can happen
13277 in an unusual circumstance -- where a subroutine A
13278 appears lexically in another subroutine B, but A actually
13279 inlines B. The recursion is broken here, rather than in
13280 inherit_abstract_dies, because it seems better to simply
13281 drop concrete children here. */
13282 if (!child_die->in_process)
13283 process_die (child_die, cu);
13284 }
e385593e
JK
13285 return;
13286 case PC_BOUNDS_INVALID:
13287 return;
13288 }
3e29f34a
MR
13289 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13290 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13291
c24bdb02 13292 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13293 if (die->child != NULL)
c906108c 13294 {
639d11d3 13295 child_die = die->child;
c906108c
SS
13296 while (child_die && child_die->tag)
13297 {
e7c27a73 13298 process_die (child_die, cu);
436c571c 13299 child_die = child_die->sibling;
c906108c
SS
13300 }
13301 }
3ea89b92 13302 inherit_abstract_dies (die, cu);
c24bdb02 13303 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13304
c24bdb02
KS
13305 if (*cu->get_builder ()->get_local_symbols () != NULL
13306 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13307 {
801e3a5b 13308 struct block *block
c24bdb02 13309 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13310 cstk.start_addr, highpc);
801e3a5b
JB
13311
13312 /* Note that recording ranges after traversing children, as we
13313 do here, means that recording a parent's ranges entails
13314 walking across all its children's ranges as they appear in
13315 the address map, which is quadratic behavior.
13316
13317 It would be nicer to record the parent's ranges before
13318 traversing its children, simply overriding whatever you find
13319 there. But since we don't even decide whether to create a
13320 block until after we've traversed its children, that's hard
13321 to do. */
13322 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13323 }
c24bdb02
KS
13324 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13325 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13326}
13327
216f72a1 13328/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13329
13330static void
13331read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13332{
5e22e966 13333 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13334 struct objfile *objfile = per_objfile->objfile;
08feed99 13335 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13336 CORE_ADDR pc, baseaddr;
13337 struct attribute *attr;
13338 struct call_site *call_site, call_site_local;
13339 void **slot;
13340 int nparams;
13341 struct die_info *child_die;
13342
b3b3bada 13343 baseaddr = objfile->text_section_offset ();
96408a79 13344
216f72a1
JK
13345 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13346 if (attr == NULL)
13347 {
13348 /* This was a pre-DWARF-5 GNU extension alias
13349 for DW_AT_call_return_pc. */
13350 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13351 }
96408a79
SA
13352 if (!attr)
13353 {
b98664d3 13354 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13355 "DIE %s [in module %s]"),
13356 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13357 return;
13358 }
cd6c91b4 13359 pc = attr->value_as_address () + baseaddr;
3e29f34a 13360 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13361
13362 if (cu->call_site_htab == NULL)
13363 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13364 NULL, &objfile->objfile_obstack,
13365 hashtab_obstack_allocate, NULL);
13366 call_site_local.pc = pc;
13367 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13368 if (*slot != NULL)
13369 {
b98664d3 13370 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13371 "DIE %s [in module %s]"),
13372 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13373 objfile_name (objfile));
96408a79
SA
13374 return;
13375 }
13376
13377 /* Count parameters at the caller. */
13378
13379 nparams = 0;
13380 for (child_die = die->child; child_die && child_die->tag;
436c571c 13381 child_die = child_die->sibling)
96408a79 13382 {
216f72a1
JK
13383 if (child_die->tag != DW_TAG_call_site_parameter
13384 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13385 {
b98664d3 13386 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13387 "DW_TAG_call_site child DIE %s [in module %s]"),
13388 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13389 objfile_name (objfile));
96408a79
SA
13390 continue;
13391 }
13392
13393 nparams++;
13394 }
13395
224c3ddb
SM
13396 call_site
13397 = ((struct call_site *)
13398 obstack_alloc (&objfile->objfile_obstack,
13399 sizeof (*call_site)
13400 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13401 *slot = call_site;
13402 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13403 call_site->pc = pc;
13404
216f72a1
JK
13405 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13406 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13407 {
13408 struct die_info *func_die;
13409
13410 /* Skip also over DW_TAG_inlined_subroutine. */
13411 for (func_die = die->parent;
13412 func_die && func_die->tag != DW_TAG_subprogram
13413 && func_die->tag != DW_TAG_subroutine_type;
13414 func_die = func_die->parent);
13415
216f72a1
JK
13416 /* DW_AT_call_all_calls is a superset
13417 of DW_AT_call_all_tail_calls. */
96408a79 13418 if (func_die
216f72a1 13419 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13420 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13421 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13422 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13423 {
13424 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13425 not complete. But keep CALL_SITE for look ups via call_site_htab,
13426 both the initial caller containing the real return address PC and
13427 the final callee containing the current PC of a chain of tail
13428 calls do not need to have the tail call list complete. But any
13429 function candidate for a virtual tail call frame searched via
13430 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13431 determined unambiguously. */
13432 }
13433 else
13434 {
13435 struct type *func_type = NULL;
13436
13437 if (func_die)
13438 func_type = get_die_type (func_die, cu);
13439 if (func_type != NULL)
13440 {
78134374 13441 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13442
13443 /* Enlist this call site to the function. */
13444 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13445 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13446 }
13447 else
b98664d3 13448 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13449 "DIE %s [in module %s]"),
13450 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13451 }
13452 }
13453
216f72a1
JK
13454 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13455 if (attr == NULL)
13456 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13457 if (attr == NULL)
13458 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13459 if (attr == NULL)
216f72a1
JK
13460 {
13461 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13462 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13463 }
96408a79 13464 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13465 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13466 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13467 else if (attr->form_is_block ())
96408a79
SA
13468 {
13469 struct dwarf2_locexpr_baton *dlbaton;
13470
8d749320 13471 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13472 dlbaton->data = DW_BLOCK (attr)->data;
13473 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13474 dlbaton->per_objfile = per_objfile;
96408a79
SA
13475 dlbaton->per_cu = cu->per_cu;
13476
13477 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13478 }
cd6c91b4 13479 else if (attr->form_is_ref ())
96408a79 13480 {
96408a79
SA
13481 struct dwarf2_cu *target_cu = cu;
13482 struct die_info *target_die;
13483
ac9ec31b 13484 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13485 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13486 if (die_is_declaration (target_die, target_cu))
13487 {
7d45c7c3 13488 const char *target_physname;
9112db09
JK
13489
13490 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13491 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13492 if (target_physname == NULL)
9112db09 13493 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13494 if (target_physname == NULL)
b98664d3 13495 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13496 "physname, for referencing DIE %s [in module %s]"),
13497 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13498 else
7d455152 13499 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13500 }
13501 else
13502 {
13503 CORE_ADDR lowpc;
13504
13505 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13506 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13507 <= PC_BOUNDS_INVALID)
b98664d3 13508 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13509 "low pc, for referencing DIE %s [in module %s]"),
13510 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13511 else
3e29f34a
MR
13512 {
13513 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13514 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13515 }
96408a79
SA
13516 }
13517 }
13518 else
b98664d3 13519 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13520 "block nor reference, for DIE %s [in module %s]"),
13521 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13522
13523 call_site->per_cu = cu->per_cu;
9f47c707 13524 call_site->per_objfile = per_objfile;
96408a79
SA
13525
13526 for (child_die = die->child;
13527 child_die && child_die->tag;
436c571c 13528 child_die = child_die->sibling)
96408a79 13529 {
96408a79 13530 struct call_site_parameter *parameter;
1788b2d3 13531 struct attribute *loc, *origin;
96408a79 13532
216f72a1
JK
13533 if (child_die->tag != DW_TAG_call_site_parameter
13534 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13535 {
13536 /* Already printed the complaint above. */
13537 continue;
13538 }
13539
13540 gdb_assert (call_site->parameter_count < nparams);
13541 parameter = &call_site->parameter[call_site->parameter_count];
13542
1788b2d3
JK
13543 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13544 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13545 register is contained in DW_AT_call_value. */
96408a79 13546
24c5c679 13547 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13548 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13549 if (origin == NULL)
13550 {
13551 /* This was a pre-DWARF-5 GNU extension alias
13552 for DW_AT_call_parameter. */
13553 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13554 }
cd6c91b4 13555 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13556 {
1788b2d3 13557 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13558
0826b30a 13559 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13560 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13561 {
13562 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13563 binding can be done only inside one CU. Such referenced DIE
13564 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13565 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13566 "DW_TAG_call_site child DIE %s [in module %s]"),
13567 sect_offset_str (child_die->sect_off),
9c541725 13568 objfile_name (objfile));
d76b7dbc
JK
13569 continue;
13570 }
9c541725
PA
13571 parameter->u.param_cu_off
13572 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13573 }
4fc6c0d5 13574 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13575 {
b98664d3 13576 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13577 "DW_TAG_call_site child DIE %s [in module %s]"),
13578 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13579 continue;
13580 }
24c5c679 13581 else
96408a79 13582 {
24c5c679
JK
13583 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13584 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13585 if (parameter->u.dwarf_reg != -1)
13586 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13587 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13588 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13589 &parameter->u.fb_offset))
13590 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13591 else
13592 {
b98664d3 13593 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13594 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13595 "DW_TAG_call_site child DIE %s "
24c5c679 13596 "[in module %s]"),
9d8780f0 13597 sect_offset_str (child_die->sect_off),
9c541725 13598 objfile_name (objfile));
24c5c679
JK
13599 continue;
13600 }
96408a79
SA
13601 }
13602
216f72a1
JK
13603 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13604 if (attr == NULL)
13605 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13606 if (attr == NULL || !attr->form_is_block ())
96408a79 13607 {
b98664d3 13608 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13609 "DW_TAG_call_site child DIE %s [in module %s]"),
13610 sect_offset_str (child_die->sect_off),
9c541725 13611 objfile_name (objfile));
96408a79
SA
13612 continue;
13613 }
13614 parameter->value = DW_BLOCK (attr)->data;
13615 parameter->value_size = DW_BLOCK (attr)->size;
13616
13617 /* Parameters are not pre-cleared by memset above. */
13618 parameter->data_value = NULL;
13619 parameter->data_value_size = 0;
13620 call_site->parameter_count++;
13621
216f72a1
JK
13622 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13623 if (attr == NULL)
13624 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13625 if (attr != nullptr)
96408a79 13626 {
4fc6c0d5 13627 if (!attr->form_is_block ())
b98664d3 13628 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13629 "DW_TAG_call_site child DIE %s [in module %s]"),
13630 sect_offset_str (child_die->sect_off),
9c541725 13631 objfile_name (objfile));
96408a79
SA
13632 else
13633 {
13634 parameter->data_value = DW_BLOCK (attr)->data;
13635 parameter->data_value_size = DW_BLOCK (attr)->size;
13636 }
13637 }
13638 }
13639}
13640
71a3c369
TT
13641/* Helper function for read_variable. If DIE represents a virtual
13642 table, then return the type of the concrete object that is
13643 associated with the virtual table. Otherwise, return NULL. */
13644
13645static struct type *
13646rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13647{
13648 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13649 if (attr == NULL)
13650 return NULL;
13651
13652 /* Find the type DIE. */
13653 struct die_info *type_die = NULL;
13654 struct dwarf2_cu *type_cu = cu;
13655
cd6c91b4 13656 if (attr->form_is_ref ())
71a3c369
TT
13657 type_die = follow_die_ref (die, attr, &type_cu);
13658 if (type_die == NULL)
13659 return NULL;
13660
13661 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13662 return NULL;
13663 return die_containing_type (type_die, type_cu);
13664}
13665
13666/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13667
13668static void
13669read_variable (struct die_info *die, struct dwarf2_cu *cu)
13670{
13671 struct rust_vtable_symbol *storage = NULL;
13672
13673 if (cu->language == language_rust)
13674 {
13675 struct type *containing_type = rust_containing_type (die, cu);
13676
13677 if (containing_type != NULL)
13678 {
5e22e966 13679 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13680
8c14c3a3 13681 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13682 storage->concrete_type = containing_type;
cf724bc9 13683 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13684 }
13685 }
13686
e4a62c65
TV
13687 struct symbol *res = new_symbol (die, NULL, cu, storage);
13688 struct attribute *abstract_origin
13689 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13690 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13691 if (res == NULL && loc && abstract_origin)
13692 {
13693 /* We have a variable without a name, but with a location and an abstract
13694 origin. This may be a concrete instance of an abstract variable
13695 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13696 later. */
13697 struct dwarf2_cu *origin_cu = cu;
13698 struct die_info *origin_die
13699 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13701 per_objfile->per_bfd->abstract_to_concrete
13702 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13703 }
71a3c369
TT
13704}
13705
43988095
JK
13706/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13707 reading .debug_rnglists.
13708 Callback's type should be:
13709 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13710 Return true if the attributes are present and valid, otherwise,
13711 return false. */
13712
13713template <typename Callback>
13714static bool
13715dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13716 Callback &&callback)
13717{
5e22e966 13718 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13719 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13720 bfd *obfd = objfile->obfd;
43988095 13721 /* Base address selection entry. */
2b24b6e4 13722 gdb::optional<CORE_ADDR> base;
43988095 13723 const gdb_byte *buffer;
43988095
JK
13724 CORE_ADDR baseaddr;
13725 bool overflow = false;
13726
43988095
JK
13727 base = cu->base_address;
13728
5989a64e
SM
13729 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13730 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13731 {
b98664d3 13732 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13733 offset);
13734 return false;
13735 }
5989a64e 13736 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13737
b3b3bada 13738 baseaddr = objfile->text_section_offset ();
43988095
JK
13739
13740 while (1)
13741 {
7814882a
JK
13742 /* Initialize it due to a false compiler warning. */
13743 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13744 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13745 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13746 unsigned int bytes_read;
13747
13748 if (buffer == buf_end)
13749 {
13750 overflow = true;
13751 break;
13752 }
13753 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13754 switch (rlet)
13755 {
13756 case DW_RLE_end_of_list:
13757 break;
13758 case DW_RLE_base_address:
13759 if (buffer + cu->header.addr_size > buf_end)
13760 {
13761 overflow = true;
13762 break;
13763 }
c8a7a66f 13764 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13765 buffer += bytes_read;
13766 break;
13767 case DW_RLE_start_length:
13768 if (buffer + cu->header.addr_size > buf_end)
13769 {
13770 overflow = true;
13771 break;
13772 }
c8a7a66f
TT
13773 range_beginning = cu->header.read_address (obfd, buffer,
13774 &bytes_read);
43988095
JK
13775 buffer += bytes_read;
13776 range_end = (range_beginning
13777 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13778 buffer += bytes_read;
13779 if (buffer > buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
13784 break;
13785 case DW_RLE_offset_pair:
13786 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13787 buffer += bytes_read;
13788 if (buffer > buf_end)
13789 {
13790 overflow = true;
13791 break;
13792 }
13793 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13794 buffer += bytes_read;
13795 if (buffer > buf_end)
13796 {
13797 overflow = true;
13798 break;
13799 }
13800 break;
13801 case DW_RLE_start_end:
13802 if (buffer + 2 * cu->header.addr_size > buf_end)
13803 {
13804 overflow = true;
13805 break;
13806 }
c8a7a66f
TT
13807 range_beginning = cu->header.read_address (obfd, buffer,
13808 &bytes_read);
43988095 13809 buffer += bytes_read;
c8a7a66f 13810 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13811 buffer += bytes_read;
13812 break;
13813 default:
b98664d3 13814 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13815 return false;
13816 }
13817 if (rlet == DW_RLE_end_of_list || overflow)
13818 break;
13819 if (rlet == DW_RLE_base_address)
13820 continue;
13821
2b24b6e4 13822 if (!base.has_value ())
43988095
JK
13823 {
13824 /* We have no valid base address for the ranges
13825 data. */
b98664d3 13826 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13827 return false;
13828 }
13829
13830 if (range_beginning > range_end)
13831 {
13832 /* Inverted range entries are invalid. */
b98664d3 13833 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13834 return false;
13835 }
13836
13837 /* Empty range entries have no effect. */
13838 if (range_beginning == range_end)
13839 continue;
13840
2b24b6e4
TT
13841 range_beginning += *base;
13842 range_end += *base;
43988095
JK
13843
13844 /* A not-uncommon case of bad debug info.
13845 Don't pollute the addrmap with bad data. */
13846 if (range_beginning + baseaddr == 0
5989a64e 13847 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13848 {
b98664d3 13849 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13850 " [in module %s]"), objfile_name (objfile));
13851 continue;
13852 }
13853
13854 callback (range_beginning, range_end);
13855 }
13856
13857 if (overflow)
13858 {
b98664d3 13859 complaint (_("Offset %d is not terminated "
43988095
JK
13860 "for DW_AT_ranges attribute"),
13861 offset);
13862 return false;
13863 }
13864
13865 return true;
13866}
13867
13868/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13869 Callback's type should be:
13870 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13871 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13872
43988095 13873template <typename Callback>
43039443 13874static int
5f46c5a5 13875dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13876 Callback &&callback)
43039443 13877{
5e22e966
SM
13878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13879 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13880 struct comp_unit_head *cu_header = &cu->header;
13881 bfd *obfd = objfile->obfd;
13882 unsigned int addr_size = cu_header->addr_size;
13883 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13884 /* Base address selection entry. */
2b24b6e4 13885 gdb::optional<CORE_ADDR> base;
43039443 13886 unsigned int dummy;
d521ce57 13887 const gdb_byte *buffer;
ff013f42 13888 CORE_ADDR baseaddr;
43039443 13889
43988095
JK
13890 if (cu_header->version >= 5)
13891 return dwarf2_rnglists_process (offset, cu, callback);
13892
d00adf39 13893 base = cu->base_address;
43039443 13894
5e22e966
SM
13895 per_objfile->per_bfd->ranges.read (objfile);
13896 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13897 {
b98664d3 13898 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13899 offset);
13900 return 0;
13901 }
5e22e966 13902 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13903
b3b3bada 13904 baseaddr = objfile->text_section_offset ();
ff013f42 13905
43039443
JK
13906 while (1)
13907 {
13908 CORE_ADDR range_beginning, range_end;
13909
c8a7a66f 13910 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13911 buffer += addr_size;
c8a7a66f 13912 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13913 buffer += addr_size;
13914 offset += 2 * addr_size;
13915
13916 /* An end of list marker is a pair of zero addresses. */
13917 if (range_beginning == 0 && range_end == 0)
13918 /* Found the end of list entry. */
13919 break;
13920
13921 /* Each base address selection entry is a pair of 2 values.
13922 The first is the largest possible address, the second is
13923 the base address. Check for a base address here. */
13924 if ((range_beginning & mask) == mask)
13925 {
28d2bfb9
AB
13926 /* If we found the largest possible address, then we already
13927 have the base address in range_end. */
13928 base = range_end;
43039443
JK
13929 continue;
13930 }
13931
2b24b6e4 13932 if (!base.has_value ())
43039443
JK
13933 {
13934 /* We have no valid base address for the ranges
13935 data. */
b98664d3 13936 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13937 return 0;
13938 }
13939
9277c30c
UW
13940 if (range_beginning > range_end)
13941 {
13942 /* Inverted range entries are invalid. */
b98664d3 13943 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13944 return 0;
13945 }
13946
13947 /* Empty range entries have no effect. */
13948 if (range_beginning == range_end)
13949 continue;
13950
2b24b6e4
TT
13951 range_beginning += *base;
13952 range_end += *base;
43039443 13953
01093045
DE
13954 /* A not-uncommon case of bad debug info.
13955 Don't pollute the addrmap with bad data. */
13956 if (range_beginning + baseaddr == 0
5e22e966 13957 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13958 {
b98664d3 13959 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13960 " [in module %s]"), objfile_name (objfile));
01093045
DE
13961 continue;
13962 }
13963
5f46c5a5
JK
13964 callback (range_beginning, range_end);
13965 }
13966
13967 return 1;
13968}
13969
13970/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13971 Return 1 if the attributes are present and valid, otherwise, return 0.
13972 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13973
13974static int
13975dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13976 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13977 dwarf2_psymtab *ranges_pst)
5f46c5a5 13978{
5e22e966 13979 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13980 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13981 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13982 int low_set = 0;
13983 CORE_ADDR low = 0;
13984 CORE_ADDR high = 0;
13985 int retval;
13986
13987 retval = dwarf2_ranges_process (offset, cu,
13988 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13989 {
9277c30c 13990 if (ranges_pst != NULL)
3e29f34a
MR
13991 {
13992 CORE_ADDR lowpc;
13993 CORE_ADDR highpc;
13994
79748972
TT
13995 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13996 range_beginning + baseaddr)
13997 - baseaddr);
13998 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13999 range_end + baseaddr)
14000 - baseaddr);
d320c2b5
TT
14001 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14002 lowpc, highpc - 1, ranges_pst);
3e29f34a 14003 }
ff013f42 14004
43039443
JK
14005 /* FIXME: This is recording everything as a low-high
14006 segment of consecutive addresses. We should have a
14007 data structure for discontiguous block ranges
14008 instead. */
14009 if (! low_set)
14010 {
14011 low = range_beginning;
14012 high = range_end;
14013 low_set = 1;
14014 }
14015 else
14016 {
14017 if (range_beginning < low)
14018 low = range_beginning;
14019 if (range_end > high)
14020 high = range_end;
14021 }
5f46c5a5
JK
14022 });
14023 if (!retval)
14024 return 0;
43039443
JK
14025
14026 if (! low_set)
14027 /* If the first entry is an end-of-list marker, the range
14028 describes an empty scope, i.e. no instructions. */
14029 return 0;
14030
14031 if (low_return)
14032 *low_return = low;
14033 if (high_return)
14034 *high_return = high;
14035 return 1;
14036}
14037
3a2b436a
JK
14038/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14039 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14040 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14041
3a2b436a 14042static enum pc_bounds_kind
af34e669 14043dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14044 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14045 dwarf2_psymtab *pst)
c906108c 14046{
5e22e966 14047 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14048 struct attribute *attr;
91da1414 14049 struct attribute *attr_high;
af34e669
DJ
14050 CORE_ADDR low = 0;
14051 CORE_ADDR high = 0;
e385593e 14052 enum pc_bounds_kind ret;
c906108c 14053
91da1414
MW
14054 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14055 if (attr_high)
af34e669 14056 {
e142c38c 14057 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14058 if (attr != nullptr)
91da1414 14059 {
cd6c91b4
TT
14060 low = attr->value_as_address ();
14061 high = attr_high->value_as_address ();
14062 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14063 high += low;
91da1414 14064 }
af34e669
DJ
14065 else
14066 /* Found high w/o low attribute. */
e385593e 14067 return PC_BOUNDS_INVALID;
af34e669
DJ
14068
14069 /* Found consecutive range of addresses. */
3a2b436a 14070 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14071 }
c906108c 14072 else
af34e669 14073 {
e142c38c 14074 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14075 if (attr != NULL)
14076 {
18a8505e 14077 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14078 We take advantage of the fact that DW_AT_ranges does not appear
14079 in DW_TAG_compile_unit of DWO files. */
14080 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14081 unsigned int ranges_offset = (DW_UNSND (attr)
14082 + (need_ranges_base
14083 ? cu->ranges_base
14084 : 0));
2e3cf129 14085
af34e669 14086 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14087 .debug_ranges section. */
2e3cf129 14088 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14089 return PC_BOUNDS_INVALID;
43039443 14090 /* Found discontinuous range of addresses. */
3a2b436a 14091 ret = PC_BOUNDS_RANGES;
af34e669 14092 }
e385593e
JK
14093 else
14094 return PC_BOUNDS_NOT_PRESENT;
af34e669 14095 }
c906108c 14096
48fbe735 14097 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14098 if (high <= low)
e385593e 14099 return PC_BOUNDS_INVALID;
c906108c
SS
14100
14101 /* When using the GNU linker, .gnu.linkonce. sections are used to
14102 eliminate duplicate copies of functions and vtables and such.
14103 The linker will arbitrarily choose one and discard the others.
14104 The AT_*_pc values for such functions refer to local labels in
14105 these sections. If the section from that file was discarded, the
14106 labels are not in the output, so the relocs get a value of 0.
14107 If this is a discarded function, mark the pc bounds as invalid,
14108 so that GDB will ignore it. */
5989a64e 14109 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14110 return PC_BOUNDS_INVALID;
c906108c
SS
14111
14112 *lowpc = low;
96408a79
SA
14113 if (highpc)
14114 *highpc = high;
af34e669 14115 return ret;
c906108c
SS
14116}
14117
b084d499
JB
14118/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14119 its low and high PC addresses. Do nothing if these addresses could not
14120 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14121 and HIGHPC to the high address if greater than HIGHPC. */
14122
14123static void
14124dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14125 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14126 struct dwarf2_cu *cu)
14127{
14128 CORE_ADDR low, high;
14129 struct die_info *child = die->child;
14130
e385593e 14131 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14132 {
325fac50
PA
14133 *lowpc = std::min (*lowpc, low);
14134 *highpc = std::max (*highpc, high);
b084d499
JB
14135 }
14136
14137 /* If the language does not allow nested subprograms (either inside
14138 subprograms or lexical blocks), we're done. */
14139 if (cu->language != language_ada)
14140 return;
6e70227d 14141
b084d499
JB
14142 /* Check all the children of the given DIE. If it contains nested
14143 subprograms, then check their pc bounds. Likewise, we need to
14144 check lexical blocks as well, as they may also contain subprogram
14145 definitions. */
14146 while (child && child->tag)
14147 {
14148 if (child->tag == DW_TAG_subprogram
14149 || child->tag == DW_TAG_lexical_block)
14150 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14151 child = child->sibling;
b084d499
JB
14152 }
14153}
14154
fae299cd
DC
14155/* Get the low and high pc's represented by the scope DIE, and store
14156 them in *LOWPC and *HIGHPC. If the correct values can't be
14157 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14158
14159static void
14160get_scope_pc_bounds (struct die_info *die,
14161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14162 struct dwarf2_cu *cu)
14163{
14164 CORE_ADDR best_low = (CORE_ADDR) -1;
14165 CORE_ADDR best_high = (CORE_ADDR) 0;
14166 CORE_ADDR current_low, current_high;
14167
3a2b436a 14168 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14169 >= PC_BOUNDS_RANGES)
fae299cd
DC
14170 {
14171 best_low = current_low;
14172 best_high = current_high;
14173 }
14174 else
14175 {
14176 struct die_info *child = die->child;
14177
14178 while (child && child->tag)
14179 {
14180 switch (child->tag) {
14181 case DW_TAG_subprogram:
b084d499 14182 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14183 break;
14184 case DW_TAG_namespace:
f55ee35c 14185 case DW_TAG_module:
fae299cd
DC
14186 /* FIXME: carlton/2004-01-16: Should we do this for
14187 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14188 that current GCC's always emit the DIEs corresponding
14189 to definitions of methods of classes as children of a
14190 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14191 the DIEs giving the declarations, which could be
14192 anywhere). But I don't see any reason why the
14193 standards says that they have to be there. */
14194 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14195
14196 if (current_low != ((CORE_ADDR) -1))
14197 {
325fac50
PA
14198 best_low = std::min (best_low, current_low);
14199 best_high = std::max (best_high, current_high);
fae299cd
DC
14200 }
14201 break;
14202 default:
0963b4bd 14203 /* Ignore. */
fae299cd
DC
14204 break;
14205 }
14206
436c571c 14207 child = child->sibling;
fae299cd
DC
14208 }
14209 }
14210
14211 *lowpc = best_low;
14212 *highpc = best_high;
14213}
14214
801e3a5b
JB
14215/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14216 in DIE. */
380bca97 14217
801e3a5b
JB
14218static void
14219dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14220 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14221{
5e22e966 14222 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14223 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14224 struct attribute *attr;
91da1414 14225 struct attribute *attr_high;
801e3a5b 14226
91da1414
MW
14227 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14228 if (attr_high)
801e3a5b 14229 {
801e3a5b 14230 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14231 if (attr != nullptr)
801e3a5b 14232 {
cd6c91b4
TT
14233 CORE_ADDR low = attr->value_as_address ();
14234 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14235
cd6c91b4 14236 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14237 high += low;
9a619af0 14238
3e29f34a
MR
14239 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14240 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14241 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14242 }
14243 }
14244
14245 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14246 if (attr != nullptr)
801e3a5b 14247 {
18a8505e 14248 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14249 We take advantage of the fact that DW_AT_ranges does not appear
14250 in DW_TAG_compile_unit of DWO files. */
14251 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14252
14253 /* The value of the DW_AT_ranges attribute is the offset of the
14254 address range list in the .debug_ranges section. */
ab435259
DE
14255 unsigned long offset = (DW_UNSND (attr)
14256 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14257
2d5f09ec 14258 std::vector<blockrange> blockvec;
5f46c5a5
JK
14259 dwarf2_ranges_process (offset, cu,
14260 [&] (CORE_ADDR start, CORE_ADDR end)
14261 {
58fdfd2c
JK
14262 start += baseaddr;
14263 end += baseaddr;
5f46c5a5
JK
14264 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14265 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14266 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14267 blockvec.emplace_back (start, end);
5f46c5a5 14268 });
2d5f09ec
KB
14269
14270 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14271 }
14272}
14273
685b1105
JK
14274/* Check whether the producer field indicates either of GCC < 4.6, or the
14275 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14276
685b1105
JK
14277static void
14278check_producer (struct dwarf2_cu *cu)
60d5a603 14279{
38360086 14280 int major, minor;
60d5a603
JK
14281
14282 if (cu->producer == NULL)
14283 {
14284 /* For unknown compilers expect their behavior is DWARF version
14285 compliant.
14286
14287 GCC started to support .debug_types sections by -gdwarf-4 since
14288 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14289 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14290 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14291 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14292 }
b1ffba5a 14293 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14294 {
38360086
MW
14295 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14296 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14297 }
5230b05a 14298 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14299 {
14300 cu->producer_is_icc = true;
14301 cu->producer_is_icc_lt_14 = major < 14;
14302 }
c258c396
JD
14303 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14304 cu->producer_is_codewarrior = true;
685b1105
JK
14305 else
14306 {
14307 /* For other non-GCC compilers, expect their behavior is DWARF version
14308 compliant. */
60d5a603
JK
14309 }
14310
9068261f 14311 cu->checked_producer = true;
685b1105 14312}
ba919b58 14313
685b1105
JK
14314/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14315 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14316 during 4.6.0 experimental. */
14317
9068261f 14318static bool
685b1105
JK
14319producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14320{
14321 if (!cu->checked_producer)
14322 check_producer (cu);
14323
14324 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14325}
14326
c258c396
JD
14327
14328/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14329 with incorrect is_stmt attributes. */
14330
14331static bool
14332producer_is_codewarrior (struct dwarf2_cu *cu)
14333{
14334 if (!cu->checked_producer)
14335 check_producer (cu);
14336
14337 return cu->producer_is_codewarrior;
14338}
14339
405feb71 14340/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14341 DW_AT_accessibility. */
14342
14343static enum dwarf_access_attribute
14344dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14345{
14346 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14347 {
14348 /* The default DWARF 2 accessibility for members is public, the default
14349 accessibility for inheritance is private. */
14350
14351 if (die->tag != DW_TAG_inheritance)
14352 return DW_ACCESS_public;
14353 else
14354 return DW_ACCESS_private;
14355 }
14356 else
14357 {
14358 /* DWARF 3+ defines the default accessibility a different way. The same
14359 rules apply now for DW_TAG_inheritance as for the members and it only
14360 depends on the container kind. */
14361
14362 if (die->parent->tag == DW_TAG_class_type)
14363 return DW_ACCESS_private;
14364 else
14365 return DW_ACCESS_public;
14366 }
14367}
14368
74ac6d43
TT
14369/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14370 offset. If the attribute was not found return 0, otherwise return
14371 1. If it was found but could not properly be handled, set *OFFSET
14372 to 0. */
14373
14374static int
14375handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14376 LONGEST *offset)
14377{
14378 struct attribute *attr;
14379
14380 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14381 if (attr != NULL)
14382 {
14383 *offset = 0;
14384
14385 /* Note that we do not check for a section offset first here.
14386 This is because DW_AT_data_member_location is new in DWARF 4,
14387 so if we see it, we can assume that a constant form is really
14388 a constant and not a section offset. */
cd6c91b4 14389 if (attr->form_is_constant ())
0826b30a 14390 *offset = attr->constant_value (0);
cd6c91b4 14391 else if (attr->form_is_section_offset ())
74ac6d43 14392 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14393 else if (attr->form_is_block ())
74ac6d43
TT
14394 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14395 else
14396 dwarf2_complex_location_expr_complaint ();
14397
14398 return 1;
14399 }
14400
14401 return 0;
14402}
14403
7d79de9a
TT
14404/* Look for DW_AT_data_member_location and store the results in FIELD. */
14405
14406static void
14407handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14408 struct field *field)
14409{
14410 struct attribute *attr;
14411
14412 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14413 if (attr != NULL)
14414 {
14415 if (attr->form_is_constant ())
14416 {
14417 LONGEST offset = attr->constant_value (0);
14418 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14419 }
14420 else if (attr->form_is_section_offset ())
14421 dwarf2_complex_location_expr_complaint ();
14422 else if (attr->form_is_block ())
14423 {
14424 bool handled;
14425 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14426 if (handled)
14427 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14428 else
14429 {
5e22e966
SM
14430 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14431 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14432 struct dwarf2_locexpr_baton *dlbaton
14433 = XOBNEW (&objfile->objfile_obstack,
14434 struct dwarf2_locexpr_baton);
14435 dlbaton->data = DW_BLOCK (attr)->data;
14436 dlbaton->size = DW_BLOCK (attr)->size;
14437 /* When using this baton, we want to compute the address
14438 of the field, not the value. This is why
14439 is_reference is set to false here. */
14440 dlbaton->is_reference = false;
5e22e966 14441 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14442 dlbaton->per_cu = cu->per_cu;
14443
14444 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14445 }
14446 }
14447 else
14448 dwarf2_complex_location_expr_complaint ();
14449 }
14450}
14451
c906108c
SS
14452/* Add an aggregate field to the field list. */
14453
14454static void
107d2387 14455dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14456 struct dwarf2_cu *cu)
6e70227d 14457{
5e22e966 14458 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14459 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14460 struct nextfield *new_field;
14461 struct attribute *attr;
14462 struct field *fp;
15d034d0 14463 const char *fieldname = "";
c906108c 14464
7d0ccb61
DJ
14465 if (die->tag == DW_TAG_inheritance)
14466 {
be2daae6
TT
14467 fip->baseclasses.emplace_back ();
14468 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14469 }
14470 else
14471 {
be2daae6
TT
14472 fip->fields.emplace_back ();
14473 new_field = &fip->fields.back ();
7d0ccb61 14474 }
be2daae6 14475
9c6a1327
TT
14476 new_field->offset = die->sect_off;
14477
e142c38c 14478 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14479 if (attr != nullptr)
c906108c 14480 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14481 else
14482 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14483 if (new_field->accessibility != DW_ACCESS_public)
14484 fip->non_public_fields = 1;
60d5a603 14485
e142c38c 14486 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14487 if (attr != nullptr)
c906108c 14488 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14489 else
14490 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14491
14492 fp = &new_field->field;
a9a9bd0f 14493
e142c38c 14494 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14495 {
a9a9bd0f 14496 /* Data member other than a C++ static data member. */
6e70227d 14497
c906108c 14498 /* Get type of field. */
e7c27a73 14499 fp->type = die_type (die, cu);
c906108c 14500
d6a843b5 14501 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14502
c906108c 14503 /* Get bit size of field (zero if none). */
e142c38c 14504 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14505 if (attr != nullptr)
c906108c
SS
14506 {
14507 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14508 }
14509 else
14510 {
14511 FIELD_BITSIZE (*fp) = 0;
14512 }
14513
14514 /* Get bit offset of field. */
7d79de9a 14515 handle_data_member_location (die, cu, fp);
e142c38c 14516 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14517 if (attr != nullptr)
c906108c 14518 {
d5a22e77 14519 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14520 {
14521 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14522 additional bit offset from the MSB of the containing
14523 anonymous object to the MSB of the field. We don't
14524 have to do anything special since we don't need to
14525 know the size of the anonymous object. */
f41f5e61 14526 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14527 }
14528 else
14529 {
14530 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14531 MSB of the anonymous object, subtract off the number of
14532 bits from the MSB of the field to the MSB of the
14533 object, and then subtract off the number of bits of
14534 the field itself. The result is the bit offset of
14535 the LSB of the field. */
c906108c
SS
14536 int anonymous_size;
14537 int bit_offset = DW_UNSND (attr);
14538
e142c38c 14539 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14540 if (attr != nullptr)
c906108c
SS
14541 {
14542 /* The size of the anonymous object containing
14543 the bit field is explicit, so use the
14544 indicated size (in bytes). */
14545 anonymous_size = DW_UNSND (attr);
14546 }
14547 else
14548 {
14549 /* The size of the anonymous object containing
14550 the bit field must be inferred from the type
14551 attribute of the data member containing the
14552 bit field. */
14553 anonymous_size = TYPE_LENGTH (fp->type);
14554 }
f41f5e61
PA
14555 SET_FIELD_BITPOS (*fp,
14556 (FIELD_BITPOS (*fp)
14557 + anonymous_size * bits_per_byte
14558 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14559 }
14560 }
da5b30da
AA
14561 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14562 if (attr != NULL)
14563 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14564 + attr->constant_value (0)));
c906108c
SS
14565
14566 /* Get name of field. */
39cbfefa
DJ
14567 fieldname = dwarf2_name (die, cu);
14568 if (fieldname == NULL)
14569 fieldname = "";
d8151005
DJ
14570
14571 /* The name is already allocated along with this objfile, so we don't
14572 need to duplicate it for the type. */
14573 fp->name = fieldname;
c906108c
SS
14574
14575 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14576 pointer or virtual base class pointer) to private. */
e142c38c 14577 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14578 {
d48cc9dd 14579 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14580 new_field->accessibility = DW_ACCESS_private;
14581 fip->non_public_fields = 1;
14582 }
14583 }
a9a9bd0f 14584 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14585 {
a9a9bd0f
DC
14586 /* C++ static member. */
14587
14588 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14589 is a declaration, but all versions of G++ as of this writing
14590 (so through at least 3.2.1) incorrectly generate
14591 DW_TAG_variable tags. */
6e70227d 14592
ff355380 14593 const char *physname;
c906108c 14594
a9a9bd0f 14595 /* Get name of field. */
39cbfefa
DJ
14596 fieldname = dwarf2_name (die, cu);
14597 if (fieldname == NULL)
c906108c
SS
14598 return;
14599
254e6b9e 14600 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14601 if (attr
14602 /* Only create a symbol if this is an external value.
14603 new_symbol checks this and puts the value in the global symbol
14604 table, which we want. If it is not external, new_symbol
14605 will try to put the value in cu->list_in_scope which is wrong. */
14606 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14607 {
14608 /* A static const member, not much different than an enum as far as
14609 we're concerned, except that we can support more types. */
14610 new_symbol (die, NULL, cu);
14611 }
14612
2df3850c 14613 /* Get physical name. */
ff355380 14614 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14615
d8151005
DJ
14616 /* The name is already allocated along with this objfile, so we don't
14617 need to duplicate it for the type. */
14618 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14619 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14620 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14621 }
14622 else if (die->tag == DW_TAG_inheritance)
14623 {
74ac6d43 14624 /* C++ base class field. */
7d79de9a 14625 handle_data_member_location (die, cu, fp);
c906108c 14626 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14627 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14628 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14629 }
2ddeaf8a
TT
14630 else
14631 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14632}
14633
883fd55a
KS
14634/* Can the type given by DIE define another type? */
14635
14636static bool
14637type_can_define_types (const struct die_info *die)
14638{
14639 switch (die->tag)
14640 {
14641 case DW_TAG_typedef:
14642 case DW_TAG_class_type:
14643 case DW_TAG_structure_type:
14644 case DW_TAG_union_type:
14645 case DW_TAG_enumeration_type:
14646 return true;
14647
14648 default:
14649 return false;
14650 }
14651}
14652
14653/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14654
14655static void
883fd55a
KS
14656dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14657 struct dwarf2_cu *cu)
6e70227d 14658{
be2daae6
TT
14659 struct decl_field fp;
14660 memset (&fp, 0, sizeof (fp));
98751a41 14661
883fd55a 14662 gdb_assert (type_can_define_types (die));
98751a41 14663
883fd55a 14664 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14665 fp.name = dwarf2_name (die, cu);
14666 fp.type = read_type_die (die, cu);
98751a41 14667
c191a687
KS
14668 /* Save accessibility. */
14669 enum dwarf_access_attribute accessibility;
14670 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14671 if (attr != NULL)
14672 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14673 else
14674 accessibility = dwarf2_default_access_attribute (die, cu);
14675 switch (accessibility)
14676 {
14677 case DW_ACCESS_public:
14678 /* The assumed value if neither private nor protected. */
14679 break;
14680 case DW_ACCESS_private:
be2daae6 14681 fp.is_private = 1;
c191a687
KS
14682 break;
14683 case DW_ACCESS_protected:
be2daae6 14684 fp.is_protected = 1;
c191a687
KS
14685 break;
14686 default:
b98664d3 14687 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14688 }
14689
883fd55a 14690 if (die->tag == DW_TAG_typedef)
be2daae6 14691 fip->typedef_field_list.push_back (fp);
883fd55a 14692 else
be2daae6 14693 fip->nested_types_list.push_back (fp);
98751a41
JK
14694}
14695
9c6a1327
TT
14696/* A convenience typedef that's used when finding the discriminant
14697 field for a variant part. */
1b95cdb7
SM
14698typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14699 offset_map_type;
9c6a1327
TT
14700
14701/* Compute the discriminant range for a given variant. OBSTACK is
14702 where the results will be stored. VARIANT is the variant to
14703 process. IS_UNSIGNED indicates whether the discriminant is signed
14704 or unsigned. */
14705
14706static const gdb::array_view<discriminant_range>
14707convert_variant_range (struct obstack *obstack, const variant_field &variant,
14708 bool is_unsigned)
14709{
14710 std::vector<discriminant_range> ranges;
14711
14712 if (variant.default_branch)
14713 return {};
14714
14715 if (variant.discr_list_data == nullptr)
14716 {
14717 discriminant_range r
14718 = {variant.discriminant_value, variant.discriminant_value};
14719 ranges.push_back (r);
14720 }
14721 else
14722 {
14723 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14724 variant.discr_list_data->size);
14725 while (!data.empty ())
14726 {
14727 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14728 {
14729 complaint (_("invalid discriminant marker: %d"), data[0]);
14730 break;
14731 }
14732 bool is_range = data[0] == DW_DSC_range;
14733 data = data.slice (1);
14734
14735 ULONGEST low, high;
14736 unsigned int bytes_read;
14737
14738 if (data.empty ())
14739 {
14740 complaint (_("DW_AT_discr_list missing low value"));
14741 break;
14742 }
14743 if (is_unsigned)
14744 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14745 else
14746 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14747 &bytes_read);
14748 data = data.slice (bytes_read);
14749
14750 if (is_range)
14751 {
14752 if (data.empty ())
14753 {
14754 complaint (_("DW_AT_discr_list missing high value"));
14755 break;
14756 }
14757 if (is_unsigned)
14758 high = read_unsigned_leb128 (nullptr, data.data (),
14759 &bytes_read);
14760 else
14761 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14762 &bytes_read);
14763 data = data.slice (bytes_read);
14764 }
14765 else
14766 high = low;
14767
14768 ranges.push_back ({ low, high });
14769 }
14770 }
14771
14772 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14773 ranges.size ());
14774 std::copy (ranges.begin (), ranges.end (), result);
14775 return gdb::array_view<discriminant_range> (result, ranges.size ());
14776}
14777
14778static const gdb::array_view<variant_part> create_variant_parts
14779 (struct obstack *obstack,
14780 const offset_map_type &offset_map,
14781 struct field_info *fi,
14782 const std::vector<variant_part_builder> &variant_parts);
14783
14784/* Fill in a "struct variant" for a given variant field. RESULT is
14785 the variant to fill in. OBSTACK is where any needed allocations
14786 will be done. OFFSET_MAP holds the mapping from section offsets to
14787 fields for the type. FI describes the fields of the type we're
14788 processing. FIELD is the variant field we're converting. */
14789
14790static void
14791create_one_variant (variant &result, struct obstack *obstack,
14792 const offset_map_type &offset_map,
14793 struct field_info *fi, const variant_field &field)
14794{
14795 result.discriminants = convert_variant_range (obstack, field, false);
14796 result.first_field = field.first_field + fi->baseclasses.size ();
14797 result.last_field = field.last_field + fi->baseclasses.size ();
14798 result.parts = create_variant_parts (obstack, offset_map, fi,
14799 field.variant_parts);
14800}
14801
14802/* Fill in a "struct variant_part" for a given variant part. RESULT
14803 is the variant part to fill in. OBSTACK is where any needed
14804 allocations will be done. OFFSET_MAP holds the mapping from
14805 section offsets to fields for the type. FI describes the fields of
14806 the type we're processing. BUILDER is the variant part to be
14807 converted. */
14808
14809static void
14810create_one_variant_part (variant_part &result,
14811 struct obstack *obstack,
14812 const offset_map_type &offset_map,
14813 struct field_info *fi,
14814 const variant_part_builder &builder)
14815{
14816 auto iter = offset_map.find (builder.discriminant_offset);
14817 if (iter == offset_map.end ())
14818 {
14819 result.discriminant_index = -1;
14820 /* Doesn't matter. */
14821 result.is_unsigned = false;
14822 }
14823 else
14824 {
14825 result.discriminant_index = iter->second;
14826 result.is_unsigned
14827 = TYPE_UNSIGNED (FIELD_TYPE
14828 (fi->fields[result.discriminant_index].field));
14829 }
14830
14831 size_t n = builder.variants.size ();
14832 variant *output = new (obstack) variant[n];
14833 for (size_t i = 0; i < n; ++i)
14834 create_one_variant (output[i], obstack, offset_map, fi,
14835 builder.variants[i]);
14836
14837 result.variants = gdb::array_view<variant> (output, n);
14838}
14839
14840/* Create a vector of variant parts that can be attached to a type.
14841 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14842 holds the mapping from section offsets to fields for the type. FI
14843 describes the fields of the type we're processing. VARIANT_PARTS
14844 is the vector to convert. */
14845
14846static const gdb::array_view<variant_part>
14847create_variant_parts (struct obstack *obstack,
14848 const offset_map_type &offset_map,
14849 struct field_info *fi,
14850 const std::vector<variant_part_builder> &variant_parts)
14851{
14852 if (variant_parts.empty ())
14853 return {};
14854
14855 size_t n = variant_parts.size ();
14856 variant_part *result = new (obstack) variant_part[n];
14857 for (size_t i = 0; i < n; ++i)
14858 create_one_variant_part (result[i], obstack, offset_map, fi,
14859 variant_parts[i]);
14860
14861 return gdb::array_view<variant_part> (result, n);
14862}
14863
14864/* Compute the variant part vector for FIP, attaching it to TYPE when
14865 done. */
14866
14867static void
14868add_variant_property (struct field_info *fip, struct type *type,
14869 struct dwarf2_cu *cu)
14870{
14871 /* Map section offsets of fields to their field index. Note the
14872 field index here does not take the number of baseclasses into
14873 account. */
14874 offset_map_type offset_map;
14875 for (int i = 0; i < fip->fields.size (); ++i)
14876 offset_map[fip->fields[i].offset] = i;
14877
5e22e966 14878 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14879 gdb::array_view<variant_part> parts
14880 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14881 fip->variant_parts);
14882
14883 struct dynamic_prop prop;
14884 prop.kind = PROP_VARIANT_PARTS;
14885 prop.data.variant_parts
14886 = ((gdb::array_view<variant_part> *)
14887 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14888
5c54719c 14889 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14890}
14891
c906108c
SS
14892/* Create the vector of fields, and attach it to the type. */
14893
14894static void
fba45db2 14895dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14896 struct dwarf2_cu *cu)
c906108c 14897{
317f7127 14898 int nfields = fip->nfields ();
c906108c
SS
14899
14900 /* Record the field count, allocate space for the array of fields,
14901 and create blank accessibility bitfields if necessary. */
5e33d5f4 14902 type->set_num_fields (nfields);
3cabb6b0
SM
14903 type->set_fields
14904 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14905
b4ba55a1 14906 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14907 {
14908 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14909
14910 TYPE_FIELD_PRIVATE_BITS (type) =
14911 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14912 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14913
14914 TYPE_FIELD_PROTECTED_BITS (type) =
14915 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14916 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14917
774b6a14
TT
14918 TYPE_FIELD_IGNORE_BITS (type) =
14919 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14920 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14921 }
14922
14923 /* If the type has baseclasses, allocate and clear a bit vector for
14924 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14925 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14926 {
be2daae6 14927 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14928 unsigned char *pointer;
c906108c
SS
14929
14930 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14931 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14932 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14933 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14934 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14935 }
14936
9c6a1327
TT
14937 if (!fip->variant_parts.empty ())
14938 add_variant_property (fip, type, cu);
2ddeaf8a 14939
be2daae6
TT
14940 /* Copy the saved-up fields into the field vector. */
14941 for (int i = 0; i < nfields; ++i)
c906108c 14942 {
be2daae6
TT
14943 struct nextfield &field
14944 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14945 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14946
ceacbf6e 14947 type->field (i) = field.field;
be2daae6 14948 switch (field.accessibility)
c906108c 14949 {
c5aa993b 14950 case DW_ACCESS_private:
b4ba55a1 14951 if (cu->language != language_ada)
be2daae6 14952 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14953 break;
c906108c 14954
c5aa993b 14955 case DW_ACCESS_protected:
b4ba55a1 14956 if (cu->language != language_ada)
be2daae6 14957 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14958 break;
c906108c 14959
c5aa993b
JM
14960 case DW_ACCESS_public:
14961 break;
c906108c 14962
c5aa993b
JM
14963 default:
14964 /* Unknown accessibility. Complain and treat it as public. */
14965 {
b98664d3 14966 complaint (_("unsupported accessibility %d"),
be2daae6 14967 field.accessibility);
c5aa993b
JM
14968 }
14969 break;
c906108c 14970 }
be2daae6 14971 if (i < fip->baseclasses.size ())
c906108c 14972 {
be2daae6 14973 switch (field.virtuality)
c906108c 14974 {
c5aa993b
JM
14975 case DW_VIRTUALITY_virtual:
14976 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14977 if (cu->language == language_ada)
a73c6dcd 14978 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14979 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14980 break;
c906108c
SS
14981 }
14982 }
c906108c
SS
14983 }
14984}
14985
7d27a96d
TT
14986/* Return true if this member function is a constructor, false
14987 otherwise. */
14988
14989static int
14990dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14991{
14992 const char *fieldname;
fe978cb0 14993 const char *type_name;
7d27a96d
TT
14994 int len;
14995
14996 if (die->parent == NULL)
14997 return 0;
14998
14999 if (die->parent->tag != DW_TAG_structure_type
15000 && die->parent->tag != DW_TAG_union_type
15001 && die->parent->tag != DW_TAG_class_type)
15002 return 0;
15003
15004 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15005 type_name = dwarf2_name (die->parent, cu);
15006 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15007 return 0;
15008
15009 len = strlen (fieldname);
fe978cb0
PA
15010 return (strncmp (fieldname, type_name, len) == 0
15011 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15012}
15013
e35000a7
TBA
15014/* Check if the given VALUE is a recognized enum
15015 dwarf_defaulted_attribute constant according to DWARF5 spec,
15016 Table 7.24. */
15017
15018static bool
15019is_valid_DW_AT_defaulted (ULONGEST value)
15020{
15021 switch (value)
15022 {
15023 case DW_DEFAULTED_no:
15024 case DW_DEFAULTED_in_class:
15025 case DW_DEFAULTED_out_of_class:
15026 return true;
15027 }
15028
3142e908 15029 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15030 return false;
15031}
15032
c906108c
SS
15033/* Add a member function to the proper fieldlist. */
15034
15035static void
107d2387 15036dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15037 struct type *type, struct dwarf2_cu *cu)
c906108c 15038{
5e22e966 15039 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15040 struct attribute *attr;
c906108c 15041 int i;
be2daae6 15042 struct fnfieldlist *flp = nullptr;
c906108c 15043 struct fn_field *fnp;
15d034d0 15044 const char *fieldname;
f792889a 15045 struct type *this_type;
60d5a603 15046 enum dwarf_access_attribute accessibility;
c906108c 15047
b4ba55a1 15048 if (cu->language == language_ada)
a73c6dcd 15049 error (_("unexpected member function in Ada type"));
b4ba55a1 15050
2df3850c 15051 /* Get name of member function. */
39cbfefa
DJ
15052 fieldname = dwarf2_name (die, cu);
15053 if (fieldname == NULL)
2df3850c 15054 return;
c906108c 15055
c906108c 15056 /* Look up member function name in fieldlist. */
be2daae6 15057 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15058 {
27bfe10e 15059 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15060 {
15061 flp = &fip->fnfieldlists[i];
15062 break;
15063 }
c906108c
SS
15064 }
15065
be2daae6
TT
15066 /* Create a new fnfieldlist if necessary. */
15067 if (flp == nullptr)
c906108c 15068 {
be2daae6
TT
15069 fip->fnfieldlists.emplace_back ();
15070 flp = &fip->fnfieldlists.back ();
c906108c 15071 flp->name = fieldname;
be2daae6 15072 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15073 }
15074
be2daae6
TT
15075 /* Create a new member function field and add it to the vector of
15076 fnfieldlists. */
15077 flp->fnfields.emplace_back ();
15078 fnp = &flp->fnfields.back ();
3da10d80
KS
15079
15080 /* Delay processing of the physname until later. */
9c37b5ae 15081 if (cu->language == language_cplus)
be2daae6
TT
15082 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15083 die, cu);
3da10d80
KS
15084 else
15085 {
1d06ead6 15086 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15087 fnp->physname = physname ? physname : "";
15088 }
15089
c906108c 15090 fnp->type = alloc_type (objfile);
f792889a 15091 this_type = read_type_die (die, cu);
78134374 15092 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15093 {
1f704f76 15094 int nparams = this_type->num_fields ();
c906108c 15095
f792889a 15096 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15097 of the method itself (TYPE_CODE_METHOD). */
15098 smash_to_method_type (fnp->type, type,
f792889a 15099 TYPE_TARGET_TYPE (this_type),
80fc5e77 15100 this_type->fields (),
1f704f76 15101 this_type->num_fields (),
f792889a 15102 TYPE_VARARGS (this_type));
c906108c
SS
15103
15104 /* Handle static member functions.
c5aa993b 15105 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15106 member functions. G++ helps GDB by marking the first
15107 parameter for non-static member functions (which is the this
15108 pointer) as artificial. We obtain this information from
15109 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15110 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15111 fnp->voffset = VOFFSET_STATIC;
15112 }
15113 else
b98664d3 15114 complaint (_("member function type missing for '%s'"),
3da10d80 15115 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15116
15117 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15118 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15119 fnp->fcontext = die_containing_type (die, cu);
c906108c 15120
3e43a32a
MS
15121 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15122 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15123
15124 /* Get accessibility. */
e142c38c 15125 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15126 if (attr != nullptr)
aead7601 15127 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15128 else
15129 accessibility = dwarf2_default_access_attribute (die, cu);
15130 switch (accessibility)
c906108c 15131 {
60d5a603
JK
15132 case DW_ACCESS_private:
15133 fnp->is_private = 1;
15134 break;
15135 case DW_ACCESS_protected:
15136 fnp->is_protected = 1;
15137 break;
c906108c
SS
15138 }
15139
b02dede2 15140 /* Check for artificial methods. */
e142c38c 15141 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15142 if (attr && DW_UNSND (attr) != 0)
15143 fnp->is_artificial = 1;
15144
e35000a7
TBA
15145 /* Check for defaulted methods. */
15146 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15147 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15148 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15149
15150 /* Check for deleted methods. */
15151 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15152 if (attr != nullptr && DW_UNSND (attr) != 0)
15153 fnp->is_deleted = 1;
15154
7d27a96d
TT
15155 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15156
0d564a31 15157 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15158 function. For older versions of GCC, this is an offset in the
15159 appropriate virtual table, as specified by DW_AT_containing_type.
15160 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15161 to the object address. */
15162
e142c38c 15163 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15164 if (attr != nullptr)
8e19ed76 15165 {
4fc6c0d5 15166 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15167 {
aec5aa8b
TT
15168 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15169 {
15170 /* Old-style GCC. */
15171 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15172 }
15173 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15174 || (DW_BLOCK (attr)->size > 1
15175 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15176 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15177 {
aec5aa8b
TT
15178 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15179 if ((fnp->voffset % cu->header.addr_size) != 0)
15180 dwarf2_complex_location_expr_complaint ();
15181 else
15182 fnp->voffset /= cu->header.addr_size;
15183 fnp->voffset += 2;
15184 }
15185 else
15186 dwarf2_complex_location_expr_complaint ();
15187
15188 if (!fnp->fcontext)
7e993ebf
KS
15189 {
15190 /* If there is no `this' field and no DW_AT_containing_type,
15191 we cannot actually find a base class context for the
15192 vtable! */
1f704f76 15193 if (this_type->num_fields () == 0
7e993ebf
KS
15194 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15195 {
b98664d3 15196 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15197 "function \"%s\" (offset %s)"),
15198 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15199 }
15200 else
15201 {
15202 fnp->fcontext
15203 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15204 }
15205 }
aec5aa8b 15206 }
cd6c91b4 15207 else if (attr->form_is_section_offset ())
8e19ed76 15208 {
4d3c2250 15209 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15210 }
15211 else
15212 {
4d3c2250
KB
15213 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15214 fieldname);
8e19ed76 15215 }
0d564a31 15216 }
d48cc9dd
DJ
15217 else
15218 {
15219 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15220 if (attr && DW_UNSND (attr))
15221 {
15222 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15223 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15224 "but the vtable offset is not specified"),
9d8780f0 15225 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15227 TYPE_CPLUS_DYNAMIC (type) = 1;
15228 }
15229 }
c906108c
SS
15230}
15231
15232/* Create the vector of member function fields, and attach it to the type. */
15233
15234static void
fba45db2 15235dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15236 struct dwarf2_cu *cu)
c906108c 15237{
b4ba55a1 15238 if (cu->language == language_ada)
a73c6dcd 15239 error (_("unexpected member functions in Ada type"));
b4ba55a1 15240
c906108c
SS
15241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15242 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15243 TYPE_ALLOC (type,
15244 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15245
be2daae6 15246 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15247 {
be2daae6 15248 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15249 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15250
be2daae6
TT
15251 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15252 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15253 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15254 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15255
15256 for (int k = 0; k < nf.fnfields.size (); ++k)
15257 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15258 }
15259
be2daae6 15260 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15261}
15262
1168df01
JB
15263/* Returns non-zero if NAME is the name of a vtable member in CU's
15264 language, zero otherwise. */
15265static int
15266is_vtable_name (const char *name, struct dwarf2_cu *cu)
15267{
15268 static const char vptr[] = "_vptr";
15269
9c37b5ae
TT
15270 /* Look for the C++ form of the vtable. */
15271 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15272 return 1;
15273
15274 return 0;
15275}
15276
c0dd20ea 15277/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15278 functions, with the ABI-specified layout. If TYPE describes
15279 such a structure, smash it into a member function type.
61049d3b
DJ
15280
15281 GCC shouldn't do this; it should just output pointer to member DIEs.
15282 This is GCC PR debug/28767. */
c0dd20ea 15283
0b92b5bb
TT
15284static void
15285quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15286{
09e2d7c7 15287 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15288
15289 /* Check for a structure with no name and two children. */
1f704f76 15290 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15291 return;
c0dd20ea
DJ
15292
15293 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15294 if (TYPE_FIELD_NAME (type, 0) == NULL
15295 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15296 || TYPE_FIELD_NAME (type, 1) == NULL
15297 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15298 return;
c0dd20ea
DJ
15299
15300 /* Find the type of the method. */
0b92b5bb 15301 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15302 if (pfn_type == NULL
78134374
SM
15303 || pfn_type->code () != TYPE_CODE_PTR
15304 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15305 return;
c0dd20ea
DJ
15306
15307 /* Look for the "this" argument. */
15308 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15309 if (pfn_type->num_fields () == 0
0b92b5bb 15310 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15311 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15312 return;
c0dd20ea 15313
09e2d7c7 15314 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15315 new_type = alloc_type (objfile);
09e2d7c7 15316 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15317 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15318 TYPE_VARARGS (pfn_type));
0b92b5bb 15319 smash_to_methodptr_type (type, new_type);
c0dd20ea 15320}
1168df01 15321
2b4424c3
TT
15322/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15323 appropriate error checking and issuing complaints if there is a
15324 problem. */
15325
15326static ULONGEST
15327get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15328{
15329 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15330
15331 if (attr == nullptr)
15332 return 0;
15333
cd6c91b4 15334 if (!attr->form_is_constant ())
2b4424c3 15335 {
b98664d3 15336 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15337 " - DIE at %s [in module %s]"),
15338 sect_offset_str (die->sect_off),
5e22e966 15339 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15340 return 0;
15341 }
15342
15343 ULONGEST align;
15344 if (attr->form == DW_FORM_sdata)
15345 {
15346 LONGEST val = DW_SND (attr);
15347 if (val < 0)
15348 {
b98664d3 15349 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15350 " - DIE at %s [in module %s]"),
15351 sect_offset_str (die->sect_off),
5e22e966 15352 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15353 return 0;
15354 }
15355 align = val;
15356 }
15357 else
15358 align = DW_UNSND (attr);
15359
15360 if (align == 0)
15361 {
b98664d3 15362 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15363 " - DIE at %s [in module %s]"),
15364 sect_offset_str (die->sect_off),
5e22e966 15365 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15366 return 0;
15367 }
15368 if ((align & (align - 1)) != 0)
15369 {
b98664d3 15370 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15371 " - DIE at %s [in module %s]"),
15372 sect_offset_str (die->sect_off),
5e22e966 15373 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15374 return 0;
15375 }
15376
15377 return align;
15378}
15379
15380/* If the DIE has a DW_AT_alignment attribute, use its value to set
15381 the alignment for TYPE. */
15382
15383static void
15384maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15385 struct type *type)
15386{
15387 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15388 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15389 " - DIE at %s [in module %s]"),
15390 sect_offset_str (die->sect_off),
5e22e966 15391 objfile_name (cu->per_objfile->objfile));
2b4424c3 15392}
685b1105 15393
e35000a7
TBA
15394/* Check if the given VALUE is a valid enum dwarf_calling_convention
15395 constant for a type, according to DWARF5 spec, Table 5.5. */
15396
15397static bool
15398is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15399{
15400 switch (value)
15401 {
15402 case DW_CC_normal:
15403 case DW_CC_pass_by_reference:
15404 case DW_CC_pass_by_value:
15405 return true;
15406
15407 default:
15408 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15409 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15410 return false;
15411 }
15412}
15413
d0922fcf
TBA
15414/* Check if the given VALUE is a valid enum dwarf_calling_convention
15415 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15416 also according to GNU-specific values (see include/dwarf2.h). */
15417
15418static bool
15419is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15420{
15421 switch (value)
15422 {
15423 case DW_CC_normal:
15424 case DW_CC_program:
15425 case DW_CC_nocall:
15426 return true;
15427
15428 case DW_CC_GNU_renesas_sh:
15429 case DW_CC_GNU_borland_fastcall_i386:
15430 case DW_CC_GDB_IBM_OpenCL:
15431 return true;
15432
15433 default:
15434 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15435 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15436 return false;
15437 }
15438}
15439
c906108c 15440/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15441 (definition) to create a type for the structure or union. Fill in
15442 the type's name and general properties; the members will not be
83655187
DE
15443 processed until process_structure_scope. A symbol table entry for
15444 the type will also not be done until process_structure_scope (assuming
15445 the type has a name).
c906108c 15446
c767944b
DJ
15447 NOTE: we need to call these functions regardless of whether or not the
15448 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15449 structure or union. This gets the type entered into our set of
83655187 15450 user defined types. */
c906108c 15451
f792889a 15452static struct type *
134d01f1 15453read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15454{
5e22e966 15455 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15456 struct type *type;
15457 struct attribute *attr;
15d034d0 15458 const char *name;
c906108c 15459
348e048f
DE
15460 /* If the definition of this type lives in .debug_types, read that type.
15461 Don't follow DW_AT_specification though, that will take us back up
15462 the chain and we want to go down. */
052c8bb8 15463 attr = die->attr (DW_AT_signature);
435d3d88 15464 if (attr != nullptr)
348e048f 15465 {
ac9ec31b 15466 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15467
ac9ec31b 15468 /* The type's CU may not be the same as CU.
02142a6c 15469 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15470 return set_die_type (die, type, cu);
15471 }
15472
c0dd20ea 15473 type = alloc_type (objfile);
c906108c 15474 INIT_CPLUS_SPECIFIC (type);
93311388 15475
39cbfefa
DJ
15476 name = dwarf2_name (die, cu);
15477 if (name != NULL)
c906108c 15478 {
987504bb 15479 if (cu->language == language_cplus
c44af4eb
TT
15480 || cu->language == language_d
15481 || cu->language == language_rust)
63d06c5c 15482 {
15d034d0 15483 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15484
15485 /* dwarf2_full_name might have already finished building the DIE's
15486 type. If so, there is no need to continue. */
15487 if (get_die_type (die, cu) != NULL)
15488 return get_die_type (die, cu);
15489
d0e39ea2 15490 type->set_name (full_name);
63d06c5c
DC
15491 }
15492 else
15493 {
d8151005
DJ
15494 /* The name is already allocated along with this objfile, so
15495 we don't need to duplicate it for the type. */
d0e39ea2 15496 type->set_name (name);
63d06c5c 15497 }
c906108c
SS
15498 }
15499
15500 if (die->tag == DW_TAG_structure_type)
15501 {
67607e24 15502 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15503 }
15504 else if (die->tag == DW_TAG_union_type)
15505 {
67607e24 15506 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15507 }
15508 else
15509 {
67607e24 15510 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15511 }
15512
0cc2414c
TT
15513 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15514 TYPE_DECLARED_CLASS (type) = 1;
15515
e35000a7
TBA
15516 /* Store the calling convention in the type if it's available in
15517 the die. Otherwise the calling convention remains set to
15518 the default value DW_CC_normal. */
15519 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15520 if (attr != nullptr
15521 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15522 {
15523 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15524 TYPE_CPLUS_CALLING_CONVENTION (type)
15525 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15526 }
15527
e142c38c 15528 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15529 if (attr != nullptr)
c906108c 15530 {
cd6c91b4 15531 if (attr->form_is_constant ())
155bfbd3
JB
15532 TYPE_LENGTH (type) = DW_UNSND (attr);
15533 else
15534 {
f8e89861 15535 struct dynamic_prop prop;
293e7e51 15536 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15537 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15538 TYPE_LENGTH (type) = 0;
15539 }
c906108c
SS
15540 }
15541 else
15542 {
15543 TYPE_LENGTH (type) = 0;
15544 }
15545
2b4424c3
TT
15546 maybe_set_alignment (cu, die, type);
15547
5230b05a 15548 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15549 {
5230b05a
WT
15550 /* ICC<14 does not output the required DW_AT_declaration on
15551 incomplete types, but gives them a size of zero. */
422b1cb0 15552 TYPE_STUB (type) = 1;
685b1105
JK
15553 }
15554 else
15555 TYPE_STUB_SUPPORTED (type) = 1;
15556
dc718098 15557 if (die_is_declaration (die, cu))
876cecd0 15558 TYPE_STUB (type) = 1;
a6c727b2
DJ
15559 else if (attr == NULL && die->child == NULL
15560 && producer_is_realview (cu->producer))
15561 /* RealView does not output the required DW_AT_declaration
15562 on incomplete types. */
15563 TYPE_STUB (type) = 1;
dc718098 15564
c906108c
SS
15565 /* We need to add the type field to the die immediately so we don't
15566 infinitely recurse when dealing with pointers to the structure
0963b4bd 15567 type within the structure itself. */
1c379e20 15568 set_die_type (die, type, cu);
c906108c 15569
7e314c57
JK
15570 /* set_die_type should be already done. */
15571 set_descriptive_type (type, die, cu);
15572
c767944b
DJ
15573 return type;
15574}
15575
9c6a1327
TT
15576static void handle_struct_member_die
15577 (struct die_info *child_die,
15578 struct type *type,
15579 struct field_info *fi,
15580 std::vector<struct symbol *> *template_args,
15581 struct dwarf2_cu *cu);
15582
15583/* A helper for handle_struct_member_die that handles
15584 DW_TAG_variant_part. */
15585
15586static void
15587handle_variant_part (struct die_info *die, struct type *type,
15588 struct field_info *fi,
15589 std::vector<struct symbol *> *template_args,
15590 struct dwarf2_cu *cu)
15591{
15592 variant_part_builder *new_part;
15593 if (fi->current_variant_part == nullptr)
15594 {
15595 fi->variant_parts.emplace_back ();
15596 new_part = &fi->variant_parts.back ();
15597 }
15598 else if (!fi->current_variant_part->processing_variant)
15599 {
15600 complaint (_("nested DW_TAG_variant_part seen "
15601 "- DIE at %s [in module %s]"),
15602 sect_offset_str (die->sect_off),
5e22e966 15603 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15604 return;
15605 }
15606 else
15607 {
15608 variant_field &current = fi->current_variant_part->variants.back ();
15609 current.variant_parts.emplace_back ();
15610 new_part = &current.variant_parts.back ();
15611 }
15612
15613 /* When we recurse, we want callees to add to this new variant
15614 part. */
15615 scoped_restore save_current_variant_part
15616 = make_scoped_restore (&fi->current_variant_part, new_part);
15617
15618 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15619 if (discr == NULL)
15620 {
15621 /* It's a univariant form, an extension we support. */
15622 }
15623 else if (discr->form_is_ref ())
15624 {
15625 struct dwarf2_cu *target_cu = cu;
15626 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15627
15628 new_part->discriminant_offset = target_die->sect_off;
15629 }
15630 else
15631 {
15632 complaint (_("DW_AT_discr does not have DIE reference form"
15633 " - DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
5e22e966 15635 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15636 }
15637
15638 for (die_info *child_die = die->child;
15639 child_die != NULL;
15640 child_die = child_die->sibling)
15641 handle_struct_member_die (child_die, type, fi, template_args, cu);
15642}
15643
15644/* A helper for handle_struct_member_die that handles
15645 DW_TAG_variant. */
15646
15647static void
15648handle_variant (struct die_info *die, struct type *type,
15649 struct field_info *fi,
15650 std::vector<struct symbol *> *template_args,
15651 struct dwarf2_cu *cu)
15652{
15653 if (fi->current_variant_part == nullptr)
15654 {
15655 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15656 "- DIE at %s [in module %s]"),
15657 sect_offset_str (die->sect_off),
5e22e966 15658 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15659 return;
15660 }
15661 if (fi->current_variant_part->processing_variant)
15662 {
15663 complaint (_("nested DW_TAG_variant seen "
15664 "- DIE at %s [in module %s]"),
15665 sect_offset_str (die->sect_off),
5e22e966 15666 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15667 return;
15668 }
15669
15670 scoped_restore save_processing_variant
15671 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15672 true);
15673
15674 fi->current_variant_part->variants.emplace_back ();
15675 variant_field &variant = fi->current_variant_part->variants.back ();
15676 variant.first_field = fi->fields.size ();
15677
15678 /* In a variant we want to get the discriminant and also add a
15679 field for our sole member child. */
15680 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15681 if (discr == nullptr)
15682 {
15683 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15684 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15685 variant.default_branch = true;
15686 else
15687 variant.discr_list_data = DW_BLOCK (discr);
15688 }
15689 else
15690 variant.discriminant_value = DW_UNSND (discr);
15691
15692 for (die_info *variant_child = die->child;
15693 variant_child != NULL;
15694 variant_child = variant_child->sibling)
15695 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15696
15697 variant.last_field = fi->fields.size ();
15698}
15699
2ddeaf8a
TT
15700/* A helper for process_structure_scope that handles a single member
15701 DIE. */
15702
15703static void
15704handle_struct_member_die (struct die_info *child_die, struct type *type,
15705 struct field_info *fi,
15706 std::vector<struct symbol *> *template_args,
15707 struct dwarf2_cu *cu)
15708{
15709 if (child_die->tag == DW_TAG_member
9c6a1327 15710 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15711 {
15712 /* NOTE: carlton/2002-11-05: A C++ static data member
15713 should be a DW_TAG_member that is a declaration, but
15714 all versions of G++ as of this writing (so through at
15715 least 3.2.1) incorrectly generate DW_TAG_variable
15716 tags for them instead. */
15717 dwarf2_add_field (fi, child_die, cu);
15718 }
15719 else if (child_die->tag == DW_TAG_subprogram)
15720 {
15721 /* Rust doesn't have member functions in the C++ sense.
15722 However, it does emit ordinary functions as children
15723 of a struct DIE. */
15724 if (cu->language == language_rust)
15725 read_func_scope (child_die, cu);
15726 else
15727 {
15728 /* C++ member function. */
15729 dwarf2_add_member_fn (fi, child_die, type, cu);
15730 }
15731 }
15732 else if (child_die->tag == DW_TAG_inheritance)
15733 {
15734 /* C++ base class field. */
15735 dwarf2_add_field (fi, child_die, cu);
15736 }
15737 else if (type_can_define_types (child_die))
15738 dwarf2_add_type_defn (fi, child_die, cu);
15739 else if (child_die->tag == DW_TAG_template_type_param
15740 || child_die->tag == DW_TAG_template_value_param)
15741 {
15742 struct symbol *arg = new_symbol (child_die, NULL, cu);
15743
15744 if (arg != NULL)
15745 template_args->push_back (arg);
15746 }
9c6a1327
TT
15747 else if (child_die->tag == DW_TAG_variant_part)
15748 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15749 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15750 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15751}
15752
c767944b
DJ
15753/* Finish creating a structure or union type, including filling in
15754 its members and creating a symbol for it. */
15755
15756static void
15757process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15758{
5e22e966 15759 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15760 struct die_info *child_die;
c767944b
DJ
15761 struct type *type;
15762
15763 type = get_die_type (die, cu);
15764 if (type == NULL)
15765 type = read_structure_type (die, cu);
15766
3e1d3d8c 15767 bool has_template_parameters = false;
e142c38c 15768 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15769 {
15770 struct field_info fi;
2f4732b0 15771 std::vector<struct symbol *> template_args;
c906108c 15772
639d11d3 15773 child_die = die->child;
c906108c
SS
15774
15775 while (child_die && child_die->tag)
15776 {
2ddeaf8a 15777 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15778 child_die = child_die->sibling;
c906108c
SS
15779 }
15780
34eaf542 15781 /* Attach template arguments to type. */
2f4732b0 15782 if (!template_args.empty ())
34eaf542 15783 {
3e1d3d8c 15784 has_template_parameters = true;
34eaf542 15785 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15786 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15787 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15788 = XOBNEWVEC (&objfile->objfile_obstack,
15789 struct symbol *,
15790 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15791 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15792 template_args.data (),
34eaf542
TT
15793 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15794 * sizeof (struct symbol *)));
34eaf542
TT
15795 }
15796
c906108c 15797 /* Attach fields and member functions to the type. */
317f7127 15798 if (fi.nfields () > 0)
e7c27a73 15799 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15800 if (!fi.fnfieldlists.empty ())
c906108c 15801 {
e7c27a73 15802 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15803
c5aa993b 15804 /* Get the type which refers to the base class (possibly this
c906108c 15805 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15806 class from the DW_AT_containing_type attribute. This use of
15807 DW_AT_containing_type is a GNU extension. */
c906108c 15808
e142c38c 15809 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15810 {
e7c27a73 15811 struct type *t = die_containing_type (die, cu);
c906108c 15812
ae6ae975 15813 set_type_vptr_basetype (type, t);
c906108c
SS
15814 if (type == t)
15815 {
c906108c
SS
15816 int i;
15817
15818 /* Our own class provides vtbl ptr. */
1f704f76 15819 for (i = t->num_fields () - 1;
c906108c
SS
15820 i >= TYPE_N_BASECLASSES (t);
15821 --i)
15822 {
0d5cff50 15823 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15824
1168df01 15825 if (is_vtable_name (fieldname, cu))
c906108c 15826 {
ae6ae975 15827 set_type_vptr_fieldno (type, i);
c906108c
SS
15828 break;
15829 }
15830 }
15831
15832 /* Complain if virtual function table field not found. */
15833 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15834 complaint (_("virtual function table pointer "
3e43a32a 15835 "not found when defining class '%s'"),
7d93a1e0 15836 type->name () ? type->name () : "");
c906108c
SS
15837 }
15838 else
15839 {
ae6ae975 15840 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15841 }
15842 }
f6235d4c 15843 else if (cu->producer
61012eef 15844 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15845 {
15846 /* The IBM XLC compiler does not provide direct indication
15847 of the containing type, but the vtable pointer is
15848 always named __vfp. */
15849
15850 int i;
15851
1f704f76 15852 for (i = type->num_fields () - 1;
f6235d4c
EZ
15853 i >= TYPE_N_BASECLASSES (type);
15854 --i)
15855 {
15856 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15857 {
ae6ae975
DE
15858 set_type_vptr_fieldno (type, i);
15859 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15860 break;
15861 }
15862 }
15863 }
c906108c 15864 }
98751a41
JK
15865
15866 /* Copy fi.typedef_field_list linked list elements content into the
15867 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15868 if (!fi.typedef_field_list.empty ())
98751a41 15869 {
be2daae6 15870 int count = fi.typedef_field_list.size ();
98751a41 15871
a0d7a4ff 15872 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15873 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15874 = ((struct decl_field *)
be2daae6
TT
15875 TYPE_ALLOC (type,
15876 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15877 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15878
be2daae6
TT
15879 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15880 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15881 }
c767944b 15882
883fd55a
KS
15883 /* Copy fi.nested_types_list linked list elements content into the
15884 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15885 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15886 {
be2daae6 15887 int count = fi.nested_types_list.size ();
883fd55a
KS
15888
15889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15890 TYPE_NESTED_TYPES_ARRAY (type)
15891 = ((struct decl_field *)
be2daae6
TT
15892 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15893 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15894
be2daae6
TT
15895 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15896 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15897 }
c906108c 15898 }
63d06c5c 15899
bb5ed363 15900 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15901 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15902 cu->rust_unions.push_back (type);
0b92b5bb 15903
90aeadfc
DC
15904 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15905 snapshots) has been known to create a die giving a declaration
15906 for a class that has, as a child, a die giving a definition for a
15907 nested class. So we have to process our children even if the
15908 current die is a declaration. Normally, of course, a declaration
15909 won't have any children at all. */
134d01f1 15910
ca040673
DE
15911 child_die = die->child;
15912
90aeadfc
DC
15913 while (child_die != NULL && child_die->tag)
15914 {
15915 if (child_die->tag == DW_TAG_member
15916 || child_die->tag == DW_TAG_variable
34eaf542
TT
15917 || child_die->tag == DW_TAG_inheritance
15918 || child_die->tag == DW_TAG_template_value_param
15919 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15920 {
90aeadfc 15921 /* Do nothing. */
134d01f1 15922 }
90aeadfc
DC
15923 else
15924 process_die (child_die, cu);
134d01f1 15925
436c571c 15926 child_die = child_die->sibling;
134d01f1
DJ
15927 }
15928
fa4028e9
JB
15929 /* Do not consider external references. According to the DWARF standard,
15930 these DIEs are identified by the fact that they have no byte_size
15931 attribute, and a declaration attribute. */
15932 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15933 || !die_is_declaration (die, cu)
15934 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15935 {
15936 struct symbol *sym = new_symbol (die, type, cu);
15937
15938 if (has_template_parameters)
15939 {
a776957c
TT
15940 struct symtab *symtab;
15941 if (sym != nullptr)
15942 symtab = symbol_symtab (sym);
15943 else if (cu->line_header != nullptr)
15944 {
15945 /* Any related symtab will do. */
15946 symtab
7ba99d21 15947 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15948 }
15949 else
15950 {
15951 symtab = nullptr;
15952 complaint (_("could not find suitable "
15953 "symtab for template parameter"
15954 " - DIE at %s [in module %s]"),
15955 sect_offset_str (die->sect_off),
15956 objfile_name (objfile));
15957 }
15958
15959 if (symtab != nullptr)
15960 {
15961 /* Make sure that the symtab is set on the new symbols.
15962 Even though they don't appear in this symtab directly,
15963 other parts of gdb assume that symbols do, and this is
15964 reasonably true. */
15965 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15966 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15967 }
3e1d3d8c
TT
15968 }
15969 }
134d01f1
DJ
15970}
15971
ed6acedd
TT
15972/* Assuming DIE is an enumeration type, and TYPE is its associated
15973 type, update TYPE using some information only available in DIE's
15974 children. In particular, the fields are computed. */
55426c9d
JB
15975
15976static void
15977update_enumeration_type_from_children (struct die_info *die,
15978 struct type *type,
15979 struct dwarf2_cu *cu)
15980{
60f7655a 15981 struct die_info *child_die;
55426c9d
JB
15982 int unsigned_enum = 1;
15983 int flag_enum = 1;
55426c9d 15984
8268c778 15985 auto_obstack obstack;
ed6acedd 15986 std::vector<struct field> fields;
55426c9d 15987
60f7655a
DE
15988 for (child_die = die->child;
15989 child_die != NULL && child_die->tag;
436c571c 15990 child_die = child_die->sibling)
55426c9d
JB
15991 {
15992 struct attribute *attr;
15993 LONGEST value;
15994 const gdb_byte *bytes;
15995 struct dwarf2_locexpr_baton *baton;
15996 const char *name;
60f7655a 15997
55426c9d
JB
15998 if (child_die->tag != DW_TAG_enumerator)
15999 continue;
16000
16001 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16002 if (attr == NULL)
16003 continue;
16004
16005 name = dwarf2_name (child_die, cu);
16006 if (name == NULL)
16007 name = "<anonymous enumerator>";
16008
16009 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16010 &value, &bytes, &baton);
16011 if (value < 0)
16012 {
16013 unsigned_enum = 0;
16014 flag_enum = 0;
16015 }
55426c9d 16016 else
edd45eb0
SM
16017 {
16018 if (count_one_bits_ll (value) >= 2)
16019 flag_enum = 0;
edd45eb0 16020 }
55426c9d 16021
ed6acedd
TT
16022 fields.emplace_back ();
16023 struct field &field = fields.back ();
16024 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16025 SET_FIELD_ENUMVAL (field, value);
16026 }
16027
16028 if (!fields.empty ())
16029 {
5e33d5f4 16030 type->set_num_fields (fields.size ());
3cabb6b0
SM
16031 type->set_fields
16032 ((struct field *)
16033 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16034 memcpy (type->fields (), fields.data (),
ed6acedd 16035 sizeof (struct field) * fields.size ());
55426c9d
JB
16036 }
16037
16038 if (unsigned_enum)
16039 TYPE_UNSIGNED (type) = 1;
16040 if (flag_enum)
16041 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16042}
16043
134d01f1
DJ
16044/* Given a DW_AT_enumeration_type die, set its type. We do not
16045 complete the type's fields yet, or create any symbols. */
c906108c 16046
f792889a 16047static struct type *
134d01f1 16048read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16049{
5e22e966 16050 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16051 struct type *type;
c906108c 16052 struct attribute *attr;
0114d602 16053 const char *name;
134d01f1 16054
348e048f
DE
16055 /* If the definition of this type lives in .debug_types, read that type.
16056 Don't follow DW_AT_specification though, that will take us back up
16057 the chain and we want to go down. */
052c8bb8 16058 attr = die->attr (DW_AT_signature);
435d3d88 16059 if (attr != nullptr)
348e048f 16060 {
ac9ec31b 16061 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16062
ac9ec31b 16063 /* The type's CU may not be the same as CU.
02142a6c 16064 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16065 return set_die_type (die, type, cu);
16066 }
16067
c906108c
SS
16068 type = alloc_type (objfile);
16069
67607e24 16070 type->set_code (TYPE_CODE_ENUM);
94af9270 16071 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16072 if (name != NULL)
d0e39ea2 16073 type->set_name (name);
c906108c 16074
0626fc76
TT
16075 attr = dwarf2_attr (die, DW_AT_type, cu);
16076 if (attr != NULL)
16077 {
16078 struct type *underlying_type = die_type (die, cu);
16079
16080 TYPE_TARGET_TYPE (type) = underlying_type;
16081 }
16082
e142c38c 16083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16084 if (attr != nullptr)
c906108c
SS
16085 {
16086 TYPE_LENGTH (type) = DW_UNSND (attr);
16087 }
16088 else
16089 {
16090 TYPE_LENGTH (type) = 0;
16091 }
16092
2b4424c3
TT
16093 maybe_set_alignment (cu, die, type);
16094
137033e9
JB
16095 /* The enumeration DIE can be incomplete. In Ada, any type can be
16096 declared as private in the package spec, and then defined only
16097 inside the package body. Such types are known as Taft Amendment
16098 Types. When another package uses such a type, an incomplete DIE
16099 may be generated by the compiler. */
02eb380e 16100 if (die_is_declaration (die, cu))
876cecd0 16101 TYPE_STUB (type) = 1;
02eb380e 16102
0626fc76
TT
16103 /* If this type has an underlying type that is not a stub, then we
16104 may use its attributes. We always use the "unsigned" attribute
16105 in this situation, because ordinarily we guess whether the type
16106 is unsigned -- but the guess can be wrong and the underlying type
16107 can tell us the reality. However, we defer to a local size
16108 attribute if one exists, because this lets the compiler override
16109 the underlying type if needed. */
16110 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16111 {
9e7c9a03
HD
16112 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16113 underlying_type = check_typedef (underlying_type);
16114 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16115 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16116 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16117 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16118 && TYPE_RAW_ALIGN (underlying_type) != 0)
16119 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16120 }
16121
3d567982
TT
16122 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16123
ed6acedd
TT
16124 set_die_type (die, type, cu);
16125
16126 /* Finish the creation of this type by using the enum's children.
16127 Note that, as usual, this must come after set_die_type to avoid
16128 infinite recursion when trying to compute the names of the
16129 enumerators. */
16130 update_enumeration_type_from_children (die, type, cu);
16131
16132 return type;
134d01f1
DJ
16133}
16134
16135/* Given a pointer to a die which begins an enumeration, process all
16136 the dies that define the members of the enumeration, and create the
16137 symbol for the enumeration type.
16138
16139 NOTE: We reverse the order of the element list. */
16140
16141static void
16142process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16143{
f792889a 16144 struct type *this_type;
134d01f1 16145
f792889a
DJ
16146 this_type = get_die_type (die, cu);
16147 if (this_type == NULL)
16148 this_type = read_enumeration_type (die, cu);
9dc481d3 16149
639d11d3 16150 if (die->child != NULL)
c906108c 16151 {
9dc481d3 16152 struct die_info *child_die;
15d034d0 16153 const char *name;
9dc481d3 16154
639d11d3 16155 child_die = die->child;
c906108c
SS
16156 while (child_die && child_die->tag)
16157 {
16158 if (child_die->tag != DW_TAG_enumerator)
16159 {
e7c27a73 16160 process_die (child_die, cu);
c906108c
SS
16161 }
16162 else
16163 {
39cbfefa
DJ
16164 name = dwarf2_name (child_die, cu);
16165 if (name)
ed6acedd 16166 new_symbol (child_die, this_type, cu);
c906108c
SS
16167 }
16168
436c571c 16169 child_die = child_die->sibling;
c906108c 16170 }
c906108c 16171 }
134d01f1 16172
6c83ed52
TT
16173 /* If we are reading an enum from a .debug_types unit, and the enum
16174 is a declaration, and the enum is not the signatured type in the
16175 unit, then we do not want to add a symbol for it. Adding a
16176 symbol would in some cases obscure the true definition of the
16177 enum, giving users an incomplete type when the definition is
16178 actually available. Note that we do not want to do this for all
16179 enums which are just declarations, because C++0x allows forward
16180 enum declarations. */
3019eac3 16181 if (cu->per_cu->is_debug_types
6c83ed52
TT
16182 && die_is_declaration (die, cu))
16183 {
52dc124a 16184 struct signatured_type *sig_type;
6c83ed52 16185
c0f78cd4 16186 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16187 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16188 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16189 return;
16190 }
16191
f792889a 16192 new_symbol (die, this_type, cu);
c906108c
SS
16193}
16194
16195/* Extract all information from a DW_TAG_array_type DIE and put it in
16196 the DIE's type field. For now, this only handles one dimensional
16197 arrays. */
16198
f792889a 16199static struct type *
e7c27a73 16200read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16201{
5e22e966 16202 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16203 struct die_info *child_die;
7e314c57 16204 struct type *type;
c906108c 16205 struct type *element_type, *range_type, *index_type;
c906108c 16206 struct attribute *attr;
15d034d0 16207 const char *name;
a405673c 16208 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16209 unsigned int bit_stride = 0;
c906108c 16210
e7c27a73 16211 element_type = die_type (die, cu);
c906108c 16212
7e314c57
JK
16213 /* The die_type call above may have already set the type for this DIE. */
16214 type = get_die_type (die, cu);
16215 if (type)
16216 return type;
16217
dc53a7ad
JB
16218 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16219 if (attr != NULL)
a405673c
JB
16220 {
16221 int stride_ok;
293e7e51 16222 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16223
16224 byte_stride_prop
16225 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16226 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16227 prop_type);
a405673c
JB
16228 if (!stride_ok)
16229 {
b98664d3 16230 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16231 " - DIE at %s [in module %s]"),
16232 sect_offset_str (die->sect_off),
5e22e966 16233 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16234 /* Ignore this attribute. We will likely not be able to print
16235 arrays of this type correctly, but there is little we can do
16236 to help if we cannot read the attribute's value. */
16237 byte_stride_prop = NULL;
16238 }
16239 }
dc53a7ad
JB
16240
16241 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16242 if (attr != NULL)
16243 bit_stride = DW_UNSND (attr);
16244
c906108c
SS
16245 /* Irix 6.2 native cc creates array types without children for
16246 arrays with unspecified length. */
639d11d3 16247 if (die->child == NULL)
c906108c 16248 {
46bf5051 16249 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16250 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16251 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16252 byte_stride_prop, bit_stride);
f792889a 16253 return set_die_type (die, type, cu);
c906108c
SS
16254 }
16255
791afaa2 16256 std::vector<struct type *> range_types;
639d11d3 16257 child_die = die->child;
c906108c
SS
16258 while (child_die && child_die->tag)
16259 {
16260 if (child_die->tag == DW_TAG_subrange_type)
16261 {
f792889a 16262 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16263
f792889a 16264 if (child_type != NULL)
a02abb62 16265 {
0963b4bd
MS
16266 /* The range type was succesfully read. Save it for the
16267 array type creation. */
791afaa2 16268 range_types.push_back (child_type);
a02abb62 16269 }
c906108c 16270 }
436c571c 16271 child_die = child_die->sibling;
c906108c
SS
16272 }
16273
16274 /* Dwarf2 dimensions are output from left to right, create the
16275 necessary array types in backwards order. */
7ca2d3a3 16276
c906108c 16277 type = element_type;
7ca2d3a3
DL
16278
16279 if (read_array_order (die, cu) == DW_ORD_col_major)
16280 {
16281 int i = 0;
9a619af0 16282
791afaa2 16283 while (i < range_types.size ())
dc53a7ad 16284 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16285 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16286 }
16287 else
16288 {
791afaa2 16289 size_t ndim = range_types.size ();
7ca2d3a3 16290 while (ndim-- > 0)
dc53a7ad 16291 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16292 byte_stride_prop, bit_stride);
7ca2d3a3 16293 }
c906108c 16294
f5f8a009
EZ
16295 /* Understand Dwarf2 support for vector types (like they occur on
16296 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16297 array type. This is not part of the Dwarf2/3 standard yet, but a
16298 custom vendor extension. The main difference between a regular
16299 array and the vector variant is that vectors are passed by value
16300 to functions. */
e142c38c 16301 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16302 if (attr != nullptr)
ea37ba09 16303 make_vector_type (type);
f5f8a009 16304
dbc98a8b
KW
16305 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16306 implementation may choose to implement triple vectors using this
16307 attribute. */
16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16309 if (attr != nullptr)
dbc98a8b
KW
16310 {
16311 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16312 TYPE_LENGTH (type) = DW_UNSND (attr);
16313 else
b98664d3 16314 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16315 "than the total size of elements"));
dbc98a8b
KW
16316 }
16317
39cbfefa
DJ
16318 name = dwarf2_name (die, cu);
16319 if (name)
d0e39ea2 16320 type->set_name (name);
6e70227d 16321
2b4424c3
TT
16322 maybe_set_alignment (cu, die, type);
16323
0963b4bd 16324 /* Install the type in the die. */
7e314c57
JK
16325 set_die_type (die, type, cu);
16326
16327 /* set_die_type should be already done. */
b4ba55a1
JB
16328 set_descriptive_type (type, die, cu);
16329
7e314c57 16330 return type;
c906108c
SS
16331}
16332
7ca2d3a3 16333static enum dwarf_array_dim_ordering
6e70227d 16334read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16335{
16336 struct attribute *attr;
16337
16338 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16339
435d3d88 16340 if (attr != nullptr)
aead7601 16341 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16342
0963b4bd
MS
16343 /* GNU F77 is a special case, as at 08/2004 array type info is the
16344 opposite order to the dwarf2 specification, but data is still
16345 laid out as per normal fortran.
7ca2d3a3 16346
0963b4bd
MS
16347 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16348 version checking. */
7ca2d3a3 16349
905e0470
PM
16350 if (cu->language == language_fortran
16351 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16352 {
16353 return DW_ORD_row_major;
16354 }
16355
6e70227d 16356 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16357 {
16358 case array_column_major:
16359 return DW_ORD_col_major;
16360 case array_row_major:
16361 default:
16362 return DW_ORD_row_major;
16363 };
16364}
16365
72019c9c 16366/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16367 the DIE's type field. */
72019c9c 16368
f792889a 16369static struct type *
72019c9c
GM
16370read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16371{
7e314c57
JK
16372 struct type *domain_type, *set_type;
16373 struct attribute *attr;
f792889a 16374
7e314c57
JK
16375 domain_type = die_type (die, cu);
16376
16377 /* The die_type call above may have already set the type for this DIE. */
16378 set_type = get_die_type (die, cu);
16379 if (set_type)
16380 return set_type;
16381
16382 set_type = create_set_type (NULL, domain_type);
16383
16384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16385 if (attr != nullptr)
d09039dd 16386 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16387
2b4424c3
TT
16388 maybe_set_alignment (cu, die, set_type);
16389
f792889a 16390 return set_die_type (die, set_type, cu);
72019c9c 16391}
7ca2d3a3 16392
0971de02
TT
16393/* A helper for read_common_block that creates a locexpr baton.
16394 SYM is the symbol which we are marking as computed.
16395 COMMON_DIE is the DIE for the common block.
16396 COMMON_LOC is the location expression attribute for the common
16397 block itself.
16398 MEMBER_LOC is the location expression attribute for the particular
16399 member of the common block that we are processing.
16400 CU is the CU from which the above come. */
16401
16402static void
16403mark_common_block_symbol_computed (struct symbol *sym,
16404 struct die_info *common_die,
16405 struct attribute *common_loc,
16406 struct attribute *member_loc,
16407 struct dwarf2_cu *cu)
16408{
5e22e966 16409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16410 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16411 struct dwarf2_locexpr_baton *baton;
16412 gdb_byte *ptr;
16413 unsigned int cu_off;
08feed99 16414 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16415 LONGEST offset = 0;
16416
16417 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16418 gdb_assert (common_loc->form_is_block ());
16419 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16420 || member_loc->form_is_constant ());
0971de02 16421
8d749320 16422 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16423 baton->per_objfile = per_objfile;
0971de02
TT
16424 baton->per_cu = cu->per_cu;
16425 gdb_assert (baton->per_cu);
16426
16427 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16428
cd6c91b4 16429 if (member_loc->form_is_constant ())
0971de02 16430 {
0826b30a 16431 offset = member_loc->constant_value (0);
0971de02
TT
16432 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16433 }
16434 else
16435 baton->size += DW_BLOCK (member_loc)->size;
16436
224c3ddb 16437 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16438 baton->data = ptr;
16439
16440 *ptr++ = DW_OP_call4;
9c541725 16441 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16442 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16443 ptr += 4;
16444
cd6c91b4 16445 if (member_loc->form_is_constant ())
0971de02
TT
16446 {
16447 *ptr++ = DW_OP_addr;
16448 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16449 ptr += cu->header.addr_size;
16450 }
16451 else
16452 {
16453 /* We have to copy the data here, because DW_OP_call4 will only
16454 use a DW_AT_location attribute. */
16455 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16456 ptr += DW_BLOCK (member_loc)->size;
16457 }
16458
16459 *ptr++ = DW_OP_plus;
16460 gdb_assert (ptr - baton->data == baton->size);
16461
0971de02 16462 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16463 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16464}
16465
4357ac6c
TT
16466/* Create appropriate locally-scoped variables for all the
16467 DW_TAG_common_block entries. Also create a struct common_block
16468 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16469 is used to separate the common blocks name namespace from regular
4357ac6c 16470 variable names. */
c906108c
SS
16471
16472static void
e7c27a73 16473read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16474{
0971de02
TT
16475 struct attribute *attr;
16476
16477 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16478 if (attr != nullptr)
0971de02
TT
16479 {
16480 /* Support the .debug_loc offsets. */
4fc6c0d5 16481 if (attr->form_is_block ())
0971de02
TT
16482 {
16483 /* Ok. */
16484 }
cd6c91b4 16485 else if (attr->form_is_section_offset ())
0971de02
TT
16486 {
16487 dwarf2_complex_location_expr_complaint ();
16488 attr = NULL;
16489 }
16490 else
16491 {
16492 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16493 "common block member");
16494 attr = NULL;
16495 }
16496 }
16497
639d11d3 16498 if (die->child != NULL)
c906108c 16499 {
5e22e966 16500 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16501 struct die_info *child_die;
16502 size_t n_entries = 0, size;
16503 struct common_block *common_block;
16504 struct symbol *sym;
74ac6d43 16505
4357ac6c
TT
16506 for (child_die = die->child;
16507 child_die && child_die->tag;
436c571c 16508 child_die = child_die->sibling)
4357ac6c
TT
16509 ++n_entries;
16510
16511 size = (sizeof (struct common_block)
16512 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16513 common_block
16514 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16515 size);
4357ac6c
TT
16516 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16517 common_block->n_entries = 0;
16518
16519 for (child_die = die->child;
16520 child_die && child_die->tag;
436c571c 16521 child_die = child_die->sibling)
4357ac6c
TT
16522 {
16523 /* Create the symbol in the DW_TAG_common_block block in the current
16524 symbol scope. */
e7c27a73 16525 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16526 if (sym != NULL)
16527 {
16528 struct attribute *member_loc;
16529
16530 common_block->contents[common_block->n_entries++] = sym;
16531
16532 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16533 cu);
16534 if (member_loc)
16535 {
16536 /* GDB has handled this for a long time, but it is
16537 not specified by DWARF. It seems to have been
16538 emitted by gfortran at least as recently as:
16539 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16540 complaint (_("Variable in common block has "
0971de02 16541 "DW_AT_data_member_location "
9d8780f0
SM
16542 "- DIE at %s [in module %s]"),
16543 sect_offset_str (child_die->sect_off),
518817b3 16544 objfile_name (objfile));
0971de02 16545
cd6c91b4 16546 if (member_loc->form_is_section_offset ())
0971de02 16547 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16548 else if (member_loc->form_is_constant ()
4fc6c0d5 16549 || member_loc->form_is_block ())
0971de02 16550 {
435d3d88 16551 if (attr != nullptr)
0971de02
TT
16552 mark_common_block_symbol_computed (sym, die, attr,
16553 member_loc, cu);
16554 }
16555 else
16556 dwarf2_complex_location_expr_complaint ();
16557 }
16558 }
c906108c 16559 }
4357ac6c
TT
16560
16561 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16562 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16563 }
16564}
16565
0114d602 16566/* Create a type for a C++ namespace. */
d9fa45fe 16567
0114d602
DJ
16568static struct type *
16569read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16570{
5e22e966 16571 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16572 const char *previous_prefix, *name;
9219021c 16573 int is_anonymous;
0114d602
DJ
16574 struct type *type;
16575
16576 /* For extensions, reuse the type of the original namespace. */
16577 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16578 {
16579 struct die_info *ext_die;
16580 struct dwarf2_cu *ext_cu = cu;
9a619af0 16581
0114d602
DJ
16582 ext_die = dwarf2_extension (die, &ext_cu);
16583 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16584
16585 /* EXT_CU may not be the same as CU.
02142a6c 16586 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16587 return set_die_type (die, type, cu);
16588 }
9219021c 16589
e142c38c 16590 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16591
16592 /* Now build the name of the current namespace. */
16593
0114d602
DJ
16594 previous_prefix = determine_prefix (die, cu);
16595 if (previous_prefix[0] != '\0')
16596 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16597 previous_prefix, name, 0, cu);
0114d602
DJ
16598
16599 /* Create the type. */
19f392bc 16600 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16601
60531b24 16602 return set_die_type (die, type, cu);
0114d602
DJ
16603}
16604
22cee43f 16605/* Read a namespace scope. */
0114d602
DJ
16606
16607static void
16608read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16609{
5e22e966 16610 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16611 int is_anonymous;
9219021c 16612
5c4e30ca
DC
16613 /* Add a symbol associated to this if we haven't seen the namespace
16614 before. Also, add a using directive if it's an anonymous
16615 namespace. */
9219021c 16616
f2f0e013 16617 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16618 {
16619 struct type *type;
16620
0114d602 16621 type = read_type_die (die, cu);
e7c27a73 16622 new_symbol (die, type, cu);
5c4e30ca 16623
e8e80198 16624 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16625 if (is_anonymous)
0114d602
DJ
16626 {
16627 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16628
eb1e02fd 16629 std::vector<const char *> excludes;
804d2729 16630 add_using_directive (using_directives (cu),
7d93a1e0 16631 previous_prefix, type->name (), NULL,
eb1e02fd 16632 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16633 }
5c4e30ca 16634 }
9219021c 16635
639d11d3 16636 if (die->child != NULL)
d9fa45fe 16637 {
639d11d3 16638 struct die_info *child_die = die->child;
6e70227d 16639
d9fa45fe
DC
16640 while (child_die && child_die->tag)
16641 {
e7c27a73 16642 process_die (child_die, cu);
436c571c 16643 child_die = child_die->sibling;
d9fa45fe
DC
16644 }
16645 }
38d518c9
EZ
16646}
16647
f55ee35c
JK
16648/* Read a Fortran module as type. This DIE can be only a declaration used for
16649 imported module. Still we need that type as local Fortran "use ... only"
16650 declaration imports depend on the created type in determine_prefix. */
16651
16652static struct type *
16653read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16654{
5e22e966 16655 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16656 const char *module_name;
f55ee35c
JK
16657 struct type *type;
16658
16659 module_name = dwarf2_name (die, cu);
19f392bc 16660 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16661
f55ee35c
JK
16662 return set_die_type (die, type, cu);
16663}
16664
5d7cb8df
JK
16665/* Read a Fortran module. */
16666
16667static void
16668read_module (struct die_info *die, struct dwarf2_cu *cu)
16669{
16670 struct die_info *child_die = die->child;
530e8392
KB
16671 struct type *type;
16672
16673 type = read_type_die (die, cu);
16674 new_symbol (die, type, cu);
5d7cb8df 16675
5d7cb8df
JK
16676 while (child_die && child_die->tag)
16677 {
16678 process_die (child_die, cu);
436c571c 16679 child_die = child_die->sibling;
5d7cb8df
JK
16680 }
16681}
16682
38d518c9
EZ
16683/* Return the name of the namespace represented by DIE. Set
16684 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16685 namespace. */
16686
16687static const char *
e142c38c 16688namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16689{
16690 struct die_info *current_die;
16691 const char *name = NULL;
16692
16693 /* Loop through the extensions until we find a name. */
16694
16695 for (current_die = die;
16696 current_die != NULL;
f2f0e013 16697 current_die = dwarf2_extension (die, &cu))
38d518c9 16698 {
96553a0c
DE
16699 /* We don't use dwarf2_name here so that we can detect the absence
16700 of a name -> anonymous namespace. */
7d45c7c3 16701 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16702
38d518c9
EZ
16703 if (name != NULL)
16704 break;
16705 }
16706
16707 /* Is it an anonymous namespace? */
16708
16709 *is_anonymous = (name == NULL);
16710 if (*is_anonymous)
2b1dbab0 16711 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16712
16713 return name;
d9fa45fe
DC
16714}
16715
c906108c
SS
16716/* Extract all information from a DW_TAG_pointer_type DIE and add to
16717 the user defined type vector. */
16718
f792889a 16719static struct type *
e7c27a73 16720read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16721{
5e22e966 16722 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16723 struct comp_unit_head *cu_header = &cu->header;
c906108c 16724 struct type *type;
8b2dbe47
KB
16725 struct attribute *attr_byte_size;
16726 struct attribute *attr_address_class;
16727 int byte_size, addr_class;
7e314c57
JK
16728 struct type *target_type;
16729
16730 target_type = die_type (die, cu);
c906108c 16731
7e314c57
JK
16732 /* The die_type call above may have already set the type for this DIE. */
16733 type = get_die_type (die, cu);
16734 if (type)
16735 return type;
16736
16737 type = lookup_pointer_type (target_type);
8b2dbe47 16738
e142c38c 16739 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16740 if (attr_byte_size)
16741 byte_size = DW_UNSND (attr_byte_size);
c906108c 16742 else
8b2dbe47
KB
16743 byte_size = cu_header->addr_size;
16744
e142c38c 16745 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16746 if (attr_address_class)
16747 addr_class = DW_UNSND (attr_address_class);
16748 else
16749 addr_class = DW_ADDR_none;
16750
2b4424c3
TT
16751 ULONGEST alignment = get_alignment (cu, die);
16752
16753 /* If the pointer size, alignment, or address class is different
16754 than the default, create a type variant marked as such and set
16755 the length accordingly. */
16756 if (TYPE_LENGTH (type) != byte_size
16757 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16758 && alignment != TYPE_RAW_ALIGN (type))
16759 || addr_class != DW_ADDR_none)
c906108c 16760 {
5e2b427d 16761 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16762 {
16763 int type_flags;
16764
849957d9 16765 type_flags = gdbarch_address_class_type_flags
5e2b427d 16766 (gdbarch, byte_size, addr_class);
876cecd0
TT
16767 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16768 == 0);
8b2dbe47
KB
16769 type = make_type_with_address_space (type, type_flags);
16770 }
16771 else if (TYPE_LENGTH (type) != byte_size)
16772 {
b98664d3 16773 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16774 }
2b4424c3
TT
16775 else if (TYPE_RAW_ALIGN (type) != alignment)
16776 {
b98664d3 16777 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16778 " - DIE at %s [in module %s]"),
16779 sect_offset_str (die->sect_off),
5e22e966 16780 objfile_name (cu->per_objfile->objfile));
2b4424c3 16781 }
6e70227d 16782 else
9a619af0
MS
16783 {
16784 /* Should we also complain about unhandled address classes? */
16785 }
c906108c 16786 }
8b2dbe47
KB
16787
16788 TYPE_LENGTH (type) = byte_size;
2b4424c3 16789 set_type_align (type, alignment);
f792889a 16790 return set_die_type (die, type, cu);
c906108c
SS
16791}
16792
16793/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16794 the user defined type vector. */
16795
f792889a 16796static struct type *
e7c27a73 16797read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16798{
16799 struct type *type;
16800 struct type *to_type;
16801 struct type *domain;
16802
e7c27a73
DJ
16803 to_type = die_type (die, cu);
16804 domain = die_containing_type (die, cu);
0d5de010 16805
7e314c57
JK
16806 /* The calls above may have already set the type for this DIE. */
16807 type = get_die_type (die, cu);
16808 if (type)
16809 return type;
16810
78134374 16811 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16812 type = lookup_methodptr_type (to_type);
78134374 16813 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16814 {
5e22e966 16815 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16816
16817 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16818 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16819 TYPE_VARARGS (to_type));
16820 type = lookup_methodptr_type (new_type);
16821 }
0d5de010
DJ
16822 else
16823 type = lookup_memberptr_type (to_type, domain);
c906108c 16824
f792889a 16825 return set_die_type (die, type, cu);
c906108c
SS
16826}
16827
4297a3f0 16828/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16829 the user defined type vector. */
16830
f792889a 16831static struct type *
4297a3f0
AV
16832read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16833 enum type_code refcode)
c906108c 16834{
e7c27a73 16835 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16836 struct type *type, *target_type;
c906108c
SS
16837 struct attribute *attr;
16838
4297a3f0
AV
16839 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16840
7e314c57
JK
16841 target_type = die_type (die, cu);
16842
16843 /* The die_type call above may have already set the type for this DIE. */
16844 type = get_die_type (die, cu);
16845 if (type)
16846 return type;
16847
4297a3f0 16848 type = lookup_reference_type (target_type, refcode);
e142c38c 16849 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16850 if (attr != nullptr)
c906108c
SS
16851 {
16852 TYPE_LENGTH (type) = DW_UNSND (attr);
16853 }
16854 else
16855 {
107d2387 16856 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16857 }
2b4424c3 16858 maybe_set_alignment (cu, die, type);
f792889a 16859 return set_die_type (die, type, cu);
c906108c
SS
16860}
16861
cf363f18
MW
16862/* Add the given cv-qualifiers to the element type of the array. GCC
16863 outputs DWARF type qualifiers that apply to an array, not the
16864 element type. But GDB relies on the array element type to carry
16865 the cv-qualifiers. This mimics section 6.7.3 of the C99
16866 specification. */
16867
16868static struct type *
16869add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16870 struct type *base_type, int cnst, int voltl)
16871{
16872 struct type *el_type, *inner_array;
16873
16874 base_type = copy_type (base_type);
16875 inner_array = base_type;
16876
78134374 16877 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16878 {
16879 TYPE_TARGET_TYPE (inner_array) =
16880 copy_type (TYPE_TARGET_TYPE (inner_array));
16881 inner_array = TYPE_TARGET_TYPE (inner_array);
16882 }
16883
16884 el_type = TYPE_TARGET_TYPE (inner_array);
16885 cnst |= TYPE_CONST (el_type);
16886 voltl |= TYPE_VOLATILE (el_type);
16887 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16888
16889 return set_die_type (die, base_type, cu);
16890}
16891
f792889a 16892static struct type *
e7c27a73 16893read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16894{
f792889a 16895 struct type *base_type, *cv_type;
c906108c 16896
e7c27a73 16897 base_type = die_type (die, cu);
7e314c57
JK
16898
16899 /* The die_type call above may have already set the type for this DIE. */
16900 cv_type = get_die_type (die, cu);
16901 if (cv_type)
16902 return cv_type;
16903
2f608a3a
KW
16904 /* In case the const qualifier is applied to an array type, the element type
16905 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16906 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16907 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16908
f792889a
DJ
16909 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16910 return set_die_type (die, cv_type, cu);
c906108c
SS
16911}
16912
f792889a 16913static struct type *
e7c27a73 16914read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16915{
f792889a 16916 struct type *base_type, *cv_type;
c906108c 16917
e7c27a73 16918 base_type = die_type (die, cu);
7e314c57
JK
16919
16920 /* The die_type call above may have already set the type for this DIE. */
16921 cv_type = get_die_type (die, cu);
16922 if (cv_type)
16923 return cv_type;
16924
cf363f18
MW
16925 /* In case the volatile qualifier is applied to an array type, the
16926 element type is so qualified, not the array type (section 6.7.3
16927 of C99). */
78134374 16928 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16929 return add_array_cv_type (die, cu, base_type, 0, 1);
16930
f792889a
DJ
16931 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16932 return set_die_type (die, cv_type, cu);
c906108c
SS
16933}
16934
06d66ee9
TT
16935/* Handle DW_TAG_restrict_type. */
16936
16937static struct type *
16938read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16939{
16940 struct type *base_type, *cv_type;
16941
16942 base_type = die_type (die, cu);
16943
16944 /* The die_type call above may have already set the type for this DIE. */
16945 cv_type = get_die_type (die, cu);
16946 if (cv_type)
16947 return cv_type;
16948
16949 cv_type = make_restrict_type (base_type);
16950 return set_die_type (die, cv_type, cu);
16951}
16952
a2c2acaf
MW
16953/* Handle DW_TAG_atomic_type. */
16954
16955static struct type *
16956read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16957{
16958 struct type *base_type, *cv_type;
16959
16960 base_type = die_type (die, cu);
16961
16962 /* The die_type call above may have already set the type for this DIE. */
16963 cv_type = get_die_type (die, cu);
16964 if (cv_type)
16965 return cv_type;
16966
16967 cv_type = make_atomic_type (base_type);
16968 return set_die_type (die, cv_type, cu);
16969}
16970
c906108c
SS
16971/* Extract all information from a DW_TAG_string_type DIE and add to
16972 the user defined type vector. It isn't really a user defined type,
16973 but it behaves like one, with other DIE's using an AT_user_def_type
16974 attribute to reference it. */
16975
f792889a 16976static struct type *
e7c27a73 16977read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16978{
5e22e966 16979 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16980 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16981 struct type *type, *range_type, *index_type, *char_type;
16982 struct attribute *attr;
216a7e6b
AB
16983 struct dynamic_prop prop;
16984 bool length_is_constant = true;
16985 LONGEST length;
16986
16987 /* There are a couple of places where bit sizes might be made use of
16988 when parsing a DW_TAG_string_type, however, no producer that we know
16989 of make use of these. Handling bit sizes that are a multiple of the
16990 byte size is easy enough, but what about other bit sizes? Lets deal
16991 with that problem when we have to. Warn about these attributes being
16992 unsupported, then parse the type and ignore them like we always
16993 have. */
16994 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16995 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16996 {
16997 static bool warning_printed = false;
16998 if (!warning_printed)
16999 {
17000 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17001 "currently supported on DW_TAG_string_type."));
17002 warning_printed = true;
17003 }
17004 }
c906108c 17005
e142c38c 17006 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17007 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17008 {
17009 /* The string length describes the location at which the length of
17010 the string can be found. The size of the length field can be
17011 specified with one of the attributes below. */
17012 struct type *prop_type;
17013 struct attribute *len
17014 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17015 if (len == nullptr)
17016 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17017 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17018 {
17019 /* Pass 0 as the default as we know this attribute is constant
17020 and the default value will not be returned. */
0826b30a 17021 LONGEST sz = len->constant_value (0);
293e7e51 17022 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17023 }
17024 else
17025 {
17026 /* If the size is not specified then we assume it is the size of
17027 an address on this target. */
293e7e51 17028 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17029 }
17030
17031 /* Convert the attribute into a dynamic property. */
17032 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17033 length = 1;
17034 else
17035 length_is_constant = false;
17036 }
17037 else if (attr != nullptr)
17038 {
17039 /* This DW_AT_string_length just contains the length with no
17040 indirection. There's no need to create a dynamic property in this
17041 case. Pass 0 for the default value as we know it will not be
17042 returned in this case. */
0826b30a 17043 length = attr->constant_value (0);
216a7e6b
AB
17044 }
17045 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17046 {
216a7e6b 17047 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17048 length = attr->constant_value (1);
c906108c
SS
17049 }
17050 else
17051 {
216a7e6b
AB
17052 /* Use 1 as a fallback length if we have nothing else. */
17053 length = 1;
c906108c 17054 }
6ccb9162 17055
46bf5051 17056 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17057 if (length_is_constant)
17058 range_type = create_static_range_type (NULL, index_type, 1, length);
17059 else
17060 {
17061 struct dynamic_prop low_bound;
17062
17063 low_bound.kind = PROP_CONST;
17064 low_bound.data.const_val = 1;
17065 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17066 }
3b7538c0
UW
17067 char_type = language_string_char_type (cu->language_defn, gdbarch);
17068 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17069
f792889a 17070 return set_die_type (die, type, cu);
c906108c
SS
17071}
17072
4d804846
JB
17073/* Assuming that DIE corresponds to a function, returns nonzero
17074 if the function is prototyped. */
17075
17076static int
17077prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17078{
17079 struct attribute *attr;
17080
17081 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17082 if (attr && (DW_UNSND (attr) != 0))
17083 return 1;
17084
17085 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17086 is only meaningful for C, but the concept also extends to other
4d804846
JB
17087 languages that allow unprototyped functions (Eg: Objective C).
17088 For all other languages, assume that functions are always
17089 prototyped. */
17090 if (cu->language != language_c
17091 && cu->language != language_objc
17092 && cu->language != language_opencl)
17093 return 1;
17094
17095 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17096 prototyped and unprototyped functions; default to prototyped,
17097 since that is more common in modern code (and RealView warns
17098 about unprototyped functions). */
17099 if (producer_is_realview (cu->producer))
17100 return 1;
17101
17102 return 0;
17103}
17104
c906108c
SS
17105/* Handle DIES due to C code like:
17106
17107 struct foo
c5aa993b
JM
17108 {
17109 int (*funcp)(int a, long l);
17110 int b;
17111 };
c906108c 17112
0963b4bd 17113 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17114
f792889a 17115static struct type *
e7c27a73 17116read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17117{
5e22e966 17118 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17119 struct type *type; /* Type that this function returns. */
17120 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17121 struct attribute *attr;
17122
e7c27a73 17123 type = die_type (die, cu);
7e314c57
JK
17124
17125 /* The die_type call above may have already set the type for this DIE. */
17126 ftype = get_die_type (die, cu);
17127 if (ftype)
17128 return ftype;
17129
0c8b41f1 17130 ftype = lookup_function_type (type);
c906108c 17131
4d804846 17132 if (prototyped_function_p (die, cu))
a6c727b2 17133 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17134
c055b101
CV
17135 /* Store the calling convention in the type if it's available in
17136 the subroutine die. Otherwise set the calling convention to
17137 the default value DW_CC_normal. */
17138 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17139 if (attr != nullptr
17140 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17141 TYPE_CALLING_CONVENTION (ftype)
17142 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17143 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17144 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17145 else
17146 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17147
743649fd
MW
17148 /* Record whether the function returns normally to its caller or not
17149 if the DWARF producer set that information. */
17150 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17151 if (attr && (DW_UNSND (attr) != 0))
17152 TYPE_NO_RETURN (ftype) = 1;
17153
76c10ea2
GM
17154 /* We need to add the subroutine type to the die immediately so
17155 we don't infinitely recurse when dealing with parameters
0963b4bd 17156 declared as the same subroutine type. */
76c10ea2 17157 set_die_type (die, ftype, cu);
6e70227d 17158
639d11d3 17159 if (die->child != NULL)
c906108c 17160 {
bb5ed363 17161 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17162 struct die_info *child_die;
8072405b 17163 int nparams, iparams;
c906108c
SS
17164
17165 /* Count the number of parameters.
17166 FIXME: GDB currently ignores vararg functions, but knows about
17167 vararg member functions. */
8072405b 17168 nparams = 0;
639d11d3 17169 child_die = die->child;
c906108c
SS
17170 while (child_die && child_die->tag)
17171 {
17172 if (child_die->tag == DW_TAG_formal_parameter)
17173 nparams++;
17174 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17175 TYPE_VARARGS (ftype) = 1;
436c571c 17176 child_die = child_die->sibling;
c906108c
SS
17177 }
17178
17179 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17180 ftype->set_num_fields (nparams);
3cabb6b0
SM
17181 ftype->set_fields
17182 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17183
8072405b
JK
17184 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17185 even if we error out during the parameters reading below. */
17186 for (iparams = 0; iparams < nparams; iparams++)
17187 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17188
17189 iparams = 0;
639d11d3 17190 child_die = die->child;
c906108c
SS
17191 while (child_die && child_die->tag)
17192 {
17193 if (child_die->tag == DW_TAG_formal_parameter)
17194 {
3ce3b1ba
PA
17195 struct type *arg_type;
17196
17197 /* DWARF version 2 has no clean way to discern C++
17198 static and non-static member functions. G++ helps
17199 GDB by marking the first parameter for non-static
17200 member functions (which is the this pointer) as
17201 artificial. We pass this information to
17202 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17203
17204 DWARF version 3 added DW_AT_object_pointer, which GCC
17205 4.5 does not yet generate. */
e142c38c 17206 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17207 if (attr != nullptr)
c906108c
SS
17208 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17209 else
9c37b5ae 17210 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17211 arg_type = die_type (child_die, cu);
17212
17213 /* RealView does not mark THIS as const, which the testsuite
17214 expects. GCC marks THIS as const in method definitions,
17215 but not in the class specifications (GCC PR 43053). */
17216 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17217 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17218 {
17219 int is_this = 0;
17220 struct dwarf2_cu *arg_cu = cu;
17221 const char *name = dwarf2_name (child_die, cu);
17222
17223 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17224 if (attr != nullptr)
3ce3b1ba
PA
17225 {
17226 /* If the compiler emits this, use it. */
17227 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17228 is_this = 1;
17229 }
17230 else if (name && strcmp (name, "this") == 0)
17231 /* Function definitions will have the argument names. */
17232 is_this = 1;
17233 else if (name == NULL && iparams == 0)
17234 /* Declarations may not have the names, so like
17235 elsewhere in GDB, assume an artificial first
17236 argument is "this". */
17237 is_this = 1;
17238
17239 if (is_this)
17240 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17241 arg_type, 0);
17242 }
17243
17244 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17245 iparams++;
17246 }
436c571c 17247 child_die = child_die->sibling;
c906108c
SS
17248 }
17249 }
17250
76c10ea2 17251 return ftype;
c906108c
SS
17252}
17253
f792889a 17254static struct type *
e7c27a73 17255read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17256{
5e22e966 17257 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17258 const char *name = NULL;
3c8e0968 17259 struct type *this_type, *target_type;
c906108c 17260
94af9270 17261 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17262 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17263 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17264 set_die_type (die, this_type, cu);
3c8e0968
DE
17265 target_type = die_type (die, cu);
17266 if (target_type != this_type)
17267 TYPE_TARGET_TYPE (this_type) = target_type;
17268 else
17269 {
17270 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17271 spec and cause infinite loops in GDB. */
b98664d3 17272 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17273 "- DIE at %s [in module %s]"),
17274 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17275 TYPE_TARGET_TYPE (this_type) = NULL;
17276 }
e4003a34
TV
17277 if (name == NULL)
17278 {
17279 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17280 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17281 Handle these by just returning the target type, rather than
17282 constructing an anonymous typedef type and trying to handle this
17283 elsewhere. */
17284 set_die_type (die, target_type, cu);
17285 return target_type;
17286 }
f792889a 17287 return this_type;
c906108c
SS
17288}
17289
9b790ce7
UW
17290/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17291 (which may be different from NAME) to the architecture back-end to allow
17292 it to guess the correct format if necessary. */
17293
17294static struct type *
17295dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17296 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17297{
08feed99 17298 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17299 const struct floatformat **format;
17300 struct type *type;
17301
17302 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17303 if (format)
103a685e 17304 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17305 else
77b7c781 17306 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17307
17308 return type;
17309}
17310
eb77c9df
AB
17311/* Allocate an integer type of size BITS and name NAME. */
17312
17313static struct type *
17314dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17315 int bits, int unsigned_p, const char *name)
17316{
17317 struct type *type;
17318
17319 /* Versions of Intel's C Compiler generate an integer type called "void"
17320 instead of using DW_TAG_unspecified_type. This has been seen on
17321 at least versions 14, 17, and 18. */
35ee2dc2
AB
17322 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17323 && strcmp (name, "void") == 0)
eb77c9df
AB
17324 type = objfile_type (objfile)->builtin_void;
17325 else
17326 type = init_integer_type (objfile, bits, unsigned_p, name);
17327
17328 return type;
17329}
17330
8bdc1658
AB
17331/* Initialise and return a floating point type of size BITS suitable for
17332 use as a component of a complex number. The NAME_HINT is passed through
17333 when initialising the floating point type and is the name of the complex
17334 type.
17335
17336 As DWARF doesn't currently provide an explicit name for the components
17337 of a complex number, but it can be helpful to have these components
17338 named, we try to select a suitable name based on the size of the
17339 component. */
17340static struct type *
17341dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17342 struct objfile *objfile,
103a685e
TT
17343 int bits, const char *name_hint,
17344 enum bfd_endian byte_order)
8bdc1658 17345{
08feed99 17346 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17347 struct type *tt = nullptr;
17348
35add35e
AB
17349 /* Try to find a suitable floating point builtin type of size BITS.
17350 We're going to use the name of this type as the name for the complex
17351 target type that we are about to create. */
1db455a7 17352 switch (cu->language)
8bdc1658 17353 {
1db455a7
AB
17354 case language_fortran:
17355 switch (bits)
17356 {
17357 case 32:
17358 tt = builtin_f_type (gdbarch)->builtin_real;
17359 break;
17360 case 64:
17361 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17362 break;
17363 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17364 case 128:
17365 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17366 break;
17367 }
8bdc1658 17368 break;
1db455a7
AB
17369 default:
17370 switch (bits)
17371 {
17372 case 32:
17373 tt = builtin_type (gdbarch)->builtin_float;
17374 break;
17375 case 64:
17376 tt = builtin_type (gdbarch)->builtin_double;
17377 break;
17378 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17379 case 128:
17380 tt = builtin_type (gdbarch)->builtin_long_double;
17381 break;
17382 }
8bdc1658
AB
17383 break;
17384 }
17385
35add35e
AB
17386 /* If the type we found doesn't match the size we were looking for, then
17387 pretend we didn't find a type at all, the complex target type we
17388 create will then be nameless. */
a12e5744 17389 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17390 tt = nullptr;
17391
7d93a1e0 17392 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17393 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17394}
17395
c906108c
SS
17396/* Find a representation of a given base type and install
17397 it in the TYPE field of the die. */
17398
f792889a 17399static struct type *
e7c27a73 17400read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17401{
5e22e966 17402 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17403 struct type *type;
17404 struct attribute *attr;
19f392bc 17405 int encoding = 0, bits = 0;
15d034d0 17406 const char *name;
34877895 17407 gdbarch *arch;
c906108c 17408
e142c38c 17409 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17410 if (attr != nullptr)
34877895 17411 encoding = DW_UNSND (attr);
e142c38c 17412 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17413 if (attr != nullptr)
34877895 17414 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17415 name = dwarf2_name (die, cu);
6ccb9162 17416 if (!name)
34877895 17417 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17418
08feed99 17419 arch = objfile->arch ();
103a685e
TT
17420 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17421
34877895
PJ
17422 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17423 if (attr)
103a685e
TT
17424 {
17425 int endianity = DW_UNSND (attr);
17426
17427 switch (endianity)
17428 {
17429 case DW_END_big:
17430 byte_order = BFD_ENDIAN_BIG;
17431 break;
17432 case DW_END_little:
17433 byte_order = BFD_ENDIAN_LITTLE;
17434 break;
17435 default:
17436 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17437 break;
17438 }
17439 }
6ccb9162
UW
17440
17441 switch (encoding)
c906108c 17442 {
6ccb9162
UW
17443 case DW_ATE_address:
17444 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17445 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17446 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17447 break;
17448 case DW_ATE_boolean:
19f392bc 17449 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17450 break;
17451 case DW_ATE_complex_float:
103a685e
TT
17452 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17453 byte_order);
78134374 17454 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17455 {
17456 if (name == nullptr)
17457 {
17458 struct obstack *obstack
5e22e966 17459 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17460 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17461 nullptr);
17462 }
17463 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17464 }
17465 else
17466 type = init_complex_type (name, type);
6ccb9162
UW
17467 break;
17468 case DW_ATE_decimal_float:
19f392bc 17469 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17470 break;
17471 case DW_ATE_float:
103a685e 17472 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17473 break;
17474 case DW_ATE_signed:
eb77c9df 17475 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17476 break;
17477 case DW_ATE_unsigned:
3b2b8fea
TT
17478 if (cu->language == language_fortran
17479 && name
61012eef 17480 && startswith (name, "character("))
19f392bc
UW
17481 type = init_character_type (objfile, bits, 1, name);
17482 else
eb77c9df 17483 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17484 break;
17485 case DW_ATE_signed_char:
6e70227d 17486 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17487 || cu->language == language_pascal
17488 || cu->language == language_fortran)
19f392bc
UW
17489 type = init_character_type (objfile, bits, 0, name);
17490 else
eb77c9df 17491 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17492 break;
17493 case DW_ATE_unsigned_char:
868a0084 17494 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17495 || cu->language == language_pascal
c44af4eb
TT
17496 || cu->language == language_fortran
17497 || cu->language == language_rust)
19f392bc
UW
17498 type = init_character_type (objfile, bits, 1, name);
17499 else
eb77c9df 17500 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17501 break;
75079b2b 17502 case DW_ATE_UTF:
53e710ac 17503 {
53e710ac
PA
17504 if (bits == 16)
17505 type = builtin_type (arch)->builtin_char16;
17506 else if (bits == 32)
17507 type = builtin_type (arch)->builtin_char32;
17508 else
17509 {
b98664d3 17510 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17511 bits);
eb77c9df 17512 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17513 }
17514 return set_die_type (die, type, cu);
17515 }
75079b2b
TT
17516 break;
17517
6ccb9162 17518 default:
b98664d3 17519 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17520 dwarf_type_encoding_name (encoding));
77b7c781 17521 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17522 break;
c906108c 17523 }
6ccb9162 17524
0114d602 17525 if (name && strcmp (name, "char") == 0)
876cecd0 17526 TYPE_NOSIGN (type) = 1;
0114d602 17527
2b4424c3
TT
17528 maybe_set_alignment (cu, die, type);
17529
103a685e 17530 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17531
f792889a 17532 return set_die_type (die, type, cu);
c906108c
SS
17533}
17534
80180f79
SA
17535/* Parse dwarf attribute if it's a block, reference or constant and put the
17536 resulting value of the attribute into struct bound_prop.
17537 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17538
17539static int
17540attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17541 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17542 struct type *default_type)
80180f79
SA
17543{
17544 struct dwarf2_property_baton *baton;
5e22e966 17545 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17546 struct objfile *objfile = per_objfile->objfile;
17547 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17548
9a49df9d
AB
17549 gdb_assert (default_type != NULL);
17550
80180f79
SA
17551 if (attr == NULL || prop == NULL)
17552 return 0;
17553
4fc6c0d5 17554 if (attr->form_is_block ())
80180f79 17555 {
8d749320 17556 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17557 baton->property_type = default_type;
80180f79 17558 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17559 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17560 baton->locexpr.size = DW_BLOCK (attr)->size;
17561 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17562 switch (attr->name)
17563 {
17564 case DW_AT_string_length:
17565 baton->locexpr.is_reference = true;
17566 break;
17567 default:
17568 baton->locexpr.is_reference = false;
17569 break;
17570 }
80180f79
SA
17571 prop->data.baton = baton;
17572 prop->kind = PROP_LOCEXPR;
17573 gdb_assert (prop->data.baton != NULL);
17574 }
cd6c91b4 17575 else if (attr->form_is_ref ())
80180f79
SA
17576 {
17577 struct dwarf2_cu *target_cu = cu;
17578 struct die_info *target_die;
17579 struct attribute *target_attr;
17580
17581 target_die = follow_die_ref (die, attr, &target_cu);
17582 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17583 if (target_attr == NULL)
17584 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17585 target_cu);
80180f79
SA
17586 if (target_attr == NULL)
17587 return 0;
17588
df25ebbd 17589 switch (target_attr->name)
80180f79 17590 {
df25ebbd 17591 case DW_AT_location:
cd6c91b4 17592 if (target_attr->form_is_section_offset ())
df25ebbd 17593 {
8d749320 17594 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17595 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17596 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17597 prop->data.baton = baton;
17598 prop->kind = PROP_LOCLIST;
17599 gdb_assert (prop->data.baton != NULL);
17600 }
4fc6c0d5 17601 else if (target_attr->form_is_block ())
df25ebbd 17602 {
8d749320 17603 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17604 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17605 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17606 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17607 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17608 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17609 baton->locexpr.is_reference = true;
df25ebbd
JB
17610 prop->data.baton = baton;
17611 prop->kind = PROP_LOCEXPR;
17612 gdb_assert (prop->data.baton != NULL);
17613 }
17614 else
17615 {
17616 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17617 "dynamic property");
17618 return 0;
17619 }
17620 break;
17621 case DW_AT_data_member_location:
17622 {
17623 LONGEST offset;
17624
17625 if (!handle_data_member_location (target_die, target_cu,
17626 &offset))
17627 return 0;
17628
8d749320 17629 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17630 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17631 target_cu);
df25ebbd
JB
17632 baton->offset_info.offset = offset;
17633 baton->offset_info.type = die_type (target_die, target_cu);
17634 prop->data.baton = baton;
17635 prop->kind = PROP_ADDR_OFFSET;
17636 break;
17637 }
80180f79
SA
17638 }
17639 }
cd6c91b4 17640 else if (attr->form_is_constant ())
80180f79 17641 {
0826b30a 17642 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17643 prop->kind = PROP_CONST;
17644 }
17645 else
17646 {
17647 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17648 dwarf2_name (die, cu));
17649 return 0;
17650 }
17651
17652 return 1;
17653}
17654
09ba997f 17655/* See read.h. */
9a49df9d 17656
09ba997f 17657struct type *
293e7e51 17658dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17659{
9a49df9d
AB
17660 struct type *int_type;
17661
17662 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17663#define TRY_TYPE(F) \
17664 int_type = (unsigned_p \
17665 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17666 : objfile_type (objfile)->builtin_ ## F); \
17667 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17668 return int_type
17669
17670 TRY_TYPE (char);
17671 TRY_TYPE (short);
17672 TRY_TYPE (int);
17673 TRY_TYPE (long);
17674 TRY_TYPE (long_long);
17675
17676#undef TRY_TYPE
17677
17678 gdb_assert_not_reached ("unable to find suitable integer type");
17679}
17680
09ba997f 17681/* See read.h. */
11a8b164 17682
09ba997f 17683struct type *
293e7e51 17684dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17685{
293e7e51
SM
17686 int addr_size = this->per_cu->addr_size ();
17687 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17688}
17689
b86352cf
AB
17690/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17691 present (which is valid) then compute the default type based on the
17692 compilation units address size. */
17693
17694static struct type *
17695read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17696{
17697 struct type *index_type = die_type (die, cu);
17698
17699 /* Dwarf-2 specifications explicitly allows to create subrange types
17700 without specifying a base type.
17701 In that case, the base type must be set to the type of
17702 the lower bound, upper bound or count, in that order, if any of these
17703 three attributes references an object that has a type.
17704 If no base type is found, the Dwarf-2 specifications say that
17705 a signed integer type of size equal to the size of an address should
17706 be used.
17707 For the following C code: `extern char gdb_int [];'
17708 GCC produces an empty range DIE.
17709 FIXME: muller/2010-05-28: Possible references to object for low bound,
17710 high bound or count are not yet handled by this code. */
78134374 17711 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17712 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17713
17714 return index_type;
17715}
17716
a02abb62
JB
17717/* Read the given DW_AT_subrange DIE. */
17718
f792889a 17719static struct type *
a02abb62
JB
17720read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17721{
4c9ad8c2 17722 struct type *base_type, *orig_base_type;
a02abb62
JB
17723 struct type *range_type;
17724 struct attribute *attr;
729efb13 17725 struct dynamic_prop low, high;
4fae6e18 17726 int low_default_is_valid;
c451ebe5 17727 int high_bound_is_count = 0;
15d034d0 17728 const char *name;
d359392f 17729 ULONGEST negative_mask;
e77813c8 17730
b86352cf
AB
17731 orig_base_type = read_subrange_index_type (die, cu);
17732
4c9ad8c2
TT
17733 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17734 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17735 creating the range type, but we use the result of check_typedef
17736 when examining properties of the type. */
17737 base_type = check_typedef (orig_base_type);
a02abb62 17738
7e314c57
JK
17739 /* The die_type call above may have already set the type for this DIE. */
17740 range_type = get_die_type (die, cu);
17741 if (range_type)
17742 return range_type;
17743
729efb13
SA
17744 low.kind = PROP_CONST;
17745 high.kind = PROP_CONST;
17746 high.data.const_val = 0;
17747
4fae6e18
JK
17748 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17749 omitting DW_AT_lower_bound. */
17750 switch (cu->language)
6e70227d 17751 {
4fae6e18
JK
17752 case language_c:
17753 case language_cplus:
729efb13 17754 low.data.const_val = 0;
4fae6e18
JK
17755 low_default_is_valid = 1;
17756 break;
17757 case language_fortran:
729efb13 17758 low.data.const_val = 1;
4fae6e18
JK
17759 low_default_is_valid = 1;
17760 break;
17761 case language_d:
4fae6e18 17762 case language_objc:
c44af4eb 17763 case language_rust:
729efb13 17764 low.data.const_val = 0;
4fae6e18
JK
17765 low_default_is_valid = (cu->header.version >= 4);
17766 break;
17767 case language_ada:
17768 case language_m2:
17769 case language_pascal:
729efb13 17770 low.data.const_val = 1;
4fae6e18
JK
17771 low_default_is_valid = (cu->header.version >= 4);
17772 break;
17773 default:
729efb13 17774 low.data.const_val = 0;
4fae6e18
JK
17775 low_default_is_valid = 0;
17776 break;
a02abb62
JB
17777 }
17778
e142c38c 17779 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17780 if (attr != nullptr)
9a49df9d 17781 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17782 else if (!low_default_is_valid)
b98664d3 17783 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17784 "- DIE at %s [in module %s]"),
17785 sect_offset_str (die->sect_off),
5e22e966 17786 objfile_name (cu->per_objfile->objfile));
a02abb62 17787
506f5c41
TV
17788 struct attribute *attr_ub, *attr_count;
17789 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17790 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17791 {
506f5c41 17792 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17793 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17794 {
c451ebe5
SA
17795 /* If bounds are constant do the final calculation here. */
17796 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17797 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17798 else
17799 high_bound_is_count = 1;
c2ff108b 17800 }
506f5c41
TV
17801 else
17802 {
17803 if (attr_ub != NULL)
17804 complaint (_("Unresolved DW_AT_upper_bound "
17805 "- DIE at %s [in module %s]"),
17806 sect_offset_str (die->sect_off),
5e22e966 17807 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17808 if (attr_count != NULL)
17809 complaint (_("Unresolved DW_AT_count "
17810 "- DIE at %s [in module %s]"),
17811 sect_offset_str (die->sect_off),
5e22e966 17812 objfile_name (cu->per_objfile->objfile));
506f5c41 17813 }
e77813c8 17814 }
a02abb62 17815
4e962e74
TT
17816 LONGEST bias = 0;
17817 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17818 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17819 bias = bias_attr->constant_value (0);
4e962e74 17820
dbb9c2b1
JB
17821 /* Normally, the DWARF producers are expected to use a signed
17822 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17823 But this is unfortunately not always the case, as witnessed
17824 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17825 is used instead. To work around that ambiguity, we treat
17826 the bounds as signed, and thus sign-extend their values, when
17827 the base type is signed. */
6e70227d 17828 negative_mask =
d359392f 17829 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17830 if (low.kind == PROP_CONST
17831 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17832 low.data.const_val |= negative_mask;
17833 if (high.kind == PROP_CONST
17834 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17835 high.data.const_val |= negative_mask;
43bbcdc2 17836
5bbd8269
AB
17837 /* Check for bit and byte strides. */
17838 struct dynamic_prop byte_stride_prop;
17839 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17840 if (attr_byte_stride != nullptr)
17841 {
293e7e51 17842 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17843 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17844 prop_type);
17845 }
17846
17847 struct dynamic_prop bit_stride_prop;
17848 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17849 if (attr_bit_stride != nullptr)
17850 {
17851 /* It only makes sense to have either a bit or byte stride. */
17852 if (attr_byte_stride != nullptr)
17853 {
17854 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17855 "- DIE at %s [in module %s]"),
17856 sect_offset_str (die->sect_off),
5e22e966 17857 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17858 attr_bit_stride = nullptr;
17859 }
17860 else
17861 {
293e7e51 17862 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17863 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17864 prop_type);
17865 }
17866 }
17867
17868 if (attr_byte_stride != nullptr
17869 || attr_bit_stride != nullptr)
17870 {
17871 bool byte_stride_p = (attr_byte_stride != nullptr);
17872 struct dynamic_prop *stride
17873 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17874
17875 range_type
17876 = create_range_type_with_stride (NULL, orig_base_type, &low,
17877 &high, bias, stride, byte_stride_p);
17878 }
17879 else
17880 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17881
c451ebe5
SA
17882 if (high_bound_is_count)
17883 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17884
c2ff108b
JK
17885 /* Ada expects an empty array on no boundary attributes. */
17886 if (attr == NULL && cu->language != language_ada)
729efb13 17887 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17888
39cbfefa
DJ
17889 name = dwarf2_name (die, cu);
17890 if (name)
d0e39ea2 17891 range_type->set_name (name);
6e70227d 17892
e142c38c 17893 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17894 if (attr != nullptr)
a02abb62
JB
17895 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17896
2b4424c3
TT
17897 maybe_set_alignment (cu, die, range_type);
17898
7e314c57
JK
17899 set_die_type (die, range_type, cu);
17900
17901 /* set_die_type should be already done. */
b4ba55a1
JB
17902 set_descriptive_type (range_type, die, cu);
17903
7e314c57 17904 return range_type;
a02abb62 17905}
6e70227d 17906
f792889a 17907static struct type *
81a17f79
JB
17908read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17909{
17910 struct type *type;
81a17f79 17911
5e22e966 17912 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17913 type->set_name (dwarf2_name (die, cu));
81a17f79 17914
74a2f8ff 17915 /* In Ada, an unspecified type is typically used when the description
85102364 17916 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17917 such a type, we treat it as a stub, and try to resolve it later on,
17918 when needed. */
17919 if (cu->language == language_ada)
17920 TYPE_STUB (type) = 1;
17921
f792889a 17922 return set_die_type (die, type, cu);
81a17f79 17923}
a02abb62 17924
639d11d3
DC
17925/* Read a single die and all its descendents. Set the die's sibling
17926 field to NULL; set other fields in the die correctly, and set all
17927 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17928 location of the info_ptr after reading all of those dies. PARENT
17929 is the parent of the die in question. */
17930
17931static struct die_info *
dee91e82 17932read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17933 const gdb_byte *info_ptr,
17934 const gdb_byte **new_info_ptr,
dee91e82 17935 struct die_info *parent)
639d11d3
DC
17936{
17937 struct die_info *die;
d521ce57 17938 const gdb_byte *cur_ptr;
639d11d3 17939
3e225074 17940 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17941 if (die == NULL)
17942 {
17943 *new_info_ptr = cur_ptr;
17944 return NULL;
17945 }
93311388 17946 store_in_ref_table (die, reader->cu);
639d11d3 17947
3e225074 17948 if (die->has_children)
bf6af496 17949 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17950 else
17951 {
17952 die->child = NULL;
17953 *new_info_ptr = cur_ptr;
17954 }
17955
17956 die->sibling = NULL;
17957 die->parent = parent;
17958 return die;
17959}
17960
17961/* Read a die, all of its descendents, and all of its siblings; set
17962 all of the fields of all of the dies correctly. Arguments are as
17963 in read_die_and_children. */
17964
17965static struct die_info *
bf6af496 17966read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17967 const gdb_byte *info_ptr,
17968 const gdb_byte **new_info_ptr,
bf6af496 17969 struct die_info *parent)
639d11d3
DC
17970{
17971 struct die_info *first_die, *last_sibling;
d521ce57 17972 const gdb_byte *cur_ptr;
639d11d3 17973
c906108c 17974 cur_ptr = info_ptr;
639d11d3
DC
17975 first_die = last_sibling = NULL;
17976
17977 while (1)
c906108c 17978 {
639d11d3 17979 struct die_info *die
dee91e82 17980 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17981
1d325ec1 17982 if (die == NULL)
c906108c 17983 {
639d11d3
DC
17984 *new_info_ptr = cur_ptr;
17985 return first_die;
c906108c 17986 }
1d325ec1
DJ
17987
17988 if (!first_die)
17989 first_die = die;
c906108c 17990 else
1d325ec1
DJ
17991 last_sibling->sibling = die;
17992
17993 last_sibling = die;
c906108c 17994 }
c906108c
SS
17995}
17996
bf6af496
DE
17997/* Read a die, all of its descendents, and all of its siblings; set
17998 all of the fields of all of the dies correctly. Arguments are as
17999 in read_die_and_children.
18000 This the main entry point for reading a DIE and all its children. */
18001
18002static struct die_info *
18003read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18004 const gdb_byte *info_ptr,
18005 const gdb_byte **new_info_ptr,
bf6af496
DE
18006 struct die_info *parent)
18007{
18008 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18009 new_info_ptr, parent);
18010
b4f54984 18011 if (dwarf_die_debug)
bf6af496
DE
18012 {
18013 fprintf_unfiltered (gdb_stdlog,
18014 "Read die from %s@0x%x of %s:\n",
96b79293 18015 reader->die_section->get_name (),
bf6af496
DE
18016 (unsigned) (info_ptr - reader->die_section->buffer),
18017 bfd_get_filename (reader->abfd));
b4f54984 18018 dump_die (die, dwarf_die_debug);
bf6af496
DE
18019 }
18020
18021 return die;
18022}
18023
3019eac3
DE
18024/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18025 attributes.
18026 The caller is responsible for filling in the extra attributes
18027 and updating (*DIEP)->num_attrs.
18028 Set DIEP to point to a newly allocated die with its information,
3e225074 18029 except for its child, sibling, and parent fields. */
93311388 18030
d521ce57 18031static const gdb_byte *
3019eac3 18032read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18033 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18034 int num_extra_attrs)
93311388 18035{
b64f50a1 18036 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18037 struct abbrev_info *abbrev;
18038 struct die_info *die;
18039 struct dwarf2_cu *cu = reader->cu;
18040 bfd *abfd = reader->abfd;
18041
9c541725 18042 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18043 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18044 info_ptr += bytes_read;
18045 if (!abbrev_number)
18046 {
18047 *diep = NULL;
93311388
DE
18048 return info_ptr;
18049 }
18050
685af9cd 18051 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18052 if (!abbrev)
348e048f
DE
18053 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18054 abbrev_number,
18055 bfd_get_filename (abfd));
18056
3019eac3 18057 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18058 die->sect_off = sect_off;
93311388
DE
18059 die->tag = abbrev->tag;
18060 die->abbrev = abbrev_number;
3e225074 18061 die->has_children = abbrev->has_children;
93311388 18062
3019eac3
DE
18063 /* Make the result usable.
18064 The caller needs to update num_attrs after adding the extra
18065 attributes. */
93311388
DE
18066 die->num_attrs = abbrev->num_attrs;
18067
18a8505e 18068 std::vector<int> indexes_that_need_reprocess;
93311388 18069 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18070 {
18071 bool need_reprocess;
18072 info_ptr =
18073 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18074 info_ptr, &need_reprocess);
18075 if (need_reprocess)
18076 indexes_that_need_reprocess.push_back (i);
18077 }
18078
052c8bb8 18079 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18080 if (attr != nullptr)
18081 cu->str_offsets_base = DW_UNSND (attr);
93311388 18082
41144253 18083 attr = die->attr (DW_AT_loclists_base);
18084 if (attr != nullptr)
18085 cu->loclist_base = DW_UNSND (attr);
18086
a39fdb41 18087 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18088 if (maybe_addr_base.has_value ())
18089 cu->addr_base = *maybe_addr_base;
18090 for (int index : indexes_that_need_reprocess)
18091 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18092 *diep = die;
93311388
DE
18093 return info_ptr;
18094}
18095
3019eac3
DE
18096/* Read a die and all its attributes.
18097 Set DIEP to point to a newly allocated die with its information,
3e225074 18098 except for its child, sibling, and parent fields. */
3019eac3 18099
d521ce57 18100static const gdb_byte *
3019eac3 18101read_full_die (const struct die_reader_specs *reader,
3e225074 18102 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18103{
d521ce57 18104 const gdb_byte *result;
bf6af496 18105
3e225074 18106 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18107
b4f54984 18108 if (dwarf_die_debug)
bf6af496
DE
18109 {
18110 fprintf_unfiltered (gdb_stdlog,
18111 "Read die from %s@0x%x of %s:\n",
96b79293 18112 reader->die_section->get_name (),
bf6af496
DE
18113 (unsigned) (info_ptr - reader->die_section->buffer),
18114 bfd_get_filename (reader->abfd));
b4f54984 18115 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18116 }
18117
18118 return result;
3019eac3 18119}
433df2d4 18120\f
c906108c 18121
72bf9492
DJ
18122/* Returns nonzero if TAG represents a type that we might generate a partial
18123 symbol for. */
18124
18125static int
18126is_type_tag_for_partial (int tag)
18127{
18128 switch (tag)
18129 {
18130#if 0
18131 /* Some types that would be reasonable to generate partial symbols for,
18132 that we don't at present. */
18133 case DW_TAG_array_type:
18134 case DW_TAG_file_type:
18135 case DW_TAG_ptr_to_member_type:
18136 case DW_TAG_set_type:
18137 case DW_TAG_string_type:
18138 case DW_TAG_subroutine_type:
18139#endif
18140 case DW_TAG_base_type:
18141 case DW_TAG_class_type:
680b30c7 18142 case DW_TAG_interface_type:
72bf9492
DJ
18143 case DW_TAG_enumeration_type:
18144 case DW_TAG_structure_type:
18145 case DW_TAG_subrange_type:
18146 case DW_TAG_typedef:
18147 case DW_TAG_union_type:
18148 return 1;
18149 default:
18150 return 0;
18151 }
18152}
18153
18154/* Load all DIEs that are interesting for partial symbols into memory. */
18155
18156static struct partial_die_info *
dee91e82 18157load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18158 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18159{
dee91e82 18160 struct dwarf2_cu *cu = reader->cu;
5e22e966 18161 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18162 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18163 unsigned int bytes_read;
5afb4e99 18164 unsigned int load_all = 0;
72bf9492
DJ
18165 int nesting_level = 1;
18166
18167 parent_die = NULL;
18168 last_die = NULL;
18169
7adf1e79
DE
18170 gdb_assert (cu->per_cu != NULL);
18171 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18172 load_all = 1;
18173
72bf9492
DJ
18174 cu->partial_dies
18175 = htab_create_alloc_ex (cu->header.length / 12,
18176 partial_die_hash,
18177 partial_die_eq,
18178 NULL,
18179 &cu->comp_unit_obstack,
18180 hashtab_obstack_allocate,
18181 dummy_obstack_deallocate);
18182
72bf9492
DJ
18183 while (1)
18184 {
685af9cd 18185 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18186
18187 /* A NULL abbrev means the end of a series of children. */
18188 if (abbrev == NULL)
18189 {
18190 if (--nesting_level == 0)
cd9983dd
YQ
18191 return first_die;
18192
72bf9492
DJ
18193 info_ptr += bytes_read;
18194 last_die = parent_die;
18195 parent_die = parent_die->die_parent;
18196 continue;
18197 }
18198
98bfdba5
PA
18199 /* Check for template arguments. We never save these; if
18200 they're seen, we just mark the parent, and go on our way. */
18201 if (parent_die != NULL
18202 && cu->language == language_cplus
18203 && (abbrev->tag == DW_TAG_template_type_param
18204 || abbrev->tag == DW_TAG_template_value_param))
18205 {
18206 parent_die->has_template_arguments = 1;
18207
18208 if (!load_all)
18209 {
18210 /* We don't need a partial DIE for the template argument. */
dee91e82 18211 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18212 continue;
18213 }
18214 }
18215
0d99eb77 18216 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18217 Skip their other children. */
18218 if (!load_all
18219 && cu->language == language_cplus
18220 && parent_die != NULL
18221 && parent_die->tag == DW_TAG_subprogram)
18222 {
dee91e82 18223 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18224 continue;
18225 }
18226
5afb4e99
DJ
18227 /* Check whether this DIE is interesting enough to save. Normally
18228 we would not be interested in members here, but there may be
18229 later variables referencing them via DW_AT_specification (for
18230 static members). */
18231 if (!load_all
18232 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18233 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18234 && abbrev->tag != DW_TAG_enumerator
18235 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18236 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18237 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18238 && abbrev->tag != DW_TAG_variable
5afb4e99 18239 && abbrev->tag != DW_TAG_namespace
f55ee35c 18240 && abbrev->tag != DW_TAG_module
95554aad 18241 && abbrev->tag != DW_TAG_member
74921315
KS
18242 && abbrev->tag != DW_TAG_imported_unit
18243 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18244 {
18245 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18246 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18247 continue;
18248 }
18249
6f06d47b
YQ
18250 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18251 abbrev);
cd9983dd 18252
48fbe735 18253 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18254
18255 /* This two-pass algorithm for processing partial symbols has a
18256 high cost in cache pressure. Thus, handle some simple cases
18257 here which cover the majority of C partial symbols. DIEs
18258 which neither have specification tags in them, nor could have
18259 specification tags elsewhere pointing at them, can simply be
18260 processed and discarded.
18261
18262 This segment is also optional; scan_partial_symbols and
18263 add_partial_symbol will handle these DIEs if we chain
18264 them in normally. When compilers which do not emit large
18265 quantities of duplicate debug information are more common,
18266 this code can probably be removed. */
18267
18268 /* Any complete simple types at the top level (pretty much all
18269 of them, for a language without namespaces), can be processed
18270 directly. */
18271 if (parent_die == NULL
cd9983dd
YQ
18272 && pdi.has_specification == 0
18273 && pdi.is_declaration == 0
18274 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18275 || pdi.tag == DW_TAG_base_type
18276 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18277 {
cd9983dd 18278 if (building_psymtab && pdi.name != NULL)
31edb802 18279 add_psymbol_to_list (pdi.name, false,
79748972 18280 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18281 psymbol_placement::STATIC,
1762568f 18282 0, cu->language, objfile);
cd9983dd 18283 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18284 continue;
18285 }
18286
d8228535
JK
18287 /* The exception for DW_TAG_typedef with has_children above is
18288 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18289 type_name_or_error will error on such types later.
d8228535
JK
18290
18291 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18292 it could not find the child DIEs referenced later, this is checked
18293 above. In correct DWARF DW_TAG_typedef should have no children. */
18294
cd9983dd 18295 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18296 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18297 "- DIE at %s [in module %s]"),
cd9983dd 18298 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18299
72bf9492
DJ
18300 /* If we're at the second level, and we're an enumerator, and
18301 our parent has no specification (meaning possibly lives in a
18302 namespace elsewhere), then we can add the partial symbol now
18303 instead of queueing it. */
cd9983dd 18304 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18305 && parent_die != NULL
18306 && parent_die->die_parent == NULL
18307 && parent_die->tag == DW_TAG_enumeration_type
18308 && parent_die->has_specification == 0)
18309 {
cd9983dd 18310 if (pdi.name == NULL)
b98664d3 18311 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18312 else if (building_psymtab)
31edb802 18313 add_psymbol_to_list (pdi.name, false,
79748972 18314 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18315 cu->language == language_cplus
75aedd27
TT
18316 ? psymbol_placement::GLOBAL
18317 : psymbol_placement::STATIC,
1762568f 18318 0, cu->language, objfile);
72bf9492 18319
cd9983dd 18320 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18321 continue;
18322 }
18323
cd9983dd 18324 struct partial_die_info *part_die
6f06d47b 18325 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18326
72bf9492
DJ
18327 /* We'll save this DIE so link it in. */
18328 part_die->die_parent = parent_die;
18329 part_die->die_sibling = NULL;
18330 part_die->die_child = NULL;
18331
18332 if (last_die && last_die == parent_die)
18333 last_die->die_child = part_die;
18334 else if (last_die)
18335 last_die->die_sibling = part_die;
18336
18337 last_die = part_die;
18338
18339 if (first_die == NULL)
18340 first_die = part_die;
18341
18342 /* Maybe add the DIE to the hash table. Not all DIEs that we
18343 find interesting need to be in the hash table, because we
18344 also have the parent/sibling/child chains; only those that we
18345 might refer to by offset later during partial symbol reading.
18346
18347 For now this means things that might have be the target of a
18348 DW_AT_specification, DW_AT_abstract_origin, or
18349 DW_AT_extension. DW_AT_extension will refer only to
18350 namespaces; DW_AT_abstract_origin refers to functions (and
18351 many things under the function DIE, but we do not recurse
18352 into function DIEs during partial symbol reading) and
18353 possibly variables as well; DW_AT_specification refers to
18354 declarations. Declarations ought to have the DW_AT_declaration
18355 flag. It happens that GCC forgets to put it in sometimes, but
18356 only for functions, not for types.
18357
18358 Adding more things than necessary to the hash table is harmless
18359 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18360 wasted time in find_partial_die, when we reread the compilation
18361 unit with load_all_dies set. */
72bf9492 18362
5afb4e99 18363 if (load_all
72929c62 18364 || abbrev->tag == DW_TAG_constant
5afb4e99 18365 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18366 || abbrev->tag == DW_TAG_variable
18367 || abbrev->tag == DW_TAG_namespace
18368 || part_die->is_declaration)
18369 {
18370 void **slot;
18371
18372 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18373 to_underlying (part_die->sect_off),
18374 INSERT);
72bf9492
DJ
18375 *slot = part_die;
18376 }
18377
72bf9492 18378 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18379 we have no reason to follow the children of structures; for other
98bfdba5
PA
18380 languages we have to, so that we can get at method physnames
18381 to infer fully qualified class names, for DW_AT_specification,
18382 and for C++ template arguments. For C++, we also look one level
18383 inside functions to find template arguments (if the name of the
18384 function does not already contain the template arguments).
bc30ff58 18385
0a4b0913
AB
18386 For Ada and Fortran, we need to scan the children of subprograms
18387 and lexical blocks as well because these languages allow the
18388 definition of nested entities that could be interesting for the
18389 debugger, such as nested subprograms for instance. */
72bf9492 18390 if (last_die->has_children
5afb4e99
DJ
18391 && (load_all
18392 || last_die->tag == DW_TAG_namespace
f55ee35c 18393 || last_die->tag == DW_TAG_module
72bf9492 18394 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18395 || (cu->language == language_cplus
18396 && last_die->tag == DW_TAG_subprogram
18397 && (last_die->name == NULL
18398 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18399 || (cu->language != language_c
18400 && (last_die->tag == DW_TAG_class_type
680b30c7 18401 || last_die->tag == DW_TAG_interface_type
72bf9492 18402 || last_die->tag == DW_TAG_structure_type
bc30ff58 18403 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18404 || ((cu->language == language_ada
18405 || cu->language == language_fortran)
bc30ff58
JB
18406 && (last_die->tag == DW_TAG_subprogram
18407 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18408 {
18409 nesting_level++;
18410 parent_die = last_die;
18411 continue;
18412 }
18413
18414 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18415 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18416
18417 /* Back to the top, do it again. */
18418 }
18419}
18420
6f06d47b
YQ
18421partial_die_info::partial_die_info (sect_offset sect_off_,
18422 struct abbrev_info *abbrev)
18423 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18424{
18425}
18426
35cc7ed7
YQ
18427/* Read a minimal amount of information into the minimal die structure.
18428 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18429
48fbe735
YQ
18430const gdb_byte *
18431partial_die_info::read (const struct die_reader_specs *reader,
18432 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18433{
dee91e82 18434 struct dwarf2_cu *cu = reader->cu;
5e22e966 18435 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18436 unsigned int i;
c5aa993b 18437 int has_low_pc_attr = 0;
c906108c 18438 int has_high_pc_attr = 0;
91da1414 18439 int high_pc_relative = 0;
c906108c 18440
fd0a254f 18441 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18442 {
e7da7f8f 18443 attribute attr;
18a8505e 18444 bool need_reprocess;
e7da7f8f 18445 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18446 info_ptr, &need_reprocess);
18447 /* String and address offsets that need to do the reprocessing have
18448 already been read at this point, so there is no need to wait until
18449 the loop terminates to do the reprocessing. */
18450 if (need_reprocess)
e7da7f8f 18451 read_attribute_reprocess (reader, &attr);
c906108c 18452 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18453 partial symbol table. */
c906108c
SS
18454 switch (attr.name)
18455 {
18456 case DW_AT_name:
48fbe735 18457 switch (tag)
71c25dea
TT
18458 {
18459 case DW_TAG_compile_unit:
95554aad 18460 case DW_TAG_partial_unit:
348e048f 18461 case DW_TAG_type_unit:
71c25dea
TT
18462 /* Compilation units have a DW_AT_name that is a filename, not
18463 a source language identifier. */
18464 case DW_TAG_enumeration_type:
18465 case DW_TAG_enumerator:
18466 /* These tags always have simple identifiers already; no need
18467 to canonicalize them. */
48fbe735 18468 name = DW_STRING (&attr);
71c25dea
TT
18469 break;
18470 default:
48fbe735
YQ
18471 {
18472 struct objfile *objfile = dwarf2_per_objfile->objfile;
18473
18474 name
be1e3d3e 18475 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18476 }
71c25dea
TT
18477 break;
18478 }
c906108c 18479 break;
31ef98ae 18480 case DW_AT_linkage_name:
c906108c 18481 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18482 /* Note that both forms of linkage name might appear. We
18483 assume they will be the same, and we only store the last
18484 one we see. */
e61108c9 18485 linkage_name = attr.value_as_string ();
787de330
TT
18486 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18487 See https://github.com/rust-lang/rust/issues/32925. */
18488 if (cu->language == language_rust && linkage_name != NULL
18489 && strchr (linkage_name, '{') != NULL)
18490 linkage_name = NULL;
c906108c
SS
18491 break;
18492 case DW_AT_low_pc:
18493 has_low_pc_attr = 1;
cd6c91b4 18494 lowpc = attr.value_as_address ();
c906108c
SS
18495 break;
18496 case DW_AT_high_pc:
18497 has_high_pc_attr = 1;
cd6c91b4
TT
18498 highpc = attr.value_as_address ();
18499 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18500 high_pc_relative = 1;
c906108c
SS
18501 break;
18502 case DW_AT_location:
0963b4bd 18503 /* Support the .debug_loc offsets. */
4fc6c0d5 18504 if (attr.form_is_block ())
8e19ed76 18505 {
48fbe735 18506 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18507 }
cd6c91b4 18508 else if (attr.form_is_section_offset ())
8e19ed76 18509 {
4d3c2250 18510 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18511 }
18512 else
18513 {
4d3c2250
KB
18514 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18515 "partial symbol information");
8e19ed76 18516 }
c906108c 18517 break;
c906108c 18518 case DW_AT_external:
48fbe735 18519 is_external = DW_UNSND (&attr);
c906108c
SS
18520 break;
18521 case DW_AT_declaration:
48fbe735 18522 is_declaration = DW_UNSND (&attr);
c906108c
SS
18523 break;
18524 case DW_AT_type:
48fbe735 18525 has_type = 1;
c906108c
SS
18526 break;
18527 case DW_AT_abstract_origin:
18528 case DW_AT_specification:
72bf9492 18529 case DW_AT_extension:
48fbe735 18530 has_specification = 1;
0826b30a 18531 spec_offset = attr.get_ref_die_offset ();
48fbe735 18532 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18533 || cu->per_cu->is_dwz);
c906108c
SS
18534 break;
18535 case DW_AT_sibling:
18536 /* Ignore absolute siblings, they might point outside of
18537 the current compile unit. */
18538 if (attr.form == DW_FORM_ref_addr)
b98664d3 18539 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18540 else
b9502d3f 18541 {
48fbe735 18542 const gdb_byte *buffer = reader->buffer;
0826b30a 18543 sect_offset off = attr.get_ref_die_offset ();
9c541725 18544 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18545
18546 if (sibling_ptr < info_ptr)
b98664d3 18547 complaint (_("DW_AT_sibling points backwards"));
22869d73 18548 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18549 reader->die_section->overflow_complaint ();
b9502d3f 18550 else
48fbe735 18551 sibling = sibling_ptr;
b9502d3f 18552 }
c906108c 18553 break;
fa4028e9 18554 case DW_AT_byte_size:
48fbe735 18555 has_byte_size = 1;
fa4028e9 18556 break;
ff908ebf 18557 case DW_AT_const_value:
48fbe735 18558 has_const_value = 1;
ff908ebf 18559 break;
68511cec
CES
18560 case DW_AT_calling_convention:
18561 /* DWARF doesn't provide a way to identify a program's source-level
18562 entry point. DW_AT_calling_convention attributes are only meant
18563 to describe functions' calling conventions.
18564
18565 However, because it's a necessary piece of information in
0c1b455e
TT
18566 Fortran, and before DWARF 4 DW_CC_program was the only
18567 piece of debugging information whose definition refers to
18568 a 'main program' at all, several compilers marked Fortran
18569 main programs with DW_CC_program --- even when those
18570 functions use the standard calling conventions.
18571
18572 Although DWARF now specifies a way to provide this
18573 information, we support this practice for backward
18574 compatibility. */
68511cec 18575 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18576 && cu->language == language_fortran)
48fbe735 18577 main_subprogram = 1;
68511cec 18578 break;
481860b3
GB
18579 case DW_AT_inline:
18580 if (DW_UNSND (&attr) == DW_INL_inlined
18581 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18582 may_be_inlined = 1;
481860b3 18583 break;
95554aad
TT
18584
18585 case DW_AT_import:
48fbe735 18586 if (tag == DW_TAG_imported_unit)
36586728 18587 {
0826b30a 18588 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18589 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18590 || cu->per_cu->is_dwz);
18591 }
95554aad
TT
18592 break;
18593
0c1b455e 18594 case DW_AT_main_subprogram:
48fbe735 18595 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18596 break;
18597
05caa1d2
TT
18598 case DW_AT_ranges:
18599 {
18600 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18601 but that requires a full DIE, so instead we just
18602 reimplement it. */
18603 int need_ranges_base = tag != DW_TAG_compile_unit;
18604 unsigned int ranges_offset = (DW_UNSND (&attr)
18605 + (need_ranges_base
18606 ? cu->ranges_base
18607 : 0));
18608
18609 /* Value of the DW_AT_ranges attribute is the offset in the
18610 .debug_ranges section. */
18611 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18612 nullptr))
18613 has_pc_info = 1;
18614 }
18615 break;
18616
c906108c
SS
18617 default:
18618 break;
18619 }
18620 }
18621
10d06d82
TT
18622 /* For Ada, if both the name and the linkage name appear, we prefer
18623 the latter. This lets "catch exception" work better, regardless
18624 of the order in which the name and linkage name were emitted.
18625 Really, though, this is just a workaround for the fact that gdb
18626 doesn't store both the name and the linkage name. */
18627 if (cu->language == language_ada && linkage_name != nullptr)
18628 name = linkage_name;
18629
91da1414 18630 if (high_pc_relative)
48fbe735 18631 highpc += lowpc;
91da1414 18632
9373cf26
JK
18633 if (has_low_pc_attr && has_high_pc_attr)
18634 {
18635 /* When using the GNU linker, .gnu.linkonce. sections are used to
18636 eliminate duplicate copies of functions and vtables and such.
18637 The linker will arbitrarily choose one and discard the others.
18638 The AT_*_pc values for such functions refer to local labels in
18639 these sections. If the section from that file was discarded, the
18640 labels are not in the output, so the relocs get a value of 0.
18641 If this is a discarded function, mark the pc bounds as invalid,
18642 so that GDB will ignore it. */
5989a64e 18643 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18644 {
48fbe735 18645 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18646 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18647
b98664d3 18648 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18649 "for DIE at %s [in module %s]"),
48fbe735
YQ
18650 paddress (gdbarch, lowpc),
18651 sect_offset_str (sect_off),
9d8780f0 18652 objfile_name (objfile));
9373cf26
JK
18653 }
18654 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18655 else if (lowpc >= highpc)
9373cf26 18656 {
48fbe735 18657 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18658 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18659
b98664d3 18660 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18661 "for DIE at %s [in module %s]"),
48fbe735
YQ
18662 paddress (gdbarch, lowpc),
18663 paddress (gdbarch, highpc),
18664 sect_offset_str (sect_off),
9c541725 18665 objfile_name (objfile));
9373cf26
JK
18666 }
18667 else
48fbe735 18668 has_pc_info = 1;
9373cf26 18669 }
85cbf3d3 18670
c906108c
SS
18671 return info_ptr;
18672}
18673
72bf9492
DJ
18674/* Find a cached partial DIE at OFFSET in CU. */
18675
d590ff25
YQ
18676struct partial_die_info *
18677dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18678{
18679 struct partial_die_info *lookup_die = NULL;
6f06d47b 18680 struct partial_die_info part_die (sect_off);
72bf9492 18681
9a3c8263 18682 lookup_die = ((struct partial_die_info *)
d590ff25 18683 htab_find_with_hash (partial_dies, &part_die,
9c541725 18684 to_underlying (sect_off)));
72bf9492 18685
72bf9492
DJ
18686 return lookup_die;
18687}
18688
348e048f
DE
18689/* Find a partial DIE at OFFSET, which may or may not be in CU,
18690 except in the case of .debug_types DIEs which do not reference
18691 outside their CU (they do however referencing other types via
55f1336d 18692 DW_FORM_ref_sig8). */
72bf9492 18693
122cf0f2 18694static const struct cu_partial_die_info
9c541725 18695find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18696{
5e22e966 18697 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18698 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18699 struct dwarf2_per_cu_data *per_cu = NULL;
18700 struct partial_die_info *pd = NULL;
72bf9492 18701
36586728 18702 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18703 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18704 {
d590ff25 18705 pd = cu->find_partial_die (sect_off);
5afb4e99 18706 if (pd != NULL)
fb816e8b 18707 return { cu, pd };
0d99eb77
DE
18708 /* We missed recording what we needed.
18709 Load all dies and try again. */
18710 per_cu = cu->per_cu;
5afb4e99 18711 }
0d99eb77
DE
18712 else
18713 {
18714 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18715 if (cu->per_cu->is_debug_types)
0d99eb77 18716 {
9d8780f0
SM
18717 error (_("Dwarf Error: Type Unit at offset %s contains"
18718 " external reference to offset %s [in module %s].\n"),
18719 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18720 bfd_get_filename (objfile->obfd));
18721 }
9c541725 18722 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18723 dwarf2_per_objfile);
72bf9492 18724
0d99eb77 18725 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
ab432490 18726 load_partial_comp_unit (per_cu, cu->per_objfile);
ae038cb0 18727
0d99eb77 18728 per_cu->cu->last_used = 0;
d590ff25 18729 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18730 }
5afb4e99 18731
dee91e82
DE
18732 /* If we didn't find it, and not all dies have been loaded,
18733 load them all and try again. */
18734
5afb4e99
DJ
18735 if (pd == NULL && per_cu->load_all_dies == 0)
18736 {
5afb4e99 18737 per_cu->load_all_dies = 1;
fd820528
DE
18738
18739 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18740 THIS_CU->cu may already be in use. So we can't just free it and
18741 replace its DIEs with the ones we read in. Instead, we leave those
18742 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18743 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18744 set. */
ab432490 18745 load_partial_comp_unit (per_cu, cu->per_objfile);
5afb4e99 18746
d590ff25 18747 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18748 }
18749
18750 if (pd == NULL)
18751 internal_error (__FILE__, __LINE__,
9d8780f0 18752 _("could not find partial DIE %s "
3e43a32a 18753 "in cache [from module %s]\n"),
9d8780f0 18754 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18755 return { per_cu->cu, pd };
72bf9492
DJ
18756}
18757
abc72ce4
DE
18758/* See if we can figure out if the class lives in a namespace. We do
18759 this by looking for a member function; its demangled name will
18760 contain namespace info, if there is any. */
18761
18762static void
18763guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18764 struct dwarf2_cu *cu)
18765{
18766 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18767 what template types look like, because the demangler
18768 frequently doesn't give the same name as the debug info. We
18769 could fix this by only using the demangled name to get the
18770 prefix (but see comment in read_structure_type). */
18771
18772 struct partial_die_info *real_pdi;
18773 struct partial_die_info *child_pdi;
18774
18775 /* If this DIE (this DIE's specification, if any) has a parent, then
18776 we should not do this. We'll prepend the parent's fully qualified
18777 name when we create the partial symbol. */
18778
18779 real_pdi = struct_pdi;
18780 while (real_pdi->has_specification)
fb816e8b 18781 {
122cf0f2
AB
18782 auto res = find_partial_die (real_pdi->spec_offset,
18783 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18784 real_pdi = res.pdi;
18785 cu = res.cu;
18786 }
abc72ce4
DE
18787
18788 if (real_pdi->die_parent != NULL)
18789 return;
18790
18791 for (child_pdi = struct_pdi->die_child;
18792 child_pdi != NULL;
18793 child_pdi = child_pdi->die_sibling)
18794 {
18795 if (child_pdi->tag == DW_TAG_subprogram
18796 && child_pdi->linkage_name != NULL)
18797 {
43816ebc
TT
18798 gdb::unique_xmalloc_ptr<char> actual_class_name
18799 (language_class_name_from_physname (cu->language_defn,
18800 child_pdi->linkage_name));
abc72ce4
DE
18801 if (actual_class_name != NULL)
18802 {
5e22e966 18803 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18804 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18805 }
18806 break;
18807 }
18808 }
18809}
18810
25c11aca
TV
18811/* Return true if a DIE with TAG may have the DW_AT_const_value
18812 attribute. */
18813
18814static bool
18815can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18816{
18817 switch (tag)
18818 {
18819 case DW_TAG_constant:
18820 case DW_TAG_enumerator:
18821 case DW_TAG_formal_parameter:
18822 case DW_TAG_template_value_param:
18823 case DW_TAG_variable:
18824 return true;
18825 }
18826
18827 return false;
18828}
18829
52356b79
YQ
18830void
18831partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18832{
abc72ce4
DE
18833 /* Once we've fixed up a die, there's no point in doing so again.
18834 This also avoids a memory leak if we were to call
18835 guess_partial_die_structure_name multiple times. */
52356b79 18836 if (fixup_called)
abc72ce4
DE
18837 return;
18838
72bf9492
DJ
18839 /* If we found a reference attribute and the DIE has no name, try
18840 to find a name in the referred to DIE. */
18841
52356b79 18842 if (name == NULL && has_specification)
72bf9492
DJ
18843 {
18844 struct partial_die_info *spec_die;
72bf9492 18845
122cf0f2 18846 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18847 spec_die = res.pdi;
18848 cu = res.cu;
72bf9492 18849
52356b79 18850 spec_die->fixup (cu);
72bf9492
DJ
18851
18852 if (spec_die->name)
18853 {
52356b79 18854 name = spec_die->name;
72bf9492
DJ
18855
18856 /* Copy DW_AT_external attribute if it is set. */
18857 if (spec_die->is_external)
52356b79 18858 is_external = spec_die->is_external;
72bf9492
DJ
18859 }
18860 }
18861
25c11aca
TV
18862 if (!has_const_value && has_specification
18863 && can_have_DW_AT_const_value_p (tag))
18864 {
18865 struct partial_die_info *spec_die;
18866
18867 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18868 spec_die = res.pdi;
18869 cu = res.cu;
18870
18871 spec_die->fixup (cu);
18872
18873 if (spec_die->has_const_value)
18874 {
18875 /* Copy DW_AT_const_value attribute if it is set. */
18876 has_const_value = spec_die->has_const_value;
18877 }
18878 }
18879
72bf9492 18880 /* Set default names for some unnamed DIEs. */
72bf9492 18881
52356b79
YQ
18882 if (name == NULL && tag == DW_TAG_namespace)
18883 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18884
abc72ce4
DE
18885 /* If there is no parent die to provide a namespace, and there are
18886 children, see if we can determine the namespace from their linkage
122d1940 18887 name. */
abc72ce4 18888 if (cu->language == language_cplus
5e22e966 18889 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18890 && die_parent == NULL
18891 && has_children
18892 && (tag == DW_TAG_class_type
18893 || tag == DW_TAG_structure_type
18894 || tag == DW_TAG_union_type))
18895 guess_partial_die_structure_name (this, cu);
abc72ce4 18896
53832f31
TT
18897 /* GCC might emit a nameless struct or union that has a linkage
18898 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18899 if (name == NULL
18900 && (tag == DW_TAG_class_type
18901 || tag == DW_TAG_interface_type
18902 || tag == DW_TAG_structure_type
18903 || tag == DW_TAG_union_type)
18904 && linkage_name != NULL)
53832f31 18905 {
43816ebc
TT
18906 gdb::unique_xmalloc_ptr<char> demangled
18907 (gdb_demangle (linkage_name, DMGL_TYPES));
18908 if (demangled != nullptr)
53832f31 18909 {
96408a79
SA
18910 const char *base;
18911
18912 /* Strip any leading namespaces/classes, keep only the base name.
18913 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18914 base = strrchr (demangled.get (), ':');
18915 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18916 base++;
18917 else
43816ebc 18918 base = demangled.get ();
96408a79 18919
5e22e966 18920 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18921 name = objfile->intern (base);
53832f31
TT
18922 }
18923 }
18924
52356b79 18925 fixup_called = 1;
72bf9492
DJ
18926}
18927
41144253 18928/* Read the .debug_loclists header contents from the given SECTION in the
18929 HEADER. */
18930static void
18931read_loclist_header (struct loclist_header *header,
18932 struct dwarf2_section_info *section)
18933{
18934 unsigned int bytes_read;
18935 bfd *abfd = section->get_bfd_owner ();
18936 const gdb_byte *info_ptr = section->buffer;
18937 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18938 info_ptr += bytes_read;
18939 header->version = read_2_bytes (abfd, info_ptr);
18940 info_ptr += 2;
18941 header->addr_size = read_1_byte (abfd, info_ptr);
18942 info_ptr += 1;
18943 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18944 info_ptr += 1;
18945 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18946}
18947
18948/* Return the DW_AT_loclists_base value for the CU. */
18949static ULONGEST
18950lookup_loclist_base (struct dwarf2_cu *cu)
18951{
18952 /* For the .dwo unit, the loclist_base points to the first offset following
18953 the header. The header consists of the following entities-
18954 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18955 bit format)
18956 2. version (2 bytes)
18957 3. address size (1 byte)
18958 4. segment selector size (1 byte)
18959 5. offset entry count (4 bytes)
18960 These sizes are derived as per the DWARFv5 standard. */
18961 if (cu->dwo_unit != nullptr)
18962 {
18963 if (cu->header.initial_length_size == 4)
18964 return LOCLIST_HEADER_SIZE32;
18965 return LOCLIST_HEADER_SIZE64;
18966 }
18967 return cu->loclist_base;
18968}
18969
18970/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18971 array of offsets in the .debug_loclists section. */
18972static CORE_ADDR
18973read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18974{
5e22e966 18975 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18976 struct objfile *objfile = dwarf2_per_objfile->objfile;
18977 bfd *abfd = objfile->obfd;
18978 ULONGEST loclist_base = lookup_loclist_base (cu);
18979 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18980
18981 section->read (objfile);
18982 if (section->buffer == NULL)
18983 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18984 "section [in module %s]"), objfile_name (objfile));
18985 struct loclist_header header;
18986 read_loclist_header (&header, section);
18987 if (loclist_index >= header.offset_entry_count)
18988 complaint (_("DW_FORM_loclistx pointing outside of "
18989 ".debug_loclists offset array [in module %s]"),
18990 objfile_name (objfile));
18991 if (loclist_base + loclist_index * cu->header.offset_size
18992 >= section->size)
18993 complaint (_("DW_FORM_loclistx pointing outside of "
18994 ".debug_loclists section [in module %s]"),
18995 objfile_name (objfile));
18996 const gdb_byte *info_ptr
18997 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18998
18999 if (cu->header.offset_size == 4)
19000 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19001 else
19002 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19003}
19004
18a8505e
AT
19005/* Process the attributes that had to be skipped in the first round. These
19006 attributes are the ones that need str_offsets_base or addr_base attributes.
19007 They could not have been processed in the first round, because at the time
19008 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19009static void
19010read_attribute_reprocess (const struct die_reader_specs *reader,
19011 struct attribute *attr)
18a8505e
AT
19012{
19013 struct dwarf2_cu *cu = reader->cu;
19014 switch (attr->form)
19015 {
19016 case DW_FORM_addrx:
19017 case DW_FORM_GNU_addr_index:
19018 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19019 break;
41144253 19020 case DW_FORM_loclistx:
19021 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19022 break;
18a8505e
AT
19023 case DW_FORM_strx:
19024 case DW_FORM_strx1:
19025 case DW_FORM_strx2:
19026 case DW_FORM_strx3:
19027 case DW_FORM_strx4:
19028 case DW_FORM_GNU_str_index:
19029 {
19030 unsigned int str_index = DW_UNSND (attr);
19031 if (reader->dwo_file != NULL)
19032 {
19033 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19034 DW_STRING_IS_CANONICAL (attr) = 0;
19035 }
19036 else
19037 {
19038 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19039 DW_STRING_IS_CANONICAL (attr) = 0;
19040 }
19041 break;
19042 }
19043 default:
19044 gdb_assert_not_reached (_("Unexpected DWARF form."));
19045 }
19046}
19047
a8329558 19048/* Read an attribute value described by an attribute form. */
c906108c 19049
d521ce57 19050static const gdb_byte *
dee91e82
DE
19051read_attribute_value (const struct die_reader_specs *reader,
19052 struct attribute *attr, unsigned form,
18a8505e
AT
19053 LONGEST implicit_const, const gdb_byte *info_ptr,
19054 bool *need_reprocess)
c906108c 19055{
dee91e82 19056 struct dwarf2_cu *cu = reader->cu;
5e22e966 19057 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19058 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19059 bfd *abfd = reader->abfd;
e7c27a73 19060 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19061 unsigned int bytes_read;
19062 struct dwarf_block *blk;
18a8505e 19063 *need_reprocess = false;
c906108c 19064
aead7601 19065 attr->form = (enum dwarf_form) form;
a8329558 19066 switch (form)
c906108c 19067 {
c906108c 19068 case DW_FORM_ref_addr:
ae411497 19069 if (cu->header.version == 2)
c8a7a66f
TT
19070 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19071 &bytes_read);
ae411497 19072 else
8266302d
TT
19073 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19074 &bytes_read);
ae411497
TT
19075 info_ptr += bytes_read;
19076 break;
36586728 19077 case DW_FORM_GNU_ref_alt:
8266302d 19078 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19079 info_ptr += bytes_read;
19080 break;
ae411497 19081 case DW_FORM_addr:
08feed99
TT
19082 {
19083 struct gdbarch *gdbarch = objfile->arch ();
19084 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19085 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19086 info_ptr += bytes_read;
19087 }
c906108c
SS
19088 break;
19089 case DW_FORM_block2:
7b5a2f43 19090 blk = dwarf_alloc_block (cu);
c906108c
SS
19091 blk->size = read_2_bytes (abfd, info_ptr);
19092 info_ptr += 2;
19093 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19094 info_ptr += blk->size;
19095 DW_BLOCK (attr) = blk;
19096 break;
19097 case DW_FORM_block4:
7b5a2f43 19098 blk = dwarf_alloc_block (cu);
c906108c
SS
19099 blk->size = read_4_bytes (abfd, info_ptr);
19100 info_ptr += 4;
19101 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19102 info_ptr += blk->size;
19103 DW_BLOCK (attr) = blk;
19104 break;
19105 case DW_FORM_data2:
19106 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19107 info_ptr += 2;
19108 break;
19109 case DW_FORM_data4:
19110 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19111 info_ptr += 4;
19112 break;
19113 case DW_FORM_data8:
19114 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19115 info_ptr += 8;
19116 break;
0224619f
JK
19117 case DW_FORM_data16:
19118 blk = dwarf_alloc_block (cu);
19119 blk->size = 16;
19120 blk->data = read_n_bytes (abfd, info_ptr, 16);
19121 info_ptr += 16;
19122 DW_BLOCK (attr) = blk;
19123 break;
2dc7f7b3 19124 case DW_FORM_sec_offset:
8266302d 19125 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19126 info_ptr += bytes_read;
19127 break;
41144253 19128 case DW_FORM_loclistx:
19129 {
19130 *need_reprocess = true;
19131 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19132 info_ptr += bytes_read;
19133 }
19134 break;
c906108c 19135 case DW_FORM_string:
9b1c24c8 19136 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19137 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19138 info_ptr += bytes_read;
19139 break;
4bdf3d34 19140 case DW_FORM_strp:
36586728
TT
19141 if (!cu->per_cu->is_dwz)
19142 {
ed2dc618
SM
19143 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19144 abfd, info_ptr, cu_header,
36586728
TT
19145 &bytes_read);
19146 DW_STRING_IS_CANONICAL (attr) = 0;
19147 info_ptr += bytes_read;
19148 break;
19149 }
19150 /* FALLTHROUGH */
43988095
JK
19151 case DW_FORM_line_strp:
19152 if (!cu->per_cu->is_dwz)
19153 {
86c0bb4c
TT
19154 DW_STRING (attr)
19155 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19156 &bytes_read);
43988095
JK
19157 DW_STRING_IS_CANONICAL (attr) = 0;
19158 info_ptr += bytes_read;
19159 break;
19160 }
19161 /* FALLTHROUGH */
36586728
TT
19162 case DW_FORM_GNU_strp_alt:
19163 {
c3699833 19164 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19165 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19166 &bytes_read);
36586728 19167
0314b390 19168 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19169 DW_STRING_IS_CANONICAL (attr) = 0;
19170 info_ptr += bytes_read;
19171 }
4bdf3d34 19172 break;
2dc7f7b3 19173 case DW_FORM_exprloc:
c906108c 19174 case DW_FORM_block:
7b5a2f43 19175 blk = dwarf_alloc_block (cu);
c906108c
SS
19176 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19179 info_ptr += blk->size;
19180 DW_BLOCK (attr) = blk;
19181 break;
19182 case DW_FORM_block1:
7b5a2f43 19183 blk = dwarf_alloc_block (cu);
c906108c
SS
19184 blk->size = read_1_byte (abfd, info_ptr);
19185 info_ptr += 1;
19186 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19187 info_ptr += blk->size;
19188 DW_BLOCK (attr) = blk;
19189 break;
19190 case DW_FORM_data1:
19191 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19192 info_ptr += 1;
19193 break;
19194 case DW_FORM_flag:
19195 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19196 info_ptr += 1;
19197 break;
2dc7f7b3
TT
19198 case DW_FORM_flag_present:
19199 DW_UNSND (attr) = 1;
19200 break;
c906108c
SS
19201 case DW_FORM_sdata:
19202 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19203 info_ptr += bytes_read;
19204 break;
19205 case DW_FORM_udata:
18a8505e 19206 case DW_FORM_rnglistx:
c906108c
SS
19207 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19208 info_ptr += bytes_read;
19209 break;
19210 case DW_FORM_ref1:
9c541725 19211 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19212 + read_1_byte (abfd, info_ptr));
c906108c
SS
19213 info_ptr += 1;
19214 break;
19215 case DW_FORM_ref2:
9c541725 19216 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19217 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19218 info_ptr += 2;
19219 break;
19220 case DW_FORM_ref4:
9c541725 19221 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19222 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19223 info_ptr += 4;
19224 break;
613e1657 19225 case DW_FORM_ref8:
9c541725 19226 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19227 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19228 info_ptr += 8;
19229 break;
55f1336d 19230 case DW_FORM_ref_sig8:
ac9ec31b 19231 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19232 info_ptr += 8;
19233 break;
c906108c 19234 case DW_FORM_ref_udata:
9c541725 19235 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19236 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19237 info_ptr += bytes_read;
19238 break;
c906108c 19239 case DW_FORM_indirect:
a8329558
KW
19240 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19241 info_ptr += bytes_read;
43988095
JK
19242 if (form == DW_FORM_implicit_const)
19243 {
19244 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19245 info_ptr += bytes_read;
19246 }
19247 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19248 info_ptr, need_reprocess);
43988095
JK
19249 break;
19250 case DW_FORM_implicit_const:
19251 DW_SND (attr) = implicit_const;
a8329558 19252 break;
336d760d 19253 case DW_FORM_addrx:
3019eac3 19254 case DW_FORM_GNU_addr_index:
18a8505e
AT
19255 *need_reprocess = true;
19256 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19257 info_ptr += bytes_read;
19258 break;
cf532bd1 19259 case DW_FORM_strx:
15f18d14
AT
19260 case DW_FORM_strx1:
19261 case DW_FORM_strx2:
19262 case DW_FORM_strx3:
19263 case DW_FORM_strx4:
3019eac3 19264 case DW_FORM_GNU_str_index:
3019eac3 19265 {
15f18d14
AT
19266 ULONGEST str_index;
19267 if (form == DW_FORM_strx1)
19268 {
19269 str_index = read_1_byte (abfd, info_ptr);
19270 info_ptr += 1;
19271 }
19272 else if (form == DW_FORM_strx2)
19273 {
19274 str_index = read_2_bytes (abfd, info_ptr);
19275 info_ptr += 2;
19276 }
19277 else if (form == DW_FORM_strx3)
19278 {
19279 str_index = read_3_bytes (abfd, info_ptr);
19280 info_ptr += 3;
19281 }
19282 else if (form == DW_FORM_strx4)
19283 {
19284 str_index = read_4_bytes (abfd, info_ptr);
19285 info_ptr += 4;
19286 }
19287 else
19288 {
19289 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19290 info_ptr += bytes_read;
19291 }
18a8505e
AT
19292 *need_reprocess = true;
19293 DW_UNSND (attr) = str_index;
19294 }
3019eac3 19295 break;
c906108c 19296 default:
8a3fe4f8 19297 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19298 dwarf_form_name (form),
19299 bfd_get_filename (abfd));
c906108c 19300 }
28e94949 19301
36586728 19302 /* Super hack. */
cd6c91b4 19303 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19304 attr->form = DW_FORM_GNU_ref_alt;
19305
28e94949
JB
19306 /* We have seen instances where the compiler tried to emit a byte
19307 size attribute of -1 which ended up being encoded as an unsigned
19308 0xffffffff. Although 0xffffffff is technically a valid size value,
19309 an object of this size seems pretty unlikely so we can relatively
19310 safely treat these cases as if the size attribute was invalid and
19311 treat them as zero by default. */
19312 if (attr->name == DW_AT_byte_size
19313 && form == DW_FORM_data4
19314 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19315 {
19316 complaint
b98664d3 19317 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19318 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19319 DW_UNSND (attr) = 0;
19320 }
28e94949 19321
c906108c
SS
19322 return info_ptr;
19323}
19324
a8329558
KW
19325/* Read an attribute described by an abbreviated attribute. */
19326
d521ce57 19327static const gdb_byte *
dee91e82
DE
19328read_attribute (const struct die_reader_specs *reader,
19329 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19330 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19331{
19332 attr->name = abbrev->name;
43988095 19333 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19334 abbrev->implicit_const, info_ptr,
19335 need_reprocess);
a8329558
KW
19336}
19337
43988095
JK
19338/* Return pointer to string at .debug_str offset STR_OFFSET. */
19339
19340static const char *
ed2dc618 19341read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19342 LONGEST str_offset)
43988095 19343{
5989a64e
SM
19344 return dwarf2_per_objfile->per_bfd->str.read_string
19345 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19346}
19347
43988095
JK
19348/* Return pointer to string at .debug_str offset as read from BUF.
19349 BUF is assumed to be in a compilation unit described by CU_HEADER.
19350 Return *BYTES_READ_PTR count of bytes read from BUF. */
19351
d521ce57 19352static const char *
ed2dc618
SM
19353read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19354 const gdb_byte *buf,
cf2c3c16
TT
19355 const struct comp_unit_head *cu_header,
19356 unsigned int *bytes_read_ptr)
19357{
8266302d 19358 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19359
4f44ae6c 19360 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19361}
19362
86c0bb4c 19363/* See read.h. */
43988095 19364
86c0bb4c
TT
19365const char *
19366dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19367 const struct comp_unit_head *cu_header,
19368 unsigned int *bytes_read_ptr)
43988095 19369{
86c0bb4c 19370 bfd *abfd = objfile->obfd;
8266302d 19371 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19372
5989a64e 19373 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19374}
19375
3019eac3 19376/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19377 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19378 ADDR_SIZE is the size of addresses from the CU header. */
19379
19380static CORE_ADDR
ed2dc618 19381read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19382 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19383 int addr_size)
3019eac3
DE
19384{
19385 struct objfile *objfile = dwarf2_per_objfile->objfile;
19386 bfd *abfd = objfile->obfd;
19387 const gdb_byte *info_ptr;
18a8505e 19388 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19389
5989a64e
SM
19390 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19391 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19392 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19393 objfile_name (objfile));
18a8505e 19394 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19395 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19396 error (_("DW_FORM_addr_index pointing outside of "
19397 ".debug_addr section [in module %s]"),
4262abfb 19398 objfile_name (objfile));
5989a64e 19399 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19400 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19401 if (addr_size == 4)
19402 return bfd_get_32 (abfd, info_ptr);
19403 else
19404 return bfd_get_64 (abfd, info_ptr);
19405}
19406
19407/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19408
19409static CORE_ADDR
19410read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19411{
5e22e966 19412 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19413 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19414}
19415
19416/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19417
19418static CORE_ADDR
d521ce57 19419read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19420 unsigned int *bytes_read)
19421{
5e22e966 19422 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19423 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19424
19425 return read_addr_index (cu, addr_index);
19426}
19427
450a1bfc 19428/* See read.h. */
3019eac3
DE
19429
19430CORE_ADDR
82ca3f51
SM
19431dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19432 dwarf2_per_objfile *dwarf2_per_objfile,
19433 unsigned int addr_index)
3019eac3 19434{
3019eac3 19435 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19436 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19437 int addr_size;
19438
3019eac3
DE
19439 /* We need addr_base and addr_size.
19440 If we don't have PER_CU->cu, we have to get it.
19441 Nasty, but the alternative is storing the needed info in PER_CU,
19442 which at this point doesn't seem justified: it's not clear how frequently
19443 it would get used and it would increase the size of every PER_CU.
19444 Entry points like dwarf2_per_cu_addr_size do a similar thing
19445 so we're not in uncharted territory here.
19446 Alas we need to be a bit more complicated as addr_base is contained
19447 in the DIE.
19448
19449 We don't need to read the entire CU(/TU).
19450 We just need the header and top level die.
a1b64ce1 19451
3019eac3 19452 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19453 For now we skip this optimization. */
3019eac3
DE
19454
19455 if (cu != NULL)
19456 {
19457 addr_base = cu->addr_base;
19458 addr_size = cu->header.addr_size;
19459 }
19460 else
19461 {
ab432490 19462 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
c0ab21c2
TT
19463 addr_base = reader.cu->addr_base;
19464 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19465 }
19466
ed2dc618
SM
19467 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19468 addr_size);
3019eac3
DE
19469}
19470
18a8505e
AT
19471/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19472 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19473 DWO file. */
3019eac3 19474
d521ce57 19475static const char *
18a8505e
AT
19476read_str_index (struct dwarf2_cu *cu,
19477 struct dwarf2_section_info *str_section,
19478 struct dwarf2_section_info *str_offsets_section,
19479 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19480{
5e22e966 19481 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19482 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19483 const char *objf_name = objfile_name (objfile);
3019eac3 19484 bfd *abfd = objfile->obfd;
d521ce57 19485 const gdb_byte *info_ptr;
3019eac3 19486 ULONGEST str_offset;
cf532bd1 19487 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19488
96b79293
TT
19489 str_section->read (objfile);
19490 str_offsets_section->read (objfile);
73869dc2 19491 if (str_section->buffer == NULL)
18a8505e 19492 error (_("%s used without %s section"
9d8780f0 19493 " in CU at offset %s [in module %s]"),
96b79293 19494 form_name, str_section->get_name (),
18a8505e 19495 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19496 if (str_offsets_section->buffer == NULL)
18a8505e 19497 error (_("%s used without %s section"
9d8780f0 19498 " in CU at offset %s [in module %s]"),
96b79293 19499 form_name, str_section->get_name (),
18a8505e 19500 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19501 info_ptr = (str_offsets_section->buffer
18a8505e 19502 + str_offsets_base
3019eac3
DE
19503 + str_index * cu->header.offset_size);
19504 if (cu->header.offset_size == 4)
19505 str_offset = bfd_get_32 (abfd, info_ptr);
19506 else
19507 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19508 if (str_offset >= str_section->size)
57d63ce2 19509 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19510 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19511 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19512 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19513}
19514
18a8505e
AT
19515/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19516
19517static const char *
19518read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19519{
19520 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19521 ? reader->cu->header.addr_size : 0;
19522 return read_str_index (reader->cu,
19523 &reader->dwo_file->sections.str,
19524 &reader->dwo_file->sections.str_offsets,
19525 str_offsets_base, str_index);
19526}
19527
19528/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19529
19530static const char *
19531read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19532{
5e22e966 19533 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19534 const char *objf_name = objfile_name (objfile);
19535 static const char form_name[] = "DW_FORM_GNU_str_index";
19536 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19537
19538 if (!cu->str_offsets_base.has_value ())
19539 error (_("%s used in Fission stub without %s"
19540 " in CU at offset 0x%lx [in module %s]"),
19541 form_name, str_offsets_attr_name,
19542 (long) cu->header.offset_size, objf_name);
19543
19544 return read_str_index (cu,
5e22e966
SM
19545 &cu->per_objfile->per_bfd->str,
19546 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19547 *cu->str_offsets_base, str_index);
19548}
19549
3019eac3
DE
19550/* Return the length of an LEB128 number in BUF. */
19551
19552static int
19553leb128_size (const gdb_byte *buf)
19554{
19555 const gdb_byte *begin = buf;
19556 gdb_byte byte;
19557
19558 while (1)
19559 {
19560 byte = *buf++;
19561 if ((byte & 128) == 0)
19562 return buf - begin;
19563 }
19564}
19565
c906108c 19566static void
e142c38c 19567set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19568{
19569 switch (lang)
19570 {
19571 case DW_LANG_C89:
76bee0cc 19572 case DW_LANG_C99:
0cfd832f 19573 case DW_LANG_C11:
c906108c 19574 case DW_LANG_C:
d1be3247 19575 case DW_LANG_UPC:
e142c38c 19576 cu->language = language_c;
c906108c 19577 break;
9c37b5ae 19578 case DW_LANG_Java:
c906108c 19579 case DW_LANG_C_plus_plus:
0cfd832f
MW
19580 case DW_LANG_C_plus_plus_11:
19581 case DW_LANG_C_plus_plus_14:
e142c38c 19582 cu->language = language_cplus;
c906108c 19583 break;
6aecb9c2
JB
19584 case DW_LANG_D:
19585 cu->language = language_d;
19586 break;
c906108c
SS
19587 case DW_LANG_Fortran77:
19588 case DW_LANG_Fortran90:
b21b22e0 19589 case DW_LANG_Fortran95:
f7de9aab
MW
19590 case DW_LANG_Fortran03:
19591 case DW_LANG_Fortran08:
e142c38c 19592 cu->language = language_fortran;
c906108c 19593 break;
a766d390
DE
19594 case DW_LANG_Go:
19595 cu->language = language_go;
19596 break;
c906108c 19597 case DW_LANG_Mips_Assembler:
e142c38c 19598 cu->language = language_asm;
c906108c
SS
19599 break;
19600 case DW_LANG_Ada83:
8aaf0b47 19601 case DW_LANG_Ada95:
bc5f45f8
JB
19602 cu->language = language_ada;
19603 break;
72019c9c
GM
19604 case DW_LANG_Modula2:
19605 cu->language = language_m2;
19606 break;
fe8e67fd
PM
19607 case DW_LANG_Pascal83:
19608 cu->language = language_pascal;
19609 break;
22566fbd
DJ
19610 case DW_LANG_ObjC:
19611 cu->language = language_objc;
19612 break;
c44af4eb
TT
19613 case DW_LANG_Rust:
19614 case DW_LANG_Rust_old:
19615 cu->language = language_rust;
19616 break;
c906108c
SS
19617 case DW_LANG_Cobol74:
19618 case DW_LANG_Cobol85:
c906108c 19619 default:
e142c38c 19620 cu->language = language_minimal;
c906108c
SS
19621 break;
19622 }
e142c38c 19623 cu->language_defn = language_def (cu->language);
c906108c
SS
19624}
19625
19626/* Return the named attribute or NULL if not there. */
19627
19628static struct attribute *
e142c38c 19629dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19630{
a48e046c 19631 for (;;)
c906108c 19632 {
a48e046c
TT
19633 unsigned int i;
19634 struct attribute *spec = NULL;
19635
19636 for (i = 0; i < die->num_attrs; ++i)
19637 {
19638 if (die->attrs[i].name == name)
19639 return &die->attrs[i];
19640 if (die->attrs[i].name == DW_AT_specification
19641 || die->attrs[i].name == DW_AT_abstract_origin)
19642 spec = &die->attrs[i];
19643 }
19644
19645 if (!spec)
19646 break;
c906108c 19647
f2f0e013 19648 die = follow_die_ref (die, spec, &cu);
f2f0e013 19649 }
c5aa993b 19650
c906108c
SS
19651 return NULL;
19652}
19653
7d45c7c3
KB
19654/* Return the string associated with a string-typed attribute, or NULL if it
19655 is either not found or is of an incorrect type. */
19656
19657static const char *
19658dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19659{
19660 struct attribute *attr;
19661 const char *str = NULL;
19662
19663 attr = dwarf2_attr (die, name, cu);
19664
19665 if (attr != NULL)
19666 {
e61108c9
TT
19667 str = attr->value_as_string ();
19668 if (str == nullptr)
b98664d3 19669 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19670 "DIE at %s in module %s"),
19671 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19672 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19673 }
19674
19675 return str;
19676}
19677
a084a2a6 19678/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19679 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19680static const char *
19681dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19682{
19683 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19684 if (dwo_name == nullptr)
19685 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19686 return dwo_name;
19687}
19688
05cf31d1
JB
19689/* Return non-zero iff the attribute NAME is defined for the given DIE,
19690 and holds a non-zero value. This function should only be used for
2dc7f7b3 19691 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19692
19693static int
19694dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19695{
19696 struct attribute *attr = dwarf2_attr (die, name, cu);
19697
19698 return (attr && DW_UNSND (attr));
19699}
19700
3ca72b44 19701static int
e142c38c 19702die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19703{
05cf31d1
JB
19704 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19705 which value is non-zero. However, we have to be careful with
19706 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19707 (via dwarf2_flag_true_p) follows this attribute. So we may
19708 end up accidently finding a declaration attribute that belongs
19709 to a different DIE referenced by the specification attribute,
19710 even though the given DIE does not have a declaration attribute. */
19711 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19712 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19713}
19714
63d06c5c 19715/* Return the die giving the specification for DIE, if there is
f2f0e013 19716 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19717 containing the return value on output. If there is no
19718 specification, but there is an abstract origin, that is
19719 returned. */
63d06c5c
DC
19720
19721static struct die_info *
f2f0e013 19722die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19723{
f2f0e013
DJ
19724 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19725 *spec_cu);
63d06c5c 19726
edb3359d
DJ
19727 if (spec_attr == NULL)
19728 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19729
63d06c5c
DC
19730 if (spec_attr == NULL)
19731 return NULL;
19732 else
f2f0e013 19733 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19734}
c906108c 19735
527f3840
JK
19736/* Stub for free_line_header to match void * callback types. */
19737
19738static void
19739free_line_header_voidp (void *arg)
19740{
9a3c8263 19741 struct line_header *lh = (struct line_header *) arg;
527f3840 19742
fff8551c 19743 delete lh;
527f3840
JK
19744}
19745
83769d0b 19746/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19747
19748static struct dwarf2_section_info *
19749get_debug_line_section (struct dwarf2_cu *cu)
19750{
19751 struct dwarf2_section_info *section;
5e22e966 19752 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19753
19754 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19755 DWO file. */
19756 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19757 section = &cu->dwo_unit->dwo_file->sections.line;
19758 else if (cu->per_cu->is_dwz)
19759 {
c3699833 19760 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19761
19762 section = &dwz->line;
19763 }
19764 else
5989a64e 19765 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19766
19767 return section;
19768}
19769
debd256d 19770/* Read the statement program header starting at OFFSET in
3019eac3 19771 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19772 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19773 Returns NULL if there is a problem reading the header, e.g., if it
19774 has a version we don't understand.
debd256d
JB
19775
19776 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19777 the returned object point into the dwarf line section buffer,
19778 and must not be freed. */
ae2de4f8 19779
fff8551c 19780static line_header_up
9c541725 19781dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19782{
3019eac3 19783 struct dwarf2_section_info *section;
5e22e966 19784 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19785
36586728 19786 section = get_debug_line_section (cu);
96b79293 19787 section->read (dwarf2_per_objfile->objfile);
3019eac3 19788 if (section->buffer == NULL)
debd256d 19789 {
3019eac3 19790 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19791 complaint (_("missing .debug_line.dwo section"));
3019eac3 19792 else
b98664d3 19793 complaint (_("missing .debug_line section"));
debd256d
JB
19794 return 0;
19795 }
19796
0df7ad3a
TT
19797 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19798 dwarf2_per_objfile, section,
19799 &cu->header);
debd256d 19800}
c906108c 19801
c6da4cef 19802/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19803 Return the file name of the psymtab for the given file_entry.
c6da4cef 19804 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19805 If space for the result is malloc'd, *NAME_HOLDER will be set.
19806 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19807
d521ce57 19808static const char *
7ba99d21 19809psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19810 const dwarf2_psymtab *pst,
c89b44cd
TT
19811 const char *comp_dir,
19812 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19813{
d521ce57
TT
19814 const char *include_name = fe.name;
19815 const char *include_name_to_compare = include_name;
72b9f47f 19816 const char *pst_filename;
c6da4cef
DE
19817 int file_is_pst;
19818
8c43009f 19819 const char *dir_name = fe.include_dir (lh);
c6da4cef 19820
c89b44cd 19821 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19822 if (!IS_ABSOLUTE_PATH (include_name)
19823 && (dir_name != NULL || comp_dir != NULL))
19824 {
19825 /* Avoid creating a duplicate psymtab for PST.
19826 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19827 Before we do the comparison, however, we need to account
19828 for DIR_NAME and COMP_DIR.
19829 First prepend dir_name (if non-NULL). If we still don't
19830 have an absolute path prepend comp_dir (if non-NULL).
19831 However, the directory we record in the include-file's
19832 psymtab does not contain COMP_DIR (to match the
19833 corresponding symtab(s)).
19834
19835 Example:
19836
19837 bash$ cd /tmp
19838 bash$ gcc -g ./hello.c
19839 include_name = "hello.c"
19840 dir_name = "."
19841 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19842 DW_AT_name = "./hello.c"
19843
19844 */
c6da4cef
DE
19845
19846 if (dir_name != NULL)
19847 {
c89b44cd
TT
19848 name_holder->reset (concat (dir_name, SLASH_STRING,
19849 include_name, (char *) NULL));
19850 include_name = name_holder->get ();
c6da4cef 19851 include_name_to_compare = include_name;
c6da4cef
DE
19852 }
19853 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19854 {
c89b44cd
TT
19855 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19856 include_name, (char *) NULL));
19857 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19858 }
19859 }
19860
19861 pst_filename = pst->filename;
c89b44cd 19862 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19863 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19864 {
c89b44cd
TT
19865 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19866 pst_filename, (char *) NULL));
19867 pst_filename = copied_name.get ();
c6da4cef
DE
19868 }
19869
1e3fad37 19870 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19871
c6da4cef
DE
19872 if (file_is_pst)
19873 return NULL;
19874 return include_name;
19875}
19876
d9b3de22
DE
19877/* State machine to track the state of the line number program. */
19878
6f77053d 19879class lnp_state_machine
d9b3de22 19880{
6f77053d
PA
19881public:
19882 /* Initialize a machine state for the start of a line number
19883 program. */
804d2729
TT
19884 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19885 bool record_lines_p);
6f77053d 19886
8c43009f
PA
19887 file_entry *current_file ()
19888 {
19889 /* lh->file_names is 0-based, but the file name numbers in the
19890 statement program are 1-based. */
6f77053d
PA
19891 return m_line_header->file_name_at (m_file);
19892 }
19893
19894 /* Record the line in the state machine. END_SEQUENCE is true if
19895 we're processing the end of a sequence. */
19896 void record_line (bool end_sequence);
19897
7ab6656f
OJ
19898 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19899 nop-out rest of the lines in this sequence. */
6f77053d
PA
19900 void check_line_address (struct dwarf2_cu *cu,
19901 const gdb_byte *line_ptr,
7ab6656f 19902 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19903
19904 void handle_set_discriminator (unsigned int discriminator)
19905 {
19906 m_discriminator = discriminator;
19907 m_line_has_non_zero_discriminator |= discriminator != 0;
19908 }
19909
19910 /* Handle DW_LNE_set_address. */
19911 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19912 {
19913 m_op_index = 0;
19914 address += baseaddr;
19915 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19916 }
19917
19918 /* Handle DW_LNS_advance_pc. */
19919 void handle_advance_pc (CORE_ADDR adjust);
19920
19921 /* Handle a special opcode. */
19922 void handle_special_opcode (unsigned char op_code);
19923
19924 /* Handle DW_LNS_advance_line. */
19925 void handle_advance_line (int line_delta)
19926 {
19927 advance_line (line_delta);
19928 }
19929
19930 /* Handle DW_LNS_set_file. */
19931 void handle_set_file (file_name_index file);
19932
19933 /* Handle DW_LNS_negate_stmt. */
19934 void handle_negate_stmt ()
19935 {
19936 m_is_stmt = !m_is_stmt;
19937 }
19938
19939 /* Handle DW_LNS_const_add_pc. */
19940 void handle_const_add_pc ();
19941
19942 /* Handle DW_LNS_fixed_advance_pc. */
19943 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19944 {
19945 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19946 m_op_index = 0;
19947 }
19948
19949 /* Handle DW_LNS_copy. */
19950 void handle_copy ()
19951 {
19952 record_line (false);
19953 m_discriminator = 0;
19954 }
19955
19956 /* Handle DW_LNE_end_sequence. */
19957 void handle_end_sequence ()
19958 {
804d2729 19959 m_currently_recording_lines = true;
6f77053d
PA
19960 }
19961
19962private:
19963 /* Advance the line by LINE_DELTA. */
19964 void advance_line (int line_delta)
19965 {
19966 m_line += line_delta;
19967
19968 if (line_delta != 0)
19969 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19970 }
19971
804d2729
TT
19972 struct dwarf2_cu *m_cu;
19973
6f77053d
PA
19974 gdbarch *m_gdbarch;
19975
19976 /* True if we're recording lines.
19977 Otherwise we're building partial symtabs and are just interested in
19978 finding include files mentioned by the line number program. */
19979 bool m_record_lines_p;
19980
8c43009f 19981 /* The line number header. */
6f77053d 19982 line_header *m_line_header;
8c43009f 19983
6f77053d
PA
19984 /* These are part of the standard DWARF line number state machine,
19985 and initialized according to the DWARF spec. */
d9b3de22 19986
6f77053d 19987 unsigned char m_op_index = 0;
7ba99d21
AT
19988 /* The line table index of the current file. */
19989 file_name_index m_file = 1;
6f77053d
PA
19990 unsigned int m_line = 1;
19991
19992 /* These are initialized in the constructor. */
19993
19994 CORE_ADDR m_address;
19995 bool m_is_stmt;
19996 unsigned int m_discriminator;
d9b3de22
DE
19997
19998 /* Additional bits of state we need to track. */
19999
20000 /* The last file that we called dwarf2_start_subfile for.
20001 This is only used for TLLs. */
6f77053d 20002 unsigned int m_last_file = 0;
d9b3de22 20003 /* The last file a line number was recorded for. */
6f77053d 20004 struct subfile *m_last_subfile = NULL;
d9b3de22 20005
804d2729
TT
20006 /* When true, record the lines we decode. */
20007 bool m_currently_recording_lines = false;
d9b3de22
DE
20008
20009 /* The last line number that was recorded, used to coalesce
20010 consecutive entries for the same line. This can happen, for
20011 example, when discriminators are present. PR 17276. */
6f77053d
PA
20012 unsigned int m_last_line = 0;
20013 bool m_line_has_non_zero_discriminator = false;
8c43009f 20014};
d9b3de22 20015
6f77053d
PA
20016void
20017lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20018{
20019 CORE_ADDR addr_adj = (((m_op_index + adjust)
20020 / m_line_header->maximum_ops_per_instruction)
20021 * m_line_header->minimum_instruction_length);
20022 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20023 m_op_index = ((m_op_index + adjust)
20024 % m_line_header->maximum_ops_per_instruction);
20025}
d9b3de22 20026
6f77053d
PA
20027void
20028lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20029{
6f77053d 20030 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20031 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20032 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20033 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20034 / m_line_header->maximum_ops_per_instruction)
20035 * m_line_header->minimum_instruction_length);
20036 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20037 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20038 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20039
258bf0ee 20040 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20041 advance_line (line_delta);
20042 record_line (false);
20043 m_discriminator = 0;
20044}
d9b3de22 20045
6f77053d
PA
20046void
20047lnp_state_machine::handle_set_file (file_name_index file)
20048{
20049 m_file = file;
20050
20051 const file_entry *fe = current_file ();
20052 if (fe == NULL)
20053 dwarf2_debug_line_missing_file_complaint ();
20054 else if (m_record_lines_p)
20055 {
20056 const char *dir = fe->include_dir (m_line_header);
20057
c24bdb02 20058 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20059 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20060 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20061 }
20062}
20063
20064void
20065lnp_state_machine::handle_const_add_pc ()
20066{
20067 CORE_ADDR adjust
20068 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20069
20070 CORE_ADDR addr_adj
20071 = (((m_op_index + adjust)
20072 / m_line_header->maximum_ops_per_instruction)
20073 * m_line_header->minimum_instruction_length);
20074
20075 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20076 m_op_index = ((m_op_index + adjust)
20077 % m_line_header->maximum_ops_per_instruction);
20078}
d9b3de22 20079
a05a36a5
DE
20080/* Return non-zero if we should add LINE to the line number table.
20081 LINE is the line to add, LAST_LINE is the last line that was added,
20082 LAST_SUBFILE is the subfile for LAST_LINE.
20083 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20084 had a non-zero discriminator.
20085
20086 We have to be careful in the presence of discriminators.
20087 E.g., for this line:
20088
20089 for (i = 0; i < 100000; i++);
20090
20091 clang can emit four line number entries for that one line,
20092 each with a different discriminator.
20093 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20094
20095 However, we want gdb to coalesce all four entries into one.
20096 Otherwise the user could stepi into the middle of the line and
20097 gdb would get confused about whether the pc really was in the
20098 middle of the line.
20099
20100 Things are further complicated by the fact that two consecutive
20101 line number entries for the same line is a heuristic used by gcc
20102 to denote the end of the prologue. So we can't just discard duplicate
20103 entries, we have to be selective about it. The heuristic we use is
20104 that we only collapse consecutive entries for the same line if at least
20105 one of those entries has a non-zero discriminator. PR 17276.
20106
20107 Note: Addresses in the line number state machine can never go backwards
20108 within one sequence, thus this coalescing is ok. */
20109
20110static int
804d2729
TT
20111dwarf_record_line_p (struct dwarf2_cu *cu,
20112 unsigned int line, unsigned int last_line,
a05a36a5
DE
20113 int line_has_non_zero_discriminator,
20114 struct subfile *last_subfile)
20115{
c24bdb02 20116 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20117 return 1;
20118 if (line != last_line)
20119 return 1;
20120 /* Same line for the same file that we've seen already.
20121 As a last check, for pr 17276, only record the line if the line
20122 has never had a non-zero discriminator. */
20123 if (!line_has_non_zero_discriminator)
20124 return 1;
20125 return 0;
20126}
20127
804d2729
TT
20128/* Use the CU's builder to record line number LINE beginning at
20129 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20130
20131static void
d9b3de22 20132dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20133 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20134 struct dwarf2_cu *cu)
252a6764
DE
20135{
20136 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20137
27e0867f
DE
20138 if (dwarf_line_debug)
20139 {
20140 fprintf_unfiltered (gdb_stdlog,
20141 "Recording line %u, file %s, address %s\n",
20142 line, lbasename (subfile->name),
20143 paddress (gdbarch, address));
20144 }
20145
804d2729 20146 if (cu != nullptr)
8c95582d 20147 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20148}
20149
20150/* Subroutine of dwarf_decode_lines_1 to simplify it.
20151 Mark the end of a set of line number records.
d9b3de22 20152 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20153 If SUBFILE is NULL the request is ignored. */
20154
20155static void
20156dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20157 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20158{
27e0867f
DE
20159 if (subfile == NULL)
20160 return;
20161
20162 if (dwarf_line_debug)
20163 {
20164 fprintf_unfiltered (gdb_stdlog,
20165 "Finishing current line, file %s, address %s\n",
20166 lbasename (subfile->name),
20167 paddress (gdbarch, address));
20168 }
20169
8c95582d 20170 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20171}
20172
6f77053d
PA
20173void
20174lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20175{
d9b3de22
DE
20176 if (dwarf_line_debug)
20177 {
20178 fprintf_unfiltered (gdb_stdlog,
20179 "Processing actual line %u: file %u,"
94a72be7 20180 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20181 m_line, m_file,
6f77053d 20182 paddress (m_gdbarch, m_address),
94a72be7
AB
20183 m_is_stmt, m_discriminator,
20184 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20185 }
20186
6f77053d 20187 file_entry *fe = current_file ();
8c43009f
PA
20188
20189 if (fe == NULL)
d9b3de22
DE
20190 dwarf2_debug_line_missing_file_complaint ();
20191 /* For now we ignore lines not starting on an instruction boundary.
20192 But not when processing end_sequence for compatibility with the
20193 previous version of the code. */
6f77053d 20194 else if (m_op_index == 0 || end_sequence)
d9b3de22 20195 {
8c43009f 20196 fe->included_p = 1;
8c95582d 20197 if (m_record_lines_p)
d9b3de22 20198 {
c24bdb02 20199 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20200 || end_sequence)
d9b3de22 20201 {
804d2729
TT
20202 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20203 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20204 }
20205
20206 if (!end_sequence)
20207 {
8c95582d
AB
20208 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20209
804d2729 20210 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20211 m_line_has_non_zero_discriminator,
20212 m_last_subfile))
d9b3de22 20213 {
c24bdb02 20214 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20215 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20216 builder->get_current_subfile (),
8c95582d 20217 m_line, m_address, is_stmt,
804d2729 20218 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20219 }
c24bdb02 20220 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20221 m_last_line = m_line;
d9b3de22
DE
20222 }
20223 }
20224 }
20225}
20226
804d2729
TT
20227lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20228 line_header *lh, bool record_lines_p)
d9b3de22 20229{
804d2729 20230 m_cu = cu;
6f77053d
PA
20231 m_gdbarch = arch;
20232 m_record_lines_p = record_lines_p;
20233 m_line_header = lh;
d9b3de22 20234
804d2729 20235 m_currently_recording_lines = true;
d9b3de22 20236
d9b3de22
DE
20237 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20238 was a line entry for it so that the backend has a chance to adjust it
20239 and also record it in case it needs it. This is currently used by MIPS
20240 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20241 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20242 m_is_stmt = lh->default_is_stmt;
20243 m_discriminator = 0;
252a6764
DE
20244}
20245
6f77053d
PA
20246void
20247lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20248 const gdb_byte *line_ptr,
7ab6656f 20249 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20250{
7ab6656f
OJ
20251 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20252 the pc range of the CU. However, we restrict the test to only ADDRESS
20253 values of zero to preserve GDB's previous behaviour which is to handle
20254 the specific case of a function being GC'd by the linker. */
924c2928 20255
7ab6656f 20256 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20257 {
20258 /* This line table is for a function which has been
20259 GCd by the linker. Ignore it. PR gdb/12528 */
20260
5e22e966 20261 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20262 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20263
b98664d3 20264 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20265 line_offset, objfile_name (objfile));
804d2729
TT
20266 m_currently_recording_lines = false;
20267 /* Note: m_currently_recording_lines is left as false until we see
20268 DW_LNE_end_sequence. */
924c2928
DE
20269 }
20270}
20271
f3f5162e 20272/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20273 Process the line number information in LH.
20274 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20275 program in order to set included_p for every referenced header. */
debd256d 20276
c906108c 20277static void
43f3e411
DE
20278dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20279 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20280{
d521ce57
TT
20281 const gdb_byte *line_ptr, *extended_end;
20282 const gdb_byte *line_end;
a8c50c1f 20283 unsigned int bytes_read, extended_len;
699ca60a 20284 unsigned char op_code, extended_op;
e142c38c 20285 CORE_ADDR baseaddr;
5e22e966 20286 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20287 bfd *abfd = objfile->obfd;
08feed99 20288 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20289 /* True if we're recording line info (as opposed to building partial
20290 symtabs and just interested in finding include files mentioned by
20291 the line number program). */
20292 bool record_lines_p = !decode_for_pst_p;
e142c38c 20293
b3b3bada 20294 baseaddr = objfile->text_section_offset ();
c906108c 20295
debd256d
JB
20296 line_ptr = lh->statement_program_start;
20297 line_end = lh->statement_program_end;
c906108c
SS
20298
20299 /* Read the statement sequences until there's nothing left. */
20300 while (line_ptr < line_end)
20301 {
6f77053d
PA
20302 /* The DWARF line number program state machine. Reset the state
20303 machine at the start of each sequence. */
804d2729 20304 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20305 bool end_sequence = false;
d9b3de22 20306
8c43009f 20307 if (record_lines_p)
c906108c 20308 {
8c43009f
PA
20309 /* Start a subfile for the current file of the state
20310 machine. */
20311 const file_entry *fe = state_machine.current_file ();
20312
20313 if (fe != NULL)
804d2729 20314 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20315 }
20316
a738430d 20317 /* Decode the table. */
d9b3de22 20318 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20319 {
20320 op_code = read_1_byte (abfd, line_ptr);
20321 line_ptr += 1;
9aa1fe7e 20322
debd256d 20323 if (op_code >= lh->opcode_base)
6e70227d 20324 {
8e07a239 20325 /* Special opcode. */
6f77053d 20326 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20327 }
20328 else switch (op_code)
c906108c
SS
20329 {
20330 case DW_LNS_extended_op:
3e43a32a
MS
20331 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20332 &bytes_read);
473b7be6 20333 line_ptr += bytes_read;
a8c50c1f 20334 extended_end = line_ptr + extended_len;
c906108c
SS
20335 extended_op = read_1_byte (abfd, line_ptr);
20336 line_ptr += 1;
20337 switch (extended_op)
20338 {
20339 case DW_LNE_end_sequence:
6f77053d
PA
20340 state_machine.handle_end_sequence ();
20341 end_sequence = true;
c906108c
SS
20342 break;
20343 case DW_LNE_set_address:
d9b3de22
DE
20344 {
20345 CORE_ADDR address
c8a7a66f 20346 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20347 line_ptr += bytes_read;
6f77053d
PA
20348
20349 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20350 lowpc - baseaddr, address);
6f77053d 20351 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20352 }
c906108c
SS
20353 break;
20354 case DW_LNE_define_file:
debd256d 20355 {
d521ce57 20356 const char *cur_file;
ecfb656c
PA
20357 unsigned int mod_time, length;
20358 dir_index dindex;
6e70227d 20359
3e43a32a
MS
20360 cur_file = read_direct_string (abfd, line_ptr,
20361 &bytes_read);
debd256d 20362 line_ptr += bytes_read;
ecfb656c 20363 dindex = (dir_index)
debd256d
JB
20364 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20365 line_ptr += bytes_read;
20366 mod_time =
20367 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20368 line_ptr += bytes_read;
20369 length =
20370 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20371 line_ptr += bytes_read;
ecfb656c 20372 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20373 }
c906108c 20374 break;
d0c6ba3d 20375 case DW_LNE_set_discriminator:
6f77053d
PA
20376 {
20377 /* The discriminator is not interesting to the
20378 debugger; just ignore it. We still need to
20379 check its value though:
20380 if there are consecutive entries for the same
20381 (non-prologue) line we want to coalesce them.
20382 PR 17276. */
20383 unsigned int discr
20384 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20385 line_ptr += bytes_read;
20386
20387 state_machine.handle_set_discriminator (discr);
20388 }
d0c6ba3d 20389 break;
c906108c 20390 default:
b98664d3 20391 complaint (_("mangled .debug_line section"));
debd256d 20392 return;
c906108c 20393 }
a8c50c1f
DJ
20394 /* Make sure that we parsed the extended op correctly. If e.g.
20395 we expected a different address size than the producer used,
20396 we may have read the wrong number of bytes. */
20397 if (line_ptr != extended_end)
20398 {
b98664d3 20399 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20400 return;
20401 }
c906108c
SS
20402 break;
20403 case DW_LNS_copy:
6f77053d 20404 state_machine.handle_copy ();
c906108c
SS
20405 break;
20406 case DW_LNS_advance_pc:
2dc7f7b3
TT
20407 {
20408 CORE_ADDR adjust
20409 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20410 line_ptr += bytes_read;
6f77053d
PA
20411
20412 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20413 }
c906108c
SS
20414 break;
20415 case DW_LNS_advance_line:
a05a36a5
DE
20416 {
20417 int line_delta
20418 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20419 line_ptr += bytes_read;
6f77053d
PA
20420
20421 state_machine.handle_advance_line (line_delta);
a05a36a5 20422 }
c906108c
SS
20423 break;
20424 case DW_LNS_set_file:
d9b3de22 20425 {
6f77053d 20426 file_name_index file
ecfb656c
PA
20427 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20428 &bytes_read);
d9b3de22 20429 line_ptr += bytes_read;
8c43009f 20430
6f77053d 20431 state_machine.handle_set_file (file);
d9b3de22 20432 }
c906108c
SS
20433 break;
20434 case DW_LNS_set_column:
0ad93d4f 20435 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20436 line_ptr += bytes_read;
20437 break;
20438 case DW_LNS_negate_stmt:
6f77053d 20439 state_machine.handle_negate_stmt ();
c906108c
SS
20440 break;
20441 case DW_LNS_set_basic_block:
c906108c 20442 break;
c2c6d25f
JM
20443 /* Add to the address register of the state machine the
20444 address increment value corresponding to special opcode
a738430d
MK
20445 255. I.e., this value is scaled by the minimum
20446 instruction length since special opcode 255 would have
b021a221 20447 scaled the increment. */
c906108c 20448 case DW_LNS_const_add_pc:
6f77053d 20449 state_machine.handle_const_add_pc ();
c906108c
SS
20450 break;
20451 case DW_LNS_fixed_advance_pc:
3e29f34a 20452 {
6f77053d 20453 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20454 line_ptr += 2;
6f77053d
PA
20455
20456 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20457 }
c906108c 20458 break;
9aa1fe7e 20459 default:
a738430d
MK
20460 {
20461 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20462 int i;
a738430d 20463
debd256d 20464 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20465 {
20466 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20467 line_ptr += bytes_read;
20468 }
20469 }
c906108c
SS
20470 }
20471 }
d9b3de22
DE
20472
20473 if (!end_sequence)
20474 dwarf2_debug_line_missing_end_sequence_complaint ();
20475
20476 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20477 in which case we still finish recording the last line). */
6f77053d 20478 state_machine.record_line (true);
c906108c 20479 }
f3f5162e
DE
20480}
20481
20482/* Decode the Line Number Program (LNP) for the given line_header
20483 structure and CU. The actual information extracted and the type
20484 of structures created from the LNP depends on the value of PST.
20485
20486 1. If PST is NULL, then this procedure uses the data from the program
20487 to create all necessary symbol tables, and their linetables.
20488
20489 2. If PST is not NULL, this procedure reads the program to determine
20490 the list of files included by the unit represented by PST, and
20491 builds all the associated partial symbol tables.
20492
20493 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20494 It is used for relative paths in the line table.
20495 NOTE: When processing partial symtabs (pst != NULL),
20496 comp_dir == pst->dirname.
20497
20498 NOTE: It is important that psymtabs have the same file name (via strcmp)
20499 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20500 symtab we don't use it in the name of the psymtabs we create.
20501 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20502 A good testcase for this is mb-inline.exp.
20503
527f3840
JK
20504 LOWPC is the lowest address in CU (or 0 if not known).
20505
20506 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20507 for its PC<->lines mapping information. Otherwise only the filename
20508 table is read in. */
f3f5162e
DE
20509
20510static void
20511dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20512 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20513 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20514{
5e22e966 20515 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20516 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20517
527f3840
JK
20518 if (decode_mapping)
20519 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20520
20521 if (decode_for_pst_p)
20522 {
aaa75496
JB
20523 /* Now that we're done scanning the Line Header Program, we can
20524 create the psymtab of each included file. */
7ba99d21
AT
20525 for (auto &file_entry : lh->file_names ())
20526 if (file_entry.included_p == 1)
aaa75496 20527 {
c89b44cd 20528 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20529 const char *include_name =
7ba99d21
AT
20530 psymtab_include_file_name (lh, file_entry, pst,
20531 comp_dir, &name_holder);
c6da4cef 20532 if (include_name != NULL)
aaa75496
JB
20533 dwarf2_create_include_psymtab (include_name, pst, objfile);
20534 }
20535 }
cb1df416
DJ
20536 else
20537 {
20538 /* Make sure a symtab is created for every file, even files
20539 which contain only variables (i.e. no code with associated
20540 line numbers). */
c24bdb02
KS
20541 buildsym_compunit *builder = cu->get_builder ();
20542 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20543
7ba99d21 20544 for (auto &fe : lh->file_names ())
cb1df416 20545 {
804d2729 20546 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20547 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20548 {
c24bdb02 20549 builder->get_current_subfile ()->symtab
804d2729 20550 = allocate_symtab (cust,
c24bdb02 20551 builder->get_current_subfile ()->name);
43f3e411 20552 }
c24bdb02 20553 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20554 }
20555 }
c906108c
SS
20556}
20557
20558/* Start a subfile for DWARF. FILENAME is the name of the file and
20559 DIRNAME the name of the source directory which contains FILENAME
4d663531 20560 or NULL if not known.
c906108c
SS
20561 This routine tries to keep line numbers from identical absolute and
20562 relative file names in a common subfile.
20563
20564 Using the `list' example from the GDB testsuite, which resides in
20565 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20566 of /srcdir/list0.c yields the following debugging information for list0.c:
20567
c5aa993b 20568 DW_AT_name: /srcdir/list0.c
4d663531 20569 DW_AT_comp_dir: /compdir
357e46e7 20570 files.files[0].name: list0.h
c5aa993b 20571 files.files[0].dir: /srcdir
357e46e7 20572 files.files[1].name: list0.c
c5aa993b 20573 files.files[1].dir: /srcdir
c906108c
SS
20574
20575 The line number information for list0.c has to end up in a single
4f1520fb
FR
20576 subfile, so that `break /srcdir/list0.c:1' works as expected.
20577 start_subfile will ensure that this happens provided that we pass the
20578 concatenation of files.files[1].dir and files.files[1].name as the
20579 subfile's name. */
c906108c
SS
20580
20581static void
804d2729
TT
20582dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20583 const char *dirname)
c906108c 20584{
43816ebc 20585 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20586
4d663531 20587 /* In order not to lose the line information directory,
4f1520fb
FR
20588 we concatenate it to the filename when it makes sense.
20589 Note that the Dwarf3 standard says (speaking of filenames in line
20590 information): ``The directory index is ignored for file names
20591 that represent full path names''. Thus ignoring dirname in the
20592 `else' branch below isn't an issue. */
c906108c 20593
d5166ae1 20594 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20595 {
43816ebc
TT
20596 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20597 filename = copy.get ();
d521ce57 20598 }
c906108c 20599
c24bdb02 20600 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20601}
20602
804d2729
TT
20603/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20604 buildsym_compunit constructor. */
f4dc4d17 20605
c24bdb02
KS
20606struct compunit_symtab *
20607dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20608 CORE_ADDR low_pc)
f4dc4d17 20609{
c24bdb02 20610 gdb_assert (m_builder == nullptr);
43f3e411 20611
c24bdb02 20612 m_builder.reset (new struct buildsym_compunit
f6e649dd 20613 (this->per_objfile->objfile,
c24bdb02 20614 name, comp_dir, language, low_pc));
93b8bea4 20615
c24bdb02 20616 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20617
c24bdb02
KS
20618 get_builder ()->record_debugformat ("DWARF 2");
20619 get_builder ()->record_producer (producer);
f4dc4d17 20620
c24bdb02 20621 processing_has_namespace_info = false;
43f3e411 20622
c24bdb02 20623 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20624}
20625
4c2df51b
DJ
20626static void
20627var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20628 struct dwarf2_cu *cu)
4c2df51b 20629{
5e22e966 20630 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20631 struct comp_unit_head *cu_header = &cu->header;
20632
4c2df51b
DJ
20633 /* NOTE drow/2003-01-30: There used to be a comment and some special
20634 code here to turn a symbol with DW_AT_external and a
20635 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20636 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20637 with some versions of binutils) where shared libraries could have
20638 relocations against symbols in their debug information - the
20639 minimal symbol would have the right address, but the debug info
20640 would not. It's no longer necessary, because we will explicitly
20641 apply relocations when we read in the debug information now. */
20642
20643 /* A DW_AT_location attribute with no contents indicates that a
20644 variable has been optimized away. */
4fc6c0d5 20645 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20646 {
f1e6e072 20647 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20648 return;
20649 }
20650
20651 /* Handle one degenerate form of location expression specially, to
20652 preserve GDB's previous behavior when section offsets are
336d760d
AT
20653 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20654 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20655
4fc6c0d5 20656 if (attr->form_is_block ()
3019eac3
DE
20657 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20658 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20659 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20660 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20661 && (DW_BLOCK (attr)->size
20662 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20663 {
891d2f0b 20664 unsigned int dummy;
4c2df51b 20665
3019eac3 20666 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20667 SET_SYMBOL_VALUE_ADDRESS
20668 (sym, cu->header.read_address (objfile->obfd,
20669 DW_BLOCK (attr)->data + 1,
20670 &dummy));
3019eac3 20671 else
38583298
TT
20672 SET_SYMBOL_VALUE_ADDRESS
20673 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20674 &dummy));
f1e6e072 20675 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20676 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20677 SET_SYMBOL_VALUE_ADDRESS
20678 (sym,
20679 SYMBOL_VALUE_ADDRESS (sym)
20680 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20681 return;
20682 }
20683
20684 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20685 expression evaluator, and use LOC_COMPUTED only when necessary
20686 (i.e. when the value of a register or memory location is
20687 referenced, or a thread-local block, etc.). Then again, it might
20688 not be worthwhile. I'm assuming that it isn't unless performance
20689 or memory numbers show me otherwise. */
20690
f1e6e072 20691 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20692
f1e6e072 20693 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20694 cu->has_loclist = true;
4c2df51b
DJ
20695}
20696
c906108c
SS
20697/* Given a pointer to a DWARF information entry, figure out if we need
20698 to make a symbol table entry for it, and if so, create a new entry
20699 and return a pointer to it.
20700 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20701 used the passed type.
20702 If SPACE is not NULL, use it to hold the new symbol. If it is
20703 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20704
20705static struct symbol *
5e2db402
TT
20706new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20707 struct symbol *space)
c906108c 20708{
5e22e966 20709 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20710 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20711 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20712 struct symbol *sym = NULL;
15d034d0 20713 const char *name;
c906108c
SS
20714 struct attribute *attr = NULL;
20715 struct attribute *attr2 = NULL;
e142c38c 20716 CORE_ADDR baseaddr;
e37fd15a
SW
20717 struct pending **list_to_add = NULL;
20718
edb3359d 20719 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20720
b3b3bada 20721 baseaddr = objfile->text_section_offset ();
c906108c 20722
94af9270 20723 name = dwarf2_name (die, cu);
c906108c
SS
20724 if (name)
20725 {
34eaf542 20726 int suppress_add = 0;
94af9270 20727
34eaf542
TT
20728 if (space)
20729 sym = space;
20730 else
8c14c3a3 20731 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20732 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20733
20734 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20735 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20736 /* Fortran does not have mangling standard and the mangling does differ
20737 between gfortran, iFort etc. */
bcfe6157
TT
20738 const char *physname
20739 = (cu->language == language_fortran
20740 ? dwarf2_full_name (name, die, cu)
20741 : dwarf2_physname (name, die, cu));
20742 const char *linkagename = dw2_linkage_name (die, cu);
20743
20744 if (linkagename == nullptr || cu->language == language_ada)
20745 sym->set_linkage_name (physname);
20746 else
20747 {
20748 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20749 sym->set_linkage_name (linkagename);
20750 }
f55ee35c 20751
c906108c 20752 /* Default assumptions.
c5aa993b 20753 Use the passed type or decode it from the die. */
176620f1 20754 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20755 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20756 if (type != NULL)
20757 SYMBOL_TYPE (sym) = type;
20758 else
e7c27a73 20759 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20760 attr = dwarf2_attr (die,
20761 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20762 cu);
435d3d88 20763 if (attr != nullptr)
c906108c
SS
20764 {
20765 SYMBOL_LINE (sym) = DW_UNSND (attr);
20766 }
cb1df416 20767
edb3359d
DJ
20768 attr = dwarf2_attr (die,
20769 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20770 cu);
435d3d88 20771 if (attr != nullptr)
cb1df416 20772 {
ecfb656c 20773 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20774 struct file_entry *fe;
9a619af0 20775
ecfb656c
PA
20776 if (cu->line_header != NULL)
20777 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20778 else
20779 fe = NULL;
20780
20781 if (fe == NULL)
b98664d3 20782 complaint (_("file index out of range"));
8c43009f
PA
20783 else
20784 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20785 }
20786
c906108c
SS
20787 switch (die->tag)
20788 {
20789 case DW_TAG_label:
e142c38c 20790 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20791 if (attr != nullptr)
3e29f34a
MR
20792 {
20793 CORE_ADDR addr;
20794
cd6c91b4 20795 addr = attr->value_as_address ();
3e29f34a 20796 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20797 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20798 }
0f5238ed
TT
20799 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20800 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20801 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20802 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20803 break;
20804 case DW_TAG_subprogram:
20805 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20806 finish_block. */
f1e6e072 20807 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20808 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20809 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20810 || cu->language == language_ada
20811 || cu->language == language_fortran)
c906108c 20812 {
2cfa0c8d 20813 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20814 Ada and Fortran subprograms, whether marked external or
20815 not, are always stored as a global symbol, because we want
20816 to be able to access them globally. For instance, we want
20817 to be able to break on a nested subprogram without having
20818 to specify the context. */
c24bdb02 20819 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20820 }
20821 else
20822 {
e37fd15a 20823 list_to_add = cu->list_in_scope;
c906108c
SS
20824 }
20825 break;
edb3359d
DJ
20826 case DW_TAG_inlined_subroutine:
20827 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20828 finish_block. */
f1e6e072 20829 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20830 SYMBOL_INLINED (sym) = 1;
481860b3 20831 list_to_add = cu->list_in_scope;
edb3359d 20832 break;
34eaf542
TT
20833 case DW_TAG_template_value_param:
20834 suppress_add = 1;
20835 /* Fall through. */
72929c62 20836 case DW_TAG_constant:
c906108c 20837 case DW_TAG_variable:
254e6b9e 20838 case DW_TAG_member:
0963b4bd
MS
20839 /* Compilation with minimal debug info may result in
20840 variables with missing type entries. Change the
20841 misleading `void' type to something sensible. */
78134374 20842 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20843 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20844
e142c38c 20845 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20846 /* In the case of DW_TAG_member, we should only be called for
20847 static const members. */
20848 if (die->tag == DW_TAG_member)
20849 {
3863f96c
DE
20850 /* dwarf2_add_field uses die_is_declaration,
20851 so we do the same. */
254e6b9e
DE
20852 gdb_assert (die_is_declaration (die, cu));
20853 gdb_assert (attr);
20854 }
435d3d88 20855 if (attr != nullptr)
c906108c 20856 {
e7c27a73 20857 dwarf2_const_value (attr, sym, cu);
e142c38c 20858 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20859 if (!suppress_add)
34eaf542
TT
20860 {
20861 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20862 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20863 else
e37fd15a 20864 list_to_add = cu->list_in_scope;
34eaf542 20865 }
c906108c
SS
20866 break;
20867 }
e142c38c 20868 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20869 if (attr != nullptr)
c906108c 20870 {
e7c27a73 20871 var_decode_location (attr, sym, cu);
e142c38c 20872 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20873
20874 /* Fortran explicitly imports any global symbols to the local
20875 scope by DW_TAG_common_block. */
20876 if (cu->language == language_fortran && die->parent
20877 && die->parent->tag == DW_TAG_common_block)
20878 attr2 = NULL;
20879
caac4577
JG
20880 if (SYMBOL_CLASS (sym) == LOC_STATIC
20881 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20882 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20883 {
20884 /* When a static variable is eliminated by the linker,
20885 the corresponding debug information is not stripped
20886 out, but the variable address is set to null;
20887 do not add such variables into symbol table. */
20888 }
20889 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20890 {
4b610737
TT
20891 if (SYMBOL_CLASS (sym) == LOC_STATIC
20892 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20893 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20894 {
20895 /* A global static variable might be subject to
20896 copy relocation. We first check for a local
20897 minsym, though, because maybe the symbol was
20898 marked hidden, in which case this would not
20899 apply. */
20900 bound_minimal_symbol found
20901 = (lookup_minimal_symbol_linkage
987012b8 20902 (sym->linkage_name (), objfile));
4b610737
TT
20903 if (found.minsym != nullptr)
20904 sym->maybe_copied = 1;
20905 }
f55ee35c 20906
1c809c68
TT
20907 /* A variable with DW_AT_external is never static,
20908 but it may be block-scoped. */
804d2729 20909 list_to_add
c24bdb02
KS
20910 = ((cu->list_in_scope
20911 == cu->get_builder ()->get_file_symbols ())
20912 ? cu->get_builder ()->get_global_symbols ()
804d2729 20913 : cu->list_in_scope);
1c809c68 20914 }
c906108c 20915 else
e37fd15a 20916 list_to_add = cu->list_in_scope;
c906108c
SS
20917 }
20918 else
20919 {
20920 /* We do not know the address of this symbol.
c5aa993b
JM
20921 If it is an external symbol and we have type information
20922 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20923 The address of the variable will then be determined from
20924 the minimal symbol table whenever the variable is
20925 referenced. */
e142c38c 20926 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20927
20928 /* Fortran explicitly imports any global symbols to the local
20929 scope by DW_TAG_common_block. */
20930 if (cu->language == language_fortran && die->parent
20931 && die->parent->tag == DW_TAG_common_block)
20932 {
20933 /* SYMBOL_CLASS doesn't matter here because
20934 read_common_block is going to reset it. */
20935 if (!suppress_add)
20936 list_to_add = cu->list_in_scope;
20937 }
20938 else if (attr2 && (DW_UNSND (attr2) != 0)
20939 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20940 {
0fe7935b
DJ
20941 /* A variable with DW_AT_external is never static, but it
20942 may be block-scoped. */
804d2729 20943 list_to_add
c24bdb02
KS
20944 = ((cu->list_in_scope
20945 == cu->get_builder ()->get_file_symbols ())
20946 ? cu->get_builder ()->get_global_symbols ()
804d2729 20947 : cu->list_in_scope);
0fe7935b 20948
f1e6e072 20949 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20950 }
442ddf59
JK
20951 else if (!die_is_declaration (die, cu))
20952 {
20953 /* Use the default LOC_OPTIMIZED_OUT class. */
20954 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20955 if (!suppress_add)
20956 list_to_add = cu->list_in_scope;
442ddf59 20957 }
c906108c
SS
20958 }
20959 break;
20960 case DW_TAG_formal_parameter:
a60f3166
TT
20961 {
20962 /* If we are inside a function, mark this as an argument. If
20963 not, we might be looking at an argument to an inlined function
20964 when we do not have enough information to show inlined frames;
20965 pretend it's a local variable in that case so that the user can
20966 still see it. */
804d2729 20967 struct context_stack *curr
c24bdb02 20968 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20969 if (curr != nullptr && curr->name != nullptr)
20970 SYMBOL_IS_ARGUMENT (sym) = 1;
20971 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20972 if (attr != nullptr)
a60f3166
TT
20973 {
20974 var_decode_location (attr, sym, cu);
20975 }
20976 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20977 if (attr != nullptr)
a60f3166
TT
20978 {
20979 dwarf2_const_value (attr, sym, cu);
20980 }
f346a30d 20981
a60f3166
TT
20982 list_to_add = cu->list_in_scope;
20983 }
c906108c
SS
20984 break;
20985 case DW_TAG_unspecified_parameters:
20986 /* From varargs functions; gdb doesn't seem to have any
20987 interest in this information, so just ignore it for now.
20988 (FIXME?) */
20989 break;
34eaf542
TT
20990 case DW_TAG_template_type_param:
20991 suppress_add = 1;
20992 /* Fall through. */
c906108c 20993 case DW_TAG_class_type:
680b30c7 20994 case DW_TAG_interface_type:
c906108c
SS
20995 case DW_TAG_structure_type:
20996 case DW_TAG_union_type:
72019c9c 20997 case DW_TAG_set_type:
c906108c 20998 case DW_TAG_enumeration_type:
f1e6e072 20999 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21000 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21001
63d06c5c 21002 {
9c37b5ae 21003 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21004 really ever be static objects: otherwise, if you try
21005 to, say, break of a class's method and you're in a file
21006 which doesn't mention that class, it won't work unless
21007 the check for all static symbols in lookup_symbol_aux
21008 saves you. See the OtherFileClass tests in
21009 gdb.c++/namespace.exp. */
21010
e37fd15a 21011 if (!suppress_add)
34eaf542 21012 {
c24bdb02 21013 buildsym_compunit *builder = cu->get_builder ();
804d2729 21014 list_to_add
c24bdb02 21015 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21016 && cu->language == language_cplus
c24bdb02 21017 ? builder->get_global_symbols ()
804d2729 21018 : cu->list_in_scope);
63d06c5c 21019
64382290 21020 /* The semantics of C++ state that "struct foo {
9c37b5ae 21021 ... }" also defines a typedef for "foo". */
64382290 21022 if (cu->language == language_cplus
45280282 21023 || cu->language == language_ada
c44af4eb
TT
21024 || cu->language == language_d
21025 || cu->language == language_rust)
64382290
TT
21026 {
21027 /* The symbol's name is already allocated along
21028 with this objfile, so we don't need to
21029 duplicate it for the type. */
7d93a1e0 21030 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21031 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21032 }
63d06c5c
DC
21033 }
21034 }
c906108c
SS
21035 break;
21036 case DW_TAG_typedef:
f1e6e072 21037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21038 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21039 list_to_add = cu->list_in_scope;
63d06c5c 21040 break;
c906108c 21041 case DW_TAG_base_type:
a02abb62 21042 case DW_TAG_subrange_type:
f1e6e072 21043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21044 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21045 list_to_add = cu->list_in_scope;
c906108c
SS
21046 break;
21047 case DW_TAG_enumerator:
e142c38c 21048 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21049 if (attr != nullptr)
c906108c 21050 {
e7c27a73 21051 dwarf2_const_value (attr, sym, cu);
c906108c 21052 }
63d06c5c
DC
21053 {
21054 /* NOTE: carlton/2003-11-10: See comment above in the
21055 DW_TAG_class_type, etc. block. */
21056
804d2729 21057 list_to_add
c24bdb02 21058 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21059 && cu->language == language_cplus
c24bdb02 21060 ? cu->get_builder ()->get_global_symbols ()
804d2729 21061 : cu->list_in_scope);
63d06c5c 21062 }
c906108c 21063 break;
74921315 21064 case DW_TAG_imported_declaration:
5c4e30ca 21065 case DW_TAG_namespace:
f1e6e072 21066 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21067 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21068 break;
530e8392
KB
21069 case DW_TAG_module:
21070 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21071 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21072 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21073 break;
4357ac6c 21074 case DW_TAG_common_block:
f1e6e072 21075 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21076 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21077 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21078 break;
c906108c
SS
21079 default:
21080 /* Not a tag we recognize. Hopefully we aren't processing
21081 trash data, but since we must specifically ignore things
21082 we don't recognize, there is nothing else we should do at
0963b4bd 21083 this point. */
b98664d3 21084 complaint (_("unsupported tag: '%s'"),
4d3c2250 21085 dwarf_tag_name (die->tag));
c906108c
SS
21086 break;
21087 }
df8a16a1 21088
e37fd15a
SW
21089 if (suppress_add)
21090 {
21091 sym->hash_next = objfile->template_symbols;
21092 objfile->template_symbols = sym;
21093 list_to_add = NULL;
21094 }
21095
21096 if (list_to_add != NULL)
d3cb6808 21097 add_symbol_to_list (sym, list_to_add);
e37fd15a 21098
df8a16a1
DJ
21099 /* For the benefit of old versions of GCC, check for anonymous
21100 namespaces based on the demangled name. */
4d4ec4e5 21101 if (!cu->processing_has_namespace_info
94af9270 21102 && cu->language == language_cplus)
c24bdb02 21103 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21104 }
21105 return (sym);
21106}
21107
98bfdba5
PA
21108/* Given an attr with a DW_FORM_dataN value in host byte order,
21109 zero-extend it as appropriate for the symbol's type. The DWARF
21110 standard (v4) is not entirely clear about the meaning of using
21111 DW_FORM_dataN for a constant with a signed type, where the type is
21112 wider than the data. The conclusion of a discussion on the DWARF
21113 list was that this is unspecified. We choose to always zero-extend
21114 because that is the interpretation long in use by GCC. */
c906108c 21115
98bfdba5 21116static gdb_byte *
ff39bb5e 21117dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21118 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21119{
5e22e966 21120 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21121 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21122 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21123 LONGEST l = DW_UNSND (attr);
21124
21125 if (bits < sizeof (*value) * 8)
21126 {
21127 l &= ((LONGEST) 1 << bits) - 1;
21128 *value = l;
21129 }
21130 else if (bits == sizeof (*value) * 8)
21131 *value = l;
21132 else
21133 {
224c3ddb 21134 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21135 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21136 return bytes;
21137 }
21138
21139 return NULL;
21140}
21141
21142/* Read a constant value from an attribute. Either set *VALUE, or if
21143 the value does not fit in *VALUE, set *BYTES - either already
21144 allocated on the objfile obstack, or newly allocated on OBSTACK,
21145 or, set *BATON, if we translated the constant to a location
21146 expression. */
21147
21148static void
ff39bb5e 21149dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21150 const char *name, struct obstack *obstack,
21151 struct dwarf2_cu *cu,
d521ce57 21152 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21153 struct dwarf2_locexpr_baton **baton)
21154{
5e22e966 21155 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21156 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21157 struct comp_unit_head *cu_header = &cu->header;
c906108c 21158 struct dwarf_block *blk;
98bfdba5
PA
21159 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21160 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21161
21162 *value = 0;
21163 *bytes = NULL;
21164 *baton = NULL;
c906108c
SS
21165
21166 switch (attr->form)
21167 {
21168 case DW_FORM_addr:
336d760d 21169 case DW_FORM_addrx:
3019eac3 21170 case DW_FORM_GNU_addr_index:
ac56253d 21171 {
ac56253d
TT
21172 gdb_byte *data;
21173
98bfdba5
PA
21174 if (TYPE_LENGTH (type) != cu_header->addr_size)
21175 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21176 cu_header->addr_size,
98bfdba5 21177 TYPE_LENGTH (type));
ac56253d
TT
21178 /* Symbols of this form are reasonably rare, so we just
21179 piggyback on the existing location code rather than writing
21180 a new implementation of symbol_computed_ops. */
8d749320 21181 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21182 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21183 (*baton)->per_cu = cu->per_cu;
21184 gdb_assert ((*baton)->per_cu);
ac56253d 21185
98bfdba5 21186 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21187 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21188 (*baton)->data = data;
ac56253d
TT
21189
21190 data[0] = DW_OP_addr;
21191 store_unsigned_integer (&data[1], cu_header->addr_size,
21192 byte_order, DW_ADDR (attr));
21193 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21194 }
c906108c 21195 break;
4ac36638 21196 case DW_FORM_string:
93b5768b 21197 case DW_FORM_strp:
cf532bd1 21198 case DW_FORM_strx:
3019eac3 21199 case DW_FORM_GNU_str_index:
36586728 21200 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21201 /* DW_STRING is already allocated on the objfile obstack, point
21202 directly to it. */
d521ce57 21203 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21204 break;
c906108c
SS
21205 case DW_FORM_block1:
21206 case DW_FORM_block2:
21207 case DW_FORM_block4:
21208 case DW_FORM_block:
2dc7f7b3 21209 case DW_FORM_exprloc:
0224619f 21210 case DW_FORM_data16:
c906108c 21211 blk = DW_BLOCK (attr);
98bfdba5
PA
21212 if (TYPE_LENGTH (type) != blk->size)
21213 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21214 TYPE_LENGTH (type));
21215 *bytes = blk->data;
c906108c 21216 break;
2df3850c
JM
21217
21218 /* The DW_AT_const_value attributes are supposed to carry the
21219 symbol's value "represented as it would be on the target
21220 architecture." By the time we get here, it's already been
21221 converted to host endianness, so we just need to sign- or
21222 zero-extend it as appropriate. */
21223 case DW_FORM_data1:
3aef2284 21224 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21225 break;
c906108c 21226 case DW_FORM_data2:
3aef2284 21227 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21228 break;
c906108c 21229 case DW_FORM_data4:
3aef2284 21230 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21231 break;
c906108c 21232 case DW_FORM_data8:
3aef2284 21233 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21234 break;
21235
c906108c 21236 case DW_FORM_sdata:
663c44ac 21237 case DW_FORM_implicit_const:
98bfdba5 21238 *value = DW_SND (attr);
2df3850c
JM
21239 break;
21240
c906108c 21241 case DW_FORM_udata:
98bfdba5 21242 *value = DW_UNSND (attr);
c906108c 21243 break;
2df3850c 21244
c906108c 21245 default:
b98664d3 21246 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21247 dwarf_form_name (attr->form));
98bfdba5 21248 *value = 0;
c906108c
SS
21249 break;
21250 }
21251}
21252
2df3850c 21253
98bfdba5
PA
21254/* Copy constant value from an attribute to a symbol. */
21255
2df3850c 21256static void
ff39bb5e 21257dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21258 struct dwarf2_cu *cu)
2df3850c 21259{
5e22e966 21260 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21261 LONGEST value;
d521ce57 21262 const gdb_byte *bytes;
98bfdba5 21263 struct dwarf2_locexpr_baton *baton;
2df3850c 21264
98bfdba5 21265 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21266 sym->print_name (),
98bfdba5
PA
21267 &objfile->objfile_obstack, cu,
21268 &value, &bytes, &baton);
2df3850c 21269
98bfdba5
PA
21270 if (baton != NULL)
21271 {
98bfdba5 21272 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21273 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21274 }
21275 else if (bytes != NULL)
21276 {
21277 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21278 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21279 }
21280 else
21281 {
21282 SYMBOL_VALUE (sym) = value;
f1e6e072 21283 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21284 }
2df3850c
JM
21285}
21286
c906108c
SS
21287/* Return the type of the die in question using its DW_AT_type attribute. */
21288
21289static struct type *
e7c27a73 21290die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21291{
c906108c 21292 struct attribute *type_attr;
c906108c 21293
e142c38c 21294 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21295 if (!type_attr)
21296 {
5e22e966 21297 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21298 /* A missing DW_AT_type represents a void type. */
518817b3 21299 return objfile_type (objfile)->builtin_void;
c906108c 21300 }
348e048f 21301
673bfd45 21302 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21303}
21304
b4ba55a1
JB
21305/* True iff CU's producer generates GNAT Ada auxiliary information
21306 that allows to find parallel types through that information instead
21307 of having to do expensive parallel lookups by type name. */
21308
21309static int
21310need_gnat_info (struct dwarf2_cu *cu)
21311{
de4cb04a
JB
21312 /* Assume that the Ada compiler was GNAT, which always produces
21313 the auxiliary information. */
21314 return (cu->language == language_ada);
b4ba55a1
JB
21315}
21316
b4ba55a1
JB
21317/* Return the auxiliary type of the die in question using its
21318 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21319 attribute is not present. */
21320
21321static struct type *
21322die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21323{
b4ba55a1 21324 struct attribute *type_attr;
b4ba55a1
JB
21325
21326 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21327 if (!type_attr)
21328 return NULL;
21329
673bfd45 21330 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21331}
21332
21333/* If DIE has a descriptive_type attribute, then set the TYPE's
21334 descriptive type accordingly. */
21335
21336static void
21337set_descriptive_type (struct type *type, struct die_info *die,
21338 struct dwarf2_cu *cu)
21339{
21340 struct type *descriptive_type = die_descriptive_type (die, cu);
21341
21342 if (descriptive_type)
21343 {
21344 ALLOCATE_GNAT_AUX_TYPE (type);
21345 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21346 }
21347}
21348
c906108c
SS
21349/* Return the containing type of the die in question using its
21350 DW_AT_containing_type attribute. */
21351
21352static struct type *
e7c27a73 21353die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21354{
c906108c 21355 struct attribute *type_attr;
5e22e966 21356 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21357
e142c38c 21358 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21359 if (!type_attr)
21360 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21361 "[in module %s]"), objfile_name (objfile));
33ac96f0 21362
673bfd45 21363 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21364}
21365
ac9ec31b
DE
21366/* Return an error marker type to use for the ill formed type in DIE/CU. */
21367
21368static struct type *
21369build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21370{
5e22e966 21371 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21372 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21373 char *saved;
ac9ec31b 21374
528e1572
SM
21375 std::string message
21376 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21377 objfile_name (objfile),
21378 sect_offset_str (cu->header.sect_off),
21379 sect_offset_str (die->sect_off));
efba19b0 21380 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21381
19f392bc 21382 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21383}
21384
673bfd45 21385/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21386 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21387 DW_AT_containing_type.
673bfd45
DE
21388 If there is no type substitute an error marker. */
21389
c906108c 21390static struct type *
ff39bb5e 21391lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21392 struct dwarf2_cu *cu)
c906108c 21393{
5e22e966 21394 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21395 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21396 struct type *this_type;
21397
ac9ec31b
DE
21398 gdb_assert (attr->name == DW_AT_type
21399 || attr->name == DW_AT_GNAT_descriptive_type
21400 || attr->name == DW_AT_containing_type);
21401
673bfd45
DE
21402 /* First see if we have it cached. */
21403
36586728
TT
21404 if (attr->form == DW_FORM_GNU_ref_alt)
21405 {
21406 struct dwarf2_per_cu_data *per_cu;
0826b30a 21407 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21408
ed2dc618
SM
21409 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21410 dwarf2_per_objfile);
aa66c379 21411 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21412 }
cd6c91b4 21413 else if (attr->form_is_ref ())
673bfd45 21414 {
0826b30a 21415 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21416
aa66c379
SM
21417 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21418 dwarf2_per_objfile);
673bfd45 21419 }
55f1336d 21420 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21421 {
ac9ec31b 21422 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21423
ac9ec31b 21424 return get_signatured_type (die, signature, cu);
673bfd45
DE
21425 }
21426 else
21427 {
b98664d3 21428 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21429 " at %s [in module %s]"),
21430 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21431 objfile_name (objfile));
ac9ec31b 21432 return build_error_marker_type (cu, die);
673bfd45
DE
21433 }
21434
21435 /* If not cached we need to read it in. */
21436
21437 if (this_type == NULL)
21438 {
ac9ec31b 21439 struct die_info *type_die = NULL;
673bfd45
DE
21440 struct dwarf2_cu *type_cu = cu;
21441
cd6c91b4 21442 if (attr->form_is_ref ())
ac9ec31b
DE
21443 type_die = follow_die_ref (die, attr, &type_cu);
21444 if (type_die == NULL)
21445 return build_error_marker_type (cu, die);
21446 /* If we find the type now, it's probably because the type came
3019eac3
DE
21447 from an inter-CU reference and the type's CU got expanded before
21448 ours. */
ac9ec31b 21449 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21450 }
21451
21452 /* If we still don't have a type use an error marker. */
21453
21454 if (this_type == NULL)
ac9ec31b 21455 return build_error_marker_type (cu, die);
673bfd45 21456
f792889a 21457 return this_type;
c906108c
SS
21458}
21459
673bfd45
DE
21460/* Return the type in DIE, CU.
21461 Returns NULL for invalid types.
21462
02142a6c 21463 This first does a lookup in die_type_hash,
673bfd45
DE
21464 and only reads the die in if necessary.
21465
21466 NOTE: This can be called when reading in partial or full symbols. */
21467
f792889a 21468static struct type *
e7c27a73 21469read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21470{
f792889a
DJ
21471 struct type *this_type;
21472
21473 this_type = get_die_type (die, cu);
21474 if (this_type)
21475 return this_type;
21476
673bfd45
DE
21477 return read_type_die_1 (die, cu);
21478}
21479
21480/* Read the type in DIE, CU.
21481 Returns NULL for invalid types. */
21482
21483static struct type *
21484read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21485{
21486 struct type *this_type = NULL;
21487
c906108c
SS
21488 switch (die->tag)
21489 {
21490 case DW_TAG_class_type:
680b30c7 21491 case DW_TAG_interface_type:
c906108c
SS
21492 case DW_TAG_structure_type:
21493 case DW_TAG_union_type:
f792889a 21494 this_type = read_structure_type (die, cu);
c906108c
SS
21495 break;
21496 case DW_TAG_enumeration_type:
f792889a 21497 this_type = read_enumeration_type (die, cu);
c906108c
SS
21498 break;
21499 case DW_TAG_subprogram:
21500 case DW_TAG_subroutine_type:
edb3359d 21501 case DW_TAG_inlined_subroutine:
f792889a 21502 this_type = read_subroutine_type (die, cu);
c906108c
SS
21503 break;
21504 case DW_TAG_array_type:
f792889a 21505 this_type = read_array_type (die, cu);
c906108c 21506 break;
72019c9c 21507 case DW_TAG_set_type:
f792889a 21508 this_type = read_set_type (die, cu);
72019c9c 21509 break;
c906108c 21510 case DW_TAG_pointer_type:
f792889a 21511 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21512 break;
21513 case DW_TAG_ptr_to_member_type:
f792889a 21514 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21515 break;
21516 case DW_TAG_reference_type:
4297a3f0
AV
21517 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21518 break;
21519 case DW_TAG_rvalue_reference_type:
21520 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21521 break;
21522 case DW_TAG_const_type:
f792889a 21523 this_type = read_tag_const_type (die, cu);
c906108c
SS
21524 break;
21525 case DW_TAG_volatile_type:
f792889a 21526 this_type = read_tag_volatile_type (die, cu);
c906108c 21527 break;
06d66ee9
TT
21528 case DW_TAG_restrict_type:
21529 this_type = read_tag_restrict_type (die, cu);
21530 break;
c906108c 21531 case DW_TAG_string_type:
f792889a 21532 this_type = read_tag_string_type (die, cu);
c906108c
SS
21533 break;
21534 case DW_TAG_typedef:
f792889a 21535 this_type = read_typedef (die, cu);
c906108c 21536 break;
a02abb62 21537 case DW_TAG_subrange_type:
f792889a 21538 this_type = read_subrange_type (die, cu);
a02abb62 21539 break;
c906108c 21540 case DW_TAG_base_type:
f792889a 21541 this_type = read_base_type (die, cu);
c906108c 21542 break;
81a17f79 21543 case DW_TAG_unspecified_type:
f792889a 21544 this_type = read_unspecified_type (die, cu);
81a17f79 21545 break;
0114d602
DJ
21546 case DW_TAG_namespace:
21547 this_type = read_namespace_type (die, cu);
21548 break;
f55ee35c
JK
21549 case DW_TAG_module:
21550 this_type = read_module_type (die, cu);
21551 break;
a2c2acaf
MW
21552 case DW_TAG_atomic_type:
21553 this_type = read_tag_atomic_type (die, cu);
21554 break;
c906108c 21555 default:
b98664d3 21556 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21557 dwarf_tag_name (die->tag));
c906108c
SS
21558 break;
21559 }
63d06c5c 21560
f792889a 21561 return this_type;
63d06c5c
DC
21562}
21563
abc72ce4
DE
21564/* See if we can figure out if the class lives in a namespace. We do
21565 this by looking for a member function; its demangled name will
21566 contain namespace info, if there is any.
21567 Return the computed name or NULL.
21568 Space for the result is allocated on the objfile's obstack.
21569 This is the full-die version of guess_partial_die_structure_name.
21570 In this case we know DIE has no useful parent. */
21571
43816ebc 21572static const char *
abc72ce4
DE
21573guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21574{
21575 struct die_info *spec_die;
21576 struct dwarf2_cu *spec_cu;
21577 struct die_info *child;
5e22e966 21578 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21579
21580 spec_cu = cu;
21581 spec_die = die_specification (die, &spec_cu);
21582 if (spec_die != NULL)
21583 {
21584 die = spec_die;
21585 cu = spec_cu;
21586 }
21587
21588 for (child = die->child;
21589 child != NULL;
21590 child = child->sibling)
21591 {
21592 if (child->tag == DW_TAG_subprogram)
21593 {
73b9be8b 21594 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21595
7d45c7c3 21596 if (linkage_name != NULL)
abc72ce4 21597 {
43816ebc
TT
21598 gdb::unique_xmalloc_ptr<char> actual_name
21599 (language_class_name_from_physname (cu->language_defn,
21600 linkage_name));
21601 const char *name = NULL;
abc72ce4
DE
21602
21603 if (actual_name != NULL)
21604 {
15d034d0 21605 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21606
21607 if (die_name != NULL
43816ebc 21608 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21609 {
21610 /* Strip off the class name from the full name.
21611 We want the prefix. */
21612 int die_name_len = strlen (die_name);
43816ebc
TT
21613 int actual_name_len = strlen (actual_name.get ());
21614 const char *ptr = actual_name.get ();
abc72ce4
DE
21615
21616 /* Test for '::' as a sanity check. */
21617 if (actual_name_len > die_name_len + 2
43816ebc 21618 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21619 name = obstack_strndup (
e3b94546 21620 &objfile->per_bfd->storage_obstack,
43816ebc 21621 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21622 }
21623 }
abc72ce4
DE
21624 return name;
21625 }
21626 }
21627 }
21628
21629 return NULL;
21630}
21631
96408a79
SA
21632/* GCC might emit a nameless typedef that has a linkage name. Determine the
21633 prefix part in such case. See
21634 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21635
a121b7c1 21636static const char *
96408a79
SA
21637anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21638{
21639 struct attribute *attr;
e6a959d6 21640 const char *base;
96408a79
SA
21641
21642 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21643 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21644 return NULL;
21645
7d45c7c3 21646 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21647 return NULL;
21648
73b9be8b 21649 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21650 if (attr == NULL || DW_STRING (attr) == NULL)
21651 return NULL;
21652
21653 /* dwarf2_name had to be already called. */
21654 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21655
21656 /* Strip the base name, keep any leading namespaces/classes. */
21657 base = strrchr (DW_STRING (attr), ':');
21658 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21659 return "";
21660
5e22e966 21661 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21662 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21663 DW_STRING (attr),
21664 &base[-1] - DW_STRING (attr));
96408a79
SA
21665}
21666
fdde2d81 21667/* Return the name of the namespace/class that DIE is defined within,
0114d602 21668 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21669
0114d602
DJ
21670 For example, if we're within the method foo() in the following
21671 code:
21672
21673 namespace N {
21674 class C {
21675 void foo () {
21676 }
21677 };
21678 }
21679
21680 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21681
0d5cff50 21682static const char *
e142c38c 21683determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21684{
5e22e966 21685 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21686 struct die_info *parent, *spec_die;
21687 struct dwarf2_cu *spec_cu;
21688 struct type *parent_type;
a121b7c1 21689 const char *retval;
63d06c5c 21690
9c37b5ae 21691 if (cu->language != language_cplus
c44af4eb
TT
21692 && cu->language != language_fortran && cu->language != language_d
21693 && cu->language != language_rust)
0114d602
DJ
21694 return "";
21695
96408a79
SA
21696 retval = anonymous_struct_prefix (die, cu);
21697 if (retval)
21698 return retval;
21699
0114d602
DJ
21700 /* We have to be careful in the presence of DW_AT_specification.
21701 For example, with GCC 3.4, given the code
21702
21703 namespace N {
21704 void foo() {
21705 // Definition of N::foo.
21706 }
21707 }
21708
21709 then we'll have a tree of DIEs like this:
21710
21711 1: DW_TAG_compile_unit
21712 2: DW_TAG_namespace // N
21713 3: DW_TAG_subprogram // declaration of N::foo
21714 4: DW_TAG_subprogram // definition of N::foo
21715 DW_AT_specification // refers to die #3
21716
21717 Thus, when processing die #4, we have to pretend that we're in
21718 the context of its DW_AT_specification, namely the contex of die
21719 #3. */
21720 spec_cu = cu;
21721 spec_die = die_specification (die, &spec_cu);
21722 if (spec_die == NULL)
21723 parent = die->parent;
21724 else
63d06c5c 21725 {
0114d602
DJ
21726 parent = spec_die->parent;
21727 cu = spec_cu;
63d06c5c 21728 }
0114d602
DJ
21729
21730 if (parent == NULL)
21731 return "";
98bfdba5
PA
21732 else if (parent->building_fullname)
21733 {
21734 const char *name;
21735 const char *parent_name;
21736
21737 /* It has been seen on RealView 2.2 built binaries,
21738 DW_TAG_template_type_param types actually _defined_ as
21739 children of the parent class:
21740
21741 enum E {};
21742 template class <class Enum> Class{};
21743 Class<enum E> class_e;
21744
21745 1: DW_TAG_class_type (Class)
21746 2: DW_TAG_enumeration_type (E)
21747 3: DW_TAG_enumerator (enum1:0)
21748 3: DW_TAG_enumerator (enum2:1)
21749 ...
21750 2: DW_TAG_template_type_param
21751 DW_AT_type DW_FORM_ref_udata (E)
21752
21753 Besides being broken debug info, it can put GDB into an
21754 infinite loop. Consider:
21755
21756 When we're building the full name for Class<E>, we'll start
21757 at Class, and go look over its template type parameters,
21758 finding E. We'll then try to build the full name of E, and
21759 reach here. We're now trying to build the full name of E,
21760 and look over the parent DIE for containing scope. In the
21761 broken case, if we followed the parent DIE of E, we'd again
21762 find Class, and once again go look at its template type
21763 arguments, etc., etc. Simply don't consider such parent die
21764 as source-level parent of this die (it can't be, the language
21765 doesn't allow it), and break the loop here. */
21766 name = dwarf2_name (die, cu);
21767 parent_name = dwarf2_name (parent, cu);
b98664d3 21768 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21769 name ? name : "<unknown>",
21770 parent_name ? parent_name : "<unknown>");
21771 return "";
21772 }
63d06c5c 21773 else
0114d602
DJ
21774 switch (parent->tag)
21775 {
63d06c5c 21776 case DW_TAG_namespace:
0114d602 21777 parent_type = read_type_die (parent, cu);
acebe513
UW
21778 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21779 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21780 Work around this problem here. */
21781 if (cu->language == language_cplus
7d93a1e0 21782 && strcmp (parent_type->name (), "::") == 0)
acebe513 21783 return "";
0114d602 21784 /* We give a name to even anonymous namespaces. */
7d93a1e0 21785 return parent_type->name ();
63d06c5c 21786 case DW_TAG_class_type:
680b30c7 21787 case DW_TAG_interface_type:
63d06c5c 21788 case DW_TAG_structure_type:
0114d602 21789 case DW_TAG_union_type:
f55ee35c 21790 case DW_TAG_module:
0114d602 21791 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21792 if (parent_type->name () != NULL)
21793 return parent_type->name ();
0114d602
DJ
21794 else
21795 /* An anonymous structure is only allowed non-static data
21796 members; no typedefs, no member functions, et cetera.
21797 So it does not need a prefix. */
21798 return "";
abc72ce4 21799 case DW_TAG_compile_unit:
95554aad 21800 case DW_TAG_partial_unit:
abc72ce4
DE
21801 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21802 if (cu->language == language_cplus
5989a64e 21803 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21804 && die->child != NULL
21805 && (die->tag == DW_TAG_class_type
21806 || die->tag == DW_TAG_structure_type
21807 || die->tag == DW_TAG_union_type))
21808 {
43816ebc 21809 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21810 if (name != NULL)
21811 return name;
21812 }
21813 return "";
0a4b0913
AB
21814 case DW_TAG_subprogram:
21815 /* Nested subroutines in Fortran get a prefix with the name
21816 of the parent's subroutine. */
21817 if (cu->language == language_fortran)
21818 {
21819 if ((die->tag == DW_TAG_subprogram)
21820 && (dwarf2_name (parent, cu) != NULL))
21821 return dwarf2_name (parent, cu);
21822 }
21823 return determine_prefix (parent, cu);
3d567982
TT
21824 case DW_TAG_enumeration_type:
21825 parent_type = read_type_die (parent, cu);
21826 if (TYPE_DECLARED_CLASS (parent_type))
21827 {
7d93a1e0
SM
21828 if (parent_type->name () != NULL)
21829 return parent_type->name ();
3d567982
TT
21830 return "";
21831 }
21832 /* Fall through. */
63d06c5c 21833 default:
8176b9b8 21834 return determine_prefix (parent, cu);
63d06c5c 21835 }
63d06c5c
DC
21836}
21837
3e43a32a
MS
21838/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21839 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21840 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21841 an obconcat, otherwise allocate storage for the result. The CU argument is
21842 used to determine the language and hence, the appropriate separator. */
987504bb 21843
f55ee35c 21844#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21845
21846static char *
f55ee35c
JK
21847typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21848 int physname, struct dwarf2_cu *cu)
63d06c5c 21849{
f55ee35c 21850 const char *lead = "";
5c315b68 21851 const char *sep;
63d06c5c 21852
3e43a32a
MS
21853 if (suffix == NULL || suffix[0] == '\0'
21854 || prefix == NULL || prefix[0] == '\0')
987504bb 21855 sep = "";
45280282
IB
21856 else if (cu->language == language_d)
21857 {
21858 /* For D, the 'main' function could be defined in any module, but it
21859 should never be prefixed. */
21860 if (strcmp (suffix, "D main") == 0)
21861 {
21862 prefix = "";
21863 sep = "";
21864 }
21865 else
21866 sep = ".";
21867 }
f55ee35c
JK
21868 else if (cu->language == language_fortran && physname)
21869 {
21870 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21871 DW_AT_MIPS_linkage_name is preferred and used instead. */
21872
21873 lead = "__";
21874 sep = "_MOD_";
21875 }
987504bb
JJ
21876 else
21877 sep = "::";
63d06c5c 21878
6dd47d34
DE
21879 if (prefix == NULL)
21880 prefix = "";
21881 if (suffix == NULL)
21882 suffix = "";
21883
987504bb
JJ
21884 if (obs == NULL)
21885 {
3e43a32a 21886 char *retval
224c3ddb
SM
21887 = ((char *)
21888 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21889
f55ee35c
JK
21890 strcpy (retval, lead);
21891 strcat (retval, prefix);
6dd47d34
DE
21892 strcat (retval, sep);
21893 strcat (retval, suffix);
63d06c5c
DC
21894 return retval;
21895 }
987504bb
JJ
21896 else
21897 {
21898 /* We have an obstack. */
f55ee35c 21899 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21900 }
63d06c5c
DC
21901}
21902
71c25dea
TT
21903/* Get name of a die, return NULL if not found. */
21904
15d034d0
TT
21905static const char *
21906dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21907 struct objfile *objfile)
71c25dea
TT
21908{
21909 if (name && cu->language == language_cplus)
21910 {
596dc4ad
TT
21911 gdb::unique_xmalloc_ptr<char> canon_name
21912 = cp_canonicalize_string (name);
71c25dea 21913
596dc4ad
TT
21914 if (canon_name != nullptr)
21915 name = objfile->intern (canon_name.get ());
71c25dea
TT
21916 }
21917
21918 return name;
c906108c
SS
21919}
21920
96553a0c
DE
21921/* Get name of a die, return NULL if not found.
21922 Anonymous namespaces are converted to their magic string. */
9219021c 21923
15d034d0 21924static const char *
e142c38c 21925dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21926{
21927 struct attribute *attr;
5e22e966 21928 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21929
e142c38c 21930 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21931 if ((!attr || !DW_STRING (attr))
96553a0c 21932 && die->tag != DW_TAG_namespace
53832f31
TT
21933 && die->tag != DW_TAG_class_type
21934 && die->tag != DW_TAG_interface_type
21935 && die->tag != DW_TAG_structure_type
21936 && die->tag != DW_TAG_union_type)
71c25dea
TT
21937 return NULL;
21938
21939 switch (die->tag)
21940 {
21941 case DW_TAG_compile_unit:
95554aad 21942 case DW_TAG_partial_unit:
71c25dea
TT
21943 /* Compilation units have a DW_AT_name that is a filename, not
21944 a source language identifier. */
21945 case DW_TAG_enumeration_type:
21946 case DW_TAG_enumerator:
21947 /* These tags always have simple identifiers already; no need
21948 to canonicalize them. */
21949 return DW_STRING (attr);
907af001 21950
96553a0c
DE
21951 case DW_TAG_namespace:
21952 if (attr != NULL && DW_STRING (attr) != NULL)
21953 return DW_STRING (attr);
21954 return CP_ANONYMOUS_NAMESPACE_STR;
21955
907af001
UW
21956 case DW_TAG_class_type:
21957 case DW_TAG_interface_type:
21958 case DW_TAG_structure_type:
21959 case DW_TAG_union_type:
21960 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21961 structures or unions. These were of the form "._%d" in GCC 4.1,
21962 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21963 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21964 if (attr && DW_STRING (attr)
61012eef
GB
21965 && (startswith (DW_STRING (attr), "._")
21966 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21967 return NULL;
53832f31
TT
21968
21969 /* GCC might emit a nameless typedef that has a linkage name. See
21970 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21971 if (!attr || DW_STRING (attr) == NULL)
21972 {
73b9be8b 21973 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21974 if (attr == NULL || DW_STRING (attr) == NULL)
21975 return NULL;
21976
df5c6c50
JK
21977 /* Avoid demangling DW_STRING (attr) the second time on a second
21978 call for the same DIE. */
21979 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21980 {
43816ebc
TT
21981 gdb::unique_xmalloc_ptr<char> demangled
21982 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21983 if (demangled == nullptr)
21984 return nullptr;
43816ebc 21985
be1e3d3e 21986 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21987 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21988 }
67430cd0
TT
21989
21990 /* Strip any leading namespaces/classes, keep only the base name.
21991 DW_AT_name for named DIEs does not contain the prefixes. */
21992 const char *base = strrchr (DW_STRING (attr), ':');
21993 if (base && base > DW_STRING (attr) && base[-1] == ':')
21994 return &base[1];
21995 else
21996 return DW_STRING (attr);
53832f31 21997 }
907af001
UW
21998 break;
21999
71c25dea 22000 default:
907af001
UW
22001 break;
22002 }
22003
22004 if (!DW_STRING_IS_CANONICAL (attr))
22005 {
be1e3d3e
TT
22006 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22007 objfile);
907af001 22008 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22009 }
907af001 22010 return DW_STRING (attr);
9219021c
DC
22011}
22012
22013/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22014 is none. *EXT_CU is the CU containing DIE on input, and the CU
22015 containing the return value on output. */
9219021c
DC
22016
22017static struct die_info *
f2f0e013 22018dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22019{
22020 struct attribute *attr;
9219021c 22021
f2f0e013 22022 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22023 if (attr == NULL)
22024 return NULL;
22025
f2f0e013 22026 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22027}
22028
f9aca02d 22029static void
d97bc12b 22030dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22031{
22032 unsigned int i;
22033
d97bc12b 22034 print_spaces (indent, f);
9d8780f0 22035 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22036 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22037 sect_offset_str (die->sect_off));
d97bc12b
DE
22038
22039 if (die->parent != NULL)
22040 {
22041 print_spaces (indent, f);
9d8780f0
SM
22042 fprintf_unfiltered (f, " parent at offset: %s\n",
22043 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22044 }
22045
22046 print_spaces (indent, f);
22047 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22048 dwarf_bool_name (die->child != NULL));
c906108c 22049
d97bc12b
DE
22050 print_spaces (indent, f);
22051 fprintf_unfiltered (f, " attributes:\n");
22052
c906108c
SS
22053 for (i = 0; i < die->num_attrs; ++i)
22054 {
d97bc12b
DE
22055 print_spaces (indent, f);
22056 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22057 dwarf_attr_name (die->attrs[i].name),
22058 dwarf_form_name (die->attrs[i].form));
d97bc12b 22059
c906108c
SS
22060 switch (die->attrs[i].form)
22061 {
c906108c 22062 case DW_FORM_addr:
336d760d 22063 case DW_FORM_addrx:
3019eac3 22064 case DW_FORM_GNU_addr_index:
d97bc12b 22065 fprintf_unfiltered (f, "address: ");
5af949e3 22066 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22067 break;
22068 case DW_FORM_block2:
22069 case DW_FORM_block4:
22070 case DW_FORM_block:
22071 case DW_FORM_block1:
56eb65bd
SP
22072 fprintf_unfiltered (f, "block: size %s",
22073 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22074 break;
2dc7f7b3 22075 case DW_FORM_exprloc:
56eb65bd
SP
22076 fprintf_unfiltered (f, "expression: size %s",
22077 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22078 break;
0224619f
JK
22079 case DW_FORM_data16:
22080 fprintf_unfiltered (f, "constant of 16 bytes");
22081 break;
4568ecf9
DE
22082 case DW_FORM_ref_addr:
22083 fprintf_unfiltered (f, "ref address: ");
22084 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22085 break;
36586728
TT
22086 case DW_FORM_GNU_ref_alt:
22087 fprintf_unfiltered (f, "alt ref address: ");
22088 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22089 break;
10b3939b
DJ
22090 case DW_FORM_ref1:
22091 case DW_FORM_ref2:
22092 case DW_FORM_ref4:
4568ecf9
DE
22093 case DW_FORM_ref8:
22094 case DW_FORM_ref_udata:
d97bc12b 22095 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22096 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22097 break;
c906108c
SS
22098 case DW_FORM_data1:
22099 case DW_FORM_data2:
22100 case DW_FORM_data4:
ce5d95e1 22101 case DW_FORM_data8:
c906108c
SS
22102 case DW_FORM_udata:
22103 case DW_FORM_sdata:
43bbcdc2
PH
22104 fprintf_unfiltered (f, "constant: %s",
22105 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22106 break;
2dc7f7b3
TT
22107 case DW_FORM_sec_offset:
22108 fprintf_unfiltered (f, "section offset: %s",
22109 pulongest (DW_UNSND (&die->attrs[i])));
22110 break;
55f1336d 22111 case DW_FORM_ref_sig8:
ac9ec31b
DE
22112 fprintf_unfiltered (f, "signature: %s",
22113 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22114 break;
c906108c 22115 case DW_FORM_string:
4bdf3d34 22116 case DW_FORM_strp:
43988095 22117 case DW_FORM_line_strp:
cf532bd1 22118 case DW_FORM_strx:
3019eac3 22119 case DW_FORM_GNU_str_index:
36586728 22120 case DW_FORM_GNU_strp_alt:
8285870a 22121 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22122 DW_STRING (&die->attrs[i])
8285870a
JK
22123 ? DW_STRING (&die->attrs[i]) : "",
22124 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22125 break;
22126 case DW_FORM_flag:
22127 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22128 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22129 else
d97bc12b 22130 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22131 break;
2dc7f7b3
TT
22132 case DW_FORM_flag_present:
22133 fprintf_unfiltered (f, "flag: TRUE");
22134 break;
a8329558 22135 case DW_FORM_indirect:
0963b4bd
MS
22136 /* The reader will have reduced the indirect form to
22137 the "base form" so this form should not occur. */
5f48f8f3 22138 fprintf_unfiltered (f,
3e43a32a 22139 "unexpected attribute form: DW_FORM_indirect");
a8329558 22140 break;
663c44ac
JK
22141 case DW_FORM_implicit_const:
22142 fprintf_unfiltered (f, "constant: %s",
22143 plongest (DW_SND (&die->attrs[i])));
22144 break;
c906108c 22145 default:
d97bc12b 22146 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22147 die->attrs[i].form);
d97bc12b 22148 break;
c906108c 22149 }
d97bc12b 22150 fprintf_unfiltered (f, "\n");
c906108c
SS
22151 }
22152}
22153
f9aca02d 22154static void
d97bc12b 22155dump_die_for_error (struct die_info *die)
c906108c 22156{
d97bc12b
DE
22157 dump_die_shallow (gdb_stderr, 0, die);
22158}
22159
22160static void
22161dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22162{
22163 int indent = level * 4;
22164
22165 gdb_assert (die != NULL);
22166
22167 if (level >= max_level)
22168 return;
22169
22170 dump_die_shallow (f, indent, die);
22171
22172 if (die->child != NULL)
c906108c 22173 {
d97bc12b
DE
22174 print_spaces (indent, f);
22175 fprintf_unfiltered (f, " Children:");
22176 if (level + 1 < max_level)
22177 {
22178 fprintf_unfiltered (f, "\n");
22179 dump_die_1 (f, level + 1, max_level, die->child);
22180 }
22181 else
22182 {
3e43a32a
MS
22183 fprintf_unfiltered (f,
22184 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22185 }
22186 }
22187
22188 if (die->sibling != NULL && level > 0)
22189 {
22190 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22191 }
22192}
22193
d97bc12b
DE
22194/* This is called from the pdie macro in gdbinit.in.
22195 It's not static so gcc will keep a copy callable from gdb. */
22196
22197void
22198dump_die (struct die_info *die, int max_level)
22199{
22200 dump_die_1 (gdb_stdlog, 0, max_level, die);
22201}
22202
f9aca02d 22203static void
51545339 22204store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22205{
51545339 22206 void **slot;
c906108c 22207
9c541725
PA
22208 slot = htab_find_slot_with_hash (cu->die_hash, die,
22209 to_underlying (die->sect_off),
b64f50a1 22210 INSERT);
51545339
DJ
22211
22212 *slot = die;
c906108c
SS
22213}
22214
348e048f
DE
22215/* Follow reference or signature attribute ATTR of SRC_DIE.
22216 On entry *REF_CU is the CU of SRC_DIE.
22217 On exit *REF_CU is the CU of the result. */
22218
22219static struct die_info *
ff39bb5e 22220follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22221 struct dwarf2_cu **ref_cu)
22222{
22223 struct die_info *die;
22224
cd6c91b4 22225 if (attr->form_is_ref ())
348e048f 22226 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22227 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22228 die = follow_die_sig (src_die, attr, ref_cu);
22229 else
22230 {
22231 dump_die_for_error (src_die);
22232 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22233 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22234 }
22235
22236 return die;
03dd20cc
DJ
22237}
22238
5c631832 22239/* Follow reference OFFSET.
673bfd45
DE
22240 On entry *REF_CU is the CU of the source die referencing OFFSET.
22241 On exit *REF_CU is the CU of the result.
22242 Returns NULL if OFFSET is invalid. */
f504f079 22243
f9aca02d 22244static struct die_info *
9c541725 22245follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22246 struct dwarf2_cu **ref_cu)
c906108c 22247{
10b3939b 22248 struct die_info temp_die;
f2f0e013 22249 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22250 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22251
348e048f
DE
22252 gdb_assert (cu->per_cu != NULL);
22253
98bfdba5
PA
22254 target_cu = cu;
22255
3019eac3 22256 if (cu->per_cu->is_debug_types)
348e048f
DE
22257 {
22258 /* .debug_types CUs cannot reference anything outside their CU.
22259 If they need to, they have to reference a signatured type via
55f1336d 22260 DW_FORM_ref_sig8. */
4057dfde 22261 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22262 return NULL;
348e048f 22263 }
36586728 22264 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22265 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22266 {
22267 struct dwarf2_per_cu_data *per_cu;
9a619af0 22268
9c541725 22269 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22270 dwarf2_per_objfile);
03dd20cc
DJ
22271
22272 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22273 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22274 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22275
10b3939b
DJ
22276 target_cu = per_cu->cu;
22277 }
98bfdba5
PA
22278 else if (cu->dies == NULL)
22279 {
22280 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22281 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22282 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22283 language_minimal);
98bfdba5 22284 }
c906108c 22285
f2f0e013 22286 *ref_cu = target_cu;
9c541725 22287 temp_die.sect_off = sect_off;
c24bdb02
KS
22288
22289 if (target_cu != cu)
22290 target_cu->ancestor = cu;
22291
9a3c8263 22292 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22293 &temp_die,
22294 to_underlying (sect_off));
5c631832 22295}
10b3939b 22296
5c631832
JK
22297/* Follow reference attribute ATTR of SRC_DIE.
22298 On entry *REF_CU is the CU of SRC_DIE.
22299 On exit *REF_CU is the CU of the result. */
22300
22301static struct die_info *
ff39bb5e 22302follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22303 struct dwarf2_cu **ref_cu)
22304{
0826b30a 22305 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22306 struct dwarf2_cu *cu = *ref_cu;
22307 struct die_info *die;
22308
9c541725 22309 die = follow_die_offset (sect_off,
36586728
TT
22310 (attr->form == DW_FORM_GNU_ref_alt
22311 || cu->per_cu->is_dwz),
22312 ref_cu);
5c631832 22313 if (!die)
9d8780f0
SM
22314 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22315 "at %s [in module %s]"),
22316 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22317 objfile_name (cu->per_objfile->objfile));
348e048f 22318
5c631832
JK
22319 return die;
22320}
22321
d4c9a4f8 22322/* See read.h. */
5c631832
JK
22323
22324struct dwarf2_locexpr_baton
9c541725 22325dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22326 dwarf2_per_cu_data *per_cu,
14095eb3 22327 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22328 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22329 void *baton, bool resolve_abstract_p)
5c631832 22330{
918dd910 22331 struct dwarf2_cu *cu;
5c631832
JK
22332 struct die_info *die;
22333 struct attribute *attr;
22334 struct dwarf2_locexpr_baton retval;
12359b5e 22335 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22336
918dd910 22337 if (per_cu->cu == NULL)
ab432490 22338 load_cu (per_cu, dwarf2_per_objfile, false);
918dd910 22339 cu = per_cu->cu;
cc12ce38
DE
22340 if (cu == NULL)
22341 {
22342 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22343 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22344 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22345 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22346 }
918dd910 22347
9c541725 22348 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22349 if (!die)
9d8780f0
SM
22350 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22351 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22352
22353 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22354 if (!attr && resolve_abstract_p
5989a64e
SM
22355 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22356 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22357 {
22358 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22359 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22360 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22361
3360b6e7 22362 for (const auto &cand_off
5989a64e 22363 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22364 {
3360b6e7
TV
22365 struct dwarf2_cu *cand_cu = cu;
22366 struct die_info *cand
22367 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22368 if (!cand
22369 || !cand->parent
e4a62c65
TV
22370 || cand->parent->tag != DW_TAG_subprogram)
22371 continue;
22372
22373 CORE_ADDR pc_low, pc_high;
22374 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22375 if (pc_low == ((CORE_ADDR) -1))
22376 continue;
22377 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22378 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22379 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22380 continue;
22381
22382 die = cand;
22383 attr = dwarf2_attr (die, DW_AT_location, cu);
22384 break;
22385 }
22386 }
22387
5c631832
JK
22388 if (!attr)
22389 {
e103e986
JK
22390 /* DWARF: "If there is no such attribute, then there is no effect.".
22391 DATA is ignored if SIZE is 0. */
5c631832 22392
e103e986 22393 retval.data = NULL;
5c631832
JK
22394 retval.size = 0;
22395 }
cd6c91b4 22396 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22397 {
22398 struct dwarf2_loclist_baton loclist_baton;
22399 CORE_ADDR pc = (*get_frame_pc) (baton);
22400 size_t size;
22401
22402 fill_in_loclist_baton (cu, &loclist_baton, attr);
22403
22404 retval.data = dwarf2_find_location_expression (&loclist_baton,
22405 &size, pc);
22406 retval.size = size;
22407 }
5c631832
JK
22408 else
22409 {
4fc6c0d5 22410 if (!attr->form_is_block ())
9d8780f0 22411 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22412 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22413 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22414
22415 retval.data = DW_BLOCK (attr)->data;
22416 retval.size = DW_BLOCK (attr)->size;
22417 }
a50264ba 22418 retval.per_objfile = dwarf2_per_objfile;
5c631832 22419 retval.per_cu = cu->per_cu;
918dd910 22420
ed2dc618 22421 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22422
5c631832 22423 return retval;
348e048f
DE
22424}
22425
d4c9a4f8 22426/* See read.h. */
8b9737bf
TT
22427
22428struct dwarf2_locexpr_baton
22429dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22430 dwarf2_per_cu_data *per_cu,
14095eb3 22431 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22432 CORE_ADDR (*get_frame_pc) (void *baton),
22433 void *baton)
22434{
9c541725 22435 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22436
14095eb3
SM
22437 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22438 get_frame_pc, baton);
8b9737bf
TT
22439}
22440
b6807d98
TT
22441/* Write a constant of a given type as target-ordered bytes into
22442 OBSTACK. */
22443
22444static const gdb_byte *
22445write_constant_as_bytes (struct obstack *obstack,
22446 enum bfd_endian byte_order,
22447 struct type *type,
22448 ULONGEST value,
22449 LONGEST *len)
22450{
22451 gdb_byte *result;
22452
22453 *len = TYPE_LENGTH (type);
224c3ddb 22454 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22455 store_unsigned_integer (result, *len, byte_order, value);
22456
22457 return result;
22458}
22459
d4c9a4f8 22460/* See read.h. */
b6807d98
TT
22461
22462const gdb_byte *
9c541725 22463dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22464 dwarf2_per_cu_data *per_cu,
14095eb3 22465 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22466 obstack *obstack,
b6807d98
TT
22467 LONGEST *len)
22468{
22469 struct dwarf2_cu *cu;
22470 struct die_info *die;
22471 struct attribute *attr;
22472 const gdb_byte *result = NULL;
22473 struct type *type;
22474 LONGEST value;
22475 enum bfd_endian byte_order;
14095eb3 22476 struct objfile *objfile = per_objfile->objfile;
b6807d98 22477
b6807d98 22478 if (per_cu->cu == NULL)
14095eb3 22479 load_cu (per_cu, per_objfile, false);
b6807d98 22480 cu = per_cu->cu;
cc12ce38
DE
22481 if (cu == NULL)
22482 {
22483 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22484 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22485 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22486 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22487 }
b6807d98 22488
9c541725 22489 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22490 if (!die)
9d8780f0
SM
22491 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22492 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22493
22494 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22495 if (attr == NULL)
22496 return NULL;
22497
e3b94546 22498 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22499 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22500
22501 switch (attr->form)
22502 {
22503 case DW_FORM_addr:
336d760d 22504 case DW_FORM_addrx:
b6807d98
TT
22505 case DW_FORM_GNU_addr_index:
22506 {
22507 gdb_byte *tem;
22508
22509 *len = cu->header.addr_size;
224c3ddb 22510 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22511 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22512 result = tem;
22513 }
22514 break;
22515 case DW_FORM_string:
22516 case DW_FORM_strp:
cf532bd1 22517 case DW_FORM_strx:
b6807d98
TT
22518 case DW_FORM_GNU_str_index:
22519 case DW_FORM_GNU_strp_alt:
22520 /* DW_STRING is already allocated on the objfile obstack, point
22521 directly to it. */
22522 result = (const gdb_byte *) DW_STRING (attr);
22523 *len = strlen (DW_STRING (attr));
22524 break;
22525 case DW_FORM_block1:
22526 case DW_FORM_block2:
22527 case DW_FORM_block4:
22528 case DW_FORM_block:
22529 case DW_FORM_exprloc:
0224619f 22530 case DW_FORM_data16:
b6807d98
TT
22531 result = DW_BLOCK (attr)->data;
22532 *len = DW_BLOCK (attr)->size;
22533 break;
22534
22535 /* The DW_AT_const_value attributes are supposed to carry the
22536 symbol's value "represented as it would be on the target
22537 architecture." By the time we get here, it's already been
22538 converted to host endianness, so we just need to sign- or
22539 zero-extend it as appropriate. */
22540 case DW_FORM_data1:
22541 type = die_type (die, cu);
22542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22543 if (result == NULL)
22544 result = write_constant_as_bytes (obstack, byte_order,
22545 type, value, len);
22546 break;
22547 case DW_FORM_data2:
22548 type = die_type (die, cu);
22549 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22550 if (result == NULL)
22551 result = write_constant_as_bytes (obstack, byte_order,
22552 type, value, len);
22553 break;
22554 case DW_FORM_data4:
22555 type = die_type (die, cu);
22556 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22557 if (result == NULL)
22558 result = write_constant_as_bytes (obstack, byte_order,
22559 type, value, len);
22560 break;
22561 case DW_FORM_data8:
22562 type = die_type (die, cu);
22563 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22564 if (result == NULL)
22565 result = write_constant_as_bytes (obstack, byte_order,
22566 type, value, len);
22567 break;
22568
22569 case DW_FORM_sdata:
663c44ac 22570 case DW_FORM_implicit_const:
b6807d98
TT
22571 type = die_type (die, cu);
22572 result = write_constant_as_bytes (obstack, byte_order,
22573 type, DW_SND (attr), len);
22574 break;
22575
22576 case DW_FORM_udata:
22577 type = die_type (die, cu);
22578 result = write_constant_as_bytes (obstack, byte_order,
22579 type, DW_UNSND (attr), len);
22580 break;
22581
22582 default:
b98664d3 22583 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22584 dwarf_form_name (attr->form));
22585 break;
22586 }
22587
22588 return result;
22589}
22590
d4c9a4f8 22591/* See read.h. */
7942e96e
AA
22592
22593struct type *
9c541725 22594dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22595 dwarf2_per_cu_data *per_cu,
22596 dwarf2_per_objfile *per_objfile)
7942e96e
AA
22597{
22598 struct dwarf2_cu *cu;
22599 struct die_info *die;
22600
7942e96e 22601 if (per_cu->cu == NULL)
14095eb3 22602 load_cu (per_cu, per_objfile, false);
7942e96e
AA
22603 cu = per_cu->cu;
22604 if (!cu)
22605 return NULL;
22606
9c541725 22607 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22608 if (!die)
22609 return NULL;
22610
22611 return die_type (die, cu);
22612}
22613
8cb5117c 22614/* See read.h. */
8a9b8146
TT
22615
22616struct type *
b64f50a1 22617dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22618 dwarf2_per_cu_data *per_cu,
22619 dwarf2_per_objfile *per_objfile)
8a9b8146 22620{
9c541725 22621 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22622 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22623}
22624
ac9ec31b 22625/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22626 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22627 On exit *REF_CU is the CU of the result.
22628 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22629
22630static struct die_info *
ac9ec31b
DE
22631follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22632 struct dwarf2_cu **ref_cu)
348e048f 22633{
348e048f 22634 struct die_info temp_die;
c24bdb02 22635 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22636 struct die_info *die;
120ce1b5
SM
22637 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22638
348e048f 22639
ac9ec31b
DE
22640 /* While it might be nice to assert sig_type->type == NULL here,
22641 we can get here for DW_AT_imported_declaration where we need
22642 the DIE not the type. */
348e048f
DE
22643
22644 /* If necessary, add it to the queue and load its DIEs. */
22645
120ce1b5
SM
22646 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22647 language_minimal))
22648 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22649
348e048f 22650 sig_cu = sig_type->per_cu.cu;
69d751e3 22651 gdb_assert (sig_cu != NULL);
9c541725
PA
22652 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22653 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22654 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22655 to_underlying (temp_die.sect_off));
348e048f
DE
22656 if (die)
22657 {
796a7ff8
DE
22658 /* For .gdb_index version 7 keep track of included TUs.
22659 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22660 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22661 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22662 {
ae640021 22663 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22664 }
22665
348e048f 22666 *ref_cu = sig_cu;
c24bdb02
KS
22667 if (sig_cu != cu)
22668 sig_cu->ancestor = cu;
22669
348e048f
DE
22670 return die;
22671 }
22672
ac9ec31b
DE
22673 return NULL;
22674}
22675
22676/* Follow signatured type referenced by ATTR in SRC_DIE.
22677 On entry *REF_CU is the CU of SRC_DIE.
22678 On exit *REF_CU is the CU of the result.
22679 The result is the DIE of the type.
22680 If the referenced type cannot be found an error is thrown. */
22681
22682static struct die_info *
ff39bb5e 22683follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22684 struct dwarf2_cu **ref_cu)
22685{
22686 ULONGEST signature = DW_SIGNATURE (attr);
22687 struct signatured_type *sig_type;
22688 struct die_info *die;
22689
22690 gdb_assert (attr->form == DW_FORM_ref_sig8);
22691
a2ce51a0 22692 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22693 /* sig_type will be NULL if the signatured type is missing from
22694 the debug info. */
22695 if (sig_type == NULL)
22696 {
22697 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22698 " from DIE at %s [in module %s]"),
22699 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22700 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22701 }
22702
22703 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22704 if (die == NULL)
22705 {
22706 dump_die_for_error (src_die);
22707 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22708 " from DIE at %s [in module %s]"),
22709 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22710 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22711 }
22712
22713 return die;
22714}
22715
22716/* Get the type specified by SIGNATURE referenced in DIE/CU,
22717 reading in and processing the type unit if necessary. */
22718
22719static struct type *
22720get_signatured_type (struct die_info *die, ULONGEST signature,
22721 struct dwarf2_cu *cu)
22722{
5e22e966 22723 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22724 struct signatured_type *sig_type;
22725 struct dwarf2_cu *type_cu;
22726 struct die_info *type_die;
22727 struct type *type;
22728
a2ce51a0 22729 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22730 /* sig_type will be NULL if the signatured type is missing from
22731 the debug info. */
22732 if (sig_type == NULL)
22733 {
b98664d3 22734 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22735 " from DIE at %s [in module %s]"),
22736 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22737 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22738 return build_error_marker_type (cu, die);
22739 }
22740
22741 /* If we already know the type we're done. */
e286671b
TT
22742 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22743 if (type != nullptr)
22744 return type;
ac9ec31b
DE
22745
22746 type_cu = cu;
22747 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22748 if (type_die != NULL)
22749 {
22750 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22751 is created. This is important, for example, because for c++ classes
22752 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22753 type = read_type_die (type_die, type_cu);
22754 if (type == NULL)
22755 {
b98664d3 22756 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22757 " referenced from DIE at %s [in module %s]"),
22758 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22759 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22760 type = build_error_marker_type (cu, die);
22761 }
22762 }
22763 else
22764 {
b98664d3 22765 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22766 " from DIE at %s [in module %s]"),
22767 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22768 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22769 type = build_error_marker_type (cu, die);
22770 }
e286671b
TT
22771
22772 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22773
22774 return type;
22775}
22776
22777/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22778 reading in and processing the type unit if necessary. */
22779
22780static struct type *
ff39bb5e 22781get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22782 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22783{
22784 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22785 if (attr->form_is_ref ())
ac9ec31b
DE
22786 {
22787 struct dwarf2_cu *type_cu = cu;
22788 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22789
22790 return read_type_die (type_die, type_cu);
22791 }
22792 else if (attr->form == DW_FORM_ref_sig8)
22793 {
22794 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22795 }
22796 else
22797 {
5e22e966 22798 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22799
b98664d3 22800 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22801 " at %s [in module %s]"),
22802 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22803 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22804 return build_error_marker_type (cu, die);
22805 }
348e048f
DE
22806}
22807
e5fe5e75 22808/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22809
22810static void
ab432490
SM
22811load_full_type_unit (dwarf2_per_cu_data *per_cu,
22812 dwarf2_per_objfile *per_objfile)
348e048f 22813{
52dc124a 22814 struct signatured_type *sig_type;
348e048f 22815
f4dc4d17 22816 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22817 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22818
6721b2ec
DE
22819 /* We have the per_cu, but we need the signatured_type.
22820 Fortunately this is an easy translation. */
22821 gdb_assert (per_cu->is_debug_types);
22822 sig_type = (struct signatured_type *) per_cu;
348e048f 22823
6721b2ec 22824 gdb_assert (per_cu->cu == NULL);
348e048f 22825
ab432490 22826 read_signatured_type (sig_type, per_objfile);
348e048f 22827
6721b2ec 22828 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22829}
22830
3019eac3
DE
22831/* Read in a signatured type and build its CU and DIEs.
22832 If the type is a stub for the real type in a DWO file,
22833 read in the real type from the DWO file as well. */
dee91e82
DE
22834
22835static void
ab432490
SM
22836read_signatured_type (signatured_type *sig_type,
22837 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22838{
22839 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22840
3019eac3 22841 gdb_assert (per_cu->is_debug_types);
dee91e82 22842 gdb_assert (per_cu->cu == NULL);
348e048f 22843
ab432490 22844 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
c0ab21c2
TT
22845
22846 if (!reader.dummy_p)
22847 {
22848 struct dwarf2_cu *cu = reader.cu;
22849 const gdb_byte *info_ptr = reader.info_ptr;
22850
22851 gdb_assert (cu->die_hash == NULL);
22852 cu->die_hash =
22853 htab_create_alloc_ex (cu->header.length / 12,
22854 die_hash,
22855 die_eq,
22856 NULL,
22857 &cu->comp_unit_obstack,
22858 hashtab_obstack_allocate,
22859 dummy_obstack_deallocate);
22860
3e225074 22861 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22862 reader.comp_unit_die->child
22863 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22864 reader.comp_unit_die);
22865 cu->dies = reader.comp_unit_die;
22866 /* comp_unit_die is not stored in die_hash, no need. */
22867
22868 /* We try not to read any attributes in this function, because
22869 not all CUs needed for references have been loaded yet, and
22870 symbol table processing isn't initialized. But we have to
22871 set the CU language, or we won't be able to build types
22872 correctly. Similarly, if we do not read the producer, we can
22873 not apply producer-specific interpretation. */
22874 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22875
22876 reader.keep ();
c0ab21c2
TT
22877 }
22878
7ee85ab1 22879 sig_type->per_cu.tu_read = 1;
c906108c
SS
22880}
22881
c906108c
SS
22882/* Decode simple location descriptions.
22883 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22884 the location and return the value. If COMPUTED is non-null, it is
22885 set to true to indicate that decoding was successful, and false
22886 otherwise. If COMPUTED is null, then this function may emit a
22887 complaint. */
c906108c
SS
22888
22889static CORE_ADDR
7d79de9a 22890decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22891{
5e22e966 22892 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22893 size_t i;
22894 size_t size = blk->size;
d521ce57 22895 const gdb_byte *data = blk->data;
21ae7a4d
JK
22896 CORE_ADDR stack[64];
22897 int stacki;
22898 unsigned int bytes_read, unsnd;
22899 gdb_byte op;
c906108c 22900
7d79de9a
TT
22901 if (computed != nullptr)
22902 *computed = false;
22903
21ae7a4d
JK
22904 i = 0;
22905 stacki = 0;
22906 stack[stacki] = 0;
22907 stack[++stacki] = 0;
22908
22909 while (i < size)
22910 {
22911 op = data[i++];
22912 switch (op)
22913 {
22914 case DW_OP_lit0:
22915 case DW_OP_lit1:
22916 case DW_OP_lit2:
22917 case DW_OP_lit3:
22918 case DW_OP_lit4:
22919 case DW_OP_lit5:
22920 case DW_OP_lit6:
22921 case DW_OP_lit7:
22922 case DW_OP_lit8:
22923 case DW_OP_lit9:
22924 case DW_OP_lit10:
22925 case DW_OP_lit11:
22926 case DW_OP_lit12:
22927 case DW_OP_lit13:
22928 case DW_OP_lit14:
22929 case DW_OP_lit15:
22930 case DW_OP_lit16:
22931 case DW_OP_lit17:
22932 case DW_OP_lit18:
22933 case DW_OP_lit19:
22934 case DW_OP_lit20:
22935 case DW_OP_lit21:
22936 case DW_OP_lit22:
22937 case DW_OP_lit23:
22938 case DW_OP_lit24:
22939 case DW_OP_lit25:
22940 case DW_OP_lit26:
22941 case DW_OP_lit27:
22942 case DW_OP_lit28:
22943 case DW_OP_lit29:
22944 case DW_OP_lit30:
22945 case DW_OP_lit31:
22946 stack[++stacki] = op - DW_OP_lit0;
22947 break;
f1bea926 22948
21ae7a4d
JK
22949 case DW_OP_reg0:
22950 case DW_OP_reg1:
22951 case DW_OP_reg2:
22952 case DW_OP_reg3:
22953 case DW_OP_reg4:
22954 case DW_OP_reg5:
22955 case DW_OP_reg6:
22956 case DW_OP_reg7:
22957 case DW_OP_reg8:
22958 case DW_OP_reg9:
22959 case DW_OP_reg10:
22960 case DW_OP_reg11:
22961 case DW_OP_reg12:
22962 case DW_OP_reg13:
22963 case DW_OP_reg14:
22964 case DW_OP_reg15:
22965 case DW_OP_reg16:
22966 case DW_OP_reg17:
22967 case DW_OP_reg18:
22968 case DW_OP_reg19:
22969 case DW_OP_reg20:
22970 case DW_OP_reg21:
22971 case DW_OP_reg22:
22972 case DW_OP_reg23:
22973 case DW_OP_reg24:
22974 case DW_OP_reg25:
22975 case DW_OP_reg26:
22976 case DW_OP_reg27:
22977 case DW_OP_reg28:
22978 case DW_OP_reg29:
22979 case DW_OP_reg30:
22980 case DW_OP_reg31:
22981 stack[++stacki] = op - DW_OP_reg0;
22982 if (i < size)
7d79de9a
TT
22983 {
22984 if (computed == nullptr)
22985 dwarf2_complex_location_expr_complaint ();
22986 else
22987 return 0;
22988 }
21ae7a4d 22989 break;
c906108c 22990
21ae7a4d
JK
22991 case DW_OP_regx:
22992 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22993 i += bytes_read;
22994 stack[++stacki] = unsnd;
22995 if (i < size)
7d79de9a
TT
22996 {
22997 if (computed == nullptr)
22998 dwarf2_complex_location_expr_complaint ();
22999 else
23000 return 0;
23001 }
21ae7a4d 23002 break;
c906108c 23003
21ae7a4d 23004 case DW_OP_addr:
c8a7a66f
TT
23005 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23006 &bytes_read);
21ae7a4d
JK
23007 i += bytes_read;
23008 break;
d53d4ac5 23009
21ae7a4d
JK
23010 case DW_OP_const1u:
23011 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23012 i += 1;
23013 break;
23014
23015 case DW_OP_const1s:
23016 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23017 i += 1;
23018 break;
23019
23020 case DW_OP_const2u:
23021 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23022 i += 2;
23023 break;
23024
23025 case DW_OP_const2s:
23026 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23027 i += 2;
23028 break;
d53d4ac5 23029
21ae7a4d
JK
23030 case DW_OP_const4u:
23031 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23032 i += 4;
23033 break;
23034
23035 case DW_OP_const4s:
23036 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23037 i += 4;
23038 break;
23039
585861ea
JK
23040 case DW_OP_const8u:
23041 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23042 i += 8;
23043 break;
23044
21ae7a4d
JK
23045 case DW_OP_constu:
23046 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_consts:
23052 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23053 i += bytes_read;
23054 break;
23055
23056 case DW_OP_dup:
23057 stack[stacki + 1] = stack[stacki];
23058 stacki++;
23059 break;
23060
23061 case DW_OP_plus:
23062 stack[stacki - 1] += stack[stacki];
23063 stacki--;
23064 break;
23065
23066 case DW_OP_plus_uconst:
23067 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23068 &bytes_read);
23069 i += bytes_read;
23070 break;
23071
23072 case DW_OP_minus:
23073 stack[stacki - 1] -= stack[stacki];
23074 stacki--;
23075 break;
23076
23077 case DW_OP_deref:
23078 /* If we're not the last op, then we definitely can't encode
23079 this using GDB's address_class enum. This is valid for partial
23080 global symbols, although the variable's address will be bogus
23081 in the psymtab. */
23082 if (i < size)
7d79de9a
TT
23083 {
23084 if (computed == nullptr)
23085 dwarf2_complex_location_expr_complaint ();
23086 else
23087 return 0;
23088 }
21ae7a4d
JK
23089 break;
23090
23091 case DW_OP_GNU_push_tls_address:
4aa4e28b 23092 case DW_OP_form_tls_address:
21ae7a4d
JK
23093 /* The top of the stack has the offset from the beginning
23094 of the thread control block at which the variable is located. */
23095 /* Nothing should follow this operator, so the top of stack would
23096 be returned. */
23097 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23098 address will be bogus in the psymtab. Make it always at least
23099 non-zero to not look as a variable garbage collected by linker
23100 which have DW_OP_addr 0. */
21ae7a4d 23101 if (i < size)
7d79de9a
TT
23102 {
23103 if (computed == nullptr)
23104 dwarf2_complex_location_expr_complaint ();
23105 else
23106 return 0;
23107 }
585861ea 23108 stack[stacki]++;
21ae7a4d
JK
23109 break;
23110
23111 case DW_OP_GNU_uninit:
7d79de9a
TT
23112 if (computed != nullptr)
23113 return 0;
21ae7a4d
JK
23114 break;
23115
336d760d 23116 case DW_OP_addrx:
3019eac3 23117 case DW_OP_GNU_addr_index:
49f6c839 23118 case DW_OP_GNU_const_index:
3019eac3
DE
23119 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23120 &bytes_read);
23121 i += bytes_read;
23122 break;
23123
21ae7a4d 23124 default:
7d79de9a
TT
23125 if (computed == nullptr)
23126 {
23127 const char *name = get_DW_OP_name (op);
21ae7a4d 23128
7d79de9a
TT
23129 if (name)
23130 complaint (_("unsupported stack op: '%s'"),
23131 name);
23132 else
23133 complaint (_("unsupported stack op: '%02x'"),
23134 op);
23135 }
21ae7a4d
JK
23136
23137 return (stack[stacki]);
d53d4ac5 23138 }
3c6e0cb3 23139
21ae7a4d
JK
23140 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23141 outside of the allocated space. Also enforce minimum>0. */
23142 if (stacki >= ARRAY_SIZE (stack) - 1)
23143 {
7d79de9a
TT
23144 if (computed == nullptr)
23145 complaint (_("location description stack overflow"));
21ae7a4d
JK
23146 return 0;
23147 }
23148
23149 if (stacki <= 0)
23150 {
7d79de9a
TT
23151 if (computed == nullptr)
23152 complaint (_("location description stack underflow"));
21ae7a4d
JK
23153 return 0;
23154 }
23155 }
7d79de9a
TT
23156
23157 if (computed != nullptr)
23158 *computed = true;
21ae7a4d 23159 return (stack[stacki]);
c906108c
SS
23160}
23161
23162/* memory allocation interface */
23163
c906108c 23164static struct dwarf_block *
7b5a2f43 23165dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23166{
8d749320 23167 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23168}
23169
c906108c 23170static struct die_info *
b60c80d6 23171dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23172{
23173 struct die_info *die;
b60c80d6
DJ
23174 size_t size = sizeof (struct die_info);
23175
23176 if (num_attrs > 1)
23177 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23178
b60c80d6 23179 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23180 memset (die, 0, sizeof (struct die_info));
23181 return (die);
23182}
2e276125
JB
23183
23184\f
a036ba48 23185
c90ec28a 23186/* Macro support. */
cf2c3c16 23187
9eac9650
TT
23188/* An overload of dwarf_decode_macros that finds the correct section
23189 and ensures it is read in before calling the other overload. */
23190
23191static void
23192dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23193 int section_is_gnu)
23194{
5e22e966 23195 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23196 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23197 const struct line_header *lh = cu->line_header;
9eac9650
TT
23198 unsigned int offset_size = cu->header.offset_size;
23199 struct dwarf2_section_info *section;
23200 const char *section_name;
23201
23202 if (cu->dwo_unit != nullptr)
23203 {
23204 if (section_is_gnu)
23205 {
23206 section = &cu->dwo_unit->dwo_file->sections.macro;
23207 section_name = ".debug_macro.dwo";
23208 }
23209 else
23210 {
23211 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23212 section_name = ".debug_macinfo.dwo";
23213 }
23214 }
23215 else
23216 {
23217 if (section_is_gnu)
23218 {
5989a64e 23219 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23220 section_name = ".debug_macro";
23221 }
23222 else
23223 {
5989a64e 23224 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23225 section_name = ".debug_macinfo";
23226 }
23227 }
23228
23229 section->read (objfile);
23230 if (section->buffer == nullptr)
23231 {
23232 complaint (_("missing %s section"), section_name);
23233 return;
23234 }
23235
23236 buildsym_compunit *builder = cu->get_builder ();
23237
23238 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23239 offset_size, offset, section_is_gnu);
23240}
23241
3019eac3
DE
23242/* Return the .debug_loc section to use for CU.
23243 For DWO files use .debug_loc.dwo. */
23244
23245static struct dwarf2_section_info *
23246cu_debug_loc_section (struct dwarf2_cu *cu)
23247{
5e22e966 23248 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23249
3019eac3 23250 if (cu->dwo_unit)
43988095
JK
23251 {
23252 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23253
43988095
JK
23254 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23255 }
5989a64e
SM
23256 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23257 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23258}
23259
8cf6f0b1
TT
23260/* A helper function that fills in a dwarf2_loclist_baton. */
23261
23262static void
23263fill_in_loclist_baton (struct dwarf2_cu *cu,
23264 struct dwarf2_loclist_baton *baton,
ff39bb5e 23265 const struct attribute *attr)
8cf6f0b1 23266{
5e22e966 23267 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23268 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23269
96b79293 23270 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23271
a50264ba 23272 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23273 baton->per_cu = cu->per_cu;
23274 gdb_assert (baton->per_cu);
23275 /* We don't know how long the location list is, but make sure we
23276 don't run off the edge of the section. */
3019eac3
DE
23277 baton->size = section->size - DW_UNSND (attr);
23278 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23279 if (cu->base_address.has_value ())
23280 baton->base_address = *cu->base_address;
23281 else
23282 baton->base_address = 0;
f664829e 23283 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23284}
23285
4c2df51b 23286static void
ff39bb5e 23287dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23288 struct dwarf2_cu *cu, int is_block)
4c2df51b 23289{
5e22e966 23290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23291 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23292 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23293
cd6c91b4 23294 if (attr->form_is_section_offset ()
3019eac3 23295 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23296 the section. If so, fall through to the complaint in the
23297 other branch. */
2c7d5afc 23298 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23299 {
0d53c4c4 23300 struct dwarf2_loclist_baton *baton;
4c2df51b 23301
8d749320 23302 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23303
8cf6f0b1 23304 fill_in_loclist_baton (cu, baton, attr);
be391dca 23305
2b24b6e4 23306 if (!cu->base_address.has_value ())
b98664d3 23307 complaint (_("Location list used without "
3e43a32a 23308 "specifying the CU base address."));
4c2df51b 23309
f1e6e072
TT
23310 SYMBOL_ACLASS_INDEX (sym) = (is_block
23311 ? dwarf2_loclist_block_index
23312 : dwarf2_loclist_index);
0d53c4c4
DJ
23313 SYMBOL_LOCATION_BATON (sym) = baton;
23314 }
23315 else
23316 {
23317 struct dwarf2_locexpr_baton *baton;
23318
8d749320 23319 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23320 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23321 baton->per_cu = cu->per_cu;
23322 gdb_assert (baton->per_cu);
0d53c4c4 23323
4fc6c0d5 23324 if (attr->form_is_block ())
0d53c4c4
DJ
23325 {
23326 /* Note that we're just copying the block's data pointer
23327 here, not the actual data. We're still pointing into the
6502dd73
DJ
23328 info_buffer for SYM's objfile; right now we never release
23329 that buffer, but when we do clean up properly this may
23330 need to change. */
0d53c4c4
DJ
23331 baton->size = DW_BLOCK (attr)->size;
23332 baton->data = DW_BLOCK (attr)->data;
23333 }
23334 else
23335 {
23336 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23337 sym->natural_name ());
0d53c4c4 23338 baton->size = 0;
0d53c4c4 23339 }
6e70227d 23340
f1e6e072
TT
23341 SYMBOL_ACLASS_INDEX (sym) = (is_block
23342 ? dwarf2_locexpr_block_index
23343 : dwarf2_locexpr_index);
0d53c4c4
DJ
23344 SYMBOL_LOCATION_BATON (sym) = baton;
23345 }
4c2df51b 23346}
6502dd73 23347
96408a79
SA
23348/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23349 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23350 CU_HEADERP first. */
23351
23352static const struct comp_unit_head *
23353per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23354 const struct dwarf2_per_cu_data *per_cu)
96408a79 23355{
d521ce57 23356 const gdb_byte *info_ptr;
96408a79
SA
23357
23358 if (per_cu->cu)
23359 return &per_cu->cu->header;
23360
9c541725 23361 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23362
23363 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23364 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23365 rcuh_kind::COMPILE);
96408a79
SA
23366
23367 return cu_headerp;
23368}
23369
09ba997f 23370/* See read.h. */
ae0d2f24 23371
98714339 23372int
09ba997f 23373dwarf2_per_cu_data::addr_size () const
ae0d2f24 23374{
96408a79
SA
23375 struct comp_unit_head cu_header_local;
23376 const struct comp_unit_head *cu_headerp;
c471e790 23377
09ba997f 23378 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23379
23380 return cu_headerp->addr_size;
ae0d2f24
UW
23381}
23382
09ba997f 23383/* See read.h. */
9eae7c52
TT
23384
23385int
09ba997f 23386dwarf2_per_cu_data::offset_size () const
9eae7c52 23387{
96408a79
SA
23388 struct comp_unit_head cu_header_local;
23389 const struct comp_unit_head *cu_headerp;
9c6c53f7 23390
09ba997f 23391 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23392
23393 return cu_headerp->offset_size;
23394}
23395
09ba997f 23396/* See read.h. */
96408a79
SA
23397
23398int
09ba997f 23399dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23400{
23401 struct comp_unit_head cu_header_local;
23402 const struct comp_unit_head *cu_headerp;
23403
09ba997f 23404 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23405
23406 if (cu_headerp->version == 2)
23407 return cu_headerp->addr_size;
23408 else
23409 return cu_headerp->offset_size;
181cebd4
JK
23410}
23411
09ba997f 23412/* See read.h. */
9aa1f1e3 23413
09ba997f 23414struct type *
293e7e51 23415dwarf2_cu::addr_type () const
9a49df9d 23416{
293e7e51 23417 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23418 struct type *void_type = objfile_type (objfile)->builtin_void;
23419 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23420 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23421
23422 if (TYPE_LENGTH (addr_type) == addr_size)
23423 return addr_type;
23424
09ba997f 23425 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23426 return addr_type;
23427}
23428
22b6cd70
TT
23429/* A helper function for dwarf2_find_containing_comp_unit that returns
23430 the index of the result, and that searches a vector. It will
23431 return a result even if the offset in question does not actually
23432 occur in any CU. This is separate so that it can be unit
23433 tested. */
ae038cb0 23434
22b6cd70
TT
23435static int
23436dwarf2_find_containing_comp_unit
23437 (sect_offset sect_off,
23438 unsigned int offset_in_dwz,
23439 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23440{
ae038cb0
DJ
23441 int low, high;
23442
ae038cb0 23443 low = 0;
22b6cd70 23444 high = all_comp_units.size () - 1;
ae038cb0
DJ
23445 while (high > low)
23446 {
36586728 23447 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23448 int mid = low + (high - low) / 2;
9a619af0 23449
22b6cd70 23450 mid_cu = all_comp_units[mid];
36586728 23451 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23452 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23453 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23454 high = mid;
23455 else
23456 low = mid + 1;
23457 }
23458 gdb_assert (low == high);
22b6cd70
TT
23459 return low;
23460}
23461
23462/* Locate the .debug_info compilation unit from CU's objfile which contains
23463 the DIE at OFFSET. Raises an error on failure. */
23464
23465static struct dwarf2_per_cu_data *
23466dwarf2_find_containing_comp_unit (sect_offset sect_off,
23467 unsigned int offset_in_dwz,
23468 struct dwarf2_per_objfile *dwarf2_per_objfile)
23469{
23470 int low
23471 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23472 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23473 struct dwarf2_per_cu_data *this_cu
5989a64e 23474 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23475
45b8ae0c 23476 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23477 {
36586728 23478 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23479 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23480 "offset %s [in module %s]"),
23481 sect_offset_str (sect_off),
ed2dc618 23482 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23483
5989a64e 23484 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23485 <= sect_off);
5989a64e 23486 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23487 }
23488 else
23489 {
5989a64e 23490 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23491 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23492 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23493 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23494 return this_cu;
23495 }
23496}
23497
22b6cd70
TT
23498#if GDB_SELF_TEST
23499
23500namespace selftests {
23501namespace find_containing_comp_unit {
23502
23503static void
23504run_test ()
23505{
23506 struct dwarf2_per_cu_data one {};
23507 struct dwarf2_per_cu_data two {};
23508 struct dwarf2_per_cu_data three {};
23509 struct dwarf2_per_cu_data four {};
23510
23511 one.length = 5;
23512 two.sect_off = sect_offset (one.length);
23513 two.length = 7;
23514
23515 three.length = 5;
23516 three.is_dwz = 1;
23517 four.sect_off = sect_offset (three.length);
23518 four.length = 7;
23519 four.is_dwz = 1;
23520
23521 std::vector<dwarf2_per_cu_data *> units;
23522 units.push_back (&one);
23523 units.push_back (&two);
23524 units.push_back (&three);
23525 units.push_back (&four);
23526
23527 int result;
23528
23529 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23530 SELF_CHECK (units[result] == &one);
23531 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23532 SELF_CHECK (units[result] == &one);
23533 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23534 SELF_CHECK (units[result] == &two);
23535
23536 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23537 SELF_CHECK (units[result] == &three);
23538 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23539 SELF_CHECK (units[result] == &three);
23540 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23541 SELF_CHECK (units[result] == &four);
23542}
23543
23544}
23545}
23546
23547#endif /* GDB_SELF_TEST */
23548
9e021579 23549/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23550
9e021579
SM
23551dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23552 dwarf2_per_objfile *per_objfile)
23553 : per_cu (per_cu),
23554 per_objfile (per_objfile),
9068261f
AB
23555 mark (false),
23556 has_loclist (false),
23557 checked_producer (false),
23558 producer_is_gxx_lt_4_6 (false),
23559 producer_is_gcc_lt_4_3 (false),
eb77c9df 23560 producer_is_icc (false),
9068261f 23561 producer_is_icc_lt_14 (false),
c258c396 23562 producer_is_codewarrior (false),
9068261f 23563 processing_has_namespace_info (false)
93311388 23564{
fcd3b13d
SM
23565 per_cu->cu = this;
23566}
23567
23568/* Destroy a dwarf2_cu. */
23569
23570dwarf2_cu::~dwarf2_cu ()
23571{
23572 per_cu->cu = NULL;
9816fde3
JK
23573}
23574
23575/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23576
23577static void
95554aad
TT
23578prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23579 enum language pretend_language)
9816fde3
JK
23580{
23581 struct attribute *attr;
23582
23583 /* Set the language we're debugging. */
23584 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23585 if (attr != nullptr)
9816fde3
JK
23586 set_cu_language (DW_UNSND (attr), cu);
23587 else
9cded63f 23588 {
95554aad 23589 cu->language = pretend_language;
9cded63f
TT
23590 cu->language_defn = language_def (cu->language);
23591 }
dee91e82 23592
7d45c7c3 23593 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23594}
23595
ae038cb0
DJ
23596/* Increase the age counter on each cached compilation unit, and free
23597 any that are too old. */
23598
23599static void
ed2dc618 23600age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23601{
23602 struct dwarf2_per_cu_data *per_cu, **last_chain;
23603
5989a64e
SM
23604 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23605 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23606 while (per_cu != NULL)
23607 {
23608 per_cu->cu->last_used ++;
b4f54984 23609 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23610 dwarf2_mark (per_cu->cu);
23611 per_cu = per_cu->cu->read_in_chain;
23612 }
23613
5989a64e
SM
23614 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23615 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23616 while (per_cu != NULL)
23617 {
23618 struct dwarf2_per_cu_data *next_cu;
23619
23620 next_cu = per_cu->cu->read_in_chain;
23621
23622 if (!per_cu->cu->mark)
23623 {
fcd3b13d 23624 delete per_cu->cu;
ae038cb0
DJ
23625 *last_chain = next_cu;
23626 }
23627 else
23628 last_chain = &per_cu->cu->read_in_chain;
23629
23630 per_cu = next_cu;
23631 }
23632}
23633
23634/* Remove a single compilation unit from the cache. */
23635
23636static void
120ce1b5
SM
23637free_one_cached_comp_unit (dwarf2_per_cu_data *target_per_cu,
23638 dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23639{
23640 struct dwarf2_per_cu_data *per_cu, **last_chain;
23641
5989a64e
SM
23642 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23643 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
ae038cb0
DJ
23644 while (per_cu != NULL)
23645 {
23646 struct dwarf2_per_cu_data *next_cu;
23647
23648 next_cu = per_cu->cu->read_in_chain;
23649
dee91e82 23650 if (per_cu == target_per_cu)
ae038cb0 23651 {
fcd3b13d 23652 delete per_cu->cu;
dee91e82 23653 per_cu->cu = NULL;
ae038cb0
DJ
23654 *last_chain = next_cu;
23655 break;
23656 }
23657 else
23658 last_chain = &per_cu->cu->read_in_chain;
23659
23660 per_cu = next_cu;
23661 }
23662}
23663
dee91e82
DE
23664/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23665 We store these in a hash table separate from the DIEs, and preserve them
23666 when the DIEs are flushed out of cache.
23667
23668 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23669 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23670 or the type may come from a DWO file. Furthermore, while it's more logical
23671 to use per_cu->section+offset, with Fission the section with the data is in
23672 the DWO file but we don't know that section at the point we need it.
23673 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23674 because we can enter the lookup routine, get_die_type_at_offset, from
23675 outside this file, and thus won't necessarily have PER_CU->cu.
23676 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23677
dee91e82 23678struct dwarf2_per_cu_offset_and_type
1c379e20 23679{
dee91e82 23680 const struct dwarf2_per_cu_data *per_cu;
9c541725 23681 sect_offset sect_off;
1c379e20
DJ
23682 struct type *type;
23683};
23684
dee91e82 23685/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23686
23687static hashval_t
dee91e82 23688per_cu_offset_and_type_hash (const void *item)
1c379e20 23689{
9a3c8263
SM
23690 const struct dwarf2_per_cu_offset_and_type *ofs
23691 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23692
9c541725 23693 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23694}
23695
dee91e82 23696/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23697
23698static int
dee91e82 23699per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23700{
9a3c8263
SM
23701 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23702 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23703 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23704 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23705
dee91e82 23706 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23707 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23708}
23709
23710/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23711 table if necessary. For convenience, return TYPE.
23712
23713 The DIEs reading must have careful ordering to:
85102364 23714 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23715 reading current DIE.
23716 * Not trying to dereference contents of still incompletely read in types
23717 while reading in other DIEs.
23718 * Enable referencing still incompletely read in types just by a pointer to
23719 the type without accessing its fields.
23720
23721 Therefore caller should follow these rules:
23722 * Try to fetch any prerequisite types we may need to build this DIE type
23723 before building the type and calling set_die_type.
e71ec853 23724 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23725 possible before fetching more types to complete the current type.
23726 * Make the type as complete as possible before fetching more types. */
1c379e20 23727
f792889a 23728static struct type *
1c379e20
DJ
23729set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23730{
5e22e966 23731 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23732 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23733 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23734 struct attribute *attr;
23735 struct dynamic_prop prop;
1c379e20 23736
b4ba55a1
JB
23737 /* For Ada types, make sure that the gnat-specific data is always
23738 initialized (if not already set). There are a few types where
23739 we should not be doing so, because the type-specific area is
23740 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23741 where the type-specific area is used to store the floatformat).
23742 But this is not a problem, because the gnat-specific information
23743 is actually not needed for these types. */
23744 if (need_gnat_info (cu)
78134374
SM
23745 && type->code () != TYPE_CODE_FUNC
23746 && type->code () != TYPE_CODE_FLT
23747 && type->code () != TYPE_CODE_METHODPTR
23748 && type->code () != TYPE_CODE_MEMBERPTR
23749 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23750 && !HAVE_GNAT_AUX_INFO (type))
23751 INIT_GNAT_SPECIFIC (type);
23752
3f2f83dd
KB
23753 /* Read DW_AT_allocated and set in type. */
23754 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23755 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23756 {
293e7e51 23757 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23758 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23759 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23760 }
23761 else if (attr != NULL)
23762 {
b98664d3 23763 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23764 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23765 sect_offset_str (die->sect_off));
3f2f83dd
KB
23766 }
23767
23768 /* Read DW_AT_associated and set in type. */
23769 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23770 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23771 {
293e7e51 23772 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23773 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23774 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23775 }
23776 else if (attr != NULL)
23777 {
b98664d3 23778 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23779 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23780 sect_offset_str (die->sect_off));
3f2f83dd
KB
23781 }
23782
3cdcd0ce
JB
23783 /* Read DW_AT_data_location and set in type. */
23784 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23785 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23786 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23787
ae090bdb
SM
23788 if (dwarf2_per_objfile->die_type_hash == NULL)
23789 dwarf2_per_objfile->die_type_hash
0335378b
TT
23790 = htab_up (htab_create_alloc (127,
23791 per_cu_offset_and_type_hash,
23792 per_cu_offset_and_type_eq,
23793 NULL, xcalloc, xfree));
1c379e20 23794
dee91e82 23795 ofs.per_cu = cu->per_cu;
9c541725 23796 ofs.sect_off = die->sect_off;
1c379e20 23797 ofs.type = type;
dee91e82 23798 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23799 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23800 if (*slot)
b98664d3 23801 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23802 sect_offset_str (die->sect_off));
8d749320
SM
23803 *slot = XOBNEW (&objfile->objfile_obstack,
23804 struct dwarf2_per_cu_offset_and_type);
1c379e20 23805 **slot = ofs;
f792889a 23806 return type;
1c379e20
DJ
23807}
23808
9c541725 23809/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23810 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23811
23812static struct type *
9c541725 23813get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23814 dwarf2_per_cu_data *per_cu,
23815 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23816{
dee91e82 23817 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23818
ae090bdb 23819 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23820 return NULL;
1c379e20 23821
dee91e82 23822 ofs.per_cu = per_cu;
9c541725 23823 ofs.sect_off = sect_off;
9a3c8263 23824 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23825 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23826 if (slot)
23827 return slot->type;
23828 else
23829 return NULL;
23830}
23831
02142a6c 23832/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23833 or return NULL if DIE does not have a saved type. */
23834
23835static struct type *
23836get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23837{
aa66c379 23838 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23839}
23840
10b3939b
DJ
23841/* Add a dependence relationship from CU to REF_PER_CU. */
23842
23843static void
23844dwarf2_add_dependence (struct dwarf2_cu *cu,
23845 struct dwarf2_per_cu_data *ref_per_cu)
23846{
23847 void **slot;
23848
23849 if (cu->dependencies == NULL)
23850 cu->dependencies
23851 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23852 NULL, &cu->comp_unit_obstack,
23853 hashtab_obstack_allocate,
23854 dummy_obstack_deallocate);
23855
23856 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23857 if (*slot == NULL)
23858 *slot = ref_per_cu;
23859}
1c379e20 23860
f504f079
DE
23861/* Subroutine of dwarf2_mark to pass to htab_traverse.
23862 Set the mark field in every compilation unit in the
ae038cb0
DJ
23863 cache that we must keep because we are keeping CU. */
23864
10b3939b
DJ
23865static int
23866dwarf2_mark_helper (void **slot, void *data)
23867{
23868 struct dwarf2_per_cu_data *per_cu;
23869
23870 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23871
23872 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23873 reading of the chain. As such dependencies remain valid it is not much
23874 useful to track and undo them during QUIT cleanups. */
23875 if (per_cu->cu == NULL)
23876 return 1;
23877
10b3939b
DJ
23878 if (per_cu->cu->mark)
23879 return 1;
9068261f 23880 per_cu->cu->mark = true;
10b3939b
DJ
23881
23882 if (per_cu->cu->dependencies != NULL)
23883 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23884
23885 return 1;
23886}
23887
f504f079
DE
23888/* Set the mark field in CU and in every other compilation unit in the
23889 cache that we must keep because we are keeping CU. */
23890
ae038cb0
DJ
23891static void
23892dwarf2_mark (struct dwarf2_cu *cu)
23893{
23894 if (cu->mark)
23895 return;
9068261f 23896 cu->mark = true;
10b3939b
DJ
23897 if (cu->dependencies != NULL)
23898 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23899}
23900
23901static void
23902dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23903{
23904 while (per_cu)
23905 {
9068261f 23906 per_cu->cu->mark = false;
ae038cb0
DJ
23907 per_cu = per_cu->cu->read_in_chain;
23908 }
72bf9492
DJ
23909}
23910
72bf9492
DJ
23911/* Trivial hash function for partial_die_info: the hash value of a DIE
23912 is its offset in .debug_info for this objfile. */
23913
23914static hashval_t
23915partial_die_hash (const void *item)
23916{
9a3c8263
SM
23917 const struct partial_die_info *part_die
23918 = (const struct partial_die_info *) item;
9a619af0 23919
9c541725 23920 return to_underlying (part_die->sect_off);
72bf9492
DJ
23921}
23922
23923/* Trivial comparison function for partial_die_info structures: two DIEs
23924 are equal if they have the same offset. */
23925
23926static int
23927partial_die_eq (const void *item_lhs, const void *item_rhs)
23928{
9a3c8263
SM
23929 const struct partial_die_info *part_die_lhs
23930 = (const struct partial_die_info *) item_lhs;
23931 const struct partial_die_info *part_die_rhs
23932 = (const struct partial_die_info *) item_rhs;
9a619af0 23933
9c541725 23934 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23935}
23936
3c3bb058
AB
23937struct cmd_list_element *set_dwarf_cmdlist;
23938struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23939
9291a0cd 23940static void
cd4fb1b2
SM
23941show_check_physname (struct ui_file *file, int from_tty,
23942 struct cmd_list_element *c, const char *value)
9291a0cd 23943{
cd4fb1b2
SM
23944 fprintf_filtered (file,
23945 _("Whether to check \"physname\" is %s.\n"),
23946 value);
9291a0cd
TT
23947}
23948
6c265988 23949void _initialize_dwarf2_read ();
cd4fb1b2 23950void
6c265988 23951_initialize_dwarf2_read ()
9291a0cd 23952{
0743fc83 23953 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23954Set DWARF specific variables.\n\
590042fc 23955Configure DWARF variables such as the cache size."),
0743fc83
TT
23956 &set_dwarf_cmdlist, "maintenance set dwarf ",
23957 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23958
0743fc83 23959 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23960Show DWARF specific variables.\n\
23961Show DWARF variables such as the cache size."),
0743fc83
TT
23962 &show_dwarf_cmdlist, "maintenance show dwarf ",
23963 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23964
cd4fb1b2
SM
23965 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23966 &dwarf_max_cache_age, _("\
23967Set the upper bound on the age of cached DWARF compilation units."), _("\
23968Show the upper bound on the age of cached DWARF compilation units."), _("\
23969A higher limit means that cached compilation units will be stored\n\
23970in memory longer, and more total memory will be used. Zero disables\n\
23971caching, which can slow down startup."),
23972 NULL,
23973 show_dwarf_max_cache_age,
23974 &set_dwarf_cmdlist,
23975 &show_dwarf_cmdlist);
156942c7 23976
cd4fb1b2
SM
23977 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23978Set debugging of the DWARF reader."), _("\
23979Show debugging of the DWARF reader."), _("\
23980When enabled (non-zero), debugging messages are printed during DWARF\n\
23981reading and symtab expansion. A value of 1 (one) provides basic\n\
23982information. A value greater than 1 provides more verbose information."),
23983 NULL,
23984 NULL,
23985 &setdebuglist, &showdebuglist);
9291a0cd 23986
cd4fb1b2
SM
23987 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23988Set debugging of the DWARF DIE reader."), _("\
23989Show debugging of the DWARF DIE reader."), _("\
23990When enabled (non-zero), DIEs are dumped after they are read in.\n\
23991The value is the maximum depth to print."),
23992 NULL,
23993 NULL,
23994 &setdebuglist, &showdebuglist);
9291a0cd 23995
cd4fb1b2
SM
23996 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23997Set debugging of the dwarf line reader."), _("\
23998Show debugging of the dwarf line reader."), _("\
23999When enabled (non-zero), line number entries are dumped as they are read in.\n\
24000A value of 1 (one) provides basic information.\n\
24001A value greater than 1 provides more verbose information."),
24002 NULL,
24003 NULL,
24004 &setdebuglist, &showdebuglist);
437afbb8 24005
cd4fb1b2
SM
24006 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24007Set cross-checking of \"physname\" code against demangler."), _("\
24008Show cross-checking of \"physname\" code against demangler."), _("\
24009When enabled, GDB's internal \"physname\" code is checked against\n\
24010the demangler."),
24011 NULL, show_check_physname,
24012 &setdebuglist, &showdebuglist);
900e11f9 24013
e615022a
DE
24014 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24015 no_class, &use_deprecated_index_sections, _("\
24016Set whether to use deprecated gdb_index sections."), _("\
24017Show whether to use deprecated gdb_index sections."), _("\
24018When enabled, deprecated .gdb_index sections are used anyway.\n\
24019Normally they are ignored either because of a missing feature or\n\
24020performance issue.\n\
24021Warning: This option must be enabled before gdb reads the file."),
24022 NULL,
24023 NULL,
24024 &setlist, &showlist);
24025
f1e6e072
TT
24026 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24027 &dwarf2_locexpr_funcs);
24028 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24029 &dwarf2_loclist_funcs);
24030
24031 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24032 &dwarf2_block_frame_base_locexpr_funcs);
24033 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24034 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24035
24036#if GDB_SELF_TEST
24037 selftests::register_test ("dw2_expand_symtabs_matching",
24038 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24039 selftests::register_test ("dwarf2_find_containing_comp_unit",
24040 selftests::find_containing_comp_unit::run_test);
c62446b1 24041#endif
6502dd73 24042}
This page took 6.22481 seconds and 4 git commands to generate.