[gdb/symtab] Read CU base address for enqueued CU
[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
17ee85fc
TT
108/* This is used to store the data that is always per objfile. */
109static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
110
111/* These are used to store the dwarf2_per_bfd objects.
112
113 objfiles having the same BFD, which doesn't require relocations, are going to
114 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
115
116 Other objfiles are not going to share a dwarf2_per_bfd with any other
117 objfiles, so they'll have their own version kept in the _objfile_data_key
118 version. */
119static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
120static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 121
f1e6e072
TT
122/* The "aclass" indices for various kinds of computed DWARF symbols. */
123
124static int dwarf2_locexpr_index;
125static int dwarf2_loclist_index;
126static int dwarf2_locexpr_block_index;
127static int dwarf2_loclist_block_index;
128
41144253 129/* Size of .debug_loclists section header for 32-bit DWARF format. */
130#define LOCLIST_HEADER_SIZE32 12
131
132/* Size of .debug_loclists section header for 64-bit DWARF format. */
133#define LOCLIST_HEADER_SIZE64 20
134
d0ce17d8
CT
135/* Size of .debug_rnglists section header for 32-bit DWARF format. */
136#define RNGLIST_HEADER_SIZE32 12
137
138/* Size of .debug_rnglists section header for 64-bit DWARF format. */
139#define RNGLIST_HEADER_SIZE64 20
140
3f563c84
PA
141/* An index into a (C++) symbol name component in a symbol name as
142 recorded in the mapped_index's symbol table. For each C++ symbol
143 in the symbol table, we record one entry for the start of each
144 component in the symbol in a table of name components, and then
145 sort the table, in order to be able to binary search symbol names,
146 ignoring leading namespaces, both completion and regular look up.
147 For example, for symbol "A::B::C", we'll have an entry that points
148 to "A::B::C", another that points to "B::C", and another for "C".
149 Note that function symbols in GDB index have no parameter
150 information, just the function/method names. You can convert a
151 name_component to a "const char *" using the
152 'mapped_index::symbol_name_at(offset_type)' method. */
153
154struct name_component
155{
156 /* Offset in the symbol name where the component starts. Stored as
157 a (32-bit) offset instead of a pointer to save memory and improve
158 locality on 64-bit architectures. */
159 offset_type name_offset;
160
161 /* The symbol's index in the symbol and constant pool tables of a
162 mapped_index. */
163 offset_type idx;
164};
165
44ed8f3e
PA
166/* Base class containing bits shared by both .gdb_index and
167 .debug_name indexes. */
168
169struct mapped_index_base
170{
22ca247e
TT
171 mapped_index_base () = default;
172 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
173
44ed8f3e
PA
174 /* The name_component table (a sorted vector). See name_component's
175 description above. */
176 std::vector<name_component> name_components;
177
178 /* How NAME_COMPONENTS is sorted. */
179 enum case_sensitivity name_components_casing;
180
181 /* Return the number of names in the symbol table. */
182 virtual size_t symbol_name_count () const = 0;
183
184 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
185 virtual const char *symbol_name_at
186 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
187
188 /* Return whether the name at IDX in the symbol table should be
189 ignored. */
190 virtual bool symbol_name_slot_invalid (offset_type idx) const
191 {
192 return false;
193 }
194
195 /* Build the symbol name component sorted vector, if we haven't
196 yet. */
fcf23d5b 197 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
198
199 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
200 possible matches for LN_NO_PARAMS in the name component
201 vector. */
202 std::pair<std::vector<name_component>::const_iterator,
203 std::vector<name_component>::const_iterator>
3b00ef10 204 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
205 enum language lang,
206 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
207
208 /* Prevent deleting/destroying via a base class pointer. */
209protected:
210 ~mapped_index_base() = default;
211};
212
9291a0cd
TT
213/* A description of the mapped index. The file format is described in
214 a comment by the code that writes the index. */
fc898b42 215struct mapped_index final : public mapped_index_base
9291a0cd 216{
f00a2de2
PA
217 /* A slot/bucket in the symbol table hash. */
218 struct symbol_table_slot
219 {
220 const offset_type name;
221 const offset_type vec;
222 };
223
559a7a62 224 /* Index data format version. */
3063847f 225 int version = 0;
559a7a62 226
f00a2de2
PA
227 /* The address table data. */
228 gdb::array_view<const gdb_byte> address_table;
b11b1f88 229
3876f04e 230 /* The symbol table, implemented as a hash table. */
f00a2de2 231 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 232
9291a0cd 233 /* A pointer to the constant pool. */
3063847f 234 const char *constant_pool = nullptr;
3f563c84 235
44ed8f3e
PA
236 bool symbol_name_slot_invalid (offset_type idx) const override
237 {
238 const auto &bucket = this->symbol_table[idx];
9ab08412 239 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 240 }
5c58de74 241
3f563c84
PA
242 /* Convenience method to get at the name of the symbol at IDX in the
243 symbol table. */
fcf23d5b
SM
244 const char *symbol_name_at
245 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 246 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 247
44ed8f3e
PA
248 size_t symbol_name_count () const override
249 { return this->symbol_table.size (); }
9291a0cd
TT
250};
251
927aa2e7
JK
252/* A description of the mapped .debug_names.
253 Uninitialized map has CU_COUNT 0. */
fc898b42 254struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
255{
256 bfd_endian dwarf5_byte_order;
257 bool dwarf5_is_dwarf64;
258 bool augmentation_is_gdb;
259 uint8_t offset_size;
260 uint32_t cu_count = 0;
261 uint32_t tu_count, bucket_count, name_count;
262 const gdb_byte *cu_table_reordered, *tu_table_reordered;
263 const uint32_t *bucket_table_reordered, *hash_table_reordered;
264 const gdb_byte *name_table_string_offs_reordered;
265 const gdb_byte *name_table_entry_offs_reordered;
266 const gdb_byte *entry_pool;
267
268 struct index_val
269 {
270 ULONGEST dwarf_tag;
271 struct attr
272 {
273 /* Attribute name DW_IDX_*. */
274 ULONGEST dw_idx;
275
276 /* Attribute form DW_FORM_*. */
277 ULONGEST form;
278
279 /* Value if FORM is DW_FORM_implicit_const. */
280 LONGEST implicit_const;
281 };
282 std::vector<attr> attr_vec;
283 };
284
285 std::unordered_map<ULONGEST, index_val> abbrev_map;
286
fcf23d5b
SM
287 const char *namei_to_name
288 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
289
290 /* Implementation of the mapped_index_base virtual interface, for
291 the name_components cache. */
292
fcf23d5b
SM
293 const char *symbol_name_at
294 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
295 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
296
297 size_t symbol_name_count () const override
298 { return this->name_count; }
927aa2e7
JK
299};
300
cd4fb1b2 301/* See dwarf2read.h. */
ed2dc618 302
cd4fb1b2 303dwarf2_per_objfile *
ed2dc618
SM
304get_dwarf2_per_objfile (struct objfile *objfile)
305{
5bfd760d 306 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 307}
c906108c 308
251d32d9 309/* Default names of the debugging sections. */
c906108c 310
233a11ab
CS
311/* Note that if the debugging section has been compressed, it might
312 have a name like .zdebug_info. */
313
9cdd5dbd
DE
314static const struct dwarf2_debug_sections dwarf2_elf_names =
315{
251d32d9
TG
316 { ".debug_info", ".zdebug_info" },
317 { ".debug_abbrev", ".zdebug_abbrev" },
318 { ".debug_line", ".zdebug_line" },
319 { ".debug_loc", ".zdebug_loc" },
43988095 320 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 321 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 322 { ".debug_macro", ".zdebug_macro" },
251d32d9 323 { ".debug_str", ".zdebug_str" },
18a8505e 324 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 325 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 326 { ".debug_ranges", ".zdebug_ranges" },
43988095 327 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 328 { ".debug_types", ".zdebug_types" },
3019eac3 329 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
330 { ".debug_frame", ".zdebug_frame" },
331 { ".eh_frame", NULL },
24d3216f 332 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
333 { ".debug_names", ".zdebug_names" },
334 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 335 23
251d32d9 336};
c906108c 337
80626a55 338/* List of DWO/DWP sections. */
3019eac3 339
80626a55 340static const struct dwop_section_names
3019eac3
DE
341{
342 struct dwarf2_section_names abbrev_dwo;
343 struct dwarf2_section_names info_dwo;
344 struct dwarf2_section_names line_dwo;
345 struct dwarf2_section_names loc_dwo;
43988095 346 struct dwarf2_section_names loclists_dwo;
09262596
DE
347 struct dwarf2_section_names macinfo_dwo;
348 struct dwarf2_section_names macro_dwo;
d0ce17d8 349 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
350 struct dwarf2_section_names str_dwo;
351 struct dwarf2_section_names str_offsets_dwo;
352 struct dwarf2_section_names types_dwo;
80626a55
DE
353 struct dwarf2_section_names cu_index;
354 struct dwarf2_section_names tu_index;
3019eac3 355}
80626a55 356dwop_section_names =
3019eac3
DE
357{
358 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
359 { ".debug_info.dwo", ".zdebug_info.dwo" },
360 { ".debug_line.dwo", ".zdebug_line.dwo" },
361 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 362 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
363 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
364 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 365 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
366 { ".debug_str.dwo", ".zdebug_str.dwo" },
367 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
368 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
369 { ".debug_cu_index", ".zdebug_cu_index" },
370 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
371};
372
c906108c
SS
373/* local data types */
374
d0ce17d8
CT
375/* The location list and range list sections (.debug_loclists & .debug_rnglists)
376 begin with a header, which contains the following information. */
377struct loclists_rnglists_header
41144253 378{
379 /* A 4-byte or 12-byte length containing the length of the
380 set of entries for this compilation unit, not including the
381 length field itself. */
382 unsigned int length;
383
384 /* A 2-byte version identifier. */
385 short version;
386
387 /* A 1-byte unsigned integer containing the size in bytes of an address on
388 the target system. */
389 unsigned char addr_size;
390
391 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
392 on the target system. */
393 unsigned char segment_collector_size;
394
395 /* A 4-byte count of the number of offsets that follow the header. */
396 unsigned int offset_entry_count;
397};
398
3da10d80
KS
399/* Type used for delaying computation of method physnames.
400 See comments for compute_delayed_physnames. */
401struct delayed_method_info
402{
403 /* The type to which the method is attached, i.e., its parent class. */
404 struct type *type;
405
406 /* The index of the method in the type's function fieldlists. */
407 int fnfield_index;
408
409 /* The index of the method in the fieldlist. */
410 int index;
411
412 /* The name of the DIE. */
413 const char *name;
414
415 /* The DIE associated with this method. */
416 struct die_info *die;
417};
418
e7c27a73
DJ
419/* Internal state when decoding a particular compilation unit. */
420struct dwarf2_cu
421{
9e021579
SM
422 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
423 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
424
425 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
426
c24bdb02
KS
427 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
428 Create the set of symtabs used by this TU, or if this TU is sharing
429 symtabs with another TU and the symtabs have already been created
430 then restore those symtabs in the line header.
431 We don't need the pc/line-number mapping for type units. */
432 void setup_type_unit_groups (struct die_info *die);
433
434 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
435 buildsym_compunit constructor. */
436 struct compunit_symtab *start_symtab (const char *name,
437 const char *comp_dir,
438 CORE_ADDR low_pc);
439
440 /* Reset the builder. */
441 void reset_builder () { m_builder.reset (); }
442
293e7e51
SM
443 /* Return a type that is a generic pointer type, the size of which
444 matches the address size given in the compilation unit header for
445 this CU. */
446 struct type *addr_type () const;
447
448 /* Find an integer type the same size as the address size given in
449 the compilation unit header for this CU. UNSIGNED_P controls if
450 the integer is unsigned or not. */
451 struct type *addr_sized_int_type (bool unsigned_p) const;
452
d00adf39 453 /* The header of the compilation unit. */
fcd3b13d 454 struct comp_unit_head header {};
e142c38c 455
d00adf39 456 /* Base address of this compilation unit. */
2b24b6e4 457 gdb::optional<CORE_ADDR> base_address;
d00adf39 458
e142c38c 459 /* The language we are debugging. */
fcd3b13d
SM
460 enum language language = language_unknown;
461 const struct language_defn *language_defn = nullptr;
e142c38c 462
fcd3b13d 463 const char *producer = nullptr;
b0f35d58 464
c24bdb02 465private:
804d2729
TT
466 /* The symtab builder for this CU. This is only non-NULL when full
467 symbols are being read. */
c24bdb02 468 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 469
c24bdb02 470public:
e142c38c
DJ
471 /* The generic symbol table building routines have separate lists for
472 file scope symbols and all all other scopes (local scopes). So
473 we need to select the right one to pass to add_symbol_to_list().
474 We do it by keeping a pointer to the correct list in list_in_scope.
475
476 FIXME: The original dwarf code just treated the file scope as the
477 first local scope, and all other local scopes as nested local
478 scopes, and worked fine. Check to see if we really need to
479 distinguish these in buildsym.c. */
fcd3b13d 480 struct pending **list_in_scope = nullptr;
e142c38c 481
b64f50a1
JK
482 /* Hash table holding all the loaded partial DIEs
483 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 484 htab_t partial_dies = nullptr;
72bf9492
DJ
485
486 /* Storage for things with the same lifetime as this read-in compilation
487 unit, including partial DIEs. */
fcd3b13d 488 auto_obstack comp_unit_obstack;
72bf9492 489
69d751e3 490 /* Backlink to our per_cu entry. */
ae038cb0
DJ
491 struct dwarf2_per_cu_data *per_cu;
492
9e021579 493 /* The dwarf2_per_objfile that owns this. */
976ca316 494 dwarf2_per_objfile *per_objfile;
9e021579 495
ae038cb0 496 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 497 int last_used = 0;
ae038cb0 498
b64f50a1
JK
499 /* A hash table of DIE cu_offset for following references with
500 die_info->offset.sect_off as hash. */
fcd3b13d 501 htab_t die_hash = nullptr;
10b3939b
DJ
502
503 /* Full DIEs if read in. */
fcd3b13d 504 struct die_info *dies = nullptr;
10b3939b
DJ
505
506 /* A set of pointers to dwarf2_per_cu_data objects for compilation
507 units referenced by this one. Only set during full symbol processing;
508 partial symbol tables do not have dependencies. */
fcd3b13d 509 htab_t dependencies = nullptr;
10b3939b 510
cb1df416 511 /* Header data from the line table, during full symbol processing. */
fcd3b13d 512 struct line_header *line_header = nullptr;
4c8aa72d 513 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 514 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
515 this is the DW_TAG_compile_unit die for this CU. We'll hold on
516 to the line header as long as this DIE is being processed. See
517 process_die_scope. */
fcd3b13d 518 die_info *line_header_die_owner = nullptr;
cb1df416 519
3da10d80
KS
520 /* A list of methods which need to have physnames computed
521 after all type information has been read. */
c89b44cd 522 std::vector<delayed_method_info> method_list;
3da10d80 523
96408a79 524 /* To be copied to symtab->call_site_htab. */
fcd3b13d 525 htab_t call_site_htab = nullptr;
96408a79 526
034e5797
DE
527 /* Non-NULL if this CU came from a DWO file.
528 There is an invariant here that is important to remember:
529 Except for attributes copied from the top level DIE in the "main"
530 (or "stub") file in preparation for reading the DWO file
18a8505e 531 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
532 Either there isn't a DWO file (in which case this is NULL and the point
533 is moot), or there is and either we're not going to read it (in which
534 case this is NULL) or there is and we are reading it (in which case this
535 is non-NULL). */
fcd3b13d 536 struct dwo_unit *dwo_unit = nullptr;
3019eac3 537
18a8505e 538 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 539 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 540 gdb::optional<ULONGEST> addr_base;
3019eac3 541
18a8505e 542 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 543 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 544 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
545 be used without needing to know whether DWO files are in use or not.
546 N.B. This does not apply to DW_AT_ranges appearing in
547 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
548 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 549 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 550 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 551 ULONGEST ranges_base = 0;
2e3cf129 552
41144253 553 /* The DW_AT_loclists_base attribute if present. */
554 ULONGEST loclist_base = 0;
555
c9317f21
TT
556 /* When reading debug info generated by older versions of rustc, we
557 have to rewrite some union types to be struct types with a
558 variant part. This rewriting must be done after the CU is fully
559 read in, because otherwise at the point of rewriting some struct
560 type might not have been fully processed. So, we keep a list of
561 all such types here and process them after expansion. */
562 std::vector<struct type *> rust_unions;
563
18a8505e
AT
564 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
565 files, the value is implicitly zero. For DWARF 5 version DWO files, the
566 value is often implicit and is the size of the header of
567 .debug_str_offsets section (8 or 4, depending on the address size). */
568 gdb::optional<ULONGEST> str_offsets_base;
569
ae038cb0 570 /* Mark used when releasing cached dies. */
9068261f 571 bool mark : 1;
ae038cb0 572
8be455d7
JK
573 /* This CU references .debug_loc. See the symtab->locations_valid field.
574 This test is imperfect as there may exist optimized debug code not using
575 any location list and still facing inlining issues if handled as
576 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 577 bool has_loclist : 1;
ba919b58 578
9068261f 579 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
580 if all the producer_is_* fields are valid. This information is cached
581 because profiling CU expansion showed excessive time spent in
582 producer_is_gxx_lt_4_6. */
9068261f
AB
583 bool checked_producer : 1;
584 bool producer_is_gxx_lt_4_6 : 1;
585 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 586 bool producer_is_icc : 1;
9068261f 587 bool producer_is_icc_lt_14 : 1;
c258c396 588 bool producer_is_codewarrior : 1;
4d4ec4e5 589
9068261f 590 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
591 debugging info for C++ namespaces. GCC 3.3.x did not produce
592 this information, but later versions do. */
593
9068261f 594 bool processing_has_namespace_info : 1;
d590ff25
YQ
595
596 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
597
598 /* If this CU was inherited by another CU (via specification,
599 abstract_origin, etc), this is the ancestor CU. */
600 dwarf2_cu *ancestor;
601
602 /* Get the buildsym_compunit for this CU. */
603 buildsym_compunit *get_builder ()
604 {
605 /* If this CU has a builder associated with it, use that. */
606 if (m_builder != nullptr)
607 return m_builder.get ();
608
609 /* Otherwise, search ancestors for a valid builder. */
610 if (ancestor != nullptr)
611 return ancestor->get_builder ();
612
613 return nullptr;
614 }
e7c27a73
DJ
615};
616
094b34ac
DE
617/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
618 This includes type_unit_group and quick_file_names. */
619
620struct stmt_list_hash
621{
622 /* The DWO unit this table is from or NULL if there is none. */
623 struct dwo_unit *dwo_unit;
624
625 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 626 sect_offset line_sect_off;
094b34ac
DE
627};
628
5989a64e 629/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
630 an object of this type. This contains elements of type unit groups
631 that can be shared across objfiles. The non-shareable parts are in
632 type_unit_group_unshareable. */
f4dc4d17
DE
633
634struct type_unit_group
635{
0186c6a7 636 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
637 To simplify things we create an artificial CU that "includes" all the
638 type units using this stmt_list so that the rest of the code still has
197400e8 639 a "per_cu" handle on the symtab. */
094b34ac
DE
640 struct dwarf2_per_cu_data per_cu;
641
0186c6a7
DE
642 /* The TUs that share this DW_AT_stmt_list entry.
643 This is added to while parsing type units to build partial symtabs,
644 and is deleted afterwards and not used again. */
a8b3b8e9 645 std::vector<signatured_type *> *tus;
f4dc4d17 646
094b34ac
DE
647 /* The data used to construct the hash key. */
648 struct stmt_list_hash hash;
f4dc4d17
DE
649};
650
73869dc2 651/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
652
653struct dwo_sections
654{
655 struct dwarf2_section_info abbrev;
3019eac3
DE
656 struct dwarf2_section_info line;
657 struct dwarf2_section_info loc;
43988095 658 struct dwarf2_section_info loclists;
09262596
DE
659 struct dwarf2_section_info macinfo;
660 struct dwarf2_section_info macro;
d0ce17d8 661 struct dwarf2_section_info rnglists;
3019eac3
DE
662 struct dwarf2_section_info str;
663 struct dwarf2_section_info str_offsets;
80626a55
DE
664 /* In the case of a virtual DWO file, these two are unused. */
665 struct dwarf2_section_info info;
fd5866f6 666 std::vector<dwarf2_section_info> types;
3019eac3
DE
667};
668
c88ee1f0 669/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
670
671struct dwo_unit
672{
673 /* Backlink to the containing struct dwo_file. */
674 struct dwo_file *dwo_file;
675
676 /* The "id" that distinguishes this CU/TU.
677 .debug_info calls this "dwo_id", .debug_types calls this "signature".
678 Since signatures came first, we stick with it for consistency. */
679 ULONGEST signature;
680
681 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 682 struct dwarf2_section_info *section;
3019eac3 683
9c541725
PA
684 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
685 sect_offset sect_off;
3019eac3
DE
686 unsigned int length;
687
688 /* For types, offset in the type's DIE of the type defined by this TU. */
689 cu_offset type_offset_in_tu;
690};
691
73869dc2
DE
692/* include/dwarf2.h defines the DWP section codes.
693 It defines a max value but it doesn't define a min value, which we
694 use for error checking, so provide one. */
695
696enum dwp_v2_section_ids
697{
698 DW_SECT_MIN = 1
699};
700
80626a55 701/* Data for one DWO file.
57d63ce2
DE
702
703 This includes virtual DWO files (a virtual DWO file is a DWO file as it
704 appears in a DWP file). DWP files don't really have DWO files per se -
705 comdat folding of types "loses" the DWO file they came from, and from
706 a high level view DWP files appear to contain a mass of random types.
707 However, to maintain consistency with the non-DWP case we pretend DWP
708 files contain virtual DWO files, and we assign each TU with one virtual
709 DWO file (generally based on the line and abbrev section offsets -
710 a heuristic that seems to work in practice). */
3019eac3
DE
711
712struct dwo_file
713{
51ac9db5
SM
714 dwo_file () = default;
715 DISABLE_COPY_AND_ASSIGN (dwo_file);
716
18a8505e 717 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
718 For virtual DWO files the name is constructed from the section offsets
719 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
720 from related CU+TUs. */
51ac9db5 721 const char *dwo_name = nullptr;
0ac5b59e
DE
722
723 /* The DW_AT_comp_dir attribute. */
51ac9db5 724 const char *comp_dir = nullptr;
3019eac3 725
80626a55
DE
726 /* The bfd, when the file is open. Otherwise this is NULL.
727 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 728 gdb_bfd_ref_ptr dbfd;
3019eac3 729
73869dc2 730 /* The sections that make up this DWO file.
d2854d8d 731 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 732 sections (for lack of a better name). */
51ac9db5 733 struct dwo_sections sections {};
3019eac3 734
33c5cd75
DB
735 /* The CUs in the file.
736 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
737 an extension to handle LLVM's Link Time Optimization output (where
738 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 739 htab_up cus;
3019eac3
DE
740
741 /* Table of TUs in the file.
742 Each element is a struct dwo_unit. */
b0b6a987 743 htab_up tus;
3019eac3
DE
744};
745
80626a55
DE
746/* These sections are what may appear in a DWP file. */
747
748struct dwp_sections
749{
d2854d8d 750 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
751 struct dwarf2_section_info str;
752 struct dwarf2_section_info cu_index;
753 struct dwarf2_section_info tu_index;
73869dc2 754
d2854d8d 755 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
756 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
757 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
758 In DWP version 2 or 5 there is at most one copy of all these sections,
759 each section being (effectively) comprised of the concatenation of all of
760 the individual sections that exist in the version 1 format.
73869dc2
DE
761 To keep the code simple we treat each of these concatenated pieces as a
762 section itself (a virtual section?). */
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info info;
765 struct dwarf2_section_info line;
766 struct dwarf2_section_info loc;
d2854d8d 767 struct dwarf2_section_info loclists;
73869dc2
DE
768 struct dwarf2_section_info macinfo;
769 struct dwarf2_section_info macro;
d2854d8d 770 struct dwarf2_section_info rnglists;
73869dc2
DE
771 struct dwarf2_section_info str_offsets;
772 struct dwarf2_section_info types;
80626a55
DE
773};
774
73869dc2
DE
775/* These sections are what may appear in a virtual DWO file in DWP version 1.
776 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 777
73869dc2 778struct virtual_v1_dwo_sections
80626a55
DE
779{
780 struct dwarf2_section_info abbrev;
781 struct dwarf2_section_info line;
782 struct dwarf2_section_info loc;
783 struct dwarf2_section_info macinfo;
784 struct dwarf2_section_info macro;
785 struct dwarf2_section_info str_offsets;
786 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 787 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
788 struct dwarf2_section_info info_or_types;
789};
790
d2854d8d 791/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
792 In version 2, the sections of the DWO files are concatenated together
793 and stored in one section of that name. Thus each ELF section contains
794 several "virtual" sections. */
795
d2854d8d 796struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
797{
798 bfd_size_type abbrev_offset;
799 bfd_size_type abbrev_size;
800
801 bfd_size_type line_offset;
802 bfd_size_type line_size;
803
804 bfd_size_type loc_offset;
805 bfd_size_type loc_size;
806
d2854d8d
CT
807 bfd_size_type loclists_offset;
808 bfd_size_type loclists_size;
809
73869dc2
DE
810 bfd_size_type macinfo_offset;
811 bfd_size_type macinfo_size;
812
813 bfd_size_type macro_offset;
814 bfd_size_type macro_size;
815
d2854d8d
CT
816 bfd_size_type rnglists_offset;
817 bfd_size_type rnglists_size;
818
73869dc2
DE
819 bfd_size_type str_offsets_offset;
820 bfd_size_type str_offsets_size;
821
822 /* Each DWP hash table entry records one CU or one TU.
823 That is recorded here, and copied to dwo_unit.section. */
824 bfd_size_type info_or_types_offset;
825 bfd_size_type info_or_types_size;
826};
827
80626a55
DE
828/* Contents of DWP hash tables. */
829
830struct dwp_hash_table
831{
73869dc2 832 uint32_t version, nr_columns;
80626a55 833 uint32_t nr_units, nr_slots;
73869dc2
DE
834 const gdb_byte *hash_table, *unit_table;
835 union
836 {
837 struct
838 {
839 const gdb_byte *indices;
840 } v1;
841 struct
842 {
843 /* This is indexed by column number and gives the id of the section
844 in that column. */
845#define MAX_NR_V2_DWO_SECTIONS \
846 (1 /* .debug_info or .debug_types */ \
847 + 1 /* .debug_abbrev */ \
848 + 1 /* .debug_line */ \
849 + 1 /* .debug_loc */ \
850 + 1 /* .debug_str_offsets */ \
851 + 1 /* .debug_macro or .debug_macinfo */)
852 int section_ids[MAX_NR_V2_DWO_SECTIONS];
853 const gdb_byte *offsets;
854 const gdb_byte *sizes;
855 } v2;
d2854d8d
CT
856 struct
857 {
858 /* This is indexed by column number and gives the id of the section
859 in that column. */
860#define MAX_NR_V5_DWO_SECTIONS \
861 (1 /* .debug_info */ \
862 + 1 /* .debug_abbrev */ \
863 + 1 /* .debug_line */ \
864 + 1 /* .debug_loclists */ \
865 + 1 /* .debug_str_offsets */ \
866 + 1 /* .debug_macro */ \
867 + 1 /* .debug_rnglists */)
868 int section_ids[MAX_NR_V5_DWO_SECTIONS];
869 const gdb_byte *offsets;
870 const gdb_byte *sizes;
871 } v5;
73869dc2 872 } section_pool;
80626a55
DE
873};
874
875/* Data for one DWP file. */
876
877struct dwp_file
878{
400174b1
TT
879 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
880 : name (name_),
881 dbfd (std::move (abfd))
882 {
883 }
884
80626a55
DE
885 /* Name of the file. */
886 const char *name;
887
73869dc2 888 /* File format version. */
400174b1 889 int version = 0;
73869dc2 890
93417882 891 /* The bfd. */
400174b1 892 gdb_bfd_ref_ptr dbfd;
80626a55
DE
893
894 /* Section info for this file. */
400174b1 895 struct dwp_sections sections {};
80626a55 896
57d63ce2 897 /* Table of CUs in the file. */
400174b1 898 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
899
900 /* Table of TUs in the file. */
400174b1 901 const struct dwp_hash_table *tus = nullptr;
80626a55 902
19ac8c2e 903 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
904 htab_up loaded_cus;
905 htab_up loaded_tus;
80626a55 906
73869dc2
DE
907 /* Table to map ELF section numbers to their sections.
908 This is only needed for the DWP V1 file format. */
400174b1
TT
909 unsigned int num_sections = 0;
910 asection **elf_sections = nullptr;
80626a55
DE
911};
912
0963b4bd
MS
913/* Struct used to pass misc. parameters to read_die_and_children, et
914 al. which are used for both .debug_info and .debug_types dies.
915 All parameters here are unchanging for the life of the call. This
dee91e82 916 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
917
918struct die_reader_specs
919{
a32a8923 920 /* The bfd of die_section. */
93311388
DE
921 bfd* abfd;
922
923 /* The CU of the DIE we are parsing. */
924 struct dwarf2_cu *cu;
925
80626a55 926 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
927 struct dwo_file *dwo_file;
928
dee91e82 929 /* The section the die comes from.
3019eac3 930 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
931 struct dwarf2_section_info *die_section;
932
933 /* die_section->buffer. */
d521ce57 934 const gdb_byte *buffer;
f664829e
DE
935
936 /* The end of the buffer. */
937 const gdb_byte *buffer_end;
a2ce51a0 938
685af9cd
TT
939 /* The abbreviation table to use when reading the DIEs. */
940 struct abbrev_table *abbrev_table;
93311388
DE
941};
942
c0ab21c2
TT
943/* A subclass of die_reader_specs that holds storage and has complex
944 constructor and destructor behavior. */
945
946class cutu_reader : public die_reader_specs
947{
948public:
949
ab432490
SM
950 cutu_reader (dwarf2_per_cu_data *this_cu,
951 dwarf2_per_objfile *per_objfile,
c0ab21c2 952 struct abbrev_table *abbrev_table,
2e671100 953 dwarf2_cu *existing_cu,
c0ab21c2
TT
954 bool skip_partial);
955
956 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 957 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
958 struct dwarf2_cu *parent_cu = nullptr,
959 struct dwo_file *dwo_file = nullptr);
960
c0ab21c2
TT
961 DISABLE_COPY_AND_ASSIGN (cutu_reader);
962
963 const gdb_byte *info_ptr = nullptr;
964 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
965 bool dummy_p = false;
966
6751ebae
TT
967 /* Release the new CU, putting it on the chain. This cannot be done
968 for dummy CUs. */
969 void keep ();
970
c0ab21c2 971private:
9e021579
SM
972 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
973 dwarf2_per_objfile *per_objfile,
2e671100 974 dwarf2_cu *existing_cu);
c0ab21c2
TT
975
976 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
977 std::unique_ptr<dwarf2_cu> m_new_cu;
978
979 /* The ordinary abbreviation table. */
980 abbrev_table_up m_abbrev_table_holder;
981
982 /* The DWO abbreviation table. */
983 abbrev_table_up m_dwo_abbrev_table;
984};
dee91e82 985
c906108c 986/* When we construct a partial symbol table entry we only
0963b4bd 987 need this much information. */
6f06d47b 988struct partial_die_info : public allocate_on_obstack
c906108c 989 {
6f06d47b
YQ
990 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
991
992 /* Disable assign but still keep copy ctor, which is needed
993 load_partial_dies. */
994 partial_die_info& operator=(const partial_die_info& rhs) = delete;
995
52356b79
YQ
996 /* Adjust the partial die before generating a symbol for it. This
997 function may set the is_external flag or change the DIE's
998 name. */
999 void fixup (struct dwarf2_cu *cu);
1000
48fbe735
YQ
1001 /* Read a minimal amount of information into the minimal die
1002 structure. */
1003 const gdb_byte *read (const struct die_reader_specs *reader,
1004 const struct abbrev_info &abbrev,
1005 const gdb_byte *info_ptr);
1006
7d00ffec
TT
1007 /* Compute the name of this partial DIE. This memoizes the
1008 result, so it is safe to call multiple times. */
1009 const char *name (dwarf2_cu *cu);
1010
72bf9492 1011 /* Offset of this DIE. */
6f06d47b 1012 const sect_offset sect_off;
72bf9492
DJ
1013
1014 /* DWARF-2 tag for this DIE. */
6f06d47b 1015 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1016
72bf9492 1017 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1018 const unsigned int has_children : 1;
1019
72bf9492
DJ
1020 unsigned int is_external : 1;
1021 unsigned int is_declaration : 1;
1022 unsigned int has_type : 1;
1023 unsigned int has_specification : 1;
1024 unsigned int has_pc_info : 1;
481860b3 1025 unsigned int may_be_inlined : 1;
72bf9492 1026
0c1b455e
TT
1027 /* This DIE has been marked DW_AT_main_subprogram. */
1028 unsigned int main_subprogram : 1;
1029
72bf9492
DJ
1030 /* Flag set if the SCOPE field of this structure has been
1031 computed. */
1032 unsigned int scope_set : 1;
1033
fa4028e9
JB
1034 /* Flag set if the DIE has a byte_size attribute. */
1035 unsigned int has_byte_size : 1;
1036
ff908ebf
AW
1037 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1038 unsigned int has_const_value : 1;
1039
98bfdba5
PA
1040 /* Flag set if any of the DIE's children are template arguments. */
1041 unsigned int has_template_arguments : 1;
1042
52356b79 1043 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1044 unsigned int fixup_called : 1;
1045
36586728
TT
1046 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1047 unsigned int is_dwz : 1;
1048
1049 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1050 unsigned int spec_is_dwz : 1;
1051
7d00ffec
TT
1052 unsigned int canonical_name : 1;
1053
72bf9492 1054 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1055 sometimes a default name for unnamed DIEs. */
7d00ffec 1056 const char *raw_name = nullptr;
72bf9492 1057
abc72ce4 1058 /* The linkage name, if present. */
6f06d47b 1059 const char *linkage_name = nullptr;
abc72ce4 1060
72bf9492
DJ
1061 /* The scope to prepend to our children. This is generally
1062 allocated on the comp_unit_obstack, so will disappear
1063 when this compilation unit leaves the cache. */
6f06d47b 1064 const char *scope = nullptr;
72bf9492 1065
95554aad
TT
1066 /* Some data associated with the partial DIE. The tag determines
1067 which field is live. */
1068 union
1069 {
1070 /* The location description associated with this DIE, if any. */
1071 struct dwarf_block *locdesc;
1072 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1073 sect_offset sect_off;
6f06d47b 1074 } d {};
72bf9492
DJ
1075
1076 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1077 CORE_ADDR lowpc = 0;
1078 CORE_ADDR highpc = 0;
72bf9492 1079
93311388 1080 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1081 DW_AT_sibling, if any. */
48fbe735
YQ
1082 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1083 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1084 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1085
1086 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1087 DW_AT_specification (or DW_AT_abstract_origin or
1088 DW_AT_extension). */
6f06d47b 1089 sect_offset spec_offset {};
72bf9492
DJ
1090
1091 /* Pointers to this DIE's parent, first child, and next sibling,
1092 if any. */
6f06d47b
YQ
1093 struct partial_die_info *die_parent = nullptr;
1094 struct partial_die_info *die_child = nullptr;
1095 struct partial_die_info *die_sibling = nullptr;
1096
1097 friend struct partial_die_info *
1098 dwarf2_cu::find_partial_die (sect_offset sect_off);
1099
1100 private:
1101 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1102 partial_die_info (sect_offset sect_off)
1103 : partial_die_info (sect_off, DW_TAG_padding, 0)
1104 {
1105 }
1106
1107 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1108 int has_children_)
1109 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1110 {
1111 is_external = 0;
1112 is_declaration = 0;
1113 has_type = 0;
1114 has_specification = 0;
1115 has_pc_info = 0;
1116 may_be_inlined = 0;
1117 main_subprogram = 0;
1118 scope_set = 0;
1119 has_byte_size = 0;
1120 has_const_value = 0;
1121 has_template_arguments = 0;
1122 fixup_called = 0;
1123 is_dwz = 0;
1124 spec_is_dwz = 0;
7d00ffec 1125 canonical_name = 0;
6f06d47b 1126 }
c906108c
SS
1127 };
1128
c906108c
SS
1129/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1130 but this would require a corresponding change in unpack_field_as_long
1131 and friends. */
1132static int bits_per_byte = 8;
1133
9c6a1327
TT
1134struct variant_part_builder;
1135
1136/* When reading a variant, we track a bit more information about the
1137 field, and store it in an object of this type. */
2ddeaf8a
TT
1138
1139struct variant_field
1140{
9c6a1327
TT
1141 int first_field = -1;
1142 int last_field = -1;
1143
1144 /* A variant can contain other variant parts. */
1145 std::vector<variant_part_builder> variant_parts;
1146
2ddeaf8a
TT
1147 /* If we see a DW_TAG_variant, then this will be set if this is the
1148 default branch. */
9c6a1327
TT
1149 bool default_branch = false;
1150 /* If we see a DW_AT_discr_value, then this will be the discriminant
1151 value. */
1152 ULONGEST discriminant_value = 0;
1153 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1154 data. */
1155 struct dwarf_block *discr_list_data = nullptr;
1156};
1157
1158/* This represents a DW_TAG_variant_part. */
1159
1160struct variant_part_builder
1161{
1162 /* The offset of the discriminant field. */
1163 sect_offset discriminant_offset {};
1164
1165 /* Variants that are direct children of this variant part. */
1166 std::vector<variant_field> variants;
1167
1168 /* True if we're currently reading a variant. */
1169 bool processing_variant = false;
2ddeaf8a
TT
1170};
1171
52059ffd
TT
1172struct nextfield
1173{
be2daae6
TT
1174 int accessibility = 0;
1175 int virtuality = 0;
9c6a1327
TT
1176 /* Variant parts need to find the discriminant, which is a DIE
1177 reference. We track the section offset of each field to make
1178 this link. */
1179 sect_offset offset;
be2daae6 1180 struct field field {};
52059ffd
TT
1181};
1182
1183struct fnfieldlist
1184{
be2daae6
TT
1185 const char *name = nullptr;
1186 std::vector<struct fn_field> fnfields;
52059ffd
TT
1187};
1188
c906108c
SS
1189/* The routines that read and process dies for a C struct or C++ class
1190 pass lists of data member fields and lists of member function fields
1191 in an instance of a field_info structure, as defined below. */
1192struct field_info
2de01bdb
SM
1193{
1194 /* List of data member and baseclasses fields. */
1195 std::vector<struct nextfield> fields;
1196 std::vector<struct nextfield> baseclasses;
1197
1198 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1199 bool non_public_fields = false;
2de01bdb
SM
1200
1201 /* Member function fieldlist array, contains name of possibly overloaded
1202 member function, number of overloaded member functions and a pointer
1203 to the head of the member function field chain. */
1204 std::vector<struct fnfieldlist> fnfieldlists;
1205
1206 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1207 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1208 std::vector<struct decl_field> typedef_field_list;
1209
1210 /* Nested types defined by this class and the number of elements in this
1211 list. */
1212 std::vector<struct decl_field> nested_types_list;
1213
1214 /* If non-null, this is the variant part we are currently
1215 reading. */
1216 variant_part_builder *current_variant_part = nullptr;
1217 /* This holds all the top-level variant parts attached to the type
1218 we're reading. */
1219 std::vector<variant_part_builder> variant_parts;
1220
1221 /* Return the total number of fields (including baseclasses). */
1222 int nfields () const
c5aa993b 1223 {
2de01bdb
SM
1224 return fields.size () + baseclasses.size ();
1225 }
1226};
c906108c 1227
ae038cb0
DJ
1228/* Loaded secondary compilation units are kept in memory until they
1229 have not been referenced for the processing of this many
1230 compilation units. Set this to zero to disable caching. Cache
1231 sizes of up to at least twenty will improve startup time for
1232 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1233static int dwarf_max_cache_age = 5;
920d2a44 1234static void
b4f54984
DE
1235show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1236 struct cmd_list_element *c, const char *value)
920d2a44 1237{
3e43a32a 1238 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1239 "DWARF compilation units is %s.\n"),
920d2a44
AC
1240 value);
1241}
4390d890 1242\f
c906108c
SS
1243/* local function prototypes */
1244
918dd910
JK
1245static void dwarf2_find_base_address (struct die_info *die,
1246 struct dwarf2_cu *cu);
1247
891813be 1248static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1249 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1250 const char *name);
0018ea6f 1251
f1902523
JK
1252static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1253 const gdb_byte *info_ptr,
3e225074 1254 struct die_info *type_unit_die);
f1902523 1255
976ca316 1256static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1257
72bf9492
DJ
1258static void scan_partial_symbols (struct partial_die_info *,
1259 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1260 int, struct dwarf2_cu *);
c906108c 1261
72bf9492
DJ
1262static void add_partial_symbol (struct partial_die_info *,
1263 struct dwarf2_cu *);
63d06c5c 1264
72bf9492
DJ
1265static void add_partial_namespace (struct partial_die_info *pdi,
1266 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1267 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1268
5d7cb8df 1269static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1270 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1271 struct dwarf2_cu *cu);
1272
72bf9492
DJ
1273static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1274 struct dwarf2_cu *cu);
91c24f0a 1275
bc30ff58
JB
1276static void add_partial_subprogram (struct partial_die_info *pdi,
1277 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1278 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1279
d521ce57 1280static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1281
dee91e82 1282static struct partial_die_info *load_partial_dies
d521ce57 1283 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1284
fb816e8b
TV
1285/* A pair of partial_die_info and compilation unit. */
1286struct cu_partial_die_info
1287{
1288 /* The compilation unit of the partial_die_info. */
1289 struct dwarf2_cu *cu;
1290 /* A partial_die_info. */
1291 struct partial_die_info *pdi;
122cf0f2
AB
1292
1293 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1294 : cu (cu),
1295 pdi (pdi)
405feb71 1296 { /* Nothing. */ }
122cf0f2
AB
1297
1298private:
1299 cu_partial_die_info () = delete;
fb816e8b
TV
1300};
1301
122cf0f2
AB
1302static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1303 struct dwarf2_cu *);
72bf9492 1304
d521ce57
TT
1305static const gdb_byte *read_attribute (const struct die_reader_specs *,
1306 struct attribute *, struct attr_abbrev *,
7a5f294d 1307 const gdb_byte *);
18a8505e
AT
1308
1309static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1310 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1311
1312static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1313
976ca316
SM
1314static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1315 dwarf2_section_info *, sect_offset);
f4dc4d17 1316
ed2dc618 1317static const char *read_indirect_string
976ca316 1318 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1319 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1320
ed2dc618 1321static const char *read_indirect_string_at_offset
976ca316 1322 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1323
d521ce57
TT
1324static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1325 const gdb_byte *,
3019eac3
DE
1326 unsigned int *);
1327
18a8505e
AT
1328static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1329 ULONGEST str_index);
1330
1331static const char *read_stub_str_index (struct dwarf2_cu *cu,
1332 ULONGEST str_index);
3019eac3 1333
e142c38c 1334static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1335
e142c38c
DJ
1336static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1337 struct dwarf2_cu *);
c906108c 1338
7d45c7c3
KB
1339static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1340 struct dwarf2_cu *cu);
1341
a084a2a6
AT
1342static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1343
05cf31d1
JB
1344static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1345 struct dwarf2_cu *cu);
1346
e142c38c 1347static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1348
e142c38c 1349static struct die_info *die_specification (struct die_info *die,
f2f0e013 1350 struct dwarf2_cu **);
63d06c5c 1351
9c541725 1352static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1353 struct dwarf2_cu *cu);
debd256d 1354
f3f5162e 1355static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1356 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1357 CORE_ADDR, int decode_mapping);
c906108c 1358
804d2729
TT
1359static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1360 const char *);
c906108c 1361
a14ed312 1362static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1363 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1364
ff39bb5e 1365static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1366 struct dwarf2_cu *);
c906108c 1367
ff39bb5e 1368static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1369 struct type *type,
1370 const char *name,
1371 struct obstack *obstack,
12df843f 1372 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1373 const gdb_byte **bytes,
98bfdba5 1374 struct dwarf2_locexpr_baton **baton);
2df3850c 1375
e7c27a73 1376static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1377
b4ba55a1
JB
1378static int need_gnat_info (struct dwarf2_cu *);
1379
3e43a32a
MS
1380static struct type *die_descriptive_type (struct die_info *,
1381 struct dwarf2_cu *);
b4ba55a1
JB
1382
1383static void set_descriptive_type (struct type *, struct die_info *,
1384 struct dwarf2_cu *);
1385
e7c27a73
DJ
1386static struct type *die_containing_type (struct die_info *,
1387 struct dwarf2_cu *);
c906108c 1388
ff39bb5e 1389static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1390 struct dwarf2_cu *);
c906108c 1391
f792889a 1392static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1393
673bfd45
DE
1394static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1395
0d5cff50 1396static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1397
6e70227d 1398static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1399 const char *suffix, int physname,
1400 struct dwarf2_cu *cu);
63d06c5c 1401
e7c27a73 1402static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1403
348e048f
DE
1404static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1405
e7c27a73 1406static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1407
e7c27a73 1408static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1409
96408a79
SA
1410static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1411
71a3c369
TT
1412static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1413
41144253 1414/* Return the .debug_loclists section to use for cu. */
1415static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1416
d0ce17d8
CT
1417/* Return the .debug_rnglists section to use for cu. */
1418static struct dwarf2_section_info *cu_debug_rnglists_section
1419 (struct dwarf2_cu *cu, dwarf_tag tag);
1420
3a2b436a 1421/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1422 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1423enum pc_bounds_kind
1424{
e385593e 1425 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1426 PC_BOUNDS_NOT_PRESENT,
1427
e385593e
JK
1428 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1429 were present but they do not form a valid range of PC addresses. */
1430 PC_BOUNDS_INVALID,
1431
3a2b436a
JK
1432 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1433 PC_BOUNDS_RANGES,
1434
1435 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1436 PC_BOUNDS_HIGH_LOW,
1437};
1438
1439static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1440 CORE_ADDR *, CORE_ADDR *,
1441 struct dwarf2_cu *,
891813be 1442 dwarf2_psymtab *);
c906108c 1443
fae299cd
DC
1444static void get_scope_pc_bounds (struct die_info *,
1445 CORE_ADDR *, CORE_ADDR *,
1446 struct dwarf2_cu *);
1447
801e3a5b
JB
1448static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1449 CORE_ADDR, struct dwarf2_cu *);
1450
a14ed312 1451static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1452 struct dwarf2_cu *);
c906108c 1453
a14ed312 1454static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1455 struct type *, struct dwarf2_cu *);
c906108c 1456
a14ed312 1457static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1458 struct die_info *, struct type *,
e7c27a73 1459 struct dwarf2_cu *);
c906108c 1460
a14ed312 1461static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1462 struct type *,
1463 struct dwarf2_cu *);
c906108c 1464
134d01f1 1465static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1466
e7c27a73 1467static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1468
e7c27a73 1469static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1470
5d7cb8df
JK
1471static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1472
804d2729 1473static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1474
27aa8d6a
SW
1475static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1476
74921315
KS
1477static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1478
f55ee35c
JK
1479static struct type *read_module_type (struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
38d518c9 1482static const char *namespace_name (struct die_info *die,
e142c38c 1483 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1484
134d01f1 1485static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1486
7d79de9a
TT
1487static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1488 bool * = nullptr);
c906108c 1489
6e70227d 1490static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1491 struct dwarf2_cu *);
1492
bf6af496 1493static struct die_info *read_die_and_siblings_1
d521ce57 1494 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1495 struct die_info *);
639d11d3 1496
dee91e82 1497static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1498 const gdb_byte *info_ptr,
1499 const gdb_byte **new_info_ptr,
639d11d3
DC
1500 struct die_info *parent);
1501
d521ce57
TT
1502static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1503 struct die_info **, const gdb_byte *,
3e225074 1504 int);
3019eac3 1505
d521ce57 1506static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1507 struct die_info **, const gdb_byte *);
93311388 1508
e7c27a73 1509static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1510
15d034d0 1511static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1512 struct objfile *);
71c25dea 1513
15d034d0 1514static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1515
15d034d0 1516static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1517 struct die_info *die,
1518 struct dwarf2_cu *cu);
1519
ca69b9e6
DE
1520static const char *dwarf2_physname (const char *name, struct die_info *die,
1521 struct dwarf2_cu *cu);
1522
e142c38c 1523static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1524 struct dwarf2_cu **);
9219021c 1525
d97bc12b
DE
1526static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1527
1528static void dump_die_for_error (struct die_info *);
1529
1530static void dump_die_1 (struct ui_file *, int level, int max_level,
1531 struct die_info *);
c906108c 1532
d97bc12b 1533/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1534
51545339 1535static void store_in_ref_table (struct die_info *,
10b3939b 1536 struct dwarf2_cu *);
c906108c 1537
348e048f 1538static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1539 const struct attribute *,
348e048f
DE
1540 struct dwarf2_cu **);
1541
10b3939b 1542static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1543 const struct attribute *,
f2f0e013 1544 struct dwarf2_cu **);
c906108c 1545
348e048f 1546static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1547 const struct attribute *,
348e048f
DE
1548 struct dwarf2_cu **);
1549
ac9ec31b
DE
1550static struct type *get_signatured_type (struct die_info *, ULONGEST,
1551 struct dwarf2_cu *);
1552
1553static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1554 const struct attribute *,
ac9ec31b
DE
1555 struct dwarf2_cu *);
1556
ab432490
SM
1557static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1558 dwarf2_per_objfile *per_objfile);
348e048f 1559
ab432490
SM
1560static void read_signatured_type (signatured_type *sig_type,
1561 dwarf2_per_objfile *per_objfile);
348e048f 1562
63e43d3a
PMR
1563static int attr_to_dynamic_prop (const struct attribute *attr,
1564 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1565 struct dynamic_prop *prop, struct type *type);
63e43d3a 1566
c906108c
SS
1567/* memory allocation interface */
1568
7b5a2f43 1569static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1570
b60c80d6 1571static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1572
43f3e411 1573static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1574
8cf6f0b1
TT
1575static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1576 struct dwarf2_loclist_baton *baton,
ff39bb5e 1577 const struct attribute *attr);
8cf6f0b1 1578
ff39bb5e 1579static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1580 struct symbol *sym,
f1e6e072
TT
1581 struct dwarf2_cu *cu,
1582 int is_block);
4c2df51b 1583
d521ce57
TT
1584static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1585 const gdb_byte *info_ptr,
1586 struct abbrev_info *abbrev);
4bb7a0a7 1587
72bf9492
DJ
1588static hashval_t partial_die_hash (const void *item);
1589
1590static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1591
ae038cb0 1592static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1593 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1594 dwarf2_per_objfile *per_objfile);
ae038cb0 1595
9816fde3 1596static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1597 struct die_info *comp_unit_die,
1598 enum language pretend_language);
93311388 1599
f792889a
DJ
1600static struct type *set_die_type (struct die_info *, struct type *,
1601 struct dwarf2_cu *);
1c379e20 1602
976ca316 1603static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1604
976ca316 1605static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1606
ab432490
SM
1607static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1608 dwarf2_per_objfile *per_objfile,
4a636814 1609 dwarf2_cu *existing_cu,
ab432490
SM
1610 bool skip_partial,
1611 enum language pretend_language);
10b3939b 1612
8fc0b21d 1613static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1614 enum language pretend_language);
10b3939b 1615
8fc0b21d 1616static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1617 enum language pretend_language);
f4dc4d17 1618
10b3939b
DJ
1619static void dwarf2_add_dependence (struct dwarf2_cu *,
1620 struct dwarf2_per_cu_data *);
1621
ae038cb0
DJ
1622static void dwarf2_mark (struct dwarf2_cu *);
1623
b64f50a1 1624static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1625 dwarf2_per_cu_data *per_cu,
1626 dwarf2_per_objfile *per_objfile);
673bfd45 1627
f792889a 1628static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1629
120ce1b5
SM
1630static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1631 dwarf2_per_objfile *per_objfile,
95554aad
TT
1632 enum language pretend_language);
1633
976ca316 1634static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1635
b303c6f6
AB
1636/* Class, the destructor of which frees all allocated queue entries. This
1637 will only have work to do if an error was thrown while processing the
1638 dwarf. If no error was thrown then the queue entries should have all
1639 been processed, and freed, as we went along. */
1640
1641class dwarf2_queue_guard
1642{
1643public:
39856def
TT
1644 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1645 : m_per_objfile (per_objfile)
1646 {
1647 }
b303c6f6
AB
1648
1649 /* Free any entries remaining on the queue. There should only be
1650 entries left if we hit an error while processing the dwarf. */
1651 ~dwarf2_queue_guard ()
1652 {
39856def
TT
1653 /* Ensure that no memory is allocated by the queue. */
1654 std::queue<dwarf2_queue_item> empty;
5989a64e 1655 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1656 }
b303c6f6 1657
39856def 1658 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1659
39856def
TT
1660private:
1661 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1662};
1663
39856def
TT
1664dwarf2_queue_item::~dwarf2_queue_item ()
1665{
1666 /* Anything still marked queued is likely to be in an
1667 inconsistent state, so discard it. */
1668 if (per_cu->queued)
1669 {
7188ed02 1670 per_objfile->remove_cu (per_cu);
39856def
TT
1671 per_cu->queued = 0;
1672 }
1673}
1674
d721ba37
PA
1675/* The return type of find_file_and_directory. Note, the enclosed
1676 string pointers are only valid while this object is valid. */
1677
1678struct file_and_directory
1679{
1680 /* The filename. This is never NULL. */
1681 const char *name;
1682
1683 /* The compilation directory. NULL if not known. If we needed to
1684 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1685 points directly to the DW_AT_comp_dir string attribute owned by
1686 the obstack that owns the DIE. */
1687 const char *comp_dir;
1688
1689 /* If we needed to build a new string for comp_dir, this is what
1690 owns the storage. */
1691 std::string comp_dir_storage;
1692};
1693
1694static file_and_directory find_file_and_directory (struct die_info *die,
1695 struct dwarf2_cu *cu);
9291a0cd 1696
298e9637 1697static htab_up allocate_signatured_type_table ();
1fd400ff 1698
298e9637 1699static htab_up allocate_dwo_unit_table ();
3019eac3 1700
57d63ce2 1701static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1702 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1703 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1704
976ca316 1705static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1706
3019eac3 1707static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1708 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1709 ULONGEST signature);
3019eac3
DE
1710
1711static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1712 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1713
1b555f17 1714static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1715
263db9a1
TT
1716/* A unique pointer to a dwo_file. */
1717
51ac9db5 1718typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1719
976ca316 1720static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1721
1b80a9fa 1722static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1723
1724static void free_line_header_voidp (void *arg);
4390d890
DE
1725\f
1726/* Various complaints about symbol reading that don't abort the process. */
1727
4390d890
DE
1728static void
1729dwarf2_debug_line_missing_file_complaint (void)
1730{
b98664d3 1731 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1732}
1733
1734static void
1735dwarf2_debug_line_missing_end_sequence_complaint (void)
1736{
b98664d3 1737 complaint (_(".debug_line section has line "
4390d890
DE
1738 "program sequence without an end"));
1739}
1740
1741static void
1742dwarf2_complex_location_expr_complaint (void)
1743{
b98664d3 1744 complaint (_("location expression too complex"));
4390d890
DE
1745}
1746
1747static void
1748dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1749 int arg3)
1750{
b98664d3 1751 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1752 arg1, arg2, arg3);
1753}
1754
4390d890
DE
1755static void
1756dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1757{
b98664d3 1758 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1759 arg1, arg2);
1760}
527f3840
JK
1761
1762/* Hash function for line_header_hash. */
1763
1764static hashval_t
1765line_header_hash (const struct line_header *ofs)
1766{
9c541725 1767 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1768}
1769
1770/* Hash function for htab_create_alloc_ex for line_header_hash. */
1771
1772static hashval_t
1773line_header_hash_voidp (const void *item)
1774{
9a3c8263 1775 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1776
1777 return line_header_hash (ofs);
1778}
1779
1780/* Equality function for line_header_hash. */
1781
1782static int
1783line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1784{
9a3c8263
SM
1785 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1786 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1787
9c541725 1788 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1789 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1790}
1791
4390d890 1792\f
9291a0cd 1793
330cdd98
PA
1794/* See declaration. */
1795
5989a64e
SM
1796dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1797 bool can_copy_)
c3699833
SM
1798 : obfd (obfd),
1799 can_copy (can_copy_)
330cdd98
PA
1800{
1801 if (names == NULL)
1802 names = &dwarf2_elf_names;
1803
330cdd98
PA
1804 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1805 locate_sections (obfd, sec, *names);
1806}
1807
5989a64e 1808dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1809{
b76e467d 1810 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1811 per_cu->imported_symtabs_free ();
fc8e7e75 1812
b2bdb8cf 1813 for (signatured_type *sig_type : all_type_units)
ae640021 1814 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1815
5989a64e 1816 /* Everything else should be on this->obstack. */
330cdd98
PA
1817}
1818
7188ed02 1819/* See read.h. */
330cdd98
PA
1820
1821void
7188ed02 1822dwarf2_per_objfile::remove_all_cus ()
330cdd98 1823{
7188ed02
SM
1824 for (auto pair : m_dwarf2_cus)
1825 delete pair.second;
330cdd98 1826
7188ed02 1827 m_dwarf2_cus.clear ();
330cdd98
PA
1828}
1829
11ed8cad
TT
1830/* A helper class that calls free_cached_comp_units on
1831 destruction. */
1832
1833class free_cached_comp_units
1834{
1835public:
1836
1837 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1838 : m_per_objfile (per_objfile)
1839 {
1840 }
1841
1842 ~free_cached_comp_units ()
1843 {
7188ed02 1844 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1845 }
1846
1847 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1848
1849private:
1850
1851 dwarf2_per_objfile *m_per_objfile;
1852};
1853
af758d11
SM
1854/* See read.h. */
1855
1856bool
1857dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1858{
1859 gdb_assert (per_cu->index < this->m_symtabs.size ());
1860
1861 return this->m_symtabs[per_cu->index] != nullptr;
1862}
1863
1864/* See read.h. */
1865
1866compunit_symtab *
1867dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1868{
1869 gdb_assert (per_cu->index < this->m_symtabs.size ());
1870
1871 return this->m_symtabs[per_cu->index];
1872}
1873
1874/* See read.h. */
1875
1876void
1877dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1878 compunit_symtab *symtab)
1879{
1880 gdb_assert (per_cu->index < this->m_symtabs.size ());
1881 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1882
1883 this->m_symtabs[per_cu->index] = symtab;
1884}
1885
c906108c 1886/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1887 information and return true if we have enough to do something.
1888 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1889 ELF names are used. CAN_COPY is true for formats where symbol
1890 interposition is possible and so symbol values must follow copy
1891 relocation rules. */
c906108c
SS
1892
1893int
251d32d9 1894dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1895 const struct dwarf2_debug_sections *names,
1896 bool can_copy)
c906108c 1897{
97cbe998
SDJ
1898 if (objfile->flags & OBJF_READNEVER)
1899 return 0;
1900
976ca316 1901 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1902
976ca316 1903 if (per_objfile == NULL)
5989a64e 1904 {
17ee85fc
TT
1905 dwarf2_per_bfd *per_bfd;
1906
1907 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1908 doesn't require relocations and if there aren't partial symbols
1909 from some other reader. */
1910 if (!objfile_has_partial_symbols (objfile)
1911 && !gdb_bfd_requires_relocations (objfile->obfd))
1912 {
1913 /* See if one has been created for this BFD yet. */
1914 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1915
1916 if (per_bfd == nullptr)
1917 {
1918 /* No, create it now. */
1919 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1920 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1921 }
1922 }
1923 else
1924 {
1925 /* No sharing possible, create one specifically for this objfile. */
1926 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1927 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1928 }
5989a64e 1929
976ca316 1930 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1931 }
5bfd760d 1932
976ca316
SM
1933 return (!per_objfile->per_bfd->info.is_virtual
1934 && per_objfile->per_bfd->info.s.section != NULL
1935 && !per_objfile->per_bfd->abbrev.is_virtual
1936 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1937}
1938
251d32d9
TG
1939/* When loading sections, we look either for uncompressed section or for
1940 compressed section names. */
233a11ab
CS
1941
1942static int
251d32d9
TG
1943section_is_p (const char *section_name,
1944 const struct dwarf2_section_names *names)
233a11ab 1945{
251d32d9
TG
1946 if (names->normal != NULL
1947 && strcmp (section_name, names->normal) == 0)
1948 return 1;
1949 if (names->compressed != NULL
1950 && strcmp (section_name, names->compressed) == 0)
1951 return 1;
1952 return 0;
233a11ab
CS
1953}
1954
330cdd98 1955/* See declaration. */
c906108c 1956
330cdd98 1957void
5989a64e
SM
1958dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1959 const dwarf2_debug_sections &names)
c906108c 1960{
fd361982 1961 flagword aflag = bfd_section_flags (sectp);
251d32d9 1962
dc7650b8
JK
1963 if ((aflag & SEC_HAS_CONTENTS) == 0)
1964 {
1965 }
950b7495
KS
1966 else if (elf_section_data (sectp)->this_hdr.sh_size
1967 > bfd_get_file_size (abfd))
1968 {
1969 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1970 warning (_("Discarding section %s which has a section size (%s"
1971 ") larger than the file size [in module %s]"),
1972 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1973 bfd_get_filename (abfd));
1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.info))
c906108c 1976 {
330cdd98 1977 this->info.s.section = sectp;
fd361982 1978 this->info.size = bfd_section_size (sectp);
c906108c 1979 }
330cdd98 1980 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1981 {
330cdd98 1982 this->abbrev.s.section = sectp;
fd361982 1983 this->abbrev.size = bfd_section_size (sectp);
c906108c 1984 }
330cdd98 1985 else if (section_is_p (sectp->name, &names.line))
c906108c 1986 {
330cdd98 1987 this->line.s.section = sectp;
fd361982 1988 this->line.size = bfd_section_size (sectp);
c906108c 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.loc))
c906108c 1991 {
330cdd98 1992 this->loc.s.section = sectp;
fd361982 1993 this->loc.size = bfd_section_size (sectp);
c906108c 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.loclists))
43988095 1996 {
330cdd98 1997 this->loclists.s.section = sectp;
fd361982 1998 this->loclists.size = bfd_section_size (sectp);
43988095 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2001 {
330cdd98 2002 this->macinfo.s.section = sectp;
fd361982 2003 this->macinfo.size = bfd_section_size (sectp);
c906108c 2004 }
330cdd98 2005 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2006 {
330cdd98 2007 this->macro.s.section = sectp;
fd361982 2008 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2009 }
330cdd98 2010 else if (section_is_p (sectp->name, &names.str))
c906108c 2011 {
330cdd98 2012 this->str.s.section = sectp;
fd361982 2013 this->str.size = bfd_section_size (sectp);
c906108c 2014 }
18a8505e
AT
2015 else if (section_is_p (sectp->name, &names.str_offsets))
2016 {
2017 this->str_offsets.s.section = sectp;
2018 this->str_offsets.size = bfd_section_size (sectp);
2019 }
330cdd98 2020 else if (section_is_p (sectp->name, &names.line_str))
43988095 2021 {
330cdd98 2022 this->line_str.s.section = sectp;
fd361982 2023 this->line_str.size = bfd_section_size (sectp);
43988095 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2026 {
330cdd98 2027 this->addr.s.section = sectp;
fd361982 2028 this->addr.size = bfd_section_size (sectp);
3019eac3 2029 }
330cdd98 2030 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2031 {
330cdd98 2032 this->frame.s.section = sectp;
fd361982 2033 this->frame.size = bfd_section_size (sectp);
b6af0555 2034 }
330cdd98 2035 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2036 {
330cdd98 2037 this->eh_frame.s.section = sectp;
fd361982 2038 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2039 }
330cdd98 2040 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2041 {
330cdd98 2042 this->ranges.s.section = sectp;
fd361982 2043 this->ranges.size = bfd_section_size (sectp);
af34e669 2044 }
330cdd98 2045 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2046 {
330cdd98 2047 this->rnglists.s.section = sectp;
fd361982 2048 this->rnglists.size = bfd_section_size (sectp);
43988095 2049 }
330cdd98 2050 else if (section_is_p (sectp->name, &names.types))
348e048f 2051 {
8b70b953
TT
2052 struct dwarf2_section_info type_section;
2053
2054 memset (&type_section, 0, sizeof (type_section));
049412e3 2055 type_section.s.section = sectp;
fd361982 2056 type_section.size = bfd_section_size (sectp);
8b70b953 2057
fd5866f6 2058 this->types.push_back (type_section);
348e048f 2059 }
330cdd98 2060 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2061 {
330cdd98 2062 this->gdb_index.s.section = sectp;
fd361982 2063 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2064 }
927aa2e7
JK
2065 else if (section_is_p (sectp->name, &names.debug_names))
2066 {
2067 this->debug_names.s.section = sectp;
fd361982 2068 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2069 }
2070 else if (section_is_p (sectp->name, &names.debug_aranges))
2071 {
2072 this->debug_aranges.s.section = sectp;
fd361982 2073 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2074 }
dce234bc 2075
fd361982
AM
2076 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2077 && bfd_section_vma (sectp) == 0)
330cdd98 2078 this->has_section_at_zero = true;
c906108c
SS
2079}
2080
dce234bc 2081/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2082 SECTION_NAME. */
af34e669 2083
dce234bc 2084void
3017a003
TG
2085dwarf2_get_section_info (struct objfile *objfile,
2086 enum dwarf2_section_enum sect,
d521ce57 2087 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2088 bfd_size_type *sizep)
2089{
976ca316 2090 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2091 struct dwarf2_section_info *info;
a3b2a86b
TT
2092
2093 /* We may see an objfile without any DWARF, in which case we just
2094 return nothing. */
976ca316 2095 if (per_objfile == NULL)
a3b2a86b
TT
2096 {
2097 *sectp = NULL;
2098 *bufp = NULL;
2099 *sizep = 0;
2100 return;
2101 }
3017a003
TG
2102 switch (sect)
2103 {
2104 case DWARF2_DEBUG_FRAME:
976ca316 2105 info = &per_objfile->per_bfd->frame;
3017a003
TG
2106 break;
2107 case DWARF2_EH_FRAME:
976ca316 2108 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2109 break;
2110 default:
2111 gdb_assert_not_reached ("unexpected section");
2112 }
dce234bc 2113
96b79293 2114 info->read (objfile);
dce234bc 2115
96b79293 2116 *sectp = info->get_bfd_section ();
dce234bc
PP
2117 *bufp = info->buffer;
2118 *sizep = info->size;
2119}
2120
36586728
TT
2121/* A helper function to find the sections for a .dwz file. */
2122
2123static void
5bb6e9dd 2124locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2125{
36586728
TT
2126 /* Note that we only support the standard ELF names, because .dwz
2127 is ELF-only (at the time of writing). */
2128 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2129 {
049412e3 2130 dwz_file->abbrev.s.section = sectp;
fd361982 2131 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2132 }
2133 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2134 {
049412e3 2135 dwz_file->info.s.section = sectp;
fd361982 2136 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2137 }
2138 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2139 {
049412e3 2140 dwz_file->str.s.section = sectp;
fd361982 2141 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2142 }
2143 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2144 {
049412e3 2145 dwz_file->line.s.section = sectp;
fd361982 2146 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2147 }
2148 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2149 {
049412e3 2150 dwz_file->macro.s.section = sectp;
fd361982 2151 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2152 }
2ec9a5e0
TT
2153 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2154 {
049412e3 2155 dwz_file->gdb_index.s.section = sectp;
fd361982 2156 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2157 }
927aa2e7
JK
2158 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2159 {
2160 dwz_file->debug_names.s.section = sectp;
fd361982 2161 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2162 }
36586728
TT
2163}
2164
c4973306 2165/* See dwarf2read.h. */
36586728 2166
c4973306 2167struct dwz_file *
c3699833 2168dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2169{
36586728 2170 const char *filename;
acd13123 2171 bfd_size_type buildid_len_arg;
dc294be5
TT
2172 size_t buildid_len;
2173 bfd_byte *buildid;
36586728 2174
c3699833
SM
2175 if (per_bfd->dwz_file != NULL)
2176 return per_bfd->dwz_file.get ();
36586728 2177
4db1a1dc 2178 bfd_set_error (bfd_error_no_error);
791afaa2 2179 gdb::unique_xmalloc_ptr<char> data
c3699833 2180 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2181 &buildid_len_arg, &buildid));
4db1a1dc
TT
2182 if (data == NULL)
2183 {
2184 if (bfd_get_error () == bfd_error_no_error)
2185 return NULL;
2186 error (_("could not read '.gnu_debugaltlink' section: %s"),
2187 bfd_errmsg (bfd_get_error ()));
2188 }
791afaa2
TT
2189
2190 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2191
acd13123
TT
2192 buildid_len = (size_t) buildid_len_arg;
2193
791afaa2 2194 filename = data.get ();
d721ba37
PA
2195
2196 std::string abs_storage;
36586728
TT
2197 if (!IS_ABSOLUTE_PATH (filename))
2198 {
14278e1f 2199 gdb::unique_xmalloc_ptr<char> abs
c3699833 2200 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2201
14278e1f 2202 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2203 filename = abs_storage.c_str ();
36586728
TT
2204 }
2205
dc294be5
TT
2206 /* First try the file name given in the section. If that doesn't
2207 work, try to use the build-id instead. */
ad80db5b 2208 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2209 if (dwz_bfd != NULL)
36586728 2210 {
192b62ce 2211 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2212 dwz_bfd.reset (nullptr);
36586728
TT
2213 }
2214
dc294be5
TT
2215 if (dwz_bfd == NULL)
2216 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2217
0d79cdc4
AM
2218 if (dwz_bfd == nullptr)
2219 {
2220 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2221 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2222
2223 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2224 buildid_len,
2225 origname,
2226 &alt_filename));
2227
2228 if (fd.get () >= 0)
2229 {
2230 /* File successfully retrieved from server. */
ad80db5b 2231 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2232
2233 if (dwz_bfd == nullptr)
2234 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2235 alt_filename.get ());
2236 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2237 dwz_bfd.reset (nullptr);
2238 }
2239 }
2240
dc294be5
TT
2241 if (dwz_bfd == NULL)
2242 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2243 bfd_get_filename (per_bfd->obfd));
dc294be5 2244
7ff8cb8c
TT
2245 std::unique_ptr<struct dwz_file> result
2246 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2247
5bb6e9dd
TT
2248 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2249 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2250
c3699833
SM
2251 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2252 per_bfd->dwz_file = std::move (result);
2253 return per_bfd->dwz_file.get ();
36586728 2254}
9291a0cd 2255\f
7b9f3c50
DE
2256/* DWARF quick_symbols_functions support. */
2257
2258/* TUs can share .debug_line entries, and there can be a lot more TUs than
2259 unique line tables, so we maintain a separate table of all .debug_line
2260 derived entries to support the sharing.
2261 All the quick functions need is the list of file names. We discard the
2262 line_header when we're done and don't need to record it here. */
2263struct quick_file_names
2264{
094b34ac
DE
2265 /* The data used to construct the hash key. */
2266 struct stmt_list_hash hash;
7b9f3c50
DE
2267
2268 /* The number of entries in file_names, real_names. */
2269 unsigned int num_file_names;
2270
2271 /* The file names from the line table, after being run through
2272 file_full_name. */
2273 const char **file_names;
2274
2275 /* The file names from the line table after being run through
2276 gdb_realpath. These are computed lazily. */
2277 const char **real_names;
2278};
2279
2280/* When using the index (and thus not using psymtabs), each CU has an
2281 object of this type. This is used to hold information needed by
2282 the various "quick" methods. */
2283struct dwarf2_per_cu_quick_data
2284{
2285 /* The file table. This can be NULL if there was no file table
2286 or it's currently not read in.
5989a64e 2287 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2288 struct quick_file_names *file_names;
2289
7b9f3c50
DE
2290 /* A temporary mark bit used when iterating over all CUs in
2291 expand_symtabs_matching. */
2292 unsigned int mark : 1;
2293
2294 /* True if we've tried to read the file table and found there isn't one.
2295 There will be no point in trying to read it again next time. */
2296 unsigned int no_file_data : 1;
2297};
2298
094b34ac
DE
2299/* Utility hash function for a stmt_list_hash. */
2300
2301static hashval_t
2302hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2303{
2304 hashval_t v = 0;
2305
2306 if (stmt_list_hash->dwo_unit != NULL)
2307 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2308 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2309 return v;
2310}
2311
2312/* Utility equality function for a stmt_list_hash. */
2313
2314static int
2315eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2316 const struct stmt_list_hash *rhs)
2317{
2318 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2319 return 0;
2320 if (lhs->dwo_unit != NULL
2321 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2322 return 0;
2323
9c541725 2324 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2325}
2326
7b9f3c50
DE
2327/* Hash function for a quick_file_names. */
2328
2329static hashval_t
2330hash_file_name_entry (const void *e)
2331{
9a3c8263
SM
2332 const struct quick_file_names *file_data
2333 = (const struct quick_file_names *) e;
7b9f3c50 2334
094b34ac 2335 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2336}
2337
2338/* Equality function for a quick_file_names. */
2339
2340static int
2341eq_file_name_entry (const void *a, const void *b)
2342{
9a3c8263
SM
2343 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2344 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2345
094b34ac 2346 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2347}
2348
2349/* Delete function for a quick_file_names. */
2350
2351static void
2352delete_file_name_entry (void *e)
2353{
9a3c8263 2354 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2355 int i;
2356
2357 for (i = 0; i < file_data->num_file_names; ++i)
2358 {
2359 xfree ((void*) file_data->file_names[i]);
2360 if (file_data->real_names)
2361 xfree ((void*) file_data->real_names[i]);
2362 }
2363
45940949
TT
2364 /* The space for the struct itself lives on the obstack, so we don't
2365 free it here. */
7b9f3c50
DE
2366}
2367
2368/* Create a quick_file_names hash table. */
2369
5895093f 2370static htab_up
7b9f3c50
DE
2371create_quick_file_names_table (unsigned int nr_initial_entries)
2372{
5895093f
TT
2373 return htab_up (htab_create_alloc (nr_initial_entries,
2374 hash_file_name_entry, eq_file_name_entry,
2375 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2376}
9291a0cd 2377
ab432490
SM
2378/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2379 function is unrelated to symtabs, symtab would have to be created afterwards.
2380 You should call age_cached_comp_units after processing the CU. */
918dd910 2381
1b555f17 2382static dwarf2_cu *
ab432490
SM
2383load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2384 bool skip_partial)
918dd910 2385{
3019eac3 2386 if (per_cu->is_debug_types)
ab432490 2387 load_full_type_unit (per_cu, per_objfile);
918dd910 2388 else
4a636814
SM
2389 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2390 skip_partial, language_minimal);
918dd910 2391
7188ed02
SM
2392 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2393 if (cu == nullptr)
1b555f17 2394 return nullptr; /* Dummy CU. */
2dc860c0 2395
7188ed02 2396 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2397
7188ed02 2398 return cu;
918dd910
JK
2399}
2400
97a1449a 2401/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2402
9291a0cd 2403static void
97a1449a 2404dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2405 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2406{
f4dc4d17
DE
2407 /* Skip type_unit_groups, reading the type units they contain
2408 is handled elsewhere. */
197400e8 2409 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2410 return;
2411
b303c6f6
AB
2412 /* The destructor of dwarf2_queue_guard frees any entries left on
2413 the queue. After this point we're guaranteed to leave this function
2414 with the dwarf queue empty. */
39856def 2415 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2416
976ca316 2417 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2418 {
976ca316
SM
2419 queue_comp_unit (per_cu, per_objfile, language_minimal);
2420 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2421
2422 /* If we just loaded a CU from a DWO, and we're working with an index
2423 that may badly handle TUs, load all the TUs in that DWO as well.
2424 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2425 if (!per_cu->is_debug_types
1b555f17
SM
2426 && cu != NULL
2427 && cu->dwo_unit != NULL
976ca316
SM
2428 && per_objfile->per_bfd->index_table != NULL
2429 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2430 /* DWP files aren't supported yet. */
976ca316 2431 && get_dwp_file (per_objfile) == NULL)
1b555f17 2432 queue_and_load_all_dwo_tus (cu);
95554aad 2433 }
9291a0cd 2434
976ca316 2435 process_queue (per_objfile);
9291a0cd
TT
2436
2437 /* Age the cache, releasing compilation units that have not
2438 been used recently. */
976ca316 2439 per_objfile->age_comp_units ();
9291a0cd
TT
2440}
2441
97a1449a
SM
2442/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2443 the per-objfile for which this symtab is instantiated.
2444
2445 Returns the resulting symbol table. */
2fdf6df6 2446
43f3e411 2447static struct compunit_symtab *
97a1449a 2448dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2449 dwarf2_per_objfile *per_objfile,
97a1449a 2450 bool skip_partial)
9291a0cd 2451{
976ca316 2452 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2453
976ca316 2454 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2455 {
976ca316 2456 free_cached_comp_units freer (per_objfile);
c83dd867 2457 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2458 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2459 process_cu_includes (per_objfile);
9291a0cd 2460 }
f194fefb 2461
976ca316 2462 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2463}
2464
ff4c9fec 2465/* See declaration. */
f4dc4d17 2466
ff4c9fec 2467dwarf2_per_cu_data *
5989a64e 2468dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2469{
b76e467d 2470 if (index >= this->all_comp_units.size ())
ff4c9fec 2471 {
b76e467d 2472 index -= this->all_comp_units.size ();
b2bdb8cf 2473 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2474 return &this->all_type_units[index]->per_cu;
2475 }
f4dc4d17 2476
ff4c9fec
SM
2477 return this->all_comp_units[index];
2478}
f4dc4d17 2479
ff4c9fec 2480/* See declaration. */
2fdf6df6 2481
ff4c9fec 2482dwarf2_per_cu_data *
5989a64e 2483dwarf2_per_bfd::get_cu (int index)
1fd400ff 2484{
b76e467d 2485 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2486
ff4c9fec 2487 return this->all_comp_units[index];
f4dc4d17
DE
2488}
2489
ff4c9fec 2490/* See declaration. */
f4dc4d17 2491
ff4c9fec 2492signatured_type *
5989a64e 2493dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2494{
b2bdb8cf 2495 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2496
ff4c9fec 2497 return this->all_type_units[index];
1fd400ff
TT
2498}
2499
d3473f0c
TT
2500/* See read.h. */
2501
2502dwarf2_per_cu_data *
5989a64e 2503dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2504{
2505 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2506 result->per_bfd = this;
d3473f0c
TT
2507 result->index = m_num_psymtabs++;
2508 return result;
2509}
2510
2511/* See read.h. */
2512
2513signatured_type *
5989a64e 2514dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2515{
2516 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2517 result->per_cu.per_bfd = this;
d3473f0c
TT
2518 result->per_cu.index = m_num_psymtabs++;
2519 return result;
2520}
2521
168c9250 2522/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2523 obstack, and constructed with the specified field values. */
4b514bc8
JK
2524
2525static dwarf2_per_cu_data *
168c9250
SM
2526create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2527 struct dwarf2_section_info *section,
2528 int is_dwz,
2529 sect_offset sect_off, ULONGEST length)
4b514bc8 2530{
168c9250 2531 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2532 the_cu->sect_off = sect_off;
2533 the_cu->length = length;
4b514bc8 2534 the_cu->section = section;
168c9250 2535 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2536 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2537 the_cu->is_dwz = is_dwz;
2538 return the_cu;
2539}
2540
2ec9a5e0
TT
2541/* A helper for create_cus_from_index that handles a given list of
2542 CUs. */
2fdf6df6 2543
74a0d9f6 2544static void
168c9250 2545create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2546 const gdb_byte *cu_list, offset_type n_elements,
2547 struct dwarf2_section_info *section,
b76e467d 2548 int is_dwz)
9291a0cd 2549{
12359b5e 2550 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2551 {
74a0d9f6 2552 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2553
2554 sect_offset sect_off
2555 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2556 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2557 cu_list += 2 * 8;
2558
b76e467d 2559 dwarf2_per_cu_data *per_cu
168c9250
SM
2560 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2561 length);
2562 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2563 }
9291a0cd
TT
2564}
2565
2ec9a5e0 2566/* Read the CU list from the mapped index, and use it to create all
168c9250 2567 the CU objects for PER_BFD. */
2ec9a5e0 2568
74a0d9f6 2569static void
168c9250 2570create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2571 const gdb_byte *cu_list, offset_type cu_list_elements,
2572 const gdb_byte *dwz_list, offset_type dwz_elements)
2573{
168c9250
SM
2574 gdb_assert (per_bfd->all_comp_units.empty ());
2575 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2576
168c9250
SM
2577 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2578 &per_bfd->info, 0);
2ec9a5e0
TT
2579
2580 if (dwz_elements == 0)
74a0d9f6 2581 return;
2ec9a5e0 2582
168c9250
SM
2583 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2584 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2585 &dwz->info, 1);
2ec9a5e0
TT
2586}
2587
1fd400ff 2588/* Create the signatured type hash table from the index. */
673bfd45 2589
74a0d9f6 2590static void
12359b5e 2591create_signatured_type_table_from_index
168c9250
SM
2592 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2593 const gdb_byte *bytes, offset_type elements)
1fd400ff 2594{
168c9250
SM
2595 gdb_assert (per_bfd->all_type_units.empty ());
2596 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2597
298e9637 2598 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2599
12359b5e 2600 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2601 {
52dc124a 2602 struct signatured_type *sig_type;
9c541725 2603 ULONGEST signature;
1fd400ff 2604 void **slot;
9c541725 2605 cu_offset type_offset_in_tu;
1fd400ff 2606
74a0d9f6 2607 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2608 sect_offset sect_off
2609 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2610 type_offset_in_tu
2611 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2612 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2613 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2614 bytes += 3 * 8;
2615
168c9250 2616 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2617 sig_type->signature = signature;
9c541725 2618 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2619 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2620 sig_type->per_cu.section = section;
9c541725 2621 sig_type->per_cu.sect_off = sect_off;
52dc124a 2622 sig_type->per_cu.v.quick
168c9250 2623 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2624 struct dwarf2_per_cu_quick_data);
2625
b0b6a987 2626 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2627 *slot = sig_type;
1fd400ff 2628
168c9250 2629 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2630 }
2631
168c9250 2632 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2633}
2634
927aa2e7
JK
2635/* Create the signatured type hash table from .debug_names. */
2636
2637static void
2638create_signatured_type_table_from_debug_names
976ca316 2639 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2640 const mapped_debug_names &map,
2641 struct dwarf2_section_info *section,
2642 struct dwarf2_section_info *abbrev_section)
2643{
976ca316 2644 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2645
96b79293
TT
2646 section->read (objfile);
2647 abbrev_section->read (objfile);
927aa2e7 2648
976ca316
SM
2649 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2650 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2651
298e9637 2652 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2653
2654 for (uint32_t i = 0; i < map.tu_count; ++i)
2655 {
2656 struct signatured_type *sig_type;
927aa2e7 2657 void **slot;
927aa2e7
JK
2658
2659 sect_offset sect_off
2660 = (sect_offset) (extract_unsigned_integer
2661 (map.tu_table_reordered + i * map.offset_size,
2662 map.offset_size,
2663 map.dwarf5_byte_order));
2664
2665 comp_unit_head cu_header;
976ca316 2666 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2667 abbrev_section,
927aa2e7
JK
2668 section->buffer + to_underlying (sect_off),
2669 rcuh_kind::TYPE);
2670
976ca316 2671 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2672 sig_type->signature = cu_header.signature;
2673 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2674 sig_type->per_cu.is_debug_types = 1;
2675 sig_type->per_cu.section = section;
2676 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2677 sig_type->per_cu.v.quick
976ca316 2678 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2679 struct dwarf2_per_cu_quick_data);
2680
b0b6a987 2681 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2682 *slot = sig_type;
2683
976ca316 2684 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2685 }
2686
976ca316 2687 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2688}
2689
9291a0cd
TT
2690/* Read the address map data from the mapped index, and use it to
2691 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2692
9291a0cd 2693static void
976ca316 2694create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2695 struct mapped_index *index)
9291a0cd 2696{
976ca316 2697 struct objfile *objfile = per_objfile->objfile;
08feed99 2698 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2699 const gdb_byte *iter, *end;
9291a0cd 2700 struct addrmap *mutable_map;
9291a0cd
TT
2701 CORE_ADDR baseaddr;
2702
8268c778
PA
2703 auto_obstack temp_obstack;
2704
9291a0cd
TT
2705 mutable_map = addrmap_create_mutable (&temp_obstack);
2706
f00a2de2
PA
2707 iter = index->address_table.data ();
2708 end = iter + index->address_table.size ();
9291a0cd 2709
b3b3bada 2710 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2711
2712 while (iter < end)
2713 {
2714 ULONGEST hi, lo, cu_index;
2715 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2716 iter += 8;
2717 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2718 iter += 8;
2719 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2720 iter += 4;
f652bce2 2721
24a55014 2722 if (lo > hi)
f652bce2 2723 {
b98664d3 2724 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2725 hex_string (lo), hex_string (hi));
24a55014 2726 continue;
f652bce2 2727 }
24a55014 2728
976ca316 2729 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2730 {
b98664d3 2731 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2732 (unsigned) cu_index);
24a55014 2733 continue;
f652bce2 2734 }
24a55014 2735
79748972
TT
2736 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2737 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2738 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2739 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2740 }
2741
d320c2b5 2742 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2743 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2744}
2745
927aa2e7
JK
2746/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2747 populate the objfile's psymtabs_addrmap. */
2748
2749static void
976ca316 2750create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2751 struct dwarf2_section_info *section)
2752{
976ca316 2753 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2754 bfd *abfd = objfile->obfd;
08feed99 2755 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2756 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2757
2758 auto_obstack temp_obstack;
2759 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2760
2761 std::unordered_map<sect_offset,
2762 dwarf2_per_cu_data *,
2763 gdb::hash_enum<sect_offset>>
2764 debug_info_offset_to_per_cu;
976ca316 2765 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2766 {
927aa2e7
JK
2767 const auto insertpair
2768 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2769 if (!insertpair.second)
2770 {
2771 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2772 "debug_info_offset %s, ignoring .debug_aranges."),
2773 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2774 return;
2775 }
2776 }
2777
96b79293 2778 section->read (objfile);
927aa2e7
JK
2779
2780 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2781
2782 const gdb_byte *addr = section->buffer;
2783
2784 while (addr < section->buffer + section->size)
2785 {
2786 const gdb_byte *const entry_addr = addr;
2787 unsigned int bytes_read;
2788
2789 const LONGEST entry_length = read_initial_length (abfd, addr,
2790 &bytes_read);
2791 addr += bytes_read;
2792
2793 const gdb_byte *const entry_end = addr + entry_length;
2794 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2795 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2796 if (addr + entry_length > section->buffer + section->size)
2797 {
47e3f474 2798 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2799 "length %s exceeds section length %s, "
2800 "ignoring .debug_aranges."),
47e3f474
TV
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer),
927aa2e7
JK
2803 plongest (bytes_read + entry_length),
2804 pulongest (section->size));
2805 return;
2806 }
2807
2808 /* The version number. */
2809 const uint16_t version = read_2_bytes (abfd, addr);
2810 addr += 2;
2811 if (version != 2)
2812 {
47e3f474 2813 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2814 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2815 objfile_name (objfile),
2816 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2817 return;
2818 }
2819
2820 const uint64_t debug_info_offset
2821 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2822 addr += offset_size;
2823 const auto per_cu_it
2824 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2825 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2826 {
47e3f474 2827 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2828 "debug_info_offset %s does not exists, "
2829 "ignoring .debug_aranges."),
47e3f474
TV
2830 objfile_name (objfile),
2831 plongest (entry_addr - section->buffer),
927aa2e7
JK
2832 pulongest (debug_info_offset));
2833 return;
2834 }
2835 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2836
2837 const uint8_t address_size = *addr++;
2838 if (address_size < 1 || address_size > 8)
2839 {
47e3f474 2840 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2841 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2842 objfile_name (objfile),
2843 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2844 return;
2845 }
2846
2847 const uint8_t segment_selector_size = *addr++;
2848 if (segment_selector_size != 0)
2849 {
47e3f474 2850 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2851 "segment_selector_size %u is not supported, "
2852 "ignoring .debug_aranges."),
47e3f474
TV
2853 objfile_name (objfile),
2854 plongest (entry_addr - section->buffer),
927aa2e7
JK
2855 segment_selector_size);
2856 return;
2857 }
2858
2859 /* Must pad to an alignment boundary that is twice the address
2860 size. It is undocumented by the DWARF standard but GCC does
2861 use it. */
2862 for (size_t padding = ((-(addr - section->buffer))
2863 & (2 * address_size - 1));
2864 padding > 0; padding--)
2865 if (*addr++ != 0)
2866 {
47e3f474 2867 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2868 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2869 objfile_name (objfile),
2870 plongest (entry_addr - section->buffer));
927aa2e7
JK
2871 return;
2872 }
2873
2874 for (;;)
2875 {
2876 if (addr + 2 * address_size > entry_end)
2877 {
47e3f474 2878 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2879 "address list is not properly terminated, "
2880 "ignoring .debug_aranges."),
47e3f474
TV
2881 objfile_name (objfile),
2882 plongest (entry_addr - section->buffer));
927aa2e7
JK
2883 return;
2884 }
2885 ULONGEST start = extract_unsigned_integer (addr, address_size,
2886 dwarf5_byte_order);
2887 addr += address_size;
2888 ULONGEST length = extract_unsigned_integer (addr, address_size,
2889 dwarf5_byte_order);
2890 addr += address_size;
2891 if (start == 0 && length == 0)
2892 break;
976ca316 2893 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2894 {
2895 /* Symbol was eliminated due to a COMDAT group. */
2896 continue;
2897 }
2898 ULONGEST end = start + length;
79748972
TT
2899 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2900 - baseaddr);
2901 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2902 - baseaddr);
927aa2e7
JK
2903 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2904 }
2905 }
2906
d320c2b5 2907 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2908 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2909}
2910
9291a0cd
TT
2911/* Find a slot in the mapped index INDEX for the object named NAME.
2912 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2913 constant pool and return true. If NAME cannot be found, return
2914 false. */
2fdf6df6 2915
109483d9 2916static bool
9291a0cd
TT
2917find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2918 offset_type **vec_out)
2919{
0cf03b49 2920 offset_type hash;
9291a0cd 2921 offset_type slot, step;
559a7a62 2922 int (*cmp) (const char *, const char *);
9291a0cd 2923
791afaa2 2924 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2925 if (current_language->la_language == language_cplus
45280282
IB
2926 || current_language->la_language == language_fortran
2927 || current_language->la_language == language_d)
0cf03b49
JK
2928 {
2929 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2930 not contain any. */
a8719064 2931
72998fb3 2932 if (strchr (name, '(') != NULL)
0cf03b49 2933 {
109483d9 2934 without_params = cp_remove_params (name);
0cf03b49 2935
72998fb3 2936 if (without_params != NULL)
791afaa2 2937 name = without_params.get ();
0cf03b49
JK
2938 }
2939 }
2940
559a7a62 2941 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2942 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2943 simulate our NAME being searched is also lowercased. */
2944 hash = mapped_index_string_hash ((index->version == 4
2945 && case_sensitivity == case_sensitive_off
2946 ? 5 : index->version),
2947 name);
2948
f00a2de2
PA
2949 slot = hash & (index->symbol_table.size () - 1);
2950 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2951 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2952
2953 for (;;)
2954 {
9291a0cd 2955 const char *str;
f00a2de2
PA
2956
2957 const auto &bucket = index->symbol_table[slot];
2958 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2959 return false;
9291a0cd 2960
f00a2de2 2961 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2962 if (!cmp (name, str))
9291a0cd
TT
2963 {
2964 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2965 + MAYBE_SWAP (bucket.vec));
109483d9 2966 return true;
9291a0cd
TT
2967 }
2968
f00a2de2 2969 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2970 }
2971}
2972
4485a1c1
SM
2973/* A helper function that reads the .gdb_index from BUFFER and fills
2974 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2975 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2976 ok to use deprecated sections.
2977
2978 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2979 out parameters that are filled in with information about the CU and
2980 TU lists in the section.
2981
4485a1c1 2982 Returns true if all went well, false otherwise. */
2fdf6df6 2983
d33bc52e 2984static bool
3810f182 2985read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2986 bool deprecated_ok,
2987 gdb::array_view<const gdb_byte> buffer,
2988 struct mapped_index *map,
2989 const gdb_byte **cu_list,
2990 offset_type *cu_list_elements,
2991 const gdb_byte **types_list,
2992 offset_type *types_list_elements)
2993{
2994 const gdb_byte *addr = &buffer[0];
82430852 2995
9291a0cd 2996 /* Version check. */
4485a1c1 2997 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2998 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2999 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3000 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3001 indices. */
831adc1f 3002 if (version < 4)
481860b3
GB
3003 {
3004 static int warning_printed = 0;
3005 if (!warning_printed)
3006 {
3007 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3008 filename);
481860b3
GB
3009 warning_printed = 1;
3010 }
3011 return 0;
3012 }
3013 /* Index version 4 uses a different hash function than index version
3014 5 and later.
3015
3016 Versions earlier than 6 did not emit psymbols for inlined
3017 functions. Using these files will cause GDB not to be able to
3018 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3019 indices unless the user has done
3020 "set use-deprecated-index-sections on". */
2ec9a5e0 3021 if (version < 6 && !deprecated_ok)
481860b3
GB
3022 {
3023 static int warning_printed = 0;
3024 if (!warning_printed)
3025 {
e615022a
DE
3026 warning (_("\
3027Skipping deprecated .gdb_index section in %s.\n\
3028Do \"set use-deprecated-index-sections on\" before the file is read\n\
3029to use the section anyway."),
2ec9a5e0 3030 filename);
481860b3
GB
3031 warning_printed = 1;
3032 }
3033 return 0;
3034 }
796a7ff8 3035 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3036 of the TU (for symbols coming from TUs),
3037 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3038 Plus gold-generated indices can have duplicate entries for global symbols,
3039 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3040 These are just performance bugs, and we can't distinguish gdb-generated
3041 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3042
481860b3 3043 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3044 longer backward compatible. */
796a7ff8 3045 if (version > 8)
594e8718 3046 return 0;
9291a0cd 3047
559a7a62 3048 map->version = version;
9291a0cd 3049
4485a1c1 3050 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3051
4485a1c1 3052 int i = 0;
2ec9a5e0
TT
3053 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3054 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3055 / 8);
1fd400ff
TT
3056 ++i;
3057
2ec9a5e0
TT
3058 *types_list = addr + MAYBE_SWAP (metadata[i]);
3059 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3060 - MAYBE_SWAP (metadata[i]))
3061 / 8);
987d643c 3062 ++i;
1fd400ff 3063
f00a2de2
PA
3064 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3065 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3066 map->address_table
3067 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3068 ++i;
3069
f00a2de2
PA
3070 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3071 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3072 map->symbol_table
3073 = gdb::array_view<mapped_index::symbol_table_slot>
3074 ((mapped_index::symbol_table_slot *) symbol_table,
3075 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3076
f00a2de2 3077 ++i;
f9d83a0b 3078 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3079
2ec9a5e0
TT
3080 return 1;
3081}
3082
4485a1c1
SM
3083/* Callback types for dwarf2_read_gdb_index. */
3084
3085typedef gdb::function_view
5989a64e 3086 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3087 get_gdb_index_contents_ftype;
3088typedef gdb::function_view
3089 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3090 get_gdb_index_contents_dwz_ftype;
3091
927aa2e7 3092/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3093 elements of all the CUs and return 1. Otherwise, return 0. */
3094
3095static int
4485a1c1 3096dwarf2_read_gdb_index
976ca316 3097 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3098 get_gdb_index_contents_ftype get_gdb_index_contents,
3099 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3100{
2ec9a5e0
TT
3101 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3102 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3103 struct dwz_file *dwz;
976ca316 3104 struct objfile *objfile = per_objfile->objfile;
f8c41851 3105 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3106
4485a1c1 3107 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3108 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3109
3110 if (main_index_contents.empty ())
3111 return 0;
3112
3063847f 3113 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3114 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3115 use_deprecated_index_sections,
3116 main_index_contents, map.get (), &cu_list,
3117 &cu_list_elements, &types_list,
3118 &types_list_elements))
2ec9a5e0
TT
3119 return 0;
3120
0fefef59 3121 /* Don't use the index if it's empty. */
3063847f 3122 if (map->symbol_table.empty ())
0fefef59
DE
3123 return 0;
3124
2ec9a5e0
TT
3125 /* If there is a .dwz file, read it so we can get its CU list as
3126 well. */
f8c41851 3127 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3128 if (dwz != NULL)
2ec9a5e0 3129 {
2ec9a5e0
TT
3130 struct mapped_index dwz_map;
3131 const gdb_byte *dwz_types_ignore;
3132 offset_type dwz_types_elements_ignore;
3133
4485a1c1
SM
3134 gdb::array_view<const gdb_byte> dwz_index_content
3135 = get_gdb_index_contents_dwz (objfile, dwz);
3136
3137 if (dwz_index_content.empty ())
3138 return 0;
3139
3810f182 3140 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3141 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3142 &dwz_list, &dwz_list_elements,
3143 &dwz_types_ignore,
3144 &dwz_types_elements_ignore))
2ec9a5e0
TT
3145 {
3146 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3147 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3148 return 0;
3149 }
3150 }
3151
f8c41851
SM
3152 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3153 dwz_list_elements);
1fd400ff 3154
8b70b953
TT
3155 if (types_list_elements)
3156 {
8b70b953
TT
3157 /* We can only handle a single .debug_types when we have an
3158 index. */
f8c41851 3159 if (per_bfd->types.size () != 1)
8b70b953
TT
3160 return 0;
3161
f8c41851 3162 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3163
f8c41851 3164 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3165 types_list_elements);
8b70b953 3166 }
9291a0cd 3167
976ca316 3168 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3169
f8c41851
SM
3170 per_bfd->index_table = std::move (map);
3171 per_bfd->using_index = 1;
3172 per_bfd->quick_file_names_table =
3173 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3174
3175 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3176 objfiles using the same BFD. */
3177 gdb_assert (per_bfd->partial_symtabs == nullptr);
3178 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3179
3180 return 1;
3181}
3182
dee91e82 3183/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3184
dee91e82
DE
3185static void
3186dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3187 const gdb_byte *info_ptr,
3e225074 3188 struct die_info *comp_unit_die)
9291a0cd 3189{
dee91e82 3190 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3191 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3193 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3194 struct attribute *attr;
7b9f3c50
DE
3195 void **slot;
3196 struct quick_file_names *qfn;
9291a0cd 3197
0186c6a7
DE
3198 gdb_assert (! this_cu->is_debug_types);
3199
07261596
TT
3200 /* Our callers never want to match partial units -- instead they
3201 will match the enclosing full CU. */
3202 if (comp_unit_die->tag == DW_TAG_partial_unit)
3203 {
3204 this_cu->v.quick->no_file_data = 1;
3205 return;
3206 }
3207
0186c6a7 3208 lh_cu = this_cu;
7b9f3c50 3209 slot = NULL;
dee91e82 3210
fff8551c 3211 line_header_up lh;
9c541725 3212 sect_offset line_offset {};
fff8551c 3213
dee91e82 3214 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3215 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3216 {
7b9f3c50
DE
3217 struct quick_file_names find_entry;
3218
d4df075e 3219 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3220
3221 /* We may have already read in this line header (TU line header sharing).
3222 If we have we're done. */
094b34ac 3223 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3224 find_entry.hash.line_sect_off = line_offset;
976ca316 3225 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3226 &find_entry, INSERT);
3227 if (*slot != NULL)
3228 {
9a3c8263 3229 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3230 return;
7b9f3c50
DE
3231 }
3232
3019eac3 3233 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3234 }
3235 if (lh == NULL)
3236 {
094b34ac 3237 lh_cu->v.quick->no_file_data = 1;
dee91e82 3238 return;
9291a0cd
TT
3239 }
3240
976ca316 3241 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3242 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3243 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3244 gdb_assert (slot != NULL);
3245 *slot = qfn;
9291a0cd 3246
d721ba37 3247 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3248
aa391654
TT
3249 int offset = 0;
3250 if (strcmp (fnd.name, "<unknown>") != 0)
3251 ++offset;
3252
7ba99d21 3253 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3254 qfn->file_names =
976ca316 3255 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3256 qfn->num_file_names);
aa391654
TT
3257 if (offset != 0)
3258 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3259 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3260 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3261 fnd.comp_dir).release ();
7b9f3c50 3262 qfn->real_names = NULL;
9291a0cd 3263
094b34ac 3264 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3265}
3266
3267/* A helper for the "quick" functions which attempts to read the line
3268 table for THIS_CU. */
3269
3270static struct quick_file_names *
ab432490
SM
3271dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3272 dwarf2_per_objfile *per_objfile)
dee91e82 3273{
0186c6a7
DE
3274 /* This should never be called for TUs. */
3275 gdb_assert (! this_cu->is_debug_types);
3276 /* Nor type unit groups. */
197400e8 3277 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3278
dee91e82
DE
3279 if (this_cu->v.quick->file_names != NULL)
3280 return this_cu->v.quick->file_names;
3281 /* If we know there is no line data, no point in looking again. */
3282 if (this_cu->v.quick->no_file_data)
3283 return NULL;
3284
ab432490 3285 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3286 if (!reader.dummy_p)
3e225074 3287 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3288
3289 if (this_cu->v.quick->no_file_data)
3290 return NULL;
3291 return this_cu->v.quick->file_names;
9291a0cd
TT
3292}
3293
3294/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3295 real path for a given file name from the line table. */
2fdf6df6 3296
9291a0cd 3297static const char *
976ca316 3298dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3299 struct quick_file_names *qfn, int index)
9291a0cd 3300{
7b9f3c50 3301 if (qfn->real_names == NULL)
976ca316 3302 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3303 qfn->num_file_names, const char *);
9291a0cd 3304
7b9f3c50 3305 if (qfn->real_names[index] == NULL)
14278e1f 3306 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3307
7b9f3c50 3308 return qfn->real_names[index];
9291a0cd
TT
3309}
3310
3311static struct symtab *
3312dw2_find_last_source_symtab (struct objfile *objfile)
3313{
976ca316
SM
3314 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3315 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3316 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3317
43f3e411
DE
3318 if (cust == NULL)
3319 return NULL;
ed2dc618 3320
43f3e411 3321 return compunit_primary_filetab (cust);
9291a0cd
TT
3322}
3323
7b9f3c50
DE
3324/* Traversal function for dw2_forget_cached_source_info. */
3325
3326static int
3327dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3328{
7b9f3c50 3329 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3330
7b9f3c50 3331 if (file_data->real_names)
9291a0cd 3332 {
7b9f3c50 3333 int i;
9291a0cd 3334
7b9f3c50 3335 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3336 {
7b9f3c50
DE
3337 xfree ((void*) file_data->real_names[i]);
3338 file_data->real_names[i] = NULL;
9291a0cd
TT
3339 }
3340 }
7b9f3c50
DE
3341
3342 return 1;
3343}
3344
3345static void
3346dw2_forget_cached_source_info (struct objfile *objfile)
3347{
976ca316 3348 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3349
976ca316 3350 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3351 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3352}
3353
f8eba3c6
TT
3354/* Helper function for dw2_map_symtabs_matching_filename that expands
3355 the symtabs and calls the iterator. */
3356
3357static int
3358dw2_map_expand_apply (struct objfile *objfile,
3359 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3360 const char *name, const char *real_path,
14bc53a8 3361 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3362{
43f3e411 3363 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3364
3365 /* Don't visit already-expanded CUs. */
af758d11
SM
3366 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3367 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3368 return 0;
3369
3370 /* This may expand more than one symtab, and we want to iterate over
3371 all of them. */
97a1449a 3372 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3373
14bc53a8
PA
3374 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3375 last_made, callback);
f8eba3c6
TT
3376}
3377
3378/* Implementation of the map_symtabs_matching_filename method. */
3379
14bc53a8
PA
3380static bool
3381dw2_map_symtabs_matching_filename
3382 (struct objfile *objfile, const char *name, const char *real_path,
3383 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3384{
c011a4f4 3385 const char *name_basename = lbasename (name);
976ca316 3386 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3387
848e3e78
DE
3388 /* The rule is CUs specify all the files, including those used by
3389 any TU, so there's no need to scan TUs here. */
f4dc4d17 3390
976ca316 3391 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3392 {
3d7bb9d9 3393 /* We only need to look at symtabs not already expanded. */
976ca316 3394 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3395 continue;
3396
976ca316 3397 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3398 if (file_data == NULL)
9291a0cd
TT
3399 continue;
3400
b76e467d 3401 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3402 {
7b9f3c50 3403 const char *this_name = file_data->file_names[j];
da235a7c 3404 const char *this_real_name;
9291a0cd 3405
af529f8f 3406 if (compare_filenames_for_search (this_name, name))
9291a0cd 3407 {
f5b95b50 3408 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3409 callback))
3410 return true;
288e77a7 3411 continue;
4aac40c8 3412 }
9291a0cd 3413
c011a4f4
DE
3414 /* Before we invoke realpath, which can get expensive when many
3415 files are involved, do a quick comparison of the basenames. */
3416 if (! basenames_may_differ
3417 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3418 continue;
3419
976ca316 3420 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3421 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3422 {
da235a7c 3423 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3424 callback))
3425 return true;
288e77a7 3426 continue;
da235a7c 3427 }
9291a0cd 3428
da235a7c
JK
3429 if (real_path != NULL)
3430 {
af529f8f
JK
3431 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3432 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3433 if (this_real_name != NULL
af529f8f 3434 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3435 {
f5b95b50 3436 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3437 callback))
3438 return true;
288e77a7 3439 continue;
9291a0cd
TT
3440 }
3441 }
3442 }
3443 }
3444
14bc53a8 3445 return false;
9291a0cd
TT
3446}
3447
da51c347
DE
3448/* Struct used to manage iterating over all CUs looking for a symbol. */
3449
3450struct dw2_symtab_iterator
9291a0cd 3451{
ed2dc618 3452 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3453 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3454 /* If set, only look for symbols that match that block. Valid values are
3455 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3456 gdb::optional<block_enum> block_index;
da51c347
DE
3457 /* The kind of symbol we're looking for. */
3458 domain_enum domain;
3459 /* The list of CUs from the index entry of the symbol,
3460 or NULL if not found. */
3461 offset_type *vec;
3462 /* The next element in VEC to look at. */
3463 int next;
3464 /* The number of elements in VEC, or zero if there is no match. */
3465 int length;
8943b874
DE
3466 /* Have we seen a global version of the symbol?
3467 If so we can ignore all further global instances.
3468 This is to work around gold/15646, inefficient gold-generated
3469 indices. */
3470 int global_seen;
da51c347 3471};
9291a0cd 3472
e5f3ece2 3473/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3474
9291a0cd 3475static void
e5f3ece2
TV
3476dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3477 dwarf2_per_objfile *per_objfile,
3478 gdb::optional<block_enum> block_index,
3479 domain_enum domain)
da51c347 3480{
976ca316 3481 iter->per_objfile = per_objfile;
da51c347
DE
3482 iter->block_index = block_index;
3483 iter->domain = domain;
3484 iter->next = 0;
8943b874 3485 iter->global_seen = 0;
e5f3ece2
TV
3486 iter->vec = NULL;
3487 iter->length = 0;
3488}
da51c347 3489
e5f3ece2
TV
3490/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3491
3492static void
3493dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3494 dwarf2_per_objfile *per_objfile,
3495 gdb::optional<block_enum> block_index,
3496 domain_enum domain,
3497 const char *name)
3498{
3499 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3500
e5f3ece2 3501 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3502 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3503 if (index == NULL)
3504 return;
3505
3506 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3507 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3508}
3509
3510/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3511
3512static void
3513dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3514 dwarf2_per_objfile *per_objfile,
3515 gdb::optional<block_enum> block_index,
3516 domain_enum domain, offset_type namei)
3517{
3518 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3519
3520 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3521 /* index is NULL if OBJF_READNOW. */
3522 if (index == NULL)
3523 return;
3524
3525 gdb_assert (!index->symbol_name_slot_invalid (namei));
3526 const auto &bucket = index->symbol_table[namei];
3527
3528 iter->vec = (offset_type *) (index->constant_pool
3529 + MAYBE_SWAP (bucket.vec));
3530 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3531}
3532
3533/* Return the next matching CU or NULL if there are no more. */
3534
3535static struct dwarf2_per_cu_data *
3536dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3537{
976ca316 3538 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3539
da51c347
DE
3540 for ( ; iter->next < iter->length; ++iter->next)
3541 {
3542 offset_type cu_index_and_attrs =
3543 MAYBE_SWAP (iter->vec[iter->next + 1]);
3544 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3545 gdb_index_symbol_kind symbol_kind =
3546 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3547 /* Only check the symbol attributes if they're present.
3548 Indices prior to version 7 don't record them,
3549 and indices >= 7 may elide them for certain symbols
3550 (gold does this). */
3551 int attrs_valid =
976ca316 3552 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3553 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3554
3190f0c6 3555 /* Don't crash on bad data. */
976ca316
SM
3556 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3557 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3558 {
b98664d3 3559 complaint (_(".gdb_index entry has bad CU index"
976ca316 3560 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3561 continue;
3562 }
3563
976ca316 3564 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3565
da51c347 3566 /* Skip if already read in. */
976ca316 3567 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3568 continue;
3569
8943b874
DE
3570 /* Check static vs global. */
3571 if (attrs_valid)
3572 {
2b79f376
SM
3573 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3574
3575 if (iter->block_index.has_value ())
3576 {
3577 bool want_static = *iter->block_index == STATIC_BLOCK;
3578
3579 if (is_static != want_static)
3580 continue;
3581 }
3582
8943b874 3583 /* Work around gold/15646. */
f030440d
TV
3584 if (!is_static
3585 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3586 {
3587 if (iter->global_seen)
3588 continue;
3589
3590 iter->global_seen = 1;
3591 }
8943b874 3592 }
da51c347
DE
3593
3594 /* Only check the symbol's kind if it has one. */
3595 if (attrs_valid)
3596 {
3597 switch (iter->domain)
3598 {
3599 case VAR_DOMAIN:
3600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3601 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3602 /* Some types are also in VAR_DOMAIN. */
3603 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3604 continue;
3605 break;
3606 case STRUCT_DOMAIN:
3607 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3608 continue;
3609 break;
3610 case LABEL_DOMAIN:
3611 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3612 continue;
3613 break;
59c35742
AB
3614 case MODULE_DOMAIN:
3615 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3616 continue;
3617 break;
da51c347
DE
3618 default:
3619 break;
3620 }
3621 }
3622
3623 ++iter->next;
3624 return per_cu;
3625 }
3626
3627 return NULL;
3628}
3629
43f3e411 3630static struct compunit_symtab *
c7f839cb 3631dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3632 const char *name, domain_enum domain)
9291a0cd 3633{
43f3e411 3634 struct compunit_symtab *stab_best = NULL;
976ca316 3635 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3636
b5ec771e
PA
3637 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3638
ed2dc618
SM
3639 struct dw2_symtab_iterator iter;
3640 struct dwarf2_per_cu_data *per_cu;
da51c347 3641
976ca316 3642 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3643
ed2dc618
SM
3644 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3645 {
3646 struct symbol *sym, *with_opaque = NULL;
97a1449a 3647 struct compunit_symtab *stab
976ca316 3648 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3649 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3650 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3651
ed2dc618
SM
3652 sym = block_find_symbol (block, name, domain,
3653 block_find_non_opaque_type_preferred,
3654 &with_opaque);
b2e2f908 3655
ed2dc618
SM
3656 /* Some caution must be observed with overloaded functions
3657 and methods, since the index will not contain any overload
3658 information (but NAME might contain it). */
da51c347 3659
ed2dc618
SM
3660 if (sym != NULL
3661 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3662 return stab;
3663 if (with_opaque != NULL
3664 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3665 stab_best = stab;
da51c347 3666
ed2dc618 3667 /* Keep looking through other CUs. */
9291a0cd 3668 }
9291a0cd 3669
da51c347 3670 return stab_best;
9291a0cd
TT
3671}
3672
3673static void
3674dw2_print_stats (struct objfile *objfile)
3675{
976ca316
SM
3676 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3677 int total = (per_objfile->per_bfd->all_comp_units.size ()
3678 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3679 int count = 0;
9291a0cd 3680
ed2dc618 3681 for (int i = 0; i < total; ++i)
9291a0cd 3682 {
976ca316 3683 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3684
976ca316 3685 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3686 ++count;
3687 }
e4a48d9d 3688 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3689 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3690}
3691
779bd270
DE
3692/* This dumps minimal information about the index.
3693 It is called via "mt print objfiles".
3694 One use is to verify .gdb_index has been loaded by the
3695 gdb.dwarf2/gdb-index.exp testcase. */
3696
9291a0cd
TT
3697static void
3698dw2_dump (struct objfile *objfile)
3699{
976ca316 3700 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3701
976ca316 3702 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3703 printf_filtered (".gdb_index:");
976ca316 3704 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3705 {
3706 printf_filtered (" version %d\n",
976ca316 3707 per_objfile->per_bfd->index_table->version);
779bd270
DE
3708 }
3709 else
3710 printf_filtered (" faked for \"readnow\"\n");
3711 printf_filtered ("\n");
9291a0cd
TT
3712}
3713
9291a0cd
TT
3714static void
3715dw2_expand_symtabs_for_function (struct objfile *objfile,
3716 const char *func_name)
3717{
976ca316 3718 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3719
ed2dc618
SM
3720 struct dw2_symtab_iterator iter;
3721 struct dwarf2_per_cu_data *per_cu;
da51c347 3722
976ca316 3723 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3724
ed2dc618 3725 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3726 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3727
9291a0cd
TT
3728}
3729
3730static void
3731dw2_expand_all_symtabs (struct objfile *objfile)
3732{
976ca316
SM
3733 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3734 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3735 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3736
ed2dc618 3737 for (int i = 0; i < total_units; ++i)
9291a0cd 3738 {
976ca316 3739 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3740
58f0c718
TT
3741 /* We don't want to directly expand a partial CU, because if we
3742 read it with the wrong language, then assertion failures can
3743 be triggered later on. See PR symtab/23010. So, tell
3744 dw2_instantiate_symtab to skip partial CUs -- any important
3745 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3746 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3747 }
3748}
3749
3750static void
652a8996
JK
3751dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3752 const char *fullname)
9291a0cd 3753{
976ca316 3754 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3755
3756 /* We don't need to consider type units here.
3757 This is only called for examining code, e.g. expand_line_sal.
3758 There can be an order of magnitude (or more) more type units
3759 than comp units, and we avoid them if we can. */
3760
976ca316 3761 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3762 {
3d7bb9d9 3763 /* We only need to look at symtabs not already expanded. */
976ca316 3764 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3765 continue;
3766
976ca316 3767 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3768 if (file_data == NULL)
9291a0cd
TT
3769 continue;
3770
b76e467d 3771 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3772 {
652a8996
JK
3773 const char *this_fullname = file_data->file_names[j];
3774
3775 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3776 {
976ca316 3777 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3778 break;
3779 }
3780 }
3781 }
3782}
3783
9a0bacfb
TV
3784static void
3785dw2_expand_symtabs_matching_symbol
3786 (mapped_index_base &index,
3787 const lookup_name_info &lookup_name_in,
3788 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3789 enum search_domain kind,
fcf23d5b
SM
3790 gdb::function_view<bool (offset_type)> match_callback,
3791 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3792
3793static void
3794dw2_expand_symtabs_matching_one
97a1449a
SM
3795 (dwarf2_per_cu_data *per_cu,
3796 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3797 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3798 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3799
9291a0cd 3800static void
199b4314
TT
3801dw2_map_matching_symbols
3802 (struct objfile *objfile,
b054970d 3803 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3804 int global,
3805 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3806 symbol_compare_ftype *ordered_compare)
9291a0cd 3807{
1aa98955 3808 /* Used for Ada. */
976ca316 3809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3810
9a0bacfb
TV
3811 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3812
976ca316 3813 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3814 {
976ca316 3815 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3816
9a0bacfb
TV
3817 const char *match_name = name.ada ().lookup_name ().c_str ();
3818 auto matcher = [&] (const char *symname)
3819 {
3820 if (ordered_compare == nullptr)
3821 return true;
3822 return ordered_compare (symname, match_name) == 0;
3823 };
3824
3825 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3826 [&] (offset_type namei)
3827 {
3828 struct dw2_symtab_iterator iter;
3829 struct dwarf2_per_cu_data *per_cu;
3830
976ca316 3831 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3832 namei);
9a0bacfb 3833 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3834 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3835 nullptr);
9a0bacfb 3836 return true;
976ca316 3837 }, per_objfile);
9a0bacfb
TV
3838 }
3839 else
3840 {
3841 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3842 proceed assuming all symtabs have been read in. */
3843 }
1aa98955
TV
3844
3845 for (compunit_symtab *cust : objfile->compunits ())
3846 {
3847 const struct block *block;
3848
3849 if (cust == NULL)
3850 continue;
3851 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3852 if (!iterate_over_symbols_terminated (block, name,
3853 domain, callback))
3854 return;
3855 }
9291a0cd
TT
3856}
3857
e1ef7d7a
PA
3858/* Starting from a search name, return the string that finds the upper
3859 bound of all strings that start with SEARCH_NAME in a sorted name
3860 list. Returns the empty string to indicate that the upper bound is
3861 the end of the list. */
3862
3863static std::string
3864make_sort_after_prefix_name (const char *search_name)
3865{
3866 /* When looking to complete "func", we find the upper bound of all
3867 symbols that start with "func" by looking for where we'd insert
3868 the closest string that would follow "func" in lexicographical
3869 order. Usually, that's "func"-with-last-character-incremented,
3870 i.e. "fund". Mind non-ASCII characters, though. Usually those
3871 will be UTF-8 multi-byte sequences, but we can't be certain.
3872 Especially mind the 0xff character, which is a valid character in
3873 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3874 rule out compilers allowing it in identifiers. Note that
3875 conveniently, strcmp/strcasecmp are specified to compare
3876 characters interpreted as unsigned char. So what we do is treat
3877 the whole string as a base 256 number composed of a sequence of
3878 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3879 to 0, and carries 1 to the following more-significant position.
3880 If the very first character in SEARCH_NAME ends up incremented
3881 and carries/overflows, then the upper bound is the end of the
3882 list. The string after the empty string is also the empty
3883 string.
3884
3885 Some examples of this operation:
3886
3887 SEARCH_NAME => "+1" RESULT
3888
3889 "abc" => "abd"
3890 "ab\xff" => "ac"
3891 "\xff" "a" "\xff" => "\xff" "b"
3892 "\xff" => ""
3893 "\xff\xff" => ""
3894 "" => ""
3895
3896 Then, with these symbols for example:
3897
3898 func
3899 func1
3900 fund
3901
3902 completing "func" looks for symbols between "func" and
3903 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3904 which finds "func" and "func1", but not "fund".
3905
3906 And with:
3907
3908 funcÿ (Latin1 'ÿ' [0xff])
3909 funcÿ1
3910 fund
3911
3912 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3913 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3914
3915 And with:
3916
3917 ÿÿ (Latin1 'ÿ' [0xff])
3918 ÿÿ1
3919
3920 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3921 the end of the list.
3922 */
3923 std::string after = search_name;
3924 while (!after.empty () && (unsigned char) after.back () == 0xff)
3925 after.pop_back ();
3926 if (!after.empty ())
3927 after.back () = (unsigned char) after.back () + 1;
3928 return after;
3929}
3930
5c58de74 3931/* See declaration. */
61d96d7e 3932
5c58de74
PA
3933std::pair<std::vector<name_component>::const_iterator,
3934 std::vector<name_component>::const_iterator>
44ed8f3e 3935mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3936 (const lookup_name_info &lookup_name_without_params, language lang,
3937 dwarf2_per_objfile *per_objfile) const
3f563c84 3938{
5c58de74
PA
3939 auto *name_cmp
3940 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3941
3b00ef10 3942 const char *lang_name
e0802d59 3943 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3944
3f563c84
PA
3945 /* Comparison function object for lower_bound that matches against a
3946 given symbol name. */
3947 auto lookup_compare_lower = [&] (const name_component &elem,
3948 const char *name)
3949 {
fcf23d5b 3950 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3951 const char *elem_name = elem_qualified + elem.name_offset;
3952 return name_cmp (elem_name, name) < 0;
3953 };
3954
3955 /* Comparison function object for upper_bound that matches against a
3956 given symbol name. */
3957 auto lookup_compare_upper = [&] (const char *name,
3958 const name_component &elem)
3959 {
fcf23d5b 3960 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3961 const char *elem_name = elem_qualified + elem.name_offset;
3962 return name_cmp (name, elem_name) < 0;
3963 };
3964
5c58de74
PA
3965 auto begin = this->name_components.begin ();
3966 auto end = this->name_components.end ();
3f563c84
PA
3967
3968 /* Find the lower bound. */
3969 auto lower = [&] ()
3970 {
3b00ef10 3971 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3972 return begin;
3973 else
3b00ef10 3974 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3975 } ();
3976
3977 /* Find the upper bound. */
3978 auto upper = [&] ()
3979 {
5c58de74 3980 if (lookup_name_without_params.completion_mode ())
3f563c84 3981 {
e1ef7d7a
PA
3982 /* In completion mode, we want UPPER to point past all
3983 symbols names that have the same prefix. I.e., with
3984 these symbols, and completing "func":
3985
3986 function << lower bound
3987 function1
3988 other_function << upper bound
3989
3990 We find the upper bound by looking for the insertion
3991 point of "func"-with-last-character-incremented,
3992 i.e. "fund". */
3b00ef10 3993 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3994 if (after.empty ())
3f563c84 3995 return end;
e6b2f5ef
PA
3996 return std::lower_bound (lower, end, after.c_str (),
3997 lookup_compare_lower);
3f563c84
PA
3998 }
3999 else
3b00ef10 4000 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4001 } ();
4002
5c58de74
PA
4003 return {lower, upper};
4004}
4005
4006/* See declaration. */
4007
4008void
fcf23d5b 4009mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4010{
4011 if (!this->name_components.empty ())
4012 return;
4013
4014 this->name_components_casing = case_sensitivity;
4015 auto *name_cmp
4016 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4017
4018 /* The code below only knows how to break apart components of C++
4019 symbol names (and other languages that use '::' as
3b00ef10 4020 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4021 auto count = this->symbol_name_count ();
4022 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4023 {
44ed8f3e 4024 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4025 continue;
4026
fcf23d5b 4027 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4028
4029 /* Add each name component to the name component table. */
4030 unsigned int previous_len = 0;
3b00ef10
TT
4031
4032 if (strstr (name, "::") != nullptr)
4033 {
4034 for (unsigned int current_len = cp_find_first_component (name);
4035 name[current_len] != '\0';
4036 current_len += cp_find_first_component (name + current_len))
4037 {
4038 gdb_assert (name[current_len] == ':');
4039 this->name_components.push_back ({previous_len, idx});
4040 /* Skip the '::'. */
4041 current_len += 2;
4042 previous_len = current_len;
4043 }
4044 }
4045 else
5c58de74 4046 {
3b00ef10
TT
4047 /* Handle the Ada encoded (aka mangled) form here. */
4048 for (const char *iter = strstr (name, "__");
4049 iter != nullptr;
4050 iter = strstr (iter, "__"))
4051 {
4052 this->name_components.push_back ({previous_len, idx});
4053 iter += 2;
4054 previous_len = iter - name;
4055 }
5c58de74 4056 }
3b00ef10 4057
5c58de74
PA
4058 this->name_components.push_back ({previous_len, idx});
4059 }
4060
4061 /* Sort name_components elements by name. */
4062 auto name_comp_compare = [&] (const name_component &left,
4063 const name_component &right)
4064 {
fcf23d5b
SM
4065 const char *left_qualified
4066 = this->symbol_name_at (left.idx, per_objfile);
4067 const char *right_qualified
4068 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4069
4070 const char *left_name = left_qualified + left.name_offset;
4071 const char *right_name = right_qualified + right.name_offset;
4072
4073 return name_cmp (left_name, right_name) < 0;
4074 };
4075
4076 std::sort (this->name_components.begin (),
4077 this->name_components.end (),
4078 name_comp_compare);
4079}
4080
4081/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4082 mapped_index_base instead of the containing objfile. This is split
4083 to a separate function in order to be able to unit test the
4084 name_components matching using a mock mapped_index_base. For each
5c58de74 4085 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4086 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4087
4088static void
4089dw2_expand_symtabs_matching_symbol
44ed8f3e 4090 (mapped_index_base &index,
5c58de74
PA
4091 const lookup_name_info &lookup_name_in,
4092 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4093 enum search_domain kind,
fcf23d5b
SM
4094 gdb::function_view<bool (offset_type)> match_callback,
4095 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4096{
4097 lookup_name_info lookup_name_without_params
4098 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4099
4100 /* Build the symbol name component sorted vector, if we haven't
4101 yet. */
fcf23d5b 4102 index.build_name_components (per_objfile);
5c58de74 4103
3f563c84
PA
4104 /* The same symbol may appear more than once in the range though.
4105 E.g., if we're looking for symbols that complete "w", and we have
4106 a symbol named "w1::w2", we'll find the two name components for
4107 that same symbol in the range. To be sure we only call the
4108 callback once per symbol, we first collect the symbol name
4109 indexes that matched in a temporary vector and ignore
4110 duplicates. */
4111 std::vector<offset_type> matches;
3f563c84 4112
3b00ef10
TT
4113 struct name_and_matcher
4114 {
4115 symbol_name_matcher_ftype *matcher;
ecc6c606 4116 const char *name;
3b00ef10
TT
4117
4118 bool operator== (const name_and_matcher &other) const
3f563c84 4119 {
ecc6c606 4120 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4121 }
4122 };
4123
4124 /* A vector holding all the different symbol name matchers, for all
4125 languages. */
4126 std::vector<name_and_matcher> matchers;
4127
4128 for (int i = 0; i < nr_languages; i++)
4129 {
4130 enum language lang_e = (enum language) i;
4131
4132 const language_defn *lang = language_def (lang_e);
4133 symbol_name_matcher_ftype *name_matcher
c9debfb9 4134 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4135
3b00ef10
TT
4136 name_and_matcher key {
4137 name_matcher,
4138 lookup_name_without_params.language_lookup_name (lang_e)
4139 };
4140
4141 /* Don't insert the same comparison routine more than once.
4142 Note that we do this linear walk. This is not a problem in
4143 practice because the number of supported languages is
4144 low. */
4145 if (std::find (matchers.begin (), matchers.end (), key)
4146 != matchers.end ())
9291a0cd 4147 continue;
3b00ef10
TT
4148 matchers.push_back (std::move (key));
4149
4150 auto bounds
4151 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4152 lang_e, per_objfile);
3b00ef10
TT
4153
4154 /* Now for each symbol name in range, check to see if we have a name
4155 match, and if so, call the MATCH_CALLBACK callback. */
4156
4157 for (; bounds.first != bounds.second; ++bounds.first)
4158 {
fcf23d5b
SM
4159 const char *qualified
4160 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4161
4162 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4163 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4164 continue;
9291a0cd 4165
3b00ef10
TT
4166 matches.push_back (bounds.first->idx);
4167 }
3f563c84
PA
4168 }
4169
4170 std::sort (matches.begin (), matches.end ());
4171
4172 /* Finally call the callback, once per match. */
4173 ULONGEST prev = -1;
4174 for (offset_type idx : matches)
4175 {
4176 if (prev != idx)
4177 {
3b00ef10
TT
4178 if (!match_callback (idx))
4179 break;
3f563c84
PA
4180 prev = idx;
4181 }
4182 }
4183
4184 /* Above we use a type wider than idx's for 'prev', since 0 and
4185 (offset_type)-1 are both possible values. */
4186 static_assert (sizeof (prev) > sizeof (offset_type), "");
4187}
4188
c62446b1
PA
4189#if GDB_SELF_TEST
4190
4191namespace selftests { namespace dw2_expand_symtabs_matching {
4192
a3c5fafd
PA
4193/* A mock .gdb_index/.debug_names-like name index table, enough to
4194 exercise dw2_expand_symtabs_matching_symbol, which works with the
4195 mapped_index_base interface. Builds an index from the symbol list
4196 passed as parameter to the constructor. */
4197class mock_mapped_index : public mapped_index_base
c62446b1
PA
4198{
4199public:
a3c5fafd
PA
4200 mock_mapped_index (gdb::array_view<const char *> symbols)
4201 : m_symbol_table (symbols)
c62446b1
PA
4202 {}
4203
a3c5fafd 4204 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4205
a3c5fafd 4206 /* Return the number of names in the symbol table. */
632e107b 4207 size_t symbol_name_count () const override
c62446b1 4208 {
a3c5fafd 4209 return m_symbol_table.size ();
c62446b1
PA
4210 }
4211
a3c5fafd 4212 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4213 const char *symbol_name_at
4214 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4215 {
4216 return m_symbol_table[idx];
4217 }
c62446b1 4218
a3c5fafd
PA
4219private:
4220 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4221};
4222
4223/* Convenience function that converts a NULL pointer to a "<null>"
4224 string, to pass to print routines. */
4225
4226static const char *
4227string_or_null (const char *str)
4228{
4229 return str != NULL ? str : "<null>";
4230}
4231
4232/* Check if a lookup_name_info built from
4233 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4234 index. EXPECTED_LIST is the list of expected matches, in expected
4235 matching order. If no match expected, then an empty list is
4236 specified. Returns true on success. On failure prints a warning
4237 indicating the file:line that failed, and returns false. */
4238
4239static bool
4240check_match (const char *file, int line,
4241 mock_mapped_index &mock_index,
4242 const char *name, symbol_name_match_type match_type,
4243 bool completion_mode,
fcf23d5b
SM
4244 std::initializer_list<const char *> expected_list,
4245 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4246{
4247 lookup_name_info lookup_name (name, match_type, completion_mode);
4248
4249 bool matched = true;
4250
4251 auto mismatch = [&] (const char *expected_str,
4252 const char *got)
4253 {
4254 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4255 "expected=\"%s\", got=\"%s\"\n"),
4256 file, line,
4257 (match_type == symbol_name_match_type::FULL
4258 ? "FULL" : "WILD"),
4259 name, string_or_null (expected_str), string_or_null (got));
4260 matched = false;
4261 };
4262
4263 auto expected_it = expected_list.begin ();
4264 auto expected_end = expected_list.end ();
4265
a3c5fafd 4266 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4267 NULL, ALL_DOMAIN,
4268 [&] (offset_type idx)
4269 {
fcf23d5b 4270 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4271 const char *expected_str
4272 = expected_it == expected_end ? NULL : *expected_it++;
4273
4274 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4275 mismatch (expected_str, matched_name);
3b00ef10 4276 return true;
fcf23d5b 4277 }, per_objfile);
c62446b1
PA
4278
4279 const char *expected_str
4280 = expected_it == expected_end ? NULL : *expected_it++;
4281 if (expected_str != NULL)
4282 mismatch (expected_str, NULL);
4283
4284 return matched;
4285}
4286
4287/* The symbols added to the mock mapped_index for testing (in
4288 canonical form). */
4289static const char *test_symbols[] = {
4290 "function",
4291 "std::bar",
4292 "std::zfunction",
4293 "std::zfunction2",
4294 "w1::w2",
4295 "ns::foo<char*>",
4296 "ns::foo<int>",
4297 "ns::foo<long>",
a20714ff
PA
4298 "ns2::tmpl<int>::foo2",
4299 "(anonymous namespace)::A::B::C",
c62446b1 4300
e1ef7d7a
PA
4301 /* These are used to check that the increment-last-char in the
4302 matching algorithm for completion doesn't match "t1_fund" when
4303 completing "t1_func". */
4304 "t1_func",
4305 "t1_func1",
4306 "t1_fund",
4307 "t1_fund1",
4308
4309 /* A UTF-8 name with multi-byte sequences to make sure that
4310 cp-name-parser understands this as a single identifier ("função"
4311 is "function" in PT). */
4312 u8"u8função",
4313
4314 /* \377 (0xff) is Latin1 'ÿ'. */
4315 "yfunc\377",
4316
4317 /* \377 (0xff) is Latin1 'ÿ'. */
4318 "\377",
4319 "\377\377123",
4320
c62446b1
PA
4321 /* A name with all sorts of complications. Starts with "z" to make
4322 it easier for the completion tests below. */
4323#define Z_SYM_NAME \
4324 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4325 "::tuple<(anonymous namespace)::ui*, " \
4326 "std::default_delete<(anonymous namespace)::ui>, void>"
4327
4328 Z_SYM_NAME
4329};
4330
a3c5fafd
PA
4331/* Returns true if the mapped_index_base::find_name_component_bounds
4332 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4333 in completion mode. */
5c58de74
PA
4334
4335static bool
a3c5fafd 4336check_find_bounds_finds (mapped_index_base &index,
5c58de74 4337 const char *search_name,
fcf23d5b
SM
4338 gdb::array_view<const char *> expected_syms,
4339 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4340{
4341 lookup_name_info lookup_name (search_name,
4342 symbol_name_match_type::FULL, true);
4343
3b00ef10 4344 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4345 language_cplus,
4346 per_objfile);
5c58de74
PA
4347
4348 size_t distance = std::distance (bounds.first, bounds.second);
4349 if (distance != expected_syms.size ())
4350 return false;
4351
4352 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4353 {
4354 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4355 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4356 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4357 return false;
4358 }
4359
4360 return true;
4361}
4362
4363/* Test the lower-level mapped_index::find_name_component_bounds
4364 method. */
4365
c62446b1 4366static void
5c58de74
PA
4367test_mapped_index_find_name_component_bounds ()
4368{
4369 mock_mapped_index mock_index (test_symbols);
4370
fcf23d5b 4371 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4372
4373 /* Test the lower-level mapped_index::find_name_component_bounds
4374 method in completion mode. */
4375 {
4376 static const char *expected_syms[] = {
4377 "t1_func",
4378 "t1_func1",
5c58de74
PA
4379 };
4380
fcf23d5b
SM
4381 SELF_CHECK (check_find_bounds_finds
4382 (mock_index, "t1_func", expected_syms,
4383 NULL /* per_objfile */));
5c58de74
PA
4384 }
4385
4386 /* Check that the increment-last-char in the name matching algorithm
4387 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4388 {
4389 static const char *expected_syms1[] = {
4390 "\377",
4391 "\377\377123",
4392 };
fcf23d5b
SM
4393 SELF_CHECK (check_find_bounds_finds
4394 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4395
4396 static const char *expected_syms2[] = {
4397 "\377\377123",
4398 };
fcf23d5b
SM
4399 SELF_CHECK (check_find_bounds_finds
4400 (mock_index, "\377\377", expected_syms2,
4401 NULL /* per_objfile */));
5c58de74
PA
4402 }
4403}
4404
4405/* Test dw2_expand_symtabs_matching_symbol. */
4406
4407static void
4408test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4409{
4410 mock_mapped_index mock_index (test_symbols);
4411
4412 /* We let all tests run until the end even if some fails, for debug
4413 convenience. */
4414 bool any_mismatch = false;
4415
4416 /* Create the expected symbols list (an initializer_list). Needed
4417 because lists have commas, and we need to pass them to CHECK,
4418 which is a macro. */
4419#define EXPECT(...) { __VA_ARGS__ }
4420
4421 /* Wrapper for check_match that passes down the current
4422 __FILE__/__LINE__. */
4423#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4424 any_mismatch |= !check_match (__FILE__, __LINE__, \
4425 mock_index, \
4426 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4427 EXPECTED_LIST, NULL)
c62446b1
PA
4428
4429 /* Identity checks. */
4430 for (const char *sym : test_symbols)
4431 {
4432 /* Should be able to match all existing symbols. */
4433 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4434 EXPECT (sym));
4435
4436 /* Should be able to match all existing symbols with
4437 parameters. */
4438 std::string with_params = std::string (sym) + "(int)";
4439 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4440 EXPECT (sym));
4441
4442 /* Should be able to match all existing symbols with
4443 parameters and qualifiers. */
4444 with_params = std::string (sym) + " ( int ) const";
4445 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4446 EXPECT (sym));
4447
4448 /* This should really find sym, but cp-name-parser.y doesn't
4449 know about lvalue/rvalue qualifiers yet. */
4450 with_params = std::string (sym) + " ( int ) &&";
4451 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4452 {});
4453 }
4454
e1ef7d7a
PA
4455 /* Check that the name matching algorithm for completion doesn't get
4456 confused with Latin1 'ÿ' / 0xff. */
4457 {
4458 static const char str[] = "\377";
4459 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4460 EXPECT ("\377", "\377\377123"));
4461 }
4462
4463 /* Check that the increment-last-char in the matching algorithm for
4464 completion doesn't match "t1_fund" when completing "t1_func". */
4465 {
4466 static const char str[] = "t1_func";
4467 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4468 EXPECT ("t1_func", "t1_func1"));
4469 }
4470
c62446b1
PA
4471 /* Check that completion mode works at each prefix of the expected
4472 symbol name. */
4473 {
4474 static const char str[] = "function(int)";
4475 size_t len = strlen (str);
4476 std::string lookup;
4477
4478 for (size_t i = 1; i < len; i++)
4479 {
4480 lookup.assign (str, i);
4481 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4482 EXPECT ("function"));
4483 }
4484 }
4485
4486 /* While "w" is a prefix of both components, the match function
4487 should still only be called once. */
4488 {
4489 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4490 EXPECT ("w1::w2"));
a20714ff
PA
4491 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4492 EXPECT ("w1::w2"));
c62446b1
PA
4493 }
4494
4495 /* Same, with a "complicated" symbol. */
4496 {
4497 static const char str[] = Z_SYM_NAME;
4498 size_t len = strlen (str);
4499 std::string lookup;
4500
4501 for (size_t i = 1; i < len; i++)
4502 {
4503 lookup.assign (str, i);
4504 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4505 EXPECT (Z_SYM_NAME));
4506 }
4507 }
4508
4509 /* In FULL mode, an incomplete symbol doesn't match. */
4510 {
4511 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4512 {});
4513 }
4514
4515 /* A complete symbol with parameters matches any overload, since the
4516 index has no overload info. */
4517 {
4518 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4519 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4520 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4521 EXPECT ("std::zfunction", "std::zfunction2"));
4522 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4523 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4524 }
4525
4526 /* Check that whitespace is ignored appropriately. A symbol with a
4527 template argument list. */
4528 {
4529 static const char expected[] = "ns::foo<int>";
4530 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4531 EXPECT (expected));
a20714ff
PA
4532 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4533 EXPECT (expected));
c62446b1
PA
4534 }
4535
4536 /* Check that whitespace is ignored appropriately. A symbol with a
4537 template argument list that includes a pointer. */
4538 {
4539 static const char expected[] = "ns::foo<char*>";
4540 /* Try both completion and non-completion modes. */
4541 static const bool completion_mode[2] = {false, true};
4542 for (size_t i = 0; i < 2; i++)
4543 {
4544 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4545 completion_mode[i], EXPECT (expected));
a20714ff
PA
4546 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4547 completion_mode[i], EXPECT (expected));
c62446b1
PA
4548
4549 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4550 completion_mode[i], EXPECT (expected));
a20714ff
PA
4551 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4552 completion_mode[i], EXPECT (expected));
c62446b1
PA
4553 }
4554 }
4555
4556 {
4557 /* Check method qualifiers are ignored. */
4558 static const char expected[] = "ns::foo<char*>";
4559 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4560 symbol_name_match_type::FULL, true, EXPECT (expected));
4561 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4562 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4563 CHECK_MATCH ("foo < char * > ( int ) const",
4564 symbol_name_match_type::WILD, true, EXPECT (expected));
4565 CHECK_MATCH ("foo < char * > ( int ) &&",
4566 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4567 }
4568
4569 /* Test lookup names that don't match anything. */
4570 {
a20714ff
PA
4571 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4572 {});
4573
c62446b1
PA
4574 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4575 {});
4576 }
4577
a20714ff
PA
4578 /* Some wild matching tests, exercising "(anonymous namespace)",
4579 which should not be confused with a parameter list. */
4580 {
4581 static const char *syms[] = {
4582 "A::B::C",
4583 "B::C",
4584 "C",
4585 "A :: B :: C ( int )",
4586 "B :: C ( int )",
4587 "C ( int )",
4588 };
4589
4590 for (const char *s : syms)
4591 {
4592 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4593 EXPECT ("(anonymous namespace)::A::B::C"));
4594 }
4595 }
4596
4597 {
4598 static const char expected[] = "ns2::tmpl<int>::foo2";
4599 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4600 EXPECT (expected));
4601 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4602 EXPECT (expected));
4603 }
4604
c62446b1
PA
4605 SELF_CHECK (!any_mismatch);
4606
4607#undef EXPECT
4608#undef CHECK_MATCH
4609}
4610
5c58de74
PA
4611static void
4612run_test ()
4613{
4614 test_mapped_index_find_name_component_bounds ();
4615 test_dw2_expand_symtabs_matching_symbol ();
4616}
4617
c62446b1
PA
4618}} // namespace selftests::dw2_expand_symtabs_matching
4619
4620#endif /* GDB_SELF_TEST */
4621
4b514bc8
JK
4622/* If FILE_MATCHER is NULL or if PER_CU has
4623 dwarf2_per_cu_quick_data::MARK set (see
4624 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4625 EXPANSION_NOTIFY on it. */
4626
4627static void
4628dw2_expand_symtabs_matching_one
97a1449a
SM
4629 (dwarf2_per_cu_data *per_cu,
4630 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4631 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4632 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4633{
4634 if (file_matcher == NULL || per_cu->v.quick->mark)
4635 {
af758d11 4636 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4637
97a1449a
SM
4638 compunit_symtab *symtab
4639 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4640 gdb_assert (symtab != nullptr);
4b514bc8 4641
af758d11
SM
4642 if (expansion_notify != NULL && symtab_was_null)
4643 expansion_notify (symtab);
4b514bc8
JK
4644 }
4645}
4646
3f563c84
PA
4647/* Helper for dw2_expand_matching symtabs. Called on each symbol
4648 matched, to expand corresponding CUs that were marked. IDX is the
4649 index of the symbol name that matched. */
4650
4651static void
4652dw2_expand_marked_cus
976ca316 4653 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4654 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4655 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4656 search_domain kind)
4657{
3f563c84
PA
4658 offset_type *vec, vec_len, vec_idx;
4659 bool global_seen = false;
976ca316 4660 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4661
61920122 4662 vec = (offset_type *) (index.constant_pool
f00a2de2 4663 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4664 vec_len = MAYBE_SWAP (vec[0]);
4665 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4666 {
61920122
PA
4667 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4668 /* This value is only valid for index versions >= 7. */
4669 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4670 gdb_index_symbol_kind symbol_kind =
4671 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4672 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4673 /* Only check the symbol attributes if they're present.
4674 Indices prior to version 7 don't record them,
4675 and indices >= 7 may elide them for certain symbols
4676 (gold does this). */
4677 int attrs_valid =
4678 (index.version >= 7
4679 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4680
4681 /* Work around gold/15646. */
f030440d
TV
4682 if (attrs_valid
4683 && !is_static
4684 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4685 {
f030440d 4686 if (global_seen)
61920122 4687 continue;
f030440d
TV
4688
4689 global_seen = true;
61920122 4690 }
3190f0c6 4691
61920122
PA
4692 /* Only check the symbol's kind if it has one. */
4693 if (attrs_valid)
4694 {
4695 switch (kind)
8943b874 4696 {
61920122
PA
4697 case VARIABLES_DOMAIN:
4698 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4699 continue;
4700 break;
4701 case FUNCTIONS_DOMAIN:
4702 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4703 continue;
61920122
PA
4704 break;
4705 case TYPES_DOMAIN:
4706 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4707 continue;
4708 break;
59c35742
AB
4709 case MODULES_DOMAIN:
4710 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4711 continue;
4712 break;
61920122
PA
4713 default:
4714 break;
8943b874 4715 }
61920122 4716 }
8943b874 4717
61920122 4718 /* Don't crash on bad data. */
976ca316
SM
4719 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4720 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4721 {
b98664d3 4722 complaint (_(".gdb_index entry has bad CU index"
976ca316 4723 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4724 continue;
4725 }
4726
976ca316
SM
4727 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4728 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4729 expansion_notify);
61920122
PA
4730 }
4731}
4732
4b514bc8
JK
4733/* If FILE_MATCHER is non-NULL, set all the
4734 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4735 that match FILE_MATCHER. */
4736
61920122 4737static void
4b514bc8 4738dw_expand_symtabs_matching_file_matcher
976ca316 4739 (dwarf2_per_objfile *per_objfile,
ed2dc618 4740 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4741{
4b514bc8 4742 if (file_matcher == NULL)
61920122
PA
4743 return;
4744
4b514bc8
JK
4745 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4746 htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4749 htab_eq_pointer,
4750 NULL, xcalloc, xfree));
61920122 4751
4b514bc8
JK
4752 /* The rule is CUs specify all the files, including those used by
4753 any TU, so there's no need to scan TUs here. */
61920122 4754
976ca316 4755 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4756 {
927aa2e7
JK
4757 QUIT;
4758
4759 per_cu->v.quick->mark = 0;
4760
4761 /* We only need to look at symtabs not already expanded. */
976ca316 4762 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4763 continue;
4764
976ca316 4765 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4766 if (file_data == NULL)
4767 continue;
4768
4769 if (htab_find (visited_not_found.get (), file_data) != NULL)
4770 continue;
4771 else if (htab_find (visited_found.get (), file_data) != NULL)
4772 {
4773 per_cu->v.quick->mark = 1;
4774 continue;
4775 }
4776
b76e467d 4777 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4778 {
4779 const char *this_real_name;
4780
4781 if (file_matcher (file_data->file_names[j], false))
4782 {
4783 per_cu->v.quick->mark = 1;
4784 break;
4785 }
4786
4787 /* Before we invoke realpath, which can get expensive when many
4788 files are involved, do a quick comparison of the basenames. */
4789 if (!basenames_may_differ
4790 && !file_matcher (lbasename (file_data->file_names[j]),
4791 true))
4792 continue;
4793
976ca316 4794 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4795 if (file_matcher (this_real_name, false))
4796 {
4797 per_cu->v.quick->mark = 1;
4798 break;
4799 }
4800 }
4801
b76e467d
SM
4802 void **slot = htab_find_slot (per_cu->v.quick->mark
4803 ? visited_found.get ()
4804 : visited_not_found.get (),
4805 file_data, INSERT);
927aa2e7
JK
4806 *slot = file_data;
4807 }
4808}
4809
4810static void
4811dw2_expand_symtabs_matching
4812 (struct objfile *objfile,
4813 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4814 const lookup_name_info *lookup_name,
927aa2e7
JK
4815 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4816 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4817 enum search_domain kind)
4818{
976ca316 4819 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4820
4821 /* index_table is NULL if OBJF_READNOW. */
976ca316 4822 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4823 return;
4824
976ca316 4825 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4826
c1a66c06
TV
4827 if (symbol_matcher == NULL && lookup_name == NULL)
4828 {
976ca316 4829 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4830 {
4831 QUIT;
4832
976ca316 4833 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4834 file_matcher, expansion_notify);
c1a66c06
TV
4835 }
4836 return;
4837 }
4838
976ca316 4839 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4840
c1a66c06 4841 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4842 symbol_matcher,
4843 kind, [&] (offset_type idx)
4844 {
976ca316
SM
4845 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4846 kind);
3b00ef10 4847 return true;
976ca316 4848 }, per_objfile);
927aa2e7
JK
4849}
4850
4851/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4852 symtab. */
4853
4854static struct compunit_symtab *
4855recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4856 CORE_ADDR pc)
4857{
4858 int i;
4859
4860 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4861 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4862 return cust;
4863
4864 if (cust->includes == NULL)
4865 return NULL;
4866
4867 for (i = 0; cust->includes[i]; ++i)
4868 {
4869 struct compunit_symtab *s = cust->includes[i];
4870
4871 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4872 if (s != NULL)
4873 return s;
4874 }
4875
4876 return NULL;
4877}
4878
4879static struct compunit_symtab *
4880dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4881 struct bound_minimal_symbol msymbol,
4882 CORE_ADDR pc,
4883 struct obj_section *section,
4884 int warn_if_readin)
4885{
4886 struct dwarf2_per_cu_data *data;
4887 struct compunit_symtab *result;
4888
d320c2b5 4889 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4890 return NULL;
4891
b3b3bada 4892 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4893 data = (struct dwarf2_per_cu_data *) addrmap_find
4894 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4895 if (!data)
4896 return NULL;
4897
af758d11
SM
4898 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4899 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4900 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4901 paddress (objfile->arch (), pc));
927aa2e7 4902
97a1449a
SM
4903 result = recursively_find_pc_sect_compunit_symtab
4904 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4905
927aa2e7
JK
4906 gdb_assert (result != NULL);
4907 return result;
4908}
4909
4910static void
4911dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4912 void *data, int need_fullname)
4913{
976ca316 4914 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4915
976ca316 4916 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4917 {
976ca316 4918 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4919
4920 htab_up visited (htab_create_alloc (10,
4921 htab_hash_pointer, htab_eq_pointer,
4922 NULL, xcalloc, xfree));
4923
4924 /* The rule is CUs specify all the files, including those used
4925 by any TU, so there's no need to scan TUs here. We can
4926 ignore file names coming from already-expanded CUs. */
4927
976ca316 4928 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4929 {
976ca316 4930 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4931 {
4932 void **slot = htab_find_slot (visited.get (),
4933 per_cu->v.quick->file_names,
4934 INSERT);
4935
4936 *slot = per_cu->v.quick->file_names;
4937 }
4938 }
4939
976ca316 4940 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4941 {
927aa2e7 4942 /* We only need to look at symtabs not already expanded. */
976ca316 4943 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4944 continue;
4945
ab432490 4946 quick_file_names *file_data
976ca316 4947 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4948 if (file_data == NULL)
4949 continue;
4950
b76e467d 4951 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4952 if (*slot)
4953 {
4954 /* Already visited. */
4955 continue;
4956 }
4957 *slot = file_data;
4958
4959 for (int j = 0; j < file_data->num_file_names; ++j)
4960 {
4961 const char *filename = file_data->file_names[j];
976ca316 4962 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4963 }
4964 }
4965 }
4966
976ca316 4967 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4968 {
4969 gdb::unique_xmalloc_ptr<char> this_real_name;
4970
4971 if (need_fullname)
4972 this_real_name = gdb_realpath (filename);
4973 (*fun) (filename, this_real_name.get (), data);
4974 });
4975}
4976
4977static int
4978dw2_has_symbols (struct objfile *objfile)
4979{
4980 return 1;
4981}
4982
4983const struct quick_symbol_functions dwarf2_gdb_index_functions =
4984{
4985 dw2_has_symbols,
4986 dw2_find_last_source_symtab,
4987 dw2_forget_cached_source_info,
4988 dw2_map_symtabs_matching_filename,
4989 dw2_lookup_symbol,
d3214198 4990 NULL,
927aa2e7
JK
4991 dw2_print_stats,
4992 dw2_dump,
927aa2e7
JK
4993 dw2_expand_symtabs_for_function,
4994 dw2_expand_all_symtabs,
4995 dw2_expand_symtabs_with_fullname,
4996 dw2_map_matching_symbols,
4997 dw2_expand_symtabs_matching,
4998 dw2_find_pc_sect_compunit_symtab,
4999 NULL,
5000 dw2_map_symbol_filenames
5001};
5002
5003/* DWARF-5 debug_names reader. */
5004
5005/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5006static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5007
5008/* A helper function that reads the .debug_names section in SECTION
5009 and fills in MAP. FILENAME is the name of the file containing the
5010 section; it is used for error reporting.
5011
5012 Returns true if all went well, false otherwise. */
5013
5014static bool
5015read_debug_names_from_section (struct objfile *objfile,
5016 const char *filename,
5017 struct dwarf2_section_info *section,
5018 mapped_debug_names &map)
5019{
96b79293 5020 if (section->empty ())
927aa2e7
JK
5021 return false;
5022
5023 /* Older elfutils strip versions could keep the section in the main
5024 executable while splitting it for the separate debug info file. */
96b79293 5025 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5026 return false;
5027
96b79293 5028 section->read (objfile);
927aa2e7 5029
08feed99 5030 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5031
5032 const gdb_byte *addr = section->buffer;
5033
96b79293 5034 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5035
5036 unsigned int bytes_read;
5037 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5038 addr += bytes_read;
5039
5040 map.dwarf5_is_dwarf64 = bytes_read != 4;
5041 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5042 if (bytes_read + length != section->size)
5043 {
5044 /* There may be multiple per-CU indices. */
5045 warning (_("Section .debug_names in %s length %s does not match "
5046 "section length %s, ignoring .debug_names."),
5047 filename, plongest (bytes_read + length),
5048 pulongest (section->size));
5049 return false;
5050 }
5051
5052 /* The version number. */
5053 uint16_t version = read_2_bytes (abfd, addr);
5054 addr += 2;
5055 if (version != 5)
5056 {
5057 warning (_("Section .debug_names in %s has unsupported version %d, "
5058 "ignoring .debug_names."),
5059 filename, version);
5060 return false;
5061 }
5062
5063 /* Padding. */
5064 uint16_t padding = read_2_bytes (abfd, addr);
5065 addr += 2;
5066 if (padding != 0)
5067 {
5068 warning (_("Section .debug_names in %s has unsupported padding %d, "
5069 "ignoring .debug_names."),
5070 filename, padding);
5071 return false;
5072 }
5073
5074 /* comp_unit_count - The number of CUs in the CU list. */
5075 map.cu_count = read_4_bytes (abfd, addr);
5076 addr += 4;
5077
5078 /* local_type_unit_count - The number of TUs in the local TU
5079 list. */
5080 map.tu_count = read_4_bytes (abfd, addr);
5081 addr += 4;
5082
5083 /* foreign_type_unit_count - The number of TUs in the foreign TU
5084 list. */
5085 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5086 addr += 4;
5087 if (foreign_tu_count != 0)
5088 {
5089 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5090 "ignoring .debug_names."),
5091 filename, static_cast<unsigned long> (foreign_tu_count));
5092 return false;
5093 }
5094
5095 /* bucket_count - The number of hash buckets in the hash lookup
5096 table. */
5097 map.bucket_count = read_4_bytes (abfd, addr);
5098 addr += 4;
5099
5100 /* name_count - The number of unique names in the index. */
5101 map.name_count = read_4_bytes (abfd, addr);
5102 addr += 4;
5103
5104 /* abbrev_table_size - The size in bytes of the abbreviations
5105 table. */
5106 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5107 addr += 4;
5108
5109 /* augmentation_string_size - The size in bytes of the augmentation
5110 string. This value is rounded up to a multiple of 4. */
5111 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5112 addr += 4;
5113 map.augmentation_is_gdb = ((augmentation_string_size
5114 == sizeof (dwarf5_augmentation))
5115 && memcmp (addr, dwarf5_augmentation,
5116 sizeof (dwarf5_augmentation)) == 0);
5117 augmentation_string_size += (-augmentation_string_size) & 3;
5118 addr += augmentation_string_size;
5119
5120 /* List of CUs */
5121 map.cu_table_reordered = addr;
5122 addr += map.cu_count * map.offset_size;
5123
5124 /* List of Local TUs */
5125 map.tu_table_reordered = addr;
5126 addr += map.tu_count * map.offset_size;
5127
5128 /* Hash Lookup Table */
5129 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5130 addr += map.bucket_count * 4;
5131 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5132 addr += map.name_count * 4;
5133
5134 /* Name Table */
5135 map.name_table_string_offs_reordered = addr;
5136 addr += map.name_count * map.offset_size;
5137 map.name_table_entry_offs_reordered = addr;
5138 addr += map.name_count * map.offset_size;
5139
5140 const gdb_byte *abbrev_table_start = addr;
5141 for (;;)
5142 {
927aa2e7
JK
5143 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5144 addr += bytes_read;
5145 if (index_num == 0)
5146 break;
5147
5148 const auto insertpair
5149 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5150 if (!insertpair.second)
5151 {
5152 warning (_("Section .debug_names in %s has duplicate index %s, "
5153 "ignoring .debug_names."),
5154 filename, pulongest (index_num));
5155 return false;
5156 }
5157 mapped_debug_names::index_val &indexval = insertpair.first->second;
5158 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5159 addr += bytes_read;
5160
5161 for (;;)
5162 {
5163 mapped_debug_names::index_val::attr attr;
5164 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5167 addr += bytes_read;
5168 if (attr.form == DW_FORM_implicit_const)
5169 {
5170 attr.implicit_const = read_signed_leb128 (abfd, addr,
5171 &bytes_read);
5172 addr += bytes_read;
5173 }
5174 if (attr.dw_idx == 0 && attr.form == 0)
5175 break;
5176 indexval.attr_vec.push_back (std::move (attr));
5177 }
5178 }
5179 if (addr != abbrev_table_start + abbrev_table_size)
5180 {
5181 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5182 "of size %s vs. written as %u, ignoring .debug_names."),
5183 filename, plongest (addr - abbrev_table_start),
5184 abbrev_table_size);
927aa2e7
JK
5185 return false;
5186 }
5187 map.entry_pool = addr;
5188
5189 return true;
5190}
5191
5192/* A helper for create_cus_from_debug_names that handles the MAP's CU
5193 list. */
5194
5195static void
168c9250 5196create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5197 const mapped_debug_names &map,
5198 dwarf2_section_info &section,
b76e467d 5199 bool is_dwz)
927aa2e7 5200{
3ee6bb11
TV
5201 if (!map.augmentation_is_gdb)
5202 {
5203 for (uint32_t i = 0; i < map.cu_count; ++i)
5204 {
5205 sect_offset sect_off
5206 = (sect_offset) (extract_unsigned_integer
5207 (map.cu_table_reordered + i * map.offset_size,
5208 map.offset_size,
5209 map.dwarf5_byte_order));
5210 /* We don't know the length of the CU, because the CU list in a
5211 .debug_names index can be incomplete, so we can't use the start of
5212 the next CU as end of this CU. We create the CUs here with length 0,
5213 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5214 dwarf2_per_cu_data *per_cu
168c9250
SM
5215 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5216 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5217 }
5218 }
5219
927aa2e7
JK
5220 sect_offset sect_off_prev;
5221 for (uint32_t i = 0; i <= map.cu_count; ++i)
5222 {
5223 sect_offset sect_off_next;
5224 if (i < map.cu_count)
5225 {
5226 sect_off_next
5227 = (sect_offset) (extract_unsigned_integer
5228 (map.cu_table_reordered + i * map.offset_size,
5229 map.offset_size,
5230 map.dwarf5_byte_order));
5231 }
5232 else
5233 sect_off_next = (sect_offset) section.size;
5234 if (i >= 1)
5235 {
5236 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5237 dwarf2_per_cu_data *per_cu
168c9250 5238 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5239 sect_off_prev, length);
168c9250 5240 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5241 }
5242 sect_off_prev = sect_off_next;
5243 }
5244}
5245
5246/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5247 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5248
5249static void
168c9250 5250create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5251 const mapped_debug_names &map,
5252 const mapped_debug_names &dwz_map)
5253{
168c9250
SM
5254 gdb_assert (per_bfd->all_comp_units.empty ());
5255 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5256
168c9250 5257 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5258 false /* is_dwz */);
927aa2e7
JK
5259
5260 if (dwz_map.cu_count == 0)
5261 return;
5262
168c9250
SM
5263 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5264 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5265 true /* is_dwz */);
927aa2e7
JK
5266}
5267
5268/* Read .debug_names. If everything went ok, initialize the "quick"
5269 elements of all the CUs and return true. Otherwise, return false. */
5270
5271static bool
976ca316 5272dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5273{
fcf23d5b
SM
5274 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5275 mapped_debug_names dwz_map;
976ca316 5276 struct objfile *objfile = per_objfile->objfile;
f8c41851 5277 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5278
5279 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5280 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5281 return false;
5282
5283 /* Don't use the index if it's empty. */
22ca247e 5284 if (map->name_count == 0)
927aa2e7
JK
5285 return false;
5286
5287 /* If there is a .dwz file, read it so we can get its CU list as
5288 well. */
f8c41851 5289 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5290 if (dwz != NULL)
5291 {
5292 if (!read_debug_names_from_section (objfile,
00f93c44 5293 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5294 &dwz->debug_names, dwz_map))
5295 {
5296 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5297 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5298 return false;
5299 }
5300 }
5301
f8c41851 5302 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5303
22ca247e 5304 if (map->tu_count != 0)
927aa2e7
JK
5305 {
5306 /* We can only handle a single .debug_types when we have an
5307 index. */
f8c41851 5308 if (per_bfd->types.size () != 1)
927aa2e7
JK
5309 return false;
5310
f8c41851 5311 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5312
5313 create_signatured_type_table_from_debug_names
f8c41851 5314 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5315 }
5316
f8c41851 5317 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5318
f8c41851
SM
5319 per_bfd->debug_names_table = std::move (map);
5320 per_bfd->using_index = 1;
5321 per_bfd->quick_file_names_table =
976ca316 5322 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5323
f8c41851
SM
5324 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5325 objfiles using the same BFD. */
5326 gdb_assert (per_bfd->partial_symtabs == nullptr);
5327 per_bfd->partial_symtabs = objfile->partial_symtabs;
5328
927aa2e7
JK
5329 return true;
5330}
5331
927aa2e7
JK
5332/* Type used to manage iterating over all CUs looking for a symbol for
5333 .debug_names. */
5334
5335class dw2_debug_names_iterator
5336{
5337public:
927aa2e7 5338 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5339 gdb::optional<block_enum> block_index,
5340 domain_enum domain,
fcf23d5b 5341 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5342 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5343 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5344 m_per_objfile (per_objfile)
927aa2e7
JK
5345 {}
5346
5347 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5348 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5349 : m_map (map),
5350 m_search (search),
fcf23d5b
SM
5351 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5352 m_per_objfile (per_objfile)
927aa2e7
JK
5353 {}
5354
3b00ef10
TT
5355 dw2_debug_names_iterator (const mapped_debug_names &map,
5356 block_enum block_index, domain_enum domain,
fcf23d5b 5357 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5358 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5359 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5360 m_per_objfile (per_objfile)
3b00ef10
TT
5361 {}
5362
927aa2e7
JK
5363 /* Return the next matching CU or NULL if there are no more. */
5364 dwarf2_per_cu_data *next ();
5365
5366private:
5367 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5368 const char *name,
5369 dwarf2_per_objfile *per_objfile);
927aa2e7 5370 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5371 uint32_t namei,
5372 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5373
5374 /* The internalized form of .debug_names. */
5375 const mapped_debug_names &m_map;
5376
2b79f376
SM
5377 /* If set, only look for symbols that match that block. Valid values are
5378 GLOBAL_BLOCK and STATIC_BLOCK. */
5379 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5380
5381 /* The kind of symbol we're looking for. */
5382 const domain_enum m_domain = UNDEF_DOMAIN;
5383 const search_domain m_search = ALL_DOMAIN;
5384
5385 /* The list of CUs from the index entry of the symbol, or NULL if
5386 not found. */
5387 const gdb_byte *m_addr;
fcf23d5b
SM
5388
5389 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5390};
5391
5392const char *
fcf23d5b 5393mapped_debug_names::namei_to_name
976ca316 5394 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5395{
5396 const ULONGEST namei_string_offs
5397 = extract_unsigned_integer ((name_table_string_offs_reordered
5398 + namei * offset_size),
5399 offset_size,
5400 dwarf5_byte_order);
976ca316 5401 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5402}
5403
5404/* Find a slot in .debug_names for the object named NAME. If NAME is
5405 found, return pointer to its pool data. If NAME cannot be found,
5406 return NULL. */
5407
5408const gdb_byte *
5409dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5410 (const mapped_debug_names &map, const char *name,
5411 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5412{
5413 int (*cmp) (const char *, const char *);
5414
54ee4252 5415 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5416 if (current_language->la_language == language_cplus
5417 || current_language->la_language == language_fortran
5418 || current_language->la_language == language_d)
5419 {
5420 /* NAME is already canonical. Drop any qualifiers as
5421 .debug_names does not contain any. */
5422
5423 if (strchr (name, '(') != NULL)
5424 {
54ee4252 5425 without_params = cp_remove_params (name);
927aa2e7 5426 if (without_params != NULL)
54ee4252 5427 name = without_params.get ();
927aa2e7
JK
5428 }
5429 }
5430
5431 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5432
5433 const uint32_t full_hash = dwarf5_djb_hash (name);
5434 uint32_t namei
5435 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5436 (map.bucket_table_reordered
5437 + (full_hash % map.bucket_count)), 4,
5438 map.dwarf5_byte_order);
5439 if (namei == 0)
5440 return NULL;
5441 --namei;
5442 if (namei >= map.name_count)
5443 {
b98664d3 5444 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5445 "[in module %s]"),
5446 namei, map.name_count,
fcf23d5b 5447 objfile_name (per_objfile->objfile));
927aa2e7
JK
5448 return NULL;
5449 }
5450
5451 for (;;)
5452 {
5453 const uint32_t namei_full_hash
5454 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5455 (map.hash_table_reordered + namei), 4,
5456 map.dwarf5_byte_order);
5457 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5458 return NULL;
5459
5460 if (full_hash == namei_full_hash)
5461 {
fcf23d5b 5462 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5463
5464#if 0 /* An expensive sanity check. */
5465 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5466 {
b98664d3 5467 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5468 "[in module %s]"),
5469 namei, objfile_name (dwarf2_per_objfile->objfile));
5470 return NULL;
5471 }
5472#endif
5473
5474 if (cmp (namei_string, name) == 0)
5475 {
5476 const ULONGEST namei_entry_offs
5477 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5478 + namei * map.offset_size),
5479 map.offset_size, map.dwarf5_byte_order);
5480 return map.entry_pool + namei_entry_offs;
5481 }
5482 }
5483
5484 ++namei;
5485 if (namei >= map.name_count)
5486 return NULL;
5487 }
5488}
5489
5490const gdb_byte *
5491dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5492 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5493{
5494 if (namei >= map.name_count)
5495 {
b98664d3 5496 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5497 "[in module %s]"),
5498 namei, map.name_count,
fcf23d5b 5499 objfile_name (per_objfile->objfile));
927aa2e7
JK
5500 return NULL;
5501 }
5502
5503 const ULONGEST namei_entry_offs
5504 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5505 + namei * map.offset_size),
5506 map.offset_size, map.dwarf5_byte_order);
5507 return map.entry_pool + namei_entry_offs;
5508}
5509
5510/* See dw2_debug_names_iterator. */
5511
5512dwarf2_per_cu_data *
5513dw2_debug_names_iterator::next ()
5514{
5515 if (m_addr == NULL)
5516 return NULL;
5517
fcf23d5b
SM
5518 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5519 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5520 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5521
5522 again:
5523
5524 unsigned int bytes_read;
5525 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5526 m_addr += bytes_read;
5527 if (abbrev == 0)
5528 return NULL;
5529
5530 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5531 if (indexval_it == m_map.abbrev_map.cend ())
5532 {
b98664d3 5533 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5534 "[in module %s]"),
ed2dc618 5535 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5536 return NULL;
5537 }
5538 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5539 enum class symbol_linkage {
5540 unknown,
5541 static_,
5542 extern_,
23c13d42 5543 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5544 dwarf2_per_cu_data *per_cu = NULL;
5545 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5546 {
5547 ULONGEST ull;
5548 switch (attr.form)
5549 {
5550 case DW_FORM_implicit_const:
5551 ull = attr.implicit_const;
5552 break;
5553 case DW_FORM_flag_present:
5554 ull = 1;
5555 break;
5556 case DW_FORM_udata:
5557 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5558 m_addr += bytes_read;
5559 break;
6dc55ce9 5560 case DW_FORM_ref4:
5561 ull = read_4_bytes (abfd, m_addr);
5562 m_addr += 4;
5563 break;
5564 case DW_FORM_ref8:
5565 ull = read_8_bytes (abfd, m_addr);
5566 m_addr += 8;
5567 break;
5568 case DW_FORM_ref_sig8:
5569 ull = read_8_bytes (abfd, m_addr);
5570 m_addr += 8;
5571 break;
927aa2e7 5572 default:
b98664d3 5573 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5574 dwarf_form_name (attr.form),
ed2dc618 5575 objfile_name (objfile));
927aa2e7
JK
5576 return NULL;
5577 }
5578 switch (attr.dw_idx)
5579 {
5580 case DW_IDX_compile_unit:
5581 /* Don't crash on bad data. */
fcf23d5b 5582 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5583 {
b98664d3 5584 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5585 " [in module %s]"),
5586 pulongest (ull),
fcf23d5b 5587 objfile_name (objfile));
927aa2e7
JK
5588 continue;
5589 }
fcf23d5b 5590 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5591 break;
8af5c486
JK
5592 case DW_IDX_type_unit:
5593 /* Don't crash on bad data. */
fcf23d5b 5594 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5595 {
b98664d3 5596 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5597 " [in module %s]"),
5598 pulongest (ull),
fcf23d5b 5599 objfile_name (objfile));
8af5c486
JK
5600 continue;
5601 }
fcf23d5b 5602 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5603 break;
6dc55ce9 5604 case DW_IDX_die_offset:
5605 /* In a per-CU index (as opposed to a per-module index), index
5606 entries without CU attribute implicitly refer to the single CU. */
5607 if (per_cu == NULL)
fcf23d5b 5608 per_cu = per_bfd->get_cu (0);
6dc55ce9 5609 break;
927aa2e7
JK
5610 case DW_IDX_GNU_internal:
5611 if (!m_map.augmentation_is_gdb)
5612 break;
23c13d42 5613 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5614 break;
5615 case DW_IDX_GNU_external:
5616 if (!m_map.augmentation_is_gdb)
5617 break;
23c13d42 5618 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5619 break;
5620 }
5621 }
5622
5623 /* Skip if already read in. */
fcf23d5b 5624 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5625 goto again;
5626
5627 /* Check static vs global. */
23c13d42 5628 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5629 {
2b79f376 5630 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5631 const bool symbol_is_static =
5632 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5633 if (want_static != symbol_is_static)
2b79f376 5634 goto again;
927aa2e7
JK
5635 }
5636
5637 /* Match dw2_symtab_iter_next, symbol_kind
5638 and debug_names::psymbol_tag. */
5639 switch (m_domain)
5640 {
5641 case VAR_DOMAIN:
5642 switch (indexval.dwarf_tag)
5643 {
5644 case DW_TAG_variable:
5645 case DW_TAG_subprogram:
5646 /* Some types are also in VAR_DOMAIN. */
5647 case DW_TAG_typedef:
5648 case DW_TAG_structure_type:
5649 break;
5650 default:
5651 goto again;
5652 }
5653 break;
5654 case STRUCT_DOMAIN:
5655 switch (indexval.dwarf_tag)
5656 {
5657 case DW_TAG_typedef:
5658 case DW_TAG_structure_type:
5659 break;
5660 default:
5661 goto again;
5662 }
5663 break;
5664 case LABEL_DOMAIN:
5665 switch (indexval.dwarf_tag)
5666 {
5667 case 0:
5668 case DW_TAG_variable:
5669 break;
5670 default:
5671 goto again;
5672 }
5673 break;
59c35742
AB
5674 case MODULE_DOMAIN:
5675 switch (indexval.dwarf_tag)
5676 {
5677 case DW_TAG_module:
5678 break;
5679 default:
5680 goto again;
5681 }
5682 break;
927aa2e7
JK
5683 default:
5684 break;
5685 }
5686
5687 /* Match dw2_expand_symtabs_matching, symbol_kind and
5688 debug_names::psymbol_tag. */
5689 switch (m_search)
4b514bc8 5690 {
927aa2e7
JK
5691 case VARIABLES_DOMAIN:
5692 switch (indexval.dwarf_tag)
4b514bc8 5693 {
927aa2e7
JK
5694 case DW_TAG_variable:
5695 break;
5696 default:
5697 goto again;
4b514bc8 5698 }
927aa2e7
JK
5699 break;
5700 case FUNCTIONS_DOMAIN:
5701 switch (indexval.dwarf_tag)
4b514bc8 5702 {
927aa2e7
JK
5703 case DW_TAG_subprogram:
5704 break;
5705 default:
5706 goto again;
4b514bc8 5707 }
927aa2e7
JK
5708 break;
5709 case TYPES_DOMAIN:
5710 switch (indexval.dwarf_tag)
5711 {
5712 case DW_TAG_typedef:
5713 case DW_TAG_structure_type:
5714 break;
5715 default:
5716 goto again;
5717 }
5718 break;
59c35742
AB
5719 case MODULES_DOMAIN:
5720 switch (indexval.dwarf_tag)
5721 {
5722 case DW_TAG_module:
5723 break;
5724 default:
5725 goto again;
5726 }
927aa2e7
JK
5727 default:
5728 break;
4b514bc8 5729 }
927aa2e7
JK
5730
5731 return per_cu;
4b514bc8 5732}
61920122 5733
927aa2e7 5734static struct compunit_symtab *
c7f839cb 5735dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5736 const char *name, domain_enum domain)
4b514bc8 5737{
976ca316 5738 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5739
976ca316 5740 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5741 if (!mapp)
61920122 5742 {
927aa2e7
JK
5743 /* index is NULL if OBJF_READNOW. */
5744 return NULL;
5745 }
5746 const auto &map = *mapp;
9291a0cd 5747
976ca316 5748 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5749
927aa2e7
JK
5750 struct compunit_symtab *stab_best = NULL;
5751 struct dwarf2_per_cu_data *per_cu;
5752 while ((per_cu = iter.next ()) != NULL)
5753 {
5754 struct symbol *sym, *with_opaque = NULL;
97a1449a 5755 compunit_symtab *stab
976ca316 5756 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5757 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5758 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5759
927aa2e7
JK
5760 sym = block_find_symbol (block, name, domain,
5761 block_find_non_opaque_type_preferred,
5762 &with_opaque);
9703b513 5763
927aa2e7
JK
5764 /* Some caution must be observed with overloaded functions and
5765 methods, since the index will not contain any overload
5766 information (but NAME might contain it). */
a3ec0bb1 5767
927aa2e7 5768 if (sym != NULL
987012b8 5769 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5770 return stab;
5771 if (with_opaque != NULL
987012b8 5772 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5773 stab_best = stab;
9703b513 5774
927aa2e7 5775 /* Keep looking through other CUs. */
9703b513
TT
5776 }
5777
927aa2e7 5778 return stab_best;
9703b513
TT
5779}
5780
927aa2e7
JK
5781/* This dumps minimal information about .debug_names. It is called
5782 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5783 uses this to verify that .debug_names has been loaded. */
9291a0cd 5784
927aa2e7
JK
5785static void
5786dw2_debug_names_dump (struct objfile *objfile)
5787{
976ca316 5788 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5789
976ca316 5790 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5791 printf_filtered (".debug_names:");
976ca316 5792 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5793 printf_filtered (" exists\n");
5794 else
5795 printf_filtered (" faked for \"readnow\"\n");
5796 printf_filtered ("\n");
9291a0cd
TT
5797}
5798
9291a0cd 5799static void
927aa2e7
JK
5800dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5801 const char *func_name)
9291a0cd 5802{
976ca316 5803 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5804
976ca316
SM
5805 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5806 if (per_objfile->per_bfd->debug_names_table)
24c79950 5807 {
976ca316 5808 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5809
fcf23d5b 5810 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5811 per_objfile);
24c79950 5812
927aa2e7
JK
5813 struct dwarf2_per_cu_data *per_cu;
5814 while ((per_cu = iter.next ()) != NULL)
976ca316 5815 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5816 }
5817}
24c79950 5818
3b00ef10
TT
5819static void
5820dw2_debug_names_map_matching_symbols
5821 (struct objfile *objfile,
5822 const lookup_name_info &name, domain_enum domain,
5823 int global,
5824 gdb::function_view<symbol_found_callback_ftype> callback,
5825 symbol_compare_ftype *ordered_compare)
5826{
976ca316 5827 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5828
5829 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5830 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5831 return;
5832
976ca316 5833 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5834 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5835
5836 const char *match_name = name.ada ().lookup_name ().c_str ();
5837 auto matcher = [&] (const char *symname)
5838 {
5839 if (ordered_compare == nullptr)
5840 return true;
5841 return ordered_compare (symname, match_name) == 0;
5842 };
5843
5844 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5845 [&] (offset_type namei)
5846 {
5847 /* The name was matched, now expand corresponding CUs that were
5848 marked. */
fcf23d5b 5849 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5850 per_objfile);
3b00ef10
TT
5851
5852 struct dwarf2_per_cu_data *per_cu;
5853 while ((per_cu = iter.next ()) != NULL)
976ca316 5854 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5855 nullptr);
3b00ef10 5856 return true;
976ca316 5857 }, per_objfile);
3b00ef10
TT
5858
5859 /* It's a shame we couldn't do this inside the
5860 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5861 that have already been expanded. Instead, this loop matches what
5862 the psymtab code does. */
976ca316 5863 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5864 {
976ca316 5865 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5866 if (symtab != nullptr)
3b00ef10
TT
5867 {
5868 const struct block *block
af758d11 5869 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5870 if (!iterate_over_symbols_terminated (block, name,
5871 domain, callback))
5872 break;
5873 }
5874 }
5875}
5876
927aa2e7
JK
5877static void
5878dw2_debug_names_expand_symtabs_matching
5879 (struct objfile *objfile,
5880 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5881 const lookup_name_info *lookup_name,
927aa2e7
JK
5882 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5883 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5884 enum search_domain kind)
5885{
976ca316 5886 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5887
927aa2e7 5888 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5889 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5890 return;
9291a0cd 5891
976ca316 5892 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5893
c1a66c06
TV
5894 if (symbol_matcher == NULL && lookup_name == NULL)
5895 {
976ca316 5896 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5897 {
5898 QUIT;
5899
976ca316
SM
5900 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5901 expansion_notify);
c1a66c06
TV
5902 }
5903 return;
5904 }
5905
976ca316 5906 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5907
c1a66c06 5908 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5909 symbol_matcher,
5910 kind, [&] (offset_type namei)
927aa2e7 5911 {
927aa2e7
JK
5912 /* The name was matched, now expand corresponding CUs that were
5913 marked. */
976ca316 5914 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5915
927aa2e7
JK
5916 struct dwarf2_per_cu_data *per_cu;
5917 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5918 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5919 expansion_notify);
3b00ef10 5920 return true;
976ca316 5921 }, per_objfile);
9291a0cd
TT
5922}
5923
927aa2e7 5924const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5925{
5926 dw2_has_symbols,
5927 dw2_find_last_source_symtab,
5928 dw2_forget_cached_source_info,
f8eba3c6 5929 dw2_map_symtabs_matching_filename,
927aa2e7 5930 dw2_debug_names_lookup_symbol,
d3214198 5931 NULL,
9291a0cd 5932 dw2_print_stats,
927aa2e7 5933 dw2_debug_names_dump,
927aa2e7 5934 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5935 dw2_expand_all_symtabs,
652a8996 5936 dw2_expand_symtabs_with_fullname,
3b00ef10 5937 dw2_debug_names_map_matching_symbols,
927aa2e7 5938 dw2_debug_names_expand_symtabs_matching,
43f3e411 5939 dw2_find_pc_sect_compunit_symtab,
71a3c369 5940 NULL,
9291a0cd
TT
5941 dw2_map_symbol_filenames
5942};
5943
4485a1c1 5944/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5945 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5946
5947template <typename T>
5948static gdb::array_view<const gdb_byte>
5949get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5950{
5951 dwarf2_section_info *section = &section_owner->gdb_index;
5952
96b79293 5953 if (section->empty ())
4485a1c1
SM
5954 return {};
5955
5956 /* Older elfutils strip versions could keep the section in the main
5957 executable while splitting it for the separate debug info file. */
96b79293 5958 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5959 return {};
5960
96b79293 5961 section->read (obj);
4485a1c1 5962
8bebfcda
PA
5963 /* dwarf2_section_info::size is a bfd_size_type, while
5964 gdb::array_view works with size_t. On 32-bit hosts, with
5965 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5966 is 32-bit. So we need an explicit narrowing conversion here.
5967 This is fine, because it's impossible to allocate or mmap an
5968 array/buffer larger than what size_t can represent. */
5969 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5970}
5971
87d6a7aa
SM
5972/* Lookup the index cache for the contents of the index associated to
5973 DWARF2_OBJ. */
5974
5975static gdb::array_view<const gdb_byte>
5989a64e 5976get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5977{
5978 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5979 if (build_id == nullptr)
5980 return {};
5981
5982 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5983 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5984}
5985
5986/* Same as the above, but for DWZ. */
5987
5988static gdb::array_view<const gdb_byte>
5989get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5990{
5991 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5992 if (build_id == nullptr)
5993 return {};
5994
5995 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5996}
5997
3c0aa29a 5998/* See symfile.h. */
9291a0cd 5999
3c0aa29a
PA
6000bool
6001dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6002{
976ca316
SM
6003 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6004 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6005
9291a0cd
TT
6006 /* If we're about to read full symbols, don't bother with the
6007 indices. In this case we also don't care if some other debug
6008 format is making psymtabs, because they are all about to be
6009 expanded anyway. */
6010 if ((objfile->flags & OBJF_READNOW))
6011 {
17ee85fc
TT
6012 /* When using READNOW, the using_index flag (set below) indicates that
6013 PER_BFD was already initialized, when we loaded some other objfile. */
6014 if (per_bfd->using_index)
6015 {
6016 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6017 per_objfile->resize_symtabs ();
17ee85fc
TT
6018 return true;
6019 }
6020
6021 per_bfd->using_index = 1;
976ca316
SM
6022 create_all_comp_units (per_objfile);
6023 create_all_type_units (per_objfile);
17ee85fc
TT
6024 per_bfd->quick_file_names_table
6025 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6026 per_objfile->resize_symtabs ();
9291a0cd 6027
17ee85fc
TT
6028 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6029 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6030 {
17ee85fc 6031 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6032
17ee85fc 6033 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6034 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6035 }
6036
6037 /* Return 1 so that gdb sees the "quick" functions. However,
6038 these functions will be no-ops because we will have expanded
6039 all symtabs. */
3c0aa29a
PA
6040 *index_kind = dw_index_kind::GDB_INDEX;
6041 return true;
9291a0cd
TT
6042 }
6043
17ee85fc
TT
6044 /* Was a debug names index already read when we processed an objfile sharing
6045 PER_BFD? */
6046 if (per_bfd->debug_names_table != nullptr)
6047 {
6048 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6049 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6050 per_objfile->resize_symtabs ();
17ee85fc
TT
6051 return true;
6052 }
6053
6054 /* Was a GDB index already read when we processed an objfile sharing
6055 PER_BFD? */
6056 if (per_bfd->index_table != nullptr)
6057 {
6058 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6059 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6060 per_objfile->resize_symtabs ();
17ee85fc
TT
6061 return true;
6062 }
6063
efb763a5
SM
6064 /* There might already be partial symtabs built for this BFD. This happens
6065 when loading the same binary twice with the index-cache enabled. If so,
6066 don't try to read an index. The objfile / per_objfile initialization will
6067 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6068 code path. */
6069 if (per_bfd->partial_symtabs != nullptr)
6070 return false;
6071
976ca316 6072 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6073 {
6074 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6075 per_objfile->resize_symtabs ();
3c0aa29a
PA
6076 return true;
6077 }
927aa2e7 6078
976ca316 6079 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6080 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6081 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6082 {
6083 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6084 per_objfile->resize_symtabs ();
3c0aa29a
PA
6085 return true;
6086 }
9291a0cd 6087
87d6a7aa 6088 /* ... otherwise, try to find the index in the index cache. */
976ca316 6089 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6090 get_gdb_index_contents_from_cache,
6091 get_gdb_index_contents_from_cache_dwz))
6092 {
6093 global_index_cache.hit ();
6094 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6095 per_objfile->resize_symtabs ();
87d6a7aa
SM
6096 return true;
6097 }
6098
6099 global_index_cache.miss ();
3c0aa29a 6100 return false;
9291a0cd
TT
6101}
6102
6103\f
6104
dce234bc
PP
6105/* Build a partial symbol table. */
6106
6107void
f29dff0a 6108dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6109{
976ca316
SM
6110 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6111 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6112
6113 if (per_bfd->partial_symtabs != nullptr)
6114 {
6115 /* Partial symbols were already read, so now we can simply
6116 attach them. */
6117 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6118 per_objfile->resize_symtabs ();
17ee85fc
TT
6119 return;
6120 }
c9bf0622 6121
6eee24ce 6122 init_psymbol_list (objfile, 1024);
c906108c 6123
a70b8144 6124 try
c9bf0622
TT
6125 {
6126 /* This isn't really ideal: all the data we allocate on the
6127 objfile's obstack is still uselessly kept around. However,
6128 freeing it seems unsafe. */
906768f9 6129 psymtab_discarder psymtabs (objfile);
976ca316 6130 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6131 psymtabs.keep ();
87d6a7aa 6132
976ca316 6133 per_objfile->resize_symtabs ();
af758d11 6134
87d6a7aa 6135 /* (maybe) store an index in the cache. */
976ca316 6136 global_index_cache.store (per_objfile);
c9bf0622 6137 }
230d2906 6138 catch (const gdb_exception_error &except)
492d29ea
PA
6139 {
6140 exception_print (gdb_stderr, except);
6141 }
17ee85fc
TT
6142
6143 /* Finish by setting the local reference to partial symtabs, so that
6144 we don't try to read them again if reading another objfile with the same
6145 BFD. If we can't in fact share, this won't make a difference anyway as
6146 the dwarf2_per_bfd object won't be shared. */
6147 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6148}
c906108c 6149
3b80fe9b
DE
6150/* Find the base address of the compilation unit for range lists and
6151 location lists. It will normally be specified by DW_AT_low_pc.
6152 In DWARF-3 draft 4, the base address could be overridden by
6153 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6154 compilation units with discontinuous ranges. */
6155
6156static void
6157dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6158{
6159 struct attribute *attr;
6160
2b24b6e4 6161 cu->base_address.reset ();
3b80fe9b
DE
6162
6163 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6164 if (attr != nullptr)
95f982e5 6165 cu->base_address = attr->as_address ();
3b80fe9b
DE
6166 else
6167 {
6168 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6169 if (attr != nullptr)
95f982e5 6170 cu->base_address = attr->as_address ();
3b80fe9b
DE
6171 }
6172}
6173
36586728
TT
6174/* Helper function that returns the proper abbrev section for
6175 THIS_CU. */
6176
6177static struct dwarf2_section_info *
6178get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6179{
6180 struct dwarf2_section_info *abbrev;
c3699833 6181 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6182
6183 if (this_cu->is_dwz)
c3699833 6184 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6185 else
c3699833 6186 abbrev = &per_bfd->abbrev;
36586728
TT
6187
6188 return abbrev;
6189}
6190
f4dc4d17
DE
6191/* Fetch the abbreviation table offset from a comp or type unit header. */
6192
6193static sect_offset
976ca316 6194read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6195 struct dwarf2_section_info *section,
9c541725 6196 sect_offset sect_off)
f4dc4d17 6197{
96b79293 6198 bfd *abfd = section->get_bfd_owner ();
d521ce57 6199 const gdb_byte *info_ptr;
ac298888 6200 unsigned int initial_length_size, offset_size;
43988095 6201 uint16_t version;
f4dc4d17 6202
976ca316 6203 section->read (per_objfile->objfile);
9c541725 6204 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6205 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6206 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6207 info_ptr += initial_length_size;
6208
6209 version = read_2_bytes (abfd, info_ptr);
6210 info_ptr += 2;
6211 if (version >= 5)
6212 {
6213 /* Skip unit type and address size. */
6214 info_ptr += 2;
6215 }
6216
24aa364d 6217 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6218}
6219
b83470bf
TT
6220/* A partial symtab that is used only for include files. */
6221struct dwarf2_include_psymtab : public partial_symtab
6222{
6223 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6224 : partial_symtab (filename, objfile)
6225 {
6226 }
6227
6228 void read_symtab (struct objfile *objfile) override
6229 {
194d088f
TV
6230 /* It's an include file, no symbols to read for it.
6231 Everything is in the includer symtab. */
6232
6233 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6234 expansion of the includer psymtab. We use the dependencies[0] field to
6235 model the includer. But if we go the regular route of calling
6236 expand_psymtab here, and having expand_psymtab call expand_dependencies
6237 to expand the includer, we'll only use expand_psymtab on the includer
6238 (making it a non-toplevel psymtab), while if we expand the includer via
6239 another path, we'll use read_symtab (making it a toplevel psymtab).
6240 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6241 psymtab, and trigger read_symtab on the includer here directly. */
6242 includer ()->read_symtab (objfile);
b83470bf
TT
6243 }
6244
6245 void expand_psymtab (struct objfile *objfile) override
6246 {
194d088f
TV
6247 /* This is not called by read_symtab, and should not be called by any
6248 expand_dependencies. */
6249 gdb_assert (false);
b83470bf
TT
6250 }
6251
5717c425 6252 bool readin_p (struct objfile *objfile) const override
b83470bf 6253 {
5717c425 6254 return includer ()->readin_p (objfile);
b83470bf
TT
6255 }
6256
5717c425 6257 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6258 {
6259 return nullptr;
6260 }
6261
6262private:
194d088f
TV
6263 partial_symtab *includer () const
6264 {
6265 /* An include psymtab has exactly one dependency: the psymtab that
6266 includes it. */
6267 gdb_assert (this->number_of_dependencies == 1);
6268 return this->dependencies[0];
6269 }
b83470bf
TT
6270};
6271
aaa75496
JB
6272/* Allocate a new partial symtab for file named NAME and mark this new
6273 partial symtab as being an include of PST. */
6274
6275static void
891813be 6276dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6277 struct objfile *objfile)
6278{
b83470bf 6279 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6280
fbd9ab74 6281 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6282 subpst->dirname = pst->dirname;
fbd9ab74 6283
a9342b62 6284 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6285 subpst->dependencies[0] = pst;
6286 subpst->number_of_dependencies = 1;
aaa75496
JB
6287}
6288
6289/* Read the Line Number Program data and extract the list of files
6290 included by the source file represented by PST. Build an include
d85a05f0 6291 partial symtab for each of these included files. */
aaa75496
JB
6292
6293static void
6294dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6295 struct die_info *die,
891813be 6296 dwarf2_psymtab *pst)
aaa75496 6297{
fff8551c 6298 line_header_up lh;
d85a05f0 6299 struct attribute *attr;
aaa75496 6300
d85a05f0 6301 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6302 if (attr != nullptr && attr->form_is_unsigned ())
6303 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6304 if (lh == NULL)
6305 return; /* No linetable, so no includes. */
6306
79748972
TT
6307 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6308 that we pass in the raw text_low here; that is ok because we're
6309 only decoding the line table to make include partial symtabs, and
6310 so the addresses aren't really used. */
4ae976d1 6311 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6312 pst->raw_text_low (), 1);
aaa75496
JB
6313}
6314
348e048f 6315static hashval_t
52dc124a 6316hash_signatured_type (const void *item)
348e048f 6317{
9a3c8263
SM
6318 const struct signatured_type *sig_type
6319 = (const struct signatured_type *) item;
9a619af0 6320
348e048f 6321 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6322 return sig_type->signature;
348e048f
DE
6323}
6324
6325static int
52dc124a 6326eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6327{
9a3c8263
SM
6328 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6329 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6330
348e048f
DE
6331 return lhs->signature == rhs->signature;
6332}
6333
1fd400ff
TT
6334/* Allocate a hash table for signatured types. */
6335
b0b6a987 6336static htab_up
298e9637 6337allocate_signatured_type_table ()
1fd400ff 6338{
b0b6a987
TT
6339 return htab_up (htab_create_alloc (41,
6340 hash_signatured_type,
6341 eq_signatured_type,
6342 NULL, xcalloc, xfree));
1fd400ff
TT
6343}
6344
d467dd73 6345/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6346
6347static int
d467dd73 6348add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6349{
9a3c8263 6350 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6351 std::vector<signatured_type *> *all_type_units
6352 = (std::vector<signatured_type *> *) datum;
1fd400ff 6353
b2bdb8cf 6354 all_type_units->push_back (sigt);
1fd400ff
TT
6355
6356 return 1;
6357}
6358
78d4d2c5 6359/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6360 and fill them into TYPES_HTAB. It will process only type units,
6361 therefore DW_UT_type. */
c88ee1f0 6362
78d4d2c5 6363static void
976ca316 6364create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6365 struct dwo_file *dwo_file,
b0b6a987 6366 dwarf2_section_info *section, htab_up &types_htab,
43988095 6367 rcuh_kind section_kind)
348e048f 6368{
976ca316 6369 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6370 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6371 bfd *abfd;
6372 const gdb_byte *info_ptr, *end_ptr;
348e048f 6373
4bdcc0c1
DE
6374 abbrev_section = (dwo_file != NULL
6375 ? &dwo_file->sections.abbrev
976ca316 6376 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6377
b4f54984 6378 if (dwarf_read_debug)
43988095 6379 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6380 section->get_name (),
6381 abbrev_section->get_file_name ());
09406207 6382
96b79293 6383 section->read (objfile);
78d4d2c5 6384 info_ptr = section->buffer;
348e048f 6385
78d4d2c5
JK
6386 if (info_ptr == NULL)
6387 return;
348e048f 6388
78d4d2c5
JK
6389 /* We can't set abfd until now because the section may be empty or
6390 not present, in which case the bfd is unknown. */
96b79293 6391 abfd = section->get_bfd_owner ();
348e048f 6392
c0ab21c2
TT
6393 /* We don't use cutu_reader here because we don't need to read
6394 any dies: the signature is in the header. */
3019eac3 6395
78d4d2c5
JK
6396 end_ptr = info_ptr + section->size;
6397 while (info_ptr < end_ptr)
6398 {
78d4d2c5
JK
6399 struct signatured_type *sig_type;
6400 struct dwo_unit *dwo_tu;
6401 void **slot;
6402 const gdb_byte *ptr = info_ptr;
6403 struct comp_unit_head header;
6404 unsigned int length;
8b70b953 6405
9c541725 6406 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6407
a49dd8dd
JK
6408 /* Initialize it due to a false compiler warning. */
6409 header.signature = -1;
9c541725 6410 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6411
78d4d2c5
JK
6412 /* We need to read the type's signature in order to build the hash
6413 table, but we don't need anything else just yet. */
348e048f 6414
976ca316 6415 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6416 abbrev_section, ptr, section_kind);
348e048f 6417
4057dfde 6418 length = header.get_length ();
6caca83c 6419
78d4d2c5
JK
6420 /* Skip dummy type units. */
6421 if (ptr >= info_ptr + length
43988095 6422 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6423 || (header.unit_type != DW_UT_type
6424 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6425 {
6426 info_ptr += length;
6427 continue;
6428 }
dee91e82 6429
78d4d2c5
JK
6430 if (types_htab == NULL)
6431 {
6432 if (dwo_file)
298e9637 6433 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6434 else
298e9637 6435 types_htab = allocate_signatured_type_table ();
78d4d2c5 6436 }
8b70b953 6437
78d4d2c5
JK
6438 if (dwo_file)
6439 {
6440 sig_type = NULL;
976ca316 6441 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6442 dwo_tu->dwo_file = dwo_file;
43988095 6443 dwo_tu->signature = header.signature;
9c541725 6444 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6445 dwo_tu->section = section;
9c541725 6446 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6447 dwo_tu->length = length;
6448 }
6449 else
6450 {
6451 /* N.B.: type_offset is not usable if this type uses a DWO file.
6452 The real type_offset is in the DWO file. */
6453 dwo_tu = NULL;
976ca316 6454 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6455 sig_type->signature = header.signature;
9c541725 6456 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6457 sig_type->per_cu.is_debug_types = 1;
6458 sig_type->per_cu.section = section;
9c541725 6459 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6460 sig_type->per_cu.length = length;
6461 }
6462
b0b6a987 6463 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6464 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6465 INSERT);
6466 gdb_assert (slot != NULL);
6467 if (*slot != NULL)
6468 {
9c541725 6469 sect_offset dup_sect_off;
0349ea22 6470
3019eac3
DE
6471 if (dwo_file)
6472 {
78d4d2c5
JK
6473 const struct dwo_unit *dup_tu
6474 = (const struct dwo_unit *) *slot;
6475
9c541725 6476 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6477 }
6478 else
6479 {
78d4d2c5
JK
6480 const struct signatured_type *dup_tu
6481 = (const struct signatured_type *) *slot;
6482
9c541725 6483 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6484 }
8b70b953 6485
b98664d3 6486 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6487 " the entry at offset %s, signature %s"),
6488 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6489 hex_string (header.signature));
78d4d2c5
JK
6490 }
6491 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6492
78d4d2c5 6493 if (dwarf_read_debug > 1)
9d8780f0
SM
6494 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6495 sect_offset_str (sect_off),
43988095 6496 hex_string (header.signature));
3019eac3 6497
78d4d2c5
JK
6498 info_ptr += length;
6499 }
6500}
3019eac3 6501
78d4d2c5
JK
6502/* Create the hash table of all entries in the .debug_types
6503 (or .debug_types.dwo) section(s).
6504 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6505 otherwise it is NULL.
b3c8eb43 6506
78d4d2c5 6507 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6508
78d4d2c5 6509 Note: This function processes DWO files only, not DWP files. */
348e048f 6510
78d4d2c5 6511static void
976ca316 6512create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6513 struct dwo_file *dwo_file,
fd5866f6 6514 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6515 htab_up &types_htab)
78d4d2c5 6516{
fd5866f6 6517 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6518 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6519 rcuh_kind::TYPE);
3019eac3
DE
6520}
6521
6522/* Create the hash table of all entries in the .debug_types section,
6523 and initialize all_type_units.
6524 The result is zero if there is an error (e.g. missing .debug_types section),
6525 otherwise non-zero. */
6526
6527static int
976ca316 6528create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6529{
b0b6a987 6530 htab_up types_htab;
3019eac3 6531
976ca316
SM
6532 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6533 types_htab, rcuh_kind::COMPILE);
6534 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6535 types_htab);
3019eac3
DE
6536 if (types_htab == NULL)
6537 {
976ca316 6538 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6539 return 0;
6540 }
6541
976ca316 6542 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6543
976ca316
SM
6544 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6545 per_objfile->per_bfd->all_type_units.reserve
6546 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6547
976ca316 6548 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6549 add_signatured_type_cu_to_table,
976ca316 6550 &per_objfile->per_bfd->all_type_units);
1fd400ff 6551
348e048f
DE
6552 return 1;
6553}
6554
5989a64e 6555/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6556 If SLOT is non-NULL, it is the entry to use in the hash table.
6557 Otherwise we find one. */
6558
6559static struct signatured_type *
976ca316 6560add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6561{
976ca316
SM
6562 if (per_objfile->per_bfd->all_type_units.size ()
6563 == per_objfile->per_bfd->all_type_units.capacity ())
6564 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6565
976ca316 6566 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6567
976ca316 6568 per_objfile->resize_symtabs ();
af758d11 6569
976ca316 6570 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6571 sig_type->signature = sig;
6572 sig_type->per_cu.is_debug_types = 1;
976ca316 6573 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6574 {
6575 sig_type->per_cu.v.quick =
976ca316 6576 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6577 struct dwarf2_per_cu_quick_data);
6578 }
6579
6580 if (slot == NULL)
6581 {
976ca316 6582 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6583 sig_type, INSERT);
6584 }
6585 gdb_assert (*slot == NULL);
6586 *slot = sig_type;
6587 /* The rest of sig_type must be filled in by the caller. */
6588 return sig_type;
6589}
6590
a2ce51a0
DE
6591/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6592 Fill in SIG_ENTRY with DWO_ENTRY. */
6593
6594static void
976ca316 6595fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6596 struct signatured_type *sig_entry,
6597 struct dwo_unit *dwo_entry)
6598{
976ca316 6599 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6600
7ee85ab1 6601 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6602 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6603 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6604 if (per_bfd->using_index)
6aa5f3a6
DE
6605 {
6606 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6607 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6608 }
6609 else
6610 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6611 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6612 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6613 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6614 gdb_assert (sig_entry->dwo_unit == NULL);
6615
6616 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6617 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6618 sig_entry->per_cu.length = dwo_entry->length;
6619 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6620 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6621 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6622 sig_entry->dwo_unit = dwo_entry;
6623}
6624
6625/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6626 If we haven't read the TU yet, create the signatured_type data structure
6627 for a TU to be read in directly from a DWO file, bypassing the stub.
6628 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6629 using .gdb_index, then when reading a CU we want to stay in the DWO file
6630 containing that CU. Otherwise we could end up reading several other DWO
6631 files (due to comdat folding) to process the transitive closure of all the
6632 mentioned TUs, and that can be slow. The current DWO file will have every
6633 type signature that it needs.
a2ce51a0
DE
6634 We only do this for .gdb_index because in the psymtab case we already have
6635 to read all the DWOs to build the type unit groups. */
6636
6637static struct signatured_type *
6638lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6639{
976ca316 6640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6641 struct dwo_file *dwo_file;
6642 struct dwo_unit find_dwo_entry, *dwo_entry;
6643 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6644 void **slot;
a2ce51a0 6645
976ca316 6646 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6647
6aa5f3a6
DE
6648 /* If TU skeletons have been removed then we may not have read in any
6649 TUs yet. */
976ca316
SM
6650 if (per_objfile->per_bfd->signatured_types == NULL)
6651 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6652
6653 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6654 Use the global signatured_types array to do our own comdat-folding
6655 of types. If this is the first time we're reading this TU, and
6656 the TU has an entry in .gdb_index, replace the recorded data from
6657 .gdb_index with this TU. */
a2ce51a0 6658
a2ce51a0 6659 find_sig_entry.signature = sig;
976ca316 6660 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6661 &find_sig_entry, INSERT);
9a3c8263 6662 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6663
6664 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6665 read. Don't reassign the global entry to point to this DWO if that's
6666 the case. Also note that if the TU is already being read, it may not
6667 have come from a DWO, the program may be a mix of Fission-compiled
6668 code and non-Fission-compiled code. */
6669
6670 /* Have we already tried to read this TU?
6671 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6672 needn't exist in the global table yet). */
6673 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6674 return sig_entry;
6675
6aa5f3a6
DE
6676 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6677 dwo_unit of the TU itself. */
6678 dwo_file = cu->dwo_unit->dwo_file;
6679
a2ce51a0
DE
6680 /* Ok, this is the first time we're reading this TU. */
6681 if (dwo_file->tus == NULL)
6682 return NULL;
6683 find_dwo_entry.signature = sig;
b0b6a987
TT
6684 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6685 &find_dwo_entry);
a2ce51a0
DE
6686 if (dwo_entry == NULL)
6687 return NULL;
6688
6aa5f3a6
DE
6689 /* If the global table doesn't have an entry for this TU, add one. */
6690 if (sig_entry == NULL)
976ca316 6691 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6692
976ca316 6693 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6694 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6695 return sig_entry;
6696}
6697
a2ce51a0
DE
6698/* Subroutine of lookup_signatured_type.
6699 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6700 then try the DWP file. If the TU stub (skeleton) has been removed then
6701 it won't be in .gdb_index. */
a2ce51a0
DE
6702
6703static struct signatured_type *
6704lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6705{
976ca316
SM
6706 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6707 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6708 struct dwo_unit *dwo_entry;
6709 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6710 void **slot;
a2ce51a0 6711
976ca316 6712 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6713 gdb_assert (dwp_file != NULL);
6714
6aa5f3a6
DE
6715 /* If TU skeletons have been removed then we may not have read in any
6716 TUs yet. */
976ca316
SM
6717 if (per_objfile->per_bfd->signatured_types == NULL)
6718 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6719
6aa5f3a6 6720 find_sig_entry.signature = sig;
976ca316 6721 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6722 &find_sig_entry, INSERT);
9a3c8263 6723 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6724
6725 /* Have we already tried to read this TU?
6726 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6727 needn't exist in the global table yet). */
6728 if (sig_entry != NULL)
6729 return sig_entry;
6730
a2ce51a0
DE
6731 if (dwp_file->tus == NULL)
6732 return NULL;
976ca316
SM
6733 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6734 1 /* is_debug_types */);
a2ce51a0
DE
6735 if (dwo_entry == NULL)
6736 return NULL;
6737
976ca316
SM
6738 sig_entry = add_type_unit (per_objfile, sig, slot);
6739 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6740
a2ce51a0
DE
6741 return sig_entry;
6742}
6743
380bca97 6744/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6745 Returns NULL if signature SIG is not present in the table.
6746 It is up to the caller to complain about this. */
348e048f
DE
6747
6748static struct signatured_type *
a2ce51a0 6749lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6750{
976ca316 6751 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6752
976ca316 6753 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6754 {
6755 /* We're in a DWO/DWP file, and we're using .gdb_index.
6756 These cases require special processing. */
976ca316 6757 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6758 return lookup_dwo_signatured_type (cu, sig);
6759 else
6760 return lookup_dwp_signatured_type (cu, sig);
6761 }
6762 else
6763 {
6764 struct signatured_type find_entry, *entry;
348e048f 6765
976ca316 6766 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6767 return NULL;
6768 find_entry.signature = sig;
9a3c8263 6769 entry = ((struct signatured_type *)
976ca316 6770 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6771 &find_entry));
a2ce51a0
DE
6772 return entry;
6773 }
348e048f 6774}
18a8505e 6775
42e7ad6c 6776/* Low level DIE reading support. */
348e048f 6777
d85a05f0
DJ
6778/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6779
6780static void
6781init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6782 struct dwarf2_cu *cu,
3019eac3 6783 struct dwarf2_section_info *section,
685af9cd
TT
6784 struct dwo_file *dwo_file,
6785 struct abbrev_table *abbrev_table)
d85a05f0 6786{
fceca515 6787 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6788 reader->abfd = section->get_bfd_owner ();
d85a05f0 6789 reader->cu = cu;
3019eac3 6790 reader->dwo_file = dwo_file;
dee91e82
DE
6791 reader->die_section = section;
6792 reader->buffer = section->buffer;
f664829e 6793 reader->buffer_end = section->buffer + section->size;
685af9cd 6794 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6795}
6796
c0ab21c2 6797/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6798 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6799 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6800 already.
6801
6802 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6803 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6804 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6805 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6806 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6807 STUB_COMP_DIR may be non-NULL.
3e225074 6808 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6809 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6810 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6811 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6812 kept around for at least as long as *RESULT_READER.
6813
b0c7bfa9
DE
6814 The result is non-zero if a valid (non-dummy) DIE was found. */
6815
6816static int
4ab09049 6817read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6818 struct dwo_unit *dwo_unit,
b0c7bfa9 6819 struct die_info *stub_comp_unit_die,
a2ce51a0 6820 const char *stub_comp_dir,
b0c7bfa9 6821 struct die_reader_specs *result_reader,
d521ce57 6822 const gdb_byte **result_info_ptr,
b0c7bfa9 6823 struct die_info **result_comp_unit_die,
685af9cd 6824 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6825{
976ca316 6826 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6827 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6828 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6829 bfd *abfd;
d521ce57 6830 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6831 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6832 int i,num_extra_attrs;
6833 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6834 struct die_info *comp_unit_die;
6835
b0aeadb3
DE
6836 /* At most one of these may be provided. */
6837 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6838
b0c7bfa9
DE
6839 /* These attributes aren't processed until later:
6840 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6841 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6842 referenced later. However, these attributes are found in the stub
6843 which we won't have later. In order to not impose this complication
6844 on the rest of the code, we read them here and copy them to the
6845 DWO CU/TU die. */
b0c7bfa9
DE
6846
6847 stmt_list = NULL;
6848 low_pc = NULL;
6849 high_pc = NULL;
6850 ranges = NULL;
6851 comp_dir = NULL;
6852
6853 if (stub_comp_unit_die != NULL)
6854 {
6855 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6856 DWO file. */
4ab09049 6857 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6858 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6859 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6860 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6861 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6862 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6863
a39fdb41 6864 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6865
18a8505e
AT
6866 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6867 here (if needed). We need the value before we can process
6868 DW_AT_ranges. */
a39fdb41 6869 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6870 }
a2ce51a0
DE
6871 else if (stub_comp_dir != NULL)
6872 {
6873 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6874 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6875 comp_dir->name = DW_AT_comp_dir;
6876 comp_dir->form = DW_FORM_string;
c6481205 6877 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6878 }
b0c7bfa9
DE
6879
6880 /* Set up for reading the DWO CU/TU. */
6881 cu->dwo_unit = dwo_unit;
685af9cd 6882 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6883 section->read (objfile);
6884 abfd = section->get_bfd_owner ();
9c541725
PA
6885 begin_info_ptr = info_ptr = (section->buffer
6886 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6887 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6888
4ab09049 6889 if (per_cu->is_debug_types)
b0c7bfa9 6890 {
4ab09049 6891 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6892
976ca316
SM
6893 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6894 section, dwo_abbrev_section,
43988095 6895 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6896 /* This is not an assert because it can be caused by bad debug info. */
43988095 6897 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6898 {
6899 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6900 " TU at offset %s [in module %s]"),
a2ce51a0 6901 hex_string (sig_type->signature),
43988095 6902 hex_string (cu->header.signature),
9d8780f0 6903 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6904 bfd_get_filename (abfd));
6905 }
9c541725 6906 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6907 /* For DWOs coming from DWP files, we don't know the CU length
6908 nor the type's offset in the TU until now. */
4057dfde 6909 dwo_unit->length = cu->header.get_length ();
9c541725 6910 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6911
6912 /* Establish the type offset that can be used to lookup the type.
6913 For DWO files, we don't know it until now. */
9c541725
PA
6914 sig_type->type_offset_in_section
6915 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6916 }
6917 else
6918 {
976ca316
SM
6919 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6920 section, dwo_abbrev_section,
43988095 6921 info_ptr, rcuh_kind::COMPILE);
9c541725 6922 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6923 /* For DWOs coming from DWP files, we don't know the CU length
6924 until now. */
4057dfde 6925 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6926 }
6927
685af9cd 6928 *result_dwo_abbrev_table
86de1d91
TT
6929 = abbrev_table::read (objfile, dwo_abbrev_section,
6930 cu->header.abbrev_sect_off);
685af9cd
TT
6931 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6932 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6933
6934 /* Read in the die, but leave space to copy over the attributes
6935 from the stub. This has the benefit of simplifying the rest of
6936 the code - all the work to maintain the illusion of a single
6937 DW_TAG_{compile,type}_unit DIE is done here. */
6938 num_extra_attrs = ((stmt_list != NULL)
6939 + (low_pc != NULL)
6940 + (high_pc != NULL)
6941 + (ranges != NULL)
6942 + (comp_dir != NULL));
6943 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6944 num_extra_attrs);
b0c7bfa9
DE
6945
6946 /* Copy over the attributes from the stub to the DIE we just read in. */
6947 comp_unit_die = *result_comp_unit_die;
6948 i = comp_unit_die->num_attrs;
6949 if (stmt_list != NULL)
6950 comp_unit_die->attrs[i++] = *stmt_list;
6951 if (low_pc != NULL)
6952 comp_unit_die->attrs[i++] = *low_pc;
6953 if (high_pc != NULL)
6954 comp_unit_die->attrs[i++] = *high_pc;
6955 if (ranges != NULL)
6956 comp_unit_die->attrs[i++] = *ranges;
6957 if (comp_dir != NULL)
6958 comp_unit_die->attrs[i++] = *comp_dir;
6959 comp_unit_die->num_attrs += num_extra_attrs;
6960
b4f54984 6961 if (dwarf_die_debug)
bf6af496
DE
6962 {
6963 fprintf_unfiltered (gdb_stdlog,
6964 "Read die from %s@0x%x of %s:\n",
96b79293 6965 section->get_name (),
bf6af496
DE
6966 (unsigned) (begin_info_ptr - section->buffer),
6967 bfd_get_filename (abfd));
b4f54984 6968 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6969 }
6970
b0c7bfa9
DE
6971 /* Skip dummy compilation units. */
6972 if (info_ptr >= begin_info_ptr + dwo_unit->length
6973 || peek_abbrev_code (abfd, info_ptr) == 0)
6974 return 0;
6975
6976 *result_info_ptr = info_ptr;
6977 return 1;
6978}
6979
a084a2a6
AT
6980/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6981 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6982 signature is part of the header. */
6983static gdb::optional<ULONGEST>
6984lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6985{
6986 if (cu->header.version >= 5)
6987 return cu->header.signature;
6988 struct attribute *attr;
6989 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6990 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6991 return gdb::optional<ULONGEST> ();
529908cb 6992 return attr->as_unsigned ();
a084a2a6
AT
6993}
6994
c0ab21c2 6995/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6996 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6997 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6998
6999static struct dwo_unit *
4ab09049 7000lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7001{
4ab09049 7002 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7003 struct dwo_unit *dwo_unit;
c0ab21c2 7004 const char *comp_dir;
b0c7bfa9 7005
a2ce51a0
DE
7006 gdb_assert (cu != NULL);
7007
b0c7bfa9 7008 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7009 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7010 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7011
4ab09049
SM
7012 if (per_cu->is_debug_types)
7013 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7014 else
7015 {
a084a2a6 7016 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7017
a084a2a6 7018 if (!signature.has_value ())
b0c7bfa9
DE
7019 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7020 " [in module %s]"),
4ab09049
SM
7021 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7022
7023 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7024 }
7025
b0c7bfa9
DE
7026 return dwo_unit;
7027}
7028
c0ab21c2 7029/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7030 See it for a description of the parameters.
fcd3b13d 7031 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7032
c0ab21c2 7033void
9e021579
SM
7034cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7035 dwarf2_per_objfile *per_objfile,
2e671100 7036 dwarf2_cu *existing_cu)
a2ce51a0 7037{
a2ce51a0 7038 struct signatured_type *sig_type;
a2ce51a0
DE
7039
7040 /* Verify we can do the following downcast, and that we have the
7041 data we need. */
7042 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7043 sig_type = (struct signatured_type *) this_cu;
7044 gdb_assert (sig_type->dwo_unit != NULL);
7045
2e671100
SM
7046 dwarf2_cu *cu;
7047
7048 if (existing_cu != nullptr)
6aa5f3a6 7049 {
2e671100
SM
7050 cu = existing_cu;
7051 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7052 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7053 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7054 }
7055 else
7056 {
7188ed02
SM
7057 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7058 in per_objfile yet. */
7059 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7060 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7061 cu = m_new_cu.get ();
6aa5f3a6
DE
7062 }
7063
7064 /* A future optimization, if needed, would be to use an existing
7065 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7066 could share abbrev tables. */
a2ce51a0 7067
2e671100 7068 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7069 NULL /* stub_comp_unit_die */,
7070 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7071 this, &info_ptr,
3e225074 7072 &comp_unit_die,
c0ab21c2 7073 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7074 {
7075 /* Dummy die. */
c0ab21c2 7076 dummy_p = true;
a2ce51a0 7077 }
a2ce51a0
DE
7078}
7079
fd820528 7080/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7081 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7082
f4dc4d17
DE
7083 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7084 Otherwise the table specified in the comp unit header is read in and used.
7085 This is an optimization for when we already have the abbrev table.
7086
2e671100
SM
7087 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7088 allocated. */
aaa75496 7089
ab432490 7090cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7091 dwarf2_per_objfile *per_objfile,
c0ab21c2 7092 struct abbrev_table *abbrev_table,
2e671100 7093 dwarf2_cu *existing_cu,
c0ab21c2
TT
7094 bool skip_partial)
7095 : die_reader_specs {},
6751ebae 7096 m_this_cu (this_cu)
c906108c 7097{
976ca316 7098 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7099 struct dwarf2_section_info *section = this_cu->section;
96b79293 7100 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7101 const gdb_byte *begin_info_ptr;
dee91e82 7102 struct signatured_type *sig_type = NULL;
4bdcc0c1 7103 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7104 /* Non-zero if CU currently points to a DWO file and we need to
7105 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7106 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7107 int rereading_dwo_cu = 0;
c906108c 7108
b4f54984 7109 if (dwarf_die_debug)
9d8780f0 7110 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7111 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7112 sect_offset_str (this_cu->sect_off));
09406207 7113
a2ce51a0
DE
7114 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7115 file (instead of going through the stub), short-circuit all of this. */
7116 if (this_cu->reading_dwo_directly)
7117 {
7118 /* Narrow down the scope of possibilities to have to understand. */
7119 gdb_assert (this_cu->is_debug_types);
7120 gdb_assert (abbrev_table == NULL);
976ca316 7121 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7122 return;
7123 }
7124
dee91e82 7125 /* This is cheap if the section is already read in. */
96b79293 7126 section->read (objfile);
dee91e82 7127
9c541725 7128 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7129
7130 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7131
2e671100
SM
7132 dwarf2_cu *cu;
7133
7134 if (existing_cu != nullptr)
dee91e82 7135 {
2e671100 7136 cu = existing_cu;
42e7ad6c
DE
7137 /* If this CU is from a DWO file we need to start over, we need to
7138 refetch the attributes from the skeleton CU.
7139 This could be optimized by retrieving those attributes from when we
7140 were here the first time: the previous comp_unit_die was stored in
7141 comp_unit_obstack. But there's no data yet that we need this
7142 optimization. */
7143 if (cu->dwo_unit != NULL)
7144 rereading_dwo_cu = 1;
dee91e82
DE
7145 }
7146 else
7147 {
7188ed02
SM
7148 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7149 in per_objfile yet. */
976ca316
SM
7150 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7151 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7152 cu = m_new_cu.get ();
42e7ad6c 7153 }
dee91e82 7154
b0c7bfa9 7155 /* Get the header. */
9c541725 7156 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7157 {
7158 /* We already have the header, there's no need to read it in again. */
9c541725 7159 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7160 }
7161 else
7162 {
3019eac3 7163 if (this_cu->is_debug_types)
dee91e82 7164 {
976ca316
SM
7165 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7166 section, abbrev_section,
7167 info_ptr, rcuh_kind::TYPE);
dee91e82 7168
42e7ad6c
DE
7169 /* Since per_cu is the first member of struct signatured_type,
7170 we can go from a pointer to one to a pointer to the other. */
7171 sig_type = (struct signatured_type *) this_cu;
43988095 7172 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7173 gdb_assert (sig_type->type_offset_in_tu
7174 == cu->header.type_cu_offset_in_tu);
7175 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7176
42e7ad6c
DE
7177 /* LENGTH has not been set yet for type units if we're
7178 using .gdb_index. */
4057dfde 7179 this_cu->length = cu->header.get_length ();
3019eac3
DE
7180
7181 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7182 sig_type->type_offset_in_section =
7183 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7184
7185 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7186 }
7187 else
7188 {
976ca316
SM
7189 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7190 section, abbrev_section,
43988095
JK
7191 info_ptr,
7192 rcuh_kind::COMPILE);
dee91e82 7193
9c541725 7194 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7195 if (this_cu->length == 0)
7196 this_cu->length = cu->header.get_length ();
7197 else
7198 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7199 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7200 }
7201 }
10b3939b 7202
6caca83c 7203 /* Skip dummy compilation units. */
dee91e82 7204 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7205 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7206 {
7207 dummy_p = true;
7208 return;
7209 }
6caca83c 7210
433df2d4
DE
7211 /* If we don't have them yet, read the abbrevs for this compilation unit.
7212 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7213 done. */
f4dc4d17 7214 if (abbrev_table != NULL)
685af9cd
TT
7215 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7216 else
f4dc4d17 7217 {
c0ab21c2 7218 m_abbrev_table_holder
86de1d91
TT
7219 = abbrev_table::read (objfile, abbrev_section,
7220 cu->header.abbrev_sect_off);
c0ab21c2 7221 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7222 }
af703f96 7223
dee91e82 7224 /* Read the top level CU/TU die. */
c0ab21c2 7225 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7226 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7227
58f0c718 7228 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7229 {
7230 dummy_p = true;
7231 return;
7232 }
58f0c718 7233
b0c7bfa9 7234 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7235 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7236 table from the DWO file and pass the ownership over to us. It will be
7237 referenced from READER, so we must make sure to free it after we're done
7238 with READER.
7239
b0c7bfa9
DE
7240 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7241 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7242 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7243 if (dwo_name != nullptr)
3019eac3 7244 {
3019eac3 7245 struct dwo_unit *dwo_unit;
b0c7bfa9 7246 struct die_info *dwo_comp_unit_die;
3019eac3 7247
3e225074 7248 if (comp_unit_die->has_children)
6a506a2d 7249 {
b98664d3 7250 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7251 " has children (offset %s) [in module %s]"),
7252 sect_offset_str (this_cu->sect_off),
7253 bfd_get_filename (abfd));
6a506a2d 7254 }
4ab09049 7255 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7256 if (dwo_unit != NULL)
3019eac3 7257 {
4ab09049 7258 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7259 comp_unit_die, NULL,
c0ab21c2 7260 this, &info_ptr,
3e225074 7261 &dwo_comp_unit_die,
c0ab21c2 7262 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7263 {
7264 /* Dummy die. */
c0ab21c2 7265 dummy_p = true;
6a506a2d
DE
7266 return;
7267 }
7268 comp_unit_die = dwo_comp_unit_die;
7269 }
7270 else
7271 {
7272 /* Yikes, we couldn't find the rest of the DIE, we only have
7273 the stub. A complaint has already been logged. There's
7274 not much more we can do except pass on the stub DIE to
7275 die_reader_func. We don't want to throw an error on bad
7276 debug info. */
3019eac3
DE
7277 }
7278 }
c0ab21c2 7279}
3019eac3 7280
6751ebae
TT
7281void
7282cutu_reader::keep ()
c0ab21c2 7283{
b0c7bfa9 7284 /* Done, clean up. */
6751ebae
TT
7285 gdb_assert (!dummy_p);
7286 if (m_new_cu != NULL)
348e048f 7287 {
7188ed02
SM
7288 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7289 now. */
7290 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7291 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7292 }
dee91e82
DE
7293}
7294
18a8505e
AT
7295/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7296 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7297 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7298
7299 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7300 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7301
7302 We fill in THIS_CU->length.
7303
dee91e82 7304 THIS_CU->cu is always freed when done.
3019eac3 7305 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7306 to care whether it refers to the "main" CU or the DWO CU.
7307
7308 When parent_cu is passed, it is used to provide a default value for
7309 str_offsets_base and addr_base from the parent. */
dee91e82 7310
ab432490 7311cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7312 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7313 struct dwarf2_cu *parent_cu,
7314 struct dwo_file *dwo_file)
7315 : die_reader_specs {},
7316 m_this_cu (this_cu)
dee91e82 7317{
976ca316 7318 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7319 struct dwarf2_section_info *section = this_cu->section;
96b79293 7320 bfd *abfd = section->get_bfd_owner ();
33e80786 7321 struct dwarf2_section_info *abbrev_section;
d521ce57 7322 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7323
b4f54984 7324 if (dwarf_die_debug)
9d8780f0 7325 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7326 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7327 sect_offset_str (this_cu->sect_off));
09406207 7328
976ca316 7329 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7330
33e80786
DE
7331 abbrev_section = (dwo_file != NULL
7332 ? &dwo_file->sections.abbrev
7333 : get_abbrev_section_for_cu (this_cu));
7334
dee91e82 7335 /* This is cheap if the section is already read in. */
96b79293 7336 section->read (objfile);
dee91e82 7337
976ca316 7338 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7339
9c541725 7340 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7341 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7342 section, abbrev_section, info_ptr,
43988095
JK
7343 (this_cu->is_debug_types
7344 ? rcuh_kind::TYPE
7345 : rcuh_kind::COMPILE));
dee91e82 7346
18a8505e
AT
7347 if (parent_cu != nullptr)
7348 {
c0ab21c2
TT
7349 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7350 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7351 }
4057dfde 7352 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7353
7354 /* Skip dummy compilation units. */
7355 if (info_ptr >= begin_info_ptr + this_cu->length
7356 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7357 {
7358 dummy_p = true;
7359 return;
7360 }
72bf9492 7361
c0ab21c2 7362 m_abbrev_table_holder
86de1d91
TT
7363 = abbrev_table::read (objfile, abbrev_section,
7364 m_new_cu->header.abbrev_sect_off);
dee91e82 7365
c0ab21c2
TT
7366 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7367 m_abbrev_table_holder.get ());
3e225074 7368 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7369}
7370
0018ea6f
DE
7371\f
7372/* Type Unit Groups.
dee91e82 7373
0018ea6f
DE
7374 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7375 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7376 so that all types coming from the same compilation (.o file) are grouped
7377 together. A future step could be to put the types in the same symtab as
7378 the CU the types ultimately came from. */
ff013f42 7379
f4dc4d17
DE
7380static hashval_t
7381hash_type_unit_group (const void *item)
7382{
9a3c8263
SM
7383 const struct type_unit_group *tu_group
7384 = (const struct type_unit_group *) item;
f4dc4d17 7385
094b34ac 7386 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7387}
348e048f
DE
7388
7389static int
f4dc4d17 7390eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7391{
9a3c8263
SM
7392 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7393 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7394
094b34ac 7395 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7396}
348e048f 7397
f4dc4d17
DE
7398/* Allocate a hash table for type unit groups. */
7399
eaa5fa8b 7400static htab_up
298e9637 7401allocate_type_unit_groups_table ()
f4dc4d17 7402{
eaa5fa8b
TT
7403 return htab_up (htab_create_alloc (3,
7404 hash_type_unit_group,
7405 eq_type_unit_group,
7406 NULL, xcalloc, xfree));
f4dc4d17 7407}
dee91e82 7408
f4dc4d17
DE
7409/* Type units that don't have DW_AT_stmt_list are grouped into their own
7410 partial symtabs. We combine several TUs per psymtab to not let the size
7411 of any one psymtab grow too big. */
7412#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7413#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7414
094b34ac 7415/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7416 Create the type_unit_group object used to hold one or more TUs. */
7417
7418static struct type_unit_group *
094b34ac 7419create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7420{
976ca316
SM
7421 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7422 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7423 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7424 struct type_unit_group *tu_group;
f4dc4d17 7425
976ca316 7426 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7427 per_cu = &tu_group->per_cu;
1859c670 7428 per_cu->per_bfd = per_bfd;
f4dc4d17 7429
1859c670 7430 if (per_bfd->using_index)
094b34ac 7431 {
1859c670 7432 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7433 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7434 }
7435 else
7436 {
9c541725 7437 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7438 dwarf2_psymtab *pst;
528e1572 7439 std::string name;
094b34ac
DE
7440
7441 /* Give the symtab a useful name for debug purposes. */
7442 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7443 name = string_printf ("<type_units_%d>",
7444 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7445 else
528e1572 7446 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7447
976ca316 7448 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7449 pst->anonymous = true;
094b34ac 7450 }
f4dc4d17 7451
094b34ac 7452 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7453 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7454
7455 return tu_group;
7456}
7457
094b34ac
DE
7458/* Look up the type_unit_group for type unit CU, and create it if necessary.
7459 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7460
7461static struct type_unit_group *
ff39bb5e 7462get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7463{
976ca316
SM
7464 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7465 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7466 struct type_unit_group *tu_group;
7467 void **slot;
7468 unsigned int line_offset;
7469 struct type_unit_group type_unit_group_for_lookup;
7470
976ca316
SM
7471 if (per_objfile->per_bfd->type_unit_groups == NULL)
7472 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7473
7474 /* Do we need to create a new group, or can we use an existing one? */
7475
529908cb 7476 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7477 {
529908cb 7478 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7479 ++tu_stats->nr_symtab_sharers;
7480 }
7481 else
7482 {
7483 /* Ugh, no stmt_list. Rare, but we have to handle it.
7484 We can do various things here like create one group per TU or
7485 spread them over multiple groups to split up the expansion work.
7486 To avoid worst case scenarios (too many groups or too large groups)
7487 we, umm, group them in bunches. */
7488 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7489 | (tu_stats->nr_stmt_less_type_units
7490 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7491 ++tu_stats->nr_stmt_less_type_units;
7492 }
7493
094b34ac 7494 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7495 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7496 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7497 &type_unit_group_for_lookup, INSERT);
7498 if (*slot != NULL)
7499 {
9a3c8263 7500 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7501 gdb_assert (tu_group != NULL);
7502 }
7503 else
7504 {
9c541725 7505 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7506 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7507 *slot = tu_group;
7508 ++tu_stats->nr_symtabs;
7509 }
7510
7511 return tu_group;
7512}
0018ea6f
DE
7513\f
7514/* Partial symbol tables. */
7515
7516/* Create a psymtab named NAME and assign it to PER_CU.
7517
7518 The caller must fill in the following details:
7519 dirname, textlow, texthigh. */
7520
891813be 7521static dwarf2_psymtab *
7aa104c4
SM
7522create_partial_symtab (dwarf2_per_cu_data *per_cu,
7523 dwarf2_per_objfile *per_objfile,
7524 const char *name)
0018ea6f 7525{
7aa104c4 7526 struct objfile *objfile = per_objfile->objfile;
891813be 7527 dwarf2_psymtab *pst;
0018ea6f 7528
9f4e76a4 7529 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7530
6d94535f 7531 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7532
7533 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7534 per_cu->v.psymtab = pst;
7535
7536 return pst;
7537}
7538
c0ab21c2 7539/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7540
7541static void
7542process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7543 const gdb_byte *info_ptr,
0018ea6f 7544 struct die_info *comp_unit_die,
c0ab21c2 7545 enum language pretend_language)
0018ea6f
DE
7546{
7547 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7548 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7549 struct objfile *objfile = per_objfile->objfile;
08feed99 7550 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7551 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7552 CORE_ADDR baseaddr;
7553 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7554 dwarf2_psymtab *pst;
3a2b436a 7555 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7556 const char *filename;
0018ea6f 7557
0018ea6f
DE
7558 gdb_assert (! per_cu->is_debug_types);
7559
c0ab21c2 7560 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7561
0018ea6f 7562 /* Allocate a new partial symbol table structure. */
2e927613
TV
7563 gdb::unique_xmalloc_ptr<char> debug_filename;
7564 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7565 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7566 if (filename == NULL)
0018ea6f 7567 filename = "";
2e927613
TV
7568 else if (strcmp (filename, artificial) == 0)
7569 {
7570 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7571 sect_offset_str (per_cu->sect_off),
7572 (char *) NULL));
2e927613
TV
7573 filename = debug_filename.get ();
7574 }
0018ea6f 7575
7aa104c4 7576 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7577
7578 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7579 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7580
b3b3bada 7581 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7582
7583 dwarf2_find_base_address (comp_unit_die, cu);
7584
7585 /* Possibly set the default values of LOWPC and HIGHPC from
7586 `DW_AT_ranges'. */
3a2b436a
JK
7587 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7588 &best_highpc, cu, pst);
7589 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7590 {
7591 CORE_ADDR low
7592 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7593 - baseaddr);
7594 CORE_ADDR high
7595 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7596 - baseaddr - 1);
7597 /* Store the contiguous range if it is not empty; it can be
7598 empty for CUs with no code. */
d320c2b5
TT
7599 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7600 low, high, pst);
79748972 7601 }
0018ea6f
DE
7602
7603 /* Check if comp unit has_children.
7604 If so, read the rest of the partial symbols from this comp unit.
7605 If not, there's no more debug_info for this comp unit. */
3e225074 7606 if (comp_unit_die->has_children)
0018ea6f
DE
7607 {
7608 struct partial_die_info *first_die;
7609 CORE_ADDR lowpc, highpc;
7610
7611 lowpc = ((CORE_ADDR) -1);
7612 highpc = ((CORE_ADDR) 0);
7613
7614 first_die = load_partial_dies (reader, info_ptr, 1);
7615
7616 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7617 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7618
7619 /* If we didn't find a lowpc, set it to highpc to avoid
7620 complaints from `maint check'. */
7621 if (lowpc == ((CORE_ADDR) -1))
7622 lowpc = highpc;
7623
7624 /* If the compilation unit didn't have an explicit address range,
7625 then use the information extracted from its child dies. */
e385593e 7626 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7627 {
7628 best_lowpc = lowpc;
7629 best_highpc = highpc;
7630 }
7631 }
4ae976d1 7632 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7633 best_lowpc + baseaddr)
7634 - baseaddr);
4ae976d1 7635 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7636 best_highpc + baseaddr)
7637 - baseaddr);
0018ea6f 7638
8763cede 7639 end_psymtab_common (objfile, pst);
0018ea6f 7640
ae640021 7641 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7642 {
7643 int i;
ae640021 7644 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7645
7646 /* Fill in 'dependencies' here; we fill in 'users' in a
7647 post-pass. */
7648 pst->number_of_dependencies = len;
a9342b62
TT
7649 pst->dependencies
7650 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7651 for (i = 0; i < len; ++i)
7652 {
7653 pst->dependencies[i]
7654 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7655 }
0018ea6f 7656
ae640021 7657 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7658 }
7659
7660 /* Get the list of files included in the current compilation unit,
7661 and build a psymtab for each of them. */
7662 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7663
b4f54984 7664 if (dwarf_read_debug)
b926417a
TT
7665 fprintf_unfiltered (gdb_stdlog,
7666 "Psymtab for %s unit @%s: %s - %s"
7667 ", %d global, %d static syms\n",
7668 per_cu->is_debug_types ? "type" : "comp",
7669 sect_offset_str (per_cu->sect_off),
7670 paddress (gdbarch, pst->text_low (objfile)),
7671 paddress (gdbarch, pst->text_high (objfile)),
932539d7
TT
7672 (int) pst->global_psymbols.size (),
7673 (int) pst->static_psymbols.size ());
0018ea6f
DE
7674}
7675
7676/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7677 Process compilation unit THIS_CU for a psymtab. */
7678
7679static void
ab432490
SM
7680process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7681 dwarf2_per_objfile *per_objfile,
135f5437 7682 bool want_partial_unit,
b93601f3 7683 enum language pretend_language)
0018ea6f
DE
7684{
7685 /* If this compilation unit was already read in, free the
7686 cached copy in order to read it in again. This is
7687 necessary because we skipped some symbols when we first
7688 read in the compilation unit (see load_partial_dies).
7689 This problem could be avoided, but the benefit is unclear. */
7188ed02 7690 per_objfile->remove_cu (this_cu);
0018ea6f 7691
2e671100 7692 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7693
58990295
TV
7694 switch (reader.comp_unit_die->tag)
7695 {
7696 case DW_TAG_compile_unit:
7697 this_cu->unit_type = DW_UT_compile;
7698 break;
7699 case DW_TAG_partial_unit:
7700 this_cu->unit_type = DW_UT_partial;
7701 break;
7702 default:
7703 abort ();
7704 }
7705
c0ab21c2 7706 if (reader.dummy_p)
f1902523 7707 {
c0ab21c2 7708 /* Nothing. */
f1902523 7709 }
c0ab21c2 7710 else if (this_cu->is_debug_types)
3e225074
TT
7711 build_type_psymtabs_reader (&reader, reader.info_ptr,
7712 reader.comp_unit_die);
135f5437
TT
7713 else if (want_partial_unit
7714 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7715 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7716 reader.comp_unit_die,
c0ab21c2 7717 pretend_language);
0018ea6f 7718
7188ed02 7719 this_cu->lang = reader.cu->language;
58990295 7720
0018ea6f 7721 /* Age out any secondary CUs. */
7188ed02 7722 per_objfile->age_comp_units ();
0018ea6f 7723}
f4dc4d17
DE
7724
7725/* Reader function for build_type_psymtabs. */
7726
7727static void
7728build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7729 const gdb_byte *info_ptr,
3e225074 7730 struct die_info *type_unit_die)
f4dc4d17 7731{
976ca316
SM
7732 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7733 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7734 struct dwarf2_cu *cu = reader->cu;
7735 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7736 struct signatured_type *sig_type;
f4dc4d17
DE
7737 struct type_unit_group *tu_group;
7738 struct attribute *attr;
7739 struct partial_die_info *first_die;
7740 CORE_ADDR lowpc, highpc;
891813be 7741 dwarf2_psymtab *pst;
f4dc4d17 7742
0186c6a7
DE
7743 gdb_assert (per_cu->is_debug_types);
7744 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7745
3e225074 7746 if (! type_unit_die->has_children)
f4dc4d17
DE
7747 return;
7748
052c8bb8 7749 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7750 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7751
df07e2c7 7752 if (tu_group->tus == nullptr)
a8b3b8e9 7753 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7754 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7755
7756 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7757 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7758 pst->anonymous = true;
f4dc4d17
DE
7759
7760 first_die = load_partial_dies (reader, info_ptr, 1);
7761
7762 lowpc = (CORE_ADDR) -1;
7763 highpc = (CORE_ADDR) 0;
7764 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7765
8763cede 7766 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7767}
7768
73051182
DE
7769/* Struct used to sort TUs by their abbreviation table offset. */
7770
7771struct tu_abbrev_offset
7772{
b2bdb8cf
SM
7773 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7774 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7775 {}
7776
7777 signatured_type *sig_type;
73051182
DE
7778 sect_offset abbrev_offset;
7779};
7780
484cf504 7781/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7782
484cf504
TT
7783static bool
7784sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7785 const struct tu_abbrev_offset &b)
73051182 7786{
484cf504 7787 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7788}
7789
7790/* Efficiently read all the type units.
7791 This does the bulk of the work for build_type_psymtabs.
7792
7793 The efficiency is because we sort TUs by the abbrev table they use and
7794 only read each abbrev table once. In one program there are 200K TUs
7795 sharing 8K abbrev tables.
7796
7797 The main purpose of this function is to support building the
5989a64e 7798 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7799 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7800 can collapse the search space by grouping them by stmt_list.
7801 The savings can be significant, in the same program from above the 200K TUs
7802 share 8K stmt_list tables.
7803
7804 FUNC is expected to call get_type_unit_group, which will create the
7805 struct type_unit_group if necessary and add it to
5989a64e 7806 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7807
7808static void
976ca316 7809build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7810{
976ca316 7811 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7812 abbrev_table_up abbrev_table;
73051182 7813 sect_offset abbrev_offset;
73051182
DE
7814
7815 /* It's up to the caller to not call us multiple times. */
976ca316 7816 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7817
976ca316 7818 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7819 return;
7820
7821 /* TUs typically share abbrev tables, and there can be way more TUs than
7822 abbrev tables. Sort by abbrev table to reduce the number of times we
7823 read each abbrev table in.
7824 Alternatives are to punt or to maintain a cache of abbrev tables.
7825 This is simpler and efficient enough for now.
7826
7827 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7828 symtab to use). Typically TUs with the same abbrev offset have the same
7829 stmt_list value too so in practice this should work well.
7830
7831 The basic algorithm here is:
7832
7833 sort TUs by abbrev table
7834 for each TU with same abbrev table:
7835 read abbrev table if first user
7836 read TU top level DIE
7837 [IWBN if DWO skeletons had DW_AT_stmt_list]
7838 call FUNC */
7839
b4f54984 7840 if (dwarf_read_debug)
73051182
DE
7841 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7842
7843 /* Sort in a separate table to maintain the order of all_type_units
7844 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7845 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7846 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7847
976ca316 7848 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7849 sorted_by_abbrev.emplace_back
976ca316 7850 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7851 sig_type->per_cu.sect_off));
73051182 7852
484cf504
TT
7853 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7854 sort_tu_by_abbrev_offset);
73051182 7855
9c541725 7856 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7857
b2bdb8cf 7858 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7859 {
73051182
DE
7860 /* Switch to the next abbrev table if necessary. */
7861 if (abbrev_table == NULL
b2bdb8cf 7862 || tu.abbrev_offset != abbrev_offset)
73051182 7863 {
b2bdb8cf 7864 abbrev_offset = tu.abbrev_offset;
73051182 7865 abbrev_table =
976ca316
SM
7866 abbrev_table::read (per_objfile->objfile,
7867 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7868 ++tu_stats->nr_uniq_abbrev_tables;
7869 }
7870
976ca316 7871 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7872 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7873 if (!reader.dummy_p)
7874 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7875 reader.comp_unit_die);
73051182 7876 }
6aa5f3a6 7877}
73051182 7878
6aa5f3a6
DE
7879/* Print collected type unit statistics. */
7880
7881static void
976ca316 7882print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7883{
976ca316 7884 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7885
7886 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7887 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7888 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7889 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7890 tu_stats->nr_uniq_abbrev_tables);
7891 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7892 tu_stats->nr_symtabs);
7893 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7894 tu_stats->nr_symtab_sharers);
7895 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7896 tu_stats->nr_stmt_less_type_units);
7897 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7898 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7899}
7900
f4dc4d17
DE
7901/* Traversal function for build_type_psymtabs. */
7902
7903static int
7904build_type_psymtab_dependencies (void **slot, void *info)
7905{
976ca316
SM
7906 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7907 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7908 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7909 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7910 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7911 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7912 int i;
7913
7914 gdb_assert (len > 0);
197400e8 7915 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7916
7917 pst->number_of_dependencies = len;
a9342b62 7918 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7919 for (i = 0; i < len; ++i)
f4dc4d17 7920 {
df07e2c7 7921 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7922 gdb_assert (iter->per_cu.is_debug_types);
7923 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7924 iter->type_unit_group = tu_group;
f4dc4d17
DE
7925 }
7926
df07e2c7
AB
7927 delete tu_group->tus;
7928 tu_group->tus = nullptr;
348e048f
DE
7929
7930 return 1;
7931}
7932
7933/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7934 Build partial symbol tables for the .debug_types comp-units. */
7935
7936static void
976ca316 7937build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7938{
976ca316 7939 if (! create_all_type_units (per_objfile))
348e048f
DE
7940 return;
7941
976ca316 7942 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7943}
f4dc4d17 7944
6aa5f3a6
DE
7945/* Traversal function for process_skeletonless_type_unit.
7946 Read a TU in a DWO file and build partial symbols for it. */
7947
7948static int
7949process_skeletonless_type_unit (void **slot, void *info)
7950{
7951 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7952 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7953 struct signatured_type find_entry, *entry;
7954
7955 /* If this TU doesn't exist in the global table, add it and read it in. */
7956
976ca316
SM
7957 if (per_objfile->per_bfd->signatured_types == NULL)
7958 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7959
7960 find_entry.signature = dwo_unit->signature;
976ca316 7961 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7962 &find_entry, INSERT);
6aa5f3a6
DE
7963 /* If we've already seen this type there's nothing to do. What's happening
7964 is we're doing our own version of comdat-folding here. */
7965 if (*slot != NULL)
7966 return 1;
7967
7968 /* This does the job that create_all_type_units would have done for
7969 this TU. */
976ca316
SM
7970 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7971 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7972 *slot = entry;
7973
7974 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7975 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7976 if (!reader.dummy_p)
7977 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7978 reader.comp_unit_die);
6aa5f3a6
DE
7979
7980 return 1;
7981}
7982
7983/* Traversal function for process_skeletonless_type_units. */
7984
7985static int
7986process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7987{
7988 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7989
7990 if (dwo_file->tus != NULL)
b0b6a987
TT
7991 htab_traverse_noresize (dwo_file->tus.get (),
7992 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7993
7994 return 1;
7995}
7996
7997/* Scan all TUs of DWO files, verifying we've processed them.
7998 This is needed in case a TU was emitted without its skeleton.
7999 Note: This can't be done until we know what all the DWO files are. */
8000
8001static void
976ca316 8002process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8003{
8004 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8005 if (get_dwp_file (per_objfile) == NULL
8006 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8007 {
976ca316 8008 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8009 process_dwo_file_for_skeletonless_type_units,
976ca316 8010 per_objfile);
6aa5f3a6 8011 }
348e048f
DE
8012}
8013
ed2dc618 8014/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8015
8016static void
976ca316 8017set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8018{
976ca316 8019 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8020 {
891813be 8021 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8022
36586728
TT
8023 if (pst == NULL)
8024 continue;
8025
b76e467d 8026 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8027 {
8028 /* Set the 'user' field only if it is not already set. */
8029 if (pst->dependencies[j]->user == NULL)
8030 pst->dependencies[j]->user = pst;
8031 }
8032 }
8033}
8034
93311388
DE
8035/* Build the partial symbol table by doing a quick pass through the
8036 .debug_info and .debug_abbrev sections. */
72bf9492 8037
93311388 8038static void
976ca316 8039dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8040{
976ca316 8041 struct objfile *objfile = per_objfile->objfile;
93311388 8042
b4f54984 8043 if (dwarf_read_debug)
45cfd468
DE
8044 {
8045 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8046 objfile_name (objfile));
45cfd468
DE
8047 }
8048
76935768 8049 scoped_restore restore_reading_psyms
976ca316 8050 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8051 true);
98bfdba5 8052
976ca316 8053 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8054
93311388
DE
8055 /* Any cached compilation units will be linked by the per-objfile
8056 read_in_chain. Make sure to free them when we're done. */
976ca316 8057 free_cached_comp_units freer (per_objfile);
72bf9492 8058
976ca316 8059 build_type_psymtabs (per_objfile);
348e048f 8060
976ca316 8061 create_all_comp_units (per_objfile);
c906108c 8062
60606b2c
TT
8063 /* Create a temporary address map on a temporary obstack. We later
8064 copy this to the final obstack. */
8268c778 8065 auto_obstack temp_obstack;
791afaa2
TT
8066
8067 scoped_restore save_psymtabs_addrmap
d320c2b5 8068 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8069 addrmap_create_mutable (&temp_obstack));
72bf9492 8070
976ca316 8071 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8072 {
8073 if (per_cu->v.psymtab != NULL)
8074 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8075 continue;
976ca316 8076 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8077 language_minimal);
3d5afab3 8078 }
ff013f42 8079
6aa5f3a6 8080 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8081 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8082
8083 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8084 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8085 {
976ca316
SM
8086 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8087 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8088 }
8089
b4f54984 8090 if (dwarf_read_debug)
976ca316 8091 print_tu_stats (per_objfile);
6aa5f3a6 8092
976ca316 8093 set_partial_user (per_objfile);
95554aad 8094
d320c2b5
TT
8095 objfile->partial_symtabs->psymtabs_addrmap
8096 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8097 objfile->partial_symtabs->obstack ());
791afaa2
TT
8098 /* At this point we want to keep the address map. */
8099 save_psymtabs_addrmap.release ();
ff013f42 8100
b4f54984 8101 if (dwarf_read_debug)
45cfd468 8102 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8103 objfile_name (objfile));
ae038cb0
DJ
8104}
8105
dee91e82
DE
8106/* Load the partial DIEs for a secondary CU into memory.
8107 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8108
dee91e82 8109static void
ab432490 8110load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8111 dwarf2_per_objfile *per_objfile,
8112 dwarf2_cu *existing_cu)
dee91e82 8113{
2e671100 8114 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8115
8116 if (!reader.dummy_p)
8117 {
8118 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8119 language_minimal);
8120
8121 /* Check if comp unit has_children.
8122 If so, read the rest of the partial symbols from this comp unit.
8123 If not, there's no more debug_info for this comp unit. */
3e225074 8124 if (reader.comp_unit_die->has_children)
c0ab21c2 8125 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8126
8127 reader.keep ();
c0ab21c2 8128 }
ae038cb0
DJ
8129}
8130
ae038cb0 8131static void
976ca316 8132read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8133 struct dwarf2_section_info *section,
f1902523 8134 struct dwarf2_section_info *abbrev_section,
b76e467d 8135 unsigned int is_dwz)
ae038cb0 8136{
d521ce57 8137 const gdb_byte *info_ptr;
976ca316 8138 struct objfile *objfile = per_objfile->objfile;
be391dca 8139
b4f54984 8140 if (dwarf_read_debug)
bf6af496 8141 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8142 section->get_name (),
8143 section->get_file_name ());
bf6af496 8144
96b79293 8145 section->read (objfile);
ae038cb0 8146
36586728 8147 info_ptr = section->buffer;
6e70227d 8148
36586728 8149 while (info_ptr < section->buffer + section->size)
ae038cb0 8150 {
ae038cb0 8151 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8152
9c541725 8153 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8154
f1902523 8155 comp_unit_head cu_header;
976ca316 8156 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8157 abbrev_section, info_ptr,
8158 rcuh_kind::COMPILE);
ae038cb0
DJ
8159
8160 /* Save the compilation unit for later lookup. */
f1902523 8161 if (cu_header.unit_type != DW_UT_type)
976ca316 8162 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8163 else
8164 {
976ca316 8165 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8166 sig_type->signature = cu_header.signature;
8167 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8168 this_cu = &sig_type->per_cu;
8169 }
8170 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8171 this_cu->sect_off = sect_off;
f1902523 8172 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8173 this_cu->is_dwz = is_dwz;
8a0459fd 8174 this_cu->section = section;
ae038cb0 8175
976ca316 8176 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8177
8178 info_ptr = info_ptr + this_cu->length;
8179 }
36586728
TT
8180}
8181
8182/* Create a list of all compilation units in OBJFILE.
8183 This is only done for -readnow and building partial symtabs. */
8184
8185static void
976ca316 8186create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8187{
976ca316
SM
8188 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8189 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8190 &per_objfile->per_bfd->abbrev, 0);
36586728 8191
976ca316 8192 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8193 if (dwz != NULL)
976ca316 8194 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8195}
8196
5734ee8b 8197/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8198 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8199 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8200 DW_AT_ranges). See the comments of add_partial_subprogram on how
8201 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8202
72bf9492
DJ
8203static void
8204scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8205 CORE_ADDR *highpc, int set_addrmap,
8206 struct dwarf2_cu *cu)
c906108c 8207{
72bf9492 8208 struct partial_die_info *pdi;
c906108c 8209
91c24f0a
DC
8210 /* Now, march along the PDI's, descending into ones which have
8211 interesting children but skipping the children of the other ones,
8212 until we reach the end of the compilation unit. */
c906108c 8213
72bf9492 8214 pdi = first_die;
91c24f0a 8215
72bf9492
DJ
8216 while (pdi != NULL)
8217 {
52356b79 8218 pdi->fixup (cu);
c906108c 8219
f55ee35c 8220 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8221 children, so we need to look at them. Ditto for anonymous
8222 enums. */
933c6fe4 8223
7d00ffec 8224 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8225 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8226 || pdi->tag == DW_TAG_imported_unit
8227 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8228 {
72bf9492 8229 switch (pdi->tag)
c906108c
SS
8230 {
8231 case DW_TAG_subprogram:
b1dc1806 8232 case DW_TAG_inlined_subroutine:
cdc07690 8233 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8234 if (cu->language == language_cplus)
8235 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8236 set_addrmap, cu);
c906108c 8237 break;
72929c62 8238 case DW_TAG_constant:
c906108c
SS
8239 case DW_TAG_variable:
8240 case DW_TAG_typedef:
91c24f0a 8241 case DW_TAG_union_type:
317d2668
TV
8242 if (!pdi->is_declaration
8243 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8244 {
72bf9492 8245 add_partial_symbol (pdi, cu);
63d06c5c
DC
8246 }
8247 break;
c906108c 8248 case DW_TAG_class_type:
680b30c7 8249 case DW_TAG_interface_type:
c906108c 8250 case DW_TAG_structure_type:
72bf9492 8251 if (!pdi->is_declaration)
c906108c 8252 {
72bf9492 8253 add_partial_symbol (pdi, cu);
c906108c 8254 }
b7fee5a3
KS
8255 if ((cu->language == language_rust
8256 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8257 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8258 set_addrmap, cu);
c906108c 8259 break;
91c24f0a 8260 case DW_TAG_enumeration_type:
72bf9492
DJ
8261 if (!pdi->is_declaration)
8262 add_partial_enumeration (pdi, cu);
c906108c
SS
8263 break;
8264 case DW_TAG_base_type:
a02abb62 8265 case DW_TAG_subrange_type:
c906108c 8266 /* File scope base type definitions are added to the partial
c5aa993b 8267 symbol table. */
72bf9492 8268 add_partial_symbol (pdi, cu);
c906108c 8269 break;
d9fa45fe 8270 case DW_TAG_namespace:
cdc07690 8271 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8272 break;
5d7cb8df 8273 case DW_TAG_module:
59c35742
AB
8274 if (!pdi->is_declaration)
8275 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8276 break;
95554aad
TT
8277 case DW_TAG_imported_unit:
8278 {
8279 struct dwarf2_per_cu_data *per_cu;
8280
f4dc4d17
DE
8281 /* For now we don't handle imported units in type units. */
8282 if (cu->per_cu->is_debug_types)
8283 {
8284 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8285 " supported in type units [in module %s]"),
5e22e966 8286 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8287 }
8288
e3b94546 8289 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8290 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8291
8292 /* Go read the partial unit, if needed. */
8293 if (per_cu->v.psymtab == NULL)
ab432490
SM
8294 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8295 cu->language);
95554aad 8296
ae640021 8297 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8298 }
8299 break;
74921315
KS
8300 case DW_TAG_imported_declaration:
8301 add_partial_symbol (pdi, cu);
8302 break;
c906108c
SS
8303 default:
8304 break;
8305 }
8306 }
8307
72bf9492
DJ
8308 /* If the die has a sibling, skip to the sibling. */
8309
8310 pdi = pdi->die_sibling;
8311 }
8312}
8313
8314/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8315
72bf9492 8316 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8317 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8318 Enumerators are an exception; they use the scope of their parent
8319 enumeration type, i.e. the name of the enumeration type is not
8320 prepended to the enumerator.
91c24f0a 8321
72bf9492
DJ
8322 There are two complexities. One is DW_AT_specification; in this
8323 case "parent" means the parent of the target of the specification,
8324 instead of the direct parent of the DIE. The other is compilers
8325 which do not emit DW_TAG_namespace; in this case we try to guess
8326 the fully qualified name of structure types from their members'
8327 linkage names. This must be done using the DIE's children rather
8328 than the children of any DW_AT_specification target. We only need
8329 to do this for structures at the top level, i.e. if the target of
8330 any DW_AT_specification (if any; otherwise the DIE itself) does not
8331 have a parent. */
8332
8333/* Compute the scope prefix associated with PDI's parent, in
8334 compilation unit CU. The result will be allocated on CU's
8335 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8336 field. NULL is returned if no prefix is necessary. */
15d034d0 8337static const char *
72bf9492
DJ
8338partial_die_parent_scope (struct partial_die_info *pdi,
8339 struct dwarf2_cu *cu)
8340{
15d034d0 8341 const char *grandparent_scope;
72bf9492 8342 struct partial_die_info *parent, *real_pdi;
91c24f0a 8343
72bf9492
DJ
8344 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8345 then this means the parent of the specification DIE. */
8346
8347 real_pdi = pdi;
72bf9492 8348 while (real_pdi->has_specification)
fb816e8b 8349 {
122cf0f2
AB
8350 auto res = find_partial_die (real_pdi->spec_offset,
8351 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8352 real_pdi = res.pdi;
8353 cu = res.cu;
8354 }
72bf9492
DJ
8355
8356 parent = real_pdi->die_parent;
8357 if (parent == NULL)
8358 return NULL;
8359
8360 if (parent->scope_set)
8361 return parent->scope;
8362
52356b79 8363 parent->fixup (cu);
72bf9492 8364
10b3939b 8365 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8366
acebe513
UW
8367 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8368 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8369 Work around this problem here. */
8370 if (cu->language == language_cplus
6e70227d 8371 && parent->tag == DW_TAG_namespace
7d00ffec 8372 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8373 && grandparent_scope == NULL)
8374 {
8375 parent->scope = NULL;
8376 parent->scope_set = 1;
8377 return NULL;
8378 }
8379
0a4b0913 8380 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8381 if (pdi->tag == DW_TAG_enumerator)
8382 /* Enumerators should not get the name of the enumeration as a prefix. */
8383 parent->scope = grandparent_scope;
8384 else if (parent->tag == DW_TAG_namespace
f55ee35c 8385 || parent->tag == DW_TAG_module
72bf9492
DJ
8386 || parent->tag == DW_TAG_structure_type
8387 || parent->tag == DW_TAG_class_type
680b30c7 8388 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8389 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8390 || parent->tag == DW_TAG_enumeration_type
8391 || (cu->language == language_fortran
8392 && parent->tag == DW_TAG_subprogram
8393 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8394 {
8395 if (grandparent_scope == NULL)
7d00ffec 8396 parent->scope = parent->name (cu);
72bf9492 8397 else
3e43a32a
MS
8398 parent->scope = typename_concat (&cu->comp_unit_obstack,
8399 grandparent_scope,
7d00ffec 8400 parent->name (cu), 0, cu);
72bf9492 8401 }
72bf9492
DJ
8402 else
8403 {
8404 /* FIXME drow/2004-04-01: What should we be doing with
8405 function-local names? For partial symbols, we should probably be
8406 ignoring them. */
fa9c3fa0
TT
8407 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8408 dwarf_tag_name (parent->tag),
8409 sect_offset_str (pdi->sect_off));
72bf9492 8410 parent->scope = grandparent_scope;
c906108c
SS
8411 }
8412
72bf9492
DJ
8413 parent->scope_set = 1;
8414 return parent->scope;
8415}
8416
8417/* Return the fully scoped name associated with PDI, from compilation unit
8418 CU. The result will be allocated with malloc. */
4568ecf9 8419
43816ebc 8420static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8421partial_die_full_name (struct partial_die_info *pdi,
8422 struct dwarf2_cu *cu)
8423{
15d034d0 8424 const char *parent_scope;
72bf9492 8425
98bfdba5
PA
8426 /* If this is a template instantiation, we can not work out the
8427 template arguments from partial DIEs. So, unfortunately, we have
8428 to go through the full DIEs. At least any work we do building
8429 types here will be reused if full symbols are loaded later. */
8430 if (pdi->has_template_arguments)
8431 {
52356b79 8432 pdi->fixup (cu);
98bfdba5 8433
7d00ffec 8434 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8435 {
8436 struct die_info *die;
8437 struct attribute attr;
8438 struct dwarf2_cu *ref_cu = cu;
8439
b64f50a1 8440 /* DW_FORM_ref_addr is using section offset. */
b4069958 8441 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8442 attr.form = DW_FORM_ref_addr;
9c541725 8443 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8444 die = follow_die_ref (NULL, &attr, &ref_cu);
8445
43816ebc 8446 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8447 }
8448 }
8449
72bf9492
DJ
8450 parent_scope = partial_die_parent_scope (pdi, cu);
8451 if (parent_scope == NULL)
8452 return NULL;
8453 else
43816ebc 8454 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8455 pdi->name (cu),
8456 0, cu));
c906108c
SS
8457}
8458
8459static void
72bf9492 8460add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8461{
976ca316
SM
8462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8463 struct objfile *objfile = per_objfile->objfile;
08feed99 8464 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8465 CORE_ADDR addr = 0;
15d034d0 8466 const char *actual_name = NULL;
e142c38c
DJ
8467 CORE_ADDR baseaddr;
8468
b3b3bada 8469 baseaddr = objfile->text_section_offset ();
c906108c 8470
43816ebc
TT
8471 gdb::unique_xmalloc_ptr<char> built_actual_name
8472 = partial_die_full_name (pdi, cu);
15d034d0 8473 if (built_actual_name != NULL)
43816ebc 8474 actual_name = built_actual_name.get ();
63d06c5c 8475
72bf9492 8476 if (actual_name == NULL)
7d00ffec 8477 actual_name = pdi->name (cu);
72bf9492 8478
76e288d1
TT
8479 partial_symbol psymbol;
8480 memset (&psymbol, 0, sizeof (psymbol));
8481 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8482 psymbol.ginfo.section = -1;
8483
8484 /* The code below indicates that the psymbol should be installed by
8485 setting this. */
8486 gdb::optional<psymbol_placement> where;
8487
c906108c
SS
8488 switch (pdi->tag)
8489 {
b1dc1806 8490 case DW_TAG_inlined_subroutine:
c906108c 8491 case DW_TAG_subprogram:
79748972
TT
8492 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8493 - baseaddr);
0a4b0913
AB
8494 if (pdi->is_external
8495 || cu->language == language_ada
8496 || (cu->language == language_fortran
8497 && pdi->die_parent != NULL
8498 && pdi->die_parent->tag == DW_TAG_subprogram))
8499 {
8500 /* Normally, only "external" DIEs are part of the global scope.
8501 But in Ada and Fortran, we want to be able to access nested
8502 procedures globally. So all Ada and Fortran subprograms are
8503 stored in the global scope. */
76e288d1 8504 where = psymbol_placement::GLOBAL;
c906108c
SS
8505 }
8506 else
76e288d1
TT
8507 where = psymbol_placement::STATIC;
8508
8509 psymbol.domain = VAR_DOMAIN;
8510 psymbol.aclass = LOC_BLOCK;
8511 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8512 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8513
8514 if (pdi->main_subprogram && actual_name != NULL)
8515 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8516 break;
72929c62 8517 case DW_TAG_constant:
76e288d1
TT
8518 psymbol.domain = VAR_DOMAIN;
8519 psymbol.aclass = LOC_STATIC;
8520 where = (pdi->is_external
8521 ? psymbol_placement::GLOBAL
8522 : psymbol_placement::STATIC);
72929c62 8523 break;
c906108c 8524 case DW_TAG_variable:
95554aad
TT
8525 if (pdi->d.locdesc)
8526 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8527
95554aad 8528 if (pdi->d.locdesc
caac4577 8529 && addr == 0
976ca316 8530 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8531 {
8532 /* A global or static variable may also have been stripped
8533 out by the linker if unused, in which case its address
8534 will be nullified; do not add such variables into partial
8535 symbol table then. */
8536 }
8537 else if (pdi->is_external)
c906108c
SS
8538 {
8539 /* Global Variable.
8540 Don't enter into the minimal symbol tables as there is
8541 a minimal symbol table entry from the ELF symbols already.
8542 Enter into partial symbol table if it has a location
8543 descriptor or a type.
8544 If the location descriptor is missing, new_symbol will create
8545 a LOC_UNRESOLVED symbol, the address of the variable will then
8546 be determined from the minimal symbol table whenever the variable
8547 is referenced.
8548 The address for the partial symbol table entry is not
8549 used by GDB, but it comes in handy for debugging partial symbol
8550 table building. */
8551
95554aad 8552 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8553 {
8554 psymbol.domain = VAR_DOMAIN;
8555 psymbol.aclass = LOC_STATIC;
8556 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8557 psymbol.ginfo.value.address = addr;
8558 where = psymbol_placement::GLOBAL;
8559 }
c906108c
SS
8560 }
8561 else
8562 {
ff908ebf
AW
8563 int has_loc = pdi->d.locdesc != NULL;
8564
8565 /* Static Variable. Skip symbols whose value we cannot know (those
8566 without location descriptors or constant values). */
8567 if (!has_loc && !pdi->has_const_value)
43816ebc 8568 return;
ff908ebf 8569
76e288d1
TT
8570 psymbol.domain = VAR_DOMAIN;
8571 psymbol.aclass = LOC_STATIC;
8572 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8573 if (has_loc)
8574 psymbol.ginfo.value.address = addr;
8575 where = psymbol_placement::STATIC;
c906108c
SS
8576 }
8577 break;
8578 case DW_TAG_typedef:
8579 case DW_TAG_base_type:
a02abb62 8580 case DW_TAG_subrange_type:
76e288d1
TT
8581 psymbol.domain = VAR_DOMAIN;
8582 psymbol.aclass = LOC_TYPEDEF;
8583 where = psymbol_placement::STATIC;
c906108c 8584 break;
74921315 8585 case DW_TAG_imported_declaration:
72bf9492 8586 case DW_TAG_namespace:
76e288d1
TT
8587 psymbol.domain = VAR_DOMAIN;
8588 psymbol.aclass = LOC_TYPEDEF;
8589 where = psymbol_placement::GLOBAL;
72bf9492 8590 break;
530e8392 8591 case DW_TAG_module:
a5fd13a9
BH
8592 /* With Fortran 77 there might be a "BLOCK DATA" module
8593 available without any name. If so, we skip the module as it
8594 doesn't bring any value. */
8595 if (actual_name != nullptr)
76e288d1
TT
8596 {
8597 psymbol.domain = MODULE_DOMAIN;
8598 psymbol.aclass = LOC_TYPEDEF;
8599 where = psymbol_placement::GLOBAL;
8600 }
530e8392 8601 break;
c906108c 8602 case DW_TAG_class_type:
680b30c7 8603 case DW_TAG_interface_type:
c906108c
SS
8604 case DW_TAG_structure_type:
8605 case DW_TAG_union_type:
8606 case DW_TAG_enumeration_type:
fa4028e9
JB
8607 /* Skip external references. The DWARF standard says in the section
8608 about "Structure, Union, and Class Type Entries": "An incomplete
8609 structure, union or class type is represented by a structure,
8610 union or class entry that does not have a byte size attribute
8611 and that has a DW_AT_declaration attribute." */
8612 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8613 return;
fa4028e9 8614
63d06c5c
DC
8615 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8616 static vs. global. */
76e288d1
TT
8617 psymbol.domain = STRUCT_DOMAIN;
8618 psymbol.aclass = LOC_TYPEDEF;
8619 where = (cu->language == language_cplus
8620 ? psymbol_placement::GLOBAL
8621 : psymbol_placement::STATIC);
c906108c
SS
8622 break;
8623 case DW_TAG_enumerator:
76e288d1
TT
8624 psymbol.domain = VAR_DOMAIN;
8625 psymbol.aclass = LOC_CONST;
8626 where = (cu->language == language_cplus
8627 ? psymbol_placement::GLOBAL
8628 : psymbol_placement::STATIC);
c906108c
SS
8629 break;
8630 default:
8631 break;
8632 }
76e288d1
TT
8633
8634 if (where.has_value ())
8635 {
f049a313
TT
8636 if (built_actual_name != nullptr)
8637 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8638 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8639 psymbol.ginfo.set_linkage_name (actual_name);
8640 else
8641 {
8642 psymbol.ginfo.set_demangled_name (actual_name,
8643 &objfile->objfile_obstack);
8644 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8645 }
932539d7 8646 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8647 }
c906108c
SS
8648}
8649
5c4e30ca
DC
8650/* Read a partial die corresponding to a namespace; also, add a symbol
8651 corresponding to that namespace to the symbol table. NAMESPACE is
8652 the name of the enclosing namespace. */
91c24f0a 8653
72bf9492
DJ
8654static void
8655add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8656 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8657 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8658{
72bf9492 8659 /* Add a symbol for the namespace. */
e7c27a73 8660
72bf9492 8661 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8662
8663 /* Now scan partial symbols in that namespace. */
8664
91c24f0a 8665 if (pdi->has_children)
cdc07690 8666 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8667}
8668
5d7cb8df
JK
8669/* Read a partial die corresponding to a Fortran module. */
8670
8671static void
8672add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8673 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8674{
530e8392
KB
8675 /* Add a symbol for the namespace. */
8676
8677 add_partial_symbol (pdi, cu);
8678
f55ee35c 8679 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8680
8681 if (pdi->has_children)
cdc07690 8682 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8683}
8684
b1dc1806
XR
8685/* Read a partial die corresponding to a subprogram or an inlined
8686 subprogram and create a partial symbol for that subprogram.
8687 When the CU language allows it, this routine also defines a partial
8688 symbol for each nested subprogram that this subprogram contains.
8689 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8690 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8691
cdc07690
YQ
8692 PDI may also be a lexical block, in which case we simply search
8693 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8694 Again, this is only performed when the CU language allows this
8695 type of definitions. */
8696
8697static void
8698add_partial_subprogram (struct partial_die_info *pdi,
8699 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8700 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8701{
b1dc1806 8702 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8703 {
8704 if (pdi->has_pc_info)
8705 {
8706 if (pdi->lowpc < *lowpc)
8707 *lowpc = pdi->lowpc;
8708 if (pdi->highpc > *highpc)
8709 *highpc = pdi->highpc;
cdc07690 8710 if (set_addrmap)
5734ee8b 8711 {
5e22e966 8712 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8713 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8714 CORE_ADDR baseaddr;
b926417a
TT
8715 CORE_ADDR this_highpc;
8716 CORE_ADDR this_lowpc;
5734ee8b 8717
b3b3bada 8718 baseaddr = objfile->text_section_offset ();
b926417a
TT
8719 this_lowpc
8720 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8721 pdi->lowpc + baseaddr)
8722 - baseaddr);
8723 this_highpc
8724 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8725 pdi->highpc + baseaddr)
8726 - baseaddr);
d320c2b5 8727 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8728 this_lowpc, this_highpc - 1,
9291a0cd 8729 cu->per_cu->v.psymtab);
5734ee8b 8730 }
481860b3
GB
8731 }
8732
8733 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8734 {
bc30ff58 8735 if (!pdi->is_declaration)
e8d05480
JB
8736 /* Ignore subprogram DIEs that do not have a name, they are
8737 illegal. Do not emit a complaint at this point, we will
8738 do so when we convert this psymtab into a symtab. */
7d00ffec 8739 if (pdi->name (cu))
e8d05480 8740 add_partial_symbol (pdi, cu);
bc30ff58
JB
8741 }
8742 }
6e70227d 8743
bc30ff58
JB
8744 if (! pdi->has_children)
8745 return;
8746
0a4b0913 8747 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8748 {
8749 pdi = pdi->die_child;
8750 while (pdi != NULL)
8751 {
52356b79 8752 pdi->fixup (cu);
bc30ff58 8753 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8754 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8755 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8756 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8757 pdi = pdi->die_sibling;
8758 }
8759 }
8760}
8761
91c24f0a
DC
8762/* Read a partial die corresponding to an enumeration type. */
8763
72bf9492
DJ
8764static void
8765add_partial_enumeration (struct partial_die_info *enum_pdi,
8766 struct dwarf2_cu *cu)
91c24f0a 8767{
72bf9492 8768 struct partial_die_info *pdi;
91c24f0a 8769
7d00ffec 8770 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8771 add_partial_symbol (enum_pdi, cu);
8772
8773 pdi = enum_pdi->die_child;
8774 while (pdi)
91c24f0a 8775 {
7d00ffec 8776 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8777 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8778 else
72bf9492
DJ
8779 add_partial_symbol (pdi, cu);
8780 pdi = pdi->die_sibling;
91c24f0a 8781 }
91c24f0a
DC
8782}
8783
6caca83c
CC
8784/* Return the initial uleb128 in the die at INFO_PTR. */
8785
8786static unsigned int
d521ce57 8787peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8788{
8789 unsigned int bytes_read;
8790
8791 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8792}
8793
685af9cd
TT
8794/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8795 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8796
4bb7a0a7
DJ
8797 Return the corresponding abbrev, or NULL if the number is zero (indicating
8798 an empty DIE). In either case *BYTES_READ will be set to the length of
8799 the initial number. */
8800
8801static struct abbrev_info *
685af9cd
TT
8802peek_die_abbrev (const die_reader_specs &reader,
8803 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8804{
685af9cd 8805 dwarf2_cu *cu = reader.cu;
5e22e966 8806 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8807 unsigned int abbrev_number
8808 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8809
8810 if (abbrev_number == 0)
8811 return NULL;
8812
685af9cd 8813 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8814 if (!abbrev)
8815 {
422b9917 8816 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8817 " at offset %s [in module %s]"),
422b9917 8818 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8819 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8820 }
8821
8822 return abbrev;
8823}
8824
93311388
DE
8825/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8826 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8827 DIE. Any children of the skipped DIEs will also be skipped. */
8828
d521ce57
TT
8829static const gdb_byte *
8830skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8831{
4bb7a0a7
DJ
8832 while (1)
8833 {
685af9cd
TT
8834 unsigned int bytes_read;
8835 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8836
4bb7a0a7
DJ
8837 if (abbrev == NULL)
8838 return info_ptr + bytes_read;
8839 else
dee91e82 8840 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8841 }
8842}
8843
93311388
DE
8844/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8845 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8846 abbrev corresponding to that skipped uleb128 should be passed in
8847 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8848 children. */
8849
d521ce57
TT
8850static const gdb_byte *
8851skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8852 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8853{
8854 unsigned int bytes_read;
8855 struct attribute attr;
dee91e82
DE
8856 bfd *abfd = reader->abfd;
8857 struct dwarf2_cu *cu = reader->cu;
d521ce57 8858 const gdb_byte *buffer = reader->buffer;
f664829e 8859 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8860 unsigned int form, i;
8861
8862 for (i = 0; i < abbrev->num_attrs; i++)
8863 {
8864 /* The only abbrev we care about is DW_AT_sibling. */
8865 if (abbrev->attrs[i].name == DW_AT_sibling)
8866 {
7a5f294d 8867 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8868 if (attr.form == DW_FORM_ref_addr)
b98664d3 8869 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8870 else
b9502d3f 8871 {
0826b30a 8872 sect_offset off = attr.get_ref_die_offset ();
9c541725 8873 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8874
8875 if (sibling_ptr < info_ptr)
b98664d3 8876 complaint (_("DW_AT_sibling points backwards"));
22869d73 8877 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8878 reader->die_section->overflow_complaint ();
b9502d3f
WN
8879 else
8880 return sibling_ptr;
8881 }
4bb7a0a7
DJ
8882 }
8883
8884 /* If it isn't DW_AT_sibling, skip this attribute. */
8885 form = abbrev->attrs[i].form;
8886 skip_attribute:
8887 switch (form)
8888 {
4bb7a0a7 8889 case DW_FORM_ref_addr:
ae411497
TT
8890 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8891 and later it is offset sized. */
8892 if (cu->header.version == 2)
8893 info_ptr += cu->header.addr_size;
8894 else
8895 info_ptr += cu->header.offset_size;
8896 break;
36586728
TT
8897 case DW_FORM_GNU_ref_alt:
8898 info_ptr += cu->header.offset_size;
8899 break;
ae411497 8900 case DW_FORM_addr:
4bb7a0a7
DJ
8901 info_ptr += cu->header.addr_size;
8902 break;
8903 case DW_FORM_data1:
8904 case DW_FORM_ref1:
8905 case DW_FORM_flag:
8fe0f950 8906 case DW_FORM_strx1:
4bb7a0a7
DJ
8907 info_ptr += 1;
8908 break;
2dc7f7b3 8909 case DW_FORM_flag_present:
43988095 8910 case DW_FORM_implicit_const:
2dc7f7b3 8911 break;
4bb7a0a7
DJ
8912 case DW_FORM_data2:
8913 case DW_FORM_ref2:
8fe0f950 8914 case DW_FORM_strx2:
4bb7a0a7
DJ
8915 info_ptr += 2;
8916 break;
8fe0f950
AT
8917 case DW_FORM_strx3:
8918 info_ptr += 3;
8919 break;
4bb7a0a7
DJ
8920 case DW_FORM_data4:
8921 case DW_FORM_ref4:
8fe0f950 8922 case DW_FORM_strx4:
4bb7a0a7
DJ
8923 info_ptr += 4;
8924 break;
8925 case DW_FORM_data8:
8926 case DW_FORM_ref8:
55f1336d 8927 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8928 info_ptr += 8;
8929 break;
0224619f
JK
8930 case DW_FORM_data16:
8931 info_ptr += 16;
8932 break;
4bb7a0a7 8933 case DW_FORM_string:
9b1c24c8 8934 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8935 info_ptr += bytes_read;
8936 break;
2dc7f7b3 8937 case DW_FORM_sec_offset:
4bb7a0a7 8938 case DW_FORM_strp:
36586728 8939 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8940 info_ptr += cu->header.offset_size;
8941 break;
2dc7f7b3 8942 case DW_FORM_exprloc:
4bb7a0a7
DJ
8943 case DW_FORM_block:
8944 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8945 info_ptr += bytes_read;
8946 break;
8947 case DW_FORM_block1:
8948 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8949 break;
8950 case DW_FORM_block2:
8951 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8952 break;
8953 case DW_FORM_block4:
8954 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8955 break;
336d760d 8956 case DW_FORM_addrx:
cf532bd1 8957 case DW_FORM_strx:
4bb7a0a7
DJ
8958 case DW_FORM_sdata:
8959 case DW_FORM_udata:
8960 case DW_FORM_ref_udata:
3019eac3
DE
8961 case DW_FORM_GNU_addr_index:
8962 case DW_FORM_GNU_str_index:
18a8505e 8963 case DW_FORM_rnglistx:
41144253 8964 case DW_FORM_loclistx:
d521ce57 8965 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8966 break;
8967 case DW_FORM_indirect:
8968 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8969 info_ptr += bytes_read;
8970 /* We need to continue parsing from here, so just go back to
8971 the top. */
8972 goto skip_attribute;
8973
8974 default:
3e43a32a
MS
8975 error (_("Dwarf Error: Cannot handle %s "
8976 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8977 dwarf_form_name (form),
8978 bfd_get_filename (abfd));
8979 }
8980 }
8981
8982 if (abbrev->has_children)
dee91e82 8983 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8984 else
8985 return info_ptr;
8986}
8987
93311388 8988/* Locate ORIG_PDI's sibling.
dee91e82 8989 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8990
d521ce57 8991static const gdb_byte *
dee91e82
DE
8992locate_pdi_sibling (const struct die_reader_specs *reader,
8993 struct partial_die_info *orig_pdi,
d521ce57 8994 const gdb_byte *info_ptr)
91c24f0a
DC
8995{
8996 /* Do we know the sibling already? */
72bf9492 8997
91c24f0a
DC
8998 if (orig_pdi->sibling)
8999 return orig_pdi->sibling;
9000
9001 /* Are there any children to deal with? */
9002
9003 if (!orig_pdi->has_children)
9004 return info_ptr;
9005
4bb7a0a7 9006 /* Skip the children the long way. */
91c24f0a 9007
dee91e82 9008 return skip_children (reader, info_ptr);
91c24f0a
DC
9009}
9010
257e7a09 9011/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9012 not NULL. */
c906108c 9013
891813be
TT
9014void
9015dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9016{
976ca316 9017 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9018
976ca316 9019 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9020
077cbab2
TT
9021 /* If this psymtab is constructed from a debug-only objfile, the
9022 has_section_at_zero flag will not necessarily be correct. We
9023 can get the correct value for this flag by looking at the data
9024 associated with the (presumably stripped) associated objfile. */
9025 if (objfile->separate_debug_objfile_backlink)
c906108c 9026 {
976ca316 9027 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9028 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9029
976ca316
SM
9030 per_objfile->per_bfd->has_section_at_zero
9031 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9032 }
98bfdba5 9033
8566b89b 9034 expand_psymtab (objfile);
95554aad 9035
976ca316 9036 process_cu_includes (per_objfile);
c906108c 9037}
9cdd5dbd
DE
9038\f
9039/* Reading in full CUs. */
c906108c 9040
10b3939b
DJ
9041/* Add PER_CU to the queue. */
9042
9043static void
120ce1b5
SM
9044queue_comp_unit (dwarf2_per_cu_data *per_cu,
9045 dwarf2_per_objfile *per_objfile,
95554aad 9046 enum language pretend_language)
10b3939b 9047{
10b3939b 9048 per_cu->queued = 1;
120ce1b5 9049 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9050}
9051
89e63ee4
DE
9052/* If PER_CU is not yet queued, add it to the queue.
9053 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9054 dependency.
0907af0c 9055 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9056 meaning either PER_CU is already queued or it is already loaded.
9057
9058 N.B. There is an invariant here that if a CU is queued then it is loaded.
9059 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9060
9061static int
89e63ee4 9062maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9063 dwarf2_per_cu_data *per_cu,
9064 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9065 enum language pretend_language)
9066{
9067 /* We may arrive here during partial symbol reading, if we need full
9068 DIEs to process an unusual case (e.g. template arguments). Do
9069 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9070 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9071 {
7188ed02
SM
9072 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9073
9074 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9075 return 1;
9076 return 0;
9077 }
9078
9079 /* Mark the dependence relation so that we don't flush PER_CU
9080 too early. */
89e63ee4
DE
9081 if (dependent_cu != NULL)
9082 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9083
9084 /* If it's already on the queue, we have nothing to do. */
9085 if (per_cu->queued)
9086 return 0;
9087
9088 /* If the compilation unit is already loaded, just mark it as
9089 used. */
7188ed02
SM
9090 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9091 if (cu != nullptr)
0907af0c 9092 {
7188ed02 9093 cu->last_used = 0;
0907af0c
DE
9094 return 0;
9095 }
9096
9097 /* Add it to the queue. */
120ce1b5 9098 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9099
9100 return 1;
9101}
9102
10b3939b
DJ
9103/* Process the queue. */
9104
9105static void
976ca316 9106process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9107{
b4f54984 9108 if (dwarf_read_debug)
45cfd468
DE
9109 {
9110 fprintf_unfiltered (gdb_stdlog,
9111 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9112 objfile_name (per_objfile->objfile));
45cfd468
DE
9113 }
9114
03dd20cc
DJ
9115 /* The queue starts out with one item, but following a DIE reference
9116 may load a new CU, adding it to the end of the queue. */
976ca316 9117 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9118 {
976ca316 9119 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9120 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9121
976ca316 9122 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9123 {
976ca316 9124 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9125
7188ed02
SM
9126 /* Skip dummy CUs. */
9127 if (cu != nullptr)
73be47f5 9128 {
7188ed02
SM
9129 unsigned int debug_print_threshold;
9130 char buf[100];
9131
9132 if (per_cu->is_debug_types)
9133 {
9134 struct signatured_type *sig_type =
9135 (struct signatured_type *) per_cu;
9136
9137 sprintf (buf, "TU %s at offset %s",
9138 hex_string (sig_type->signature),
9139 sect_offset_str (per_cu->sect_off));
9140 /* There can be 100s of TUs.
9141 Only print them in verbose mode. */
9142 debug_print_threshold = 2;
9143 }
9144 else
9145 {
9146 sprintf (buf, "CU at offset %s",
9147 sect_offset_str (per_cu->sect_off));
9148 debug_print_threshold = 1;
9149 }
247f5c4f 9150
7188ed02
SM
9151 if (dwarf_read_debug >= debug_print_threshold)
9152 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9153
7188ed02
SM
9154 if (per_cu->is_debug_types)
9155 process_full_type_unit (cu, item.pretend_language);
9156 else
9157 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9158
7188ed02
SM
9159 if (dwarf_read_debug >= debug_print_threshold)
9160 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9161 }
f4dc4d17 9162 }
10b3939b 9163
7188ed02 9164 per_cu->queued = 0;
976ca316 9165 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9166 }
9167
b4f54984 9168 if (dwarf_read_debug)
45cfd468
DE
9169 {
9170 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9171 objfile_name (per_objfile->objfile));
45cfd468 9172 }
10b3939b
DJ
9173}
9174
10b3939b
DJ
9175/* Read in full symbols for PST, and anything it depends on. */
9176
8566b89b
TT
9177void
9178dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9179{
af758d11 9180 gdb_assert (!readin_p (objfile));
95554aad 9181
17ee85fc
TT
9182 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9183 free_cached_comp_units freer (per_objfile);
48993951 9184 expand_dependencies (objfile);
aaa75496 9185
97a1449a 9186 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9187 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9188}
9189
af758d11
SM
9190/* See psympriv.h. */
9191
9192bool
9193dwarf2_psymtab::readin_p (struct objfile *objfile) const
9194{
9195 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9196 return per_objfile->symtab_set_p (per_cu_data);
9197}
9198
9199/* See psympriv.h. */
9200
9201compunit_symtab *
9202dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9203{
9204 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9205 return per_objfile->get_symtab (per_cu_data);
9206}
9207
dee91e82
DE
9208/* Trivial hash function for die_info: the hash value of a DIE
9209 is its offset in .debug_info for this objfile. */
10b3939b 9210
dee91e82
DE
9211static hashval_t
9212die_hash (const void *item)
10b3939b 9213{
9a3c8263 9214 const struct die_info *die = (const struct die_info *) item;
6502dd73 9215
9c541725 9216 return to_underlying (die->sect_off);
dee91e82 9217}
63d06c5c 9218
dee91e82
DE
9219/* Trivial comparison function for die_info structures: two DIEs
9220 are equal if they have the same offset. */
98bfdba5 9221
dee91e82
DE
9222static int
9223die_eq (const void *item_lhs, const void *item_rhs)
9224{
9a3c8263
SM
9225 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9226 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9227
9c541725 9228 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9229}
c906108c 9230
4a636814
SM
9231/* Load the DIEs associated with PER_CU into memory.
9232
9233 In some cases, the caller, while reading partial symbols, will need to load
9234 the full symbols for the CU for some reason. It will already have a
9235 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9236 rather than creating a new one. */
c906108c 9237
dee91e82 9238static void
ab432490
SM
9239load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9240 dwarf2_per_objfile *per_objfile,
4a636814 9241 dwarf2_cu *existing_cu,
c0ab21c2
TT
9242 bool skip_partial,
9243 enum language pretend_language)
dee91e82 9244{
c0ab21c2
TT
9245 gdb_assert (! this_cu->is_debug_types);
9246
7188ed02 9247 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9248 if (reader.dummy_p)
9249 return;
9250
9251 struct dwarf2_cu *cu = reader.cu;
9252 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9253
dee91e82
DE
9254 gdb_assert (cu->die_hash == NULL);
9255 cu->die_hash =
9256 htab_create_alloc_ex (cu->header.length / 12,
9257 die_hash,
9258 die_eq,
9259 NULL,
9260 &cu->comp_unit_obstack,
9261 hashtab_obstack_allocate,
9262 dummy_obstack_deallocate);
e142c38c 9263
3e225074 9264 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9265 reader.comp_unit_die->child
9266 = read_die_and_siblings (&reader, reader.info_ptr,
9267 &info_ptr, reader.comp_unit_die);
9268 cu->dies = reader.comp_unit_die;
dee91e82 9269 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9270
9271 /* We try not to read any attributes in this function, because not
9cdd5dbd 9272 all CUs needed for references have been loaded yet, and symbol
10b3939b 9273 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9274 or we won't be able to build types correctly.
9275 Similarly, if we do not read the producer, we can not apply
9276 producer-specific interpretation. */
c0ab21c2 9277 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9278
9279 reader.keep ();
10b3939b
DJ
9280}
9281
3da10d80
KS
9282/* Add a DIE to the delayed physname list. */
9283
9284static void
9285add_to_method_list (struct type *type, int fnfield_index, int index,
9286 const char *name, struct die_info *die,
9287 struct dwarf2_cu *cu)
9288{
9289 struct delayed_method_info mi;
9290 mi.type = type;
9291 mi.fnfield_index = fnfield_index;
9292 mi.index = index;
9293 mi.name = name;
9294 mi.die = die;
c89b44cd 9295 cu->method_list.push_back (mi);
3da10d80
KS
9296}
9297
3693fdb3
PA
9298/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9299 "const" / "volatile". If so, decrements LEN by the length of the
9300 modifier and return true. Otherwise return false. */
9301
9302template<size_t N>
9303static bool
9304check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9305{
9306 size_t mod_len = sizeof (mod) - 1;
9307 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9308 {
9309 len -= mod_len;
9310 return true;
9311 }
9312 return false;
9313}
9314
3da10d80
KS
9315/* Compute the physnames of any methods on the CU's method list.
9316
9317 The computation of method physnames is delayed in order to avoid the
9318 (bad) condition that one of the method's formal parameters is of an as yet
9319 incomplete type. */
9320
9321static void
9322compute_delayed_physnames (struct dwarf2_cu *cu)
9323{
3693fdb3 9324 /* Only C++ delays computing physnames. */
c89b44cd 9325 if (cu->method_list.empty ())
3693fdb3
PA
9326 return;
9327 gdb_assert (cu->language == language_cplus);
9328
52941706 9329 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9330 {
1d06ead6 9331 const char *physname;
3da10d80 9332 struct fn_fieldlist *fn_flp
c89b44cd
TT
9333 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9334 physname = dwarf2_physname (mi.name, mi.die, cu);
9335 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9336 = physname ? physname : "";
3693fdb3
PA
9337
9338 /* Since there's no tag to indicate whether a method is a
9339 const/volatile overload, extract that information out of the
9340 demangled name. */
9341 if (physname != NULL)
9342 {
9343 size_t len = strlen (physname);
9344
9345 while (1)
9346 {
9347 if (physname[len] == ')') /* shortcut */
9348 break;
9349 else if (check_modifier (physname, len, " const"))
c89b44cd 9350 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9351 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9352 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9353 else
9354 break;
9355 }
9356 }
3da10d80 9357 }
c89b44cd
TT
9358
9359 /* The list is no longer needed. */
9360 cu->method_list.clear ();
3da10d80
KS
9361}
9362
a766d390
DE
9363/* Go objects should be embedded in a DW_TAG_module DIE,
9364 and it's not clear if/how imported objects will appear.
9365 To keep Go support simple until that's worked out,
9366 go back through what we've read and create something usable.
9367 We could do this while processing each DIE, and feels kinda cleaner,
9368 but that way is more invasive.
9369 This is to, for example, allow the user to type "p var" or "b main"
9370 without having to specify the package name, and allow lookups
9371 of module.object to work in contexts that use the expression
9372 parser. */
9373
9374static void
9375fixup_go_packaging (struct dwarf2_cu *cu)
9376{
421d1616 9377 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9378 struct pending *list;
9379 int i;
9380
c24bdb02 9381 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9382 list != NULL;
9383 list = list->next)
a766d390
DE
9384 {
9385 for (i = 0; i < list->nsyms; ++i)
9386 {
9387 struct symbol *sym = list->symbol[i];
9388
c1b5c1eb 9389 if (sym->language () == language_go
a766d390
DE
9390 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9391 {
421d1616
TT
9392 gdb::unique_xmalloc_ptr<char> this_package_name
9393 (go_symbol_package_name (sym));
a766d390
DE
9394
9395 if (this_package_name == NULL)
9396 continue;
9397 if (package_name == NULL)
421d1616 9398 package_name = std::move (this_package_name);
a766d390
DE
9399 else
9400 {
5e22e966 9401 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9402 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9403 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9404 (symbol_symtab (sym) != NULL
9405 ? symtab_to_filename_for_display
9406 (symbol_symtab (sym))
e3b94546 9407 : objfile_name (objfile)),
421d1616 9408 this_package_name.get (), package_name.get ());
a766d390
DE
9409 }
9410 }
9411 }
9412 }
9413
9414 if (package_name != NULL)
9415 {
5e22e966 9416 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9417 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9418 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9419 saved_package_name);
a766d390
DE
9420 struct symbol *sym;
9421
8c14c3a3 9422 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9423 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9424 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9425 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9426 e.g., "main" finds the "main" module and not C's main(). */
9427 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9428 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9429 SYMBOL_TYPE (sym) = type;
9430
c24bdb02 9431 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9432 }
9433}
9434
c9317f21
TT
9435/* Allocate a fully-qualified name consisting of the two parts on the
9436 obstack. */
9437
9438static const char *
9439rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9440{
9441 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9442}
9443
9c6a1327
TT
9444/* A helper that allocates a variant part to attach to a Rust enum
9445 type. OBSTACK is where the results should be allocated. TYPE is
9446 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9447 discriminant. It must be the index of one of the fields of TYPE,
9448 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9449 DEFAULT_INDEX is the index of the default field; or -1 if there is
9450 no default. RANGES is indexed by "effective" field number (the
9451 field index, but omitting the discriminant and default fields) and
9452 must hold the discriminant values used by the variants. Note that
9453 RANGES must have a lifetime at least as long as OBSTACK -- either
9454 already allocated on it, or static. */
c9317f21 9455
9c6a1327
TT
9456static void
9457alloc_rust_variant (struct obstack *obstack, struct type *type,
9458 int discriminant_index, int default_index,
9459 gdb::array_view<discriminant_range> ranges)
9460{
57d02173
TT
9461 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9462 gdb_assert (discriminant_index == -1
9463 || (discriminant_index >= 0
9464 && discriminant_index < type->num_fields ()));
c9317f21 9465 gdb_assert (default_index == -1
1f704f76 9466 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9467
9c6a1327 9468 /* We have one variant for each non-discriminant field. */
57d02173
TT
9469 int n_variants = type->num_fields ();
9470 if (discriminant_index != -1)
9471 --n_variants;
c9317f21 9472
9c6a1327
TT
9473 variant *variants = new (obstack) variant[n_variants];
9474 int var_idx = 0;
9475 int range_idx = 0;
1f704f76 9476 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9477 {
9478 if (i == discriminant_index)
9479 continue;
c9317f21 9480
9c6a1327
TT
9481 variants[var_idx].first_field = i;
9482 variants[var_idx].last_field = i + 1;
9483
9484 /* The default field does not need a range, but other fields do.
9485 We skipped the discriminant above. */
9486 if (i != default_index)
9487 {
9488 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9489 ++range_idx;
9490 }
c9317f21 9491
9c6a1327
TT
9492 ++var_idx;
9493 }
9494
9495 gdb_assert (range_idx == ranges.size ());
9496 gdb_assert (var_idx == n_variants);
9497
9498 variant_part *part = new (obstack) variant_part;
9499 part->discriminant_index = discriminant_index;
57d02173
TT
9500 /* If there is no discriminant, then whether it is signed is of no
9501 consequence. */
9502 part->is_unsigned
9503 = (discriminant_index == -1
9504 ? false
c6d940a9 9505 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9506 part->variants = gdb::array_view<variant> (variants, n_variants);
9507
9508 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9509 gdb::array_view<variant_part> *prop_value
9510 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9511
9c6a1327 9512 struct dynamic_prop prop;
8c2e4e06 9513 prop.set_variant_parts (prop_value);
9c6a1327 9514
5c54719c 9515 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9516}
9517
9518/* Some versions of rustc emitted enums in an unusual way.
9519
9520 Ordinary enums were emitted as unions. The first element of each
9521 structure in the union was named "RUST$ENUM$DISR". This element
9522 held the discriminant.
9523
9524 These versions of Rust also implemented the "non-zero"
9525 optimization. When the enum had two values, and one is empty and
9526 the other holds a pointer that cannot be zero, the pointer is used
9527 as the discriminant, with a zero value meaning the empty variant.
9528 Here, the union's first member is of the form
9529 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9530 where the fieldnos are the indices of the fields that should be
9531 traversed in order to find the field (which may be several fields deep)
9532 and the variantname is the name of the variant of the case when the
9533 field is zero.
9534
9535 This function recognizes whether TYPE is of one of these forms,
9536 and, if so, smashes it to be a variant type. */
9537
9538static void
9539quirk_rust_enum (struct type *type, struct objfile *objfile)
9540{
78134374 9541 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9542
9543 /* We don't need to deal with empty enums. */
1f704f76 9544 if (type->num_fields () == 0)
c9317f21
TT
9545 return;
9546
9547#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9548 if (type->num_fields () == 1
c9317f21
TT
9549 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9550 {
9551 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9552
9553 /* Decode the field name to find the offset of the
9554 discriminant. */
9555 ULONGEST bit_offset = 0;
940da03e 9556 struct type *field_type = type->field (0).type ();
c9317f21
TT
9557 while (name[0] >= '0' && name[0] <= '9')
9558 {
9559 char *tail;
9560 unsigned long index = strtoul (name, &tail, 10);
9561 name = tail;
9562 if (*name != '$'
1f704f76 9563 || index >= field_type->num_fields ()
c9317f21
TT
9564 || (TYPE_FIELD_LOC_KIND (field_type, index)
9565 != FIELD_LOC_KIND_BITPOS))
9566 {
b98664d3 9567 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9568 "[in module %s]"),
9569 TYPE_FIELD_NAME (type, 0),
9570 objfile_name (objfile));
9571 return;
9572 }
9573 ++name;
9574
9575 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9576 field_type = field_type->field (index).type ();
c9317f21
TT
9577 }
9578
9c6a1327
TT
9579 /* Smash this type to be a structure type. We have to do this
9580 because the type has already been recorded. */
67607e24 9581 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9582 type->set_num_fields (3);
9c6a1327 9583 /* Save the field we care about. */
ceacbf6e 9584 struct field saved_field = type->field (0);
3cabb6b0
SM
9585 type->set_fields
9586 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9587
9c6a1327 9588 /* Put the discriminant at index 0. */
5d14b6e5 9589 type->field (0).set_type (field_type);
9c6a1327
TT
9590 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9591 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9592 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9593
9594 /* The order of fields doesn't really matter, so put the real
9595 field at index 1 and the data-less field at index 2. */
ceacbf6e 9596 type->field (1) = saved_field;
9c6a1327 9597 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9598 = rust_last_path_segment (type->field (1).type ()->name ());
9599 type->field (1).type ()->set_name
7d93a1e0 9600 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9601 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9602
9603 const char *dataless_name
7d93a1e0 9604 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9605 name);
9606 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9607 dataless_name);
5d14b6e5 9608 type->field (2).set_type (dataless_type);
c9317f21
TT
9609 /* NAME points into the original discriminant name, which
9610 already has the correct lifetime. */
9c6a1327 9611 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9612 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9613
9c6a1327
TT
9614 /* Indicate that this is a variant type. */
9615 static discriminant_range ranges[1] = { { 0, 0 } };
9616 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9617 }
77c2dba3
TT
9618 /* A union with a single anonymous field is probably an old-style
9619 univariant enum. */
1f704f76 9620 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9621 {
c9317f21
TT
9622 /* Smash this type to be a structure type. We have to do this
9623 because the type has already been recorded. */
67607e24 9624 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9625
940da03e 9626 struct type *field_type = type->field (0).type ();
c9317f21 9627 const char *variant_name
7d93a1e0 9628 = rust_last_path_segment (field_type->name ());
9c6a1327 9629 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9630 field_type->set_name
9631 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9632 type->name (), variant_name));
57d02173
TT
9633
9634 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9635 }
9636 else
9637 {
9638 struct type *disr_type = nullptr;
1f704f76 9639 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9640 {
940da03e 9641 disr_type = type->field (i).type ();
c9317f21 9642
78134374 9643 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9644 {
9645 /* All fields of a true enum will be structs. */
9646 return;
9647 }
1f704f76 9648 else if (disr_type->num_fields () == 0)
c9317f21
TT
9649 {
9650 /* Could be data-less variant, so keep going. */
a037790e 9651 disr_type = nullptr;
c9317f21
TT
9652 }
9653 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9654 "RUST$ENUM$DISR") != 0)
9655 {
9656 /* Not a Rust enum. */
9657 return;
9658 }
9659 else
9660 {
9661 /* Found one. */
9662 break;
9663 }
9664 }
9665
9666 /* If we got here without a discriminant, then it's probably
9667 just a union. */
9668 if (disr_type == nullptr)
9669 return;
9670
9671 /* Smash this type to be a structure type. We have to do this
9672 because the type has already been recorded. */
67607e24 9673 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9674
9c6a1327 9675 /* Make space for the discriminant field. */
ceacbf6e 9676 struct field *disr_field = &disr_type->field (0);
9c6a1327 9677 field *new_fields
1f704f76 9678 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9679 * sizeof (struct field)));
80fc5e77 9680 memcpy (new_fields + 1, type->fields (),
1f704f76 9681 type->num_fields () * sizeof (struct field));
3cabb6b0 9682 type->set_fields (new_fields);
1f704f76 9683 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9684
9685 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9686 type->field (0) = *disr_field;
9c6a1327
TT
9687 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9688 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9689
9690 /* We need a way to find the correct discriminant given a
9691 variant name. For convenience we build a map here. */
b6cdac4b 9692 struct type *enum_type = disr_field->type ();
c9317f21 9693 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9694 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9695 {
9696 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9697 {
9698 const char *name
9699 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9700 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9701 }
9702 }
9703
1f704f76 9704 int n_fields = type->num_fields ();
9c6a1327
TT
9705 /* We don't need a range entry for the discriminant, but we do
9706 need one for every other field, as there is no default
9707 variant. */
9708 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9709 discriminant_range,
9710 n_fields - 1);
c9317f21
TT
9711 /* Skip the discriminant here. */
9712 for (int i = 1; i < n_fields; ++i)
9713 {
9714 /* Find the final word in the name of this variant's type.
9715 That name can be used to look up the correct
9716 discriminant. */
9717 const char *variant_name
940da03e 9718 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9719
9720 auto iter = discriminant_map.find (variant_name);
9721 if (iter != discriminant_map.end ())
9c6a1327 9722 {
57d02173
TT
9723 ranges[i - 1].low = iter->second;
9724 ranges[i - 1].high = iter->second;
9c6a1327 9725 }
c9317f21 9726
57d02173
TT
9727 /* In Rust, each element should have the size of the
9728 enclosing enum. */
9729 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9730
bedda9ac 9731 /* Remove the discriminant field, if it exists. */
940da03e 9732 struct type *sub_type = type->field (i).type ();
1f704f76 9733 if (sub_type->num_fields () > 0)
bedda9ac 9734 {
5e33d5f4 9735 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9736 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9737 }
9c6a1327 9738 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9739 sub_type->set_name
9740 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9741 type->name (), variant_name));
c9317f21 9742 }
9c6a1327
TT
9743
9744 /* Indicate that this is a variant type. */
a1520ad8 9745 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9746 gdb::array_view<discriminant_range> (ranges,
9747 n_fields - 1));
c9317f21
TT
9748 }
9749}
9750
9751/* Rewrite some Rust unions to be structures with variants parts. */
9752
9753static void
9754rust_union_quirks (struct dwarf2_cu *cu)
9755{
9756 gdb_assert (cu->language == language_rust);
52941706 9757 for (type *type_ : cu->rust_unions)
5e22e966 9758 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9759 /* We don't need this any more. */
9760 cu->rust_unions.clear ();
c9317f21
TT
9761}
9762
8adb8487
TT
9763/* See read.h. */
9764
9765type_unit_group_unshareable *
9766dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9767{
9768 auto iter = this->m_type_units.find (tu_group);
9769 if (iter != this->m_type_units.end ())
9770 return iter->second.get ();
9771
9772 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9773 type_unit_group_unshareable *result = uniq.get ();
9774 this->m_type_units[tu_group] = std::move (uniq);
9775 return result;
9776}
9777
e286671b
TT
9778struct type *
9779dwarf2_per_objfile::get_type_for_signatured_type
9780 (signatured_type *sig_type) const
9781{
9782 auto iter = this->m_type_map.find (sig_type);
9783 if (iter == this->m_type_map.end ())
9784 return nullptr;
9785
9786 return iter->second;
9787}
9788
9789void dwarf2_per_objfile::set_type_for_signatured_type
9790 (signatured_type *sig_type, struct type *type)
9791{
9792 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9793
9794 this->m_type_map[sig_type] = type;
9795}
9796
95554aad
TT
9797/* A helper function for computing the list of all symbol tables
9798 included by PER_CU. */
9799
9800static void
4c39bc03 9801recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9802 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9803 dwarf2_per_cu_data *per_cu,
9804 dwarf2_per_objfile *per_objfile,
43f3e411 9805 struct compunit_symtab *immediate_parent)
95554aad 9806{
af758d11 9807 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9808 if (*slot != NULL)
9809 {
9810 /* This inclusion and its children have been processed. */
9811 return;
9812 }
9813
9814 *slot = per_cu;
af758d11 9815
95554aad 9816 /* Only add a CU if it has a symbol table. */
43182c09 9817 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9818 if (cust != NULL)
ec94af83
DE
9819 {
9820 /* If this is a type unit only add its symbol table if we haven't
9821 seen it yet (type unit per_cu's can share symtabs). */
9822 if (per_cu->is_debug_types)
9823 {
43f3e411 9824 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9825 if (*slot == NULL)
9826 {
43f3e411 9827 *slot = cust;
4c39bc03 9828 result->push_back (cust);
43f3e411
DE
9829 if (cust->user == NULL)
9830 cust->user = immediate_parent;
ec94af83
DE
9831 }
9832 }
9833 else
f9125b6c 9834 {
4c39bc03 9835 result->push_back (cust);
43f3e411
DE
9836 if (cust->user == NULL)
9837 cust->user = immediate_parent;
f9125b6c 9838 }
ec94af83 9839 }
95554aad 9840
ae640021
AB
9841 if (!per_cu->imported_symtabs_empty ())
9842 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9843 {
9844 recursively_compute_inclusions (result, all_children,
43182c09
SM
9845 all_type_symtabs, ptr, per_objfile,
9846 cust);
ae640021 9847 }
95554aad
TT
9848}
9849
43f3e411 9850/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9851 PER_CU. */
9852
9853static void
43182c09
SM
9854compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9855 dwarf2_per_objfile *per_objfile)
95554aad 9856{
f4dc4d17
DE
9857 gdb_assert (! per_cu->is_debug_types);
9858
ae640021 9859 if (!per_cu->imported_symtabs_empty ())
95554aad 9860 {
ae640021 9861 int len;
4c39bc03 9862 std::vector<compunit_symtab *> result_symtabs;
43182c09 9863 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9864
9865 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9866 if (cust == NULL)
95554aad
TT
9867 return;
9868
280a9412
TT
9869 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9870 htab_eq_pointer,
9871 NULL, xcalloc, xfree));
9872 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9873 htab_eq_pointer,
9874 NULL, xcalloc, xfree));
95554aad 9875
ae640021 9876 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9877 {
280a9412
TT
9878 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9879 all_type_symtabs.get (), ptr,
9880 per_objfile, cust);
ec94af83 9881 }
95554aad 9882
ec94af83 9883 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9884 len = result_symtabs.size ();
43f3e411 9885 cust->includes
f6e649dd 9886 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9887 struct compunit_symtab *, len + 1);
4c39bc03
TT
9888 memcpy (cust->includes, result_symtabs.data (),
9889 len * sizeof (compunit_symtab *));
43f3e411 9890 cust->includes[len] = NULL;
95554aad
TT
9891 }
9892}
9893
9894/* Compute the 'includes' field for the symtabs of all the CUs we just
9895 read. */
9896
9897static void
976ca316 9898process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9899{
976ca316 9900 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9901 {
9902 if (! iter->is_debug_types)
976ca316 9903 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9904 }
95554aad 9905
976ca316 9906 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9907}
9908
8fc0b21d 9909/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9910 already been loaded into memory. */
9911
9912static void
8fc0b21d 9913process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9914{
976ca316
SM
9915 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9916 struct objfile *objfile = per_objfile->objfile;
08feed99 9917 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9918 CORE_ADDR lowpc, highpc;
43f3e411 9919 struct compunit_symtab *cust;
10b3939b 9920 CORE_ADDR baseaddr;
4359dff1 9921 struct block *static_block;
3e29f34a 9922 CORE_ADDR addr;
10b3939b 9923
b3b3bada 9924 baseaddr = objfile->text_section_offset ();
10b3939b 9925
c89b44cd
TT
9926 /* Clear the list here in case something was left over. */
9927 cu->method_list.clear ();
10b3939b 9928
95554aad
TT
9929 cu->language = pretend_language;
9930 cu->language_defn = language_def (cu->language);
9931
1c47ec3e
TV
9932 dwarf2_find_base_address (cu->dies, cu);
9933
c906108c 9934 /* Do line number decoding in read_file_scope () */
10b3939b 9935 process_die (cu->dies, cu);
c906108c 9936
a766d390
DE
9937 /* For now fudge the Go package. */
9938 if (cu->language == language_go)
9939 fixup_go_packaging (cu);
9940
5f48f8f3 9941 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9942 should be complete, and it should now be safe to compute all of the
9943 physnames. */
9944 compute_delayed_physnames (cu);
3da10d80 9945
c9317f21
TT
9946 if (cu->language == language_rust)
9947 rust_union_quirks (cu);
9948
fae299cd
DC
9949 /* Some compilers don't define a DW_AT_high_pc attribute for the
9950 compilation unit. If the DW_AT_high_pc is missing, synthesize
9951 it, by scanning the DIE's below the compilation unit. */
10b3939b 9952 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9953
3e29f34a 9954 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9955 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9956
9957 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9958 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9959 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9960 addrmap to help ensure it has an accurate map of pc values belonging to
9961 this comp unit. */
9962 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9963
c24bdb02 9964 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9965 SECT_OFF_TEXT (objfile),
9966 0);
c906108c 9967
43f3e411 9968 if (cust != NULL)
c906108c 9969 {
df15bd07 9970 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9971
8be455d7
JK
9972 /* Set symtab language to language from DW_AT_language. If the
9973 compilation is from a C file generated by language preprocessors, do
9974 not set the language if it was already deduced by start_subfile. */
43f3e411 9975 if (!(cu->language == language_c
40e3ad0e 9976 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9977 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9978
9979 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9980 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9981 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9982 there were bugs in prologue debug info, fixed later in GCC-4.5
9983 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9984
9985 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9986 needed, it would be wrong due to missing DW_AT_producer there.
9987
9988 Still one can confuse GDB by using non-standard GCC compilation
9989 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9990 */
ab260dad 9991 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9992 cust->locations_valid = 1;
e0d00bc7
JK
9993
9994 if (gcc_4_minor >= 5)
43f3e411 9995 cust->epilogue_unwind_valid = 1;
96408a79 9996
43f3e411 9997 cust->call_site_htab = cu->call_site_htab;
c906108c 9998 }
9291a0cd 9999
976ca316 10000 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10001
95554aad 10002 /* Push it for inclusion processing later. */
976ca316 10003 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10004
10005 /* Not needed any more. */
c24bdb02 10006 cu->reset_builder ();
f4dc4d17 10007}
45cfd468 10008
8fc0b21d 10009/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10010 already been loaded into memory. */
10011
10012static void
8fc0b21d 10013process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10014 enum language pretend_language)
10015{
976ca316
SM
10016 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10017 struct objfile *objfile = per_objfile->objfile;
43f3e411 10018 struct compunit_symtab *cust;
0186c6a7
DE
10019 struct signatured_type *sig_type;
10020
8fc0b21d
SM
10021 gdb_assert (cu->per_cu->is_debug_types);
10022 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10023
c89b44cd
TT
10024 /* Clear the list here in case something was left over. */
10025 cu->method_list.clear ();
f4dc4d17 10026
f4dc4d17
DE
10027 cu->language = pretend_language;
10028 cu->language_defn = language_def (cu->language);
10029
10030 /* The symbol tables are set up in read_type_unit_scope. */
10031 process_die (cu->dies, cu);
10032
10033 /* For now fudge the Go package. */
10034 if (cu->language == language_go)
10035 fixup_go_packaging (cu);
10036
5f48f8f3 10037 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10038 should be complete, and it should now be safe to compute all of the
10039 physnames. */
10040 compute_delayed_physnames (cu);
f4dc4d17 10041
c9317f21
TT
10042 if (cu->language == language_rust)
10043 rust_union_quirks (cu);
10044
f4dc4d17
DE
10045 /* TUs share symbol tables.
10046 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10047 of it with end_expandable_symtab. Otherwise, complete the addition of
10048 this TU's symbols to the existing symtab. */
8adb8487 10049 type_unit_group_unshareable *tug_unshare =
976ca316 10050 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10051 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10052 {
c24bdb02
KS
10053 buildsym_compunit *builder = cu->get_builder ();
10054 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10055 tug_unshare->compunit_symtab = cust;
f4dc4d17 10056
43f3e411 10057 if (cust != NULL)
f4dc4d17
DE
10058 {
10059 /* Set symtab language to language from DW_AT_language. If the
10060 compilation is from a C file generated by language preprocessors,
10061 do not set the language if it was already deduced by
10062 start_subfile. */
43f3e411
DE
10063 if (!(cu->language == language_c
10064 && COMPUNIT_FILETABS (cust)->language != language_c))
10065 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10066 }
10067 }
10068 else
10069 {
c24bdb02 10070 cu->get_builder ()->augment_type_symtab ();
8adb8487 10071 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10072 }
10073
976ca316 10074 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10075
10076 /* Not needed any more. */
c24bdb02 10077 cu->reset_builder ();
c906108c
SS
10078}
10079
95554aad
TT
10080/* Process an imported unit DIE. */
10081
10082static void
10083process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10084{
10085 struct attribute *attr;
10086
f4dc4d17
DE
10087 /* For now we don't handle imported units in type units. */
10088 if (cu->per_cu->is_debug_types)
10089 {
10090 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10091 " supported in type units [in module %s]"),
5e22e966 10092 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10093 }
10094
95554aad
TT
10095 attr = dwarf2_attr (die, DW_AT_import, cu);
10096 if (attr != NULL)
10097 {
0826b30a 10098 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10099 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10100 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10101 dwarf2_per_cu_data *per_cu
ab432490 10102 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10103
58990295
TV
10104 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10105 into another compilation unit, at root level. Regard this as a hint,
10106 and ignore it. */
10107 if (die->parent && die->parent->parent == NULL
10108 && per_cu->unit_type == DW_UT_compile
10109 && per_cu->lang == language_cplus)
10110 return;
10111
69d751e3 10112 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10113 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10114 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10115 false, cu->language);
95554aad 10116
ae640021 10117 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10118 }
10119}
10120
4c8aa72d
PA
10121/* RAII object that represents a process_die scope: i.e.,
10122 starts/finishes processing a DIE. */
10123class process_die_scope
adde2bff 10124{
4c8aa72d
PA
10125public:
10126 process_die_scope (die_info *die, dwarf2_cu *cu)
10127 : m_die (die), m_cu (cu)
10128 {
10129 /* We should only be processing DIEs not already in process. */
10130 gdb_assert (!m_die->in_process);
10131 m_die->in_process = true;
10132 }
8c3cb9fa 10133
4c8aa72d
PA
10134 ~process_die_scope ()
10135 {
10136 m_die->in_process = false;
10137
10138 /* If we're done processing the DIE for the CU that owns the line
10139 header, we don't need the line header anymore. */
10140 if (m_cu->line_header_die_owner == m_die)
10141 {
10142 delete m_cu->line_header;
10143 m_cu->line_header = NULL;
10144 m_cu->line_header_die_owner = NULL;
10145 }
10146 }
10147
10148private:
10149 die_info *m_die;
10150 dwarf2_cu *m_cu;
10151};
adde2bff 10152
c906108c
SS
10153/* Process a die and its children. */
10154
10155static void
e7c27a73 10156process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10157{
4c8aa72d 10158 process_die_scope scope (die, cu);
adde2bff 10159
c906108c
SS
10160 switch (die->tag)
10161 {
10162 case DW_TAG_padding:
10163 break;
10164 case DW_TAG_compile_unit:
95554aad 10165 case DW_TAG_partial_unit:
e7c27a73 10166 read_file_scope (die, cu);
c906108c 10167 break;
348e048f
DE
10168 case DW_TAG_type_unit:
10169 read_type_unit_scope (die, cu);
10170 break;
c906108c 10171 case DW_TAG_subprogram:
0a4b0913
AB
10172 /* Nested subprograms in Fortran get a prefix. */
10173 if (cu->language == language_fortran
10174 && die->parent != NULL
10175 && die->parent->tag == DW_TAG_subprogram)
10176 cu->processing_has_namespace_info = true;
10177 /* Fall through. */
c906108c 10178 case DW_TAG_inlined_subroutine:
edb3359d 10179 read_func_scope (die, cu);
c906108c
SS
10180 break;
10181 case DW_TAG_lexical_block:
14898363
L
10182 case DW_TAG_try_block:
10183 case DW_TAG_catch_block:
e7c27a73 10184 read_lexical_block_scope (die, cu);
c906108c 10185 break;
216f72a1 10186 case DW_TAG_call_site:
96408a79
SA
10187 case DW_TAG_GNU_call_site:
10188 read_call_site_scope (die, cu);
10189 break;
c906108c 10190 case DW_TAG_class_type:
680b30c7 10191 case DW_TAG_interface_type:
c906108c
SS
10192 case DW_TAG_structure_type:
10193 case DW_TAG_union_type:
134d01f1 10194 process_structure_scope (die, cu);
c906108c
SS
10195 break;
10196 case DW_TAG_enumeration_type:
134d01f1 10197 process_enumeration_scope (die, cu);
c906108c 10198 break;
134d01f1 10199
f792889a
DJ
10200 /* These dies have a type, but processing them does not create
10201 a symbol or recurse to process the children. Therefore we can
10202 read them on-demand through read_type_die. */
c906108c 10203 case DW_TAG_subroutine_type:
72019c9c 10204 case DW_TAG_set_type:
c906108c 10205 case DW_TAG_array_type:
c906108c 10206 case DW_TAG_pointer_type:
c906108c 10207 case DW_TAG_ptr_to_member_type:
c906108c 10208 case DW_TAG_reference_type:
4297a3f0 10209 case DW_TAG_rvalue_reference_type:
c906108c 10210 case DW_TAG_string_type:
c906108c 10211 break;
134d01f1 10212
c906108c 10213 case DW_TAG_base_type:
a02abb62 10214 case DW_TAG_subrange_type:
cb249c71 10215 case DW_TAG_typedef:
134d01f1
DJ
10216 /* Add a typedef symbol for the type definition, if it has a
10217 DW_AT_name. */
f792889a 10218 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10219 break;
c906108c 10220 case DW_TAG_common_block:
e7c27a73 10221 read_common_block (die, cu);
c906108c
SS
10222 break;
10223 case DW_TAG_common_inclusion:
10224 break;
d9fa45fe 10225 case DW_TAG_namespace:
9068261f 10226 cu->processing_has_namespace_info = true;
e7c27a73 10227 read_namespace (die, cu);
d9fa45fe 10228 break;
5d7cb8df 10229 case DW_TAG_module:
9068261f 10230 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10231 read_module (die, cu);
10232 break;
d9fa45fe 10233 case DW_TAG_imported_declaration:
9068261f 10234 cu->processing_has_namespace_info = true;
74921315
KS
10235 if (read_namespace_alias (die, cu))
10236 break;
86a73007
TT
10237 /* The declaration is not a global namespace alias. */
10238 /* Fall through. */
d9fa45fe 10239 case DW_TAG_imported_module:
9068261f 10240 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10241 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10242 || cu->language != language_fortran))
b98664d3 10243 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10244 dwarf_tag_name (die->tag));
10245 read_import_statement (die, cu);
d9fa45fe 10246 break;
95554aad
TT
10247
10248 case DW_TAG_imported_unit:
10249 process_imported_unit_die (die, cu);
10250 break;
10251
71a3c369
TT
10252 case DW_TAG_variable:
10253 read_variable (die, cu);
10254 break;
10255
c906108c 10256 default:
e7c27a73 10257 new_symbol (die, NULL, cu);
c906108c
SS
10258 break;
10259 }
10260}
ca69b9e6
DE
10261\f
10262/* DWARF name computation. */
c906108c 10263
94af9270
KS
10264/* A helper function for dwarf2_compute_name which determines whether DIE
10265 needs to have the name of the scope prepended to the name listed in the
10266 die. */
10267
10268static int
10269die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10270{
1c809c68
TT
10271 struct attribute *attr;
10272
94af9270
KS
10273 switch (die->tag)
10274 {
10275 case DW_TAG_namespace:
10276 case DW_TAG_typedef:
10277 case DW_TAG_class_type:
10278 case DW_TAG_interface_type:
10279 case DW_TAG_structure_type:
10280 case DW_TAG_union_type:
10281 case DW_TAG_enumeration_type:
10282 case DW_TAG_enumerator:
10283 case DW_TAG_subprogram:
08a76f8a 10284 case DW_TAG_inlined_subroutine:
94af9270 10285 case DW_TAG_member:
74921315 10286 case DW_TAG_imported_declaration:
94af9270
KS
10287 return 1;
10288
10289 case DW_TAG_variable:
c2b0a229 10290 case DW_TAG_constant:
94af9270
KS
10291 /* We only need to prefix "globally" visible variables. These include
10292 any variable marked with DW_AT_external or any variable that
10293 lives in a namespace. [Variables in anonymous namespaces
10294 require prefixing, but they are not DW_AT_external.] */
10295
10296 if (dwarf2_attr (die, DW_AT_specification, cu))
10297 {
10298 struct dwarf2_cu *spec_cu = cu;
9a619af0 10299
94af9270
KS
10300 return die_needs_namespace (die_specification (die, &spec_cu),
10301 spec_cu);
10302 }
10303
1c809c68 10304 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10305 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10306 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10307 return 0;
10308 /* A variable in a lexical block of some kind does not need a
10309 namespace, even though in C++ such variables may be external
10310 and have a mangled name. */
10311 if (die->parent->tag == DW_TAG_lexical_block
10312 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10313 || die->parent->tag == DW_TAG_catch_block
10314 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10315 return 0;
10316 return 1;
94af9270
KS
10317
10318 default:
10319 return 0;
10320 }
10321}
10322
73b9be8b
KS
10323/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10324 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10325 defined for the given DIE. */
10326
10327static struct attribute *
10328dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10329{
10330 struct attribute *attr;
10331
10332 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10333 if (attr == NULL)
10334 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10335
10336 return attr;
10337}
10338
10339/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10340 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10341 defined for the given DIE. */
10342
10343static const char *
10344dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10345{
10346 const char *linkage_name;
10347
10348 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10349 if (linkage_name == NULL)
10350 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10351
787de330
TT
10352 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10353 See https://github.com/rust-lang/rust/issues/32925. */
10354 if (cu->language == language_rust && linkage_name != NULL
10355 && strchr (linkage_name, '{') != NULL)
10356 linkage_name = NULL;
10357
73b9be8b
KS
10358 return linkage_name;
10359}
10360
94af9270 10361/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10362 compute the physname for the object, which include a method's:
9c37b5ae 10363 - formal parameters (C++),
a766d390 10364 - receiver type (Go),
a766d390
DE
10365
10366 The term "physname" is a bit confusing.
10367 For C++, for example, it is the demangled name.
10368 For Go, for example, it's the mangled name.
94af9270 10369
af6b7be1
JB
10370 For Ada, return the DIE's linkage name rather than the fully qualified
10371 name. PHYSNAME is ignored..
10372
5989a64e 10373 The result is allocated on the objfile->per_bfd's obstack and
45940949 10374 canonicalized. */
94af9270
KS
10375
10376static const char *
15d034d0
TT
10377dwarf2_compute_name (const char *name,
10378 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10379 int physname)
10380{
5e22e966 10381 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10382
94af9270
KS
10383 if (name == NULL)
10384 name = dwarf2_name (die, cu);
10385
2ee7123e
DE
10386 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10387 but otherwise compute it by typename_concat inside GDB.
10388 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10389 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10390 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10391 will set the demangled name to the result of dwarf2_full_name, and it is
10392 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10393 if (cu->language == language_ada
10394 || (cu->language == language_fortran && physname))
10395 {
10396 /* For Ada unit, we prefer the linkage name over the name, as
10397 the former contains the exported name, which the user expects
10398 to be able to reference. Ideally, we want the user to be able
10399 to reference this entity using either natural or linkage name,
10400 but we haven't started looking at this enhancement yet. */
73b9be8b 10401 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10402
2ee7123e
DE
10403 if (linkage_name != NULL)
10404 return linkage_name;
f55ee35c
JK
10405 }
10406
94af9270
KS
10407 /* These are the only languages we know how to qualify names in. */
10408 if (name != NULL
9c37b5ae 10409 && (cu->language == language_cplus
c44af4eb
TT
10410 || cu->language == language_fortran || cu->language == language_d
10411 || cu->language == language_rust))
94af9270
KS
10412 {
10413 if (die_needs_namespace (die, cu))
10414 {
0d5cff50 10415 const char *prefix;
34a68019 10416 const char *canonical_name = NULL;
94af9270 10417
d7e74731
PA
10418 string_file buf;
10419
94af9270 10420 prefix = determine_prefix (die, cu);
94af9270
KS
10421 if (*prefix != '\0')
10422 {
43816ebc
TT
10423 gdb::unique_xmalloc_ptr<char> prefixed_name
10424 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10425
43816ebc 10426 buf.puts (prefixed_name.get ());
94af9270
KS
10427 }
10428 else
d7e74731 10429 buf.puts (name);
94af9270 10430
98bfdba5
PA
10431 /* Template parameters may be specified in the DIE's DW_AT_name, or
10432 as children with DW_TAG_template_type_param or
10433 DW_TAG_value_type_param. If the latter, add them to the name
10434 here. If the name already has template parameters, then
10435 skip this step; some versions of GCC emit both, and
10436 it is more efficient to use the pre-computed name.
10437
10438 Something to keep in mind about this process: it is very
10439 unlikely, or in some cases downright impossible, to produce
10440 something that will match the mangled name of a function.
10441 If the definition of the function has the same debug info,
10442 we should be able to match up with it anyway. But fallbacks
10443 using the minimal symbol, for instance to find a method
10444 implemented in a stripped copy of libstdc++, will not work.
10445 If we do not have debug info for the definition, we will have to
10446 match them up some other way.
10447
10448 When we do name matching there is a related problem with function
10449 templates; two instantiated function templates are allowed to
10450 differ only by their return types, which we do not add here. */
10451
10452 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10453 {
10454 struct attribute *attr;
10455 struct die_info *child;
10456 int first = 1;
10457
10458 die->building_fullname = 1;
10459
10460 for (child = die->child; child != NULL; child = child->sibling)
10461 {
10462 struct type *type;
12df843f 10463 LONGEST value;
d521ce57 10464 const gdb_byte *bytes;
98bfdba5
PA
10465 struct dwarf2_locexpr_baton *baton;
10466 struct value *v;
10467
10468 if (child->tag != DW_TAG_template_type_param
10469 && child->tag != DW_TAG_template_value_param)
10470 continue;
10471
10472 if (first)
10473 {
d7e74731 10474 buf.puts ("<");
98bfdba5
PA
10475 first = 0;
10476 }
10477 else
d7e74731 10478 buf.puts (", ");
98bfdba5
PA
10479
10480 attr = dwarf2_attr (child, DW_AT_type, cu);
10481 if (attr == NULL)
10482 {
b98664d3 10483 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10484 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10485 continue;
10486 }
10487 type = die_type (child, cu);
10488
10489 if (child->tag == DW_TAG_template_type_param)
10490 {
c1ec8cea
TT
10491 c_print_type (type, "", &buf, -1, 0, cu->language,
10492 &type_print_raw_options);
98bfdba5
PA
10493 continue;
10494 }
10495
10496 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10497 if (attr == NULL)
10498 {
b98664d3 10499 complaint (_("template parameter missing "
3e43a32a 10500 "DW_AT_const_value"));
d7e74731 10501 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10502 continue;
10503 }
10504
10505 dwarf2_const_value_attr (attr, type, name,
10506 &cu->comp_unit_obstack, cu,
10507 &value, &bytes, &baton);
10508
20ce4123 10509 if (type->has_no_signedness ())
98bfdba5
PA
10510 /* GDB prints characters as NUMBER 'CHAR'. If that's
10511 changed, this can use value_print instead. */
d7e74731 10512 c_printchar (value, type, &buf);
98bfdba5
PA
10513 else
10514 {
10515 struct value_print_options opts;
10516
10517 if (baton != NULL)
10518 v = dwarf2_evaluate_loc_desc (type, NULL,
10519 baton->data,
10520 baton->size,
9f47c707
SM
10521 baton->per_cu,
10522 baton->per_objfile);
98bfdba5
PA
10523 else if (bytes != NULL)
10524 {
10525 v = allocate_value (type);
10526 memcpy (value_contents_writeable (v), bytes,
10527 TYPE_LENGTH (type));
10528 }
10529 else
10530 v = value_from_longest (type, value);
10531
3e43a32a
MS
10532 /* Specify decimal so that we do not depend on
10533 the radix. */
98bfdba5
PA
10534 get_formatted_print_options (&opts, 'd');
10535 opts.raw = 1;
d7e74731 10536 value_print (v, &buf, &opts);
98bfdba5 10537 release_value (v);
98bfdba5
PA
10538 }
10539 }
10540
10541 die->building_fullname = 0;
10542
10543 if (!first)
10544 {
10545 /* Close the argument list, with a space if necessary
10546 (nested templates). */
d7e74731
PA
10547 if (!buf.empty () && buf.string ().back () == '>')
10548 buf.puts (" >");
98bfdba5 10549 else
d7e74731 10550 buf.puts (">");
98bfdba5
PA
10551 }
10552 }
10553
9c37b5ae 10554 /* For C++ methods, append formal parameter type
94af9270 10555 information, if PHYSNAME. */
6e70227d 10556
94af9270 10557 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10558 && cu->language == language_cplus)
94af9270
KS
10559 {
10560 struct type *type = read_type_die (die, cu);
10561
d7e74731 10562 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10563 &type_print_raw_options);
94af9270 10564
9c37b5ae 10565 if (cu->language == language_cplus)
94af9270 10566 {
60430eff
DJ
10567 /* Assume that an artificial first parameter is
10568 "this", but do not crash if it is not. RealView
10569 marks unnamed (and thus unused) parameters as
10570 artificial; there is no way to differentiate
10571 the two cases. */
1f704f76 10572 if (type->num_fields () > 0
94af9270 10573 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10574 && type->field (0).type ()->code () == TYPE_CODE_PTR
10575 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10576 buf.puts (" const");
94af9270
KS
10577 }
10578 }
10579
d7e74731 10580 const std::string &intermediate_name = buf.string ();
94af9270
KS
10581
10582 if (cu->language == language_cplus)
34a68019 10583 canonical_name
322a8516 10584 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10585 objfile);
34a68019
TT
10586
10587 /* If we only computed INTERMEDIATE_NAME, or if
10588 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10589 intern it. */
322a8516 10590 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10591 name = objfile->intern (intermediate_name);
34a68019
TT
10592 else
10593 name = canonical_name;
94af9270
KS
10594 }
10595 }
10596
10597 return name;
10598}
10599
0114d602
DJ
10600/* Return the fully qualified name of DIE, based on its DW_AT_name.
10601 If scope qualifiers are appropriate they will be added. The result
34a68019 10602 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10603 not have a name. NAME may either be from a previous call to
10604 dwarf2_name or NULL.
10605
9c37b5ae 10606 The output string will be canonicalized (if C++). */
0114d602
DJ
10607
10608static const char *
15d034d0 10609dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10610{
94af9270
KS
10611 return dwarf2_compute_name (name, die, cu, 0);
10612}
0114d602 10613
94af9270
KS
10614/* Construct a physname for the given DIE in CU. NAME may either be
10615 from a previous call to dwarf2_name or NULL. The result will be
10616 allocated on the objfile_objstack or NULL if the DIE does not have a
10617 name.
0114d602 10618
9c37b5ae 10619 The output string will be canonicalized (if C++). */
0114d602 10620
94af9270 10621static const char *
15d034d0 10622dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10623{
5e22e966 10624 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10625 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10626 int need_copy = 1;
10627
10628 /* In this case dwarf2_compute_name is just a shortcut not building anything
10629 on its own. */
10630 if (!die_needs_namespace (die, cu))
10631 return dwarf2_compute_name (name, die, cu, 1);
10632
906bb4c5
TT
10633 if (cu->language != language_rust)
10634 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10635
10636 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10637 has computed. */
791afaa2 10638 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10639 if (mangled != NULL)
900e11f9 10640 {
900e11f9 10641
d3355e4d 10642 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10643 {
10644 /* Do nothing (do not demangle the symbol name). */
10645 }
a766d390
DE
10646 else
10647 {
0eb876f5
JB
10648 /* Use DMGL_RET_DROP for C++ template functions to suppress
10649 their return type. It is easier for GDB users to search
10650 for such functions as `name(params)' than `long name(params)'.
10651 In such case the minimal symbol names do not match the full
10652 symbol names but for template functions there is never a need
10653 to look up their definition from their declaration so
10654 the only disadvantage remains the minimal symbol variant
10655 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10656 demangled.reset (gdb_demangle (mangled,
10657 (DMGL_PARAMS | DMGL_ANSI
10658 | DMGL_RET_DROP)));
a766d390 10659 }
900e11f9 10660 if (demangled)
791afaa2 10661 canon = demangled.get ();
900e11f9
JK
10662 else
10663 {
10664 canon = mangled;
10665 need_copy = 0;
10666 }
10667 }
10668
10669 if (canon == NULL || check_physname)
10670 {
10671 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10672
10673 if (canon != NULL && strcmp (physname, canon) != 0)
10674 {
10675 /* It may not mean a bug in GDB. The compiler could also
10676 compute DW_AT_linkage_name incorrectly. But in such case
10677 GDB would need to be bug-to-bug compatible. */
10678
b98664d3 10679 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10680 "(from linkage <%s>) - DIE at %s [in module %s]"),
10681 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10682 objfile_name (objfile));
900e11f9
JK
10683
10684 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10685 is available here - over computed PHYSNAME. It is safer
10686 against both buggy GDB and buggy compilers. */
10687
10688 retval = canon;
10689 }
10690 else
10691 {
10692 retval = physname;
10693 need_copy = 0;
10694 }
10695 }
10696 else
10697 retval = canon;
10698
10699 if (need_copy)
be1e3d3e 10700 retval = objfile->intern (retval);
900e11f9 10701
900e11f9 10702 return retval;
0114d602
DJ
10703}
10704
74921315
KS
10705/* Inspect DIE in CU for a namespace alias. If one exists, record
10706 a new symbol for it.
10707
10708 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10709
10710static int
10711read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10712{
10713 struct attribute *attr;
10714
10715 /* If the die does not have a name, this is not a namespace
10716 alias. */
10717 attr = dwarf2_attr (die, DW_AT_name, cu);
10718 if (attr != NULL)
10719 {
10720 int num;
10721 struct die_info *d = die;
10722 struct dwarf2_cu *imported_cu = cu;
10723
10724 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10725 keep inspecting DIEs until we hit the underlying import. */
10726#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10727 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10728 {
10729 attr = dwarf2_attr (d, DW_AT_import, cu);
10730 if (attr == NULL)
10731 break;
10732
10733 d = follow_die_ref (d, attr, &imported_cu);
10734 if (d->tag != DW_TAG_imported_declaration)
10735 break;
10736 }
10737
10738 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10739 {
b98664d3 10740 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10741 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10742 return 0;
10743 }
10744
10745 if (attr != NULL)
10746 {
10747 struct type *type;
0826b30a 10748 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10749
aa66c379 10750 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10751 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10752 {
10753 /* This declaration is a global namespace alias. Add
10754 a symbol for it whose type is the aliased namespace. */
10755 new_symbol (die, type, cu);
10756 return 1;
10757 }
10758 }
10759 }
10760
10761 return 0;
10762}
10763
22cee43f 10764/* Return the using directives repository (global or local?) to use in the
804d2729 10765 current context for CU.
22cee43f
PMR
10766
10767 For Ada, imported declarations can materialize renamings, which *may* be
10768 global. However it is impossible (for now?) in DWARF to distinguish
10769 "external" imported declarations and "static" ones. As all imported
10770 declarations seem to be static in all other languages, make them all CU-wide
10771 global only in Ada. */
10772
10773static struct using_direct **
804d2729 10774using_directives (struct dwarf2_cu *cu)
22cee43f 10775{
c24bdb02
KS
10776 if (cu->language == language_ada
10777 && cu->get_builder ()->outermost_context_p ())
10778 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10779 else
c24bdb02 10780 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10781}
10782
27aa8d6a
SW
10783/* Read the import statement specified by the given die and record it. */
10784
10785static void
10786read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10787{
5e22e966 10788 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10789 struct attribute *import_attr;
32019081 10790 struct die_info *imported_die, *child_die;
de4affc9 10791 struct dwarf2_cu *imported_cu;
27aa8d6a 10792 const char *imported_name;
794684b6 10793 const char *imported_name_prefix;
13387711
SW
10794 const char *canonical_name;
10795 const char *import_alias;
10796 const char *imported_declaration = NULL;
794684b6 10797 const char *import_prefix;
eb1e02fd 10798 std::vector<const char *> excludes;
13387711 10799
27aa8d6a
SW
10800 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10801 if (import_attr == NULL)
10802 {
b98664d3 10803 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10804 dwarf_tag_name (die->tag));
10805 return;
10806 }
10807
de4affc9
CC
10808 imported_cu = cu;
10809 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10810 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10811 if (imported_name == NULL)
10812 {
10813 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10814
10815 The import in the following code:
10816 namespace A
10817 {
10818 typedef int B;
10819 }
10820
10821 int main ()
10822 {
10823 using A::B;
10824 B b;
10825 return b;
10826 }
10827
10828 ...
10829 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10830 <52> DW_AT_decl_file : 1
10831 <53> DW_AT_decl_line : 6
10832 <54> DW_AT_import : <0x75>
10833 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10834 <59> DW_AT_name : B
10835 <5b> DW_AT_decl_file : 1
10836 <5c> DW_AT_decl_line : 2
10837 <5d> DW_AT_type : <0x6e>
10838 ...
10839 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10840 <76> DW_AT_byte_size : 4
10841 <77> DW_AT_encoding : 5 (signed)
10842
10843 imports the wrong die ( 0x75 instead of 0x58 ).
10844 This case will be ignored until the gcc bug is fixed. */
10845 return;
10846 }
10847
82856980
SW
10848 /* Figure out the local name after import. */
10849 import_alias = dwarf2_name (die, cu);
27aa8d6a 10850
794684b6
SW
10851 /* Figure out where the statement is being imported to. */
10852 import_prefix = determine_prefix (die, cu);
10853
10854 /* Figure out what the scope of the imported die is and prepend it
10855 to the name of the imported die. */
de4affc9 10856 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10857
f55ee35c
JK
10858 if (imported_die->tag != DW_TAG_namespace
10859 && imported_die->tag != DW_TAG_module)
794684b6 10860 {
13387711
SW
10861 imported_declaration = imported_name;
10862 canonical_name = imported_name_prefix;
794684b6 10863 }
13387711 10864 else if (strlen (imported_name_prefix) > 0)
12aaed36 10865 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10866 imported_name_prefix,
10867 (cu->language == language_d ? "." : "::"),
10868 imported_name, (char *) NULL);
13387711
SW
10869 else
10870 canonical_name = imported_name;
794684b6 10871
32019081
JK
10872 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10873 for (child_die = die->child; child_die && child_die->tag;
436c571c 10874 child_die = child_die->sibling)
32019081
JK
10875 {
10876 /* DWARF-4: A Fortran use statement with a “rename list” may be
10877 represented by an imported module entry with an import attribute
10878 referring to the module and owned entries corresponding to those
10879 entities that are renamed as part of being imported. */
10880
10881 if (child_die->tag != DW_TAG_imported_declaration)
10882 {
b98664d3 10883 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10884 "- DIE at %s [in module %s]"),
10885 sect_offset_str (child_die->sect_off),
10886 objfile_name (objfile));
32019081
JK
10887 continue;
10888 }
10889
10890 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10891 if (import_attr == NULL)
10892 {
b98664d3 10893 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10894 dwarf_tag_name (child_die->tag));
10895 continue;
10896 }
10897
10898 imported_cu = cu;
10899 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10900 &imported_cu);
10901 imported_name = dwarf2_name (imported_die, imported_cu);
10902 if (imported_name == NULL)
10903 {
b98664d3 10904 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10905 "imported name - DIE at %s [in module %s]"),
10906 sect_offset_str (child_die->sect_off),
10907 objfile_name (objfile));
32019081
JK
10908 continue;
10909 }
10910
eb1e02fd 10911 excludes.push_back (imported_name);
32019081
JK
10912
10913 process_die (child_die, cu);
10914 }
10915
804d2729 10916 add_using_directive (using_directives (cu),
22cee43f
PMR
10917 import_prefix,
10918 canonical_name,
10919 import_alias,
10920 imported_declaration,
10921 excludes,
10922 0,
10923 &objfile->objfile_obstack);
27aa8d6a
SW
10924}
10925
5230b05a
WT
10926/* ICC<14 does not output the required DW_AT_declaration on incomplete
10927 types, but gives them a size of zero. Starting with version 14,
10928 ICC is compatible with GCC. */
10929
9068261f 10930static bool
5230b05a
WT
10931producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10932{
10933 if (!cu->checked_producer)
10934 check_producer (cu);
10935
10936 return cu->producer_is_icc_lt_14;
10937}
10938
eb77c9df
AB
10939/* ICC generates a DW_AT_type for C void functions. This was observed on
10940 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10941 which says that void functions should not have a DW_AT_type. */
10942
10943static bool
10944producer_is_icc (struct dwarf2_cu *cu)
10945{
10946 if (!cu->checked_producer)
10947 check_producer (cu);
10948
10949 return cu->producer_is_icc;
10950}
10951
1b80a9fa
JK
10952/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10953 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10954 this, it was first present in GCC release 4.3.0. */
10955
9068261f 10956static bool
1b80a9fa
JK
10957producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10958{
10959 if (!cu->checked_producer)
10960 check_producer (cu);
10961
10962 return cu->producer_is_gcc_lt_4_3;
10963}
10964
d721ba37
PA
10965static file_and_directory
10966find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10967{
d721ba37
PA
10968 file_and_directory res;
10969
9291a0cd
TT
10970 /* Find the filename. Do not use dwarf2_name here, since the filename
10971 is not a source language identifier. */
d721ba37
PA
10972 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10973 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10974
d721ba37
PA
10975 if (res.comp_dir == NULL
10976 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10977 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10978 {
d721ba37
PA
10979 res.comp_dir_storage = ldirname (res.name);
10980 if (!res.comp_dir_storage.empty ())
10981 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10982 }
d721ba37 10983 if (res.comp_dir != NULL)
9291a0cd
TT
10984 {
10985 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10986 directory, get rid of it. */
d721ba37 10987 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10988
d721ba37
PA
10989 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10990 res.comp_dir = cp + 1;
9291a0cd
TT
10991 }
10992
d721ba37
PA
10993 if (res.name == NULL)
10994 res.name = "<unknown>";
10995
10996 return res;
9291a0cd
TT
10997}
10998
f4dc4d17
DE
10999/* Handle DW_AT_stmt_list for a compilation unit.
11000 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11001 COMP_DIR is the compilation directory. LOWPC is passed to
11002 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11003
11004static void
11005handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11006 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11007{
976ca316 11008 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11009 struct attribute *attr;
527f3840
JK
11010 struct line_header line_header_local;
11011 hashval_t line_header_local_hash;
527f3840
JK
11012 void **slot;
11013 int decode_mapping;
2ab95328 11014
f4dc4d17
DE
11015 gdb_assert (! cu->per_cu->is_debug_types);
11016
2ab95328 11017 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11018 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11019 return;
11020
d4df075e 11021 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11022
11023 /* The line header hash table is only created if needed (it exists to
11024 prevent redundant reading of the line table for partial_units).
11025 If we're given a partial_unit, we'll need it. If we're given a
11026 compile_unit, then use the line header hash table if it's already
11027 created, but don't create one just yet. */
11028
976ca316 11029 if (per_objfile->line_header_hash == NULL
527f3840 11030 && die->tag == DW_TAG_partial_unit)
2ab95328 11031 {
976ca316 11032 per_objfile->line_header_hash
d15acc42
TT
11033 .reset (htab_create_alloc (127, line_header_hash_voidp,
11034 line_header_eq_voidp,
11035 free_line_header_voidp,
11036 xcalloc, xfree));
527f3840 11037 }
2ab95328 11038
9c541725 11039 line_header_local.sect_off = line_offset;
527f3840
JK
11040 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11041 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11042 if (per_objfile->line_header_hash != NULL)
527f3840 11043 {
976ca316 11044 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11045 &line_header_local,
11046 line_header_local_hash, NO_INSERT);
11047
11048 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11049 is not present in *SLOT (since if there is something in *SLOT then
11050 it will be for a partial_unit). */
11051 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11052 {
527f3840 11053 gdb_assert (*slot != NULL);
9a3c8263 11054 cu->line_header = (struct line_header *) *slot;
527f3840 11055 return;
dee91e82 11056 }
2ab95328 11057 }
527f3840
JK
11058
11059 /* dwarf_decode_line_header does not yet provide sufficient information.
11060 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11061 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11062 if (lh == NULL)
527f3840 11063 return;
4c8aa72d
PA
11064
11065 cu->line_header = lh.release ();
11066 cu->line_header_die_owner = die;
527f3840 11067
976ca316 11068 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11069 slot = NULL;
11070 else
11071 {
976ca316 11072 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11073 &line_header_local,
11074 line_header_local_hash, INSERT);
11075 gdb_assert (slot != NULL);
11076 }
11077 if (slot != NULL && *slot == NULL)
11078 {
11079 /* This newly decoded line number information unit will be owned
11080 by line_header_hash hash table. */
11081 *slot = cu->line_header;
4c8aa72d 11082 cu->line_header_die_owner = NULL;
527f3840
JK
11083 }
11084 else
11085 {
11086 /* We cannot free any current entry in (*slot) as that struct line_header
11087 may be already used by multiple CUs. Create only temporary decoded
11088 line_header for this CU - it may happen at most once for each line
11089 number information unit. And if we're not using line_header_hash
11090 then this is what we want as well. */
11091 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11092 }
11093 decode_mapping = (die->tag != DW_TAG_partial_unit);
11094 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11095 decode_mapping);
fff8551c 11096
2ab95328
TT
11097}
11098
95554aad 11099/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11100
c906108c 11101static void
e7c27a73 11102read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11103{
976ca316
SM
11104 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11105 struct objfile *objfile = per_objfile->objfile;
08feed99 11106 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11107 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11108 CORE_ADDR highpc = ((CORE_ADDR) 0);
11109 struct attribute *attr;
c906108c 11110 struct die_info *child_die;
e142c38c 11111 CORE_ADDR baseaddr;
6e70227d 11112
380618d6 11113 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11114 baseaddr = objfile->text_section_offset ();
c906108c 11115
fae299cd 11116 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11117
11118 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11119 from finish_block. */
2acceee2 11120 if (lowpc == ((CORE_ADDR) -1))
c906108c 11121 lowpc = highpc;
3e29f34a 11122 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11123
d721ba37 11124 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11125
f4b8a18d
KW
11126 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11127 standardised yet. As a workaround for the language detection we fall
11128 back to the DW_AT_producer string. */
11129 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11130 cu->language = language_opencl;
11131
3019eac3
DE
11132 /* Similar hack for Go. */
11133 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11134 set_cu_language (DW_LANG_Go, cu);
11135
c24bdb02 11136 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11137
11138 /* Decode line number information if present. We do this before
11139 processing child DIEs, so that the line header table is available
11140 for DW_AT_decl_file. */
d721ba37 11141 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11142
11143 /* Process all dies in compilation unit. */
11144 if (die->child != NULL)
11145 {
11146 child_die = die->child;
11147 while (child_die && child_die->tag)
11148 {
11149 process_die (child_die, cu);
436c571c 11150 child_die = child_die->sibling;
3019eac3
DE
11151 }
11152 }
11153
11154 /* Decode macro information, if present. Dwarf 2 macro information
11155 refers to information in the line number info statement program
11156 header, so we can only read it if we've read the header
11157 successfully. */
0af92d60
JK
11158 attr = dwarf2_attr (die, DW_AT_macros, cu);
11159 if (attr == NULL)
11160 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11161 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11162 {
11163 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11164 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11165
529908cb 11166 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11167 }
11168 else
11169 {
11170 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11171 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11172 {
529908cb 11173 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11174
43f3e411 11175 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11176 }
11177 }
3019eac3
DE
11178}
11179
c24bdb02
KS
11180void
11181dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11182{
f4dc4d17
DE
11183 struct type_unit_group *tu_group;
11184 int first_time;
3019eac3 11185 struct attribute *attr;
9c541725 11186 unsigned int i;
0186c6a7 11187 struct signatured_type *sig_type;
3019eac3 11188
f4dc4d17 11189 gdb_assert (per_cu->is_debug_types);
0186c6a7 11190 sig_type = (struct signatured_type *) per_cu;
3019eac3 11191
c24bdb02 11192 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11193
f4dc4d17 11194 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11195 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11196 if (sig_type->type_unit_group == NULL)
c24bdb02 11197 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11198 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11199
11200 /* If we've already processed this stmt_list there's no real need to
11201 do it again, we could fake it and just recreate the part we need
11202 (file name,index -> symtab mapping). If data shows this optimization
11203 is useful we can do it then. */
8adb8487
TT
11204 type_unit_group_unshareable *tug_unshare
11205 = per_objfile->get_type_unit_group_unshareable (tu_group);
11206 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11207
11208 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11209 debug info. */
fff8551c 11210 line_header_up lh;
d4df075e 11211 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11212 {
d4df075e 11213 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11214 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11215 }
11216 if (lh == NULL)
11217 {
11218 if (first_time)
c24bdb02 11219 start_symtab ("", NULL, 0);
f4dc4d17
DE
11220 else
11221 {
8adb8487 11222 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11223 gdb_assert (m_builder == nullptr);
8adb8487 11224 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11225 m_builder.reset (new struct buildsym_compunit
11226 (COMPUNIT_OBJFILE (cust), "",
11227 COMPUNIT_DIRNAME (cust),
11228 compunit_language (cust),
11229 0, cust));
770479f2 11230 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11231 }
f4dc4d17 11232 return;
3019eac3
DE
11233 }
11234
c24bdb02
KS
11235 line_header = lh.release ();
11236 line_header_die_owner = die;
3019eac3 11237
f4dc4d17
DE
11238 if (first_time)
11239 {
c24bdb02 11240 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11241
1fd60fc0
DE
11242 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11243 still initializing it, and our caller (a few levels up)
11244 process_full_type_unit still needs to know if this is the first
11245 time. */
11246
8adb8487 11247 tug_unshare->symtabs
4ac93832
TT
11248 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11249 struct symtab *, line_header->file_names_size ());
3019eac3 11250
7ba99d21
AT
11251 auto &file_names = line_header->file_names ();
11252 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11253 {
7ba99d21 11254 file_entry &fe = file_names[i];
c24bdb02
KS
11255 dwarf2_start_subfile (this, fe.name,
11256 fe.include_dir (line_header));
11257 buildsym_compunit *b = get_builder ();
11258 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11259 {
4c8aa72d
PA
11260 /* NOTE: start_subfile will recognize when it's been
11261 passed a file it has already seen. So we can't
11262 assume there's a simple mapping from
11263 cu->line_header->file_names to subfiles, plus
11264 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11265 b->get_current_subfile ()->symtab
11266 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11267 }
11268
c24bdb02 11269 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11270 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11271 }
11272 }
11273 else
3019eac3 11274 {
c24bdb02 11275 gdb_assert (m_builder == nullptr);
8adb8487 11276 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11277 m_builder.reset (new struct buildsym_compunit
11278 (COMPUNIT_OBJFILE (cust), "",
11279 COMPUNIT_DIRNAME (cust),
11280 compunit_language (cust),
11281 0, cust));
770479f2 11282 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11283
7ba99d21
AT
11284 auto &file_names = line_header->file_names ();
11285 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11286 {
7ba99d21 11287 file_entry &fe = file_names[i];
8adb8487 11288 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11289 }
3019eac3
DE
11290 }
11291
f4dc4d17
DE
11292 /* The main symtab is allocated last. Type units don't have DW_AT_name
11293 so they don't have a "real" (so to speak) symtab anyway.
11294 There is later code that will assign the main symtab to all symbols
11295 that don't have one. We need to handle the case of a symbol with a
11296 missing symtab (DW_AT_decl_file) anyway. */
11297}
3019eac3 11298
f4dc4d17
DE
11299/* Process DW_TAG_type_unit.
11300 For TUs we want to skip the first top level sibling if it's not the
11301 actual type being defined by this TU. In this case the first top
11302 level sibling is there to provide context only. */
3019eac3 11303
f4dc4d17
DE
11304static void
11305read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11306{
11307 struct die_info *child_die;
3019eac3 11308
f4dc4d17
DE
11309 prepare_one_comp_unit (cu, die, language_minimal);
11310
11311 /* Initialize (or reinitialize) the machinery for building symtabs.
11312 We do this before processing child DIEs, so that the line header table
11313 is available for DW_AT_decl_file. */
c24bdb02 11314 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11315
11316 if (die->child != NULL)
11317 {
11318 child_die = die->child;
11319 while (child_die && child_die->tag)
11320 {
11321 process_die (child_die, cu);
436c571c 11322 child_die = child_die->sibling;
f4dc4d17
DE
11323 }
11324 }
3019eac3
DE
11325}
11326\f
80626a55
DE
11327/* DWO/DWP files.
11328
11329 http://gcc.gnu.org/wiki/DebugFission
11330 http://gcc.gnu.org/wiki/DebugFissionDWP
11331
11332 To simplify handling of both DWO files ("object" files with the DWARF info)
11333 and DWP files (a file with the DWOs packaged up into one file), we treat
11334 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11335
11336static hashval_t
11337hash_dwo_file (const void *item)
11338{
9a3c8263 11339 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11340 hashval_t hash;
3019eac3 11341
a2ce51a0
DE
11342 hash = htab_hash_string (dwo_file->dwo_name);
11343 if (dwo_file->comp_dir != NULL)
11344 hash += htab_hash_string (dwo_file->comp_dir);
11345 return hash;
3019eac3
DE
11346}
11347
11348static int
11349eq_dwo_file (const void *item_lhs, const void *item_rhs)
11350{
9a3c8263
SM
11351 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11352 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11353
a2ce51a0
DE
11354 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11355 return 0;
11356 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11357 return lhs->comp_dir == rhs->comp_dir;
11358 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11359}
11360
11361/* Allocate a hash table for DWO files. */
11362
51ac9db5 11363static htab_up
298e9637 11364allocate_dwo_file_hash_table ()
3019eac3 11365{
51ac9db5
SM
11366 auto delete_dwo_file = [] (void *item)
11367 {
11368 struct dwo_file *dwo_file = (struct dwo_file *) item;
11369
11370 delete dwo_file;
11371 };
11372
bc68fb19
TT
11373 return htab_up (htab_create_alloc (41,
11374 hash_dwo_file,
11375 eq_dwo_file,
11376 delete_dwo_file,
11377 xcalloc, xfree));
3019eac3
DE
11378}
11379
80626a55
DE
11380/* Lookup DWO file DWO_NAME. */
11381
11382static void **
976ca316 11383lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11384 const char *dwo_name,
11385 const char *comp_dir)
80626a55
DE
11386{
11387 struct dwo_file find_entry;
11388 void **slot;
11389
976ca316
SM
11390 if (per_objfile->per_bfd->dwo_files == NULL)
11391 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11392
0ac5b59e
DE
11393 find_entry.dwo_name = dwo_name;
11394 find_entry.comp_dir = comp_dir;
976ca316 11395 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11396 INSERT);
80626a55
DE
11397
11398 return slot;
11399}
11400
3019eac3
DE
11401static hashval_t
11402hash_dwo_unit (const void *item)
11403{
9a3c8263 11404 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11405
11406 /* This drops the top 32 bits of the id, but is ok for a hash. */
11407 return dwo_unit->signature;
11408}
11409
11410static int
11411eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11412{
9a3c8263
SM
11413 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11414 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11415
11416 /* The signature is assumed to be unique within the DWO file.
11417 So while object file CU dwo_id's always have the value zero,
11418 that's OK, assuming each object file DWO file has only one CU,
11419 and that's the rule for now. */
11420 return lhs->signature == rhs->signature;
11421}
11422
11423/* Allocate a hash table for DWO CUs,TUs.
11424 There is one of these tables for each of CUs,TUs for each DWO file. */
11425
b0b6a987 11426static htab_up
298e9637 11427allocate_dwo_unit_table ()
3019eac3
DE
11428{
11429 /* Start out with a pretty small number.
11430 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11431 return htab_up (htab_create_alloc (3,
11432 hash_dwo_unit,
11433 eq_dwo_unit,
11434 NULL, xcalloc, xfree));
3019eac3
DE
11435}
11436
19c3d4c9 11437/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11438
11439static void
19c3d4c9
DE
11440create_dwo_cu_reader (const struct die_reader_specs *reader,
11441 const gdb_byte *info_ptr,
11442 struct die_info *comp_unit_die,
c0ab21c2
TT
11443 struct dwo_file *dwo_file,
11444 struct dwo_unit *dwo_unit)
3019eac3
DE
11445{
11446 struct dwarf2_cu *cu = reader->cu;
9c541725 11447 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11448 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11449
a084a2a6
AT
11450 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11451 if (!signature.has_value ())
3019eac3 11452 {
b98664d3 11453 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11454 " its dwo_id [in module %s]"),
9d8780f0 11455 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11456 return;
11457 }
11458
3019eac3 11459 dwo_unit->dwo_file = dwo_file;
a084a2a6 11460 dwo_unit->signature = *signature;
8a0459fd 11461 dwo_unit->section = section;
9c541725 11462 dwo_unit->sect_off = sect_off;
3019eac3
DE
11463 dwo_unit->length = cu->per_cu->length;
11464
b4f54984 11465 if (dwarf_read_debug)
9d8780f0
SM
11466 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11467 sect_offset_str (sect_off),
9c541725 11468 hex_string (dwo_unit->signature));
3019eac3
DE
11469}
11470
33c5cd75 11471/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11472 Note: This function processes DWO files only, not DWP files. */
3019eac3 11473
33c5cd75 11474static void
976ca316 11475create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11476 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11477 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11478{
976ca316
SM
11479 struct objfile *objfile = per_objfile->objfile;
11480 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11481 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11482
96b79293 11483 section.read (objfile);
33c5cd75 11484 info_ptr = section.buffer;
3019eac3
DE
11485
11486 if (info_ptr == NULL)
33c5cd75 11487 return;
3019eac3 11488
b4f54984 11489 if (dwarf_read_debug)
19c3d4c9
DE
11490 {
11491 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11492 section.get_name (),
11493 section.get_file_name ());
19c3d4c9 11494 }
3019eac3 11495
33c5cd75 11496 end_ptr = info_ptr + section.size;
3019eac3
DE
11497 while (info_ptr < end_ptr)
11498 {
11499 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11500 struct dwo_unit read_unit {};
33c5cd75
DB
11501 struct dwo_unit *dwo_unit;
11502 void **slot;
11503 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11504
11505 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11506 per_cu.per_bfd = per_bfd;
3019eac3 11507 per_cu.is_debug_types = 0;
33c5cd75
DB
11508 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11509 per_cu.section = &section;
11510
976ca316 11511 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11512 if (!reader.dummy_p)
11513 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11514 &dwo_file, &read_unit);
33c5cd75
DB
11515 info_ptr += per_cu.length;
11516
11517 // If the unit could not be parsed, skip it.
c0ab21c2 11518 if (read_unit.dwo_file == NULL)
33c5cd75 11519 continue;
3019eac3 11520
33c5cd75 11521 if (cus_htab == NULL)
298e9637 11522 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11523
1859c670 11524 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11525 struct dwo_unit);
c0ab21c2 11526 *dwo_unit = read_unit;
b0b6a987 11527 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11528 gdb_assert (slot != NULL);
11529 if (*slot != NULL)
19c3d4c9 11530 {
33c5cd75
DB
11531 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11532 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11533
b98664d3 11534 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11535 " the entry at offset %s, signature %s"),
11536 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11537 hex_string (dwo_unit->signature));
19c3d4c9 11538 }
33c5cd75 11539 *slot = (void *)dwo_unit;
3019eac3 11540 }
3019eac3
DE
11541}
11542
80626a55
DE
11543/* DWP file .debug_{cu,tu}_index section format:
11544 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11545 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11546
11547 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11548 officially standard DWP format was published with DWARF v5 and is called
11549 Version 5. There are no versions 3 or 4.
80626a55 11550
d2415c6c
DE
11551 DWP Version 1:
11552
80626a55
DE
11553 Both index sections have the same format, and serve to map a 64-bit
11554 signature to a set of section numbers. Each section begins with a header,
11555 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11556 indexes, and a pool of 32-bit section numbers. The index sections will be
11557 aligned at 8-byte boundaries in the file.
11558
d2415c6c
DE
11559 The index section header consists of:
11560
11561 V, 32 bit version number
11562 -, 32 bits unused
11563 N, 32 bit number of compilation units or type units in the index
11564 M, 32 bit number of slots in the hash table
80626a55 11565
d2415c6c 11566 Numbers are recorded using the byte order of the application binary.
80626a55 11567
d2415c6c
DE
11568 The hash table begins at offset 16 in the section, and consists of an array
11569 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11570 order of the application binary). Unused slots in the hash table are 0.
11571 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11572
d2415c6c
DE
11573 The parallel table begins immediately after the hash table
11574 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11575 array of 32-bit indexes (using the byte order of the application binary),
11576 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11577 table contains a 32-bit index into the pool of section numbers. For unused
11578 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11579
73869dc2
DE
11580 The pool of section numbers begins immediately following the hash table
11581 (at offset 16 + 12 * M from the beginning of the section). The pool of
11582 section numbers consists of an array of 32-bit words (using the byte order
11583 of the application binary). Each item in the array is indexed starting
11584 from 0. The hash table entry provides the index of the first section
11585 number in the set. Additional section numbers in the set follow, and the
11586 set is terminated by a 0 entry (section number 0 is not used in ELF).
11587
11588 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11589 section must be the first entry in the set, and the .debug_abbrev.dwo must
11590 be the second entry. Other members of the set may follow in any order.
11591
11592 ---
11593
d2854d8d 11594 DWP Versions 2 and 5:
73869dc2 11595
d2854d8d 11596 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11597 and the entries in the index tables are now offsets into these sections.
11598 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11599 section.
11600
11601 Index Section Contents:
11602 Header
11603 Hash Table of Signatures dwp_hash_table.hash_table
11604 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11605 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11606 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11607
11608 The index section header consists of:
11609
11610 V, 32 bit version number
11611 L, 32 bit number of columns in the table of section offsets
11612 N, 32 bit number of compilation units or type units in the index
11613 M, 32 bit number of slots in the hash table
11614
11615 Numbers are recorded using the byte order of the application binary.
11616
11617 The hash table has the same format as version 1.
11618 The parallel table of indices has the same format as version 1,
11619 except that the entries are origin-1 indices into the table of sections
11620 offsets and the table of section sizes.
11621
11622 The table of offsets begins immediately following the parallel table
11623 (at offset 16 + 12 * M from the beginning of the section). The table is
11624 a two-dimensional array of 32-bit words (using the byte order of the
11625 application binary), with L columns and N+1 rows, in row-major order.
11626 Each row in the array is indexed starting from 0. The first row provides
11627 a key to the remaining rows: each column in this row provides an identifier
11628 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11629 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11630
11631 DW_SECT_INFO 1 .debug_info.dwo
11632 DW_SECT_TYPES 2 .debug_types.dwo
11633 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11634 DW_SECT_LINE 4 .debug_line.dwo
11635 DW_SECT_LOC 5 .debug_loc.dwo
11636 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11637 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11638 DW_SECT_MACRO 8 .debug_macro.dwo
11639
d2854d8d
CT
11640 The section identifiers for Version 5 are:
11641
11642 DW_SECT_INFO_V5 1 .debug_info.dwo
11643 DW_SECT_RESERVED_V5 2 --
11644 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11645 DW_SECT_LINE_V5 4 .debug_line.dwo
11646 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11647 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11648 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11649 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11650
73869dc2
DE
11651 The offsets provided by the CU and TU index sections are the base offsets
11652 for the contributions made by each CU or TU to the corresponding section
11653 in the package file. Each CU and TU header contains an abbrev_offset
11654 field, used to find the abbreviations table for that CU or TU within the
11655 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11656 be interpreted as relative to the base offset given in the index section.
11657 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11658 should be interpreted as relative to the base offset for .debug_line.dwo,
11659 and offsets into other debug sections obtained from DWARF attributes should
11660 also be interpreted as relative to the corresponding base offset.
11661
11662 The table of sizes begins immediately following the table of offsets.
11663 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11664 with L columns and N rows, in row-major order. Each row in the array is
11665 indexed starting from 1 (row 0 is shared by the two tables).
11666
11667 ---
11668
11669 Hash table lookup is handled the same in version 1 and 2:
11670
11671 We assume that N and M will not exceed 2^32 - 1.
11672 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11673
d2415c6c
DE
11674 Given a 64-bit compilation unit signature or a type signature S, an entry
11675 in the hash table is located as follows:
80626a55 11676
d2415c6c
DE
11677 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11678 the low-order k bits all set to 1.
80626a55 11679
d2415c6c 11680 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11681
d2415c6c
DE
11682 3) If the hash table entry at index H matches the signature, use that
11683 entry. If the hash table entry at index H is unused (all zeroes),
11684 terminate the search: the signature is not present in the table.
80626a55 11685
d2415c6c 11686 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11687
d2415c6c 11688 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11689 to stop at an unused slot or find the match. */
80626a55
DE
11690
11691/* Create a hash table to map DWO IDs to their CU/TU entry in
11692 .debug_{info,types}.dwo in DWP_FILE.
11693 Returns NULL if there isn't one.
11694 Note: This function processes DWP files only, not DWO files. */
11695
11696static struct dwp_hash_table *
976ca316 11697create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11698 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11699{
976ca316 11700 struct objfile *objfile = per_objfile->objfile;
400174b1 11701 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11702 const gdb_byte *index_ptr, *index_end;
80626a55 11703 struct dwarf2_section_info *index;
73869dc2 11704 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11705 struct dwp_hash_table *htab;
11706
11707 if (is_debug_types)
11708 index = &dwp_file->sections.tu_index;
11709 else
11710 index = &dwp_file->sections.cu_index;
11711
96b79293 11712 if (index->empty ())
80626a55 11713 return NULL;
96b79293 11714 index->read (objfile);
80626a55
DE
11715
11716 index_ptr = index->buffer;
11717 index_end = index_ptr + index->size;
11718
d2854d8d
CT
11719 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11720 For now it's safe to just read 4 bytes (particularly as it's difficult to
11721 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11722 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11723 index_ptr += 4;
d2854d8d 11724 if (version == 2 || version == 5)
73869dc2
DE
11725 nr_columns = read_4_bytes (dbfd, index_ptr);
11726 else
11727 nr_columns = 0;
11728 index_ptr += 4;
80626a55
DE
11729 nr_units = read_4_bytes (dbfd, index_ptr);
11730 index_ptr += 4;
11731 nr_slots = read_4_bytes (dbfd, index_ptr);
11732 index_ptr += 4;
11733
d2854d8d 11734 if (version != 1 && version != 2 && version != 5)
80626a55 11735 {
21aa081e 11736 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11737 " [in module %s]"),
21aa081e 11738 pulongest (version), dwp_file->name);
80626a55
DE
11739 }
11740 if (nr_slots != (nr_slots & -nr_slots))
11741 {
21aa081e 11742 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11743 " is not power of 2 [in module %s]"),
21aa081e 11744 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11745 }
11746
976ca316 11747 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11748 htab->version = version;
11749 htab->nr_columns = nr_columns;
80626a55
DE
11750 htab->nr_units = nr_units;
11751 htab->nr_slots = nr_slots;
11752 htab->hash_table = index_ptr;
11753 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11754
11755 /* Exit early if the table is empty. */
11756 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11757 || (version == 2 && nr_columns == 0)
11758 || (version == 5 && nr_columns == 0))
73869dc2
DE
11759 {
11760 /* All must be zero. */
11761 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11762 || (version == 2 && nr_columns != 0)
11763 || (version == 5 && nr_columns != 0))
73869dc2 11764 {
b98664d3 11765 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11766 " all zero [in modules %s]"),
11767 dwp_file->name);
11768 }
11769 return htab;
11770 }
11771
11772 if (version == 1)
11773 {
11774 htab->section_pool.v1.indices =
11775 htab->unit_table + sizeof (uint32_t) * nr_slots;
11776 /* It's harder to decide whether the section is too small in v1.
11777 V1 is deprecated anyway so we punt. */
11778 }
d2854d8d 11779 else if (version == 2)
73869dc2
DE
11780 {
11781 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11782 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11783 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11784 /* Reverse map for error checking. */
11785 int ids_seen[DW_SECT_MAX + 1];
11786 int i;
11787
11788 if (nr_columns < 2)
11789 {
11790 error (_("Dwarf Error: bad DWP hash table, too few columns"
11791 " in section table [in module %s]"),
11792 dwp_file->name);
11793 }
11794 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11795 {
11796 error (_("Dwarf Error: bad DWP hash table, too many columns"
11797 " in section table [in module %s]"),
11798 dwp_file->name);
11799 }
04fd5eed
GB
11800 memset (ids, 255, sizeof_ids);
11801 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11802 for (i = 0; i < nr_columns; ++i)
11803 {
11804 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11805
11806 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11807 {
11808 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11809 " in section table [in module %s]"),
11810 id, dwp_file->name);
11811 }
11812 if (ids_seen[id] != -1)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11815 " id %d in section table [in module %s]"),
11816 id, dwp_file->name);
11817 }
11818 ids_seen[id] = i;
11819 ids[i] = id;
11820 }
11821 /* Must have exactly one info or types section. */
11822 if (((ids_seen[DW_SECT_INFO] != -1)
11823 + (ids_seen[DW_SECT_TYPES] != -1))
11824 != 1)
11825 {
11826 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11827 " DWO info/types section [in module %s]"),
11828 dwp_file->name);
11829 }
11830 /* Must have an abbrev section. */
11831 if (ids_seen[DW_SECT_ABBREV] == -1)
11832 {
11833 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11834 " section [in module %s]"),
11835 dwp_file->name);
11836 }
11837 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11838 htab->section_pool.v2.sizes =
11839 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11840 * nr_units * nr_columns);
11841 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11842 * nr_units * nr_columns))
11843 > index_end)
11844 {
11845 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11846 " [in module %s]"),
11847 dwp_file->name);
11848 }
11849 }
d2854d8d
CT
11850 else /* version == 5 */
11851 {
11852 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11853 int *ids = htab->section_pool.v5.section_ids;
11854 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11855 /* Reverse map for error checking. */
11856 int ids_seen[DW_SECT_MAX_V5 + 1];
11857
11858 if (nr_columns < 2)
11859 {
11860 error (_("Dwarf Error: bad DWP hash table, too few columns"
11861 " in section table [in module %s]"),
11862 dwp_file->name);
11863 }
11864 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11865 {
11866 error (_("Dwarf Error: bad DWP hash table, too many columns"
11867 " in section table [in module %s]"),
11868 dwp_file->name);
11869 }
11870 memset (ids, 255, sizeof_ids);
11871 memset (ids_seen, 255, sizeof (ids_seen));
11872 for (int i = 0; i < nr_columns; ++i)
11873 {
11874 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11875
11876 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11877 {
11878 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11879 " in section table [in module %s]"),
11880 id, dwp_file->name);
11881 }
11882 if (ids_seen[id] != -1)
11883 {
11884 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11885 " id %d in section table [in module %s]"),
11886 id, dwp_file->name);
11887 }
11888 ids_seen[id] = i;
11889 ids[i] = id;
11890 }
11891 /* Must have seen an info section. */
11892 if (ids_seen[DW_SECT_INFO_V5] == -1)
11893 {
11894 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11895 " DWO info/types section [in module %s]"),
11896 dwp_file->name);
11897 }
11898 /* Must have an abbrev section. */
11899 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11900 {
11901 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11902 " section [in module %s]"),
11903 dwp_file->name);
11904 }
11905 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11906 htab->section_pool.v5.sizes
11907 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11908 * nr_units * nr_columns);
11909 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11910 * nr_units * nr_columns))
11911 > index_end)
11912 {
11913 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11914 " [in module %s]"),
11915 dwp_file->name);
11916 }
11917 }
80626a55
DE
11918
11919 return htab;
11920}
11921
11922/* Update SECTIONS with the data from SECTP.
11923
5bb6e9dd
TT
11924 This function is like the other "locate" section routines, but in
11925 this context the sections to read comes from the DWP V1 hash table,
11926 not the full ELF section table.
80626a55
DE
11927
11928 The result is non-zero for success, or zero if an error was found. */
11929
11930static int
73869dc2
DE
11931locate_v1_virtual_dwo_sections (asection *sectp,
11932 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11933{
11934 const struct dwop_section_names *names = &dwop_section_names;
11935
11936 if (section_is_p (sectp->name, &names->abbrev_dwo))
11937 {
11938 /* There can be only one. */
049412e3 11939 if (sections->abbrev.s.section != NULL)
80626a55 11940 return 0;
049412e3 11941 sections->abbrev.s.section = sectp;
fd361982 11942 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11943 }
11944 else if (section_is_p (sectp->name, &names->info_dwo)
11945 || section_is_p (sectp->name, &names->types_dwo))
11946 {
11947 /* There can be only one. */
049412e3 11948 if (sections->info_or_types.s.section != NULL)
80626a55 11949 return 0;
049412e3 11950 sections->info_or_types.s.section = sectp;
fd361982 11951 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11952 }
11953 else if (section_is_p (sectp->name, &names->line_dwo))
11954 {
11955 /* There can be only one. */
049412e3 11956 if (sections->line.s.section != NULL)
80626a55 11957 return 0;
049412e3 11958 sections->line.s.section = sectp;
fd361982 11959 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11960 }
11961 else if (section_is_p (sectp->name, &names->loc_dwo))
11962 {
11963 /* There can be only one. */
049412e3 11964 if (sections->loc.s.section != NULL)
80626a55 11965 return 0;
049412e3 11966 sections->loc.s.section = sectp;
fd361982 11967 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11968 }
11969 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11970 {
11971 /* There can be only one. */
049412e3 11972 if (sections->macinfo.s.section != NULL)
80626a55 11973 return 0;
049412e3 11974 sections->macinfo.s.section = sectp;
fd361982 11975 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11976 }
11977 else if (section_is_p (sectp->name, &names->macro_dwo))
11978 {
11979 /* There can be only one. */
049412e3 11980 if (sections->macro.s.section != NULL)
80626a55 11981 return 0;
049412e3 11982 sections->macro.s.section = sectp;
fd361982 11983 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11984 }
11985 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11986 {
11987 /* There can be only one. */
049412e3 11988 if (sections->str_offsets.s.section != NULL)
80626a55 11989 return 0;
049412e3 11990 sections->str_offsets.s.section = sectp;
fd361982 11991 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11992 }
11993 else
11994 {
11995 /* No other kind of section is valid. */
11996 return 0;
11997 }
11998
11999 return 1;
12000}
12001
73869dc2
DE
12002/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12003 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12004 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12005 This is for DWP version 1 files. */
80626a55
DE
12006
12007static struct dwo_unit *
976ca316 12008create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12009 struct dwp_file *dwp_file,
73869dc2
DE
12010 uint32_t unit_index,
12011 const char *comp_dir,
12012 ULONGEST signature, int is_debug_types)
80626a55 12013{
73869dc2
DE
12014 const struct dwp_hash_table *dwp_htab =
12015 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12016 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12017 const char *kind = is_debug_types ? "TU" : "CU";
12018 struct dwo_file *dwo_file;
12019 struct dwo_unit *dwo_unit;
73869dc2 12020 struct virtual_v1_dwo_sections sections;
80626a55 12021 void **dwo_file_slot;
80626a55
DE
12022 int i;
12023
73869dc2
DE
12024 gdb_assert (dwp_file->version == 1);
12025
b4f54984 12026 if (dwarf_read_debug)
80626a55 12027 {
73869dc2 12028 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12029 kind,
73869dc2 12030 pulongest (unit_index), hex_string (signature),
80626a55
DE
12031 dwp_file->name);
12032 }
12033
19ac8c2e 12034 /* Fetch the sections of this DWO unit.
80626a55
DE
12035 Put a limit on the number of sections we look for so that bad data
12036 doesn't cause us to loop forever. */
12037
73869dc2 12038#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12039 (1 /* .debug_info or .debug_types */ \
12040 + 1 /* .debug_abbrev */ \
12041 + 1 /* .debug_line */ \
12042 + 1 /* .debug_loc */ \
12043 + 1 /* .debug_str_offsets */ \
19ac8c2e 12044 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12045 + 1 /* trailing zero */)
12046
12047 memset (&sections, 0, sizeof (sections));
80626a55 12048
73869dc2 12049 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12050 {
12051 asection *sectp;
12052 uint32_t section_nr =
12053 read_4_bytes (dbfd,
73869dc2
DE
12054 dwp_htab->section_pool.v1.indices
12055 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12056
12057 if (section_nr == 0)
12058 break;
12059 if (section_nr >= dwp_file->num_sections)
12060 {
12061 error (_("Dwarf Error: bad DWP hash table, section number too large"
12062 " [in module %s]"),
12063 dwp_file->name);
12064 }
12065
12066 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12067 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12068 {
12069 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12070 " [in module %s]"),
12071 dwp_file->name);
12072 }
12073 }
12074
12075 if (i < 2
96b79293
TT
12076 || sections.info_or_types.empty ()
12077 || sections.abbrev.empty ())
80626a55
DE
12078 {
12079 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12080 " [in module %s]"),
12081 dwp_file->name);
12082 }
73869dc2 12083 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12084 {
12085 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12086 " [in module %s]"),
12087 dwp_file->name);
12088 }
12089
12090 /* It's easier for the rest of the code if we fake a struct dwo_file and
12091 have dwo_unit "live" in that. At least for now.
12092
12093 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12094 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12095 file, we can combine them back into a virtual DWO file to save space
12096 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12097 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12098
791afaa2
TT
12099 std::string virtual_dwo_name =
12100 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12101 sections.abbrev.get_id (),
12102 sections.line.get_id (),
12103 sections.loc.get_id (),
12104 sections.str_offsets.get_id ());
80626a55 12105 /* Can we use an existing virtual DWO file? */
976ca316 12106 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12107 comp_dir);
80626a55
DE
12108 /* Create one if necessary. */
12109 if (*dwo_file_slot == NULL)
12110 {
b4f54984 12111 if (dwarf_read_debug)
80626a55
DE
12112 {
12113 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12114 virtual_dwo_name.c_str ());
80626a55 12115 }
51ac9db5 12116 dwo_file = new struct dwo_file;
976ca316 12117 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12118 dwo_file->comp_dir = comp_dir;
80626a55
DE
12119 dwo_file->sections.abbrev = sections.abbrev;
12120 dwo_file->sections.line = sections.line;
12121 dwo_file->sections.loc = sections.loc;
12122 dwo_file->sections.macinfo = sections.macinfo;
12123 dwo_file->sections.macro = sections.macro;
12124 dwo_file->sections.str_offsets = sections.str_offsets;
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12127 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
80626a55
DE
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
b4f54984 12138 if (dwarf_read_debug)
80626a55
DE
12139 {
12140 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12141 virtual_dwo_name.c_str ());
80626a55 12142 }
9a3c8263 12143 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12144 }
80626a55 12145
976ca316 12146 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12147 dwo_unit->dwo_file = dwo_file;
12148 dwo_unit->signature = signature;
8d749320 12149 dwo_unit->section =
976ca316 12150 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12151 *dwo_unit->section = sections.info_or_types;
57d63ce2 12152 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12153
12154 return dwo_unit;
12155}
12156
d2854d8d
CT
12157/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12158 simplify them. Given a pointer to the containing section SECTION, and
12159 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12160 virtual section of just that piece. */
73869dc2
DE
12161
12162static struct dwarf2_section_info
d2854d8d
CT
12163create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12164 struct dwarf2_section_info *section,
12165 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12166{
12167 struct dwarf2_section_info result;
12168 asection *sectp;
12169
12170 gdb_assert (section != NULL);
12171 gdb_assert (!section->is_virtual);
12172
12173 memset (&result, 0, sizeof (result));
12174 result.s.containing_section = section;
dc4ccb6f 12175 result.is_virtual = true;
73869dc2
DE
12176
12177 if (size == 0)
12178 return result;
12179
96b79293 12180 sectp = section->get_bfd_section ();
73869dc2
DE
12181
12182 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12183 bounds of the real section. This is a pretty-rare event, so just
12184 flag an error (easier) instead of a warning and trying to cope. */
12185 if (sectp == NULL
fd361982 12186 || offset + size > bfd_section_size (sectp))
73869dc2 12187 {
d2854d8d 12188 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12189 " in section %s [in module %s]"),
fd361982 12190 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12191 objfile_name (per_objfile->objfile));
73869dc2
DE
12192 }
12193
12194 result.virtual_offset = offset;
12195 result.size = size;
12196 return result;
12197}
12198
12199/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12200 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12201 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12202 This is for DWP version 2 files. */
12203
12204static struct dwo_unit *
976ca316 12205create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12206 struct dwp_file *dwp_file,
73869dc2
DE
12207 uint32_t unit_index,
12208 const char *comp_dir,
12209 ULONGEST signature, int is_debug_types)
12210{
73869dc2
DE
12211 const struct dwp_hash_table *dwp_htab =
12212 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12213 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12214 const char *kind = is_debug_types ? "TU" : "CU";
12215 struct dwo_file *dwo_file;
12216 struct dwo_unit *dwo_unit;
d2854d8d 12217 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12218 void **dwo_file_slot;
73869dc2
DE
12219 int i;
12220
12221 gdb_assert (dwp_file->version == 2);
12222
b4f54984 12223 if (dwarf_read_debug)
73869dc2
DE
12224 {
12225 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12226 kind,
12227 pulongest (unit_index), hex_string (signature),
12228 dwp_file->name);
12229 }
12230
12231 /* Fetch the section offsets of this DWO unit. */
12232
12233 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12234
12235 for (i = 0; i < dwp_htab->nr_columns; ++i)
12236 {
12237 uint32_t offset = read_4_bytes (dbfd,
12238 dwp_htab->section_pool.v2.offsets
12239 + (((unit_index - 1) * dwp_htab->nr_columns
12240 + i)
12241 * sizeof (uint32_t)));
12242 uint32_t size = read_4_bytes (dbfd,
12243 dwp_htab->section_pool.v2.sizes
12244 + (((unit_index - 1) * dwp_htab->nr_columns
12245 + i)
12246 * sizeof (uint32_t)));
12247
12248 switch (dwp_htab->section_pool.v2.section_ids[i])
12249 {
12250 case DW_SECT_INFO:
12251 case DW_SECT_TYPES:
12252 sections.info_or_types_offset = offset;
12253 sections.info_or_types_size = size;
12254 break;
12255 case DW_SECT_ABBREV:
12256 sections.abbrev_offset = offset;
12257 sections.abbrev_size = size;
12258 break;
12259 case DW_SECT_LINE:
12260 sections.line_offset = offset;
12261 sections.line_size = size;
12262 break;
12263 case DW_SECT_LOC:
12264 sections.loc_offset = offset;
12265 sections.loc_size = size;
12266 break;
12267 case DW_SECT_STR_OFFSETS:
12268 sections.str_offsets_offset = offset;
12269 sections.str_offsets_size = size;
12270 break;
12271 case DW_SECT_MACINFO:
12272 sections.macinfo_offset = offset;
12273 sections.macinfo_size = size;
12274 break;
12275 case DW_SECT_MACRO:
12276 sections.macro_offset = offset;
12277 sections.macro_size = size;
12278 break;
12279 }
12280 }
12281
12282 /* It's easier for the rest of the code if we fake a struct dwo_file and
12283 have dwo_unit "live" in that. At least for now.
12284
12285 The DWP file can be made up of a random collection of CUs and TUs.
12286 However, for each CU + set of TUs that came from the same original DWO
12287 file, we can combine them back into a virtual DWO file to save space
12288 (fewer struct dwo_file objects to allocate). Remember that for really
12289 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12290
791afaa2
TT
12291 std::string virtual_dwo_name =
12292 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12293 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12294 (long) (sections.line_size ? sections.line_offset : 0),
12295 (long) (sections.loc_size ? sections.loc_offset : 0),
12296 (long) (sections.str_offsets_size
12297 ? sections.str_offsets_offset : 0));
73869dc2 12298 /* Can we use an existing virtual DWO file? */
976ca316 12299 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12300 comp_dir);
73869dc2
DE
12301 /* Create one if necessary. */
12302 if (*dwo_file_slot == NULL)
12303 {
b4f54984 12304 if (dwarf_read_debug)
73869dc2
DE
12305 {
12306 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12307 virtual_dwo_name.c_str ());
73869dc2 12308 }
51ac9db5 12309 dwo_file = new struct dwo_file;
976ca316 12310 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12311 dwo_file->comp_dir = comp_dir;
12312 dwo_file->sections.abbrev =
d2854d8d
CT
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12314 sections.abbrev_offset,
12315 sections.abbrev_size);
73869dc2 12316 dwo_file->sections.line =
d2854d8d
CT
12317 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12318 sections.line_offset,
12319 sections.line_size);
73869dc2 12320 dwo_file->sections.loc =
d2854d8d
CT
12321 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12322 sections.loc_offset, sections.loc_size);
73869dc2 12323 dwo_file->sections.macinfo =
d2854d8d
CT
12324 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12325 sections.macinfo_offset,
12326 sections.macinfo_size);
73869dc2 12327 dwo_file->sections.macro =
d2854d8d
CT
12328 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12329 sections.macro_offset,
12330 sections.macro_size);
73869dc2 12331 dwo_file->sections.str_offsets =
d2854d8d
CT
12332 create_dwp_v2_or_v5_section (per_objfile,
12333 &dwp_file->sections.str_offsets,
12334 sections.str_offsets_offset,
12335 sections.str_offsets_size);
73869dc2
DE
12336 /* The "str" section is global to the entire DWP file. */
12337 dwo_file->sections.str = dwp_file->sections.str;
12338 /* The info or types section is assigned below to dwo_unit,
12339 there's no need to record it in dwo_file.
12340 Also, we can't simply record type sections in dwo_file because
12341 we record a pointer into the vector in dwo_unit. As we collect more
12342 types we'll grow the vector and eventually have to reallocate space
12343 for it, invalidating all copies of pointers into the previous
12344 contents. */
12345 *dwo_file_slot = dwo_file;
12346 }
12347 else
12348 {
b4f54984 12349 if (dwarf_read_debug)
73869dc2
DE
12350 {
12351 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12352 virtual_dwo_name.c_str ());
73869dc2 12353 }
9a3c8263 12354 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12355 }
73869dc2 12356
976ca316 12357 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12358 dwo_unit->dwo_file = dwo_file;
12359 dwo_unit->signature = signature;
8d749320 12360 dwo_unit->section =
976ca316 12361 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d
CT
12362 *dwo_unit->section = create_dwp_v2_or_v5_section
12363 (per_objfile,
12364 is_debug_types
12365 ? &dwp_file->sections.types
12366 : &dwp_file->sections.info,
12367 sections.info_or_types_offset,
12368 sections.info_or_types_size);
12369 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12370
12371 return dwo_unit;
12372}
12373
12374/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12375 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12376 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12377 This is for DWP version 5 files. */
12378
12379static struct dwo_unit *
12380create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12381 struct dwp_file *dwp_file,
12382 uint32_t unit_index,
12383 const char *comp_dir,
12384 ULONGEST signature, int is_debug_types)
12385{
12386 const struct dwp_hash_table *dwp_htab
12387 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12388 bfd *dbfd = dwp_file->dbfd.get ();
12389 const char *kind = is_debug_types ? "TU" : "CU";
12390 struct dwo_file *dwo_file;
12391 struct dwo_unit *dwo_unit;
12392 struct virtual_v2_or_v5_dwo_sections sections {};
12393 void **dwo_file_slot;
12394
12395 gdb_assert (dwp_file->version == 5);
12396
12397 if (dwarf_read_debug)
12398 {
12399 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12400 kind,
12401 pulongest (unit_index), hex_string (signature),
12402 dwp_file->name);
12403 }
12404
12405 /* Fetch the section offsets of this DWO unit. */
12406
12407 /* memset (&sections, 0, sizeof (sections)); */
12408
12409 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12410 {
12411 uint32_t offset = read_4_bytes (dbfd,
12412 dwp_htab->section_pool.v5.offsets
12413 + (((unit_index - 1)
12414 * dwp_htab->nr_columns
12415 + i)
12416 * sizeof (uint32_t)));
12417 uint32_t size = read_4_bytes (dbfd,
12418 dwp_htab->section_pool.v5.sizes
12419 + (((unit_index - 1) * dwp_htab->nr_columns
12420 + i)
12421 * sizeof (uint32_t)));
12422
12423 switch (dwp_htab->section_pool.v5.section_ids[i])
12424 {
12425 case DW_SECT_ABBREV_V5:
12426 sections.abbrev_offset = offset;
12427 sections.abbrev_size = size;
12428 break;
12429 case DW_SECT_INFO_V5:
12430 sections.info_or_types_offset = offset;
12431 sections.info_or_types_size = size;
12432 break;
12433 case DW_SECT_LINE_V5:
12434 sections.line_offset = offset;
12435 sections.line_size = size;
12436 break;
12437 case DW_SECT_LOCLISTS_V5:
12438 sections.loclists_offset = offset;
12439 sections.loclists_size = size;
12440 break;
12441 case DW_SECT_MACRO_V5:
12442 sections.macro_offset = offset;
12443 sections.macro_size = size;
12444 break;
12445 case DW_SECT_RNGLISTS_V5:
12446 sections.rnglists_offset = offset;
12447 sections.rnglists_size = size;
12448 break;
12449 case DW_SECT_STR_OFFSETS_V5:
12450 sections.str_offsets_offset = offset;
12451 sections.str_offsets_size = size;
12452 break;
12453 case DW_SECT_RESERVED_V5:
12454 default:
12455 break;
12456 }
12457 }
12458
12459 /* It's easier for the rest of the code if we fake a struct dwo_file and
12460 have dwo_unit "live" in that. At least for now.
12461
12462 The DWP file can be made up of a random collection of CUs and TUs.
12463 However, for each CU + set of TUs that came from the same original DWO
12464 file, we can combine them back into a virtual DWO file to save space
12465 (fewer struct dwo_file objects to allocate). Remember that for really
12466 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12467
12468 std::string virtual_dwo_name =
12469 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12470 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12471 (long) (sections.line_size ? sections.line_offset : 0),
12472 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12473 (long) (sections.str_offsets_size
12474 ? sections.str_offsets_offset : 0),
12475 (long) (sections.macro_size ? sections.macro_offset : 0),
12476 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12477 /* Can we use an existing virtual DWO file? */
12478 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12479 virtual_dwo_name.c_str (),
12480 comp_dir);
12481 /* Create one if necessary. */
12482 if (*dwo_file_slot == NULL)
12483 {
12484 if (dwarf_read_debug)
12485 {
12486 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12487 virtual_dwo_name.c_str ());
12488 }
12489 dwo_file = new struct dwo_file;
12490 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12491 dwo_file->comp_dir = comp_dir;
12492 dwo_file->sections.abbrev =
12493 create_dwp_v2_or_v5_section (per_objfile,
12494 &dwp_file->sections.abbrev,
12495 sections.abbrev_offset,
12496 sections.abbrev_size);
12497 dwo_file->sections.line =
12498 create_dwp_v2_or_v5_section (per_objfile,
12499 &dwp_file->sections.line,
12500 sections.line_offset, sections.line_size);
12501 dwo_file->sections.macro =
12502 create_dwp_v2_or_v5_section (per_objfile,
12503 &dwp_file->sections.macro,
12504 sections.macro_offset,
12505 sections.macro_size);
12506 dwo_file->sections.loclists =
12507 create_dwp_v2_or_v5_section (per_objfile,
12508 &dwp_file->sections.loclists,
12509 sections.loclists_offset,
12510 sections.loclists_size);
12511 dwo_file->sections.rnglists =
12512 create_dwp_v2_or_v5_section (per_objfile,
12513 &dwp_file->sections.rnglists,
12514 sections.rnglists_offset,
12515 sections.rnglists_size);
12516 dwo_file->sections.str_offsets =
12517 create_dwp_v2_or_v5_section (per_objfile,
12518 &dwp_file->sections.str_offsets,
12519 sections.str_offsets_offset,
12520 sections.str_offsets_size);
12521 /* The "str" section is global to the entire DWP file. */
12522 dwo_file->sections.str = dwp_file->sections.str;
12523 /* The info or types section is assigned below to dwo_unit,
12524 there's no need to record it in dwo_file.
12525 Also, we can't simply record type sections in dwo_file because
12526 we record a pointer into the vector in dwo_unit. As we collect more
12527 types we'll grow the vector and eventually have to reallocate space
12528 for it, invalidating all copies of pointers into the previous
12529 contents. */
12530 *dwo_file_slot = dwo_file;
12531 }
12532 else
12533 {
12534 if (dwarf_read_debug)
12535 {
12536 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12537 virtual_dwo_name.c_str ());
12538 }
12539 dwo_file = (struct dwo_file *) *dwo_file_slot;
12540 }
12541
12542 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12543 dwo_unit->dwo_file = dwo_file;
12544 dwo_unit->signature = signature;
12545 dwo_unit->section
12546 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12547 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12548 &dwp_file->sections.info,
12549 sections.info_or_types_offset,
12550 sections.info_or_types_size);
73869dc2
DE
12551 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12552
12553 return dwo_unit;
12554}
12555
57d63ce2
DE
12556/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12557 Returns NULL if the signature isn't found. */
80626a55
DE
12558
12559static struct dwo_unit *
976ca316 12560lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12561 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12562 ULONGEST signature, int is_debug_types)
80626a55 12563{
57d63ce2
DE
12564 const struct dwp_hash_table *dwp_htab =
12565 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12566 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12567 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12568 uint32_t hash = signature & mask;
12569 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12570 unsigned int i;
12571 void **slot;
870f88f7 12572 struct dwo_unit find_dwo_cu;
80626a55
DE
12573
12574 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12575 find_dwo_cu.signature = signature;
19ac8c2e 12576 slot = htab_find_slot (is_debug_types
48b490f2
TT
12577 ? dwp_file->loaded_tus.get ()
12578 : dwp_file->loaded_cus.get (),
19ac8c2e 12579 &find_dwo_cu, INSERT);
80626a55
DE
12580
12581 if (*slot != NULL)
9a3c8263 12582 return (struct dwo_unit *) *slot;
80626a55
DE
12583
12584 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12585 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12586 {
12587 ULONGEST signature_in_table;
12588
12589 signature_in_table =
57d63ce2 12590 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12591 if (signature_in_table == signature)
12592 {
57d63ce2
DE
12593 uint32_t unit_index =
12594 read_4_bytes (dbfd,
12595 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12596
73869dc2
DE
12597 if (dwp_file->version == 1)
12598 {
976ca316
SM
12599 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12600 unit_index, comp_dir,
12601 signature, is_debug_types);
73869dc2 12602 }
d2854d8d 12603 else if (dwp_file->version == 2)
73869dc2 12604 {
976ca316
SM
12605 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12606 unit_index, comp_dir,
12607 signature, is_debug_types);
73869dc2 12608 }
d2854d8d
CT
12609 else /* version == 5 */
12610 {
12611 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12612 unit_index, comp_dir,
12613 signature, is_debug_types);
12614 }
9a3c8263 12615 return (struct dwo_unit *) *slot;
80626a55
DE
12616 }
12617 if (signature_in_table == 0)
12618 return NULL;
12619 hash = (hash + hash2) & mask;
12620 }
12621
12622 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12623 " [in module %s]"),
12624 dwp_file->name);
12625}
12626
ab5088bf 12627/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12628 Open the file specified by FILE_NAME and hand it off to BFD for
12629 preliminary analysis. Return a newly initialized bfd *, which
12630 includes a canonicalized copy of FILE_NAME.
80626a55 12631 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12632 SEARCH_CWD is true if the current directory is to be searched.
12633 It will be searched before debug-file-directory.
13aaf454
DE
12634 If successful, the file is added to the bfd include table of the
12635 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12636 If unable to find/open the file, return NULL.
3019eac3
DE
12637 NOTE: This function is derived from symfile_bfd_open. */
12638
192b62ce 12639static gdb_bfd_ref_ptr
976ca316 12640try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12641 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12642{
24b9144d 12643 int desc;
9c02c129
DE
12644 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12645 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12646 to debug_file_directory. */
e0cc99a6 12647 const char *search_path;
9c02c129
DE
12648 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12649
e0cc99a6 12650 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12651 if (search_cwd)
12652 {
12653 if (*debug_file_directory != '\0')
e0cc99a6
TT
12654 {
12655 search_path_holder.reset (concat (".", dirname_separator_string,
12656 debug_file_directory,
12657 (char *) NULL));
12658 search_path = search_path_holder.get ();
12659 }
6ac97d4c 12660 else
e0cc99a6 12661 search_path = ".";
6ac97d4c 12662 }
9c02c129 12663 else
e0cc99a6 12664 search_path = debug_file_directory;
3019eac3 12665
24b9144d 12666 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12667 if (is_dwp)
12668 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12669
12670 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12671 desc = openp (search_path, flags, file_name,
3019eac3
DE
12672 O_RDONLY | O_BINARY, &absolute_name);
12673 if (desc < 0)
12674 return NULL;
12675
e0cc99a6
TT
12676 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12677 gnutarget, desc));
9c02c129
DE
12678 if (sym_bfd == NULL)
12679 return NULL;
192b62ce 12680 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12681
192b62ce
TT
12682 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12683 return NULL;
3019eac3 12684
13aaf454
DE
12685 /* Success. Record the bfd as having been included by the objfile's bfd.
12686 This is important because things like demangled_names_hash lives in the
12687 objfile's per_bfd space and may have references to things like symbol
12688 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12689 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12690
3019eac3
DE
12691 return sym_bfd;
12692}
12693
ab5088bf 12694/* Try to open DWO file FILE_NAME.
3019eac3
DE
12695 COMP_DIR is the DW_AT_comp_dir attribute.
12696 The result is the bfd handle of the file.
12697 If there is a problem finding or opening the file, return NULL.
12698 Upon success, the canonicalized path of the file is stored in the bfd,
12699 same as symfile_bfd_open. */
12700
192b62ce 12701static gdb_bfd_ref_ptr
976ca316 12702open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12703 const char *file_name, const char *comp_dir)
3019eac3 12704{
80626a55 12705 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12706 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12707 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12708
12709 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12710
12711 if (comp_dir != NULL)
12712 {
43816ebc
TT
12713 gdb::unique_xmalloc_ptr<char> path_to_try
12714 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12715
12716 /* NOTE: If comp_dir is a relative path, this will also try the
12717 search path, which seems useful. */
976ca316 12718 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12719 0 /*is_dwp*/,
192b62ce 12720 1 /*search_cwd*/));
3019eac3
DE
12721 if (abfd != NULL)
12722 return abfd;
12723 }
12724
12725 /* That didn't work, try debug-file-directory, which, despite its name,
12726 is a list of paths. */
12727
12728 if (*debug_file_directory == '\0')
12729 return NULL;
12730
976ca316 12731 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12732 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12733}
12734
80626a55
DE
12735/* This function is mapped across the sections and remembers the offset and
12736 size of each of the DWO debugging sections we are interested in. */
12737
12738static void
5bb6e9dd
TT
12739dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12740 dwo_sections *dwo_sections)
80626a55 12741{
80626a55
DE
12742 const struct dwop_section_names *names = &dwop_section_names;
12743
12744 if (section_is_p (sectp->name, &names->abbrev_dwo))
12745 {
049412e3 12746 dwo_sections->abbrev.s.section = sectp;
fd361982 12747 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12748 }
12749 else if (section_is_p (sectp->name, &names->info_dwo))
12750 {
049412e3 12751 dwo_sections->info.s.section = sectp;
fd361982 12752 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12753 }
12754 else if (section_is_p (sectp->name, &names->line_dwo))
12755 {
049412e3 12756 dwo_sections->line.s.section = sectp;
fd361982 12757 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12758 }
12759 else if (section_is_p (sectp->name, &names->loc_dwo))
12760 {
049412e3 12761 dwo_sections->loc.s.section = sectp;
fd361982 12762 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12763 }
41144253 12764 else if (section_is_p (sectp->name, &names->loclists_dwo))
12765 {
12766 dwo_sections->loclists.s.section = sectp;
12767 dwo_sections->loclists.size = bfd_section_size (sectp);
12768 }
80626a55
DE
12769 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12770 {
049412e3 12771 dwo_sections->macinfo.s.section = sectp;
fd361982 12772 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12773 }
12774 else if (section_is_p (sectp->name, &names->macro_dwo))
12775 {
049412e3 12776 dwo_sections->macro.s.section = sectp;
fd361982 12777 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12778 }
d0ce17d8
CT
12779 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12780 {
12781 dwo_sections->rnglists.s.section = sectp;
12782 dwo_sections->rnglists.size = bfd_section_size (sectp);
12783 }
80626a55
DE
12784 else if (section_is_p (sectp->name, &names->str_dwo))
12785 {
049412e3 12786 dwo_sections->str.s.section = sectp;
fd361982 12787 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12788 }
12789 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12790 {
049412e3 12791 dwo_sections->str_offsets.s.section = sectp;
fd361982 12792 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12793 }
12794 else if (section_is_p (sectp->name, &names->types_dwo))
12795 {
12796 struct dwarf2_section_info type_section;
12797
12798 memset (&type_section, 0, sizeof (type_section));
049412e3 12799 type_section.s.section = sectp;
fd361982 12800 type_section.size = bfd_section_size (sectp);
fd5866f6 12801 dwo_sections->types.push_back (type_section);
80626a55
DE
12802 }
12803}
12804
ab5088bf 12805/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12806 by PER_CU. This is for the non-DWP case.
80626a55 12807 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12808
12809static struct dwo_file *
4ab09049
SM
12810open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12811 const char *comp_dir)
3019eac3 12812{
976ca316 12813 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12814
976ca316 12815 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12816 if (dbfd == NULL)
12817 {
b4f54984 12818 if (dwarf_read_debug)
80626a55
DE
12819 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12820 return NULL;
12821 }
263db9a1 12822
51ac9db5 12823 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12824 dwo_file->dwo_name = dwo_name;
12825 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12826 dwo_file->dbfd = std::move (dbfd);
3019eac3 12827
5bb6e9dd
TT
12828 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12829 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12830 &dwo_file->sections);
3019eac3 12831
976ca316
SM
12832 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12833 dwo_file->cus);
3019eac3 12834
d2854d8d
CT
12835 if (cu->per_cu->dwarf_version < 5)
12836 {
12837 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12838 dwo_file->sections.types, dwo_file->tus);
12839 }
12840 else
12841 {
12842 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12843 &dwo_file->sections.info, dwo_file->tus,
12844 rcuh_kind::TYPE);
12845 }
3019eac3 12846
b4f54984 12847 if (dwarf_read_debug)
80626a55
DE
12848 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12849
263db9a1 12850 return dwo_file.release ();
3019eac3
DE
12851}
12852
80626a55 12853/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12854 size of each of the DWP debugging sections common to version 1 and 2 that
12855 we are interested in. */
3019eac3 12856
80626a55 12857static void
73869dc2 12858dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12859 dwp_file *dwp_file)
3019eac3 12860{
80626a55
DE
12861 const struct dwop_section_names *names = &dwop_section_names;
12862 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12863
80626a55 12864 /* Record the ELF section number for later lookup: this is what the
73869dc2 12865 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12866 gdb_assert (elf_section_nr < dwp_file->num_sections);
12867 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12868
80626a55
DE
12869 /* Look for specific sections that we need. */
12870 if (section_is_p (sectp->name, &names->str_dwo))
12871 {
049412e3 12872 dwp_file->sections.str.s.section = sectp;
fd361982 12873 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12874 }
12875 else if (section_is_p (sectp->name, &names->cu_index))
12876 {
049412e3 12877 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12878 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12879 }
12880 else if (section_is_p (sectp->name, &names->tu_index))
12881 {
049412e3 12882 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12883 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12884 }
12885}
3019eac3 12886
73869dc2
DE
12887/* This function is mapped across the sections and remembers the offset and
12888 size of each of the DWP version 2 debugging sections that we are interested
12889 in. This is split into a separate function because we don't know if we
d2854d8d 12890 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12891
12892static void
12893dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12894{
9a3c8263 12895 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12896 const struct dwop_section_names *names = &dwop_section_names;
12897 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12898
12899 /* Record the ELF section number for later lookup: this is what the
12900 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12901 gdb_assert (elf_section_nr < dwp_file->num_sections);
12902 dwp_file->elf_sections[elf_section_nr] = sectp;
12903
12904 /* Look for specific sections that we need. */
12905 if (section_is_p (sectp->name, &names->abbrev_dwo))
12906 {
049412e3 12907 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12908 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12909 }
12910 else if (section_is_p (sectp->name, &names->info_dwo))
12911 {
049412e3 12912 dwp_file->sections.info.s.section = sectp;
fd361982 12913 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12914 }
12915 else if (section_is_p (sectp->name, &names->line_dwo))
12916 {
049412e3 12917 dwp_file->sections.line.s.section = sectp;
fd361982 12918 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12919 }
12920 else if (section_is_p (sectp->name, &names->loc_dwo))
12921 {
049412e3 12922 dwp_file->sections.loc.s.section = sectp;
fd361982 12923 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12924 }
12925 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12926 {
049412e3 12927 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12928 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12929 }
12930 else if (section_is_p (sectp->name, &names->macro_dwo))
12931 {
049412e3 12932 dwp_file->sections.macro.s.section = sectp;
fd361982 12933 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12934 }
12935 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12936 {
049412e3 12937 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12938 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12939 }
12940 else if (section_is_p (sectp->name, &names->types_dwo))
12941 {
049412e3 12942 dwp_file->sections.types.s.section = sectp;
fd361982 12943 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12944 }
12945}
12946
d2854d8d
CT
12947/* This function is mapped across the sections and remembers the offset and
12948 size of each of the DWP version 5 debugging sections that we are interested
12949 in. This is split into a separate function because we don't know if we
12950 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12951
12952static void
12953dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12954{
12955 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12956 const struct dwop_section_names *names = &dwop_section_names;
12957 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12958
12959 /* Record the ELF section number for later lookup: this is what the
12960 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12961 gdb_assert (elf_section_nr < dwp_file->num_sections);
12962 dwp_file->elf_sections[elf_section_nr] = sectp;
12963
12964 /* Look for specific sections that we need. */
12965 if (section_is_p (sectp->name, &names->abbrev_dwo))
12966 {
12967 dwp_file->sections.abbrev.s.section = sectp;
12968 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12969 }
12970 else if (section_is_p (sectp->name, &names->info_dwo))
12971 {
12972 dwp_file->sections.info.s.section = sectp;
12973 dwp_file->sections.info.size = bfd_section_size (sectp);
12974 }
12975 else if (section_is_p (sectp->name, &names->line_dwo))
12976 {
12977 dwp_file->sections.line.s.section = sectp;
12978 dwp_file->sections.line.size = bfd_section_size (sectp);
12979 }
12980 else if (section_is_p (sectp->name, &names->loclists_dwo))
12981 {
12982 dwp_file->sections.loclists.s.section = sectp;
12983 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12984 }
12985 else if (section_is_p (sectp->name, &names->macro_dwo))
12986 {
12987 dwp_file->sections.macro.s.section = sectp;
12988 dwp_file->sections.macro.size = bfd_section_size (sectp);
12989 }
12990 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12991 {
12992 dwp_file->sections.rnglists.s.section = sectp;
12993 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12994 }
12995 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12996 {
12997 dwp_file->sections.str_offsets.s.section = sectp;
12998 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12999 }
13000}
13001
80626a55 13002/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13003
80626a55
DE
13004static hashval_t
13005hash_dwp_loaded_cutus (const void *item)
13006{
9a3c8263 13007 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13008
80626a55
DE
13009 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13010 return dwo_unit->signature;
3019eac3
DE
13011}
13012
80626a55 13013/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13014
80626a55
DE
13015static int
13016eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13017{
9a3c8263
SM
13018 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13019 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13020
80626a55
DE
13021 return dua->signature == dub->signature;
13022}
3019eac3 13023
80626a55 13024/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13025
48b490f2 13026static htab_up
298e9637 13027allocate_dwp_loaded_cutus_table ()
80626a55 13028{
48b490f2
TT
13029 return htab_up (htab_create_alloc (3,
13030 hash_dwp_loaded_cutus,
13031 eq_dwp_loaded_cutus,
13032 NULL, xcalloc, xfree));
80626a55 13033}
3019eac3 13034
ab5088bf
DE
13035/* Try to open DWP file FILE_NAME.
13036 The result is the bfd handle of the file.
13037 If there is a problem finding or opening the file, return NULL.
13038 Upon success, the canonicalized path of the file is stored in the bfd,
13039 same as symfile_bfd_open. */
13040
192b62ce 13041static gdb_bfd_ref_ptr
976ca316 13042open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13043{
976ca316 13044 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13045 1 /*is_dwp*/,
192b62ce 13046 1 /*search_cwd*/));
6ac97d4c
DE
13047 if (abfd != NULL)
13048 return abfd;
13049
13050 /* Work around upstream bug 15652.
13051 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13052 [Whether that's a "bug" is debatable, but it is getting in our way.]
13053 We have no real idea where the dwp file is, because gdb's realpath-ing
13054 of the executable's path may have discarded the needed info.
13055 [IWBN if the dwp file name was recorded in the executable, akin to
13056 .gnu_debuglink, but that doesn't exist yet.]
13057 Strip the directory from FILE_NAME and search again. */
13058 if (*debug_file_directory != '\0')
13059 {
13060 /* Don't implicitly search the current directory here.
13061 If the user wants to search "." to handle this case,
13062 it must be added to debug-file-directory. */
976ca316
SM
13063 return try_open_dwop_file (per_objfile, lbasename (file_name),
13064 1 /*is_dwp*/,
6ac97d4c
DE
13065 0 /*search_cwd*/);
13066 }
13067
13068 return NULL;
ab5088bf
DE
13069}
13070
80626a55
DE
13071/* Initialize the use of the DWP file for the current objfile.
13072 By convention the name of the DWP file is ${objfile}.dwp.
13073 The result is NULL if it can't be found. */
a766d390 13074
400174b1 13075static std::unique_ptr<struct dwp_file>
976ca316 13076open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13077{
976ca316 13078 struct objfile *objfile = per_objfile->objfile;
80626a55 13079
82bf32bc
JK
13080 /* Try to find first .dwp for the binary file before any symbolic links
13081 resolving. */
6c447423
DE
13082
13083 /* If the objfile is a debug file, find the name of the real binary
13084 file and get the name of dwp file from there. */
d721ba37 13085 std::string dwp_name;
6c447423
DE
13086 if (objfile->separate_debug_objfile_backlink != NULL)
13087 {
13088 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13089 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13090
d721ba37 13091 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13092 }
13093 else
d721ba37
PA
13094 dwp_name = objfile->original_name;
13095
13096 dwp_name += ".dwp";
80626a55 13097
976ca316 13098 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13099 if (dbfd == NULL
13100 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13101 {
13102 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13103 dwp_name = objfile_name (objfile);
13104 dwp_name += ".dwp";
976ca316 13105 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13106 }
13107
80626a55
DE
13108 if (dbfd == NULL)
13109 {
b4f54984 13110 if (dwarf_read_debug)
d721ba37 13111 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13112 return std::unique_ptr<dwp_file> ();
3019eac3 13113 }
400174b1
TT
13114
13115 const char *name = bfd_get_filename (dbfd.get ());
13116 std::unique_ptr<struct dwp_file> dwp_file
13117 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13118
0a0f4c01 13119 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13120 dwp_file->elf_sections =
976ca316 13121 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13122 dwp_file->num_sections, asection *);
13123
5bb6e9dd
TT
13124 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13125 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13126 dwp_file.get ());
80626a55 13127
976ca316 13128 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13129
976ca316 13130 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13131
73869dc2 13132 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13133 if (dwp_file->cus && dwp_file->tus
13134 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13135 {
13136 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13137 pretty bizarre. We use pulongest here because that's the established
4d65956b 13138 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13139 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13140 " TU version %s [in DWP file %s]"),
13141 pulongest (dwp_file->cus->version),
d721ba37 13142 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13143 }
08302ed2
DE
13144
13145 if (dwp_file->cus)
13146 dwp_file->version = dwp_file->cus->version;
13147 else if (dwp_file->tus)
13148 dwp_file->version = dwp_file->tus->version;
13149 else
13150 dwp_file->version = 2;
73869dc2 13151
5bb6e9dd
TT
13152 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13153 {
13154 if (dwp_file->version == 2)
13155 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13156 dwp_file.get ());
13157 else
13158 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13159 dwp_file.get ());
13160 }
73869dc2 13161
298e9637
SM
13162 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13163 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13164
b4f54984 13165 if (dwarf_read_debug)
80626a55
DE
13166 {
13167 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13168 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13169 " %s CUs, %s TUs\n",
13170 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13171 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13172 }
13173
13174 return dwp_file;
3019eac3 13175}
c906108c 13176
ab5088bf
DE
13177/* Wrapper around open_and_init_dwp_file, only open it once. */
13178
13179static struct dwp_file *
976ca316 13180get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13181{
976ca316 13182 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13183 {
976ca316
SM
13184 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13185 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13186 }
976ca316 13187 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13188}
13189
80626a55
DE
13190/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13191 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13192 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13193 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13194 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13195
13196 This is called, for example, when wanting to read a variable with a
13197 complex location. Therefore we don't want to do file i/o for every call.
13198 Therefore we don't want to look for a DWO file on every call.
13199 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13200 then we check if we've already seen DWO_NAME, and only THEN do we check
13201 for a DWO file.
13202
1c658ad5 13203 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13204 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13205
3019eac3 13206static struct dwo_unit *
4ab09049 13207lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13208 ULONGEST signature, int is_debug_types)
3019eac3 13209{
976ca316
SM
13210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13211 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13212 const char *kind = is_debug_types ? "TU" : "CU";
13213 void **dwo_file_slot;
3019eac3 13214 struct dwo_file *dwo_file;
80626a55 13215 struct dwp_file *dwp_file;
cb1df416 13216
6a506a2d
DE
13217 /* First see if there's a DWP file.
13218 If we have a DWP file but didn't find the DWO inside it, don't
13219 look for the original DWO file. It makes gdb behave differently
13220 depending on whether one is debugging in the build tree. */
cf2c3c16 13221
976ca316 13222 dwp_file = get_dwp_file (per_objfile);
80626a55 13223 if (dwp_file != NULL)
cf2c3c16 13224 {
80626a55
DE
13225 const struct dwp_hash_table *dwp_htab =
13226 is_debug_types ? dwp_file->tus : dwp_file->cus;
13227
13228 if (dwp_htab != NULL)
13229 {
13230 struct dwo_unit *dwo_cutu =
976ca316
SM
13231 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13232 is_debug_types);
80626a55
DE
13233
13234 if (dwo_cutu != NULL)
13235 {
b4f54984 13236 if (dwarf_read_debug)
80626a55
DE
13237 {
13238 fprintf_unfiltered (gdb_stdlog,
13239 "Virtual DWO %s %s found: @%s\n",
13240 kind, hex_string (signature),
13241 host_address_to_string (dwo_cutu));
13242 }
13243 return dwo_cutu;
13244 }
13245 }
13246 }
6a506a2d 13247 else
80626a55 13248 {
6a506a2d 13249 /* No DWP file, look for the DWO file. */
80626a55 13250
976ca316 13251 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13252 if (*dwo_file_slot == NULL)
80626a55 13253 {
6a506a2d 13254 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13255 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13256 }
6a506a2d 13257 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13258 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13259
6a506a2d 13260 if (dwo_file != NULL)
19c3d4c9 13261 {
6a506a2d
DE
13262 struct dwo_unit *dwo_cutu = NULL;
13263
13264 if (is_debug_types && dwo_file->tus)
13265 {
13266 struct dwo_unit find_dwo_cutu;
13267
13268 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13269 find_dwo_cutu.signature = signature;
9a3c8263 13270 dwo_cutu
b0b6a987
TT
13271 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13272 &find_dwo_cutu);
6a506a2d 13273 }
33c5cd75 13274 else if (!is_debug_types && dwo_file->cus)
80626a55 13275 {
33c5cd75
DB
13276 struct dwo_unit find_dwo_cutu;
13277
13278 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13279 find_dwo_cutu.signature = signature;
b0b6a987 13280 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13281 &find_dwo_cutu);
6a506a2d
DE
13282 }
13283
13284 if (dwo_cutu != NULL)
13285 {
b4f54984 13286 if (dwarf_read_debug)
6a506a2d
DE
13287 {
13288 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13289 kind, dwo_name, hex_string (signature),
13290 host_address_to_string (dwo_cutu));
13291 }
13292 return dwo_cutu;
80626a55
DE
13293 }
13294 }
2e276125 13295 }
9cdd5dbd 13296
80626a55
DE
13297 /* We didn't find it. This could mean a dwo_id mismatch, or
13298 someone deleted the DWO/DWP file, or the search path isn't set up
13299 correctly to find the file. */
13300
b4f54984 13301 if (dwarf_read_debug)
80626a55
DE
13302 {
13303 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13304 kind, dwo_name, hex_string (signature));
13305 }
3019eac3 13306
6656a72d
DE
13307 /* This is a warning and not a complaint because it can be caused by
13308 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13309 {
13310 /* Print the name of the DWP file if we looked there, helps the user
13311 better diagnose the problem. */
791afaa2 13312 std::string dwp_text;
43942612
DE
13313
13314 if (dwp_file != NULL)
791afaa2
TT
13315 dwp_text = string_printf (" [in DWP file %s]",
13316 lbasename (dwp_file->name));
43942612 13317
9d8780f0 13318 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13319 " [in module %s]"),
4ab09049
SM
13320 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13321 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13322 }
3019eac3 13323 return NULL;
5fb290d7
DJ
13324}
13325
80626a55
DE
13326/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13327 See lookup_dwo_cutu_unit for details. */
13328
13329static struct dwo_unit *
4ab09049 13330lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13331 ULONGEST signature)
13332{
4ab09049
SM
13333 gdb_assert (!cu->per_cu->is_debug_types);
13334
13335 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13336}
13337
13338/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13339 See lookup_dwo_cutu_unit for details. */
13340
13341static struct dwo_unit *
4ab09049 13342lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13343{
4ab09049
SM
13344 gdb_assert (cu->per_cu->is_debug_types);
13345
13346 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13347
13348 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13349}
13350
89e63ee4
DE
13351/* Traversal function for queue_and_load_all_dwo_tus. */
13352
13353static int
13354queue_and_load_dwo_tu (void **slot, void *info)
13355{
13356 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13357 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13358 ULONGEST signature = dwo_unit->signature;
d460f660 13359 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13360
13361 if (sig_type != NULL)
13362 {
13363 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13364
13365 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13366 a real dependency of PER_CU on SIG_TYPE. That is detected later
13367 while processing PER_CU. */
120ce1b5 13368 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13369 load_full_type_unit (sig_cu, cu->per_objfile);
13370 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13371 }
13372
13373 return 1;
13374}
13375
1b555f17 13376/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13377 The DWO may have the only definition of the type, though it may not be
13378 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13379 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13380
13381static void
1b555f17 13382queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13383{
13384 struct dwo_unit *dwo_unit;
13385 struct dwo_file *dwo_file;
13386
1b555f17
SM
13387 gdb_assert (cu != nullptr);
13388 gdb_assert (!cu->per_cu->is_debug_types);
13389 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13390
1b555f17 13391 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13392 gdb_assert (dwo_unit != NULL);
13393
13394 dwo_file = dwo_unit->dwo_file;
13395 if (dwo_file->tus != NULL)
1b555f17 13396 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13397}
13398
3019eac3 13399/* Read in various DIEs. */
348e048f 13400
d389af10 13401/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13402 Inherit only the children of the DW_AT_abstract_origin DIE not being
13403 already referenced by DW_AT_abstract_origin from the children of the
13404 current DIE. */
d389af10
JK
13405
13406static void
13407inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13408{
13409 struct die_info *child_die;
791afaa2 13410 sect_offset *offsetp;
d389af10
JK
13411 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13412 struct die_info *origin_die;
13413 /* Iterator of the ORIGIN_DIE children. */
13414 struct die_info *origin_child_die;
d389af10 13415 struct attribute *attr;
cd02d79d
PA
13416 struct dwarf2_cu *origin_cu;
13417 struct pending **origin_previous_list_in_scope;
d389af10
JK
13418
13419 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13420 if (!attr)
13421 return;
13422
cd02d79d
PA
13423 /* Note that following die references may follow to a die in a
13424 different cu. */
13425
13426 origin_cu = cu;
13427 origin_die = follow_die_ref (die, attr, &origin_cu);
13428
13429 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13430 symbols in. */
13431 origin_previous_list_in_scope = origin_cu->list_in_scope;
13432 origin_cu->list_in_scope = cu->list_in_scope;
13433
edb3359d
DJ
13434 if (die->tag != origin_die->tag
13435 && !(die->tag == DW_TAG_inlined_subroutine
13436 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13437 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13438 sect_offset_str (die->sect_off),
13439 sect_offset_str (origin_die->sect_off));
d389af10 13440
791afaa2 13441 std::vector<sect_offset> offsets;
d389af10 13442
3ea89b92
PMR
13443 for (child_die = die->child;
13444 child_die && child_die->tag;
436c571c 13445 child_die = child_die->sibling)
3ea89b92
PMR
13446 {
13447 struct die_info *child_origin_die;
13448 struct dwarf2_cu *child_origin_cu;
13449
13450 /* We are trying to process concrete instance entries:
216f72a1 13451 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13452 it's not relevant to our analysis here. i.e. detecting DIEs that are
13453 present in the abstract instance but not referenced in the concrete
13454 one. */
216f72a1
JK
13455 if (child_die->tag == DW_TAG_call_site
13456 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13457 continue;
13458
c38f313d
DJ
13459 /* For each CHILD_DIE, find the corresponding child of
13460 ORIGIN_DIE. If there is more than one layer of
13461 DW_AT_abstract_origin, follow them all; there shouldn't be,
13462 but GCC versions at least through 4.4 generate this (GCC PR
13463 40573). */
3ea89b92
PMR
13464 child_origin_die = child_die;
13465 child_origin_cu = cu;
c38f313d
DJ
13466 while (1)
13467 {
cd02d79d
PA
13468 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13469 child_origin_cu);
c38f313d
DJ
13470 if (attr == NULL)
13471 break;
cd02d79d
PA
13472 child_origin_die = follow_die_ref (child_origin_die, attr,
13473 &child_origin_cu);
c38f313d
DJ
13474 }
13475
d389af10
JK
13476 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13477 counterpart may exist. */
c38f313d 13478 if (child_origin_die != child_die)
d389af10 13479 {
edb3359d
DJ
13480 if (child_die->tag != child_origin_die->tag
13481 && !(child_die->tag == DW_TAG_inlined_subroutine
13482 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13483 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13484 "different tags"),
9d8780f0
SM
13485 sect_offset_str (child_die->sect_off),
13486 sect_offset_str (child_origin_die->sect_off));
c38f313d 13487 if (child_origin_die->parent != origin_die)
b98664d3 13488 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13489 "different parents"),
9d8780f0
SM
13490 sect_offset_str (child_die->sect_off),
13491 sect_offset_str (child_origin_die->sect_off));
c38f313d 13492 else
791afaa2 13493 offsets.push_back (child_origin_die->sect_off);
d389af10 13494 }
d389af10 13495 }
791afaa2
TT
13496 std::sort (offsets.begin (), offsets.end ());
13497 sect_offset *offsets_end = offsets.data () + offsets.size ();
13498 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13499 if (offsetp[-1] == *offsetp)
b98664d3 13500 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13501 "to DIE %s as their abstract origin"),
13502 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13503
791afaa2 13504 offsetp = offsets.data ();
d389af10
JK
13505 origin_child_die = origin_die->child;
13506 while (origin_child_die && origin_child_die->tag)
13507 {
13508 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13509 while (offsetp < offsets_end
9c541725 13510 && *offsetp < origin_child_die->sect_off)
d389af10 13511 offsetp++;
b64f50a1 13512 if (offsetp >= offsets_end
9c541725 13513 || *offsetp > origin_child_die->sect_off)
d389af10 13514 {
adde2bff
DE
13515 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13516 Check whether we're already processing ORIGIN_CHILD_DIE.
13517 This can happen with mutually referenced abstract_origins.
13518 PR 16581. */
13519 if (!origin_child_die->in_process)
13520 process_die (origin_child_die, origin_cu);
d389af10 13521 }
436c571c 13522 origin_child_die = origin_child_die->sibling;
d389af10 13523 }
cd02d79d 13524 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13525
13526 if (cu != origin_cu)
13527 compute_delayed_physnames (origin_cu);
d389af10
JK
13528}
13529
c906108c 13530static void
e7c27a73 13531read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13532{
5e22e966 13533 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13534 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13535 struct context_stack *newobj;
c906108c
SS
13536 CORE_ADDR lowpc;
13537 CORE_ADDR highpc;
13538 struct die_info *child_die;
edb3359d 13539 struct attribute *attr, *call_line, *call_file;
15d034d0 13540 const char *name;
e142c38c 13541 CORE_ADDR baseaddr;
801e3a5b 13542 struct block *block;
edb3359d 13543 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13544 std::vector<struct symbol *> template_args;
34eaf542 13545 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13546
13547 if (inlined_func)
13548 {
13549 /* If we do not have call site information, we can't show the
13550 caller of this inlined function. That's too confusing, so
13551 only use the scope for local variables. */
13552 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13553 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13554 if (call_line == NULL || call_file == NULL)
13555 {
13556 read_lexical_block_scope (die, cu);
13557 return;
13558 }
13559 }
c906108c 13560
b3b3bada 13561 baseaddr = objfile->text_section_offset ();
e142c38c 13562
94af9270 13563 name = dwarf2_name (die, cu);
c906108c 13564
e8d05480
JB
13565 /* Ignore functions with missing or empty names. These are actually
13566 illegal according to the DWARF standard. */
13567 if (name == NULL)
13568 {
b98664d3 13569 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13570 sect_offset_str (die->sect_off));
e8d05480
JB
13571 return;
13572 }
13573
13574 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13575 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13576 <= PC_BOUNDS_INVALID)
e8d05480 13577 {
ae4d0c03 13578 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13579 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13580 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13581 "for subprogram DIE at %s"),
13582 sect_offset_str (die->sect_off));
e8d05480
JB
13583 return;
13584 }
c906108c 13585
3e29f34a
MR
13586 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13587 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13588
34eaf542
TT
13589 /* If we have any template arguments, then we must allocate a
13590 different sort of symbol. */
436c571c 13591 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13592 {
13593 if (child_die->tag == DW_TAG_template_type_param
13594 || child_die->tag == DW_TAG_template_value_param)
13595 {
8c14c3a3 13596 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13597 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13598 break;
13599 }
13600 }
13601
c24bdb02 13602 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13603 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13604 (struct symbol *) templ_func);
4c2df51b 13605
81873cc8 13606 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13607 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13608 cu->language);
13609
4cecd739
DJ
13610 /* If there is a location expression for DW_AT_frame_base, record
13611 it. */
e142c38c 13612 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13613 if (attr != nullptr)
fe978cb0 13614 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13615
63e43d3a
PMR
13616 /* If there is a location for the static link, record it. */
13617 newobj->static_link = NULL;
13618 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13619 if (attr != nullptr)
63e43d3a 13620 {
224c3ddb
SM
13621 newobj->static_link
13622 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13623 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13624 cu->addr_type ());
63e43d3a
PMR
13625 }
13626
c24bdb02 13627 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13628
639d11d3 13629 if (die->child != NULL)
c906108c 13630 {
639d11d3 13631 child_die = die->child;
c906108c
SS
13632 while (child_die && child_die->tag)
13633 {
34eaf542
TT
13634 if (child_die->tag == DW_TAG_template_type_param
13635 || child_die->tag == DW_TAG_template_value_param)
13636 {
13637 struct symbol *arg = new_symbol (child_die, NULL, cu);
13638
f1078f66 13639 if (arg != NULL)
2f4732b0 13640 template_args.push_back (arg);
34eaf542
TT
13641 }
13642 else
13643 process_die (child_die, cu);
436c571c 13644 child_die = child_die->sibling;
c906108c
SS
13645 }
13646 }
13647
d389af10
JK
13648 inherit_abstract_dies (die, cu);
13649
4a811a97
UW
13650 /* If we have a DW_AT_specification, we might need to import using
13651 directives from the context of the specification DIE. See the
13652 comment in determine_prefix. */
13653 if (cu->language == language_cplus
13654 && dwarf2_attr (die, DW_AT_specification, cu))
13655 {
13656 struct dwarf2_cu *spec_cu = cu;
13657 struct die_info *spec_die = die_specification (die, &spec_cu);
13658
13659 while (spec_die)
13660 {
13661 child_die = spec_die->child;
13662 while (child_die && child_die->tag)
13663 {
13664 if (child_die->tag == DW_TAG_imported_module)
13665 process_die (child_die, spec_cu);
436c571c 13666 child_die = child_die->sibling;
4a811a97
UW
13667 }
13668
13669 /* In some cases, GCC generates specification DIEs that
13670 themselves contain DW_AT_specification attributes. */
13671 spec_die = die_specification (spec_die, &spec_cu);
13672 }
13673 }
13674
c24bdb02 13675 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13676 /* Make a block for the local symbols within. */
c24bdb02 13677 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13678 cstk.static_link, lowpc, highpc);
801e3a5b 13679
df8a16a1 13680 /* For C++, set the block's scope. */
45280282
IB
13681 if ((cu->language == language_cplus
13682 || cu->language == language_fortran
c44af4eb
TT
13683 || cu->language == language_d
13684 || cu->language == language_rust)
4d4ec4e5 13685 && cu->processing_has_namespace_info)
195a3f6c
TT
13686 block_set_scope (block, determine_prefix (die, cu),
13687 &objfile->objfile_obstack);
df8a16a1 13688
801e3a5b
JB
13689 /* If we have address ranges, record them. */
13690 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13691
a60f3166 13692 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13693
34eaf542 13694 /* Attach template arguments to function. */
2f4732b0 13695 if (!template_args.empty ())
34eaf542
TT
13696 {
13697 gdb_assert (templ_func != NULL);
13698
2f4732b0 13699 templ_func->n_template_arguments = template_args.size ();
34eaf542 13700 templ_func->template_arguments
8d749320
SM
13701 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13702 templ_func->n_template_arguments);
34eaf542 13703 memcpy (templ_func->template_arguments,
2f4732b0 13704 template_args.data (),
34eaf542 13705 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13706
13707 /* Make sure that the symtab is set on the new symbols. Even
13708 though they don't appear in this symtab directly, other parts
13709 of gdb assume that symbols do, and this is reasonably
13710 true. */
8634679f 13711 for (symbol *sym : template_args)
3e1d3d8c 13712 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13713 }
13714
208d8187
JB
13715 /* In C++, we can have functions nested inside functions (e.g., when
13716 a function declares a class that has methods). This means that
13717 when we finish processing a function scope, we may need to go
13718 back to building a containing block's symbol lists. */
c24bdb02
KS
13719 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13720 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13721
921e78cf
JB
13722 /* If we've finished processing a top-level function, subsequent
13723 symbols go in the file symbol list. */
c24bdb02
KS
13724 if (cu->get_builder ()->outermost_context_p ())
13725 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13726}
13727
13728/* Process all the DIES contained within a lexical block scope. Start
13729 a new scope, process the dies, and then close the scope. */
13730
13731static void
e7c27a73 13732read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13733{
5e22e966 13734 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13735 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13736 CORE_ADDR lowpc, highpc;
13737 struct die_info *child_die;
e142c38c
DJ
13738 CORE_ADDR baseaddr;
13739
b3b3bada 13740 baseaddr = objfile->text_section_offset ();
c906108c
SS
13741
13742 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13743 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13744 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13745 be nasty. Might be easier to properly extend generic blocks to
af34e669 13746 describe ranges. */
e385593e
JK
13747 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13748 {
13749 case PC_BOUNDS_NOT_PRESENT:
13750 /* DW_TAG_lexical_block has no attributes, process its children as if
13751 there was no wrapping by that DW_TAG_lexical_block.
13752 GCC does no longer produces such DWARF since GCC r224161. */
13753 for (child_die = die->child;
13754 child_die != NULL && child_die->tag;
436c571c 13755 child_die = child_die->sibling)
4f7bc5ed
TT
13756 {
13757 /* We might already be processing this DIE. This can happen
13758 in an unusual circumstance -- where a subroutine A
13759 appears lexically in another subroutine B, but A actually
13760 inlines B. The recursion is broken here, rather than in
13761 inherit_abstract_dies, because it seems better to simply
13762 drop concrete children here. */
13763 if (!child_die->in_process)
13764 process_die (child_die, cu);
13765 }
e385593e
JK
13766 return;
13767 case PC_BOUNDS_INVALID:
13768 return;
13769 }
3e29f34a
MR
13770 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13771 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13772
c24bdb02 13773 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13774 if (die->child != NULL)
c906108c 13775 {
639d11d3 13776 child_die = die->child;
c906108c
SS
13777 while (child_die && child_die->tag)
13778 {
e7c27a73 13779 process_die (child_die, cu);
436c571c 13780 child_die = child_die->sibling;
c906108c
SS
13781 }
13782 }
3ea89b92 13783 inherit_abstract_dies (die, cu);
c24bdb02 13784 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13785
c24bdb02
KS
13786 if (*cu->get_builder ()->get_local_symbols () != NULL
13787 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13788 {
801e3a5b 13789 struct block *block
c24bdb02 13790 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13791 cstk.start_addr, highpc);
801e3a5b
JB
13792
13793 /* Note that recording ranges after traversing children, as we
13794 do here, means that recording a parent's ranges entails
13795 walking across all its children's ranges as they appear in
13796 the address map, which is quadratic behavior.
13797
13798 It would be nicer to record the parent's ranges before
13799 traversing its children, simply overriding whatever you find
13800 there. But since we don't even decide whether to create a
13801 block until after we've traversed its children, that's hard
13802 to do. */
13803 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13804 }
c24bdb02
KS
13805 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13806 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13807}
13808
216f72a1 13809/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13810
13811static void
13812read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13813{
5e22e966 13814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13815 struct objfile *objfile = per_objfile->objfile;
08feed99 13816 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13817 CORE_ADDR pc, baseaddr;
13818 struct attribute *attr;
13819 struct call_site *call_site, call_site_local;
13820 void **slot;
13821 int nparams;
13822 struct die_info *child_die;
13823
b3b3bada 13824 baseaddr = objfile->text_section_offset ();
96408a79 13825
216f72a1
JK
13826 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13827 if (attr == NULL)
13828 {
13829 /* This was a pre-DWARF-5 GNU extension alias
13830 for DW_AT_call_return_pc. */
13831 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13832 }
96408a79
SA
13833 if (!attr)
13834 {
b98664d3 13835 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13836 "DIE %s [in module %s]"),
13837 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13838 return;
13839 }
95f982e5 13840 pc = attr->as_address () + baseaddr;
3e29f34a 13841 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13842
13843 if (cu->call_site_htab == NULL)
13844 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13845 NULL, &objfile->objfile_obstack,
13846 hashtab_obstack_allocate, NULL);
13847 call_site_local.pc = pc;
13848 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13849 if (*slot != NULL)
13850 {
b98664d3 13851 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13852 "DIE %s [in module %s]"),
13853 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13854 objfile_name (objfile));
96408a79
SA
13855 return;
13856 }
13857
13858 /* Count parameters at the caller. */
13859
13860 nparams = 0;
13861 for (child_die = die->child; child_die && child_die->tag;
436c571c 13862 child_die = child_die->sibling)
96408a79 13863 {
216f72a1
JK
13864 if (child_die->tag != DW_TAG_call_site_parameter
13865 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13866 {
b98664d3 13867 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13868 "DW_TAG_call_site child DIE %s [in module %s]"),
13869 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13870 objfile_name (objfile));
96408a79
SA
13871 continue;
13872 }
13873
13874 nparams++;
13875 }
13876
224c3ddb
SM
13877 call_site
13878 = ((struct call_site *)
13879 obstack_alloc (&objfile->objfile_obstack,
13880 sizeof (*call_site)
13881 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13882 *slot = call_site;
13883 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13884 call_site->pc = pc;
13885
216f72a1
JK
13886 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13887 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13888 {
13889 struct die_info *func_die;
13890
13891 /* Skip also over DW_TAG_inlined_subroutine. */
13892 for (func_die = die->parent;
13893 func_die && func_die->tag != DW_TAG_subprogram
13894 && func_die->tag != DW_TAG_subroutine_type;
13895 func_die = func_die->parent);
13896
216f72a1
JK
13897 /* DW_AT_call_all_calls is a superset
13898 of DW_AT_call_all_tail_calls. */
96408a79 13899 if (func_die
216f72a1 13900 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13901 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13902 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13903 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13904 {
13905 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13906 not complete. But keep CALL_SITE for look ups via call_site_htab,
13907 both the initial caller containing the real return address PC and
13908 the final callee containing the current PC of a chain of tail
13909 calls do not need to have the tail call list complete. But any
13910 function candidate for a virtual tail call frame searched via
13911 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13912 determined unambiguously. */
13913 }
13914 else
13915 {
13916 struct type *func_type = NULL;
13917
13918 if (func_die)
13919 func_type = get_die_type (func_die, cu);
13920 if (func_type != NULL)
13921 {
78134374 13922 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13923
13924 /* Enlist this call site to the function. */
13925 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13926 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13927 }
13928 else
b98664d3 13929 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13930 "DIE %s [in module %s]"),
13931 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13932 }
13933 }
13934
216f72a1
JK
13935 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13936 if (attr == NULL)
13937 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13938 if (attr == NULL)
13939 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13940 if (attr == NULL)
216f72a1
JK
13941 {
13942 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13943 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13944 }
96408a79 13945 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13946 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13947 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13948 else if (attr->form_is_block ())
96408a79
SA
13949 {
13950 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13951 struct dwarf_block *block = attr->as_block ();
96408a79 13952
8d749320 13953 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13954 dlbaton->data = block->data;
13955 dlbaton->size = block->size;
a50264ba 13956 dlbaton->per_objfile = per_objfile;
96408a79
SA
13957 dlbaton->per_cu = cu->per_cu;
13958
13959 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13960 }
cd6c91b4 13961 else if (attr->form_is_ref ())
96408a79 13962 {
96408a79
SA
13963 struct dwarf2_cu *target_cu = cu;
13964 struct die_info *target_die;
13965
ac9ec31b 13966 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13967 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13968 if (die_is_declaration (target_die, target_cu))
13969 {
7d45c7c3 13970 const char *target_physname;
9112db09
JK
13971
13972 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13973 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13974 if (target_physname == NULL)
9112db09 13975 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13976 if (target_physname == NULL)
b98664d3 13977 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13978 "physname, for referencing DIE %s [in module %s]"),
13979 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13980 else
7d455152 13981 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13982 }
13983 else
13984 {
13985 CORE_ADDR lowpc;
13986
13987 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13988 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13989 <= PC_BOUNDS_INVALID)
b98664d3 13990 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13991 "low pc, for referencing DIE %s [in module %s]"),
13992 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13993 else
3e29f34a
MR
13994 {
13995 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13996 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13997 }
96408a79
SA
13998 }
13999 }
14000 else
b98664d3 14001 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14002 "block nor reference, for DIE %s [in module %s]"),
14003 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14004
14005 call_site->per_cu = cu->per_cu;
9f47c707 14006 call_site->per_objfile = per_objfile;
96408a79
SA
14007
14008 for (child_die = die->child;
14009 child_die && child_die->tag;
436c571c 14010 child_die = child_die->sibling)
96408a79 14011 {
96408a79 14012 struct call_site_parameter *parameter;
1788b2d3 14013 struct attribute *loc, *origin;
96408a79 14014
216f72a1
JK
14015 if (child_die->tag != DW_TAG_call_site_parameter
14016 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14017 {
14018 /* Already printed the complaint above. */
14019 continue;
14020 }
14021
14022 gdb_assert (call_site->parameter_count < nparams);
14023 parameter = &call_site->parameter[call_site->parameter_count];
14024
1788b2d3
JK
14025 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14026 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14027 register is contained in DW_AT_call_value. */
96408a79 14028
24c5c679 14029 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14030 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14031 if (origin == NULL)
14032 {
14033 /* This was a pre-DWARF-5 GNU extension alias
14034 for DW_AT_call_parameter. */
14035 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14036 }
cd6c91b4 14037 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14038 {
1788b2d3 14039 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14040
0826b30a 14041 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14042 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14043 {
14044 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14045 binding can be done only inside one CU. Such referenced DIE
14046 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14047 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14048 "DW_TAG_call_site child DIE %s [in module %s]"),
14049 sect_offset_str (child_die->sect_off),
9c541725 14050 objfile_name (objfile));
d76b7dbc
JK
14051 continue;
14052 }
9c541725
PA
14053 parameter->u.param_cu_off
14054 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14055 }
4fc6c0d5 14056 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14057 {
b98664d3 14058 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14059 "DW_TAG_call_site child DIE %s [in module %s]"),
14060 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14061 continue;
14062 }
24c5c679 14063 else
96408a79 14064 {
9d2246fc
TT
14065 struct dwarf_block *block = loc->as_block ();
14066
24c5c679 14067 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14068 (block->data, &block->data[block->size]);
24c5c679
JK
14069 if (parameter->u.dwarf_reg != -1)
14070 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14071 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14072 &block->data[block->size],
24c5c679
JK
14073 &parameter->u.fb_offset))
14074 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14075 else
14076 {
b98664d3 14077 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14078 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14079 "DW_TAG_call_site child DIE %s "
24c5c679 14080 "[in module %s]"),
9d8780f0 14081 sect_offset_str (child_die->sect_off),
9c541725 14082 objfile_name (objfile));
24c5c679
JK
14083 continue;
14084 }
96408a79
SA
14085 }
14086
216f72a1
JK
14087 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14088 if (attr == NULL)
14089 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14090 if (attr == NULL || !attr->form_is_block ())
96408a79 14091 {
b98664d3 14092 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14093 "DW_TAG_call_site child DIE %s [in module %s]"),
14094 sect_offset_str (child_die->sect_off),
9c541725 14095 objfile_name (objfile));
96408a79
SA
14096 continue;
14097 }
9d2246fc
TT
14098
14099 struct dwarf_block *block = attr->as_block ();
14100 parameter->value = block->data;
14101 parameter->value_size = block->size;
96408a79
SA
14102
14103 /* Parameters are not pre-cleared by memset above. */
14104 parameter->data_value = NULL;
14105 parameter->data_value_size = 0;
14106 call_site->parameter_count++;
14107
216f72a1
JK
14108 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14109 if (attr == NULL)
14110 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14111 if (attr != nullptr)
96408a79 14112 {
4fc6c0d5 14113 if (!attr->form_is_block ())
b98664d3 14114 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14115 "DW_TAG_call_site child DIE %s [in module %s]"),
14116 sect_offset_str (child_die->sect_off),
9c541725 14117 objfile_name (objfile));
96408a79
SA
14118 else
14119 {
9d2246fc
TT
14120 block = attr->as_block ();
14121 parameter->data_value = block->data;
14122 parameter->data_value_size = block->size;
96408a79
SA
14123 }
14124 }
14125 }
14126}
14127
71a3c369
TT
14128/* Helper function for read_variable. If DIE represents a virtual
14129 table, then return the type of the concrete object that is
14130 associated with the virtual table. Otherwise, return NULL. */
14131
14132static struct type *
14133rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14134{
14135 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14136 if (attr == NULL)
14137 return NULL;
14138
14139 /* Find the type DIE. */
14140 struct die_info *type_die = NULL;
14141 struct dwarf2_cu *type_cu = cu;
14142
cd6c91b4 14143 if (attr->form_is_ref ())
71a3c369
TT
14144 type_die = follow_die_ref (die, attr, &type_cu);
14145 if (type_die == NULL)
14146 return NULL;
14147
14148 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14149 return NULL;
14150 return die_containing_type (type_die, type_cu);
14151}
14152
14153/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14154
14155static void
14156read_variable (struct die_info *die, struct dwarf2_cu *cu)
14157{
14158 struct rust_vtable_symbol *storage = NULL;
14159
14160 if (cu->language == language_rust)
14161 {
14162 struct type *containing_type = rust_containing_type (die, cu);
14163
14164 if (containing_type != NULL)
14165 {
5e22e966 14166 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14167
8c14c3a3 14168 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14169 storage->concrete_type = containing_type;
cf724bc9 14170 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14171 }
14172 }
14173
e4a62c65
TV
14174 struct symbol *res = new_symbol (die, NULL, cu, storage);
14175 struct attribute *abstract_origin
14176 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14177 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14178 if (res == NULL && loc && abstract_origin)
14179 {
14180 /* We have a variable without a name, but with a location and an abstract
14181 origin. This may be a concrete instance of an abstract variable
14182 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14183 later. */
14184 struct dwarf2_cu *origin_cu = cu;
14185 struct die_info *origin_die
14186 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14187 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14188 per_objfile->per_bfd->abstract_to_concrete
14189 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14190 }
71a3c369
TT
14191}
14192
43988095
JK
14193/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14194 reading .debug_rnglists.
14195 Callback's type should be:
14196 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14197 Return true if the attributes are present and valid, otherwise,
14198 return false. */
14199
14200template <typename Callback>
14201static bool
14202dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14203 dwarf_tag tag, Callback &&callback)
43988095 14204{
976ca316
SM
14205 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14206 struct objfile *objfile = per_objfile->objfile;
43988095 14207 bfd *obfd = objfile->obfd;
43988095 14208 /* Base address selection entry. */
2b24b6e4 14209 gdb::optional<CORE_ADDR> base;
43988095 14210 const gdb_byte *buffer;
43988095
JK
14211 CORE_ADDR baseaddr;
14212 bool overflow = false;
d0ce17d8
CT
14213 ULONGEST addr_index;
14214 struct dwarf2_section_info *rnglists_section;
43988095 14215
43988095 14216 base = cu->base_address;
d0ce17d8
CT
14217 rnglists_section = cu_debug_rnglists_section (cu, tag);
14218 rnglists_section->read (objfile);
43988095 14219
d0ce17d8 14220 if (offset >= rnglists_section->size)
43988095 14221 {
b98664d3 14222 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14223 offset);
14224 return false;
14225 }
d0ce17d8 14226 buffer = rnglists_section->buffer + offset;
43988095 14227
b3b3bada 14228 baseaddr = objfile->text_section_offset ();
43988095
JK
14229
14230 while (1)
14231 {
7814882a
JK
14232 /* Initialize it due to a false compiler warning. */
14233 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14234 const gdb_byte *buf_end = (rnglists_section->buffer
14235 + rnglists_section->size);
43988095
JK
14236 unsigned int bytes_read;
14237
14238 if (buffer == buf_end)
14239 {
14240 overflow = true;
14241 break;
14242 }
14243 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14244 switch (rlet)
14245 {
14246 case DW_RLE_end_of_list:
14247 break;
14248 case DW_RLE_base_address:
14249 if (buffer + cu->header.addr_size > buf_end)
14250 {
14251 overflow = true;
14252 break;
14253 }
c8a7a66f 14254 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14255 buffer += bytes_read;
14256 break;
d0ce17d8
CT
14257 case DW_RLE_base_addressx:
14258 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14259 buffer += bytes_read;
14260 base = read_addr_index (cu, addr_index);
14261 break;
43988095
JK
14262 case DW_RLE_start_length:
14263 if (buffer + cu->header.addr_size > buf_end)
14264 {
14265 overflow = true;
14266 break;
14267 }
c8a7a66f
TT
14268 range_beginning = cu->header.read_address (obfd, buffer,
14269 &bytes_read);
43988095
JK
14270 buffer += bytes_read;
14271 range_end = (range_beginning
14272 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14273 buffer += bytes_read;
14274 if (buffer > buf_end)
14275 {
14276 overflow = true;
14277 break;
14278 }
14279 break;
d0ce17d8
CT
14280 case DW_RLE_startx_length:
14281 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14282 buffer += bytes_read;
14283 range_beginning = read_addr_index (cu, addr_index);
14284 if (buffer > buf_end)
14285 {
14286 overflow = true;
14287 break;
14288 }
14289 range_end = (range_beginning
14290 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14291 buffer += bytes_read;
14292 break;
43988095
JK
14293 case DW_RLE_offset_pair:
14294 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14295 buffer += bytes_read;
14296 if (buffer > buf_end)
14297 {
14298 overflow = true;
14299 break;
14300 }
14301 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14302 buffer += bytes_read;
14303 if (buffer > buf_end)
14304 {
14305 overflow = true;
14306 break;
14307 }
14308 break;
14309 case DW_RLE_start_end:
14310 if (buffer + 2 * cu->header.addr_size > buf_end)
14311 {
14312 overflow = true;
14313 break;
14314 }
c8a7a66f
TT
14315 range_beginning = cu->header.read_address (obfd, buffer,
14316 &bytes_read);
43988095 14317 buffer += bytes_read;
c8a7a66f 14318 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14319 buffer += bytes_read;
14320 break;
d0ce17d8
CT
14321 case DW_RLE_startx_endx:
14322 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14323 buffer += bytes_read;
14324 range_beginning = read_addr_index (cu, addr_index);
14325 if (buffer > buf_end)
14326 {
14327 overflow = true;
14328 break;
14329 }
14330 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14331 buffer += bytes_read;
14332 range_end = read_addr_index (cu, addr_index);
14333 break;
43988095 14334 default:
b98664d3 14335 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14336 return false;
14337 }
14338 if (rlet == DW_RLE_end_of_list || overflow)
14339 break;
14340 if (rlet == DW_RLE_base_address)
14341 continue;
14342
43988095
JK
14343 if (range_beginning > range_end)
14344 {
14345 /* Inverted range entries are invalid. */
b98664d3 14346 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14347 return false;
14348 }
14349
14350 /* Empty range entries have no effect. */
14351 if (range_beginning == range_end)
14352 continue;
14353
d0ce17d8
CT
14354 /* Only DW_RLE_offset_pair needs the base address added. */
14355 if (rlet == DW_RLE_offset_pair)
14356 {
14357 if (!base.has_value ())
14358 {
14359 /* We have no valid base address for the DW_RLE_offset_pair. */
14360 complaint (_("Invalid .debug_rnglists data (no base address for "
14361 "DW_RLE_offset_pair)"));
14362 return false;
14363 }
14364
14365 range_beginning += *base;
14366 range_end += *base;
14367 }
43988095
JK
14368
14369 /* A not-uncommon case of bad debug info.
14370 Don't pollute the addrmap with bad data. */
14371 if (range_beginning + baseaddr == 0
976ca316 14372 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14373 {
b98664d3 14374 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14375 " [in module %s]"), objfile_name (objfile));
14376 continue;
14377 }
14378
14379 callback (range_beginning, range_end);
14380 }
14381
14382 if (overflow)
14383 {
b98664d3 14384 complaint (_("Offset %d is not terminated "
43988095
JK
14385 "for DW_AT_ranges attribute"),
14386 offset);
14387 return false;
14388 }
14389
14390 return true;
14391}
14392
14393/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14394 Callback's type should be:
14395 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14396 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14397
43988095 14398template <typename Callback>
43039443 14399static int
d0ce17d8 14400dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14401 Callback &&callback)
43039443 14402{
5e22e966
SM
14403 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14404 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14405 struct comp_unit_head *cu_header = &cu->header;
14406 bfd *obfd = objfile->obfd;
14407 unsigned int addr_size = cu_header->addr_size;
14408 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14409 /* Base address selection entry. */
2b24b6e4 14410 gdb::optional<CORE_ADDR> base;
43039443 14411 unsigned int dummy;
d521ce57 14412 const gdb_byte *buffer;
ff013f42 14413 CORE_ADDR baseaddr;
43039443 14414
43988095 14415 if (cu_header->version >= 5)
d0ce17d8 14416 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14417
d00adf39 14418 base = cu->base_address;
43039443 14419
5e22e966
SM
14420 per_objfile->per_bfd->ranges.read (objfile);
14421 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14422 {
b98664d3 14423 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14424 offset);
14425 return 0;
14426 }
5e22e966 14427 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14428
b3b3bada 14429 baseaddr = objfile->text_section_offset ();
ff013f42 14430
43039443
JK
14431 while (1)
14432 {
14433 CORE_ADDR range_beginning, range_end;
14434
c8a7a66f 14435 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14436 buffer += addr_size;
c8a7a66f 14437 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14438 buffer += addr_size;
14439 offset += 2 * addr_size;
14440
14441 /* An end of list marker is a pair of zero addresses. */
14442 if (range_beginning == 0 && range_end == 0)
14443 /* Found the end of list entry. */
14444 break;
14445
14446 /* Each base address selection entry is a pair of 2 values.
14447 The first is the largest possible address, the second is
14448 the base address. Check for a base address here. */
14449 if ((range_beginning & mask) == mask)
14450 {
28d2bfb9
AB
14451 /* If we found the largest possible address, then we already
14452 have the base address in range_end. */
14453 base = range_end;
43039443
JK
14454 continue;
14455 }
14456
2b24b6e4 14457 if (!base.has_value ())
43039443
JK
14458 {
14459 /* We have no valid base address for the ranges
14460 data. */
b98664d3 14461 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14462 return 0;
14463 }
14464
9277c30c
UW
14465 if (range_beginning > range_end)
14466 {
14467 /* Inverted range entries are invalid. */
b98664d3 14468 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14469 return 0;
14470 }
14471
14472 /* Empty range entries have no effect. */
14473 if (range_beginning == range_end)
14474 continue;
14475
2b24b6e4
TT
14476 range_beginning += *base;
14477 range_end += *base;
43039443 14478
01093045
DE
14479 /* A not-uncommon case of bad debug info.
14480 Don't pollute the addrmap with bad data. */
14481 if (range_beginning + baseaddr == 0
5e22e966 14482 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14483 {
b98664d3 14484 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14485 " [in module %s]"), objfile_name (objfile));
01093045
DE
14486 continue;
14487 }
14488
5f46c5a5
JK
14489 callback (range_beginning, range_end);
14490 }
14491
14492 return 1;
14493}
14494
14495/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14496 Return 1 if the attributes are present and valid, otherwise, return 0.
14497 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14498
14499static int
14500dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14501 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14502 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14503{
5e22e966 14504 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14505 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14506 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14507 int low_set = 0;
14508 CORE_ADDR low = 0;
14509 CORE_ADDR high = 0;
14510 int retval;
14511
d0ce17d8 14512 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14513 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14514 {
9277c30c 14515 if (ranges_pst != NULL)
3e29f34a
MR
14516 {
14517 CORE_ADDR lowpc;
14518 CORE_ADDR highpc;
14519
79748972
TT
14520 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14521 range_beginning + baseaddr)
14522 - baseaddr);
14523 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14524 range_end + baseaddr)
14525 - baseaddr);
d320c2b5
TT
14526 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14527 lowpc, highpc - 1, ranges_pst);
3e29f34a 14528 }
ff013f42 14529
43039443
JK
14530 /* FIXME: This is recording everything as a low-high
14531 segment of consecutive addresses. We should have a
14532 data structure for discontiguous block ranges
14533 instead. */
14534 if (! low_set)
14535 {
14536 low = range_beginning;
14537 high = range_end;
14538 low_set = 1;
14539 }
14540 else
14541 {
14542 if (range_beginning < low)
14543 low = range_beginning;
14544 if (range_end > high)
14545 high = range_end;
14546 }
5f46c5a5
JK
14547 });
14548 if (!retval)
14549 return 0;
43039443
JK
14550
14551 if (! low_set)
14552 /* If the first entry is an end-of-list marker, the range
14553 describes an empty scope, i.e. no instructions. */
14554 return 0;
14555
14556 if (low_return)
14557 *low_return = low;
14558 if (high_return)
14559 *high_return = high;
14560 return 1;
14561}
14562
3a2b436a
JK
14563/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14564 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14565 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14566
3a2b436a 14567static enum pc_bounds_kind
af34e669 14568dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14569 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14570 dwarf2_psymtab *pst)
c906108c 14571{
976ca316 14572 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14573 struct attribute *attr;
91da1414 14574 struct attribute *attr_high;
af34e669
DJ
14575 CORE_ADDR low = 0;
14576 CORE_ADDR high = 0;
e385593e 14577 enum pc_bounds_kind ret;
c906108c 14578
91da1414
MW
14579 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14580 if (attr_high)
af34e669 14581 {
e142c38c 14582 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14583 if (attr != nullptr)
91da1414 14584 {
95f982e5
TT
14585 low = attr->as_address ();
14586 high = attr_high->as_address ();
cd6c91b4 14587 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14588 high += low;
91da1414 14589 }
af34e669
DJ
14590 else
14591 /* Found high w/o low attribute. */
e385593e 14592 return PC_BOUNDS_INVALID;
af34e669
DJ
14593
14594 /* Found consecutive range of addresses. */
3a2b436a 14595 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14596 }
c906108c 14597 else
af34e669 14598 {
e142c38c 14599 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14600 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14601 {
18a8505e 14602 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14603 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14604 in DW_TAG_compile_unit of DWO files.
14605
14606 Attributes of the form DW_FORM_rnglistx have already had their
14607 value changed by read_rnglist_index and already include
14608 DW_AT_rnglists_base, so don't need to add the ranges base,
14609 either. */
14610 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14611 && attr->form != DW_FORM_rnglistx);
529908cb 14612 unsigned int ranges_offset = (attr->as_unsigned ()
ab435259
DE
14613 + (need_ranges_base
14614 ? cu->ranges_base
14615 : 0));
2e3cf129 14616
af34e669 14617 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14618 .debug_ranges section. */
d0ce17d8
CT
14619 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14620 die->tag))
e385593e 14621 return PC_BOUNDS_INVALID;
43039443 14622 /* Found discontinuous range of addresses. */
3a2b436a 14623 ret = PC_BOUNDS_RANGES;
af34e669 14624 }
e385593e
JK
14625 else
14626 return PC_BOUNDS_NOT_PRESENT;
af34e669 14627 }
c906108c 14628
48fbe735 14629 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14630 if (high <= low)
e385593e 14631 return PC_BOUNDS_INVALID;
c906108c
SS
14632
14633 /* When using the GNU linker, .gnu.linkonce. sections are used to
14634 eliminate duplicate copies of functions and vtables and such.
14635 The linker will arbitrarily choose one and discard the others.
14636 The AT_*_pc values for such functions refer to local labels in
14637 these sections. If the section from that file was discarded, the
14638 labels are not in the output, so the relocs get a value of 0.
14639 If this is a discarded function, mark the pc bounds as invalid,
14640 so that GDB will ignore it. */
976ca316 14641 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14642 return PC_BOUNDS_INVALID;
c906108c
SS
14643
14644 *lowpc = low;
96408a79
SA
14645 if (highpc)
14646 *highpc = high;
af34e669 14647 return ret;
c906108c
SS
14648}
14649
b084d499
JB
14650/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14651 its low and high PC addresses. Do nothing if these addresses could not
14652 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14653 and HIGHPC to the high address if greater than HIGHPC. */
14654
14655static void
14656dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14657 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14658 struct dwarf2_cu *cu)
14659{
14660 CORE_ADDR low, high;
14661 struct die_info *child = die->child;
14662
e385593e 14663 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14664 {
325fac50
PA
14665 *lowpc = std::min (*lowpc, low);
14666 *highpc = std::max (*highpc, high);
b084d499
JB
14667 }
14668
14669 /* If the language does not allow nested subprograms (either inside
14670 subprograms or lexical blocks), we're done. */
14671 if (cu->language != language_ada)
14672 return;
6e70227d 14673
b084d499
JB
14674 /* Check all the children of the given DIE. If it contains nested
14675 subprograms, then check their pc bounds. Likewise, we need to
14676 check lexical blocks as well, as they may also contain subprogram
14677 definitions. */
14678 while (child && child->tag)
14679 {
14680 if (child->tag == DW_TAG_subprogram
14681 || child->tag == DW_TAG_lexical_block)
14682 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14683 child = child->sibling;
b084d499
JB
14684 }
14685}
14686
fae299cd
DC
14687/* Get the low and high pc's represented by the scope DIE, and store
14688 them in *LOWPC and *HIGHPC. If the correct values can't be
14689 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14690
14691static void
14692get_scope_pc_bounds (struct die_info *die,
14693 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14694 struct dwarf2_cu *cu)
14695{
14696 CORE_ADDR best_low = (CORE_ADDR) -1;
14697 CORE_ADDR best_high = (CORE_ADDR) 0;
14698 CORE_ADDR current_low, current_high;
14699
3a2b436a 14700 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14701 >= PC_BOUNDS_RANGES)
fae299cd
DC
14702 {
14703 best_low = current_low;
14704 best_high = current_high;
14705 }
14706 else
14707 {
14708 struct die_info *child = die->child;
14709
14710 while (child && child->tag)
14711 {
14712 switch (child->tag) {
14713 case DW_TAG_subprogram:
b084d499 14714 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14715 break;
14716 case DW_TAG_namespace:
f55ee35c 14717 case DW_TAG_module:
fae299cd
DC
14718 /* FIXME: carlton/2004-01-16: Should we do this for
14719 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14720 that current GCC's always emit the DIEs corresponding
14721 to definitions of methods of classes as children of a
14722 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14723 the DIEs giving the declarations, which could be
14724 anywhere). But I don't see any reason why the
14725 standards says that they have to be there. */
14726 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14727
14728 if (current_low != ((CORE_ADDR) -1))
14729 {
325fac50
PA
14730 best_low = std::min (best_low, current_low);
14731 best_high = std::max (best_high, current_high);
fae299cd
DC
14732 }
14733 break;
14734 default:
0963b4bd 14735 /* Ignore. */
fae299cd
DC
14736 break;
14737 }
14738
436c571c 14739 child = child->sibling;
fae299cd
DC
14740 }
14741 }
14742
14743 *lowpc = best_low;
14744 *highpc = best_high;
14745}
14746
801e3a5b
JB
14747/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14748 in DIE. */
380bca97 14749
801e3a5b
JB
14750static void
14751dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14752 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14753{
5e22e966 14754 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14755 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14756 struct attribute *attr;
91da1414 14757 struct attribute *attr_high;
801e3a5b 14758
91da1414
MW
14759 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14760 if (attr_high)
801e3a5b 14761 {
801e3a5b 14762 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14763 if (attr != nullptr)
801e3a5b 14764 {
95f982e5
TT
14765 CORE_ADDR low = attr->as_address ();
14766 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14767
cd6c91b4 14768 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14769 high += low;
9a619af0 14770
3e29f34a
MR
14771 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14772 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14773 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14774 }
14775 }
14776
14777 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14778 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14779 {
18a8505e 14780 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14781 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14782 in DW_TAG_compile_unit of DWO files.
14783
14784 Attributes of the form DW_FORM_rnglistx have already had their
14785 value changed by read_rnglist_index and already include
14786 DW_AT_rnglists_base, so don't need to add the ranges base,
14787 either. */
14788 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14789 && attr->form != DW_FORM_rnglistx);
801e3a5b
JB
14790
14791 /* The value of the DW_AT_ranges attribute is the offset of the
14792 address range list in the .debug_ranges section. */
529908cb 14793 unsigned long offset = (attr->as_unsigned ()
ab435259 14794 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14795
2d5f09ec 14796 std::vector<blockrange> blockvec;
d0ce17d8 14797 dwarf2_ranges_process (offset, cu, die->tag,
5f46c5a5
JK
14798 [&] (CORE_ADDR start, CORE_ADDR end)
14799 {
58fdfd2c
JK
14800 start += baseaddr;
14801 end += baseaddr;
5f46c5a5
JK
14802 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14803 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14804 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14805 blockvec.emplace_back (start, end);
5f46c5a5 14806 });
2d5f09ec
KB
14807
14808 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14809 }
14810}
14811
685b1105
JK
14812/* Check whether the producer field indicates either of GCC < 4.6, or the
14813 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14814
685b1105
JK
14815static void
14816check_producer (struct dwarf2_cu *cu)
60d5a603 14817{
38360086 14818 int major, minor;
60d5a603
JK
14819
14820 if (cu->producer == NULL)
14821 {
14822 /* For unknown compilers expect their behavior is DWARF version
14823 compliant.
14824
14825 GCC started to support .debug_types sections by -gdwarf-4 since
14826 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14827 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14828 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14829 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14830 }
b1ffba5a 14831 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14832 {
38360086
MW
14833 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14834 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14835 }
5230b05a 14836 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14837 {
14838 cu->producer_is_icc = true;
14839 cu->producer_is_icc_lt_14 = major < 14;
14840 }
c258c396
JD
14841 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14842 cu->producer_is_codewarrior = true;
685b1105
JK
14843 else
14844 {
14845 /* For other non-GCC compilers, expect their behavior is DWARF version
14846 compliant. */
60d5a603
JK
14847 }
14848
9068261f 14849 cu->checked_producer = true;
685b1105 14850}
ba919b58 14851
685b1105
JK
14852/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14853 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14854 during 4.6.0 experimental. */
14855
9068261f 14856static bool
685b1105
JK
14857producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14858{
14859 if (!cu->checked_producer)
14860 check_producer (cu);
14861
14862 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14863}
14864
c258c396
JD
14865
14866/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14867 with incorrect is_stmt attributes. */
14868
14869static bool
14870producer_is_codewarrior (struct dwarf2_cu *cu)
14871{
14872 if (!cu->checked_producer)
14873 check_producer (cu);
14874
14875 return cu->producer_is_codewarrior;
14876}
14877
bf23a268
TT
14878/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14879 If that attribute is not available, return the appropriate
14880 default. */
60d5a603
JK
14881
14882static enum dwarf_access_attribute
bf23a268 14883dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14884{
bf23a268
TT
14885 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14886 if (attr != nullptr)
14887 {
14888 LONGEST value = attr->constant_value (-1);
14889 if (value == DW_ACCESS_public
14890 || value == DW_ACCESS_protected
14891 || value == DW_ACCESS_private)
14892 return (dwarf_access_attribute) value;
14893 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14894 plongest (value));
14895 }
14896
60d5a603
JK
14897 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14898 {
14899 /* The default DWARF 2 accessibility for members is public, the default
14900 accessibility for inheritance is private. */
14901
14902 if (die->tag != DW_TAG_inheritance)
14903 return DW_ACCESS_public;
14904 else
14905 return DW_ACCESS_private;
14906 }
14907 else
14908 {
14909 /* DWARF 3+ defines the default accessibility a different way. The same
14910 rules apply now for DW_TAG_inheritance as for the members and it only
14911 depends on the container kind. */
14912
14913 if (die->parent->tag == DW_TAG_class_type)
14914 return DW_ACCESS_private;
14915 else
14916 return DW_ACCESS_public;
14917 }
14918}
14919
74ac6d43
TT
14920/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14921 offset. If the attribute was not found return 0, otherwise return
14922 1. If it was found but could not properly be handled, set *OFFSET
14923 to 0. */
14924
14925static int
14926handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14927 LONGEST *offset)
14928{
14929 struct attribute *attr;
14930
14931 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14932 if (attr != NULL)
14933 {
14934 *offset = 0;
14935
14936 /* Note that we do not check for a section offset first here.
14937 This is because DW_AT_data_member_location is new in DWARF 4,
14938 so if we see it, we can assume that a constant form is really
14939 a constant and not a section offset. */
cd6c91b4 14940 if (attr->form_is_constant ())
0826b30a 14941 *offset = attr->constant_value (0);
cd6c91b4 14942 else if (attr->form_is_section_offset ())
74ac6d43 14943 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14944 else if (attr->form_is_block ())
9d2246fc 14945 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14946 else
14947 dwarf2_complex_location_expr_complaint ();
14948
14949 return 1;
14950 }
14951
14952 return 0;
14953}
14954
7d79de9a
TT
14955/* Look for DW_AT_data_member_location and store the results in FIELD. */
14956
14957static void
14958handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14959 struct field *field)
14960{
14961 struct attribute *attr;
14962
14963 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14964 if (attr != NULL)
14965 {
14966 if (attr->form_is_constant ())
14967 {
14968 LONGEST offset = attr->constant_value (0);
14969 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14970 }
14971 else if (attr->form_is_section_offset ())
14972 dwarf2_complex_location_expr_complaint ();
14973 else if (attr->form_is_block ())
14974 {
14975 bool handled;
9d2246fc 14976 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14977 if (handled)
14978 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14979 else
14980 {
5e22e966
SM
14981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14982 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14983 struct dwarf2_locexpr_baton *dlbaton
14984 = XOBNEW (&objfile->objfile_obstack,
14985 struct dwarf2_locexpr_baton);
9d2246fc
TT
14986 dlbaton->data = attr->as_block ()->data;
14987 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14988 /* When using this baton, we want to compute the address
14989 of the field, not the value. This is why
14990 is_reference is set to false here. */
14991 dlbaton->is_reference = false;
5e22e966 14992 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14993 dlbaton->per_cu = cu->per_cu;
14994
14995 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14996 }
14997 }
14998 else
14999 dwarf2_complex_location_expr_complaint ();
15000 }
15001}
15002
c906108c
SS
15003/* Add an aggregate field to the field list. */
15004
15005static void
107d2387 15006dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15007 struct dwarf2_cu *cu)
6e70227d 15008{
5e22e966 15009 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15010 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15011 struct nextfield *new_field;
15012 struct attribute *attr;
15013 struct field *fp;
15d034d0 15014 const char *fieldname = "";
c906108c 15015
7d0ccb61
DJ
15016 if (die->tag == DW_TAG_inheritance)
15017 {
be2daae6
TT
15018 fip->baseclasses.emplace_back ();
15019 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15020 }
15021 else
15022 {
be2daae6
TT
15023 fip->fields.emplace_back ();
15024 new_field = &fip->fields.back ();
7d0ccb61 15025 }
be2daae6 15026
9c6a1327
TT
15027 new_field->offset = die->sect_off;
15028
bf23a268 15029 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15030 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15031 fip->non_public_fields = true;
60d5a603 15032
e142c38c 15033 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15034 if (attr != nullptr)
23dca5c3 15035 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15036 else
15037 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15038
15039 fp = &new_field->field;
a9a9bd0f 15040
e142c38c 15041 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15042 {
a9a9bd0f 15043 /* Data member other than a C++ static data member. */
6e70227d 15044
c906108c 15045 /* Get type of field. */
5d14b6e5 15046 fp->set_type (die_type (die, cu));
c906108c 15047
d6a843b5 15048 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15049
c906108c 15050 /* Get bit size of field (zero if none). */
e142c38c 15051 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15052 if (attr != nullptr)
c906108c 15053 {
529908cb 15054 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15055 }
15056 else
15057 {
15058 FIELD_BITSIZE (*fp) = 0;
15059 }
15060
15061 /* Get bit offset of field. */
7d79de9a 15062 handle_data_member_location (die, cu, fp);
e142c38c 15063 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15064 if (attr != nullptr && attr->form_is_constant ())
c906108c 15065 {
d5a22e77 15066 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15067 {
15068 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15069 additional bit offset from the MSB of the containing
15070 anonymous object to the MSB of the field. We don't
15071 have to do anything special since we don't need to
15072 know the size of the anonymous object. */
529908cb 15073 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15074 + attr->constant_value (0)));
c906108c
SS
15075 }
15076 else
15077 {
15078 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15079 MSB of the anonymous object, subtract off the number of
15080 bits from the MSB of the field to the MSB of the
15081 object, and then subtract off the number of bits of
15082 the field itself. The result is the bit offset of
15083 the LSB of the field. */
c906108c 15084 int anonymous_size;
7c184d33 15085 int bit_offset = attr->constant_value (0);
c906108c 15086
e142c38c 15087 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15088 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15089 {
15090 /* The size of the anonymous object containing
15091 the bit field is explicit, so use the
15092 indicated size (in bytes). */
7c184d33 15093 anonymous_size = attr->constant_value (0);
c906108c
SS
15094 }
15095 else
15096 {
15097 /* The size of the anonymous object containing
15098 the bit field must be inferred from the type
15099 attribute of the data member containing the
15100 bit field. */
5d14b6e5 15101 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15102 }
f41f5e61
PA
15103 SET_FIELD_BITPOS (*fp,
15104 (FIELD_BITPOS (*fp)
15105 + anonymous_size * bits_per_byte
15106 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15107 }
15108 }
da5b30da
AA
15109 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15110 if (attr != NULL)
15111 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15112 + attr->constant_value (0)));
c906108c
SS
15113
15114 /* Get name of field. */
39cbfefa
DJ
15115 fieldname = dwarf2_name (die, cu);
15116 if (fieldname == NULL)
15117 fieldname = "";
d8151005
DJ
15118
15119 /* The name is already allocated along with this objfile, so we don't
15120 need to duplicate it for the type. */
15121 fp->name = fieldname;
c906108c
SS
15122
15123 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15124 pointer or virtual base class pointer) to private. */
e142c38c 15125 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15126 {
d48cc9dd 15127 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15128 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15129 fip->non_public_fields = true;
c906108c
SS
15130 }
15131 }
a9a9bd0f 15132 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15133 {
a9a9bd0f
DC
15134 /* C++ static member. */
15135
15136 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15137 is a declaration, but all versions of G++ as of this writing
15138 (so through at least 3.2.1) incorrectly generate
15139 DW_TAG_variable tags. */
6e70227d 15140
ff355380 15141 const char *physname;
c906108c 15142
a9a9bd0f 15143 /* Get name of field. */
39cbfefa
DJ
15144 fieldname = dwarf2_name (die, cu);
15145 if (fieldname == NULL)
c906108c
SS
15146 return;
15147
254e6b9e 15148 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15149 if (attr
15150 /* Only create a symbol if this is an external value.
15151 new_symbol checks this and puts the value in the global symbol
15152 table, which we want. If it is not external, new_symbol
15153 will try to put the value in cu->list_in_scope which is wrong. */
15154 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15155 {
15156 /* A static const member, not much different than an enum as far as
15157 we're concerned, except that we can support more types. */
15158 new_symbol (die, NULL, cu);
15159 }
15160
2df3850c 15161 /* Get physical name. */
ff355380 15162 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15163
d8151005
DJ
15164 /* The name is already allocated along with this objfile, so we don't
15165 need to duplicate it for the type. */
15166 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15167 fp->set_type (die_type (die, cu));
d8151005 15168 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15169 }
15170 else if (die->tag == DW_TAG_inheritance)
15171 {
74ac6d43 15172 /* C++ base class field. */
7d79de9a 15173 handle_data_member_location (die, cu, fp);
c906108c 15174 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15175 fp->set_type (die_type (die, cu));
15176 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15177 }
2ddeaf8a
TT
15178 else
15179 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15180}
15181
883fd55a
KS
15182/* Can the type given by DIE define another type? */
15183
15184static bool
15185type_can_define_types (const struct die_info *die)
15186{
15187 switch (die->tag)
15188 {
15189 case DW_TAG_typedef:
15190 case DW_TAG_class_type:
15191 case DW_TAG_structure_type:
15192 case DW_TAG_union_type:
15193 case DW_TAG_enumeration_type:
15194 return true;
15195
15196 default:
15197 return false;
15198 }
15199}
15200
15201/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15202
15203static void
883fd55a
KS
15204dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15205 struct dwarf2_cu *cu)
6e70227d 15206{
be2daae6
TT
15207 struct decl_field fp;
15208 memset (&fp, 0, sizeof (fp));
98751a41 15209
883fd55a 15210 gdb_assert (type_can_define_types (die));
98751a41 15211
883fd55a 15212 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15213 fp.name = dwarf2_name (die, cu);
15214 fp.type = read_type_die (die, cu);
98751a41 15215
c191a687 15216 /* Save accessibility. */
bf23a268 15217 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15218 switch (accessibility)
15219 {
15220 case DW_ACCESS_public:
15221 /* The assumed value if neither private nor protected. */
15222 break;
15223 case DW_ACCESS_private:
be2daae6 15224 fp.is_private = 1;
c191a687
KS
15225 break;
15226 case DW_ACCESS_protected:
be2daae6 15227 fp.is_protected = 1;
c191a687 15228 break;
c191a687
KS
15229 }
15230
883fd55a 15231 if (die->tag == DW_TAG_typedef)
be2daae6 15232 fip->typedef_field_list.push_back (fp);
883fd55a 15233 else
be2daae6 15234 fip->nested_types_list.push_back (fp);
98751a41
JK
15235}
15236
9c6a1327
TT
15237/* A convenience typedef that's used when finding the discriminant
15238 field for a variant part. */
1b95cdb7
SM
15239typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15240 offset_map_type;
9c6a1327
TT
15241
15242/* Compute the discriminant range for a given variant. OBSTACK is
15243 where the results will be stored. VARIANT is the variant to
15244 process. IS_UNSIGNED indicates whether the discriminant is signed
15245 or unsigned. */
15246
15247static const gdb::array_view<discriminant_range>
15248convert_variant_range (struct obstack *obstack, const variant_field &variant,
15249 bool is_unsigned)
15250{
15251 std::vector<discriminant_range> ranges;
15252
15253 if (variant.default_branch)
15254 return {};
15255
15256 if (variant.discr_list_data == nullptr)
15257 {
15258 discriminant_range r
15259 = {variant.discriminant_value, variant.discriminant_value};
15260 ranges.push_back (r);
15261 }
15262 else
15263 {
15264 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15265 variant.discr_list_data->size);
15266 while (!data.empty ())
15267 {
15268 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15269 {
15270 complaint (_("invalid discriminant marker: %d"), data[0]);
15271 break;
15272 }
15273 bool is_range = data[0] == DW_DSC_range;
15274 data = data.slice (1);
15275
15276 ULONGEST low, high;
15277 unsigned int bytes_read;
15278
15279 if (data.empty ())
15280 {
15281 complaint (_("DW_AT_discr_list missing low value"));
15282 break;
15283 }
15284 if (is_unsigned)
15285 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15286 else
15287 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15288 &bytes_read);
15289 data = data.slice (bytes_read);
15290
15291 if (is_range)
15292 {
15293 if (data.empty ())
15294 {
15295 complaint (_("DW_AT_discr_list missing high value"));
15296 break;
15297 }
15298 if (is_unsigned)
15299 high = read_unsigned_leb128 (nullptr, data.data (),
15300 &bytes_read);
15301 else
15302 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15303 &bytes_read);
15304 data = data.slice (bytes_read);
15305 }
15306 else
15307 high = low;
15308
15309 ranges.push_back ({ low, high });
15310 }
15311 }
15312
15313 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15314 ranges.size ());
15315 std::copy (ranges.begin (), ranges.end (), result);
15316 return gdb::array_view<discriminant_range> (result, ranges.size ());
15317}
15318
15319static const gdb::array_view<variant_part> create_variant_parts
15320 (struct obstack *obstack,
15321 const offset_map_type &offset_map,
15322 struct field_info *fi,
15323 const std::vector<variant_part_builder> &variant_parts);
15324
15325/* Fill in a "struct variant" for a given variant field. RESULT is
15326 the variant to fill in. OBSTACK is where any needed allocations
15327 will be done. OFFSET_MAP holds the mapping from section offsets to
15328 fields for the type. FI describes the fields of the type we're
15329 processing. FIELD is the variant field we're converting. */
15330
15331static void
15332create_one_variant (variant &result, struct obstack *obstack,
15333 const offset_map_type &offset_map,
15334 struct field_info *fi, const variant_field &field)
15335{
15336 result.discriminants = convert_variant_range (obstack, field, false);
15337 result.first_field = field.first_field + fi->baseclasses.size ();
15338 result.last_field = field.last_field + fi->baseclasses.size ();
15339 result.parts = create_variant_parts (obstack, offset_map, fi,
15340 field.variant_parts);
15341}
15342
15343/* Fill in a "struct variant_part" for a given variant part. RESULT
15344 is the variant part to fill in. OBSTACK is where any needed
15345 allocations will be done. OFFSET_MAP holds the mapping from
15346 section offsets to fields for the type. FI describes the fields of
15347 the type we're processing. BUILDER is the variant part to be
15348 converted. */
15349
15350static void
15351create_one_variant_part (variant_part &result,
15352 struct obstack *obstack,
15353 const offset_map_type &offset_map,
15354 struct field_info *fi,
15355 const variant_part_builder &builder)
15356{
15357 auto iter = offset_map.find (builder.discriminant_offset);
15358 if (iter == offset_map.end ())
15359 {
15360 result.discriminant_index = -1;
15361 /* Doesn't matter. */
15362 result.is_unsigned = false;
15363 }
15364 else
15365 {
15366 result.discriminant_index = iter->second;
15367 result.is_unsigned
c6d940a9 15368 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15369 }
15370
15371 size_t n = builder.variants.size ();
15372 variant *output = new (obstack) variant[n];
15373 for (size_t i = 0; i < n; ++i)
15374 create_one_variant (output[i], obstack, offset_map, fi,
15375 builder.variants[i]);
15376
15377 result.variants = gdb::array_view<variant> (output, n);
15378}
15379
15380/* Create a vector of variant parts that can be attached to a type.
15381 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15382 holds the mapping from section offsets to fields for the type. FI
15383 describes the fields of the type we're processing. VARIANT_PARTS
15384 is the vector to convert. */
15385
15386static const gdb::array_view<variant_part>
15387create_variant_parts (struct obstack *obstack,
15388 const offset_map_type &offset_map,
15389 struct field_info *fi,
15390 const std::vector<variant_part_builder> &variant_parts)
15391{
15392 if (variant_parts.empty ())
15393 return {};
15394
15395 size_t n = variant_parts.size ();
15396 variant_part *result = new (obstack) variant_part[n];
15397 for (size_t i = 0; i < n; ++i)
15398 create_one_variant_part (result[i], obstack, offset_map, fi,
15399 variant_parts[i]);
15400
15401 return gdb::array_view<variant_part> (result, n);
15402}
15403
15404/* Compute the variant part vector for FIP, attaching it to TYPE when
15405 done. */
15406
15407static void
15408add_variant_property (struct field_info *fip, struct type *type,
15409 struct dwarf2_cu *cu)
15410{
15411 /* Map section offsets of fields to their field index. Note the
15412 field index here does not take the number of baseclasses into
15413 account. */
15414 offset_map_type offset_map;
15415 for (int i = 0; i < fip->fields.size (); ++i)
15416 offset_map[fip->fields[i].offset] = i;
15417
5e22e966 15418 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15419 gdb::array_view<variant_part> parts
15420 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15421 fip->variant_parts);
15422
15423 struct dynamic_prop prop;
8c2e4e06
SM
15424 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15425 obstack_copy (&objfile->objfile_obstack, &parts,
15426 sizeof (parts)));
9c6a1327 15427
5c54719c 15428 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15429}
15430
c906108c
SS
15431/* Create the vector of fields, and attach it to the type. */
15432
15433static void
fba45db2 15434dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15435 struct dwarf2_cu *cu)
c906108c 15436{
317f7127 15437 int nfields = fip->nfields ();
c906108c
SS
15438
15439 /* Record the field count, allocate space for the array of fields,
15440 and create blank accessibility bitfields if necessary. */
5e33d5f4 15441 type->set_num_fields (nfields);
3cabb6b0
SM
15442 type->set_fields
15443 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15444
b4ba55a1 15445 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15446 {
15447 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15448
15449 TYPE_FIELD_PRIVATE_BITS (type) =
15450 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15451 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15452
15453 TYPE_FIELD_PROTECTED_BITS (type) =
15454 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15455 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15456
774b6a14
TT
15457 TYPE_FIELD_IGNORE_BITS (type) =
15458 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15459 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15460 }
15461
15462 /* If the type has baseclasses, allocate and clear a bit vector for
15463 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15464 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15465 {
be2daae6 15466 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15467 unsigned char *pointer;
c906108c
SS
15468
15469 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15470 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15471 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15472 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15473 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15474 }
15475
9c6a1327
TT
15476 if (!fip->variant_parts.empty ())
15477 add_variant_property (fip, type, cu);
2ddeaf8a 15478
be2daae6
TT
15479 /* Copy the saved-up fields into the field vector. */
15480 for (int i = 0; i < nfields; ++i)
c906108c 15481 {
be2daae6
TT
15482 struct nextfield &field
15483 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15484 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15485
ceacbf6e 15486 type->field (i) = field.field;
be2daae6 15487 switch (field.accessibility)
c906108c 15488 {
c5aa993b 15489 case DW_ACCESS_private:
b4ba55a1 15490 if (cu->language != language_ada)
be2daae6 15491 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15492 break;
c906108c 15493
c5aa993b 15494 case DW_ACCESS_protected:
b4ba55a1 15495 if (cu->language != language_ada)
be2daae6 15496 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15497 break;
c906108c 15498
c5aa993b
JM
15499 case DW_ACCESS_public:
15500 break;
c906108c 15501
c5aa993b
JM
15502 default:
15503 /* Unknown accessibility. Complain and treat it as public. */
15504 {
b98664d3 15505 complaint (_("unsupported accessibility %d"),
be2daae6 15506 field.accessibility);
c5aa993b
JM
15507 }
15508 break;
c906108c 15509 }
be2daae6 15510 if (i < fip->baseclasses.size ())
c906108c 15511 {
be2daae6 15512 switch (field.virtuality)
c906108c 15513 {
c5aa993b
JM
15514 case DW_VIRTUALITY_virtual:
15515 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15516 if (cu->language == language_ada)
a73c6dcd 15517 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15518 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15519 break;
c906108c
SS
15520 }
15521 }
c906108c
SS
15522 }
15523}
15524
7d27a96d
TT
15525/* Return true if this member function is a constructor, false
15526 otherwise. */
15527
15528static int
15529dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15530{
15531 const char *fieldname;
fe978cb0 15532 const char *type_name;
7d27a96d
TT
15533 int len;
15534
15535 if (die->parent == NULL)
15536 return 0;
15537
15538 if (die->parent->tag != DW_TAG_structure_type
15539 && die->parent->tag != DW_TAG_union_type
15540 && die->parent->tag != DW_TAG_class_type)
15541 return 0;
15542
15543 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15544 type_name = dwarf2_name (die->parent, cu);
15545 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15546 return 0;
15547
15548 len = strlen (fieldname);
fe978cb0
PA
15549 return (strncmp (fieldname, type_name, len) == 0
15550 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15551}
15552
c906108c
SS
15553/* Add a member function to the proper fieldlist. */
15554
15555static void
107d2387 15556dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15557 struct type *type, struct dwarf2_cu *cu)
c906108c 15558{
5e22e966 15559 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15560 struct attribute *attr;
c906108c 15561 int i;
be2daae6 15562 struct fnfieldlist *flp = nullptr;
c906108c 15563 struct fn_field *fnp;
15d034d0 15564 const char *fieldname;
f792889a 15565 struct type *this_type;
c906108c 15566
b4ba55a1 15567 if (cu->language == language_ada)
a73c6dcd 15568 error (_("unexpected member function in Ada type"));
b4ba55a1 15569
2df3850c 15570 /* Get name of member function. */
39cbfefa
DJ
15571 fieldname = dwarf2_name (die, cu);
15572 if (fieldname == NULL)
2df3850c 15573 return;
c906108c 15574
c906108c 15575 /* Look up member function name in fieldlist. */
be2daae6 15576 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15577 {
27bfe10e 15578 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15579 {
15580 flp = &fip->fnfieldlists[i];
15581 break;
15582 }
c906108c
SS
15583 }
15584
be2daae6
TT
15585 /* Create a new fnfieldlist if necessary. */
15586 if (flp == nullptr)
c906108c 15587 {
be2daae6
TT
15588 fip->fnfieldlists.emplace_back ();
15589 flp = &fip->fnfieldlists.back ();
c906108c 15590 flp->name = fieldname;
be2daae6 15591 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15592 }
15593
be2daae6
TT
15594 /* Create a new member function field and add it to the vector of
15595 fnfieldlists. */
15596 flp->fnfields.emplace_back ();
15597 fnp = &flp->fnfields.back ();
3da10d80
KS
15598
15599 /* Delay processing of the physname until later. */
9c37b5ae 15600 if (cu->language == language_cplus)
be2daae6
TT
15601 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15602 die, cu);
3da10d80
KS
15603 else
15604 {
1d06ead6 15605 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15606 fnp->physname = physname ? physname : "";
15607 }
15608
c906108c 15609 fnp->type = alloc_type (objfile);
f792889a 15610 this_type = read_type_die (die, cu);
78134374 15611 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15612 {
1f704f76 15613 int nparams = this_type->num_fields ();
c906108c 15614
f792889a 15615 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15616 of the method itself (TYPE_CODE_METHOD). */
15617 smash_to_method_type (fnp->type, type,
f792889a 15618 TYPE_TARGET_TYPE (this_type),
80fc5e77 15619 this_type->fields (),
1f704f76 15620 this_type->num_fields (),
a409645d 15621 this_type->has_varargs ());
c906108c
SS
15622
15623 /* Handle static member functions.
c5aa993b 15624 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15625 member functions. G++ helps GDB by marking the first
15626 parameter for non-static member functions (which is the this
15627 pointer) as artificial. We obtain this information from
15628 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15629 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15630 fnp->voffset = VOFFSET_STATIC;
15631 }
15632 else
b98664d3 15633 complaint (_("member function type missing for '%s'"),
3da10d80 15634 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15635
15636 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15637 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15638 fnp->fcontext = die_containing_type (die, cu);
c906108c 15639
3e43a32a
MS
15640 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15641 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15642
15643 /* Get accessibility. */
bf23a268 15644 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15645 switch (accessibility)
c906108c 15646 {
60d5a603
JK
15647 case DW_ACCESS_private:
15648 fnp->is_private = 1;
15649 break;
15650 case DW_ACCESS_protected:
15651 fnp->is_protected = 1;
15652 break;
c906108c
SS
15653 }
15654
b02dede2 15655 /* Check for artificial methods. */
e142c38c 15656 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15657 if (attr && attr->as_boolean ())
b02dede2
DJ
15658 fnp->is_artificial = 1;
15659
e35000a7
TBA
15660 /* Check for defaulted methods. */
15661 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15662 if (attr != nullptr)
15663 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15664
15665 /* Check for deleted methods. */
15666 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15667 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15668 fnp->is_deleted = 1;
15669
7d27a96d
TT
15670 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15671
0d564a31 15672 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15673 function. For older versions of GCC, this is an offset in the
15674 appropriate virtual table, as specified by DW_AT_containing_type.
15675 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15676 to the object address. */
15677
e142c38c 15678 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15679 if (attr != nullptr)
8e19ed76 15680 {
9d2246fc 15681 if (attr->form_is_block () && attr->as_block ()->size > 0)
8e19ed76 15682 {
9d2246fc
TT
15683 struct dwarf_block *block = attr->as_block ();
15684
15685 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15686 {
15687 /* Old-style GCC. */
9d2246fc 15688 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15689 }
9d2246fc
TT
15690 else if (block->data[0] == DW_OP_deref
15691 || (block->size > 1
15692 && block->data[0] == DW_OP_deref_size
15693 && block->data[1] == cu->header.addr_size))
aec5aa8b 15694 {
9d2246fc 15695 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15696 if ((fnp->voffset % cu->header.addr_size) != 0)
15697 dwarf2_complex_location_expr_complaint ();
15698 else
15699 fnp->voffset /= cu->header.addr_size;
15700 fnp->voffset += 2;
15701 }
15702 else
15703 dwarf2_complex_location_expr_complaint ();
15704
15705 if (!fnp->fcontext)
7e993ebf
KS
15706 {
15707 /* If there is no `this' field and no DW_AT_containing_type,
15708 we cannot actually find a base class context for the
15709 vtable! */
1f704f76 15710 if (this_type->num_fields () == 0
7e993ebf
KS
15711 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15712 {
b98664d3 15713 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15714 "function \"%s\" (offset %s)"),
15715 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15716 }
15717 else
15718 {
15719 fnp->fcontext
940da03e 15720 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15721 }
15722 }
aec5aa8b 15723 }
cd6c91b4 15724 else if (attr->form_is_section_offset ())
8e19ed76 15725 {
4d3c2250 15726 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15727 }
15728 else
15729 {
4d3c2250
KB
15730 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15731 fieldname);
8e19ed76 15732 }
0d564a31 15733 }
d48cc9dd
DJ
15734 else
15735 {
15736 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15737 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15738 {
15739 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15740 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15741 "but the vtable offset is not specified"),
9d8780f0 15742 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15743 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15744 TYPE_CPLUS_DYNAMIC (type) = 1;
15745 }
15746 }
c906108c
SS
15747}
15748
15749/* Create the vector of member function fields, and attach it to the type. */
15750
15751static void
fba45db2 15752dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15753 struct dwarf2_cu *cu)
c906108c 15754{
b4ba55a1 15755 if (cu->language == language_ada)
a73c6dcd 15756 error (_("unexpected member functions in Ada type"));
b4ba55a1 15757
c906108c
SS
15758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15759 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15760 TYPE_ALLOC (type,
15761 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15762
be2daae6 15763 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15764 {
be2daae6 15765 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15766 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15767
be2daae6
TT
15768 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15769 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15770 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15771 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15772
15773 for (int k = 0; k < nf.fnfields.size (); ++k)
15774 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15775 }
15776
be2daae6 15777 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15778}
15779
1168df01
JB
15780/* Returns non-zero if NAME is the name of a vtable member in CU's
15781 language, zero otherwise. */
15782static int
15783is_vtable_name (const char *name, struct dwarf2_cu *cu)
15784{
15785 static const char vptr[] = "_vptr";
15786
9c37b5ae
TT
15787 /* Look for the C++ form of the vtable. */
15788 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15789 return 1;
15790
15791 return 0;
15792}
15793
c0dd20ea 15794/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15795 functions, with the ABI-specified layout. If TYPE describes
15796 such a structure, smash it into a member function type.
61049d3b
DJ
15797
15798 GCC shouldn't do this; it should just output pointer to member DIEs.
15799 This is GCC PR debug/28767. */
c0dd20ea 15800
0b92b5bb
TT
15801static void
15802quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15803{
09e2d7c7 15804 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15805
15806 /* Check for a structure with no name and two children. */
1f704f76 15807 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15808 return;
c0dd20ea
DJ
15809
15810 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15811 if (TYPE_FIELD_NAME (type, 0) == NULL
15812 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15813 || TYPE_FIELD_NAME (type, 1) == NULL
15814 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15815 return;
c0dd20ea
DJ
15816
15817 /* Find the type of the method. */
940da03e 15818 pfn_type = type->field (0).type ();
c0dd20ea 15819 if (pfn_type == NULL
78134374
SM
15820 || pfn_type->code () != TYPE_CODE_PTR
15821 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15822 return;
c0dd20ea
DJ
15823
15824 /* Look for the "this" argument. */
15825 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15826 if (pfn_type->num_fields () == 0
940da03e
SM
15827 /* || pfn_type->field (0).type () == NULL */
15828 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15829 return;
c0dd20ea 15830
940da03e 15831 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15832 new_type = alloc_type (objfile);
09e2d7c7 15833 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15834 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15835 pfn_type->has_varargs ());
0b92b5bb 15836 smash_to_methodptr_type (type, new_type);
c0dd20ea 15837}
1168df01 15838
2b4424c3
TT
15839/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15840 appropriate error checking and issuing complaints if there is a
15841 problem. */
15842
15843static ULONGEST
15844get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15845{
15846 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15847
15848 if (attr == nullptr)
15849 return 0;
15850
cd6c91b4 15851 if (!attr->form_is_constant ())
2b4424c3 15852 {
b98664d3 15853 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15854 " - DIE at %s [in module %s]"),
15855 sect_offset_str (die->sect_off),
5e22e966 15856 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15857 return 0;
15858 }
15859
529908cb
TT
15860 LONGEST val = attr->constant_value (0);
15861 if (val < 0)
2b4424c3 15862 {
529908cb
TT
15863 complaint (_("DW_AT_alignment value must not be negative"
15864 " - DIE at %s [in module %s]"),
15865 sect_offset_str (die->sect_off),
15866 objfile_name (cu->per_objfile->objfile));
15867 return 0;
2b4424c3 15868 }
529908cb 15869 ULONGEST align = val;
2b4424c3
TT
15870
15871 if (align == 0)
15872 {
b98664d3 15873 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15874 " - DIE at %s [in module %s]"),
15875 sect_offset_str (die->sect_off),
5e22e966 15876 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15877 return 0;
15878 }
15879 if ((align & (align - 1)) != 0)
15880 {
b98664d3 15881 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15882 " - DIE at %s [in module %s]"),
15883 sect_offset_str (die->sect_off),
5e22e966 15884 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15885 return 0;
15886 }
15887
15888 return align;
15889}
15890
15891/* If the DIE has a DW_AT_alignment attribute, use its value to set
15892 the alignment for TYPE. */
15893
15894static void
15895maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15896 struct type *type)
15897{
15898 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15899 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15900 " - DIE at %s [in module %s]"),
15901 sect_offset_str (die->sect_off),
5e22e966 15902 objfile_name (cu->per_objfile->objfile));
2b4424c3 15903}
685b1105 15904
e35000a7
TBA
15905/* Check if the given VALUE is a valid enum dwarf_calling_convention
15906 constant for a type, according to DWARF5 spec, Table 5.5. */
15907
15908static bool
15909is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15910{
15911 switch (value)
15912 {
15913 case DW_CC_normal:
15914 case DW_CC_pass_by_reference:
15915 case DW_CC_pass_by_value:
15916 return true;
15917
15918 default:
15919 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15920 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15921 return false;
15922 }
15923}
15924
d0922fcf
TBA
15925/* Check if the given VALUE is a valid enum dwarf_calling_convention
15926 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15927 also according to GNU-specific values (see include/dwarf2.h). */
15928
15929static bool
15930is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15931{
15932 switch (value)
15933 {
15934 case DW_CC_normal:
15935 case DW_CC_program:
15936 case DW_CC_nocall:
15937 return true;
15938
15939 case DW_CC_GNU_renesas_sh:
15940 case DW_CC_GNU_borland_fastcall_i386:
15941 case DW_CC_GDB_IBM_OpenCL:
15942 return true;
15943
15944 default:
15945 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15946 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15947 return false;
15948 }
15949}
15950
c906108c 15951/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15952 (definition) to create a type for the structure or union. Fill in
15953 the type's name and general properties; the members will not be
83655187
DE
15954 processed until process_structure_scope. A symbol table entry for
15955 the type will also not be done until process_structure_scope (assuming
15956 the type has a name).
c906108c 15957
c767944b
DJ
15958 NOTE: we need to call these functions regardless of whether or not the
15959 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15960 structure or union. This gets the type entered into our set of
83655187 15961 user defined types. */
c906108c 15962
f792889a 15963static struct type *
134d01f1 15964read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15965{
5e22e966 15966 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15967 struct type *type;
15968 struct attribute *attr;
15d034d0 15969 const char *name;
c906108c 15970
348e048f
DE
15971 /* If the definition of this type lives in .debug_types, read that type.
15972 Don't follow DW_AT_specification though, that will take us back up
15973 the chain and we want to go down. */
052c8bb8 15974 attr = die->attr (DW_AT_signature);
435d3d88 15975 if (attr != nullptr)
348e048f 15976 {
ac9ec31b 15977 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15978
ac9ec31b 15979 /* The type's CU may not be the same as CU.
02142a6c 15980 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15981 return set_die_type (die, type, cu);
15982 }
15983
c0dd20ea 15984 type = alloc_type (objfile);
c906108c 15985 INIT_CPLUS_SPECIFIC (type);
93311388 15986
39cbfefa
DJ
15987 name = dwarf2_name (die, cu);
15988 if (name != NULL)
c906108c 15989 {
987504bb 15990 if (cu->language == language_cplus
c44af4eb
TT
15991 || cu->language == language_d
15992 || cu->language == language_rust)
63d06c5c 15993 {
15d034d0 15994 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15995
15996 /* dwarf2_full_name might have already finished building the DIE's
15997 type. If so, there is no need to continue. */
15998 if (get_die_type (die, cu) != NULL)
15999 return get_die_type (die, cu);
16000
d0e39ea2 16001 type->set_name (full_name);
63d06c5c
DC
16002 }
16003 else
16004 {
d8151005
DJ
16005 /* The name is already allocated along with this objfile, so
16006 we don't need to duplicate it for the type. */
d0e39ea2 16007 type->set_name (name);
63d06c5c 16008 }
c906108c
SS
16009 }
16010
16011 if (die->tag == DW_TAG_structure_type)
16012 {
67607e24 16013 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16014 }
16015 else if (die->tag == DW_TAG_union_type)
16016 {
67607e24 16017 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16018 }
16019 else
16020 {
67607e24 16021 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16022 }
16023
0cc2414c
TT
16024 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16025 TYPE_DECLARED_CLASS (type) = 1;
16026
e35000a7
TBA
16027 /* Store the calling convention in the type if it's available in
16028 the die. Otherwise the calling convention remains set to
16029 the default value DW_CC_normal. */
16030 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16031 if (attr != nullptr
529908cb 16032 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16033 {
16034 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16035 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16036 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16037 }
16038
e142c38c 16039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16040 if (attr != nullptr)
c906108c 16041 {
cd6c91b4 16042 if (attr->form_is_constant ())
529908cb 16043 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16044 else
16045 {
f8e89861 16046 struct dynamic_prop prop;
293e7e51 16047 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16048 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
16049 TYPE_LENGTH (type) = 0;
16050 }
c906108c
SS
16051 }
16052 else
16053 {
16054 TYPE_LENGTH (type) = 0;
16055 }
16056
2b4424c3
TT
16057 maybe_set_alignment (cu, die, type);
16058
5230b05a 16059 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16060 {
5230b05a
WT
16061 /* ICC<14 does not output the required DW_AT_declaration on
16062 incomplete types, but gives them a size of zero. */
b4b73759 16063 type->set_is_stub (true);
685b1105
JK
16064 }
16065 else
9baccff6 16066 type->set_stub_is_supported (true);
685b1105 16067
dc718098 16068 if (die_is_declaration (die, cu))
b4b73759 16069 type->set_is_stub (true);
a6c727b2
DJ
16070 else if (attr == NULL && die->child == NULL
16071 && producer_is_realview (cu->producer))
16072 /* RealView does not output the required DW_AT_declaration
16073 on incomplete types. */
b4b73759 16074 type->set_is_stub (true);
dc718098 16075
c906108c
SS
16076 /* We need to add the type field to the die immediately so we don't
16077 infinitely recurse when dealing with pointers to the structure
0963b4bd 16078 type within the structure itself. */
1c379e20 16079 set_die_type (die, type, cu);
c906108c 16080
7e314c57
JK
16081 /* set_die_type should be already done. */
16082 set_descriptive_type (type, die, cu);
16083
c767944b
DJ
16084 return type;
16085}
16086
9c6a1327
TT
16087static void handle_struct_member_die
16088 (struct die_info *child_die,
16089 struct type *type,
16090 struct field_info *fi,
16091 std::vector<struct symbol *> *template_args,
16092 struct dwarf2_cu *cu);
16093
16094/* A helper for handle_struct_member_die that handles
16095 DW_TAG_variant_part. */
16096
16097static void
16098handle_variant_part (struct die_info *die, struct type *type,
16099 struct field_info *fi,
16100 std::vector<struct symbol *> *template_args,
16101 struct dwarf2_cu *cu)
16102{
16103 variant_part_builder *new_part;
16104 if (fi->current_variant_part == nullptr)
16105 {
16106 fi->variant_parts.emplace_back ();
16107 new_part = &fi->variant_parts.back ();
16108 }
16109 else if (!fi->current_variant_part->processing_variant)
16110 {
16111 complaint (_("nested DW_TAG_variant_part seen "
16112 "- DIE at %s [in module %s]"),
16113 sect_offset_str (die->sect_off),
5e22e966 16114 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16115 return;
16116 }
16117 else
16118 {
16119 variant_field &current = fi->current_variant_part->variants.back ();
16120 current.variant_parts.emplace_back ();
16121 new_part = &current.variant_parts.back ();
16122 }
16123
16124 /* When we recurse, we want callees to add to this new variant
16125 part. */
16126 scoped_restore save_current_variant_part
16127 = make_scoped_restore (&fi->current_variant_part, new_part);
16128
16129 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16130 if (discr == NULL)
16131 {
16132 /* It's a univariant form, an extension we support. */
16133 }
16134 else if (discr->form_is_ref ())
16135 {
16136 struct dwarf2_cu *target_cu = cu;
16137 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16138
16139 new_part->discriminant_offset = target_die->sect_off;
16140 }
16141 else
16142 {
16143 complaint (_("DW_AT_discr does not have DIE reference form"
16144 " - DIE at %s [in module %s]"),
16145 sect_offset_str (die->sect_off),
5e22e966 16146 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16147 }
16148
16149 for (die_info *child_die = die->child;
16150 child_die != NULL;
16151 child_die = child_die->sibling)
16152 handle_struct_member_die (child_die, type, fi, template_args, cu);
16153}
16154
16155/* A helper for handle_struct_member_die that handles
16156 DW_TAG_variant. */
16157
16158static void
16159handle_variant (struct die_info *die, struct type *type,
16160 struct field_info *fi,
16161 std::vector<struct symbol *> *template_args,
16162 struct dwarf2_cu *cu)
16163{
16164 if (fi->current_variant_part == nullptr)
16165 {
16166 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16167 "- DIE at %s [in module %s]"),
16168 sect_offset_str (die->sect_off),
5e22e966 16169 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16170 return;
16171 }
16172 if (fi->current_variant_part->processing_variant)
16173 {
16174 complaint (_("nested DW_TAG_variant seen "
16175 "- DIE at %s [in module %s]"),
16176 sect_offset_str (die->sect_off),
5e22e966 16177 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16178 return;
16179 }
16180
16181 scoped_restore save_processing_variant
16182 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16183 true);
16184
16185 fi->current_variant_part->variants.emplace_back ();
16186 variant_field &variant = fi->current_variant_part->variants.back ();
16187 variant.first_field = fi->fields.size ();
16188
16189 /* In a variant we want to get the discriminant and also add a
16190 field for our sole member child. */
16191 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16192 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16193 {
16194 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16195 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16196 variant.default_branch = true;
16197 else
9d2246fc 16198 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16199 }
16200 else
cae21f8e 16201 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16202
16203 for (die_info *variant_child = die->child;
16204 variant_child != NULL;
16205 variant_child = variant_child->sibling)
16206 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16207
16208 variant.last_field = fi->fields.size ();
16209}
16210
2ddeaf8a
TT
16211/* A helper for process_structure_scope that handles a single member
16212 DIE. */
16213
16214static void
16215handle_struct_member_die (struct die_info *child_die, struct type *type,
16216 struct field_info *fi,
16217 std::vector<struct symbol *> *template_args,
16218 struct dwarf2_cu *cu)
16219{
16220 if (child_die->tag == DW_TAG_member
9c6a1327 16221 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16222 {
16223 /* NOTE: carlton/2002-11-05: A C++ static data member
16224 should be a DW_TAG_member that is a declaration, but
16225 all versions of G++ as of this writing (so through at
16226 least 3.2.1) incorrectly generate DW_TAG_variable
16227 tags for them instead. */
16228 dwarf2_add_field (fi, child_die, cu);
16229 }
16230 else if (child_die->tag == DW_TAG_subprogram)
16231 {
16232 /* Rust doesn't have member functions in the C++ sense.
16233 However, it does emit ordinary functions as children
16234 of a struct DIE. */
16235 if (cu->language == language_rust)
16236 read_func_scope (child_die, cu);
16237 else
16238 {
16239 /* C++ member function. */
16240 dwarf2_add_member_fn (fi, child_die, type, cu);
16241 }
16242 }
16243 else if (child_die->tag == DW_TAG_inheritance)
16244 {
16245 /* C++ base class field. */
16246 dwarf2_add_field (fi, child_die, cu);
16247 }
16248 else if (type_can_define_types (child_die))
16249 dwarf2_add_type_defn (fi, child_die, cu);
16250 else if (child_die->tag == DW_TAG_template_type_param
16251 || child_die->tag == DW_TAG_template_value_param)
16252 {
16253 struct symbol *arg = new_symbol (child_die, NULL, cu);
16254
16255 if (arg != NULL)
16256 template_args->push_back (arg);
16257 }
9c6a1327
TT
16258 else if (child_die->tag == DW_TAG_variant_part)
16259 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16260 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16261 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16262}
16263
c767944b
DJ
16264/* Finish creating a structure or union type, including filling in
16265 its members and creating a symbol for it. */
16266
16267static void
16268process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16269{
5e22e966 16270 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16271 struct die_info *child_die;
c767944b
DJ
16272 struct type *type;
16273
16274 type = get_die_type (die, cu);
16275 if (type == NULL)
16276 type = read_structure_type (die, cu);
16277
3e1d3d8c 16278 bool has_template_parameters = false;
e142c38c 16279 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16280 {
16281 struct field_info fi;
2f4732b0 16282 std::vector<struct symbol *> template_args;
c906108c 16283
639d11d3 16284 child_die = die->child;
c906108c
SS
16285
16286 while (child_die && child_die->tag)
16287 {
2ddeaf8a 16288 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16289 child_die = child_die->sibling;
c906108c
SS
16290 }
16291
34eaf542 16292 /* Attach template arguments to type. */
2f4732b0 16293 if (!template_args.empty ())
34eaf542 16294 {
3e1d3d8c 16295 has_template_parameters = true;
34eaf542 16296 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16297 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16298 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16299 = XOBNEWVEC (&objfile->objfile_obstack,
16300 struct symbol *,
16301 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16302 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16303 template_args.data (),
34eaf542
TT
16304 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16305 * sizeof (struct symbol *)));
34eaf542
TT
16306 }
16307
c906108c 16308 /* Attach fields and member functions to the type. */
317f7127 16309 if (fi.nfields () > 0)
e7c27a73 16310 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16311 if (!fi.fnfieldlists.empty ())
c906108c 16312 {
e7c27a73 16313 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16314
c5aa993b 16315 /* Get the type which refers to the base class (possibly this
c906108c 16316 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16317 class from the DW_AT_containing_type attribute. This use of
16318 DW_AT_containing_type is a GNU extension. */
c906108c 16319
e142c38c 16320 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16321 {
e7c27a73 16322 struct type *t = die_containing_type (die, cu);
c906108c 16323
ae6ae975 16324 set_type_vptr_basetype (type, t);
c906108c
SS
16325 if (type == t)
16326 {
c906108c
SS
16327 int i;
16328
16329 /* Our own class provides vtbl ptr. */
1f704f76 16330 for (i = t->num_fields () - 1;
c906108c
SS
16331 i >= TYPE_N_BASECLASSES (t);
16332 --i)
16333 {
0d5cff50 16334 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16335
1168df01 16336 if (is_vtable_name (fieldname, cu))
c906108c 16337 {
ae6ae975 16338 set_type_vptr_fieldno (type, i);
c906108c
SS
16339 break;
16340 }
16341 }
16342
16343 /* Complain if virtual function table field not found. */
16344 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16345 complaint (_("virtual function table pointer "
3e43a32a 16346 "not found when defining class '%s'"),
7d93a1e0 16347 type->name () ? type->name () : "");
c906108c
SS
16348 }
16349 else
16350 {
ae6ae975 16351 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16352 }
16353 }
f6235d4c 16354 else if (cu->producer
61012eef 16355 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16356 {
16357 /* The IBM XLC compiler does not provide direct indication
16358 of the containing type, but the vtable pointer is
16359 always named __vfp. */
16360
16361 int i;
16362
1f704f76 16363 for (i = type->num_fields () - 1;
f6235d4c
EZ
16364 i >= TYPE_N_BASECLASSES (type);
16365 --i)
16366 {
16367 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16368 {
ae6ae975
DE
16369 set_type_vptr_fieldno (type, i);
16370 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16371 break;
16372 }
16373 }
16374 }
c906108c 16375 }
98751a41
JK
16376
16377 /* Copy fi.typedef_field_list linked list elements content into the
16378 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16379 if (!fi.typedef_field_list.empty ())
98751a41 16380 {
be2daae6 16381 int count = fi.typedef_field_list.size ();
98751a41 16382
a0d7a4ff 16383 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16384 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16385 = ((struct decl_field *)
be2daae6
TT
16386 TYPE_ALLOC (type,
16387 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16388 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16389
be2daae6
TT
16390 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16391 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16392 }
c767944b 16393
883fd55a
KS
16394 /* Copy fi.nested_types_list linked list elements content into the
16395 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16396 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16397 {
be2daae6 16398 int count = fi.nested_types_list.size ();
883fd55a
KS
16399
16400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16401 TYPE_NESTED_TYPES_ARRAY (type)
16402 = ((struct decl_field *)
be2daae6
TT
16403 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16404 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16405
be2daae6
TT
16406 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16407 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16408 }
c906108c 16409 }
63d06c5c 16410
bb5ed363 16411 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16412 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16413 cu->rust_unions.push_back (type);
0b92b5bb 16414
90aeadfc
DC
16415 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16416 snapshots) has been known to create a die giving a declaration
16417 for a class that has, as a child, a die giving a definition for a
16418 nested class. So we have to process our children even if the
16419 current die is a declaration. Normally, of course, a declaration
16420 won't have any children at all. */
134d01f1 16421
ca040673
DE
16422 child_die = die->child;
16423
90aeadfc
DC
16424 while (child_die != NULL && child_die->tag)
16425 {
16426 if (child_die->tag == DW_TAG_member
16427 || child_die->tag == DW_TAG_variable
34eaf542
TT
16428 || child_die->tag == DW_TAG_inheritance
16429 || child_die->tag == DW_TAG_template_value_param
16430 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16431 {
90aeadfc 16432 /* Do nothing. */
134d01f1 16433 }
90aeadfc
DC
16434 else
16435 process_die (child_die, cu);
134d01f1 16436
436c571c 16437 child_die = child_die->sibling;
134d01f1
DJ
16438 }
16439
fa4028e9
JB
16440 /* Do not consider external references. According to the DWARF standard,
16441 these DIEs are identified by the fact that they have no byte_size
16442 attribute, and a declaration attribute. */
16443 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16444 || !die_is_declaration (die, cu)
16445 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16446 {
16447 struct symbol *sym = new_symbol (die, type, cu);
16448
16449 if (has_template_parameters)
16450 {
a776957c
TT
16451 struct symtab *symtab;
16452 if (sym != nullptr)
16453 symtab = symbol_symtab (sym);
16454 else if (cu->line_header != nullptr)
16455 {
16456 /* Any related symtab will do. */
16457 symtab
7ba99d21 16458 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16459 }
16460 else
16461 {
16462 symtab = nullptr;
16463 complaint (_("could not find suitable "
16464 "symtab for template parameter"
16465 " - DIE at %s [in module %s]"),
16466 sect_offset_str (die->sect_off),
16467 objfile_name (objfile));
16468 }
16469
16470 if (symtab != nullptr)
16471 {
16472 /* Make sure that the symtab is set on the new symbols.
16473 Even though they don't appear in this symtab directly,
16474 other parts of gdb assume that symbols do, and this is
16475 reasonably true. */
16476 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16477 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16478 }
3e1d3d8c
TT
16479 }
16480 }
134d01f1
DJ
16481}
16482
ed6acedd
TT
16483/* Assuming DIE is an enumeration type, and TYPE is its associated
16484 type, update TYPE using some information only available in DIE's
16485 children. In particular, the fields are computed. */
55426c9d
JB
16486
16487static void
16488update_enumeration_type_from_children (struct die_info *die,
16489 struct type *type,
16490 struct dwarf2_cu *cu)
16491{
60f7655a 16492 struct die_info *child_die;
55426c9d
JB
16493 int unsigned_enum = 1;
16494 int flag_enum = 1;
55426c9d 16495
8268c778 16496 auto_obstack obstack;
ed6acedd 16497 std::vector<struct field> fields;
55426c9d 16498
60f7655a
DE
16499 for (child_die = die->child;
16500 child_die != NULL && child_die->tag;
436c571c 16501 child_die = child_die->sibling)
55426c9d
JB
16502 {
16503 struct attribute *attr;
16504 LONGEST value;
16505 const gdb_byte *bytes;
16506 struct dwarf2_locexpr_baton *baton;
16507 const char *name;
60f7655a 16508
55426c9d
JB
16509 if (child_die->tag != DW_TAG_enumerator)
16510 continue;
16511
16512 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16513 if (attr == NULL)
16514 continue;
16515
16516 name = dwarf2_name (child_die, cu);
16517 if (name == NULL)
16518 name = "<anonymous enumerator>";
16519
16520 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16521 &value, &bytes, &baton);
16522 if (value < 0)
16523 {
16524 unsigned_enum = 0;
16525 flag_enum = 0;
16526 }
55426c9d 16527 else
edd45eb0
SM
16528 {
16529 if (count_one_bits_ll (value) >= 2)
16530 flag_enum = 0;
edd45eb0 16531 }
55426c9d 16532
ed6acedd
TT
16533 fields.emplace_back ();
16534 struct field &field = fields.back ();
16535 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16536 SET_FIELD_ENUMVAL (field, value);
16537 }
16538
16539 if (!fields.empty ())
16540 {
5e33d5f4 16541 type->set_num_fields (fields.size ());
3cabb6b0
SM
16542 type->set_fields
16543 ((struct field *)
16544 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16545 memcpy (type->fields (), fields.data (),
ed6acedd 16546 sizeof (struct field) * fields.size ());
55426c9d
JB
16547 }
16548
16549 if (unsigned_enum)
653223d3
SM
16550 type->set_is_unsigned (true);
16551
55426c9d
JB
16552 if (flag_enum)
16553 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16554}
16555
134d01f1
DJ
16556/* Given a DW_AT_enumeration_type die, set its type. We do not
16557 complete the type's fields yet, or create any symbols. */
c906108c 16558
f792889a 16559static struct type *
134d01f1 16560read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16561{
5e22e966 16562 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16563 struct type *type;
c906108c 16564 struct attribute *attr;
0114d602 16565 const char *name;
134d01f1 16566
348e048f
DE
16567 /* If the definition of this type lives in .debug_types, read that type.
16568 Don't follow DW_AT_specification though, that will take us back up
16569 the chain and we want to go down. */
052c8bb8 16570 attr = die->attr (DW_AT_signature);
435d3d88 16571 if (attr != nullptr)
348e048f 16572 {
ac9ec31b 16573 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16574
ac9ec31b 16575 /* The type's CU may not be the same as CU.
02142a6c 16576 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16577 return set_die_type (die, type, cu);
16578 }
16579
c906108c
SS
16580 type = alloc_type (objfile);
16581
67607e24 16582 type->set_code (TYPE_CODE_ENUM);
94af9270 16583 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16584 if (name != NULL)
d0e39ea2 16585 type->set_name (name);
c906108c 16586
0626fc76
TT
16587 attr = dwarf2_attr (die, DW_AT_type, cu);
16588 if (attr != NULL)
16589 {
16590 struct type *underlying_type = die_type (die, cu);
16591
16592 TYPE_TARGET_TYPE (type) = underlying_type;
16593 }
16594
e142c38c 16595 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16596 if (attr != nullptr)
c906108c 16597 {
529908cb 16598 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16599 }
16600 else
16601 {
16602 TYPE_LENGTH (type) = 0;
16603 }
16604
2b4424c3
TT
16605 maybe_set_alignment (cu, die, type);
16606
137033e9
JB
16607 /* The enumeration DIE can be incomplete. In Ada, any type can be
16608 declared as private in the package spec, and then defined only
16609 inside the package body. Such types are known as Taft Amendment
16610 Types. When another package uses such a type, an incomplete DIE
16611 may be generated by the compiler. */
02eb380e 16612 if (die_is_declaration (die, cu))
b4b73759 16613 type->set_is_stub (true);
02eb380e 16614
0626fc76
TT
16615 /* If this type has an underlying type that is not a stub, then we
16616 may use its attributes. We always use the "unsigned" attribute
16617 in this situation, because ordinarily we guess whether the type
16618 is unsigned -- but the guess can be wrong and the underlying type
16619 can tell us the reality. However, we defer to a local size
16620 attribute if one exists, because this lets the compiler override
16621 the underlying type if needed. */
e46d3488 16622 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16623 {
9e7c9a03
HD
16624 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16625 underlying_type = check_typedef (underlying_type);
653223d3
SM
16626
16627 type->set_is_unsigned (underlying_type->is_unsigned ());
16628
0626fc76 16629 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16630 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16631
2b4424c3 16632 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16633 && TYPE_RAW_ALIGN (underlying_type) != 0)
16634 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16635 }
16636
3d567982
TT
16637 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16638
ed6acedd
TT
16639 set_die_type (die, type, cu);
16640
16641 /* Finish the creation of this type by using the enum's children.
16642 Note that, as usual, this must come after set_die_type to avoid
16643 infinite recursion when trying to compute the names of the
16644 enumerators. */
16645 update_enumeration_type_from_children (die, type, cu);
16646
16647 return type;
134d01f1
DJ
16648}
16649
16650/* Given a pointer to a die which begins an enumeration, process all
16651 the dies that define the members of the enumeration, and create the
16652 symbol for the enumeration type.
16653
16654 NOTE: We reverse the order of the element list. */
16655
16656static void
16657process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16658{
f792889a 16659 struct type *this_type;
134d01f1 16660
f792889a
DJ
16661 this_type = get_die_type (die, cu);
16662 if (this_type == NULL)
16663 this_type = read_enumeration_type (die, cu);
9dc481d3 16664
639d11d3 16665 if (die->child != NULL)
c906108c 16666 {
9dc481d3 16667 struct die_info *child_die;
15d034d0 16668 const char *name;
9dc481d3 16669
639d11d3 16670 child_die = die->child;
c906108c
SS
16671 while (child_die && child_die->tag)
16672 {
16673 if (child_die->tag != DW_TAG_enumerator)
16674 {
e7c27a73 16675 process_die (child_die, cu);
c906108c
SS
16676 }
16677 else
16678 {
39cbfefa
DJ
16679 name = dwarf2_name (child_die, cu);
16680 if (name)
ed6acedd 16681 new_symbol (child_die, this_type, cu);
c906108c
SS
16682 }
16683
436c571c 16684 child_die = child_die->sibling;
c906108c 16685 }
c906108c 16686 }
134d01f1 16687
6c83ed52
TT
16688 /* If we are reading an enum from a .debug_types unit, and the enum
16689 is a declaration, and the enum is not the signatured type in the
16690 unit, then we do not want to add a symbol for it. Adding a
16691 symbol would in some cases obscure the true definition of the
16692 enum, giving users an incomplete type when the definition is
16693 actually available. Note that we do not want to do this for all
16694 enums which are just declarations, because C++0x allows forward
16695 enum declarations. */
3019eac3 16696 if (cu->per_cu->is_debug_types
6c83ed52
TT
16697 && die_is_declaration (die, cu))
16698 {
52dc124a 16699 struct signatured_type *sig_type;
6c83ed52 16700
c0f78cd4 16701 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16702 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16703 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16704 return;
16705 }
16706
f792889a 16707 new_symbol (die, this_type, cu);
c906108c
SS
16708}
16709
16710/* Extract all information from a DW_TAG_array_type DIE and put it in
16711 the DIE's type field. For now, this only handles one dimensional
16712 arrays. */
16713
f792889a 16714static struct type *
e7c27a73 16715read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16716{
5e22e966 16717 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16718 struct die_info *child_die;
7e314c57 16719 struct type *type;
c906108c 16720 struct type *element_type, *range_type, *index_type;
c906108c 16721 struct attribute *attr;
15d034d0 16722 const char *name;
a405673c 16723 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16724 unsigned int bit_stride = 0;
c906108c 16725
e7c27a73 16726 element_type = die_type (die, cu);
c906108c 16727
7e314c57
JK
16728 /* The die_type call above may have already set the type for this DIE. */
16729 type = get_die_type (die, cu);
16730 if (type)
16731 return type;
16732
dc53a7ad
JB
16733 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16734 if (attr != NULL)
a405673c
JB
16735 {
16736 int stride_ok;
293e7e51 16737 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16738
16739 byte_stride_prop
16740 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16741 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16742 prop_type);
a405673c
JB
16743 if (!stride_ok)
16744 {
b98664d3 16745 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16746 " - DIE at %s [in module %s]"),
16747 sect_offset_str (die->sect_off),
5e22e966 16748 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16749 /* Ignore this attribute. We will likely not be able to print
16750 arrays of this type correctly, but there is little we can do
16751 to help if we cannot read the attribute's value. */
16752 byte_stride_prop = NULL;
16753 }
16754 }
dc53a7ad
JB
16755
16756 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16757 if (attr != NULL)
529908cb 16758 bit_stride = attr->constant_value (0);
dc53a7ad 16759
c906108c
SS
16760 /* Irix 6.2 native cc creates array types without children for
16761 arrays with unspecified length. */
639d11d3 16762 if (die->child == NULL)
c906108c 16763 {
46bf5051 16764 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16765 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16766 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16767 byte_stride_prop, bit_stride);
f792889a 16768 return set_die_type (die, type, cu);
c906108c
SS
16769 }
16770
791afaa2 16771 std::vector<struct type *> range_types;
639d11d3 16772 child_die = die->child;
c906108c
SS
16773 while (child_die && child_die->tag)
16774 {
16775 if (child_die->tag == DW_TAG_subrange_type)
16776 {
f792889a 16777 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16778
f792889a 16779 if (child_type != NULL)
a02abb62 16780 {
0963b4bd
MS
16781 /* The range type was succesfully read. Save it for the
16782 array type creation. */
791afaa2 16783 range_types.push_back (child_type);
a02abb62 16784 }
c906108c 16785 }
436c571c 16786 child_die = child_die->sibling;
c906108c
SS
16787 }
16788
16789 /* Dwarf2 dimensions are output from left to right, create the
16790 necessary array types in backwards order. */
7ca2d3a3 16791
c906108c 16792 type = element_type;
7ca2d3a3
DL
16793
16794 if (read_array_order (die, cu) == DW_ORD_col_major)
16795 {
16796 int i = 0;
9a619af0 16797
791afaa2 16798 while (i < range_types.size ())
dc53a7ad 16799 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16800 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16801 }
16802 else
16803 {
791afaa2 16804 size_t ndim = range_types.size ();
7ca2d3a3 16805 while (ndim-- > 0)
dc53a7ad 16806 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16807 byte_stride_prop, bit_stride);
7ca2d3a3 16808 }
c906108c 16809
f5f8a009
EZ
16810 /* Understand Dwarf2 support for vector types (like they occur on
16811 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16812 array type. This is not part of the Dwarf2/3 standard yet, but a
16813 custom vendor extension. The main difference between a regular
16814 array and the vector variant is that vectors are passed by value
16815 to functions. */
e142c38c 16816 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16817 if (attr != nullptr)
ea37ba09 16818 make_vector_type (type);
f5f8a009 16819
dbc98a8b
KW
16820 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16821 implementation may choose to implement triple vectors using this
16822 attribute. */
16823 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 16824 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 16825 {
529908cb
TT
16826 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16827 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 16828 else
b98664d3 16829 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16830 "than the total size of elements"));
dbc98a8b
KW
16831 }
16832
39cbfefa
DJ
16833 name = dwarf2_name (die, cu);
16834 if (name)
d0e39ea2 16835 type->set_name (name);
6e70227d 16836
2b4424c3
TT
16837 maybe_set_alignment (cu, die, type);
16838
0963b4bd 16839 /* Install the type in the die. */
7e314c57
JK
16840 set_die_type (die, type, cu);
16841
16842 /* set_die_type should be already done. */
b4ba55a1
JB
16843 set_descriptive_type (type, die, cu);
16844
7e314c57 16845 return type;
c906108c
SS
16846}
16847
7ca2d3a3 16848static enum dwarf_array_dim_ordering
6e70227d 16849read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16850{
16851 struct attribute *attr;
16852
16853 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16854
435d3d88 16855 if (attr != nullptr)
1bc397c5
TT
16856 {
16857 LONGEST val = attr->constant_value (-1);
16858 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16859 return (enum dwarf_array_dim_ordering) val;
16860 }
7ca2d3a3 16861
0963b4bd
MS
16862 /* GNU F77 is a special case, as at 08/2004 array type info is the
16863 opposite order to the dwarf2 specification, but data is still
16864 laid out as per normal fortran.
7ca2d3a3 16865
0963b4bd
MS
16866 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16867 version checking. */
7ca2d3a3 16868
905e0470
PM
16869 if (cu->language == language_fortran
16870 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16871 {
16872 return DW_ORD_row_major;
16873 }
16874
3a3440fb 16875 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16876 {
16877 case array_column_major:
16878 return DW_ORD_col_major;
16879 case array_row_major:
16880 default:
16881 return DW_ORD_row_major;
16882 };
16883}
16884
72019c9c 16885/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16886 the DIE's type field. */
72019c9c 16887
f792889a 16888static struct type *
72019c9c
GM
16889read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16890{
7e314c57
JK
16891 struct type *domain_type, *set_type;
16892 struct attribute *attr;
f792889a 16893
7e314c57
JK
16894 domain_type = die_type (die, cu);
16895
16896 /* The die_type call above may have already set the type for this DIE. */
16897 set_type = get_die_type (die, cu);
16898 if (set_type)
16899 return set_type;
16900
16901 set_type = create_set_type (NULL, domain_type);
16902
16903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
16904 if (attr != nullptr && attr->form_is_unsigned ())
16905 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 16906
2b4424c3
TT
16907 maybe_set_alignment (cu, die, set_type);
16908
f792889a 16909 return set_die_type (die, set_type, cu);
72019c9c 16910}
7ca2d3a3 16911
0971de02
TT
16912/* A helper for read_common_block that creates a locexpr baton.
16913 SYM is the symbol which we are marking as computed.
16914 COMMON_DIE is the DIE for the common block.
16915 COMMON_LOC is the location expression attribute for the common
16916 block itself.
16917 MEMBER_LOC is the location expression attribute for the particular
16918 member of the common block that we are processing.
16919 CU is the CU from which the above come. */
16920
16921static void
16922mark_common_block_symbol_computed (struct symbol *sym,
16923 struct die_info *common_die,
16924 struct attribute *common_loc,
16925 struct attribute *member_loc,
16926 struct dwarf2_cu *cu)
16927{
5e22e966 16928 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16929 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16930 struct dwarf2_locexpr_baton *baton;
16931 gdb_byte *ptr;
16932 unsigned int cu_off;
08feed99 16933 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16934 LONGEST offset = 0;
16935
16936 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16937 gdb_assert (common_loc->form_is_block ());
16938 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16939 || member_loc->form_is_constant ());
0971de02 16940
8d749320 16941 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16942 baton->per_objfile = per_objfile;
0971de02
TT
16943 baton->per_cu = cu->per_cu;
16944 gdb_assert (baton->per_cu);
16945
16946 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16947
cd6c91b4 16948 if (member_loc->form_is_constant ())
0971de02 16949 {
0826b30a 16950 offset = member_loc->constant_value (0);
0971de02
TT
16951 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16952 }
16953 else
9d2246fc 16954 baton->size += member_loc->as_block ()->size;
0971de02 16955
224c3ddb 16956 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16957 baton->data = ptr;
16958
16959 *ptr++ = DW_OP_call4;
9c541725 16960 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16961 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16962 ptr += 4;
16963
cd6c91b4 16964 if (member_loc->form_is_constant ())
0971de02
TT
16965 {
16966 *ptr++ = DW_OP_addr;
16967 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16968 ptr += cu->header.addr_size;
16969 }
16970 else
16971 {
16972 /* We have to copy the data here, because DW_OP_call4 will only
16973 use a DW_AT_location attribute. */
9d2246fc
TT
16974 struct dwarf_block *block = member_loc->as_block ();
16975 memcpy (ptr, block->data, block->size);
16976 ptr += block->size;
0971de02
TT
16977 }
16978
16979 *ptr++ = DW_OP_plus;
16980 gdb_assert (ptr - baton->data == baton->size);
16981
0971de02 16982 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16983 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16984}
16985
4357ac6c
TT
16986/* Create appropriate locally-scoped variables for all the
16987 DW_TAG_common_block entries. Also create a struct common_block
16988 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16989 is used to separate the common blocks name namespace from regular
4357ac6c 16990 variable names. */
c906108c
SS
16991
16992static void
e7c27a73 16993read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16994{
0971de02
TT
16995 struct attribute *attr;
16996
16997 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16998 if (attr != nullptr)
0971de02
TT
16999 {
17000 /* Support the .debug_loc offsets. */
4fc6c0d5 17001 if (attr->form_is_block ())
0971de02
TT
17002 {
17003 /* Ok. */
17004 }
cd6c91b4 17005 else if (attr->form_is_section_offset ())
0971de02
TT
17006 {
17007 dwarf2_complex_location_expr_complaint ();
17008 attr = NULL;
17009 }
17010 else
17011 {
17012 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17013 "common block member");
17014 attr = NULL;
17015 }
17016 }
17017
639d11d3 17018 if (die->child != NULL)
c906108c 17019 {
5e22e966 17020 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17021 struct die_info *child_die;
17022 size_t n_entries = 0, size;
17023 struct common_block *common_block;
17024 struct symbol *sym;
74ac6d43 17025
4357ac6c
TT
17026 for (child_die = die->child;
17027 child_die && child_die->tag;
436c571c 17028 child_die = child_die->sibling)
4357ac6c
TT
17029 ++n_entries;
17030
17031 size = (sizeof (struct common_block)
17032 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17033 common_block
17034 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17035 size);
4357ac6c
TT
17036 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17037 common_block->n_entries = 0;
17038
17039 for (child_die = die->child;
17040 child_die && child_die->tag;
436c571c 17041 child_die = child_die->sibling)
4357ac6c
TT
17042 {
17043 /* Create the symbol in the DW_TAG_common_block block in the current
17044 symbol scope. */
e7c27a73 17045 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17046 if (sym != NULL)
17047 {
17048 struct attribute *member_loc;
17049
17050 common_block->contents[common_block->n_entries++] = sym;
17051
17052 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17053 cu);
17054 if (member_loc)
17055 {
17056 /* GDB has handled this for a long time, but it is
17057 not specified by DWARF. It seems to have been
17058 emitted by gfortran at least as recently as:
17059 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17060 complaint (_("Variable in common block has "
0971de02 17061 "DW_AT_data_member_location "
9d8780f0
SM
17062 "- DIE at %s [in module %s]"),
17063 sect_offset_str (child_die->sect_off),
518817b3 17064 objfile_name (objfile));
0971de02 17065
cd6c91b4 17066 if (member_loc->form_is_section_offset ())
0971de02 17067 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17068 else if (member_loc->form_is_constant ()
4fc6c0d5 17069 || member_loc->form_is_block ())
0971de02 17070 {
435d3d88 17071 if (attr != nullptr)
0971de02
TT
17072 mark_common_block_symbol_computed (sym, die, attr,
17073 member_loc, cu);
17074 }
17075 else
17076 dwarf2_complex_location_expr_complaint ();
17077 }
17078 }
c906108c 17079 }
4357ac6c
TT
17080
17081 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17082 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17083 }
17084}
17085
0114d602 17086/* Create a type for a C++ namespace. */
d9fa45fe 17087
0114d602
DJ
17088static struct type *
17089read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17090{
5e22e966 17091 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17092 const char *previous_prefix, *name;
9219021c 17093 int is_anonymous;
0114d602
DJ
17094 struct type *type;
17095
17096 /* For extensions, reuse the type of the original namespace. */
17097 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17098 {
17099 struct die_info *ext_die;
17100 struct dwarf2_cu *ext_cu = cu;
9a619af0 17101
0114d602
DJ
17102 ext_die = dwarf2_extension (die, &ext_cu);
17103 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17104
17105 /* EXT_CU may not be the same as CU.
02142a6c 17106 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17107 return set_die_type (die, type, cu);
17108 }
9219021c 17109
e142c38c 17110 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17111
17112 /* Now build the name of the current namespace. */
17113
0114d602
DJ
17114 previous_prefix = determine_prefix (die, cu);
17115 if (previous_prefix[0] != '\0')
17116 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17117 previous_prefix, name, 0, cu);
0114d602
DJ
17118
17119 /* Create the type. */
19f392bc 17120 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17121
60531b24 17122 return set_die_type (die, type, cu);
0114d602
DJ
17123}
17124
22cee43f 17125/* Read a namespace scope. */
0114d602
DJ
17126
17127static void
17128read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17129{
5e22e966 17130 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17131 int is_anonymous;
9219021c 17132
5c4e30ca
DC
17133 /* Add a symbol associated to this if we haven't seen the namespace
17134 before. Also, add a using directive if it's an anonymous
17135 namespace. */
9219021c 17136
f2f0e013 17137 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17138 {
17139 struct type *type;
17140
0114d602 17141 type = read_type_die (die, cu);
e7c27a73 17142 new_symbol (die, type, cu);
5c4e30ca 17143
e8e80198 17144 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17145 if (is_anonymous)
0114d602
DJ
17146 {
17147 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17148
eb1e02fd 17149 std::vector<const char *> excludes;
804d2729 17150 add_using_directive (using_directives (cu),
7d93a1e0 17151 previous_prefix, type->name (), NULL,
eb1e02fd 17152 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17153 }
5c4e30ca 17154 }
9219021c 17155
639d11d3 17156 if (die->child != NULL)
d9fa45fe 17157 {
639d11d3 17158 struct die_info *child_die = die->child;
6e70227d 17159
d9fa45fe
DC
17160 while (child_die && child_die->tag)
17161 {
e7c27a73 17162 process_die (child_die, cu);
436c571c 17163 child_die = child_die->sibling;
d9fa45fe
DC
17164 }
17165 }
38d518c9
EZ
17166}
17167
f55ee35c
JK
17168/* Read a Fortran module as type. This DIE can be only a declaration used for
17169 imported module. Still we need that type as local Fortran "use ... only"
17170 declaration imports depend on the created type in determine_prefix. */
17171
17172static struct type *
17173read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17174{
5e22e966 17175 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17176 const char *module_name;
f55ee35c
JK
17177 struct type *type;
17178
17179 module_name = dwarf2_name (die, cu);
19f392bc 17180 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17181
f55ee35c
JK
17182 return set_die_type (die, type, cu);
17183}
17184
5d7cb8df
JK
17185/* Read a Fortran module. */
17186
17187static void
17188read_module (struct die_info *die, struct dwarf2_cu *cu)
17189{
17190 struct die_info *child_die = die->child;
530e8392
KB
17191 struct type *type;
17192
17193 type = read_type_die (die, cu);
17194 new_symbol (die, type, cu);
5d7cb8df 17195
5d7cb8df
JK
17196 while (child_die && child_die->tag)
17197 {
17198 process_die (child_die, cu);
436c571c 17199 child_die = child_die->sibling;
5d7cb8df
JK
17200 }
17201}
17202
38d518c9
EZ
17203/* Return the name of the namespace represented by DIE. Set
17204 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17205 namespace. */
17206
17207static const char *
e142c38c 17208namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17209{
17210 struct die_info *current_die;
17211 const char *name = NULL;
17212
17213 /* Loop through the extensions until we find a name. */
17214
17215 for (current_die = die;
17216 current_die != NULL;
f2f0e013 17217 current_die = dwarf2_extension (die, &cu))
38d518c9 17218 {
96553a0c
DE
17219 /* We don't use dwarf2_name here so that we can detect the absence
17220 of a name -> anonymous namespace. */
7d45c7c3 17221 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17222
38d518c9
EZ
17223 if (name != NULL)
17224 break;
17225 }
17226
17227 /* Is it an anonymous namespace? */
17228
17229 *is_anonymous = (name == NULL);
17230 if (*is_anonymous)
2b1dbab0 17231 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17232
17233 return name;
d9fa45fe
DC
17234}
17235
c906108c
SS
17236/* Extract all information from a DW_TAG_pointer_type DIE and add to
17237 the user defined type vector. */
17238
f792889a 17239static struct type *
e7c27a73 17240read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17241{
5e22e966 17242 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17243 struct comp_unit_head *cu_header = &cu->header;
c906108c 17244 struct type *type;
8b2dbe47
KB
17245 struct attribute *attr_byte_size;
17246 struct attribute *attr_address_class;
17247 int byte_size, addr_class;
7e314c57
JK
17248 struct type *target_type;
17249
17250 target_type = die_type (die, cu);
c906108c 17251
7e314c57
JK
17252 /* The die_type call above may have already set the type for this DIE. */
17253 type = get_die_type (die, cu);
17254 if (type)
17255 return type;
17256
17257 type = lookup_pointer_type (target_type);
8b2dbe47 17258
e142c38c 17259 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17260 if (attr_byte_size)
529908cb 17261 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17262 else
8b2dbe47
KB
17263 byte_size = cu_header->addr_size;
17264
e142c38c 17265 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17266 if (attr_address_class)
529908cb 17267 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17268 else
17269 addr_class = DW_ADDR_none;
17270
2b4424c3
TT
17271 ULONGEST alignment = get_alignment (cu, die);
17272
17273 /* If the pointer size, alignment, or address class is different
17274 than the default, create a type variant marked as such and set
17275 the length accordingly. */
17276 if (TYPE_LENGTH (type) != byte_size
17277 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17278 && alignment != TYPE_RAW_ALIGN (type))
17279 || addr_class != DW_ADDR_none)
c906108c 17280 {
5e2b427d 17281 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17282 {
314ad88d
PA
17283 type_instance_flags type_flags
17284 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17285 addr_class);
876cecd0
TT
17286 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17287 == 0);
8b2dbe47
KB
17288 type = make_type_with_address_space (type, type_flags);
17289 }
17290 else if (TYPE_LENGTH (type) != byte_size)
17291 {
b98664d3 17292 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17293 }
2b4424c3
TT
17294 else if (TYPE_RAW_ALIGN (type) != alignment)
17295 {
b98664d3 17296 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17297 " - DIE at %s [in module %s]"),
17298 sect_offset_str (die->sect_off),
5e22e966 17299 objfile_name (cu->per_objfile->objfile));
2b4424c3 17300 }
6e70227d 17301 else
9a619af0
MS
17302 {
17303 /* Should we also complain about unhandled address classes? */
17304 }
c906108c 17305 }
8b2dbe47
KB
17306
17307 TYPE_LENGTH (type) = byte_size;
2b4424c3 17308 set_type_align (type, alignment);
f792889a 17309 return set_die_type (die, type, cu);
c906108c
SS
17310}
17311
17312/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17313 the user defined type vector. */
17314
f792889a 17315static struct type *
e7c27a73 17316read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17317{
17318 struct type *type;
17319 struct type *to_type;
17320 struct type *domain;
17321
e7c27a73
DJ
17322 to_type = die_type (die, cu);
17323 domain = die_containing_type (die, cu);
0d5de010 17324
7e314c57
JK
17325 /* The calls above may have already set the type for this DIE. */
17326 type = get_die_type (die, cu);
17327 if (type)
17328 return type;
17329
78134374 17330 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17331 type = lookup_methodptr_type (to_type);
78134374 17332 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17333 {
5e22e966 17334 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17335
17336 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17337 to_type->fields (), to_type->num_fields (),
a409645d 17338 to_type->has_varargs ());
7078baeb
TT
17339 type = lookup_methodptr_type (new_type);
17340 }
0d5de010
DJ
17341 else
17342 type = lookup_memberptr_type (to_type, domain);
c906108c 17343
f792889a 17344 return set_die_type (die, type, cu);
c906108c
SS
17345}
17346
4297a3f0 17347/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17348 the user defined type vector. */
17349
f792889a 17350static struct type *
4297a3f0
AV
17351read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17352 enum type_code refcode)
c906108c 17353{
e7c27a73 17354 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17355 struct type *type, *target_type;
c906108c
SS
17356 struct attribute *attr;
17357
4297a3f0
AV
17358 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17359
7e314c57
JK
17360 target_type = die_type (die, cu);
17361
17362 /* The die_type call above may have already set the type for this DIE. */
17363 type = get_die_type (die, cu);
17364 if (type)
17365 return type;
17366
4297a3f0 17367 type = lookup_reference_type (target_type, refcode);
e142c38c 17368 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17369 if (attr != nullptr)
c906108c 17370 {
529908cb 17371 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17372 }
17373 else
17374 {
107d2387 17375 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17376 }
2b4424c3 17377 maybe_set_alignment (cu, die, type);
f792889a 17378 return set_die_type (die, type, cu);
c906108c
SS
17379}
17380
cf363f18
MW
17381/* Add the given cv-qualifiers to the element type of the array. GCC
17382 outputs DWARF type qualifiers that apply to an array, not the
17383 element type. But GDB relies on the array element type to carry
17384 the cv-qualifiers. This mimics section 6.7.3 of the C99
17385 specification. */
17386
17387static struct type *
17388add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17389 struct type *base_type, int cnst, int voltl)
17390{
17391 struct type *el_type, *inner_array;
17392
17393 base_type = copy_type (base_type);
17394 inner_array = base_type;
17395
78134374 17396 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17397 {
17398 TYPE_TARGET_TYPE (inner_array) =
17399 copy_type (TYPE_TARGET_TYPE (inner_array));
17400 inner_array = TYPE_TARGET_TYPE (inner_array);
17401 }
17402
17403 el_type = TYPE_TARGET_TYPE (inner_array);
17404 cnst |= TYPE_CONST (el_type);
17405 voltl |= TYPE_VOLATILE (el_type);
17406 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17407
17408 return set_die_type (die, base_type, cu);
17409}
17410
f792889a 17411static struct type *
e7c27a73 17412read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17413{
f792889a 17414 struct type *base_type, *cv_type;
c906108c 17415
e7c27a73 17416 base_type = die_type (die, cu);
7e314c57
JK
17417
17418 /* The die_type call above may have already set the type for this DIE. */
17419 cv_type = get_die_type (die, cu);
17420 if (cv_type)
17421 return cv_type;
17422
2f608a3a
KW
17423 /* In case the const qualifier is applied to an array type, the element type
17424 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17425 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17426 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17427
f792889a
DJ
17428 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17429 return set_die_type (die, cv_type, cu);
c906108c
SS
17430}
17431
f792889a 17432static struct type *
e7c27a73 17433read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17434{
f792889a 17435 struct type *base_type, *cv_type;
c906108c 17436
e7c27a73 17437 base_type = die_type (die, cu);
7e314c57
JK
17438
17439 /* The die_type call above may have already set the type for this DIE. */
17440 cv_type = get_die_type (die, cu);
17441 if (cv_type)
17442 return cv_type;
17443
cf363f18
MW
17444 /* In case the volatile qualifier is applied to an array type, the
17445 element type is so qualified, not the array type (section 6.7.3
17446 of C99). */
78134374 17447 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17448 return add_array_cv_type (die, cu, base_type, 0, 1);
17449
f792889a
DJ
17450 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17451 return set_die_type (die, cv_type, cu);
c906108c
SS
17452}
17453
06d66ee9
TT
17454/* Handle DW_TAG_restrict_type. */
17455
17456static struct type *
17457read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17458{
17459 struct type *base_type, *cv_type;
17460
17461 base_type = die_type (die, cu);
17462
17463 /* The die_type call above may have already set the type for this DIE. */
17464 cv_type = get_die_type (die, cu);
17465 if (cv_type)
17466 return cv_type;
17467
17468 cv_type = make_restrict_type (base_type);
17469 return set_die_type (die, cv_type, cu);
17470}
17471
a2c2acaf
MW
17472/* Handle DW_TAG_atomic_type. */
17473
17474static struct type *
17475read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17476{
17477 struct type *base_type, *cv_type;
17478
17479 base_type = die_type (die, cu);
17480
17481 /* The die_type call above may have already set the type for this DIE. */
17482 cv_type = get_die_type (die, cu);
17483 if (cv_type)
17484 return cv_type;
17485
17486 cv_type = make_atomic_type (base_type);
17487 return set_die_type (die, cv_type, cu);
17488}
17489
c906108c
SS
17490/* Extract all information from a DW_TAG_string_type DIE and add to
17491 the user defined type vector. It isn't really a user defined type,
17492 but it behaves like one, with other DIE's using an AT_user_def_type
17493 attribute to reference it. */
17494
f792889a 17495static struct type *
e7c27a73 17496read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17497{
5e22e966 17498 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17499 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17500 struct type *type, *range_type, *index_type, *char_type;
17501 struct attribute *attr;
216a7e6b
AB
17502 struct dynamic_prop prop;
17503 bool length_is_constant = true;
17504 LONGEST length;
17505
17506 /* There are a couple of places where bit sizes might be made use of
17507 when parsing a DW_TAG_string_type, however, no producer that we know
17508 of make use of these. Handling bit sizes that are a multiple of the
17509 byte size is easy enough, but what about other bit sizes? Lets deal
17510 with that problem when we have to. Warn about these attributes being
17511 unsupported, then parse the type and ignore them like we always
17512 have. */
17513 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17514 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17515 {
17516 static bool warning_printed = false;
17517 if (!warning_printed)
17518 {
17519 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17520 "currently supported on DW_TAG_string_type."));
17521 warning_printed = true;
17522 }
17523 }
c906108c 17524
e142c38c 17525 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17526 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17527 {
17528 /* The string length describes the location at which the length of
17529 the string can be found. The size of the length field can be
17530 specified with one of the attributes below. */
17531 struct type *prop_type;
17532 struct attribute *len
17533 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17534 if (len == nullptr)
17535 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17536 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17537 {
17538 /* Pass 0 as the default as we know this attribute is constant
17539 and the default value will not be returned. */
0826b30a 17540 LONGEST sz = len->constant_value (0);
293e7e51 17541 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17542 }
17543 else
17544 {
17545 /* If the size is not specified then we assume it is the size of
17546 an address on this target. */
293e7e51 17547 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17548 }
17549
17550 /* Convert the attribute into a dynamic property. */
17551 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17552 length = 1;
17553 else
17554 length_is_constant = false;
17555 }
17556 else if (attr != nullptr)
17557 {
17558 /* This DW_AT_string_length just contains the length with no
17559 indirection. There's no need to create a dynamic property in this
17560 case. Pass 0 for the default value as we know it will not be
17561 returned in this case. */
0826b30a 17562 length = attr->constant_value (0);
216a7e6b
AB
17563 }
17564 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17565 {
216a7e6b 17566 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17567 length = attr->constant_value (1);
c906108c
SS
17568 }
17569 else
17570 {
216a7e6b
AB
17571 /* Use 1 as a fallback length if we have nothing else. */
17572 length = 1;
c906108c 17573 }
6ccb9162 17574
46bf5051 17575 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17576 if (length_is_constant)
17577 range_type = create_static_range_type (NULL, index_type, 1, length);
17578 else
17579 {
17580 struct dynamic_prop low_bound;
17581
8c2e4e06 17582 low_bound.set_const_val (1);
216a7e6b
AB
17583 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17584 }
3b7538c0
UW
17585 char_type = language_string_char_type (cu->language_defn, gdbarch);
17586 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17587
f792889a 17588 return set_die_type (die, type, cu);
c906108c
SS
17589}
17590
4d804846
JB
17591/* Assuming that DIE corresponds to a function, returns nonzero
17592 if the function is prototyped. */
17593
17594static int
17595prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17596{
17597 struct attribute *attr;
17598
17599 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17600 if (attr && attr->as_boolean ())
4d804846
JB
17601 return 1;
17602
17603 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17604 is only meaningful for C, but the concept also extends to other
4d804846
JB
17605 languages that allow unprototyped functions (Eg: Objective C).
17606 For all other languages, assume that functions are always
17607 prototyped. */
17608 if (cu->language != language_c
17609 && cu->language != language_objc
17610 && cu->language != language_opencl)
17611 return 1;
17612
17613 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17614 prototyped and unprototyped functions; default to prototyped,
17615 since that is more common in modern code (and RealView warns
17616 about unprototyped functions). */
17617 if (producer_is_realview (cu->producer))
17618 return 1;
17619
17620 return 0;
17621}
17622
c906108c
SS
17623/* Handle DIES due to C code like:
17624
17625 struct foo
c5aa993b
JM
17626 {
17627 int (*funcp)(int a, long l);
17628 int b;
17629 };
c906108c 17630
0963b4bd 17631 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17632
f792889a 17633static struct type *
e7c27a73 17634read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17635{
5e22e966 17636 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17637 struct type *type; /* Type that this function returns. */
17638 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17639 struct attribute *attr;
17640
e7c27a73 17641 type = die_type (die, cu);
7e314c57
JK
17642
17643 /* The die_type call above may have already set the type for this DIE. */
17644 ftype = get_die_type (die, cu);
17645 if (ftype)
17646 return ftype;
17647
0c8b41f1 17648 ftype = lookup_function_type (type);
c906108c 17649
4d804846 17650 if (prototyped_function_p (die, cu))
27e69b7a 17651 ftype->set_is_prototyped (true);
c906108c 17652
c055b101
CV
17653 /* Store the calling convention in the type if it's available in
17654 the subroutine die. Otherwise set the calling convention to
17655 the default value DW_CC_normal. */
17656 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17657 if (attr != nullptr
529908cb 17658 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17659 TYPE_CALLING_CONVENTION (ftype)
529908cb 17660 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17661 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17662 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17663 else
17664 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17665
743649fd
MW
17666 /* Record whether the function returns normally to its caller or not
17667 if the DWARF producer set that information. */
17668 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17669 if (attr && attr->as_boolean ())
743649fd
MW
17670 TYPE_NO_RETURN (ftype) = 1;
17671
76c10ea2
GM
17672 /* We need to add the subroutine type to the die immediately so
17673 we don't infinitely recurse when dealing with parameters
0963b4bd 17674 declared as the same subroutine type. */
76c10ea2 17675 set_die_type (die, ftype, cu);
6e70227d 17676
639d11d3 17677 if (die->child != NULL)
c906108c 17678 {
bb5ed363 17679 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17680 struct die_info *child_die;
8072405b 17681 int nparams, iparams;
c906108c
SS
17682
17683 /* Count the number of parameters.
17684 FIXME: GDB currently ignores vararg functions, but knows about
17685 vararg member functions. */
8072405b 17686 nparams = 0;
639d11d3 17687 child_die = die->child;
c906108c
SS
17688 while (child_die && child_die->tag)
17689 {
17690 if (child_die->tag == DW_TAG_formal_parameter)
17691 nparams++;
17692 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17693 ftype->set_has_varargs (true);
17694
436c571c 17695 child_die = child_die->sibling;
c906108c
SS
17696 }
17697
17698 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17699 ftype->set_num_fields (nparams);
3cabb6b0
SM
17700 ftype->set_fields
17701 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17702
8072405b
JK
17703 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17704 even if we error out during the parameters reading below. */
17705 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17706 ftype->field (iparams).set_type (void_type);
8072405b
JK
17707
17708 iparams = 0;
639d11d3 17709 child_die = die->child;
c906108c
SS
17710 while (child_die && child_die->tag)
17711 {
17712 if (child_die->tag == DW_TAG_formal_parameter)
17713 {
3ce3b1ba
PA
17714 struct type *arg_type;
17715
17716 /* DWARF version 2 has no clean way to discern C++
17717 static and non-static member functions. G++ helps
17718 GDB by marking the first parameter for non-static
17719 member functions (which is the this pointer) as
17720 artificial. We pass this information to
17721 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17722
17723 DWARF version 3 added DW_AT_object_pointer, which GCC
17724 4.5 does not yet generate. */
e142c38c 17725 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17726 if (attr != nullptr)
c45bc3f8 17727 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17728 else
9c37b5ae 17729 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17730 arg_type = die_type (child_die, cu);
17731
17732 /* RealView does not mark THIS as const, which the testsuite
17733 expects. GCC marks THIS as const in method definitions,
17734 but not in the class specifications (GCC PR 43053). */
17735 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17736 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17737 {
17738 int is_this = 0;
17739 struct dwarf2_cu *arg_cu = cu;
17740 const char *name = dwarf2_name (child_die, cu);
17741
17742 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17743 if (attr != nullptr)
3ce3b1ba
PA
17744 {
17745 /* If the compiler emits this, use it. */
17746 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17747 is_this = 1;
17748 }
17749 else if (name && strcmp (name, "this") == 0)
17750 /* Function definitions will have the argument names. */
17751 is_this = 1;
17752 else if (name == NULL && iparams == 0)
17753 /* Declarations may not have the names, so like
17754 elsewhere in GDB, assume an artificial first
17755 argument is "this". */
17756 is_this = 1;
17757
17758 if (is_this)
17759 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17760 arg_type, 0);
17761 }
17762
5d14b6e5 17763 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17764 iparams++;
17765 }
436c571c 17766 child_die = child_die->sibling;
c906108c
SS
17767 }
17768 }
17769
76c10ea2 17770 return ftype;
c906108c
SS
17771}
17772
f792889a 17773static struct type *
e7c27a73 17774read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17775{
5e22e966 17776 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17777 const char *name = NULL;
3c8e0968 17778 struct type *this_type, *target_type;
c906108c 17779
94af9270 17780 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17781 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17782 this_type->set_target_is_stub (true);
f792889a 17783 set_die_type (die, this_type, cu);
3c8e0968
DE
17784 target_type = die_type (die, cu);
17785 if (target_type != this_type)
17786 TYPE_TARGET_TYPE (this_type) = target_type;
17787 else
17788 {
17789 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17790 spec and cause infinite loops in GDB. */
b98664d3 17791 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17792 "- DIE at %s [in module %s]"),
17793 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17794 TYPE_TARGET_TYPE (this_type) = NULL;
17795 }
e4003a34
TV
17796 if (name == NULL)
17797 {
17798 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17799 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17800 Handle these by just returning the target type, rather than
17801 constructing an anonymous typedef type and trying to handle this
17802 elsewhere. */
17803 set_die_type (die, target_type, cu);
17804 return target_type;
17805 }
f792889a 17806 return this_type;
c906108c
SS
17807}
17808
9b790ce7
UW
17809/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17810 (which may be different from NAME) to the architecture back-end to allow
17811 it to guess the correct format if necessary. */
17812
17813static struct type *
17814dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17815 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17816{
08feed99 17817 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17818 const struct floatformat **format;
17819 struct type *type;
17820
17821 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17822 if (format)
103a685e 17823 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17824 else
77b7c781 17825 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17826
17827 return type;
17828}
17829
eb77c9df
AB
17830/* Allocate an integer type of size BITS and name NAME. */
17831
17832static struct type *
17833dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17834 int bits, int unsigned_p, const char *name)
17835{
17836 struct type *type;
17837
17838 /* Versions of Intel's C Compiler generate an integer type called "void"
17839 instead of using DW_TAG_unspecified_type. This has been seen on
17840 at least versions 14, 17, and 18. */
35ee2dc2
AB
17841 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17842 && strcmp (name, "void") == 0)
eb77c9df
AB
17843 type = objfile_type (objfile)->builtin_void;
17844 else
17845 type = init_integer_type (objfile, bits, unsigned_p, name);
17846
17847 return type;
17848}
17849
8bdc1658
AB
17850/* Initialise and return a floating point type of size BITS suitable for
17851 use as a component of a complex number. The NAME_HINT is passed through
17852 when initialising the floating point type and is the name of the complex
17853 type.
17854
17855 As DWARF doesn't currently provide an explicit name for the components
17856 of a complex number, but it can be helpful to have these components
17857 named, we try to select a suitable name based on the size of the
17858 component. */
17859static struct type *
17860dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17861 struct objfile *objfile,
103a685e
TT
17862 int bits, const char *name_hint,
17863 enum bfd_endian byte_order)
8bdc1658 17864{
08feed99 17865 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17866 struct type *tt = nullptr;
17867
35add35e
AB
17868 /* Try to find a suitable floating point builtin type of size BITS.
17869 We're going to use the name of this type as the name for the complex
17870 target type that we are about to create. */
1db455a7 17871 switch (cu->language)
8bdc1658 17872 {
1db455a7
AB
17873 case language_fortran:
17874 switch (bits)
17875 {
17876 case 32:
17877 tt = builtin_f_type (gdbarch)->builtin_real;
17878 break;
17879 case 64:
17880 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17881 break;
17882 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17883 case 128:
17884 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17885 break;
17886 }
8bdc1658 17887 break;
1db455a7
AB
17888 default:
17889 switch (bits)
17890 {
17891 case 32:
17892 tt = builtin_type (gdbarch)->builtin_float;
17893 break;
17894 case 64:
17895 tt = builtin_type (gdbarch)->builtin_double;
17896 break;
17897 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17898 case 128:
17899 tt = builtin_type (gdbarch)->builtin_long_double;
17900 break;
17901 }
8bdc1658
AB
17902 break;
17903 }
17904
35add35e
AB
17905 /* If the type we found doesn't match the size we were looking for, then
17906 pretend we didn't find a type at all, the complex target type we
17907 create will then be nameless. */
a12e5744 17908 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17909 tt = nullptr;
17910
7d93a1e0 17911 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17912 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17913}
17914
c906108c
SS
17915/* Find a representation of a given base type and install
17916 it in the TYPE field of the die. */
17917
f792889a 17918static struct type *
e7c27a73 17919read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17920{
5e22e966 17921 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17922 struct type *type;
17923 struct attribute *attr;
19f392bc 17924 int encoding = 0, bits = 0;
15d034d0 17925 const char *name;
34877895 17926 gdbarch *arch;
c906108c 17927
e142c38c 17928 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
17929 if (attr != nullptr && attr->form_is_constant ())
17930 encoding = attr->constant_value (0);
e142c38c 17931 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17932 if (attr != nullptr)
529908cb 17933 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 17934 name = dwarf2_name (die, cu);
6ccb9162 17935 if (!name)
34877895 17936 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17937
08feed99 17938 arch = objfile->arch ();
103a685e
TT
17939 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17940
34877895 17941 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 17942 if (attr != nullptr && attr->form_is_constant ())
103a685e 17943 {
529908cb 17944 int endianity = attr->constant_value (0);
103a685e
TT
17945
17946 switch (endianity)
17947 {
17948 case DW_END_big:
17949 byte_order = BFD_ENDIAN_BIG;
17950 break;
17951 case DW_END_little:
17952 byte_order = BFD_ENDIAN_LITTLE;
17953 break;
17954 default:
17955 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17956 break;
17957 }
17958 }
6ccb9162
UW
17959
17960 switch (encoding)
c906108c 17961 {
6ccb9162
UW
17962 case DW_ATE_address:
17963 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17964 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17965 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17966 break;
17967 case DW_ATE_boolean:
19f392bc 17968 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17969 break;
17970 case DW_ATE_complex_float:
103a685e
TT
17971 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17972 byte_order);
78134374 17973 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17974 {
17975 if (name == nullptr)
17976 {
17977 struct obstack *obstack
5e22e966 17978 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17979 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17980 nullptr);
17981 }
17982 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17983 }
17984 else
17985 type = init_complex_type (name, type);
6ccb9162
UW
17986 break;
17987 case DW_ATE_decimal_float:
19f392bc 17988 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17989 break;
17990 case DW_ATE_float:
103a685e 17991 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17992 break;
17993 case DW_ATE_signed:
eb77c9df 17994 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17995 break;
17996 case DW_ATE_unsigned:
3b2b8fea
TT
17997 if (cu->language == language_fortran
17998 && name
61012eef 17999 && startswith (name, "character("))
19f392bc
UW
18000 type = init_character_type (objfile, bits, 1, name);
18001 else
eb77c9df 18002 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18003 break;
18004 case DW_ATE_signed_char:
6e70227d 18005 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18006 || cu->language == language_pascal
18007 || cu->language == language_fortran)
19f392bc
UW
18008 type = init_character_type (objfile, bits, 0, name);
18009 else
eb77c9df 18010 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18011 break;
18012 case DW_ATE_unsigned_char:
868a0084 18013 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18014 || cu->language == language_pascal
c44af4eb
TT
18015 || cu->language == language_fortran
18016 || cu->language == language_rust)
19f392bc
UW
18017 type = init_character_type (objfile, bits, 1, name);
18018 else
eb77c9df 18019 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18020 break;
75079b2b 18021 case DW_ATE_UTF:
53e710ac 18022 {
53e710ac
PA
18023 if (bits == 16)
18024 type = builtin_type (arch)->builtin_char16;
18025 else if (bits == 32)
18026 type = builtin_type (arch)->builtin_char32;
18027 else
18028 {
b98664d3 18029 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18030 bits);
eb77c9df 18031 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18032 }
18033 return set_die_type (die, type, cu);
18034 }
75079b2b
TT
18035 break;
18036
6ccb9162 18037 default:
b98664d3 18038 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18039 dwarf_type_encoding_name (encoding));
77b7c781 18040 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18041 break;
c906108c 18042 }
6ccb9162 18043
0114d602 18044 if (name && strcmp (name, "char") == 0)
15152a54 18045 type->set_has_no_signedness (true);
0114d602 18046
2b4424c3
TT
18047 maybe_set_alignment (cu, die, type);
18048
db558e34 18049 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18050
20a5fcbd
TT
18051 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18052 {
18053 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18054 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18055 {
529908cb 18056 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18057 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18058 /* Only use the attributes if they make sense together. */
18059 if (attr == nullptr
529908cb
TT
18060 || (attr->as_unsigned () + real_bit_size
18061 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18062 {
18063 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18064 = real_bit_size;
18065 if (attr != nullptr)
18066 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18067 = attr->as_unsigned ();
20a5fcbd
TT
18068 }
18069 }
18070 }
18071
f792889a 18072 return set_die_type (die, type, cu);
c906108c
SS
18073}
18074
80180f79
SA
18075/* Parse dwarf attribute if it's a block, reference or constant and put the
18076 resulting value of the attribute into struct bound_prop.
18077 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18078
18079static int
18080attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18081 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18082 struct type *default_type)
80180f79
SA
18083{
18084 struct dwarf2_property_baton *baton;
5e22e966 18085 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18086 struct objfile *objfile = per_objfile->objfile;
18087 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18088
9a49df9d
AB
18089 gdb_assert (default_type != NULL);
18090
80180f79
SA
18091 if (attr == NULL || prop == NULL)
18092 return 0;
18093
4fc6c0d5 18094 if (attr->form_is_block ())
80180f79 18095 {
8d749320 18096 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18097 baton->property_type = default_type;
80180f79 18098 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18099 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18100
18101 struct dwarf_block *block = attr->as_block ();
18102 baton->locexpr.size = block->size;
18103 baton->locexpr.data = block->data;
216a7e6b
AB
18104 switch (attr->name)
18105 {
18106 case DW_AT_string_length:
18107 baton->locexpr.is_reference = true;
18108 break;
18109 default:
18110 baton->locexpr.is_reference = false;
18111 break;
18112 }
8c2e4e06
SM
18113
18114 prop->set_locexpr (baton);
18115 gdb_assert (prop->baton () != NULL);
80180f79 18116 }
cd6c91b4 18117 else if (attr->form_is_ref ())
80180f79
SA
18118 {
18119 struct dwarf2_cu *target_cu = cu;
18120 struct die_info *target_die;
18121 struct attribute *target_attr;
18122
18123 target_die = follow_die_ref (die, attr, &target_cu);
18124 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18125 if (target_attr == NULL)
18126 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18127 target_cu);
80180f79
SA
18128 if (target_attr == NULL)
18129 return 0;
18130
df25ebbd 18131 switch (target_attr->name)
80180f79 18132 {
df25ebbd 18133 case DW_AT_location:
cd6c91b4 18134 if (target_attr->form_is_section_offset ())
df25ebbd 18135 {
8d749320 18136 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18137 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18138 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18139 prop->set_loclist (baton);
18140 gdb_assert (prop->baton () != NULL);
df25ebbd 18141 }
4fc6c0d5 18142 else if (target_attr->form_is_block ())
df25ebbd 18143 {
8d749320 18144 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18145 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18146 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18147 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18148 struct dwarf_block *block = target_attr->as_block ();
18149 baton->locexpr.size = block->size;
18150 baton->locexpr.data = block->data;
9a49df9d 18151 baton->locexpr.is_reference = true;
8c2e4e06
SM
18152 prop->set_locexpr (baton);
18153 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18154 }
18155 else
18156 {
18157 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18158 "dynamic property");
18159 return 0;
18160 }
18161 break;
18162 case DW_AT_data_member_location:
18163 {
18164 LONGEST offset;
18165
18166 if (!handle_data_member_location (target_die, target_cu,
18167 &offset))
18168 return 0;
18169
8d749320 18170 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18171 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18172 target_cu);
df25ebbd
JB
18173 baton->offset_info.offset = offset;
18174 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18175 prop->set_addr_offset (baton);
df25ebbd
JB
18176 break;
18177 }
80180f79
SA
18178 }
18179 }
cd6c91b4 18180 else if (attr->form_is_constant ())
8c2e4e06 18181 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18182 else
18183 {
18184 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18185 dwarf2_name (die, cu));
18186 return 0;
18187 }
18188
18189 return 1;
18190}
18191
09ba997f 18192/* See read.h. */
9a49df9d 18193
09ba997f 18194struct type *
293e7e51 18195dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18196{
9a49df9d
AB
18197 struct type *int_type;
18198
18199 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18200#define TRY_TYPE(F) \
18201 int_type = (unsigned_p \
18202 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18203 : objfile_type (objfile)->builtin_ ## F); \
18204 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18205 return int_type
18206
18207 TRY_TYPE (char);
18208 TRY_TYPE (short);
18209 TRY_TYPE (int);
18210 TRY_TYPE (long);
18211 TRY_TYPE (long_long);
18212
18213#undef TRY_TYPE
18214
18215 gdb_assert_not_reached ("unable to find suitable integer type");
18216}
18217
09ba997f 18218/* See read.h. */
11a8b164 18219
09ba997f 18220struct type *
293e7e51 18221dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18222{
293e7e51
SM
18223 int addr_size = this->per_cu->addr_size ();
18224 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18225}
18226
b86352cf
AB
18227/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18228 present (which is valid) then compute the default type based on the
18229 compilation units address size. */
18230
18231static struct type *
18232read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18233{
18234 struct type *index_type = die_type (die, cu);
18235
18236 /* Dwarf-2 specifications explicitly allows to create subrange types
18237 without specifying a base type.
18238 In that case, the base type must be set to the type of
18239 the lower bound, upper bound or count, in that order, if any of these
18240 three attributes references an object that has a type.
18241 If no base type is found, the Dwarf-2 specifications say that
18242 a signed integer type of size equal to the size of an address should
18243 be used.
18244 For the following C code: `extern char gdb_int [];'
18245 GCC produces an empty range DIE.
18246 FIXME: muller/2010-05-28: Possible references to object for low bound,
18247 high bound or count are not yet handled by this code. */
78134374 18248 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18249 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18250
18251 return index_type;
18252}
18253
a02abb62
JB
18254/* Read the given DW_AT_subrange DIE. */
18255
f792889a 18256static struct type *
a02abb62
JB
18257read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18258{
4c9ad8c2 18259 struct type *base_type, *orig_base_type;
a02abb62
JB
18260 struct type *range_type;
18261 struct attribute *attr;
729efb13 18262 struct dynamic_prop low, high;
4fae6e18 18263 int low_default_is_valid;
c451ebe5 18264 int high_bound_is_count = 0;
15d034d0 18265 const char *name;
d359392f 18266 ULONGEST negative_mask;
e77813c8 18267
b86352cf
AB
18268 orig_base_type = read_subrange_index_type (die, cu);
18269
4c9ad8c2
TT
18270 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18271 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18272 creating the range type, but we use the result of check_typedef
18273 when examining properties of the type. */
18274 base_type = check_typedef (orig_base_type);
a02abb62 18275
7e314c57
JK
18276 /* The die_type call above may have already set the type for this DIE. */
18277 range_type = get_die_type (die, cu);
18278 if (range_type)
18279 return range_type;
18280
8c2e4e06 18281 high.set_const_val (0);
729efb13 18282
4fae6e18
JK
18283 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18284 omitting DW_AT_lower_bound. */
18285 switch (cu->language)
6e70227d 18286 {
4fae6e18
JK
18287 case language_c:
18288 case language_cplus:
8c2e4e06 18289 low.set_const_val (0);
4fae6e18
JK
18290 low_default_is_valid = 1;
18291 break;
18292 case language_fortran:
8c2e4e06 18293 low.set_const_val (1);
4fae6e18
JK
18294 low_default_is_valid = 1;
18295 break;
18296 case language_d:
4fae6e18 18297 case language_objc:
c44af4eb 18298 case language_rust:
8c2e4e06 18299 low.set_const_val (0);
4fae6e18
JK
18300 low_default_is_valid = (cu->header.version >= 4);
18301 break;
18302 case language_ada:
18303 case language_m2:
18304 case language_pascal:
8c2e4e06 18305 low.set_const_val (1);
4fae6e18
JK
18306 low_default_is_valid = (cu->header.version >= 4);
18307 break;
18308 default:
8c2e4e06 18309 low.set_const_val (0);
4fae6e18
JK
18310 low_default_is_valid = 0;
18311 break;
a02abb62
JB
18312 }
18313
e142c38c 18314 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18315 if (attr != nullptr)
9a49df9d 18316 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18317 else if (!low_default_is_valid)
b98664d3 18318 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18319 "- DIE at %s [in module %s]"),
18320 sect_offset_str (die->sect_off),
5e22e966 18321 objfile_name (cu->per_objfile->objfile));
a02abb62 18322
506f5c41
TV
18323 struct attribute *attr_ub, *attr_count;
18324 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18325 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18326 {
506f5c41 18327 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18328 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18329 {
c451ebe5 18330 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18331 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18332 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18333 else
18334 high_bound_is_count = 1;
c2ff108b 18335 }
506f5c41
TV
18336 else
18337 {
18338 if (attr_ub != NULL)
18339 complaint (_("Unresolved DW_AT_upper_bound "
18340 "- DIE at %s [in module %s]"),
18341 sect_offset_str (die->sect_off),
5e22e966 18342 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18343 if (attr_count != NULL)
18344 complaint (_("Unresolved DW_AT_count "
18345 "- DIE at %s [in module %s]"),
18346 sect_offset_str (die->sect_off),
5e22e966 18347 objfile_name (cu->per_objfile->objfile));
506f5c41 18348 }
e77813c8 18349 }
a02abb62 18350
4e962e74
TT
18351 LONGEST bias = 0;
18352 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18353 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18354 bias = bias_attr->constant_value (0);
4e962e74 18355
dbb9c2b1
JB
18356 /* Normally, the DWARF producers are expected to use a signed
18357 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18358 But this is unfortunately not always the case, as witnessed
18359 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18360 is used instead. To work around that ambiguity, we treat
18361 the bounds as signed, and thus sign-extend their values, when
18362 the base type is signed. */
6e70227d 18363 negative_mask =
d359392f 18364 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18365 if (low.kind () == PROP_CONST
c6d940a9 18366 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18367 low.set_const_val (low.const_val () | negative_mask);
18368 if (high.kind () == PROP_CONST
c6d940a9 18369 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18370 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18371
5bbd8269
AB
18372 /* Check for bit and byte strides. */
18373 struct dynamic_prop byte_stride_prop;
18374 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18375 if (attr_byte_stride != nullptr)
18376 {
293e7e51 18377 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18378 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18379 prop_type);
18380 }
18381
18382 struct dynamic_prop bit_stride_prop;
18383 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18384 if (attr_bit_stride != nullptr)
18385 {
18386 /* It only makes sense to have either a bit or byte stride. */
18387 if (attr_byte_stride != nullptr)
18388 {
18389 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18390 "- DIE at %s [in module %s]"),
18391 sect_offset_str (die->sect_off),
5e22e966 18392 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18393 attr_bit_stride = nullptr;
18394 }
18395 else
18396 {
293e7e51 18397 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18398 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18399 prop_type);
18400 }
18401 }
18402
18403 if (attr_byte_stride != nullptr
18404 || attr_bit_stride != nullptr)
18405 {
18406 bool byte_stride_p = (attr_byte_stride != nullptr);
18407 struct dynamic_prop *stride
18408 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18409
18410 range_type
18411 = create_range_type_with_stride (NULL, orig_base_type, &low,
18412 &high, bias, stride, byte_stride_p);
18413 }
18414 else
18415 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18416
c451ebe5 18417 if (high_bound_is_count)
599088e3 18418 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18419
c2ff108b
JK
18420 /* Ada expects an empty array on no boundary attributes. */
18421 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18422 range_type->bounds ()->high.set_undefined ();
c2ff108b 18423
39cbfefa
DJ
18424 name = dwarf2_name (die, cu);
18425 if (name)
d0e39ea2 18426 range_type->set_name (name);
6e70227d 18427
e142c38c 18428 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18429 if (attr != nullptr)
529908cb 18430 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 18431
2b4424c3
TT
18432 maybe_set_alignment (cu, die, range_type);
18433
7e314c57
JK
18434 set_die_type (die, range_type, cu);
18435
18436 /* set_die_type should be already done. */
b4ba55a1
JB
18437 set_descriptive_type (range_type, die, cu);
18438
7e314c57 18439 return range_type;
a02abb62 18440}
6e70227d 18441
f792889a 18442static struct type *
81a17f79
JB
18443read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18444{
18445 struct type *type;
81a17f79 18446
5e22e966 18447 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18448 type->set_name (dwarf2_name (die, cu));
81a17f79 18449
74a2f8ff 18450 /* In Ada, an unspecified type is typically used when the description
85102364 18451 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18452 such a type, we treat it as a stub, and try to resolve it later on,
18453 when needed. */
18454 if (cu->language == language_ada)
b4b73759 18455 type->set_is_stub (true);
74a2f8ff 18456
f792889a 18457 return set_die_type (die, type, cu);
81a17f79 18458}
a02abb62 18459
639d11d3
DC
18460/* Read a single die and all its descendents. Set the die's sibling
18461 field to NULL; set other fields in the die correctly, and set all
18462 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18463 location of the info_ptr after reading all of those dies. PARENT
18464 is the parent of the die in question. */
18465
18466static struct die_info *
dee91e82 18467read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18468 const gdb_byte *info_ptr,
18469 const gdb_byte **new_info_ptr,
dee91e82 18470 struct die_info *parent)
639d11d3
DC
18471{
18472 struct die_info *die;
d521ce57 18473 const gdb_byte *cur_ptr;
639d11d3 18474
3e225074 18475 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18476 if (die == NULL)
18477 {
18478 *new_info_ptr = cur_ptr;
18479 return NULL;
18480 }
93311388 18481 store_in_ref_table (die, reader->cu);
639d11d3 18482
3e225074 18483 if (die->has_children)
bf6af496 18484 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18485 else
18486 {
18487 die->child = NULL;
18488 *new_info_ptr = cur_ptr;
18489 }
18490
18491 die->sibling = NULL;
18492 die->parent = parent;
18493 return die;
18494}
18495
18496/* Read a die, all of its descendents, and all of its siblings; set
18497 all of the fields of all of the dies correctly. Arguments are as
18498 in read_die_and_children. */
18499
18500static struct die_info *
bf6af496 18501read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18502 const gdb_byte *info_ptr,
18503 const gdb_byte **new_info_ptr,
bf6af496 18504 struct die_info *parent)
639d11d3
DC
18505{
18506 struct die_info *first_die, *last_sibling;
d521ce57 18507 const gdb_byte *cur_ptr;
639d11d3 18508
c906108c 18509 cur_ptr = info_ptr;
639d11d3
DC
18510 first_die = last_sibling = NULL;
18511
18512 while (1)
c906108c 18513 {
639d11d3 18514 struct die_info *die
dee91e82 18515 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18516
1d325ec1 18517 if (die == NULL)
c906108c 18518 {
639d11d3
DC
18519 *new_info_ptr = cur_ptr;
18520 return first_die;
c906108c 18521 }
1d325ec1
DJ
18522
18523 if (!first_die)
18524 first_die = die;
c906108c 18525 else
1d325ec1
DJ
18526 last_sibling->sibling = die;
18527
18528 last_sibling = die;
c906108c 18529 }
c906108c
SS
18530}
18531
bf6af496
DE
18532/* Read a die, all of its descendents, and all of its siblings; set
18533 all of the fields of all of the dies correctly. Arguments are as
18534 in read_die_and_children.
18535 This the main entry point for reading a DIE and all its children. */
18536
18537static struct die_info *
18538read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18539 const gdb_byte *info_ptr,
18540 const gdb_byte **new_info_ptr,
bf6af496
DE
18541 struct die_info *parent)
18542{
18543 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18544 new_info_ptr, parent);
18545
b4f54984 18546 if (dwarf_die_debug)
bf6af496
DE
18547 {
18548 fprintf_unfiltered (gdb_stdlog,
18549 "Read die from %s@0x%x of %s:\n",
96b79293 18550 reader->die_section->get_name (),
bf6af496
DE
18551 (unsigned) (info_ptr - reader->die_section->buffer),
18552 bfd_get_filename (reader->abfd));
b4f54984 18553 dump_die (die, dwarf_die_debug);
bf6af496
DE
18554 }
18555
18556 return die;
18557}
18558
3019eac3
DE
18559/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18560 attributes.
18561 The caller is responsible for filling in the extra attributes
18562 and updating (*DIEP)->num_attrs.
18563 Set DIEP to point to a newly allocated die with its information,
3e225074 18564 except for its child, sibling, and parent fields. */
93311388 18565
d521ce57 18566static const gdb_byte *
3019eac3 18567read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18568 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18569 int num_extra_attrs)
93311388 18570{
b64f50a1 18571 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18572 struct abbrev_info *abbrev;
18573 struct die_info *die;
18574 struct dwarf2_cu *cu = reader->cu;
18575 bfd *abfd = reader->abfd;
18576
9c541725 18577 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18578 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18579 info_ptr += bytes_read;
18580 if (!abbrev_number)
18581 {
18582 *diep = NULL;
93311388
DE
18583 return info_ptr;
18584 }
18585
685af9cd 18586 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18587 if (!abbrev)
348e048f
DE
18588 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18589 abbrev_number,
18590 bfd_get_filename (abfd));
18591
3019eac3 18592 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18593 die->sect_off = sect_off;
93311388
DE
18594 die->tag = abbrev->tag;
18595 die->abbrev = abbrev_number;
3e225074 18596 die->has_children = abbrev->has_children;
93311388 18597
3019eac3
DE
18598 /* Make the result usable.
18599 The caller needs to update num_attrs after adding the extra
18600 attributes. */
93311388
DE
18601 die->num_attrs = abbrev->num_attrs;
18602
7a5f294d 18603 bool any_need_reprocess = false;
93311388 18604 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18605 {
7a5f294d
TT
18606 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18607 info_ptr);
18608 if (die->attrs[i].requires_reprocessing_p ())
18609 any_need_reprocess = true;
18a8505e
AT
18610 }
18611
052c8bb8 18612 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
18613 if (attr != nullptr && attr->form_is_unsigned ())
18614 cu->str_offsets_base = attr->as_unsigned ();
93311388 18615
41144253 18616 attr = die->attr (DW_AT_loclists_base);
18617 if (attr != nullptr)
529908cb 18618 cu->loclist_base = attr->as_unsigned ();
41144253 18619
a39fdb41 18620 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18621 if (maybe_addr_base.has_value ())
18622 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18623
18624 attr = die->attr (DW_AT_rnglists_base);
18625 if (attr != nullptr)
529908cb 18626 cu->ranges_base = attr->as_unsigned ();
d0ce17d8 18627
7a5f294d
TT
18628 if (any_need_reprocess)
18629 {
18630 for (i = 0; i < abbrev->num_attrs; ++i)
18631 {
18632 if (die->attrs[i].requires_reprocessing_p ())
18633 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18634 }
18635 }
93311388 18636 *diep = die;
93311388
DE
18637 return info_ptr;
18638}
18639
3019eac3
DE
18640/* Read a die and all its attributes.
18641 Set DIEP to point to a newly allocated die with its information,
3e225074 18642 except for its child, sibling, and parent fields. */
3019eac3 18643
d521ce57 18644static const gdb_byte *
3019eac3 18645read_full_die (const struct die_reader_specs *reader,
3e225074 18646 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18647{
d521ce57 18648 const gdb_byte *result;
bf6af496 18649
3e225074 18650 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18651
b4f54984 18652 if (dwarf_die_debug)
bf6af496
DE
18653 {
18654 fprintf_unfiltered (gdb_stdlog,
18655 "Read die from %s@0x%x of %s:\n",
96b79293 18656 reader->die_section->get_name (),
bf6af496
DE
18657 (unsigned) (info_ptr - reader->die_section->buffer),
18658 bfd_get_filename (reader->abfd));
b4f54984 18659 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18660 }
18661
18662 return result;
3019eac3 18663}
433df2d4 18664\f
c906108c 18665
72bf9492
DJ
18666/* Returns nonzero if TAG represents a type that we might generate a partial
18667 symbol for. */
18668
18669static int
18670is_type_tag_for_partial (int tag)
18671{
18672 switch (tag)
18673 {
18674#if 0
18675 /* Some types that would be reasonable to generate partial symbols for,
18676 that we don't at present. */
18677 case DW_TAG_array_type:
18678 case DW_TAG_file_type:
18679 case DW_TAG_ptr_to_member_type:
18680 case DW_TAG_set_type:
18681 case DW_TAG_string_type:
18682 case DW_TAG_subroutine_type:
18683#endif
18684 case DW_TAG_base_type:
18685 case DW_TAG_class_type:
680b30c7 18686 case DW_TAG_interface_type:
72bf9492
DJ
18687 case DW_TAG_enumeration_type:
18688 case DW_TAG_structure_type:
18689 case DW_TAG_subrange_type:
18690 case DW_TAG_typedef:
18691 case DW_TAG_union_type:
18692 return 1;
18693 default:
18694 return 0;
18695 }
18696}
18697
18698/* Load all DIEs that are interesting for partial symbols into memory. */
18699
18700static struct partial_die_info *
dee91e82 18701load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18702 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18703{
dee91e82 18704 struct dwarf2_cu *cu = reader->cu;
5e22e966 18705 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18706 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18707 unsigned int bytes_read;
5afb4e99 18708 unsigned int load_all = 0;
72bf9492
DJ
18709 int nesting_level = 1;
18710
18711 parent_die = NULL;
18712 last_die = NULL;
18713
7adf1e79
DE
18714 gdb_assert (cu->per_cu != NULL);
18715 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18716 load_all = 1;
18717
72bf9492
DJ
18718 cu->partial_dies
18719 = htab_create_alloc_ex (cu->header.length / 12,
18720 partial_die_hash,
18721 partial_die_eq,
18722 NULL,
18723 &cu->comp_unit_obstack,
18724 hashtab_obstack_allocate,
18725 dummy_obstack_deallocate);
18726
72bf9492
DJ
18727 while (1)
18728 {
685af9cd 18729 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18730
18731 /* A NULL abbrev means the end of a series of children. */
18732 if (abbrev == NULL)
18733 {
18734 if (--nesting_level == 0)
cd9983dd
YQ
18735 return first_die;
18736
72bf9492
DJ
18737 info_ptr += bytes_read;
18738 last_die = parent_die;
18739 parent_die = parent_die->die_parent;
18740 continue;
18741 }
18742
98bfdba5
PA
18743 /* Check for template arguments. We never save these; if
18744 they're seen, we just mark the parent, and go on our way. */
18745 if (parent_die != NULL
18746 && cu->language == language_cplus
18747 && (abbrev->tag == DW_TAG_template_type_param
18748 || abbrev->tag == DW_TAG_template_value_param))
18749 {
18750 parent_die->has_template_arguments = 1;
18751
18752 if (!load_all)
18753 {
18754 /* We don't need a partial DIE for the template argument. */
dee91e82 18755 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18756 continue;
18757 }
18758 }
18759
0d99eb77 18760 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18761 Skip their other children. */
18762 if (!load_all
18763 && cu->language == language_cplus
18764 && parent_die != NULL
f9b5d5ea
TV
18765 && parent_die->tag == DW_TAG_subprogram
18766 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18767 {
dee91e82 18768 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18769 continue;
18770 }
18771
5afb4e99
DJ
18772 /* Check whether this DIE is interesting enough to save. Normally
18773 we would not be interested in members here, but there may be
18774 later variables referencing them via DW_AT_specification (for
18775 static members). */
18776 if (!load_all
18777 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18778 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18779 && abbrev->tag != DW_TAG_enumerator
18780 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18781 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18782 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18783 && abbrev->tag != DW_TAG_variable
5afb4e99 18784 && abbrev->tag != DW_TAG_namespace
f55ee35c 18785 && abbrev->tag != DW_TAG_module
95554aad 18786 && abbrev->tag != DW_TAG_member
74921315
KS
18787 && abbrev->tag != DW_TAG_imported_unit
18788 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18789 {
18790 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18791 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18792 continue;
18793 }
18794
6f06d47b
YQ
18795 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18796 abbrev);
cd9983dd 18797
48fbe735 18798 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18799
18800 /* This two-pass algorithm for processing partial symbols has a
18801 high cost in cache pressure. Thus, handle some simple cases
18802 here which cover the majority of C partial symbols. DIEs
18803 which neither have specification tags in them, nor could have
18804 specification tags elsewhere pointing at them, can simply be
18805 processed and discarded.
18806
18807 This segment is also optional; scan_partial_symbols and
18808 add_partial_symbol will handle these DIEs if we chain
18809 them in normally. When compilers which do not emit large
18810 quantities of duplicate debug information are more common,
18811 this code can probably be removed. */
18812
18813 /* Any complete simple types at the top level (pretty much all
18814 of them, for a language without namespaces), can be processed
18815 directly. */
18816 if (parent_die == NULL
cd9983dd
YQ
18817 && pdi.has_specification == 0
18818 && pdi.is_declaration == 0
18819 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18820 || pdi.tag == DW_TAG_base_type
18821 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18822 {
7d00ffec 18823 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18824 add_partial_symbol (&pdi, cu);
18825
cd9983dd 18826 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18827 continue;
18828 }
18829
d8228535
JK
18830 /* The exception for DW_TAG_typedef with has_children above is
18831 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18832 type_name_or_error will error on such types later.
d8228535
JK
18833
18834 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18835 it could not find the child DIEs referenced later, this is checked
18836 above. In correct DWARF DW_TAG_typedef should have no children. */
18837
cd9983dd 18838 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18839 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18840 "- DIE at %s [in module %s]"),
cd9983dd 18841 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18842
72bf9492
DJ
18843 /* If we're at the second level, and we're an enumerator, and
18844 our parent has no specification (meaning possibly lives in a
18845 namespace elsewhere), then we can add the partial symbol now
18846 instead of queueing it. */
cd9983dd 18847 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18848 && parent_die != NULL
18849 && parent_die->die_parent == NULL
18850 && parent_die->tag == DW_TAG_enumeration_type
18851 && parent_die->has_specification == 0)
18852 {
7d00ffec 18853 if (pdi.raw_name == NULL)
b98664d3 18854 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18855 else if (building_psymtab)
f0fbb768 18856 add_partial_symbol (&pdi, cu);
72bf9492 18857
cd9983dd 18858 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18859 continue;
18860 }
18861
cd9983dd 18862 struct partial_die_info *part_die
6f06d47b 18863 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18864
72bf9492
DJ
18865 /* We'll save this DIE so link it in. */
18866 part_die->die_parent = parent_die;
18867 part_die->die_sibling = NULL;
18868 part_die->die_child = NULL;
18869
18870 if (last_die && last_die == parent_die)
18871 last_die->die_child = part_die;
18872 else if (last_die)
18873 last_die->die_sibling = part_die;
18874
18875 last_die = part_die;
18876
18877 if (first_die == NULL)
18878 first_die = part_die;
18879
18880 /* Maybe add the DIE to the hash table. Not all DIEs that we
18881 find interesting need to be in the hash table, because we
18882 also have the parent/sibling/child chains; only those that we
18883 might refer to by offset later during partial symbol reading.
18884
18885 For now this means things that might have be the target of a
18886 DW_AT_specification, DW_AT_abstract_origin, or
18887 DW_AT_extension. DW_AT_extension will refer only to
18888 namespaces; DW_AT_abstract_origin refers to functions (and
18889 many things under the function DIE, but we do not recurse
18890 into function DIEs during partial symbol reading) and
18891 possibly variables as well; DW_AT_specification refers to
18892 declarations. Declarations ought to have the DW_AT_declaration
18893 flag. It happens that GCC forgets to put it in sometimes, but
18894 only for functions, not for types.
18895
18896 Adding more things than necessary to the hash table is harmless
18897 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18898 wasted time in find_partial_die, when we reread the compilation
18899 unit with load_all_dies set. */
72bf9492 18900
5afb4e99 18901 if (load_all
72929c62 18902 || abbrev->tag == DW_TAG_constant
5afb4e99 18903 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18904 || abbrev->tag == DW_TAG_variable
18905 || abbrev->tag == DW_TAG_namespace
18906 || part_die->is_declaration)
18907 {
18908 void **slot;
18909
18910 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18911 to_underlying (part_die->sect_off),
18912 INSERT);
72bf9492
DJ
18913 *slot = part_die;
18914 }
18915
72bf9492 18916 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18917 we have no reason to follow the children of structures; for other
98bfdba5
PA
18918 languages we have to, so that we can get at method physnames
18919 to infer fully qualified class names, for DW_AT_specification,
18920 and for C++ template arguments. For C++, we also look one level
18921 inside functions to find template arguments (if the name of the
18922 function does not already contain the template arguments).
bc30ff58 18923
0a4b0913
AB
18924 For Ada and Fortran, we need to scan the children of subprograms
18925 and lexical blocks as well because these languages allow the
18926 definition of nested entities that could be interesting for the
18927 debugger, such as nested subprograms for instance. */
72bf9492 18928 if (last_die->has_children
5afb4e99
DJ
18929 && (load_all
18930 || last_die->tag == DW_TAG_namespace
f55ee35c 18931 || last_die->tag == DW_TAG_module
72bf9492 18932 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18933 || (cu->language == language_cplus
18934 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18935 && (last_die->raw_name == NULL
18936 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18937 || (cu->language != language_c
18938 && (last_die->tag == DW_TAG_class_type
680b30c7 18939 || last_die->tag == DW_TAG_interface_type
72bf9492 18940 || last_die->tag == DW_TAG_structure_type
bc30ff58 18941 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18942 || ((cu->language == language_ada
18943 || cu->language == language_fortran)
bc30ff58
JB
18944 && (last_die->tag == DW_TAG_subprogram
18945 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18946 {
18947 nesting_level++;
18948 parent_die = last_die;
18949 continue;
18950 }
18951
18952 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18953 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18954
18955 /* Back to the top, do it again. */
18956 }
18957}
18958
6f06d47b
YQ
18959partial_die_info::partial_die_info (sect_offset sect_off_,
18960 struct abbrev_info *abbrev)
18961 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18962{
18963}
18964
7d00ffec
TT
18965/* See class definition. */
18966
18967const char *
18968partial_die_info::name (dwarf2_cu *cu)
18969{
18970 if (!canonical_name && raw_name != nullptr)
18971 {
18972 struct objfile *objfile = cu->per_objfile->objfile;
18973 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18974 canonical_name = 1;
18975 }
18976
18977 return raw_name;
18978}
18979
35cc7ed7
YQ
18980/* Read a minimal amount of information into the minimal die structure.
18981 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18982
48fbe735
YQ
18983const gdb_byte *
18984partial_die_info::read (const struct die_reader_specs *reader,
18985 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18986{
dee91e82 18987 struct dwarf2_cu *cu = reader->cu;
976ca316 18988 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18989 unsigned int i;
c5aa993b 18990 int has_low_pc_attr = 0;
c906108c 18991 int has_high_pc_attr = 0;
91da1414 18992 int high_pc_relative = 0;
c906108c 18993
fd0a254f 18994 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18995 {
e7da7f8f 18996 attribute attr;
7a5f294d 18997 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e
AT
18998 /* String and address offsets that need to do the reprocessing have
18999 already been read at this point, so there is no need to wait until
19000 the loop terminates to do the reprocessing. */
7a5f294d 19001 if (attr.requires_reprocessing_p ())
d0ce17d8 19002 read_attribute_reprocess (reader, &attr, tag);
c906108c 19003 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 19004 partial symbol table. */
c906108c
SS
19005 switch (attr.name)
19006 {
19007 case DW_AT_name:
48fbe735 19008 switch (tag)
71c25dea
TT
19009 {
19010 case DW_TAG_compile_unit:
95554aad 19011 case DW_TAG_partial_unit:
348e048f 19012 case DW_TAG_type_unit:
71c25dea
TT
19013 /* Compilation units have a DW_AT_name that is a filename, not
19014 a source language identifier. */
19015 case DW_TAG_enumeration_type:
19016 case DW_TAG_enumerator:
19017 /* These tags always have simple identifiers already; no need
19018 to canonicalize them. */
7d00ffec 19019 canonical_name = 1;
2c830f54 19020 raw_name = attr.as_string ();
71c25dea
TT
19021 break;
19022 default:
7d00ffec 19023 canonical_name = 0;
2c830f54 19024 raw_name = attr.as_string ();
71c25dea
TT
19025 break;
19026 }
c906108c 19027 break;
31ef98ae 19028 case DW_AT_linkage_name:
c906108c 19029 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19030 /* Note that both forms of linkage name might appear. We
19031 assume they will be the same, and we only store the last
19032 one we see. */
95f982e5 19033 linkage_name = attr.as_string ();
c906108c
SS
19034 break;
19035 case DW_AT_low_pc:
19036 has_low_pc_attr = 1;
95f982e5 19037 lowpc = attr.as_address ();
c906108c
SS
19038 break;
19039 case DW_AT_high_pc:
19040 has_high_pc_attr = 1;
95f982e5 19041 highpc = attr.as_address ();
cd6c91b4 19042 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19043 high_pc_relative = 1;
c906108c
SS
19044 break;
19045 case DW_AT_location:
0963b4bd 19046 /* Support the .debug_loc offsets. */
4fc6c0d5 19047 if (attr.form_is_block ())
8e19ed76 19048 {
9d2246fc 19049 d.locdesc = attr.as_block ();
8e19ed76 19050 }
cd6c91b4 19051 else if (attr.form_is_section_offset ())
8e19ed76 19052 {
4d3c2250 19053 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19054 }
19055 else
19056 {
4d3c2250
KB
19057 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19058 "partial symbol information");
8e19ed76 19059 }
c906108c 19060 break;
c906108c 19061 case DW_AT_external:
c45bc3f8 19062 is_external = attr.as_boolean ();
c906108c
SS
19063 break;
19064 case DW_AT_declaration:
c45bc3f8 19065 is_declaration = attr.as_boolean ();
c906108c
SS
19066 break;
19067 case DW_AT_type:
48fbe735 19068 has_type = 1;
c906108c
SS
19069 break;
19070 case DW_AT_abstract_origin:
19071 case DW_AT_specification:
72bf9492 19072 case DW_AT_extension:
48fbe735 19073 has_specification = 1;
0826b30a 19074 spec_offset = attr.get_ref_die_offset ();
48fbe735 19075 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19076 || cu->per_cu->is_dwz);
c906108c
SS
19077 break;
19078 case DW_AT_sibling:
19079 /* Ignore absolute siblings, they might point outside of
19080 the current compile unit. */
19081 if (attr.form == DW_FORM_ref_addr)
b98664d3 19082 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19083 else
b9502d3f 19084 {
48fbe735 19085 const gdb_byte *buffer = reader->buffer;
0826b30a 19086 sect_offset off = attr.get_ref_die_offset ();
9c541725 19087 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19088
19089 if (sibling_ptr < info_ptr)
b98664d3 19090 complaint (_("DW_AT_sibling points backwards"));
22869d73 19091 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19092 reader->die_section->overflow_complaint ();
b9502d3f 19093 else
48fbe735 19094 sibling = sibling_ptr;
b9502d3f 19095 }
c906108c 19096 break;
fa4028e9 19097 case DW_AT_byte_size:
48fbe735 19098 has_byte_size = 1;
fa4028e9 19099 break;
ff908ebf 19100 case DW_AT_const_value:
48fbe735 19101 has_const_value = 1;
ff908ebf 19102 break;
68511cec
CES
19103 case DW_AT_calling_convention:
19104 /* DWARF doesn't provide a way to identify a program's source-level
19105 entry point. DW_AT_calling_convention attributes are only meant
19106 to describe functions' calling conventions.
19107
19108 However, because it's a necessary piece of information in
0c1b455e
TT
19109 Fortran, and before DWARF 4 DW_CC_program was the only
19110 piece of debugging information whose definition refers to
19111 a 'main program' at all, several compilers marked Fortran
19112 main programs with DW_CC_program --- even when those
19113 functions use the standard calling conventions.
19114
19115 Although DWARF now specifies a way to provide this
19116 information, we support this practice for backward
19117 compatibility. */
529908cb 19118 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19119 && cu->language == language_fortran)
48fbe735 19120 main_subprogram = 1;
68511cec 19121 break;
481860b3 19122 case DW_AT_inline:
529908cb
TT
19123 {
19124 LONGEST value = attr.constant_value (-1);
19125 if (value == DW_INL_inlined
19126 || value == DW_INL_declared_inlined)
19127 may_be_inlined = 1;
19128 }
481860b3 19129 break;
95554aad
TT
19130
19131 case DW_AT_import:
48fbe735 19132 if (tag == DW_TAG_imported_unit)
36586728 19133 {
0826b30a 19134 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19135 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19136 || cu->per_cu->is_dwz);
19137 }
95554aad
TT
19138 break;
19139
0c1b455e 19140 case DW_AT_main_subprogram:
c45bc3f8 19141 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19142 break;
19143
05caa1d2
TT
19144 case DW_AT_ranges:
19145 {
d0ce17d8
CT
19146 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19147 skeleton. We take advantage of the fact the DW_AT_ranges
19148 does not appear in DW_TAG_compile_unit of DWO files.
19149
19150 Attributes of the form DW_FORM_rnglistx have already had
19151 their value changed by read_rnglist_index and already
19152 include DW_AT_rnglists_base, so don't need to add the ranges
19153 base, either. */
19154 int need_ranges_base = (tag != DW_TAG_compile_unit
19155 && attr.form != DW_FORM_rnglistx);
529908cb
TT
19156 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19157 but that requires a full DIE, so instead we just
19158 reimplement it. */
19159 unsigned int ranges_offset = (attr.constant_value (0)
05caa1d2
TT
19160 + (need_ranges_base
19161 ? cu->ranges_base
19162 : 0));
19163
19164 /* Value of the DW_AT_ranges attribute is the offset in the
19165 .debug_ranges section. */
19166 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19167 nullptr, tag))
05caa1d2
TT
19168 has_pc_info = 1;
19169 }
19170 break;
19171
c906108c
SS
19172 default:
19173 break;
19174 }
19175 }
19176
10d06d82
TT
19177 /* For Ada, if both the name and the linkage name appear, we prefer
19178 the latter. This lets "catch exception" work better, regardless
19179 of the order in which the name and linkage name were emitted.
19180 Really, though, this is just a workaround for the fact that gdb
19181 doesn't store both the name and the linkage name. */
19182 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19183 raw_name = linkage_name;
10d06d82 19184
91da1414 19185 if (high_pc_relative)
48fbe735 19186 highpc += lowpc;
91da1414 19187
9373cf26
JK
19188 if (has_low_pc_attr && has_high_pc_attr)
19189 {
19190 /* When using the GNU linker, .gnu.linkonce. sections are used to
19191 eliminate duplicate copies of functions and vtables and such.
19192 The linker will arbitrarily choose one and discard the others.
19193 The AT_*_pc values for such functions refer to local labels in
19194 these sections. If the section from that file was discarded, the
19195 labels are not in the output, so the relocs get a value of 0.
19196 If this is a discarded function, mark the pc bounds as invalid,
19197 so that GDB will ignore it. */
976ca316 19198 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19199 {
976ca316 19200 struct objfile *objfile = per_objfile->objfile;
08feed99 19201 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19202
b98664d3 19203 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19204 "for DIE at %s [in module %s]"),
48fbe735
YQ
19205 paddress (gdbarch, lowpc),
19206 sect_offset_str (sect_off),
9d8780f0 19207 objfile_name (objfile));
9373cf26
JK
19208 }
19209 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19210 else if (lowpc >= highpc)
9373cf26 19211 {
976ca316 19212 struct objfile *objfile = per_objfile->objfile;
08feed99 19213 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19214
b98664d3 19215 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19216 "for DIE at %s [in module %s]"),
48fbe735
YQ
19217 paddress (gdbarch, lowpc),
19218 paddress (gdbarch, highpc),
19219 sect_offset_str (sect_off),
9c541725 19220 objfile_name (objfile));
9373cf26
JK
19221 }
19222 else
48fbe735 19223 has_pc_info = 1;
9373cf26 19224 }
85cbf3d3 19225
c906108c
SS
19226 return info_ptr;
19227}
19228
72bf9492
DJ
19229/* Find a cached partial DIE at OFFSET in CU. */
19230
d590ff25
YQ
19231struct partial_die_info *
19232dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19233{
19234 struct partial_die_info *lookup_die = NULL;
6f06d47b 19235 struct partial_die_info part_die (sect_off);
72bf9492 19236
9a3c8263 19237 lookup_die = ((struct partial_die_info *)
d590ff25 19238 htab_find_with_hash (partial_dies, &part_die,
9c541725 19239 to_underlying (sect_off)));
72bf9492 19240
72bf9492
DJ
19241 return lookup_die;
19242}
19243
348e048f
DE
19244/* Find a partial DIE at OFFSET, which may or may not be in CU,
19245 except in the case of .debug_types DIEs which do not reference
19246 outside their CU (they do however referencing other types via
55f1336d 19247 DW_FORM_ref_sig8). */
72bf9492 19248
122cf0f2 19249static const struct cu_partial_die_info
9c541725 19250find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19251{
976ca316
SM
19252 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19253 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19254 struct partial_die_info *pd = NULL;
72bf9492 19255
36586728 19256 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19257 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19258 {
d590ff25 19259 pd = cu->find_partial_die (sect_off);
5afb4e99 19260 if (pd != NULL)
fb816e8b 19261 return { cu, pd };
0d99eb77
DE
19262 /* We missed recording what we needed.
19263 Load all dies and try again. */
5afb4e99 19264 }
0d99eb77
DE
19265 else
19266 {
19267 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19268 if (cu->per_cu->is_debug_types)
0d99eb77 19269 {
9d8780f0
SM
19270 error (_("Dwarf Error: Type Unit at offset %s contains"
19271 " external reference to offset %s [in module %s].\n"),
19272 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19273 bfd_get_filename (objfile->obfd));
19274 }
7188ed02
SM
19275 dwarf2_per_cu_data *per_cu
19276 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19277 per_objfile);
72bf9492 19278
976ca316 19279 cu = per_objfile->get_cu (per_cu);
7188ed02 19280 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19281 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19282
976ca316 19283 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19284
19285 cu->last_used = 0;
19286 pd = cu->find_partial_die (sect_off);
0d99eb77 19287 }
5afb4e99 19288
dee91e82
DE
19289 /* If we didn't find it, and not all dies have been loaded,
19290 load them all and try again. */
19291
7188ed02 19292 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19293 {
7188ed02 19294 cu->per_cu->load_all_dies = 1;
fd820528
DE
19295
19296 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19297 THIS_CU->cu may already be in use. So we can't just free it and
19298 replace its DIEs with the ones we read in. Instead, we leave those
19299 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19300 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19301 set. */
976ca316 19302 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19303
7188ed02 19304 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19305 }
19306
19307 if (pd == NULL)
521894aa 19308 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19309 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19310 return { cu, pd };
72bf9492
DJ
19311}
19312
abc72ce4
DE
19313/* See if we can figure out if the class lives in a namespace. We do
19314 this by looking for a member function; its demangled name will
19315 contain namespace info, if there is any. */
19316
19317static void
19318guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19319 struct dwarf2_cu *cu)
19320{
19321 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19322 what template types look like, because the demangler
19323 frequently doesn't give the same name as the debug info. We
19324 could fix this by only using the demangled name to get the
19325 prefix (but see comment in read_structure_type). */
19326
19327 struct partial_die_info *real_pdi;
19328 struct partial_die_info *child_pdi;
19329
19330 /* If this DIE (this DIE's specification, if any) has a parent, then
19331 we should not do this. We'll prepend the parent's fully qualified
19332 name when we create the partial symbol. */
19333
19334 real_pdi = struct_pdi;
19335 while (real_pdi->has_specification)
fb816e8b 19336 {
122cf0f2
AB
19337 auto res = find_partial_die (real_pdi->spec_offset,
19338 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19339 real_pdi = res.pdi;
19340 cu = res.cu;
19341 }
abc72ce4
DE
19342
19343 if (real_pdi->die_parent != NULL)
19344 return;
19345
19346 for (child_pdi = struct_pdi->die_child;
19347 child_pdi != NULL;
19348 child_pdi = child_pdi->die_sibling)
19349 {
19350 if (child_pdi->tag == DW_TAG_subprogram
19351 && child_pdi->linkage_name != NULL)
19352 {
43816ebc 19353 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19354 (cu->language_defn->class_name_from_physname
19355 (child_pdi->linkage_name));
abc72ce4
DE
19356 if (actual_class_name != NULL)
19357 {
5e22e966 19358 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19359 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19360 struct_pdi->canonical_name = 1;
abc72ce4
DE
19361 }
19362 break;
19363 }
19364 }
19365}
19366
25c11aca
TV
19367/* Return true if a DIE with TAG may have the DW_AT_const_value
19368 attribute. */
19369
19370static bool
19371can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19372{
19373 switch (tag)
19374 {
19375 case DW_TAG_constant:
19376 case DW_TAG_enumerator:
19377 case DW_TAG_formal_parameter:
19378 case DW_TAG_template_value_param:
19379 case DW_TAG_variable:
19380 return true;
19381 }
19382
19383 return false;
19384}
19385
52356b79
YQ
19386void
19387partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19388{
abc72ce4
DE
19389 /* Once we've fixed up a die, there's no point in doing so again.
19390 This also avoids a memory leak if we were to call
19391 guess_partial_die_structure_name multiple times. */
52356b79 19392 if (fixup_called)
abc72ce4
DE
19393 return;
19394
72bf9492
DJ
19395 /* If we found a reference attribute and the DIE has no name, try
19396 to find a name in the referred to DIE. */
19397
7d00ffec 19398 if (raw_name == NULL && has_specification)
72bf9492
DJ
19399 {
19400 struct partial_die_info *spec_die;
72bf9492 19401
122cf0f2 19402 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19403 spec_die = res.pdi;
19404 cu = res.cu;
72bf9492 19405
52356b79 19406 spec_die->fixup (cu);
72bf9492 19407
7d00ffec 19408 if (spec_die->raw_name)
72bf9492 19409 {
7d00ffec
TT
19410 raw_name = spec_die->raw_name;
19411 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19412
19413 /* Copy DW_AT_external attribute if it is set. */
19414 if (spec_die->is_external)
52356b79 19415 is_external = spec_die->is_external;
72bf9492
DJ
19416 }
19417 }
19418
25c11aca
TV
19419 if (!has_const_value && has_specification
19420 && can_have_DW_AT_const_value_p (tag))
19421 {
19422 struct partial_die_info *spec_die;
19423
19424 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19425 spec_die = res.pdi;
19426 cu = res.cu;
19427
19428 spec_die->fixup (cu);
19429
19430 if (spec_die->has_const_value)
19431 {
19432 /* Copy DW_AT_const_value attribute if it is set. */
19433 has_const_value = spec_die->has_const_value;
19434 }
19435 }
19436
72bf9492 19437 /* Set default names for some unnamed DIEs. */
72bf9492 19438
7d00ffec
TT
19439 if (raw_name == NULL && tag == DW_TAG_namespace)
19440 {
19441 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19442 canonical_name = 1;
19443 }
72bf9492 19444
abc72ce4
DE
19445 /* If there is no parent die to provide a namespace, and there are
19446 children, see if we can determine the namespace from their linkage
122d1940 19447 name. */
abc72ce4 19448 if (cu->language == language_cplus
5e22e966 19449 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19450 && die_parent == NULL
19451 && has_children
19452 && (tag == DW_TAG_class_type
19453 || tag == DW_TAG_structure_type
19454 || tag == DW_TAG_union_type))
19455 guess_partial_die_structure_name (this, cu);
abc72ce4 19456
53832f31
TT
19457 /* GCC might emit a nameless struct or union that has a linkage
19458 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19459 if (raw_name == NULL
52356b79
YQ
19460 && (tag == DW_TAG_class_type
19461 || tag == DW_TAG_interface_type
19462 || tag == DW_TAG_structure_type
19463 || tag == DW_TAG_union_type)
19464 && linkage_name != NULL)
53832f31 19465 {
43816ebc
TT
19466 gdb::unique_xmalloc_ptr<char> demangled
19467 (gdb_demangle (linkage_name, DMGL_TYPES));
19468 if (demangled != nullptr)
53832f31 19469 {
96408a79
SA
19470 const char *base;
19471
19472 /* Strip any leading namespaces/classes, keep only the base name.
19473 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19474 base = strrchr (demangled.get (), ':');
19475 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19476 base++;
19477 else
43816ebc 19478 base = demangled.get ();
96408a79 19479
5e22e966 19480 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19481 raw_name = objfile->intern (base);
19482 canonical_name = 1;
53832f31
TT
19483 }
19484 }
19485
52356b79 19486 fixup_called = 1;
72bf9492
DJ
19487}
19488
d0ce17d8
CT
19489/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19490 contents from the given SECTION in the HEADER. */
41144253 19491static void
d0ce17d8
CT
19492read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19493 struct dwarf2_section_info *section)
41144253 19494{
19495 unsigned int bytes_read;
19496 bfd *abfd = section->get_bfd_owner ();
19497 const gdb_byte *info_ptr = section->buffer;
19498 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19499 info_ptr += bytes_read;
19500 header->version = read_2_bytes (abfd, info_ptr);
19501 info_ptr += 2;
19502 header->addr_size = read_1_byte (abfd, info_ptr);
19503 info_ptr += 1;
19504 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19505 info_ptr += 1;
19506 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19507}
19508
19509/* Return the DW_AT_loclists_base value for the CU. */
19510static ULONGEST
19511lookup_loclist_base (struct dwarf2_cu *cu)
19512{
19513 /* For the .dwo unit, the loclist_base points to the first offset following
19514 the header. The header consists of the following entities-
19515 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19516 bit format)
19517 2. version (2 bytes)
19518 3. address size (1 byte)
19519 4. segment selector size (1 byte)
19520 5. offset entry count (4 bytes)
19521 These sizes are derived as per the DWARFv5 standard. */
19522 if (cu->dwo_unit != nullptr)
19523 {
19524 if (cu->header.initial_length_size == 4)
19525 return LOCLIST_HEADER_SIZE32;
19526 return LOCLIST_HEADER_SIZE64;
19527 }
19528 return cu->loclist_base;
19529}
19530
19531/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19532 array of offsets in the .debug_loclists section. */
19533static CORE_ADDR
19534read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19535{
976ca316
SM
19536 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19537 struct objfile *objfile = per_objfile->objfile;
41144253 19538 bfd *abfd = objfile->obfd;
19539 ULONGEST loclist_base = lookup_loclist_base (cu);
19540 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19541
19542 section->read (objfile);
19543 if (section->buffer == NULL)
19544 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19545 "section [in module %s]"), objfile_name (objfile));
d0ce17d8
CT
19546 struct loclists_rnglists_header header;
19547 read_loclists_rnglists_header (&header, section);
41144253 19548 if (loclist_index >= header.offset_entry_count)
19549 complaint (_("DW_FORM_loclistx pointing outside of "
19550 ".debug_loclists offset array [in module %s]"),
19551 objfile_name (objfile));
19552 if (loclist_base + loclist_index * cu->header.offset_size
19553 >= section->size)
19554 complaint (_("DW_FORM_loclistx pointing outside of "
19555 ".debug_loclists section [in module %s]"),
19556 objfile_name (objfile));
19557 const gdb_byte *info_ptr
19558 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19559
19560 if (cu->header.offset_size == 4)
19561 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19562 else
19563 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19564}
19565
d0ce17d8
CT
19566/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19567 array of offsets in the .debug_rnglists section. */
19568static CORE_ADDR
19569read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19570 dwarf_tag tag)
19571{
19572 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19573 struct objfile *objfile = dwarf2_per_objfile->objfile;
19574 bfd *abfd = objfile->obfd;
19575 ULONGEST rnglist_header_size =
19576 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19577 : RNGLIST_HEADER_SIZE64);
19578 ULONGEST rnglist_base =
19579 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19580 ULONGEST start_offset =
19581 rnglist_base + rnglist_index * cu->header.offset_size;
19582
19583 /* Get rnglists section. */
19584 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19585
19586 /* Read the rnglists section content. */
19587 section->read (objfile);
19588 if (section->buffer == nullptr)
19589 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19590 "[in module %s]"),
19591 objfile_name (objfile));
19592
19593 /* Verify the rnglist index is valid. */
19594 struct loclists_rnglists_header header;
19595 read_loclists_rnglists_header (&header, section);
19596 if (rnglist_index >= header.offset_entry_count)
19597 error (_("DW_FORM_rnglistx index pointing outside of "
19598 ".debug_rnglists offset array [in module %s]"),
19599 objfile_name (objfile));
19600
19601 /* Validate that the offset is within the section's range. */
19602 if (start_offset >= section->size)
19603 error (_("DW_FORM_rnglistx pointing outside of "
19604 ".debug_rnglists section [in module %s]"),
19605 objfile_name (objfile));
19606
19607 /* Validate that reading won't go beyond the end of the section. */
19608 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19609 error (_("Reading DW_FORM_rnglistx index beyond end of"
19610 ".debug_rnglists section [in module %s]"),
19611 objfile_name (objfile));
19612
19613 const gdb_byte *info_ptr = section->buffer + start_offset;
19614
19615 if (cu->header.offset_size == 4)
19616 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19617 else
19618 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19619}
19620
18a8505e
AT
19621/* Process the attributes that had to be skipped in the first round. These
19622 attributes are the ones that need str_offsets_base or addr_base attributes.
19623 They could not have been processed in the first round, because at the time
19624 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19625static void
19626read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19627 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19628{
19629 struct dwarf2_cu *cu = reader->cu;
19630 switch (attr->form)
19631 {
19632 case DW_FORM_addrx:
19633 case DW_FORM_GNU_addr_index:
36d378cf
TT
19634 attr->set_address (read_addr_index (cu,
19635 attr->as_unsigned_reprocess ()));
18a8505e 19636 break;
41144253 19637 case DW_FORM_loclistx:
529908cb 19638 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
41144253 19639 break;
d0ce17d8 19640 case DW_FORM_rnglistx:
529908cb 19641 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
d0ce17d8 19642 break;
18a8505e
AT
19643 case DW_FORM_strx:
19644 case DW_FORM_strx1:
19645 case DW_FORM_strx2:
19646 case DW_FORM_strx3:
19647 case DW_FORM_strx4:
19648 case DW_FORM_GNU_str_index:
19649 {
fe56917a 19650 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19651 gdb_assert (!attr->canonical_string_p ());
18a8505e 19652 if (reader->dwo_file != NULL)
c6481205
TT
19653 attr->set_string_noncanonical (read_dwo_str_index (reader,
19654 str_index));
18a8505e 19655 else
c6481205
TT
19656 attr->set_string_noncanonical (read_stub_str_index (cu,
19657 str_index));
18a8505e
AT
19658 break;
19659 }
19660 default:
19661 gdb_assert_not_reached (_("Unexpected DWARF form."));
19662 }
19663}
19664
a8329558 19665/* Read an attribute value described by an attribute form. */
c906108c 19666
d521ce57 19667static const gdb_byte *
dee91e82
DE
19668read_attribute_value (const struct die_reader_specs *reader,
19669 struct attribute *attr, unsigned form,
7a5f294d 19670 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19671{
dee91e82 19672 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19673 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19674 struct objfile *objfile = per_objfile->objfile;
dee91e82 19675 bfd *abfd = reader->abfd;
e7c27a73 19676 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19677 unsigned int bytes_read;
19678 struct dwarf_block *blk;
19679
aead7601 19680 attr->form = (enum dwarf_form) form;
a8329558 19681 switch (form)
c906108c 19682 {
c906108c 19683 case DW_FORM_ref_addr:
ae411497 19684 if (cu->header.version == 2)
414ad644
TT
19685 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
19686 &bytes_read));
ae411497 19687 else
414ad644
TT
19688 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19689 &bytes_read));
ae411497
TT
19690 info_ptr += bytes_read;
19691 break;
36586728 19692 case DW_FORM_GNU_ref_alt:
414ad644
TT
19693 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19694 &bytes_read));
36586728
TT
19695 info_ptr += bytes_read;
19696 break;
ae411497 19697 case DW_FORM_addr:
08feed99
TT
19698 {
19699 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
19700 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
19701 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19702 attr->set_address (addr);
08feed99
TT
19703 info_ptr += bytes_read;
19704 }
c906108c
SS
19705 break;
19706 case DW_FORM_block2:
7b5a2f43 19707 blk = dwarf_alloc_block (cu);
c906108c
SS
19708 blk->size = read_2_bytes (abfd, info_ptr);
19709 info_ptr += 2;
19710 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19711 info_ptr += blk->size;
9d2246fc 19712 attr->set_block (blk);
c906108c
SS
19713 break;
19714 case DW_FORM_block4:
7b5a2f43 19715 blk = dwarf_alloc_block (cu);
c906108c
SS
19716 blk->size = read_4_bytes (abfd, info_ptr);
19717 info_ptr += 4;
19718 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19719 info_ptr += blk->size;
9d2246fc 19720 attr->set_block (blk);
c906108c
SS
19721 break;
19722 case DW_FORM_data2:
414ad644 19723 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19724 info_ptr += 2;
19725 break;
19726 case DW_FORM_data4:
414ad644 19727 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19728 info_ptr += 4;
19729 break;
19730 case DW_FORM_data8:
414ad644 19731 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19732 info_ptr += 8;
19733 break;
0224619f
JK
19734 case DW_FORM_data16:
19735 blk = dwarf_alloc_block (cu);
19736 blk->size = 16;
19737 blk->data = read_n_bytes (abfd, info_ptr, 16);
19738 info_ptr += 16;
9d2246fc 19739 attr->set_block (blk);
0224619f 19740 break;
2dc7f7b3 19741 case DW_FORM_sec_offset:
414ad644
TT
19742 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19743 &bytes_read));
2dc7f7b3
TT
19744 info_ptr += bytes_read;
19745 break;
41144253 19746 case DW_FORM_loclistx:
19747 {
7a5f294d
TT
19748 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19749 &bytes_read));
19750 info_ptr += bytes_read;
41144253 19751 }
19752 break;
c906108c 19753 case DW_FORM_string:
c6481205
TT
19754 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19755 &bytes_read));
c906108c
SS
19756 info_ptr += bytes_read;
19757 break;
4bdf3d34 19758 case DW_FORM_strp:
36586728
TT
19759 if (!cu->per_cu->is_dwz)
19760 {
c6481205
TT
19761 attr->set_string_noncanonical
19762 (read_indirect_string (per_objfile,
19763 abfd, info_ptr, cu_header,
19764 &bytes_read));
36586728
TT
19765 info_ptr += bytes_read;
19766 break;
19767 }
19768 /* FALLTHROUGH */
43988095
JK
19769 case DW_FORM_line_strp:
19770 if (!cu->per_cu->is_dwz)
19771 {
c6481205
TT
19772 attr->set_string_noncanonical
19773 (per_objfile->read_line_string (info_ptr, cu_header,
19774 &bytes_read));
43988095
JK
19775 info_ptr += bytes_read;
19776 break;
19777 }
19778 /* FALLTHROUGH */
36586728
TT
19779 case DW_FORM_GNU_strp_alt:
19780 {
976ca316 19781 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19782 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19783 &bytes_read);
36586728 19784
c6481205
TT
19785 attr->set_string_noncanonical
19786 (dwz->read_string (objfile, str_offset));
36586728
TT
19787 info_ptr += bytes_read;
19788 }
4bdf3d34 19789 break;
2dc7f7b3 19790 case DW_FORM_exprloc:
c906108c 19791 case DW_FORM_block:
7b5a2f43 19792 blk = dwarf_alloc_block (cu);
c906108c
SS
19793 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19794 info_ptr += bytes_read;
19795 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19796 info_ptr += blk->size;
9d2246fc 19797 attr->set_block (blk);
c906108c
SS
19798 break;
19799 case DW_FORM_block1:
7b5a2f43 19800 blk = dwarf_alloc_block (cu);
c906108c
SS
19801 blk->size = read_1_byte (abfd, info_ptr);
19802 info_ptr += 1;
19803 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19804 info_ptr += blk->size;
9d2246fc 19805 attr->set_block (blk);
c906108c
SS
19806 break;
19807 case DW_FORM_data1:
c906108c 19808 case DW_FORM_flag:
414ad644 19809 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
19810 info_ptr += 1;
19811 break;
2dc7f7b3 19812 case DW_FORM_flag_present:
414ad644 19813 attr->set_unsigned (1);
2dc7f7b3 19814 break;
c906108c 19815 case DW_FORM_sdata:
1bc397c5 19816 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19817 info_ptr += bytes_read;
19818 break;
18a8505e 19819 case DW_FORM_rnglistx:
7a5f294d
TT
19820 {
19821 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19822 &bytes_read));
19823 info_ptr += bytes_read;
19824 }
19825 break;
d0ce17d8 19826 case DW_FORM_udata:
414ad644 19827 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19828 info_ptr += bytes_read;
19829 break;
19830 case DW_FORM_ref1:
414ad644
TT
19831 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19832 + read_1_byte (abfd, info_ptr)));
c906108c
SS
19833 info_ptr += 1;
19834 break;
19835 case DW_FORM_ref2:
414ad644
TT
19836 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19837 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
19838 info_ptr += 2;
19839 break;
19840 case DW_FORM_ref4:
414ad644
TT
19841 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19842 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
19843 info_ptr += 4;
19844 break;
613e1657 19845 case DW_FORM_ref8:
414ad644
TT
19846 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19847 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
19848 info_ptr += 8;
19849 break;
55f1336d 19850 case DW_FORM_ref_sig8:
630ed6b9 19851 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
19852 info_ptr += 8;
19853 break;
c906108c 19854 case DW_FORM_ref_udata:
414ad644
TT
19855 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19856 + read_unsigned_leb128 (abfd, info_ptr,
19857 &bytes_read)));
c906108c
SS
19858 info_ptr += bytes_read;
19859 break;
c906108c 19860 case DW_FORM_indirect:
a8329558
KW
19861 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19862 info_ptr += bytes_read;
43988095
JK
19863 if (form == DW_FORM_implicit_const)
19864 {
19865 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19866 info_ptr += bytes_read;
19867 }
19868 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 19869 info_ptr);
43988095
JK
19870 break;
19871 case DW_FORM_implicit_const:
1bc397c5 19872 attr->set_signed (implicit_const);
a8329558 19873 break;
336d760d 19874 case DW_FORM_addrx:
3019eac3 19875 case DW_FORM_GNU_addr_index:
fe56917a
TT
19876 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19877 &bytes_read));
3019eac3
DE
19878 info_ptr += bytes_read;
19879 break;
cf532bd1 19880 case DW_FORM_strx:
15f18d14
AT
19881 case DW_FORM_strx1:
19882 case DW_FORM_strx2:
19883 case DW_FORM_strx3:
19884 case DW_FORM_strx4:
3019eac3 19885 case DW_FORM_GNU_str_index:
3019eac3 19886 {
15f18d14
AT
19887 ULONGEST str_index;
19888 if (form == DW_FORM_strx1)
19889 {
19890 str_index = read_1_byte (abfd, info_ptr);
19891 info_ptr += 1;
19892 }
19893 else if (form == DW_FORM_strx2)
19894 {
19895 str_index = read_2_bytes (abfd, info_ptr);
19896 info_ptr += 2;
19897 }
19898 else if (form == DW_FORM_strx3)
19899 {
19900 str_index = read_3_bytes (abfd, info_ptr);
19901 info_ptr += 3;
19902 }
19903 else if (form == DW_FORM_strx4)
19904 {
19905 str_index = read_4_bytes (abfd, info_ptr);
19906 info_ptr += 4;
19907 }
19908 else
19909 {
19910 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19911 info_ptr += bytes_read;
19912 }
fe56917a 19913 attr->set_unsigned_reprocess (str_index);
7a5f294d 19914 }
3019eac3 19915 break;
c906108c 19916 default:
8a3fe4f8 19917 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19918 dwarf_form_name (form),
19919 bfd_get_filename (abfd));
c906108c 19920 }
28e94949 19921
36586728 19922 /* Super hack. */
cd6c91b4 19923 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19924 attr->form = DW_FORM_GNU_ref_alt;
19925
28e94949
JB
19926 /* We have seen instances where the compiler tried to emit a byte
19927 size attribute of -1 which ended up being encoded as an unsigned
19928 0xffffffff. Although 0xffffffff is technically a valid size value,
19929 an object of this size seems pretty unlikely so we can relatively
19930 safely treat these cases as if the size attribute was invalid and
19931 treat them as zero by default. */
19932 if (attr->name == DW_AT_byte_size
19933 && form == DW_FORM_data4
529908cb 19934 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
19935 {
19936 complaint
b98664d3 19937 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
529908cb 19938 hex_string (attr->as_unsigned ()));
414ad644 19939 attr->set_unsigned (0);
01c66ae6 19940 }
28e94949 19941
c906108c
SS
19942 return info_ptr;
19943}
19944
a8329558
KW
19945/* Read an attribute described by an abbreviated attribute. */
19946
d521ce57 19947static const gdb_byte *
dee91e82
DE
19948read_attribute (const struct die_reader_specs *reader,
19949 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 19950 const gdb_byte *info_ptr)
a8329558
KW
19951{
19952 attr->name = abbrev->name;
c6481205 19953 attr->string_is_canonical = 0;
fe56917a 19954 attr->requires_reprocessing = 0;
43988095 19955 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 19956 abbrev->implicit_const, info_ptr);
a8329558
KW
19957}
19958
43988095
JK
19959/* Return pointer to string at .debug_str offset STR_OFFSET. */
19960
19961static const char *
976ca316 19962read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19963 LONGEST str_offset)
43988095 19964{
976ca316
SM
19965 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19966 str_offset, "DW_FORM_strp");
c906108c
SS
19967}
19968
43988095
JK
19969/* Return pointer to string at .debug_str offset as read from BUF.
19970 BUF is assumed to be in a compilation unit described by CU_HEADER.
19971 Return *BYTES_READ_PTR count of bytes read from BUF. */
19972
d521ce57 19973static const char *
976ca316 19974read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19975 const gdb_byte *buf,
cf2c3c16
TT
19976 const struct comp_unit_head *cu_header,
19977 unsigned int *bytes_read_ptr)
19978{
8266302d 19979 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19980
976ca316 19981 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19982}
19983
86c0bb4c 19984/* See read.h. */
43988095 19985
86c0bb4c
TT
19986const char *
19987dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19988 const struct comp_unit_head *cu_header,
19989 unsigned int *bytes_read_ptr)
43988095 19990{
86c0bb4c 19991 bfd *abfd = objfile->obfd;
8266302d 19992 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19993
5989a64e 19994 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19995}
19996
3019eac3 19997/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19998 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19999 ADDR_SIZE is the size of addresses from the CU header. */
20000
20001static CORE_ADDR
976ca316
SM
20002read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20003 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20004{
976ca316 20005 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20006 bfd *abfd = objfile->obfd;
20007 const gdb_byte *info_ptr;
18a8505e 20008 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20009
976ca316
SM
20010 per_objfile->per_bfd->addr.read (objfile);
20011 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20012 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20013 objfile_name (objfile));
18a8505e 20014 if (addr_base_or_zero + addr_index * addr_size
976ca316 20015 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20016 error (_("DW_FORM_addr_index pointing outside of "
20017 ".debug_addr section [in module %s]"),
4262abfb 20018 objfile_name (objfile));
976ca316
SM
20019 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20020 + addr_index * addr_size);
3019eac3
DE
20021 if (addr_size == 4)
20022 return bfd_get_32 (abfd, info_ptr);
20023 else
20024 return bfd_get_64 (abfd, info_ptr);
20025}
20026
20027/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20028
20029static CORE_ADDR
20030read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20031{
5e22e966 20032 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20033 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20034}
20035
20036/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20037
20038static CORE_ADDR
d521ce57 20039read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20040 unsigned int *bytes_read)
20041{
5e22e966 20042 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20043 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20044
20045 return read_addr_index (cu, addr_index);
20046}
20047
450a1bfc 20048/* See read.h. */
3019eac3
DE
20049
20050CORE_ADDR
82ca3f51 20051dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20052 dwarf2_per_objfile *per_objfile,
82ca3f51 20053 unsigned int addr_index)
3019eac3 20054{
976ca316 20055 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20056 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20057 int addr_size;
20058
3019eac3
DE
20059 /* We need addr_base and addr_size.
20060 If we don't have PER_CU->cu, we have to get it.
20061 Nasty, but the alternative is storing the needed info in PER_CU,
20062 which at this point doesn't seem justified: it's not clear how frequently
20063 it would get used and it would increase the size of every PER_CU.
20064 Entry points like dwarf2_per_cu_addr_size do a similar thing
20065 so we're not in uncharted territory here.
20066 Alas we need to be a bit more complicated as addr_base is contained
20067 in the DIE.
20068
20069 We don't need to read the entire CU(/TU).
20070 We just need the header and top level die.
a1b64ce1 20071
3019eac3 20072 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20073 For now we skip this optimization. */
3019eac3
DE
20074
20075 if (cu != NULL)
20076 {
20077 addr_base = cu->addr_base;
20078 addr_size = cu->header.addr_size;
20079 }
20080 else
20081 {
976ca316 20082 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20083 addr_base = reader.cu->addr_base;
20084 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20085 }
20086
976ca316 20087 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20088}
20089
18a8505e
AT
20090/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20091 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20092 DWO file. */
3019eac3 20093
d521ce57 20094static const char *
18a8505e
AT
20095read_str_index (struct dwarf2_cu *cu,
20096 struct dwarf2_section_info *str_section,
20097 struct dwarf2_section_info *str_offsets_section,
20098 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20099{
976ca316
SM
20100 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20101 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20102 const char *objf_name = objfile_name (objfile);
3019eac3 20103 bfd *abfd = objfile->obfd;
d521ce57 20104 const gdb_byte *info_ptr;
3019eac3 20105 ULONGEST str_offset;
cf532bd1 20106 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20107
96b79293
TT
20108 str_section->read (objfile);
20109 str_offsets_section->read (objfile);
73869dc2 20110 if (str_section->buffer == NULL)
18a8505e 20111 error (_("%s used without %s section"
9d8780f0 20112 " in CU at offset %s [in module %s]"),
96b79293 20113 form_name, str_section->get_name (),
18a8505e 20114 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20115 if (str_offsets_section->buffer == NULL)
18a8505e 20116 error (_("%s used without %s section"
9d8780f0 20117 " in CU at offset %s [in module %s]"),
96b79293 20118 form_name, str_section->get_name (),
18a8505e 20119 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20120 info_ptr = (str_offsets_section->buffer
18a8505e 20121 + str_offsets_base
3019eac3
DE
20122 + str_index * cu->header.offset_size);
20123 if (cu->header.offset_size == 4)
20124 str_offset = bfd_get_32 (abfd, info_ptr);
20125 else
20126 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20127 if (str_offset >= str_section->size)
57d63ce2 20128 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20129 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20130 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20131 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20132}
20133
18a8505e
AT
20134/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20135
20136static const char *
20137read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20138{
20139 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20140 ? reader->cu->header.addr_size : 0;
20141 return read_str_index (reader->cu,
20142 &reader->dwo_file->sections.str,
20143 &reader->dwo_file->sections.str_offsets,
20144 str_offsets_base, str_index);
20145}
20146
20147/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20148
20149static const char *
20150read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20151{
5e22e966 20152 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20153 const char *objf_name = objfile_name (objfile);
20154 static const char form_name[] = "DW_FORM_GNU_str_index";
20155 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20156
20157 if (!cu->str_offsets_base.has_value ())
20158 error (_("%s used in Fission stub without %s"
20159 " in CU at offset 0x%lx [in module %s]"),
20160 form_name, str_offsets_attr_name,
20161 (long) cu->header.offset_size, objf_name);
20162
20163 return read_str_index (cu,
5e22e966
SM
20164 &cu->per_objfile->per_bfd->str,
20165 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20166 *cu->str_offsets_base, str_index);
20167}
20168
3019eac3
DE
20169/* Return the length of an LEB128 number in BUF. */
20170
20171static int
20172leb128_size (const gdb_byte *buf)
20173{
20174 const gdb_byte *begin = buf;
20175 gdb_byte byte;
20176
20177 while (1)
20178 {
20179 byte = *buf++;
20180 if ((byte & 128) == 0)
20181 return buf - begin;
20182 }
20183}
20184
c906108c 20185static void
e142c38c 20186set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20187{
20188 switch (lang)
20189 {
20190 case DW_LANG_C89:
76bee0cc 20191 case DW_LANG_C99:
0cfd832f 20192 case DW_LANG_C11:
c906108c 20193 case DW_LANG_C:
d1be3247 20194 case DW_LANG_UPC:
e142c38c 20195 cu->language = language_c;
c906108c 20196 break;
9c37b5ae 20197 case DW_LANG_Java:
c906108c 20198 case DW_LANG_C_plus_plus:
0cfd832f
MW
20199 case DW_LANG_C_plus_plus_11:
20200 case DW_LANG_C_plus_plus_14:
e142c38c 20201 cu->language = language_cplus;
c906108c 20202 break;
6aecb9c2
JB
20203 case DW_LANG_D:
20204 cu->language = language_d;
20205 break;
c906108c
SS
20206 case DW_LANG_Fortran77:
20207 case DW_LANG_Fortran90:
b21b22e0 20208 case DW_LANG_Fortran95:
f7de9aab
MW
20209 case DW_LANG_Fortran03:
20210 case DW_LANG_Fortran08:
e142c38c 20211 cu->language = language_fortran;
c906108c 20212 break;
a766d390
DE
20213 case DW_LANG_Go:
20214 cu->language = language_go;
20215 break;
c906108c 20216 case DW_LANG_Mips_Assembler:
e142c38c 20217 cu->language = language_asm;
c906108c
SS
20218 break;
20219 case DW_LANG_Ada83:
8aaf0b47 20220 case DW_LANG_Ada95:
bc5f45f8
JB
20221 cu->language = language_ada;
20222 break;
72019c9c
GM
20223 case DW_LANG_Modula2:
20224 cu->language = language_m2;
20225 break;
fe8e67fd
PM
20226 case DW_LANG_Pascal83:
20227 cu->language = language_pascal;
20228 break;
22566fbd
DJ
20229 case DW_LANG_ObjC:
20230 cu->language = language_objc;
20231 break;
c44af4eb
TT
20232 case DW_LANG_Rust:
20233 case DW_LANG_Rust_old:
20234 cu->language = language_rust;
20235 break;
c906108c
SS
20236 case DW_LANG_Cobol74:
20237 case DW_LANG_Cobol85:
c906108c 20238 default:
e142c38c 20239 cu->language = language_minimal;
c906108c
SS
20240 break;
20241 }
e142c38c 20242 cu->language_defn = language_def (cu->language);
c906108c
SS
20243}
20244
20245/* Return the named attribute or NULL if not there. */
20246
20247static struct attribute *
e142c38c 20248dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20249{
a48e046c 20250 for (;;)
c906108c 20251 {
a48e046c
TT
20252 unsigned int i;
20253 struct attribute *spec = NULL;
20254
20255 for (i = 0; i < die->num_attrs; ++i)
20256 {
20257 if (die->attrs[i].name == name)
20258 return &die->attrs[i];
20259 if (die->attrs[i].name == DW_AT_specification
20260 || die->attrs[i].name == DW_AT_abstract_origin)
20261 spec = &die->attrs[i];
20262 }
20263
20264 if (!spec)
20265 break;
c906108c 20266
f2f0e013 20267 die = follow_die_ref (die, spec, &cu);
f2f0e013 20268 }
c5aa993b 20269
c906108c
SS
20270 return NULL;
20271}
20272
7d45c7c3
KB
20273/* Return the string associated with a string-typed attribute, or NULL if it
20274 is either not found or is of an incorrect type. */
20275
20276static const char *
20277dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20278{
20279 struct attribute *attr;
20280 const char *str = NULL;
20281
20282 attr = dwarf2_attr (die, name, cu);
20283
20284 if (attr != NULL)
20285 {
95f982e5 20286 str = attr->as_string ();
e61108c9 20287 if (str == nullptr)
b98664d3 20288 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20289 "DIE at %s in module %s"),
20290 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20291 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20292 }
20293
20294 return str;
20295}
20296
a084a2a6 20297/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20298 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20299static const char *
20300dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20301{
20302 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20303 if (dwo_name == nullptr)
20304 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20305 return dwo_name;
20306}
20307
05cf31d1
JB
20308/* Return non-zero iff the attribute NAME is defined for the given DIE,
20309 and holds a non-zero value. This function should only be used for
2dc7f7b3 20310 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20311
20312static int
20313dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20314{
20315 struct attribute *attr = dwarf2_attr (die, name, cu);
20316
c45bc3f8 20317 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20318}
20319
3ca72b44 20320static int
e142c38c 20321die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20322{
05cf31d1
JB
20323 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20324 which value is non-zero. However, we have to be careful with
20325 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20326 (via dwarf2_flag_true_p) follows this attribute. So we may
20327 end up accidently finding a declaration attribute that belongs
20328 to a different DIE referenced by the specification attribute,
20329 even though the given DIE does not have a declaration attribute. */
20330 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20331 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20332}
20333
63d06c5c 20334/* Return the die giving the specification for DIE, if there is
f2f0e013 20335 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20336 containing the return value on output. If there is no
20337 specification, but there is an abstract origin, that is
20338 returned. */
63d06c5c
DC
20339
20340static struct die_info *
f2f0e013 20341die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20342{
f2f0e013
DJ
20343 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20344 *spec_cu);
63d06c5c 20345
edb3359d
DJ
20346 if (spec_attr == NULL)
20347 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20348
63d06c5c
DC
20349 if (spec_attr == NULL)
20350 return NULL;
20351 else
f2f0e013 20352 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20353}
c906108c 20354
527f3840
JK
20355/* Stub for free_line_header to match void * callback types. */
20356
20357static void
20358free_line_header_voidp (void *arg)
20359{
9a3c8263 20360 struct line_header *lh = (struct line_header *) arg;
527f3840 20361
fff8551c 20362 delete lh;
527f3840
JK
20363}
20364
83769d0b 20365/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20366
20367static struct dwarf2_section_info *
20368get_debug_line_section (struct dwarf2_cu *cu)
20369{
20370 struct dwarf2_section_info *section;
976ca316 20371 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20372
20373 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20374 DWO file. */
20375 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20376 section = &cu->dwo_unit->dwo_file->sections.line;
20377 else if (cu->per_cu->is_dwz)
20378 {
976ca316 20379 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
20380
20381 section = &dwz->line;
20382 }
20383 else
976ca316 20384 section = &per_objfile->per_bfd->line;
36586728
TT
20385
20386 return section;
20387}
20388
debd256d 20389/* Read the statement program header starting at OFFSET in
3019eac3 20390 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20391 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20392 Returns NULL if there is a problem reading the header, e.g., if it
20393 has a version we don't understand.
debd256d
JB
20394
20395 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20396 the returned object point into the dwarf line section buffer,
20397 and must not be freed. */
ae2de4f8 20398
fff8551c 20399static line_header_up
9c541725 20400dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20401{
3019eac3 20402 struct dwarf2_section_info *section;
976ca316 20403 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20404
36586728 20405 section = get_debug_line_section (cu);
976ca316 20406 section->read (per_objfile->objfile);
3019eac3 20407 if (section->buffer == NULL)
debd256d 20408 {
3019eac3 20409 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20410 complaint (_("missing .debug_line.dwo section"));
3019eac3 20411 else
b98664d3 20412 complaint (_("missing .debug_line section"));
debd256d
JB
20413 return 0;
20414 }
20415
0df7ad3a 20416 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20417 per_objfile, section, &cu->header);
debd256d 20418}
c906108c 20419
c6da4cef 20420/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20421 Return the file name of the psymtab for the given file_entry.
c6da4cef 20422 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20423 If space for the result is malloc'd, *NAME_HOLDER will be set.
20424 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20425
d521ce57 20426static const char *
7ba99d21 20427psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20428 const dwarf2_psymtab *pst,
c89b44cd
TT
20429 const char *comp_dir,
20430 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20431{
d521ce57
TT
20432 const char *include_name = fe.name;
20433 const char *include_name_to_compare = include_name;
72b9f47f 20434 const char *pst_filename;
c6da4cef
DE
20435 int file_is_pst;
20436
8c43009f 20437 const char *dir_name = fe.include_dir (lh);
c6da4cef 20438
c89b44cd 20439 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20440 if (!IS_ABSOLUTE_PATH (include_name)
20441 && (dir_name != NULL || comp_dir != NULL))
20442 {
20443 /* Avoid creating a duplicate psymtab for PST.
20444 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20445 Before we do the comparison, however, we need to account
20446 for DIR_NAME and COMP_DIR.
20447 First prepend dir_name (if non-NULL). If we still don't
20448 have an absolute path prepend comp_dir (if non-NULL).
20449 However, the directory we record in the include-file's
20450 psymtab does not contain COMP_DIR (to match the
20451 corresponding symtab(s)).
20452
20453 Example:
20454
20455 bash$ cd /tmp
20456 bash$ gcc -g ./hello.c
20457 include_name = "hello.c"
20458 dir_name = "."
20459 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20460 DW_AT_name = "./hello.c"
20461
20462 */
c6da4cef
DE
20463
20464 if (dir_name != NULL)
20465 {
c89b44cd
TT
20466 name_holder->reset (concat (dir_name, SLASH_STRING,
20467 include_name, (char *) NULL));
20468 include_name = name_holder->get ();
c6da4cef 20469 include_name_to_compare = include_name;
c6da4cef
DE
20470 }
20471 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20472 {
c89b44cd
TT
20473 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20474 include_name, (char *) NULL));
20475 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20476 }
20477 }
20478
20479 pst_filename = pst->filename;
c89b44cd 20480 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20481 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20482 {
c89b44cd
TT
20483 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20484 pst_filename, (char *) NULL));
20485 pst_filename = copied_name.get ();
c6da4cef
DE
20486 }
20487
1e3fad37 20488 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20489
c6da4cef
DE
20490 if (file_is_pst)
20491 return NULL;
20492 return include_name;
20493}
20494
d9b3de22
DE
20495/* State machine to track the state of the line number program. */
20496
6f77053d 20497class lnp_state_machine
d9b3de22 20498{
6f77053d
PA
20499public:
20500 /* Initialize a machine state for the start of a line number
20501 program. */
804d2729
TT
20502 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20503 bool record_lines_p);
6f77053d 20504
8c43009f
PA
20505 file_entry *current_file ()
20506 {
20507 /* lh->file_names is 0-based, but the file name numbers in the
20508 statement program are 1-based. */
6f77053d
PA
20509 return m_line_header->file_name_at (m_file);
20510 }
20511
20512 /* Record the line in the state machine. END_SEQUENCE is true if
20513 we're processing the end of a sequence. */
20514 void record_line (bool end_sequence);
20515
a8caed5d 20516 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20517 nop-out rest of the lines in this sequence. */
6f77053d
PA
20518 void check_line_address (struct dwarf2_cu *cu,
20519 const gdb_byte *line_ptr,
7ab6656f 20520 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20521
20522 void handle_set_discriminator (unsigned int discriminator)
20523 {
20524 m_discriminator = discriminator;
20525 m_line_has_non_zero_discriminator |= discriminator != 0;
20526 }
20527
20528 /* Handle DW_LNE_set_address. */
20529 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20530 {
20531 m_op_index = 0;
20532 address += baseaddr;
20533 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20534 }
20535
20536 /* Handle DW_LNS_advance_pc. */
20537 void handle_advance_pc (CORE_ADDR adjust);
20538
20539 /* Handle a special opcode. */
20540 void handle_special_opcode (unsigned char op_code);
20541
20542 /* Handle DW_LNS_advance_line. */
20543 void handle_advance_line (int line_delta)
20544 {
20545 advance_line (line_delta);
20546 }
20547
20548 /* Handle DW_LNS_set_file. */
20549 void handle_set_file (file_name_index file);
20550
20551 /* Handle DW_LNS_negate_stmt. */
20552 void handle_negate_stmt ()
20553 {
20554 m_is_stmt = !m_is_stmt;
20555 }
20556
20557 /* Handle DW_LNS_const_add_pc. */
20558 void handle_const_add_pc ();
20559
20560 /* Handle DW_LNS_fixed_advance_pc. */
20561 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20562 {
20563 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20564 m_op_index = 0;
20565 }
20566
20567 /* Handle DW_LNS_copy. */
20568 void handle_copy ()
20569 {
20570 record_line (false);
20571 m_discriminator = 0;
20572 }
20573
20574 /* Handle DW_LNE_end_sequence. */
20575 void handle_end_sequence ()
20576 {
804d2729 20577 m_currently_recording_lines = true;
6f77053d
PA
20578 }
20579
20580private:
20581 /* Advance the line by LINE_DELTA. */
20582 void advance_line (int line_delta)
20583 {
20584 m_line += line_delta;
20585
20586 if (line_delta != 0)
20587 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20588 }
20589
804d2729
TT
20590 struct dwarf2_cu *m_cu;
20591
6f77053d
PA
20592 gdbarch *m_gdbarch;
20593
20594 /* True if we're recording lines.
20595 Otherwise we're building partial symtabs and are just interested in
20596 finding include files mentioned by the line number program. */
20597 bool m_record_lines_p;
20598
8c43009f 20599 /* The line number header. */
6f77053d 20600 line_header *m_line_header;
8c43009f 20601
6f77053d
PA
20602 /* These are part of the standard DWARF line number state machine,
20603 and initialized according to the DWARF spec. */
d9b3de22 20604
6f77053d 20605 unsigned char m_op_index = 0;
7ba99d21
AT
20606 /* The line table index of the current file. */
20607 file_name_index m_file = 1;
6f77053d
PA
20608 unsigned int m_line = 1;
20609
20610 /* These are initialized in the constructor. */
20611
20612 CORE_ADDR m_address;
20613 bool m_is_stmt;
20614 unsigned int m_discriminator;
d9b3de22
DE
20615
20616 /* Additional bits of state we need to track. */
20617
20618 /* The last file that we called dwarf2_start_subfile for.
20619 This is only used for TLLs. */
6f77053d 20620 unsigned int m_last_file = 0;
d9b3de22 20621 /* The last file a line number was recorded for. */
6f77053d 20622 struct subfile *m_last_subfile = NULL;
d9b3de22 20623
1313c56e
AB
20624 /* The address of the last line entry. */
20625 CORE_ADDR m_last_address;
20626
20627 /* Set to true when a previous line at the same address (using
20628 m_last_address) had m_is_stmt true. This is reset to false when a
20629 line entry at a new address (m_address different to m_last_address) is
20630 processed. */
20631 bool m_stmt_at_address = false;
20632
804d2729
TT
20633 /* When true, record the lines we decode. */
20634 bool m_currently_recording_lines = false;
d9b3de22
DE
20635
20636 /* The last line number that was recorded, used to coalesce
20637 consecutive entries for the same line. This can happen, for
20638 example, when discriminators are present. PR 17276. */
6f77053d
PA
20639 unsigned int m_last_line = 0;
20640 bool m_line_has_non_zero_discriminator = false;
8c43009f 20641};
d9b3de22 20642
6f77053d
PA
20643void
20644lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20645{
20646 CORE_ADDR addr_adj = (((m_op_index + adjust)
20647 / m_line_header->maximum_ops_per_instruction)
20648 * m_line_header->minimum_instruction_length);
20649 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20650 m_op_index = ((m_op_index + adjust)
20651 % m_line_header->maximum_ops_per_instruction);
20652}
d9b3de22 20653
6f77053d
PA
20654void
20655lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20656{
6f77053d 20657 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20658 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20659 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20660 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20661 / m_line_header->maximum_ops_per_instruction)
20662 * m_line_header->minimum_instruction_length);
20663 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20664 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20665 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20666
258bf0ee 20667 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20668 advance_line (line_delta);
20669 record_line (false);
20670 m_discriminator = 0;
20671}
d9b3de22 20672
6f77053d
PA
20673void
20674lnp_state_machine::handle_set_file (file_name_index file)
20675{
20676 m_file = file;
20677
20678 const file_entry *fe = current_file ();
20679 if (fe == NULL)
20680 dwarf2_debug_line_missing_file_complaint ();
20681 else if (m_record_lines_p)
20682 {
20683 const char *dir = fe->include_dir (m_line_header);
20684
c24bdb02 20685 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20686 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20687 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20688 }
20689}
20690
20691void
20692lnp_state_machine::handle_const_add_pc ()
20693{
20694 CORE_ADDR adjust
20695 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20696
20697 CORE_ADDR addr_adj
20698 = (((m_op_index + adjust)
20699 / m_line_header->maximum_ops_per_instruction)
20700 * m_line_header->minimum_instruction_length);
20701
20702 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20703 m_op_index = ((m_op_index + adjust)
20704 % m_line_header->maximum_ops_per_instruction);
20705}
d9b3de22 20706
a05a36a5
DE
20707/* Return non-zero if we should add LINE to the line number table.
20708 LINE is the line to add, LAST_LINE is the last line that was added,
20709 LAST_SUBFILE is the subfile for LAST_LINE.
20710 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20711 had a non-zero discriminator.
20712
20713 We have to be careful in the presence of discriminators.
20714 E.g., for this line:
20715
20716 for (i = 0; i < 100000; i++);
20717
20718 clang can emit four line number entries for that one line,
20719 each with a different discriminator.
20720 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20721
20722 However, we want gdb to coalesce all four entries into one.
20723 Otherwise the user could stepi into the middle of the line and
20724 gdb would get confused about whether the pc really was in the
20725 middle of the line.
20726
20727 Things are further complicated by the fact that two consecutive
20728 line number entries for the same line is a heuristic used by gcc
20729 to denote the end of the prologue. So we can't just discard duplicate
20730 entries, we have to be selective about it. The heuristic we use is
20731 that we only collapse consecutive entries for the same line if at least
20732 one of those entries has a non-zero discriminator. PR 17276.
20733
20734 Note: Addresses in the line number state machine can never go backwards
20735 within one sequence, thus this coalescing is ok. */
20736
20737static int
804d2729
TT
20738dwarf_record_line_p (struct dwarf2_cu *cu,
20739 unsigned int line, unsigned int last_line,
a05a36a5
DE
20740 int line_has_non_zero_discriminator,
20741 struct subfile *last_subfile)
20742{
c24bdb02 20743 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20744 return 1;
20745 if (line != last_line)
20746 return 1;
20747 /* Same line for the same file that we've seen already.
20748 As a last check, for pr 17276, only record the line if the line
20749 has never had a non-zero discriminator. */
20750 if (!line_has_non_zero_discriminator)
20751 return 1;
20752 return 0;
20753}
20754
804d2729
TT
20755/* Use the CU's builder to record line number LINE beginning at
20756 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20757
20758static void
d9b3de22 20759dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20760 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20761 struct dwarf2_cu *cu)
252a6764
DE
20762{
20763 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20764
27e0867f
DE
20765 if (dwarf_line_debug)
20766 {
20767 fprintf_unfiltered (gdb_stdlog,
20768 "Recording line %u, file %s, address %s\n",
20769 line, lbasename (subfile->name),
20770 paddress (gdbarch, address));
20771 }
20772
804d2729 20773 if (cu != nullptr)
8c95582d 20774 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20775}
20776
20777/* Subroutine of dwarf_decode_lines_1 to simplify it.
20778 Mark the end of a set of line number records.
d9b3de22 20779 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20780 If SUBFILE is NULL the request is ignored. */
20781
20782static void
20783dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20784 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20785{
27e0867f
DE
20786 if (subfile == NULL)
20787 return;
20788
20789 if (dwarf_line_debug)
20790 {
20791 fprintf_unfiltered (gdb_stdlog,
20792 "Finishing current line, file %s, address %s\n",
20793 lbasename (subfile->name),
20794 paddress (gdbarch, address));
20795 }
20796
8c95582d 20797 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20798}
20799
6f77053d
PA
20800void
20801lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20802{
d9b3de22
DE
20803 if (dwarf_line_debug)
20804 {
20805 fprintf_unfiltered (gdb_stdlog,
20806 "Processing actual line %u: file %u,"
94a72be7 20807 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20808 m_line, m_file,
6f77053d 20809 paddress (m_gdbarch, m_address),
94a72be7
AB
20810 m_is_stmt, m_discriminator,
20811 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20812 }
20813
6f77053d 20814 file_entry *fe = current_file ();
8c43009f
PA
20815
20816 if (fe == NULL)
d9b3de22
DE
20817 dwarf2_debug_line_missing_file_complaint ();
20818 /* For now we ignore lines not starting on an instruction boundary.
20819 But not when processing end_sequence for compatibility with the
20820 previous version of the code. */
6f77053d 20821 else if (m_op_index == 0 || end_sequence)
d9b3de22 20822 {
8c43009f 20823 fe->included_p = 1;
8c95582d 20824 if (m_record_lines_p)
d9b3de22 20825 {
1313c56e
AB
20826 /* When we switch files we insert an end maker in the first file,
20827 switch to the second file and add a new line entry. The
20828 problem is that the end marker inserted in the first file will
20829 discard any previous line entries at the same address. If the
20830 line entries in the first file are marked as is-stmt, while
20831 the new line in the second file is non-stmt, then this means
20832 the end marker will discard is-stmt lines so we can have a
20833 non-stmt line. This means that there are less addresses at
20834 which the user can insert a breakpoint.
20835
20836 To improve this we track the last address in m_last_address,
20837 and whether we have seen an is-stmt at this address. Then
20838 when switching files, if we have seen a stmt at the current
20839 address, and we are switching to create a non-stmt line, then
20840 discard the new line. */
20841 bool file_changed
20842 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20843 bool ignore_this_line
876518dd
TV
20844 = ((file_changed && !end_sequence && m_last_address == m_address
20845 && !m_is_stmt && m_stmt_at_address)
20846 || (!end_sequence && m_line == 0));
1313c56e
AB
20847
20848 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20849 {
804d2729
TT
20850 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20851 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20852 }
20853
1313c56e 20854 if (!end_sequence && !ignore_this_line)
d9b3de22 20855 {
8c95582d
AB
20856 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20857
804d2729 20858 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20859 m_line_has_non_zero_discriminator,
20860 m_last_subfile))
d9b3de22 20861 {
c24bdb02 20862 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20863 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20864 builder->get_current_subfile (),
8c95582d 20865 m_line, m_address, is_stmt,
804d2729 20866 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20867 }
c24bdb02 20868 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20869 m_last_line = m_line;
d9b3de22
DE
20870 }
20871 }
20872 }
1313c56e
AB
20873
20874 /* Track whether we have seen any m_is_stmt true at m_address in case we
20875 have multiple line table entries all at m_address. */
20876 if (m_last_address != m_address)
20877 {
20878 m_stmt_at_address = false;
20879 m_last_address = m_address;
20880 }
20881 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20882}
20883
804d2729
TT
20884lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20885 line_header *lh, bool record_lines_p)
d9b3de22 20886{
804d2729 20887 m_cu = cu;
6f77053d
PA
20888 m_gdbarch = arch;
20889 m_record_lines_p = record_lines_p;
20890 m_line_header = lh;
d9b3de22 20891
804d2729 20892 m_currently_recording_lines = true;
d9b3de22 20893
d9b3de22
DE
20894 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20895 was a line entry for it so that the backend has a chance to adjust it
20896 and also record it in case it needs it. This is currently used by MIPS
20897 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20898 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20899 m_is_stmt = lh->default_is_stmt;
20900 m_discriminator = 0;
1313c56e
AB
20901
20902 m_last_address = m_address;
20903 m_stmt_at_address = false;
252a6764
DE
20904}
20905
6f77053d
PA
20906void
20907lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20908 const gdb_byte *line_ptr,
7ab6656f 20909 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20910{
a8caed5d
FS
20911 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20912 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20913 located at 0x0. In this case, additionally check that if
20914 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 20915
a8caed5d
FS
20916 if ((address == 0 && address < unrelocated_lowpc)
20917 || address == (CORE_ADDR) -1)
924c2928
DE
20918 {
20919 /* This line table is for a function which has been
20920 GCd by the linker. Ignore it. PR gdb/12528 */
20921
5e22e966 20922 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20923 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20924
b98664d3 20925 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20926 line_offset, objfile_name (objfile));
804d2729
TT
20927 m_currently_recording_lines = false;
20928 /* Note: m_currently_recording_lines is left as false until we see
20929 DW_LNE_end_sequence. */
924c2928
DE
20930 }
20931}
20932
f3f5162e 20933/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20934 Process the line number information in LH.
20935 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20936 program in order to set included_p for every referenced header. */
debd256d 20937
c906108c 20938static void
43f3e411
DE
20939dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20940 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20941{
d521ce57
TT
20942 const gdb_byte *line_ptr, *extended_end;
20943 const gdb_byte *line_end;
a8c50c1f 20944 unsigned int bytes_read, extended_len;
699ca60a 20945 unsigned char op_code, extended_op;
e142c38c 20946 CORE_ADDR baseaddr;
5e22e966 20947 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20948 bfd *abfd = objfile->obfd;
08feed99 20949 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20950 /* True if we're recording line info (as opposed to building partial
20951 symtabs and just interested in finding include files mentioned by
20952 the line number program). */
20953 bool record_lines_p = !decode_for_pst_p;
e142c38c 20954
b3b3bada 20955 baseaddr = objfile->text_section_offset ();
c906108c 20956
debd256d
JB
20957 line_ptr = lh->statement_program_start;
20958 line_end = lh->statement_program_end;
c906108c
SS
20959
20960 /* Read the statement sequences until there's nothing left. */
20961 while (line_ptr < line_end)
20962 {
6f77053d
PA
20963 /* The DWARF line number program state machine. Reset the state
20964 machine at the start of each sequence. */
804d2729 20965 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20966 bool end_sequence = false;
d9b3de22 20967
8c43009f 20968 if (record_lines_p)
c906108c 20969 {
8c43009f
PA
20970 /* Start a subfile for the current file of the state
20971 machine. */
20972 const file_entry *fe = state_machine.current_file ();
20973
20974 if (fe != NULL)
804d2729 20975 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20976 }
20977
a738430d 20978 /* Decode the table. */
d9b3de22 20979 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20980 {
20981 op_code = read_1_byte (abfd, line_ptr);
20982 line_ptr += 1;
9aa1fe7e 20983
debd256d 20984 if (op_code >= lh->opcode_base)
6e70227d 20985 {
8e07a239 20986 /* Special opcode. */
6f77053d 20987 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20988 }
20989 else switch (op_code)
c906108c
SS
20990 {
20991 case DW_LNS_extended_op:
3e43a32a
MS
20992 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20993 &bytes_read);
473b7be6 20994 line_ptr += bytes_read;
a8c50c1f 20995 extended_end = line_ptr + extended_len;
c906108c
SS
20996 extended_op = read_1_byte (abfd, line_ptr);
20997 line_ptr += 1;
8f34b746
TV
20998 if (DW_LNE_lo_user <= extended_op
20999 && extended_op <= DW_LNE_hi_user)
21000 {
21001 /* Vendor extension, ignore. */
21002 line_ptr = extended_end;
21003 break;
21004 }
c906108c
SS
21005 switch (extended_op)
21006 {
21007 case DW_LNE_end_sequence:
6f77053d
PA
21008 state_machine.handle_end_sequence ();
21009 end_sequence = true;
c906108c
SS
21010 break;
21011 case DW_LNE_set_address:
d9b3de22
DE
21012 {
21013 CORE_ADDR address
c8a7a66f 21014 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21015 line_ptr += bytes_read;
6f77053d
PA
21016
21017 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21018 lowpc - baseaddr, address);
6f77053d 21019 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21020 }
c906108c
SS
21021 break;
21022 case DW_LNE_define_file:
debd256d 21023 {
d521ce57 21024 const char *cur_file;
ecfb656c
PA
21025 unsigned int mod_time, length;
21026 dir_index dindex;
6e70227d 21027
3e43a32a
MS
21028 cur_file = read_direct_string (abfd, line_ptr,
21029 &bytes_read);
debd256d 21030 line_ptr += bytes_read;
ecfb656c 21031 dindex = (dir_index)
debd256d
JB
21032 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21033 line_ptr += bytes_read;
21034 mod_time =
21035 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21036 line_ptr += bytes_read;
21037 length =
21038 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21039 line_ptr += bytes_read;
ecfb656c 21040 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21041 }
c906108c 21042 break;
d0c6ba3d 21043 case DW_LNE_set_discriminator:
6f77053d
PA
21044 {
21045 /* The discriminator is not interesting to the
21046 debugger; just ignore it. We still need to
21047 check its value though:
21048 if there are consecutive entries for the same
21049 (non-prologue) line we want to coalesce them.
21050 PR 17276. */
21051 unsigned int discr
21052 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21053 line_ptr += bytes_read;
21054
21055 state_machine.handle_set_discriminator (discr);
21056 }
d0c6ba3d 21057 break;
c906108c 21058 default:
b98664d3 21059 complaint (_("mangled .debug_line section"));
debd256d 21060 return;
c906108c 21061 }
a8c50c1f
DJ
21062 /* Make sure that we parsed the extended op correctly. If e.g.
21063 we expected a different address size than the producer used,
21064 we may have read the wrong number of bytes. */
21065 if (line_ptr != extended_end)
21066 {
b98664d3 21067 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21068 return;
21069 }
c906108c
SS
21070 break;
21071 case DW_LNS_copy:
6f77053d 21072 state_machine.handle_copy ();
c906108c
SS
21073 break;
21074 case DW_LNS_advance_pc:
2dc7f7b3
TT
21075 {
21076 CORE_ADDR adjust
21077 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21078 line_ptr += bytes_read;
6f77053d
PA
21079
21080 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21081 }
c906108c
SS
21082 break;
21083 case DW_LNS_advance_line:
a05a36a5
DE
21084 {
21085 int line_delta
21086 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21087 line_ptr += bytes_read;
6f77053d
PA
21088
21089 state_machine.handle_advance_line (line_delta);
a05a36a5 21090 }
c906108c
SS
21091 break;
21092 case DW_LNS_set_file:
d9b3de22 21093 {
6f77053d 21094 file_name_index file
ecfb656c
PA
21095 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21096 &bytes_read);
d9b3de22 21097 line_ptr += bytes_read;
8c43009f 21098
6f77053d 21099 state_machine.handle_set_file (file);
d9b3de22 21100 }
c906108c
SS
21101 break;
21102 case DW_LNS_set_column:
0ad93d4f 21103 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21104 line_ptr += bytes_read;
21105 break;
21106 case DW_LNS_negate_stmt:
6f77053d 21107 state_machine.handle_negate_stmt ();
c906108c
SS
21108 break;
21109 case DW_LNS_set_basic_block:
c906108c 21110 break;
c2c6d25f
JM
21111 /* Add to the address register of the state machine the
21112 address increment value corresponding to special opcode
a738430d
MK
21113 255. I.e., this value is scaled by the minimum
21114 instruction length since special opcode 255 would have
b021a221 21115 scaled the increment. */
c906108c 21116 case DW_LNS_const_add_pc:
6f77053d 21117 state_machine.handle_const_add_pc ();
c906108c
SS
21118 break;
21119 case DW_LNS_fixed_advance_pc:
3e29f34a 21120 {
6f77053d 21121 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21122 line_ptr += 2;
6f77053d
PA
21123
21124 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21125 }
c906108c 21126 break;
9aa1fe7e 21127 default:
a738430d
MK
21128 {
21129 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21130 int i;
a738430d 21131
debd256d 21132 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21133 {
21134 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21135 line_ptr += bytes_read;
21136 }
21137 }
c906108c
SS
21138 }
21139 }
d9b3de22
DE
21140
21141 if (!end_sequence)
21142 dwarf2_debug_line_missing_end_sequence_complaint ();
21143
21144 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21145 in which case we still finish recording the last line). */
6f77053d 21146 state_machine.record_line (true);
c906108c 21147 }
f3f5162e
DE
21148}
21149
21150/* Decode the Line Number Program (LNP) for the given line_header
21151 structure and CU. The actual information extracted and the type
21152 of structures created from the LNP depends on the value of PST.
21153
21154 1. If PST is NULL, then this procedure uses the data from the program
21155 to create all necessary symbol tables, and their linetables.
21156
21157 2. If PST is not NULL, this procedure reads the program to determine
21158 the list of files included by the unit represented by PST, and
21159 builds all the associated partial symbol tables.
21160
21161 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21162 It is used for relative paths in the line table.
21163 NOTE: When processing partial symtabs (pst != NULL),
21164 comp_dir == pst->dirname.
21165
21166 NOTE: It is important that psymtabs have the same file name (via strcmp)
21167 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21168 symtab we don't use it in the name of the psymtabs we create.
21169 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21170 A good testcase for this is mb-inline.exp.
21171
527f3840
JK
21172 LOWPC is the lowest address in CU (or 0 if not known).
21173
21174 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21175 for its PC<->lines mapping information. Otherwise only the filename
21176 table is read in. */
f3f5162e
DE
21177
21178static void
21179dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21180 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21181 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21182{
5e22e966 21183 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21184 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21185
527f3840
JK
21186 if (decode_mapping)
21187 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21188
21189 if (decode_for_pst_p)
21190 {
aaa75496
JB
21191 /* Now that we're done scanning the Line Header Program, we can
21192 create the psymtab of each included file. */
7ba99d21
AT
21193 for (auto &file_entry : lh->file_names ())
21194 if (file_entry.included_p == 1)
aaa75496 21195 {
c89b44cd 21196 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21197 const char *include_name =
7ba99d21
AT
21198 psymtab_include_file_name (lh, file_entry, pst,
21199 comp_dir, &name_holder);
c6da4cef 21200 if (include_name != NULL)
aaa75496
JB
21201 dwarf2_create_include_psymtab (include_name, pst, objfile);
21202 }
21203 }
cb1df416
DJ
21204 else
21205 {
21206 /* Make sure a symtab is created for every file, even files
21207 which contain only variables (i.e. no code with associated
21208 line numbers). */
c24bdb02
KS
21209 buildsym_compunit *builder = cu->get_builder ();
21210 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21211
7ba99d21 21212 for (auto &fe : lh->file_names ())
cb1df416 21213 {
804d2729 21214 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21215 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21216 {
c24bdb02 21217 builder->get_current_subfile ()->symtab
804d2729 21218 = allocate_symtab (cust,
c24bdb02 21219 builder->get_current_subfile ()->name);
43f3e411 21220 }
c24bdb02 21221 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21222 }
21223 }
c906108c
SS
21224}
21225
21226/* Start a subfile for DWARF. FILENAME is the name of the file and
21227 DIRNAME the name of the source directory which contains FILENAME
4d663531 21228 or NULL if not known.
c906108c
SS
21229 This routine tries to keep line numbers from identical absolute and
21230 relative file names in a common subfile.
21231
21232 Using the `list' example from the GDB testsuite, which resides in
21233 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21234 of /srcdir/list0.c yields the following debugging information for list0.c:
21235
c5aa993b 21236 DW_AT_name: /srcdir/list0.c
4d663531 21237 DW_AT_comp_dir: /compdir
357e46e7 21238 files.files[0].name: list0.h
c5aa993b 21239 files.files[0].dir: /srcdir
357e46e7 21240 files.files[1].name: list0.c
c5aa993b 21241 files.files[1].dir: /srcdir
c906108c
SS
21242
21243 The line number information for list0.c has to end up in a single
4f1520fb
FR
21244 subfile, so that `break /srcdir/list0.c:1' works as expected.
21245 start_subfile will ensure that this happens provided that we pass the
21246 concatenation of files.files[1].dir and files.files[1].name as the
21247 subfile's name. */
c906108c
SS
21248
21249static void
804d2729
TT
21250dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21251 const char *dirname)
c906108c 21252{
43816ebc 21253 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21254
4d663531 21255 /* In order not to lose the line information directory,
4f1520fb
FR
21256 we concatenate it to the filename when it makes sense.
21257 Note that the Dwarf3 standard says (speaking of filenames in line
21258 information): ``The directory index is ignored for file names
21259 that represent full path names''. Thus ignoring dirname in the
21260 `else' branch below isn't an issue. */
c906108c 21261
d5166ae1 21262 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21263 {
43816ebc
TT
21264 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21265 filename = copy.get ();
d521ce57 21266 }
c906108c 21267
c24bdb02 21268 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21269}
21270
804d2729
TT
21271/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21272 buildsym_compunit constructor. */
f4dc4d17 21273
c24bdb02
KS
21274struct compunit_symtab *
21275dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21276 CORE_ADDR low_pc)
f4dc4d17 21277{
c24bdb02 21278 gdb_assert (m_builder == nullptr);
43f3e411 21279
c24bdb02 21280 m_builder.reset (new struct buildsym_compunit
f6e649dd 21281 (this->per_objfile->objfile,
c24bdb02 21282 name, comp_dir, language, low_pc));
93b8bea4 21283
c24bdb02 21284 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21285
c24bdb02
KS
21286 get_builder ()->record_debugformat ("DWARF 2");
21287 get_builder ()->record_producer (producer);
f4dc4d17 21288
c24bdb02 21289 processing_has_namespace_info = false;
43f3e411 21290
c24bdb02 21291 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21292}
21293
4c2df51b
DJ
21294static void
21295var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21296 struct dwarf2_cu *cu)
4c2df51b 21297{
5e22e966 21298 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21299 struct comp_unit_head *cu_header = &cu->header;
21300
4c2df51b
DJ
21301 /* NOTE drow/2003-01-30: There used to be a comment and some special
21302 code here to turn a symbol with DW_AT_external and a
21303 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21304 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21305 with some versions of binutils) where shared libraries could have
21306 relocations against symbols in their debug information - the
21307 minimal symbol would have the right address, but the debug info
21308 would not. It's no longer necessary, because we will explicitly
21309 apply relocations when we read in the debug information now. */
21310
21311 /* A DW_AT_location attribute with no contents indicates that a
21312 variable has been optimized away. */
9d2246fc 21313 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21314 {
f1e6e072 21315 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21316 return;
21317 }
21318
21319 /* Handle one degenerate form of location expression specially, to
21320 preserve GDB's previous behavior when section offsets are
336d760d
AT
21321 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21322 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21323
9d2246fc
TT
21324 if (attr->form_is_block ())
21325 {
21326 struct dwarf_block *block = attr->as_block ();
21327
21328 if ((block->data[0] == DW_OP_addr
21329 && block->size == 1 + cu_header->addr_size)
21330 || ((block->data[0] == DW_OP_GNU_addr_index
21331 || block->data[0] == DW_OP_addrx)
21332 && (block->size
21333 == 1 + leb128_size (&block->data[1]))))
21334 {
21335 unsigned int dummy;
21336
21337 if (block->data[0] == DW_OP_addr)
21338 SET_SYMBOL_VALUE_ADDRESS
21339 (sym, cu->header.read_address (objfile->obfd,
21340 block->data + 1,
38583298 21341 &dummy));
9d2246fc
TT
21342 else
21343 SET_SYMBOL_VALUE_ADDRESS
21344 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21345 &dummy));
21346 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21347 fixup_symbol_section (sym, objfile);
21348 SET_SYMBOL_VALUE_ADDRESS
21349 (sym,
21350 SYMBOL_VALUE_ADDRESS (sym)
21351 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21352 return;
21353 }
4c2df51b
DJ
21354 }
21355
21356 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21357 expression evaluator, and use LOC_COMPUTED only when necessary
21358 (i.e. when the value of a register or memory location is
21359 referenced, or a thread-local block, etc.). Then again, it might
21360 not be worthwhile. I'm assuming that it isn't unless performance
21361 or memory numbers show me otherwise. */
21362
f1e6e072 21363 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21364
f1e6e072 21365 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21366 cu->has_loclist = true;
4c2df51b
DJ
21367}
21368
c906108c
SS
21369/* Given a pointer to a DWARF information entry, figure out if we need
21370 to make a symbol table entry for it, and if so, create a new entry
21371 and return a pointer to it.
21372 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21373 used the passed type.
21374 If SPACE is not NULL, use it to hold the new symbol. If it is
21375 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21376
21377static struct symbol *
5e2db402
TT
21378new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21379 struct symbol *space)
c906108c 21380{
976ca316
SM
21381 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21382 struct objfile *objfile = per_objfile->objfile;
08feed99 21383 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21384 struct symbol *sym = NULL;
15d034d0 21385 const char *name;
c906108c
SS
21386 struct attribute *attr = NULL;
21387 struct attribute *attr2 = NULL;
e142c38c 21388 CORE_ADDR baseaddr;
e37fd15a
SW
21389 struct pending **list_to_add = NULL;
21390
edb3359d 21391 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21392
b3b3bada 21393 baseaddr = objfile->text_section_offset ();
c906108c 21394
94af9270 21395 name = dwarf2_name (die, cu);
c906108c
SS
21396 if (name)
21397 {
34eaf542 21398 int suppress_add = 0;
94af9270 21399
34eaf542
TT
21400 if (space)
21401 sym = space;
21402 else
8c14c3a3 21403 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21404 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21405
21406 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21407 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21408 /* Fortran does not have mangling standard and the mangling does differ
21409 between gfortran, iFort etc. */
bcfe6157
TT
21410 const char *physname
21411 = (cu->language == language_fortran
21412 ? dwarf2_full_name (name, die, cu)
21413 : dwarf2_physname (name, die, cu));
21414 const char *linkagename = dw2_linkage_name (die, cu);
21415
21416 if (linkagename == nullptr || cu->language == language_ada)
21417 sym->set_linkage_name (physname);
21418 else
21419 {
21420 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21421 sym->set_linkage_name (linkagename);
21422 }
f55ee35c 21423
c906108c 21424 /* Default assumptions.
c5aa993b 21425 Use the passed type or decode it from the die. */
176620f1 21426 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21427 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21428 if (type != NULL)
21429 SYMBOL_TYPE (sym) = type;
21430 else
e7c27a73 21431 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21432 attr = dwarf2_attr (die,
21433 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21434 cu);
435d3d88 21435 if (attr != nullptr)
529908cb 21436 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 21437
edb3359d
DJ
21438 attr = dwarf2_attr (die,
21439 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21440 cu);
529908cb 21441 if (attr != nullptr && attr->form_is_unsigned ())
cb1df416 21442 {
529908cb
TT
21443 file_name_index file_index
21444 = (file_name_index) attr->as_unsigned ();
8c43009f 21445 struct file_entry *fe;
9a619af0 21446
ecfb656c
PA
21447 if (cu->line_header != NULL)
21448 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21449 else
21450 fe = NULL;
21451
21452 if (fe == NULL)
b98664d3 21453 complaint (_("file index out of range"));
8c43009f
PA
21454 else
21455 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21456 }
21457
c906108c
SS
21458 switch (die->tag)
21459 {
21460 case DW_TAG_label:
e142c38c 21461 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21462 if (attr != nullptr)
3e29f34a
MR
21463 {
21464 CORE_ADDR addr;
21465
95f982e5 21466 addr = attr->as_address ();
3e29f34a 21467 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21468 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21469 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21470 }
8f5c6526
TV
21471 else
21472 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21473 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21474 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21475 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21476 break;
21477 case DW_TAG_subprogram:
21478 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21479 finish_block. */
f1e6e072 21480 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21481 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 21482 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
21483 || cu->language == language_ada
21484 || cu->language == language_fortran)
c906108c 21485 {
2cfa0c8d 21486 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21487 Ada and Fortran subprograms, whether marked external or
21488 not, are always stored as a global symbol, because we want
21489 to be able to access them globally. For instance, we want
21490 to be able to break on a nested subprogram without having
21491 to specify the context. */
c24bdb02 21492 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21493 }
21494 else
21495 {
e37fd15a 21496 list_to_add = cu->list_in_scope;
c906108c
SS
21497 }
21498 break;
edb3359d
DJ
21499 case DW_TAG_inlined_subroutine:
21500 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21501 finish_block. */
f1e6e072 21502 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21503 SYMBOL_INLINED (sym) = 1;
481860b3 21504 list_to_add = cu->list_in_scope;
edb3359d 21505 break;
34eaf542
TT
21506 case DW_TAG_template_value_param:
21507 suppress_add = 1;
21508 /* Fall through. */
72929c62 21509 case DW_TAG_constant:
c906108c 21510 case DW_TAG_variable:
254e6b9e 21511 case DW_TAG_member:
0963b4bd
MS
21512 /* Compilation with minimal debug info may result in
21513 variables with missing type entries. Change the
21514 misleading `void' type to something sensible. */
78134374 21515 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21516 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21517
e142c38c 21518 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21519 /* In the case of DW_TAG_member, we should only be called for
21520 static const members. */
21521 if (die->tag == DW_TAG_member)
21522 {
3863f96c
DE
21523 /* dwarf2_add_field uses die_is_declaration,
21524 so we do the same. */
254e6b9e
DE
21525 gdb_assert (die_is_declaration (die, cu));
21526 gdb_assert (attr);
21527 }
435d3d88 21528 if (attr != nullptr)
c906108c 21529 {
e7c27a73 21530 dwarf2_const_value (attr, sym, cu);
e142c38c 21531 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21532 if (!suppress_add)
34eaf542 21533 {
c45bc3f8 21534 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 21535 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21536 else
e37fd15a 21537 list_to_add = cu->list_in_scope;
34eaf542 21538 }
c906108c
SS
21539 break;
21540 }
e142c38c 21541 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21542 if (attr != nullptr)
c906108c 21543 {
e7c27a73 21544 var_decode_location (attr, sym, cu);
e142c38c 21545 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21546
21547 /* Fortran explicitly imports any global symbols to the local
21548 scope by DW_TAG_common_block. */
21549 if (cu->language == language_fortran && die->parent
21550 && die->parent->tag == DW_TAG_common_block)
21551 attr2 = NULL;
21552
caac4577
JG
21553 if (SYMBOL_CLASS (sym) == LOC_STATIC
21554 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21555 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21556 {
21557 /* When a static variable is eliminated by the linker,
21558 the corresponding debug information is not stripped
21559 out, but the variable address is set to null;
21560 do not add such variables into symbol table. */
21561 }
c45bc3f8 21562 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 21563 {
4b610737
TT
21564 if (SYMBOL_CLASS (sym) == LOC_STATIC
21565 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21566 && per_objfile->per_bfd->can_copy)
4b610737
TT
21567 {
21568 /* A global static variable might be subject to
21569 copy relocation. We first check for a local
21570 minsym, though, because maybe the symbol was
21571 marked hidden, in which case this would not
21572 apply. */
21573 bound_minimal_symbol found
21574 = (lookup_minimal_symbol_linkage
987012b8 21575 (sym->linkage_name (), objfile));
4b610737
TT
21576 if (found.minsym != nullptr)
21577 sym->maybe_copied = 1;
21578 }
f55ee35c 21579
1c809c68
TT
21580 /* A variable with DW_AT_external is never static,
21581 but it may be block-scoped. */
804d2729 21582 list_to_add
c24bdb02
KS
21583 = ((cu->list_in_scope
21584 == cu->get_builder ()->get_file_symbols ())
21585 ? cu->get_builder ()->get_global_symbols ()
804d2729 21586 : cu->list_in_scope);
1c809c68 21587 }
c906108c 21588 else
e37fd15a 21589 list_to_add = cu->list_in_scope;
c906108c
SS
21590 }
21591 else
21592 {
21593 /* We do not know the address of this symbol.
c5aa993b
JM
21594 If it is an external symbol and we have type information
21595 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21596 The address of the variable will then be determined from
21597 the minimal symbol table whenever the variable is
21598 referenced. */
e142c38c 21599 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21600
21601 /* Fortran explicitly imports any global symbols to the local
21602 scope by DW_TAG_common_block. */
21603 if (cu->language == language_fortran && die->parent
21604 && die->parent->tag == DW_TAG_common_block)
21605 {
21606 /* SYMBOL_CLASS doesn't matter here because
21607 read_common_block is going to reset it. */
21608 if (!suppress_add)
21609 list_to_add = cu->list_in_scope;
21610 }
c45bc3f8 21611 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 21612 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21613 {
0fe7935b
DJ
21614 /* A variable with DW_AT_external is never static, but it
21615 may be block-scoped. */
804d2729 21616 list_to_add
c24bdb02
KS
21617 = ((cu->list_in_scope
21618 == cu->get_builder ()->get_file_symbols ())
21619 ? cu->get_builder ()->get_global_symbols ()
804d2729 21620 : cu->list_in_scope);
0fe7935b 21621
f1e6e072 21622 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21623 }
442ddf59
JK
21624 else if (!die_is_declaration (die, cu))
21625 {
21626 /* Use the default LOC_OPTIMIZED_OUT class. */
21627 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21628 if (!suppress_add)
21629 list_to_add = cu->list_in_scope;
442ddf59 21630 }
c906108c
SS
21631 }
21632 break;
21633 case DW_TAG_formal_parameter:
a60f3166
TT
21634 {
21635 /* If we are inside a function, mark this as an argument. If
21636 not, we might be looking at an argument to an inlined function
21637 when we do not have enough information to show inlined frames;
21638 pretend it's a local variable in that case so that the user can
21639 still see it. */
804d2729 21640 struct context_stack *curr
c24bdb02 21641 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21642 if (curr != nullptr && curr->name != nullptr)
21643 SYMBOL_IS_ARGUMENT (sym) = 1;
21644 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21645 if (attr != nullptr)
a60f3166
TT
21646 {
21647 var_decode_location (attr, sym, cu);
21648 }
21649 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21650 if (attr != nullptr)
a60f3166
TT
21651 {
21652 dwarf2_const_value (attr, sym, cu);
21653 }
f346a30d 21654
a60f3166
TT
21655 list_to_add = cu->list_in_scope;
21656 }
c906108c
SS
21657 break;
21658 case DW_TAG_unspecified_parameters:
21659 /* From varargs functions; gdb doesn't seem to have any
21660 interest in this information, so just ignore it for now.
21661 (FIXME?) */
21662 break;
34eaf542
TT
21663 case DW_TAG_template_type_param:
21664 suppress_add = 1;
21665 /* Fall through. */
c906108c 21666 case DW_TAG_class_type:
680b30c7 21667 case DW_TAG_interface_type:
c906108c
SS
21668 case DW_TAG_structure_type:
21669 case DW_TAG_union_type:
72019c9c 21670 case DW_TAG_set_type:
c906108c 21671 case DW_TAG_enumeration_type:
f1e6e072 21672 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21673 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21674
63d06c5c 21675 {
9c37b5ae 21676 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21677 really ever be static objects: otherwise, if you try
21678 to, say, break of a class's method and you're in a file
21679 which doesn't mention that class, it won't work unless
21680 the check for all static symbols in lookup_symbol_aux
21681 saves you. See the OtherFileClass tests in
21682 gdb.c++/namespace.exp. */
21683
e37fd15a 21684 if (!suppress_add)
34eaf542 21685 {
c24bdb02 21686 buildsym_compunit *builder = cu->get_builder ();
804d2729 21687 list_to_add
c24bdb02 21688 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21689 && cu->language == language_cplus
c24bdb02 21690 ? builder->get_global_symbols ()
804d2729 21691 : cu->list_in_scope);
63d06c5c 21692
64382290 21693 /* The semantics of C++ state that "struct foo {
9c37b5ae 21694 ... }" also defines a typedef for "foo". */
64382290 21695 if (cu->language == language_cplus
45280282 21696 || cu->language == language_ada
c44af4eb
TT
21697 || cu->language == language_d
21698 || cu->language == language_rust)
64382290
TT
21699 {
21700 /* The symbol's name is already allocated along
21701 with this objfile, so we don't need to
21702 duplicate it for the type. */
7d93a1e0 21703 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21704 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21705 }
63d06c5c
DC
21706 }
21707 }
c906108c
SS
21708 break;
21709 case DW_TAG_typedef:
f1e6e072 21710 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21711 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21712 list_to_add = cu->list_in_scope;
63d06c5c 21713 break;
c906108c 21714 case DW_TAG_base_type:
a02abb62 21715 case DW_TAG_subrange_type:
f1e6e072 21716 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21717 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21718 list_to_add = cu->list_in_scope;
c906108c
SS
21719 break;
21720 case DW_TAG_enumerator:
e142c38c 21721 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21722 if (attr != nullptr)
c906108c 21723 {
e7c27a73 21724 dwarf2_const_value (attr, sym, cu);
c906108c 21725 }
63d06c5c
DC
21726 {
21727 /* NOTE: carlton/2003-11-10: See comment above in the
21728 DW_TAG_class_type, etc. block. */
21729
804d2729 21730 list_to_add
c24bdb02 21731 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21732 && cu->language == language_cplus
c24bdb02 21733 ? cu->get_builder ()->get_global_symbols ()
804d2729 21734 : cu->list_in_scope);
63d06c5c 21735 }
c906108c 21736 break;
74921315 21737 case DW_TAG_imported_declaration:
5c4e30ca 21738 case DW_TAG_namespace:
f1e6e072 21739 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21740 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21741 break;
530e8392
KB
21742 case DW_TAG_module:
21743 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21744 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21745 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21746 break;
4357ac6c 21747 case DW_TAG_common_block:
f1e6e072 21748 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21749 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21750 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21751 break;
c906108c
SS
21752 default:
21753 /* Not a tag we recognize. Hopefully we aren't processing
21754 trash data, but since we must specifically ignore things
21755 we don't recognize, there is nothing else we should do at
0963b4bd 21756 this point. */
b98664d3 21757 complaint (_("unsupported tag: '%s'"),
4d3c2250 21758 dwarf_tag_name (die->tag));
c906108c
SS
21759 break;
21760 }
df8a16a1 21761
e37fd15a
SW
21762 if (suppress_add)
21763 {
21764 sym->hash_next = objfile->template_symbols;
21765 objfile->template_symbols = sym;
21766 list_to_add = NULL;
21767 }
21768
21769 if (list_to_add != NULL)
d3cb6808 21770 add_symbol_to_list (sym, list_to_add);
e37fd15a 21771
df8a16a1
DJ
21772 /* For the benefit of old versions of GCC, check for anonymous
21773 namespaces based on the demangled name. */
4d4ec4e5 21774 if (!cu->processing_has_namespace_info
94af9270 21775 && cu->language == language_cplus)
c24bdb02 21776 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21777 }
21778 return (sym);
21779}
21780
98bfdba5
PA
21781/* Given an attr with a DW_FORM_dataN value in host byte order,
21782 zero-extend it as appropriate for the symbol's type. The DWARF
21783 standard (v4) is not entirely clear about the meaning of using
21784 DW_FORM_dataN for a constant with a signed type, where the type is
21785 wider than the data. The conclusion of a discussion on the DWARF
21786 list was that this is unspecified. We choose to always zero-extend
21787 because that is the interpretation long in use by GCC. */
c906108c 21788
98bfdba5 21789static gdb_byte *
ff39bb5e 21790dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21791 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21792{
5e22e966 21793 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21794 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21795 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 21796 LONGEST l = attr->constant_value (0);
98bfdba5
PA
21797
21798 if (bits < sizeof (*value) * 8)
21799 {
21800 l &= ((LONGEST) 1 << bits) - 1;
21801 *value = l;
21802 }
21803 else if (bits == sizeof (*value) * 8)
21804 *value = l;
21805 else
21806 {
224c3ddb 21807 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21808 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21809 return bytes;
21810 }
21811
21812 return NULL;
21813}
21814
21815/* Read a constant value from an attribute. Either set *VALUE, or if
21816 the value does not fit in *VALUE, set *BYTES - either already
21817 allocated on the objfile obstack, or newly allocated on OBSTACK,
21818 or, set *BATON, if we translated the constant to a location
21819 expression. */
21820
21821static void
ff39bb5e 21822dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21823 const char *name, struct obstack *obstack,
21824 struct dwarf2_cu *cu,
d521ce57 21825 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21826 struct dwarf2_locexpr_baton **baton)
21827{
5e22e966 21828 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21829 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21830 struct comp_unit_head *cu_header = &cu->header;
c906108c 21831 struct dwarf_block *blk;
98bfdba5
PA
21832 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21833 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21834
21835 *value = 0;
21836 *bytes = NULL;
21837 *baton = NULL;
c906108c
SS
21838
21839 switch (attr->form)
21840 {
21841 case DW_FORM_addr:
336d760d 21842 case DW_FORM_addrx:
3019eac3 21843 case DW_FORM_GNU_addr_index:
ac56253d 21844 {
ac56253d
TT
21845 gdb_byte *data;
21846
98bfdba5
PA
21847 if (TYPE_LENGTH (type) != cu_header->addr_size)
21848 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21849 cu_header->addr_size,
98bfdba5 21850 TYPE_LENGTH (type));
ac56253d
TT
21851 /* Symbols of this form are reasonably rare, so we just
21852 piggyback on the existing location code rather than writing
21853 a new implementation of symbol_computed_ops. */
8d749320 21854 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21855 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21856 (*baton)->per_cu = cu->per_cu;
21857 gdb_assert ((*baton)->per_cu);
ac56253d 21858
98bfdba5 21859 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21860 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21861 (*baton)->data = data;
ac56253d
TT
21862
21863 data[0] = DW_OP_addr;
21864 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 21865 byte_order, attr->as_address ());
ac56253d 21866 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21867 }
c906108c 21868 break;
4ac36638 21869 case DW_FORM_string:
93b5768b 21870 case DW_FORM_strp:
cf532bd1 21871 case DW_FORM_strx:
3019eac3 21872 case DW_FORM_GNU_str_index:
36586728 21873 case DW_FORM_GNU_strp_alt:
c6481205 21874 /* The string is already allocated on the objfile obstack, point
98bfdba5 21875 directly to it. */
2c830f54 21876 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 21877 break;
c906108c
SS
21878 case DW_FORM_block1:
21879 case DW_FORM_block2:
21880 case DW_FORM_block4:
21881 case DW_FORM_block:
2dc7f7b3 21882 case DW_FORM_exprloc:
0224619f 21883 case DW_FORM_data16:
9d2246fc 21884 blk = attr->as_block ();
98bfdba5
PA
21885 if (TYPE_LENGTH (type) != blk->size)
21886 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21887 TYPE_LENGTH (type));
21888 *bytes = blk->data;
c906108c 21889 break;
2df3850c
JM
21890
21891 /* The DW_AT_const_value attributes are supposed to carry the
21892 symbol's value "represented as it would be on the target
21893 architecture." By the time we get here, it's already been
21894 converted to host endianness, so we just need to sign- or
21895 zero-extend it as appropriate. */
21896 case DW_FORM_data1:
3aef2284 21897 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21898 break;
c906108c 21899 case DW_FORM_data2:
3aef2284 21900 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21901 break;
c906108c 21902 case DW_FORM_data4:
3aef2284 21903 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21904 break;
c906108c 21905 case DW_FORM_data8:
3aef2284 21906 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21907 break;
21908
c906108c 21909 case DW_FORM_sdata:
663c44ac 21910 case DW_FORM_implicit_const:
1bc397c5 21911 *value = attr->as_signed ();
2df3850c
JM
21912 break;
21913
c906108c 21914 case DW_FORM_udata:
529908cb 21915 *value = attr->as_unsigned ();
c906108c 21916 break;
2df3850c 21917
c906108c 21918 default:
b98664d3 21919 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21920 dwarf_form_name (attr->form));
98bfdba5 21921 *value = 0;
c906108c
SS
21922 break;
21923 }
21924}
21925
2df3850c 21926
98bfdba5
PA
21927/* Copy constant value from an attribute to a symbol. */
21928
2df3850c 21929static void
ff39bb5e 21930dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21931 struct dwarf2_cu *cu)
2df3850c 21932{
5e22e966 21933 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21934 LONGEST value;
d521ce57 21935 const gdb_byte *bytes;
98bfdba5 21936 struct dwarf2_locexpr_baton *baton;
2df3850c 21937
98bfdba5 21938 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21939 sym->print_name (),
98bfdba5
PA
21940 &objfile->objfile_obstack, cu,
21941 &value, &bytes, &baton);
2df3850c 21942
98bfdba5
PA
21943 if (baton != NULL)
21944 {
98bfdba5 21945 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21946 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21947 }
21948 else if (bytes != NULL)
21949 {
21950 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21951 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21952 }
21953 else
21954 {
21955 SYMBOL_VALUE (sym) = value;
f1e6e072 21956 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21957 }
2df3850c
JM
21958}
21959
c906108c
SS
21960/* Return the type of the die in question using its DW_AT_type attribute. */
21961
21962static struct type *
e7c27a73 21963die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21964{
c906108c 21965 struct attribute *type_attr;
c906108c 21966
e142c38c 21967 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21968 if (!type_attr)
21969 {
5e22e966 21970 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21971 /* A missing DW_AT_type represents a void type. */
518817b3 21972 return objfile_type (objfile)->builtin_void;
c906108c 21973 }
348e048f 21974
673bfd45 21975 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21976}
21977
b4ba55a1
JB
21978/* True iff CU's producer generates GNAT Ada auxiliary information
21979 that allows to find parallel types through that information instead
21980 of having to do expensive parallel lookups by type name. */
21981
21982static int
21983need_gnat_info (struct dwarf2_cu *cu)
21984{
de4cb04a
JB
21985 /* Assume that the Ada compiler was GNAT, which always produces
21986 the auxiliary information. */
21987 return (cu->language == language_ada);
b4ba55a1
JB
21988}
21989
b4ba55a1
JB
21990/* Return the auxiliary type of the die in question using its
21991 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21992 attribute is not present. */
21993
21994static struct type *
21995die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21996{
b4ba55a1 21997 struct attribute *type_attr;
b4ba55a1
JB
21998
21999 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22000 if (!type_attr)
22001 return NULL;
22002
673bfd45 22003 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22004}
22005
22006/* If DIE has a descriptive_type attribute, then set the TYPE's
22007 descriptive type accordingly. */
22008
22009static void
22010set_descriptive_type (struct type *type, struct die_info *die,
22011 struct dwarf2_cu *cu)
22012{
22013 struct type *descriptive_type = die_descriptive_type (die, cu);
22014
22015 if (descriptive_type)
22016 {
22017 ALLOCATE_GNAT_AUX_TYPE (type);
22018 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22019 }
22020}
22021
c906108c
SS
22022/* Return the containing type of the die in question using its
22023 DW_AT_containing_type attribute. */
22024
22025static struct type *
e7c27a73 22026die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22027{
c906108c 22028 struct attribute *type_attr;
5e22e966 22029 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22030
e142c38c 22031 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22032 if (!type_attr)
22033 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22034 "[in module %s]"), objfile_name (objfile));
33ac96f0 22035
673bfd45 22036 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22037}
22038
ac9ec31b
DE
22039/* Return an error marker type to use for the ill formed type in DIE/CU. */
22040
22041static struct type *
22042build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22043{
976ca316
SM
22044 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22045 struct objfile *objfile = per_objfile->objfile;
528e1572 22046 char *saved;
ac9ec31b 22047
528e1572
SM
22048 std::string message
22049 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22050 objfile_name (objfile),
22051 sect_offset_str (cu->header.sect_off),
22052 sect_offset_str (die->sect_off));
efba19b0 22053 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22054
19f392bc 22055 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22056}
22057
673bfd45 22058/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22059 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22060 DW_AT_containing_type.
673bfd45
DE
22061 If there is no type substitute an error marker. */
22062
c906108c 22063static struct type *
ff39bb5e 22064lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22065 struct dwarf2_cu *cu)
c906108c 22066{
976ca316
SM
22067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22068 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22069 struct type *this_type;
22070
ac9ec31b
DE
22071 gdb_assert (attr->name == DW_AT_type
22072 || attr->name == DW_AT_GNAT_descriptive_type
22073 || attr->name == DW_AT_containing_type);
22074
673bfd45
DE
22075 /* First see if we have it cached. */
22076
36586728
TT
22077 if (attr->form == DW_FORM_GNU_ref_alt)
22078 {
22079 struct dwarf2_per_cu_data *per_cu;
0826b30a 22080 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22081
976ca316
SM
22082 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22083 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22084 }
cd6c91b4 22085 else if (attr->form_is_ref ())
673bfd45 22086 {
0826b30a 22087 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22088
976ca316 22089 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22090 }
55f1336d 22091 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22092 {
630ed6b9 22093 ULONGEST signature = attr->as_signature ();
673bfd45 22094
ac9ec31b 22095 return get_signatured_type (die, signature, cu);
673bfd45
DE
22096 }
22097 else
22098 {
b98664d3 22099 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22100 " at %s [in module %s]"),
22101 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22102 objfile_name (objfile));
ac9ec31b 22103 return build_error_marker_type (cu, die);
673bfd45
DE
22104 }
22105
22106 /* If not cached we need to read it in. */
22107
22108 if (this_type == NULL)
22109 {
ac9ec31b 22110 struct die_info *type_die = NULL;
673bfd45
DE
22111 struct dwarf2_cu *type_cu = cu;
22112
cd6c91b4 22113 if (attr->form_is_ref ())
ac9ec31b
DE
22114 type_die = follow_die_ref (die, attr, &type_cu);
22115 if (type_die == NULL)
22116 return build_error_marker_type (cu, die);
22117 /* If we find the type now, it's probably because the type came
3019eac3
DE
22118 from an inter-CU reference and the type's CU got expanded before
22119 ours. */
ac9ec31b 22120 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22121 }
22122
22123 /* If we still don't have a type use an error marker. */
22124
22125 if (this_type == NULL)
ac9ec31b 22126 return build_error_marker_type (cu, die);
673bfd45 22127
f792889a 22128 return this_type;
c906108c
SS
22129}
22130
673bfd45
DE
22131/* Return the type in DIE, CU.
22132 Returns NULL for invalid types.
22133
02142a6c 22134 This first does a lookup in die_type_hash,
673bfd45
DE
22135 and only reads the die in if necessary.
22136
22137 NOTE: This can be called when reading in partial or full symbols. */
22138
f792889a 22139static struct type *
e7c27a73 22140read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22141{
f792889a
DJ
22142 struct type *this_type;
22143
22144 this_type = get_die_type (die, cu);
22145 if (this_type)
22146 return this_type;
22147
673bfd45
DE
22148 return read_type_die_1 (die, cu);
22149}
22150
22151/* Read the type in DIE, CU.
22152 Returns NULL for invalid types. */
22153
22154static struct type *
22155read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22156{
22157 struct type *this_type = NULL;
22158
c906108c
SS
22159 switch (die->tag)
22160 {
22161 case DW_TAG_class_type:
680b30c7 22162 case DW_TAG_interface_type:
c906108c
SS
22163 case DW_TAG_structure_type:
22164 case DW_TAG_union_type:
f792889a 22165 this_type = read_structure_type (die, cu);
c906108c
SS
22166 break;
22167 case DW_TAG_enumeration_type:
f792889a 22168 this_type = read_enumeration_type (die, cu);
c906108c
SS
22169 break;
22170 case DW_TAG_subprogram:
22171 case DW_TAG_subroutine_type:
edb3359d 22172 case DW_TAG_inlined_subroutine:
f792889a 22173 this_type = read_subroutine_type (die, cu);
c906108c
SS
22174 break;
22175 case DW_TAG_array_type:
f792889a 22176 this_type = read_array_type (die, cu);
c906108c 22177 break;
72019c9c 22178 case DW_TAG_set_type:
f792889a 22179 this_type = read_set_type (die, cu);
72019c9c 22180 break;
c906108c 22181 case DW_TAG_pointer_type:
f792889a 22182 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22183 break;
22184 case DW_TAG_ptr_to_member_type:
f792889a 22185 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22186 break;
22187 case DW_TAG_reference_type:
4297a3f0
AV
22188 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22189 break;
22190 case DW_TAG_rvalue_reference_type:
22191 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22192 break;
22193 case DW_TAG_const_type:
f792889a 22194 this_type = read_tag_const_type (die, cu);
c906108c
SS
22195 break;
22196 case DW_TAG_volatile_type:
f792889a 22197 this_type = read_tag_volatile_type (die, cu);
c906108c 22198 break;
06d66ee9
TT
22199 case DW_TAG_restrict_type:
22200 this_type = read_tag_restrict_type (die, cu);
22201 break;
c906108c 22202 case DW_TAG_string_type:
f792889a 22203 this_type = read_tag_string_type (die, cu);
c906108c
SS
22204 break;
22205 case DW_TAG_typedef:
f792889a 22206 this_type = read_typedef (die, cu);
c906108c 22207 break;
a02abb62 22208 case DW_TAG_subrange_type:
f792889a 22209 this_type = read_subrange_type (die, cu);
a02abb62 22210 break;
c906108c 22211 case DW_TAG_base_type:
f792889a 22212 this_type = read_base_type (die, cu);
c906108c 22213 break;
81a17f79 22214 case DW_TAG_unspecified_type:
f792889a 22215 this_type = read_unspecified_type (die, cu);
81a17f79 22216 break;
0114d602
DJ
22217 case DW_TAG_namespace:
22218 this_type = read_namespace_type (die, cu);
22219 break;
f55ee35c
JK
22220 case DW_TAG_module:
22221 this_type = read_module_type (die, cu);
22222 break;
a2c2acaf
MW
22223 case DW_TAG_atomic_type:
22224 this_type = read_tag_atomic_type (die, cu);
22225 break;
c906108c 22226 default:
b98664d3 22227 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22228 dwarf_tag_name (die->tag));
c906108c
SS
22229 break;
22230 }
63d06c5c 22231
f792889a 22232 return this_type;
63d06c5c
DC
22233}
22234
abc72ce4
DE
22235/* See if we can figure out if the class lives in a namespace. We do
22236 this by looking for a member function; its demangled name will
22237 contain namespace info, if there is any.
22238 Return the computed name or NULL.
22239 Space for the result is allocated on the objfile's obstack.
22240 This is the full-die version of guess_partial_die_structure_name.
22241 In this case we know DIE has no useful parent. */
22242
43816ebc 22243static const char *
abc72ce4
DE
22244guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22245{
22246 struct die_info *spec_die;
22247 struct dwarf2_cu *spec_cu;
22248 struct die_info *child;
5e22e966 22249 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22250
22251 spec_cu = cu;
22252 spec_die = die_specification (die, &spec_cu);
22253 if (spec_die != NULL)
22254 {
22255 die = spec_die;
22256 cu = spec_cu;
22257 }
22258
22259 for (child = die->child;
22260 child != NULL;
22261 child = child->sibling)
22262 {
22263 if (child->tag == DW_TAG_subprogram)
22264 {
73b9be8b 22265 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22266
7d45c7c3 22267 if (linkage_name != NULL)
abc72ce4 22268 {
43816ebc 22269 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22270 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22271 const char *name = NULL;
abc72ce4
DE
22272
22273 if (actual_name != NULL)
22274 {
15d034d0 22275 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22276
22277 if (die_name != NULL
43816ebc 22278 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22279 {
22280 /* Strip off the class name from the full name.
22281 We want the prefix. */
22282 int die_name_len = strlen (die_name);
43816ebc
TT
22283 int actual_name_len = strlen (actual_name.get ());
22284 const char *ptr = actual_name.get ();
abc72ce4
DE
22285
22286 /* Test for '::' as a sanity check. */
22287 if (actual_name_len > die_name_len + 2
43816ebc 22288 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22289 name = obstack_strndup (
e3b94546 22290 &objfile->per_bfd->storage_obstack,
43816ebc 22291 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22292 }
22293 }
abc72ce4
DE
22294 return name;
22295 }
22296 }
22297 }
22298
22299 return NULL;
22300}
22301
96408a79
SA
22302/* GCC might emit a nameless typedef that has a linkage name. Determine the
22303 prefix part in such case. See
22304 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22305
a121b7c1 22306static const char *
96408a79
SA
22307anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22308{
22309 struct attribute *attr;
e6a959d6 22310 const char *base;
96408a79
SA
22311
22312 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22313 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22314 return NULL;
22315
7d45c7c3 22316 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22317 return NULL;
22318
73b9be8b 22319 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22320 const char *attr_name = attr->as_string ();
22321 if (attr == NULL || attr_name == NULL)
96408a79
SA
22322 return NULL;
22323
22324 /* dwarf2_name had to be already called. */
3b64bf15 22325 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22326
22327 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22328 base = strrchr (attr_name, ':');
22329 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22330 return "";
22331
5e22e966 22332 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22333 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22334 attr_name,
22335 &base[-1] - attr_name);
96408a79
SA
22336}
22337
fdde2d81 22338/* Return the name of the namespace/class that DIE is defined within,
0114d602 22339 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22340
0114d602
DJ
22341 For example, if we're within the method foo() in the following
22342 code:
22343
22344 namespace N {
22345 class C {
22346 void foo () {
22347 }
22348 };
22349 }
22350
22351 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22352
0d5cff50 22353static const char *
e142c38c 22354determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22355{
976ca316 22356 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22357 struct die_info *parent, *spec_die;
22358 struct dwarf2_cu *spec_cu;
22359 struct type *parent_type;
a121b7c1 22360 const char *retval;
63d06c5c 22361
9c37b5ae 22362 if (cu->language != language_cplus
c44af4eb
TT
22363 && cu->language != language_fortran && cu->language != language_d
22364 && cu->language != language_rust)
0114d602
DJ
22365 return "";
22366
96408a79
SA
22367 retval = anonymous_struct_prefix (die, cu);
22368 if (retval)
22369 return retval;
22370
0114d602
DJ
22371 /* We have to be careful in the presence of DW_AT_specification.
22372 For example, with GCC 3.4, given the code
22373
22374 namespace N {
22375 void foo() {
22376 // Definition of N::foo.
22377 }
22378 }
22379
22380 then we'll have a tree of DIEs like this:
22381
22382 1: DW_TAG_compile_unit
22383 2: DW_TAG_namespace // N
22384 3: DW_TAG_subprogram // declaration of N::foo
22385 4: DW_TAG_subprogram // definition of N::foo
22386 DW_AT_specification // refers to die #3
22387
22388 Thus, when processing die #4, we have to pretend that we're in
22389 the context of its DW_AT_specification, namely the contex of die
22390 #3. */
22391 spec_cu = cu;
22392 spec_die = die_specification (die, &spec_cu);
22393 if (spec_die == NULL)
22394 parent = die->parent;
22395 else
63d06c5c 22396 {
0114d602
DJ
22397 parent = spec_die->parent;
22398 cu = spec_cu;
63d06c5c 22399 }
0114d602
DJ
22400
22401 if (parent == NULL)
22402 return "";
98bfdba5
PA
22403 else if (parent->building_fullname)
22404 {
22405 const char *name;
22406 const char *parent_name;
22407
22408 /* It has been seen on RealView 2.2 built binaries,
22409 DW_TAG_template_type_param types actually _defined_ as
22410 children of the parent class:
22411
22412 enum E {};
22413 template class <class Enum> Class{};
22414 Class<enum E> class_e;
22415
22416 1: DW_TAG_class_type (Class)
22417 2: DW_TAG_enumeration_type (E)
22418 3: DW_TAG_enumerator (enum1:0)
22419 3: DW_TAG_enumerator (enum2:1)
22420 ...
22421 2: DW_TAG_template_type_param
22422 DW_AT_type DW_FORM_ref_udata (E)
22423
22424 Besides being broken debug info, it can put GDB into an
22425 infinite loop. Consider:
22426
22427 When we're building the full name for Class<E>, we'll start
22428 at Class, and go look over its template type parameters,
22429 finding E. We'll then try to build the full name of E, and
22430 reach here. We're now trying to build the full name of E,
22431 and look over the parent DIE for containing scope. In the
22432 broken case, if we followed the parent DIE of E, we'd again
22433 find Class, and once again go look at its template type
22434 arguments, etc., etc. Simply don't consider such parent die
22435 as source-level parent of this die (it can't be, the language
22436 doesn't allow it), and break the loop here. */
22437 name = dwarf2_name (die, cu);
22438 parent_name = dwarf2_name (parent, cu);
b98664d3 22439 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22440 name ? name : "<unknown>",
22441 parent_name ? parent_name : "<unknown>");
22442 return "";
22443 }
63d06c5c 22444 else
0114d602
DJ
22445 switch (parent->tag)
22446 {
63d06c5c 22447 case DW_TAG_namespace:
0114d602 22448 parent_type = read_type_die (parent, cu);
acebe513
UW
22449 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22450 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22451 Work around this problem here. */
22452 if (cu->language == language_cplus
7d93a1e0 22453 && strcmp (parent_type->name (), "::") == 0)
acebe513 22454 return "";
0114d602 22455 /* We give a name to even anonymous namespaces. */
7d93a1e0 22456 return parent_type->name ();
63d06c5c 22457 case DW_TAG_class_type:
680b30c7 22458 case DW_TAG_interface_type:
63d06c5c 22459 case DW_TAG_structure_type:
0114d602 22460 case DW_TAG_union_type:
f55ee35c 22461 case DW_TAG_module:
0114d602 22462 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22463 if (parent_type->name () != NULL)
22464 return parent_type->name ();
0114d602
DJ
22465 else
22466 /* An anonymous structure is only allowed non-static data
22467 members; no typedefs, no member functions, et cetera.
22468 So it does not need a prefix. */
22469 return "";
abc72ce4 22470 case DW_TAG_compile_unit:
95554aad 22471 case DW_TAG_partial_unit:
abc72ce4
DE
22472 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22473 if (cu->language == language_cplus
976ca316 22474 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22475 && die->child != NULL
22476 && (die->tag == DW_TAG_class_type
22477 || die->tag == DW_TAG_structure_type
22478 || die->tag == DW_TAG_union_type))
22479 {
43816ebc 22480 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22481 if (name != NULL)
22482 return name;
22483 }
22484 return "";
0a4b0913
AB
22485 case DW_TAG_subprogram:
22486 /* Nested subroutines in Fortran get a prefix with the name
22487 of the parent's subroutine. */
22488 if (cu->language == language_fortran)
22489 {
22490 if ((die->tag == DW_TAG_subprogram)
22491 && (dwarf2_name (parent, cu) != NULL))
22492 return dwarf2_name (parent, cu);
22493 }
22494 return determine_prefix (parent, cu);
3d567982
TT
22495 case DW_TAG_enumeration_type:
22496 parent_type = read_type_die (parent, cu);
22497 if (TYPE_DECLARED_CLASS (parent_type))
22498 {
7d93a1e0
SM
22499 if (parent_type->name () != NULL)
22500 return parent_type->name ();
3d567982
TT
22501 return "";
22502 }
22503 /* Fall through. */
63d06c5c 22504 default:
8176b9b8 22505 return determine_prefix (parent, cu);
63d06c5c 22506 }
63d06c5c
DC
22507}
22508
3e43a32a
MS
22509/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22510 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22511 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22512 an obconcat, otherwise allocate storage for the result. The CU argument is
22513 used to determine the language and hence, the appropriate separator. */
987504bb 22514
f55ee35c 22515#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22516
22517static char *
f55ee35c
JK
22518typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22519 int physname, struct dwarf2_cu *cu)
63d06c5c 22520{
f55ee35c 22521 const char *lead = "";
5c315b68 22522 const char *sep;
63d06c5c 22523
3e43a32a
MS
22524 if (suffix == NULL || suffix[0] == '\0'
22525 || prefix == NULL || prefix[0] == '\0')
987504bb 22526 sep = "";
45280282
IB
22527 else if (cu->language == language_d)
22528 {
22529 /* For D, the 'main' function could be defined in any module, but it
22530 should never be prefixed. */
22531 if (strcmp (suffix, "D main") == 0)
22532 {
22533 prefix = "";
22534 sep = "";
22535 }
22536 else
22537 sep = ".";
22538 }
f55ee35c
JK
22539 else if (cu->language == language_fortran && physname)
22540 {
22541 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22542 DW_AT_MIPS_linkage_name is preferred and used instead. */
22543
22544 lead = "__";
22545 sep = "_MOD_";
22546 }
987504bb
JJ
22547 else
22548 sep = "::";
63d06c5c 22549
6dd47d34
DE
22550 if (prefix == NULL)
22551 prefix = "";
22552 if (suffix == NULL)
22553 suffix = "";
22554
987504bb
JJ
22555 if (obs == NULL)
22556 {
3e43a32a 22557 char *retval
224c3ddb
SM
22558 = ((char *)
22559 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22560
f55ee35c
JK
22561 strcpy (retval, lead);
22562 strcat (retval, prefix);
6dd47d34
DE
22563 strcat (retval, sep);
22564 strcat (retval, suffix);
63d06c5c
DC
22565 return retval;
22566 }
987504bb
JJ
22567 else
22568 {
22569 /* We have an obstack. */
f55ee35c 22570 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22571 }
63d06c5c
DC
22572}
22573
71c25dea
TT
22574/* Get name of a die, return NULL if not found. */
22575
15d034d0
TT
22576static const char *
22577dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22578 struct objfile *objfile)
71c25dea
TT
22579{
22580 if (name && cu->language == language_cplus)
22581 {
596dc4ad
TT
22582 gdb::unique_xmalloc_ptr<char> canon_name
22583 = cp_canonicalize_string (name);
71c25dea 22584
596dc4ad
TT
22585 if (canon_name != nullptr)
22586 name = objfile->intern (canon_name.get ());
71c25dea
TT
22587 }
22588
22589 return name;
c906108c
SS
22590}
22591
96553a0c
DE
22592/* Get name of a die, return NULL if not found.
22593 Anonymous namespaces are converted to their magic string. */
9219021c 22594
15d034d0 22595static const char *
e142c38c 22596dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22597{
22598 struct attribute *attr;
5e22e966 22599 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22600
e142c38c 22601 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22602 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22603 if (attr_name == nullptr
96553a0c 22604 && die->tag != DW_TAG_namespace
53832f31
TT
22605 && die->tag != DW_TAG_class_type
22606 && die->tag != DW_TAG_interface_type
22607 && die->tag != DW_TAG_structure_type
22608 && die->tag != DW_TAG_union_type)
71c25dea
TT
22609 return NULL;
22610
22611 switch (die->tag)
22612 {
22613 case DW_TAG_compile_unit:
95554aad 22614 case DW_TAG_partial_unit:
71c25dea
TT
22615 /* Compilation units have a DW_AT_name that is a filename, not
22616 a source language identifier. */
22617 case DW_TAG_enumeration_type:
22618 case DW_TAG_enumerator:
22619 /* These tags always have simple identifiers already; no need
22620 to canonicalize them. */
2c830f54 22621 return attr_name;
907af001 22622
96553a0c 22623 case DW_TAG_namespace:
2c830f54
TT
22624 if (attr_name != nullptr)
22625 return attr_name;
96553a0c
DE
22626 return CP_ANONYMOUS_NAMESPACE_STR;
22627
907af001
UW
22628 case DW_TAG_class_type:
22629 case DW_TAG_interface_type:
22630 case DW_TAG_structure_type:
22631 case DW_TAG_union_type:
22632 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22633 structures or unions. These were of the form "._%d" in GCC 4.1,
22634 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22635 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22636 if (attr_name != nullptr
22637 && (startswith (attr_name, "._")
22638 || startswith (attr_name, "<anonymous")))
907af001 22639 return NULL;
53832f31
TT
22640
22641 /* GCC might emit a nameless typedef that has a linkage name. See
22642 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22643 if (!attr || attr_name == NULL)
53832f31 22644 {
73b9be8b 22645 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 22646 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 22647 if (attr == NULL || attr_name == NULL)
53832f31
TT
22648 return NULL;
22649
2c830f54 22650 /* Avoid demangling attr_name the second time on a second
df5c6c50 22651 call for the same DIE. */
3b64bf15 22652 if (!attr->canonical_string_p ())
53832f31 22653 {
43816ebc 22654 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22655 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22656 if (demangled == nullptr)
22657 return nullptr;
43816ebc 22658
c6481205 22659 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 22660 attr_name = attr->as_string ();
53832f31 22661 }
67430cd0 22662
2c830f54
TT
22663 /* Strip any leading namespaces/classes, keep only the
22664 base name. DW_AT_name for named DIEs does not
22665 contain the prefixes. */
22666 const char *base = strrchr (attr_name, ':');
22667 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22668 return &base[1];
22669 else
2c830f54 22670 return attr_name;
53832f31 22671 }
907af001
UW
22672 break;
22673
71c25dea 22674 default:
907af001
UW
22675 break;
22676 }
22677
3b64bf15 22678 if (!attr->canonical_string_p ())
c6481205
TT
22679 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22680 objfile));
2c830f54 22681 return attr->as_string ();
9219021c
DC
22682}
22683
22684/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22685 is none. *EXT_CU is the CU containing DIE on input, and the CU
22686 containing the return value on output. */
9219021c
DC
22687
22688static struct die_info *
f2f0e013 22689dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22690{
22691 struct attribute *attr;
9219021c 22692
f2f0e013 22693 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22694 if (attr == NULL)
22695 return NULL;
22696
f2f0e013 22697 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22698}
22699
f9aca02d 22700static void
d97bc12b 22701dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22702{
22703 unsigned int i;
22704
d97bc12b 22705 print_spaces (indent, f);
9d8780f0 22706 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22707 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22708 sect_offset_str (die->sect_off));
d97bc12b
DE
22709
22710 if (die->parent != NULL)
22711 {
22712 print_spaces (indent, f);
9d8780f0
SM
22713 fprintf_unfiltered (f, " parent at offset: %s\n",
22714 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22715 }
22716
22717 print_spaces (indent, f);
22718 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22719 dwarf_bool_name (die->child != NULL));
c906108c 22720
d97bc12b
DE
22721 print_spaces (indent, f);
22722 fprintf_unfiltered (f, " attributes:\n");
22723
c906108c
SS
22724 for (i = 0; i < die->num_attrs; ++i)
22725 {
d97bc12b
DE
22726 print_spaces (indent, f);
22727 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22728 dwarf_attr_name (die->attrs[i].name),
22729 dwarf_form_name (die->attrs[i].form));
d97bc12b 22730
c906108c
SS
22731 switch (die->attrs[i].form)
22732 {
c906108c 22733 case DW_FORM_addr:
336d760d 22734 case DW_FORM_addrx:
3019eac3 22735 case DW_FORM_GNU_addr_index:
d97bc12b 22736 fprintf_unfiltered (f, "address: ");
36d378cf 22737 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22738 break;
22739 case DW_FORM_block2:
22740 case DW_FORM_block4:
22741 case DW_FORM_block:
22742 case DW_FORM_block1:
56eb65bd 22743 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22744 pulongest (die->attrs[i].as_block ()->size));
c906108c 22745 break;
2dc7f7b3 22746 case DW_FORM_exprloc:
56eb65bd 22747 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22748 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22749 break;
0224619f
JK
22750 case DW_FORM_data16:
22751 fprintf_unfiltered (f, "constant of 16 bytes");
22752 break;
4568ecf9
DE
22753 case DW_FORM_ref_addr:
22754 fprintf_unfiltered (f, "ref address: ");
529908cb 22755 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 22756 break;
36586728
TT
22757 case DW_FORM_GNU_ref_alt:
22758 fprintf_unfiltered (f, "alt ref address: ");
529908cb 22759 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 22760 break;
10b3939b
DJ
22761 case DW_FORM_ref1:
22762 case DW_FORM_ref2:
22763 case DW_FORM_ref4:
4568ecf9
DE
22764 case DW_FORM_ref8:
22765 case DW_FORM_ref_udata:
d97bc12b 22766 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 22767 (long) (die->attrs[i].as_unsigned ()));
10b3939b 22768 break;
c906108c
SS
22769 case DW_FORM_data1:
22770 case DW_FORM_data2:
22771 case DW_FORM_data4:
ce5d95e1 22772 case DW_FORM_data8:
c906108c 22773 case DW_FORM_udata:
43bbcdc2 22774 fprintf_unfiltered (f, "constant: %s",
529908cb 22775 pulongest (die->attrs[i].as_unsigned ()));
c906108c 22776 break;
2dc7f7b3
TT
22777 case DW_FORM_sec_offset:
22778 fprintf_unfiltered (f, "section offset: %s",
529908cb 22779 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 22780 break;
55f1336d 22781 case DW_FORM_ref_sig8:
ac9ec31b 22782 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22783 hex_string (die->attrs[i].as_signature ()));
348e048f 22784 break;
c906108c 22785 case DW_FORM_string:
4bdf3d34 22786 case DW_FORM_strp:
43988095 22787 case DW_FORM_line_strp:
cf532bd1 22788 case DW_FORM_strx:
3019eac3 22789 case DW_FORM_GNU_str_index:
36586728 22790 case DW_FORM_GNU_strp_alt:
8285870a 22791 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22792 die->attrs[i].as_string ()
22793 ? die->attrs[i].as_string () : "",
22794 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22795 break;
22796 case DW_FORM_flag:
c45bc3f8 22797 if (die->attrs[i].as_boolean ())
d97bc12b 22798 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22799 else
d97bc12b 22800 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22801 break;
2dc7f7b3
TT
22802 case DW_FORM_flag_present:
22803 fprintf_unfiltered (f, "flag: TRUE");
22804 break;
a8329558 22805 case DW_FORM_indirect:
0963b4bd
MS
22806 /* The reader will have reduced the indirect form to
22807 the "base form" so this form should not occur. */
5f48f8f3 22808 fprintf_unfiltered (f,
3e43a32a 22809 "unexpected attribute form: DW_FORM_indirect");
a8329558 22810 break;
1bc397c5 22811 case DW_FORM_sdata:
663c44ac
JK
22812 case DW_FORM_implicit_const:
22813 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22814 plongest (die->attrs[i].as_signed ()));
663c44ac 22815 break;
c906108c 22816 default:
d97bc12b 22817 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22818 die->attrs[i].form);
d97bc12b 22819 break;
c906108c 22820 }
d97bc12b 22821 fprintf_unfiltered (f, "\n");
c906108c
SS
22822 }
22823}
22824
f9aca02d 22825static void
d97bc12b 22826dump_die_for_error (struct die_info *die)
c906108c 22827{
d97bc12b
DE
22828 dump_die_shallow (gdb_stderr, 0, die);
22829}
22830
22831static void
22832dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22833{
22834 int indent = level * 4;
22835
22836 gdb_assert (die != NULL);
22837
22838 if (level >= max_level)
22839 return;
22840
22841 dump_die_shallow (f, indent, die);
22842
22843 if (die->child != NULL)
c906108c 22844 {
d97bc12b
DE
22845 print_spaces (indent, f);
22846 fprintf_unfiltered (f, " Children:");
22847 if (level + 1 < max_level)
22848 {
22849 fprintf_unfiltered (f, "\n");
22850 dump_die_1 (f, level + 1, max_level, die->child);
22851 }
22852 else
22853 {
3e43a32a
MS
22854 fprintf_unfiltered (f,
22855 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22856 }
22857 }
22858
22859 if (die->sibling != NULL && level > 0)
22860 {
22861 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22862 }
22863}
22864
d97bc12b
DE
22865/* This is called from the pdie macro in gdbinit.in.
22866 It's not static so gcc will keep a copy callable from gdb. */
22867
22868void
22869dump_die (struct die_info *die, int max_level)
22870{
22871 dump_die_1 (gdb_stdlog, 0, max_level, die);
22872}
22873
f9aca02d 22874static void
51545339 22875store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22876{
51545339 22877 void **slot;
c906108c 22878
9c541725
PA
22879 slot = htab_find_slot_with_hash (cu->die_hash, die,
22880 to_underlying (die->sect_off),
b64f50a1 22881 INSERT);
51545339
DJ
22882
22883 *slot = die;
c906108c
SS
22884}
22885
348e048f
DE
22886/* Follow reference or signature attribute ATTR of SRC_DIE.
22887 On entry *REF_CU is the CU of SRC_DIE.
22888 On exit *REF_CU is the CU of the result. */
22889
22890static struct die_info *
ff39bb5e 22891follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22892 struct dwarf2_cu **ref_cu)
22893{
22894 struct die_info *die;
22895
cd6c91b4 22896 if (attr->form_is_ref ())
348e048f 22897 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22898 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22899 die = follow_die_sig (src_die, attr, ref_cu);
22900 else
22901 {
22902 dump_die_for_error (src_die);
22903 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22904 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22905 }
22906
22907 return die;
03dd20cc
DJ
22908}
22909
5c631832 22910/* Follow reference OFFSET.
673bfd45
DE
22911 On entry *REF_CU is the CU of the source die referencing OFFSET.
22912 On exit *REF_CU is the CU of the result.
22913 Returns NULL if OFFSET is invalid. */
f504f079 22914
f9aca02d 22915static struct die_info *
9c541725 22916follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22917 struct dwarf2_cu **ref_cu)
c906108c 22918{
10b3939b 22919 struct die_info temp_die;
f2f0e013 22920 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22921 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22922
348e048f
DE
22923 gdb_assert (cu->per_cu != NULL);
22924
98bfdba5
PA
22925 target_cu = cu;
22926
3019eac3 22927 if (cu->per_cu->is_debug_types)
348e048f
DE
22928 {
22929 /* .debug_types CUs cannot reference anything outside their CU.
22930 If they need to, they have to reference a signatured type via
55f1336d 22931 DW_FORM_ref_sig8. */
4057dfde 22932 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22933 return NULL;
348e048f 22934 }
36586728 22935 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22936 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22937 {
22938 struct dwarf2_per_cu_data *per_cu;
9a619af0 22939
9c541725 22940 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22941 per_objfile);
03dd20cc
DJ
22942
22943 /* If necessary, add it to the queue and load its DIEs. */
976ca316 22944 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
22945 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
22946 false, cu->language);
03dd20cc 22947
976ca316 22948 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22949 }
98bfdba5
PA
22950 else if (cu->dies == NULL)
22951 {
22952 /* We're loading full DIEs during partial symbol reading. */
976ca316 22953 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
22954 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
22955 language_minimal);
98bfdba5 22956 }
c906108c 22957
f2f0e013 22958 *ref_cu = target_cu;
9c541725 22959 temp_die.sect_off = sect_off;
c24bdb02
KS
22960
22961 if (target_cu != cu)
22962 target_cu->ancestor = cu;
22963
9a3c8263 22964 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22965 &temp_die,
22966 to_underlying (sect_off));
5c631832 22967}
10b3939b 22968
5c631832
JK
22969/* Follow reference attribute ATTR of SRC_DIE.
22970 On entry *REF_CU is the CU of SRC_DIE.
22971 On exit *REF_CU is the CU of the result. */
22972
22973static struct die_info *
ff39bb5e 22974follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22975 struct dwarf2_cu **ref_cu)
22976{
0826b30a 22977 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22978 struct dwarf2_cu *cu = *ref_cu;
22979 struct die_info *die;
22980
9c541725 22981 die = follow_die_offset (sect_off,
36586728
TT
22982 (attr->form == DW_FORM_GNU_ref_alt
22983 || cu->per_cu->is_dwz),
22984 ref_cu);
5c631832 22985 if (!die)
9d8780f0
SM
22986 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22987 "at %s [in module %s]"),
22988 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22989 objfile_name (cu->per_objfile->objfile));
348e048f 22990
5c631832
JK
22991 return die;
22992}
22993
d4c9a4f8 22994/* See read.h. */
5c631832
JK
22995
22996struct dwarf2_locexpr_baton
9c541725 22997dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22998 dwarf2_per_cu_data *per_cu,
976ca316 22999 dwarf2_per_objfile *per_objfile,
041d9819
SM
23000 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23001 bool resolve_abstract_p)
5c631832 23002{
5c631832
JK
23003 struct die_info *die;
23004 struct attribute *attr;
23005 struct dwarf2_locexpr_baton retval;
976ca316 23006 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23007
976ca316 23008 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23009 if (cu == nullptr)
976ca316 23010 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23011
23012 if (cu == nullptr)
cc12ce38
DE
23013 {
23014 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23015 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23016 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23017 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23018 }
918dd910 23019
9c541725 23020 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23021 if (!die)
9d8780f0
SM
23022 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23023 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23024
23025 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23026 if (!attr && resolve_abstract_p
976ca316
SM
23027 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23028 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23029 {
041d9819 23030 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23031 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23032 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23033
3360b6e7 23034 for (const auto &cand_off
976ca316 23035 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23036 {
3360b6e7
TV
23037 struct dwarf2_cu *cand_cu = cu;
23038 struct die_info *cand
23039 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23040 if (!cand
23041 || !cand->parent
e4a62c65
TV
23042 || cand->parent->tag != DW_TAG_subprogram)
23043 continue;
23044
23045 CORE_ADDR pc_low, pc_high;
23046 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23047 if (pc_low == ((CORE_ADDR) -1))
23048 continue;
23049 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23050 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23051 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23052 continue;
23053
23054 die = cand;
23055 attr = dwarf2_attr (die, DW_AT_location, cu);
23056 break;
23057 }
23058 }
23059
5c631832
JK
23060 if (!attr)
23061 {
e103e986
JK
23062 /* DWARF: "If there is no such attribute, then there is no effect.".
23063 DATA is ignored if SIZE is 0. */
5c631832 23064
e103e986 23065 retval.data = NULL;
5c631832
JK
23066 retval.size = 0;
23067 }
cd6c91b4 23068 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23069 {
23070 struct dwarf2_loclist_baton loclist_baton;
041d9819 23071 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23072 size_t size;
23073
23074 fill_in_loclist_baton (cu, &loclist_baton, attr);
23075
23076 retval.data = dwarf2_find_location_expression (&loclist_baton,
23077 &size, pc);
23078 retval.size = size;
23079 }
5c631832
JK
23080 else
23081 {
4fc6c0d5 23082 if (!attr->form_is_block ())
9d8780f0 23083 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23084 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23085 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23086
9d2246fc
TT
23087 struct dwarf_block *block = attr->as_block ();
23088 retval.data = block->data;
23089 retval.size = block->size;
5c631832 23090 }
976ca316 23091 retval.per_objfile = per_objfile;
5c631832 23092 retval.per_cu = cu->per_cu;
918dd910 23093
976ca316 23094 per_objfile->age_comp_units ();
918dd910 23095
5c631832 23096 return retval;
348e048f
DE
23097}
23098
d4c9a4f8 23099/* See read.h. */
8b9737bf
TT
23100
23101struct dwarf2_locexpr_baton
23102dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23103 dwarf2_per_cu_data *per_cu,
14095eb3 23104 dwarf2_per_objfile *per_objfile,
041d9819 23105 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23106{
9c541725 23107 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23108
14095eb3 23109 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23110 get_frame_pc);
8b9737bf
TT
23111}
23112
b6807d98
TT
23113/* Write a constant of a given type as target-ordered bytes into
23114 OBSTACK. */
23115
23116static const gdb_byte *
23117write_constant_as_bytes (struct obstack *obstack,
23118 enum bfd_endian byte_order,
23119 struct type *type,
23120 ULONGEST value,
23121 LONGEST *len)
23122{
23123 gdb_byte *result;
23124
23125 *len = TYPE_LENGTH (type);
224c3ddb 23126 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23127 store_unsigned_integer (result, *len, byte_order, value);
23128
23129 return result;
23130}
23131
d4c9a4f8 23132/* See read.h. */
b6807d98
TT
23133
23134const gdb_byte *
9c541725 23135dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23136 dwarf2_per_cu_data *per_cu,
14095eb3 23137 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23138 obstack *obstack,
b6807d98
TT
23139 LONGEST *len)
23140{
b6807d98
TT
23141 struct die_info *die;
23142 struct attribute *attr;
23143 const gdb_byte *result = NULL;
23144 struct type *type;
23145 LONGEST value;
23146 enum bfd_endian byte_order;
14095eb3 23147 struct objfile *objfile = per_objfile->objfile;
b6807d98 23148
7188ed02 23149 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23150 if (cu == nullptr)
23151 cu = load_cu (per_cu, per_objfile, false);
23152
23153 if (cu == nullptr)
cc12ce38
DE
23154 {
23155 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23156 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23157 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23158 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23159 }
b6807d98 23160
9c541725 23161 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23162 if (!die)
9d8780f0
SM
23163 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23164 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23165
23166 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23167 if (attr == NULL)
23168 return NULL;
23169
e3b94546 23170 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23171 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23172
23173 switch (attr->form)
23174 {
23175 case DW_FORM_addr:
336d760d 23176 case DW_FORM_addrx:
b6807d98
TT
23177 case DW_FORM_GNU_addr_index:
23178 {
23179 gdb_byte *tem;
23180
23181 *len = cu->header.addr_size;
224c3ddb 23182 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23183 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23184 result = tem;
23185 }
23186 break;
23187 case DW_FORM_string:
23188 case DW_FORM_strp:
cf532bd1 23189 case DW_FORM_strx:
b6807d98
TT
23190 case DW_FORM_GNU_str_index:
23191 case DW_FORM_GNU_strp_alt:
c6481205 23192 /* The string is already allocated on the objfile obstack, point
b6807d98 23193 directly to it. */
2c830f54
TT
23194 {
23195 const char *attr_name = attr->as_string ();
23196 result = (const gdb_byte *) attr_name;
23197 *len = strlen (attr_name);
23198 }
b6807d98
TT
23199 break;
23200 case DW_FORM_block1:
23201 case DW_FORM_block2:
23202 case DW_FORM_block4:
23203 case DW_FORM_block:
23204 case DW_FORM_exprloc:
0224619f 23205 case DW_FORM_data16:
9d2246fc
TT
23206 {
23207 struct dwarf_block *block = attr->as_block ();
23208 result = block->data;
23209 *len = block->size;
23210 }
b6807d98
TT
23211 break;
23212
23213 /* The DW_AT_const_value attributes are supposed to carry the
23214 symbol's value "represented as it would be on the target
23215 architecture." By the time we get here, it's already been
23216 converted to host endianness, so we just need to sign- or
23217 zero-extend it as appropriate. */
23218 case DW_FORM_data1:
23219 type = die_type (die, cu);
23220 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23221 if (result == NULL)
23222 result = write_constant_as_bytes (obstack, byte_order,
23223 type, value, len);
23224 break;
23225 case DW_FORM_data2:
23226 type = die_type (die, cu);
23227 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23228 if (result == NULL)
23229 result = write_constant_as_bytes (obstack, byte_order,
23230 type, value, len);
23231 break;
23232 case DW_FORM_data4:
23233 type = die_type (die, cu);
23234 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23235 if (result == NULL)
23236 result = write_constant_as_bytes (obstack, byte_order,
23237 type, value, len);
23238 break;
23239 case DW_FORM_data8:
23240 type = die_type (die, cu);
23241 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23242 if (result == NULL)
23243 result = write_constant_as_bytes (obstack, byte_order,
23244 type, value, len);
23245 break;
23246
23247 case DW_FORM_sdata:
663c44ac 23248 case DW_FORM_implicit_const:
b6807d98
TT
23249 type = die_type (die, cu);
23250 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23251 type, attr->as_signed (), len);
b6807d98
TT
23252 break;
23253
23254 case DW_FORM_udata:
23255 type = die_type (die, cu);
23256 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23257 type, attr->as_unsigned (), len);
b6807d98
TT
23258 break;
23259
23260 default:
b98664d3 23261 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23262 dwarf_form_name (attr->form));
23263 break;
23264 }
23265
23266 return result;
23267}
23268
d4c9a4f8 23269/* See read.h. */
7942e96e
AA
23270
23271struct type *
9c541725 23272dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23273 dwarf2_per_cu_data *per_cu,
23274 dwarf2_per_objfile *per_objfile)
7942e96e 23275{
7942e96e
AA
23276 struct die_info *die;
23277
7188ed02 23278 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23279 if (cu == nullptr)
23280 cu = load_cu (per_cu, per_objfile, false);
23281
23282 if (cu == nullptr)
23283 return nullptr;
7942e96e 23284
9c541725 23285 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23286 if (!die)
23287 return NULL;
23288
23289 return die_type (die, cu);
23290}
23291
8cb5117c 23292/* See read.h. */
8a9b8146
TT
23293
23294struct type *
b64f50a1 23295dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23296 dwarf2_per_cu_data *per_cu,
23297 dwarf2_per_objfile *per_objfile)
8a9b8146 23298{
9c541725 23299 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23300 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23301}
23302
ac9ec31b 23303/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23304 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23305 On exit *REF_CU is the CU of the result.
23306 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23307
23308static struct die_info *
ac9ec31b
DE
23309follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23310 struct dwarf2_cu **ref_cu)
348e048f 23311{
348e048f 23312 struct die_info temp_die;
c24bdb02 23313 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23314 struct die_info *die;
976ca316 23315 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23316
348e048f 23317
ac9ec31b
DE
23318 /* While it might be nice to assert sig_type->type == NULL here,
23319 we can get here for DW_AT_imported_declaration where we need
23320 the DIE not the type. */
348e048f
DE
23321
23322 /* If necessary, add it to the queue and load its DIEs. */
23323
976ca316 23324 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 23325 language_minimal))
976ca316 23326 read_signatured_type (sig_type, per_objfile);
348e048f 23327
976ca316 23328 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23329 gdb_assert (sig_cu != NULL);
9c541725
PA
23330 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23331 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23332 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23333 to_underlying (temp_die.sect_off));
348e048f
DE
23334 if (die)
23335 {
796a7ff8
DE
23336 /* For .gdb_index version 7 keep track of included TUs.
23337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23338 if (per_objfile->per_bfd->index_table != NULL
23339 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23340 {
ae640021 23341 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23342 }
23343
348e048f 23344 *ref_cu = sig_cu;
c24bdb02
KS
23345 if (sig_cu != cu)
23346 sig_cu->ancestor = cu;
23347
348e048f
DE
23348 return die;
23349 }
23350
ac9ec31b
DE
23351 return NULL;
23352}
23353
23354/* Follow signatured type referenced by ATTR in SRC_DIE.
23355 On entry *REF_CU is the CU of SRC_DIE.
23356 On exit *REF_CU is the CU of the result.
23357 The result is the DIE of the type.
23358 If the referenced type cannot be found an error is thrown. */
23359
23360static struct die_info *
ff39bb5e 23361follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23362 struct dwarf2_cu **ref_cu)
23363{
630ed6b9 23364 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23365 struct signatured_type *sig_type;
23366 struct die_info *die;
23367
23368 gdb_assert (attr->form == DW_FORM_ref_sig8);
23369
a2ce51a0 23370 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23371 /* sig_type will be NULL if the signatured type is missing from
23372 the debug info. */
23373 if (sig_type == NULL)
23374 {
23375 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23376 " from DIE at %s [in module %s]"),
23377 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23378 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23379 }
23380
23381 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23382 if (die == NULL)
23383 {
23384 dump_die_for_error (src_die);
23385 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23386 " from DIE at %s [in module %s]"),
23387 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23388 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23389 }
23390
23391 return die;
23392}
23393
23394/* Get the type specified by SIGNATURE referenced in DIE/CU,
23395 reading in and processing the type unit if necessary. */
23396
23397static struct type *
23398get_signatured_type (struct die_info *die, ULONGEST signature,
23399 struct dwarf2_cu *cu)
23400{
976ca316 23401 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23402 struct signatured_type *sig_type;
23403 struct dwarf2_cu *type_cu;
23404 struct die_info *type_die;
23405 struct type *type;
23406
a2ce51a0 23407 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23408 /* sig_type will be NULL if the signatured type is missing from
23409 the debug info. */
23410 if (sig_type == NULL)
23411 {
b98664d3 23412 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23413 " from DIE at %s [in module %s]"),
23414 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23415 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23416 return build_error_marker_type (cu, die);
23417 }
23418
23419 /* If we already know the type we're done. */
976ca316 23420 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23421 if (type != nullptr)
23422 return type;
ac9ec31b
DE
23423
23424 type_cu = cu;
23425 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23426 if (type_die != NULL)
23427 {
23428 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23429 is created. This is important, for example, because for c++ classes
23430 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23431 type = read_type_die (type_die, type_cu);
23432 if (type == NULL)
23433 {
b98664d3 23434 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23435 " referenced from DIE at %s [in module %s]"),
23436 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23437 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23438 type = build_error_marker_type (cu, die);
23439 }
23440 }
23441 else
23442 {
b98664d3 23443 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23444 " from DIE at %s [in module %s]"),
23445 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23446 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23447 type = build_error_marker_type (cu, die);
23448 }
e286671b 23449
976ca316 23450 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23451
23452 return type;
23453}
23454
23455/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23456 reading in and processing the type unit if necessary. */
23457
23458static struct type *
ff39bb5e 23459get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23460 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23461{
23462 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23463 if (attr->form_is_ref ())
ac9ec31b
DE
23464 {
23465 struct dwarf2_cu *type_cu = cu;
23466 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23467
23468 return read_type_die (type_die, type_cu);
23469 }
23470 else if (attr->form == DW_FORM_ref_sig8)
23471 {
630ed6b9 23472 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23473 }
23474 else
23475 {
976ca316 23476 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23477
b98664d3 23478 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23479 " at %s [in module %s]"),
23480 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23481 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23482 return build_error_marker_type (cu, die);
23483 }
348e048f
DE
23484}
23485
e5fe5e75 23486/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23487
23488static void
ab432490
SM
23489load_full_type_unit (dwarf2_per_cu_data *per_cu,
23490 dwarf2_per_objfile *per_objfile)
348e048f 23491{
52dc124a 23492 struct signatured_type *sig_type;
348e048f 23493
f4dc4d17 23494 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23495 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23496
6721b2ec
DE
23497 /* We have the per_cu, but we need the signatured_type.
23498 Fortunately this is an easy translation. */
23499 gdb_assert (per_cu->is_debug_types);
23500 sig_type = (struct signatured_type *) per_cu;
348e048f 23501
7188ed02 23502 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23503
ab432490 23504 read_signatured_type (sig_type, per_objfile);
348e048f 23505
7188ed02 23506 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23507}
23508
3019eac3
DE
23509/* Read in a signatured type and build its CU and DIEs.
23510 If the type is a stub for the real type in a DWO file,
23511 read in the real type from the DWO file as well. */
dee91e82
DE
23512
23513static void
ab432490
SM
23514read_signatured_type (signatured_type *sig_type,
23515 dwarf2_per_objfile *per_objfile)
dee91e82
DE
23516{
23517 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23518
3019eac3 23519 gdb_assert (per_cu->is_debug_types);
7188ed02 23520 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23521
2e671100 23522 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23523
23524 if (!reader.dummy_p)
23525 {
23526 struct dwarf2_cu *cu = reader.cu;
23527 const gdb_byte *info_ptr = reader.info_ptr;
23528
23529 gdb_assert (cu->die_hash == NULL);
23530 cu->die_hash =
23531 htab_create_alloc_ex (cu->header.length / 12,
23532 die_hash,
23533 die_eq,
23534 NULL,
23535 &cu->comp_unit_obstack,
23536 hashtab_obstack_allocate,
23537 dummy_obstack_deallocate);
23538
3e225074 23539 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23540 reader.comp_unit_die->child
23541 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23542 reader.comp_unit_die);
23543 cu->dies = reader.comp_unit_die;
23544 /* comp_unit_die is not stored in die_hash, no need. */
23545
23546 /* We try not to read any attributes in this function, because
23547 not all CUs needed for references have been loaded yet, and
23548 symbol table processing isn't initialized. But we have to
23549 set the CU language, or we won't be able to build types
23550 correctly. Similarly, if we do not read the producer, we can
23551 not apply producer-specific interpretation. */
23552 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23553
23554 reader.keep ();
c0ab21c2
TT
23555 }
23556
7ee85ab1 23557 sig_type->per_cu.tu_read = 1;
c906108c
SS
23558}
23559
c906108c
SS
23560/* Decode simple location descriptions.
23561 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23562 the location and return the value. If COMPUTED is non-null, it is
23563 set to true to indicate that decoding was successful, and false
23564 otherwise. If COMPUTED is null, then this function may emit a
23565 complaint. */
c906108c
SS
23566
23567static CORE_ADDR
7d79de9a 23568decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23569{
5e22e966 23570 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23571 size_t i;
23572 size_t size = blk->size;
d521ce57 23573 const gdb_byte *data = blk->data;
21ae7a4d
JK
23574 CORE_ADDR stack[64];
23575 int stacki;
23576 unsigned int bytes_read, unsnd;
23577 gdb_byte op;
c906108c 23578
7d79de9a
TT
23579 if (computed != nullptr)
23580 *computed = false;
23581
21ae7a4d
JK
23582 i = 0;
23583 stacki = 0;
23584 stack[stacki] = 0;
23585 stack[++stacki] = 0;
23586
23587 while (i < size)
23588 {
23589 op = data[i++];
23590 switch (op)
23591 {
23592 case DW_OP_lit0:
23593 case DW_OP_lit1:
23594 case DW_OP_lit2:
23595 case DW_OP_lit3:
23596 case DW_OP_lit4:
23597 case DW_OP_lit5:
23598 case DW_OP_lit6:
23599 case DW_OP_lit7:
23600 case DW_OP_lit8:
23601 case DW_OP_lit9:
23602 case DW_OP_lit10:
23603 case DW_OP_lit11:
23604 case DW_OP_lit12:
23605 case DW_OP_lit13:
23606 case DW_OP_lit14:
23607 case DW_OP_lit15:
23608 case DW_OP_lit16:
23609 case DW_OP_lit17:
23610 case DW_OP_lit18:
23611 case DW_OP_lit19:
23612 case DW_OP_lit20:
23613 case DW_OP_lit21:
23614 case DW_OP_lit22:
23615 case DW_OP_lit23:
23616 case DW_OP_lit24:
23617 case DW_OP_lit25:
23618 case DW_OP_lit26:
23619 case DW_OP_lit27:
23620 case DW_OP_lit28:
23621 case DW_OP_lit29:
23622 case DW_OP_lit30:
23623 case DW_OP_lit31:
23624 stack[++stacki] = op - DW_OP_lit0;
23625 break;
f1bea926 23626
21ae7a4d
JK
23627 case DW_OP_reg0:
23628 case DW_OP_reg1:
23629 case DW_OP_reg2:
23630 case DW_OP_reg3:
23631 case DW_OP_reg4:
23632 case DW_OP_reg5:
23633 case DW_OP_reg6:
23634 case DW_OP_reg7:
23635 case DW_OP_reg8:
23636 case DW_OP_reg9:
23637 case DW_OP_reg10:
23638 case DW_OP_reg11:
23639 case DW_OP_reg12:
23640 case DW_OP_reg13:
23641 case DW_OP_reg14:
23642 case DW_OP_reg15:
23643 case DW_OP_reg16:
23644 case DW_OP_reg17:
23645 case DW_OP_reg18:
23646 case DW_OP_reg19:
23647 case DW_OP_reg20:
23648 case DW_OP_reg21:
23649 case DW_OP_reg22:
23650 case DW_OP_reg23:
23651 case DW_OP_reg24:
23652 case DW_OP_reg25:
23653 case DW_OP_reg26:
23654 case DW_OP_reg27:
23655 case DW_OP_reg28:
23656 case DW_OP_reg29:
23657 case DW_OP_reg30:
23658 case DW_OP_reg31:
23659 stack[++stacki] = op - DW_OP_reg0;
23660 if (i < size)
7d79de9a
TT
23661 {
23662 if (computed == nullptr)
23663 dwarf2_complex_location_expr_complaint ();
23664 else
23665 return 0;
23666 }
21ae7a4d 23667 break;
c906108c 23668
21ae7a4d
JK
23669 case DW_OP_regx:
23670 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23671 i += bytes_read;
23672 stack[++stacki] = unsnd;
23673 if (i < size)
7d79de9a
TT
23674 {
23675 if (computed == nullptr)
23676 dwarf2_complex_location_expr_complaint ();
23677 else
23678 return 0;
23679 }
21ae7a4d 23680 break;
c906108c 23681
21ae7a4d 23682 case DW_OP_addr:
c8a7a66f
TT
23683 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23684 &bytes_read);
21ae7a4d
JK
23685 i += bytes_read;
23686 break;
d53d4ac5 23687
21ae7a4d
JK
23688 case DW_OP_const1u:
23689 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23690 i += 1;
23691 break;
23692
23693 case DW_OP_const1s:
23694 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23695 i += 1;
23696 break;
23697
23698 case DW_OP_const2u:
23699 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23700 i += 2;
23701 break;
23702
23703 case DW_OP_const2s:
23704 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23705 i += 2;
23706 break;
d53d4ac5 23707
21ae7a4d
JK
23708 case DW_OP_const4u:
23709 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23710 i += 4;
23711 break;
23712
23713 case DW_OP_const4s:
23714 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23715 i += 4;
23716 break;
23717
585861ea
JK
23718 case DW_OP_const8u:
23719 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23720 i += 8;
23721 break;
23722
21ae7a4d
JK
23723 case DW_OP_constu:
23724 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23725 &bytes_read);
23726 i += bytes_read;
23727 break;
23728
23729 case DW_OP_consts:
23730 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23731 i += bytes_read;
23732 break;
23733
23734 case DW_OP_dup:
23735 stack[stacki + 1] = stack[stacki];
23736 stacki++;
23737 break;
23738
23739 case DW_OP_plus:
23740 stack[stacki - 1] += stack[stacki];
23741 stacki--;
23742 break;
23743
23744 case DW_OP_plus_uconst:
23745 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23746 &bytes_read);
23747 i += bytes_read;
23748 break;
23749
23750 case DW_OP_minus:
23751 stack[stacki - 1] -= stack[stacki];
23752 stacki--;
23753 break;
23754
23755 case DW_OP_deref:
23756 /* If we're not the last op, then we definitely can't encode
23757 this using GDB's address_class enum. This is valid for partial
23758 global symbols, although the variable's address will be bogus
23759 in the psymtab. */
23760 if (i < size)
7d79de9a
TT
23761 {
23762 if (computed == nullptr)
23763 dwarf2_complex_location_expr_complaint ();
23764 else
23765 return 0;
23766 }
21ae7a4d
JK
23767 break;
23768
23769 case DW_OP_GNU_push_tls_address:
4aa4e28b 23770 case DW_OP_form_tls_address:
21ae7a4d
JK
23771 /* The top of the stack has the offset from the beginning
23772 of the thread control block at which the variable is located. */
23773 /* Nothing should follow this operator, so the top of stack would
23774 be returned. */
23775 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23776 address will be bogus in the psymtab. Make it always at least
23777 non-zero to not look as a variable garbage collected by linker
23778 which have DW_OP_addr 0. */
21ae7a4d 23779 if (i < size)
7d79de9a
TT
23780 {
23781 if (computed == nullptr)
23782 dwarf2_complex_location_expr_complaint ();
23783 else
23784 return 0;
23785 }
585861ea 23786 stack[stacki]++;
21ae7a4d
JK
23787 break;
23788
23789 case DW_OP_GNU_uninit:
7d79de9a
TT
23790 if (computed != nullptr)
23791 return 0;
21ae7a4d
JK
23792 break;
23793
336d760d 23794 case DW_OP_addrx:
3019eac3 23795 case DW_OP_GNU_addr_index:
49f6c839 23796 case DW_OP_GNU_const_index:
3019eac3
DE
23797 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23798 &bytes_read);
23799 i += bytes_read;
23800 break;
23801
21ae7a4d 23802 default:
7d79de9a
TT
23803 if (computed == nullptr)
23804 {
23805 const char *name = get_DW_OP_name (op);
21ae7a4d 23806
7d79de9a
TT
23807 if (name)
23808 complaint (_("unsupported stack op: '%s'"),
23809 name);
23810 else
23811 complaint (_("unsupported stack op: '%02x'"),
23812 op);
23813 }
21ae7a4d
JK
23814
23815 return (stack[stacki]);
d53d4ac5 23816 }
3c6e0cb3 23817
21ae7a4d
JK
23818 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23819 outside of the allocated space. Also enforce minimum>0. */
23820 if (stacki >= ARRAY_SIZE (stack) - 1)
23821 {
7d79de9a
TT
23822 if (computed == nullptr)
23823 complaint (_("location description stack overflow"));
21ae7a4d
JK
23824 return 0;
23825 }
23826
23827 if (stacki <= 0)
23828 {
7d79de9a
TT
23829 if (computed == nullptr)
23830 complaint (_("location description stack underflow"));
21ae7a4d
JK
23831 return 0;
23832 }
23833 }
7d79de9a
TT
23834
23835 if (computed != nullptr)
23836 *computed = true;
21ae7a4d 23837 return (stack[stacki]);
c906108c
SS
23838}
23839
23840/* memory allocation interface */
23841
c906108c 23842static struct dwarf_block *
7b5a2f43 23843dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23844{
8d749320 23845 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23846}
23847
c906108c 23848static struct die_info *
b60c80d6 23849dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23850{
23851 struct die_info *die;
b60c80d6
DJ
23852 size_t size = sizeof (struct die_info);
23853
23854 if (num_attrs > 1)
23855 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23856
b60c80d6 23857 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23858 memset (die, 0, sizeof (struct die_info));
23859 return (die);
23860}
2e276125
JB
23861
23862\f
a036ba48 23863
c90ec28a 23864/* Macro support. */
cf2c3c16 23865
9eac9650
TT
23866/* An overload of dwarf_decode_macros that finds the correct section
23867 and ensures it is read in before calling the other overload. */
23868
23869static void
23870dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23871 int section_is_gnu)
23872{
976ca316
SM
23873 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23874 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23875 const struct line_header *lh = cu->line_header;
9eac9650
TT
23876 unsigned int offset_size = cu->header.offset_size;
23877 struct dwarf2_section_info *section;
23878 const char *section_name;
23879
23880 if (cu->dwo_unit != nullptr)
23881 {
23882 if (section_is_gnu)
23883 {
23884 section = &cu->dwo_unit->dwo_file->sections.macro;
23885 section_name = ".debug_macro.dwo";
23886 }
23887 else
23888 {
23889 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23890 section_name = ".debug_macinfo.dwo";
23891 }
23892 }
23893 else
23894 {
23895 if (section_is_gnu)
23896 {
976ca316 23897 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23898 section_name = ".debug_macro";
23899 }
23900 else
23901 {
976ca316 23902 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23903 section_name = ".debug_macinfo";
23904 }
23905 }
23906
23907 section->read (objfile);
23908 if (section->buffer == nullptr)
23909 {
23910 complaint (_("missing %s section"), section_name);
23911 return;
23912 }
23913
23914 buildsym_compunit *builder = cu->get_builder ();
23915
048fde1e 23916 struct dwarf2_section_info *str_offsets_section;
23917 struct dwarf2_section_info *str_section;
23918 ULONGEST str_offsets_base;
23919
23920 if (cu->dwo_unit != nullptr)
23921 {
23922 str_offsets_section = &cu->dwo_unit->dwo_file
23923 ->sections.str_offsets;
23924 str_section = &cu->dwo_unit->dwo_file->sections.str;
23925 str_offsets_base = cu->header.addr_size;
23926 }
23927 else
23928 {
23929 str_offsets_section = &per_objfile->per_bfd->str_offsets;
23930 str_section = &per_objfile->per_bfd->str;
23931 str_offsets_base = *cu->str_offsets_base;
23932 }
23933
976ca316 23934 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 23935 offset_size, offset, str_section, str_offsets_section,
23936 str_offsets_base, section_is_gnu);
9eac9650
TT
23937}
23938
3019eac3
DE
23939/* Return the .debug_loc section to use for CU.
23940 For DWO files use .debug_loc.dwo. */
23941
23942static struct dwarf2_section_info *
23943cu_debug_loc_section (struct dwarf2_cu *cu)
23944{
976ca316 23945 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23946
3019eac3 23947 if (cu->dwo_unit)
43988095
JK
23948 {
23949 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23950
43988095
JK
23951 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23952 }
976ca316
SM
23953 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23954 : &per_objfile->per_bfd->loc);
3019eac3
DE
23955}
23956
d0ce17d8
CT
23957/* Return the .debug_rnglists section to use for CU. */
23958static struct dwarf2_section_info *
23959cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23960{
23961 if (cu->header.version < 5)
23962 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23963 cu->header.version);
23964 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23965
23966 /* Make sure we read the .debug_rnglists section from the file that
23967 contains the DW_AT_ranges attribute we are reading. Normally that
23968 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23969 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23970 program. */
23971 if (cu->dwo_unit != nullptr
23972 && tag != DW_TAG_compile_unit
23973 && tag != DW_TAG_skeleton_unit)
23974 {
23975 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23976
23977 if (sections->rnglists.size > 0)
23978 return &sections->rnglists;
23979 else
23980 error (_(".debug_rnglists section is missing from .dwo file."));
23981 }
23982 return &dwarf2_per_objfile->per_bfd->rnglists;
23983}
23984
8cf6f0b1
TT
23985/* A helper function that fills in a dwarf2_loclist_baton. */
23986
23987static void
23988fill_in_loclist_baton (struct dwarf2_cu *cu,
23989 struct dwarf2_loclist_baton *baton,
ff39bb5e 23990 const struct attribute *attr)
8cf6f0b1 23991{
976ca316 23992 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23993 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23994
976ca316 23995 section->read (per_objfile->objfile);
8cf6f0b1 23996
976ca316 23997 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23998 baton->per_cu = cu->per_cu;
23999 gdb_assert (baton->per_cu);
24000 /* We don't know how long the location list is, but make sure we
24001 don't run off the edge of the section. */
d4df075e
TT
24002 baton->size = section->size - attr->as_unsigned ();
24003 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24004 if (cu->base_address.has_value ())
24005 baton->base_address = *cu->base_address;
24006 else
24007 baton->base_address = 0;
f664829e 24008 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24009}
24010
4c2df51b 24011static void
ff39bb5e 24012dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24013 struct dwarf2_cu *cu, int is_block)
4c2df51b 24014{
976ca316
SM
24015 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24016 struct objfile *objfile = per_objfile->objfile;
3019eac3 24017 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24018
cd6c91b4 24019 if (attr->form_is_section_offset ()
3019eac3 24020 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24021 the section. If so, fall through to the complaint in the
24022 other branch. */
d4df075e 24023 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24024 {
0d53c4c4 24025 struct dwarf2_loclist_baton *baton;
4c2df51b 24026
8d749320 24027 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24028
8cf6f0b1 24029 fill_in_loclist_baton (cu, baton, attr);
be391dca 24030
2b24b6e4 24031 if (!cu->base_address.has_value ())
b98664d3 24032 complaint (_("Location list used without "
3e43a32a 24033 "specifying the CU base address."));
4c2df51b 24034
f1e6e072
TT
24035 SYMBOL_ACLASS_INDEX (sym) = (is_block
24036 ? dwarf2_loclist_block_index
24037 : dwarf2_loclist_index);
0d53c4c4
DJ
24038 SYMBOL_LOCATION_BATON (sym) = baton;
24039 }
24040 else
24041 {
24042 struct dwarf2_locexpr_baton *baton;
24043
8d749320 24044 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24045 baton->per_objfile = per_objfile;
ae0d2f24
UW
24046 baton->per_cu = cu->per_cu;
24047 gdb_assert (baton->per_cu);
0d53c4c4 24048
4fc6c0d5 24049 if (attr->form_is_block ())
0d53c4c4
DJ
24050 {
24051 /* Note that we're just copying the block's data pointer
24052 here, not the actual data. We're still pointing into the
6502dd73
DJ
24053 info_buffer for SYM's objfile; right now we never release
24054 that buffer, but when we do clean up properly this may
24055 need to change. */
9d2246fc
TT
24056 struct dwarf_block *block = attr->as_block ();
24057 baton->size = block->size;
24058 baton->data = block->data;
0d53c4c4
DJ
24059 }
24060 else
24061 {
24062 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24063 sym->natural_name ());
0d53c4c4 24064 baton->size = 0;
0d53c4c4 24065 }
6e70227d 24066
f1e6e072
TT
24067 SYMBOL_ACLASS_INDEX (sym) = (is_block
24068 ? dwarf2_locexpr_block_index
24069 : dwarf2_locexpr_index);
0d53c4c4
DJ
24070 SYMBOL_LOCATION_BATON (sym) = baton;
24071 }
4c2df51b 24072}
6502dd73 24073
2e6a9f79 24074/* See read.h. */
96408a79 24075
2e6a9f79
SM
24076const comp_unit_head *
24077dwarf2_per_cu_data::get_header () const
96408a79 24078{
2e6a9f79
SM
24079 if (!m_header_read_in)
24080 {
24081 const gdb_byte *info_ptr
24082 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24083
2e6a9f79 24084 memset (&m_header, 0, sizeof (m_header));
96408a79 24085
2e6a9f79
SM
24086 read_comp_unit_head (&m_header, info_ptr, this->section,
24087 rcuh_kind::COMPILE);
24088 }
96408a79 24089
2e6a9f79 24090 return &m_header;
96408a79
SA
24091}
24092
09ba997f 24093/* See read.h. */
ae0d2f24 24094
98714339 24095int
09ba997f 24096dwarf2_per_cu_data::addr_size () const
ae0d2f24 24097{
2e6a9f79 24098 return this->get_header ()->addr_size;
ae0d2f24
UW
24099}
24100
09ba997f 24101/* See read.h. */
9eae7c52
TT
24102
24103int
09ba997f 24104dwarf2_per_cu_data::offset_size () const
9eae7c52 24105{
2e6a9f79 24106 return this->get_header ()->offset_size;
96408a79
SA
24107}
24108
09ba997f 24109/* See read.h. */
96408a79
SA
24110
24111int
09ba997f 24112dwarf2_per_cu_data::ref_addr_size () const
96408a79 24113{
2e6a9f79 24114 const comp_unit_head *header = this->get_header ();
96408a79 24115
2e6a9f79
SM
24116 if (header->version == 2)
24117 return header->addr_size;
96408a79 24118 else
2e6a9f79 24119 return header->offset_size;
181cebd4
JK
24120}
24121
09ba997f 24122/* See read.h. */
9aa1f1e3 24123
09ba997f 24124struct type *
293e7e51 24125dwarf2_cu::addr_type () const
9a49df9d 24126{
293e7e51 24127 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24128 struct type *void_type = objfile_type (objfile)->builtin_void;
24129 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24130 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24131
24132 if (TYPE_LENGTH (addr_type) == addr_size)
24133 return addr_type;
24134
c6d940a9 24135 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24136 return addr_type;
24137}
24138
22b6cd70
TT
24139/* A helper function for dwarf2_find_containing_comp_unit that returns
24140 the index of the result, and that searches a vector. It will
24141 return a result even if the offset in question does not actually
24142 occur in any CU. This is separate so that it can be unit
24143 tested. */
ae038cb0 24144
22b6cd70
TT
24145static int
24146dwarf2_find_containing_comp_unit
24147 (sect_offset sect_off,
24148 unsigned int offset_in_dwz,
24149 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24150{
ae038cb0
DJ
24151 int low, high;
24152
ae038cb0 24153 low = 0;
22b6cd70 24154 high = all_comp_units.size () - 1;
ae038cb0
DJ
24155 while (high > low)
24156 {
36586728 24157 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24158 int mid = low + (high - low) / 2;
9a619af0 24159
22b6cd70 24160 mid_cu = all_comp_units[mid];
36586728 24161 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24162 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24163 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24164 high = mid;
24165 else
24166 low = mid + 1;
24167 }
24168 gdb_assert (low == high);
22b6cd70
TT
24169 return low;
24170}
24171
24172/* Locate the .debug_info compilation unit from CU's objfile which contains
24173 the DIE at OFFSET. Raises an error on failure. */
24174
24175static struct dwarf2_per_cu_data *
24176dwarf2_find_containing_comp_unit (sect_offset sect_off,
24177 unsigned int offset_in_dwz,
976ca316 24178 dwarf2_per_objfile *per_objfile)
22b6cd70 24179{
976ca316
SM
24180 int low = dwarf2_find_containing_comp_unit
24181 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24182 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24183
45b8ae0c 24184 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24185 {
36586728 24186 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24187 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24188 "offset %s [in module %s]"),
24189 sect_offset_str (sect_off),
976ca316 24190 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24191
976ca316 24192 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24193 <= sect_off);
976ca316 24194 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24195 }
24196 else
24197 {
976ca316 24198 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24199 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24200 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24201 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24202 return this_cu;
24203 }
24204}
24205
22b6cd70
TT
24206#if GDB_SELF_TEST
24207
24208namespace selftests {
24209namespace find_containing_comp_unit {
24210
24211static void
24212run_test ()
24213{
24214 struct dwarf2_per_cu_data one {};
24215 struct dwarf2_per_cu_data two {};
24216 struct dwarf2_per_cu_data three {};
24217 struct dwarf2_per_cu_data four {};
24218
24219 one.length = 5;
24220 two.sect_off = sect_offset (one.length);
24221 two.length = 7;
24222
24223 three.length = 5;
24224 three.is_dwz = 1;
24225 four.sect_off = sect_offset (three.length);
24226 four.length = 7;
24227 four.is_dwz = 1;
24228
24229 std::vector<dwarf2_per_cu_data *> units;
24230 units.push_back (&one);
24231 units.push_back (&two);
24232 units.push_back (&three);
24233 units.push_back (&four);
24234
24235 int result;
24236
24237 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24238 SELF_CHECK (units[result] == &one);
24239 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24240 SELF_CHECK (units[result] == &one);
24241 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24242 SELF_CHECK (units[result] == &two);
24243
24244 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24245 SELF_CHECK (units[result] == &three);
24246 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24247 SELF_CHECK (units[result] == &three);
24248 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24249 SELF_CHECK (units[result] == &four);
24250}
24251
24252}
24253}
24254
24255#endif /* GDB_SELF_TEST */
24256
9e021579 24257/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24258
9e021579
SM
24259dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24260 dwarf2_per_objfile *per_objfile)
24261 : per_cu (per_cu),
24262 per_objfile (per_objfile),
9068261f
AB
24263 mark (false),
24264 has_loclist (false),
24265 checked_producer (false),
24266 producer_is_gxx_lt_4_6 (false),
24267 producer_is_gcc_lt_4_3 (false),
eb77c9df 24268 producer_is_icc (false),
9068261f 24269 producer_is_icc_lt_14 (false),
c258c396 24270 producer_is_codewarrior (false),
9068261f 24271 processing_has_namespace_info (false)
93311388 24272{
9816fde3
JK
24273}
24274
24275/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24276
24277static void
95554aad
TT
24278prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24279 enum language pretend_language)
9816fde3
JK
24280{
24281 struct attribute *attr;
24282
24283 /* Set the language we're debugging. */
24284 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24285 if (attr != nullptr)
529908cb 24286 set_cu_language (attr->constant_value (0), cu);
9816fde3 24287 else
9cded63f 24288 {
95554aad 24289 cu->language = pretend_language;
9cded63f
TT
24290 cu->language_defn = language_def (cu->language);
24291 }
dee91e82 24292
7d45c7c3 24293 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24294}
24295
7188ed02 24296/* See read.h. */
ae038cb0 24297
7188ed02
SM
24298dwarf2_cu *
24299dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24300{
7188ed02
SM
24301 auto it = m_dwarf2_cus.find (per_cu);
24302 if (it == m_dwarf2_cus.end ())
24303 return nullptr;
ae038cb0 24304
7188ed02
SM
24305 return it->second;
24306}
24307
24308/* See read.h. */
24309
24310void
24311dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24312{
24313 gdb_assert (this->get_cu (per_cu) == nullptr);
24314
24315 m_dwarf2_cus[per_cu] = cu;
24316}
24317
24318/* See read.h. */
24319
24320void
24321dwarf2_per_objfile::age_comp_units ()
24322{
24323 /* Start by clearing all marks. */
24324 for (auto pair : m_dwarf2_cus)
24325 pair.second->mark = false;
24326
24327 /* Traverse all CUs, mark them and their dependencies if used recently
24328 enough. */
24329 for (auto pair : m_dwarf2_cus)
ae038cb0 24330 {
7188ed02
SM
24331 dwarf2_cu *cu = pair.second;
24332
24333 cu->last_used++;
24334 if (cu->last_used <= dwarf_max_cache_age)
24335 dwarf2_mark (cu);
ae038cb0
DJ
24336 }
24337
7188ed02
SM
24338 /* Delete all CUs still not marked. */
24339 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24340 {
7188ed02 24341 dwarf2_cu *cu = it->second;
ae038cb0 24342
7188ed02 24343 if (!cu->mark)
ae038cb0 24344 {
7188ed02
SM
24345 delete cu;
24346 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24347 }
24348 else
7188ed02 24349 it++;
ae038cb0
DJ
24350 }
24351}
24352
7188ed02 24353/* See read.h. */
ae038cb0 24354
7188ed02
SM
24355void
24356dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24357{
7188ed02
SM
24358 auto it = m_dwarf2_cus.find (per_cu);
24359 if (it == m_dwarf2_cus.end ())
24360 return;
ae038cb0 24361
7188ed02 24362 delete it->second;
ae038cb0 24363
7188ed02
SM
24364 m_dwarf2_cus.erase (it);
24365}
ae038cb0 24366
7188ed02
SM
24367dwarf2_per_objfile::~dwarf2_per_objfile ()
24368{
24369 remove_all_cus ();
ae038cb0
DJ
24370}
24371
dee91e82
DE
24372/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24373 We store these in a hash table separate from the DIEs, and preserve them
24374 when the DIEs are flushed out of cache.
24375
24376 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24377 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24378 or the type may come from a DWO file. Furthermore, while it's more logical
24379 to use per_cu->section+offset, with Fission the section with the data is in
24380 the DWO file but we don't know that section at the point we need it.
24381 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24382 because we can enter the lookup routine, get_die_type_at_offset, from
24383 outside this file, and thus won't necessarily have PER_CU->cu.
24384 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24385
dee91e82 24386struct dwarf2_per_cu_offset_and_type
1c379e20 24387{
dee91e82 24388 const struct dwarf2_per_cu_data *per_cu;
9c541725 24389 sect_offset sect_off;
1c379e20
DJ
24390 struct type *type;
24391};
24392
dee91e82 24393/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24394
24395static hashval_t
dee91e82 24396per_cu_offset_and_type_hash (const void *item)
1c379e20 24397{
9a3c8263
SM
24398 const struct dwarf2_per_cu_offset_and_type *ofs
24399 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24400
9c541725 24401 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24402}
24403
dee91e82 24404/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24405
24406static int
dee91e82 24407per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24408{
9a3c8263
SM
24409 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24410 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24411 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24412 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24413
dee91e82 24414 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24415 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24416}
24417
24418/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24419 table if necessary. For convenience, return TYPE.
24420
24421 The DIEs reading must have careful ordering to:
85102364 24422 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24423 reading current DIE.
24424 * Not trying to dereference contents of still incompletely read in types
24425 while reading in other DIEs.
24426 * Enable referencing still incompletely read in types just by a pointer to
24427 the type without accessing its fields.
24428
24429 Therefore caller should follow these rules:
24430 * Try to fetch any prerequisite types we may need to build this DIE type
24431 before building the type and calling set_die_type.
e71ec853 24432 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24433 possible before fetching more types to complete the current type.
24434 * Make the type as complete as possible before fetching more types. */
1c379e20 24435
f792889a 24436static struct type *
1c379e20
DJ
24437set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24438{
976ca316 24439 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24440 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24441 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24442 struct attribute *attr;
24443 struct dynamic_prop prop;
1c379e20 24444
b4ba55a1
JB
24445 /* For Ada types, make sure that the gnat-specific data is always
24446 initialized (if not already set). There are a few types where
24447 we should not be doing so, because the type-specific area is
24448 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24449 where the type-specific area is used to store the floatformat).
24450 But this is not a problem, because the gnat-specific information
24451 is actually not needed for these types. */
24452 if (need_gnat_info (cu)
78134374
SM
24453 && type->code () != TYPE_CODE_FUNC
24454 && type->code () != TYPE_CODE_FLT
24455 && type->code () != TYPE_CODE_METHODPTR
24456 && type->code () != TYPE_CODE_MEMBERPTR
24457 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
24458 && !HAVE_GNAT_AUX_INFO (type))
24459 INIT_GNAT_SPECIFIC (type);
24460
3f2f83dd
KB
24461 /* Read DW_AT_allocated and set in type. */
24462 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24463 if (attr != NULL)
3f2f83dd 24464 {
293e7e51 24465 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24466 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 24467 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24468 }
3f2f83dd
KB
24469
24470 /* Read DW_AT_associated and set in type. */
24471 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24472 if (attr != NULL)
3f2f83dd 24473 {
293e7e51 24474 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24475 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 24476 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24477 }
3f2f83dd 24478
3cdcd0ce
JB
24479 /* Read DW_AT_data_location and set in type. */
24480 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 24481 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 24482 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 24483
976ca316
SM
24484 if (per_objfile->die_type_hash == NULL)
24485 per_objfile->die_type_hash
0335378b
TT
24486 = htab_up (htab_create_alloc (127,
24487 per_cu_offset_and_type_hash,
24488 per_cu_offset_and_type_eq,
24489 NULL, xcalloc, xfree));
1c379e20 24490
dee91e82 24491 ofs.per_cu = cu->per_cu;
9c541725 24492 ofs.sect_off = die->sect_off;
1c379e20 24493 ofs.type = type;
dee91e82 24494 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24495 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24496 if (*slot)
b98664d3 24497 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24498 sect_offset_str (die->sect_off));
8d749320
SM
24499 *slot = XOBNEW (&objfile->objfile_obstack,
24500 struct dwarf2_per_cu_offset_and_type);
1c379e20 24501 **slot = ofs;
f792889a 24502 return type;
1c379e20
DJ
24503}
24504
9c541725 24505/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24506 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24507
24508static struct type *
9c541725 24509get_die_type_at_offset (sect_offset sect_off,
aa66c379 24510 dwarf2_per_cu_data *per_cu,
976ca316 24511 dwarf2_per_objfile *per_objfile)
1c379e20 24512{
dee91e82 24513 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24514
976ca316 24515 if (per_objfile->die_type_hash == NULL)
f792889a 24516 return NULL;
1c379e20 24517
dee91e82 24518 ofs.per_cu = per_cu;
9c541725 24519 ofs.sect_off = sect_off;
9a3c8263 24520 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24521 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24522 if (slot)
24523 return slot->type;
24524 else
24525 return NULL;
24526}
24527
02142a6c 24528/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24529 or return NULL if DIE does not have a saved type. */
24530
24531static struct type *
24532get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24533{
aa66c379 24534 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24535}
24536
10b3939b
DJ
24537/* Add a dependence relationship from CU to REF_PER_CU. */
24538
24539static void
24540dwarf2_add_dependence (struct dwarf2_cu *cu,
24541 struct dwarf2_per_cu_data *ref_per_cu)
24542{
24543 void **slot;
24544
24545 if (cu->dependencies == NULL)
24546 cu->dependencies
24547 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24548 NULL, &cu->comp_unit_obstack,
24549 hashtab_obstack_allocate,
24550 dummy_obstack_deallocate);
24551
24552 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24553 if (*slot == NULL)
24554 *slot = ref_per_cu;
24555}
1c379e20 24556
f504f079
DE
24557/* Subroutine of dwarf2_mark to pass to htab_traverse.
24558 Set the mark field in every compilation unit in the
7188ed02
SM
24559 cache that we must keep because we are keeping CU.
24560
24561 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 24562
10b3939b
DJ
24563static int
24564dwarf2_mark_helper (void **slot, void *data)
24565{
7188ed02
SM
24566 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24567 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24568 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
24569
24570 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24571 reading of the chain. As such dependencies remain valid it is not much
24572 useful to track and undo them during QUIT cleanups. */
7188ed02 24573 if (cu == nullptr)
d07ed419
JK
24574 return 1;
24575
7188ed02 24576 if (cu->mark)
10b3939b 24577 return 1;
10b3939b 24578
7188ed02
SM
24579 cu->mark = true;
24580
24581 if (cu->dependencies != nullptr)
24582 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
24583
24584 return 1;
24585}
24586
f504f079
DE
24587/* Set the mark field in CU and in every other compilation unit in the
24588 cache that we must keep because we are keeping CU. */
24589
ae038cb0
DJ
24590static void
24591dwarf2_mark (struct dwarf2_cu *cu)
24592{
24593 if (cu->mark)
24594 return;
7188ed02 24595
9068261f 24596 cu->mark = true;
ae038cb0 24597
7188ed02
SM
24598 if (cu->dependencies != nullptr)
24599 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
24600}
24601
72bf9492
DJ
24602/* Trivial hash function for partial_die_info: the hash value of a DIE
24603 is its offset in .debug_info for this objfile. */
24604
24605static hashval_t
24606partial_die_hash (const void *item)
24607{
9a3c8263
SM
24608 const struct partial_die_info *part_die
24609 = (const struct partial_die_info *) item;
9a619af0 24610
9c541725 24611 return to_underlying (part_die->sect_off);
72bf9492
DJ
24612}
24613
24614/* Trivial comparison function for partial_die_info structures: two DIEs
24615 are equal if they have the same offset. */
24616
24617static int
24618partial_die_eq (const void *item_lhs, const void *item_rhs)
24619{
9a3c8263
SM
24620 const struct partial_die_info *part_die_lhs
24621 = (const struct partial_die_info *) item_lhs;
24622 const struct partial_die_info *part_die_rhs
24623 = (const struct partial_die_info *) item_rhs;
9a619af0 24624
9c541725 24625 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24626}
24627
3c3bb058
AB
24628struct cmd_list_element *set_dwarf_cmdlist;
24629struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24630
9291a0cd 24631static void
cd4fb1b2
SM
24632show_check_physname (struct ui_file *file, int from_tty,
24633 struct cmd_list_element *c, const char *value)
9291a0cd 24634{
cd4fb1b2
SM
24635 fprintf_filtered (file,
24636 _("Whether to check \"physname\" is %s.\n"),
24637 value);
9291a0cd
TT
24638}
24639
6c265988 24640void _initialize_dwarf2_read ();
cd4fb1b2 24641void
6c265988 24642_initialize_dwarf2_read ()
9291a0cd 24643{
0743fc83 24644 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24645Set DWARF specific variables.\n\
590042fc 24646Configure DWARF variables such as the cache size."),
0743fc83
TT
24647 &set_dwarf_cmdlist, "maintenance set dwarf ",
24648 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24649
0743fc83 24650 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24651Show DWARF specific variables.\n\
24652Show DWARF variables such as the cache size."),
0743fc83
TT
24653 &show_dwarf_cmdlist, "maintenance show dwarf ",
24654 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24655
cd4fb1b2
SM
24656 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24657 &dwarf_max_cache_age, _("\
24658Set the upper bound on the age of cached DWARF compilation units."), _("\
24659Show the upper bound on the age of cached DWARF compilation units."), _("\
24660A higher limit means that cached compilation units will be stored\n\
24661in memory longer, and more total memory will be used. Zero disables\n\
24662caching, which can slow down startup."),
24663 NULL,
24664 show_dwarf_max_cache_age,
24665 &set_dwarf_cmdlist,
24666 &show_dwarf_cmdlist);
156942c7 24667
cd4fb1b2
SM
24668 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24669Set debugging of the DWARF reader."), _("\
24670Show debugging of the DWARF reader."), _("\
24671When enabled (non-zero), debugging messages are printed during DWARF\n\
24672reading and symtab expansion. A value of 1 (one) provides basic\n\
24673information. A value greater than 1 provides more verbose information."),
24674 NULL,
24675 NULL,
24676 &setdebuglist, &showdebuglist);
9291a0cd 24677
cd4fb1b2
SM
24678 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24679Set debugging of the DWARF DIE reader."), _("\
24680Show debugging of the DWARF DIE reader."), _("\
24681When enabled (non-zero), DIEs are dumped after they are read in.\n\
24682The value is the maximum depth to print."),
24683 NULL,
24684 NULL,
24685 &setdebuglist, &showdebuglist);
9291a0cd 24686
cd4fb1b2
SM
24687 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24688Set debugging of the dwarf line reader."), _("\
24689Show debugging of the dwarf line reader."), _("\
24690When enabled (non-zero), line number entries are dumped as they are read in.\n\
24691A value of 1 (one) provides basic information.\n\
24692A value greater than 1 provides more verbose information."),
24693 NULL,
24694 NULL,
24695 &setdebuglist, &showdebuglist);
437afbb8 24696
cd4fb1b2
SM
24697 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24698Set cross-checking of \"physname\" code against demangler."), _("\
24699Show cross-checking of \"physname\" code against demangler."), _("\
24700When enabled, GDB's internal \"physname\" code is checked against\n\
24701the demangler."),
24702 NULL, show_check_physname,
24703 &setdebuglist, &showdebuglist);
900e11f9 24704
e615022a
DE
24705 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24706 no_class, &use_deprecated_index_sections, _("\
24707Set whether to use deprecated gdb_index sections."), _("\
24708Show whether to use deprecated gdb_index sections."), _("\
24709When enabled, deprecated .gdb_index sections are used anyway.\n\
24710Normally they are ignored either because of a missing feature or\n\
24711performance issue.\n\
24712Warning: This option must be enabled before gdb reads the file."),
24713 NULL,
24714 NULL,
24715 &setlist, &showlist);
24716
f1e6e072
TT
24717 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24718 &dwarf2_locexpr_funcs);
24719 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24720 &dwarf2_loclist_funcs);
24721
24722 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24723 &dwarf2_block_frame_base_locexpr_funcs);
24724 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24725 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24726
24727#if GDB_SELF_TEST
24728 selftests::register_test ("dw2_expand_symtabs_matching",
24729 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24730 selftests::register_test ("dwarf2_find_containing_comp_unit",
24731 selftests::find_containing_comp_unit::run_test);
c62446b1 24732#endif
6502dd73 24733}
This page took 5.551667 seconds and 4 git commands to generate.