Add attribute::as_virtuality method
[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,
1609 bool skip_partial,
1610 enum language pretend_language);
10b3939b 1611
8fc0b21d 1612static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1613 enum language pretend_language);
10b3939b 1614
8fc0b21d 1615static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1616 enum language pretend_language);
f4dc4d17 1617
10b3939b
DJ
1618static void dwarf2_add_dependence (struct dwarf2_cu *,
1619 struct dwarf2_per_cu_data *);
1620
ae038cb0
DJ
1621static void dwarf2_mark (struct dwarf2_cu *);
1622
b64f50a1 1623static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1624 dwarf2_per_cu_data *per_cu,
1625 dwarf2_per_objfile *per_objfile);
673bfd45 1626
f792889a 1627static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1628
120ce1b5
SM
1629static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1630 dwarf2_per_objfile *per_objfile,
95554aad
TT
1631 enum language pretend_language);
1632
976ca316 1633static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1634
b303c6f6
AB
1635/* Class, the destructor of which frees all allocated queue entries. This
1636 will only have work to do if an error was thrown while processing the
1637 dwarf. If no error was thrown then the queue entries should have all
1638 been processed, and freed, as we went along. */
1639
1640class dwarf2_queue_guard
1641{
1642public:
39856def
TT
1643 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1644 : m_per_objfile (per_objfile)
1645 {
1646 }
b303c6f6
AB
1647
1648 /* Free any entries remaining on the queue. There should only be
1649 entries left if we hit an error while processing the dwarf. */
1650 ~dwarf2_queue_guard ()
1651 {
39856def
TT
1652 /* Ensure that no memory is allocated by the queue. */
1653 std::queue<dwarf2_queue_item> empty;
5989a64e 1654 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1655 }
b303c6f6 1656
39856def 1657 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1658
39856def
TT
1659private:
1660 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1661};
1662
39856def
TT
1663dwarf2_queue_item::~dwarf2_queue_item ()
1664{
1665 /* Anything still marked queued is likely to be in an
1666 inconsistent state, so discard it. */
1667 if (per_cu->queued)
1668 {
7188ed02 1669 per_objfile->remove_cu (per_cu);
39856def
TT
1670 per_cu->queued = 0;
1671 }
1672}
1673
d721ba37
PA
1674/* The return type of find_file_and_directory. Note, the enclosed
1675 string pointers are only valid while this object is valid. */
1676
1677struct file_and_directory
1678{
1679 /* The filename. This is never NULL. */
1680 const char *name;
1681
1682 /* The compilation directory. NULL if not known. If we needed to
1683 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1684 points directly to the DW_AT_comp_dir string attribute owned by
1685 the obstack that owns the DIE. */
1686 const char *comp_dir;
1687
1688 /* If we needed to build a new string for comp_dir, this is what
1689 owns the storage. */
1690 std::string comp_dir_storage;
1691};
1692
1693static file_and_directory find_file_and_directory (struct die_info *die,
1694 struct dwarf2_cu *cu);
9291a0cd 1695
298e9637 1696static htab_up allocate_signatured_type_table ();
1fd400ff 1697
298e9637 1698static htab_up allocate_dwo_unit_table ();
3019eac3 1699
57d63ce2 1700static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1701 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1702 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1703
976ca316 1704static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1705
3019eac3 1706static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1707 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1708 ULONGEST signature);
3019eac3
DE
1709
1710static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1711 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1712
1b555f17 1713static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1714
263db9a1
TT
1715/* A unique pointer to a dwo_file. */
1716
51ac9db5 1717typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1718
976ca316 1719static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1720
1b80a9fa 1721static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1722
1723static void free_line_header_voidp (void *arg);
4390d890
DE
1724\f
1725/* Various complaints about symbol reading that don't abort the process. */
1726
4390d890
DE
1727static void
1728dwarf2_debug_line_missing_file_complaint (void)
1729{
b98664d3 1730 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1731}
1732
1733static void
1734dwarf2_debug_line_missing_end_sequence_complaint (void)
1735{
b98664d3 1736 complaint (_(".debug_line section has line "
4390d890
DE
1737 "program sequence without an end"));
1738}
1739
1740static void
1741dwarf2_complex_location_expr_complaint (void)
1742{
b98664d3 1743 complaint (_("location expression too complex"));
4390d890
DE
1744}
1745
1746static void
1747dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1748 int arg3)
1749{
b98664d3 1750 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1751 arg1, arg2, arg3);
1752}
1753
4390d890
DE
1754static void
1755dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1756{
b98664d3 1757 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1758 arg1, arg2);
1759}
527f3840
JK
1760
1761/* Hash function for line_header_hash. */
1762
1763static hashval_t
1764line_header_hash (const struct line_header *ofs)
1765{
9c541725 1766 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1767}
1768
1769/* Hash function for htab_create_alloc_ex for line_header_hash. */
1770
1771static hashval_t
1772line_header_hash_voidp (const void *item)
1773{
9a3c8263 1774 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1775
1776 return line_header_hash (ofs);
1777}
1778
1779/* Equality function for line_header_hash. */
1780
1781static int
1782line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1783{
9a3c8263
SM
1784 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1785 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1786
9c541725 1787 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1788 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1789}
1790
4390d890 1791\f
9291a0cd 1792
330cdd98
PA
1793/* See declaration. */
1794
5989a64e
SM
1795dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1796 bool can_copy_)
c3699833
SM
1797 : obfd (obfd),
1798 can_copy (can_copy_)
330cdd98
PA
1799{
1800 if (names == NULL)
1801 names = &dwarf2_elf_names;
1802
330cdd98
PA
1803 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1804 locate_sections (obfd, sec, *names);
1805}
1806
5989a64e 1807dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1808{
b76e467d 1809 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1810 per_cu->imported_symtabs_free ();
fc8e7e75 1811
b2bdb8cf 1812 for (signatured_type *sig_type : all_type_units)
ae640021 1813 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1814
5989a64e 1815 /* Everything else should be on this->obstack. */
330cdd98
PA
1816}
1817
7188ed02 1818/* See read.h. */
330cdd98
PA
1819
1820void
7188ed02 1821dwarf2_per_objfile::remove_all_cus ()
330cdd98 1822{
7188ed02
SM
1823 for (auto pair : m_dwarf2_cus)
1824 delete pair.second;
330cdd98 1825
7188ed02 1826 m_dwarf2_cus.clear ();
330cdd98
PA
1827}
1828
11ed8cad
TT
1829/* A helper class that calls free_cached_comp_units on
1830 destruction. */
1831
1832class free_cached_comp_units
1833{
1834public:
1835
1836 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1837 : m_per_objfile (per_objfile)
1838 {
1839 }
1840
1841 ~free_cached_comp_units ()
1842 {
7188ed02 1843 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1844 }
1845
1846 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1847
1848private:
1849
1850 dwarf2_per_objfile *m_per_objfile;
1851};
1852
af758d11
SM
1853/* See read.h. */
1854
1855bool
1856dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1857{
1858 gdb_assert (per_cu->index < this->m_symtabs.size ());
1859
1860 return this->m_symtabs[per_cu->index] != nullptr;
1861}
1862
1863/* See read.h. */
1864
1865compunit_symtab *
1866dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1867{
1868 gdb_assert (per_cu->index < this->m_symtabs.size ());
1869
1870 return this->m_symtabs[per_cu->index];
1871}
1872
1873/* See read.h. */
1874
1875void
1876dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1877 compunit_symtab *symtab)
1878{
1879 gdb_assert (per_cu->index < this->m_symtabs.size ());
1880 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1881
1882 this->m_symtabs[per_cu->index] = symtab;
1883}
1884
c906108c 1885/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1886 information and return true if we have enough to do something.
1887 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1888 ELF names are used. CAN_COPY is true for formats where symbol
1889 interposition is possible and so symbol values must follow copy
1890 relocation rules. */
c906108c
SS
1891
1892int
251d32d9 1893dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1894 const struct dwarf2_debug_sections *names,
1895 bool can_copy)
c906108c 1896{
97cbe998
SDJ
1897 if (objfile->flags & OBJF_READNEVER)
1898 return 0;
1899
976ca316 1900 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1901
976ca316 1902 if (per_objfile == NULL)
5989a64e 1903 {
17ee85fc
TT
1904 dwarf2_per_bfd *per_bfd;
1905
1906 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1907 doesn't require relocations and if there aren't partial symbols
1908 from some other reader. */
1909 if (!objfile_has_partial_symbols (objfile)
1910 && !gdb_bfd_requires_relocations (objfile->obfd))
1911 {
1912 /* See if one has been created for this BFD yet. */
1913 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1914
1915 if (per_bfd == nullptr)
1916 {
1917 /* No, create it now. */
1918 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1919 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1920 }
1921 }
1922 else
1923 {
1924 /* No sharing possible, create one specifically for this objfile. */
1925 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1926 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1927 }
5989a64e 1928
976ca316 1929 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1930 }
5bfd760d 1931
976ca316
SM
1932 return (!per_objfile->per_bfd->info.is_virtual
1933 && per_objfile->per_bfd->info.s.section != NULL
1934 && !per_objfile->per_bfd->abbrev.is_virtual
1935 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1936}
1937
251d32d9
TG
1938/* When loading sections, we look either for uncompressed section or for
1939 compressed section names. */
233a11ab
CS
1940
1941static int
251d32d9
TG
1942section_is_p (const char *section_name,
1943 const struct dwarf2_section_names *names)
233a11ab 1944{
251d32d9
TG
1945 if (names->normal != NULL
1946 && strcmp (section_name, names->normal) == 0)
1947 return 1;
1948 if (names->compressed != NULL
1949 && strcmp (section_name, names->compressed) == 0)
1950 return 1;
1951 return 0;
233a11ab
CS
1952}
1953
330cdd98 1954/* See declaration. */
c906108c 1955
330cdd98 1956void
5989a64e
SM
1957dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1958 const dwarf2_debug_sections &names)
c906108c 1959{
fd361982 1960 flagword aflag = bfd_section_flags (sectp);
251d32d9 1961
dc7650b8
JK
1962 if ((aflag & SEC_HAS_CONTENTS) == 0)
1963 {
1964 }
950b7495
KS
1965 else if (elf_section_data (sectp)->this_hdr.sh_size
1966 > bfd_get_file_size (abfd))
1967 {
1968 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1969 warning (_("Discarding section %s which has a section size (%s"
1970 ") larger than the file size [in module %s]"),
1971 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1972 bfd_get_filename (abfd));
1973 }
330cdd98 1974 else if (section_is_p (sectp->name, &names.info))
c906108c 1975 {
330cdd98 1976 this->info.s.section = sectp;
fd361982 1977 this->info.size = bfd_section_size (sectp);
c906108c 1978 }
330cdd98 1979 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1980 {
330cdd98 1981 this->abbrev.s.section = sectp;
fd361982 1982 this->abbrev.size = bfd_section_size (sectp);
c906108c 1983 }
330cdd98 1984 else if (section_is_p (sectp->name, &names.line))
c906108c 1985 {
330cdd98 1986 this->line.s.section = sectp;
fd361982 1987 this->line.size = bfd_section_size (sectp);
c906108c 1988 }
330cdd98 1989 else if (section_is_p (sectp->name, &names.loc))
c906108c 1990 {
330cdd98 1991 this->loc.s.section = sectp;
fd361982 1992 this->loc.size = bfd_section_size (sectp);
c906108c 1993 }
330cdd98 1994 else if (section_is_p (sectp->name, &names.loclists))
43988095 1995 {
330cdd98 1996 this->loclists.s.section = sectp;
fd361982 1997 this->loclists.size = bfd_section_size (sectp);
43988095 1998 }
330cdd98 1999 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2000 {
330cdd98 2001 this->macinfo.s.section = sectp;
fd361982 2002 this->macinfo.size = bfd_section_size (sectp);
c906108c 2003 }
330cdd98 2004 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2005 {
330cdd98 2006 this->macro.s.section = sectp;
fd361982 2007 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2008 }
330cdd98 2009 else if (section_is_p (sectp->name, &names.str))
c906108c 2010 {
330cdd98 2011 this->str.s.section = sectp;
fd361982 2012 this->str.size = bfd_section_size (sectp);
c906108c 2013 }
18a8505e
AT
2014 else if (section_is_p (sectp->name, &names.str_offsets))
2015 {
2016 this->str_offsets.s.section = sectp;
2017 this->str_offsets.size = bfd_section_size (sectp);
2018 }
330cdd98 2019 else if (section_is_p (sectp->name, &names.line_str))
43988095 2020 {
330cdd98 2021 this->line_str.s.section = sectp;
fd361982 2022 this->line_str.size = bfd_section_size (sectp);
43988095 2023 }
330cdd98 2024 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2025 {
330cdd98 2026 this->addr.s.section = sectp;
fd361982 2027 this->addr.size = bfd_section_size (sectp);
3019eac3 2028 }
330cdd98 2029 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2030 {
330cdd98 2031 this->frame.s.section = sectp;
fd361982 2032 this->frame.size = bfd_section_size (sectp);
b6af0555 2033 }
330cdd98 2034 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2035 {
330cdd98 2036 this->eh_frame.s.section = sectp;
fd361982 2037 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2038 }
330cdd98 2039 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2040 {
330cdd98 2041 this->ranges.s.section = sectp;
fd361982 2042 this->ranges.size = bfd_section_size (sectp);
af34e669 2043 }
330cdd98 2044 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2045 {
330cdd98 2046 this->rnglists.s.section = sectp;
fd361982 2047 this->rnglists.size = bfd_section_size (sectp);
43988095 2048 }
330cdd98 2049 else if (section_is_p (sectp->name, &names.types))
348e048f 2050 {
8b70b953
TT
2051 struct dwarf2_section_info type_section;
2052
2053 memset (&type_section, 0, sizeof (type_section));
049412e3 2054 type_section.s.section = sectp;
fd361982 2055 type_section.size = bfd_section_size (sectp);
8b70b953 2056
fd5866f6 2057 this->types.push_back (type_section);
348e048f 2058 }
330cdd98 2059 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2060 {
330cdd98 2061 this->gdb_index.s.section = sectp;
fd361982 2062 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2063 }
927aa2e7
JK
2064 else if (section_is_p (sectp->name, &names.debug_names))
2065 {
2066 this->debug_names.s.section = sectp;
fd361982 2067 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2068 }
2069 else if (section_is_p (sectp->name, &names.debug_aranges))
2070 {
2071 this->debug_aranges.s.section = sectp;
fd361982 2072 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2073 }
dce234bc 2074
fd361982
AM
2075 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2076 && bfd_section_vma (sectp) == 0)
330cdd98 2077 this->has_section_at_zero = true;
c906108c
SS
2078}
2079
dce234bc 2080/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2081 SECTION_NAME. */
af34e669 2082
dce234bc 2083void
3017a003
TG
2084dwarf2_get_section_info (struct objfile *objfile,
2085 enum dwarf2_section_enum sect,
d521ce57 2086 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2087 bfd_size_type *sizep)
2088{
976ca316 2089 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2090 struct dwarf2_section_info *info;
a3b2a86b
TT
2091
2092 /* We may see an objfile without any DWARF, in which case we just
2093 return nothing. */
976ca316 2094 if (per_objfile == NULL)
a3b2a86b
TT
2095 {
2096 *sectp = NULL;
2097 *bufp = NULL;
2098 *sizep = 0;
2099 return;
2100 }
3017a003
TG
2101 switch (sect)
2102 {
2103 case DWARF2_DEBUG_FRAME:
976ca316 2104 info = &per_objfile->per_bfd->frame;
3017a003
TG
2105 break;
2106 case DWARF2_EH_FRAME:
976ca316 2107 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2108 break;
2109 default:
2110 gdb_assert_not_reached ("unexpected section");
2111 }
dce234bc 2112
96b79293 2113 info->read (objfile);
dce234bc 2114
96b79293 2115 *sectp = info->get_bfd_section ();
dce234bc
PP
2116 *bufp = info->buffer;
2117 *sizep = info->size;
2118}
2119
36586728
TT
2120/* A helper function to find the sections for a .dwz file. */
2121
2122static void
5bb6e9dd 2123locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2124{
36586728
TT
2125 /* Note that we only support the standard ELF names, because .dwz
2126 is ELF-only (at the time of writing). */
2127 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2128 {
049412e3 2129 dwz_file->abbrev.s.section = sectp;
fd361982 2130 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2131 }
2132 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2133 {
049412e3 2134 dwz_file->info.s.section = sectp;
fd361982 2135 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2136 }
2137 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2138 {
049412e3 2139 dwz_file->str.s.section = sectp;
fd361982 2140 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2141 }
2142 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2143 {
049412e3 2144 dwz_file->line.s.section = sectp;
fd361982 2145 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2146 }
2147 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2148 {
049412e3 2149 dwz_file->macro.s.section = sectp;
fd361982 2150 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2151 }
2ec9a5e0
TT
2152 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2153 {
049412e3 2154 dwz_file->gdb_index.s.section = sectp;
fd361982 2155 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2156 }
927aa2e7
JK
2157 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2158 {
2159 dwz_file->debug_names.s.section = sectp;
fd361982 2160 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2161 }
36586728
TT
2162}
2163
c4973306 2164/* See dwarf2read.h. */
36586728 2165
c4973306 2166struct dwz_file *
c3699833 2167dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2168{
36586728 2169 const char *filename;
acd13123 2170 bfd_size_type buildid_len_arg;
dc294be5
TT
2171 size_t buildid_len;
2172 bfd_byte *buildid;
36586728 2173
c3699833
SM
2174 if (per_bfd->dwz_file != NULL)
2175 return per_bfd->dwz_file.get ();
36586728 2176
4db1a1dc 2177 bfd_set_error (bfd_error_no_error);
791afaa2 2178 gdb::unique_xmalloc_ptr<char> data
c3699833 2179 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2180 &buildid_len_arg, &buildid));
4db1a1dc
TT
2181 if (data == NULL)
2182 {
2183 if (bfd_get_error () == bfd_error_no_error)
2184 return NULL;
2185 error (_("could not read '.gnu_debugaltlink' section: %s"),
2186 bfd_errmsg (bfd_get_error ()));
2187 }
791afaa2
TT
2188
2189 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2190
acd13123
TT
2191 buildid_len = (size_t) buildid_len_arg;
2192
791afaa2 2193 filename = data.get ();
d721ba37
PA
2194
2195 std::string abs_storage;
36586728
TT
2196 if (!IS_ABSOLUTE_PATH (filename))
2197 {
14278e1f 2198 gdb::unique_xmalloc_ptr<char> abs
c3699833 2199 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2200
14278e1f 2201 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2202 filename = abs_storage.c_str ();
36586728
TT
2203 }
2204
dc294be5
TT
2205 /* First try the file name given in the section. If that doesn't
2206 work, try to use the build-id instead. */
ad80db5b 2207 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2208 if (dwz_bfd != NULL)
36586728 2209 {
192b62ce 2210 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2211 dwz_bfd.reset (nullptr);
36586728
TT
2212 }
2213
dc294be5
TT
2214 if (dwz_bfd == NULL)
2215 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2216
0d79cdc4
AM
2217 if (dwz_bfd == nullptr)
2218 {
2219 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2220 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2221
2222 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2223 buildid_len,
2224 origname,
2225 &alt_filename));
2226
2227 if (fd.get () >= 0)
2228 {
2229 /* File successfully retrieved from server. */
ad80db5b 2230 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2231
2232 if (dwz_bfd == nullptr)
2233 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2234 alt_filename.get ());
2235 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2236 dwz_bfd.reset (nullptr);
2237 }
2238 }
2239
dc294be5
TT
2240 if (dwz_bfd == NULL)
2241 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2242 bfd_get_filename (per_bfd->obfd));
dc294be5 2243
7ff8cb8c
TT
2244 std::unique_ptr<struct dwz_file> result
2245 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2246
5bb6e9dd
TT
2247 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2248 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2249
c3699833
SM
2250 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2251 per_bfd->dwz_file = std::move (result);
2252 return per_bfd->dwz_file.get ();
36586728 2253}
9291a0cd 2254\f
7b9f3c50
DE
2255/* DWARF quick_symbols_functions support. */
2256
2257/* TUs can share .debug_line entries, and there can be a lot more TUs than
2258 unique line tables, so we maintain a separate table of all .debug_line
2259 derived entries to support the sharing.
2260 All the quick functions need is the list of file names. We discard the
2261 line_header when we're done and don't need to record it here. */
2262struct quick_file_names
2263{
094b34ac
DE
2264 /* The data used to construct the hash key. */
2265 struct stmt_list_hash hash;
7b9f3c50
DE
2266
2267 /* The number of entries in file_names, real_names. */
2268 unsigned int num_file_names;
2269
2270 /* The file names from the line table, after being run through
2271 file_full_name. */
2272 const char **file_names;
2273
2274 /* The file names from the line table after being run through
2275 gdb_realpath. These are computed lazily. */
2276 const char **real_names;
2277};
2278
2279/* When using the index (and thus not using psymtabs), each CU has an
2280 object of this type. This is used to hold information needed by
2281 the various "quick" methods. */
2282struct dwarf2_per_cu_quick_data
2283{
2284 /* The file table. This can be NULL if there was no file table
2285 or it's currently not read in.
5989a64e 2286 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2287 struct quick_file_names *file_names;
2288
7b9f3c50
DE
2289 /* A temporary mark bit used when iterating over all CUs in
2290 expand_symtabs_matching. */
2291 unsigned int mark : 1;
2292
2293 /* True if we've tried to read the file table and found there isn't one.
2294 There will be no point in trying to read it again next time. */
2295 unsigned int no_file_data : 1;
2296};
2297
094b34ac
DE
2298/* Utility hash function for a stmt_list_hash. */
2299
2300static hashval_t
2301hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2302{
2303 hashval_t v = 0;
2304
2305 if (stmt_list_hash->dwo_unit != NULL)
2306 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2307 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2308 return v;
2309}
2310
2311/* Utility equality function for a stmt_list_hash. */
2312
2313static int
2314eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2315 const struct stmt_list_hash *rhs)
2316{
2317 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2318 return 0;
2319 if (lhs->dwo_unit != NULL
2320 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2321 return 0;
2322
9c541725 2323 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2324}
2325
7b9f3c50
DE
2326/* Hash function for a quick_file_names. */
2327
2328static hashval_t
2329hash_file_name_entry (const void *e)
2330{
9a3c8263
SM
2331 const struct quick_file_names *file_data
2332 = (const struct quick_file_names *) e;
7b9f3c50 2333
094b34ac 2334 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2335}
2336
2337/* Equality function for a quick_file_names. */
2338
2339static int
2340eq_file_name_entry (const void *a, const void *b)
2341{
9a3c8263
SM
2342 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2343 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2344
094b34ac 2345 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2346}
2347
2348/* Delete function for a quick_file_names. */
2349
2350static void
2351delete_file_name_entry (void *e)
2352{
9a3c8263 2353 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2354 int i;
2355
2356 for (i = 0; i < file_data->num_file_names; ++i)
2357 {
2358 xfree ((void*) file_data->file_names[i]);
2359 if (file_data->real_names)
2360 xfree ((void*) file_data->real_names[i]);
2361 }
2362
45940949
TT
2363 /* The space for the struct itself lives on the obstack, so we don't
2364 free it here. */
7b9f3c50
DE
2365}
2366
2367/* Create a quick_file_names hash table. */
2368
5895093f 2369static htab_up
7b9f3c50
DE
2370create_quick_file_names_table (unsigned int nr_initial_entries)
2371{
5895093f
TT
2372 return htab_up (htab_create_alloc (nr_initial_entries,
2373 hash_file_name_entry, eq_file_name_entry,
2374 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2375}
9291a0cd 2376
ab432490
SM
2377/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2378 function is unrelated to symtabs, symtab would have to be created afterwards.
2379 You should call age_cached_comp_units after processing the CU. */
918dd910 2380
1b555f17 2381static dwarf2_cu *
ab432490
SM
2382load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2383 bool skip_partial)
918dd910 2384{
3019eac3 2385 if (per_cu->is_debug_types)
ab432490 2386 load_full_type_unit (per_cu, per_objfile);
918dd910 2387 else
ab432490 2388 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2389
7188ed02
SM
2390 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2391 if (cu == nullptr)
1b555f17 2392 return nullptr; /* Dummy CU. */
2dc860c0 2393
7188ed02 2394 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2395
7188ed02 2396 return cu;
918dd910
JK
2397}
2398
97a1449a 2399/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2400
9291a0cd 2401static void
97a1449a 2402dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2403 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2404{
f4dc4d17
DE
2405 /* Skip type_unit_groups, reading the type units they contain
2406 is handled elsewhere. */
197400e8 2407 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2408 return;
2409
b303c6f6
AB
2410 /* The destructor of dwarf2_queue_guard frees any entries left on
2411 the queue. After this point we're guaranteed to leave this function
2412 with the dwarf queue empty. */
39856def 2413 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2414
976ca316 2415 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2416 {
976ca316
SM
2417 queue_comp_unit (per_cu, per_objfile, language_minimal);
2418 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2419
2420 /* If we just loaded a CU from a DWO, and we're working with an index
2421 that may badly handle TUs, load all the TUs in that DWO as well.
2422 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2423 if (!per_cu->is_debug_types
1b555f17
SM
2424 && cu != NULL
2425 && cu->dwo_unit != NULL
976ca316
SM
2426 && per_objfile->per_bfd->index_table != NULL
2427 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2428 /* DWP files aren't supported yet. */
976ca316 2429 && get_dwp_file (per_objfile) == NULL)
1b555f17 2430 queue_and_load_all_dwo_tus (cu);
95554aad 2431 }
9291a0cd 2432
976ca316 2433 process_queue (per_objfile);
9291a0cd
TT
2434
2435 /* Age the cache, releasing compilation units that have not
2436 been used recently. */
976ca316 2437 per_objfile->age_comp_units ();
9291a0cd
TT
2438}
2439
97a1449a
SM
2440/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2441 the per-objfile for which this symtab is instantiated.
2442
2443 Returns the resulting symbol table. */
2fdf6df6 2444
43f3e411 2445static struct compunit_symtab *
97a1449a 2446dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2447 dwarf2_per_objfile *per_objfile,
97a1449a 2448 bool skip_partial)
9291a0cd 2449{
976ca316 2450 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2451
976ca316 2452 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2453 {
976ca316 2454 free_cached_comp_units freer (per_objfile);
c83dd867 2455 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2456 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2457 process_cu_includes (per_objfile);
9291a0cd 2458 }
f194fefb 2459
976ca316 2460 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2461}
2462
ff4c9fec 2463/* See declaration. */
f4dc4d17 2464
ff4c9fec 2465dwarf2_per_cu_data *
5989a64e 2466dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2467{
b76e467d 2468 if (index >= this->all_comp_units.size ())
ff4c9fec 2469 {
b76e467d 2470 index -= this->all_comp_units.size ();
b2bdb8cf 2471 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2472 return &this->all_type_units[index]->per_cu;
2473 }
f4dc4d17 2474
ff4c9fec
SM
2475 return this->all_comp_units[index];
2476}
f4dc4d17 2477
ff4c9fec 2478/* See declaration. */
2fdf6df6 2479
ff4c9fec 2480dwarf2_per_cu_data *
5989a64e 2481dwarf2_per_bfd::get_cu (int index)
1fd400ff 2482{
b76e467d 2483 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2484
ff4c9fec 2485 return this->all_comp_units[index];
f4dc4d17
DE
2486}
2487
ff4c9fec 2488/* See declaration. */
f4dc4d17 2489
ff4c9fec 2490signatured_type *
5989a64e 2491dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2492{
b2bdb8cf 2493 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2494
ff4c9fec 2495 return this->all_type_units[index];
1fd400ff
TT
2496}
2497
d3473f0c
TT
2498/* See read.h. */
2499
2500dwarf2_per_cu_data *
5989a64e 2501dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2502{
2503 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2504 result->per_bfd = this;
d3473f0c
TT
2505 result->index = m_num_psymtabs++;
2506 return result;
2507}
2508
2509/* See read.h. */
2510
2511signatured_type *
5989a64e 2512dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2513{
2514 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2515 result->per_cu.per_bfd = this;
d3473f0c
TT
2516 result->per_cu.index = m_num_psymtabs++;
2517 return result;
2518}
2519
168c9250 2520/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2521 obstack, and constructed with the specified field values. */
4b514bc8
JK
2522
2523static dwarf2_per_cu_data *
168c9250
SM
2524create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2525 struct dwarf2_section_info *section,
2526 int is_dwz,
2527 sect_offset sect_off, ULONGEST length)
4b514bc8 2528{
168c9250 2529 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2530 the_cu->sect_off = sect_off;
2531 the_cu->length = length;
4b514bc8 2532 the_cu->section = section;
168c9250 2533 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2534 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2535 the_cu->is_dwz = is_dwz;
2536 return the_cu;
2537}
2538
2ec9a5e0
TT
2539/* A helper for create_cus_from_index that handles a given list of
2540 CUs. */
2fdf6df6 2541
74a0d9f6 2542static void
168c9250 2543create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2544 const gdb_byte *cu_list, offset_type n_elements,
2545 struct dwarf2_section_info *section,
b76e467d 2546 int is_dwz)
9291a0cd 2547{
12359b5e 2548 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2549 {
74a0d9f6 2550 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2551
2552 sect_offset sect_off
2553 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2554 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2555 cu_list += 2 * 8;
2556
b76e467d 2557 dwarf2_per_cu_data *per_cu
168c9250
SM
2558 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2559 length);
2560 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2561 }
9291a0cd
TT
2562}
2563
2ec9a5e0 2564/* Read the CU list from the mapped index, and use it to create all
168c9250 2565 the CU objects for PER_BFD. */
2ec9a5e0 2566
74a0d9f6 2567static void
168c9250 2568create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2569 const gdb_byte *cu_list, offset_type cu_list_elements,
2570 const gdb_byte *dwz_list, offset_type dwz_elements)
2571{
168c9250
SM
2572 gdb_assert (per_bfd->all_comp_units.empty ());
2573 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2574
168c9250
SM
2575 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2576 &per_bfd->info, 0);
2ec9a5e0
TT
2577
2578 if (dwz_elements == 0)
74a0d9f6 2579 return;
2ec9a5e0 2580
168c9250
SM
2581 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2582 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2583 &dwz->info, 1);
2ec9a5e0
TT
2584}
2585
1fd400ff 2586/* Create the signatured type hash table from the index. */
673bfd45 2587
74a0d9f6 2588static void
12359b5e 2589create_signatured_type_table_from_index
168c9250
SM
2590 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2591 const gdb_byte *bytes, offset_type elements)
1fd400ff 2592{
168c9250
SM
2593 gdb_assert (per_bfd->all_type_units.empty ());
2594 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2595
298e9637 2596 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2597
12359b5e 2598 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2599 {
52dc124a 2600 struct signatured_type *sig_type;
9c541725 2601 ULONGEST signature;
1fd400ff 2602 void **slot;
9c541725 2603 cu_offset type_offset_in_tu;
1fd400ff 2604
74a0d9f6 2605 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2606 sect_offset sect_off
2607 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2608 type_offset_in_tu
2609 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2610 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2611 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2612 bytes += 3 * 8;
2613
168c9250 2614 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2615 sig_type->signature = signature;
9c541725 2616 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2617 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2618 sig_type->per_cu.section = section;
9c541725 2619 sig_type->per_cu.sect_off = sect_off;
52dc124a 2620 sig_type->per_cu.v.quick
168c9250 2621 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2622 struct dwarf2_per_cu_quick_data);
2623
b0b6a987 2624 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2625 *slot = sig_type;
1fd400ff 2626
168c9250 2627 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2628 }
2629
168c9250 2630 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2631}
2632
927aa2e7
JK
2633/* Create the signatured type hash table from .debug_names. */
2634
2635static void
2636create_signatured_type_table_from_debug_names
976ca316 2637 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2638 const mapped_debug_names &map,
2639 struct dwarf2_section_info *section,
2640 struct dwarf2_section_info *abbrev_section)
2641{
976ca316 2642 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2643
96b79293
TT
2644 section->read (objfile);
2645 abbrev_section->read (objfile);
927aa2e7 2646
976ca316
SM
2647 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2648 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2649
298e9637 2650 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2651
2652 for (uint32_t i = 0; i < map.tu_count; ++i)
2653 {
2654 struct signatured_type *sig_type;
927aa2e7 2655 void **slot;
927aa2e7
JK
2656
2657 sect_offset sect_off
2658 = (sect_offset) (extract_unsigned_integer
2659 (map.tu_table_reordered + i * map.offset_size,
2660 map.offset_size,
2661 map.dwarf5_byte_order));
2662
2663 comp_unit_head cu_header;
976ca316 2664 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2665 abbrev_section,
927aa2e7
JK
2666 section->buffer + to_underlying (sect_off),
2667 rcuh_kind::TYPE);
2668
976ca316 2669 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2670 sig_type->signature = cu_header.signature;
2671 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2672 sig_type->per_cu.is_debug_types = 1;
2673 sig_type->per_cu.section = section;
2674 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2675 sig_type->per_cu.v.quick
976ca316 2676 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2677 struct dwarf2_per_cu_quick_data);
2678
b0b6a987 2679 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2680 *slot = sig_type;
2681
976ca316 2682 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2683 }
2684
976ca316 2685 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2686}
2687
9291a0cd
TT
2688/* Read the address map data from the mapped index, and use it to
2689 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2690
9291a0cd 2691static void
976ca316 2692create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2693 struct mapped_index *index)
9291a0cd 2694{
976ca316 2695 struct objfile *objfile = per_objfile->objfile;
08feed99 2696 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2697 const gdb_byte *iter, *end;
9291a0cd 2698 struct addrmap *mutable_map;
9291a0cd
TT
2699 CORE_ADDR baseaddr;
2700
8268c778
PA
2701 auto_obstack temp_obstack;
2702
9291a0cd
TT
2703 mutable_map = addrmap_create_mutable (&temp_obstack);
2704
f00a2de2
PA
2705 iter = index->address_table.data ();
2706 end = iter + index->address_table.size ();
9291a0cd 2707
b3b3bada 2708 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2709
2710 while (iter < end)
2711 {
2712 ULONGEST hi, lo, cu_index;
2713 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2714 iter += 8;
2715 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2716 iter += 8;
2717 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2718 iter += 4;
f652bce2 2719
24a55014 2720 if (lo > hi)
f652bce2 2721 {
b98664d3 2722 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2723 hex_string (lo), hex_string (hi));
24a55014 2724 continue;
f652bce2 2725 }
24a55014 2726
976ca316 2727 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2728 {
b98664d3 2729 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2730 (unsigned) cu_index);
24a55014 2731 continue;
f652bce2 2732 }
24a55014 2733
79748972
TT
2734 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2735 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2736 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2737 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2738 }
2739
d320c2b5 2740 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2741 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2742}
2743
927aa2e7
JK
2744/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2745 populate the objfile's psymtabs_addrmap. */
2746
2747static void
976ca316 2748create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2749 struct dwarf2_section_info *section)
2750{
976ca316 2751 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2752 bfd *abfd = objfile->obfd;
08feed99 2753 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2754 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2755
2756 auto_obstack temp_obstack;
2757 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2758
2759 std::unordered_map<sect_offset,
2760 dwarf2_per_cu_data *,
2761 gdb::hash_enum<sect_offset>>
2762 debug_info_offset_to_per_cu;
976ca316 2763 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2764 {
927aa2e7
JK
2765 const auto insertpair
2766 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2767 if (!insertpair.second)
2768 {
2769 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2770 "debug_info_offset %s, ignoring .debug_aranges."),
2771 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2772 return;
2773 }
2774 }
2775
96b79293 2776 section->read (objfile);
927aa2e7
JK
2777
2778 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2779
2780 const gdb_byte *addr = section->buffer;
2781
2782 while (addr < section->buffer + section->size)
2783 {
2784 const gdb_byte *const entry_addr = addr;
2785 unsigned int bytes_read;
2786
2787 const LONGEST entry_length = read_initial_length (abfd, addr,
2788 &bytes_read);
2789 addr += bytes_read;
2790
2791 const gdb_byte *const entry_end = addr + entry_length;
2792 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2793 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2794 if (addr + entry_length > section->buffer + section->size)
2795 {
47e3f474 2796 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2797 "length %s exceeds section length %s, "
2798 "ignoring .debug_aranges."),
47e3f474
TV
2799 objfile_name (objfile),
2800 plongest (entry_addr - section->buffer),
927aa2e7
JK
2801 plongest (bytes_read + entry_length),
2802 pulongest (section->size));
2803 return;
2804 }
2805
2806 /* The version number. */
2807 const uint16_t version = read_2_bytes (abfd, addr);
2808 addr += 2;
2809 if (version != 2)
2810 {
47e3f474 2811 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2812 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2815 return;
2816 }
2817
2818 const uint64_t debug_info_offset
2819 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2820 addr += offset_size;
2821 const auto per_cu_it
2822 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2823 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2824 {
47e3f474 2825 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2826 "debug_info_offset %s does not exists, "
2827 "ignoring .debug_aranges."),
47e3f474
TV
2828 objfile_name (objfile),
2829 plongest (entry_addr - section->buffer),
927aa2e7
JK
2830 pulongest (debug_info_offset));
2831 return;
2832 }
2833 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2834
2835 const uint8_t address_size = *addr++;
2836 if (address_size < 1 || address_size > 8)
2837 {
47e3f474 2838 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2839 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2840 objfile_name (objfile),
2841 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2842 return;
2843 }
2844
2845 const uint8_t segment_selector_size = *addr++;
2846 if (segment_selector_size != 0)
2847 {
47e3f474 2848 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2849 "segment_selector_size %u is not supported, "
2850 "ignoring .debug_aranges."),
47e3f474
TV
2851 objfile_name (objfile),
2852 plongest (entry_addr - section->buffer),
927aa2e7
JK
2853 segment_selector_size);
2854 return;
2855 }
2856
2857 /* Must pad to an alignment boundary that is twice the address
2858 size. It is undocumented by the DWARF standard but GCC does
2859 use it. */
2860 for (size_t padding = ((-(addr - section->buffer))
2861 & (2 * address_size - 1));
2862 padding > 0; padding--)
2863 if (*addr++ != 0)
2864 {
47e3f474 2865 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2866 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2867 objfile_name (objfile),
2868 plongest (entry_addr - section->buffer));
927aa2e7
JK
2869 return;
2870 }
2871
2872 for (;;)
2873 {
2874 if (addr + 2 * address_size > entry_end)
2875 {
47e3f474 2876 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2877 "address list is not properly terminated, "
2878 "ignoring .debug_aranges."),
47e3f474
TV
2879 objfile_name (objfile),
2880 plongest (entry_addr - section->buffer));
927aa2e7
JK
2881 return;
2882 }
2883 ULONGEST start = extract_unsigned_integer (addr, address_size,
2884 dwarf5_byte_order);
2885 addr += address_size;
2886 ULONGEST length = extract_unsigned_integer (addr, address_size,
2887 dwarf5_byte_order);
2888 addr += address_size;
2889 if (start == 0 && length == 0)
2890 break;
976ca316 2891 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2892 {
2893 /* Symbol was eliminated due to a COMDAT group. */
2894 continue;
2895 }
2896 ULONGEST end = start + length;
79748972
TT
2897 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2898 - baseaddr);
2899 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2900 - baseaddr);
927aa2e7
JK
2901 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2902 }
2903 }
2904
d320c2b5 2905 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2906 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2907}
2908
9291a0cd
TT
2909/* Find a slot in the mapped index INDEX for the object named NAME.
2910 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2911 constant pool and return true. If NAME cannot be found, return
2912 false. */
2fdf6df6 2913
109483d9 2914static bool
9291a0cd
TT
2915find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2916 offset_type **vec_out)
2917{
0cf03b49 2918 offset_type hash;
9291a0cd 2919 offset_type slot, step;
559a7a62 2920 int (*cmp) (const char *, const char *);
9291a0cd 2921
791afaa2 2922 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2923 if (current_language->la_language == language_cplus
45280282
IB
2924 || current_language->la_language == language_fortran
2925 || current_language->la_language == language_d)
0cf03b49
JK
2926 {
2927 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2928 not contain any. */
a8719064 2929
72998fb3 2930 if (strchr (name, '(') != NULL)
0cf03b49 2931 {
109483d9 2932 without_params = cp_remove_params (name);
0cf03b49 2933
72998fb3 2934 if (without_params != NULL)
791afaa2 2935 name = without_params.get ();
0cf03b49
JK
2936 }
2937 }
2938
559a7a62 2939 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2940 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2941 simulate our NAME being searched is also lowercased. */
2942 hash = mapped_index_string_hash ((index->version == 4
2943 && case_sensitivity == case_sensitive_off
2944 ? 5 : index->version),
2945 name);
2946
f00a2de2
PA
2947 slot = hash & (index->symbol_table.size () - 1);
2948 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2949 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2950
2951 for (;;)
2952 {
9291a0cd 2953 const char *str;
f00a2de2
PA
2954
2955 const auto &bucket = index->symbol_table[slot];
2956 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2957 return false;
9291a0cd 2958
f00a2de2 2959 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2960 if (!cmp (name, str))
9291a0cd
TT
2961 {
2962 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2963 + MAYBE_SWAP (bucket.vec));
109483d9 2964 return true;
9291a0cd
TT
2965 }
2966
f00a2de2 2967 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2968 }
2969}
2970
4485a1c1
SM
2971/* A helper function that reads the .gdb_index from BUFFER and fills
2972 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2973 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2974 ok to use deprecated sections.
2975
2976 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2977 out parameters that are filled in with information about the CU and
2978 TU lists in the section.
2979
4485a1c1 2980 Returns true if all went well, false otherwise. */
2fdf6df6 2981
d33bc52e 2982static bool
3810f182 2983read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2984 bool deprecated_ok,
2985 gdb::array_view<const gdb_byte> buffer,
2986 struct mapped_index *map,
2987 const gdb_byte **cu_list,
2988 offset_type *cu_list_elements,
2989 const gdb_byte **types_list,
2990 offset_type *types_list_elements)
2991{
2992 const gdb_byte *addr = &buffer[0];
82430852 2993
9291a0cd 2994 /* Version check. */
4485a1c1 2995 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2996 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2997 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2998 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2999 indices. */
831adc1f 3000 if (version < 4)
481860b3
GB
3001 {
3002 static int warning_printed = 0;
3003 if (!warning_printed)
3004 {
3005 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3006 filename);
481860b3
GB
3007 warning_printed = 1;
3008 }
3009 return 0;
3010 }
3011 /* Index version 4 uses a different hash function than index version
3012 5 and later.
3013
3014 Versions earlier than 6 did not emit psymbols for inlined
3015 functions. Using these files will cause GDB not to be able to
3016 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3017 indices unless the user has done
3018 "set use-deprecated-index-sections on". */
2ec9a5e0 3019 if (version < 6 && !deprecated_ok)
481860b3
GB
3020 {
3021 static int warning_printed = 0;
3022 if (!warning_printed)
3023 {
e615022a
DE
3024 warning (_("\
3025Skipping deprecated .gdb_index section in %s.\n\
3026Do \"set use-deprecated-index-sections on\" before the file is read\n\
3027to use the section anyway."),
2ec9a5e0 3028 filename);
481860b3
GB
3029 warning_printed = 1;
3030 }
3031 return 0;
3032 }
796a7ff8 3033 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3034 of the TU (for symbols coming from TUs),
3035 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3036 Plus gold-generated indices can have duplicate entries for global symbols,
3037 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3038 These are just performance bugs, and we can't distinguish gdb-generated
3039 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3040
481860b3 3041 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3042 longer backward compatible. */
796a7ff8 3043 if (version > 8)
594e8718 3044 return 0;
9291a0cd 3045
559a7a62 3046 map->version = version;
9291a0cd 3047
4485a1c1 3048 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3049
4485a1c1 3050 int i = 0;
2ec9a5e0
TT
3051 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3052 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3053 / 8);
1fd400ff
TT
3054 ++i;
3055
2ec9a5e0
TT
3056 *types_list = addr + MAYBE_SWAP (metadata[i]);
3057 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3058 - MAYBE_SWAP (metadata[i]))
3059 / 8);
987d643c 3060 ++i;
1fd400ff 3061
f00a2de2
PA
3062 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3063 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3064 map->address_table
3065 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3066 ++i;
3067
f00a2de2
PA
3068 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3069 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3070 map->symbol_table
3071 = gdb::array_view<mapped_index::symbol_table_slot>
3072 ((mapped_index::symbol_table_slot *) symbol_table,
3073 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3074
f00a2de2 3075 ++i;
f9d83a0b 3076 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3077
2ec9a5e0
TT
3078 return 1;
3079}
3080
4485a1c1
SM
3081/* Callback types for dwarf2_read_gdb_index. */
3082
3083typedef gdb::function_view
5989a64e 3084 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3085 get_gdb_index_contents_ftype;
3086typedef gdb::function_view
3087 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3088 get_gdb_index_contents_dwz_ftype;
3089
927aa2e7 3090/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3091 elements of all the CUs and return 1. Otherwise, return 0. */
3092
3093static int
4485a1c1 3094dwarf2_read_gdb_index
976ca316 3095 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3096 get_gdb_index_contents_ftype get_gdb_index_contents,
3097 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3098{
2ec9a5e0
TT
3099 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3100 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3101 struct dwz_file *dwz;
976ca316 3102 struct objfile *objfile = per_objfile->objfile;
f8c41851 3103 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3104
4485a1c1 3105 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3106 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3107
3108 if (main_index_contents.empty ())
3109 return 0;
3110
3063847f 3111 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3112 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3113 use_deprecated_index_sections,
3114 main_index_contents, map.get (), &cu_list,
3115 &cu_list_elements, &types_list,
3116 &types_list_elements))
2ec9a5e0
TT
3117 return 0;
3118
0fefef59 3119 /* Don't use the index if it's empty. */
3063847f 3120 if (map->symbol_table.empty ())
0fefef59
DE
3121 return 0;
3122
2ec9a5e0
TT
3123 /* If there is a .dwz file, read it so we can get its CU list as
3124 well. */
f8c41851 3125 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3126 if (dwz != NULL)
2ec9a5e0 3127 {
2ec9a5e0
TT
3128 struct mapped_index dwz_map;
3129 const gdb_byte *dwz_types_ignore;
3130 offset_type dwz_types_elements_ignore;
3131
4485a1c1
SM
3132 gdb::array_view<const gdb_byte> dwz_index_content
3133 = get_gdb_index_contents_dwz (objfile, dwz);
3134
3135 if (dwz_index_content.empty ())
3136 return 0;
3137
3810f182 3138 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3139 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3140 &dwz_list, &dwz_list_elements,
3141 &dwz_types_ignore,
3142 &dwz_types_elements_ignore))
2ec9a5e0
TT
3143 {
3144 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3145 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3146 return 0;
3147 }
3148 }
3149
f8c41851
SM
3150 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3151 dwz_list_elements);
1fd400ff 3152
8b70b953
TT
3153 if (types_list_elements)
3154 {
8b70b953
TT
3155 /* We can only handle a single .debug_types when we have an
3156 index. */
f8c41851 3157 if (per_bfd->types.size () != 1)
8b70b953
TT
3158 return 0;
3159
f8c41851 3160 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3161
f8c41851 3162 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3163 types_list_elements);
8b70b953 3164 }
9291a0cd 3165
976ca316 3166 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3167
f8c41851
SM
3168 per_bfd->index_table = std::move (map);
3169 per_bfd->using_index = 1;
3170 per_bfd->quick_file_names_table =
3171 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3172
3173 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3174 objfiles using the same BFD. */
3175 gdb_assert (per_bfd->partial_symtabs == nullptr);
3176 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3177
3178 return 1;
3179}
3180
dee91e82 3181/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3182
dee91e82
DE
3183static void
3184dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3185 const gdb_byte *info_ptr,
3e225074 3186 struct die_info *comp_unit_die)
9291a0cd 3187{
dee91e82 3188 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3189 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3191 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3192 struct attribute *attr;
7b9f3c50
DE
3193 void **slot;
3194 struct quick_file_names *qfn;
9291a0cd 3195
0186c6a7
DE
3196 gdb_assert (! this_cu->is_debug_types);
3197
07261596
TT
3198 /* Our callers never want to match partial units -- instead they
3199 will match the enclosing full CU. */
3200 if (comp_unit_die->tag == DW_TAG_partial_unit)
3201 {
3202 this_cu->v.quick->no_file_data = 1;
3203 return;
3204 }
3205
0186c6a7 3206 lh_cu = this_cu;
7b9f3c50 3207 slot = NULL;
dee91e82 3208
fff8551c 3209 line_header_up lh;
9c541725 3210 sect_offset line_offset {};
fff8551c 3211
dee91e82 3212 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3213 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3214 {
7b9f3c50
DE
3215 struct quick_file_names find_entry;
3216
d4df075e 3217 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3218
3219 /* We may have already read in this line header (TU line header sharing).
3220 If we have we're done. */
094b34ac 3221 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3222 find_entry.hash.line_sect_off = line_offset;
976ca316 3223 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3224 &find_entry, INSERT);
3225 if (*slot != NULL)
3226 {
9a3c8263 3227 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3228 return;
7b9f3c50
DE
3229 }
3230
3019eac3 3231 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3232 }
3233 if (lh == NULL)
3234 {
094b34ac 3235 lh_cu->v.quick->no_file_data = 1;
dee91e82 3236 return;
9291a0cd
TT
3237 }
3238
976ca316 3239 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3240 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3241 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3242 gdb_assert (slot != NULL);
3243 *slot = qfn;
9291a0cd 3244
d721ba37 3245 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3246
aa391654
TT
3247 int offset = 0;
3248 if (strcmp (fnd.name, "<unknown>") != 0)
3249 ++offset;
3250
7ba99d21 3251 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3252 qfn->file_names =
976ca316 3253 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3254 qfn->num_file_names);
aa391654
TT
3255 if (offset != 0)
3256 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3257 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3258 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3259 fnd.comp_dir).release ();
7b9f3c50 3260 qfn->real_names = NULL;
9291a0cd 3261
094b34ac 3262 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3263}
3264
3265/* A helper for the "quick" functions which attempts to read the line
3266 table for THIS_CU. */
3267
3268static struct quick_file_names *
ab432490
SM
3269dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3270 dwarf2_per_objfile *per_objfile)
dee91e82 3271{
0186c6a7
DE
3272 /* This should never be called for TUs. */
3273 gdb_assert (! this_cu->is_debug_types);
3274 /* Nor type unit groups. */
197400e8 3275 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3276
dee91e82
DE
3277 if (this_cu->v.quick->file_names != NULL)
3278 return this_cu->v.quick->file_names;
3279 /* If we know there is no line data, no point in looking again. */
3280 if (this_cu->v.quick->no_file_data)
3281 return NULL;
3282
ab432490 3283 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3284 if (!reader.dummy_p)
3e225074 3285 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3286
3287 if (this_cu->v.quick->no_file_data)
3288 return NULL;
3289 return this_cu->v.quick->file_names;
9291a0cd
TT
3290}
3291
3292/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3293 real path for a given file name from the line table. */
2fdf6df6 3294
9291a0cd 3295static const char *
976ca316 3296dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3297 struct quick_file_names *qfn, int index)
9291a0cd 3298{
7b9f3c50 3299 if (qfn->real_names == NULL)
976ca316 3300 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3301 qfn->num_file_names, const char *);
9291a0cd 3302
7b9f3c50 3303 if (qfn->real_names[index] == NULL)
14278e1f 3304 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3305
7b9f3c50 3306 return qfn->real_names[index];
9291a0cd
TT
3307}
3308
3309static struct symtab *
3310dw2_find_last_source_symtab (struct objfile *objfile)
3311{
976ca316
SM
3312 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3313 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3314 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3315
43f3e411
DE
3316 if (cust == NULL)
3317 return NULL;
ed2dc618 3318
43f3e411 3319 return compunit_primary_filetab (cust);
9291a0cd
TT
3320}
3321
7b9f3c50
DE
3322/* Traversal function for dw2_forget_cached_source_info. */
3323
3324static int
3325dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3326{
7b9f3c50 3327 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3328
7b9f3c50 3329 if (file_data->real_names)
9291a0cd 3330 {
7b9f3c50 3331 int i;
9291a0cd 3332
7b9f3c50 3333 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3334 {
7b9f3c50
DE
3335 xfree ((void*) file_data->real_names[i]);
3336 file_data->real_names[i] = NULL;
9291a0cd
TT
3337 }
3338 }
7b9f3c50
DE
3339
3340 return 1;
3341}
3342
3343static void
3344dw2_forget_cached_source_info (struct objfile *objfile)
3345{
976ca316 3346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3347
976ca316 3348 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3349 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3350}
3351
f8eba3c6
TT
3352/* Helper function for dw2_map_symtabs_matching_filename that expands
3353 the symtabs and calls the iterator. */
3354
3355static int
3356dw2_map_expand_apply (struct objfile *objfile,
3357 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3358 const char *name, const char *real_path,
14bc53a8 3359 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3360{
43f3e411 3361 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3362
3363 /* Don't visit already-expanded CUs. */
af758d11
SM
3364 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3365 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3366 return 0;
3367
3368 /* This may expand more than one symtab, and we want to iterate over
3369 all of them. */
97a1449a 3370 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3371
14bc53a8
PA
3372 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3373 last_made, callback);
f8eba3c6
TT
3374}
3375
3376/* Implementation of the map_symtabs_matching_filename method. */
3377
14bc53a8
PA
3378static bool
3379dw2_map_symtabs_matching_filename
3380 (struct objfile *objfile, const char *name, const char *real_path,
3381 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3382{
c011a4f4 3383 const char *name_basename = lbasename (name);
976ca316 3384 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3385
848e3e78
DE
3386 /* The rule is CUs specify all the files, including those used by
3387 any TU, so there's no need to scan TUs here. */
f4dc4d17 3388
976ca316 3389 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3390 {
3d7bb9d9 3391 /* We only need to look at symtabs not already expanded. */
976ca316 3392 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3393 continue;
3394
976ca316 3395 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3396 if (file_data == NULL)
9291a0cd
TT
3397 continue;
3398
b76e467d 3399 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3400 {
7b9f3c50 3401 const char *this_name = file_data->file_names[j];
da235a7c 3402 const char *this_real_name;
9291a0cd 3403
af529f8f 3404 if (compare_filenames_for_search (this_name, name))
9291a0cd 3405 {
f5b95b50 3406 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3407 callback))
3408 return true;
288e77a7 3409 continue;
4aac40c8 3410 }
9291a0cd 3411
c011a4f4
DE
3412 /* Before we invoke realpath, which can get expensive when many
3413 files are involved, do a quick comparison of the basenames. */
3414 if (! basenames_may_differ
3415 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3416 continue;
3417
976ca316 3418 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3419 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3420 {
da235a7c 3421 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3422 callback))
3423 return true;
288e77a7 3424 continue;
da235a7c 3425 }
9291a0cd 3426
da235a7c
JK
3427 if (real_path != NULL)
3428 {
af529f8f
JK
3429 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3430 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3431 if (this_real_name != NULL
af529f8f 3432 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3433 {
f5b95b50 3434 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3435 callback))
3436 return true;
288e77a7 3437 continue;
9291a0cd
TT
3438 }
3439 }
3440 }
3441 }
3442
14bc53a8 3443 return false;
9291a0cd
TT
3444}
3445
da51c347
DE
3446/* Struct used to manage iterating over all CUs looking for a symbol. */
3447
3448struct dw2_symtab_iterator
9291a0cd 3449{
ed2dc618 3450 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3451 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3452 /* If set, only look for symbols that match that block. Valid values are
3453 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3454 gdb::optional<block_enum> block_index;
da51c347
DE
3455 /* The kind of symbol we're looking for. */
3456 domain_enum domain;
3457 /* The list of CUs from the index entry of the symbol,
3458 or NULL if not found. */
3459 offset_type *vec;
3460 /* The next element in VEC to look at. */
3461 int next;
3462 /* The number of elements in VEC, or zero if there is no match. */
3463 int length;
8943b874
DE
3464 /* Have we seen a global version of the symbol?
3465 If so we can ignore all further global instances.
3466 This is to work around gold/15646, inefficient gold-generated
3467 indices. */
3468 int global_seen;
da51c347 3469};
9291a0cd 3470
e5f3ece2 3471/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3472
9291a0cd 3473static void
e5f3ece2
TV
3474dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3475 dwarf2_per_objfile *per_objfile,
3476 gdb::optional<block_enum> block_index,
3477 domain_enum domain)
da51c347 3478{
976ca316 3479 iter->per_objfile = per_objfile;
da51c347
DE
3480 iter->block_index = block_index;
3481 iter->domain = domain;
3482 iter->next = 0;
8943b874 3483 iter->global_seen = 0;
e5f3ece2
TV
3484 iter->vec = NULL;
3485 iter->length = 0;
3486}
da51c347 3487
e5f3ece2
TV
3488/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3489
3490static void
3491dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3492 dwarf2_per_objfile *per_objfile,
3493 gdb::optional<block_enum> block_index,
3494 domain_enum domain,
3495 const char *name)
3496{
3497 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3498
e5f3ece2 3499 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3500 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3501 if (index == NULL)
3502 return;
3503
3504 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3505 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3506}
3507
3508/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3509
3510static void
3511dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3512 dwarf2_per_objfile *per_objfile,
3513 gdb::optional<block_enum> block_index,
3514 domain_enum domain, offset_type namei)
3515{
3516 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3517
3518 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3519 /* index is NULL if OBJF_READNOW. */
3520 if (index == NULL)
3521 return;
3522
3523 gdb_assert (!index->symbol_name_slot_invalid (namei));
3524 const auto &bucket = index->symbol_table[namei];
3525
3526 iter->vec = (offset_type *) (index->constant_pool
3527 + MAYBE_SWAP (bucket.vec));
3528 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3529}
3530
3531/* Return the next matching CU or NULL if there are no more. */
3532
3533static struct dwarf2_per_cu_data *
3534dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3535{
976ca316 3536 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3537
da51c347
DE
3538 for ( ; iter->next < iter->length; ++iter->next)
3539 {
3540 offset_type cu_index_and_attrs =
3541 MAYBE_SWAP (iter->vec[iter->next + 1]);
3542 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3543 gdb_index_symbol_kind symbol_kind =
3544 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3545 /* Only check the symbol attributes if they're present.
3546 Indices prior to version 7 don't record them,
3547 and indices >= 7 may elide them for certain symbols
3548 (gold does this). */
3549 int attrs_valid =
976ca316 3550 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3551 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3552
3190f0c6 3553 /* Don't crash on bad data. */
976ca316
SM
3554 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3555 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3556 {
b98664d3 3557 complaint (_(".gdb_index entry has bad CU index"
976ca316 3558 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3559 continue;
3560 }
3561
976ca316 3562 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3563
da51c347 3564 /* Skip if already read in. */
976ca316 3565 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3566 continue;
3567
8943b874
DE
3568 /* Check static vs global. */
3569 if (attrs_valid)
3570 {
2b79f376
SM
3571 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3572
3573 if (iter->block_index.has_value ())
3574 {
3575 bool want_static = *iter->block_index == STATIC_BLOCK;
3576
3577 if (is_static != want_static)
3578 continue;
3579 }
3580
8943b874 3581 /* Work around gold/15646. */
f030440d
TV
3582 if (!is_static
3583 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3584 {
3585 if (iter->global_seen)
3586 continue;
3587
3588 iter->global_seen = 1;
3589 }
8943b874 3590 }
da51c347
DE
3591
3592 /* Only check the symbol's kind if it has one. */
3593 if (attrs_valid)
3594 {
3595 switch (iter->domain)
3596 {
3597 case VAR_DOMAIN:
3598 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3599 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3600 /* Some types are also in VAR_DOMAIN. */
3601 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3602 continue;
3603 break;
3604 case STRUCT_DOMAIN:
3605 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3606 continue;
3607 break;
3608 case LABEL_DOMAIN:
3609 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3610 continue;
3611 break;
59c35742
AB
3612 case MODULE_DOMAIN:
3613 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3614 continue;
3615 break;
da51c347
DE
3616 default:
3617 break;
3618 }
3619 }
3620
3621 ++iter->next;
3622 return per_cu;
3623 }
3624
3625 return NULL;
3626}
3627
43f3e411 3628static struct compunit_symtab *
c7f839cb 3629dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3630 const char *name, domain_enum domain)
9291a0cd 3631{
43f3e411 3632 struct compunit_symtab *stab_best = NULL;
976ca316 3633 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3634
b5ec771e
PA
3635 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3636
ed2dc618
SM
3637 struct dw2_symtab_iterator iter;
3638 struct dwarf2_per_cu_data *per_cu;
da51c347 3639
976ca316 3640 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3641
ed2dc618
SM
3642 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3643 {
3644 struct symbol *sym, *with_opaque = NULL;
97a1449a 3645 struct compunit_symtab *stab
976ca316 3646 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3647 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3648 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3649
ed2dc618
SM
3650 sym = block_find_symbol (block, name, domain,
3651 block_find_non_opaque_type_preferred,
3652 &with_opaque);
b2e2f908 3653
ed2dc618
SM
3654 /* Some caution must be observed with overloaded functions
3655 and methods, since the index will not contain any overload
3656 information (but NAME might contain it). */
da51c347 3657
ed2dc618
SM
3658 if (sym != NULL
3659 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3660 return stab;
3661 if (with_opaque != NULL
3662 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3663 stab_best = stab;
da51c347 3664
ed2dc618 3665 /* Keep looking through other CUs. */
9291a0cd 3666 }
9291a0cd 3667
da51c347 3668 return stab_best;
9291a0cd
TT
3669}
3670
3671static void
3672dw2_print_stats (struct objfile *objfile)
3673{
976ca316
SM
3674 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3675 int total = (per_objfile->per_bfd->all_comp_units.size ()
3676 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3677 int count = 0;
9291a0cd 3678
ed2dc618 3679 for (int i = 0; i < total; ++i)
9291a0cd 3680 {
976ca316 3681 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3682
976ca316 3683 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3684 ++count;
3685 }
e4a48d9d 3686 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3687 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3688}
3689
779bd270
DE
3690/* This dumps minimal information about the index.
3691 It is called via "mt print objfiles".
3692 One use is to verify .gdb_index has been loaded by the
3693 gdb.dwarf2/gdb-index.exp testcase. */
3694
9291a0cd
TT
3695static void
3696dw2_dump (struct objfile *objfile)
3697{
976ca316 3698 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3699
976ca316 3700 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3701 printf_filtered (".gdb_index:");
976ca316 3702 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3703 {
3704 printf_filtered (" version %d\n",
976ca316 3705 per_objfile->per_bfd->index_table->version);
779bd270
DE
3706 }
3707 else
3708 printf_filtered (" faked for \"readnow\"\n");
3709 printf_filtered ("\n");
9291a0cd
TT
3710}
3711
9291a0cd
TT
3712static void
3713dw2_expand_symtabs_for_function (struct objfile *objfile,
3714 const char *func_name)
3715{
976ca316 3716 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3717
ed2dc618
SM
3718 struct dw2_symtab_iterator iter;
3719 struct dwarf2_per_cu_data *per_cu;
da51c347 3720
976ca316 3721 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3722
ed2dc618 3723 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3724 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3725
9291a0cd
TT
3726}
3727
3728static void
3729dw2_expand_all_symtabs (struct objfile *objfile)
3730{
976ca316
SM
3731 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3732 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3733 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3734
ed2dc618 3735 for (int i = 0; i < total_units; ++i)
9291a0cd 3736 {
976ca316 3737 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3738
58f0c718
TT
3739 /* We don't want to directly expand a partial CU, because if we
3740 read it with the wrong language, then assertion failures can
3741 be triggered later on. See PR symtab/23010. So, tell
3742 dw2_instantiate_symtab to skip partial CUs -- any important
3743 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3744 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3745 }
3746}
3747
3748static void
652a8996
JK
3749dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3750 const char *fullname)
9291a0cd 3751{
976ca316 3752 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3753
3754 /* We don't need to consider type units here.
3755 This is only called for examining code, e.g. expand_line_sal.
3756 There can be an order of magnitude (or more) more type units
3757 than comp units, and we avoid them if we can. */
3758
976ca316 3759 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3760 {
3d7bb9d9 3761 /* We only need to look at symtabs not already expanded. */
976ca316 3762 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3763 continue;
3764
976ca316 3765 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3766 if (file_data == NULL)
9291a0cd
TT
3767 continue;
3768
b76e467d 3769 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3770 {
652a8996
JK
3771 const char *this_fullname = file_data->file_names[j];
3772
3773 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3774 {
976ca316 3775 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3776 break;
3777 }
3778 }
3779 }
3780}
3781
9a0bacfb
TV
3782static void
3783dw2_expand_symtabs_matching_symbol
3784 (mapped_index_base &index,
3785 const lookup_name_info &lookup_name_in,
3786 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3787 enum search_domain kind,
fcf23d5b
SM
3788 gdb::function_view<bool (offset_type)> match_callback,
3789 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3790
3791static void
3792dw2_expand_symtabs_matching_one
97a1449a
SM
3793 (dwarf2_per_cu_data *per_cu,
3794 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3795 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3796 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3797
9291a0cd 3798static void
199b4314
TT
3799dw2_map_matching_symbols
3800 (struct objfile *objfile,
b054970d 3801 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3802 int global,
3803 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3804 symbol_compare_ftype *ordered_compare)
9291a0cd 3805{
1aa98955 3806 /* Used for Ada. */
976ca316 3807 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3808
9a0bacfb
TV
3809 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3810
976ca316 3811 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3812 {
976ca316 3813 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3814
9a0bacfb
TV
3815 const char *match_name = name.ada ().lookup_name ().c_str ();
3816 auto matcher = [&] (const char *symname)
3817 {
3818 if (ordered_compare == nullptr)
3819 return true;
3820 return ordered_compare (symname, match_name) == 0;
3821 };
3822
3823 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3824 [&] (offset_type namei)
3825 {
3826 struct dw2_symtab_iterator iter;
3827 struct dwarf2_per_cu_data *per_cu;
3828
976ca316 3829 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3830 namei);
9a0bacfb 3831 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3832 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3833 nullptr);
9a0bacfb 3834 return true;
976ca316 3835 }, per_objfile);
9a0bacfb
TV
3836 }
3837 else
3838 {
3839 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3840 proceed assuming all symtabs have been read in. */
3841 }
1aa98955
TV
3842
3843 for (compunit_symtab *cust : objfile->compunits ())
3844 {
3845 const struct block *block;
3846
3847 if (cust == NULL)
3848 continue;
3849 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3850 if (!iterate_over_symbols_terminated (block, name,
3851 domain, callback))
3852 return;
3853 }
9291a0cd
TT
3854}
3855
e1ef7d7a
PA
3856/* Starting from a search name, return the string that finds the upper
3857 bound of all strings that start with SEARCH_NAME in a sorted name
3858 list. Returns the empty string to indicate that the upper bound is
3859 the end of the list. */
3860
3861static std::string
3862make_sort_after_prefix_name (const char *search_name)
3863{
3864 /* When looking to complete "func", we find the upper bound of all
3865 symbols that start with "func" by looking for where we'd insert
3866 the closest string that would follow "func" in lexicographical
3867 order. Usually, that's "func"-with-last-character-incremented,
3868 i.e. "fund". Mind non-ASCII characters, though. Usually those
3869 will be UTF-8 multi-byte sequences, but we can't be certain.
3870 Especially mind the 0xff character, which is a valid character in
3871 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3872 rule out compilers allowing it in identifiers. Note that
3873 conveniently, strcmp/strcasecmp are specified to compare
3874 characters interpreted as unsigned char. So what we do is treat
3875 the whole string as a base 256 number composed of a sequence of
3876 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3877 to 0, and carries 1 to the following more-significant position.
3878 If the very first character in SEARCH_NAME ends up incremented
3879 and carries/overflows, then the upper bound is the end of the
3880 list. The string after the empty string is also the empty
3881 string.
3882
3883 Some examples of this operation:
3884
3885 SEARCH_NAME => "+1" RESULT
3886
3887 "abc" => "abd"
3888 "ab\xff" => "ac"
3889 "\xff" "a" "\xff" => "\xff" "b"
3890 "\xff" => ""
3891 "\xff\xff" => ""
3892 "" => ""
3893
3894 Then, with these symbols for example:
3895
3896 func
3897 func1
3898 fund
3899
3900 completing "func" looks for symbols between "func" and
3901 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3902 which finds "func" and "func1", but not "fund".
3903
3904 And with:
3905
3906 funcÿ (Latin1 'ÿ' [0xff])
3907 funcÿ1
3908 fund
3909
3910 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3911 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3912
3913 And with:
3914
3915 ÿÿ (Latin1 'ÿ' [0xff])
3916 ÿÿ1
3917
3918 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3919 the end of the list.
3920 */
3921 std::string after = search_name;
3922 while (!after.empty () && (unsigned char) after.back () == 0xff)
3923 after.pop_back ();
3924 if (!after.empty ())
3925 after.back () = (unsigned char) after.back () + 1;
3926 return after;
3927}
3928
5c58de74 3929/* See declaration. */
61d96d7e 3930
5c58de74
PA
3931std::pair<std::vector<name_component>::const_iterator,
3932 std::vector<name_component>::const_iterator>
44ed8f3e 3933mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3934 (const lookup_name_info &lookup_name_without_params, language lang,
3935 dwarf2_per_objfile *per_objfile) const
3f563c84 3936{
5c58de74
PA
3937 auto *name_cmp
3938 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3939
3b00ef10 3940 const char *lang_name
e0802d59 3941 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3942
3f563c84
PA
3943 /* Comparison function object for lower_bound that matches against a
3944 given symbol name. */
3945 auto lookup_compare_lower = [&] (const name_component &elem,
3946 const char *name)
3947 {
fcf23d5b 3948 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3949 const char *elem_name = elem_qualified + elem.name_offset;
3950 return name_cmp (elem_name, name) < 0;
3951 };
3952
3953 /* Comparison function object for upper_bound that matches against a
3954 given symbol name. */
3955 auto lookup_compare_upper = [&] (const char *name,
3956 const name_component &elem)
3957 {
fcf23d5b 3958 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3959 const char *elem_name = elem_qualified + elem.name_offset;
3960 return name_cmp (name, elem_name) < 0;
3961 };
3962
5c58de74
PA
3963 auto begin = this->name_components.begin ();
3964 auto end = this->name_components.end ();
3f563c84
PA
3965
3966 /* Find the lower bound. */
3967 auto lower = [&] ()
3968 {
3b00ef10 3969 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3970 return begin;
3971 else
3b00ef10 3972 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3973 } ();
3974
3975 /* Find the upper bound. */
3976 auto upper = [&] ()
3977 {
5c58de74 3978 if (lookup_name_without_params.completion_mode ())
3f563c84 3979 {
e1ef7d7a
PA
3980 /* In completion mode, we want UPPER to point past all
3981 symbols names that have the same prefix. I.e., with
3982 these symbols, and completing "func":
3983
3984 function << lower bound
3985 function1
3986 other_function << upper bound
3987
3988 We find the upper bound by looking for the insertion
3989 point of "func"-with-last-character-incremented,
3990 i.e. "fund". */
3b00ef10 3991 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3992 if (after.empty ())
3f563c84 3993 return end;
e6b2f5ef
PA
3994 return std::lower_bound (lower, end, after.c_str (),
3995 lookup_compare_lower);
3f563c84
PA
3996 }
3997 else
3b00ef10 3998 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3999 } ();
4000
5c58de74
PA
4001 return {lower, upper};
4002}
4003
4004/* See declaration. */
4005
4006void
fcf23d5b 4007mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4008{
4009 if (!this->name_components.empty ())
4010 return;
4011
4012 this->name_components_casing = case_sensitivity;
4013 auto *name_cmp
4014 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4015
4016 /* The code below only knows how to break apart components of C++
4017 symbol names (and other languages that use '::' as
3b00ef10 4018 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4019 auto count = this->symbol_name_count ();
4020 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4021 {
44ed8f3e 4022 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4023 continue;
4024
fcf23d5b 4025 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4026
4027 /* Add each name component to the name component table. */
4028 unsigned int previous_len = 0;
3b00ef10
TT
4029
4030 if (strstr (name, "::") != nullptr)
4031 {
4032 for (unsigned int current_len = cp_find_first_component (name);
4033 name[current_len] != '\0';
4034 current_len += cp_find_first_component (name + current_len))
4035 {
4036 gdb_assert (name[current_len] == ':');
4037 this->name_components.push_back ({previous_len, idx});
4038 /* Skip the '::'. */
4039 current_len += 2;
4040 previous_len = current_len;
4041 }
4042 }
4043 else
5c58de74 4044 {
3b00ef10
TT
4045 /* Handle the Ada encoded (aka mangled) form here. */
4046 for (const char *iter = strstr (name, "__");
4047 iter != nullptr;
4048 iter = strstr (iter, "__"))
4049 {
4050 this->name_components.push_back ({previous_len, idx});
4051 iter += 2;
4052 previous_len = iter - name;
4053 }
5c58de74 4054 }
3b00ef10 4055
5c58de74
PA
4056 this->name_components.push_back ({previous_len, idx});
4057 }
4058
4059 /* Sort name_components elements by name. */
4060 auto name_comp_compare = [&] (const name_component &left,
4061 const name_component &right)
4062 {
fcf23d5b
SM
4063 const char *left_qualified
4064 = this->symbol_name_at (left.idx, per_objfile);
4065 const char *right_qualified
4066 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4067
4068 const char *left_name = left_qualified + left.name_offset;
4069 const char *right_name = right_qualified + right.name_offset;
4070
4071 return name_cmp (left_name, right_name) < 0;
4072 };
4073
4074 std::sort (this->name_components.begin (),
4075 this->name_components.end (),
4076 name_comp_compare);
4077}
4078
4079/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4080 mapped_index_base instead of the containing objfile. This is split
4081 to a separate function in order to be able to unit test the
4082 name_components matching using a mock mapped_index_base. For each
5c58de74 4083 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4084 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4085
4086static void
4087dw2_expand_symtabs_matching_symbol
44ed8f3e 4088 (mapped_index_base &index,
5c58de74
PA
4089 const lookup_name_info &lookup_name_in,
4090 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4091 enum search_domain kind,
fcf23d5b
SM
4092 gdb::function_view<bool (offset_type)> match_callback,
4093 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4094{
4095 lookup_name_info lookup_name_without_params
4096 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4097
4098 /* Build the symbol name component sorted vector, if we haven't
4099 yet. */
fcf23d5b 4100 index.build_name_components (per_objfile);
5c58de74 4101
3f563c84
PA
4102 /* The same symbol may appear more than once in the range though.
4103 E.g., if we're looking for symbols that complete "w", and we have
4104 a symbol named "w1::w2", we'll find the two name components for
4105 that same symbol in the range. To be sure we only call the
4106 callback once per symbol, we first collect the symbol name
4107 indexes that matched in a temporary vector and ignore
4108 duplicates. */
4109 std::vector<offset_type> matches;
3f563c84 4110
3b00ef10
TT
4111 struct name_and_matcher
4112 {
4113 symbol_name_matcher_ftype *matcher;
ecc6c606 4114 const char *name;
3b00ef10
TT
4115
4116 bool operator== (const name_and_matcher &other) const
3f563c84 4117 {
ecc6c606 4118 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4119 }
4120 };
4121
4122 /* A vector holding all the different symbol name matchers, for all
4123 languages. */
4124 std::vector<name_and_matcher> matchers;
4125
4126 for (int i = 0; i < nr_languages; i++)
4127 {
4128 enum language lang_e = (enum language) i;
4129
4130 const language_defn *lang = language_def (lang_e);
4131 symbol_name_matcher_ftype *name_matcher
c9debfb9 4132 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4133
3b00ef10
TT
4134 name_and_matcher key {
4135 name_matcher,
4136 lookup_name_without_params.language_lookup_name (lang_e)
4137 };
4138
4139 /* Don't insert the same comparison routine more than once.
4140 Note that we do this linear walk. This is not a problem in
4141 practice because the number of supported languages is
4142 low. */
4143 if (std::find (matchers.begin (), matchers.end (), key)
4144 != matchers.end ())
9291a0cd 4145 continue;
3b00ef10
TT
4146 matchers.push_back (std::move (key));
4147
4148 auto bounds
4149 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4150 lang_e, per_objfile);
3b00ef10
TT
4151
4152 /* Now for each symbol name in range, check to see if we have a name
4153 match, and if so, call the MATCH_CALLBACK callback. */
4154
4155 for (; bounds.first != bounds.second; ++bounds.first)
4156 {
fcf23d5b
SM
4157 const char *qualified
4158 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4159
4160 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4161 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4162 continue;
9291a0cd 4163
3b00ef10
TT
4164 matches.push_back (bounds.first->idx);
4165 }
3f563c84
PA
4166 }
4167
4168 std::sort (matches.begin (), matches.end ());
4169
4170 /* Finally call the callback, once per match. */
4171 ULONGEST prev = -1;
4172 for (offset_type idx : matches)
4173 {
4174 if (prev != idx)
4175 {
3b00ef10
TT
4176 if (!match_callback (idx))
4177 break;
3f563c84
PA
4178 prev = idx;
4179 }
4180 }
4181
4182 /* Above we use a type wider than idx's for 'prev', since 0 and
4183 (offset_type)-1 are both possible values. */
4184 static_assert (sizeof (prev) > sizeof (offset_type), "");
4185}
4186
c62446b1
PA
4187#if GDB_SELF_TEST
4188
4189namespace selftests { namespace dw2_expand_symtabs_matching {
4190
a3c5fafd
PA
4191/* A mock .gdb_index/.debug_names-like name index table, enough to
4192 exercise dw2_expand_symtabs_matching_symbol, which works with the
4193 mapped_index_base interface. Builds an index from the symbol list
4194 passed as parameter to the constructor. */
4195class mock_mapped_index : public mapped_index_base
c62446b1
PA
4196{
4197public:
a3c5fafd
PA
4198 mock_mapped_index (gdb::array_view<const char *> symbols)
4199 : m_symbol_table (symbols)
c62446b1
PA
4200 {}
4201
a3c5fafd 4202 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4203
a3c5fafd 4204 /* Return the number of names in the symbol table. */
632e107b 4205 size_t symbol_name_count () const override
c62446b1 4206 {
a3c5fafd 4207 return m_symbol_table.size ();
c62446b1
PA
4208 }
4209
a3c5fafd 4210 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4211 const char *symbol_name_at
4212 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4213 {
4214 return m_symbol_table[idx];
4215 }
c62446b1 4216
a3c5fafd
PA
4217private:
4218 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4219};
4220
4221/* Convenience function that converts a NULL pointer to a "<null>"
4222 string, to pass to print routines. */
4223
4224static const char *
4225string_or_null (const char *str)
4226{
4227 return str != NULL ? str : "<null>";
4228}
4229
4230/* Check if a lookup_name_info built from
4231 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4232 index. EXPECTED_LIST is the list of expected matches, in expected
4233 matching order. If no match expected, then an empty list is
4234 specified. Returns true on success. On failure prints a warning
4235 indicating the file:line that failed, and returns false. */
4236
4237static bool
4238check_match (const char *file, int line,
4239 mock_mapped_index &mock_index,
4240 const char *name, symbol_name_match_type match_type,
4241 bool completion_mode,
fcf23d5b
SM
4242 std::initializer_list<const char *> expected_list,
4243 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4244{
4245 lookup_name_info lookup_name (name, match_type, completion_mode);
4246
4247 bool matched = true;
4248
4249 auto mismatch = [&] (const char *expected_str,
4250 const char *got)
4251 {
4252 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4253 "expected=\"%s\", got=\"%s\"\n"),
4254 file, line,
4255 (match_type == symbol_name_match_type::FULL
4256 ? "FULL" : "WILD"),
4257 name, string_or_null (expected_str), string_or_null (got));
4258 matched = false;
4259 };
4260
4261 auto expected_it = expected_list.begin ();
4262 auto expected_end = expected_list.end ();
4263
a3c5fafd 4264 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4265 NULL, ALL_DOMAIN,
4266 [&] (offset_type idx)
4267 {
fcf23d5b 4268 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4269 const char *expected_str
4270 = expected_it == expected_end ? NULL : *expected_it++;
4271
4272 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4273 mismatch (expected_str, matched_name);
3b00ef10 4274 return true;
fcf23d5b 4275 }, per_objfile);
c62446b1
PA
4276
4277 const char *expected_str
4278 = expected_it == expected_end ? NULL : *expected_it++;
4279 if (expected_str != NULL)
4280 mismatch (expected_str, NULL);
4281
4282 return matched;
4283}
4284
4285/* The symbols added to the mock mapped_index for testing (in
4286 canonical form). */
4287static const char *test_symbols[] = {
4288 "function",
4289 "std::bar",
4290 "std::zfunction",
4291 "std::zfunction2",
4292 "w1::w2",
4293 "ns::foo<char*>",
4294 "ns::foo<int>",
4295 "ns::foo<long>",
a20714ff
PA
4296 "ns2::tmpl<int>::foo2",
4297 "(anonymous namespace)::A::B::C",
c62446b1 4298
e1ef7d7a
PA
4299 /* These are used to check that the increment-last-char in the
4300 matching algorithm for completion doesn't match "t1_fund" when
4301 completing "t1_func". */
4302 "t1_func",
4303 "t1_func1",
4304 "t1_fund",
4305 "t1_fund1",
4306
4307 /* A UTF-8 name with multi-byte sequences to make sure that
4308 cp-name-parser understands this as a single identifier ("função"
4309 is "function" in PT). */
4310 u8"u8função",
4311
4312 /* \377 (0xff) is Latin1 'ÿ'. */
4313 "yfunc\377",
4314
4315 /* \377 (0xff) is Latin1 'ÿ'. */
4316 "\377",
4317 "\377\377123",
4318
c62446b1
PA
4319 /* A name with all sorts of complications. Starts with "z" to make
4320 it easier for the completion tests below. */
4321#define Z_SYM_NAME \
4322 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4323 "::tuple<(anonymous namespace)::ui*, " \
4324 "std::default_delete<(anonymous namespace)::ui>, void>"
4325
4326 Z_SYM_NAME
4327};
4328
a3c5fafd
PA
4329/* Returns true if the mapped_index_base::find_name_component_bounds
4330 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4331 in completion mode. */
5c58de74
PA
4332
4333static bool
a3c5fafd 4334check_find_bounds_finds (mapped_index_base &index,
5c58de74 4335 const char *search_name,
fcf23d5b
SM
4336 gdb::array_view<const char *> expected_syms,
4337 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4338{
4339 lookup_name_info lookup_name (search_name,
4340 symbol_name_match_type::FULL, true);
4341
3b00ef10 4342 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4343 language_cplus,
4344 per_objfile);
5c58de74
PA
4345
4346 size_t distance = std::distance (bounds.first, bounds.second);
4347 if (distance != expected_syms.size ())
4348 return false;
4349
4350 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4351 {
4352 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4353 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4354 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4355 return false;
4356 }
4357
4358 return true;
4359}
4360
4361/* Test the lower-level mapped_index::find_name_component_bounds
4362 method. */
4363
c62446b1 4364static void
5c58de74
PA
4365test_mapped_index_find_name_component_bounds ()
4366{
4367 mock_mapped_index mock_index (test_symbols);
4368
fcf23d5b 4369 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4370
4371 /* Test the lower-level mapped_index::find_name_component_bounds
4372 method in completion mode. */
4373 {
4374 static const char *expected_syms[] = {
4375 "t1_func",
4376 "t1_func1",
5c58de74
PA
4377 };
4378
fcf23d5b
SM
4379 SELF_CHECK (check_find_bounds_finds
4380 (mock_index, "t1_func", expected_syms,
4381 NULL /* per_objfile */));
5c58de74
PA
4382 }
4383
4384 /* Check that the increment-last-char in the name matching algorithm
4385 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4386 {
4387 static const char *expected_syms1[] = {
4388 "\377",
4389 "\377\377123",
4390 };
fcf23d5b
SM
4391 SELF_CHECK (check_find_bounds_finds
4392 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4393
4394 static const char *expected_syms2[] = {
4395 "\377\377123",
4396 };
fcf23d5b
SM
4397 SELF_CHECK (check_find_bounds_finds
4398 (mock_index, "\377\377", expected_syms2,
4399 NULL /* per_objfile */));
5c58de74
PA
4400 }
4401}
4402
4403/* Test dw2_expand_symtabs_matching_symbol. */
4404
4405static void
4406test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4407{
4408 mock_mapped_index mock_index (test_symbols);
4409
4410 /* We let all tests run until the end even if some fails, for debug
4411 convenience. */
4412 bool any_mismatch = false;
4413
4414 /* Create the expected symbols list (an initializer_list). Needed
4415 because lists have commas, and we need to pass them to CHECK,
4416 which is a macro. */
4417#define EXPECT(...) { __VA_ARGS__ }
4418
4419 /* Wrapper for check_match that passes down the current
4420 __FILE__/__LINE__. */
4421#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4422 any_mismatch |= !check_match (__FILE__, __LINE__, \
4423 mock_index, \
4424 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4425 EXPECTED_LIST, NULL)
c62446b1
PA
4426
4427 /* Identity checks. */
4428 for (const char *sym : test_symbols)
4429 {
4430 /* Should be able to match all existing symbols. */
4431 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4432 EXPECT (sym));
4433
4434 /* Should be able to match all existing symbols with
4435 parameters. */
4436 std::string with_params = std::string (sym) + "(int)";
4437 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4438 EXPECT (sym));
4439
4440 /* Should be able to match all existing symbols with
4441 parameters and qualifiers. */
4442 with_params = std::string (sym) + " ( int ) const";
4443 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4444 EXPECT (sym));
4445
4446 /* This should really find sym, but cp-name-parser.y doesn't
4447 know about lvalue/rvalue qualifiers yet. */
4448 with_params = std::string (sym) + " ( int ) &&";
4449 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4450 {});
4451 }
4452
e1ef7d7a
PA
4453 /* Check that the name matching algorithm for completion doesn't get
4454 confused with Latin1 'ÿ' / 0xff. */
4455 {
4456 static const char str[] = "\377";
4457 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4458 EXPECT ("\377", "\377\377123"));
4459 }
4460
4461 /* Check that the increment-last-char in the matching algorithm for
4462 completion doesn't match "t1_fund" when completing "t1_func". */
4463 {
4464 static const char str[] = "t1_func";
4465 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4466 EXPECT ("t1_func", "t1_func1"));
4467 }
4468
c62446b1
PA
4469 /* Check that completion mode works at each prefix of the expected
4470 symbol name. */
4471 {
4472 static const char str[] = "function(int)";
4473 size_t len = strlen (str);
4474 std::string lookup;
4475
4476 for (size_t i = 1; i < len; i++)
4477 {
4478 lookup.assign (str, i);
4479 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4480 EXPECT ("function"));
4481 }
4482 }
4483
4484 /* While "w" is a prefix of both components, the match function
4485 should still only be called once. */
4486 {
4487 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4488 EXPECT ("w1::w2"));
a20714ff
PA
4489 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4490 EXPECT ("w1::w2"));
c62446b1
PA
4491 }
4492
4493 /* Same, with a "complicated" symbol. */
4494 {
4495 static const char str[] = Z_SYM_NAME;
4496 size_t len = strlen (str);
4497 std::string lookup;
4498
4499 for (size_t i = 1; i < len; i++)
4500 {
4501 lookup.assign (str, i);
4502 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4503 EXPECT (Z_SYM_NAME));
4504 }
4505 }
4506
4507 /* In FULL mode, an incomplete symbol doesn't match. */
4508 {
4509 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4510 {});
4511 }
4512
4513 /* A complete symbol with parameters matches any overload, since the
4514 index has no overload info. */
4515 {
4516 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4517 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4518 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4519 EXPECT ("std::zfunction", "std::zfunction2"));
4520 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4521 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4522 }
4523
4524 /* Check that whitespace is ignored appropriately. A symbol with a
4525 template argument list. */
4526 {
4527 static const char expected[] = "ns::foo<int>";
4528 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4529 EXPECT (expected));
a20714ff
PA
4530 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4531 EXPECT (expected));
c62446b1
PA
4532 }
4533
4534 /* Check that whitespace is ignored appropriately. A symbol with a
4535 template argument list that includes a pointer. */
4536 {
4537 static const char expected[] = "ns::foo<char*>";
4538 /* Try both completion and non-completion modes. */
4539 static const bool completion_mode[2] = {false, true};
4540 for (size_t i = 0; i < 2; i++)
4541 {
4542 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4543 completion_mode[i], EXPECT (expected));
a20714ff
PA
4544 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4545 completion_mode[i], EXPECT (expected));
c62446b1
PA
4546
4547 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4548 completion_mode[i], EXPECT (expected));
a20714ff
PA
4549 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4550 completion_mode[i], EXPECT (expected));
c62446b1
PA
4551 }
4552 }
4553
4554 {
4555 /* Check method qualifiers are ignored. */
4556 static const char expected[] = "ns::foo<char*>";
4557 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4558 symbol_name_match_type::FULL, true, EXPECT (expected));
4559 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4560 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4561 CHECK_MATCH ("foo < char * > ( int ) const",
4562 symbol_name_match_type::WILD, true, EXPECT (expected));
4563 CHECK_MATCH ("foo < char * > ( int ) &&",
4564 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4565 }
4566
4567 /* Test lookup names that don't match anything. */
4568 {
a20714ff
PA
4569 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4570 {});
4571
c62446b1
PA
4572 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4573 {});
4574 }
4575
a20714ff
PA
4576 /* Some wild matching tests, exercising "(anonymous namespace)",
4577 which should not be confused with a parameter list. */
4578 {
4579 static const char *syms[] = {
4580 "A::B::C",
4581 "B::C",
4582 "C",
4583 "A :: B :: C ( int )",
4584 "B :: C ( int )",
4585 "C ( int )",
4586 };
4587
4588 for (const char *s : syms)
4589 {
4590 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4591 EXPECT ("(anonymous namespace)::A::B::C"));
4592 }
4593 }
4594
4595 {
4596 static const char expected[] = "ns2::tmpl<int>::foo2";
4597 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4598 EXPECT (expected));
4599 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4600 EXPECT (expected));
4601 }
4602
c62446b1
PA
4603 SELF_CHECK (!any_mismatch);
4604
4605#undef EXPECT
4606#undef CHECK_MATCH
4607}
4608
5c58de74
PA
4609static void
4610run_test ()
4611{
4612 test_mapped_index_find_name_component_bounds ();
4613 test_dw2_expand_symtabs_matching_symbol ();
4614}
4615
c62446b1
PA
4616}} // namespace selftests::dw2_expand_symtabs_matching
4617
4618#endif /* GDB_SELF_TEST */
4619
4b514bc8
JK
4620/* If FILE_MATCHER is NULL or if PER_CU has
4621 dwarf2_per_cu_quick_data::MARK set (see
4622 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4623 EXPANSION_NOTIFY on it. */
4624
4625static void
4626dw2_expand_symtabs_matching_one
97a1449a
SM
4627 (dwarf2_per_cu_data *per_cu,
4628 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4629 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4630 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4631{
4632 if (file_matcher == NULL || per_cu->v.quick->mark)
4633 {
af758d11 4634 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4635
97a1449a
SM
4636 compunit_symtab *symtab
4637 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4638 gdb_assert (symtab != nullptr);
4b514bc8 4639
af758d11
SM
4640 if (expansion_notify != NULL && symtab_was_null)
4641 expansion_notify (symtab);
4b514bc8
JK
4642 }
4643}
4644
3f563c84
PA
4645/* Helper for dw2_expand_matching symtabs. Called on each symbol
4646 matched, to expand corresponding CUs that were marked. IDX is the
4647 index of the symbol name that matched. */
4648
4649static void
4650dw2_expand_marked_cus
976ca316 4651 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4652 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4653 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4654 search_domain kind)
4655{
3f563c84
PA
4656 offset_type *vec, vec_len, vec_idx;
4657 bool global_seen = false;
976ca316 4658 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4659
61920122 4660 vec = (offset_type *) (index.constant_pool
f00a2de2 4661 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4662 vec_len = MAYBE_SWAP (vec[0]);
4663 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4664 {
61920122
PA
4665 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4666 /* This value is only valid for index versions >= 7. */
4667 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4668 gdb_index_symbol_kind symbol_kind =
4669 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4670 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4671 /* Only check the symbol attributes if they're present.
4672 Indices prior to version 7 don't record them,
4673 and indices >= 7 may elide them for certain symbols
4674 (gold does this). */
4675 int attrs_valid =
4676 (index.version >= 7
4677 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4678
4679 /* Work around gold/15646. */
f030440d
TV
4680 if (attrs_valid
4681 && !is_static
4682 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4683 {
f030440d 4684 if (global_seen)
61920122 4685 continue;
f030440d
TV
4686
4687 global_seen = true;
61920122 4688 }
3190f0c6 4689
61920122
PA
4690 /* Only check the symbol's kind if it has one. */
4691 if (attrs_valid)
4692 {
4693 switch (kind)
8943b874 4694 {
61920122
PA
4695 case VARIABLES_DOMAIN:
4696 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4697 continue;
4698 break;
4699 case FUNCTIONS_DOMAIN:
4700 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4701 continue;
61920122
PA
4702 break;
4703 case TYPES_DOMAIN:
4704 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4705 continue;
4706 break;
59c35742
AB
4707 case MODULES_DOMAIN:
4708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4709 continue;
4710 break;
61920122
PA
4711 default:
4712 break;
8943b874 4713 }
61920122 4714 }
8943b874 4715
61920122 4716 /* Don't crash on bad data. */
976ca316
SM
4717 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4718 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4719 {
b98664d3 4720 complaint (_(".gdb_index entry has bad CU index"
976ca316 4721 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4722 continue;
4723 }
4724
976ca316
SM
4725 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4726 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4727 expansion_notify);
61920122
PA
4728 }
4729}
4730
4b514bc8
JK
4731/* If FILE_MATCHER is non-NULL, set all the
4732 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4733 that match FILE_MATCHER. */
4734
61920122 4735static void
4b514bc8 4736dw_expand_symtabs_matching_file_matcher
976ca316 4737 (dwarf2_per_objfile *per_objfile,
ed2dc618 4738 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4739{
4b514bc8 4740 if (file_matcher == NULL)
61920122
PA
4741 return;
4742
4b514bc8
JK
4743 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4744 htab_eq_pointer,
4745 NULL, xcalloc, xfree));
4746 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4747 htab_eq_pointer,
4748 NULL, xcalloc, xfree));
61920122 4749
4b514bc8
JK
4750 /* The rule is CUs specify all the files, including those used by
4751 any TU, so there's no need to scan TUs here. */
61920122 4752
976ca316 4753 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4754 {
927aa2e7
JK
4755 QUIT;
4756
4757 per_cu->v.quick->mark = 0;
4758
4759 /* We only need to look at symtabs not already expanded. */
976ca316 4760 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4761 continue;
4762
976ca316 4763 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4764 if (file_data == NULL)
4765 continue;
4766
4767 if (htab_find (visited_not_found.get (), file_data) != NULL)
4768 continue;
4769 else if (htab_find (visited_found.get (), file_data) != NULL)
4770 {
4771 per_cu->v.quick->mark = 1;
4772 continue;
4773 }
4774
b76e467d 4775 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4776 {
4777 const char *this_real_name;
4778
4779 if (file_matcher (file_data->file_names[j], false))
4780 {
4781 per_cu->v.quick->mark = 1;
4782 break;
4783 }
4784
4785 /* Before we invoke realpath, which can get expensive when many
4786 files are involved, do a quick comparison of the basenames. */
4787 if (!basenames_may_differ
4788 && !file_matcher (lbasename (file_data->file_names[j]),
4789 true))
4790 continue;
4791
976ca316 4792 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4793 if (file_matcher (this_real_name, false))
4794 {
4795 per_cu->v.quick->mark = 1;
4796 break;
4797 }
4798 }
4799
b76e467d
SM
4800 void **slot = htab_find_slot (per_cu->v.quick->mark
4801 ? visited_found.get ()
4802 : visited_not_found.get (),
4803 file_data, INSERT);
927aa2e7
JK
4804 *slot = file_data;
4805 }
4806}
4807
4808static void
4809dw2_expand_symtabs_matching
4810 (struct objfile *objfile,
4811 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4812 const lookup_name_info *lookup_name,
927aa2e7
JK
4813 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4814 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4815 enum search_domain kind)
4816{
976ca316 4817 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4818
4819 /* index_table is NULL if OBJF_READNOW. */
976ca316 4820 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4821 return;
4822
976ca316 4823 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4824
c1a66c06
TV
4825 if (symbol_matcher == NULL && lookup_name == NULL)
4826 {
976ca316 4827 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4828 {
4829 QUIT;
4830
976ca316 4831 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4832 file_matcher, expansion_notify);
c1a66c06
TV
4833 }
4834 return;
4835 }
4836
976ca316 4837 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4838
c1a66c06 4839 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4840 symbol_matcher,
4841 kind, [&] (offset_type idx)
4842 {
976ca316
SM
4843 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4844 kind);
3b00ef10 4845 return true;
976ca316 4846 }, per_objfile);
927aa2e7
JK
4847}
4848
4849/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4850 symtab. */
4851
4852static struct compunit_symtab *
4853recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4854 CORE_ADDR pc)
4855{
4856 int i;
4857
4858 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4859 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4860 return cust;
4861
4862 if (cust->includes == NULL)
4863 return NULL;
4864
4865 for (i = 0; cust->includes[i]; ++i)
4866 {
4867 struct compunit_symtab *s = cust->includes[i];
4868
4869 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4870 if (s != NULL)
4871 return s;
4872 }
4873
4874 return NULL;
4875}
4876
4877static struct compunit_symtab *
4878dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4879 struct bound_minimal_symbol msymbol,
4880 CORE_ADDR pc,
4881 struct obj_section *section,
4882 int warn_if_readin)
4883{
4884 struct dwarf2_per_cu_data *data;
4885 struct compunit_symtab *result;
4886
d320c2b5 4887 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4888 return NULL;
4889
b3b3bada 4890 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4891 data = (struct dwarf2_per_cu_data *) addrmap_find
4892 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4893 if (!data)
4894 return NULL;
4895
af758d11
SM
4896 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4897 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4898 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4899 paddress (objfile->arch (), pc));
927aa2e7 4900
97a1449a
SM
4901 result = recursively_find_pc_sect_compunit_symtab
4902 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4903
927aa2e7
JK
4904 gdb_assert (result != NULL);
4905 return result;
4906}
4907
4908static void
4909dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4910 void *data, int need_fullname)
4911{
976ca316 4912 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4913
976ca316 4914 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4915 {
976ca316 4916 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4917
4918 htab_up visited (htab_create_alloc (10,
4919 htab_hash_pointer, htab_eq_pointer,
4920 NULL, xcalloc, xfree));
4921
4922 /* The rule is CUs specify all the files, including those used
4923 by any TU, so there's no need to scan TUs here. We can
4924 ignore file names coming from already-expanded CUs. */
4925
976ca316 4926 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4927 {
976ca316 4928 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4929 {
4930 void **slot = htab_find_slot (visited.get (),
4931 per_cu->v.quick->file_names,
4932 INSERT);
4933
4934 *slot = per_cu->v.quick->file_names;
4935 }
4936 }
4937
976ca316 4938 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4939 {
927aa2e7 4940 /* We only need to look at symtabs not already expanded. */
976ca316 4941 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4942 continue;
4943
ab432490 4944 quick_file_names *file_data
976ca316 4945 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4946 if (file_data == NULL)
4947 continue;
4948
b76e467d 4949 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4950 if (*slot)
4951 {
4952 /* Already visited. */
4953 continue;
4954 }
4955 *slot = file_data;
4956
4957 for (int j = 0; j < file_data->num_file_names; ++j)
4958 {
4959 const char *filename = file_data->file_names[j];
976ca316 4960 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4961 }
4962 }
4963 }
4964
976ca316 4965 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4966 {
4967 gdb::unique_xmalloc_ptr<char> this_real_name;
4968
4969 if (need_fullname)
4970 this_real_name = gdb_realpath (filename);
4971 (*fun) (filename, this_real_name.get (), data);
4972 });
4973}
4974
4975static int
4976dw2_has_symbols (struct objfile *objfile)
4977{
4978 return 1;
4979}
4980
4981const struct quick_symbol_functions dwarf2_gdb_index_functions =
4982{
4983 dw2_has_symbols,
4984 dw2_find_last_source_symtab,
4985 dw2_forget_cached_source_info,
4986 dw2_map_symtabs_matching_filename,
4987 dw2_lookup_symbol,
d3214198 4988 NULL,
927aa2e7
JK
4989 dw2_print_stats,
4990 dw2_dump,
927aa2e7
JK
4991 dw2_expand_symtabs_for_function,
4992 dw2_expand_all_symtabs,
4993 dw2_expand_symtabs_with_fullname,
4994 dw2_map_matching_symbols,
4995 dw2_expand_symtabs_matching,
4996 dw2_find_pc_sect_compunit_symtab,
4997 NULL,
4998 dw2_map_symbol_filenames
4999};
5000
5001/* DWARF-5 debug_names reader. */
5002
5003/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5004static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5005
5006/* A helper function that reads the .debug_names section in SECTION
5007 and fills in MAP. FILENAME is the name of the file containing the
5008 section; it is used for error reporting.
5009
5010 Returns true if all went well, false otherwise. */
5011
5012static bool
5013read_debug_names_from_section (struct objfile *objfile,
5014 const char *filename,
5015 struct dwarf2_section_info *section,
5016 mapped_debug_names &map)
5017{
96b79293 5018 if (section->empty ())
927aa2e7
JK
5019 return false;
5020
5021 /* Older elfutils strip versions could keep the section in the main
5022 executable while splitting it for the separate debug info file. */
96b79293 5023 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5024 return false;
5025
96b79293 5026 section->read (objfile);
927aa2e7 5027
08feed99 5028 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5029
5030 const gdb_byte *addr = section->buffer;
5031
96b79293 5032 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5033
5034 unsigned int bytes_read;
5035 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5036 addr += bytes_read;
5037
5038 map.dwarf5_is_dwarf64 = bytes_read != 4;
5039 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5040 if (bytes_read + length != section->size)
5041 {
5042 /* There may be multiple per-CU indices. */
5043 warning (_("Section .debug_names in %s length %s does not match "
5044 "section length %s, ignoring .debug_names."),
5045 filename, plongest (bytes_read + length),
5046 pulongest (section->size));
5047 return false;
5048 }
5049
5050 /* The version number. */
5051 uint16_t version = read_2_bytes (abfd, addr);
5052 addr += 2;
5053 if (version != 5)
5054 {
5055 warning (_("Section .debug_names in %s has unsupported version %d, "
5056 "ignoring .debug_names."),
5057 filename, version);
5058 return false;
5059 }
5060
5061 /* Padding. */
5062 uint16_t padding = read_2_bytes (abfd, addr);
5063 addr += 2;
5064 if (padding != 0)
5065 {
5066 warning (_("Section .debug_names in %s has unsupported padding %d, "
5067 "ignoring .debug_names."),
5068 filename, padding);
5069 return false;
5070 }
5071
5072 /* comp_unit_count - The number of CUs in the CU list. */
5073 map.cu_count = read_4_bytes (abfd, addr);
5074 addr += 4;
5075
5076 /* local_type_unit_count - The number of TUs in the local TU
5077 list. */
5078 map.tu_count = read_4_bytes (abfd, addr);
5079 addr += 4;
5080
5081 /* foreign_type_unit_count - The number of TUs in the foreign TU
5082 list. */
5083 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5084 addr += 4;
5085 if (foreign_tu_count != 0)
5086 {
5087 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5088 "ignoring .debug_names."),
5089 filename, static_cast<unsigned long> (foreign_tu_count));
5090 return false;
5091 }
5092
5093 /* bucket_count - The number of hash buckets in the hash lookup
5094 table. */
5095 map.bucket_count = read_4_bytes (abfd, addr);
5096 addr += 4;
5097
5098 /* name_count - The number of unique names in the index. */
5099 map.name_count = read_4_bytes (abfd, addr);
5100 addr += 4;
5101
5102 /* abbrev_table_size - The size in bytes of the abbreviations
5103 table. */
5104 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5105 addr += 4;
5106
5107 /* augmentation_string_size - The size in bytes of the augmentation
5108 string. This value is rounded up to a multiple of 4. */
5109 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5110 addr += 4;
5111 map.augmentation_is_gdb = ((augmentation_string_size
5112 == sizeof (dwarf5_augmentation))
5113 && memcmp (addr, dwarf5_augmentation,
5114 sizeof (dwarf5_augmentation)) == 0);
5115 augmentation_string_size += (-augmentation_string_size) & 3;
5116 addr += augmentation_string_size;
5117
5118 /* List of CUs */
5119 map.cu_table_reordered = addr;
5120 addr += map.cu_count * map.offset_size;
5121
5122 /* List of Local TUs */
5123 map.tu_table_reordered = addr;
5124 addr += map.tu_count * map.offset_size;
5125
5126 /* Hash Lookup Table */
5127 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5128 addr += map.bucket_count * 4;
5129 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5130 addr += map.name_count * 4;
5131
5132 /* Name Table */
5133 map.name_table_string_offs_reordered = addr;
5134 addr += map.name_count * map.offset_size;
5135 map.name_table_entry_offs_reordered = addr;
5136 addr += map.name_count * map.offset_size;
5137
5138 const gdb_byte *abbrev_table_start = addr;
5139 for (;;)
5140 {
927aa2e7
JK
5141 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5142 addr += bytes_read;
5143 if (index_num == 0)
5144 break;
5145
5146 const auto insertpair
5147 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5148 if (!insertpair.second)
5149 {
5150 warning (_("Section .debug_names in %s has duplicate index %s, "
5151 "ignoring .debug_names."),
5152 filename, pulongest (index_num));
5153 return false;
5154 }
5155 mapped_debug_names::index_val &indexval = insertpair.first->second;
5156 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5157 addr += bytes_read;
5158
5159 for (;;)
5160 {
5161 mapped_debug_names::index_val::attr attr;
5162 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5163 addr += bytes_read;
5164 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5165 addr += bytes_read;
5166 if (attr.form == DW_FORM_implicit_const)
5167 {
5168 attr.implicit_const = read_signed_leb128 (abfd, addr,
5169 &bytes_read);
5170 addr += bytes_read;
5171 }
5172 if (attr.dw_idx == 0 && attr.form == 0)
5173 break;
5174 indexval.attr_vec.push_back (std::move (attr));
5175 }
5176 }
5177 if (addr != abbrev_table_start + abbrev_table_size)
5178 {
5179 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5180 "of size %s vs. written as %u, ignoring .debug_names."),
5181 filename, plongest (addr - abbrev_table_start),
5182 abbrev_table_size);
927aa2e7
JK
5183 return false;
5184 }
5185 map.entry_pool = addr;
5186
5187 return true;
5188}
5189
5190/* A helper for create_cus_from_debug_names that handles the MAP's CU
5191 list. */
5192
5193static void
168c9250 5194create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5195 const mapped_debug_names &map,
5196 dwarf2_section_info &section,
b76e467d 5197 bool is_dwz)
927aa2e7 5198{
3ee6bb11
TV
5199 if (!map.augmentation_is_gdb)
5200 {
5201 for (uint32_t i = 0; i < map.cu_count; ++i)
5202 {
5203 sect_offset sect_off
5204 = (sect_offset) (extract_unsigned_integer
5205 (map.cu_table_reordered + i * map.offset_size,
5206 map.offset_size,
5207 map.dwarf5_byte_order));
5208 /* We don't know the length of the CU, because the CU list in a
5209 .debug_names index can be incomplete, so we can't use the start of
5210 the next CU as end of this CU. We create the CUs here with length 0,
5211 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5212 dwarf2_per_cu_data *per_cu
168c9250
SM
5213 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5214 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5215 }
5216 }
5217
927aa2e7
JK
5218 sect_offset sect_off_prev;
5219 for (uint32_t i = 0; i <= map.cu_count; ++i)
5220 {
5221 sect_offset sect_off_next;
5222 if (i < map.cu_count)
5223 {
5224 sect_off_next
5225 = (sect_offset) (extract_unsigned_integer
5226 (map.cu_table_reordered + i * map.offset_size,
5227 map.offset_size,
5228 map.dwarf5_byte_order));
5229 }
5230 else
5231 sect_off_next = (sect_offset) section.size;
5232 if (i >= 1)
5233 {
5234 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5235 dwarf2_per_cu_data *per_cu
168c9250 5236 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5237 sect_off_prev, length);
168c9250 5238 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5239 }
5240 sect_off_prev = sect_off_next;
5241 }
5242}
5243
5244/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5245 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5246
5247static void
168c9250 5248create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5249 const mapped_debug_names &map,
5250 const mapped_debug_names &dwz_map)
5251{
168c9250
SM
5252 gdb_assert (per_bfd->all_comp_units.empty ());
5253 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5254
168c9250 5255 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5256 false /* is_dwz */);
927aa2e7
JK
5257
5258 if (dwz_map.cu_count == 0)
5259 return;
5260
168c9250
SM
5261 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5262 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5263 true /* is_dwz */);
927aa2e7
JK
5264}
5265
5266/* Read .debug_names. If everything went ok, initialize the "quick"
5267 elements of all the CUs and return true. Otherwise, return false. */
5268
5269static bool
976ca316 5270dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5271{
fcf23d5b
SM
5272 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5273 mapped_debug_names dwz_map;
976ca316 5274 struct objfile *objfile = per_objfile->objfile;
f8c41851 5275 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5276
5277 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5278 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5279 return false;
5280
5281 /* Don't use the index if it's empty. */
22ca247e 5282 if (map->name_count == 0)
927aa2e7
JK
5283 return false;
5284
5285 /* If there is a .dwz file, read it so we can get its CU list as
5286 well. */
f8c41851 5287 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5288 if (dwz != NULL)
5289 {
5290 if (!read_debug_names_from_section (objfile,
00f93c44 5291 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5292 &dwz->debug_names, dwz_map))
5293 {
5294 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5295 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5296 return false;
5297 }
5298 }
5299
f8c41851 5300 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5301
22ca247e 5302 if (map->tu_count != 0)
927aa2e7
JK
5303 {
5304 /* We can only handle a single .debug_types when we have an
5305 index. */
f8c41851 5306 if (per_bfd->types.size () != 1)
927aa2e7
JK
5307 return false;
5308
f8c41851 5309 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5310
5311 create_signatured_type_table_from_debug_names
f8c41851 5312 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5313 }
5314
f8c41851 5315 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5316
f8c41851
SM
5317 per_bfd->debug_names_table = std::move (map);
5318 per_bfd->using_index = 1;
5319 per_bfd->quick_file_names_table =
976ca316 5320 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5321
f8c41851
SM
5322 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5323 objfiles using the same BFD. */
5324 gdb_assert (per_bfd->partial_symtabs == nullptr);
5325 per_bfd->partial_symtabs = objfile->partial_symtabs;
5326
927aa2e7
JK
5327 return true;
5328}
5329
927aa2e7
JK
5330/* Type used to manage iterating over all CUs looking for a symbol for
5331 .debug_names. */
5332
5333class dw2_debug_names_iterator
5334{
5335public:
927aa2e7 5336 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5337 gdb::optional<block_enum> block_index,
5338 domain_enum domain,
fcf23d5b 5339 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5340 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5341 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5342 m_per_objfile (per_objfile)
927aa2e7
JK
5343 {}
5344
5345 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5346 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5347 : m_map (map),
5348 m_search (search),
fcf23d5b
SM
5349 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5350 m_per_objfile (per_objfile)
927aa2e7
JK
5351 {}
5352
3b00ef10
TT
5353 dw2_debug_names_iterator (const mapped_debug_names &map,
5354 block_enum block_index, domain_enum domain,
fcf23d5b 5355 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5356 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5357 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5358 m_per_objfile (per_objfile)
3b00ef10
TT
5359 {}
5360
927aa2e7
JK
5361 /* Return the next matching CU or NULL if there are no more. */
5362 dwarf2_per_cu_data *next ();
5363
5364private:
5365 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5366 const char *name,
5367 dwarf2_per_objfile *per_objfile);
927aa2e7 5368 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5369 uint32_t namei,
5370 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5371
5372 /* The internalized form of .debug_names. */
5373 const mapped_debug_names &m_map;
5374
2b79f376
SM
5375 /* If set, only look for symbols that match that block. Valid values are
5376 GLOBAL_BLOCK and STATIC_BLOCK. */
5377 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5378
5379 /* The kind of symbol we're looking for. */
5380 const domain_enum m_domain = UNDEF_DOMAIN;
5381 const search_domain m_search = ALL_DOMAIN;
5382
5383 /* The list of CUs from the index entry of the symbol, or NULL if
5384 not found. */
5385 const gdb_byte *m_addr;
fcf23d5b
SM
5386
5387 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5388};
5389
5390const char *
fcf23d5b 5391mapped_debug_names::namei_to_name
976ca316 5392 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5393{
5394 const ULONGEST namei_string_offs
5395 = extract_unsigned_integer ((name_table_string_offs_reordered
5396 + namei * offset_size),
5397 offset_size,
5398 dwarf5_byte_order);
976ca316 5399 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5400}
5401
5402/* Find a slot in .debug_names for the object named NAME. If NAME is
5403 found, return pointer to its pool data. If NAME cannot be found,
5404 return NULL. */
5405
5406const gdb_byte *
5407dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5408 (const mapped_debug_names &map, const char *name,
5409 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5410{
5411 int (*cmp) (const char *, const char *);
5412
54ee4252 5413 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5414 if (current_language->la_language == language_cplus
5415 || current_language->la_language == language_fortran
5416 || current_language->la_language == language_d)
5417 {
5418 /* NAME is already canonical. Drop any qualifiers as
5419 .debug_names does not contain any. */
5420
5421 if (strchr (name, '(') != NULL)
5422 {
54ee4252 5423 without_params = cp_remove_params (name);
927aa2e7 5424 if (without_params != NULL)
54ee4252 5425 name = without_params.get ();
927aa2e7
JK
5426 }
5427 }
5428
5429 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5430
5431 const uint32_t full_hash = dwarf5_djb_hash (name);
5432 uint32_t namei
5433 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5434 (map.bucket_table_reordered
5435 + (full_hash % map.bucket_count)), 4,
5436 map.dwarf5_byte_order);
5437 if (namei == 0)
5438 return NULL;
5439 --namei;
5440 if (namei >= map.name_count)
5441 {
b98664d3 5442 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5443 "[in module %s]"),
5444 namei, map.name_count,
fcf23d5b 5445 objfile_name (per_objfile->objfile));
927aa2e7
JK
5446 return NULL;
5447 }
5448
5449 for (;;)
5450 {
5451 const uint32_t namei_full_hash
5452 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5453 (map.hash_table_reordered + namei), 4,
5454 map.dwarf5_byte_order);
5455 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5456 return NULL;
5457
5458 if (full_hash == namei_full_hash)
5459 {
fcf23d5b 5460 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5461
5462#if 0 /* An expensive sanity check. */
5463 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5464 {
b98664d3 5465 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5466 "[in module %s]"),
5467 namei, objfile_name (dwarf2_per_objfile->objfile));
5468 return NULL;
5469 }
5470#endif
5471
5472 if (cmp (namei_string, name) == 0)
5473 {
5474 const ULONGEST namei_entry_offs
5475 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5476 + namei * map.offset_size),
5477 map.offset_size, map.dwarf5_byte_order);
5478 return map.entry_pool + namei_entry_offs;
5479 }
5480 }
5481
5482 ++namei;
5483 if (namei >= map.name_count)
5484 return NULL;
5485 }
5486}
5487
5488const gdb_byte *
5489dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5490 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5491{
5492 if (namei >= map.name_count)
5493 {
b98664d3 5494 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5495 "[in module %s]"),
5496 namei, map.name_count,
fcf23d5b 5497 objfile_name (per_objfile->objfile));
927aa2e7
JK
5498 return NULL;
5499 }
5500
5501 const ULONGEST namei_entry_offs
5502 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5503 + namei * map.offset_size),
5504 map.offset_size, map.dwarf5_byte_order);
5505 return map.entry_pool + namei_entry_offs;
5506}
5507
5508/* See dw2_debug_names_iterator. */
5509
5510dwarf2_per_cu_data *
5511dw2_debug_names_iterator::next ()
5512{
5513 if (m_addr == NULL)
5514 return NULL;
5515
fcf23d5b
SM
5516 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5517 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5518 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5519
5520 again:
5521
5522 unsigned int bytes_read;
5523 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5524 m_addr += bytes_read;
5525 if (abbrev == 0)
5526 return NULL;
5527
5528 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5529 if (indexval_it == m_map.abbrev_map.cend ())
5530 {
b98664d3 5531 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5532 "[in module %s]"),
ed2dc618 5533 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5534 return NULL;
5535 }
5536 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5537 enum class symbol_linkage {
5538 unknown,
5539 static_,
5540 extern_,
23c13d42 5541 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5542 dwarf2_per_cu_data *per_cu = NULL;
5543 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5544 {
5545 ULONGEST ull;
5546 switch (attr.form)
5547 {
5548 case DW_FORM_implicit_const:
5549 ull = attr.implicit_const;
5550 break;
5551 case DW_FORM_flag_present:
5552 ull = 1;
5553 break;
5554 case DW_FORM_udata:
5555 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5556 m_addr += bytes_read;
5557 break;
6dc55ce9 5558 case DW_FORM_ref4:
5559 ull = read_4_bytes (abfd, m_addr);
5560 m_addr += 4;
5561 break;
5562 case DW_FORM_ref8:
5563 ull = read_8_bytes (abfd, m_addr);
5564 m_addr += 8;
5565 break;
5566 case DW_FORM_ref_sig8:
5567 ull = read_8_bytes (abfd, m_addr);
5568 m_addr += 8;
5569 break;
927aa2e7 5570 default:
b98664d3 5571 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5572 dwarf_form_name (attr.form),
ed2dc618 5573 objfile_name (objfile));
927aa2e7
JK
5574 return NULL;
5575 }
5576 switch (attr.dw_idx)
5577 {
5578 case DW_IDX_compile_unit:
5579 /* Don't crash on bad data. */
fcf23d5b 5580 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5581 {
b98664d3 5582 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5583 " [in module %s]"),
5584 pulongest (ull),
fcf23d5b 5585 objfile_name (objfile));
927aa2e7
JK
5586 continue;
5587 }
fcf23d5b 5588 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5589 break;
8af5c486
JK
5590 case DW_IDX_type_unit:
5591 /* Don't crash on bad data. */
fcf23d5b 5592 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5593 {
b98664d3 5594 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5595 " [in module %s]"),
5596 pulongest (ull),
fcf23d5b 5597 objfile_name (objfile));
8af5c486
JK
5598 continue;
5599 }
fcf23d5b 5600 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5601 break;
6dc55ce9 5602 case DW_IDX_die_offset:
5603 /* In a per-CU index (as opposed to a per-module index), index
5604 entries without CU attribute implicitly refer to the single CU. */
5605 if (per_cu == NULL)
fcf23d5b 5606 per_cu = per_bfd->get_cu (0);
6dc55ce9 5607 break;
927aa2e7
JK
5608 case DW_IDX_GNU_internal:
5609 if (!m_map.augmentation_is_gdb)
5610 break;
23c13d42 5611 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5612 break;
5613 case DW_IDX_GNU_external:
5614 if (!m_map.augmentation_is_gdb)
5615 break;
23c13d42 5616 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5617 break;
5618 }
5619 }
5620
5621 /* Skip if already read in. */
fcf23d5b 5622 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5623 goto again;
5624
5625 /* Check static vs global. */
23c13d42 5626 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5627 {
2b79f376 5628 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5629 const bool symbol_is_static =
5630 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5631 if (want_static != symbol_is_static)
2b79f376 5632 goto again;
927aa2e7
JK
5633 }
5634
5635 /* Match dw2_symtab_iter_next, symbol_kind
5636 and debug_names::psymbol_tag. */
5637 switch (m_domain)
5638 {
5639 case VAR_DOMAIN:
5640 switch (indexval.dwarf_tag)
5641 {
5642 case DW_TAG_variable:
5643 case DW_TAG_subprogram:
5644 /* Some types are also in VAR_DOMAIN. */
5645 case DW_TAG_typedef:
5646 case DW_TAG_structure_type:
5647 break;
5648 default:
5649 goto again;
5650 }
5651 break;
5652 case STRUCT_DOMAIN:
5653 switch (indexval.dwarf_tag)
5654 {
5655 case DW_TAG_typedef:
5656 case DW_TAG_structure_type:
5657 break;
5658 default:
5659 goto again;
5660 }
5661 break;
5662 case LABEL_DOMAIN:
5663 switch (indexval.dwarf_tag)
5664 {
5665 case 0:
5666 case DW_TAG_variable:
5667 break;
5668 default:
5669 goto again;
5670 }
5671 break;
59c35742
AB
5672 case MODULE_DOMAIN:
5673 switch (indexval.dwarf_tag)
5674 {
5675 case DW_TAG_module:
5676 break;
5677 default:
5678 goto again;
5679 }
5680 break;
927aa2e7
JK
5681 default:
5682 break;
5683 }
5684
5685 /* Match dw2_expand_symtabs_matching, symbol_kind and
5686 debug_names::psymbol_tag. */
5687 switch (m_search)
4b514bc8 5688 {
927aa2e7
JK
5689 case VARIABLES_DOMAIN:
5690 switch (indexval.dwarf_tag)
4b514bc8 5691 {
927aa2e7
JK
5692 case DW_TAG_variable:
5693 break;
5694 default:
5695 goto again;
4b514bc8 5696 }
927aa2e7
JK
5697 break;
5698 case FUNCTIONS_DOMAIN:
5699 switch (indexval.dwarf_tag)
4b514bc8 5700 {
927aa2e7
JK
5701 case DW_TAG_subprogram:
5702 break;
5703 default:
5704 goto again;
4b514bc8 5705 }
927aa2e7
JK
5706 break;
5707 case TYPES_DOMAIN:
5708 switch (indexval.dwarf_tag)
5709 {
5710 case DW_TAG_typedef:
5711 case DW_TAG_structure_type:
5712 break;
5713 default:
5714 goto again;
5715 }
5716 break;
59c35742
AB
5717 case MODULES_DOMAIN:
5718 switch (indexval.dwarf_tag)
5719 {
5720 case DW_TAG_module:
5721 break;
5722 default:
5723 goto again;
5724 }
927aa2e7
JK
5725 default:
5726 break;
4b514bc8 5727 }
927aa2e7
JK
5728
5729 return per_cu;
4b514bc8 5730}
61920122 5731
927aa2e7 5732static struct compunit_symtab *
c7f839cb 5733dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5734 const char *name, domain_enum domain)
4b514bc8 5735{
976ca316 5736 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5737
976ca316 5738 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5739 if (!mapp)
61920122 5740 {
927aa2e7
JK
5741 /* index is NULL if OBJF_READNOW. */
5742 return NULL;
5743 }
5744 const auto &map = *mapp;
9291a0cd 5745
976ca316 5746 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5747
927aa2e7
JK
5748 struct compunit_symtab *stab_best = NULL;
5749 struct dwarf2_per_cu_data *per_cu;
5750 while ((per_cu = iter.next ()) != NULL)
5751 {
5752 struct symbol *sym, *with_opaque = NULL;
97a1449a 5753 compunit_symtab *stab
976ca316 5754 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5755 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5756 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5757
927aa2e7
JK
5758 sym = block_find_symbol (block, name, domain,
5759 block_find_non_opaque_type_preferred,
5760 &with_opaque);
9703b513 5761
927aa2e7
JK
5762 /* Some caution must be observed with overloaded functions and
5763 methods, since the index will not contain any overload
5764 information (but NAME might contain it). */
a3ec0bb1 5765
927aa2e7 5766 if (sym != NULL
987012b8 5767 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5768 return stab;
5769 if (with_opaque != NULL
987012b8 5770 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5771 stab_best = stab;
9703b513 5772
927aa2e7 5773 /* Keep looking through other CUs. */
9703b513
TT
5774 }
5775
927aa2e7 5776 return stab_best;
9703b513
TT
5777}
5778
927aa2e7
JK
5779/* This dumps minimal information about .debug_names. It is called
5780 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5781 uses this to verify that .debug_names has been loaded. */
9291a0cd 5782
927aa2e7
JK
5783static void
5784dw2_debug_names_dump (struct objfile *objfile)
5785{
976ca316 5786 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5787
976ca316 5788 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5789 printf_filtered (".debug_names:");
976ca316 5790 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5791 printf_filtered (" exists\n");
5792 else
5793 printf_filtered (" faked for \"readnow\"\n");
5794 printf_filtered ("\n");
9291a0cd
TT
5795}
5796
9291a0cd 5797static void
927aa2e7
JK
5798dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5799 const char *func_name)
9291a0cd 5800{
976ca316 5801 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5802
976ca316
SM
5803 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5804 if (per_objfile->per_bfd->debug_names_table)
24c79950 5805 {
976ca316 5806 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5807
fcf23d5b 5808 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5809 per_objfile);
24c79950 5810
927aa2e7
JK
5811 struct dwarf2_per_cu_data *per_cu;
5812 while ((per_cu = iter.next ()) != NULL)
976ca316 5813 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5814 }
5815}
24c79950 5816
3b00ef10
TT
5817static void
5818dw2_debug_names_map_matching_symbols
5819 (struct objfile *objfile,
5820 const lookup_name_info &name, domain_enum domain,
5821 int global,
5822 gdb::function_view<symbol_found_callback_ftype> callback,
5823 symbol_compare_ftype *ordered_compare)
5824{
976ca316 5825 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5826
5827 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5828 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5829 return;
5830
976ca316 5831 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5832 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5833
5834 const char *match_name = name.ada ().lookup_name ().c_str ();
5835 auto matcher = [&] (const char *symname)
5836 {
5837 if (ordered_compare == nullptr)
5838 return true;
5839 return ordered_compare (symname, match_name) == 0;
5840 };
5841
5842 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5843 [&] (offset_type namei)
5844 {
5845 /* The name was matched, now expand corresponding CUs that were
5846 marked. */
fcf23d5b 5847 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5848 per_objfile);
3b00ef10
TT
5849
5850 struct dwarf2_per_cu_data *per_cu;
5851 while ((per_cu = iter.next ()) != NULL)
976ca316 5852 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5853 nullptr);
3b00ef10 5854 return true;
976ca316 5855 }, per_objfile);
3b00ef10
TT
5856
5857 /* It's a shame we couldn't do this inside the
5858 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5859 that have already been expanded. Instead, this loop matches what
5860 the psymtab code does. */
976ca316 5861 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5862 {
976ca316 5863 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5864 if (symtab != nullptr)
3b00ef10
TT
5865 {
5866 const struct block *block
af758d11 5867 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5868 if (!iterate_over_symbols_terminated (block, name,
5869 domain, callback))
5870 break;
5871 }
5872 }
5873}
5874
927aa2e7
JK
5875static void
5876dw2_debug_names_expand_symtabs_matching
5877 (struct objfile *objfile,
5878 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5879 const lookup_name_info *lookup_name,
927aa2e7
JK
5880 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5881 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5882 enum search_domain kind)
5883{
976ca316 5884 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5885
927aa2e7 5886 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5887 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5888 return;
9291a0cd 5889
976ca316 5890 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5891
c1a66c06
TV
5892 if (symbol_matcher == NULL && lookup_name == NULL)
5893 {
976ca316 5894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5895 {
5896 QUIT;
5897
976ca316
SM
5898 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5899 expansion_notify);
c1a66c06
TV
5900 }
5901 return;
5902 }
5903
976ca316 5904 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5905
c1a66c06 5906 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5907 symbol_matcher,
5908 kind, [&] (offset_type namei)
927aa2e7 5909 {
927aa2e7
JK
5910 /* The name was matched, now expand corresponding CUs that were
5911 marked. */
976ca316 5912 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5913
927aa2e7
JK
5914 struct dwarf2_per_cu_data *per_cu;
5915 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5916 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5917 expansion_notify);
3b00ef10 5918 return true;
976ca316 5919 }, per_objfile);
9291a0cd
TT
5920}
5921
927aa2e7 5922const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5923{
5924 dw2_has_symbols,
5925 dw2_find_last_source_symtab,
5926 dw2_forget_cached_source_info,
f8eba3c6 5927 dw2_map_symtabs_matching_filename,
927aa2e7 5928 dw2_debug_names_lookup_symbol,
d3214198 5929 NULL,
9291a0cd 5930 dw2_print_stats,
927aa2e7 5931 dw2_debug_names_dump,
927aa2e7 5932 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5933 dw2_expand_all_symtabs,
652a8996 5934 dw2_expand_symtabs_with_fullname,
3b00ef10 5935 dw2_debug_names_map_matching_symbols,
927aa2e7 5936 dw2_debug_names_expand_symtabs_matching,
43f3e411 5937 dw2_find_pc_sect_compunit_symtab,
71a3c369 5938 NULL,
9291a0cd
TT
5939 dw2_map_symbol_filenames
5940};
5941
4485a1c1 5942/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5943 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5944
5945template <typename T>
5946static gdb::array_view<const gdb_byte>
5947get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5948{
5949 dwarf2_section_info *section = &section_owner->gdb_index;
5950
96b79293 5951 if (section->empty ())
4485a1c1
SM
5952 return {};
5953
5954 /* Older elfutils strip versions could keep the section in the main
5955 executable while splitting it for the separate debug info file. */
96b79293 5956 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5957 return {};
5958
96b79293 5959 section->read (obj);
4485a1c1 5960
8bebfcda
PA
5961 /* dwarf2_section_info::size is a bfd_size_type, while
5962 gdb::array_view works with size_t. On 32-bit hosts, with
5963 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5964 is 32-bit. So we need an explicit narrowing conversion here.
5965 This is fine, because it's impossible to allocate or mmap an
5966 array/buffer larger than what size_t can represent. */
5967 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5968}
5969
87d6a7aa
SM
5970/* Lookup the index cache for the contents of the index associated to
5971 DWARF2_OBJ. */
5972
5973static gdb::array_view<const gdb_byte>
5989a64e 5974get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5975{
5976 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5977 if (build_id == nullptr)
5978 return {};
5979
5980 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5981 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5982}
5983
5984/* Same as the above, but for DWZ. */
5985
5986static gdb::array_view<const gdb_byte>
5987get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5988{
5989 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5990 if (build_id == nullptr)
5991 return {};
5992
5993 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5994}
5995
3c0aa29a 5996/* See symfile.h. */
9291a0cd 5997
3c0aa29a
PA
5998bool
5999dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6000{
976ca316
SM
6001 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6002 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6003
9291a0cd
TT
6004 /* If we're about to read full symbols, don't bother with the
6005 indices. In this case we also don't care if some other debug
6006 format is making psymtabs, because they are all about to be
6007 expanded anyway. */
6008 if ((objfile->flags & OBJF_READNOW))
6009 {
17ee85fc
TT
6010 /* When using READNOW, the using_index flag (set below) indicates that
6011 PER_BFD was already initialized, when we loaded some other objfile. */
6012 if (per_bfd->using_index)
6013 {
6014 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6015 per_objfile->resize_symtabs ();
17ee85fc
TT
6016 return true;
6017 }
6018
6019 per_bfd->using_index = 1;
976ca316
SM
6020 create_all_comp_units (per_objfile);
6021 create_all_type_units (per_objfile);
17ee85fc
TT
6022 per_bfd->quick_file_names_table
6023 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6024 per_objfile->resize_symtabs ();
9291a0cd 6025
17ee85fc
TT
6026 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6027 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6028 {
17ee85fc 6029 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6030
17ee85fc 6031 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6032 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6033 }
6034
6035 /* Return 1 so that gdb sees the "quick" functions. However,
6036 these functions will be no-ops because we will have expanded
6037 all symtabs. */
3c0aa29a
PA
6038 *index_kind = dw_index_kind::GDB_INDEX;
6039 return true;
9291a0cd
TT
6040 }
6041
17ee85fc
TT
6042 /* Was a debug names index already read when we processed an objfile sharing
6043 PER_BFD? */
6044 if (per_bfd->debug_names_table != nullptr)
6045 {
6046 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6047 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6048 per_objfile->resize_symtabs ();
17ee85fc
TT
6049 return true;
6050 }
6051
6052 /* Was a GDB index already read when we processed an objfile sharing
6053 PER_BFD? */
6054 if (per_bfd->index_table != nullptr)
6055 {
6056 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6057 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6058 per_objfile->resize_symtabs ();
17ee85fc
TT
6059 return true;
6060 }
6061
efb763a5
SM
6062 /* There might already be partial symtabs built for this BFD. This happens
6063 when loading the same binary twice with the index-cache enabled. If so,
6064 don't try to read an index. The objfile / per_objfile initialization will
6065 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6066 code path. */
6067 if (per_bfd->partial_symtabs != nullptr)
6068 return false;
6069
976ca316 6070 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6071 {
6072 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6073 per_objfile->resize_symtabs ();
3c0aa29a
PA
6074 return true;
6075 }
927aa2e7 6076
976ca316 6077 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6078 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6079 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6080 {
6081 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6082 per_objfile->resize_symtabs ();
3c0aa29a
PA
6083 return true;
6084 }
9291a0cd 6085
87d6a7aa 6086 /* ... otherwise, try to find the index in the index cache. */
976ca316 6087 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6088 get_gdb_index_contents_from_cache,
6089 get_gdb_index_contents_from_cache_dwz))
6090 {
6091 global_index_cache.hit ();
6092 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6093 per_objfile->resize_symtabs ();
87d6a7aa
SM
6094 return true;
6095 }
6096
6097 global_index_cache.miss ();
3c0aa29a 6098 return false;
9291a0cd
TT
6099}
6100
6101\f
6102
dce234bc
PP
6103/* Build a partial symbol table. */
6104
6105void
f29dff0a 6106dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6107{
976ca316
SM
6108 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6109 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6110
6111 if (per_bfd->partial_symtabs != nullptr)
6112 {
6113 /* Partial symbols were already read, so now we can simply
6114 attach them. */
6115 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6116 per_objfile->resize_symtabs ();
17ee85fc
TT
6117 return;
6118 }
c9bf0622 6119
6eee24ce 6120 init_psymbol_list (objfile, 1024);
c906108c 6121
a70b8144 6122 try
c9bf0622
TT
6123 {
6124 /* This isn't really ideal: all the data we allocate on the
6125 objfile's obstack is still uselessly kept around. However,
6126 freeing it seems unsafe. */
906768f9 6127 psymtab_discarder psymtabs (objfile);
976ca316 6128 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6129 psymtabs.keep ();
87d6a7aa 6130
976ca316 6131 per_objfile->resize_symtabs ();
af758d11 6132
87d6a7aa 6133 /* (maybe) store an index in the cache. */
976ca316 6134 global_index_cache.store (per_objfile);
c9bf0622 6135 }
230d2906 6136 catch (const gdb_exception_error &except)
492d29ea
PA
6137 {
6138 exception_print (gdb_stderr, except);
6139 }
17ee85fc
TT
6140
6141 /* Finish by setting the local reference to partial symtabs, so that
6142 we don't try to read them again if reading another objfile with the same
6143 BFD. If we can't in fact share, this won't make a difference anyway as
6144 the dwarf2_per_bfd object won't be shared. */
6145 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6146}
c906108c 6147
3b80fe9b
DE
6148/* Find the base address of the compilation unit for range lists and
6149 location lists. It will normally be specified by DW_AT_low_pc.
6150 In DWARF-3 draft 4, the base address could be overridden by
6151 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6152 compilation units with discontinuous ranges. */
6153
6154static void
6155dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6156{
6157 struct attribute *attr;
6158
2b24b6e4 6159 cu->base_address.reset ();
3b80fe9b
DE
6160
6161 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6162 if (attr != nullptr)
95f982e5 6163 cu->base_address = attr->as_address ();
3b80fe9b
DE
6164 else
6165 {
6166 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6167 if (attr != nullptr)
95f982e5 6168 cu->base_address = attr->as_address ();
3b80fe9b
DE
6169 }
6170}
6171
36586728
TT
6172/* Helper function that returns the proper abbrev section for
6173 THIS_CU. */
6174
6175static struct dwarf2_section_info *
6176get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6177{
6178 struct dwarf2_section_info *abbrev;
c3699833 6179 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6180
6181 if (this_cu->is_dwz)
c3699833 6182 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6183 else
c3699833 6184 abbrev = &per_bfd->abbrev;
36586728
TT
6185
6186 return abbrev;
6187}
6188
f4dc4d17
DE
6189/* Fetch the abbreviation table offset from a comp or type unit header. */
6190
6191static sect_offset
976ca316 6192read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6193 struct dwarf2_section_info *section,
9c541725 6194 sect_offset sect_off)
f4dc4d17 6195{
96b79293 6196 bfd *abfd = section->get_bfd_owner ();
d521ce57 6197 const gdb_byte *info_ptr;
ac298888 6198 unsigned int initial_length_size, offset_size;
43988095 6199 uint16_t version;
f4dc4d17 6200
976ca316 6201 section->read (per_objfile->objfile);
9c541725 6202 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6203 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6204 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6205 info_ptr += initial_length_size;
6206
6207 version = read_2_bytes (abfd, info_ptr);
6208 info_ptr += 2;
6209 if (version >= 5)
6210 {
6211 /* Skip unit type and address size. */
6212 info_ptr += 2;
6213 }
6214
24aa364d 6215 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6216}
6217
b83470bf
TT
6218/* A partial symtab that is used only for include files. */
6219struct dwarf2_include_psymtab : public partial_symtab
6220{
6221 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6222 : partial_symtab (filename, objfile)
6223 {
6224 }
6225
6226 void read_symtab (struct objfile *objfile) override
6227 {
194d088f
TV
6228 /* It's an include file, no symbols to read for it.
6229 Everything is in the includer symtab. */
6230
6231 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6232 expansion of the includer psymtab. We use the dependencies[0] field to
6233 model the includer. But if we go the regular route of calling
6234 expand_psymtab here, and having expand_psymtab call expand_dependencies
6235 to expand the includer, we'll only use expand_psymtab on the includer
6236 (making it a non-toplevel psymtab), while if we expand the includer via
6237 another path, we'll use read_symtab (making it a toplevel psymtab).
6238 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6239 psymtab, and trigger read_symtab on the includer here directly. */
6240 includer ()->read_symtab (objfile);
b83470bf
TT
6241 }
6242
6243 void expand_psymtab (struct objfile *objfile) override
6244 {
194d088f
TV
6245 /* This is not called by read_symtab, and should not be called by any
6246 expand_dependencies. */
6247 gdb_assert (false);
b83470bf
TT
6248 }
6249
5717c425 6250 bool readin_p (struct objfile *objfile) const override
b83470bf 6251 {
5717c425 6252 return includer ()->readin_p (objfile);
b83470bf
TT
6253 }
6254
5717c425 6255 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6256 {
6257 return nullptr;
6258 }
6259
6260private:
194d088f
TV
6261 partial_symtab *includer () const
6262 {
6263 /* An include psymtab has exactly one dependency: the psymtab that
6264 includes it. */
6265 gdb_assert (this->number_of_dependencies == 1);
6266 return this->dependencies[0];
6267 }
b83470bf
TT
6268};
6269
aaa75496
JB
6270/* Allocate a new partial symtab for file named NAME and mark this new
6271 partial symtab as being an include of PST. */
6272
6273static void
891813be 6274dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6275 struct objfile *objfile)
6276{
b83470bf 6277 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6278
fbd9ab74 6279 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6280 subpst->dirname = pst->dirname;
fbd9ab74 6281
a9342b62 6282 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6283 subpst->dependencies[0] = pst;
6284 subpst->number_of_dependencies = 1;
aaa75496
JB
6285}
6286
6287/* Read the Line Number Program data and extract the list of files
6288 included by the source file represented by PST. Build an include
d85a05f0 6289 partial symtab for each of these included files. */
aaa75496
JB
6290
6291static void
6292dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6293 struct die_info *die,
891813be 6294 dwarf2_psymtab *pst)
aaa75496 6295{
fff8551c 6296 line_header_up lh;
d85a05f0 6297 struct attribute *attr;
aaa75496 6298
d85a05f0 6299 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6300 if (attr != nullptr && attr->form_is_unsigned ())
6301 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6302 if (lh == NULL)
6303 return; /* No linetable, so no includes. */
6304
79748972
TT
6305 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6306 that we pass in the raw text_low here; that is ok because we're
6307 only decoding the line table to make include partial symtabs, and
6308 so the addresses aren't really used. */
4ae976d1 6309 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6310 pst->raw_text_low (), 1);
aaa75496
JB
6311}
6312
348e048f 6313static hashval_t
52dc124a 6314hash_signatured_type (const void *item)
348e048f 6315{
9a3c8263
SM
6316 const struct signatured_type *sig_type
6317 = (const struct signatured_type *) item;
9a619af0 6318
348e048f 6319 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6320 return sig_type->signature;
348e048f
DE
6321}
6322
6323static int
52dc124a 6324eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6325{
9a3c8263
SM
6326 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6327 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6328
348e048f
DE
6329 return lhs->signature == rhs->signature;
6330}
6331
1fd400ff
TT
6332/* Allocate a hash table for signatured types. */
6333
b0b6a987 6334static htab_up
298e9637 6335allocate_signatured_type_table ()
1fd400ff 6336{
b0b6a987
TT
6337 return htab_up (htab_create_alloc (41,
6338 hash_signatured_type,
6339 eq_signatured_type,
6340 NULL, xcalloc, xfree));
1fd400ff
TT
6341}
6342
d467dd73 6343/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6344
6345static int
d467dd73 6346add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6347{
9a3c8263 6348 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6349 std::vector<signatured_type *> *all_type_units
6350 = (std::vector<signatured_type *> *) datum;
1fd400ff 6351
b2bdb8cf 6352 all_type_units->push_back (sigt);
1fd400ff
TT
6353
6354 return 1;
6355}
6356
78d4d2c5 6357/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6358 and fill them into TYPES_HTAB. It will process only type units,
6359 therefore DW_UT_type. */
c88ee1f0 6360
78d4d2c5 6361static void
976ca316 6362create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6363 struct dwo_file *dwo_file,
b0b6a987 6364 dwarf2_section_info *section, htab_up &types_htab,
43988095 6365 rcuh_kind section_kind)
348e048f 6366{
976ca316 6367 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6368 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6369 bfd *abfd;
6370 const gdb_byte *info_ptr, *end_ptr;
348e048f 6371
4bdcc0c1
DE
6372 abbrev_section = (dwo_file != NULL
6373 ? &dwo_file->sections.abbrev
976ca316 6374 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6375
b4f54984 6376 if (dwarf_read_debug)
43988095 6377 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6378 section->get_name (),
6379 abbrev_section->get_file_name ());
09406207 6380
96b79293 6381 section->read (objfile);
78d4d2c5 6382 info_ptr = section->buffer;
348e048f 6383
78d4d2c5
JK
6384 if (info_ptr == NULL)
6385 return;
348e048f 6386
78d4d2c5
JK
6387 /* We can't set abfd until now because the section may be empty or
6388 not present, in which case the bfd is unknown. */
96b79293 6389 abfd = section->get_bfd_owner ();
348e048f 6390
c0ab21c2
TT
6391 /* We don't use cutu_reader here because we don't need to read
6392 any dies: the signature is in the header. */
3019eac3 6393
78d4d2c5
JK
6394 end_ptr = info_ptr + section->size;
6395 while (info_ptr < end_ptr)
6396 {
78d4d2c5
JK
6397 struct signatured_type *sig_type;
6398 struct dwo_unit *dwo_tu;
6399 void **slot;
6400 const gdb_byte *ptr = info_ptr;
6401 struct comp_unit_head header;
6402 unsigned int length;
8b70b953 6403
9c541725 6404 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6405
a49dd8dd
JK
6406 /* Initialize it due to a false compiler warning. */
6407 header.signature = -1;
9c541725 6408 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6409
78d4d2c5
JK
6410 /* We need to read the type's signature in order to build the hash
6411 table, but we don't need anything else just yet. */
348e048f 6412
976ca316 6413 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6414 abbrev_section, ptr, section_kind);
348e048f 6415
4057dfde 6416 length = header.get_length ();
6caca83c 6417
78d4d2c5
JK
6418 /* Skip dummy type units. */
6419 if (ptr >= info_ptr + length
43988095 6420 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6421 || (header.unit_type != DW_UT_type
6422 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6423 {
6424 info_ptr += length;
6425 continue;
6426 }
dee91e82 6427
78d4d2c5
JK
6428 if (types_htab == NULL)
6429 {
6430 if (dwo_file)
298e9637 6431 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6432 else
298e9637 6433 types_htab = allocate_signatured_type_table ();
78d4d2c5 6434 }
8b70b953 6435
78d4d2c5
JK
6436 if (dwo_file)
6437 {
6438 sig_type = NULL;
976ca316 6439 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6440 dwo_tu->dwo_file = dwo_file;
43988095 6441 dwo_tu->signature = header.signature;
9c541725 6442 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6443 dwo_tu->section = section;
9c541725 6444 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6445 dwo_tu->length = length;
6446 }
6447 else
6448 {
6449 /* N.B.: type_offset is not usable if this type uses a DWO file.
6450 The real type_offset is in the DWO file. */
6451 dwo_tu = NULL;
976ca316 6452 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6453 sig_type->signature = header.signature;
9c541725 6454 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6455 sig_type->per_cu.is_debug_types = 1;
6456 sig_type->per_cu.section = section;
9c541725 6457 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6458 sig_type->per_cu.length = length;
6459 }
6460
b0b6a987 6461 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6462 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6463 INSERT);
6464 gdb_assert (slot != NULL);
6465 if (*slot != NULL)
6466 {
9c541725 6467 sect_offset dup_sect_off;
0349ea22 6468
3019eac3
DE
6469 if (dwo_file)
6470 {
78d4d2c5
JK
6471 const struct dwo_unit *dup_tu
6472 = (const struct dwo_unit *) *slot;
6473
9c541725 6474 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6475 }
6476 else
6477 {
78d4d2c5
JK
6478 const struct signatured_type *dup_tu
6479 = (const struct signatured_type *) *slot;
6480
9c541725 6481 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6482 }
8b70b953 6483
b98664d3 6484 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6485 " the entry at offset %s, signature %s"),
6486 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6487 hex_string (header.signature));
78d4d2c5
JK
6488 }
6489 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6490
78d4d2c5 6491 if (dwarf_read_debug > 1)
9d8780f0
SM
6492 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6493 sect_offset_str (sect_off),
43988095 6494 hex_string (header.signature));
3019eac3 6495
78d4d2c5
JK
6496 info_ptr += length;
6497 }
6498}
3019eac3 6499
78d4d2c5
JK
6500/* Create the hash table of all entries in the .debug_types
6501 (or .debug_types.dwo) section(s).
6502 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6503 otherwise it is NULL.
b3c8eb43 6504
78d4d2c5 6505 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6506
78d4d2c5 6507 Note: This function processes DWO files only, not DWP files. */
348e048f 6508
78d4d2c5 6509static void
976ca316 6510create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6511 struct dwo_file *dwo_file,
fd5866f6 6512 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6513 htab_up &types_htab)
78d4d2c5 6514{
fd5866f6 6515 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6516 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6517 rcuh_kind::TYPE);
3019eac3
DE
6518}
6519
6520/* Create the hash table of all entries in the .debug_types section,
6521 and initialize all_type_units.
6522 The result is zero if there is an error (e.g. missing .debug_types section),
6523 otherwise non-zero. */
6524
6525static int
976ca316 6526create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6527{
b0b6a987 6528 htab_up types_htab;
3019eac3 6529
976ca316
SM
6530 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6531 types_htab, rcuh_kind::COMPILE);
6532 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6533 types_htab);
3019eac3
DE
6534 if (types_htab == NULL)
6535 {
976ca316 6536 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6537 return 0;
6538 }
6539
976ca316 6540 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6541
976ca316
SM
6542 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6543 per_objfile->per_bfd->all_type_units.reserve
6544 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6545
976ca316 6546 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6547 add_signatured_type_cu_to_table,
976ca316 6548 &per_objfile->per_bfd->all_type_units);
1fd400ff 6549
348e048f
DE
6550 return 1;
6551}
6552
5989a64e 6553/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6554 If SLOT is non-NULL, it is the entry to use in the hash table.
6555 Otherwise we find one. */
6556
6557static struct signatured_type *
976ca316 6558add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6559{
976ca316
SM
6560 if (per_objfile->per_bfd->all_type_units.size ()
6561 == per_objfile->per_bfd->all_type_units.capacity ())
6562 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6563
976ca316 6564 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6565
976ca316 6566 per_objfile->resize_symtabs ();
af758d11 6567
976ca316 6568 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6569 sig_type->signature = sig;
6570 sig_type->per_cu.is_debug_types = 1;
976ca316 6571 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6572 {
6573 sig_type->per_cu.v.quick =
976ca316 6574 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6575 struct dwarf2_per_cu_quick_data);
6576 }
6577
6578 if (slot == NULL)
6579 {
976ca316 6580 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6581 sig_type, INSERT);
6582 }
6583 gdb_assert (*slot == NULL);
6584 *slot = sig_type;
6585 /* The rest of sig_type must be filled in by the caller. */
6586 return sig_type;
6587}
6588
a2ce51a0
DE
6589/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6590 Fill in SIG_ENTRY with DWO_ENTRY. */
6591
6592static void
976ca316 6593fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6594 struct signatured_type *sig_entry,
6595 struct dwo_unit *dwo_entry)
6596{
976ca316 6597 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6598
7ee85ab1 6599 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6600 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6601 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6602 if (per_bfd->using_index)
6aa5f3a6
DE
6603 {
6604 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6605 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6606 }
6607 else
6608 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6609 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6610 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6611 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6612 gdb_assert (sig_entry->dwo_unit == NULL);
6613
6614 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6615 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6616 sig_entry->per_cu.length = dwo_entry->length;
6617 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6618 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6619 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6620 sig_entry->dwo_unit = dwo_entry;
6621}
6622
6623/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6624 If we haven't read the TU yet, create the signatured_type data structure
6625 for a TU to be read in directly from a DWO file, bypassing the stub.
6626 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6627 using .gdb_index, then when reading a CU we want to stay in the DWO file
6628 containing that CU. Otherwise we could end up reading several other DWO
6629 files (due to comdat folding) to process the transitive closure of all the
6630 mentioned TUs, and that can be slow. The current DWO file will have every
6631 type signature that it needs.
a2ce51a0
DE
6632 We only do this for .gdb_index because in the psymtab case we already have
6633 to read all the DWOs to build the type unit groups. */
6634
6635static struct signatured_type *
6636lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6637{
976ca316 6638 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6639 struct dwo_file *dwo_file;
6640 struct dwo_unit find_dwo_entry, *dwo_entry;
6641 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6642 void **slot;
a2ce51a0 6643
976ca316 6644 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6645
6aa5f3a6
DE
6646 /* If TU skeletons have been removed then we may not have read in any
6647 TUs yet. */
976ca316
SM
6648 if (per_objfile->per_bfd->signatured_types == NULL)
6649 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6650
6651 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6652 Use the global signatured_types array to do our own comdat-folding
6653 of types. If this is the first time we're reading this TU, and
6654 the TU has an entry in .gdb_index, replace the recorded data from
6655 .gdb_index with this TU. */
a2ce51a0 6656
a2ce51a0 6657 find_sig_entry.signature = sig;
976ca316 6658 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6659 &find_sig_entry, INSERT);
9a3c8263 6660 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6661
6662 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6663 read. Don't reassign the global entry to point to this DWO if that's
6664 the case. Also note that if the TU is already being read, it may not
6665 have come from a DWO, the program may be a mix of Fission-compiled
6666 code and non-Fission-compiled code. */
6667
6668 /* Have we already tried to read this TU?
6669 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6670 needn't exist in the global table yet). */
6671 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6672 return sig_entry;
6673
6aa5f3a6
DE
6674 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6675 dwo_unit of the TU itself. */
6676 dwo_file = cu->dwo_unit->dwo_file;
6677
a2ce51a0
DE
6678 /* Ok, this is the first time we're reading this TU. */
6679 if (dwo_file->tus == NULL)
6680 return NULL;
6681 find_dwo_entry.signature = sig;
b0b6a987
TT
6682 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6683 &find_dwo_entry);
a2ce51a0
DE
6684 if (dwo_entry == NULL)
6685 return NULL;
6686
6aa5f3a6
DE
6687 /* If the global table doesn't have an entry for this TU, add one. */
6688 if (sig_entry == NULL)
976ca316 6689 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6690
976ca316 6691 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6692 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6693 return sig_entry;
6694}
6695
a2ce51a0
DE
6696/* Subroutine of lookup_signatured_type.
6697 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6698 then try the DWP file. If the TU stub (skeleton) has been removed then
6699 it won't be in .gdb_index. */
a2ce51a0
DE
6700
6701static struct signatured_type *
6702lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6703{
976ca316
SM
6704 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6705 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6706 struct dwo_unit *dwo_entry;
6707 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6708 void **slot;
a2ce51a0 6709
976ca316 6710 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6711 gdb_assert (dwp_file != NULL);
6712
6aa5f3a6
DE
6713 /* If TU skeletons have been removed then we may not have read in any
6714 TUs yet. */
976ca316
SM
6715 if (per_objfile->per_bfd->signatured_types == NULL)
6716 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6717
6aa5f3a6 6718 find_sig_entry.signature = sig;
976ca316 6719 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6720 &find_sig_entry, INSERT);
9a3c8263 6721 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6722
6723 /* Have we already tried to read this TU?
6724 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6725 needn't exist in the global table yet). */
6726 if (sig_entry != NULL)
6727 return sig_entry;
6728
a2ce51a0
DE
6729 if (dwp_file->tus == NULL)
6730 return NULL;
976ca316
SM
6731 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6732 1 /* is_debug_types */);
a2ce51a0
DE
6733 if (dwo_entry == NULL)
6734 return NULL;
6735
976ca316
SM
6736 sig_entry = add_type_unit (per_objfile, sig, slot);
6737 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6738
a2ce51a0
DE
6739 return sig_entry;
6740}
6741
380bca97 6742/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6743 Returns NULL if signature SIG is not present in the table.
6744 It is up to the caller to complain about this. */
348e048f
DE
6745
6746static struct signatured_type *
a2ce51a0 6747lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6748{
976ca316 6749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6750
976ca316 6751 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6752 {
6753 /* We're in a DWO/DWP file, and we're using .gdb_index.
6754 These cases require special processing. */
976ca316 6755 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6756 return lookup_dwo_signatured_type (cu, sig);
6757 else
6758 return lookup_dwp_signatured_type (cu, sig);
6759 }
6760 else
6761 {
6762 struct signatured_type find_entry, *entry;
348e048f 6763
976ca316 6764 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6765 return NULL;
6766 find_entry.signature = sig;
9a3c8263 6767 entry = ((struct signatured_type *)
976ca316 6768 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6769 &find_entry));
a2ce51a0
DE
6770 return entry;
6771 }
348e048f 6772}
18a8505e 6773
42e7ad6c 6774/* Low level DIE reading support. */
348e048f 6775
d85a05f0
DJ
6776/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6777
6778static void
6779init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6780 struct dwarf2_cu *cu,
3019eac3 6781 struct dwarf2_section_info *section,
685af9cd
TT
6782 struct dwo_file *dwo_file,
6783 struct abbrev_table *abbrev_table)
d85a05f0 6784{
fceca515 6785 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6786 reader->abfd = section->get_bfd_owner ();
d85a05f0 6787 reader->cu = cu;
3019eac3 6788 reader->dwo_file = dwo_file;
dee91e82
DE
6789 reader->die_section = section;
6790 reader->buffer = section->buffer;
f664829e 6791 reader->buffer_end = section->buffer + section->size;
685af9cd 6792 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6793}
6794
c0ab21c2 6795/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6796 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6797 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6798 already.
6799
6800 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6801 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6802 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6803 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6804 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6805 STUB_COMP_DIR may be non-NULL.
3e225074 6806 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6807 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6808 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6809 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6810 kept around for at least as long as *RESULT_READER.
6811
b0c7bfa9
DE
6812 The result is non-zero if a valid (non-dummy) DIE was found. */
6813
6814static int
4ab09049 6815read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6816 struct dwo_unit *dwo_unit,
b0c7bfa9 6817 struct die_info *stub_comp_unit_die,
a2ce51a0 6818 const char *stub_comp_dir,
b0c7bfa9 6819 struct die_reader_specs *result_reader,
d521ce57 6820 const gdb_byte **result_info_ptr,
b0c7bfa9 6821 struct die_info **result_comp_unit_die,
685af9cd 6822 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6823{
976ca316 6824 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6825 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6826 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6827 bfd *abfd;
d521ce57 6828 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6829 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6830 int i,num_extra_attrs;
6831 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6832 struct die_info *comp_unit_die;
6833
b0aeadb3
DE
6834 /* At most one of these may be provided. */
6835 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6836
b0c7bfa9
DE
6837 /* These attributes aren't processed until later:
6838 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6839 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6840 referenced later. However, these attributes are found in the stub
6841 which we won't have later. In order to not impose this complication
6842 on the rest of the code, we read them here and copy them to the
6843 DWO CU/TU die. */
b0c7bfa9
DE
6844
6845 stmt_list = NULL;
6846 low_pc = NULL;
6847 high_pc = NULL;
6848 ranges = NULL;
6849 comp_dir = NULL;
6850
6851 if (stub_comp_unit_die != NULL)
6852 {
6853 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6854 DWO file. */
4ab09049 6855 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6856 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6857 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6858 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6859 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6860 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6861
a39fdb41 6862 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6863
18a8505e
AT
6864 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6865 here (if needed). We need the value before we can process
6866 DW_AT_ranges. */
a39fdb41 6867 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6868 }
a2ce51a0
DE
6869 else if (stub_comp_dir != NULL)
6870 {
6871 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6872 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6873 comp_dir->name = DW_AT_comp_dir;
6874 comp_dir->form = DW_FORM_string;
c6481205 6875 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6876 }
b0c7bfa9
DE
6877
6878 /* Set up for reading the DWO CU/TU. */
6879 cu->dwo_unit = dwo_unit;
685af9cd 6880 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6881 section->read (objfile);
6882 abfd = section->get_bfd_owner ();
9c541725
PA
6883 begin_info_ptr = info_ptr = (section->buffer
6884 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6885 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6886
4ab09049 6887 if (per_cu->is_debug_types)
b0c7bfa9 6888 {
4ab09049 6889 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6890
976ca316
SM
6891 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6892 section, dwo_abbrev_section,
43988095 6893 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6894 /* This is not an assert because it can be caused by bad debug info. */
43988095 6895 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6896 {
6897 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6898 " TU at offset %s [in module %s]"),
a2ce51a0 6899 hex_string (sig_type->signature),
43988095 6900 hex_string (cu->header.signature),
9d8780f0 6901 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6902 bfd_get_filename (abfd));
6903 }
9c541725 6904 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6905 /* For DWOs coming from DWP files, we don't know the CU length
6906 nor the type's offset in the TU until now. */
4057dfde 6907 dwo_unit->length = cu->header.get_length ();
9c541725 6908 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6909
6910 /* Establish the type offset that can be used to lookup the type.
6911 For DWO files, we don't know it until now. */
9c541725
PA
6912 sig_type->type_offset_in_section
6913 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6914 }
6915 else
6916 {
976ca316
SM
6917 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6918 section, dwo_abbrev_section,
43988095 6919 info_ptr, rcuh_kind::COMPILE);
9c541725 6920 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6921 /* For DWOs coming from DWP files, we don't know the CU length
6922 until now. */
4057dfde 6923 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6924 }
6925
685af9cd 6926 *result_dwo_abbrev_table
86de1d91
TT
6927 = abbrev_table::read (objfile, dwo_abbrev_section,
6928 cu->header.abbrev_sect_off);
685af9cd
TT
6929 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6930 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6931
6932 /* Read in the die, but leave space to copy over the attributes
6933 from the stub. This has the benefit of simplifying the rest of
6934 the code - all the work to maintain the illusion of a single
6935 DW_TAG_{compile,type}_unit DIE is done here. */
6936 num_extra_attrs = ((stmt_list != NULL)
6937 + (low_pc != NULL)
6938 + (high_pc != NULL)
6939 + (ranges != NULL)
6940 + (comp_dir != NULL));
6941 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6942 num_extra_attrs);
b0c7bfa9
DE
6943
6944 /* Copy over the attributes from the stub to the DIE we just read in. */
6945 comp_unit_die = *result_comp_unit_die;
6946 i = comp_unit_die->num_attrs;
6947 if (stmt_list != NULL)
6948 comp_unit_die->attrs[i++] = *stmt_list;
6949 if (low_pc != NULL)
6950 comp_unit_die->attrs[i++] = *low_pc;
6951 if (high_pc != NULL)
6952 comp_unit_die->attrs[i++] = *high_pc;
6953 if (ranges != NULL)
6954 comp_unit_die->attrs[i++] = *ranges;
6955 if (comp_dir != NULL)
6956 comp_unit_die->attrs[i++] = *comp_dir;
6957 comp_unit_die->num_attrs += num_extra_attrs;
6958
b4f54984 6959 if (dwarf_die_debug)
bf6af496
DE
6960 {
6961 fprintf_unfiltered (gdb_stdlog,
6962 "Read die from %s@0x%x of %s:\n",
96b79293 6963 section->get_name (),
bf6af496
DE
6964 (unsigned) (begin_info_ptr - section->buffer),
6965 bfd_get_filename (abfd));
b4f54984 6966 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6967 }
6968
b0c7bfa9
DE
6969 /* Skip dummy compilation units. */
6970 if (info_ptr >= begin_info_ptr + dwo_unit->length
6971 || peek_abbrev_code (abfd, info_ptr) == 0)
6972 return 0;
6973
6974 *result_info_ptr = info_ptr;
6975 return 1;
6976}
6977
a084a2a6
AT
6978/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6979 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6980 signature is part of the header. */
6981static gdb::optional<ULONGEST>
6982lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6983{
6984 if (cu->header.version >= 5)
6985 return cu->header.signature;
6986 struct attribute *attr;
6987 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6988 if (attr == nullptr)
6989 return gdb::optional<ULONGEST> ();
6990 return DW_UNSND (attr);
6991}
6992
c0ab21c2 6993/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6994 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6995 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6996
6997static struct dwo_unit *
4ab09049 6998lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6999{
4ab09049 7000 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7001 struct dwo_unit *dwo_unit;
c0ab21c2 7002 const char *comp_dir;
b0c7bfa9 7003
a2ce51a0
DE
7004 gdb_assert (cu != NULL);
7005
b0c7bfa9 7006 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7007 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7008 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7009
4ab09049
SM
7010 if (per_cu->is_debug_types)
7011 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7012 else
7013 {
a084a2a6 7014 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7015
a084a2a6 7016 if (!signature.has_value ())
b0c7bfa9
DE
7017 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7018 " [in module %s]"),
4ab09049
SM
7019 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7020
7021 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7022 }
7023
b0c7bfa9
DE
7024 return dwo_unit;
7025}
7026
c0ab21c2 7027/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7028 See it for a description of the parameters.
fcd3b13d 7029 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7030
c0ab21c2 7031void
9e021579
SM
7032cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7033 dwarf2_per_objfile *per_objfile,
2e671100 7034 dwarf2_cu *existing_cu)
a2ce51a0 7035{
a2ce51a0 7036 struct signatured_type *sig_type;
a2ce51a0
DE
7037
7038 /* Verify we can do the following downcast, and that we have the
7039 data we need. */
7040 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7041 sig_type = (struct signatured_type *) this_cu;
7042 gdb_assert (sig_type->dwo_unit != NULL);
7043
2e671100
SM
7044 dwarf2_cu *cu;
7045
7046 if (existing_cu != nullptr)
6aa5f3a6 7047 {
2e671100
SM
7048 cu = existing_cu;
7049 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7050 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7051 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7052 }
7053 else
7054 {
7188ed02
SM
7055 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7056 in per_objfile yet. */
7057 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7058 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7059 cu = m_new_cu.get ();
6aa5f3a6
DE
7060 }
7061
7062 /* A future optimization, if needed, would be to use an existing
7063 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7064 could share abbrev tables. */
a2ce51a0 7065
2e671100 7066 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7067 NULL /* stub_comp_unit_die */,
7068 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7069 this, &info_ptr,
3e225074 7070 &comp_unit_die,
c0ab21c2 7071 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7072 {
7073 /* Dummy die. */
c0ab21c2 7074 dummy_p = true;
a2ce51a0 7075 }
a2ce51a0
DE
7076}
7077
fd820528 7078/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7079 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7080
f4dc4d17
DE
7081 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7082 Otherwise the table specified in the comp unit header is read in and used.
7083 This is an optimization for when we already have the abbrev table.
7084
2e671100
SM
7085 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7086 allocated. */
aaa75496 7087
ab432490 7088cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7089 dwarf2_per_objfile *per_objfile,
c0ab21c2 7090 struct abbrev_table *abbrev_table,
2e671100 7091 dwarf2_cu *existing_cu,
c0ab21c2
TT
7092 bool skip_partial)
7093 : die_reader_specs {},
6751ebae 7094 m_this_cu (this_cu)
c906108c 7095{
976ca316 7096 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7097 struct dwarf2_section_info *section = this_cu->section;
96b79293 7098 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7099 const gdb_byte *begin_info_ptr;
dee91e82 7100 struct signatured_type *sig_type = NULL;
4bdcc0c1 7101 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7102 /* Non-zero if CU currently points to a DWO file and we need to
7103 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7104 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7105 int rereading_dwo_cu = 0;
c906108c 7106
b4f54984 7107 if (dwarf_die_debug)
9d8780f0 7108 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7109 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7110 sect_offset_str (this_cu->sect_off));
09406207 7111
a2ce51a0
DE
7112 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7113 file (instead of going through the stub), short-circuit all of this. */
7114 if (this_cu->reading_dwo_directly)
7115 {
7116 /* Narrow down the scope of possibilities to have to understand. */
7117 gdb_assert (this_cu->is_debug_types);
7118 gdb_assert (abbrev_table == NULL);
976ca316 7119 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7120 return;
7121 }
7122
dee91e82 7123 /* This is cheap if the section is already read in. */
96b79293 7124 section->read (objfile);
dee91e82 7125
9c541725 7126 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7127
7128 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7129
2e671100
SM
7130 dwarf2_cu *cu;
7131
7132 if (existing_cu != nullptr)
dee91e82 7133 {
2e671100 7134 cu = existing_cu;
42e7ad6c
DE
7135 /* If this CU is from a DWO file we need to start over, we need to
7136 refetch the attributes from the skeleton CU.
7137 This could be optimized by retrieving those attributes from when we
7138 were here the first time: the previous comp_unit_die was stored in
7139 comp_unit_obstack. But there's no data yet that we need this
7140 optimization. */
7141 if (cu->dwo_unit != NULL)
7142 rereading_dwo_cu = 1;
dee91e82
DE
7143 }
7144 else
7145 {
7188ed02
SM
7146 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7147 in per_objfile yet. */
976ca316
SM
7148 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7149 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7150 cu = m_new_cu.get ();
42e7ad6c 7151 }
dee91e82 7152
b0c7bfa9 7153 /* Get the header. */
9c541725 7154 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7155 {
7156 /* We already have the header, there's no need to read it in again. */
9c541725 7157 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7158 }
7159 else
7160 {
3019eac3 7161 if (this_cu->is_debug_types)
dee91e82 7162 {
976ca316
SM
7163 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7164 section, abbrev_section,
7165 info_ptr, rcuh_kind::TYPE);
dee91e82 7166
42e7ad6c
DE
7167 /* Since per_cu is the first member of struct signatured_type,
7168 we can go from a pointer to one to a pointer to the other. */
7169 sig_type = (struct signatured_type *) this_cu;
43988095 7170 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7171 gdb_assert (sig_type->type_offset_in_tu
7172 == cu->header.type_cu_offset_in_tu);
7173 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7174
42e7ad6c
DE
7175 /* LENGTH has not been set yet for type units if we're
7176 using .gdb_index. */
4057dfde 7177 this_cu->length = cu->header.get_length ();
3019eac3
DE
7178
7179 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7180 sig_type->type_offset_in_section =
7181 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7182
7183 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7184 }
7185 else
7186 {
976ca316
SM
7187 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7188 section, abbrev_section,
43988095
JK
7189 info_ptr,
7190 rcuh_kind::COMPILE);
dee91e82 7191
9c541725 7192 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7193 if (this_cu->length == 0)
7194 this_cu->length = cu->header.get_length ();
7195 else
7196 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7197 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7198 }
7199 }
10b3939b 7200
6caca83c 7201 /* Skip dummy compilation units. */
dee91e82 7202 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7203 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7204 {
7205 dummy_p = true;
7206 return;
7207 }
6caca83c 7208
433df2d4
DE
7209 /* If we don't have them yet, read the abbrevs for this compilation unit.
7210 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7211 done. */
f4dc4d17 7212 if (abbrev_table != NULL)
685af9cd
TT
7213 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7214 else
f4dc4d17 7215 {
c0ab21c2 7216 m_abbrev_table_holder
86de1d91
TT
7217 = abbrev_table::read (objfile, abbrev_section,
7218 cu->header.abbrev_sect_off);
c0ab21c2 7219 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7220 }
af703f96 7221
dee91e82 7222 /* Read the top level CU/TU die. */
c0ab21c2 7223 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7224 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7225
58f0c718 7226 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7227 {
7228 dummy_p = true;
7229 return;
7230 }
58f0c718 7231
b0c7bfa9 7232 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7233 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7234 table from the DWO file and pass the ownership over to us. It will be
7235 referenced from READER, so we must make sure to free it after we're done
7236 with READER.
7237
b0c7bfa9
DE
7238 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7239 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7240 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7241 if (dwo_name != nullptr)
3019eac3 7242 {
3019eac3 7243 struct dwo_unit *dwo_unit;
b0c7bfa9 7244 struct die_info *dwo_comp_unit_die;
3019eac3 7245
3e225074 7246 if (comp_unit_die->has_children)
6a506a2d 7247 {
b98664d3 7248 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7249 " has children (offset %s) [in module %s]"),
7250 sect_offset_str (this_cu->sect_off),
7251 bfd_get_filename (abfd));
6a506a2d 7252 }
4ab09049 7253 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7254 if (dwo_unit != NULL)
3019eac3 7255 {
4ab09049 7256 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7257 comp_unit_die, NULL,
c0ab21c2 7258 this, &info_ptr,
3e225074 7259 &dwo_comp_unit_die,
c0ab21c2 7260 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7261 {
7262 /* Dummy die. */
c0ab21c2 7263 dummy_p = true;
6a506a2d
DE
7264 return;
7265 }
7266 comp_unit_die = dwo_comp_unit_die;
7267 }
7268 else
7269 {
7270 /* Yikes, we couldn't find the rest of the DIE, we only have
7271 the stub. A complaint has already been logged. There's
7272 not much more we can do except pass on the stub DIE to
7273 die_reader_func. We don't want to throw an error on bad
7274 debug info. */
3019eac3
DE
7275 }
7276 }
c0ab21c2 7277}
3019eac3 7278
6751ebae
TT
7279void
7280cutu_reader::keep ()
c0ab21c2 7281{
b0c7bfa9 7282 /* Done, clean up. */
6751ebae
TT
7283 gdb_assert (!dummy_p);
7284 if (m_new_cu != NULL)
348e048f 7285 {
7188ed02
SM
7286 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7287 now. */
7288 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7289 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7290 }
dee91e82
DE
7291}
7292
18a8505e
AT
7293/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7294 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7295 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7296
7297 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7298 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7299
7300 We fill in THIS_CU->length.
7301
dee91e82 7302 THIS_CU->cu is always freed when done.
3019eac3 7303 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7304 to care whether it refers to the "main" CU or the DWO CU.
7305
7306 When parent_cu is passed, it is used to provide a default value for
7307 str_offsets_base and addr_base from the parent. */
dee91e82 7308
ab432490 7309cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7310 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7311 struct dwarf2_cu *parent_cu,
7312 struct dwo_file *dwo_file)
7313 : die_reader_specs {},
7314 m_this_cu (this_cu)
dee91e82 7315{
976ca316 7316 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7317 struct dwarf2_section_info *section = this_cu->section;
96b79293 7318 bfd *abfd = section->get_bfd_owner ();
33e80786 7319 struct dwarf2_section_info *abbrev_section;
d521ce57 7320 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7321
b4f54984 7322 if (dwarf_die_debug)
9d8780f0 7323 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7324 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7325 sect_offset_str (this_cu->sect_off));
09406207 7326
976ca316 7327 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7328
33e80786
DE
7329 abbrev_section = (dwo_file != NULL
7330 ? &dwo_file->sections.abbrev
7331 : get_abbrev_section_for_cu (this_cu));
7332
dee91e82 7333 /* This is cheap if the section is already read in. */
96b79293 7334 section->read (objfile);
dee91e82 7335
976ca316 7336 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7337
9c541725 7338 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7339 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7340 section, abbrev_section, info_ptr,
43988095
JK
7341 (this_cu->is_debug_types
7342 ? rcuh_kind::TYPE
7343 : rcuh_kind::COMPILE));
dee91e82 7344
18a8505e
AT
7345 if (parent_cu != nullptr)
7346 {
c0ab21c2
TT
7347 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7348 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7349 }
4057dfde 7350 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7351
7352 /* Skip dummy compilation units. */
7353 if (info_ptr >= begin_info_ptr + this_cu->length
7354 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7355 {
7356 dummy_p = true;
7357 return;
7358 }
72bf9492 7359
c0ab21c2 7360 m_abbrev_table_holder
86de1d91
TT
7361 = abbrev_table::read (objfile, abbrev_section,
7362 m_new_cu->header.abbrev_sect_off);
dee91e82 7363
c0ab21c2
TT
7364 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7365 m_abbrev_table_holder.get ());
3e225074 7366 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7367}
7368
0018ea6f
DE
7369\f
7370/* Type Unit Groups.
dee91e82 7371
0018ea6f
DE
7372 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7373 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7374 so that all types coming from the same compilation (.o file) are grouped
7375 together. A future step could be to put the types in the same symtab as
7376 the CU the types ultimately came from. */
ff013f42 7377
f4dc4d17
DE
7378static hashval_t
7379hash_type_unit_group (const void *item)
7380{
9a3c8263
SM
7381 const struct type_unit_group *tu_group
7382 = (const struct type_unit_group *) item;
f4dc4d17 7383
094b34ac 7384 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7385}
348e048f
DE
7386
7387static int
f4dc4d17 7388eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7389{
9a3c8263
SM
7390 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7391 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7392
094b34ac 7393 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7394}
348e048f 7395
f4dc4d17
DE
7396/* Allocate a hash table for type unit groups. */
7397
eaa5fa8b 7398static htab_up
298e9637 7399allocate_type_unit_groups_table ()
f4dc4d17 7400{
eaa5fa8b
TT
7401 return htab_up (htab_create_alloc (3,
7402 hash_type_unit_group,
7403 eq_type_unit_group,
7404 NULL, xcalloc, xfree));
f4dc4d17 7405}
dee91e82 7406
f4dc4d17
DE
7407/* Type units that don't have DW_AT_stmt_list are grouped into their own
7408 partial symtabs. We combine several TUs per psymtab to not let the size
7409 of any one psymtab grow too big. */
7410#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7411#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7412
094b34ac 7413/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7414 Create the type_unit_group object used to hold one or more TUs. */
7415
7416static struct type_unit_group *
094b34ac 7417create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7418{
976ca316
SM
7419 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7420 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7421 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7422 struct type_unit_group *tu_group;
f4dc4d17 7423
976ca316 7424 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7425 per_cu = &tu_group->per_cu;
1859c670 7426 per_cu->per_bfd = per_bfd;
f4dc4d17 7427
1859c670 7428 if (per_bfd->using_index)
094b34ac 7429 {
1859c670 7430 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7431 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7432 }
7433 else
7434 {
9c541725 7435 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7436 dwarf2_psymtab *pst;
528e1572 7437 std::string name;
094b34ac
DE
7438
7439 /* Give the symtab a useful name for debug purposes. */
7440 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7441 name = string_printf ("<type_units_%d>",
7442 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7443 else
528e1572 7444 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7445
976ca316 7446 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7447 pst->anonymous = true;
094b34ac 7448 }
f4dc4d17 7449
094b34ac 7450 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7451 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7452
7453 return tu_group;
7454}
7455
094b34ac
DE
7456/* Look up the type_unit_group for type unit CU, and create it if necessary.
7457 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7458
7459static struct type_unit_group *
ff39bb5e 7460get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7461{
976ca316
SM
7462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7463 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7464 struct type_unit_group *tu_group;
7465 void **slot;
7466 unsigned int line_offset;
7467 struct type_unit_group type_unit_group_for_lookup;
7468
976ca316
SM
7469 if (per_objfile->per_bfd->type_unit_groups == NULL)
7470 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7471
7472 /* Do we need to create a new group, or can we use an existing one? */
7473
7474 if (stmt_list)
7475 {
7476 line_offset = DW_UNSND (stmt_list);
7477 ++tu_stats->nr_symtab_sharers;
7478 }
7479 else
7480 {
7481 /* Ugh, no stmt_list. Rare, but we have to handle it.
7482 We can do various things here like create one group per TU or
7483 spread them over multiple groups to split up the expansion work.
7484 To avoid worst case scenarios (too many groups or too large groups)
7485 we, umm, group them in bunches. */
7486 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7487 | (tu_stats->nr_stmt_less_type_units
7488 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7489 ++tu_stats->nr_stmt_less_type_units;
7490 }
7491
094b34ac 7492 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7493 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7494 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7495 &type_unit_group_for_lookup, INSERT);
7496 if (*slot != NULL)
7497 {
9a3c8263 7498 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7499 gdb_assert (tu_group != NULL);
7500 }
7501 else
7502 {
9c541725 7503 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7504 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7505 *slot = tu_group;
7506 ++tu_stats->nr_symtabs;
7507 }
7508
7509 return tu_group;
7510}
0018ea6f
DE
7511\f
7512/* Partial symbol tables. */
7513
7514/* Create a psymtab named NAME and assign it to PER_CU.
7515
7516 The caller must fill in the following details:
7517 dirname, textlow, texthigh. */
7518
891813be 7519static dwarf2_psymtab *
7aa104c4
SM
7520create_partial_symtab (dwarf2_per_cu_data *per_cu,
7521 dwarf2_per_objfile *per_objfile,
7522 const char *name)
0018ea6f 7523{
7aa104c4 7524 struct objfile *objfile = per_objfile->objfile;
891813be 7525 dwarf2_psymtab *pst;
0018ea6f 7526
9f4e76a4 7527 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7528
6d94535f 7529 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7530
7531 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7532 per_cu->v.psymtab = pst;
7533
7534 return pst;
7535}
7536
c0ab21c2 7537/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7538
7539static void
7540process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7541 const gdb_byte *info_ptr,
0018ea6f 7542 struct die_info *comp_unit_die,
c0ab21c2 7543 enum language pretend_language)
0018ea6f
DE
7544{
7545 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7546 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7547 struct objfile *objfile = per_objfile->objfile;
08feed99 7548 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7549 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7550 CORE_ADDR baseaddr;
7551 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7552 dwarf2_psymtab *pst;
3a2b436a 7553 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7554 const char *filename;
0018ea6f 7555
0018ea6f
DE
7556 gdb_assert (! per_cu->is_debug_types);
7557
c0ab21c2 7558 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7559
0018ea6f 7560 /* Allocate a new partial symbol table structure. */
2e927613
TV
7561 gdb::unique_xmalloc_ptr<char> debug_filename;
7562 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7563 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7564 if (filename == NULL)
0018ea6f 7565 filename = "";
2e927613
TV
7566 else if (strcmp (filename, artificial) == 0)
7567 {
7568 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7569 sect_offset_str (per_cu->sect_off),
7570 (char *) NULL));
2e927613
TV
7571 filename = debug_filename.get ();
7572 }
0018ea6f 7573
7aa104c4 7574 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7575
7576 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7577 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7578
b3b3bada 7579 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7580
7581 dwarf2_find_base_address (comp_unit_die, cu);
7582
7583 /* Possibly set the default values of LOWPC and HIGHPC from
7584 `DW_AT_ranges'. */
3a2b436a
JK
7585 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7586 &best_highpc, cu, pst);
7587 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7588 {
7589 CORE_ADDR low
7590 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7591 - baseaddr);
7592 CORE_ADDR high
7593 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7594 - baseaddr - 1);
7595 /* Store the contiguous range if it is not empty; it can be
7596 empty for CUs with no code. */
d320c2b5
TT
7597 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7598 low, high, pst);
79748972 7599 }
0018ea6f
DE
7600
7601 /* Check if comp unit has_children.
7602 If so, read the rest of the partial symbols from this comp unit.
7603 If not, there's no more debug_info for this comp unit. */
3e225074 7604 if (comp_unit_die->has_children)
0018ea6f
DE
7605 {
7606 struct partial_die_info *first_die;
7607 CORE_ADDR lowpc, highpc;
7608
7609 lowpc = ((CORE_ADDR) -1);
7610 highpc = ((CORE_ADDR) 0);
7611
7612 first_die = load_partial_dies (reader, info_ptr, 1);
7613
7614 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7615 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7616
7617 /* If we didn't find a lowpc, set it to highpc to avoid
7618 complaints from `maint check'. */
7619 if (lowpc == ((CORE_ADDR) -1))
7620 lowpc = highpc;
7621
7622 /* If the compilation unit didn't have an explicit address range,
7623 then use the information extracted from its child dies. */
e385593e 7624 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7625 {
7626 best_lowpc = lowpc;
7627 best_highpc = highpc;
7628 }
7629 }
4ae976d1 7630 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7631 best_lowpc + baseaddr)
7632 - baseaddr);
4ae976d1 7633 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7634 best_highpc + baseaddr)
7635 - baseaddr);
0018ea6f 7636
8763cede 7637 end_psymtab_common (objfile, pst);
0018ea6f 7638
ae640021 7639 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7640 {
7641 int i;
ae640021 7642 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7643
7644 /* Fill in 'dependencies' here; we fill in 'users' in a
7645 post-pass. */
7646 pst->number_of_dependencies = len;
a9342b62
TT
7647 pst->dependencies
7648 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7649 for (i = 0; i < len; ++i)
7650 {
7651 pst->dependencies[i]
7652 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7653 }
0018ea6f 7654
ae640021 7655 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7656 }
7657
7658 /* Get the list of files included in the current compilation unit,
7659 and build a psymtab for each of them. */
7660 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7661
b4f54984 7662 if (dwarf_read_debug)
b926417a
TT
7663 fprintf_unfiltered (gdb_stdlog,
7664 "Psymtab for %s unit @%s: %s - %s"
7665 ", %d global, %d static syms\n",
7666 per_cu->is_debug_types ? "type" : "comp",
7667 sect_offset_str (per_cu->sect_off),
7668 paddress (gdbarch, pst->text_low (objfile)),
7669 paddress (gdbarch, pst->text_high (objfile)),
7670 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7671}
7672
7673/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7674 Process compilation unit THIS_CU for a psymtab. */
7675
7676static void
ab432490
SM
7677process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7678 dwarf2_per_objfile *per_objfile,
135f5437 7679 bool want_partial_unit,
b93601f3 7680 enum language pretend_language)
0018ea6f
DE
7681{
7682 /* If this compilation unit was already read in, free the
7683 cached copy in order to read it in again. This is
7684 necessary because we skipped some symbols when we first
7685 read in the compilation unit (see load_partial_dies).
7686 This problem could be avoided, but the benefit is unclear. */
7188ed02 7687 per_objfile->remove_cu (this_cu);
0018ea6f 7688
2e671100 7689 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7690
58990295
TV
7691 switch (reader.comp_unit_die->tag)
7692 {
7693 case DW_TAG_compile_unit:
7694 this_cu->unit_type = DW_UT_compile;
7695 break;
7696 case DW_TAG_partial_unit:
7697 this_cu->unit_type = DW_UT_partial;
7698 break;
7699 default:
7700 abort ();
7701 }
7702
c0ab21c2 7703 if (reader.dummy_p)
f1902523 7704 {
c0ab21c2 7705 /* Nothing. */
f1902523 7706 }
c0ab21c2 7707 else if (this_cu->is_debug_types)
3e225074
TT
7708 build_type_psymtabs_reader (&reader, reader.info_ptr,
7709 reader.comp_unit_die);
135f5437
TT
7710 else if (want_partial_unit
7711 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7712 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7713 reader.comp_unit_die,
c0ab21c2 7714 pretend_language);
0018ea6f 7715
7188ed02 7716 this_cu->lang = reader.cu->language;
58990295 7717
0018ea6f 7718 /* Age out any secondary CUs. */
7188ed02 7719 per_objfile->age_comp_units ();
0018ea6f 7720}
f4dc4d17
DE
7721
7722/* Reader function for build_type_psymtabs. */
7723
7724static void
7725build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7726 const gdb_byte *info_ptr,
3e225074 7727 struct die_info *type_unit_die)
f4dc4d17 7728{
976ca316
SM
7729 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7730 struct objfile *objfile = per_objfile->objfile;
f4dc4d17
DE
7731 struct dwarf2_cu *cu = reader->cu;
7732 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7733 struct signatured_type *sig_type;
f4dc4d17
DE
7734 struct type_unit_group *tu_group;
7735 struct attribute *attr;
7736 struct partial_die_info *first_die;
7737 CORE_ADDR lowpc, highpc;
891813be 7738 dwarf2_psymtab *pst;
f4dc4d17 7739
0186c6a7
DE
7740 gdb_assert (per_cu->is_debug_types);
7741 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7742
3e225074 7743 if (! type_unit_die->has_children)
f4dc4d17
DE
7744 return;
7745
052c8bb8 7746 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7747 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7748
df07e2c7 7749 if (tu_group->tus == nullptr)
a8b3b8e9 7750 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7751 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7752
7753 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7754 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7755 pst->anonymous = true;
f4dc4d17
DE
7756
7757 first_die = load_partial_dies (reader, info_ptr, 1);
7758
7759 lowpc = (CORE_ADDR) -1;
7760 highpc = (CORE_ADDR) 0;
7761 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7762
8763cede 7763 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7764}
7765
73051182
DE
7766/* Struct used to sort TUs by their abbreviation table offset. */
7767
7768struct tu_abbrev_offset
7769{
b2bdb8cf
SM
7770 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7771 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7772 {}
7773
7774 signatured_type *sig_type;
73051182
DE
7775 sect_offset abbrev_offset;
7776};
7777
484cf504 7778/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7779
484cf504
TT
7780static bool
7781sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7782 const struct tu_abbrev_offset &b)
73051182 7783{
484cf504 7784 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7785}
7786
7787/* Efficiently read all the type units.
7788 This does the bulk of the work for build_type_psymtabs.
7789
7790 The efficiency is because we sort TUs by the abbrev table they use and
7791 only read each abbrev table once. In one program there are 200K TUs
7792 sharing 8K abbrev tables.
7793
7794 The main purpose of this function is to support building the
5989a64e 7795 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7796 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7797 can collapse the search space by grouping them by stmt_list.
7798 The savings can be significant, in the same program from above the 200K TUs
7799 share 8K stmt_list tables.
7800
7801 FUNC is expected to call get_type_unit_group, which will create the
7802 struct type_unit_group if necessary and add it to
5989a64e 7803 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7804
7805static void
976ca316 7806build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7807{
976ca316 7808 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7809 abbrev_table_up abbrev_table;
73051182 7810 sect_offset abbrev_offset;
73051182
DE
7811
7812 /* It's up to the caller to not call us multiple times. */
976ca316 7813 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7814
976ca316 7815 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7816 return;
7817
7818 /* TUs typically share abbrev tables, and there can be way more TUs than
7819 abbrev tables. Sort by abbrev table to reduce the number of times we
7820 read each abbrev table in.
7821 Alternatives are to punt or to maintain a cache of abbrev tables.
7822 This is simpler and efficient enough for now.
7823
7824 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7825 symtab to use). Typically TUs with the same abbrev offset have the same
7826 stmt_list value too so in practice this should work well.
7827
7828 The basic algorithm here is:
7829
7830 sort TUs by abbrev table
7831 for each TU with same abbrev table:
7832 read abbrev table if first user
7833 read TU top level DIE
7834 [IWBN if DWO skeletons had DW_AT_stmt_list]
7835 call FUNC */
7836
b4f54984 7837 if (dwarf_read_debug)
73051182
DE
7838 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7839
7840 /* Sort in a separate table to maintain the order of all_type_units
7841 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7842 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7843 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7844
976ca316 7845 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7846 sorted_by_abbrev.emplace_back
976ca316 7847 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7848 sig_type->per_cu.sect_off));
73051182 7849
484cf504
TT
7850 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7851 sort_tu_by_abbrev_offset);
73051182 7852
9c541725 7853 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7854
b2bdb8cf 7855 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7856 {
73051182
DE
7857 /* Switch to the next abbrev table if necessary. */
7858 if (abbrev_table == NULL
b2bdb8cf 7859 || tu.abbrev_offset != abbrev_offset)
73051182 7860 {
b2bdb8cf 7861 abbrev_offset = tu.abbrev_offset;
73051182 7862 abbrev_table =
976ca316
SM
7863 abbrev_table::read (per_objfile->objfile,
7864 &per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7865 ++tu_stats->nr_uniq_abbrev_tables;
7866 }
7867
976ca316 7868 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7869 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7870 if (!reader.dummy_p)
7871 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7872 reader.comp_unit_die);
73051182 7873 }
6aa5f3a6 7874}
73051182 7875
6aa5f3a6
DE
7876/* Print collected type unit statistics. */
7877
7878static void
976ca316 7879print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7880{
976ca316 7881 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7882
7883 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7884 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
976ca316 7885 per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7886 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7887 tu_stats->nr_uniq_abbrev_tables);
7888 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7889 tu_stats->nr_symtabs);
7890 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7891 tu_stats->nr_symtab_sharers);
7892 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7893 tu_stats->nr_stmt_less_type_units);
7894 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7895 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7896}
7897
f4dc4d17
DE
7898/* Traversal function for build_type_psymtabs. */
7899
7900static int
7901build_type_psymtab_dependencies (void **slot, void *info)
7902{
976ca316
SM
7903 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7904 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7905 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7906 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7907 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7908 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7909 int i;
7910
7911 gdb_assert (len > 0);
197400e8 7912 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7913
7914 pst->number_of_dependencies = len;
a9342b62 7915 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7916 for (i = 0; i < len; ++i)
f4dc4d17 7917 {
df07e2c7 7918 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7919 gdb_assert (iter->per_cu.is_debug_types);
7920 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7921 iter->type_unit_group = tu_group;
f4dc4d17
DE
7922 }
7923
df07e2c7
AB
7924 delete tu_group->tus;
7925 tu_group->tus = nullptr;
348e048f
DE
7926
7927 return 1;
7928}
7929
7930/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7931 Build partial symbol tables for the .debug_types comp-units. */
7932
7933static void
976ca316 7934build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7935{
976ca316 7936 if (! create_all_type_units (per_objfile))
348e048f
DE
7937 return;
7938
976ca316 7939 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7940}
f4dc4d17 7941
6aa5f3a6
DE
7942/* Traversal function for process_skeletonless_type_unit.
7943 Read a TU in a DWO file and build partial symbols for it. */
7944
7945static int
7946process_skeletonless_type_unit (void **slot, void *info)
7947{
7948 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7949 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7950 struct signatured_type find_entry, *entry;
7951
7952 /* If this TU doesn't exist in the global table, add it and read it in. */
7953
976ca316
SM
7954 if (per_objfile->per_bfd->signatured_types == NULL)
7955 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7956
7957 find_entry.signature = dwo_unit->signature;
976ca316 7958 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7959 &find_entry, INSERT);
6aa5f3a6
DE
7960 /* If we've already seen this type there's nothing to do. What's happening
7961 is we're doing our own version of comdat-folding here. */
7962 if (*slot != NULL)
7963 return 1;
7964
7965 /* This does the job that create_all_type_units would have done for
7966 this TU. */
976ca316
SM
7967 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7968 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7969 *slot = entry;
7970
7971 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7972 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7973 if (!reader.dummy_p)
7974 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7975 reader.comp_unit_die);
6aa5f3a6
DE
7976
7977 return 1;
7978}
7979
7980/* Traversal function for process_skeletonless_type_units. */
7981
7982static int
7983process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7984{
7985 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7986
7987 if (dwo_file->tus != NULL)
b0b6a987
TT
7988 htab_traverse_noresize (dwo_file->tus.get (),
7989 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7990
7991 return 1;
7992}
7993
7994/* Scan all TUs of DWO files, verifying we've processed them.
7995 This is needed in case a TU was emitted without its skeleton.
7996 Note: This can't be done until we know what all the DWO files are. */
7997
7998static void
976ca316 7999process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8000{
8001 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8002 if (get_dwp_file (per_objfile) == NULL
8003 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8004 {
976ca316 8005 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8006 process_dwo_file_for_skeletonless_type_units,
976ca316 8007 per_objfile);
6aa5f3a6 8008 }
348e048f
DE
8009}
8010
ed2dc618 8011/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8012
8013static void
976ca316 8014set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8015{
976ca316 8016 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8017 {
891813be 8018 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8019
36586728
TT
8020 if (pst == NULL)
8021 continue;
8022
b76e467d 8023 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8024 {
8025 /* Set the 'user' field only if it is not already set. */
8026 if (pst->dependencies[j]->user == NULL)
8027 pst->dependencies[j]->user = pst;
8028 }
8029 }
8030}
8031
93311388
DE
8032/* Build the partial symbol table by doing a quick pass through the
8033 .debug_info and .debug_abbrev sections. */
72bf9492 8034
93311388 8035static void
976ca316 8036dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8037{
976ca316 8038 struct objfile *objfile = per_objfile->objfile;
93311388 8039
b4f54984 8040 if (dwarf_read_debug)
45cfd468
DE
8041 {
8042 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8043 objfile_name (objfile));
45cfd468
DE
8044 }
8045
76935768 8046 scoped_restore restore_reading_psyms
976ca316 8047 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8048 true);
98bfdba5 8049
976ca316 8050 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8051
93311388
DE
8052 /* Any cached compilation units will be linked by the per-objfile
8053 read_in_chain. Make sure to free them when we're done. */
976ca316 8054 free_cached_comp_units freer (per_objfile);
72bf9492 8055
976ca316 8056 build_type_psymtabs (per_objfile);
348e048f 8057
976ca316 8058 create_all_comp_units (per_objfile);
c906108c 8059
60606b2c
TT
8060 /* Create a temporary address map on a temporary obstack. We later
8061 copy this to the final obstack. */
8268c778 8062 auto_obstack temp_obstack;
791afaa2
TT
8063
8064 scoped_restore save_psymtabs_addrmap
d320c2b5 8065 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8066 addrmap_create_mutable (&temp_obstack));
72bf9492 8067
976ca316 8068 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8069 {
8070 if (per_cu->v.psymtab != NULL)
8071 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8072 continue;
976ca316 8073 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8074 language_minimal);
3d5afab3 8075 }
ff013f42 8076
6aa5f3a6 8077 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8078 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8079
8080 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8081 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8082 {
976ca316
SM
8083 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8084 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8085 }
8086
b4f54984 8087 if (dwarf_read_debug)
976ca316 8088 print_tu_stats (per_objfile);
6aa5f3a6 8089
976ca316 8090 set_partial_user (per_objfile);
95554aad 8091
d320c2b5
TT
8092 objfile->partial_symtabs->psymtabs_addrmap
8093 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8094 objfile->partial_symtabs->obstack ());
791afaa2
TT
8095 /* At this point we want to keep the address map. */
8096 save_psymtabs_addrmap.release ();
ff013f42 8097
b4f54984 8098 if (dwarf_read_debug)
45cfd468 8099 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8100 objfile_name (objfile));
ae038cb0
DJ
8101}
8102
dee91e82
DE
8103/* Load the partial DIEs for a secondary CU into memory.
8104 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8105
dee91e82 8106static void
ab432490 8107load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8108 dwarf2_per_objfile *per_objfile,
8109 dwarf2_cu *existing_cu)
dee91e82 8110{
2e671100 8111 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8112
8113 if (!reader.dummy_p)
8114 {
8115 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8116 language_minimal);
8117
8118 /* Check if comp unit has_children.
8119 If so, read the rest of the partial symbols from this comp unit.
8120 If not, there's no more debug_info for this comp unit. */
3e225074 8121 if (reader.comp_unit_die->has_children)
c0ab21c2 8122 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8123
8124 reader.keep ();
c0ab21c2 8125 }
ae038cb0
DJ
8126}
8127
ae038cb0 8128static void
976ca316 8129read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8130 struct dwarf2_section_info *section,
f1902523 8131 struct dwarf2_section_info *abbrev_section,
b76e467d 8132 unsigned int is_dwz)
ae038cb0 8133{
d521ce57 8134 const gdb_byte *info_ptr;
976ca316 8135 struct objfile *objfile = per_objfile->objfile;
be391dca 8136
b4f54984 8137 if (dwarf_read_debug)
bf6af496 8138 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
8139 section->get_name (),
8140 section->get_file_name ());
bf6af496 8141
96b79293 8142 section->read (objfile);
ae038cb0 8143
36586728 8144 info_ptr = section->buffer;
6e70227d 8145
36586728 8146 while (info_ptr < section->buffer + section->size)
ae038cb0 8147 {
ae038cb0 8148 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8149
9c541725 8150 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8151
f1902523 8152 comp_unit_head cu_header;
976ca316 8153 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8154 abbrev_section, info_ptr,
8155 rcuh_kind::COMPILE);
ae038cb0
DJ
8156
8157 /* Save the compilation unit for later lookup. */
f1902523 8158 if (cu_header.unit_type != DW_UT_type)
976ca316 8159 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8160 else
8161 {
976ca316 8162 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8163 sig_type->signature = cu_header.signature;
8164 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8165 this_cu = &sig_type->per_cu;
8166 }
8167 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8168 this_cu->sect_off = sect_off;
f1902523 8169 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8170 this_cu->is_dwz = is_dwz;
8a0459fd 8171 this_cu->section = section;
ae038cb0 8172
976ca316 8173 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8174
8175 info_ptr = info_ptr + this_cu->length;
8176 }
36586728
TT
8177}
8178
8179/* Create a list of all compilation units in OBJFILE.
8180 This is only done for -readnow and building partial symtabs. */
8181
8182static void
976ca316 8183create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8184{
976ca316
SM
8185 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8186 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8187 &per_objfile->per_bfd->abbrev, 0);
36586728 8188
976ca316 8189 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8190 if (dwz != NULL)
976ca316 8191 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8192}
8193
5734ee8b 8194/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8195 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8196 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8197 DW_AT_ranges). See the comments of add_partial_subprogram on how
8198 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8199
72bf9492
DJ
8200static void
8201scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8202 CORE_ADDR *highpc, int set_addrmap,
8203 struct dwarf2_cu *cu)
c906108c 8204{
72bf9492 8205 struct partial_die_info *pdi;
c906108c 8206
91c24f0a
DC
8207 /* Now, march along the PDI's, descending into ones which have
8208 interesting children but skipping the children of the other ones,
8209 until we reach the end of the compilation unit. */
c906108c 8210
72bf9492 8211 pdi = first_die;
91c24f0a 8212
72bf9492
DJ
8213 while (pdi != NULL)
8214 {
52356b79 8215 pdi->fixup (cu);
c906108c 8216
f55ee35c 8217 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8218 children, so we need to look at them. Ditto for anonymous
8219 enums. */
933c6fe4 8220
7d00ffec 8221 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8222 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8223 || pdi->tag == DW_TAG_imported_unit
8224 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8225 {
72bf9492 8226 switch (pdi->tag)
c906108c
SS
8227 {
8228 case DW_TAG_subprogram:
b1dc1806 8229 case DW_TAG_inlined_subroutine:
cdc07690 8230 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8231 if (cu->language == language_cplus)
8232 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8233 set_addrmap, cu);
c906108c 8234 break;
72929c62 8235 case DW_TAG_constant:
c906108c
SS
8236 case DW_TAG_variable:
8237 case DW_TAG_typedef:
91c24f0a 8238 case DW_TAG_union_type:
317d2668
TV
8239 if (!pdi->is_declaration
8240 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8241 {
72bf9492 8242 add_partial_symbol (pdi, cu);
63d06c5c
DC
8243 }
8244 break;
c906108c 8245 case DW_TAG_class_type:
680b30c7 8246 case DW_TAG_interface_type:
c906108c 8247 case DW_TAG_structure_type:
72bf9492 8248 if (!pdi->is_declaration)
c906108c 8249 {
72bf9492 8250 add_partial_symbol (pdi, cu);
c906108c 8251 }
b7fee5a3
KS
8252 if ((cu->language == language_rust
8253 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8254 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8255 set_addrmap, cu);
c906108c 8256 break;
91c24f0a 8257 case DW_TAG_enumeration_type:
72bf9492
DJ
8258 if (!pdi->is_declaration)
8259 add_partial_enumeration (pdi, cu);
c906108c
SS
8260 break;
8261 case DW_TAG_base_type:
a02abb62 8262 case DW_TAG_subrange_type:
c906108c 8263 /* File scope base type definitions are added to the partial
c5aa993b 8264 symbol table. */
72bf9492 8265 add_partial_symbol (pdi, cu);
c906108c 8266 break;
d9fa45fe 8267 case DW_TAG_namespace:
cdc07690 8268 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8269 break;
5d7cb8df 8270 case DW_TAG_module:
59c35742
AB
8271 if (!pdi->is_declaration)
8272 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8273 break;
95554aad
TT
8274 case DW_TAG_imported_unit:
8275 {
8276 struct dwarf2_per_cu_data *per_cu;
8277
f4dc4d17
DE
8278 /* For now we don't handle imported units in type units. */
8279 if (cu->per_cu->is_debug_types)
8280 {
8281 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8282 " supported in type units [in module %s]"),
5e22e966 8283 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8284 }
8285
e3b94546 8286 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8287 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8288
8289 /* Go read the partial unit, if needed. */
8290 if (per_cu->v.psymtab == NULL)
ab432490
SM
8291 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8292 cu->language);
95554aad 8293
ae640021 8294 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8295 }
8296 break;
74921315
KS
8297 case DW_TAG_imported_declaration:
8298 add_partial_symbol (pdi, cu);
8299 break;
c906108c
SS
8300 default:
8301 break;
8302 }
8303 }
8304
72bf9492
DJ
8305 /* If the die has a sibling, skip to the sibling. */
8306
8307 pdi = pdi->die_sibling;
8308 }
8309}
8310
8311/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8312
72bf9492 8313 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8314 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8315 Enumerators are an exception; they use the scope of their parent
8316 enumeration type, i.e. the name of the enumeration type is not
8317 prepended to the enumerator.
91c24f0a 8318
72bf9492
DJ
8319 There are two complexities. One is DW_AT_specification; in this
8320 case "parent" means the parent of the target of the specification,
8321 instead of the direct parent of the DIE. The other is compilers
8322 which do not emit DW_TAG_namespace; in this case we try to guess
8323 the fully qualified name of structure types from their members'
8324 linkage names. This must be done using the DIE's children rather
8325 than the children of any DW_AT_specification target. We only need
8326 to do this for structures at the top level, i.e. if the target of
8327 any DW_AT_specification (if any; otherwise the DIE itself) does not
8328 have a parent. */
8329
8330/* Compute the scope prefix associated with PDI's parent, in
8331 compilation unit CU. The result will be allocated on CU's
8332 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8333 field. NULL is returned if no prefix is necessary. */
15d034d0 8334static const char *
72bf9492
DJ
8335partial_die_parent_scope (struct partial_die_info *pdi,
8336 struct dwarf2_cu *cu)
8337{
15d034d0 8338 const char *grandparent_scope;
72bf9492 8339 struct partial_die_info *parent, *real_pdi;
91c24f0a 8340
72bf9492
DJ
8341 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8342 then this means the parent of the specification DIE. */
8343
8344 real_pdi = pdi;
72bf9492 8345 while (real_pdi->has_specification)
fb816e8b 8346 {
122cf0f2
AB
8347 auto res = find_partial_die (real_pdi->spec_offset,
8348 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8349 real_pdi = res.pdi;
8350 cu = res.cu;
8351 }
72bf9492
DJ
8352
8353 parent = real_pdi->die_parent;
8354 if (parent == NULL)
8355 return NULL;
8356
8357 if (parent->scope_set)
8358 return parent->scope;
8359
52356b79 8360 parent->fixup (cu);
72bf9492 8361
10b3939b 8362 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8363
acebe513
UW
8364 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8365 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8366 Work around this problem here. */
8367 if (cu->language == language_cplus
6e70227d 8368 && parent->tag == DW_TAG_namespace
7d00ffec 8369 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8370 && grandparent_scope == NULL)
8371 {
8372 parent->scope = NULL;
8373 parent->scope_set = 1;
8374 return NULL;
8375 }
8376
0a4b0913 8377 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8378 if (pdi->tag == DW_TAG_enumerator)
8379 /* Enumerators should not get the name of the enumeration as a prefix. */
8380 parent->scope = grandparent_scope;
8381 else if (parent->tag == DW_TAG_namespace
f55ee35c 8382 || parent->tag == DW_TAG_module
72bf9492
DJ
8383 || parent->tag == DW_TAG_structure_type
8384 || parent->tag == DW_TAG_class_type
680b30c7 8385 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8386 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8387 || parent->tag == DW_TAG_enumeration_type
8388 || (cu->language == language_fortran
8389 && parent->tag == DW_TAG_subprogram
8390 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8391 {
8392 if (grandparent_scope == NULL)
7d00ffec 8393 parent->scope = parent->name (cu);
72bf9492 8394 else
3e43a32a
MS
8395 parent->scope = typename_concat (&cu->comp_unit_obstack,
8396 grandparent_scope,
7d00ffec 8397 parent->name (cu), 0, cu);
72bf9492 8398 }
72bf9492
DJ
8399 else
8400 {
8401 /* FIXME drow/2004-04-01: What should we be doing with
8402 function-local names? For partial symbols, we should probably be
8403 ignoring them. */
fa9c3fa0
TT
8404 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8405 dwarf_tag_name (parent->tag),
8406 sect_offset_str (pdi->sect_off));
72bf9492 8407 parent->scope = grandparent_scope;
c906108c
SS
8408 }
8409
72bf9492
DJ
8410 parent->scope_set = 1;
8411 return parent->scope;
8412}
8413
8414/* Return the fully scoped name associated with PDI, from compilation unit
8415 CU. The result will be allocated with malloc. */
4568ecf9 8416
43816ebc 8417static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8418partial_die_full_name (struct partial_die_info *pdi,
8419 struct dwarf2_cu *cu)
8420{
15d034d0 8421 const char *parent_scope;
72bf9492 8422
98bfdba5
PA
8423 /* If this is a template instantiation, we can not work out the
8424 template arguments from partial DIEs. So, unfortunately, we have
8425 to go through the full DIEs. At least any work we do building
8426 types here will be reused if full symbols are loaded later. */
8427 if (pdi->has_template_arguments)
8428 {
52356b79 8429 pdi->fixup (cu);
98bfdba5 8430
7d00ffec 8431 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8432 {
8433 struct die_info *die;
8434 struct attribute attr;
8435 struct dwarf2_cu *ref_cu = cu;
8436
b64f50a1 8437 /* DW_FORM_ref_addr is using section offset. */
b4069958 8438 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8439 attr.form = DW_FORM_ref_addr;
9c541725 8440 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8441 die = follow_die_ref (NULL, &attr, &ref_cu);
8442
43816ebc 8443 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8444 }
8445 }
8446
72bf9492
DJ
8447 parent_scope = partial_die_parent_scope (pdi, cu);
8448 if (parent_scope == NULL)
8449 return NULL;
8450 else
43816ebc 8451 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8452 pdi->name (cu),
8453 0, cu));
c906108c
SS
8454}
8455
8456static void
72bf9492 8457add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8458{
976ca316
SM
8459 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8460 struct objfile *objfile = per_objfile->objfile;
08feed99 8461 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8462 CORE_ADDR addr = 0;
15d034d0 8463 const char *actual_name = NULL;
e142c38c
DJ
8464 CORE_ADDR baseaddr;
8465
b3b3bada 8466 baseaddr = objfile->text_section_offset ();
c906108c 8467
43816ebc
TT
8468 gdb::unique_xmalloc_ptr<char> built_actual_name
8469 = partial_die_full_name (pdi, cu);
15d034d0 8470 if (built_actual_name != NULL)
43816ebc 8471 actual_name = built_actual_name.get ();
63d06c5c 8472
72bf9492 8473 if (actual_name == NULL)
7d00ffec 8474 actual_name = pdi->name (cu);
72bf9492 8475
76e288d1
TT
8476 partial_symbol psymbol;
8477 memset (&psymbol, 0, sizeof (psymbol));
8478 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8479 psymbol.ginfo.section = -1;
8480
8481 /* The code below indicates that the psymbol should be installed by
8482 setting this. */
8483 gdb::optional<psymbol_placement> where;
8484
c906108c
SS
8485 switch (pdi->tag)
8486 {
b1dc1806 8487 case DW_TAG_inlined_subroutine:
c906108c 8488 case DW_TAG_subprogram:
79748972
TT
8489 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8490 - baseaddr);
0a4b0913
AB
8491 if (pdi->is_external
8492 || cu->language == language_ada
8493 || (cu->language == language_fortran
8494 && pdi->die_parent != NULL
8495 && pdi->die_parent->tag == DW_TAG_subprogram))
8496 {
8497 /* Normally, only "external" DIEs are part of the global scope.
8498 But in Ada and Fortran, we want to be able to access nested
8499 procedures globally. So all Ada and Fortran subprograms are
8500 stored in the global scope. */
76e288d1 8501 where = psymbol_placement::GLOBAL;
c906108c
SS
8502 }
8503 else
76e288d1
TT
8504 where = psymbol_placement::STATIC;
8505
8506 psymbol.domain = VAR_DOMAIN;
8507 psymbol.aclass = LOC_BLOCK;
8508 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8509 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8510
8511 if (pdi->main_subprogram && actual_name != NULL)
8512 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8513 break;
72929c62 8514 case DW_TAG_constant:
76e288d1
TT
8515 psymbol.domain = VAR_DOMAIN;
8516 psymbol.aclass = LOC_STATIC;
8517 where = (pdi->is_external
8518 ? psymbol_placement::GLOBAL
8519 : psymbol_placement::STATIC);
72929c62 8520 break;
c906108c 8521 case DW_TAG_variable:
95554aad
TT
8522 if (pdi->d.locdesc)
8523 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8524
95554aad 8525 if (pdi->d.locdesc
caac4577 8526 && addr == 0
976ca316 8527 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8528 {
8529 /* A global or static variable may also have been stripped
8530 out by the linker if unused, in which case its address
8531 will be nullified; do not add such variables into partial
8532 symbol table then. */
8533 }
8534 else if (pdi->is_external)
c906108c
SS
8535 {
8536 /* Global Variable.
8537 Don't enter into the minimal symbol tables as there is
8538 a minimal symbol table entry from the ELF symbols already.
8539 Enter into partial symbol table if it has a location
8540 descriptor or a type.
8541 If the location descriptor is missing, new_symbol will create
8542 a LOC_UNRESOLVED symbol, the address of the variable will then
8543 be determined from the minimal symbol table whenever the variable
8544 is referenced.
8545 The address for the partial symbol table entry is not
8546 used by GDB, but it comes in handy for debugging partial symbol
8547 table building. */
8548
95554aad 8549 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8550 {
8551 psymbol.domain = VAR_DOMAIN;
8552 psymbol.aclass = LOC_STATIC;
8553 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8554 psymbol.ginfo.value.address = addr;
8555 where = psymbol_placement::GLOBAL;
8556 }
c906108c
SS
8557 }
8558 else
8559 {
ff908ebf
AW
8560 int has_loc = pdi->d.locdesc != NULL;
8561
8562 /* Static Variable. Skip symbols whose value we cannot know (those
8563 without location descriptors or constant values). */
8564 if (!has_loc && !pdi->has_const_value)
43816ebc 8565 return;
ff908ebf 8566
76e288d1
TT
8567 psymbol.domain = VAR_DOMAIN;
8568 psymbol.aclass = LOC_STATIC;
8569 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8570 if (has_loc)
8571 psymbol.ginfo.value.address = addr;
8572 where = psymbol_placement::STATIC;
c906108c
SS
8573 }
8574 break;
8575 case DW_TAG_typedef:
8576 case DW_TAG_base_type:
a02abb62 8577 case DW_TAG_subrange_type:
76e288d1
TT
8578 psymbol.domain = VAR_DOMAIN;
8579 psymbol.aclass = LOC_TYPEDEF;
8580 where = psymbol_placement::STATIC;
c906108c 8581 break;
74921315 8582 case DW_TAG_imported_declaration:
72bf9492 8583 case DW_TAG_namespace:
76e288d1
TT
8584 psymbol.domain = VAR_DOMAIN;
8585 psymbol.aclass = LOC_TYPEDEF;
8586 where = psymbol_placement::GLOBAL;
72bf9492 8587 break;
530e8392 8588 case DW_TAG_module:
a5fd13a9
BH
8589 /* With Fortran 77 there might be a "BLOCK DATA" module
8590 available without any name. If so, we skip the module as it
8591 doesn't bring any value. */
8592 if (actual_name != nullptr)
76e288d1
TT
8593 {
8594 psymbol.domain = MODULE_DOMAIN;
8595 psymbol.aclass = LOC_TYPEDEF;
8596 where = psymbol_placement::GLOBAL;
8597 }
530e8392 8598 break;
c906108c 8599 case DW_TAG_class_type:
680b30c7 8600 case DW_TAG_interface_type:
c906108c
SS
8601 case DW_TAG_structure_type:
8602 case DW_TAG_union_type:
8603 case DW_TAG_enumeration_type:
fa4028e9
JB
8604 /* Skip external references. The DWARF standard says in the section
8605 about "Structure, Union, and Class Type Entries": "An incomplete
8606 structure, union or class type is represented by a structure,
8607 union or class entry that does not have a byte size attribute
8608 and that has a DW_AT_declaration attribute." */
8609 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8610 return;
fa4028e9 8611
63d06c5c
DC
8612 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8613 static vs. global. */
76e288d1
TT
8614 psymbol.domain = STRUCT_DOMAIN;
8615 psymbol.aclass = LOC_TYPEDEF;
8616 where = (cu->language == language_cplus
8617 ? psymbol_placement::GLOBAL
8618 : psymbol_placement::STATIC);
c906108c
SS
8619 break;
8620 case DW_TAG_enumerator:
76e288d1
TT
8621 psymbol.domain = VAR_DOMAIN;
8622 psymbol.aclass = LOC_CONST;
8623 where = (cu->language == language_cplus
8624 ? psymbol_placement::GLOBAL
8625 : psymbol_placement::STATIC);
c906108c
SS
8626 break;
8627 default:
8628 break;
8629 }
76e288d1
TT
8630
8631 if (where.has_value ())
8632 {
f049a313
TT
8633 if (built_actual_name != nullptr)
8634 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8635 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8636 psymbol.ginfo.set_linkage_name (actual_name);
8637 else
8638 {
8639 psymbol.ginfo.set_demangled_name (actual_name,
8640 &objfile->objfile_obstack);
8641 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8642 }
76e288d1
TT
8643 add_psymbol_to_list (psymbol, *where, objfile);
8644 }
c906108c
SS
8645}
8646
5c4e30ca
DC
8647/* Read a partial die corresponding to a namespace; also, add a symbol
8648 corresponding to that namespace to the symbol table. NAMESPACE is
8649 the name of the enclosing namespace. */
91c24f0a 8650
72bf9492
DJ
8651static void
8652add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8653 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8654 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8655{
72bf9492 8656 /* Add a symbol for the namespace. */
e7c27a73 8657
72bf9492 8658 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8659
8660 /* Now scan partial symbols in that namespace. */
8661
91c24f0a 8662 if (pdi->has_children)
cdc07690 8663 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8664}
8665
5d7cb8df
JK
8666/* Read a partial die corresponding to a Fortran module. */
8667
8668static void
8669add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8670 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8671{
530e8392
KB
8672 /* Add a symbol for the namespace. */
8673
8674 add_partial_symbol (pdi, cu);
8675
f55ee35c 8676 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8677
8678 if (pdi->has_children)
cdc07690 8679 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8680}
8681
b1dc1806
XR
8682/* Read a partial die corresponding to a subprogram or an inlined
8683 subprogram and create a partial symbol for that subprogram.
8684 When the CU language allows it, this routine also defines a partial
8685 symbol for each nested subprogram that this subprogram contains.
8686 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8687 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8688
cdc07690
YQ
8689 PDI may also be a lexical block, in which case we simply search
8690 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8691 Again, this is only performed when the CU language allows this
8692 type of definitions. */
8693
8694static void
8695add_partial_subprogram (struct partial_die_info *pdi,
8696 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8697 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8698{
b1dc1806 8699 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8700 {
8701 if (pdi->has_pc_info)
8702 {
8703 if (pdi->lowpc < *lowpc)
8704 *lowpc = pdi->lowpc;
8705 if (pdi->highpc > *highpc)
8706 *highpc = pdi->highpc;
cdc07690 8707 if (set_addrmap)
5734ee8b 8708 {
5e22e966 8709 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8710 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8711 CORE_ADDR baseaddr;
b926417a
TT
8712 CORE_ADDR this_highpc;
8713 CORE_ADDR this_lowpc;
5734ee8b 8714
b3b3bada 8715 baseaddr = objfile->text_section_offset ();
b926417a
TT
8716 this_lowpc
8717 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8718 pdi->lowpc + baseaddr)
8719 - baseaddr);
8720 this_highpc
8721 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8722 pdi->highpc + baseaddr)
8723 - baseaddr);
d320c2b5 8724 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8725 this_lowpc, this_highpc - 1,
9291a0cd 8726 cu->per_cu->v.psymtab);
5734ee8b 8727 }
481860b3
GB
8728 }
8729
8730 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8731 {
bc30ff58 8732 if (!pdi->is_declaration)
e8d05480
JB
8733 /* Ignore subprogram DIEs that do not have a name, they are
8734 illegal. Do not emit a complaint at this point, we will
8735 do so when we convert this psymtab into a symtab. */
7d00ffec 8736 if (pdi->name (cu))
e8d05480 8737 add_partial_symbol (pdi, cu);
bc30ff58
JB
8738 }
8739 }
6e70227d 8740
bc30ff58
JB
8741 if (! pdi->has_children)
8742 return;
8743
0a4b0913 8744 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8745 {
8746 pdi = pdi->die_child;
8747 while (pdi != NULL)
8748 {
52356b79 8749 pdi->fixup (cu);
bc30ff58 8750 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8751 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8752 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8753 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8754 pdi = pdi->die_sibling;
8755 }
8756 }
8757}
8758
91c24f0a
DC
8759/* Read a partial die corresponding to an enumeration type. */
8760
72bf9492
DJ
8761static void
8762add_partial_enumeration (struct partial_die_info *enum_pdi,
8763 struct dwarf2_cu *cu)
91c24f0a 8764{
72bf9492 8765 struct partial_die_info *pdi;
91c24f0a 8766
7d00ffec 8767 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8768 add_partial_symbol (enum_pdi, cu);
8769
8770 pdi = enum_pdi->die_child;
8771 while (pdi)
91c24f0a 8772 {
7d00ffec 8773 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8774 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8775 else
72bf9492
DJ
8776 add_partial_symbol (pdi, cu);
8777 pdi = pdi->die_sibling;
91c24f0a 8778 }
91c24f0a
DC
8779}
8780
6caca83c
CC
8781/* Return the initial uleb128 in the die at INFO_PTR. */
8782
8783static unsigned int
d521ce57 8784peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8785{
8786 unsigned int bytes_read;
8787
8788 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8789}
8790
685af9cd
TT
8791/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8792 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8793
4bb7a0a7
DJ
8794 Return the corresponding abbrev, or NULL if the number is zero (indicating
8795 an empty DIE). In either case *BYTES_READ will be set to the length of
8796 the initial number. */
8797
8798static struct abbrev_info *
685af9cd
TT
8799peek_die_abbrev (const die_reader_specs &reader,
8800 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8801{
685af9cd 8802 dwarf2_cu *cu = reader.cu;
5e22e966 8803 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8804 unsigned int abbrev_number
8805 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8806
8807 if (abbrev_number == 0)
8808 return NULL;
8809
685af9cd 8810 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8811 if (!abbrev)
8812 {
422b9917 8813 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8814 " at offset %s [in module %s]"),
422b9917 8815 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8816 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8817 }
8818
8819 return abbrev;
8820}
8821
93311388
DE
8822/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8823 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8824 DIE. Any children of the skipped DIEs will also be skipped. */
8825
d521ce57
TT
8826static const gdb_byte *
8827skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8828{
4bb7a0a7
DJ
8829 while (1)
8830 {
685af9cd
TT
8831 unsigned int bytes_read;
8832 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8833
4bb7a0a7
DJ
8834 if (abbrev == NULL)
8835 return info_ptr + bytes_read;
8836 else
dee91e82 8837 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8838 }
8839}
8840
93311388
DE
8841/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8842 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8843 abbrev corresponding to that skipped uleb128 should be passed in
8844 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8845 children. */
8846
d521ce57
TT
8847static const gdb_byte *
8848skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8849 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8850{
8851 unsigned int bytes_read;
8852 struct attribute attr;
dee91e82
DE
8853 bfd *abfd = reader->abfd;
8854 struct dwarf2_cu *cu = reader->cu;
d521ce57 8855 const gdb_byte *buffer = reader->buffer;
f664829e 8856 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8857 unsigned int form, i;
8858
8859 for (i = 0; i < abbrev->num_attrs; i++)
8860 {
8861 /* The only abbrev we care about is DW_AT_sibling. */
8862 if (abbrev->attrs[i].name == DW_AT_sibling)
8863 {
7a5f294d 8864 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8865 if (attr.form == DW_FORM_ref_addr)
b98664d3 8866 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8867 else
b9502d3f 8868 {
0826b30a 8869 sect_offset off = attr.get_ref_die_offset ();
9c541725 8870 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8871
8872 if (sibling_ptr < info_ptr)
b98664d3 8873 complaint (_("DW_AT_sibling points backwards"));
22869d73 8874 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8875 reader->die_section->overflow_complaint ();
b9502d3f
WN
8876 else
8877 return sibling_ptr;
8878 }
4bb7a0a7
DJ
8879 }
8880
8881 /* If it isn't DW_AT_sibling, skip this attribute. */
8882 form = abbrev->attrs[i].form;
8883 skip_attribute:
8884 switch (form)
8885 {
4bb7a0a7 8886 case DW_FORM_ref_addr:
ae411497
TT
8887 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8888 and later it is offset sized. */
8889 if (cu->header.version == 2)
8890 info_ptr += cu->header.addr_size;
8891 else
8892 info_ptr += cu->header.offset_size;
8893 break;
36586728
TT
8894 case DW_FORM_GNU_ref_alt:
8895 info_ptr += cu->header.offset_size;
8896 break;
ae411497 8897 case DW_FORM_addr:
4bb7a0a7
DJ
8898 info_ptr += cu->header.addr_size;
8899 break;
8900 case DW_FORM_data1:
8901 case DW_FORM_ref1:
8902 case DW_FORM_flag:
8fe0f950 8903 case DW_FORM_strx1:
4bb7a0a7
DJ
8904 info_ptr += 1;
8905 break;
2dc7f7b3 8906 case DW_FORM_flag_present:
43988095 8907 case DW_FORM_implicit_const:
2dc7f7b3 8908 break;
4bb7a0a7
DJ
8909 case DW_FORM_data2:
8910 case DW_FORM_ref2:
8fe0f950 8911 case DW_FORM_strx2:
4bb7a0a7
DJ
8912 info_ptr += 2;
8913 break;
8fe0f950
AT
8914 case DW_FORM_strx3:
8915 info_ptr += 3;
8916 break;
4bb7a0a7
DJ
8917 case DW_FORM_data4:
8918 case DW_FORM_ref4:
8fe0f950 8919 case DW_FORM_strx4:
4bb7a0a7
DJ
8920 info_ptr += 4;
8921 break;
8922 case DW_FORM_data8:
8923 case DW_FORM_ref8:
55f1336d 8924 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8925 info_ptr += 8;
8926 break;
0224619f
JK
8927 case DW_FORM_data16:
8928 info_ptr += 16;
8929 break;
4bb7a0a7 8930 case DW_FORM_string:
9b1c24c8 8931 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8932 info_ptr += bytes_read;
8933 break;
2dc7f7b3 8934 case DW_FORM_sec_offset:
4bb7a0a7 8935 case DW_FORM_strp:
36586728 8936 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8937 info_ptr += cu->header.offset_size;
8938 break;
2dc7f7b3 8939 case DW_FORM_exprloc:
4bb7a0a7
DJ
8940 case DW_FORM_block:
8941 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8942 info_ptr += bytes_read;
8943 break;
8944 case DW_FORM_block1:
8945 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8946 break;
8947 case DW_FORM_block2:
8948 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8949 break;
8950 case DW_FORM_block4:
8951 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8952 break;
336d760d 8953 case DW_FORM_addrx:
cf532bd1 8954 case DW_FORM_strx:
4bb7a0a7
DJ
8955 case DW_FORM_sdata:
8956 case DW_FORM_udata:
8957 case DW_FORM_ref_udata:
3019eac3
DE
8958 case DW_FORM_GNU_addr_index:
8959 case DW_FORM_GNU_str_index:
18a8505e 8960 case DW_FORM_rnglistx:
41144253 8961 case DW_FORM_loclistx:
d521ce57 8962 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8963 break;
8964 case DW_FORM_indirect:
8965 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8966 info_ptr += bytes_read;
8967 /* We need to continue parsing from here, so just go back to
8968 the top. */
8969 goto skip_attribute;
8970
8971 default:
3e43a32a
MS
8972 error (_("Dwarf Error: Cannot handle %s "
8973 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8974 dwarf_form_name (form),
8975 bfd_get_filename (abfd));
8976 }
8977 }
8978
8979 if (abbrev->has_children)
dee91e82 8980 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8981 else
8982 return info_ptr;
8983}
8984
93311388 8985/* Locate ORIG_PDI's sibling.
dee91e82 8986 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8987
d521ce57 8988static const gdb_byte *
dee91e82
DE
8989locate_pdi_sibling (const struct die_reader_specs *reader,
8990 struct partial_die_info *orig_pdi,
d521ce57 8991 const gdb_byte *info_ptr)
91c24f0a
DC
8992{
8993 /* Do we know the sibling already? */
72bf9492 8994
91c24f0a
DC
8995 if (orig_pdi->sibling)
8996 return orig_pdi->sibling;
8997
8998 /* Are there any children to deal with? */
8999
9000 if (!orig_pdi->has_children)
9001 return info_ptr;
9002
4bb7a0a7 9003 /* Skip the children the long way. */
91c24f0a 9004
dee91e82 9005 return skip_children (reader, info_ptr);
91c24f0a
DC
9006}
9007
257e7a09 9008/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9009 not NULL. */
c906108c 9010
891813be
TT
9011void
9012dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9013{
976ca316 9014 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9015
976ca316 9016 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9017
077cbab2
TT
9018 /* If this psymtab is constructed from a debug-only objfile, the
9019 has_section_at_zero flag will not necessarily be correct. We
9020 can get the correct value for this flag by looking at the data
9021 associated with the (presumably stripped) associated objfile. */
9022 if (objfile->separate_debug_objfile_backlink)
c906108c 9023 {
976ca316 9024 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9025 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9026
976ca316
SM
9027 per_objfile->per_bfd->has_section_at_zero
9028 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9029 }
98bfdba5 9030
8566b89b 9031 expand_psymtab (objfile);
95554aad 9032
976ca316 9033 process_cu_includes (per_objfile);
c906108c 9034}
9cdd5dbd
DE
9035\f
9036/* Reading in full CUs. */
c906108c 9037
10b3939b
DJ
9038/* Add PER_CU to the queue. */
9039
9040static void
120ce1b5
SM
9041queue_comp_unit (dwarf2_per_cu_data *per_cu,
9042 dwarf2_per_objfile *per_objfile,
95554aad 9043 enum language pretend_language)
10b3939b 9044{
10b3939b 9045 per_cu->queued = 1;
120ce1b5 9046 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9047}
9048
89e63ee4
DE
9049/* If PER_CU is not yet queued, add it to the queue.
9050 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9051 dependency.
0907af0c 9052 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9053 meaning either PER_CU is already queued or it is already loaded.
9054
9055 N.B. There is an invariant here that if a CU is queued then it is loaded.
9056 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9057
9058static int
89e63ee4 9059maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9060 dwarf2_per_cu_data *per_cu,
9061 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9062 enum language pretend_language)
9063{
9064 /* We may arrive here during partial symbol reading, if we need full
9065 DIEs to process an unusual case (e.g. template arguments). Do
9066 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9067 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9068 {
7188ed02
SM
9069 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9070
9071 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9072 return 1;
9073 return 0;
9074 }
9075
9076 /* Mark the dependence relation so that we don't flush PER_CU
9077 too early. */
89e63ee4
DE
9078 if (dependent_cu != NULL)
9079 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9080
9081 /* If it's already on the queue, we have nothing to do. */
9082 if (per_cu->queued)
9083 return 0;
9084
9085 /* If the compilation unit is already loaded, just mark it as
9086 used. */
7188ed02
SM
9087 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9088 if (cu != nullptr)
0907af0c 9089 {
7188ed02 9090 cu->last_used = 0;
0907af0c
DE
9091 return 0;
9092 }
9093
9094 /* Add it to the queue. */
120ce1b5 9095 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9096
9097 return 1;
9098}
9099
10b3939b
DJ
9100/* Process the queue. */
9101
9102static void
976ca316 9103process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9104{
b4f54984 9105 if (dwarf_read_debug)
45cfd468
DE
9106 {
9107 fprintf_unfiltered (gdb_stdlog,
9108 "Expanding one or more symtabs of objfile %s ...\n",
976ca316 9109 objfile_name (per_objfile->objfile));
45cfd468
DE
9110 }
9111
03dd20cc
DJ
9112 /* The queue starts out with one item, but following a DIE reference
9113 may load a new CU, adding it to the end of the queue. */
976ca316 9114 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9115 {
976ca316 9116 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9117 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9118
976ca316 9119 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9120 {
976ca316 9121 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9122
7188ed02
SM
9123 /* Skip dummy CUs. */
9124 if (cu != nullptr)
73be47f5 9125 {
7188ed02
SM
9126 unsigned int debug_print_threshold;
9127 char buf[100];
9128
9129 if (per_cu->is_debug_types)
9130 {
9131 struct signatured_type *sig_type =
9132 (struct signatured_type *) per_cu;
9133
9134 sprintf (buf, "TU %s at offset %s",
9135 hex_string (sig_type->signature),
9136 sect_offset_str (per_cu->sect_off));
9137 /* There can be 100s of TUs.
9138 Only print them in verbose mode. */
9139 debug_print_threshold = 2;
9140 }
9141 else
9142 {
9143 sprintf (buf, "CU at offset %s",
9144 sect_offset_str (per_cu->sect_off));
9145 debug_print_threshold = 1;
9146 }
247f5c4f 9147
7188ed02
SM
9148 if (dwarf_read_debug >= debug_print_threshold)
9149 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9150
7188ed02
SM
9151 if (per_cu->is_debug_types)
9152 process_full_type_unit (cu, item.pretend_language);
9153 else
9154 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9155
7188ed02
SM
9156 if (dwarf_read_debug >= debug_print_threshold)
9157 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9158 }
f4dc4d17 9159 }
10b3939b 9160
7188ed02 9161 per_cu->queued = 0;
976ca316 9162 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9163 }
9164
b4f54984 9165 if (dwarf_read_debug)
45cfd468
DE
9166 {
9167 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
976ca316 9168 objfile_name (per_objfile->objfile));
45cfd468 9169 }
10b3939b
DJ
9170}
9171
10b3939b
DJ
9172/* Read in full symbols for PST, and anything it depends on. */
9173
8566b89b
TT
9174void
9175dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9176{
af758d11 9177 gdb_assert (!readin_p (objfile));
95554aad 9178
17ee85fc
TT
9179 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9180 free_cached_comp_units freer (per_objfile);
48993951 9181 expand_dependencies (objfile);
aaa75496 9182
97a1449a 9183 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9184 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9185}
9186
af758d11
SM
9187/* See psympriv.h. */
9188
9189bool
9190dwarf2_psymtab::readin_p (struct objfile *objfile) const
9191{
9192 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9193 return per_objfile->symtab_set_p (per_cu_data);
9194}
9195
9196/* See psympriv.h. */
9197
9198compunit_symtab *
9199dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9200{
9201 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9202 return per_objfile->get_symtab (per_cu_data);
9203}
9204
dee91e82
DE
9205/* Trivial hash function for die_info: the hash value of a DIE
9206 is its offset in .debug_info for this objfile. */
10b3939b 9207
dee91e82
DE
9208static hashval_t
9209die_hash (const void *item)
10b3939b 9210{
9a3c8263 9211 const struct die_info *die = (const struct die_info *) item;
6502dd73 9212
9c541725 9213 return to_underlying (die->sect_off);
dee91e82 9214}
63d06c5c 9215
dee91e82
DE
9216/* Trivial comparison function for die_info structures: two DIEs
9217 are equal if they have the same offset. */
98bfdba5 9218
dee91e82
DE
9219static int
9220die_eq (const void *item_lhs, const void *item_rhs)
9221{
9a3c8263
SM
9222 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9223 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9224
9c541725 9225 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9226}
c906108c 9227
c0ab21c2 9228/* Load the DIEs associated with PER_CU into memory. */
c906108c 9229
dee91e82 9230static void
ab432490
SM
9231load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9232 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9233 bool skip_partial,
9234 enum language pretend_language)
dee91e82 9235{
c0ab21c2
TT
9236 gdb_assert (! this_cu->is_debug_types);
9237
7188ed02
SM
9238 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9239 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9240 if (reader.dummy_p)
9241 return;
9242
9243 struct dwarf2_cu *cu = reader.cu;
9244 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9245
dee91e82
DE
9246 gdb_assert (cu->die_hash == NULL);
9247 cu->die_hash =
9248 htab_create_alloc_ex (cu->header.length / 12,
9249 die_hash,
9250 die_eq,
9251 NULL,
9252 &cu->comp_unit_obstack,
9253 hashtab_obstack_allocate,
9254 dummy_obstack_deallocate);
e142c38c 9255
3e225074 9256 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9257 reader.comp_unit_die->child
9258 = read_die_and_siblings (&reader, reader.info_ptr,
9259 &info_ptr, reader.comp_unit_die);
9260 cu->dies = reader.comp_unit_die;
dee91e82 9261 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9262
9263 /* We try not to read any attributes in this function, because not
9cdd5dbd 9264 all CUs needed for references have been loaded yet, and symbol
10b3939b 9265 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9266 or we won't be able to build types correctly.
9267 Similarly, if we do not read the producer, we can not apply
9268 producer-specific interpretation. */
c0ab21c2 9269 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9270
9271 reader.keep ();
10b3939b
DJ
9272}
9273
3da10d80
KS
9274/* Add a DIE to the delayed physname list. */
9275
9276static void
9277add_to_method_list (struct type *type, int fnfield_index, int index,
9278 const char *name, struct die_info *die,
9279 struct dwarf2_cu *cu)
9280{
9281 struct delayed_method_info mi;
9282 mi.type = type;
9283 mi.fnfield_index = fnfield_index;
9284 mi.index = index;
9285 mi.name = name;
9286 mi.die = die;
c89b44cd 9287 cu->method_list.push_back (mi);
3da10d80
KS
9288}
9289
3693fdb3
PA
9290/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9291 "const" / "volatile". If so, decrements LEN by the length of the
9292 modifier and return true. Otherwise return false. */
9293
9294template<size_t N>
9295static bool
9296check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9297{
9298 size_t mod_len = sizeof (mod) - 1;
9299 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9300 {
9301 len -= mod_len;
9302 return true;
9303 }
9304 return false;
9305}
9306
3da10d80
KS
9307/* Compute the physnames of any methods on the CU's method list.
9308
9309 The computation of method physnames is delayed in order to avoid the
9310 (bad) condition that one of the method's formal parameters is of an as yet
9311 incomplete type. */
9312
9313static void
9314compute_delayed_physnames (struct dwarf2_cu *cu)
9315{
3693fdb3 9316 /* Only C++ delays computing physnames. */
c89b44cd 9317 if (cu->method_list.empty ())
3693fdb3
PA
9318 return;
9319 gdb_assert (cu->language == language_cplus);
9320
52941706 9321 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9322 {
1d06ead6 9323 const char *physname;
3da10d80 9324 struct fn_fieldlist *fn_flp
c89b44cd
TT
9325 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9326 physname = dwarf2_physname (mi.name, mi.die, cu);
9327 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9328 = physname ? physname : "";
3693fdb3
PA
9329
9330 /* Since there's no tag to indicate whether a method is a
9331 const/volatile overload, extract that information out of the
9332 demangled name. */
9333 if (physname != NULL)
9334 {
9335 size_t len = strlen (physname);
9336
9337 while (1)
9338 {
9339 if (physname[len] == ')') /* shortcut */
9340 break;
9341 else if (check_modifier (physname, len, " const"))
c89b44cd 9342 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9343 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9344 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9345 else
9346 break;
9347 }
9348 }
3da10d80 9349 }
c89b44cd
TT
9350
9351 /* The list is no longer needed. */
9352 cu->method_list.clear ();
3da10d80
KS
9353}
9354
a766d390
DE
9355/* Go objects should be embedded in a DW_TAG_module DIE,
9356 and it's not clear if/how imported objects will appear.
9357 To keep Go support simple until that's worked out,
9358 go back through what we've read and create something usable.
9359 We could do this while processing each DIE, and feels kinda cleaner,
9360 but that way is more invasive.
9361 This is to, for example, allow the user to type "p var" or "b main"
9362 without having to specify the package name, and allow lookups
9363 of module.object to work in contexts that use the expression
9364 parser. */
9365
9366static void
9367fixup_go_packaging (struct dwarf2_cu *cu)
9368{
421d1616 9369 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9370 struct pending *list;
9371 int i;
9372
c24bdb02 9373 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9374 list != NULL;
9375 list = list->next)
a766d390
DE
9376 {
9377 for (i = 0; i < list->nsyms; ++i)
9378 {
9379 struct symbol *sym = list->symbol[i];
9380
c1b5c1eb 9381 if (sym->language () == language_go
a766d390
DE
9382 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9383 {
421d1616
TT
9384 gdb::unique_xmalloc_ptr<char> this_package_name
9385 (go_symbol_package_name (sym));
a766d390
DE
9386
9387 if (this_package_name == NULL)
9388 continue;
9389 if (package_name == NULL)
421d1616 9390 package_name = std::move (this_package_name);
a766d390
DE
9391 else
9392 {
5e22e966 9393 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9394 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9395 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9396 (symbol_symtab (sym) != NULL
9397 ? symtab_to_filename_for_display
9398 (symbol_symtab (sym))
e3b94546 9399 : objfile_name (objfile)),
421d1616 9400 this_package_name.get (), package_name.get ());
a766d390
DE
9401 }
9402 }
9403 }
9404 }
9405
9406 if (package_name != NULL)
9407 {
5e22e966 9408 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9409 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9410 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9411 saved_package_name);
a766d390
DE
9412 struct symbol *sym;
9413
8c14c3a3 9414 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9415 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9416 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9417 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9418 e.g., "main" finds the "main" module and not C's main(). */
9419 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9420 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9421 SYMBOL_TYPE (sym) = type;
9422
c24bdb02 9423 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9424 }
9425}
9426
c9317f21
TT
9427/* Allocate a fully-qualified name consisting of the two parts on the
9428 obstack. */
9429
9430static const char *
9431rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9432{
9433 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9434}
9435
9c6a1327
TT
9436/* A helper that allocates a variant part to attach to a Rust enum
9437 type. OBSTACK is where the results should be allocated. TYPE is
9438 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9439 discriminant. It must be the index of one of the fields of TYPE,
9440 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9441 DEFAULT_INDEX is the index of the default field; or -1 if there is
9442 no default. RANGES is indexed by "effective" field number (the
9443 field index, but omitting the discriminant and default fields) and
9444 must hold the discriminant values used by the variants. Note that
9445 RANGES must have a lifetime at least as long as OBSTACK -- either
9446 already allocated on it, or static. */
c9317f21 9447
9c6a1327
TT
9448static void
9449alloc_rust_variant (struct obstack *obstack, struct type *type,
9450 int discriminant_index, int default_index,
9451 gdb::array_view<discriminant_range> ranges)
9452{
57d02173
TT
9453 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9454 gdb_assert (discriminant_index == -1
9455 || (discriminant_index >= 0
9456 && discriminant_index < type->num_fields ()));
c9317f21 9457 gdb_assert (default_index == -1
1f704f76 9458 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9459
9c6a1327 9460 /* We have one variant for each non-discriminant field. */
57d02173
TT
9461 int n_variants = type->num_fields ();
9462 if (discriminant_index != -1)
9463 --n_variants;
c9317f21 9464
9c6a1327
TT
9465 variant *variants = new (obstack) variant[n_variants];
9466 int var_idx = 0;
9467 int range_idx = 0;
1f704f76 9468 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9469 {
9470 if (i == discriminant_index)
9471 continue;
c9317f21 9472
9c6a1327
TT
9473 variants[var_idx].first_field = i;
9474 variants[var_idx].last_field = i + 1;
9475
9476 /* The default field does not need a range, but other fields do.
9477 We skipped the discriminant above. */
9478 if (i != default_index)
9479 {
9480 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9481 ++range_idx;
9482 }
c9317f21 9483
9c6a1327
TT
9484 ++var_idx;
9485 }
9486
9487 gdb_assert (range_idx == ranges.size ());
9488 gdb_assert (var_idx == n_variants);
9489
9490 variant_part *part = new (obstack) variant_part;
9491 part->discriminant_index = discriminant_index;
57d02173
TT
9492 /* If there is no discriminant, then whether it is signed is of no
9493 consequence. */
9494 part->is_unsigned
9495 = (discriminant_index == -1
9496 ? false
c6d940a9 9497 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9498 part->variants = gdb::array_view<variant> (variants, n_variants);
9499
9500 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9501 gdb::array_view<variant_part> *prop_value
9502 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9503
9c6a1327 9504 struct dynamic_prop prop;
8c2e4e06 9505 prop.set_variant_parts (prop_value);
9c6a1327 9506
5c54719c 9507 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9508}
9509
9510/* Some versions of rustc emitted enums in an unusual way.
9511
9512 Ordinary enums were emitted as unions. The first element of each
9513 structure in the union was named "RUST$ENUM$DISR". This element
9514 held the discriminant.
9515
9516 These versions of Rust also implemented the "non-zero"
9517 optimization. When the enum had two values, and one is empty and
9518 the other holds a pointer that cannot be zero, the pointer is used
9519 as the discriminant, with a zero value meaning the empty variant.
9520 Here, the union's first member is of the form
9521 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9522 where the fieldnos are the indices of the fields that should be
9523 traversed in order to find the field (which may be several fields deep)
9524 and the variantname is the name of the variant of the case when the
9525 field is zero.
9526
9527 This function recognizes whether TYPE is of one of these forms,
9528 and, if so, smashes it to be a variant type. */
9529
9530static void
9531quirk_rust_enum (struct type *type, struct objfile *objfile)
9532{
78134374 9533 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9534
9535 /* We don't need to deal with empty enums. */
1f704f76 9536 if (type->num_fields () == 0)
c9317f21
TT
9537 return;
9538
9539#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9540 if (type->num_fields () == 1
c9317f21
TT
9541 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9542 {
9543 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9544
9545 /* Decode the field name to find the offset of the
9546 discriminant. */
9547 ULONGEST bit_offset = 0;
940da03e 9548 struct type *field_type = type->field (0).type ();
c9317f21
TT
9549 while (name[0] >= '0' && name[0] <= '9')
9550 {
9551 char *tail;
9552 unsigned long index = strtoul (name, &tail, 10);
9553 name = tail;
9554 if (*name != '$'
1f704f76 9555 || index >= field_type->num_fields ()
c9317f21
TT
9556 || (TYPE_FIELD_LOC_KIND (field_type, index)
9557 != FIELD_LOC_KIND_BITPOS))
9558 {
b98664d3 9559 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9560 "[in module %s]"),
9561 TYPE_FIELD_NAME (type, 0),
9562 objfile_name (objfile));
9563 return;
9564 }
9565 ++name;
9566
9567 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9568 field_type = field_type->field (index).type ();
c9317f21
TT
9569 }
9570
9c6a1327
TT
9571 /* Smash this type to be a structure type. We have to do this
9572 because the type has already been recorded. */
67607e24 9573 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9574 type->set_num_fields (3);
9c6a1327 9575 /* Save the field we care about. */
ceacbf6e 9576 struct field saved_field = type->field (0);
3cabb6b0
SM
9577 type->set_fields
9578 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9579
9c6a1327 9580 /* Put the discriminant at index 0. */
5d14b6e5 9581 type->field (0).set_type (field_type);
9c6a1327
TT
9582 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9583 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9584 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9585
9586 /* The order of fields doesn't really matter, so put the real
9587 field at index 1 and the data-less field at index 2. */
ceacbf6e 9588 type->field (1) = saved_field;
9c6a1327 9589 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9590 = rust_last_path_segment (type->field (1).type ()->name ());
9591 type->field (1).type ()->set_name
7d93a1e0 9592 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9593 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9594
9595 const char *dataless_name
7d93a1e0 9596 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9597 name);
9598 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9599 dataless_name);
5d14b6e5 9600 type->field (2).set_type (dataless_type);
c9317f21
TT
9601 /* NAME points into the original discriminant name, which
9602 already has the correct lifetime. */
9c6a1327 9603 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9604 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9605
9c6a1327
TT
9606 /* Indicate that this is a variant type. */
9607 static discriminant_range ranges[1] = { { 0, 0 } };
9608 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9609 }
77c2dba3
TT
9610 /* A union with a single anonymous field is probably an old-style
9611 univariant enum. */
1f704f76 9612 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9613 {
c9317f21
TT
9614 /* Smash this type to be a structure type. We have to do this
9615 because the type has already been recorded. */
67607e24 9616 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9617
940da03e 9618 struct type *field_type = type->field (0).type ();
c9317f21 9619 const char *variant_name
7d93a1e0 9620 = rust_last_path_segment (field_type->name ());
9c6a1327 9621 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9622 field_type->set_name
9623 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9624 type->name (), variant_name));
57d02173
TT
9625
9626 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9627 }
9628 else
9629 {
9630 struct type *disr_type = nullptr;
1f704f76 9631 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9632 {
940da03e 9633 disr_type = type->field (i).type ();
c9317f21 9634
78134374 9635 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9636 {
9637 /* All fields of a true enum will be structs. */
9638 return;
9639 }
1f704f76 9640 else if (disr_type->num_fields () == 0)
c9317f21
TT
9641 {
9642 /* Could be data-less variant, so keep going. */
a037790e 9643 disr_type = nullptr;
c9317f21
TT
9644 }
9645 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9646 "RUST$ENUM$DISR") != 0)
9647 {
9648 /* Not a Rust enum. */
9649 return;
9650 }
9651 else
9652 {
9653 /* Found one. */
9654 break;
9655 }
9656 }
9657
9658 /* If we got here without a discriminant, then it's probably
9659 just a union. */
9660 if (disr_type == nullptr)
9661 return;
9662
9663 /* Smash this type to be a structure type. We have to do this
9664 because the type has already been recorded. */
67607e24 9665 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9666
9c6a1327 9667 /* Make space for the discriminant field. */
ceacbf6e 9668 struct field *disr_field = &disr_type->field (0);
9c6a1327 9669 field *new_fields
1f704f76 9670 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9671 * sizeof (struct field)));
80fc5e77 9672 memcpy (new_fields + 1, type->fields (),
1f704f76 9673 type->num_fields () * sizeof (struct field));
3cabb6b0 9674 type->set_fields (new_fields);
1f704f76 9675 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9676
9677 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9678 type->field (0) = *disr_field;
9c6a1327
TT
9679 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9680 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9681
9682 /* We need a way to find the correct discriminant given a
9683 variant name. For convenience we build a map here. */
b6cdac4b 9684 struct type *enum_type = disr_field->type ();
c9317f21 9685 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9686 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9687 {
9688 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9689 {
9690 const char *name
9691 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9692 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9693 }
9694 }
9695
1f704f76 9696 int n_fields = type->num_fields ();
9c6a1327
TT
9697 /* We don't need a range entry for the discriminant, but we do
9698 need one for every other field, as there is no default
9699 variant. */
9700 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9701 discriminant_range,
9702 n_fields - 1);
c9317f21
TT
9703 /* Skip the discriminant here. */
9704 for (int i = 1; i < n_fields; ++i)
9705 {
9706 /* Find the final word in the name of this variant's type.
9707 That name can be used to look up the correct
9708 discriminant. */
9709 const char *variant_name
940da03e 9710 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9711
9712 auto iter = discriminant_map.find (variant_name);
9713 if (iter != discriminant_map.end ())
9c6a1327 9714 {
57d02173
TT
9715 ranges[i - 1].low = iter->second;
9716 ranges[i - 1].high = iter->second;
9c6a1327 9717 }
c9317f21 9718
57d02173
TT
9719 /* In Rust, each element should have the size of the
9720 enclosing enum. */
9721 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9722
bedda9ac 9723 /* Remove the discriminant field, if it exists. */
940da03e 9724 struct type *sub_type = type->field (i).type ();
1f704f76 9725 if (sub_type->num_fields () > 0)
bedda9ac 9726 {
5e33d5f4 9727 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9728 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9729 }
9c6a1327 9730 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9731 sub_type->set_name
9732 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9733 type->name (), variant_name));
c9317f21 9734 }
9c6a1327
TT
9735
9736 /* Indicate that this is a variant type. */
a1520ad8 9737 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9738 gdb::array_view<discriminant_range> (ranges,
9739 n_fields - 1));
c9317f21
TT
9740 }
9741}
9742
9743/* Rewrite some Rust unions to be structures with variants parts. */
9744
9745static void
9746rust_union_quirks (struct dwarf2_cu *cu)
9747{
9748 gdb_assert (cu->language == language_rust);
52941706 9749 for (type *type_ : cu->rust_unions)
5e22e966 9750 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9751 /* We don't need this any more. */
9752 cu->rust_unions.clear ();
c9317f21
TT
9753}
9754
8adb8487
TT
9755/* See read.h. */
9756
9757type_unit_group_unshareable *
9758dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9759{
9760 auto iter = this->m_type_units.find (tu_group);
9761 if (iter != this->m_type_units.end ())
9762 return iter->second.get ();
9763
9764 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9765 type_unit_group_unshareable *result = uniq.get ();
9766 this->m_type_units[tu_group] = std::move (uniq);
9767 return result;
9768}
9769
e286671b
TT
9770struct type *
9771dwarf2_per_objfile::get_type_for_signatured_type
9772 (signatured_type *sig_type) const
9773{
9774 auto iter = this->m_type_map.find (sig_type);
9775 if (iter == this->m_type_map.end ())
9776 return nullptr;
9777
9778 return iter->second;
9779}
9780
9781void dwarf2_per_objfile::set_type_for_signatured_type
9782 (signatured_type *sig_type, struct type *type)
9783{
9784 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9785
9786 this->m_type_map[sig_type] = type;
9787}
9788
95554aad
TT
9789/* A helper function for computing the list of all symbol tables
9790 included by PER_CU. */
9791
9792static void
4c39bc03 9793recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9794 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9795 dwarf2_per_cu_data *per_cu,
9796 dwarf2_per_objfile *per_objfile,
43f3e411 9797 struct compunit_symtab *immediate_parent)
95554aad 9798{
af758d11 9799 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9800 if (*slot != NULL)
9801 {
9802 /* This inclusion and its children have been processed. */
9803 return;
9804 }
9805
9806 *slot = per_cu;
af758d11 9807
95554aad 9808 /* Only add a CU if it has a symbol table. */
43182c09 9809 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9810 if (cust != NULL)
ec94af83
DE
9811 {
9812 /* If this is a type unit only add its symbol table if we haven't
9813 seen it yet (type unit per_cu's can share symtabs). */
9814 if (per_cu->is_debug_types)
9815 {
43f3e411 9816 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9817 if (*slot == NULL)
9818 {
43f3e411 9819 *slot = cust;
4c39bc03 9820 result->push_back (cust);
43f3e411
DE
9821 if (cust->user == NULL)
9822 cust->user = immediate_parent;
ec94af83
DE
9823 }
9824 }
9825 else
f9125b6c 9826 {
4c39bc03 9827 result->push_back (cust);
43f3e411
DE
9828 if (cust->user == NULL)
9829 cust->user = immediate_parent;
f9125b6c 9830 }
ec94af83 9831 }
95554aad 9832
ae640021
AB
9833 if (!per_cu->imported_symtabs_empty ())
9834 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9835 {
9836 recursively_compute_inclusions (result, all_children,
43182c09
SM
9837 all_type_symtabs, ptr, per_objfile,
9838 cust);
ae640021 9839 }
95554aad
TT
9840}
9841
43f3e411 9842/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9843 PER_CU. */
9844
9845static void
43182c09
SM
9846compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9847 dwarf2_per_objfile *per_objfile)
95554aad 9848{
f4dc4d17
DE
9849 gdb_assert (! per_cu->is_debug_types);
9850
ae640021 9851 if (!per_cu->imported_symtabs_empty ())
95554aad 9852 {
ae640021 9853 int len;
4c39bc03 9854 std::vector<compunit_symtab *> result_symtabs;
43182c09 9855 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9856
9857 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9858 if (cust == NULL)
95554aad
TT
9859 return;
9860
280a9412
TT
9861 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9862 htab_eq_pointer,
9863 NULL, xcalloc, xfree));
9864 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9865 htab_eq_pointer,
9866 NULL, xcalloc, xfree));
95554aad 9867
ae640021 9868 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9869 {
280a9412
TT
9870 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9871 all_type_symtabs.get (), ptr,
9872 per_objfile, cust);
ec94af83 9873 }
95554aad 9874
ec94af83 9875 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9876 len = result_symtabs.size ();
43f3e411 9877 cust->includes
f6e649dd 9878 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9879 struct compunit_symtab *, len + 1);
4c39bc03
TT
9880 memcpy (cust->includes, result_symtabs.data (),
9881 len * sizeof (compunit_symtab *));
43f3e411 9882 cust->includes[len] = NULL;
95554aad
TT
9883 }
9884}
9885
9886/* Compute the 'includes' field for the symtabs of all the CUs we just
9887 read. */
9888
9889static void
976ca316 9890process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9891{
976ca316 9892 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9893 {
9894 if (! iter->is_debug_types)
976ca316 9895 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9896 }
95554aad 9897
976ca316 9898 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9899}
9900
8fc0b21d 9901/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9902 already been loaded into memory. */
9903
9904static void
8fc0b21d 9905process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9906{
976ca316
SM
9907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9908 struct objfile *objfile = per_objfile->objfile;
08feed99 9909 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9910 CORE_ADDR lowpc, highpc;
43f3e411 9911 struct compunit_symtab *cust;
10b3939b 9912 CORE_ADDR baseaddr;
4359dff1 9913 struct block *static_block;
3e29f34a 9914 CORE_ADDR addr;
10b3939b 9915
b3b3bada 9916 baseaddr = objfile->text_section_offset ();
10b3939b 9917
c89b44cd
TT
9918 /* Clear the list here in case something was left over. */
9919 cu->method_list.clear ();
10b3939b 9920
95554aad
TT
9921 cu->language = pretend_language;
9922 cu->language_defn = language_def (cu->language);
9923
c906108c 9924 /* Do line number decoding in read_file_scope () */
10b3939b 9925 process_die (cu->dies, cu);
c906108c 9926
a766d390
DE
9927 /* For now fudge the Go package. */
9928 if (cu->language == language_go)
9929 fixup_go_packaging (cu);
9930
5f48f8f3 9931 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9932 should be complete, and it should now be safe to compute all of the
9933 physnames. */
9934 compute_delayed_physnames (cu);
3da10d80 9935
c9317f21
TT
9936 if (cu->language == language_rust)
9937 rust_union_quirks (cu);
9938
fae299cd
DC
9939 /* Some compilers don't define a DW_AT_high_pc attribute for the
9940 compilation unit. If the DW_AT_high_pc is missing, synthesize
9941 it, by scanning the DIE's below the compilation unit. */
10b3939b 9942 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9943
3e29f34a 9944 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9945 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9946
9947 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9948 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9949 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9950 addrmap to help ensure it has an accurate map of pc values belonging to
9951 this comp unit. */
9952 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9953
c24bdb02 9954 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9955 SECT_OFF_TEXT (objfile),
9956 0);
c906108c 9957
43f3e411 9958 if (cust != NULL)
c906108c 9959 {
df15bd07 9960 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9961
8be455d7
JK
9962 /* Set symtab language to language from DW_AT_language. If the
9963 compilation is from a C file generated by language preprocessors, do
9964 not set the language if it was already deduced by start_subfile. */
43f3e411 9965 if (!(cu->language == language_c
40e3ad0e 9966 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9967 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9968
9969 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9970 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9971 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9972 there were bugs in prologue debug info, fixed later in GCC-4.5
9973 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9974
9975 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9976 needed, it would be wrong due to missing DW_AT_producer there.
9977
9978 Still one can confuse GDB by using non-standard GCC compilation
9979 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9980 */
ab260dad 9981 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9982 cust->locations_valid = 1;
e0d00bc7
JK
9983
9984 if (gcc_4_minor >= 5)
43f3e411 9985 cust->epilogue_unwind_valid = 1;
96408a79 9986
43f3e411 9987 cust->call_site_htab = cu->call_site_htab;
c906108c 9988 }
9291a0cd 9989
976ca316 9990 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9991
95554aad 9992 /* Push it for inclusion processing later. */
976ca316 9993 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9994
9995 /* Not needed any more. */
c24bdb02 9996 cu->reset_builder ();
f4dc4d17 9997}
45cfd468 9998
8fc0b21d 9999/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10000 already been loaded into memory. */
10001
10002static void
8fc0b21d 10003process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10004 enum language pretend_language)
10005{
976ca316
SM
10006 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10007 struct objfile *objfile = per_objfile->objfile;
43f3e411 10008 struct compunit_symtab *cust;
0186c6a7
DE
10009 struct signatured_type *sig_type;
10010
8fc0b21d
SM
10011 gdb_assert (cu->per_cu->is_debug_types);
10012 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10013
c89b44cd
TT
10014 /* Clear the list here in case something was left over. */
10015 cu->method_list.clear ();
f4dc4d17 10016
f4dc4d17
DE
10017 cu->language = pretend_language;
10018 cu->language_defn = language_def (cu->language);
10019
10020 /* The symbol tables are set up in read_type_unit_scope. */
10021 process_die (cu->dies, cu);
10022
10023 /* For now fudge the Go package. */
10024 if (cu->language == language_go)
10025 fixup_go_packaging (cu);
10026
5f48f8f3 10027 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10028 should be complete, and it should now be safe to compute all of the
10029 physnames. */
10030 compute_delayed_physnames (cu);
f4dc4d17 10031
c9317f21
TT
10032 if (cu->language == language_rust)
10033 rust_union_quirks (cu);
10034
f4dc4d17
DE
10035 /* TUs share symbol tables.
10036 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10037 of it with end_expandable_symtab. Otherwise, complete the addition of
10038 this TU's symbols to the existing symtab. */
8adb8487 10039 type_unit_group_unshareable *tug_unshare =
976ca316 10040 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10041 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10042 {
c24bdb02
KS
10043 buildsym_compunit *builder = cu->get_builder ();
10044 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10045 tug_unshare->compunit_symtab = cust;
f4dc4d17 10046
43f3e411 10047 if (cust != NULL)
f4dc4d17
DE
10048 {
10049 /* Set symtab language to language from DW_AT_language. If the
10050 compilation is from a C file generated by language preprocessors,
10051 do not set the language if it was already deduced by
10052 start_subfile. */
43f3e411
DE
10053 if (!(cu->language == language_c
10054 && COMPUNIT_FILETABS (cust)->language != language_c))
10055 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10056 }
10057 }
10058 else
10059 {
c24bdb02 10060 cu->get_builder ()->augment_type_symtab ();
8adb8487 10061 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10062 }
10063
976ca316 10064 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10065
10066 /* Not needed any more. */
c24bdb02 10067 cu->reset_builder ();
c906108c
SS
10068}
10069
95554aad
TT
10070/* Process an imported unit DIE. */
10071
10072static void
10073process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10074{
10075 struct attribute *attr;
10076
f4dc4d17
DE
10077 /* For now we don't handle imported units in type units. */
10078 if (cu->per_cu->is_debug_types)
10079 {
10080 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10081 " supported in type units [in module %s]"),
5e22e966 10082 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10083 }
10084
95554aad
TT
10085 attr = dwarf2_attr (die, DW_AT_import, cu);
10086 if (attr != NULL)
10087 {
0826b30a 10088 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10089 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10090 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10091 dwarf2_per_cu_data *per_cu
ab432490 10092 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10093
58990295
TV
10094 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10095 into another compilation unit, at root level. Regard this as a hint,
10096 and ignore it. */
10097 if (die->parent && die->parent->parent == NULL
10098 && per_cu->unit_type == DW_UT_compile
10099 && per_cu->lang == language_cplus)
10100 return;
10101
69d751e3 10102 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10103 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 10104 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 10105
ae640021 10106 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10107 }
10108}
10109
4c8aa72d
PA
10110/* RAII object that represents a process_die scope: i.e.,
10111 starts/finishes processing a DIE. */
10112class process_die_scope
adde2bff 10113{
4c8aa72d
PA
10114public:
10115 process_die_scope (die_info *die, dwarf2_cu *cu)
10116 : m_die (die), m_cu (cu)
10117 {
10118 /* We should only be processing DIEs not already in process. */
10119 gdb_assert (!m_die->in_process);
10120 m_die->in_process = true;
10121 }
8c3cb9fa 10122
4c8aa72d
PA
10123 ~process_die_scope ()
10124 {
10125 m_die->in_process = false;
10126
10127 /* If we're done processing the DIE for the CU that owns the line
10128 header, we don't need the line header anymore. */
10129 if (m_cu->line_header_die_owner == m_die)
10130 {
10131 delete m_cu->line_header;
10132 m_cu->line_header = NULL;
10133 m_cu->line_header_die_owner = NULL;
10134 }
10135 }
10136
10137private:
10138 die_info *m_die;
10139 dwarf2_cu *m_cu;
10140};
adde2bff 10141
c906108c
SS
10142/* Process a die and its children. */
10143
10144static void
e7c27a73 10145process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10146{
4c8aa72d 10147 process_die_scope scope (die, cu);
adde2bff 10148
c906108c
SS
10149 switch (die->tag)
10150 {
10151 case DW_TAG_padding:
10152 break;
10153 case DW_TAG_compile_unit:
95554aad 10154 case DW_TAG_partial_unit:
e7c27a73 10155 read_file_scope (die, cu);
c906108c 10156 break;
348e048f
DE
10157 case DW_TAG_type_unit:
10158 read_type_unit_scope (die, cu);
10159 break;
c906108c 10160 case DW_TAG_subprogram:
0a4b0913
AB
10161 /* Nested subprograms in Fortran get a prefix. */
10162 if (cu->language == language_fortran
10163 && die->parent != NULL
10164 && die->parent->tag == DW_TAG_subprogram)
10165 cu->processing_has_namespace_info = true;
10166 /* Fall through. */
c906108c 10167 case DW_TAG_inlined_subroutine:
edb3359d 10168 read_func_scope (die, cu);
c906108c
SS
10169 break;
10170 case DW_TAG_lexical_block:
14898363
L
10171 case DW_TAG_try_block:
10172 case DW_TAG_catch_block:
e7c27a73 10173 read_lexical_block_scope (die, cu);
c906108c 10174 break;
216f72a1 10175 case DW_TAG_call_site:
96408a79
SA
10176 case DW_TAG_GNU_call_site:
10177 read_call_site_scope (die, cu);
10178 break;
c906108c 10179 case DW_TAG_class_type:
680b30c7 10180 case DW_TAG_interface_type:
c906108c
SS
10181 case DW_TAG_structure_type:
10182 case DW_TAG_union_type:
134d01f1 10183 process_structure_scope (die, cu);
c906108c
SS
10184 break;
10185 case DW_TAG_enumeration_type:
134d01f1 10186 process_enumeration_scope (die, cu);
c906108c 10187 break;
134d01f1 10188
f792889a
DJ
10189 /* These dies have a type, but processing them does not create
10190 a symbol or recurse to process the children. Therefore we can
10191 read them on-demand through read_type_die. */
c906108c 10192 case DW_TAG_subroutine_type:
72019c9c 10193 case DW_TAG_set_type:
c906108c 10194 case DW_TAG_array_type:
c906108c 10195 case DW_TAG_pointer_type:
c906108c 10196 case DW_TAG_ptr_to_member_type:
c906108c 10197 case DW_TAG_reference_type:
4297a3f0 10198 case DW_TAG_rvalue_reference_type:
c906108c 10199 case DW_TAG_string_type:
c906108c 10200 break;
134d01f1 10201
c906108c 10202 case DW_TAG_base_type:
a02abb62 10203 case DW_TAG_subrange_type:
cb249c71 10204 case DW_TAG_typedef:
134d01f1
DJ
10205 /* Add a typedef symbol for the type definition, if it has a
10206 DW_AT_name. */
f792889a 10207 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10208 break;
c906108c 10209 case DW_TAG_common_block:
e7c27a73 10210 read_common_block (die, cu);
c906108c
SS
10211 break;
10212 case DW_TAG_common_inclusion:
10213 break;
d9fa45fe 10214 case DW_TAG_namespace:
9068261f 10215 cu->processing_has_namespace_info = true;
e7c27a73 10216 read_namespace (die, cu);
d9fa45fe 10217 break;
5d7cb8df 10218 case DW_TAG_module:
9068261f 10219 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10220 read_module (die, cu);
10221 break;
d9fa45fe 10222 case DW_TAG_imported_declaration:
9068261f 10223 cu->processing_has_namespace_info = true;
74921315
KS
10224 if (read_namespace_alias (die, cu))
10225 break;
86a73007
TT
10226 /* The declaration is not a global namespace alias. */
10227 /* Fall through. */
d9fa45fe 10228 case DW_TAG_imported_module:
9068261f 10229 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10230 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10231 || cu->language != language_fortran))
b98664d3 10232 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10233 dwarf_tag_name (die->tag));
10234 read_import_statement (die, cu);
d9fa45fe 10235 break;
95554aad
TT
10236
10237 case DW_TAG_imported_unit:
10238 process_imported_unit_die (die, cu);
10239 break;
10240
71a3c369
TT
10241 case DW_TAG_variable:
10242 read_variable (die, cu);
10243 break;
10244
c906108c 10245 default:
e7c27a73 10246 new_symbol (die, NULL, cu);
c906108c
SS
10247 break;
10248 }
10249}
ca69b9e6
DE
10250\f
10251/* DWARF name computation. */
c906108c 10252
94af9270
KS
10253/* A helper function for dwarf2_compute_name which determines whether DIE
10254 needs to have the name of the scope prepended to the name listed in the
10255 die. */
10256
10257static int
10258die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10259{
1c809c68
TT
10260 struct attribute *attr;
10261
94af9270
KS
10262 switch (die->tag)
10263 {
10264 case DW_TAG_namespace:
10265 case DW_TAG_typedef:
10266 case DW_TAG_class_type:
10267 case DW_TAG_interface_type:
10268 case DW_TAG_structure_type:
10269 case DW_TAG_union_type:
10270 case DW_TAG_enumeration_type:
10271 case DW_TAG_enumerator:
10272 case DW_TAG_subprogram:
08a76f8a 10273 case DW_TAG_inlined_subroutine:
94af9270 10274 case DW_TAG_member:
74921315 10275 case DW_TAG_imported_declaration:
94af9270
KS
10276 return 1;
10277
10278 case DW_TAG_variable:
c2b0a229 10279 case DW_TAG_constant:
94af9270
KS
10280 /* We only need to prefix "globally" visible variables. These include
10281 any variable marked with DW_AT_external or any variable that
10282 lives in a namespace. [Variables in anonymous namespaces
10283 require prefixing, but they are not DW_AT_external.] */
10284
10285 if (dwarf2_attr (die, DW_AT_specification, cu))
10286 {
10287 struct dwarf2_cu *spec_cu = cu;
9a619af0 10288
94af9270
KS
10289 return die_needs_namespace (die_specification (die, &spec_cu),
10290 spec_cu);
10291 }
10292
1c809c68 10293 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10294 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10295 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10296 return 0;
10297 /* A variable in a lexical block of some kind does not need a
10298 namespace, even though in C++ such variables may be external
10299 and have a mangled name. */
10300 if (die->parent->tag == DW_TAG_lexical_block
10301 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10302 || die->parent->tag == DW_TAG_catch_block
10303 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10304 return 0;
10305 return 1;
94af9270
KS
10306
10307 default:
10308 return 0;
10309 }
10310}
10311
73b9be8b
KS
10312/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10313 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10314 defined for the given DIE. */
10315
10316static struct attribute *
10317dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10318{
10319 struct attribute *attr;
10320
10321 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10322 if (attr == NULL)
10323 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10324
10325 return attr;
10326}
10327
10328/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10329 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10330 defined for the given DIE. */
10331
10332static const char *
10333dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10334{
10335 const char *linkage_name;
10336
10337 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10338 if (linkage_name == NULL)
10339 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10340
787de330
TT
10341 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10342 See https://github.com/rust-lang/rust/issues/32925. */
10343 if (cu->language == language_rust && linkage_name != NULL
10344 && strchr (linkage_name, '{') != NULL)
10345 linkage_name = NULL;
10346
73b9be8b
KS
10347 return linkage_name;
10348}
10349
94af9270 10350/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10351 compute the physname for the object, which include a method's:
9c37b5ae 10352 - formal parameters (C++),
a766d390 10353 - receiver type (Go),
a766d390
DE
10354
10355 The term "physname" is a bit confusing.
10356 For C++, for example, it is the demangled name.
10357 For Go, for example, it's the mangled name.
94af9270 10358
af6b7be1
JB
10359 For Ada, return the DIE's linkage name rather than the fully qualified
10360 name. PHYSNAME is ignored..
10361
5989a64e 10362 The result is allocated on the objfile->per_bfd's obstack and
45940949 10363 canonicalized. */
94af9270
KS
10364
10365static const char *
15d034d0
TT
10366dwarf2_compute_name (const char *name,
10367 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10368 int physname)
10369{
5e22e966 10370 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10371
94af9270
KS
10372 if (name == NULL)
10373 name = dwarf2_name (die, cu);
10374
2ee7123e
DE
10375 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10376 but otherwise compute it by typename_concat inside GDB.
10377 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10378 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10379 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10380 will set the demangled name to the result of dwarf2_full_name, and it is
10381 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10382 if (cu->language == language_ada
10383 || (cu->language == language_fortran && physname))
10384 {
10385 /* For Ada unit, we prefer the linkage name over the name, as
10386 the former contains the exported name, which the user expects
10387 to be able to reference. Ideally, we want the user to be able
10388 to reference this entity using either natural or linkage name,
10389 but we haven't started looking at this enhancement yet. */
73b9be8b 10390 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10391
2ee7123e
DE
10392 if (linkage_name != NULL)
10393 return linkage_name;
f55ee35c
JK
10394 }
10395
94af9270
KS
10396 /* These are the only languages we know how to qualify names in. */
10397 if (name != NULL
9c37b5ae 10398 && (cu->language == language_cplus
c44af4eb
TT
10399 || cu->language == language_fortran || cu->language == language_d
10400 || cu->language == language_rust))
94af9270
KS
10401 {
10402 if (die_needs_namespace (die, cu))
10403 {
0d5cff50 10404 const char *prefix;
34a68019 10405 const char *canonical_name = NULL;
94af9270 10406
d7e74731
PA
10407 string_file buf;
10408
94af9270 10409 prefix = determine_prefix (die, cu);
94af9270
KS
10410 if (*prefix != '\0')
10411 {
43816ebc
TT
10412 gdb::unique_xmalloc_ptr<char> prefixed_name
10413 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10414
43816ebc 10415 buf.puts (prefixed_name.get ());
94af9270
KS
10416 }
10417 else
d7e74731 10418 buf.puts (name);
94af9270 10419
98bfdba5
PA
10420 /* Template parameters may be specified in the DIE's DW_AT_name, or
10421 as children with DW_TAG_template_type_param or
10422 DW_TAG_value_type_param. If the latter, add them to the name
10423 here. If the name already has template parameters, then
10424 skip this step; some versions of GCC emit both, and
10425 it is more efficient to use the pre-computed name.
10426
10427 Something to keep in mind about this process: it is very
10428 unlikely, or in some cases downright impossible, to produce
10429 something that will match the mangled name of a function.
10430 If the definition of the function has the same debug info,
10431 we should be able to match up with it anyway. But fallbacks
10432 using the minimal symbol, for instance to find a method
10433 implemented in a stripped copy of libstdc++, will not work.
10434 If we do not have debug info for the definition, we will have to
10435 match them up some other way.
10436
10437 When we do name matching there is a related problem with function
10438 templates; two instantiated function templates are allowed to
10439 differ only by their return types, which we do not add here. */
10440
10441 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10442 {
10443 struct attribute *attr;
10444 struct die_info *child;
10445 int first = 1;
10446
10447 die->building_fullname = 1;
10448
10449 for (child = die->child; child != NULL; child = child->sibling)
10450 {
10451 struct type *type;
12df843f 10452 LONGEST value;
d521ce57 10453 const gdb_byte *bytes;
98bfdba5
PA
10454 struct dwarf2_locexpr_baton *baton;
10455 struct value *v;
10456
10457 if (child->tag != DW_TAG_template_type_param
10458 && child->tag != DW_TAG_template_value_param)
10459 continue;
10460
10461 if (first)
10462 {
d7e74731 10463 buf.puts ("<");
98bfdba5
PA
10464 first = 0;
10465 }
10466 else
d7e74731 10467 buf.puts (", ");
98bfdba5
PA
10468
10469 attr = dwarf2_attr (child, DW_AT_type, cu);
10470 if (attr == NULL)
10471 {
b98664d3 10472 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10473 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10474 continue;
10475 }
10476 type = die_type (child, cu);
10477
10478 if (child->tag == DW_TAG_template_type_param)
10479 {
c1ec8cea
TT
10480 c_print_type (type, "", &buf, -1, 0, cu->language,
10481 &type_print_raw_options);
98bfdba5
PA
10482 continue;
10483 }
10484
10485 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10486 if (attr == NULL)
10487 {
b98664d3 10488 complaint (_("template parameter missing "
3e43a32a 10489 "DW_AT_const_value"));
d7e74731 10490 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10491 continue;
10492 }
10493
10494 dwarf2_const_value_attr (attr, type, name,
10495 &cu->comp_unit_obstack, cu,
10496 &value, &bytes, &baton);
10497
20ce4123 10498 if (type->has_no_signedness ())
98bfdba5
PA
10499 /* GDB prints characters as NUMBER 'CHAR'. If that's
10500 changed, this can use value_print instead. */
d7e74731 10501 c_printchar (value, type, &buf);
98bfdba5
PA
10502 else
10503 {
10504 struct value_print_options opts;
10505
10506 if (baton != NULL)
10507 v = dwarf2_evaluate_loc_desc (type, NULL,
10508 baton->data,
10509 baton->size,
9f47c707
SM
10510 baton->per_cu,
10511 baton->per_objfile);
98bfdba5
PA
10512 else if (bytes != NULL)
10513 {
10514 v = allocate_value (type);
10515 memcpy (value_contents_writeable (v), bytes,
10516 TYPE_LENGTH (type));
10517 }
10518 else
10519 v = value_from_longest (type, value);
10520
3e43a32a
MS
10521 /* Specify decimal so that we do not depend on
10522 the radix. */
98bfdba5
PA
10523 get_formatted_print_options (&opts, 'd');
10524 opts.raw = 1;
d7e74731 10525 value_print (v, &buf, &opts);
98bfdba5 10526 release_value (v);
98bfdba5
PA
10527 }
10528 }
10529
10530 die->building_fullname = 0;
10531
10532 if (!first)
10533 {
10534 /* Close the argument list, with a space if necessary
10535 (nested templates). */
d7e74731
PA
10536 if (!buf.empty () && buf.string ().back () == '>')
10537 buf.puts (" >");
98bfdba5 10538 else
d7e74731 10539 buf.puts (">");
98bfdba5
PA
10540 }
10541 }
10542
9c37b5ae 10543 /* For C++ methods, append formal parameter type
94af9270 10544 information, if PHYSNAME. */
6e70227d 10545
94af9270 10546 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10547 && cu->language == language_cplus)
94af9270
KS
10548 {
10549 struct type *type = read_type_die (die, cu);
10550
d7e74731 10551 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10552 &type_print_raw_options);
94af9270 10553
9c37b5ae 10554 if (cu->language == language_cplus)
94af9270 10555 {
60430eff
DJ
10556 /* Assume that an artificial first parameter is
10557 "this", but do not crash if it is not. RealView
10558 marks unnamed (and thus unused) parameters as
10559 artificial; there is no way to differentiate
10560 the two cases. */
1f704f76 10561 if (type->num_fields () > 0
94af9270 10562 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10563 && type->field (0).type ()->code () == TYPE_CODE_PTR
10564 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10565 buf.puts (" const");
94af9270
KS
10566 }
10567 }
10568
d7e74731 10569 const std::string &intermediate_name = buf.string ();
94af9270
KS
10570
10571 if (cu->language == language_cplus)
34a68019 10572 canonical_name
322a8516 10573 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10574 objfile);
34a68019
TT
10575
10576 /* If we only computed INTERMEDIATE_NAME, or if
10577 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10578 intern it. */
322a8516 10579 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10580 name = objfile->intern (intermediate_name);
34a68019
TT
10581 else
10582 name = canonical_name;
94af9270
KS
10583 }
10584 }
10585
10586 return name;
10587}
10588
0114d602
DJ
10589/* Return the fully qualified name of DIE, based on its DW_AT_name.
10590 If scope qualifiers are appropriate they will be added. The result
34a68019 10591 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10592 not have a name. NAME may either be from a previous call to
10593 dwarf2_name or NULL.
10594
9c37b5ae 10595 The output string will be canonicalized (if C++). */
0114d602
DJ
10596
10597static const char *
15d034d0 10598dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10599{
94af9270
KS
10600 return dwarf2_compute_name (name, die, cu, 0);
10601}
0114d602 10602
94af9270
KS
10603/* Construct a physname for the given DIE in CU. NAME may either be
10604 from a previous call to dwarf2_name or NULL. The result will be
10605 allocated on the objfile_objstack or NULL if the DIE does not have a
10606 name.
0114d602 10607
9c37b5ae 10608 The output string will be canonicalized (if C++). */
0114d602 10609
94af9270 10610static const char *
15d034d0 10611dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10612{
5e22e966 10613 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10614 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10615 int need_copy = 1;
10616
10617 /* In this case dwarf2_compute_name is just a shortcut not building anything
10618 on its own. */
10619 if (!die_needs_namespace (die, cu))
10620 return dwarf2_compute_name (name, die, cu, 1);
10621
906bb4c5
TT
10622 if (cu->language != language_rust)
10623 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10624
10625 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10626 has computed. */
791afaa2 10627 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10628 if (mangled != NULL)
900e11f9 10629 {
900e11f9 10630
d3355e4d 10631 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10632 {
10633 /* Do nothing (do not demangle the symbol name). */
10634 }
a766d390
DE
10635 else
10636 {
0eb876f5
JB
10637 /* Use DMGL_RET_DROP for C++ template functions to suppress
10638 their return type. It is easier for GDB users to search
10639 for such functions as `name(params)' than `long name(params)'.
10640 In such case the minimal symbol names do not match the full
10641 symbol names but for template functions there is never a need
10642 to look up their definition from their declaration so
10643 the only disadvantage remains the minimal symbol variant
10644 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10645 demangled.reset (gdb_demangle (mangled,
10646 (DMGL_PARAMS | DMGL_ANSI
10647 | DMGL_RET_DROP)));
a766d390 10648 }
900e11f9 10649 if (demangled)
791afaa2 10650 canon = demangled.get ();
900e11f9
JK
10651 else
10652 {
10653 canon = mangled;
10654 need_copy = 0;
10655 }
10656 }
10657
10658 if (canon == NULL || check_physname)
10659 {
10660 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10661
10662 if (canon != NULL && strcmp (physname, canon) != 0)
10663 {
10664 /* It may not mean a bug in GDB. The compiler could also
10665 compute DW_AT_linkage_name incorrectly. But in such case
10666 GDB would need to be bug-to-bug compatible. */
10667
b98664d3 10668 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10669 "(from linkage <%s>) - DIE at %s [in module %s]"),
10670 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10671 objfile_name (objfile));
900e11f9
JK
10672
10673 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10674 is available here - over computed PHYSNAME. It is safer
10675 against both buggy GDB and buggy compilers. */
10676
10677 retval = canon;
10678 }
10679 else
10680 {
10681 retval = physname;
10682 need_copy = 0;
10683 }
10684 }
10685 else
10686 retval = canon;
10687
10688 if (need_copy)
be1e3d3e 10689 retval = objfile->intern (retval);
900e11f9 10690
900e11f9 10691 return retval;
0114d602
DJ
10692}
10693
74921315
KS
10694/* Inspect DIE in CU for a namespace alias. If one exists, record
10695 a new symbol for it.
10696
10697 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10698
10699static int
10700read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10701{
10702 struct attribute *attr;
10703
10704 /* If the die does not have a name, this is not a namespace
10705 alias. */
10706 attr = dwarf2_attr (die, DW_AT_name, cu);
10707 if (attr != NULL)
10708 {
10709 int num;
10710 struct die_info *d = die;
10711 struct dwarf2_cu *imported_cu = cu;
10712
10713 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10714 keep inspecting DIEs until we hit the underlying import. */
10715#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10716 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10717 {
10718 attr = dwarf2_attr (d, DW_AT_import, cu);
10719 if (attr == NULL)
10720 break;
10721
10722 d = follow_die_ref (d, attr, &imported_cu);
10723 if (d->tag != DW_TAG_imported_declaration)
10724 break;
10725 }
10726
10727 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10728 {
b98664d3 10729 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10730 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10731 return 0;
10732 }
10733
10734 if (attr != NULL)
10735 {
10736 struct type *type;
0826b30a 10737 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10738
aa66c379 10739 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10740 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10741 {
10742 /* This declaration is a global namespace alias. Add
10743 a symbol for it whose type is the aliased namespace. */
10744 new_symbol (die, type, cu);
10745 return 1;
10746 }
10747 }
10748 }
10749
10750 return 0;
10751}
10752
22cee43f 10753/* Return the using directives repository (global or local?) to use in the
804d2729 10754 current context for CU.
22cee43f
PMR
10755
10756 For Ada, imported declarations can materialize renamings, which *may* be
10757 global. However it is impossible (for now?) in DWARF to distinguish
10758 "external" imported declarations and "static" ones. As all imported
10759 declarations seem to be static in all other languages, make them all CU-wide
10760 global only in Ada. */
10761
10762static struct using_direct **
804d2729 10763using_directives (struct dwarf2_cu *cu)
22cee43f 10764{
c24bdb02
KS
10765 if (cu->language == language_ada
10766 && cu->get_builder ()->outermost_context_p ())
10767 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10768 else
c24bdb02 10769 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10770}
10771
27aa8d6a
SW
10772/* Read the import statement specified by the given die and record it. */
10773
10774static void
10775read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10776{
5e22e966 10777 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10778 struct attribute *import_attr;
32019081 10779 struct die_info *imported_die, *child_die;
de4affc9 10780 struct dwarf2_cu *imported_cu;
27aa8d6a 10781 const char *imported_name;
794684b6 10782 const char *imported_name_prefix;
13387711
SW
10783 const char *canonical_name;
10784 const char *import_alias;
10785 const char *imported_declaration = NULL;
794684b6 10786 const char *import_prefix;
eb1e02fd 10787 std::vector<const char *> excludes;
13387711 10788
27aa8d6a
SW
10789 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10790 if (import_attr == NULL)
10791 {
b98664d3 10792 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10793 dwarf_tag_name (die->tag));
10794 return;
10795 }
10796
de4affc9
CC
10797 imported_cu = cu;
10798 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10799 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10800 if (imported_name == NULL)
10801 {
10802 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10803
10804 The import in the following code:
10805 namespace A
10806 {
10807 typedef int B;
10808 }
10809
10810 int main ()
10811 {
10812 using A::B;
10813 B b;
10814 return b;
10815 }
10816
10817 ...
10818 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10819 <52> DW_AT_decl_file : 1
10820 <53> DW_AT_decl_line : 6
10821 <54> DW_AT_import : <0x75>
10822 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10823 <59> DW_AT_name : B
10824 <5b> DW_AT_decl_file : 1
10825 <5c> DW_AT_decl_line : 2
10826 <5d> DW_AT_type : <0x6e>
10827 ...
10828 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10829 <76> DW_AT_byte_size : 4
10830 <77> DW_AT_encoding : 5 (signed)
10831
10832 imports the wrong die ( 0x75 instead of 0x58 ).
10833 This case will be ignored until the gcc bug is fixed. */
10834 return;
10835 }
10836
82856980
SW
10837 /* Figure out the local name after import. */
10838 import_alias = dwarf2_name (die, cu);
27aa8d6a 10839
794684b6
SW
10840 /* Figure out where the statement is being imported to. */
10841 import_prefix = determine_prefix (die, cu);
10842
10843 /* Figure out what the scope of the imported die is and prepend it
10844 to the name of the imported die. */
de4affc9 10845 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10846
f55ee35c
JK
10847 if (imported_die->tag != DW_TAG_namespace
10848 && imported_die->tag != DW_TAG_module)
794684b6 10849 {
13387711
SW
10850 imported_declaration = imported_name;
10851 canonical_name = imported_name_prefix;
794684b6 10852 }
13387711 10853 else if (strlen (imported_name_prefix) > 0)
12aaed36 10854 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10855 imported_name_prefix,
10856 (cu->language == language_d ? "." : "::"),
10857 imported_name, (char *) NULL);
13387711
SW
10858 else
10859 canonical_name = imported_name;
794684b6 10860
32019081
JK
10861 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10862 for (child_die = die->child; child_die && child_die->tag;
436c571c 10863 child_die = child_die->sibling)
32019081
JK
10864 {
10865 /* DWARF-4: A Fortran use statement with a “rename list” may be
10866 represented by an imported module entry with an import attribute
10867 referring to the module and owned entries corresponding to those
10868 entities that are renamed as part of being imported. */
10869
10870 if (child_die->tag != DW_TAG_imported_declaration)
10871 {
b98664d3 10872 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10873 "- DIE at %s [in module %s]"),
10874 sect_offset_str (child_die->sect_off),
10875 objfile_name (objfile));
32019081
JK
10876 continue;
10877 }
10878
10879 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10880 if (import_attr == NULL)
10881 {
b98664d3 10882 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10883 dwarf_tag_name (child_die->tag));
10884 continue;
10885 }
10886
10887 imported_cu = cu;
10888 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10889 &imported_cu);
10890 imported_name = dwarf2_name (imported_die, imported_cu);
10891 if (imported_name == NULL)
10892 {
b98664d3 10893 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10894 "imported name - DIE at %s [in module %s]"),
10895 sect_offset_str (child_die->sect_off),
10896 objfile_name (objfile));
32019081
JK
10897 continue;
10898 }
10899
eb1e02fd 10900 excludes.push_back (imported_name);
32019081
JK
10901
10902 process_die (child_die, cu);
10903 }
10904
804d2729 10905 add_using_directive (using_directives (cu),
22cee43f
PMR
10906 import_prefix,
10907 canonical_name,
10908 import_alias,
10909 imported_declaration,
10910 excludes,
10911 0,
10912 &objfile->objfile_obstack);
27aa8d6a
SW
10913}
10914
5230b05a
WT
10915/* ICC<14 does not output the required DW_AT_declaration on incomplete
10916 types, but gives them a size of zero. Starting with version 14,
10917 ICC is compatible with GCC. */
10918
9068261f 10919static bool
5230b05a
WT
10920producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10921{
10922 if (!cu->checked_producer)
10923 check_producer (cu);
10924
10925 return cu->producer_is_icc_lt_14;
10926}
10927
eb77c9df
AB
10928/* ICC generates a DW_AT_type for C void functions. This was observed on
10929 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10930 which says that void functions should not have a DW_AT_type. */
10931
10932static bool
10933producer_is_icc (struct dwarf2_cu *cu)
10934{
10935 if (!cu->checked_producer)
10936 check_producer (cu);
10937
10938 return cu->producer_is_icc;
10939}
10940
1b80a9fa
JK
10941/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10942 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10943 this, it was first present in GCC release 4.3.0. */
10944
9068261f 10945static bool
1b80a9fa
JK
10946producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10947{
10948 if (!cu->checked_producer)
10949 check_producer (cu);
10950
10951 return cu->producer_is_gcc_lt_4_3;
10952}
10953
d721ba37
PA
10954static file_and_directory
10955find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10956{
d721ba37
PA
10957 file_and_directory res;
10958
9291a0cd
TT
10959 /* Find the filename. Do not use dwarf2_name here, since the filename
10960 is not a source language identifier. */
d721ba37
PA
10961 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10962 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10963
d721ba37
PA
10964 if (res.comp_dir == NULL
10965 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10966 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10967 {
d721ba37
PA
10968 res.comp_dir_storage = ldirname (res.name);
10969 if (!res.comp_dir_storage.empty ())
10970 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10971 }
d721ba37 10972 if (res.comp_dir != NULL)
9291a0cd
TT
10973 {
10974 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10975 directory, get rid of it. */
d721ba37 10976 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10977
d721ba37
PA
10978 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10979 res.comp_dir = cp + 1;
9291a0cd
TT
10980 }
10981
d721ba37
PA
10982 if (res.name == NULL)
10983 res.name = "<unknown>";
10984
10985 return res;
9291a0cd
TT
10986}
10987
f4dc4d17
DE
10988/* Handle DW_AT_stmt_list for a compilation unit.
10989 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10990 COMP_DIR is the compilation directory. LOWPC is passed to
10991 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10992
10993static void
10994handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10995 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10996{
976ca316 10997 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10998 struct attribute *attr;
527f3840
JK
10999 struct line_header line_header_local;
11000 hashval_t line_header_local_hash;
527f3840
JK
11001 void **slot;
11002 int decode_mapping;
2ab95328 11003
f4dc4d17
DE
11004 gdb_assert (! cu->per_cu->is_debug_types);
11005
2ab95328 11006 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11007 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11008 return;
11009
d4df075e 11010 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11011
11012 /* The line header hash table is only created if needed (it exists to
11013 prevent redundant reading of the line table for partial_units).
11014 If we're given a partial_unit, we'll need it. If we're given a
11015 compile_unit, then use the line header hash table if it's already
11016 created, but don't create one just yet. */
11017
976ca316 11018 if (per_objfile->line_header_hash == NULL
527f3840 11019 && die->tag == DW_TAG_partial_unit)
2ab95328 11020 {
976ca316 11021 per_objfile->line_header_hash
d15acc42
TT
11022 .reset (htab_create_alloc (127, line_header_hash_voidp,
11023 line_header_eq_voidp,
11024 free_line_header_voidp,
11025 xcalloc, xfree));
527f3840 11026 }
2ab95328 11027
9c541725 11028 line_header_local.sect_off = line_offset;
527f3840
JK
11029 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11030 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11031 if (per_objfile->line_header_hash != NULL)
527f3840 11032 {
976ca316 11033 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11034 &line_header_local,
11035 line_header_local_hash, NO_INSERT);
11036
11037 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11038 is not present in *SLOT (since if there is something in *SLOT then
11039 it will be for a partial_unit). */
11040 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11041 {
527f3840 11042 gdb_assert (*slot != NULL);
9a3c8263 11043 cu->line_header = (struct line_header *) *slot;
527f3840 11044 return;
dee91e82 11045 }
2ab95328 11046 }
527f3840
JK
11047
11048 /* dwarf_decode_line_header does not yet provide sufficient information.
11049 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11050 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11051 if (lh == NULL)
527f3840 11052 return;
4c8aa72d
PA
11053
11054 cu->line_header = lh.release ();
11055 cu->line_header_die_owner = die;
527f3840 11056
976ca316 11057 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11058 slot = NULL;
11059 else
11060 {
976ca316 11061 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11062 &line_header_local,
11063 line_header_local_hash, INSERT);
11064 gdb_assert (slot != NULL);
11065 }
11066 if (slot != NULL && *slot == NULL)
11067 {
11068 /* This newly decoded line number information unit will be owned
11069 by line_header_hash hash table. */
11070 *slot = cu->line_header;
4c8aa72d 11071 cu->line_header_die_owner = NULL;
527f3840
JK
11072 }
11073 else
11074 {
11075 /* We cannot free any current entry in (*slot) as that struct line_header
11076 may be already used by multiple CUs. Create only temporary decoded
11077 line_header for this CU - it may happen at most once for each line
11078 number information unit. And if we're not using line_header_hash
11079 then this is what we want as well. */
11080 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11081 }
11082 decode_mapping = (die->tag != DW_TAG_partial_unit);
11083 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11084 decode_mapping);
fff8551c 11085
2ab95328
TT
11086}
11087
95554aad 11088/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11089
c906108c 11090static void
e7c27a73 11091read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11092{
976ca316
SM
11093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11094 struct objfile *objfile = per_objfile->objfile;
08feed99 11095 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11096 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11097 CORE_ADDR highpc = ((CORE_ADDR) 0);
11098 struct attribute *attr;
c906108c 11099 struct die_info *child_die;
e142c38c 11100 CORE_ADDR baseaddr;
6e70227d 11101
380618d6 11102 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11103 baseaddr = objfile->text_section_offset ();
c906108c 11104
fae299cd 11105 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11106
11107 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11108 from finish_block. */
2acceee2 11109 if (lowpc == ((CORE_ADDR) -1))
c906108c 11110 lowpc = highpc;
3e29f34a 11111 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11112
d721ba37 11113 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11114
f4b8a18d
KW
11115 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11116 standardised yet. As a workaround for the language detection we fall
11117 back to the DW_AT_producer string. */
11118 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11119 cu->language = language_opencl;
11120
3019eac3
DE
11121 /* Similar hack for Go. */
11122 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11123 set_cu_language (DW_LANG_Go, cu);
11124
c24bdb02 11125 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11126
11127 /* Decode line number information if present. We do this before
11128 processing child DIEs, so that the line header table is available
11129 for DW_AT_decl_file. */
d721ba37 11130 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11131
11132 /* Process all dies in compilation unit. */
11133 if (die->child != NULL)
11134 {
11135 child_die = die->child;
11136 while (child_die && child_die->tag)
11137 {
11138 process_die (child_die, cu);
436c571c 11139 child_die = child_die->sibling;
3019eac3
DE
11140 }
11141 }
11142
11143 /* Decode macro information, if present. Dwarf 2 macro information
11144 refers to information in the line number info statement program
11145 header, so we can only read it if we've read the header
11146 successfully. */
0af92d60
JK
11147 attr = dwarf2_attr (die, DW_AT_macros, cu);
11148 if (attr == NULL)
11149 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11150 if (attr && cu->line_header)
11151 {
11152 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11153 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11154
43f3e411 11155 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11156 }
11157 else
11158 {
11159 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11160 if (attr && cu->line_header)
11161 {
11162 unsigned int macro_offset = DW_UNSND (attr);
11163
43f3e411 11164 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11165 }
11166 }
3019eac3
DE
11167}
11168
c24bdb02
KS
11169void
11170dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11171{
f4dc4d17
DE
11172 struct type_unit_group *tu_group;
11173 int first_time;
3019eac3 11174 struct attribute *attr;
9c541725 11175 unsigned int i;
0186c6a7 11176 struct signatured_type *sig_type;
3019eac3 11177
f4dc4d17 11178 gdb_assert (per_cu->is_debug_types);
0186c6a7 11179 sig_type = (struct signatured_type *) per_cu;
3019eac3 11180
c24bdb02 11181 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11182
f4dc4d17 11183 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11184 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11185 if (sig_type->type_unit_group == NULL)
c24bdb02 11186 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11187 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11188
11189 /* If we've already processed this stmt_list there's no real need to
11190 do it again, we could fake it and just recreate the part we need
11191 (file name,index -> symtab mapping). If data shows this optimization
11192 is useful we can do it then. */
8adb8487
TT
11193 type_unit_group_unshareable *tug_unshare
11194 = per_objfile->get_type_unit_group_unshareable (tu_group);
11195 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11196
11197 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11198 debug info. */
fff8551c 11199 line_header_up lh;
d4df075e 11200 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11201 {
d4df075e 11202 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11203 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11204 }
11205 if (lh == NULL)
11206 {
11207 if (first_time)
c24bdb02 11208 start_symtab ("", NULL, 0);
f4dc4d17
DE
11209 else
11210 {
8adb8487 11211 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11212 gdb_assert (m_builder == nullptr);
8adb8487 11213 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11214 m_builder.reset (new struct buildsym_compunit
11215 (COMPUNIT_OBJFILE (cust), "",
11216 COMPUNIT_DIRNAME (cust),
11217 compunit_language (cust),
11218 0, cust));
770479f2 11219 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11220 }
f4dc4d17 11221 return;
3019eac3
DE
11222 }
11223
c24bdb02
KS
11224 line_header = lh.release ();
11225 line_header_die_owner = die;
3019eac3 11226
f4dc4d17
DE
11227 if (first_time)
11228 {
c24bdb02 11229 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11230
1fd60fc0
DE
11231 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11232 still initializing it, and our caller (a few levels up)
11233 process_full_type_unit still needs to know if this is the first
11234 time. */
11235
8adb8487 11236 tug_unshare->symtabs
4ac93832
TT
11237 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11238 struct symtab *, line_header->file_names_size ());
3019eac3 11239
7ba99d21
AT
11240 auto &file_names = line_header->file_names ();
11241 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11242 {
7ba99d21 11243 file_entry &fe = file_names[i];
c24bdb02
KS
11244 dwarf2_start_subfile (this, fe.name,
11245 fe.include_dir (line_header));
11246 buildsym_compunit *b = get_builder ();
11247 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11248 {
4c8aa72d
PA
11249 /* NOTE: start_subfile will recognize when it's been
11250 passed a file it has already seen. So we can't
11251 assume there's a simple mapping from
11252 cu->line_header->file_names to subfiles, plus
11253 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11254 b->get_current_subfile ()->symtab
11255 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11256 }
11257
c24bdb02 11258 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11259 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11260 }
11261 }
11262 else
3019eac3 11263 {
c24bdb02 11264 gdb_assert (m_builder == nullptr);
8adb8487 11265 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11266 m_builder.reset (new struct buildsym_compunit
11267 (COMPUNIT_OBJFILE (cust), "",
11268 COMPUNIT_DIRNAME (cust),
11269 compunit_language (cust),
11270 0, cust));
770479f2 11271 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11272
7ba99d21
AT
11273 auto &file_names = line_header->file_names ();
11274 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11275 {
7ba99d21 11276 file_entry &fe = file_names[i];
8adb8487 11277 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11278 }
3019eac3
DE
11279 }
11280
f4dc4d17
DE
11281 /* The main symtab is allocated last. Type units don't have DW_AT_name
11282 so they don't have a "real" (so to speak) symtab anyway.
11283 There is later code that will assign the main symtab to all symbols
11284 that don't have one. We need to handle the case of a symbol with a
11285 missing symtab (DW_AT_decl_file) anyway. */
11286}
3019eac3 11287
f4dc4d17
DE
11288/* Process DW_TAG_type_unit.
11289 For TUs we want to skip the first top level sibling if it's not the
11290 actual type being defined by this TU. In this case the first top
11291 level sibling is there to provide context only. */
3019eac3 11292
f4dc4d17
DE
11293static void
11294read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11295{
11296 struct die_info *child_die;
3019eac3 11297
f4dc4d17
DE
11298 prepare_one_comp_unit (cu, die, language_minimal);
11299
11300 /* Initialize (or reinitialize) the machinery for building symtabs.
11301 We do this before processing child DIEs, so that the line header table
11302 is available for DW_AT_decl_file. */
c24bdb02 11303 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11304
11305 if (die->child != NULL)
11306 {
11307 child_die = die->child;
11308 while (child_die && child_die->tag)
11309 {
11310 process_die (child_die, cu);
436c571c 11311 child_die = child_die->sibling;
f4dc4d17
DE
11312 }
11313 }
3019eac3
DE
11314}
11315\f
80626a55
DE
11316/* DWO/DWP files.
11317
11318 http://gcc.gnu.org/wiki/DebugFission
11319 http://gcc.gnu.org/wiki/DebugFissionDWP
11320
11321 To simplify handling of both DWO files ("object" files with the DWARF info)
11322 and DWP files (a file with the DWOs packaged up into one file), we treat
11323 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11324
11325static hashval_t
11326hash_dwo_file (const void *item)
11327{
9a3c8263 11328 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11329 hashval_t hash;
3019eac3 11330
a2ce51a0
DE
11331 hash = htab_hash_string (dwo_file->dwo_name);
11332 if (dwo_file->comp_dir != NULL)
11333 hash += htab_hash_string (dwo_file->comp_dir);
11334 return hash;
3019eac3
DE
11335}
11336
11337static int
11338eq_dwo_file (const void *item_lhs, const void *item_rhs)
11339{
9a3c8263
SM
11340 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11341 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11342
a2ce51a0
DE
11343 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11344 return 0;
11345 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11346 return lhs->comp_dir == rhs->comp_dir;
11347 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11348}
11349
11350/* Allocate a hash table for DWO files. */
11351
51ac9db5 11352static htab_up
298e9637 11353allocate_dwo_file_hash_table ()
3019eac3 11354{
51ac9db5
SM
11355 auto delete_dwo_file = [] (void *item)
11356 {
11357 struct dwo_file *dwo_file = (struct dwo_file *) item;
11358
11359 delete dwo_file;
11360 };
11361
bc68fb19
TT
11362 return htab_up (htab_create_alloc (41,
11363 hash_dwo_file,
11364 eq_dwo_file,
11365 delete_dwo_file,
11366 xcalloc, xfree));
3019eac3
DE
11367}
11368
80626a55
DE
11369/* Lookup DWO file DWO_NAME. */
11370
11371static void **
976ca316 11372lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11373 const char *dwo_name,
11374 const char *comp_dir)
80626a55
DE
11375{
11376 struct dwo_file find_entry;
11377 void **slot;
11378
976ca316
SM
11379 if (per_objfile->per_bfd->dwo_files == NULL)
11380 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11381
0ac5b59e
DE
11382 find_entry.dwo_name = dwo_name;
11383 find_entry.comp_dir = comp_dir;
976ca316 11384 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11385 INSERT);
80626a55
DE
11386
11387 return slot;
11388}
11389
3019eac3
DE
11390static hashval_t
11391hash_dwo_unit (const void *item)
11392{
9a3c8263 11393 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11394
11395 /* This drops the top 32 bits of the id, but is ok for a hash. */
11396 return dwo_unit->signature;
11397}
11398
11399static int
11400eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11401{
9a3c8263
SM
11402 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11403 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11404
11405 /* The signature is assumed to be unique within the DWO file.
11406 So while object file CU dwo_id's always have the value zero,
11407 that's OK, assuming each object file DWO file has only one CU,
11408 and that's the rule for now. */
11409 return lhs->signature == rhs->signature;
11410}
11411
11412/* Allocate a hash table for DWO CUs,TUs.
11413 There is one of these tables for each of CUs,TUs for each DWO file. */
11414
b0b6a987 11415static htab_up
298e9637 11416allocate_dwo_unit_table ()
3019eac3
DE
11417{
11418 /* Start out with a pretty small number.
11419 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11420 return htab_up (htab_create_alloc (3,
11421 hash_dwo_unit,
11422 eq_dwo_unit,
11423 NULL, xcalloc, xfree));
3019eac3
DE
11424}
11425
19c3d4c9 11426/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11427
11428static void
19c3d4c9
DE
11429create_dwo_cu_reader (const struct die_reader_specs *reader,
11430 const gdb_byte *info_ptr,
11431 struct die_info *comp_unit_die,
c0ab21c2
TT
11432 struct dwo_file *dwo_file,
11433 struct dwo_unit *dwo_unit)
3019eac3
DE
11434{
11435 struct dwarf2_cu *cu = reader->cu;
9c541725 11436 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11437 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11438
a084a2a6
AT
11439 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11440 if (!signature.has_value ())
3019eac3 11441 {
b98664d3 11442 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11443 " its dwo_id [in module %s]"),
9d8780f0 11444 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11445 return;
11446 }
11447
3019eac3 11448 dwo_unit->dwo_file = dwo_file;
a084a2a6 11449 dwo_unit->signature = *signature;
8a0459fd 11450 dwo_unit->section = section;
9c541725 11451 dwo_unit->sect_off = sect_off;
3019eac3
DE
11452 dwo_unit->length = cu->per_cu->length;
11453
b4f54984 11454 if (dwarf_read_debug)
9d8780f0
SM
11455 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11456 sect_offset_str (sect_off),
9c541725 11457 hex_string (dwo_unit->signature));
3019eac3
DE
11458}
11459
33c5cd75 11460/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11461 Note: This function processes DWO files only, not DWP files. */
3019eac3 11462
33c5cd75 11463static void
976ca316 11464create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11465 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11466 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11467{
976ca316
SM
11468 struct objfile *objfile = per_objfile->objfile;
11469 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11470 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11471
96b79293 11472 section.read (objfile);
33c5cd75 11473 info_ptr = section.buffer;
3019eac3
DE
11474
11475 if (info_ptr == NULL)
33c5cd75 11476 return;
3019eac3 11477
b4f54984 11478 if (dwarf_read_debug)
19c3d4c9
DE
11479 {
11480 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11481 section.get_name (),
11482 section.get_file_name ());
19c3d4c9 11483 }
3019eac3 11484
33c5cd75 11485 end_ptr = info_ptr + section.size;
3019eac3
DE
11486 while (info_ptr < end_ptr)
11487 {
11488 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11489 struct dwo_unit read_unit {};
33c5cd75
DB
11490 struct dwo_unit *dwo_unit;
11491 void **slot;
11492 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11493
11494 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11495 per_cu.per_bfd = per_bfd;
3019eac3 11496 per_cu.is_debug_types = 0;
33c5cd75
DB
11497 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11498 per_cu.section = &section;
11499
976ca316 11500 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11501 if (!reader.dummy_p)
11502 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11503 &dwo_file, &read_unit);
33c5cd75
DB
11504 info_ptr += per_cu.length;
11505
11506 // If the unit could not be parsed, skip it.
c0ab21c2 11507 if (read_unit.dwo_file == NULL)
33c5cd75 11508 continue;
3019eac3 11509
33c5cd75 11510 if (cus_htab == NULL)
298e9637 11511 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11512
1859c670 11513 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11514 struct dwo_unit);
c0ab21c2 11515 *dwo_unit = read_unit;
b0b6a987 11516 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11517 gdb_assert (slot != NULL);
11518 if (*slot != NULL)
19c3d4c9 11519 {
33c5cd75
DB
11520 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11521 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11522
b98664d3 11523 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11524 " the entry at offset %s, signature %s"),
11525 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11526 hex_string (dwo_unit->signature));
19c3d4c9 11527 }
33c5cd75 11528 *slot = (void *)dwo_unit;
3019eac3 11529 }
3019eac3
DE
11530}
11531
80626a55
DE
11532/* DWP file .debug_{cu,tu}_index section format:
11533 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11534 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11535
11536 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11537 officially standard DWP format was published with DWARF v5 and is called
11538 Version 5. There are no versions 3 or 4.
80626a55 11539
d2415c6c
DE
11540 DWP Version 1:
11541
80626a55
DE
11542 Both index sections have the same format, and serve to map a 64-bit
11543 signature to a set of section numbers. Each section begins with a header,
11544 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11545 indexes, and a pool of 32-bit section numbers. The index sections will be
11546 aligned at 8-byte boundaries in the file.
11547
d2415c6c
DE
11548 The index section header consists of:
11549
11550 V, 32 bit version number
11551 -, 32 bits unused
11552 N, 32 bit number of compilation units or type units in the index
11553 M, 32 bit number of slots in the hash table
80626a55 11554
d2415c6c 11555 Numbers are recorded using the byte order of the application binary.
80626a55 11556
d2415c6c
DE
11557 The hash table begins at offset 16 in the section, and consists of an array
11558 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11559 order of the application binary). Unused slots in the hash table are 0.
11560 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11561
d2415c6c
DE
11562 The parallel table begins immediately after the hash table
11563 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11564 array of 32-bit indexes (using the byte order of the application binary),
11565 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11566 table contains a 32-bit index into the pool of section numbers. For unused
11567 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11568
73869dc2
DE
11569 The pool of section numbers begins immediately following the hash table
11570 (at offset 16 + 12 * M from the beginning of the section). The pool of
11571 section numbers consists of an array of 32-bit words (using the byte order
11572 of the application binary). Each item in the array is indexed starting
11573 from 0. The hash table entry provides the index of the first section
11574 number in the set. Additional section numbers in the set follow, and the
11575 set is terminated by a 0 entry (section number 0 is not used in ELF).
11576
11577 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11578 section must be the first entry in the set, and the .debug_abbrev.dwo must
11579 be the second entry. Other members of the set may follow in any order.
11580
11581 ---
11582
d2854d8d 11583 DWP Versions 2 and 5:
73869dc2 11584
d2854d8d 11585 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11586 and the entries in the index tables are now offsets into these sections.
11587 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11588 section.
11589
11590 Index Section Contents:
11591 Header
11592 Hash Table of Signatures dwp_hash_table.hash_table
11593 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11594 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11595 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11596
11597 The index section header consists of:
11598
11599 V, 32 bit version number
11600 L, 32 bit number of columns in the table of section offsets
11601 N, 32 bit number of compilation units or type units in the index
11602 M, 32 bit number of slots in the hash table
11603
11604 Numbers are recorded using the byte order of the application binary.
11605
11606 The hash table has the same format as version 1.
11607 The parallel table of indices has the same format as version 1,
11608 except that the entries are origin-1 indices into the table of sections
11609 offsets and the table of section sizes.
11610
11611 The table of offsets begins immediately following the parallel table
11612 (at offset 16 + 12 * M from the beginning of the section). The table is
11613 a two-dimensional array of 32-bit words (using the byte order of the
11614 application binary), with L columns and N+1 rows, in row-major order.
11615 Each row in the array is indexed starting from 0. The first row provides
11616 a key to the remaining rows: each column in this row provides an identifier
11617 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11618 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11619
11620 DW_SECT_INFO 1 .debug_info.dwo
11621 DW_SECT_TYPES 2 .debug_types.dwo
11622 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11623 DW_SECT_LINE 4 .debug_line.dwo
11624 DW_SECT_LOC 5 .debug_loc.dwo
11625 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11626 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11627 DW_SECT_MACRO 8 .debug_macro.dwo
11628
d2854d8d
CT
11629 The section identifiers for Version 5 are:
11630
11631 DW_SECT_INFO_V5 1 .debug_info.dwo
11632 DW_SECT_RESERVED_V5 2 --
11633 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11634 DW_SECT_LINE_V5 4 .debug_line.dwo
11635 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11636 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11637 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11638 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11639
73869dc2
DE
11640 The offsets provided by the CU and TU index sections are the base offsets
11641 for the contributions made by each CU or TU to the corresponding section
11642 in the package file. Each CU and TU header contains an abbrev_offset
11643 field, used to find the abbreviations table for that CU or TU within the
11644 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11645 be interpreted as relative to the base offset given in the index section.
11646 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11647 should be interpreted as relative to the base offset for .debug_line.dwo,
11648 and offsets into other debug sections obtained from DWARF attributes should
11649 also be interpreted as relative to the corresponding base offset.
11650
11651 The table of sizes begins immediately following the table of offsets.
11652 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11653 with L columns and N rows, in row-major order. Each row in the array is
11654 indexed starting from 1 (row 0 is shared by the two tables).
11655
11656 ---
11657
11658 Hash table lookup is handled the same in version 1 and 2:
11659
11660 We assume that N and M will not exceed 2^32 - 1.
11661 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11662
d2415c6c
DE
11663 Given a 64-bit compilation unit signature or a type signature S, an entry
11664 in the hash table is located as follows:
80626a55 11665
d2415c6c
DE
11666 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11667 the low-order k bits all set to 1.
80626a55 11668
d2415c6c 11669 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11670
d2415c6c
DE
11671 3) If the hash table entry at index H matches the signature, use that
11672 entry. If the hash table entry at index H is unused (all zeroes),
11673 terminate the search: the signature is not present in the table.
80626a55 11674
d2415c6c 11675 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11676
d2415c6c 11677 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11678 to stop at an unused slot or find the match. */
80626a55
DE
11679
11680/* Create a hash table to map DWO IDs to their CU/TU entry in
11681 .debug_{info,types}.dwo in DWP_FILE.
11682 Returns NULL if there isn't one.
11683 Note: This function processes DWP files only, not DWO files. */
11684
11685static struct dwp_hash_table *
976ca316 11686create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11687 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11688{
976ca316 11689 struct objfile *objfile = per_objfile->objfile;
400174b1 11690 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11691 const gdb_byte *index_ptr, *index_end;
80626a55 11692 struct dwarf2_section_info *index;
73869dc2 11693 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11694 struct dwp_hash_table *htab;
11695
11696 if (is_debug_types)
11697 index = &dwp_file->sections.tu_index;
11698 else
11699 index = &dwp_file->sections.cu_index;
11700
96b79293 11701 if (index->empty ())
80626a55 11702 return NULL;
96b79293 11703 index->read (objfile);
80626a55
DE
11704
11705 index_ptr = index->buffer;
11706 index_end = index_ptr + index->size;
11707
d2854d8d
CT
11708 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11709 For now it's safe to just read 4 bytes (particularly as it's difficult to
11710 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11711 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11712 index_ptr += 4;
d2854d8d 11713 if (version == 2 || version == 5)
73869dc2
DE
11714 nr_columns = read_4_bytes (dbfd, index_ptr);
11715 else
11716 nr_columns = 0;
11717 index_ptr += 4;
80626a55
DE
11718 nr_units = read_4_bytes (dbfd, index_ptr);
11719 index_ptr += 4;
11720 nr_slots = read_4_bytes (dbfd, index_ptr);
11721 index_ptr += 4;
11722
d2854d8d 11723 if (version != 1 && version != 2 && version != 5)
80626a55 11724 {
21aa081e 11725 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11726 " [in module %s]"),
21aa081e 11727 pulongest (version), dwp_file->name);
80626a55
DE
11728 }
11729 if (nr_slots != (nr_slots & -nr_slots))
11730 {
21aa081e 11731 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11732 " is not power of 2 [in module %s]"),
21aa081e 11733 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11734 }
11735
976ca316 11736 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11737 htab->version = version;
11738 htab->nr_columns = nr_columns;
80626a55
DE
11739 htab->nr_units = nr_units;
11740 htab->nr_slots = nr_slots;
11741 htab->hash_table = index_ptr;
11742 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11743
11744 /* Exit early if the table is empty. */
11745 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11746 || (version == 2 && nr_columns == 0)
11747 || (version == 5 && nr_columns == 0))
73869dc2
DE
11748 {
11749 /* All must be zero. */
11750 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11751 || (version == 2 && nr_columns != 0)
11752 || (version == 5 && nr_columns != 0))
73869dc2 11753 {
b98664d3 11754 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11755 " all zero [in modules %s]"),
11756 dwp_file->name);
11757 }
11758 return htab;
11759 }
11760
11761 if (version == 1)
11762 {
11763 htab->section_pool.v1.indices =
11764 htab->unit_table + sizeof (uint32_t) * nr_slots;
11765 /* It's harder to decide whether the section is too small in v1.
11766 V1 is deprecated anyway so we punt. */
11767 }
d2854d8d 11768 else if (version == 2)
73869dc2
DE
11769 {
11770 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11771 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11772 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11773 /* Reverse map for error checking. */
11774 int ids_seen[DW_SECT_MAX + 1];
11775 int i;
11776
11777 if (nr_columns < 2)
11778 {
11779 error (_("Dwarf Error: bad DWP hash table, too few columns"
11780 " in section table [in module %s]"),
11781 dwp_file->name);
11782 }
11783 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11784 {
11785 error (_("Dwarf Error: bad DWP hash table, too many columns"
11786 " in section table [in module %s]"),
11787 dwp_file->name);
11788 }
04fd5eed
GB
11789 memset (ids, 255, sizeof_ids);
11790 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11791 for (i = 0; i < nr_columns; ++i)
11792 {
11793 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11794
11795 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11796 {
11797 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11798 " in section table [in module %s]"),
11799 id, dwp_file->name);
11800 }
11801 if (ids_seen[id] != -1)
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11804 " id %d in section table [in module %s]"),
11805 id, dwp_file->name);
11806 }
11807 ids_seen[id] = i;
11808 ids[i] = id;
11809 }
11810 /* Must have exactly one info or types section. */
11811 if (((ids_seen[DW_SECT_INFO] != -1)
11812 + (ids_seen[DW_SECT_TYPES] != -1))
11813 != 1)
11814 {
11815 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11816 " DWO info/types section [in module %s]"),
11817 dwp_file->name);
11818 }
11819 /* Must have an abbrev section. */
11820 if (ids_seen[DW_SECT_ABBREV] == -1)
11821 {
11822 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11823 " section [in module %s]"),
11824 dwp_file->name);
11825 }
11826 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11827 htab->section_pool.v2.sizes =
11828 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11829 * nr_units * nr_columns);
11830 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11831 * nr_units * nr_columns))
11832 > index_end)
11833 {
11834 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11835 " [in module %s]"),
11836 dwp_file->name);
11837 }
11838 }
d2854d8d
CT
11839 else /* version == 5 */
11840 {
11841 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11842 int *ids = htab->section_pool.v5.section_ids;
11843 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11844 /* Reverse map for error checking. */
11845 int ids_seen[DW_SECT_MAX_V5 + 1];
11846
11847 if (nr_columns < 2)
11848 {
11849 error (_("Dwarf Error: bad DWP hash table, too few columns"
11850 " in section table [in module %s]"),
11851 dwp_file->name);
11852 }
11853 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11854 {
11855 error (_("Dwarf Error: bad DWP hash table, too many columns"
11856 " in section table [in module %s]"),
11857 dwp_file->name);
11858 }
11859 memset (ids, 255, sizeof_ids);
11860 memset (ids_seen, 255, sizeof (ids_seen));
11861 for (int i = 0; i < nr_columns; ++i)
11862 {
11863 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11864
11865 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11866 {
11867 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11868 " in section table [in module %s]"),
11869 id, dwp_file->name);
11870 }
11871 if (ids_seen[id] != -1)
11872 {
11873 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11874 " id %d in section table [in module %s]"),
11875 id, dwp_file->name);
11876 }
11877 ids_seen[id] = i;
11878 ids[i] = id;
11879 }
11880 /* Must have seen an info section. */
11881 if (ids_seen[DW_SECT_INFO_V5] == -1)
11882 {
11883 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11884 " DWO info/types section [in module %s]"),
11885 dwp_file->name);
11886 }
11887 /* Must have an abbrev section. */
11888 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11889 {
11890 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11891 " section [in module %s]"),
11892 dwp_file->name);
11893 }
11894 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11895 htab->section_pool.v5.sizes
11896 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11897 * nr_units * nr_columns);
11898 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11899 * nr_units * nr_columns))
11900 > index_end)
11901 {
11902 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11903 " [in module %s]"),
11904 dwp_file->name);
11905 }
11906 }
80626a55
DE
11907
11908 return htab;
11909}
11910
11911/* Update SECTIONS with the data from SECTP.
11912
5bb6e9dd
TT
11913 This function is like the other "locate" section routines, but in
11914 this context the sections to read comes from the DWP V1 hash table,
11915 not the full ELF section table.
80626a55
DE
11916
11917 The result is non-zero for success, or zero if an error was found. */
11918
11919static int
73869dc2
DE
11920locate_v1_virtual_dwo_sections (asection *sectp,
11921 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11922{
11923 const struct dwop_section_names *names = &dwop_section_names;
11924
11925 if (section_is_p (sectp->name, &names->abbrev_dwo))
11926 {
11927 /* There can be only one. */
049412e3 11928 if (sections->abbrev.s.section != NULL)
80626a55 11929 return 0;
049412e3 11930 sections->abbrev.s.section = sectp;
fd361982 11931 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11932 }
11933 else if (section_is_p (sectp->name, &names->info_dwo)
11934 || section_is_p (sectp->name, &names->types_dwo))
11935 {
11936 /* There can be only one. */
049412e3 11937 if (sections->info_or_types.s.section != NULL)
80626a55 11938 return 0;
049412e3 11939 sections->info_or_types.s.section = sectp;
fd361982 11940 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11941 }
11942 else if (section_is_p (sectp->name, &names->line_dwo))
11943 {
11944 /* There can be only one. */
049412e3 11945 if (sections->line.s.section != NULL)
80626a55 11946 return 0;
049412e3 11947 sections->line.s.section = sectp;
fd361982 11948 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11949 }
11950 else if (section_is_p (sectp->name, &names->loc_dwo))
11951 {
11952 /* There can be only one. */
049412e3 11953 if (sections->loc.s.section != NULL)
80626a55 11954 return 0;
049412e3 11955 sections->loc.s.section = sectp;
fd361982 11956 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11957 }
11958 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11959 {
11960 /* There can be only one. */
049412e3 11961 if (sections->macinfo.s.section != NULL)
80626a55 11962 return 0;
049412e3 11963 sections->macinfo.s.section = sectp;
fd361982 11964 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11965 }
11966 else if (section_is_p (sectp->name, &names->macro_dwo))
11967 {
11968 /* There can be only one. */
049412e3 11969 if (sections->macro.s.section != NULL)
80626a55 11970 return 0;
049412e3 11971 sections->macro.s.section = sectp;
fd361982 11972 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11973 }
11974 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11975 {
11976 /* There can be only one. */
049412e3 11977 if (sections->str_offsets.s.section != NULL)
80626a55 11978 return 0;
049412e3 11979 sections->str_offsets.s.section = sectp;
fd361982 11980 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11981 }
11982 else
11983 {
11984 /* No other kind of section is valid. */
11985 return 0;
11986 }
11987
11988 return 1;
11989}
11990
73869dc2
DE
11991/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11992 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11993 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11994 This is for DWP version 1 files. */
80626a55
DE
11995
11996static struct dwo_unit *
976ca316 11997create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11998 struct dwp_file *dwp_file,
73869dc2
DE
11999 uint32_t unit_index,
12000 const char *comp_dir,
12001 ULONGEST signature, int is_debug_types)
80626a55 12002{
73869dc2
DE
12003 const struct dwp_hash_table *dwp_htab =
12004 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12005 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12006 const char *kind = is_debug_types ? "TU" : "CU";
12007 struct dwo_file *dwo_file;
12008 struct dwo_unit *dwo_unit;
73869dc2 12009 struct virtual_v1_dwo_sections sections;
80626a55 12010 void **dwo_file_slot;
80626a55
DE
12011 int i;
12012
73869dc2
DE
12013 gdb_assert (dwp_file->version == 1);
12014
b4f54984 12015 if (dwarf_read_debug)
80626a55 12016 {
73869dc2 12017 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12018 kind,
73869dc2 12019 pulongest (unit_index), hex_string (signature),
80626a55
DE
12020 dwp_file->name);
12021 }
12022
19ac8c2e 12023 /* Fetch the sections of this DWO unit.
80626a55
DE
12024 Put a limit on the number of sections we look for so that bad data
12025 doesn't cause us to loop forever. */
12026
73869dc2 12027#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12028 (1 /* .debug_info or .debug_types */ \
12029 + 1 /* .debug_abbrev */ \
12030 + 1 /* .debug_line */ \
12031 + 1 /* .debug_loc */ \
12032 + 1 /* .debug_str_offsets */ \
19ac8c2e 12033 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12034 + 1 /* trailing zero */)
12035
12036 memset (&sections, 0, sizeof (sections));
80626a55 12037
73869dc2 12038 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12039 {
12040 asection *sectp;
12041 uint32_t section_nr =
12042 read_4_bytes (dbfd,
73869dc2
DE
12043 dwp_htab->section_pool.v1.indices
12044 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12045
12046 if (section_nr == 0)
12047 break;
12048 if (section_nr >= dwp_file->num_sections)
12049 {
12050 error (_("Dwarf Error: bad DWP hash table, section number too large"
12051 " [in module %s]"),
12052 dwp_file->name);
12053 }
12054
12055 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12056 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12057 {
12058 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12059 " [in module %s]"),
12060 dwp_file->name);
12061 }
12062 }
12063
12064 if (i < 2
96b79293
TT
12065 || sections.info_or_types.empty ()
12066 || sections.abbrev.empty ())
80626a55
DE
12067 {
12068 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12069 " [in module %s]"),
12070 dwp_file->name);
12071 }
73869dc2 12072 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12073 {
12074 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12075 " [in module %s]"),
12076 dwp_file->name);
12077 }
12078
12079 /* It's easier for the rest of the code if we fake a struct dwo_file and
12080 have dwo_unit "live" in that. At least for now.
12081
12082 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12083 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12084 file, we can combine them back into a virtual DWO file to save space
12085 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12086 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12087
791afaa2
TT
12088 std::string virtual_dwo_name =
12089 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12090 sections.abbrev.get_id (),
12091 sections.line.get_id (),
12092 sections.loc.get_id (),
12093 sections.str_offsets.get_id ());
80626a55 12094 /* Can we use an existing virtual DWO file? */
976ca316 12095 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12096 comp_dir);
80626a55
DE
12097 /* Create one if necessary. */
12098 if (*dwo_file_slot == NULL)
12099 {
b4f54984 12100 if (dwarf_read_debug)
80626a55
DE
12101 {
12102 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12103 virtual_dwo_name.c_str ());
80626a55 12104 }
51ac9db5 12105 dwo_file = new struct dwo_file;
976ca316 12106 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12107 dwo_file->comp_dir = comp_dir;
80626a55
DE
12108 dwo_file->sections.abbrev = sections.abbrev;
12109 dwo_file->sections.line = sections.line;
12110 dwo_file->sections.loc = sections.loc;
12111 dwo_file->sections.macinfo = sections.macinfo;
12112 dwo_file->sections.macro = sections.macro;
12113 dwo_file->sections.str_offsets = sections.str_offsets;
12114 /* The "str" section is global to the entire DWP file. */
12115 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12116 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12117 there's no need to record it in dwo_file.
12118 Also, we can't simply record type sections in dwo_file because
12119 we record a pointer into the vector in dwo_unit. As we collect more
12120 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12121 for it, invalidating all copies of pointers into the previous
12122 contents. */
80626a55
DE
12123 *dwo_file_slot = dwo_file;
12124 }
12125 else
12126 {
b4f54984 12127 if (dwarf_read_debug)
80626a55
DE
12128 {
12129 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12130 virtual_dwo_name.c_str ());
80626a55 12131 }
9a3c8263 12132 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12133 }
80626a55 12134
976ca316 12135 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12136 dwo_unit->dwo_file = dwo_file;
12137 dwo_unit->signature = signature;
8d749320 12138 dwo_unit->section =
976ca316 12139 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12140 *dwo_unit->section = sections.info_or_types;
57d63ce2 12141 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12142
12143 return dwo_unit;
12144}
12145
d2854d8d
CT
12146/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12147 simplify them. Given a pointer to the containing section SECTION, and
12148 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12149 virtual section of just that piece. */
73869dc2
DE
12150
12151static struct dwarf2_section_info
d2854d8d
CT
12152create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12153 struct dwarf2_section_info *section,
12154 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12155{
12156 struct dwarf2_section_info result;
12157 asection *sectp;
12158
12159 gdb_assert (section != NULL);
12160 gdb_assert (!section->is_virtual);
12161
12162 memset (&result, 0, sizeof (result));
12163 result.s.containing_section = section;
dc4ccb6f 12164 result.is_virtual = true;
73869dc2
DE
12165
12166 if (size == 0)
12167 return result;
12168
96b79293 12169 sectp = section->get_bfd_section ();
73869dc2
DE
12170
12171 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12172 bounds of the real section. This is a pretty-rare event, so just
12173 flag an error (easier) instead of a warning and trying to cope. */
12174 if (sectp == NULL
fd361982 12175 || offset + size > bfd_section_size (sectp))
73869dc2 12176 {
d2854d8d 12177 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12178 " in section %s [in module %s]"),
fd361982 12179 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12180 objfile_name (per_objfile->objfile));
73869dc2
DE
12181 }
12182
12183 result.virtual_offset = offset;
12184 result.size = size;
12185 return result;
12186}
12187
12188/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12189 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12190 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12191 This is for DWP version 2 files. */
12192
12193static struct dwo_unit *
976ca316 12194create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12195 struct dwp_file *dwp_file,
73869dc2
DE
12196 uint32_t unit_index,
12197 const char *comp_dir,
12198 ULONGEST signature, int is_debug_types)
12199{
73869dc2
DE
12200 const struct dwp_hash_table *dwp_htab =
12201 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12202 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12203 const char *kind = is_debug_types ? "TU" : "CU";
12204 struct dwo_file *dwo_file;
12205 struct dwo_unit *dwo_unit;
d2854d8d 12206 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12207 void **dwo_file_slot;
73869dc2
DE
12208 int i;
12209
12210 gdb_assert (dwp_file->version == 2);
12211
b4f54984 12212 if (dwarf_read_debug)
73869dc2
DE
12213 {
12214 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12215 kind,
12216 pulongest (unit_index), hex_string (signature),
12217 dwp_file->name);
12218 }
12219
12220 /* Fetch the section offsets of this DWO unit. */
12221
12222 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12223
12224 for (i = 0; i < dwp_htab->nr_columns; ++i)
12225 {
12226 uint32_t offset = read_4_bytes (dbfd,
12227 dwp_htab->section_pool.v2.offsets
12228 + (((unit_index - 1) * dwp_htab->nr_columns
12229 + i)
12230 * sizeof (uint32_t)));
12231 uint32_t size = read_4_bytes (dbfd,
12232 dwp_htab->section_pool.v2.sizes
12233 + (((unit_index - 1) * dwp_htab->nr_columns
12234 + i)
12235 * sizeof (uint32_t)));
12236
12237 switch (dwp_htab->section_pool.v2.section_ids[i])
12238 {
12239 case DW_SECT_INFO:
12240 case DW_SECT_TYPES:
12241 sections.info_or_types_offset = offset;
12242 sections.info_or_types_size = size;
12243 break;
12244 case DW_SECT_ABBREV:
12245 sections.abbrev_offset = offset;
12246 sections.abbrev_size = size;
12247 break;
12248 case DW_SECT_LINE:
12249 sections.line_offset = offset;
12250 sections.line_size = size;
12251 break;
12252 case DW_SECT_LOC:
12253 sections.loc_offset = offset;
12254 sections.loc_size = size;
12255 break;
12256 case DW_SECT_STR_OFFSETS:
12257 sections.str_offsets_offset = offset;
12258 sections.str_offsets_size = size;
12259 break;
12260 case DW_SECT_MACINFO:
12261 sections.macinfo_offset = offset;
12262 sections.macinfo_size = size;
12263 break;
12264 case DW_SECT_MACRO:
12265 sections.macro_offset = offset;
12266 sections.macro_size = size;
12267 break;
12268 }
12269 }
12270
12271 /* It's easier for the rest of the code if we fake a struct dwo_file and
12272 have dwo_unit "live" in that. At least for now.
12273
12274 The DWP file can be made up of a random collection of CUs and TUs.
12275 However, for each CU + set of TUs that came from the same original DWO
12276 file, we can combine them back into a virtual DWO file to save space
12277 (fewer struct dwo_file objects to allocate). Remember that for really
12278 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12279
791afaa2
TT
12280 std::string virtual_dwo_name =
12281 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12282 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12283 (long) (sections.line_size ? sections.line_offset : 0),
12284 (long) (sections.loc_size ? sections.loc_offset : 0),
12285 (long) (sections.str_offsets_size
12286 ? sections.str_offsets_offset : 0));
73869dc2 12287 /* Can we use an existing virtual DWO file? */
976ca316 12288 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12289 comp_dir);
73869dc2
DE
12290 /* Create one if necessary. */
12291 if (*dwo_file_slot == NULL)
12292 {
b4f54984 12293 if (dwarf_read_debug)
73869dc2
DE
12294 {
12295 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12296 virtual_dwo_name.c_str ());
73869dc2 12297 }
51ac9db5 12298 dwo_file = new struct dwo_file;
976ca316 12299 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12300 dwo_file->comp_dir = comp_dir;
12301 dwo_file->sections.abbrev =
d2854d8d
CT
12302 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12303 sections.abbrev_offset,
12304 sections.abbrev_size);
73869dc2 12305 dwo_file->sections.line =
d2854d8d
CT
12306 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12307 sections.line_offset,
12308 sections.line_size);
73869dc2 12309 dwo_file->sections.loc =
d2854d8d
CT
12310 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12311 sections.loc_offset, sections.loc_size);
73869dc2 12312 dwo_file->sections.macinfo =
d2854d8d
CT
12313 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12314 sections.macinfo_offset,
12315 sections.macinfo_size);
73869dc2 12316 dwo_file->sections.macro =
d2854d8d
CT
12317 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12318 sections.macro_offset,
12319 sections.macro_size);
73869dc2 12320 dwo_file->sections.str_offsets =
d2854d8d
CT
12321 create_dwp_v2_or_v5_section (per_objfile,
12322 &dwp_file->sections.str_offsets,
12323 sections.str_offsets_offset,
12324 sections.str_offsets_size);
73869dc2
DE
12325 /* The "str" section is global to the entire DWP file. */
12326 dwo_file->sections.str = dwp_file->sections.str;
12327 /* The info or types section is assigned below to dwo_unit,
12328 there's no need to record it in dwo_file.
12329 Also, we can't simply record type sections in dwo_file because
12330 we record a pointer into the vector in dwo_unit. As we collect more
12331 types we'll grow the vector and eventually have to reallocate space
12332 for it, invalidating all copies of pointers into the previous
12333 contents. */
12334 *dwo_file_slot = dwo_file;
12335 }
12336 else
12337 {
b4f54984 12338 if (dwarf_read_debug)
73869dc2
DE
12339 {
12340 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12341 virtual_dwo_name.c_str ());
73869dc2 12342 }
9a3c8263 12343 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12344 }
73869dc2 12345
976ca316 12346 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12347 dwo_unit->dwo_file = dwo_file;
12348 dwo_unit->signature = signature;
8d749320 12349 dwo_unit->section =
976ca316 12350 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d
CT
12351 *dwo_unit->section = create_dwp_v2_or_v5_section
12352 (per_objfile,
12353 is_debug_types
12354 ? &dwp_file->sections.types
12355 : &dwp_file->sections.info,
12356 sections.info_or_types_offset,
12357 sections.info_or_types_size);
12358 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12359
12360 return dwo_unit;
12361}
12362
12363/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12364 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12365 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12366 This is for DWP version 5 files. */
12367
12368static struct dwo_unit *
12369create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12370 struct dwp_file *dwp_file,
12371 uint32_t unit_index,
12372 const char *comp_dir,
12373 ULONGEST signature, int is_debug_types)
12374{
12375 const struct dwp_hash_table *dwp_htab
12376 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12377 bfd *dbfd = dwp_file->dbfd.get ();
12378 const char *kind = is_debug_types ? "TU" : "CU";
12379 struct dwo_file *dwo_file;
12380 struct dwo_unit *dwo_unit;
12381 struct virtual_v2_or_v5_dwo_sections sections {};
12382 void **dwo_file_slot;
12383
12384 gdb_assert (dwp_file->version == 5);
12385
12386 if (dwarf_read_debug)
12387 {
12388 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V5 file: %s\n",
12389 kind,
12390 pulongest (unit_index), hex_string (signature),
12391 dwp_file->name);
12392 }
12393
12394 /* Fetch the section offsets of this DWO unit. */
12395
12396 /* memset (&sections, 0, sizeof (sections)); */
12397
12398 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12399 {
12400 uint32_t offset = read_4_bytes (dbfd,
12401 dwp_htab->section_pool.v5.offsets
12402 + (((unit_index - 1)
12403 * dwp_htab->nr_columns
12404 + i)
12405 * sizeof (uint32_t)));
12406 uint32_t size = read_4_bytes (dbfd,
12407 dwp_htab->section_pool.v5.sizes
12408 + (((unit_index - 1) * dwp_htab->nr_columns
12409 + i)
12410 * sizeof (uint32_t)));
12411
12412 switch (dwp_htab->section_pool.v5.section_ids[i])
12413 {
12414 case DW_SECT_ABBREV_V5:
12415 sections.abbrev_offset = offset;
12416 sections.abbrev_size = size;
12417 break;
12418 case DW_SECT_INFO_V5:
12419 sections.info_or_types_offset = offset;
12420 sections.info_or_types_size = size;
12421 break;
12422 case DW_SECT_LINE_V5:
12423 sections.line_offset = offset;
12424 sections.line_size = size;
12425 break;
12426 case DW_SECT_LOCLISTS_V5:
12427 sections.loclists_offset = offset;
12428 sections.loclists_size = size;
12429 break;
12430 case DW_SECT_MACRO_V5:
12431 sections.macro_offset = offset;
12432 sections.macro_size = size;
12433 break;
12434 case DW_SECT_RNGLISTS_V5:
12435 sections.rnglists_offset = offset;
12436 sections.rnglists_size = size;
12437 break;
12438 case DW_SECT_STR_OFFSETS_V5:
12439 sections.str_offsets_offset = offset;
12440 sections.str_offsets_size = size;
12441 break;
12442 case DW_SECT_RESERVED_V5:
12443 default:
12444 break;
12445 }
12446 }
12447
12448 /* It's easier for the rest of the code if we fake a struct dwo_file and
12449 have dwo_unit "live" in that. At least for now.
12450
12451 The DWP file can be made up of a random collection of CUs and TUs.
12452 However, for each CU + set of TUs that came from the same original DWO
12453 file, we can combine them back into a virtual DWO file to save space
12454 (fewer struct dwo_file objects to allocate). Remember that for really
12455 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12456
12457 std::string virtual_dwo_name =
12458 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12459 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12460 (long) (sections.line_size ? sections.line_offset : 0),
12461 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12462 (long) (sections.str_offsets_size
12463 ? sections.str_offsets_offset : 0),
12464 (long) (sections.macro_size ? sections.macro_offset : 0),
12465 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12466 /* Can we use an existing virtual DWO file? */
12467 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12468 virtual_dwo_name.c_str (),
12469 comp_dir);
12470 /* Create one if necessary. */
12471 if (*dwo_file_slot == NULL)
12472 {
12473 if (dwarf_read_debug)
12474 {
12475 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12476 virtual_dwo_name.c_str ());
12477 }
12478 dwo_file = new struct dwo_file;
12479 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12480 dwo_file->comp_dir = comp_dir;
12481 dwo_file->sections.abbrev =
12482 create_dwp_v2_or_v5_section (per_objfile,
12483 &dwp_file->sections.abbrev,
12484 sections.abbrev_offset,
12485 sections.abbrev_size);
12486 dwo_file->sections.line =
12487 create_dwp_v2_or_v5_section (per_objfile,
12488 &dwp_file->sections.line,
12489 sections.line_offset, sections.line_size);
12490 dwo_file->sections.macro =
12491 create_dwp_v2_or_v5_section (per_objfile,
12492 &dwp_file->sections.macro,
12493 sections.macro_offset,
12494 sections.macro_size);
12495 dwo_file->sections.loclists =
12496 create_dwp_v2_or_v5_section (per_objfile,
12497 &dwp_file->sections.loclists,
12498 sections.loclists_offset,
12499 sections.loclists_size);
12500 dwo_file->sections.rnglists =
12501 create_dwp_v2_or_v5_section (per_objfile,
12502 &dwp_file->sections.rnglists,
12503 sections.rnglists_offset,
12504 sections.rnglists_size);
12505 dwo_file->sections.str_offsets =
12506 create_dwp_v2_or_v5_section (per_objfile,
12507 &dwp_file->sections.str_offsets,
12508 sections.str_offsets_offset,
12509 sections.str_offsets_size);
12510 /* The "str" section is global to the entire DWP file. */
12511 dwo_file->sections.str = dwp_file->sections.str;
12512 /* The info or types section is assigned below to dwo_unit,
12513 there's no need to record it in dwo_file.
12514 Also, we can't simply record type sections in dwo_file because
12515 we record a pointer into the vector in dwo_unit. As we collect more
12516 types we'll grow the vector and eventually have to reallocate space
12517 for it, invalidating all copies of pointers into the previous
12518 contents. */
12519 *dwo_file_slot = dwo_file;
12520 }
12521 else
12522 {
12523 if (dwarf_read_debug)
12524 {
12525 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12526 virtual_dwo_name.c_str ());
12527 }
12528 dwo_file = (struct dwo_file *) *dwo_file_slot;
12529 }
12530
12531 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12532 dwo_unit->dwo_file = dwo_file;
12533 dwo_unit->signature = signature;
12534 dwo_unit->section
12535 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12536 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12537 &dwp_file->sections.info,
12538 sections.info_or_types_offset,
12539 sections.info_or_types_size);
73869dc2
DE
12540 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12541
12542 return dwo_unit;
12543}
12544
57d63ce2
DE
12545/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12546 Returns NULL if the signature isn't found. */
80626a55
DE
12547
12548static struct dwo_unit *
976ca316 12549lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12550 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12551 ULONGEST signature, int is_debug_types)
80626a55 12552{
57d63ce2
DE
12553 const struct dwp_hash_table *dwp_htab =
12554 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12555 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12556 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12557 uint32_t hash = signature & mask;
12558 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12559 unsigned int i;
12560 void **slot;
870f88f7 12561 struct dwo_unit find_dwo_cu;
80626a55
DE
12562
12563 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12564 find_dwo_cu.signature = signature;
19ac8c2e 12565 slot = htab_find_slot (is_debug_types
48b490f2
TT
12566 ? dwp_file->loaded_tus.get ()
12567 : dwp_file->loaded_cus.get (),
19ac8c2e 12568 &find_dwo_cu, INSERT);
80626a55
DE
12569
12570 if (*slot != NULL)
9a3c8263 12571 return (struct dwo_unit *) *slot;
80626a55
DE
12572
12573 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12574 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12575 {
12576 ULONGEST signature_in_table;
12577
12578 signature_in_table =
57d63ce2 12579 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12580 if (signature_in_table == signature)
12581 {
57d63ce2
DE
12582 uint32_t unit_index =
12583 read_4_bytes (dbfd,
12584 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12585
73869dc2
DE
12586 if (dwp_file->version == 1)
12587 {
976ca316
SM
12588 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12589 unit_index, comp_dir,
12590 signature, is_debug_types);
73869dc2 12591 }
d2854d8d 12592 else if (dwp_file->version == 2)
73869dc2 12593 {
976ca316
SM
12594 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12595 unit_index, comp_dir,
12596 signature, is_debug_types);
73869dc2 12597 }
d2854d8d
CT
12598 else /* version == 5 */
12599 {
12600 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12601 unit_index, comp_dir,
12602 signature, is_debug_types);
12603 }
9a3c8263 12604 return (struct dwo_unit *) *slot;
80626a55
DE
12605 }
12606 if (signature_in_table == 0)
12607 return NULL;
12608 hash = (hash + hash2) & mask;
12609 }
12610
12611 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12612 " [in module %s]"),
12613 dwp_file->name);
12614}
12615
ab5088bf 12616/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12617 Open the file specified by FILE_NAME and hand it off to BFD for
12618 preliminary analysis. Return a newly initialized bfd *, which
12619 includes a canonicalized copy of FILE_NAME.
80626a55 12620 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12621 SEARCH_CWD is true if the current directory is to be searched.
12622 It will be searched before debug-file-directory.
13aaf454
DE
12623 If successful, the file is added to the bfd include table of the
12624 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12625 If unable to find/open the file, return NULL.
3019eac3
DE
12626 NOTE: This function is derived from symfile_bfd_open. */
12627
192b62ce 12628static gdb_bfd_ref_ptr
976ca316 12629try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12630 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12631{
24b9144d 12632 int desc;
9c02c129
DE
12633 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12634 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12635 to debug_file_directory. */
e0cc99a6 12636 const char *search_path;
9c02c129
DE
12637 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12638
e0cc99a6 12639 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12640 if (search_cwd)
12641 {
12642 if (*debug_file_directory != '\0')
e0cc99a6
TT
12643 {
12644 search_path_holder.reset (concat (".", dirname_separator_string,
12645 debug_file_directory,
12646 (char *) NULL));
12647 search_path = search_path_holder.get ();
12648 }
6ac97d4c 12649 else
e0cc99a6 12650 search_path = ".";
6ac97d4c 12651 }
9c02c129 12652 else
e0cc99a6 12653 search_path = debug_file_directory;
3019eac3 12654
24b9144d 12655 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12656 if (is_dwp)
12657 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12658
12659 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12660 desc = openp (search_path, flags, file_name,
3019eac3
DE
12661 O_RDONLY | O_BINARY, &absolute_name);
12662 if (desc < 0)
12663 return NULL;
12664
e0cc99a6
TT
12665 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12666 gnutarget, desc));
9c02c129
DE
12667 if (sym_bfd == NULL)
12668 return NULL;
192b62ce 12669 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12670
192b62ce
TT
12671 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12672 return NULL;
3019eac3 12673
13aaf454
DE
12674 /* Success. Record the bfd as having been included by the objfile's bfd.
12675 This is important because things like demangled_names_hash lives in the
12676 objfile's per_bfd space and may have references to things like symbol
12677 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12678 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12679
3019eac3
DE
12680 return sym_bfd;
12681}
12682
ab5088bf 12683/* Try to open DWO file FILE_NAME.
3019eac3
DE
12684 COMP_DIR is the DW_AT_comp_dir attribute.
12685 The result is the bfd handle of the file.
12686 If there is a problem finding or opening the file, return NULL.
12687 Upon success, the canonicalized path of the file is stored in the bfd,
12688 same as symfile_bfd_open. */
12689
192b62ce 12690static gdb_bfd_ref_ptr
976ca316 12691open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12692 const char *file_name, const char *comp_dir)
3019eac3 12693{
80626a55 12694 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12695 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12696 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12697
12698 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12699
12700 if (comp_dir != NULL)
12701 {
43816ebc
TT
12702 gdb::unique_xmalloc_ptr<char> path_to_try
12703 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12704
12705 /* NOTE: If comp_dir is a relative path, this will also try the
12706 search path, which seems useful. */
976ca316 12707 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12708 0 /*is_dwp*/,
192b62ce 12709 1 /*search_cwd*/));
3019eac3
DE
12710 if (abfd != NULL)
12711 return abfd;
12712 }
12713
12714 /* That didn't work, try debug-file-directory, which, despite its name,
12715 is a list of paths. */
12716
12717 if (*debug_file_directory == '\0')
12718 return NULL;
12719
976ca316 12720 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12721 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12722}
12723
80626a55
DE
12724/* This function is mapped across the sections and remembers the offset and
12725 size of each of the DWO debugging sections we are interested in. */
12726
12727static void
5bb6e9dd
TT
12728dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12729 dwo_sections *dwo_sections)
80626a55 12730{
80626a55
DE
12731 const struct dwop_section_names *names = &dwop_section_names;
12732
12733 if (section_is_p (sectp->name, &names->abbrev_dwo))
12734 {
049412e3 12735 dwo_sections->abbrev.s.section = sectp;
fd361982 12736 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12737 }
12738 else if (section_is_p (sectp->name, &names->info_dwo))
12739 {
049412e3 12740 dwo_sections->info.s.section = sectp;
fd361982 12741 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12742 }
12743 else if (section_is_p (sectp->name, &names->line_dwo))
12744 {
049412e3 12745 dwo_sections->line.s.section = sectp;
fd361982 12746 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12747 }
12748 else if (section_is_p (sectp->name, &names->loc_dwo))
12749 {
049412e3 12750 dwo_sections->loc.s.section = sectp;
fd361982 12751 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12752 }
41144253 12753 else if (section_is_p (sectp->name, &names->loclists_dwo))
12754 {
12755 dwo_sections->loclists.s.section = sectp;
12756 dwo_sections->loclists.size = bfd_section_size (sectp);
12757 }
80626a55
DE
12758 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12759 {
049412e3 12760 dwo_sections->macinfo.s.section = sectp;
fd361982 12761 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12762 }
12763 else if (section_is_p (sectp->name, &names->macro_dwo))
12764 {
049412e3 12765 dwo_sections->macro.s.section = sectp;
fd361982 12766 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12767 }
d0ce17d8
CT
12768 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12769 {
12770 dwo_sections->rnglists.s.section = sectp;
12771 dwo_sections->rnglists.size = bfd_section_size (sectp);
12772 }
80626a55
DE
12773 else if (section_is_p (sectp->name, &names->str_dwo))
12774 {
049412e3 12775 dwo_sections->str.s.section = sectp;
fd361982 12776 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12777 }
12778 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12779 {
049412e3 12780 dwo_sections->str_offsets.s.section = sectp;
fd361982 12781 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12782 }
12783 else if (section_is_p (sectp->name, &names->types_dwo))
12784 {
12785 struct dwarf2_section_info type_section;
12786
12787 memset (&type_section, 0, sizeof (type_section));
049412e3 12788 type_section.s.section = sectp;
fd361982 12789 type_section.size = bfd_section_size (sectp);
fd5866f6 12790 dwo_sections->types.push_back (type_section);
80626a55
DE
12791 }
12792}
12793
ab5088bf 12794/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12795 by PER_CU. This is for the non-DWP case.
80626a55 12796 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12797
12798static struct dwo_file *
4ab09049
SM
12799open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12800 const char *comp_dir)
3019eac3 12801{
976ca316 12802 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12803
976ca316 12804 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12805 if (dbfd == NULL)
12806 {
b4f54984 12807 if (dwarf_read_debug)
80626a55
DE
12808 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12809 return NULL;
12810 }
263db9a1 12811
51ac9db5 12812 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12813 dwo_file->dwo_name = dwo_name;
12814 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12815 dwo_file->dbfd = std::move (dbfd);
3019eac3 12816
5bb6e9dd
TT
12817 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12818 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12819 &dwo_file->sections);
3019eac3 12820
976ca316
SM
12821 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12822 dwo_file->cus);
3019eac3 12823
d2854d8d
CT
12824 if (cu->per_cu->dwarf_version < 5)
12825 {
12826 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12827 dwo_file->sections.types, dwo_file->tus);
12828 }
12829 else
12830 {
12831 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12832 &dwo_file->sections.info, dwo_file->tus,
12833 rcuh_kind::TYPE);
12834 }
3019eac3 12835
b4f54984 12836 if (dwarf_read_debug)
80626a55
DE
12837 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12838
263db9a1 12839 return dwo_file.release ();
3019eac3
DE
12840}
12841
80626a55 12842/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12843 size of each of the DWP debugging sections common to version 1 and 2 that
12844 we are interested in. */
3019eac3 12845
80626a55 12846static void
73869dc2 12847dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12848 dwp_file *dwp_file)
3019eac3 12849{
80626a55
DE
12850 const struct dwop_section_names *names = &dwop_section_names;
12851 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12852
80626a55 12853 /* Record the ELF section number for later lookup: this is what the
73869dc2 12854 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12855 gdb_assert (elf_section_nr < dwp_file->num_sections);
12856 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12857
80626a55
DE
12858 /* Look for specific sections that we need. */
12859 if (section_is_p (sectp->name, &names->str_dwo))
12860 {
049412e3 12861 dwp_file->sections.str.s.section = sectp;
fd361982 12862 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12863 }
12864 else if (section_is_p (sectp->name, &names->cu_index))
12865 {
049412e3 12866 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12867 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12868 }
12869 else if (section_is_p (sectp->name, &names->tu_index))
12870 {
049412e3 12871 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12872 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12873 }
12874}
3019eac3 12875
73869dc2
DE
12876/* This function is mapped across the sections and remembers the offset and
12877 size of each of the DWP version 2 debugging sections that we are interested
12878 in. This is split into a separate function because we don't know if we
d2854d8d 12879 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12880
12881static void
12882dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12883{
9a3c8263 12884 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12885 const struct dwop_section_names *names = &dwop_section_names;
12886 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12887
12888 /* Record the ELF section number for later lookup: this is what the
12889 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12890 gdb_assert (elf_section_nr < dwp_file->num_sections);
12891 dwp_file->elf_sections[elf_section_nr] = sectp;
12892
12893 /* Look for specific sections that we need. */
12894 if (section_is_p (sectp->name, &names->abbrev_dwo))
12895 {
049412e3 12896 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12897 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12898 }
12899 else if (section_is_p (sectp->name, &names->info_dwo))
12900 {
049412e3 12901 dwp_file->sections.info.s.section = sectp;
fd361982 12902 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12903 }
12904 else if (section_is_p (sectp->name, &names->line_dwo))
12905 {
049412e3 12906 dwp_file->sections.line.s.section = sectp;
fd361982 12907 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12908 }
12909 else if (section_is_p (sectp->name, &names->loc_dwo))
12910 {
049412e3 12911 dwp_file->sections.loc.s.section = sectp;
fd361982 12912 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12913 }
12914 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12915 {
049412e3 12916 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12917 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12918 }
12919 else if (section_is_p (sectp->name, &names->macro_dwo))
12920 {
049412e3 12921 dwp_file->sections.macro.s.section = sectp;
fd361982 12922 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12923 }
12924 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12925 {
049412e3 12926 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12927 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12928 }
12929 else if (section_is_p (sectp->name, &names->types_dwo))
12930 {
049412e3 12931 dwp_file->sections.types.s.section = sectp;
fd361982 12932 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12933 }
12934}
12935
d2854d8d
CT
12936/* This function is mapped across the sections and remembers the offset and
12937 size of each of the DWP version 5 debugging sections that we are interested
12938 in. This is split into a separate function because we don't know if we
12939 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12940
12941static void
12942dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12943{
12944 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12945 const struct dwop_section_names *names = &dwop_section_names;
12946 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12947
12948 /* Record the ELF section number for later lookup: this is what the
12949 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12950 gdb_assert (elf_section_nr < dwp_file->num_sections);
12951 dwp_file->elf_sections[elf_section_nr] = sectp;
12952
12953 /* Look for specific sections that we need. */
12954 if (section_is_p (sectp->name, &names->abbrev_dwo))
12955 {
12956 dwp_file->sections.abbrev.s.section = sectp;
12957 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12958 }
12959 else if (section_is_p (sectp->name, &names->info_dwo))
12960 {
12961 dwp_file->sections.info.s.section = sectp;
12962 dwp_file->sections.info.size = bfd_section_size (sectp);
12963 }
12964 else if (section_is_p (sectp->name, &names->line_dwo))
12965 {
12966 dwp_file->sections.line.s.section = sectp;
12967 dwp_file->sections.line.size = bfd_section_size (sectp);
12968 }
12969 else if (section_is_p (sectp->name, &names->loclists_dwo))
12970 {
12971 dwp_file->sections.loclists.s.section = sectp;
12972 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12973 }
12974 else if (section_is_p (sectp->name, &names->macro_dwo))
12975 {
12976 dwp_file->sections.macro.s.section = sectp;
12977 dwp_file->sections.macro.size = bfd_section_size (sectp);
12978 }
12979 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12980 {
12981 dwp_file->sections.rnglists.s.section = sectp;
12982 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12983 }
12984 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12985 {
12986 dwp_file->sections.str_offsets.s.section = sectp;
12987 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12988 }
12989}
12990
80626a55 12991/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12992
80626a55
DE
12993static hashval_t
12994hash_dwp_loaded_cutus (const void *item)
12995{
9a3c8263 12996 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12997
80626a55
DE
12998 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12999 return dwo_unit->signature;
3019eac3
DE
13000}
13001
80626a55 13002/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13003
80626a55
DE
13004static int
13005eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13006{
9a3c8263
SM
13007 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13008 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13009
80626a55
DE
13010 return dua->signature == dub->signature;
13011}
3019eac3 13012
80626a55 13013/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13014
48b490f2 13015static htab_up
298e9637 13016allocate_dwp_loaded_cutus_table ()
80626a55 13017{
48b490f2
TT
13018 return htab_up (htab_create_alloc (3,
13019 hash_dwp_loaded_cutus,
13020 eq_dwp_loaded_cutus,
13021 NULL, xcalloc, xfree));
80626a55 13022}
3019eac3 13023
ab5088bf
DE
13024/* Try to open DWP file FILE_NAME.
13025 The result is the bfd handle of the file.
13026 If there is a problem finding or opening the file, return NULL.
13027 Upon success, the canonicalized path of the file is stored in the bfd,
13028 same as symfile_bfd_open. */
13029
192b62ce 13030static gdb_bfd_ref_ptr
976ca316 13031open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13032{
976ca316 13033 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13034 1 /*is_dwp*/,
192b62ce 13035 1 /*search_cwd*/));
6ac97d4c
DE
13036 if (abfd != NULL)
13037 return abfd;
13038
13039 /* Work around upstream bug 15652.
13040 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13041 [Whether that's a "bug" is debatable, but it is getting in our way.]
13042 We have no real idea where the dwp file is, because gdb's realpath-ing
13043 of the executable's path may have discarded the needed info.
13044 [IWBN if the dwp file name was recorded in the executable, akin to
13045 .gnu_debuglink, but that doesn't exist yet.]
13046 Strip the directory from FILE_NAME and search again. */
13047 if (*debug_file_directory != '\0')
13048 {
13049 /* Don't implicitly search the current directory here.
13050 If the user wants to search "." to handle this case,
13051 it must be added to debug-file-directory. */
976ca316
SM
13052 return try_open_dwop_file (per_objfile, lbasename (file_name),
13053 1 /*is_dwp*/,
6ac97d4c
DE
13054 0 /*search_cwd*/);
13055 }
13056
13057 return NULL;
ab5088bf
DE
13058}
13059
80626a55
DE
13060/* Initialize the use of the DWP file for the current objfile.
13061 By convention the name of the DWP file is ${objfile}.dwp.
13062 The result is NULL if it can't be found. */
a766d390 13063
400174b1 13064static std::unique_ptr<struct dwp_file>
976ca316 13065open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13066{
976ca316 13067 struct objfile *objfile = per_objfile->objfile;
80626a55 13068
82bf32bc
JK
13069 /* Try to find first .dwp for the binary file before any symbolic links
13070 resolving. */
6c447423
DE
13071
13072 /* If the objfile is a debug file, find the name of the real binary
13073 file and get the name of dwp file from there. */
d721ba37 13074 std::string dwp_name;
6c447423
DE
13075 if (objfile->separate_debug_objfile_backlink != NULL)
13076 {
13077 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13078 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13079
d721ba37 13080 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13081 }
13082 else
d721ba37
PA
13083 dwp_name = objfile->original_name;
13084
13085 dwp_name += ".dwp";
80626a55 13086
976ca316 13087 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13088 if (dbfd == NULL
13089 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13090 {
13091 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13092 dwp_name = objfile_name (objfile);
13093 dwp_name += ".dwp";
976ca316 13094 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13095 }
13096
80626a55
DE
13097 if (dbfd == NULL)
13098 {
b4f54984 13099 if (dwarf_read_debug)
d721ba37 13100 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13101 return std::unique_ptr<dwp_file> ();
3019eac3 13102 }
400174b1
TT
13103
13104 const char *name = bfd_get_filename (dbfd.get ());
13105 std::unique_ptr<struct dwp_file> dwp_file
13106 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13107
0a0f4c01 13108 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13109 dwp_file->elf_sections =
976ca316 13110 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13111 dwp_file->num_sections, asection *);
13112
5bb6e9dd
TT
13113 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13114 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13115 dwp_file.get ());
80626a55 13116
976ca316 13117 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13118
976ca316 13119 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13120
73869dc2 13121 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13122 if (dwp_file->cus && dwp_file->tus
13123 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13124 {
13125 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13126 pretty bizarre. We use pulongest here because that's the established
4d65956b 13127 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13128 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13129 " TU version %s [in DWP file %s]"),
13130 pulongest (dwp_file->cus->version),
d721ba37 13131 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13132 }
08302ed2
DE
13133
13134 if (dwp_file->cus)
13135 dwp_file->version = dwp_file->cus->version;
13136 else if (dwp_file->tus)
13137 dwp_file->version = dwp_file->tus->version;
13138 else
13139 dwp_file->version = 2;
73869dc2 13140
5bb6e9dd
TT
13141 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13142 {
13143 if (dwp_file->version == 2)
13144 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13145 dwp_file.get ());
13146 else
13147 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13148 dwp_file.get ());
13149 }
73869dc2 13150
298e9637
SM
13151 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13152 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13153
b4f54984 13154 if (dwarf_read_debug)
80626a55
DE
13155 {
13156 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13157 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13158 " %s CUs, %s TUs\n",
13159 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13160 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13161 }
13162
13163 return dwp_file;
3019eac3 13164}
c906108c 13165
ab5088bf
DE
13166/* Wrapper around open_and_init_dwp_file, only open it once. */
13167
13168static struct dwp_file *
976ca316 13169get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13170{
976ca316 13171 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13172 {
976ca316
SM
13173 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13174 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13175 }
976ca316 13176 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13177}
13178
80626a55
DE
13179/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13180 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13181 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13182 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13183 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13184
13185 This is called, for example, when wanting to read a variable with a
13186 complex location. Therefore we don't want to do file i/o for every call.
13187 Therefore we don't want to look for a DWO file on every call.
13188 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13189 then we check if we've already seen DWO_NAME, and only THEN do we check
13190 for a DWO file.
13191
1c658ad5 13192 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13193 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13194
3019eac3 13195static struct dwo_unit *
4ab09049 13196lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13197 ULONGEST signature, int is_debug_types)
3019eac3 13198{
976ca316
SM
13199 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13200 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13201 const char *kind = is_debug_types ? "TU" : "CU";
13202 void **dwo_file_slot;
3019eac3 13203 struct dwo_file *dwo_file;
80626a55 13204 struct dwp_file *dwp_file;
cb1df416 13205
6a506a2d
DE
13206 /* First see if there's a DWP file.
13207 If we have a DWP file but didn't find the DWO inside it, don't
13208 look for the original DWO file. It makes gdb behave differently
13209 depending on whether one is debugging in the build tree. */
cf2c3c16 13210
976ca316 13211 dwp_file = get_dwp_file (per_objfile);
80626a55 13212 if (dwp_file != NULL)
cf2c3c16 13213 {
80626a55
DE
13214 const struct dwp_hash_table *dwp_htab =
13215 is_debug_types ? dwp_file->tus : dwp_file->cus;
13216
13217 if (dwp_htab != NULL)
13218 {
13219 struct dwo_unit *dwo_cutu =
976ca316
SM
13220 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13221 is_debug_types);
80626a55
DE
13222
13223 if (dwo_cutu != NULL)
13224 {
b4f54984 13225 if (dwarf_read_debug)
80626a55
DE
13226 {
13227 fprintf_unfiltered (gdb_stdlog,
13228 "Virtual DWO %s %s found: @%s\n",
13229 kind, hex_string (signature),
13230 host_address_to_string (dwo_cutu));
13231 }
13232 return dwo_cutu;
13233 }
13234 }
13235 }
6a506a2d 13236 else
80626a55 13237 {
6a506a2d 13238 /* No DWP file, look for the DWO file. */
80626a55 13239
976ca316 13240 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13241 if (*dwo_file_slot == NULL)
80626a55 13242 {
6a506a2d 13243 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13244 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13245 }
6a506a2d 13246 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13247 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13248
6a506a2d 13249 if (dwo_file != NULL)
19c3d4c9 13250 {
6a506a2d
DE
13251 struct dwo_unit *dwo_cutu = NULL;
13252
13253 if (is_debug_types && dwo_file->tus)
13254 {
13255 struct dwo_unit find_dwo_cutu;
13256
13257 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13258 find_dwo_cutu.signature = signature;
9a3c8263 13259 dwo_cutu
b0b6a987
TT
13260 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13261 &find_dwo_cutu);
6a506a2d 13262 }
33c5cd75 13263 else if (!is_debug_types && dwo_file->cus)
80626a55 13264 {
33c5cd75
DB
13265 struct dwo_unit find_dwo_cutu;
13266
13267 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13268 find_dwo_cutu.signature = signature;
b0b6a987 13269 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13270 &find_dwo_cutu);
6a506a2d
DE
13271 }
13272
13273 if (dwo_cutu != NULL)
13274 {
b4f54984 13275 if (dwarf_read_debug)
6a506a2d
DE
13276 {
13277 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13278 kind, dwo_name, hex_string (signature),
13279 host_address_to_string (dwo_cutu));
13280 }
13281 return dwo_cutu;
80626a55
DE
13282 }
13283 }
2e276125 13284 }
9cdd5dbd 13285
80626a55
DE
13286 /* We didn't find it. This could mean a dwo_id mismatch, or
13287 someone deleted the DWO/DWP file, or the search path isn't set up
13288 correctly to find the file. */
13289
b4f54984 13290 if (dwarf_read_debug)
80626a55
DE
13291 {
13292 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13293 kind, dwo_name, hex_string (signature));
13294 }
3019eac3 13295
6656a72d
DE
13296 /* This is a warning and not a complaint because it can be caused by
13297 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13298 {
13299 /* Print the name of the DWP file if we looked there, helps the user
13300 better diagnose the problem. */
791afaa2 13301 std::string dwp_text;
43942612
DE
13302
13303 if (dwp_file != NULL)
791afaa2
TT
13304 dwp_text = string_printf (" [in DWP file %s]",
13305 lbasename (dwp_file->name));
43942612 13306
9d8780f0 13307 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13308 " [in module %s]"),
4ab09049
SM
13309 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13310 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13311 }
3019eac3 13312 return NULL;
5fb290d7
DJ
13313}
13314
80626a55
DE
13315/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13316 See lookup_dwo_cutu_unit for details. */
13317
13318static struct dwo_unit *
4ab09049 13319lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13320 ULONGEST signature)
13321{
4ab09049
SM
13322 gdb_assert (!cu->per_cu->is_debug_types);
13323
13324 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13325}
13326
13327/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13328 See lookup_dwo_cutu_unit for details. */
13329
13330static struct dwo_unit *
4ab09049 13331lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13332{
4ab09049
SM
13333 gdb_assert (cu->per_cu->is_debug_types);
13334
13335 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13336
13337 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13338}
13339
89e63ee4
DE
13340/* Traversal function for queue_and_load_all_dwo_tus. */
13341
13342static int
13343queue_and_load_dwo_tu (void **slot, void *info)
13344{
13345 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13346 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13347 ULONGEST signature = dwo_unit->signature;
d460f660 13348 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13349
13350 if (sig_type != NULL)
13351 {
13352 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13353
13354 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13355 a real dependency of PER_CU on SIG_TYPE. That is detected later
13356 while processing PER_CU. */
120ce1b5 13357 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13358 load_full_type_unit (sig_cu, cu->per_objfile);
13359 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13360 }
13361
13362 return 1;
13363}
13364
1b555f17 13365/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13366 The DWO may have the only definition of the type, though it may not be
13367 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13368 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13369
13370static void
1b555f17 13371queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13372{
13373 struct dwo_unit *dwo_unit;
13374 struct dwo_file *dwo_file;
13375
1b555f17
SM
13376 gdb_assert (cu != nullptr);
13377 gdb_assert (!cu->per_cu->is_debug_types);
13378 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13379
1b555f17 13380 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13381 gdb_assert (dwo_unit != NULL);
13382
13383 dwo_file = dwo_unit->dwo_file;
13384 if (dwo_file->tus != NULL)
1b555f17 13385 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13386}
13387
3019eac3 13388/* Read in various DIEs. */
348e048f 13389
d389af10 13390/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13391 Inherit only the children of the DW_AT_abstract_origin DIE not being
13392 already referenced by DW_AT_abstract_origin from the children of the
13393 current DIE. */
d389af10
JK
13394
13395static void
13396inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13397{
13398 struct die_info *child_die;
791afaa2 13399 sect_offset *offsetp;
d389af10
JK
13400 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13401 struct die_info *origin_die;
13402 /* Iterator of the ORIGIN_DIE children. */
13403 struct die_info *origin_child_die;
d389af10 13404 struct attribute *attr;
cd02d79d
PA
13405 struct dwarf2_cu *origin_cu;
13406 struct pending **origin_previous_list_in_scope;
d389af10
JK
13407
13408 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13409 if (!attr)
13410 return;
13411
cd02d79d
PA
13412 /* Note that following die references may follow to a die in a
13413 different cu. */
13414
13415 origin_cu = cu;
13416 origin_die = follow_die_ref (die, attr, &origin_cu);
13417
13418 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13419 symbols in. */
13420 origin_previous_list_in_scope = origin_cu->list_in_scope;
13421 origin_cu->list_in_scope = cu->list_in_scope;
13422
edb3359d
DJ
13423 if (die->tag != origin_die->tag
13424 && !(die->tag == DW_TAG_inlined_subroutine
13425 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13426 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13427 sect_offset_str (die->sect_off),
13428 sect_offset_str (origin_die->sect_off));
d389af10 13429
791afaa2 13430 std::vector<sect_offset> offsets;
d389af10 13431
3ea89b92
PMR
13432 for (child_die = die->child;
13433 child_die && child_die->tag;
436c571c 13434 child_die = child_die->sibling)
3ea89b92
PMR
13435 {
13436 struct die_info *child_origin_die;
13437 struct dwarf2_cu *child_origin_cu;
13438
13439 /* We are trying to process concrete instance entries:
216f72a1 13440 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13441 it's not relevant to our analysis here. i.e. detecting DIEs that are
13442 present in the abstract instance but not referenced in the concrete
13443 one. */
216f72a1
JK
13444 if (child_die->tag == DW_TAG_call_site
13445 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13446 continue;
13447
c38f313d
DJ
13448 /* For each CHILD_DIE, find the corresponding child of
13449 ORIGIN_DIE. If there is more than one layer of
13450 DW_AT_abstract_origin, follow them all; there shouldn't be,
13451 but GCC versions at least through 4.4 generate this (GCC PR
13452 40573). */
3ea89b92
PMR
13453 child_origin_die = child_die;
13454 child_origin_cu = cu;
c38f313d
DJ
13455 while (1)
13456 {
cd02d79d
PA
13457 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13458 child_origin_cu);
c38f313d
DJ
13459 if (attr == NULL)
13460 break;
cd02d79d
PA
13461 child_origin_die = follow_die_ref (child_origin_die, attr,
13462 &child_origin_cu);
c38f313d
DJ
13463 }
13464
d389af10
JK
13465 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13466 counterpart may exist. */
c38f313d 13467 if (child_origin_die != child_die)
d389af10 13468 {
edb3359d
DJ
13469 if (child_die->tag != child_origin_die->tag
13470 && !(child_die->tag == DW_TAG_inlined_subroutine
13471 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13472 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13473 "different tags"),
9d8780f0
SM
13474 sect_offset_str (child_die->sect_off),
13475 sect_offset_str (child_origin_die->sect_off));
c38f313d 13476 if (child_origin_die->parent != origin_die)
b98664d3 13477 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13478 "different parents"),
9d8780f0
SM
13479 sect_offset_str (child_die->sect_off),
13480 sect_offset_str (child_origin_die->sect_off));
c38f313d 13481 else
791afaa2 13482 offsets.push_back (child_origin_die->sect_off);
d389af10 13483 }
d389af10 13484 }
791afaa2
TT
13485 std::sort (offsets.begin (), offsets.end ());
13486 sect_offset *offsets_end = offsets.data () + offsets.size ();
13487 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13488 if (offsetp[-1] == *offsetp)
b98664d3 13489 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13490 "to DIE %s as their abstract origin"),
13491 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13492
791afaa2 13493 offsetp = offsets.data ();
d389af10
JK
13494 origin_child_die = origin_die->child;
13495 while (origin_child_die && origin_child_die->tag)
13496 {
13497 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13498 while (offsetp < offsets_end
9c541725 13499 && *offsetp < origin_child_die->sect_off)
d389af10 13500 offsetp++;
b64f50a1 13501 if (offsetp >= offsets_end
9c541725 13502 || *offsetp > origin_child_die->sect_off)
d389af10 13503 {
adde2bff
DE
13504 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13505 Check whether we're already processing ORIGIN_CHILD_DIE.
13506 This can happen with mutually referenced abstract_origins.
13507 PR 16581. */
13508 if (!origin_child_die->in_process)
13509 process_die (origin_child_die, origin_cu);
d389af10 13510 }
436c571c 13511 origin_child_die = origin_child_die->sibling;
d389af10 13512 }
cd02d79d 13513 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13514
13515 if (cu != origin_cu)
13516 compute_delayed_physnames (origin_cu);
d389af10
JK
13517}
13518
c906108c 13519static void
e7c27a73 13520read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13521{
5e22e966 13522 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13523 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13524 struct context_stack *newobj;
c906108c
SS
13525 CORE_ADDR lowpc;
13526 CORE_ADDR highpc;
13527 struct die_info *child_die;
edb3359d 13528 struct attribute *attr, *call_line, *call_file;
15d034d0 13529 const char *name;
e142c38c 13530 CORE_ADDR baseaddr;
801e3a5b 13531 struct block *block;
edb3359d 13532 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13533 std::vector<struct symbol *> template_args;
34eaf542 13534 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13535
13536 if (inlined_func)
13537 {
13538 /* If we do not have call site information, we can't show the
13539 caller of this inlined function. That's too confusing, so
13540 only use the scope for local variables. */
13541 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13542 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13543 if (call_line == NULL || call_file == NULL)
13544 {
13545 read_lexical_block_scope (die, cu);
13546 return;
13547 }
13548 }
c906108c 13549
b3b3bada 13550 baseaddr = objfile->text_section_offset ();
e142c38c 13551
94af9270 13552 name = dwarf2_name (die, cu);
c906108c 13553
e8d05480
JB
13554 /* Ignore functions with missing or empty names. These are actually
13555 illegal according to the DWARF standard. */
13556 if (name == NULL)
13557 {
b98664d3 13558 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13559 sect_offset_str (die->sect_off));
e8d05480
JB
13560 return;
13561 }
13562
13563 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13564 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13565 <= PC_BOUNDS_INVALID)
e8d05480 13566 {
ae4d0c03
PM
13567 attr = dwarf2_attr (die, DW_AT_external, cu);
13568 if (!attr || !DW_UNSND (attr))
b98664d3 13569 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13570 "for subprogram DIE at %s"),
13571 sect_offset_str (die->sect_off));
e8d05480
JB
13572 return;
13573 }
c906108c 13574
3e29f34a
MR
13575 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13576 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13577
34eaf542
TT
13578 /* If we have any template arguments, then we must allocate a
13579 different sort of symbol. */
436c571c 13580 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13581 {
13582 if (child_die->tag == DW_TAG_template_type_param
13583 || child_die->tag == DW_TAG_template_value_param)
13584 {
8c14c3a3 13585 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13586 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13587 break;
13588 }
13589 }
13590
c24bdb02 13591 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13592 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13593 (struct symbol *) templ_func);
4c2df51b 13594
81873cc8 13595 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13596 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13597 cu->language);
13598
4cecd739
DJ
13599 /* If there is a location expression for DW_AT_frame_base, record
13600 it. */
e142c38c 13601 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13602 if (attr != nullptr)
fe978cb0 13603 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13604
63e43d3a
PMR
13605 /* If there is a location for the static link, record it. */
13606 newobj->static_link = NULL;
13607 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13608 if (attr != nullptr)
63e43d3a 13609 {
224c3ddb
SM
13610 newobj->static_link
13611 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13612 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13613 cu->addr_type ());
63e43d3a
PMR
13614 }
13615
c24bdb02 13616 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13617
639d11d3 13618 if (die->child != NULL)
c906108c 13619 {
639d11d3 13620 child_die = die->child;
c906108c
SS
13621 while (child_die && child_die->tag)
13622 {
34eaf542
TT
13623 if (child_die->tag == DW_TAG_template_type_param
13624 || child_die->tag == DW_TAG_template_value_param)
13625 {
13626 struct symbol *arg = new_symbol (child_die, NULL, cu);
13627
f1078f66 13628 if (arg != NULL)
2f4732b0 13629 template_args.push_back (arg);
34eaf542
TT
13630 }
13631 else
13632 process_die (child_die, cu);
436c571c 13633 child_die = child_die->sibling;
c906108c
SS
13634 }
13635 }
13636
d389af10
JK
13637 inherit_abstract_dies (die, cu);
13638
4a811a97
UW
13639 /* If we have a DW_AT_specification, we might need to import using
13640 directives from the context of the specification DIE. See the
13641 comment in determine_prefix. */
13642 if (cu->language == language_cplus
13643 && dwarf2_attr (die, DW_AT_specification, cu))
13644 {
13645 struct dwarf2_cu *spec_cu = cu;
13646 struct die_info *spec_die = die_specification (die, &spec_cu);
13647
13648 while (spec_die)
13649 {
13650 child_die = spec_die->child;
13651 while (child_die && child_die->tag)
13652 {
13653 if (child_die->tag == DW_TAG_imported_module)
13654 process_die (child_die, spec_cu);
436c571c 13655 child_die = child_die->sibling;
4a811a97
UW
13656 }
13657
13658 /* In some cases, GCC generates specification DIEs that
13659 themselves contain DW_AT_specification attributes. */
13660 spec_die = die_specification (spec_die, &spec_cu);
13661 }
13662 }
13663
c24bdb02 13664 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13665 /* Make a block for the local symbols within. */
c24bdb02 13666 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13667 cstk.static_link, lowpc, highpc);
801e3a5b 13668
df8a16a1 13669 /* For C++, set the block's scope. */
45280282
IB
13670 if ((cu->language == language_cplus
13671 || cu->language == language_fortran
c44af4eb
TT
13672 || cu->language == language_d
13673 || cu->language == language_rust)
4d4ec4e5 13674 && cu->processing_has_namespace_info)
195a3f6c
TT
13675 block_set_scope (block, determine_prefix (die, cu),
13676 &objfile->objfile_obstack);
df8a16a1 13677
801e3a5b
JB
13678 /* If we have address ranges, record them. */
13679 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13680
a60f3166 13681 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13682
34eaf542 13683 /* Attach template arguments to function. */
2f4732b0 13684 if (!template_args.empty ())
34eaf542
TT
13685 {
13686 gdb_assert (templ_func != NULL);
13687
2f4732b0 13688 templ_func->n_template_arguments = template_args.size ();
34eaf542 13689 templ_func->template_arguments
8d749320
SM
13690 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13691 templ_func->n_template_arguments);
34eaf542 13692 memcpy (templ_func->template_arguments,
2f4732b0 13693 template_args.data (),
34eaf542 13694 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13695
13696 /* Make sure that the symtab is set on the new symbols. Even
13697 though they don't appear in this symtab directly, other parts
13698 of gdb assume that symbols do, and this is reasonably
13699 true. */
8634679f 13700 for (symbol *sym : template_args)
3e1d3d8c 13701 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13702 }
13703
208d8187
JB
13704 /* In C++, we can have functions nested inside functions (e.g., when
13705 a function declares a class that has methods). This means that
13706 when we finish processing a function scope, we may need to go
13707 back to building a containing block's symbol lists. */
c24bdb02
KS
13708 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13709 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13710
921e78cf
JB
13711 /* If we've finished processing a top-level function, subsequent
13712 symbols go in the file symbol list. */
c24bdb02
KS
13713 if (cu->get_builder ()->outermost_context_p ())
13714 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13715}
13716
13717/* Process all the DIES contained within a lexical block scope. Start
13718 a new scope, process the dies, and then close the scope. */
13719
13720static void
e7c27a73 13721read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13722{
5e22e966 13723 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13724 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13725 CORE_ADDR lowpc, highpc;
13726 struct die_info *child_die;
e142c38c
DJ
13727 CORE_ADDR baseaddr;
13728
b3b3bada 13729 baseaddr = objfile->text_section_offset ();
c906108c
SS
13730
13731 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13732 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13733 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13734 be nasty. Might be easier to properly extend generic blocks to
af34e669 13735 describe ranges. */
e385593e
JK
13736 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13737 {
13738 case PC_BOUNDS_NOT_PRESENT:
13739 /* DW_TAG_lexical_block has no attributes, process its children as if
13740 there was no wrapping by that DW_TAG_lexical_block.
13741 GCC does no longer produces such DWARF since GCC r224161. */
13742 for (child_die = die->child;
13743 child_die != NULL && child_die->tag;
436c571c 13744 child_die = child_die->sibling)
4f7bc5ed
TT
13745 {
13746 /* We might already be processing this DIE. This can happen
13747 in an unusual circumstance -- where a subroutine A
13748 appears lexically in another subroutine B, but A actually
13749 inlines B. The recursion is broken here, rather than in
13750 inherit_abstract_dies, because it seems better to simply
13751 drop concrete children here. */
13752 if (!child_die->in_process)
13753 process_die (child_die, cu);
13754 }
e385593e
JK
13755 return;
13756 case PC_BOUNDS_INVALID:
13757 return;
13758 }
3e29f34a
MR
13759 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13760 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13761
c24bdb02 13762 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13763 if (die->child != NULL)
c906108c 13764 {
639d11d3 13765 child_die = die->child;
c906108c
SS
13766 while (child_die && child_die->tag)
13767 {
e7c27a73 13768 process_die (child_die, cu);
436c571c 13769 child_die = child_die->sibling;
c906108c
SS
13770 }
13771 }
3ea89b92 13772 inherit_abstract_dies (die, cu);
c24bdb02 13773 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13774
c24bdb02
KS
13775 if (*cu->get_builder ()->get_local_symbols () != NULL
13776 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13777 {
801e3a5b 13778 struct block *block
c24bdb02 13779 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13780 cstk.start_addr, highpc);
801e3a5b
JB
13781
13782 /* Note that recording ranges after traversing children, as we
13783 do here, means that recording a parent's ranges entails
13784 walking across all its children's ranges as they appear in
13785 the address map, which is quadratic behavior.
13786
13787 It would be nicer to record the parent's ranges before
13788 traversing its children, simply overriding whatever you find
13789 there. But since we don't even decide whether to create a
13790 block until after we've traversed its children, that's hard
13791 to do. */
13792 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13793 }
c24bdb02
KS
13794 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13795 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13796}
13797
216f72a1 13798/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13799
13800static void
13801read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13802{
5e22e966 13803 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13804 struct objfile *objfile = per_objfile->objfile;
08feed99 13805 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13806 CORE_ADDR pc, baseaddr;
13807 struct attribute *attr;
13808 struct call_site *call_site, call_site_local;
13809 void **slot;
13810 int nparams;
13811 struct die_info *child_die;
13812
b3b3bada 13813 baseaddr = objfile->text_section_offset ();
96408a79 13814
216f72a1
JK
13815 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13816 if (attr == NULL)
13817 {
13818 /* This was a pre-DWARF-5 GNU extension alias
13819 for DW_AT_call_return_pc. */
13820 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13821 }
96408a79
SA
13822 if (!attr)
13823 {
b98664d3 13824 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13825 "DIE %s [in module %s]"),
13826 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13827 return;
13828 }
95f982e5 13829 pc = attr->as_address () + baseaddr;
3e29f34a 13830 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13831
13832 if (cu->call_site_htab == NULL)
13833 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13834 NULL, &objfile->objfile_obstack,
13835 hashtab_obstack_allocate, NULL);
13836 call_site_local.pc = pc;
13837 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13838 if (*slot != NULL)
13839 {
b98664d3 13840 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13841 "DIE %s [in module %s]"),
13842 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13843 objfile_name (objfile));
96408a79
SA
13844 return;
13845 }
13846
13847 /* Count parameters at the caller. */
13848
13849 nparams = 0;
13850 for (child_die = die->child; child_die && child_die->tag;
436c571c 13851 child_die = child_die->sibling)
96408a79 13852 {
216f72a1
JK
13853 if (child_die->tag != DW_TAG_call_site_parameter
13854 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13855 {
b98664d3 13856 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13857 "DW_TAG_call_site child DIE %s [in module %s]"),
13858 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13859 objfile_name (objfile));
96408a79
SA
13860 continue;
13861 }
13862
13863 nparams++;
13864 }
13865
224c3ddb
SM
13866 call_site
13867 = ((struct call_site *)
13868 obstack_alloc (&objfile->objfile_obstack,
13869 sizeof (*call_site)
13870 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13871 *slot = call_site;
13872 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13873 call_site->pc = pc;
13874
216f72a1
JK
13875 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13876 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13877 {
13878 struct die_info *func_die;
13879
13880 /* Skip also over DW_TAG_inlined_subroutine. */
13881 for (func_die = die->parent;
13882 func_die && func_die->tag != DW_TAG_subprogram
13883 && func_die->tag != DW_TAG_subroutine_type;
13884 func_die = func_die->parent);
13885
216f72a1
JK
13886 /* DW_AT_call_all_calls is a superset
13887 of DW_AT_call_all_tail_calls. */
96408a79 13888 if (func_die
216f72a1 13889 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13890 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13891 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13892 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13893 {
13894 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13895 not complete. But keep CALL_SITE for look ups via call_site_htab,
13896 both the initial caller containing the real return address PC and
13897 the final callee containing the current PC of a chain of tail
13898 calls do not need to have the tail call list complete. But any
13899 function candidate for a virtual tail call frame searched via
13900 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13901 determined unambiguously. */
13902 }
13903 else
13904 {
13905 struct type *func_type = NULL;
13906
13907 if (func_die)
13908 func_type = get_die_type (func_die, cu);
13909 if (func_type != NULL)
13910 {
78134374 13911 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13912
13913 /* Enlist this call site to the function. */
13914 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13915 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13916 }
13917 else
b98664d3 13918 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13919 "DIE %s [in module %s]"),
13920 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13921 }
13922 }
13923
216f72a1
JK
13924 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13925 if (attr == NULL)
13926 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13927 if (attr == NULL)
13928 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13929 if (attr == NULL)
216f72a1
JK
13930 {
13931 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13932 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13933 }
96408a79 13934 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13935 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13936 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13937 else if (attr->form_is_block ())
96408a79
SA
13938 {
13939 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13940 struct dwarf_block *block = attr->as_block ();
96408a79 13941
8d749320 13942 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13943 dlbaton->data = block->data;
13944 dlbaton->size = block->size;
a50264ba 13945 dlbaton->per_objfile = per_objfile;
96408a79
SA
13946 dlbaton->per_cu = cu->per_cu;
13947
13948 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13949 }
cd6c91b4 13950 else if (attr->form_is_ref ())
96408a79 13951 {
96408a79
SA
13952 struct dwarf2_cu *target_cu = cu;
13953 struct die_info *target_die;
13954
ac9ec31b 13955 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13956 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13957 if (die_is_declaration (target_die, target_cu))
13958 {
7d45c7c3 13959 const char *target_physname;
9112db09
JK
13960
13961 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13962 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13963 if (target_physname == NULL)
9112db09 13964 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13965 if (target_physname == NULL)
b98664d3 13966 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13967 "physname, for referencing DIE %s [in module %s]"),
13968 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13969 else
7d455152 13970 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13971 }
13972 else
13973 {
13974 CORE_ADDR lowpc;
13975
13976 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13977 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13978 <= PC_BOUNDS_INVALID)
b98664d3 13979 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13980 "low pc, for referencing DIE %s [in module %s]"),
13981 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13982 else
3e29f34a
MR
13983 {
13984 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13985 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13986 }
96408a79
SA
13987 }
13988 }
13989 else
b98664d3 13990 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13991 "block nor reference, for DIE %s [in module %s]"),
13992 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13993
13994 call_site->per_cu = cu->per_cu;
9f47c707 13995 call_site->per_objfile = per_objfile;
96408a79
SA
13996
13997 for (child_die = die->child;
13998 child_die && child_die->tag;
436c571c 13999 child_die = child_die->sibling)
96408a79 14000 {
96408a79 14001 struct call_site_parameter *parameter;
1788b2d3 14002 struct attribute *loc, *origin;
96408a79 14003
216f72a1
JK
14004 if (child_die->tag != DW_TAG_call_site_parameter
14005 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14006 {
14007 /* Already printed the complaint above. */
14008 continue;
14009 }
14010
14011 gdb_assert (call_site->parameter_count < nparams);
14012 parameter = &call_site->parameter[call_site->parameter_count];
14013
1788b2d3
JK
14014 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14015 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14016 register is contained in DW_AT_call_value. */
96408a79 14017
24c5c679 14018 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14019 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14020 if (origin == NULL)
14021 {
14022 /* This was a pre-DWARF-5 GNU extension alias
14023 for DW_AT_call_parameter. */
14024 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14025 }
cd6c91b4 14026 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14027 {
1788b2d3 14028 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14029
0826b30a 14030 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14031 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14032 {
14033 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14034 binding can be done only inside one CU. Such referenced DIE
14035 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14036 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14037 "DW_TAG_call_site child DIE %s [in module %s]"),
14038 sect_offset_str (child_die->sect_off),
9c541725 14039 objfile_name (objfile));
d76b7dbc
JK
14040 continue;
14041 }
9c541725
PA
14042 parameter->u.param_cu_off
14043 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14044 }
4fc6c0d5 14045 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14046 {
b98664d3 14047 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14048 "DW_TAG_call_site child DIE %s [in module %s]"),
14049 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14050 continue;
14051 }
24c5c679 14052 else
96408a79 14053 {
9d2246fc
TT
14054 struct dwarf_block *block = loc->as_block ();
14055
24c5c679 14056 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14057 (block->data, &block->data[block->size]);
24c5c679
JK
14058 if (parameter->u.dwarf_reg != -1)
14059 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14060 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14061 &block->data[block->size],
24c5c679
JK
14062 &parameter->u.fb_offset))
14063 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14064 else
14065 {
b98664d3 14066 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14067 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14068 "DW_TAG_call_site child DIE %s "
24c5c679 14069 "[in module %s]"),
9d8780f0 14070 sect_offset_str (child_die->sect_off),
9c541725 14071 objfile_name (objfile));
24c5c679
JK
14072 continue;
14073 }
96408a79
SA
14074 }
14075
216f72a1
JK
14076 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14077 if (attr == NULL)
14078 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14079 if (attr == NULL || !attr->form_is_block ())
96408a79 14080 {
b98664d3 14081 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14082 "DW_TAG_call_site child DIE %s [in module %s]"),
14083 sect_offset_str (child_die->sect_off),
9c541725 14084 objfile_name (objfile));
96408a79
SA
14085 continue;
14086 }
9d2246fc
TT
14087
14088 struct dwarf_block *block = attr->as_block ();
14089 parameter->value = block->data;
14090 parameter->value_size = block->size;
96408a79
SA
14091
14092 /* Parameters are not pre-cleared by memset above. */
14093 parameter->data_value = NULL;
14094 parameter->data_value_size = 0;
14095 call_site->parameter_count++;
14096
216f72a1
JK
14097 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14098 if (attr == NULL)
14099 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14100 if (attr != nullptr)
96408a79 14101 {
4fc6c0d5 14102 if (!attr->form_is_block ())
b98664d3 14103 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14104 "DW_TAG_call_site child DIE %s [in module %s]"),
14105 sect_offset_str (child_die->sect_off),
9c541725 14106 objfile_name (objfile));
96408a79
SA
14107 else
14108 {
9d2246fc
TT
14109 block = attr->as_block ();
14110 parameter->data_value = block->data;
14111 parameter->data_value_size = block->size;
96408a79
SA
14112 }
14113 }
14114 }
14115}
14116
71a3c369
TT
14117/* Helper function for read_variable. If DIE represents a virtual
14118 table, then return the type of the concrete object that is
14119 associated with the virtual table. Otherwise, return NULL. */
14120
14121static struct type *
14122rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14123{
14124 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14125 if (attr == NULL)
14126 return NULL;
14127
14128 /* Find the type DIE. */
14129 struct die_info *type_die = NULL;
14130 struct dwarf2_cu *type_cu = cu;
14131
cd6c91b4 14132 if (attr->form_is_ref ())
71a3c369
TT
14133 type_die = follow_die_ref (die, attr, &type_cu);
14134 if (type_die == NULL)
14135 return NULL;
14136
14137 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14138 return NULL;
14139 return die_containing_type (type_die, type_cu);
14140}
14141
14142/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14143
14144static void
14145read_variable (struct die_info *die, struct dwarf2_cu *cu)
14146{
14147 struct rust_vtable_symbol *storage = NULL;
14148
14149 if (cu->language == language_rust)
14150 {
14151 struct type *containing_type = rust_containing_type (die, cu);
14152
14153 if (containing_type != NULL)
14154 {
5e22e966 14155 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14156
8c14c3a3 14157 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14158 storage->concrete_type = containing_type;
cf724bc9 14159 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14160 }
14161 }
14162
e4a62c65
TV
14163 struct symbol *res = new_symbol (die, NULL, cu, storage);
14164 struct attribute *abstract_origin
14165 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14166 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14167 if (res == NULL && loc && abstract_origin)
14168 {
14169 /* We have a variable without a name, but with a location and an abstract
14170 origin. This may be a concrete instance of an abstract variable
14171 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14172 later. */
14173 struct dwarf2_cu *origin_cu = cu;
14174 struct die_info *origin_die
14175 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14176 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14177 per_objfile->per_bfd->abstract_to_concrete
14178 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14179 }
71a3c369
TT
14180}
14181
43988095
JK
14182/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14183 reading .debug_rnglists.
14184 Callback's type should be:
14185 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14186 Return true if the attributes are present and valid, otherwise,
14187 return false. */
14188
14189template <typename Callback>
14190static bool
14191dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14192 dwarf_tag tag, Callback &&callback)
43988095 14193{
976ca316
SM
14194 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14195 struct objfile *objfile = per_objfile->objfile;
43988095 14196 bfd *obfd = objfile->obfd;
43988095 14197 /* Base address selection entry. */
2b24b6e4 14198 gdb::optional<CORE_ADDR> base;
43988095 14199 const gdb_byte *buffer;
43988095
JK
14200 CORE_ADDR baseaddr;
14201 bool overflow = false;
d0ce17d8
CT
14202 ULONGEST addr_index;
14203 struct dwarf2_section_info *rnglists_section;
43988095 14204
43988095 14205 base = cu->base_address;
d0ce17d8
CT
14206 rnglists_section = cu_debug_rnglists_section (cu, tag);
14207 rnglists_section->read (objfile);
43988095 14208
d0ce17d8 14209 if (offset >= rnglists_section->size)
43988095 14210 {
b98664d3 14211 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14212 offset);
14213 return false;
14214 }
d0ce17d8 14215 buffer = rnglists_section->buffer + offset;
43988095 14216
b3b3bada 14217 baseaddr = objfile->text_section_offset ();
43988095
JK
14218
14219 while (1)
14220 {
7814882a
JK
14221 /* Initialize it due to a false compiler warning. */
14222 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14223 const gdb_byte *buf_end = (rnglists_section->buffer
14224 + rnglists_section->size);
43988095
JK
14225 unsigned int bytes_read;
14226
14227 if (buffer == buf_end)
14228 {
14229 overflow = true;
14230 break;
14231 }
14232 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14233 switch (rlet)
14234 {
14235 case DW_RLE_end_of_list:
14236 break;
14237 case DW_RLE_base_address:
14238 if (buffer + cu->header.addr_size > buf_end)
14239 {
14240 overflow = true;
14241 break;
14242 }
c8a7a66f 14243 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14244 buffer += bytes_read;
14245 break;
d0ce17d8
CT
14246 case DW_RLE_base_addressx:
14247 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14248 buffer += bytes_read;
14249 base = read_addr_index (cu, addr_index);
14250 break;
43988095
JK
14251 case DW_RLE_start_length:
14252 if (buffer + cu->header.addr_size > buf_end)
14253 {
14254 overflow = true;
14255 break;
14256 }
c8a7a66f
TT
14257 range_beginning = cu->header.read_address (obfd, buffer,
14258 &bytes_read);
43988095
JK
14259 buffer += bytes_read;
14260 range_end = (range_beginning
14261 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14262 buffer += bytes_read;
14263 if (buffer > buf_end)
14264 {
14265 overflow = true;
14266 break;
14267 }
14268 break;
d0ce17d8
CT
14269 case DW_RLE_startx_length:
14270 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14271 buffer += bytes_read;
14272 range_beginning = read_addr_index (cu, addr_index);
14273 if (buffer > buf_end)
14274 {
14275 overflow = true;
14276 break;
14277 }
14278 range_end = (range_beginning
14279 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14280 buffer += bytes_read;
14281 break;
43988095
JK
14282 case DW_RLE_offset_pair:
14283 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14284 buffer += bytes_read;
14285 if (buffer > buf_end)
14286 {
14287 overflow = true;
14288 break;
14289 }
14290 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14291 buffer += bytes_read;
14292 if (buffer > buf_end)
14293 {
14294 overflow = true;
14295 break;
14296 }
14297 break;
14298 case DW_RLE_start_end:
14299 if (buffer + 2 * cu->header.addr_size > buf_end)
14300 {
14301 overflow = true;
14302 break;
14303 }
c8a7a66f
TT
14304 range_beginning = cu->header.read_address (obfd, buffer,
14305 &bytes_read);
43988095 14306 buffer += bytes_read;
c8a7a66f 14307 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14308 buffer += bytes_read;
14309 break;
d0ce17d8
CT
14310 case DW_RLE_startx_endx:
14311 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14312 buffer += bytes_read;
14313 range_beginning = read_addr_index (cu, addr_index);
14314 if (buffer > buf_end)
14315 {
14316 overflow = true;
14317 break;
14318 }
14319 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14320 buffer += bytes_read;
14321 range_end = read_addr_index (cu, addr_index);
14322 break;
43988095 14323 default:
b98664d3 14324 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14325 return false;
14326 }
14327 if (rlet == DW_RLE_end_of_list || overflow)
14328 break;
14329 if (rlet == DW_RLE_base_address)
14330 continue;
14331
43988095
JK
14332 if (range_beginning > range_end)
14333 {
14334 /* Inverted range entries are invalid. */
b98664d3 14335 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14336 return false;
14337 }
14338
14339 /* Empty range entries have no effect. */
14340 if (range_beginning == range_end)
14341 continue;
14342
d0ce17d8
CT
14343 /* Only DW_RLE_offset_pair needs the base address added. */
14344 if (rlet == DW_RLE_offset_pair)
14345 {
14346 if (!base.has_value ())
14347 {
14348 /* We have no valid base address for the DW_RLE_offset_pair. */
14349 complaint (_("Invalid .debug_rnglists data (no base address for "
14350 "DW_RLE_offset_pair)"));
14351 return false;
14352 }
14353
14354 range_beginning += *base;
14355 range_end += *base;
14356 }
43988095
JK
14357
14358 /* A not-uncommon case of bad debug info.
14359 Don't pollute the addrmap with bad data. */
14360 if (range_beginning + baseaddr == 0
976ca316 14361 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14362 {
b98664d3 14363 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14364 " [in module %s]"), objfile_name (objfile));
14365 continue;
14366 }
14367
14368 callback (range_beginning, range_end);
14369 }
14370
14371 if (overflow)
14372 {
b98664d3 14373 complaint (_("Offset %d is not terminated "
43988095
JK
14374 "for DW_AT_ranges attribute"),
14375 offset);
14376 return false;
14377 }
14378
14379 return true;
14380}
14381
14382/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14383 Callback's type should be:
14384 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14385 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14386
43988095 14387template <typename Callback>
43039443 14388static int
d0ce17d8 14389dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14390 Callback &&callback)
43039443 14391{
5e22e966
SM
14392 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14393 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14394 struct comp_unit_head *cu_header = &cu->header;
14395 bfd *obfd = objfile->obfd;
14396 unsigned int addr_size = cu_header->addr_size;
14397 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14398 /* Base address selection entry. */
2b24b6e4 14399 gdb::optional<CORE_ADDR> base;
43039443 14400 unsigned int dummy;
d521ce57 14401 const gdb_byte *buffer;
ff013f42 14402 CORE_ADDR baseaddr;
43039443 14403
43988095 14404 if (cu_header->version >= 5)
d0ce17d8 14405 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14406
d00adf39 14407 base = cu->base_address;
43039443 14408
5e22e966
SM
14409 per_objfile->per_bfd->ranges.read (objfile);
14410 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14411 {
b98664d3 14412 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14413 offset);
14414 return 0;
14415 }
5e22e966 14416 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14417
b3b3bada 14418 baseaddr = objfile->text_section_offset ();
ff013f42 14419
43039443
JK
14420 while (1)
14421 {
14422 CORE_ADDR range_beginning, range_end;
14423
c8a7a66f 14424 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14425 buffer += addr_size;
c8a7a66f 14426 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14427 buffer += addr_size;
14428 offset += 2 * addr_size;
14429
14430 /* An end of list marker is a pair of zero addresses. */
14431 if (range_beginning == 0 && range_end == 0)
14432 /* Found the end of list entry. */
14433 break;
14434
14435 /* Each base address selection entry is a pair of 2 values.
14436 The first is the largest possible address, the second is
14437 the base address. Check for a base address here. */
14438 if ((range_beginning & mask) == mask)
14439 {
28d2bfb9
AB
14440 /* If we found the largest possible address, then we already
14441 have the base address in range_end. */
14442 base = range_end;
43039443
JK
14443 continue;
14444 }
14445
2b24b6e4 14446 if (!base.has_value ())
43039443
JK
14447 {
14448 /* We have no valid base address for the ranges
14449 data. */
b98664d3 14450 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14451 return 0;
14452 }
14453
9277c30c
UW
14454 if (range_beginning > range_end)
14455 {
14456 /* Inverted range entries are invalid. */
b98664d3 14457 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14458 return 0;
14459 }
14460
14461 /* Empty range entries have no effect. */
14462 if (range_beginning == range_end)
14463 continue;
14464
2b24b6e4
TT
14465 range_beginning += *base;
14466 range_end += *base;
43039443 14467
01093045
DE
14468 /* A not-uncommon case of bad debug info.
14469 Don't pollute the addrmap with bad data. */
14470 if (range_beginning + baseaddr == 0
5e22e966 14471 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14472 {
b98664d3 14473 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14474 " [in module %s]"), objfile_name (objfile));
01093045
DE
14475 continue;
14476 }
14477
5f46c5a5
JK
14478 callback (range_beginning, range_end);
14479 }
14480
14481 return 1;
14482}
14483
14484/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14485 Return 1 if the attributes are present and valid, otherwise, return 0.
14486 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14487
14488static int
14489dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14490 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14491 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14492{
5e22e966 14493 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14494 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14495 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14496 int low_set = 0;
14497 CORE_ADDR low = 0;
14498 CORE_ADDR high = 0;
14499 int retval;
14500
d0ce17d8 14501 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14502 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14503 {
9277c30c 14504 if (ranges_pst != NULL)
3e29f34a
MR
14505 {
14506 CORE_ADDR lowpc;
14507 CORE_ADDR highpc;
14508
79748972
TT
14509 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14510 range_beginning + baseaddr)
14511 - baseaddr);
14512 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14513 range_end + baseaddr)
14514 - baseaddr);
d320c2b5
TT
14515 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14516 lowpc, highpc - 1, ranges_pst);
3e29f34a 14517 }
ff013f42 14518
43039443
JK
14519 /* FIXME: This is recording everything as a low-high
14520 segment of consecutive addresses. We should have a
14521 data structure for discontiguous block ranges
14522 instead. */
14523 if (! low_set)
14524 {
14525 low = range_beginning;
14526 high = range_end;
14527 low_set = 1;
14528 }
14529 else
14530 {
14531 if (range_beginning < low)
14532 low = range_beginning;
14533 if (range_end > high)
14534 high = range_end;
14535 }
5f46c5a5
JK
14536 });
14537 if (!retval)
14538 return 0;
43039443
JK
14539
14540 if (! low_set)
14541 /* If the first entry is an end-of-list marker, the range
14542 describes an empty scope, i.e. no instructions. */
14543 return 0;
14544
14545 if (low_return)
14546 *low_return = low;
14547 if (high_return)
14548 *high_return = high;
14549 return 1;
14550}
14551
3a2b436a
JK
14552/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14553 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14554 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14555
3a2b436a 14556static enum pc_bounds_kind
af34e669 14557dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14558 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14559 dwarf2_psymtab *pst)
c906108c 14560{
976ca316 14561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14562 struct attribute *attr;
91da1414 14563 struct attribute *attr_high;
af34e669
DJ
14564 CORE_ADDR low = 0;
14565 CORE_ADDR high = 0;
e385593e 14566 enum pc_bounds_kind ret;
c906108c 14567
91da1414
MW
14568 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14569 if (attr_high)
af34e669 14570 {
e142c38c 14571 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14572 if (attr != nullptr)
91da1414 14573 {
95f982e5
TT
14574 low = attr->as_address ();
14575 high = attr_high->as_address ();
cd6c91b4 14576 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14577 high += low;
91da1414 14578 }
af34e669
DJ
14579 else
14580 /* Found high w/o low attribute. */
e385593e 14581 return PC_BOUNDS_INVALID;
af34e669
DJ
14582
14583 /* Found consecutive range of addresses. */
3a2b436a 14584 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14585 }
c906108c 14586 else
af34e669 14587 {
e142c38c 14588 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14589 if (attr != NULL)
14590 {
18a8505e 14591 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14592 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14593 in DW_TAG_compile_unit of DWO files.
14594
14595 Attributes of the form DW_FORM_rnglistx have already had their
14596 value changed by read_rnglist_index and already include
14597 DW_AT_rnglists_base, so don't need to add the ranges base,
14598 either. */
14599 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14600 && attr->form != DW_FORM_rnglistx);
ab435259
DE
14601 unsigned int ranges_offset = (DW_UNSND (attr)
14602 + (need_ranges_base
14603 ? cu->ranges_base
14604 : 0));
2e3cf129 14605
af34e669 14606 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14607 .debug_ranges section. */
d0ce17d8
CT
14608 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14609 die->tag))
e385593e 14610 return PC_BOUNDS_INVALID;
43039443 14611 /* Found discontinuous range of addresses. */
3a2b436a 14612 ret = PC_BOUNDS_RANGES;
af34e669 14613 }
e385593e
JK
14614 else
14615 return PC_BOUNDS_NOT_PRESENT;
af34e669 14616 }
c906108c 14617
48fbe735 14618 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14619 if (high <= low)
e385593e 14620 return PC_BOUNDS_INVALID;
c906108c
SS
14621
14622 /* When using the GNU linker, .gnu.linkonce. sections are used to
14623 eliminate duplicate copies of functions and vtables and such.
14624 The linker will arbitrarily choose one and discard the others.
14625 The AT_*_pc values for such functions refer to local labels in
14626 these sections. If the section from that file was discarded, the
14627 labels are not in the output, so the relocs get a value of 0.
14628 If this is a discarded function, mark the pc bounds as invalid,
14629 so that GDB will ignore it. */
976ca316 14630 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14631 return PC_BOUNDS_INVALID;
c906108c
SS
14632
14633 *lowpc = low;
96408a79
SA
14634 if (highpc)
14635 *highpc = high;
af34e669 14636 return ret;
c906108c
SS
14637}
14638
b084d499
JB
14639/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14640 its low and high PC addresses. Do nothing if these addresses could not
14641 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14642 and HIGHPC to the high address if greater than HIGHPC. */
14643
14644static void
14645dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14646 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14647 struct dwarf2_cu *cu)
14648{
14649 CORE_ADDR low, high;
14650 struct die_info *child = die->child;
14651
e385593e 14652 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14653 {
325fac50
PA
14654 *lowpc = std::min (*lowpc, low);
14655 *highpc = std::max (*highpc, high);
b084d499
JB
14656 }
14657
14658 /* If the language does not allow nested subprograms (either inside
14659 subprograms or lexical blocks), we're done. */
14660 if (cu->language != language_ada)
14661 return;
6e70227d 14662
b084d499
JB
14663 /* Check all the children of the given DIE. If it contains nested
14664 subprograms, then check their pc bounds. Likewise, we need to
14665 check lexical blocks as well, as they may also contain subprogram
14666 definitions. */
14667 while (child && child->tag)
14668 {
14669 if (child->tag == DW_TAG_subprogram
14670 || child->tag == DW_TAG_lexical_block)
14671 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14672 child = child->sibling;
b084d499
JB
14673 }
14674}
14675
fae299cd
DC
14676/* Get the low and high pc's represented by the scope DIE, and store
14677 them in *LOWPC and *HIGHPC. If the correct values can't be
14678 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14679
14680static void
14681get_scope_pc_bounds (struct die_info *die,
14682 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14683 struct dwarf2_cu *cu)
14684{
14685 CORE_ADDR best_low = (CORE_ADDR) -1;
14686 CORE_ADDR best_high = (CORE_ADDR) 0;
14687 CORE_ADDR current_low, current_high;
14688
3a2b436a 14689 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14690 >= PC_BOUNDS_RANGES)
fae299cd
DC
14691 {
14692 best_low = current_low;
14693 best_high = current_high;
14694 }
14695 else
14696 {
14697 struct die_info *child = die->child;
14698
14699 while (child && child->tag)
14700 {
14701 switch (child->tag) {
14702 case DW_TAG_subprogram:
b084d499 14703 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14704 break;
14705 case DW_TAG_namespace:
f55ee35c 14706 case DW_TAG_module:
fae299cd
DC
14707 /* FIXME: carlton/2004-01-16: Should we do this for
14708 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14709 that current GCC's always emit the DIEs corresponding
14710 to definitions of methods of classes as children of a
14711 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14712 the DIEs giving the declarations, which could be
14713 anywhere). But I don't see any reason why the
14714 standards says that they have to be there. */
14715 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14716
14717 if (current_low != ((CORE_ADDR) -1))
14718 {
325fac50
PA
14719 best_low = std::min (best_low, current_low);
14720 best_high = std::max (best_high, current_high);
fae299cd
DC
14721 }
14722 break;
14723 default:
0963b4bd 14724 /* Ignore. */
fae299cd
DC
14725 break;
14726 }
14727
436c571c 14728 child = child->sibling;
fae299cd
DC
14729 }
14730 }
14731
14732 *lowpc = best_low;
14733 *highpc = best_high;
14734}
14735
801e3a5b
JB
14736/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14737 in DIE. */
380bca97 14738
801e3a5b
JB
14739static void
14740dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14741 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14742{
5e22e966 14743 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14744 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14745 struct attribute *attr;
91da1414 14746 struct attribute *attr_high;
801e3a5b 14747
91da1414
MW
14748 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14749 if (attr_high)
801e3a5b 14750 {
801e3a5b 14751 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14752 if (attr != nullptr)
801e3a5b 14753 {
95f982e5
TT
14754 CORE_ADDR low = attr->as_address ();
14755 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14756
cd6c91b4 14757 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14758 high += low;
9a619af0 14759
3e29f34a
MR
14760 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14761 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14762 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14763 }
14764 }
14765
14766 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14767 if (attr != nullptr)
801e3a5b 14768 {
18a8505e 14769 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14770 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14771 in DW_TAG_compile_unit of DWO files.
14772
14773 Attributes of the form DW_FORM_rnglistx have already had their
14774 value changed by read_rnglist_index and already include
14775 DW_AT_rnglists_base, so don't need to add the ranges base,
14776 either. */
14777 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14778 && attr->form != DW_FORM_rnglistx);
801e3a5b
JB
14779
14780 /* The value of the DW_AT_ranges attribute is the offset of the
14781 address range list in the .debug_ranges section. */
ab435259
DE
14782 unsigned long offset = (DW_UNSND (attr)
14783 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14784
2d5f09ec 14785 std::vector<blockrange> blockvec;
d0ce17d8 14786 dwarf2_ranges_process (offset, cu, die->tag,
5f46c5a5
JK
14787 [&] (CORE_ADDR start, CORE_ADDR end)
14788 {
58fdfd2c
JK
14789 start += baseaddr;
14790 end += baseaddr;
5f46c5a5
JK
14791 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14792 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14793 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14794 blockvec.emplace_back (start, end);
5f46c5a5 14795 });
2d5f09ec
KB
14796
14797 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14798 }
14799}
14800
685b1105
JK
14801/* Check whether the producer field indicates either of GCC < 4.6, or the
14802 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14803
685b1105
JK
14804static void
14805check_producer (struct dwarf2_cu *cu)
60d5a603 14806{
38360086 14807 int major, minor;
60d5a603
JK
14808
14809 if (cu->producer == NULL)
14810 {
14811 /* For unknown compilers expect their behavior is DWARF version
14812 compliant.
14813
14814 GCC started to support .debug_types sections by -gdwarf-4 since
14815 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14816 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14817 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14818 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14819 }
b1ffba5a 14820 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14821 {
38360086
MW
14822 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14823 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14824 }
5230b05a 14825 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14826 {
14827 cu->producer_is_icc = true;
14828 cu->producer_is_icc_lt_14 = major < 14;
14829 }
c258c396
JD
14830 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14831 cu->producer_is_codewarrior = true;
685b1105
JK
14832 else
14833 {
14834 /* For other non-GCC compilers, expect their behavior is DWARF version
14835 compliant. */
60d5a603
JK
14836 }
14837
9068261f 14838 cu->checked_producer = true;
685b1105 14839}
ba919b58 14840
685b1105
JK
14841/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14842 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14843 during 4.6.0 experimental. */
14844
9068261f 14845static bool
685b1105
JK
14846producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14847{
14848 if (!cu->checked_producer)
14849 check_producer (cu);
14850
14851 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14852}
14853
c258c396
JD
14854
14855/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14856 with incorrect is_stmt attributes. */
14857
14858static bool
14859producer_is_codewarrior (struct dwarf2_cu *cu)
14860{
14861 if (!cu->checked_producer)
14862 check_producer (cu);
14863
14864 return cu->producer_is_codewarrior;
14865}
14866
bf23a268
TT
14867/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14868 If that attribute is not available, return the appropriate
14869 default. */
60d5a603
JK
14870
14871static enum dwarf_access_attribute
bf23a268 14872dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14873{
bf23a268
TT
14874 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14875 if (attr != nullptr)
14876 {
14877 LONGEST value = attr->constant_value (-1);
14878 if (value == DW_ACCESS_public
14879 || value == DW_ACCESS_protected
14880 || value == DW_ACCESS_private)
14881 return (dwarf_access_attribute) value;
14882 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14883 plongest (value));
14884 }
14885
60d5a603
JK
14886 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14887 {
14888 /* The default DWARF 2 accessibility for members is public, the default
14889 accessibility for inheritance is private. */
14890
14891 if (die->tag != DW_TAG_inheritance)
14892 return DW_ACCESS_public;
14893 else
14894 return DW_ACCESS_private;
14895 }
14896 else
14897 {
14898 /* DWARF 3+ defines the default accessibility a different way. The same
14899 rules apply now for DW_TAG_inheritance as for the members and it only
14900 depends on the container kind. */
14901
14902 if (die->parent->tag == DW_TAG_class_type)
14903 return DW_ACCESS_private;
14904 else
14905 return DW_ACCESS_public;
14906 }
14907}
14908
74ac6d43
TT
14909/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14910 offset. If the attribute was not found return 0, otherwise return
14911 1. If it was found but could not properly be handled, set *OFFSET
14912 to 0. */
14913
14914static int
14915handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14916 LONGEST *offset)
14917{
14918 struct attribute *attr;
14919
14920 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14921 if (attr != NULL)
14922 {
14923 *offset = 0;
14924
14925 /* Note that we do not check for a section offset first here.
14926 This is because DW_AT_data_member_location is new in DWARF 4,
14927 so if we see it, we can assume that a constant form is really
14928 a constant and not a section offset. */
cd6c91b4 14929 if (attr->form_is_constant ())
0826b30a 14930 *offset = attr->constant_value (0);
cd6c91b4 14931 else if (attr->form_is_section_offset ())
74ac6d43 14932 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14933 else if (attr->form_is_block ())
9d2246fc 14934 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14935 else
14936 dwarf2_complex_location_expr_complaint ();
14937
14938 return 1;
14939 }
14940
14941 return 0;
14942}
14943
7d79de9a
TT
14944/* Look for DW_AT_data_member_location and store the results in FIELD. */
14945
14946static void
14947handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14948 struct field *field)
14949{
14950 struct attribute *attr;
14951
14952 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14953 if (attr != NULL)
14954 {
14955 if (attr->form_is_constant ())
14956 {
14957 LONGEST offset = attr->constant_value (0);
14958 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14959 }
14960 else if (attr->form_is_section_offset ())
14961 dwarf2_complex_location_expr_complaint ();
14962 else if (attr->form_is_block ())
14963 {
14964 bool handled;
9d2246fc 14965 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14966 if (handled)
14967 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14968 else
14969 {
5e22e966
SM
14970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14971 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14972 struct dwarf2_locexpr_baton *dlbaton
14973 = XOBNEW (&objfile->objfile_obstack,
14974 struct dwarf2_locexpr_baton);
9d2246fc
TT
14975 dlbaton->data = attr->as_block ()->data;
14976 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14977 /* When using this baton, we want to compute the address
14978 of the field, not the value. This is why
14979 is_reference is set to false here. */
14980 dlbaton->is_reference = false;
5e22e966 14981 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14982 dlbaton->per_cu = cu->per_cu;
14983
14984 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14985 }
14986 }
14987 else
14988 dwarf2_complex_location_expr_complaint ();
14989 }
14990}
14991
c906108c
SS
14992/* Add an aggregate field to the field list. */
14993
14994static void
107d2387 14995dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14996 struct dwarf2_cu *cu)
6e70227d 14997{
5e22e966 14998 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14999 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15000 struct nextfield *new_field;
15001 struct attribute *attr;
15002 struct field *fp;
15d034d0 15003 const char *fieldname = "";
c906108c 15004
7d0ccb61
DJ
15005 if (die->tag == DW_TAG_inheritance)
15006 {
be2daae6
TT
15007 fip->baseclasses.emplace_back ();
15008 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15009 }
15010 else
15011 {
be2daae6
TT
15012 fip->fields.emplace_back ();
15013 new_field = &fip->fields.back ();
7d0ccb61 15014 }
be2daae6 15015
9c6a1327
TT
15016 new_field->offset = die->sect_off;
15017
bf23a268 15018 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15019 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15020 fip->non_public_fields = true;
60d5a603 15021
e142c38c 15022 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15023 if (attr != nullptr)
23dca5c3 15024 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15025 else
15026 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15027
15028 fp = &new_field->field;
a9a9bd0f 15029
e142c38c 15030 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15031 {
a9a9bd0f 15032 /* Data member other than a C++ static data member. */
6e70227d 15033
c906108c 15034 /* Get type of field. */
5d14b6e5 15035 fp->set_type (die_type (die, cu));
c906108c 15036
d6a843b5 15037 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15038
c906108c 15039 /* Get bit size of field (zero if none). */
e142c38c 15040 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15041 if (attr != nullptr)
c906108c
SS
15042 {
15043 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15044 }
15045 else
15046 {
15047 FIELD_BITSIZE (*fp) = 0;
15048 }
15049
15050 /* Get bit offset of field. */
7d79de9a 15051 handle_data_member_location (die, cu, fp);
e142c38c 15052 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 15053 if (attr != nullptr)
c906108c 15054 {
d5a22e77 15055 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15056 {
15057 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15058 additional bit offset from the MSB of the containing
15059 anonymous object to the MSB of the field. We don't
15060 have to do anything special since we don't need to
15061 know the size of the anonymous object. */
f41f5e61 15062 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15063 }
15064 else
15065 {
15066 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15067 MSB of the anonymous object, subtract off the number of
15068 bits from the MSB of the field to the MSB of the
15069 object, and then subtract off the number of bits of
15070 the field itself. The result is the bit offset of
15071 the LSB of the field. */
c906108c
SS
15072 int anonymous_size;
15073 int bit_offset = DW_UNSND (attr);
15074
e142c38c 15075 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15076 if (attr != nullptr)
c906108c
SS
15077 {
15078 /* The size of the anonymous object containing
15079 the bit field is explicit, so use the
15080 indicated size (in bytes). */
15081 anonymous_size = DW_UNSND (attr);
15082 }
15083 else
15084 {
15085 /* The size of the anonymous object containing
15086 the bit field must be inferred from the type
15087 attribute of the data member containing the
15088 bit field. */
5d14b6e5 15089 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15090 }
f41f5e61
PA
15091 SET_FIELD_BITPOS (*fp,
15092 (FIELD_BITPOS (*fp)
15093 + anonymous_size * bits_per_byte
15094 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15095 }
15096 }
da5b30da
AA
15097 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15098 if (attr != NULL)
15099 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15100 + attr->constant_value (0)));
c906108c
SS
15101
15102 /* Get name of field. */
39cbfefa
DJ
15103 fieldname = dwarf2_name (die, cu);
15104 if (fieldname == NULL)
15105 fieldname = "";
d8151005
DJ
15106
15107 /* The name is already allocated along with this objfile, so we don't
15108 need to duplicate it for the type. */
15109 fp->name = fieldname;
c906108c
SS
15110
15111 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15112 pointer or virtual base class pointer) to private. */
e142c38c 15113 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15114 {
d48cc9dd 15115 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15116 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15117 fip->non_public_fields = true;
c906108c
SS
15118 }
15119 }
a9a9bd0f 15120 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15121 {
a9a9bd0f
DC
15122 /* C++ static member. */
15123
15124 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15125 is a declaration, but all versions of G++ as of this writing
15126 (so through at least 3.2.1) incorrectly generate
15127 DW_TAG_variable tags. */
6e70227d 15128
ff355380 15129 const char *physname;
c906108c 15130
a9a9bd0f 15131 /* Get name of field. */
39cbfefa
DJ
15132 fieldname = dwarf2_name (die, cu);
15133 if (fieldname == NULL)
c906108c
SS
15134 return;
15135
254e6b9e 15136 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15137 if (attr
15138 /* Only create a symbol if this is an external value.
15139 new_symbol checks this and puts the value in the global symbol
15140 table, which we want. If it is not external, new_symbol
15141 will try to put the value in cu->list_in_scope which is wrong. */
15142 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15143 {
15144 /* A static const member, not much different than an enum as far as
15145 we're concerned, except that we can support more types. */
15146 new_symbol (die, NULL, cu);
15147 }
15148
2df3850c 15149 /* Get physical name. */
ff355380 15150 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15151
d8151005
DJ
15152 /* The name is already allocated along with this objfile, so we don't
15153 need to duplicate it for the type. */
15154 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15155 fp->set_type (die_type (die, cu));
d8151005 15156 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15157 }
15158 else if (die->tag == DW_TAG_inheritance)
15159 {
74ac6d43 15160 /* C++ base class field. */
7d79de9a 15161 handle_data_member_location (die, cu, fp);
c906108c 15162 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15163 fp->set_type (die_type (die, cu));
15164 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15165 }
2ddeaf8a
TT
15166 else
15167 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15168}
15169
883fd55a
KS
15170/* Can the type given by DIE define another type? */
15171
15172static bool
15173type_can_define_types (const struct die_info *die)
15174{
15175 switch (die->tag)
15176 {
15177 case DW_TAG_typedef:
15178 case DW_TAG_class_type:
15179 case DW_TAG_structure_type:
15180 case DW_TAG_union_type:
15181 case DW_TAG_enumeration_type:
15182 return true;
15183
15184 default:
15185 return false;
15186 }
15187}
15188
15189/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15190
15191static void
883fd55a
KS
15192dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15193 struct dwarf2_cu *cu)
6e70227d 15194{
be2daae6
TT
15195 struct decl_field fp;
15196 memset (&fp, 0, sizeof (fp));
98751a41 15197
883fd55a 15198 gdb_assert (type_can_define_types (die));
98751a41 15199
883fd55a 15200 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15201 fp.name = dwarf2_name (die, cu);
15202 fp.type = read_type_die (die, cu);
98751a41 15203
c191a687 15204 /* Save accessibility. */
bf23a268 15205 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15206 switch (accessibility)
15207 {
15208 case DW_ACCESS_public:
15209 /* The assumed value if neither private nor protected. */
15210 break;
15211 case DW_ACCESS_private:
be2daae6 15212 fp.is_private = 1;
c191a687
KS
15213 break;
15214 case DW_ACCESS_protected:
be2daae6 15215 fp.is_protected = 1;
c191a687 15216 break;
c191a687
KS
15217 }
15218
883fd55a 15219 if (die->tag == DW_TAG_typedef)
be2daae6 15220 fip->typedef_field_list.push_back (fp);
883fd55a 15221 else
be2daae6 15222 fip->nested_types_list.push_back (fp);
98751a41
JK
15223}
15224
9c6a1327
TT
15225/* A convenience typedef that's used when finding the discriminant
15226 field for a variant part. */
1b95cdb7
SM
15227typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15228 offset_map_type;
9c6a1327
TT
15229
15230/* Compute the discriminant range for a given variant. OBSTACK is
15231 where the results will be stored. VARIANT is the variant to
15232 process. IS_UNSIGNED indicates whether the discriminant is signed
15233 or unsigned. */
15234
15235static const gdb::array_view<discriminant_range>
15236convert_variant_range (struct obstack *obstack, const variant_field &variant,
15237 bool is_unsigned)
15238{
15239 std::vector<discriminant_range> ranges;
15240
15241 if (variant.default_branch)
15242 return {};
15243
15244 if (variant.discr_list_data == nullptr)
15245 {
15246 discriminant_range r
15247 = {variant.discriminant_value, variant.discriminant_value};
15248 ranges.push_back (r);
15249 }
15250 else
15251 {
15252 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15253 variant.discr_list_data->size);
15254 while (!data.empty ())
15255 {
15256 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15257 {
15258 complaint (_("invalid discriminant marker: %d"), data[0]);
15259 break;
15260 }
15261 bool is_range = data[0] == DW_DSC_range;
15262 data = data.slice (1);
15263
15264 ULONGEST low, high;
15265 unsigned int bytes_read;
15266
15267 if (data.empty ())
15268 {
15269 complaint (_("DW_AT_discr_list missing low value"));
15270 break;
15271 }
15272 if (is_unsigned)
15273 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15274 else
15275 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15276 &bytes_read);
15277 data = data.slice (bytes_read);
15278
15279 if (is_range)
15280 {
15281 if (data.empty ())
15282 {
15283 complaint (_("DW_AT_discr_list missing high value"));
15284 break;
15285 }
15286 if (is_unsigned)
15287 high = read_unsigned_leb128 (nullptr, data.data (),
15288 &bytes_read);
15289 else
15290 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15291 &bytes_read);
15292 data = data.slice (bytes_read);
15293 }
15294 else
15295 high = low;
15296
15297 ranges.push_back ({ low, high });
15298 }
15299 }
15300
15301 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15302 ranges.size ());
15303 std::copy (ranges.begin (), ranges.end (), result);
15304 return gdb::array_view<discriminant_range> (result, ranges.size ());
15305}
15306
15307static const gdb::array_view<variant_part> create_variant_parts
15308 (struct obstack *obstack,
15309 const offset_map_type &offset_map,
15310 struct field_info *fi,
15311 const std::vector<variant_part_builder> &variant_parts);
15312
15313/* Fill in a "struct variant" for a given variant field. RESULT is
15314 the variant to fill in. OBSTACK is where any needed allocations
15315 will be done. OFFSET_MAP holds the mapping from section offsets to
15316 fields for the type. FI describes the fields of the type we're
15317 processing. FIELD is the variant field we're converting. */
15318
15319static void
15320create_one_variant (variant &result, struct obstack *obstack,
15321 const offset_map_type &offset_map,
15322 struct field_info *fi, const variant_field &field)
15323{
15324 result.discriminants = convert_variant_range (obstack, field, false);
15325 result.first_field = field.first_field + fi->baseclasses.size ();
15326 result.last_field = field.last_field + fi->baseclasses.size ();
15327 result.parts = create_variant_parts (obstack, offset_map, fi,
15328 field.variant_parts);
15329}
15330
15331/* Fill in a "struct variant_part" for a given variant part. RESULT
15332 is the variant part to fill in. OBSTACK is where any needed
15333 allocations will be done. OFFSET_MAP holds the mapping from
15334 section offsets to fields for the type. FI describes the fields of
15335 the type we're processing. BUILDER is the variant part to be
15336 converted. */
15337
15338static void
15339create_one_variant_part (variant_part &result,
15340 struct obstack *obstack,
15341 const offset_map_type &offset_map,
15342 struct field_info *fi,
15343 const variant_part_builder &builder)
15344{
15345 auto iter = offset_map.find (builder.discriminant_offset);
15346 if (iter == offset_map.end ())
15347 {
15348 result.discriminant_index = -1;
15349 /* Doesn't matter. */
15350 result.is_unsigned = false;
15351 }
15352 else
15353 {
15354 result.discriminant_index = iter->second;
15355 result.is_unsigned
c6d940a9 15356 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15357 }
15358
15359 size_t n = builder.variants.size ();
15360 variant *output = new (obstack) variant[n];
15361 for (size_t i = 0; i < n; ++i)
15362 create_one_variant (output[i], obstack, offset_map, fi,
15363 builder.variants[i]);
15364
15365 result.variants = gdb::array_view<variant> (output, n);
15366}
15367
15368/* Create a vector of variant parts that can be attached to a type.
15369 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15370 holds the mapping from section offsets to fields for the type. FI
15371 describes the fields of the type we're processing. VARIANT_PARTS
15372 is the vector to convert. */
15373
15374static const gdb::array_view<variant_part>
15375create_variant_parts (struct obstack *obstack,
15376 const offset_map_type &offset_map,
15377 struct field_info *fi,
15378 const std::vector<variant_part_builder> &variant_parts)
15379{
15380 if (variant_parts.empty ())
15381 return {};
15382
15383 size_t n = variant_parts.size ();
15384 variant_part *result = new (obstack) variant_part[n];
15385 for (size_t i = 0; i < n; ++i)
15386 create_one_variant_part (result[i], obstack, offset_map, fi,
15387 variant_parts[i]);
15388
15389 return gdb::array_view<variant_part> (result, n);
15390}
15391
15392/* Compute the variant part vector for FIP, attaching it to TYPE when
15393 done. */
15394
15395static void
15396add_variant_property (struct field_info *fip, struct type *type,
15397 struct dwarf2_cu *cu)
15398{
15399 /* Map section offsets of fields to their field index. Note the
15400 field index here does not take the number of baseclasses into
15401 account. */
15402 offset_map_type offset_map;
15403 for (int i = 0; i < fip->fields.size (); ++i)
15404 offset_map[fip->fields[i].offset] = i;
15405
5e22e966 15406 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15407 gdb::array_view<variant_part> parts
15408 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15409 fip->variant_parts);
15410
15411 struct dynamic_prop prop;
8c2e4e06
SM
15412 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15413 obstack_copy (&objfile->objfile_obstack, &parts,
15414 sizeof (parts)));
9c6a1327 15415
5c54719c 15416 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15417}
15418
c906108c
SS
15419/* Create the vector of fields, and attach it to the type. */
15420
15421static void
fba45db2 15422dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15423 struct dwarf2_cu *cu)
c906108c 15424{
317f7127 15425 int nfields = fip->nfields ();
c906108c
SS
15426
15427 /* Record the field count, allocate space for the array of fields,
15428 and create blank accessibility bitfields if necessary. */
5e33d5f4 15429 type->set_num_fields (nfields);
3cabb6b0
SM
15430 type->set_fields
15431 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15432
b4ba55a1 15433 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15434 {
15435 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15436
15437 TYPE_FIELD_PRIVATE_BITS (type) =
15438 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15439 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15440
15441 TYPE_FIELD_PROTECTED_BITS (type) =
15442 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15443 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15444
774b6a14
TT
15445 TYPE_FIELD_IGNORE_BITS (type) =
15446 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15447 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15448 }
15449
15450 /* If the type has baseclasses, allocate and clear a bit vector for
15451 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15452 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15453 {
be2daae6 15454 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15455 unsigned char *pointer;
c906108c
SS
15456
15457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15458 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15459 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15460 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15461 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15462 }
15463
9c6a1327
TT
15464 if (!fip->variant_parts.empty ())
15465 add_variant_property (fip, type, cu);
2ddeaf8a 15466
be2daae6
TT
15467 /* Copy the saved-up fields into the field vector. */
15468 for (int i = 0; i < nfields; ++i)
c906108c 15469 {
be2daae6
TT
15470 struct nextfield &field
15471 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15472 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15473
ceacbf6e 15474 type->field (i) = field.field;
be2daae6 15475 switch (field.accessibility)
c906108c 15476 {
c5aa993b 15477 case DW_ACCESS_private:
b4ba55a1 15478 if (cu->language != language_ada)
be2daae6 15479 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15480 break;
c906108c 15481
c5aa993b 15482 case DW_ACCESS_protected:
b4ba55a1 15483 if (cu->language != language_ada)
be2daae6 15484 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15485 break;
c906108c 15486
c5aa993b
JM
15487 case DW_ACCESS_public:
15488 break;
c906108c 15489
c5aa993b
JM
15490 default:
15491 /* Unknown accessibility. Complain and treat it as public. */
15492 {
b98664d3 15493 complaint (_("unsupported accessibility %d"),
be2daae6 15494 field.accessibility);
c5aa993b
JM
15495 }
15496 break;
c906108c 15497 }
be2daae6 15498 if (i < fip->baseclasses.size ())
c906108c 15499 {
be2daae6 15500 switch (field.virtuality)
c906108c 15501 {
c5aa993b
JM
15502 case DW_VIRTUALITY_virtual:
15503 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15504 if (cu->language == language_ada)
a73c6dcd 15505 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15506 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15507 break;
c906108c
SS
15508 }
15509 }
c906108c
SS
15510 }
15511}
15512
7d27a96d
TT
15513/* Return true if this member function is a constructor, false
15514 otherwise. */
15515
15516static int
15517dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15518{
15519 const char *fieldname;
fe978cb0 15520 const char *type_name;
7d27a96d
TT
15521 int len;
15522
15523 if (die->parent == NULL)
15524 return 0;
15525
15526 if (die->parent->tag != DW_TAG_structure_type
15527 && die->parent->tag != DW_TAG_union_type
15528 && die->parent->tag != DW_TAG_class_type)
15529 return 0;
15530
15531 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15532 type_name = dwarf2_name (die->parent, cu);
15533 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15534 return 0;
15535
15536 len = strlen (fieldname);
fe978cb0
PA
15537 return (strncmp (fieldname, type_name, len) == 0
15538 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15539}
15540
c906108c
SS
15541/* Add a member function to the proper fieldlist. */
15542
15543static void
107d2387 15544dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15545 struct type *type, struct dwarf2_cu *cu)
c906108c 15546{
5e22e966 15547 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15548 struct attribute *attr;
c906108c 15549 int i;
be2daae6 15550 struct fnfieldlist *flp = nullptr;
c906108c 15551 struct fn_field *fnp;
15d034d0 15552 const char *fieldname;
f792889a 15553 struct type *this_type;
c906108c 15554
b4ba55a1 15555 if (cu->language == language_ada)
a73c6dcd 15556 error (_("unexpected member function in Ada type"));
b4ba55a1 15557
2df3850c 15558 /* Get name of member function. */
39cbfefa
DJ
15559 fieldname = dwarf2_name (die, cu);
15560 if (fieldname == NULL)
2df3850c 15561 return;
c906108c 15562
c906108c 15563 /* Look up member function name in fieldlist. */
be2daae6 15564 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15565 {
27bfe10e 15566 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15567 {
15568 flp = &fip->fnfieldlists[i];
15569 break;
15570 }
c906108c
SS
15571 }
15572
be2daae6
TT
15573 /* Create a new fnfieldlist if necessary. */
15574 if (flp == nullptr)
c906108c 15575 {
be2daae6
TT
15576 fip->fnfieldlists.emplace_back ();
15577 flp = &fip->fnfieldlists.back ();
c906108c 15578 flp->name = fieldname;
be2daae6 15579 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15580 }
15581
be2daae6
TT
15582 /* Create a new member function field and add it to the vector of
15583 fnfieldlists. */
15584 flp->fnfields.emplace_back ();
15585 fnp = &flp->fnfields.back ();
3da10d80
KS
15586
15587 /* Delay processing of the physname until later. */
9c37b5ae 15588 if (cu->language == language_cplus)
be2daae6
TT
15589 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15590 die, cu);
3da10d80
KS
15591 else
15592 {
1d06ead6 15593 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15594 fnp->physname = physname ? physname : "";
15595 }
15596
c906108c 15597 fnp->type = alloc_type (objfile);
f792889a 15598 this_type = read_type_die (die, cu);
78134374 15599 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15600 {
1f704f76 15601 int nparams = this_type->num_fields ();
c906108c 15602
f792889a 15603 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15604 of the method itself (TYPE_CODE_METHOD). */
15605 smash_to_method_type (fnp->type, type,
f792889a 15606 TYPE_TARGET_TYPE (this_type),
80fc5e77 15607 this_type->fields (),
1f704f76 15608 this_type->num_fields (),
a409645d 15609 this_type->has_varargs ());
c906108c
SS
15610
15611 /* Handle static member functions.
c5aa993b 15612 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15613 member functions. G++ helps GDB by marking the first
15614 parameter for non-static member functions (which is the this
15615 pointer) as artificial. We obtain this information from
15616 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15617 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15618 fnp->voffset = VOFFSET_STATIC;
15619 }
15620 else
b98664d3 15621 complaint (_("member function type missing for '%s'"),
3da10d80 15622 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15623
15624 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15625 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15626 fnp->fcontext = die_containing_type (die, cu);
c906108c 15627
3e43a32a
MS
15628 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15629 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15630
15631 /* Get accessibility. */
bf23a268 15632 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15633 switch (accessibility)
c906108c 15634 {
60d5a603
JK
15635 case DW_ACCESS_private:
15636 fnp->is_private = 1;
15637 break;
15638 case DW_ACCESS_protected:
15639 fnp->is_protected = 1;
15640 break;
c906108c
SS
15641 }
15642
b02dede2 15643 /* Check for artificial methods. */
e142c38c 15644 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15645 if (attr && DW_UNSND (attr) != 0)
15646 fnp->is_artificial = 1;
15647
e35000a7
TBA
15648 /* Check for defaulted methods. */
15649 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15650 if (attr != nullptr)
15651 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15652
15653 /* Check for deleted methods. */
15654 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15655 if (attr != nullptr && DW_UNSND (attr) != 0)
15656 fnp->is_deleted = 1;
15657
7d27a96d
TT
15658 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15659
0d564a31 15660 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15661 function. For older versions of GCC, this is an offset in the
15662 appropriate virtual table, as specified by DW_AT_containing_type.
15663 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15664 to the object address. */
15665
e142c38c 15666 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15667 if (attr != nullptr)
8e19ed76 15668 {
9d2246fc 15669 if (attr->form_is_block () && attr->as_block ()->size > 0)
8e19ed76 15670 {
9d2246fc
TT
15671 struct dwarf_block *block = attr->as_block ();
15672
15673 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15674 {
15675 /* Old-style GCC. */
9d2246fc 15676 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15677 }
9d2246fc
TT
15678 else if (block->data[0] == DW_OP_deref
15679 || (block->size > 1
15680 && block->data[0] == DW_OP_deref_size
15681 && block->data[1] == cu->header.addr_size))
aec5aa8b 15682 {
9d2246fc 15683 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15684 if ((fnp->voffset % cu->header.addr_size) != 0)
15685 dwarf2_complex_location_expr_complaint ();
15686 else
15687 fnp->voffset /= cu->header.addr_size;
15688 fnp->voffset += 2;
15689 }
15690 else
15691 dwarf2_complex_location_expr_complaint ();
15692
15693 if (!fnp->fcontext)
7e993ebf
KS
15694 {
15695 /* If there is no `this' field and no DW_AT_containing_type,
15696 we cannot actually find a base class context for the
15697 vtable! */
1f704f76 15698 if (this_type->num_fields () == 0
7e993ebf
KS
15699 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15700 {
b98664d3 15701 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15702 "function \"%s\" (offset %s)"),
15703 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15704 }
15705 else
15706 {
15707 fnp->fcontext
940da03e 15708 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15709 }
15710 }
aec5aa8b 15711 }
cd6c91b4 15712 else if (attr->form_is_section_offset ())
8e19ed76 15713 {
4d3c2250 15714 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15715 }
15716 else
15717 {
4d3c2250
KB
15718 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15719 fieldname);
8e19ed76 15720 }
0d564a31 15721 }
d48cc9dd
DJ
15722 else
15723 {
15724 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15725 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15726 {
15727 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15728 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15729 "but the vtable offset is not specified"),
9d8780f0 15730 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15731 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15732 TYPE_CPLUS_DYNAMIC (type) = 1;
15733 }
15734 }
c906108c
SS
15735}
15736
15737/* Create the vector of member function fields, and attach it to the type. */
15738
15739static void
fba45db2 15740dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15741 struct dwarf2_cu *cu)
c906108c 15742{
b4ba55a1 15743 if (cu->language == language_ada)
a73c6dcd 15744 error (_("unexpected member functions in Ada type"));
b4ba55a1 15745
c906108c
SS
15746 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15747 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15748 TYPE_ALLOC (type,
15749 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15750
be2daae6 15751 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15752 {
be2daae6 15753 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15754 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15755
be2daae6
TT
15756 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15757 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15758 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15759 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15760
15761 for (int k = 0; k < nf.fnfields.size (); ++k)
15762 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15763 }
15764
be2daae6 15765 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15766}
15767
1168df01
JB
15768/* Returns non-zero if NAME is the name of a vtable member in CU's
15769 language, zero otherwise. */
15770static int
15771is_vtable_name (const char *name, struct dwarf2_cu *cu)
15772{
15773 static const char vptr[] = "_vptr";
15774
9c37b5ae
TT
15775 /* Look for the C++ form of the vtable. */
15776 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15777 return 1;
15778
15779 return 0;
15780}
15781
c0dd20ea 15782/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15783 functions, with the ABI-specified layout. If TYPE describes
15784 such a structure, smash it into a member function type.
61049d3b
DJ
15785
15786 GCC shouldn't do this; it should just output pointer to member DIEs.
15787 This is GCC PR debug/28767. */
c0dd20ea 15788
0b92b5bb
TT
15789static void
15790quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15791{
09e2d7c7 15792 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15793
15794 /* Check for a structure with no name and two children. */
1f704f76 15795 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15796 return;
c0dd20ea
DJ
15797
15798 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15799 if (TYPE_FIELD_NAME (type, 0) == NULL
15800 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15801 || TYPE_FIELD_NAME (type, 1) == NULL
15802 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15803 return;
c0dd20ea
DJ
15804
15805 /* Find the type of the method. */
940da03e 15806 pfn_type = type->field (0).type ();
c0dd20ea 15807 if (pfn_type == NULL
78134374
SM
15808 || pfn_type->code () != TYPE_CODE_PTR
15809 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15810 return;
c0dd20ea
DJ
15811
15812 /* Look for the "this" argument. */
15813 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15814 if (pfn_type->num_fields () == 0
940da03e
SM
15815 /* || pfn_type->field (0).type () == NULL */
15816 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15817 return;
c0dd20ea 15818
940da03e 15819 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15820 new_type = alloc_type (objfile);
09e2d7c7 15821 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15822 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15823 pfn_type->has_varargs ());
0b92b5bb 15824 smash_to_methodptr_type (type, new_type);
c0dd20ea 15825}
1168df01 15826
2b4424c3
TT
15827/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15828 appropriate error checking and issuing complaints if there is a
15829 problem. */
15830
15831static ULONGEST
15832get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15833{
15834 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15835
15836 if (attr == nullptr)
15837 return 0;
15838
cd6c91b4 15839 if (!attr->form_is_constant ())
2b4424c3 15840 {
b98664d3 15841 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15842 " - DIE at %s [in module %s]"),
15843 sect_offset_str (die->sect_off),
5e22e966 15844 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15845 return 0;
15846 }
15847
15848 ULONGEST align;
15849 if (attr->form == DW_FORM_sdata)
15850 {
1bc397c5 15851 LONGEST val = attr->as_signed ();
2b4424c3
TT
15852 if (val < 0)
15853 {
b98664d3 15854 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15855 " - DIE at %s [in module %s]"),
15856 sect_offset_str (die->sect_off),
5e22e966 15857 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15858 return 0;
15859 }
15860 align = val;
15861 }
15862 else
15863 align = DW_UNSND (attr);
15864
15865 if (align == 0)
15866 {
b98664d3 15867 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15868 " - DIE at %s [in module %s]"),
15869 sect_offset_str (die->sect_off),
5e22e966 15870 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15871 return 0;
15872 }
15873 if ((align & (align - 1)) != 0)
15874 {
b98664d3 15875 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15876 " - DIE at %s [in module %s]"),
15877 sect_offset_str (die->sect_off),
5e22e966 15878 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15879 return 0;
15880 }
15881
15882 return align;
15883}
15884
15885/* If the DIE has a DW_AT_alignment attribute, use its value to set
15886 the alignment for TYPE. */
15887
15888static void
15889maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15890 struct type *type)
15891{
15892 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15893 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15894 " - DIE at %s [in module %s]"),
15895 sect_offset_str (die->sect_off),
5e22e966 15896 objfile_name (cu->per_objfile->objfile));
2b4424c3 15897}
685b1105 15898
e35000a7
TBA
15899/* Check if the given VALUE is a valid enum dwarf_calling_convention
15900 constant for a type, according to DWARF5 spec, Table 5.5. */
15901
15902static bool
15903is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15904{
15905 switch (value)
15906 {
15907 case DW_CC_normal:
15908 case DW_CC_pass_by_reference:
15909 case DW_CC_pass_by_value:
15910 return true;
15911
15912 default:
15913 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15914 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15915 return false;
15916 }
15917}
15918
d0922fcf
TBA
15919/* Check if the given VALUE is a valid enum dwarf_calling_convention
15920 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15921 also according to GNU-specific values (see include/dwarf2.h). */
15922
15923static bool
15924is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15925{
15926 switch (value)
15927 {
15928 case DW_CC_normal:
15929 case DW_CC_program:
15930 case DW_CC_nocall:
15931 return true;
15932
15933 case DW_CC_GNU_renesas_sh:
15934 case DW_CC_GNU_borland_fastcall_i386:
15935 case DW_CC_GDB_IBM_OpenCL:
15936 return true;
15937
15938 default:
15939 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15940 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15941 return false;
15942 }
15943}
15944
c906108c 15945/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15946 (definition) to create a type for the structure or union. Fill in
15947 the type's name and general properties; the members will not be
83655187
DE
15948 processed until process_structure_scope. A symbol table entry for
15949 the type will also not be done until process_structure_scope (assuming
15950 the type has a name).
c906108c 15951
c767944b
DJ
15952 NOTE: we need to call these functions regardless of whether or not the
15953 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15954 structure or union. This gets the type entered into our set of
83655187 15955 user defined types. */
c906108c 15956
f792889a 15957static struct type *
134d01f1 15958read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15959{
5e22e966 15960 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15961 struct type *type;
15962 struct attribute *attr;
15d034d0 15963 const char *name;
c906108c 15964
348e048f
DE
15965 /* If the definition of this type lives in .debug_types, read that type.
15966 Don't follow DW_AT_specification though, that will take us back up
15967 the chain and we want to go down. */
052c8bb8 15968 attr = die->attr (DW_AT_signature);
435d3d88 15969 if (attr != nullptr)
348e048f 15970 {
ac9ec31b 15971 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15972
ac9ec31b 15973 /* The type's CU may not be the same as CU.
02142a6c 15974 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15975 return set_die_type (die, type, cu);
15976 }
15977
c0dd20ea 15978 type = alloc_type (objfile);
c906108c 15979 INIT_CPLUS_SPECIFIC (type);
93311388 15980
39cbfefa
DJ
15981 name = dwarf2_name (die, cu);
15982 if (name != NULL)
c906108c 15983 {
987504bb 15984 if (cu->language == language_cplus
c44af4eb
TT
15985 || cu->language == language_d
15986 || cu->language == language_rust)
63d06c5c 15987 {
15d034d0 15988 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15989
15990 /* dwarf2_full_name might have already finished building the DIE's
15991 type. If so, there is no need to continue. */
15992 if (get_die_type (die, cu) != NULL)
15993 return get_die_type (die, cu);
15994
d0e39ea2 15995 type->set_name (full_name);
63d06c5c
DC
15996 }
15997 else
15998 {
d8151005
DJ
15999 /* The name is already allocated along with this objfile, so
16000 we don't need to duplicate it for the type. */
d0e39ea2 16001 type->set_name (name);
63d06c5c 16002 }
c906108c
SS
16003 }
16004
16005 if (die->tag == DW_TAG_structure_type)
16006 {
67607e24 16007 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16008 }
16009 else if (die->tag == DW_TAG_union_type)
16010 {
67607e24 16011 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16012 }
16013 else
16014 {
67607e24 16015 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16016 }
16017
0cc2414c
TT
16018 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16019 TYPE_DECLARED_CLASS (type) = 1;
16020
e35000a7
TBA
16021 /* Store the calling convention in the type if it's available in
16022 the die. Otherwise the calling convention remains set to
16023 the default value DW_CC_normal. */
16024 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16025 if (attr != nullptr
16026 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
16027 {
16028 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16029 TYPE_CPLUS_CALLING_CONVENTION (type)
16030 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16031 }
16032
e142c38c 16033 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16034 if (attr != nullptr)
c906108c 16035 {
cd6c91b4 16036 if (attr->form_is_constant ())
155bfbd3
JB
16037 TYPE_LENGTH (type) = DW_UNSND (attr);
16038 else
16039 {
f8e89861 16040 struct dynamic_prop prop;
293e7e51 16041 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16042 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
16043 TYPE_LENGTH (type) = 0;
16044 }
c906108c
SS
16045 }
16046 else
16047 {
16048 TYPE_LENGTH (type) = 0;
16049 }
16050
2b4424c3
TT
16051 maybe_set_alignment (cu, die, type);
16052
5230b05a 16053 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16054 {
5230b05a
WT
16055 /* ICC<14 does not output the required DW_AT_declaration on
16056 incomplete types, but gives them a size of zero. */
b4b73759 16057 type->set_is_stub (true);
685b1105
JK
16058 }
16059 else
9baccff6 16060 type->set_stub_is_supported (true);
685b1105 16061
dc718098 16062 if (die_is_declaration (die, cu))
b4b73759 16063 type->set_is_stub (true);
a6c727b2
DJ
16064 else if (attr == NULL && die->child == NULL
16065 && producer_is_realview (cu->producer))
16066 /* RealView does not output the required DW_AT_declaration
16067 on incomplete types. */
b4b73759 16068 type->set_is_stub (true);
dc718098 16069
c906108c
SS
16070 /* We need to add the type field to the die immediately so we don't
16071 infinitely recurse when dealing with pointers to the structure
0963b4bd 16072 type within the structure itself. */
1c379e20 16073 set_die_type (die, type, cu);
c906108c 16074
7e314c57
JK
16075 /* set_die_type should be already done. */
16076 set_descriptive_type (type, die, cu);
16077
c767944b
DJ
16078 return type;
16079}
16080
9c6a1327
TT
16081static void handle_struct_member_die
16082 (struct die_info *child_die,
16083 struct type *type,
16084 struct field_info *fi,
16085 std::vector<struct symbol *> *template_args,
16086 struct dwarf2_cu *cu);
16087
16088/* A helper for handle_struct_member_die that handles
16089 DW_TAG_variant_part. */
16090
16091static void
16092handle_variant_part (struct die_info *die, struct type *type,
16093 struct field_info *fi,
16094 std::vector<struct symbol *> *template_args,
16095 struct dwarf2_cu *cu)
16096{
16097 variant_part_builder *new_part;
16098 if (fi->current_variant_part == nullptr)
16099 {
16100 fi->variant_parts.emplace_back ();
16101 new_part = &fi->variant_parts.back ();
16102 }
16103 else if (!fi->current_variant_part->processing_variant)
16104 {
16105 complaint (_("nested DW_TAG_variant_part seen "
16106 "- DIE at %s [in module %s]"),
16107 sect_offset_str (die->sect_off),
5e22e966 16108 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16109 return;
16110 }
16111 else
16112 {
16113 variant_field &current = fi->current_variant_part->variants.back ();
16114 current.variant_parts.emplace_back ();
16115 new_part = &current.variant_parts.back ();
16116 }
16117
16118 /* When we recurse, we want callees to add to this new variant
16119 part. */
16120 scoped_restore save_current_variant_part
16121 = make_scoped_restore (&fi->current_variant_part, new_part);
16122
16123 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16124 if (discr == NULL)
16125 {
16126 /* It's a univariant form, an extension we support. */
16127 }
16128 else if (discr->form_is_ref ())
16129 {
16130 struct dwarf2_cu *target_cu = cu;
16131 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16132
16133 new_part->discriminant_offset = target_die->sect_off;
16134 }
16135 else
16136 {
16137 complaint (_("DW_AT_discr does not have DIE reference form"
16138 " - DIE at %s [in module %s]"),
16139 sect_offset_str (die->sect_off),
5e22e966 16140 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16141 }
16142
16143 for (die_info *child_die = die->child;
16144 child_die != NULL;
16145 child_die = child_die->sibling)
16146 handle_struct_member_die (child_die, type, fi, template_args, cu);
16147}
16148
16149/* A helper for handle_struct_member_die that handles
16150 DW_TAG_variant. */
16151
16152static void
16153handle_variant (struct die_info *die, struct type *type,
16154 struct field_info *fi,
16155 std::vector<struct symbol *> *template_args,
16156 struct dwarf2_cu *cu)
16157{
16158 if (fi->current_variant_part == nullptr)
16159 {
16160 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16161 "- DIE at %s [in module %s]"),
16162 sect_offset_str (die->sect_off),
5e22e966 16163 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16164 return;
16165 }
16166 if (fi->current_variant_part->processing_variant)
16167 {
16168 complaint (_("nested DW_TAG_variant seen "
16169 "- DIE at %s [in module %s]"),
16170 sect_offset_str (die->sect_off),
5e22e966 16171 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16172 return;
16173 }
16174
16175 scoped_restore save_processing_variant
16176 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16177 true);
16178
16179 fi->current_variant_part->variants.emplace_back ();
16180 variant_field &variant = fi->current_variant_part->variants.back ();
16181 variant.first_field = fi->fields.size ();
16182
16183 /* In a variant we want to get the discriminant and also add a
16184 field for our sole member child. */
16185 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16186 if (discr == nullptr)
16187 {
16188 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16189 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16190 variant.default_branch = true;
16191 else
9d2246fc 16192 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16193 }
16194 else
16195 variant.discriminant_value = DW_UNSND (discr);
16196
16197 for (die_info *variant_child = die->child;
16198 variant_child != NULL;
16199 variant_child = variant_child->sibling)
16200 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16201
16202 variant.last_field = fi->fields.size ();
16203}
16204
2ddeaf8a
TT
16205/* A helper for process_structure_scope that handles a single member
16206 DIE. */
16207
16208static void
16209handle_struct_member_die (struct die_info *child_die, struct type *type,
16210 struct field_info *fi,
16211 std::vector<struct symbol *> *template_args,
16212 struct dwarf2_cu *cu)
16213{
16214 if (child_die->tag == DW_TAG_member
9c6a1327 16215 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16216 {
16217 /* NOTE: carlton/2002-11-05: A C++ static data member
16218 should be a DW_TAG_member that is a declaration, but
16219 all versions of G++ as of this writing (so through at
16220 least 3.2.1) incorrectly generate DW_TAG_variable
16221 tags for them instead. */
16222 dwarf2_add_field (fi, child_die, cu);
16223 }
16224 else if (child_die->tag == DW_TAG_subprogram)
16225 {
16226 /* Rust doesn't have member functions in the C++ sense.
16227 However, it does emit ordinary functions as children
16228 of a struct DIE. */
16229 if (cu->language == language_rust)
16230 read_func_scope (child_die, cu);
16231 else
16232 {
16233 /* C++ member function. */
16234 dwarf2_add_member_fn (fi, child_die, type, cu);
16235 }
16236 }
16237 else if (child_die->tag == DW_TAG_inheritance)
16238 {
16239 /* C++ base class field. */
16240 dwarf2_add_field (fi, child_die, cu);
16241 }
16242 else if (type_can_define_types (child_die))
16243 dwarf2_add_type_defn (fi, child_die, cu);
16244 else if (child_die->tag == DW_TAG_template_type_param
16245 || child_die->tag == DW_TAG_template_value_param)
16246 {
16247 struct symbol *arg = new_symbol (child_die, NULL, cu);
16248
16249 if (arg != NULL)
16250 template_args->push_back (arg);
16251 }
9c6a1327
TT
16252 else if (child_die->tag == DW_TAG_variant_part)
16253 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16254 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16255 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16256}
16257
c767944b
DJ
16258/* Finish creating a structure or union type, including filling in
16259 its members and creating a symbol for it. */
16260
16261static void
16262process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16263{
5e22e966 16264 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16265 struct die_info *child_die;
c767944b
DJ
16266 struct type *type;
16267
16268 type = get_die_type (die, cu);
16269 if (type == NULL)
16270 type = read_structure_type (die, cu);
16271
3e1d3d8c 16272 bool has_template_parameters = false;
e142c38c 16273 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16274 {
16275 struct field_info fi;
2f4732b0 16276 std::vector<struct symbol *> template_args;
c906108c 16277
639d11d3 16278 child_die = die->child;
c906108c
SS
16279
16280 while (child_die && child_die->tag)
16281 {
2ddeaf8a 16282 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16283 child_die = child_die->sibling;
c906108c
SS
16284 }
16285
34eaf542 16286 /* Attach template arguments to type. */
2f4732b0 16287 if (!template_args.empty ())
34eaf542 16288 {
3e1d3d8c 16289 has_template_parameters = true;
34eaf542 16290 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16291 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16292 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16293 = XOBNEWVEC (&objfile->objfile_obstack,
16294 struct symbol *,
16295 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16296 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16297 template_args.data (),
34eaf542
TT
16298 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16299 * sizeof (struct symbol *)));
34eaf542
TT
16300 }
16301
c906108c 16302 /* Attach fields and member functions to the type. */
317f7127 16303 if (fi.nfields () > 0)
e7c27a73 16304 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16305 if (!fi.fnfieldlists.empty ())
c906108c 16306 {
e7c27a73 16307 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16308
c5aa993b 16309 /* Get the type which refers to the base class (possibly this
c906108c 16310 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16311 class from the DW_AT_containing_type attribute. This use of
16312 DW_AT_containing_type is a GNU extension. */
c906108c 16313
e142c38c 16314 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16315 {
e7c27a73 16316 struct type *t = die_containing_type (die, cu);
c906108c 16317
ae6ae975 16318 set_type_vptr_basetype (type, t);
c906108c
SS
16319 if (type == t)
16320 {
c906108c
SS
16321 int i;
16322
16323 /* Our own class provides vtbl ptr. */
1f704f76 16324 for (i = t->num_fields () - 1;
c906108c
SS
16325 i >= TYPE_N_BASECLASSES (t);
16326 --i)
16327 {
0d5cff50 16328 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16329
1168df01 16330 if (is_vtable_name (fieldname, cu))
c906108c 16331 {
ae6ae975 16332 set_type_vptr_fieldno (type, i);
c906108c
SS
16333 break;
16334 }
16335 }
16336
16337 /* Complain if virtual function table field not found. */
16338 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16339 complaint (_("virtual function table pointer "
3e43a32a 16340 "not found when defining class '%s'"),
7d93a1e0 16341 type->name () ? type->name () : "");
c906108c
SS
16342 }
16343 else
16344 {
ae6ae975 16345 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16346 }
16347 }
f6235d4c 16348 else if (cu->producer
61012eef 16349 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16350 {
16351 /* The IBM XLC compiler does not provide direct indication
16352 of the containing type, but the vtable pointer is
16353 always named __vfp. */
16354
16355 int i;
16356
1f704f76 16357 for (i = type->num_fields () - 1;
f6235d4c
EZ
16358 i >= TYPE_N_BASECLASSES (type);
16359 --i)
16360 {
16361 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16362 {
ae6ae975
DE
16363 set_type_vptr_fieldno (type, i);
16364 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16365 break;
16366 }
16367 }
16368 }
c906108c 16369 }
98751a41
JK
16370
16371 /* Copy fi.typedef_field_list linked list elements content into the
16372 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16373 if (!fi.typedef_field_list.empty ())
98751a41 16374 {
be2daae6 16375 int count = fi.typedef_field_list.size ();
98751a41 16376
a0d7a4ff 16377 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16378 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16379 = ((struct decl_field *)
be2daae6
TT
16380 TYPE_ALLOC (type,
16381 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16382 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16383
be2daae6
TT
16384 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16385 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16386 }
c767944b 16387
883fd55a
KS
16388 /* Copy fi.nested_types_list linked list elements content into the
16389 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16390 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16391 {
be2daae6 16392 int count = fi.nested_types_list.size ();
883fd55a
KS
16393
16394 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16395 TYPE_NESTED_TYPES_ARRAY (type)
16396 = ((struct decl_field *)
be2daae6
TT
16397 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16398 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16399
be2daae6
TT
16400 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16401 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16402 }
c906108c 16403 }
63d06c5c 16404
bb5ed363 16405 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16406 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16407 cu->rust_unions.push_back (type);
0b92b5bb 16408
90aeadfc
DC
16409 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16410 snapshots) has been known to create a die giving a declaration
16411 for a class that has, as a child, a die giving a definition for a
16412 nested class. So we have to process our children even if the
16413 current die is a declaration. Normally, of course, a declaration
16414 won't have any children at all. */
134d01f1 16415
ca040673
DE
16416 child_die = die->child;
16417
90aeadfc
DC
16418 while (child_die != NULL && child_die->tag)
16419 {
16420 if (child_die->tag == DW_TAG_member
16421 || child_die->tag == DW_TAG_variable
34eaf542
TT
16422 || child_die->tag == DW_TAG_inheritance
16423 || child_die->tag == DW_TAG_template_value_param
16424 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16425 {
90aeadfc 16426 /* Do nothing. */
134d01f1 16427 }
90aeadfc
DC
16428 else
16429 process_die (child_die, cu);
134d01f1 16430
436c571c 16431 child_die = child_die->sibling;
134d01f1
DJ
16432 }
16433
fa4028e9
JB
16434 /* Do not consider external references. According to the DWARF standard,
16435 these DIEs are identified by the fact that they have no byte_size
16436 attribute, and a declaration attribute. */
16437 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16438 || !die_is_declaration (die, cu)
16439 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16440 {
16441 struct symbol *sym = new_symbol (die, type, cu);
16442
16443 if (has_template_parameters)
16444 {
a776957c
TT
16445 struct symtab *symtab;
16446 if (sym != nullptr)
16447 symtab = symbol_symtab (sym);
16448 else if (cu->line_header != nullptr)
16449 {
16450 /* Any related symtab will do. */
16451 symtab
7ba99d21 16452 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16453 }
16454 else
16455 {
16456 symtab = nullptr;
16457 complaint (_("could not find suitable "
16458 "symtab for template parameter"
16459 " - DIE at %s [in module %s]"),
16460 sect_offset_str (die->sect_off),
16461 objfile_name (objfile));
16462 }
16463
16464 if (symtab != nullptr)
16465 {
16466 /* Make sure that the symtab is set on the new symbols.
16467 Even though they don't appear in this symtab directly,
16468 other parts of gdb assume that symbols do, and this is
16469 reasonably true. */
16470 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16471 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16472 }
3e1d3d8c
TT
16473 }
16474 }
134d01f1
DJ
16475}
16476
ed6acedd
TT
16477/* Assuming DIE is an enumeration type, and TYPE is its associated
16478 type, update TYPE using some information only available in DIE's
16479 children. In particular, the fields are computed. */
55426c9d
JB
16480
16481static void
16482update_enumeration_type_from_children (struct die_info *die,
16483 struct type *type,
16484 struct dwarf2_cu *cu)
16485{
60f7655a 16486 struct die_info *child_die;
55426c9d
JB
16487 int unsigned_enum = 1;
16488 int flag_enum = 1;
55426c9d 16489
8268c778 16490 auto_obstack obstack;
ed6acedd 16491 std::vector<struct field> fields;
55426c9d 16492
60f7655a
DE
16493 for (child_die = die->child;
16494 child_die != NULL && child_die->tag;
436c571c 16495 child_die = child_die->sibling)
55426c9d
JB
16496 {
16497 struct attribute *attr;
16498 LONGEST value;
16499 const gdb_byte *bytes;
16500 struct dwarf2_locexpr_baton *baton;
16501 const char *name;
60f7655a 16502
55426c9d
JB
16503 if (child_die->tag != DW_TAG_enumerator)
16504 continue;
16505
16506 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16507 if (attr == NULL)
16508 continue;
16509
16510 name = dwarf2_name (child_die, cu);
16511 if (name == NULL)
16512 name = "<anonymous enumerator>";
16513
16514 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16515 &value, &bytes, &baton);
16516 if (value < 0)
16517 {
16518 unsigned_enum = 0;
16519 flag_enum = 0;
16520 }
55426c9d 16521 else
edd45eb0
SM
16522 {
16523 if (count_one_bits_ll (value) >= 2)
16524 flag_enum = 0;
edd45eb0 16525 }
55426c9d 16526
ed6acedd
TT
16527 fields.emplace_back ();
16528 struct field &field = fields.back ();
16529 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16530 SET_FIELD_ENUMVAL (field, value);
16531 }
16532
16533 if (!fields.empty ())
16534 {
5e33d5f4 16535 type->set_num_fields (fields.size ());
3cabb6b0
SM
16536 type->set_fields
16537 ((struct field *)
16538 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16539 memcpy (type->fields (), fields.data (),
ed6acedd 16540 sizeof (struct field) * fields.size ());
55426c9d
JB
16541 }
16542
16543 if (unsigned_enum)
653223d3
SM
16544 type->set_is_unsigned (true);
16545
55426c9d
JB
16546 if (flag_enum)
16547 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16548}
16549
134d01f1
DJ
16550/* Given a DW_AT_enumeration_type die, set its type. We do not
16551 complete the type's fields yet, or create any symbols. */
c906108c 16552
f792889a 16553static struct type *
134d01f1 16554read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16555{
5e22e966 16556 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16557 struct type *type;
c906108c 16558 struct attribute *attr;
0114d602 16559 const char *name;
134d01f1 16560
348e048f
DE
16561 /* If the definition of this type lives in .debug_types, read that type.
16562 Don't follow DW_AT_specification though, that will take us back up
16563 the chain and we want to go down. */
052c8bb8 16564 attr = die->attr (DW_AT_signature);
435d3d88 16565 if (attr != nullptr)
348e048f 16566 {
ac9ec31b 16567 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16568
ac9ec31b 16569 /* The type's CU may not be the same as CU.
02142a6c 16570 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16571 return set_die_type (die, type, cu);
16572 }
16573
c906108c
SS
16574 type = alloc_type (objfile);
16575
67607e24 16576 type->set_code (TYPE_CODE_ENUM);
94af9270 16577 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16578 if (name != NULL)
d0e39ea2 16579 type->set_name (name);
c906108c 16580
0626fc76
TT
16581 attr = dwarf2_attr (die, DW_AT_type, cu);
16582 if (attr != NULL)
16583 {
16584 struct type *underlying_type = die_type (die, cu);
16585
16586 TYPE_TARGET_TYPE (type) = underlying_type;
16587 }
16588
e142c38c 16589 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16590 if (attr != nullptr)
c906108c
SS
16591 {
16592 TYPE_LENGTH (type) = DW_UNSND (attr);
16593 }
16594 else
16595 {
16596 TYPE_LENGTH (type) = 0;
16597 }
16598
2b4424c3
TT
16599 maybe_set_alignment (cu, die, type);
16600
137033e9
JB
16601 /* The enumeration DIE can be incomplete. In Ada, any type can be
16602 declared as private in the package spec, and then defined only
16603 inside the package body. Such types are known as Taft Amendment
16604 Types. When another package uses such a type, an incomplete DIE
16605 may be generated by the compiler. */
02eb380e 16606 if (die_is_declaration (die, cu))
b4b73759 16607 type->set_is_stub (true);
02eb380e 16608
0626fc76
TT
16609 /* If this type has an underlying type that is not a stub, then we
16610 may use its attributes. We always use the "unsigned" attribute
16611 in this situation, because ordinarily we guess whether the type
16612 is unsigned -- but the guess can be wrong and the underlying type
16613 can tell us the reality. However, we defer to a local size
16614 attribute if one exists, because this lets the compiler override
16615 the underlying type if needed. */
e46d3488 16616 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16617 {
9e7c9a03
HD
16618 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16619 underlying_type = check_typedef (underlying_type);
653223d3
SM
16620
16621 type->set_is_unsigned (underlying_type->is_unsigned ());
16622
0626fc76 16623 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16624 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16625
2b4424c3 16626 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16627 && TYPE_RAW_ALIGN (underlying_type) != 0)
16628 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16629 }
16630
3d567982
TT
16631 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16632
ed6acedd
TT
16633 set_die_type (die, type, cu);
16634
16635 /* Finish the creation of this type by using the enum's children.
16636 Note that, as usual, this must come after set_die_type to avoid
16637 infinite recursion when trying to compute the names of the
16638 enumerators. */
16639 update_enumeration_type_from_children (die, type, cu);
16640
16641 return type;
134d01f1
DJ
16642}
16643
16644/* Given a pointer to a die which begins an enumeration, process all
16645 the dies that define the members of the enumeration, and create the
16646 symbol for the enumeration type.
16647
16648 NOTE: We reverse the order of the element list. */
16649
16650static void
16651process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16652{
f792889a 16653 struct type *this_type;
134d01f1 16654
f792889a
DJ
16655 this_type = get_die_type (die, cu);
16656 if (this_type == NULL)
16657 this_type = read_enumeration_type (die, cu);
9dc481d3 16658
639d11d3 16659 if (die->child != NULL)
c906108c 16660 {
9dc481d3 16661 struct die_info *child_die;
15d034d0 16662 const char *name;
9dc481d3 16663
639d11d3 16664 child_die = die->child;
c906108c
SS
16665 while (child_die && child_die->tag)
16666 {
16667 if (child_die->tag != DW_TAG_enumerator)
16668 {
e7c27a73 16669 process_die (child_die, cu);
c906108c
SS
16670 }
16671 else
16672 {
39cbfefa
DJ
16673 name = dwarf2_name (child_die, cu);
16674 if (name)
ed6acedd 16675 new_symbol (child_die, this_type, cu);
c906108c
SS
16676 }
16677
436c571c 16678 child_die = child_die->sibling;
c906108c 16679 }
c906108c 16680 }
134d01f1 16681
6c83ed52
TT
16682 /* If we are reading an enum from a .debug_types unit, and the enum
16683 is a declaration, and the enum is not the signatured type in the
16684 unit, then we do not want to add a symbol for it. Adding a
16685 symbol would in some cases obscure the true definition of the
16686 enum, giving users an incomplete type when the definition is
16687 actually available. Note that we do not want to do this for all
16688 enums which are just declarations, because C++0x allows forward
16689 enum declarations. */
3019eac3 16690 if (cu->per_cu->is_debug_types
6c83ed52
TT
16691 && die_is_declaration (die, cu))
16692 {
52dc124a 16693 struct signatured_type *sig_type;
6c83ed52 16694
c0f78cd4 16695 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16696 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16697 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16698 return;
16699 }
16700
f792889a 16701 new_symbol (die, this_type, cu);
c906108c
SS
16702}
16703
16704/* Extract all information from a DW_TAG_array_type DIE and put it in
16705 the DIE's type field. For now, this only handles one dimensional
16706 arrays. */
16707
f792889a 16708static struct type *
e7c27a73 16709read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16710{
5e22e966 16711 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16712 struct die_info *child_die;
7e314c57 16713 struct type *type;
c906108c 16714 struct type *element_type, *range_type, *index_type;
c906108c 16715 struct attribute *attr;
15d034d0 16716 const char *name;
a405673c 16717 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16718 unsigned int bit_stride = 0;
c906108c 16719
e7c27a73 16720 element_type = die_type (die, cu);
c906108c 16721
7e314c57
JK
16722 /* The die_type call above may have already set the type for this DIE. */
16723 type = get_die_type (die, cu);
16724 if (type)
16725 return type;
16726
dc53a7ad
JB
16727 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16728 if (attr != NULL)
a405673c
JB
16729 {
16730 int stride_ok;
293e7e51 16731 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16732
16733 byte_stride_prop
16734 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16735 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16736 prop_type);
a405673c
JB
16737 if (!stride_ok)
16738 {
b98664d3 16739 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16740 " - DIE at %s [in module %s]"),
16741 sect_offset_str (die->sect_off),
5e22e966 16742 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16743 /* Ignore this attribute. We will likely not be able to print
16744 arrays of this type correctly, but there is little we can do
16745 to help if we cannot read the attribute's value. */
16746 byte_stride_prop = NULL;
16747 }
16748 }
dc53a7ad
JB
16749
16750 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16751 if (attr != NULL)
16752 bit_stride = DW_UNSND (attr);
16753
c906108c
SS
16754 /* Irix 6.2 native cc creates array types without children for
16755 arrays with unspecified length. */
639d11d3 16756 if (die->child == NULL)
c906108c 16757 {
46bf5051 16758 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16759 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16760 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16761 byte_stride_prop, bit_stride);
f792889a 16762 return set_die_type (die, type, cu);
c906108c
SS
16763 }
16764
791afaa2 16765 std::vector<struct type *> range_types;
639d11d3 16766 child_die = die->child;
c906108c
SS
16767 while (child_die && child_die->tag)
16768 {
16769 if (child_die->tag == DW_TAG_subrange_type)
16770 {
f792889a 16771 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16772
f792889a 16773 if (child_type != NULL)
a02abb62 16774 {
0963b4bd
MS
16775 /* The range type was succesfully read. Save it for the
16776 array type creation. */
791afaa2 16777 range_types.push_back (child_type);
a02abb62 16778 }
c906108c 16779 }
436c571c 16780 child_die = child_die->sibling;
c906108c
SS
16781 }
16782
16783 /* Dwarf2 dimensions are output from left to right, create the
16784 necessary array types in backwards order. */
7ca2d3a3 16785
c906108c 16786 type = element_type;
7ca2d3a3
DL
16787
16788 if (read_array_order (die, cu) == DW_ORD_col_major)
16789 {
16790 int i = 0;
9a619af0 16791
791afaa2 16792 while (i < range_types.size ())
dc53a7ad 16793 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16794 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16795 }
16796 else
16797 {
791afaa2 16798 size_t ndim = range_types.size ();
7ca2d3a3 16799 while (ndim-- > 0)
dc53a7ad 16800 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16801 byte_stride_prop, bit_stride);
7ca2d3a3 16802 }
c906108c 16803
f5f8a009
EZ
16804 /* Understand Dwarf2 support for vector types (like they occur on
16805 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16806 array type. This is not part of the Dwarf2/3 standard yet, but a
16807 custom vendor extension. The main difference between a regular
16808 array and the vector variant is that vectors are passed by value
16809 to functions. */
e142c38c 16810 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16811 if (attr != nullptr)
ea37ba09 16812 make_vector_type (type);
f5f8a009 16813
dbc98a8b
KW
16814 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16815 implementation may choose to implement triple vectors using this
16816 attribute. */
16817 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16818 if (attr != nullptr)
dbc98a8b
KW
16819 {
16820 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16821 TYPE_LENGTH (type) = DW_UNSND (attr);
16822 else
b98664d3 16823 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16824 "than the total size of elements"));
dbc98a8b
KW
16825 }
16826
39cbfefa
DJ
16827 name = dwarf2_name (die, cu);
16828 if (name)
d0e39ea2 16829 type->set_name (name);
6e70227d 16830
2b4424c3
TT
16831 maybe_set_alignment (cu, die, type);
16832
0963b4bd 16833 /* Install the type in the die. */
7e314c57
JK
16834 set_die_type (die, type, cu);
16835
16836 /* set_die_type should be already done. */
b4ba55a1
JB
16837 set_descriptive_type (type, die, cu);
16838
7e314c57 16839 return type;
c906108c
SS
16840}
16841
7ca2d3a3 16842static enum dwarf_array_dim_ordering
6e70227d 16843read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16844{
16845 struct attribute *attr;
16846
16847 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16848
435d3d88 16849 if (attr != nullptr)
1bc397c5
TT
16850 {
16851 LONGEST val = attr->constant_value (-1);
16852 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16853 return (enum dwarf_array_dim_ordering) val;
16854 }
7ca2d3a3 16855
0963b4bd
MS
16856 /* GNU F77 is a special case, as at 08/2004 array type info is the
16857 opposite order to the dwarf2 specification, but data is still
16858 laid out as per normal fortran.
7ca2d3a3 16859
0963b4bd
MS
16860 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16861 version checking. */
7ca2d3a3 16862
905e0470
PM
16863 if (cu->language == language_fortran
16864 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16865 {
16866 return DW_ORD_row_major;
16867 }
16868
3a3440fb 16869 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
16870 {
16871 case array_column_major:
16872 return DW_ORD_col_major;
16873 case array_row_major:
16874 default:
16875 return DW_ORD_row_major;
16876 };
16877}
16878
72019c9c 16879/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16880 the DIE's type field. */
72019c9c 16881
f792889a 16882static struct type *
72019c9c
GM
16883read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16884{
7e314c57
JK
16885 struct type *domain_type, *set_type;
16886 struct attribute *attr;
f792889a 16887
7e314c57
JK
16888 domain_type = die_type (die, cu);
16889
16890 /* The die_type call above may have already set the type for this DIE. */
16891 set_type = get_die_type (die, cu);
16892 if (set_type)
16893 return set_type;
16894
16895 set_type = create_set_type (NULL, domain_type);
16896
16897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16898 if (attr != nullptr)
d09039dd 16899 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16900
2b4424c3
TT
16901 maybe_set_alignment (cu, die, set_type);
16902
f792889a 16903 return set_die_type (die, set_type, cu);
72019c9c 16904}
7ca2d3a3 16905
0971de02
TT
16906/* A helper for read_common_block that creates a locexpr baton.
16907 SYM is the symbol which we are marking as computed.
16908 COMMON_DIE is the DIE for the common block.
16909 COMMON_LOC is the location expression attribute for the common
16910 block itself.
16911 MEMBER_LOC is the location expression attribute for the particular
16912 member of the common block that we are processing.
16913 CU is the CU from which the above come. */
16914
16915static void
16916mark_common_block_symbol_computed (struct symbol *sym,
16917 struct die_info *common_die,
16918 struct attribute *common_loc,
16919 struct attribute *member_loc,
16920 struct dwarf2_cu *cu)
16921{
5e22e966 16922 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16923 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16924 struct dwarf2_locexpr_baton *baton;
16925 gdb_byte *ptr;
16926 unsigned int cu_off;
08feed99 16927 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16928 LONGEST offset = 0;
16929
16930 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16931 gdb_assert (common_loc->form_is_block ());
16932 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16933 || member_loc->form_is_constant ());
0971de02 16934
8d749320 16935 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16936 baton->per_objfile = per_objfile;
0971de02
TT
16937 baton->per_cu = cu->per_cu;
16938 gdb_assert (baton->per_cu);
16939
16940 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16941
cd6c91b4 16942 if (member_loc->form_is_constant ())
0971de02 16943 {
0826b30a 16944 offset = member_loc->constant_value (0);
0971de02
TT
16945 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16946 }
16947 else
9d2246fc 16948 baton->size += member_loc->as_block ()->size;
0971de02 16949
224c3ddb 16950 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16951 baton->data = ptr;
16952
16953 *ptr++ = DW_OP_call4;
9c541725 16954 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16955 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16956 ptr += 4;
16957
cd6c91b4 16958 if (member_loc->form_is_constant ())
0971de02
TT
16959 {
16960 *ptr++ = DW_OP_addr;
16961 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16962 ptr += cu->header.addr_size;
16963 }
16964 else
16965 {
16966 /* We have to copy the data here, because DW_OP_call4 will only
16967 use a DW_AT_location attribute. */
9d2246fc
TT
16968 struct dwarf_block *block = member_loc->as_block ();
16969 memcpy (ptr, block->data, block->size);
16970 ptr += block->size;
0971de02
TT
16971 }
16972
16973 *ptr++ = DW_OP_plus;
16974 gdb_assert (ptr - baton->data == baton->size);
16975
0971de02 16976 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16977 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16978}
16979
4357ac6c
TT
16980/* Create appropriate locally-scoped variables for all the
16981 DW_TAG_common_block entries. Also create a struct common_block
16982 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16983 is used to separate the common blocks name namespace from regular
4357ac6c 16984 variable names. */
c906108c
SS
16985
16986static void
e7c27a73 16987read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16988{
0971de02
TT
16989 struct attribute *attr;
16990
16991 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16992 if (attr != nullptr)
0971de02
TT
16993 {
16994 /* Support the .debug_loc offsets. */
4fc6c0d5 16995 if (attr->form_is_block ())
0971de02
TT
16996 {
16997 /* Ok. */
16998 }
cd6c91b4 16999 else if (attr->form_is_section_offset ())
0971de02
TT
17000 {
17001 dwarf2_complex_location_expr_complaint ();
17002 attr = NULL;
17003 }
17004 else
17005 {
17006 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17007 "common block member");
17008 attr = NULL;
17009 }
17010 }
17011
639d11d3 17012 if (die->child != NULL)
c906108c 17013 {
5e22e966 17014 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17015 struct die_info *child_die;
17016 size_t n_entries = 0, size;
17017 struct common_block *common_block;
17018 struct symbol *sym;
74ac6d43 17019
4357ac6c
TT
17020 for (child_die = die->child;
17021 child_die && child_die->tag;
436c571c 17022 child_die = child_die->sibling)
4357ac6c
TT
17023 ++n_entries;
17024
17025 size = (sizeof (struct common_block)
17026 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17027 common_block
17028 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17029 size);
4357ac6c
TT
17030 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17031 common_block->n_entries = 0;
17032
17033 for (child_die = die->child;
17034 child_die && child_die->tag;
436c571c 17035 child_die = child_die->sibling)
4357ac6c
TT
17036 {
17037 /* Create the symbol in the DW_TAG_common_block block in the current
17038 symbol scope. */
e7c27a73 17039 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17040 if (sym != NULL)
17041 {
17042 struct attribute *member_loc;
17043
17044 common_block->contents[common_block->n_entries++] = sym;
17045
17046 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17047 cu);
17048 if (member_loc)
17049 {
17050 /* GDB has handled this for a long time, but it is
17051 not specified by DWARF. It seems to have been
17052 emitted by gfortran at least as recently as:
17053 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17054 complaint (_("Variable in common block has "
0971de02 17055 "DW_AT_data_member_location "
9d8780f0
SM
17056 "- DIE at %s [in module %s]"),
17057 sect_offset_str (child_die->sect_off),
518817b3 17058 objfile_name (objfile));
0971de02 17059
cd6c91b4 17060 if (member_loc->form_is_section_offset ())
0971de02 17061 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17062 else if (member_loc->form_is_constant ()
4fc6c0d5 17063 || member_loc->form_is_block ())
0971de02 17064 {
435d3d88 17065 if (attr != nullptr)
0971de02
TT
17066 mark_common_block_symbol_computed (sym, die, attr,
17067 member_loc, cu);
17068 }
17069 else
17070 dwarf2_complex_location_expr_complaint ();
17071 }
17072 }
c906108c 17073 }
4357ac6c
TT
17074
17075 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17076 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17077 }
17078}
17079
0114d602 17080/* Create a type for a C++ namespace. */
d9fa45fe 17081
0114d602
DJ
17082static struct type *
17083read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17084{
5e22e966 17085 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17086 const char *previous_prefix, *name;
9219021c 17087 int is_anonymous;
0114d602
DJ
17088 struct type *type;
17089
17090 /* For extensions, reuse the type of the original namespace. */
17091 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17092 {
17093 struct die_info *ext_die;
17094 struct dwarf2_cu *ext_cu = cu;
9a619af0 17095
0114d602
DJ
17096 ext_die = dwarf2_extension (die, &ext_cu);
17097 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17098
17099 /* EXT_CU may not be the same as CU.
02142a6c 17100 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17101 return set_die_type (die, type, cu);
17102 }
9219021c 17103
e142c38c 17104 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17105
17106 /* Now build the name of the current namespace. */
17107
0114d602
DJ
17108 previous_prefix = determine_prefix (die, cu);
17109 if (previous_prefix[0] != '\0')
17110 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17111 previous_prefix, name, 0, cu);
0114d602
DJ
17112
17113 /* Create the type. */
19f392bc 17114 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17115
60531b24 17116 return set_die_type (die, type, cu);
0114d602
DJ
17117}
17118
22cee43f 17119/* Read a namespace scope. */
0114d602
DJ
17120
17121static void
17122read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17123{
5e22e966 17124 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17125 int is_anonymous;
9219021c 17126
5c4e30ca
DC
17127 /* Add a symbol associated to this if we haven't seen the namespace
17128 before. Also, add a using directive if it's an anonymous
17129 namespace. */
9219021c 17130
f2f0e013 17131 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17132 {
17133 struct type *type;
17134
0114d602 17135 type = read_type_die (die, cu);
e7c27a73 17136 new_symbol (die, type, cu);
5c4e30ca 17137
e8e80198 17138 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17139 if (is_anonymous)
0114d602
DJ
17140 {
17141 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17142
eb1e02fd 17143 std::vector<const char *> excludes;
804d2729 17144 add_using_directive (using_directives (cu),
7d93a1e0 17145 previous_prefix, type->name (), NULL,
eb1e02fd 17146 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17147 }
5c4e30ca 17148 }
9219021c 17149
639d11d3 17150 if (die->child != NULL)
d9fa45fe 17151 {
639d11d3 17152 struct die_info *child_die = die->child;
6e70227d 17153
d9fa45fe
DC
17154 while (child_die && child_die->tag)
17155 {
e7c27a73 17156 process_die (child_die, cu);
436c571c 17157 child_die = child_die->sibling;
d9fa45fe
DC
17158 }
17159 }
38d518c9
EZ
17160}
17161
f55ee35c
JK
17162/* Read a Fortran module as type. This DIE can be only a declaration used for
17163 imported module. Still we need that type as local Fortran "use ... only"
17164 declaration imports depend on the created type in determine_prefix. */
17165
17166static struct type *
17167read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17168{
5e22e966 17169 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17170 const char *module_name;
f55ee35c
JK
17171 struct type *type;
17172
17173 module_name = dwarf2_name (die, cu);
19f392bc 17174 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17175
f55ee35c
JK
17176 return set_die_type (die, type, cu);
17177}
17178
5d7cb8df
JK
17179/* Read a Fortran module. */
17180
17181static void
17182read_module (struct die_info *die, struct dwarf2_cu *cu)
17183{
17184 struct die_info *child_die = die->child;
530e8392
KB
17185 struct type *type;
17186
17187 type = read_type_die (die, cu);
17188 new_symbol (die, type, cu);
5d7cb8df 17189
5d7cb8df
JK
17190 while (child_die && child_die->tag)
17191 {
17192 process_die (child_die, cu);
436c571c 17193 child_die = child_die->sibling;
5d7cb8df
JK
17194 }
17195}
17196
38d518c9
EZ
17197/* Return the name of the namespace represented by DIE. Set
17198 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17199 namespace. */
17200
17201static const char *
e142c38c 17202namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17203{
17204 struct die_info *current_die;
17205 const char *name = NULL;
17206
17207 /* Loop through the extensions until we find a name. */
17208
17209 for (current_die = die;
17210 current_die != NULL;
f2f0e013 17211 current_die = dwarf2_extension (die, &cu))
38d518c9 17212 {
96553a0c
DE
17213 /* We don't use dwarf2_name here so that we can detect the absence
17214 of a name -> anonymous namespace. */
7d45c7c3 17215 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17216
38d518c9
EZ
17217 if (name != NULL)
17218 break;
17219 }
17220
17221 /* Is it an anonymous namespace? */
17222
17223 *is_anonymous = (name == NULL);
17224 if (*is_anonymous)
2b1dbab0 17225 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17226
17227 return name;
d9fa45fe
DC
17228}
17229
c906108c
SS
17230/* Extract all information from a DW_TAG_pointer_type DIE and add to
17231 the user defined type vector. */
17232
f792889a 17233static struct type *
e7c27a73 17234read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17235{
5e22e966 17236 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17237 struct comp_unit_head *cu_header = &cu->header;
c906108c 17238 struct type *type;
8b2dbe47
KB
17239 struct attribute *attr_byte_size;
17240 struct attribute *attr_address_class;
17241 int byte_size, addr_class;
7e314c57
JK
17242 struct type *target_type;
17243
17244 target_type = die_type (die, cu);
c906108c 17245
7e314c57
JK
17246 /* The die_type call above may have already set the type for this DIE. */
17247 type = get_die_type (die, cu);
17248 if (type)
17249 return type;
17250
17251 type = lookup_pointer_type (target_type);
8b2dbe47 17252
e142c38c 17253 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17254 if (attr_byte_size)
17255 byte_size = DW_UNSND (attr_byte_size);
c906108c 17256 else
8b2dbe47
KB
17257 byte_size = cu_header->addr_size;
17258
e142c38c 17259 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17260 if (attr_address_class)
17261 addr_class = DW_UNSND (attr_address_class);
17262 else
17263 addr_class = DW_ADDR_none;
17264
2b4424c3
TT
17265 ULONGEST alignment = get_alignment (cu, die);
17266
17267 /* If the pointer size, alignment, or address class is different
17268 than the default, create a type variant marked as such and set
17269 the length accordingly. */
17270 if (TYPE_LENGTH (type) != byte_size
17271 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17272 && alignment != TYPE_RAW_ALIGN (type))
17273 || addr_class != DW_ADDR_none)
c906108c 17274 {
5e2b427d 17275 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17276 {
314ad88d
PA
17277 type_instance_flags type_flags
17278 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17279 addr_class);
876cecd0
TT
17280 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17281 == 0);
8b2dbe47
KB
17282 type = make_type_with_address_space (type, type_flags);
17283 }
17284 else if (TYPE_LENGTH (type) != byte_size)
17285 {
b98664d3 17286 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17287 }
2b4424c3
TT
17288 else if (TYPE_RAW_ALIGN (type) != alignment)
17289 {
b98664d3 17290 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17291 " - DIE at %s [in module %s]"),
17292 sect_offset_str (die->sect_off),
5e22e966 17293 objfile_name (cu->per_objfile->objfile));
2b4424c3 17294 }
6e70227d 17295 else
9a619af0
MS
17296 {
17297 /* Should we also complain about unhandled address classes? */
17298 }
c906108c 17299 }
8b2dbe47
KB
17300
17301 TYPE_LENGTH (type) = byte_size;
2b4424c3 17302 set_type_align (type, alignment);
f792889a 17303 return set_die_type (die, type, cu);
c906108c
SS
17304}
17305
17306/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17307 the user defined type vector. */
17308
f792889a 17309static struct type *
e7c27a73 17310read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17311{
17312 struct type *type;
17313 struct type *to_type;
17314 struct type *domain;
17315
e7c27a73
DJ
17316 to_type = die_type (die, cu);
17317 domain = die_containing_type (die, cu);
0d5de010 17318
7e314c57
JK
17319 /* The calls above may have already set the type for this DIE. */
17320 type = get_die_type (die, cu);
17321 if (type)
17322 return type;
17323
78134374 17324 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17325 type = lookup_methodptr_type (to_type);
78134374 17326 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17327 {
5e22e966 17328 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17329
17330 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17331 to_type->fields (), to_type->num_fields (),
a409645d 17332 to_type->has_varargs ());
7078baeb
TT
17333 type = lookup_methodptr_type (new_type);
17334 }
0d5de010
DJ
17335 else
17336 type = lookup_memberptr_type (to_type, domain);
c906108c 17337
f792889a 17338 return set_die_type (die, type, cu);
c906108c
SS
17339}
17340
4297a3f0 17341/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17342 the user defined type vector. */
17343
f792889a 17344static struct type *
4297a3f0
AV
17345read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17346 enum type_code refcode)
c906108c 17347{
e7c27a73 17348 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17349 struct type *type, *target_type;
c906108c
SS
17350 struct attribute *attr;
17351
4297a3f0
AV
17352 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17353
7e314c57
JK
17354 target_type = die_type (die, cu);
17355
17356 /* The die_type call above may have already set the type for this DIE. */
17357 type = get_die_type (die, cu);
17358 if (type)
17359 return type;
17360
4297a3f0 17361 type = lookup_reference_type (target_type, refcode);
e142c38c 17362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17363 if (attr != nullptr)
c906108c
SS
17364 {
17365 TYPE_LENGTH (type) = DW_UNSND (attr);
17366 }
17367 else
17368 {
107d2387 17369 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17370 }
2b4424c3 17371 maybe_set_alignment (cu, die, type);
f792889a 17372 return set_die_type (die, type, cu);
c906108c
SS
17373}
17374
cf363f18
MW
17375/* Add the given cv-qualifiers to the element type of the array. GCC
17376 outputs DWARF type qualifiers that apply to an array, not the
17377 element type. But GDB relies on the array element type to carry
17378 the cv-qualifiers. This mimics section 6.7.3 of the C99
17379 specification. */
17380
17381static struct type *
17382add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17383 struct type *base_type, int cnst, int voltl)
17384{
17385 struct type *el_type, *inner_array;
17386
17387 base_type = copy_type (base_type);
17388 inner_array = base_type;
17389
78134374 17390 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17391 {
17392 TYPE_TARGET_TYPE (inner_array) =
17393 copy_type (TYPE_TARGET_TYPE (inner_array));
17394 inner_array = TYPE_TARGET_TYPE (inner_array);
17395 }
17396
17397 el_type = TYPE_TARGET_TYPE (inner_array);
17398 cnst |= TYPE_CONST (el_type);
17399 voltl |= TYPE_VOLATILE (el_type);
17400 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17401
17402 return set_die_type (die, base_type, cu);
17403}
17404
f792889a 17405static struct type *
e7c27a73 17406read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17407{
f792889a 17408 struct type *base_type, *cv_type;
c906108c 17409
e7c27a73 17410 base_type = die_type (die, cu);
7e314c57
JK
17411
17412 /* The die_type call above may have already set the type for this DIE. */
17413 cv_type = get_die_type (die, cu);
17414 if (cv_type)
17415 return cv_type;
17416
2f608a3a
KW
17417 /* In case the const qualifier is applied to an array type, the element type
17418 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17419 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17420 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17421
f792889a
DJ
17422 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17423 return set_die_type (die, cv_type, cu);
c906108c
SS
17424}
17425
f792889a 17426static struct type *
e7c27a73 17427read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17428{
f792889a 17429 struct type *base_type, *cv_type;
c906108c 17430
e7c27a73 17431 base_type = die_type (die, cu);
7e314c57
JK
17432
17433 /* The die_type call above may have already set the type for this DIE. */
17434 cv_type = get_die_type (die, cu);
17435 if (cv_type)
17436 return cv_type;
17437
cf363f18
MW
17438 /* In case the volatile qualifier is applied to an array type, the
17439 element type is so qualified, not the array type (section 6.7.3
17440 of C99). */
78134374 17441 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17442 return add_array_cv_type (die, cu, base_type, 0, 1);
17443
f792889a
DJ
17444 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17445 return set_die_type (die, cv_type, cu);
c906108c
SS
17446}
17447
06d66ee9
TT
17448/* Handle DW_TAG_restrict_type. */
17449
17450static struct type *
17451read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17452{
17453 struct type *base_type, *cv_type;
17454
17455 base_type = die_type (die, cu);
17456
17457 /* The die_type call above may have already set the type for this DIE. */
17458 cv_type = get_die_type (die, cu);
17459 if (cv_type)
17460 return cv_type;
17461
17462 cv_type = make_restrict_type (base_type);
17463 return set_die_type (die, cv_type, cu);
17464}
17465
a2c2acaf
MW
17466/* Handle DW_TAG_atomic_type. */
17467
17468static struct type *
17469read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17470{
17471 struct type *base_type, *cv_type;
17472
17473 base_type = die_type (die, cu);
17474
17475 /* The die_type call above may have already set the type for this DIE. */
17476 cv_type = get_die_type (die, cu);
17477 if (cv_type)
17478 return cv_type;
17479
17480 cv_type = make_atomic_type (base_type);
17481 return set_die_type (die, cv_type, cu);
17482}
17483
c906108c
SS
17484/* Extract all information from a DW_TAG_string_type DIE and add to
17485 the user defined type vector. It isn't really a user defined type,
17486 but it behaves like one, with other DIE's using an AT_user_def_type
17487 attribute to reference it. */
17488
f792889a 17489static struct type *
e7c27a73 17490read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17491{
5e22e966 17492 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17493 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17494 struct type *type, *range_type, *index_type, *char_type;
17495 struct attribute *attr;
216a7e6b
AB
17496 struct dynamic_prop prop;
17497 bool length_is_constant = true;
17498 LONGEST length;
17499
17500 /* There are a couple of places where bit sizes might be made use of
17501 when parsing a DW_TAG_string_type, however, no producer that we know
17502 of make use of these. Handling bit sizes that are a multiple of the
17503 byte size is easy enough, but what about other bit sizes? Lets deal
17504 with that problem when we have to. Warn about these attributes being
17505 unsupported, then parse the type and ignore them like we always
17506 have. */
17507 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17508 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17509 {
17510 static bool warning_printed = false;
17511 if (!warning_printed)
17512 {
17513 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17514 "currently supported on DW_TAG_string_type."));
17515 warning_printed = true;
17516 }
17517 }
c906108c 17518
e142c38c 17519 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17520 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17521 {
17522 /* The string length describes the location at which the length of
17523 the string can be found. The size of the length field can be
17524 specified with one of the attributes below. */
17525 struct type *prop_type;
17526 struct attribute *len
17527 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17528 if (len == nullptr)
17529 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17530 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17531 {
17532 /* Pass 0 as the default as we know this attribute is constant
17533 and the default value will not be returned. */
0826b30a 17534 LONGEST sz = len->constant_value (0);
293e7e51 17535 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17536 }
17537 else
17538 {
17539 /* If the size is not specified then we assume it is the size of
17540 an address on this target. */
293e7e51 17541 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17542 }
17543
17544 /* Convert the attribute into a dynamic property. */
17545 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17546 length = 1;
17547 else
17548 length_is_constant = false;
17549 }
17550 else if (attr != nullptr)
17551 {
17552 /* This DW_AT_string_length just contains the length with no
17553 indirection. There's no need to create a dynamic property in this
17554 case. Pass 0 for the default value as we know it will not be
17555 returned in this case. */
0826b30a 17556 length = attr->constant_value (0);
216a7e6b
AB
17557 }
17558 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17559 {
216a7e6b 17560 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17561 length = attr->constant_value (1);
c906108c
SS
17562 }
17563 else
17564 {
216a7e6b
AB
17565 /* Use 1 as a fallback length if we have nothing else. */
17566 length = 1;
c906108c 17567 }
6ccb9162 17568
46bf5051 17569 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17570 if (length_is_constant)
17571 range_type = create_static_range_type (NULL, index_type, 1, length);
17572 else
17573 {
17574 struct dynamic_prop low_bound;
17575
8c2e4e06 17576 low_bound.set_const_val (1);
216a7e6b
AB
17577 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17578 }
3b7538c0
UW
17579 char_type = language_string_char_type (cu->language_defn, gdbarch);
17580 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17581
f792889a 17582 return set_die_type (die, type, cu);
c906108c
SS
17583}
17584
4d804846
JB
17585/* Assuming that DIE corresponds to a function, returns nonzero
17586 if the function is prototyped. */
17587
17588static int
17589prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17590{
17591 struct attribute *attr;
17592
17593 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17594 if (attr && (DW_UNSND (attr) != 0))
17595 return 1;
17596
17597 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17598 is only meaningful for C, but the concept also extends to other
4d804846
JB
17599 languages that allow unprototyped functions (Eg: Objective C).
17600 For all other languages, assume that functions are always
17601 prototyped. */
17602 if (cu->language != language_c
17603 && cu->language != language_objc
17604 && cu->language != language_opencl)
17605 return 1;
17606
17607 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17608 prototyped and unprototyped functions; default to prototyped,
17609 since that is more common in modern code (and RealView warns
17610 about unprototyped functions). */
17611 if (producer_is_realview (cu->producer))
17612 return 1;
17613
17614 return 0;
17615}
17616
c906108c
SS
17617/* Handle DIES due to C code like:
17618
17619 struct foo
c5aa993b
JM
17620 {
17621 int (*funcp)(int a, long l);
17622 int b;
17623 };
c906108c 17624
0963b4bd 17625 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17626
f792889a 17627static struct type *
e7c27a73 17628read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17629{
5e22e966 17630 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17631 struct type *type; /* Type that this function returns. */
17632 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17633 struct attribute *attr;
17634
e7c27a73 17635 type = die_type (die, cu);
7e314c57
JK
17636
17637 /* The die_type call above may have already set the type for this DIE. */
17638 ftype = get_die_type (die, cu);
17639 if (ftype)
17640 return ftype;
17641
0c8b41f1 17642 ftype = lookup_function_type (type);
c906108c 17643
4d804846 17644 if (prototyped_function_p (die, cu))
27e69b7a 17645 ftype->set_is_prototyped (true);
c906108c 17646
c055b101
CV
17647 /* Store the calling convention in the type if it's available in
17648 the subroutine die. Otherwise set the calling convention to
17649 the default value DW_CC_normal. */
17650 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17651 if (attr != nullptr
17652 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17653 TYPE_CALLING_CONVENTION (ftype)
17654 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17655 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17656 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17657 else
17658 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17659
743649fd
MW
17660 /* Record whether the function returns normally to its caller or not
17661 if the DWARF producer set that information. */
17662 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17663 if (attr && (DW_UNSND (attr) != 0))
17664 TYPE_NO_RETURN (ftype) = 1;
17665
76c10ea2
GM
17666 /* We need to add the subroutine type to the die immediately so
17667 we don't infinitely recurse when dealing with parameters
0963b4bd 17668 declared as the same subroutine type. */
76c10ea2 17669 set_die_type (die, ftype, cu);
6e70227d 17670
639d11d3 17671 if (die->child != NULL)
c906108c 17672 {
bb5ed363 17673 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17674 struct die_info *child_die;
8072405b 17675 int nparams, iparams;
c906108c
SS
17676
17677 /* Count the number of parameters.
17678 FIXME: GDB currently ignores vararg functions, but knows about
17679 vararg member functions. */
8072405b 17680 nparams = 0;
639d11d3 17681 child_die = die->child;
c906108c
SS
17682 while (child_die && child_die->tag)
17683 {
17684 if (child_die->tag == DW_TAG_formal_parameter)
17685 nparams++;
17686 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17687 ftype->set_has_varargs (true);
17688
436c571c 17689 child_die = child_die->sibling;
c906108c
SS
17690 }
17691
17692 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17693 ftype->set_num_fields (nparams);
3cabb6b0
SM
17694 ftype->set_fields
17695 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17696
8072405b
JK
17697 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17698 even if we error out during the parameters reading below. */
17699 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17700 ftype->field (iparams).set_type (void_type);
8072405b
JK
17701
17702 iparams = 0;
639d11d3 17703 child_die = die->child;
c906108c
SS
17704 while (child_die && child_die->tag)
17705 {
17706 if (child_die->tag == DW_TAG_formal_parameter)
17707 {
3ce3b1ba
PA
17708 struct type *arg_type;
17709
17710 /* DWARF version 2 has no clean way to discern C++
17711 static and non-static member functions. G++ helps
17712 GDB by marking the first parameter for non-static
17713 member functions (which is the this pointer) as
17714 artificial. We pass this information to
17715 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17716
17717 DWARF version 3 added DW_AT_object_pointer, which GCC
17718 4.5 does not yet generate. */
e142c38c 17719 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17720 if (attr != nullptr)
c906108c
SS
17721 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17722 else
9c37b5ae 17723 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17724 arg_type = die_type (child_die, cu);
17725
17726 /* RealView does not mark THIS as const, which the testsuite
17727 expects. GCC marks THIS as const in method definitions,
17728 but not in the class specifications (GCC PR 43053). */
17729 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17730 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17731 {
17732 int is_this = 0;
17733 struct dwarf2_cu *arg_cu = cu;
17734 const char *name = dwarf2_name (child_die, cu);
17735
17736 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17737 if (attr != nullptr)
3ce3b1ba
PA
17738 {
17739 /* If the compiler emits this, use it. */
17740 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17741 is_this = 1;
17742 }
17743 else if (name && strcmp (name, "this") == 0)
17744 /* Function definitions will have the argument names. */
17745 is_this = 1;
17746 else if (name == NULL && iparams == 0)
17747 /* Declarations may not have the names, so like
17748 elsewhere in GDB, assume an artificial first
17749 argument is "this". */
17750 is_this = 1;
17751
17752 if (is_this)
17753 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17754 arg_type, 0);
17755 }
17756
5d14b6e5 17757 ftype->field (iparams).set_type (arg_type);
c906108c
SS
17758 iparams++;
17759 }
436c571c 17760 child_die = child_die->sibling;
c906108c
SS
17761 }
17762 }
17763
76c10ea2 17764 return ftype;
c906108c
SS
17765}
17766
f792889a 17767static struct type *
e7c27a73 17768read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17769{
5e22e966 17770 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17771 const char *name = NULL;
3c8e0968 17772 struct type *this_type, *target_type;
c906108c 17773
94af9270 17774 name = dwarf2_full_name (NULL, die, cu);
19f392bc 17775 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 17776 this_type->set_target_is_stub (true);
f792889a 17777 set_die_type (die, this_type, cu);
3c8e0968
DE
17778 target_type = die_type (die, cu);
17779 if (target_type != this_type)
17780 TYPE_TARGET_TYPE (this_type) = target_type;
17781 else
17782 {
17783 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17784 spec and cause infinite loops in GDB. */
b98664d3 17785 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17786 "- DIE at %s [in module %s]"),
17787 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17788 TYPE_TARGET_TYPE (this_type) = NULL;
17789 }
e4003a34
TV
17790 if (name == NULL)
17791 {
17792 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17793 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17794 Handle these by just returning the target type, rather than
17795 constructing an anonymous typedef type and trying to handle this
17796 elsewhere. */
17797 set_die_type (die, target_type, cu);
17798 return target_type;
17799 }
f792889a 17800 return this_type;
c906108c
SS
17801}
17802
9b790ce7
UW
17803/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17804 (which may be different from NAME) to the architecture back-end to allow
17805 it to guess the correct format if necessary. */
17806
17807static struct type *
17808dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17809 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17810{
08feed99 17811 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17812 const struct floatformat **format;
17813 struct type *type;
17814
17815 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17816 if (format)
103a685e 17817 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17818 else
77b7c781 17819 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17820
17821 return type;
17822}
17823
eb77c9df
AB
17824/* Allocate an integer type of size BITS and name NAME. */
17825
17826static struct type *
17827dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17828 int bits, int unsigned_p, const char *name)
17829{
17830 struct type *type;
17831
17832 /* Versions of Intel's C Compiler generate an integer type called "void"
17833 instead of using DW_TAG_unspecified_type. This has been seen on
17834 at least versions 14, 17, and 18. */
35ee2dc2
AB
17835 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17836 && strcmp (name, "void") == 0)
eb77c9df
AB
17837 type = objfile_type (objfile)->builtin_void;
17838 else
17839 type = init_integer_type (objfile, bits, unsigned_p, name);
17840
17841 return type;
17842}
17843
8bdc1658
AB
17844/* Initialise and return a floating point type of size BITS suitable for
17845 use as a component of a complex number. The NAME_HINT is passed through
17846 when initialising the floating point type and is the name of the complex
17847 type.
17848
17849 As DWARF doesn't currently provide an explicit name for the components
17850 of a complex number, but it can be helpful to have these components
17851 named, we try to select a suitable name based on the size of the
17852 component. */
17853static struct type *
17854dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17855 struct objfile *objfile,
103a685e
TT
17856 int bits, const char *name_hint,
17857 enum bfd_endian byte_order)
8bdc1658 17858{
08feed99 17859 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17860 struct type *tt = nullptr;
17861
35add35e
AB
17862 /* Try to find a suitable floating point builtin type of size BITS.
17863 We're going to use the name of this type as the name for the complex
17864 target type that we are about to create. */
1db455a7 17865 switch (cu->language)
8bdc1658 17866 {
1db455a7
AB
17867 case language_fortran:
17868 switch (bits)
17869 {
17870 case 32:
17871 tt = builtin_f_type (gdbarch)->builtin_real;
17872 break;
17873 case 64:
17874 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17875 break;
17876 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17877 case 128:
17878 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17879 break;
17880 }
8bdc1658 17881 break;
1db455a7
AB
17882 default:
17883 switch (bits)
17884 {
17885 case 32:
17886 tt = builtin_type (gdbarch)->builtin_float;
17887 break;
17888 case 64:
17889 tt = builtin_type (gdbarch)->builtin_double;
17890 break;
17891 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17892 case 128:
17893 tt = builtin_type (gdbarch)->builtin_long_double;
17894 break;
17895 }
8bdc1658
AB
17896 break;
17897 }
17898
35add35e
AB
17899 /* If the type we found doesn't match the size we were looking for, then
17900 pretend we didn't find a type at all, the complex target type we
17901 create will then be nameless. */
a12e5744 17902 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17903 tt = nullptr;
17904
7d93a1e0 17905 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17906 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17907}
17908
c906108c
SS
17909/* Find a representation of a given base type and install
17910 it in the TYPE field of the die. */
17911
f792889a 17912static struct type *
e7c27a73 17913read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17914{
5e22e966 17915 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17916 struct type *type;
17917 struct attribute *attr;
19f392bc 17918 int encoding = 0, bits = 0;
15d034d0 17919 const char *name;
34877895 17920 gdbarch *arch;
c906108c 17921
e142c38c 17922 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17923 if (attr != nullptr)
34877895 17924 encoding = DW_UNSND (attr);
e142c38c 17925 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17926 if (attr != nullptr)
34877895 17927 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17928 name = dwarf2_name (die, cu);
6ccb9162 17929 if (!name)
34877895 17930 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17931
08feed99 17932 arch = objfile->arch ();
103a685e
TT
17933 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17934
34877895
PJ
17935 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17936 if (attr)
103a685e
TT
17937 {
17938 int endianity = DW_UNSND (attr);
17939
17940 switch (endianity)
17941 {
17942 case DW_END_big:
17943 byte_order = BFD_ENDIAN_BIG;
17944 break;
17945 case DW_END_little:
17946 byte_order = BFD_ENDIAN_LITTLE;
17947 break;
17948 default:
17949 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17950 break;
17951 }
17952 }
6ccb9162
UW
17953
17954 switch (encoding)
c906108c 17955 {
6ccb9162
UW
17956 case DW_ATE_address:
17957 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17958 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17959 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17960 break;
17961 case DW_ATE_boolean:
19f392bc 17962 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17963 break;
17964 case DW_ATE_complex_float:
103a685e
TT
17965 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17966 byte_order);
78134374 17967 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17968 {
17969 if (name == nullptr)
17970 {
17971 struct obstack *obstack
5e22e966 17972 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17973 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17974 nullptr);
17975 }
17976 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17977 }
17978 else
17979 type = init_complex_type (name, type);
6ccb9162
UW
17980 break;
17981 case DW_ATE_decimal_float:
19f392bc 17982 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17983 break;
17984 case DW_ATE_float:
103a685e 17985 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17986 break;
17987 case DW_ATE_signed:
eb77c9df 17988 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17989 break;
17990 case DW_ATE_unsigned:
3b2b8fea
TT
17991 if (cu->language == language_fortran
17992 && name
61012eef 17993 && startswith (name, "character("))
19f392bc
UW
17994 type = init_character_type (objfile, bits, 1, name);
17995 else
eb77c9df 17996 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17997 break;
17998 case DW_ATE_signed_char:
6e70227d 17999 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18000 || cu->language == language_pascal
18001 || cu->language == language_fortran)
19f392bc
UW
18002 type = init_character_type (objfile, bits, 0, name);
18003 else
eb77c9df 18004 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18005 break;
18006 case DW_ATE_unsigned_char:
868a0084 18007 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18008 || cu->language == language_pascal
c44af4eb
TT
18009 || cu->language == language_fortran
18010 || cu->language == language_rust)
19f392bc
UW
18011 type = init_character_type (objfile, bits, 1, name);
18012 else
eb77c9df 18013 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18014 break;
75079b2b 18015 case DW_ATE_UTF:
53e710ac 18016 {
53e710ac
PA
18017 if (bits == 16)
18018 type = builtin_type (arch)->builtin_char16;
18019 else if (bits == 32)
18020 type = builtin_type (arch)->builtin_char32;
18021 else
18022 {
b98664d3 18023 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18024 bits);
eb77c9df 18025 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18026 }
18027 return set_die_type (die, type, cu);
18028 }
75079b2b
TT
18029 break;
18030
6ccb9162 18031 default:
b98664d3 18032 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18033 dwarf_type_encoding_name (encoding));
77b7c781 18034 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18035 break;
c906108c 18036 }
6ccb9162 18037
0114d602 18038 if (name && strcmp (name, "char") == 0)
15152a54 18039 type->set_has_no_signedness (true);
0114d602 18040
2b4424c3
TT
18041 maybe_set_alignment (cu, die, type);
18042
db558e34 18043 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18044
20a5fcbd
TT
18045 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18046 {
18047 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18048 if (attr != nullptr && DW_UNSND (attr) <= 8 * TYPE_LENGTH (type))
18049 {
18050 unsigned real_bit_size = DW_UNSND (attr);
18051 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18052 /* Only use the attributes if they make sense together. */
18053 if (attr == nullptr
18054 || DW_UNSND (attr) + real_bit_size <= 8 * TYPE_LENGTH (type))
18055 {
18056 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18057 = real_bit_size;
18058 if (attr != nullptr)
18059 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18060 = DW_UNSND (attr);
18061 }
18062 }
18063 }
18064
f792889a 18065 return set_die_type (die, type, cu);
c906108c
SS
18066}
18067
80180f79
SA
18068/* Parse dwarf attribute if it's a block, reference or constant and put the
18069 resulting value of the attribute into struct bound_prop.
18070 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18071
18072static int
18073attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18074 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18075 struct type *default_type)
80180f79
SA
18076{
18077 struct dwarf2_property_baton *baton;
5e22e966 18078 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18079 struct objfile *objfile = per_objfile->objfile;
18080 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18081
9a49df9d
AB
18082 gdb_assert (default_type != NULL);
18083
80180f79
SA
18084 if (attr == NULL || prop == NULL)
18085 return 0;
18086
4fc6c0d5 18087 if (attr->form_is_block ())
80180f79 18088 {
8d749320 18089 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18090 baton->property_type = default_type;
80180f79 18091 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18092 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18093
18094 struct dwarf_block *block = attr->as_block ();
18095 baton->locexpr.size = block->size;
18096 baton->locexpr.data = block->data;
216a7e6b
AB
18097 switch (attr->name)
18098 {
18099 case DW_AT_string_length:
18100 baton->locexpr.is_reference = true;
18101 break;
18102 default:
18103 baton->locexpr.is_reference = false;
18104 break;
18105 }
8c2e4e06
SM
18106
18107 prop->set_locexpr (baton);
18108 gdb_assert (prop->baton () != NULL);
80180f79 18109 }
cd6c91b4 18110 else if (attr->form_is_ref ())
80180f79
SA
18111 {
18112 struct dwarf2_cu *target_cu = cu;
18113 struct die_info *target_die;
18114 struct attribute *target_attr;
18115
18116 target_die = follow_die_ref (die, attr, &target_cu);
18117 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18118 if (target_attr == NULL)
18119 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18120 target_cu);
80180f79
SA
18121 if (target_attr == NULL)
18122 return 0;
18123
df25ebbd 18124 switch (target_attr->name)
80180f79 18125 {
df25ebbd 18126 case DW_AT_location:
cd6c91b4 18127 if (target_attr->form_is_section_offset ())
df25ebbd 18128 {
8d749320 18129 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18130 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18131 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18132 prop->set_loclist (baton);
18133 gdb_assert (prop->baton () != NULL);
df25ebbd 18134 }
4fc6c0d5 18135 else if (target_attr->form_is_block ())
df25ebbd 18136 {
8d749320 18137 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18138 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18139 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18140 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18141 struct dwarf_block *block = target_attr->as_block ();
18142 baton->locexpr.size = block->size;
18143 baton->locexpr.data = block->data;
9a49df9d 18144 baton->locexpr.is_reference = true;
8c2e4e06
SM
18145 prop->set_locexpr (baton);
18146 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18147 }
18148 else
18149 {
18150 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18151 "dynamic property");
18152 return 0;
18153 }
18154 break;
18155 case DW_AT_data_member_location:
18156 {
18157 LONGEST offset;
18158
18159 if (!handle_data_member_location (target_die, target_cu,
18160 &offset))
18161 return 0;
18162
8d749320 18163 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18164 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18165 target_cu);
df25ebbd
JB
18166 baton->offset_info.offset = offset;
18167 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18168 prop->set_addr_offset (baton);
df25ebbd
JB
18169 break;
18170 }
80180f79
SA
18171 }
18172 }
cd6c91b4 18173 else if (attr->form_is_constant ())
8c2e4e06 18174 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18175 else
18176 {
18177 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18178 dwarf2_name (die, cu));
18179 return 0;
18180 }
18181
18182 return 1;
18183}
18184
09ba997f 18185/* See read.h. */
9a49df9d 18186
09ba997f 18187struct type *
293e7e51 18188dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18189{
9a49df9d
AB
18190 struct type *int_type;
18191
18192 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18193#define TRY_TYPE(F) \
18194 int_type = (unsigned_p \
18195 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18196 : objfile_type (objfile)->builtin_ ## F); \
18197 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18198 return int_type
18199
18200 TRY_TYPE (char);
18201 TRY_TYPE (short);
18202 TRY_TYPE (int);
18203 TRY_TYPE (long);
18204 TRY_TYPE (long_long);
18205
18206#undef TRY_TYPE
18207
18208 gdb_assert_not_reached ("unable to find suitable integer type");
18209}
18210
09ba997f 18211/* See read.h. */
11a8b164 18212
09ba997f 18213struct type *
293e7e51 18214dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18215{
293e7e51
SM
18216 int addr_size = this->per_cu->addr_size ();
18217 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18218}
18219
b86352cf
AB
18220/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18221 present (which is valid) then compute the default type based on the
18222 compilation units address size. */
18223
18224static struct type *
18225read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18226{
18227 struct type *index_type = die_type (die, cu);
18228
18229 /* Dwarf-2 specifications explicitly allows to create subrange types
18230 without specifying a base type.
18231 In that case, the base type must be set to the type of
18232 the lower bound, upper bound or count, in that order, if any of these
18233 three attributes references an object that has a type.
18234 If no base type is found, the Dwarf-2 specifications say that
18235 a signed integer type of size equal to the size of an address should
18236 be used.
18237 For the following C code: `extern char gdb_int [];'
18238 GCC produces an empty range DIE.
18239 FIXME: muller/2010-05-28: Possible references to object for low bound,
18240 high bound or count are not yet handled by this code. */
78134374 18241 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18242 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18243
18244 return index_type;
18245}
18246
a02abb62
JB
18247/* Read the given DW_AT_subrange DIE. */
18248
f792889a 18249static struct type *
a02abb62
JB
18250read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18251{
4c9ad8c2 18252 struct type *base_type, *orig_base_type;
a02abb62
JB
18253 struct type *range_type;
18254 struct attribute *attr;
729efb13 18255 struct dynamic_prop low, high;
4fae6e18 18256 int low_default_is_valid;
c451ebe5 18257 int high_bound_is_count = 0;
15d034d0 18258 const char *name;
d359392f 18259 ULONGEST negative_mask;
e77813c8 18260
b86352cf
AB
18261 orig_base_type = read_subrange_index_type (die, cu);
18262
4c9ad8c2
TT
18263 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18264 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18265 creating the range type, but we use the result of check_typedef
18266 when examining properties of the type. */
18267 base_type = check_typedef (orig_base_type);
a02abb62 18268
7e314c57
JK
18269 /* The die_type call above may have already set the type for this DIE. */
18270 range_type = get_die_type (die, cu);
18271 if (range_type)
18272 return range_type;
18273
8c2e4e06 18274 high.set_const_val (0);
729efb13 18275
4fae6e18
JK
18276 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18277 omitting DW_AT_lower_bound. */
18278 switch (cu->language)
6e70227d 18279 {
4fae6e18
JK
18280 case language_c:
18281 case language_cplus:
8c2e4e06 18282 low.set_const_val (0);
4fae6e18
JK
18283 low_default_is_valid = 1;
18284 break;
18285 case language_fortran:
8c2e4e06 18286 low.set_const_val (1);
4fae6e18
JK
18287 low_default_is_valid = 1;
18288 break;
18289 case language_d:
4fae6e18 18290 case language_objc:
c44af4eb 18291 case language_rust:
8c2e4e06 18292 low.set_const_val (0);
4fae6e18
JK
18293 low_default_is_valid = (cu->header.version >= 4);
18294 break;
18295 case language_ada:
18296 case language_m2:
18297 case language_pascal:
8c2e4e06 18298 low.set_const_val (1);
4fae6e18
JK
18299 low_default_is_valid = (cu->header.version >= 4);
18300 break;
18301 default:
8c2e4e06 18302 low.set_const_val (0);
4fae6e18
JK
18303 low_default_is_valid = 0;
18304 break;
a02abb62
JB
18305 }
18306
e142c38c 18307 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18308 if (attr != nullptr)
9a49df9d 18309 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18310 else if (!low_default_is_valid)
b98664d3 18311 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18312 "- DIE at %s [in module %s]"),
18313 sect_offset_str (die->sect_off),
5e22e966 18314 objfile_name (cu->per_objfile->objfile));
a02abb62 18315
506f5c41
TV
18316 struct attribute *attr_ub, *attr_count;
18317 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18318 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18319 {
506f5c41 18320 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18321 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18322 {
c451ebe5 18323 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18324 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18325 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18326 else
18327 high_bound_is_count = 1;
c2ff108b 18328 }
506f5c41
TV
18329 else
18330 {
18331 if (attr_ub != NULL)
18332 complaint (_("Unresolved DW_AT_upper_bound "
18333 "- DIE at %s [in module %s]"),
18334 sect_offset_str (die->sect_off),
5e22e966 18335 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18336 if (attr_count != NULL)
18337 complaint (_("Unresolved DW_AT_count "
18338 "- DIE at %s [in module %s]"),
18339 sect_offset_str (die->sect_off),
5e22e966 18340 objfile_name (cu->per_objfile->objfile));
506f5c41 18341 }
e77813c8 18342 }
a02abb62 18343
4e962e74
TT
18344 LONGEST bias = 0;
18345 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18346 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18347 bias = bias_attr->constant_value (0);
4e962e74 18348
dbb9c2b1
JB
18349 /* Normally, the DWARF producers are expected to use a signed
18350 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18351 But this is unfortunately not always the case, as witnessed
18352 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18353 is used instead. To work around that ambiguity, we treat
18354 the bounds as signed, and thus sign-extend their values, when
18355 the base type is signed. */
6e70227d 18356 negative_mask =
d359392f 18357 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18358 if (low.kind () == PROP_CONST
c6d940a9 18359 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18360 low.set_const_val (low.const_val () | negative_mask);
18361 if (high.kind () == PROP_CONST
c6d940a9 18362 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18363 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18364
5bbd8269
AB
18365 /* Check for bit and byte strides. */
18366 struct dynamic_prop byte_stride_prop;
18367 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18368 if (attr_byte_stride != nullptr)
18369 {
293e7e51 18370 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18371 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18372 prop_type);
18373 }
18374
18375 struct dynamic_prop bit_stride_prop;
18376 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18377 if (attr_bit_stride != nullptr)
18378 {
18379 /* It only makes sense to have either a bit or byte stride. */
18380 if (attr_byte_stride != nullptr)
18381 {
18382 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18383 "- DIE at %s [in module %s]"),
18384 sect_offset_str (die->sect_off),
5e22e966 18385 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18386 attr_bit_stride = nullptr;
18387 }
18388 else
18389 {
293e7e51 18390 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18391 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18392 prop_type);
18393 }
18394 }
18395
18396 if (attr_byte_stride != nullptr
18397 || attr_bit_stride != nullptr)
18398 {
18399 bool byte_stride_p = (attr_byte_stride != nullptr);
18400 struct dynamic_prop *stride
18401 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18402
18403 range_type
18404 = create_range_type_with_stride (NULL, orig_base_type, &low,
18405 &high, bias, stride, byte_stride_p);
18406 }
18407 else
18408 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18409
c451ebe5 18410 if (high_bound_is_count)
599088e3 18411 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 18412
c2ff108b
JK
18413 /* Ada expects an empty array on no boundary attributes. */
18414 if (attr == NULL && cu->language != language_ada)
8c2e4e06 18415 range_type->bounds ()->high.set_undefined ();
c2ff108b 18416
39cbfefa
DJ
18417 name = dwarf2_name (die, cu);
18418 if (name)
d0e39ea2 18419 range_type->set_name (name);
6e70227d 18420
e142c38c 18421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18422 if (attr != nullptr)
a02abb62
JB
18423 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18424
2b4424c3
TT
18425 maybe_set_alignment (cu, die, range_type);
18426
7e314c57
JK
18427 set_die_type (die, range_type, cu);
18428
18429 /* set_die_type should be already done. */
b4ba55a1
JB
18430 set_descriptive_type (range_type, die, cu);
18431
7e314c57 18432 return range_type;
a02abb62 18433}
6e70227d 18434
f792889a 18435static struct type *
81a17f79
JB
18436read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18437{
18438 struct type *type;
81a17f79 18439
5e22e966 18440 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 18441 type->set_name (dwarf2_name (die, cu));
81a17f79 18442
74a2f8ff 18443 /* In Ada, an unspecified type is typically used when the description
85102364 18444 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18445 such a type, we treat it as a stub, and try to resolve it later on,
18446 when needed. */
18447 if (cu->language == language_ada)
b4b73759 18448 type->set_is_stub (true);
74a2f8ff 18449
f792889a 18450 return set_die_type (die, type, cu);
81a17f79 18451}
a02abb62 18452
639d11d3
DC
18453/* Read a single die and all its descendents. Set the die's sibling
18454 field to NULL; set other fields in the die correctly, and set all
18455 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18456 location of the info_ptr after reading all of those dies. PARENT
18457 is the parent of the die in question. */
18458
18459static struct die_info *
dee91e82 18460read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18461 const gdb_byte *info_ptr,
18462 const gdb_byte **new_info_ptr,
dee91e82 18463 struct die_info *parent)
639d11d3
DC
18464{
18465 struct die_info *die;
d521ce57 18466 const gdb_byte *cur_ptr;
639d11d3 18467
3e225074 18468 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
18469 if (die == NULL)
18470 {
18471 *new_info_ptr = cur_ptr;
18472 return NULL;
18473 }
93311388 18474 store_in_ref_table (die, reader->cu);
639d11d3 18475
3e225074 18476 if (die->has_children)
bf6af496 18477 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18478 else
18479 {
18480 die->child = NULL;
18481 *new_info_ptr = cur_ptr;
18482 }
18483
18484 die->sibling = NULL;
18485 die->parent = parent;
18486 return die;
18487}
18488
18489/* Read a die, all of its descendents, and all of its siblings; set
18490 all of the fields of all of the dies correctly. Arguments are as
18491 in read_die_and_children. */
18492
18493static struct die_info *
bf6af496 18494read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18495 const gdb_byte *info_ptr,
18496 const gdb_byte **new_info_ptr,
bf6af496 18497 struct die_info *parent)
639d11d3
DC
18498{
18499 struct die_info *first_die, *last_sibling;
d521ce57 18500 const gdb_byte *cur_ptr;
639d11d3 18501
c906108c 18502 cur_ptr = info_ptr;
639d11d3
DC
18503 first_die = last_sibling = NULL;
18504
18505 while (1)
c906108c 18506 {
639d11d3 18507 struct die_info *die
dee91e82 18508 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18509
1d325ec1 18510 if (die == NULL)
c906108c 18511 {
639d11d3
DC
18512 *new_info_ptr = cur_ptr;
18513 return first_die;
c906108c 18514 }
1d325ec1
DJ
18515
18516 if (!first_die)
18517 first_die = die;
c906108c 18518 else
1d325ec1
DJ
18519 last_sibling->sibling = die;
18520
18521 last_sibling = die;
c906108c 18522 }
c906108c
SS
18523}
18524
bf6af496
DE
18525/* Read a die, all of its descendents, and all of its siblings; set
18526 all of the fields of all of the dies correctly. Arguments are as
18527 in read_die_and_children.
18528 This the main entry point for reading a DIE and all its children. */
18529
18530static struct die_info *
18531read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18532 const gdb_byte *info_ptr,
18533 const gdb_byte **new_info_ptr,
bf6af496
DE
18534 struct die_info *parent)
18535{
18536 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18537 new_info_ptr, parent);
18538
b4f54984 18539 if (dwarf_die_debug)
bf6af496
DE
18540 {
18541 fprintf_unfiltered (gdb_stdlog,
18542 "Read die from %s@0x%x of %s:\n",
96b79293 18543 reader->die_section->get_name (),
bf6af496
DE
18544 (unsigned) (info_ptr - reader->die_section->buffer),
18545 bfd_get_filename (reader->abfd));
b4f54984 18546 dump_die (die, dwarf_die_debug);
bf6af496
DE
18547 }
18548
18549 return die;
18550}
18551
3019eac3
DE
18552/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18553 attributes.
18554 The caller is responsible for filling in the extra attributes
18555 and updating (*DIEP)->num_attrs.
18556 Set DIEP to point to a newly allocated die with its information,
3e225074 18557 except for its child, sibling, and parent fields. */
93311388 18558
d521ce57 18559static const gdb_byte *
3019eac3 18560read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18561 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18562 int num_extra_attrs)
93311388 18563{
b64f50a1 18564 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18565 struct abbrev_info *abbrev;
18566 struct die_info *die;
18567 struct dwarf2_cu *cu = reader->cu;
18568 bfd *abfd = reader->abfd;
18569
9c541725 18570 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18571 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18572 info_ptr += bytes_read;
18573 if (!abbrev_number)
18574 {
18575 *diep = NULL;
93311388
DE
18576 return info_ptr;
18577 }
18578
685af9cd 18579 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18580 if (!abbrev)
348e048f
DE
18581 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18582 abbrev_number,
18583 bfd_get_filename (abfd));
18584
3019eac3 18585 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18586 die->sect_off = sect_off;
93311388
DE
18587 die->tag = abbrev->tag;
18588 die->abbrev = abbrev_number;
3e225074 18589 die->has_children = abbrev->has_children;
93311388 18590
3019eac3
DE
18591 /* Make the result usable.
18592 The caller needs to update num_attrs after adding the extra
18593 attributes. */
93311388
DE
18594 die->num_attrs = abbrev->num_attrs;
18595
7a5f294d 18596 bool any_need_reprocess = false;
93311388 18597 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 18598 {
7a5f294d
TT
18599 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18600 info_ptr);
18601 if (die->attrs[i].requires_reprocessing_p ())
18602 any_need_reprocess = true;
18a8505e
AT
18603 }
18604
052c8bb8 18605 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18606 if (attr != nullptr)
18607 cu->str_offsets_base = DW_UNSND (attr);
93311388 18608
41144253 18609 attr = die->attr (DW_AT_loclists_base);
18610 if (attr != nullptr)
18611 cu->loclist_base = DW_UNSND (attr);
18612
a39fdb41 18613 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18614 if (maybe_addr_base.has_value ())
18615 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
18616
18617 attr = die->attr (DW_AT_rnglists_base);
18618 if (attr != nullptr)
18619 cu->ranges_base = DW_UNSND (attr);
18620
7a5f294d
TT
18621 if (any_need_reprocess)
18622 {
18623 for (i = 0; i < abbrev->num_attrs; ++i)
18624 {
18625 if (die->attrs[i].requires_reprocessing_p ())
18626 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18627 }
18628 }
93311388 18629 *diep = die;
93311388
DE
18630 return info_ptr;
18631}
18632
3019eac3
DE
18633/* Read a die and all its attributes.
18634 Set DIEP to point to a newly allocated die with its information,
3e225074 18635 except for its child, sibling, and parent fields. */
3019eac3 18636
d521ce57 18637static const gdb_byte *
3019eac3 18638read_full_die (const struct die_reader_specs *reader,
3e225074 18639 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18640{
d521ce57 18641 const gdb_byte *result;
bf6af496 18642
3e225074 18643 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18644
b4f54984 18645 if (dwarf_die_debug)
bf6af496
DE
18646 {
18647 fprintf_unfiltered (gdb_stdlog,
18648 "Read die from %s@0x%x of %s:\n",
96b79293 18649 reader->die_section->get_name (),
bf6af496
DE
18650 (unsigned) (info_ptr - reader->die_section->buffer),
18651 bfd_get_filename (reader->abfd));
b4f54984 18652 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18653 }
18654
18655 return result;
3019eac3 18656}
433df2d4 18657\f
c906108c 18658
72bf9492
DJ
18659/* Returns nonzero if TAG represents a type that we might generate a partial
18660 symbol for. */
18661
18662static int
18663is_type_tag_for_partial (int tag)
18664{
18665 switch (tag)
18666 {
18667#if 0
18668 /* Some types that would be reasonable to generate partial symbols for,
18669 that we don't at present. */
18670 case DW_TAG_array_type:
18671 case DW_TAG_file_type:
18672 case DW_TAG_ptr_to_member_type:
18673 case DW_TAG_set_type:
18674 case DW_TAG_string_type:
18675 case DW_TAG_subroutine_type:
18676#endif
18677 case DW_TAG_base_type:
18678 case DW_TAG_class_type:
680b30c7 18679 case DW_TAG_interface_type:
72bf9492
DJ
18680 case DW_TAG_enumeration_type:
18681 case DW_TAG_structure_type:
18682 case DW_TAG_subrange_type:
18683 case DW_TAG_typedef:
18684 case DW_TAG_union_type:
18685 return 1;
18686 default:
18687 return 0;
18688 }
18689}
18690
18691/* Load all DIEs that are interesting for partial symbols into memory. */
18692
18693static struct partial_die_info *
dee91e82 18694load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18695 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18696{
dee91e82 18697 struct dwarf2_cu *cu = reader->cu;
5e22e966 18698 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18699 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18700 unsigned int bytes_read;
5afb4e99 18701 unsigned int load_all = 0;
72bf9492
DJ
18702 int nesting_level = 1;
18703
18704 parent_die = NULL;
18705 last_die = NULL;
18706
7adf1e79
DE
18707 gdb_assert (cu->per_cu != NULL);
18708 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18709 load_all = 1;
18710
72bf9492
DJ
18711 cu->partial_dies
18712 = htab_create_alloc_ex (cu->header.length / 12,
18713 partial_die_hash,
18714 partial_die_eq,
18715 NULL,
18716 &cu->comp_unit_obstack,
18717 hashtab_obstack_allocate,
18718 dummy_obstack_deallocate);
18719
72bf9492
DJ
18720 while (1)
18721 {
685af9cd 18722 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18723
18724 /* A NULL abbrev means the end of a series of children. */
18725 if (abbrev == NULL)
18726 {
18727 if (--nesting_level == 0)
cd9983dd
YQ
18728 return first_die;
18729
72bf9492
DJ
18730 info_ptr += bytes_read;
18731 last_die = parent_die;
18732 parent_die = parent_die->die_parent;
18733 continue;
18734 }
18735
98bfdba5
PA
18736 /* Check for template arguments. We never save these; if
18737 they're seen, we just mark the parent, and go on our way. */
18738 if (parent_die != NULL
18739 && cu->language == language_cplus
18740 && (abbrev->tag == DW_TAG_template_type_param
18741 || abbrev->tag == DW_TAG_template_value_param))
18742 {
18743 parent_die->has_template_arguments = 1;
18744
18745 if (!load_all)
18746 {
18747 /* We don't need a partial DIE for the template argument. */
dee91e82 18748 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18749 continue;
18750 }
18751 }
18752
0d99eb77 18753 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18754 Skip their other children. */
18755 if (!load_all
18756 && cu->language == language_cplus
18757 && parent_die != NULL
f9b5d5ea
TV
18758 && parent_die->tag == DW_TAG_subprogram
18759 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 18760 {
dee91e82 18761 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18762 continue;
18763 }
18764
5afb4e99
DJ
18765 /* Check whether this DIE is interesting enough to save. Normally
18766 we would not be interested in members here, but there may be
18767 later variables referencing them via DW_AT_specification (for
18768 static members). */
18769 if (!load_all
18770 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18771 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18772 && abbrev->tag != DW_TAG_enumerator
18773 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18774 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18775 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18776 && abbrev->tag != DW_TAG_variable
5afb4e99 18777 && abbrev->tag != DW_TAG_namespace
f55ee35c 18778 && abbrev->tag != DW_TAG_module
95554aad 18779 && abbrev->tag != DW_TAG_member
74921315
KS
18780 && abbrev->tag != DW_TAG_imported_unit
18781 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18782 {
18783 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18784 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18785 continue;
18786 }
18787
6f06d47b
YQ
18788 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18789 abbrev);
cd9983dd 18790
48fbe735 18791 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18792
18793 /* This two-pass algorithm for processing partial symbols has a
18794 high cost in cache pressure. Thus, handle some simple cases
18795 here which cover the majority of C partial symbols. DIEs
18796 which neither have specification tags in them, nor could have
18797 specification tags elsewhere pointing at them, can simply be
18798 processed and discarded.
18799
18800 This segment is also optional; scan_partial_symbols and
18801 add_partial_symbol will handle these DIEs if we chain
18802 them in normally. When compilers which do not emit large
18803 quantities of duplicate debug information are more common,
18804 this code can probably be removed. */
18805
18806 /* Any complete simple types at the top level (pretty much all
18807 of them, for a language without namespaces), can be processed
18808 directly. */
18809 if (parent_die == NULL
cd9983dd
YQ
18810 && pdi.has_specification == 0
18811 && pdi.is_declaration == 0
18812 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18813 || pdi.tag == DW_TAG_base_type
18814 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18815 {
7d00ffec 18816 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
18817 add_partial_symbol (&pdi, cu);
18818
cd9983dd 18819 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18820 continue;
18821 }
18822
d8228535
JK
18823 /* The exception for DW_TAG_typedef with has_children above is
18824 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18825 type_name_or_error will error on such types later.
d8228535
JK
18826
18827 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18828 it could not find the child DIEs referenced later, this is checked
18829 above. In correct DWARF DW_TAG_typedef should have no children. */
18830
cd9983dd 18831 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18832 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18833 "- DIE at %s [in module %s]"),
cd9983dd 18834 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18835
72bf9492
DJ
18836 /* If we're at the second level, and we're an enumerator, and
18837 our parent has no specification (meaning possibly lives in a
18838 namespace elsewhere), then we can add the partial symbol now
18839 instead of queueing it. */
cd9983dd 18840 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18841 && parent_die != NULL
18842 && parent_die->die_parent == NULL
18843 && parent_die->tag == DW_TAG_enumeration_type
18844 && parent_die->has_specification == 0)
18845 {
7d00ffec 18846 if (pdi.raw_name == NULL)
b98664d3 18847 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18848 else if (building_psymtab)
f0fbb768 18849 add_partial_symbol (&pdi, cu);
72bf9492 18850
cd9983dd 18851 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18852 continue;
18853 }
18854
cd9983dd 18855 struct partial_die_info *part_die
6f06d47b 18856 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18857
72bf9492
DJ
18858 /* We'll save this DIE so link it in. */
18859 part_die->die_parent = parent_die;
18860 part_die->die_sibling = NULL;
18861 part_die->die_child = NULL;
18862
18863 if (last_die && last_die == parent_die)
18864 last_die->die_child = part_die;
18865 else if (last_die)
18866 last_die->die_sibling = part_die;
18867
18868 last_die = part_die;
18869
18870 if (first_die == NULL)
18871 first_die = part_die;
18872
18873 /* Maybe add the DIE to the hash table. Not all DIEs that we
18874 find interesting need to be in the hash table, because we
18875 also have the parent/sibling/child chains; only those that we
18876 might refer to by offset later during partial symbol reading.
18877
18878 For now this means things that might have be the target of a
18879 DW_AT_specification, DW_AT_abstract_origin, or
18880 DW_AT_extension. DW_AT_extension will refer only to
18881 namespaces; DW_AT_abstract_origin refers to functions (and
18882 many things under the function DIE, but we do not recurse
18883 into function DIEs during partial symbol reading) and
18884 possibly variables as well; DW_AT_specification refers to
18885 declarations. Declarations ought to have the DW_AT_declaration
18886 flag. It happens that GCC forgets to put it in sometimes, but
18887 only for functions, not for types.
18888
18889 Adding more things than necessary to the hash table is harmless
18890 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18891 wasted time in find_partial_die, when we reread the compilation
18892 unit with load_all_dies set. */
72bf9492 18893
5afb4e99 18894 if (load_all
72929c62 18895 || abbrev->tag == DW_TAG_constant
5afb4e99 18896 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18897 || abbrev->tag == DW_TAG_variable
18898 || abbrev->tag == DW_TAG_namespace
18899 || part_die->is_declaration)
18900 {
18901 void **slot;
18902
18903 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18904 to_underlying (part_die->sect_off),
18905 INSERT);
72bf9492
DJ
18906 *slot = part_die;
18907 }
18908
72bf9492 18909 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18910 we have no reason to follow the children of structures; for other
98bfdba5
PA
18911 languages we have to, so that we can get at method physnames
18912 to infer fully qualified class names, for DW_AT_specification,
18913 and for C++ template arguments. For C++, we also look one level
18914 inside functions to find template arguments (if the name of the
18915 function does not already contain the template arguments).
bc30ff58 18916
0a4b0913
AB
18917 For Ada and Fortran, we need to scan the children of subprograms
18918 and lexical blocks as well because these languages allow the
18919 definition of nested entities that could be interesting for the
18920 debugger, such as nested subprograms for instance. */
72bf9492 18921 if (last_die->has_children
5afb4e99
DJ
18922 && (load_all
18923 || last_die->tag == DW_TAG_namespace
f55ee35c 18924 || last_die->tag == DW_TAG_module
72bf9492 18925 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18926 || (cu->language == language_cplus
18927 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
18928 && (last_die->raw_name == NULL
18929 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
18930 || (cu->language != language_c
18931 && (last_die->tag == DW_TAG_class_type
680b30c7 18932 || last_die->tag == DW_TAG_interface_type
72bf9492 18933 || last_die->tag == DW_TAG_structure_type
bc30ff58 18934 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18935 || ((cu->language == language_ada
18936 || cu->language == language_fortran)
bc30ff58
JB
18937 && (last_die->tag == DW_TAG_subprogram
18938 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18939 {
18940 nesting_level++;
18941 parent_die = last_die;
18942 continue;
18943 }
18944
18945 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18946 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18947
18948 /* Back to the top, do it again. */
18949 }
18950}
18951
6f06d47b
YQ
18952partial_die_info::partial_die_info (sect_offset sect_off_,
18953 struct abbrev_info *abbrev)
18954 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18955{
18956}
18957
7d00ffec
TT
18958/* See class definition. */
18959
18960const char *
18961partial_die_info::name (dwarf2_cu *cu)
18962{
18963 if (!canonical_name && raw_name != nullptr)
18964 {
18965 struct objfile *objfile = cu->per_objfile->objfile;
18966 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
18967 canonical_name = 1;
18968 }
18969
18970 return raw_name;
18971}
18972
35cc7ed7
YQ
18973/* Read a minimal amount of information into the minimal die structure.
18974 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18975
48fbe735
YQ
18976const gdb_byte *
18977partial_die_info::read (const struct die_reader_specs *reader,
18978 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18979{
dee91e82 18980 struct dwarf2_cu *cu = reader->cu;
976ca316 18981 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 18982 unsigned int i;
c5aa993b 18983 int has_low_pc_attr = 0;
c906108c 18984 int has_high_pc_attr = 0;
91da1414 18985 int high_pc_relative = 0;
c906108c 18986
fd0a254f 18987 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18988 {
e7da7f8f 18989 attribute attr;
7a5f294d 18990 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e
AT
18991 /* String and address offsets that need to do the reprocessing have
18992 already been read at this point, so there is no need to wait until
18993 the loop terminates to do the reprocessing. */
7a5f294d 18994 if (attr.requires_reprocessing_p ())
d0ce17d8 18995 read_attribute_reprocess (reader, &attr, tag);
c906108c 18996 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18997 partial symbol table. */
c906108c
SS
18998 switch (attr.name)
18999 {
19000 case DW_AT_name:
48fbe735 19001 switch (tag)
71c25dea
TT
19002 {
19003 case DW_TAG_compile_unit:
95554aad 19004 case DW_TAG_partial_unit:
348e048f 19005 case DW_TAG_type_unit:
71c25dea
TT
19006 /* Compilation units have a DW_AT_name that is a filename, not
19007 a source language identifier. */
19008 case DW_TAG_enumeration_type:
19009 case DW_TAG_enumerator:
19010 /* These tags always have simple identifiers already; no need
19011 to canonicalize them. */
7d00ffec 19012 canonical_name = 1;
2c830f54 19013 raw_name = attr.as_string ();
71c25dea
TT
19014 break;
19015 default:
7d00ffec 19016 canonical_name = 0;
2c830f54 19017 raw_name = attr.as_string ();
71c25dea
TT
19018 break;
19019 }
c906108c 19020 break;
31ef98ae 19021 case DW_AT_linkage_name:
c906108c 19022 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19023 /* Note that both forms of linkage name might appear. We
19024 assume they will be the same, and we only store the last
19025 one we see. */
95f982e5 19026 linkage_name = attr.as_string ();
c906108c
SS
19027 break;
19028 case DW_AT_low_pc:
19029 has_low_pc_attr = 1;
95f982e5 19030 lowpc = attr.as_address ();
c906108c
SS
19031 break;
19032 case DW_AT_high_pc:
19033 has_high_pc_attr = 1;
95f982e5 19034 highpc = attr.as_address ();
cd6c91b4 19035 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19036 high_pc_relative = 1;
c906108c
SS
19037 break;
19038 case DW_AT_location:
0963b4bd 19039 /* Support the .debug_loc offsets. */
4fc6c0d5 19040 if (attr.form_is_block ())
8e19ed76 19041 {
9d2246fc 19042 d.locdesc = attr.as_block ();
8e19ed76 19043 }
cd6c91b4 19044 else if (attr.form_is_section_offset ())
8e19ed76 19045 {
4d3c2250 19046 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
19047 }
19048 else
19049 {
4d3c2250
KB
19050 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19051 "partial symbol information");
8e19ed76 19052 }
c906108c 19053 break;
c906108c 19054 case DW_AT_external:
48fbe735 19055 is_external = DW_UNSND (&attr);
c906108c
SS
19056 break;
19057 case DW_AT_declaration:
48fbe735 19058 is_declaration = DW_UNSND (&attr);
c906108c
SS
19059 break;
19060 case DW_AT_type:
48fbe735 19061 has_type = 1;
c906108c
SS
19062 break;
19063 case DW_AT_abstract_origin:
19064 case DW_AT_specification:
72bf9492 19065 case DW_AT_extension:
48fbe735 19066 has_specification = 1;
0826b30a 19067 spec_offset = attr.get_ref_die_offset ();
48fbe735 19068 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19069 || cu->per_cu->is_dwz);
c906108c
SS
19070 break;
19071 case DW_AT_sibling:
19072 /* Ignore absolute siblings, they might point outside of
19073 the current compile unit. */
19074 if (attr.form == DW_FORM_ref_addr)
b98664d3 19075 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19076 else
b9502d3f 19077 {
48fbe735 19078 const gdb_byte *buffer = reader->buffer;
0826b30a 19079 sect_offset off = attr.get_ref_die_offset ();
9c541725 19080 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19081
19082 if (sibling_ptr < info_ptr)
b98664d3 19083 complaint (_("DW_AT_sibling points backwards"));
22869d73 19084 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19085 reader->die_section->overflow_complaint ();
b9502d3f 19086 else
48fbe735 19087 sibling = sibling_ptr;
b9502d3f 19088 }
c906108c 19089 break;
fa4028e9 19090 case DW_AT_byte_size:
48fbe735 19091 has_byte_size = 1;
fa4028e9 19092 break;
ff908ebf 19093 case DW_AT_const_value:
48fbe735 19094 has_const_value = 1;
ff908ebf 19095 break;
68511cec
CES
19096 case DW_AT_calling_convention:
19097 /* DWARF doesn't provide a way to identify a program's source-level
19098 entry point. DW_AT_calling_convention attributes are only meant
19099 to describe functions' calling conventions.
19100
19101 However, because it's a necessary piece of information in
0c1b455e
TT
19102 Fortran, and before DWARF 4 DW_CC_program was the only
19103 piece of debugging information whose definition refers to
19104 a 'main program' at all, several compilers marked Fortran
19105 main programs with DW_CC_program --- even when those
19106 functions use the standard calling conventions.
19107
19108 Although DWARF now specifies a way to provide this
19109 information, we support this practice for backward
19110 compatibility. */
68511cec 19111 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 19112 && cu->language == language_fortran)
48fbe735 19113 main_subprogram = 1;
68511cec 19114 break;
481860b3
GB
19115 case DW_AT_inline:
19116 if (DW_UNSND (&attr) == DW_INL_inlined
19117 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 19118 may_be_inlined = 1;
481860b3 19119 break;
95554aad
TT
19120
19121 case DW_AT_import:
48fbe735 19122 if (tag == DW_TAG_imported_unit)
36586728 19123 {
0826b30a 19124 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19125 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19126 || cu->per_cu->is_dwz);
19127 }
95554aad
TT
19128 break;
19129
0c1b455e 19130 case DW_AT_main_subprogram:
48fbe735 19131 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
19132 break;
19133
05caa1d2
TT
19134 case DW_AT_ranges:
19135 {
d0ce17d8
CT
19136 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19137 skeleton. We take advantage of the fact the DW_AT_ranges
19138 does not appear in DW_TAG_compile_unit of DWO files.
19139
19140 Attributes of the form DW_FORM_rnglistx have already had
19141 their value changed by read_rnglist_index and already
19142 include DW_AT_rnglists_base, so don't need to add the ranges
19143 base, either. */
19144 int need_ranges_base = (tag != DW_TAG_compile_unit
19145 && attr.form != DW_FORM_rnglistx);
05caa1d2
TT
19146 unsigned int ranges_offset = (DW_UNSND (&attr)
19147 + (need_ranges_base
19148 ? cu->ranges_base
19149 : 0));
19150
19151 /* Value of the DW_AT_ranges attribute is the offset in the
19152 .debug_ranges section. */
19153 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19154 nullptr, tag))
05caa1d2
TT
19155 has_pc_info = 1;
19156 }
19157 break;
19158
c906108c
SS
19159 default:
19160 break;
19161 }
19162 }
19163
10d06d82
TT
19164 /* For Ada, if both the name and the linkage name appear, we prefer
19165 the latter. This lets "catch exception" work better, regardless
19166 of the order in which the name and linkage name were emitted.
19167 Really, though, this is just a workaround for the fact that gdb
19168 doesn't store both the name and the linkage name. */
19169 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19170 raw_name = linkage_name;
10d06d82 19171
91da1414 19172 if (high_pc_relative)
48fbe735 19173 highpc += lowpc;
91da1414 19174
9373cf26
JK
19175 if (has_low_pc_attr && has_high_pc_attr)
19176 {
19177 /* When using the GNU linker, .gnu.linkonce. sections are used to
19178 eliminate duplicate copies of functions and vtables and such.
19179 The linker will arbitrarily choose one and discard the others.
19180 The AT_*_pc values for such functions refer to local labels in
19181 these sections. If the section from that file was discarded, the
19182 labels are not in the output, so the relocs get a value of 0.
19183 If this is a discarded function, mark the pc bounds as invalid,
19184 so that GDB will ignore it. */
976ca316 19185 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19186 {
976ca316 19187 struct objfile *objfile = per_objfile->objfile;
08feed99 19188 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19189
b98664d3 19190 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19191 "for DIE at %s [in module %s]"),
48fbe735
YQ
19192 paddress (gdbarch, lowpc),
19193 sect_offset_str (sect_off),
9d8780f0 19194 objfile_name (objfile));
9373cf26
JK
19195 }
19196 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19197 else if (lowpc >= highpc)
9373cf26 19198 {
976ca316 19199 struct objfile *objfile = per_objfile->objfile;
08feed99 19200 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19201
b98664d3 19202 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19203 "for DIE at %s [in module %s]"),
48fbe735
YQ
19204 paddress (gdbarch, lowpc),
19205 paddress (gdbarch, highpc),
19206 sect_offset_str (sect_off),
9c541725 19207 objfile_name (objfile));
9373cf26
JK
19208 }
19209 else
48fbe735 19210 has_pc_info = 1;
9373cf26 19211 }
85cbf3d3 19212
c906108c
SS
19213 return info_ptr;
19214}
19215
72bf9492
DJ
19216/* Find a cached partial DIE at OFFSET in CU. */
19217
d590ff25
YQ
19218struct partial_die_info *
19219dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19220{
19221 struct partial_die_info *lookup_die = NULL;
6f06d47b 19222 struct partial_die_info part_die (sect_off);
72bf9492 19223
9a3c8263 19224 lookup_die = ((struct partial_die_info *)
d590ff25 19225 htab_find_with_hash (partial_dies, &part_die,
9c541725 19226 to_underlying (sect_off)));
72bf9492 19227
72bf9492
DJ
19228 return lookup_die;
19229}
19230
348e048f
DE
19231/* Find a partial DIE at OFFSET, which may or may not be in CU,
19232 except in the case of .debug_types DIEs which do not reference
19233 outside their CU (they do however referencing other types via
55f1336d 19234 DW_FORM_ref_sig8). */
72bf9492 19235
122cf0f2 19236static const struct cu_partial_die_info
9c541725 19237find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19238{
976ca316
SM
19239 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19240 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19241 struct partial_die_info *pd = NULL;
72bf9492 19242
36586728 19243 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19244 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19245 {
d590ff25 19246 pd = cu->find_partial_die (sect_off);
5afb4e99 19247 if (pd != NULL)
fb816e8b 19248 return { cu, pd };
0d99eb77
DE
19249 /* We missed recording what we needed.
19250 Load all dies and try again. */
5afb4e99 19251 }
0d99eb77
DE
19252 else
19253 {
19254 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19255 if (cu->per_cu->is_debug_types)
0d99eb77 19256 {
9d8780f0
SM
19257 error (_("Dwarf Error: Type Unit at offset %s contains"
19258 " external reference to offset %s [in module %s].\n"),
19259 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19260 bfd_get_filename (objfile->obfd));
19261 }
7188ed02
SM
19262 dwarf2_per_cu_data *per_cu
19263 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19264 per_objfile);
72bf9492 19265
976ca316 19266 cu = per_objfile->get_cu (per_cu);
7188ed02 19267 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19268 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19269
976ca316 19270 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19271
19272 cu->last_used = 0;
19273 pd = cu->find_partial_die (sect_off);
0d99eb77 19274 }
5afb4e99 19275
dee91e82
DE
19276 /* If we didn't find it, and not all dies have been loaded,
19277 load them all and try again. */
19278
7188ed02 19279 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19280 {
7188ed02 19281 cu->per_cu->load_all_dies = 1;
fd820528
DE
19282
19283 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19284 THIS_CU->cu may already be in use. So we can't just free it and
19285 replace its DIEs with the ones we read in. Instead, we leave those
19286 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19287 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19288 set. */
976ca316 19289 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19290
7188ed02 19291 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19292 }
19293
19294 if (pd == NULL)
521894aa 19295 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19296 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19297 return { cu, pd };
72bf9492
DJ
19298}
19299
abc72ce4
DE
19300/* See if we can figure out if the class lives in a namespace. We do
19301 this by looking for a member function; its demangled name will
19302 contain namespace info, if there is any. */
19303
19304static void
19305guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19306 struct dwarf2_cu *cu)
19307{
19308 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19309 what template types look like, because the demangler
19310 frequently doesn't give the same name as the debug info. We
19311 could fix this by only using the demangled name to get the
19312 prefix (but see comment in read_structure_type). */
19313
19314 struct partial_die_info *real_pdi;
19315 struct partial_die_info *child_pdi;
19316
19317 /* If this DIE (this DIE's specification, if any) has a parent, then
19318 we should not do this. We'll prepend the parent's fully qualified
19319 name when we create the partial symbol. */
19320
19321 real_pdi = struct_pdi;
19322 while (real_pdi->has_specification)
fb816e8b 19323 {
122cf0f2
AB
19324 auto res = find_partial_die (real_pdi->spec_offset,
19325 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19326 real_pdi = res.pdi;
19327 cu = res.cu;
19328 }
abc72ce4
DE
19329
19330 if (real_pdi->die_parent != NULL)
19331 return;
19332
19333 for (child_pdi = struct_pdi->die_child;
19334 child_pdi != NULL;
19335 child_pdi = child_pdi->die_sibling)
19336 {
19337 if (child_pdi->tag == DW_TAG_subprogram
19338 && child_pdi->linkage_name != NULL)
19339 {
43816ebc 19340 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19341 (cu->language_defn->class_name_from_physname
19342 (child_pdi->linkage_name));
abc72ce4
DE
19343 if (actual_class_name != NULL)
19344 {
5e22e966 19345 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19346 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19347 struct_pdi->canonical_name = 1;
abc72ce4
DE
19348 }
19349 break;
19350 }
19351 }
19352}
19353
25c11aca
TV
19354/* Return true if a DIE with TAG may have the DW_AT_const_value
19355 attribute. */
19356
19357static bool
19358can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19359{
19360 switch (tag)
19361 {
19362 case DW_TAG_constant:
19363 case DW_TAG_enumerator:
19364 case DW_TAG_formal_parameter:
19365 case DW_TAG_template_value_param:
19366 case DW_TAG_variable:
19367 return true;
19368 }
19369
19370 return false;
19371}
19372
52356b79
YQ
19373void
19374partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19375{
abc72ce4
DE
19376 /* Once we've fixed up a die, there's no point in doing so again.
19377 This also avoids a memory leak if we were to call
19378 guess_partial_die_structure_name multiple times. */
52356b79 19379 if (fixup_called)
abc72ce4
DE
19380 return;
19381
72bf9492
DJ
19382 /* If we found a reference attribute and the DIE has no name, try
19383 to find a name in the referred to DIE. */
19384
7d00ffec 19385 if (raw_name == NULL && has_specification)
72bf9492
DJ
19386 {
19387 struct partial_die_info *spec_die;
72bf9492 19388
122cf0f2 19389 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19390 spec_die = res.pdi;
19391 cu = res.cu;
72bf9492 19392
52356b79 19393 spec_die->fixup (cu);
72bf9492 19394
7d00ffec 19395 if (spec_die->raw_name)
72bf9492 19396 {
7d00ffec
TT
19397 raw_name = spec_die->raw_name;
19398 canonical_name = spec_die->canonical_name;
72bf9492
DJ
19399
19400 /* Copy DW_AT_external attribute if it is set. */
19401 if (spec_die->is_external)
52356b79 19402 is_external = spec_die->is_external;
72bf9492
DJ
19403 }
19404 }
19405
25c11aca
TV
19406 if (!has_const_value && has_specification
19407 && can_have_DW_AT_const_value_p (tag))
19408 {
19409 struct partial_die_info *spec_die;
19410
19411 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19412 spec_die = res.pdi;
19413 cu = res.cu;
19414
19415 spec_die->fixup (cu);
19416
19417 if (spec_die->has_const_value)
19418 {
19419 /* Copy DW_AT_const_value attribute if it is set. */
19420 has_const_value = spec_die->has_const_value;
19421 }
19422 }
19423
72bf9492 19424 /* Set default names for some unnamed DIEs. */
72bf9492 19425
7d00ffec
TT
19426 if (raw_name == NULL && tag == DW_TAG_namespace)
19427 {
19428 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19429 canonical_name = 1;
19430 }
72bf9492 19431
abc72ce4
DE
19432 /* If there is no parent die to provide a namespace, and there are
19433 children, see if we can determine the namespace from their linkage
122d1940 19434 name. */
abc72ce4 19435 if (cu->language == language_cplus
5e22e966 19436 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
19437 && die_parent == NULL
19438 && has_children
19439 && (tag == DW_TAG_class_type
19440 || tag == DW_TAG_structure_type
19441 || tag == DW_TAG_union_type))
19442 guess_partial_die_structure_name (this, cu);
abc72ce4 19443
53832f31
TT
19444 /* GCC might emit a nameless struct or union that has a linkage
19445 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 19446 if (raw_name == NULL
52356b79
YQ
19447 && (tag == DW_TAG_class_type
19448 || tag == DW_TAG_interface_type
19449 || tag == DW_TAG_structure_type
19450 || tag == DW_TAG_union_type)
19451 && linkage_name != NULL)
53832f31 19452 {
43816ebc
TT
19453 gdb::unique_xmalloc_ptr<char> demangled
19454 (gdb_demangle (linkage_name, DMGL_TYPES));
19455 if (demangled != nullptr)
53832f31 19456 {
96408a79
SA
19457 const char *base;
19458
19459 /* Strip any leading namespaces/classes, keep only the base name.
19460 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
19461 base = strrchr (demangled.get (), ':');
19462 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
19463 base++;
19464 else
43816ebc 19465 base = demangled.get ();
96408a79 19466
5e22e966 19467 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19468 raw_name = objfile->intern (base);
19469 canonical_name = 1;
53832f31
TT
19470 }
19471 }
19472
52356b79 19473 fixup_called = 1;
72bf9492
DJ
19474}
19475
d0ce17d8
CT
19476/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19477 contents from the given SECTION in the HEADER. */
41144253 19478static void
d0ce17d8
CT
19479read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19480 struct dwarf2_section_info *section)
41144253 19481{
19482 unsigned int bytes_read;
19483 bfd *abfd = section->get_bfd_owner ();
19484 const gdb_byte *info_ptr = section->buffer;
19485 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19486 info_ptr += bytes_read;
19487 header->version = read_2_bytes (abfd, info_ptr);
19488 info_ptr += 2;
19489 header->addr_size = read_1_byte (abfd, info_ptr);
19490 info_ptr += 1;
19491 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19492 info_ptr += 1;
19493 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19494}
19495
19496/* Return the DW_AT_loclists_base value for the CU. */
19497static ULONGEST
19498lookup_loclist_base (struct dwarf2_cu *cu)
19499{
19500 /* For the .dwo unit, the loclist_base points to the first offset following
19501 the header. The header consists of the following entities-
19502 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19503 bit format)
19504 2. version (2 bytes)
19505 3. address size (1 byte)
19506 4. segment selector size (1 byte)
19507 5. offset entry count (4 bytes)
19508 These sizes are derived as per the DWARFv5 standard. */
19509 if (cu->dwo_unit != nullptr)
19510 {
19511 if (cu->header.initial_length_size == 4)
19512 return LOCLIST_HEADER_SIZE32;
19513 return LOCLIST_HEADER_SIZE64;
19514 }
19515 return cu->loclist_base;
19516}
19517
19518/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19519 array of offsets in the .debug_loclists section. */
19520static CORE_ADDR
19521read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19522{
976ca316
SM
19523 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19524 struct objfile *objfile = per_objfile->objfile;
41144253 19525 bfd *abfd = objfile->obfd;
19526 ULONGEST loclist_base = lookup_loclist_base (cu);
19527 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19528
19529 section->read (objfile);
19530 if (section->buffer == NULL)
19531 complaint (_("DW_FORM_loclistx used without .debug_loclists "
19532 "section [in module %s]"), objfile_name (objfile));
d0ce17d8
CT
19533 struct loclists_rnglists_header header;
19534 read_loclists_rnglists_header (&header, section);
41144253 19535 if (loclist_index >= header.offset_entry_count)
19536 complaint (_("DW_FORM_loclistx pointing outside of "
19537 ".debug_loclists offset array [in module %s]"),
19538 objfile_name (objfile));
19539 if (loclist_base + loclist_index * cu->header.offset_size
19540 >= section->size)
19541 complaint (_("DW_FORM_loclistx pointing outside of "
19542 ".debug_loclists section [in module %s]"),
19543 objfile_name (objfile));
19544 const gdb_byte *info_ptr
19545 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
19546
19547 if (cu->header.offset_size == 4)
19548 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19549 else
19550 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19551}
19552
d0ce17d8
CT
19553/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19554 array of offsets in the .debug_rnglists section. */
19555static CORE_ADDR
19556read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19557 dwarf_tag tag)
19558{
19559 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19560 struct objfile *objfile = dwarf2_per_objfile->objfile;
19561 bfd *abfd = objfile->obfd;
19562 ULONGEST rnglist_header_size =
19563 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19564 : RNGLIST_HEADER_SIZE64);
19565 ULONGEST rnglist_base =
19566 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
19567 ULONGEST start_offset =
19568 rnglist_base + rnglist_index * cu->header.offset_size;
19569
19570 /* Get rnglists section. */
19571 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19572
19573 /* Read the rnglists section content. */
19574 section->read (objfile);
19575 if (section->buffer == nullptr)
19576 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19577 "[in module %s]"),
19578 objfile_name (objfile));
19579
19580 /* Verify the rnglist index is valid. */
19581 struct loclists_rnglists_header header;
19582 read_loclists_rnglists_header (&header, section);
19583 if (rnglist_index >= header.offset_entry_count)
19584 error (_("DW_FORM_rnglistx index pointing outside of "
19585 ".debug_rnglists offset array [in module %s]"),
19586 objfile_name (objfile));
19587
19588 /* Validate that the offset is within the section's range. */
19589 if (start_offset >= section->size)
19590 error (_("DW_FORM_rnglistx pointing outside of "
19591 ".debug_rnglists section [in module %s]"),
19592 objfile_name (objfile));
19593
19594 /* Validate that reading won't go beyond the end of the section. */
19595 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
19596 error (_("Reading DW_FORM_rnglistx index beyond end of"
19597 ".debug_rnglists section [in module %s]"),
19598 objfile_name (objfile));
19599
19600 const gdb_byte *info_ptr = section->buffer + start_offset;
19601
19602 if (cu->header.offset_size == 4)
19603 return read_4_bytes (abfd, info_ptr) + rnglist_base;
19604 else
19605 return read_8_bytes (abfd, info_ptr) + rnglist_base;
19606}
19607
18a8505e
AT
19608/* Process the attributes that had to be skipped in the first round. These
19609 attributes are the ones that need str_offsets_base or addr_base attributes.
19610 They could not have been processed in the first round, because at the time
19611 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
19612static void
19613read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 19614 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
19615{
19616 struct dwarf2_cu *cu = reader->cu;
19617 switch (attr->form)
19618 {
19619 case DW_FORM_addrx:
19620 case DW_FORM_GNU_addr_index:
36d378cf
TT
19621 attr->set_address (read_addr_index (cu,
19622 attr->as_unsigned_reprocess ()));
18a8505e 19623 break;
41144253 19624 case DW_FORM_loclistx:
19625 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19626 break;
d0ce17d8
CT
19627 case DW_FORM_rnglistx:
19628 DW_UNSND (attr) = read_rnglist_index (cu, DW_UNSND (attr), tag);
19629 break;
18a8505e
AT
19630 case DW_FORM_strx:
19631 case DW_FORM_strx1:
19632 case DW_FORM_strx2:
19633 case DW_FORM_strx3:
19634 case DW_FORM_strx4:
19635 case DW_FORM_GNU_str_index:
19636 {
fe56917a 19637 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 19638 gdb_assert (!attr->canonical_string_p ());
18a8505e 19639 if (reader->dwo_file != NULL)
c6481205
TT
19640 attr->set_string_noncanonical (read_dwo_str_index (reader,
19641 str_index));
18a8505e 19642 else
c6481205
TT
19643 attr->set_string_noncanonical (read_stub_str_index (cu,
19644 str_index));
18a8505e
AT
19645 break;
19646 }
19647 default:
19648 gdb_assert_not_reached (_("Unexpected DWARF form."));
19649 }
19650}
19651
a8329558 19652/* Read an attribute value described by an attribute form. */
c906108c 19653
d521ce57 19654static const gdb_byte *
dee91e82
DE
19655read_attribute_value (const struct die_reader_specs *reader,
19656 struct attribute *attr, unsigned form,
7a5f294d 19657 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19658{
dee91e82 19659 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
19660 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19661 struct objfile *objfile = per_objfile->objfile;
dee91e82 19662 bfd *abfd = reader->abfd;
e7c27a73 19663 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19664 unsigned int bytes_read;
19665 struct dwarf_block *blk;
19666
aead7601 19667 attr->form = (enum dwarf_form) form;
a8329558 19668 switch (form)
c906108c 19669 {
c906108c 19670 case DW_FORM_ref_addr:
ae411497 19671 if (cu->header.version == 2)
414ad644
TT
19672 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
19673 &bytes_read));
ae411497 19674 else
414ad644
TT
19675 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19676 &bytes_read));
ae411497
TT
19677 info_ptr += bytes_read;
19678 break;
36586728 19679 case DW_FORM_GNU_ref_alt:
414ad644
TT
19680 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19681 &bytes_read));
36586728
TT
19682 info_ptr += bytes_read;
19683 break;
ae411497 19684 case DW_FORM_addr:
08feed99
TT
19685 {
19686 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
19687 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
19688 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
19689 attr->set_address (addr);
08feed99
TT
19690 info_ptr += bytes_read;
19691 }
c906108c
SS
19692 break;
19693 case DW_FORM_block2:
7b5a2f43 19694 blk = dwarf_alloc_block (cu);
c906108c
SS
19695 blk->size = read_2_bytes (abfd, info_ptr);
19696 info_ptr += 2;
19697 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19698 info_ptr += blk->size;
9d2246fc 19699 attr->set_block (blk);
c906108c
SS
19700 break;
19701 case DW_FORM_block4:
7b5a2f43 19702 blk = dwarf_alloc_block (cu);
c906108c
SS
19703 blk->size = read_4_bytes (abfd, info_ptr);
19704 info_ptr += 4;
19705 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19706 info_ptr += blk->size;
9d2246fc 19707 attr->set_block (blk);
c906108c
SS
19708 break;
19709 case DW_FORM_data2:
414ad644 19710 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
19711 info_ptr += 2;
19712 break;
19713 case DW_FORM_data4:
414ad644 19714 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
19715 info_ptr += 4;
19716 break;
19717 case DW_FORM_data8:
414ad644 19718 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
19719 info_ptr += 8;
19720 break;
0224619f
JK
19721 case DW_FORM_data16:
19722 blk = dwarf_alloc_block (cu);
19723 blk->size = 16;
19724 blk->data = read_n_bytes (abfd, info_ptr, 16);
19725 info_ptr += 16;
9d2246fc 19726 attr->set_block (blk);
0224619f 19727 break;
2dc7f7b3 19728 case DW_FORM_sec_offset:
414ad644
TT
19729 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
19730 &bytes_read));
2dc7f7b3
TT
19731 info_ptr += bytes_read;
19732 break;
41144253 19733 case DW_FORM_loclistx:
19734 {
7a5f294d
TT
19735 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19736 &bytes_read));
19737 info_ptr += bytes_read;
41144253 19738 }
19739 break;
c906108c 19740 case DW_FORM_string:
c6481205
TT
19741 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
19742 &bytes_read));
c906108c
SS
19743 info_ptr += bytes_read;
19744 break;
4bdf3d34 19745 case DW_FORM_strp:
36586728
TT
19746 if (!cu->per_cu->is_dwz)
19747 {
c6481205
TT
19748 attr->set_string_noncanonical
19749 (read_indirect_string (per_objfile,
19750 abfd, info_ptr, cu_header,
19751 &bytes_read));
36586728
TT
19752 info_ptr += bytes_read;
19753 break;
19754 }
19755 /* FALLTHROUGH */
43988095
JK
19756 case DW_FORM_line_strp:
19757 if (!cu->per_cu->is_dwz)
19758 {
c6481205
TT
19759 attr->set_string_noncanonical
19760 (per_objfile->read_line_string (info_ptr, cu_header,
19761 &bytes_read));
43988095
JK
19762 info_ptr += bytes_read;
19763 break;
19764 }
19765 /* FALLTHROUGH */
36586728
TT
19766 case DW_FORM_GNU_strp_alt:
19767 {
976ca316 19768 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
19769 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19770 &bytes_read);
36586728 19771
c6481205
TT
19772 attr->set_string_noncanonical
19773 (dwz->read_string (objfile, str_offset));
36586728
TT
19774 info_ptr += bytes_read;
19775 }
4bdf3d34 19776 break;
2dc7f7b3 19777 case DW_FORM_exprloc:
c906108c 19778 case DW_FORM_block:
7b5a2f43 19779 blk = dwarf_alloc_block (cu);
c906108c
SS
19780 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19781 info_ptr += bytes_read;
19782 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19783 info_ptr += blk->size;
9d2246fc 19784 attr->set_block (blk);
c906108c
SS
19785 break;
19786 case DW_FORM_block1:
7b5a2f43 19787 blk = dwarf_alloc_block (cu);
c906108c
SS
19788 blk->size = read_1_byte (abfd, info_ptr);
19789 info_ptr += 1;
19790 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19791 info_ptr += blk->size;
9d2246fc 19792 attr->set_block (blk);
c906108c
SS
19793 break;
19794 case DW_FORM_data1:
c906108c 19795 case DW_FORM_flag:
414ad644 19796 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
19797 info_ptr += 1;
19798 break;
2dc7f7b3 19799 case DW_FORM_flag_present:
414ad644 19800 attr->set_unsigned (1);
2dc7f7b3 19801 break;
c906108c 19802 case DW_FORM_sdata:
1bc397c5 19803 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19804 info_ptr += bytes_read;
19805 break;
18a8505e 19806 case DW_FORM_rnglistx:
7a5f294d
TT
19807 {
19808 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19809 &bytes_read));
19810 info_ptr += bytes_read;
19811 }
19812 break;
d0ce17d8 19813 case DW_FORM_udata:
414ad644 19814 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19815 info_ptr += bytes_read;
19816 break;
19817 case DW_FORM_ref1:
414ad644
TT
19818 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19819 + read_1_byte (abfd, info_ptr)));
c906108c
SS
19820 info_ptr += 1;
19821 break;
19822 case DW_FORM_ref2:
414ad644
TT
19823 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19824 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
19825 info_ptr += 2;
19826 break;
19827 case DW_FORM_ref4:
414ad644
TT
19828 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19829 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
19830 info_ptr += 4;
19831 break;
613e1657 19832 case DW_FORM_ref8:
414ad644
TT
19833 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19834 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
19835 info_ptr += 8;
19836 break;
55f1336d 19837 case DW_FORM_ref_sig8:
630ed6b9 19838 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
19839 info_ptr += 8;
19840 break;
c906108c 19841 case DW_FORM_ref_udata:
414ad644
TT
19842 attr->set_unsigned ((to_underlying (cu->header.sect_off)
19843 + read_unsigned_leb128 (abfd, info_ptr,
19844 &bytes_read)));
c906108c
SS
19845 info_ptr += bytes_read;
19846 break;
c906108c 19847 case DW_FORM_indirect:
a8329558
KW
19848 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19849 info_ptr += bytes_read;
43988095
JK
19850 if (form == DW_FORM_implicit_const)
19851 {
19852 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19853 info_ptr += bytes_read;
19854 }
19855 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 19856 info_ptr);
43988095
JK
19857 break;
19858 case DW_FORM_implicit_const:
1bc397c5 19859 attr->set_signed (implicit_const);
a8329558 19860 break;
336d760d 19861 case DW_FORM_addrx:
3019eac3 19862 case DW_FORM_GNU_addr_index:
fe56917a
TT
19863 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
19864 &bytes_read));
3019eac3
DE
19865 info_ptr += bytes_read;
19866 break;
cf532bd1 19867 case DW_FORM_strx:
15f18d14
AT
19868 case DW_FORM_strx1:
19869 case DW_FORM_strx2:
19870 case DW_FORM_strx3:
19871 case DW_FORM_strx4:
3019eac3 19872 case DW_FORM_GNU_str_index:
3019eac3 19873 {
15f18d14
AT
19874 ULONGEST str_index;
19875 if (form == DW_FORM_strx1)
19876 {
19877 str_index = read_1_byte (abfd, info_ptr);
19878 info_ptr += 1;
19879 }
19880 else if (form == DW_FORM_strx2)
19881 {
19882 str_index = read_2_bytes (abfd, info_ptr);
19883 info_ptr += 2;
19884 }
19885 else if (form == DW_FORM_strx3)
19886 {
19887 str_index = read_3_bytes (abfd, info_ptr);
19888 info_ptr += 3;
19889 }
19890 else if (form == DW_FORM_strx4)
19891 {
19892 str_index = read_4_bytes (abfd, info_ptr);
19893 info_ptr += 4;
19894 }
19895 else
19896 {
19897 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19898 info_ptr += bytes_read;
19899 }
fe56917a 19900 attr->set_unsigned_reprocess (str_index);
7a5f294d 19901 }
3019eac3 19902 break;
c906108c 19903 default:
8a3fe4f8 19904 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19905 dwarf_form_name (form),
19906 bfd_get_filename (abfd));
c906108c 19907 }
28e94949 19908
36586728 19909 /* Super hack. */
cd6c91b4 19910 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19911 attr->form = DW_FORM_GNU_ref_alt;
19912
28e94949
JB
19913 /* We have seen instances where the compiler tried to emit a byte
19914 size attribute of -1 which ended up being encoded as an unsigned
19915 0xffffffff. Although 0xffffffff is technically a valid size value,
19916 an object of this size seems pretty unlikely so we can relatively
19917 safely treat these cases as if the size attribute was invalid and
19918 treat them as zero by default. */
19919 if (attr->name == DW_AT_byte_size
19920 && form == DW_FORM_data4
19921 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19922 {
19923 complaint
b98664d3 19924 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19925 hex_string (DW_UNSND (attr)));
414ad644 19926 attr->set_unsigned (0);
01c66ae6 19927 }
28e94949 19928
c906108c
SS
19929 return info_ptr;
19930}
19931
a8329558
KW
19932/* Read an attribute described by an abbreviated attribute. */
19933
d521ce57 19934static const gdb_byte *
dee91e82
DE
19935read_attribute (const struct die_reader_specs *reader,
19936 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 19937 const gdb_byte *info_ptr)
a8329558
KW
19938{
19939 attr->name = abbrev->name;
c6481205 19940 attr->string_is_canonical = 0;
fe56917a 19941 attr->requires_reprocessing = 0;
43988095 19942 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 19943 abbrev->implicit_const, info_ptr);
a8329558
KW
19944}
19945
43988095
JK
19946/* Return pointer to string at .debug_str offset STR_OFFSET. */
19947
19948static const char *
976ca316 19949read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 19950 LONGEST str_offset)
43988095 19951{
976ca316
SM
19952 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
19953 str_offset, "DW_FORM_strp");
c906108c
SS
19954}
19955
43988095
JK
19956/* Return pointer to string at .debug_str offset as read from BUF.
19957 BUF is assumed to be in a compilation unit described by CU_HEADER.
19958 Return *BYTES_READ_PTR count of bytes read from BUF. */
19959
d521ce57 19960static const char *
976ca316 19961read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 19962 const gdb_byte *buf,
cf2c3c16
TT
19963 const struct comp_unit_head *cu_header,
19964 unsigned int *bytes_read_ptr)
19965{
8266302d 19966 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19967
976ca316 19968 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
19969}
19970
86c0bb4c 19971/* See read.h. */
43988095 19972
86c0bb4c
TT
19973const char *
19974dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19975 const struct comp_unit_head *cu_header,
19976 unsigned int *bytes_read_ptr)
43988095 19977{
86c0bb4c 19978 bfd *abfd = objfile->obfd;
8266302d 19979 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19980
5989a64e 19981 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19982}
19983
3019eac3 19984/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19985 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19986 ADDR_SIZE is the size of addresses from the CU header. */
19987
19988static CORE_ADDR
976ca316
SM
19989read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
19990 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 19991{
976ca316 19992 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
19993 bfd *abfd = objfile->obfd;
19994 const gdb_byte *info_ptr;
18a8505e 19995 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19996
976ca316
SM
19997 per_objfile->per_bfd->addr.read (objfile);
19998 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19999 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20000 objfile_name (objfile));
18a8505e 20001 if (addr_base_or_zero + addr_index * addr_size
976ca316 20002 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20003 error (_("DW_FORM_addr_index pointing outside of "
20004 ".debug_addr section [in module %s]"),
4262abfb 20005 objfile_name (objfile));
976ca316
SM
20006 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20007 + addr_index * addr_size);
3019eac3
DE
20008 if (addr_size == 4)
20009 return bfd_get_32 (abfd, info_ptr);
20010 else
20011 return bfd_get_64 (abfd, info_ptr);
20012}
20013
20014/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20015
20016static CORE_ADDR
20017read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20018{
5e22e966 20019 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20020 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20021}
20022
20023/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20024
20025static CORE_ADDR
d521ce57 20026read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20027 unsigned int *bytes_read)
20028{
5e22e966 20029 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20030 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20031
20032 return read_addr_index (cu, addr_index);
20033}
20034
450a1bfc 20035/* See read.h. */
3019eac3
DE
20036
20037CORE_ADDR
82ca3f51 20038dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20039 dwarf2_per_objfile *per_objfile,
82ca3f51 20040 unsigned int addr_index)
3019eac3 20041{
976ca316 20042 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20043 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20044 int addr_size;
20045
3019eac3
DE
20046 /* We need addr_base and addr_size.
20047 If we don't have PER_CU->cu, we have to get it.
20048 Nasty, but the alternative is storing the needed info in PER_CU,
20049 which at this point doesn't seem justified: it's not clear how frequently
20050 it would get used and it would increase the size of every PER_CU.
20051 Entry points like dwarf2_per_cu_addr_size do a similar thing
20052 so we're not in uncharted territory here.
20053 Alas we need to be a bit more complicated as addr_base is contained
20054 in the DIE.
20055
20056 We don't need to read the entire CU(/TU).
20057 We just need the header and top level die.
a1b64ce1 20058
3019eac3 20059 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20060 For now we skip this optimization. */
3019eac3
DE
20061
20062 if (cu != NULL)
20063 {
20064 addr_base = cu->addr_base;
20065 addr_size = cu->header.addr_size;
20066 }
20067 else
20068 {
976ca316 20069 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20070 addr_base = reader.cu->addr_base;
20071 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20072 }
20073
976ca316 20074 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20075}
20076
18a8505e
AT
20077/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20078 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20079 DWO file. */
3019eac3 20080
d521ce57 20081static const char *
18a8505e
AT
20082read_str_index (struct dwarf2_cu *cu,
20083 struct dwarf2_section_info *str_section,
20084 struct dwarf2_section_info *str_offsets_section,
20085 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20086{
976ca316
SM
20087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20088 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20089 const char *objf_name = objfile_name (objfile);
3019eac3 20090 bfd *abfd = objfile->obfd;
d521ce57 20091 const gdb_byte *info_ptr;
3019eac3 20092 ULONGEST str_offset;
cf532bd1 20093 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20094
96b79293
TT
20095 str_section->read (objfile);
20096 str_offsets_section->read (objfile);
73869dc2 20097 if (str_section->buffer == NULL)
18a8505e 20098 error (_("%s used without %s section"
9d8780f0 20099 " in CU at offset %s [in module %s]"),
96b79293 20100 form_name, str_section->get_name (),
18a8505e 20101 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20102 if (str_offsets_section->buffer == NULL)
18a8505e 20103 error (_("%s used without %s section"
9d8780f0 20104 " in CU at offset %s [in module %s]"),
96b79293 20105 form_name, str_section->get_name (),
18a8505e 20106 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20107 info_ptr = (str_offsets_section->buffer
18a8505e 20108 + str_offsets_base
3019eac3
DE
20109 + str_index * cu->header.offset_size);
20110 if (cu->header.offset_size == 4)
20111 str_offset = bfd_get_32 (abfd, info_ptr);
20112 else
20113 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20114 if (str_offset >= str_section->size)
57d63ce2 20115 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20116 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20117 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20118 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20119}
20120
18a8505e
AT
20121/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20122
20123static const char *
20124read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20125{
20126 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20127 ? reader->cu->header.addr_size : 0;
20128 return read_str_index (reader->cu,
20129 &reader->dwo_file->sections.str,
20130 &reader->dwo_file->sections.str_offsets,
20131 str_offsets_base, str_index);
20132}
20133
20134/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20135
20136static const char *
20137read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20138{
5e22e966 20139 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20140 const char *objf_name = objfile_name (objfile);
20141 static const char form_name[] = "DW_FORM_GNU_str_index";
20142 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20143
20144 if (!cu->str_offsets_base.has_value ())
20145 error (_("%s used in Fission stub without %s"
20146 " in CU at offset 0x%lx [in module %s]"),
20147 form_name, str_offsets_attr_name,
20148 (long) cu->header.offset_size, objf_name);
20149
20150 return read_str_index (cu,
5e22e966
SM
20151 &cu->per_objfile->per_bfd->str,
20152 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20153 *cu->str_offsets_base, str_index);
20154}
20155
3019eac3
DE
20156/* Return the length of an LEB128 number in BUF. */
20157
20158static int
20159leb128_size (const gdb_byte *buf)
20160{
20161 const gdb_byte *begin = buf;
20162 gdb_byte byte;
20163
20164 while (1)
20165 {
20166 byte = *buf++;
20167 if ((byte & 128) == 0)
20168 return buf - begin;
20169 }
20170}
20171
c906108c 20172static void
e142c38c 20173set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20174{
20175 switch (lang)
20176 {
20177 case DW_LANG_C89:
76bee0cc 20178 case DW_LANG_C99:
0cfd832f 20179 case DW_LANG_C11:
c906108c 20180 case DW_LANG_C:
d1be3247 20181 case DW_LANG_UPC:
e142c38c 20182 cu->language = language_c;
c906108c 20183 break;
9c37b5ae 20184 case DW_LANG_Java:
c906108c 20185 case DW_LANG_C_plus_plus:
0cfd832f
MW
20186 case DW_LANG_C_plus_plus_11:
20187 case DW_LANG_C_plus_plus_14:
e142c38c 20188 cu->language = language_cplus;
c906108c 20189 break;
6aecb9c2
JB
20190 case DW_LANG_D:
20191 cu->language = language_d;
20192 break;
c906108c
SS
20193 case DW_LANG_Fortran77:
20194 case DW_LANG_Fortran90:
b21b22e0 20195 case DW_LANG_Fortran95:
f7de9aab
MW
20196 case DW_LANG_Fortran03:
20197 case DW_LANG_Fortran08:
e142c38c 20198 cu->language = language_fortran;
c906108c 20199 break;
a766d390
DE
20200 case DW_LANG_Go:
20201 cu->language = language_go;
20202 break;
c906108c 20203 case DW_LANG_Mips_Assembler:
e142c38c 20204 cu->language = language_asm;
c906108c
SS
20205 break;
20206 case DW_LANG_Ada83:
8aaf0b47 20207 case DW_LANG_Ada95:
bc5f45f8
JB
20208 cu->language = language_ada;
20209 break;
72019c9c
GM
20210 case DW_LANG_Modula2:
20211 cu->language = language_m2;
20212 break;
fe8e67fd
PM
20213 case DW_LANG_Pascal83:
20214 cu->language = language_pascal;
20215 break;
22566fbd
DJ
20216 case DW_LANG_ObjC:
20217 cu->language = language_objc;
20218 break;
c44af4eb
TT
20219 case DW_LANG_Rust:
20220 case DW_LANG_Rust_old:
20221 cu->language = language_rust;
20222 break;
c906108c
SS
20223 case DW_LANG_Cobol74:
20224 case DW_LANG_Cobol85:
c906108c 20225 default:
e142c38c 20226 cu->language = language_minimal;
c906108c
SS
20227 break;
20228 }
e142c38c 20229 cu->language_defn = language_def (cu->language);
c906108c
SS
20230}
20231
20232/* Return the named attribute or NULL if not there. */
20233
20234static struct attribute *
e142c38c 20235dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20236{
a48e046c 20237 for (;;)
c906108c 20238 {
a48e046c
TT
20239 unsigned int i;
20240 struct attribute *spec = NULL;
20241
20242 for (i = 0; i < die->num_attrs; ++i)
20243 {
20244 if (die->attrs[i].name == name)
20245 return &die->attrs[i];
20246 if (die->attrs[i].name == DW_AT_specification
20247 || die->attrs[i].name == DW_AT_abstract_origin)
20248 spec = &die->attrs[i];
20249 }
20250
20251 if (!spec)
20252 break;
c906108c 20253
f2f0e013 20254 die = follow_die_ref (die, spec, &cu);
f2f0e013 20255 }
c5aa993b 20256
c906108c
SS
20257 return NULL;
20258}
20259
7d45c7c3
KB
20260/* Return the string associated with a string-typed attribute, or NULL if it
20261 is either not found or is of an incorrect type. */
20262
20263static const char *
20264dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20265{
20266 struct attribute *attr;
20267 const char *str = NULL;
20268
20269 attr = dwarf2_attr (die, name, cu);
20270
20271 if (attr != NULL)
20272 {
95f982e5 20273 str = attr->as_string ();
e61108c9 20274 if (str == nullptr)
b98664d3 20275 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20276 "DIE at %s in module %s"),
20277 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20278 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20279 }
20280
20281 return str;
20282}
20283
a084a2a6 20284/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20285 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20286static const char *
20287dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20288{
20289 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20290 if (dwo_name == nullptr)
20291 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20292 return dwo_name;
20293}
20294
05cf31d1
JB
20295/* Return non-zero iff the attribute NAME is defined for the given DIE,
20296 and holds a non-zero value. This function should only be used for
2dc7f7b3 20297 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20298
20299static int
20300dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20301{
20302 struct attribute *attr = dwarf2_attr (die, name, cu);
20303
20304 return (attr && DW_UNSND (attr));
20305}
20306
3ca72b44 20307static int
e142c38c 20308die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20309{
05cf31d1
JB
20310 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20311 which value is non-zero. However, we have to be careful with
20312 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20313 (via dwarf2_flag_true_p) follows this attribute. So we may
20314 end up accidently finding a declaration attribute that belongs
20315 to a different DIE referenced by the specification attribute,
20316 even though the given DIE does not have a declaration attribute. */
20317 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20318 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20319}
20320
63d06c5c 20321/* Return the die giving the specification for DIE, if there is
f2f0e013 20322 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20323 containing the return value on output. If there is no
20324 specification, but there is an abstract origin, that is
20325 returned. */
63d06c5c
DC
20326
20327static struct die_info *
f2f0e013 20328die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20329{
f2f0e013
DJ
20330 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20331 *spec_cu);
63d06c5c 20332
edb3359d
DJ
20333 if (spec_attr == NULL)
20334 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20335
63d06c5c
DC
20336 if (spec_attr == NULL)
20337 return NULL;
20338 else
f2f0e013 20339 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20340}
c906108c 20341
527f3840
JK
20342/* Stub for free_line_header to match void * callback types. */
20343
20344static void
20345free_line_header_voidp (void *arg)
20346{
9a3c8263 20347 struct line_header *lh = (struct line_header *) arg;
527f3840 20348
fff8551c 20349 delete lh;
527f3840
JK
20350}
20351
83769d0b 20352/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20353
20354static struct dwarf2_section_info *
20355get_debug_line_section (struct dwarf2_cu *cu)
20356{
20357 struct dwarf2_section_info *section;
976ca316 20358 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20359
20360 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20361 DWO file. */
20362 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20363 section = &cu->dwo_unit->dwo_file->sections.line;
20364 else if (cu->per_cu->is_dwz)
20365 {
976ca316 20366 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
20367
20368 section = &dwz->line;
20369 }
20370 else
976ca316 20371 section = &per_objfile->per_bfd->line;
36586728
TT
20372
20373 return section;
20374}
20375
debd256d 20376/* Read the statement program header starting at OFFSET in
3019eac3 20377 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20378 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20379 Returns NULL if there is a problem reading the header, e.g., if it
20380 has a version we don't understand.
debd256d
JB
20381
20382 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20383 the returned object point into the dwarf line section buffer,
20384 and must not be freed. */
ae2de4f8 20385
fff8551c 20386static line_header_up
9c541725 20387dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20388{
3019eac3 20389 struct dwarf2_section_info *section;
976ca316 20390 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 20391
36586728 20392 section = get_debug_line_section (cu);
976ca316 20393 section->read (per_objfile->objfile);
3019eac3 20394 if (section->buffer == NULL)
debd256d 20395 {
3019eac3 20396 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20397 complaint (_("missing .debug_line.dwo section"));
3019eac3 20398 else
b98664d3 20399 complaint (_("missing .debug_line section"));
debd256d
JB
20400 return 0;
20401 }
20402
0df7ad3a 20403 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 20404 per_objfile, section, &cu->header);
debd256d 20405}
c906108c 20406
c6da4cef 20407/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20408 Return the file name of the psymtab for the given file_entry.
c6da4cef 20409 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20410 If space for the result is malloc'd, *NAME_HOLDER will be set.
20411 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20412
d521ce57 20413static const char *
7ba99d21 20414psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 20415 const dwarf2_psymtab *pst,
c89b44cd
TT
20416 const char *comp_dir,
20417 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20418{
d521ce57
TT
20419 const char *include_name = fe.name;
20420 const char *include_name_to_compare = include_name;
72b9f47f 20421 const char *pst_filename;
c6da4cef
DE
20422 int file_is_pst;
20423
8c43009f 20424 const char *dir_name = fe.include_dir (lh);
c6da4cef 20425
c89b44cd 20426 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20427 if (!IS_ABSOLUTE_PATH (include_name)
20428 && (dir_name != NULL || comp_dir != NULL))
20429 {
20430 /* Avoid creating a duplicate psymtab for PST.
20431 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20432 Before we do the comparison, however, we need to account
20433 for DIR_NAME and COMP_DIR.
20434 First prepend dir_name (if non-NULL). If we still don't
20435 have an absolute path prepend comp_dir (if non-NULL).
20436 However, the directory we record in the include-file's
20437 psymtab does not contain COMP_DIR (to match the
20438 corresponding symtab(s)).
20439
20440 Example:
20441
20442 bash$ cd /tmp
20443 bash$ gcc -g ./hello.c
20444 include_name = "hello.c"
20445 dir_name = "."
20446 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20447 DW_AT_name = "./hello.c"
20448
20449 */
c6da4cef
DE
20450
20451 if (dir_name != NULL)
20452 {
c89b44cd
TT
20453 name_holder->reset (concat (dir_name, SLASH_STRING,
20454 include_name, (char *) NULL));
20455 include_name = name_holder->get ();
c6da4cef 20456 include_name_to_compare = include_name;
c6da4cef
DE
20457 }
20458 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20459 {
c89b44cd
TT
20460 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20461 include_name, (char *) NULL));
20462 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20463 }
20464 }
20465
20466 pst_filename = pst->filename;
c89b44cd 20467 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20468 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20469 {
c89b44cd
TT
20470 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20471 pst_filename, (char *) NULL));
20472 pst_filename = copied_name.get ();
c6da4cef
DE
20473 }
20474
1e3fad37 20475 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20476
c6da4cef
DE
20477 if (file_is_pst)
20478 return NULL;
20479 return include_name;
20480}
20481
d9b3de22
DE
20482/* State machine to track the state of the line number program. */
20483
6f77053d 20484class lnp_state_machine
d9b3de22 20485{
6f77053d
PA
20486public:
20487 /* Initialize a machine state for the start of a line number
20488 program. */
804d2729
TT
20489 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20490 bool record_lines_p);
6f77053d 20491
8c43009f
PA
20492 file_entry *current_file ()
20493 {
20494 /* lh->file_names is 0-based, but the file name numbers in the
20495 statement program are 1-based. */
6f77053d
PA
20496 return m_line_header->file_name_at (m_file);
20497 }
20498
20499 /* Record the line in the state machine. END_SEQUENCE is true if
20500 we're processing the end of a sequence. */
20501 void record_line (bool end_sequence);
20502
a8caed5d 20503 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 20504 nop-out rest of the lines in this sequence. */
6f77053d
PA
20505 void check_line_address (struct dwarf2_cu *cu,
20506 const gdb_byte *line_ptr,
7ab6656f 20507 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20508
20509 void handle_set_discriminator (unsigned int discriminator)
20510 {
20511 m_discriminator = discriminator;
20512 m_line_has_non_zero_discriminator |= discriminator != 0;
20513 }
20514
20515 /* Handle DW_LNE_set_address. */
20516 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20517 {
20518 m_op_index = 0;
20519 address += baseaddr;
20520 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20521 }
20522
20523 /* Handle DW_LNS_advance_pc. */
20524 void handle_advance_pc (CORE_ADDR adjust);
20525
20526 /* Handle a special opcode. */
20527 void handle_special_opcode (unsigned char op_code);
20528
20529 /* Handle DW_LNS_advance_line. */
20530 void handle_advance_line (int line_delta)
20531 {
20532 advance_line (line_delta);
20533 }
20534
20535 /* Handle DW_LNS_set_file. */
20536 void handle_set_file (file_name_index file);
20537
20538 /* Handle DW_LNS_negate_stmt. */
20539 void handle_negate_stmt ()
20540 {
20541 m_is_stmt = !m_is_stmt;
20542 }
20543
20544 /* Handle DW_LNS_const_add_pc. */
20545 void handle_const_add_pc ();
20546
20547 /* Handle DW_LNS_fixed_advance_pc. */
20548 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20549 {
20550 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20551 m_op_index = 0;
20552 }
20553
20554 /* Handle DW_LNS_copy. */
20555 void handle_copy ()
20556 {
20557 record_line (false);
20558 m_discriminator = 0;
20559 }
20560
20561 /* Handle DW_LNE_end_sequence. */
20562 void handle_end_sequence ()
20563 {
804d2729 20564 m_currently_recording_lines = true;
6f77053d
PA
20565 }
20566
20567private:
20568 /* Advance the line by LINE_DELTA. */
20569 void advance_line (int line_delta)
20570 {
20571 m_line += line_delta;
20572
20573 if (line_delta != 0)
20574 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20575 }
20576
804d2729
TT
20577 struct dwarf2_cu *m_cu;
20578
6f77053d
PA
20579 gdbarch *m_gdbarch;
20580
20581 /* True if we're recording lines.
20582 Otherwise we're building partial symtabs and are just interested in
20583 finding include files mentioned by the line number program. */
20584 bool m_record_lines_p;
20585
8c43009f 20586 /* The line number header. */
6f77053d 20587 line_header *m_line_header;
8c43009f 20588
6f77053d
PA
20589 /* These are part of the standard DWARF line number state machine,
20590 and initialized according to the DWARF spec. */
d9b3de22 20591
6f77053d 20592 unsigned char m_op_index = 0;
7ba99d21
AT
20593 /* The line table index of the current file. */
20594 file_name_index m_file = 1;
6f77053d
PA
20595 unsigned int m_line = 1;
20596
20597 /* These are initialized in the constructor. */
20598
20599 CORE_ADDR m_address;
20600 bool m_is_stmt;
20601 unsigned int m_discriminator;
d9b3de22
DE
20602
20603 /* Additional bits of state we need to track. */
20604
20605 /* The last file that we called dwarf2_start_subfile for.
20606 This is only used for TLLs. */
6f77053d 20607 unsigned int m_last_file = 0;
d9b3de22 20608 /* The last file a line number was recorded for. */
6f77053d 20609 struct subfile *m_last_subfile = NULL;
d9b3de22 20610
1313c56e
AB
20611 /* The address of the last line entry. */
20612 CORE_ADDR m_last_address;
20613
20614 /* Set to true when a previous line at the same address (using
20615 m_last_address) had m_is_stmt true. This is reset to false when a
20616 line entry at a new address (m_address different to m_last_address) is
20617 processed. */
20618 bool m_stmt_at_address = false;
20619
804d2729
TT
20620 /* When true, record the lines we decode. */
20621 bool m_currently_recording_lines = false;
d9b3de22
DE
20622
20623 /* The last line number that was recorded, used to coalesce
20624 consecutive entries for the same line. This can happen, for
20625 example, when discriminators are present. PR 17276. */
6f77053d
PA
20626 unsigned int m_last_line = 0;
20627 bool m_line_has_non_zero_discriminator = false;
8c43009f 20628};
d9b3de22 20629
6f77053d
PA
20630void
20631lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20632{
20633 CORE_ADDR addr_adj = (((m_op_index + adjust)
20634 / m_line_header->maximum_ops_per_instruction)
20635 * m_line_header->minimum_instruction_length);
20636 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20637 m_op_index = ((m_op_index + adjust)
20638 % m_line_header->maximum_ops_per_instruction);
20639}
d9b3de22 20640
6f77053d
PA
20641void
20642lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20643{
6f77053d 20644 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20645 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20646 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20647 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20648 / m_line_header->maximum_ops_per_instruction)
20649 * m_line_header->minimum_instruction_length);
20650 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20651 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20652 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20653
258bf0ee 20654 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20655 advance_line (line_delta);
20656 record_line (false);
20657 m_discriminator = 0;
20658}
d9b3de22 20659
6f77053d
PA
20660void
20661lnp_state_machine::handle_set_file (file_name_index file)
20662{
20663 m_file = file;
20664
20665 const file_entry *fe = current_file ();
20666 if (fe == NULL)
20667 dwarf2_debug_line_missing_file_complaint ();
20668 else if (m_record_lines_p)
20669 {
20670 const char *dir = fe->include_dir (m_line_header);
20671
c24bdb02 20672 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20673 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20674 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20675 }
20676}
20677
20678void
20679lnp_state_machine::handle_const_add_pc ()
20680{
20681 CORE_ADDR adjust
20682 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20683
20684 CORE_ADDR addr_adj
20685 = (((m_op_index + adjust)
20686 / m_line_header->maximum_ops_per_instruction)
20687 * m_line_header->minimum_instruction_length);
20688
20689 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20690 m_op_index = ((m_op_index + adjust)
20691 % m_line_header->maximum_ops_per_instruction);
20692}
d9b3de22 20693
a05a36a5
DE
20694/* Return non-zero if we should add LINE to the line number table.
20695 LINE is the line to add, LAST_LINE is the last line that was added,
20696 LAST_SUBFILE is the subfile for LAST_LINE.
20697 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20698 had a non-zero discriminator.
20699
20700 We have to be careful in the presence of discriminators.
20701 E.g., for this line:
20702
20703 for (i = 0; i < 100000; i++);
20704
20705 clang can emit four line number entries for that one line,
20706 each with a different discriminator.
20707 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20708
20709 However, we want gdb to coalesce all four entries into one.
20710 Otherwise the user could stepi into the middle of the line and
20711 gdb would get confused about whether the pc really was in the
20712 middle of the line.
20713
20714 Things are further complicated by the fact that two consecutive
20715 line number entries for the same line is a heuristic used by gcc
20716 to denote the end of the prologue. So we can't just discard duplicate
20717 entries, we have to be selective about it. The heuristic we use is
20718 that we only collapse consecutive entries for the same line if at least
20719 one of those entries has a non-zero discriminator. PR 17276.
20720
20721 Note: Addresses in the line number state machine can never go backwards
20722 within one sequence, thus this coalescing is ok. */
20723
20724static int
804d2729
TT
20725dwarf_record_line_p (struct dwarf2_cu *cu,
20726 unsigned int line, unsigned int last_line,
a05a36a5
DE
20727 int line_has_non_zero_discriminator,
20728 struct subfile *last_subfile)
20729{
c24bdb02 20730 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20731 return 1;
20732 if (line != last_line)
20733 return 1;
20734 /* Same line for the same file that we've seen already.
20735 As a last check, for pr 17276, only record the line if the line
20736 has never had a non-zero discriminator. */
20737 if (!line_has_non_zero_discriminator)
20738 return 1;
20739 return 0;
20740}
20741
804d2729
TT
20742/* Use the CU's builder to record line number LINE beginning at
20743 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20744
20745static void
d9b3de22 20746dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20747 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20748 struct dwarf2_cu *cu)
252a6764
DE
20749{
20750 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20751
27e0867f
DE
20752 if (dwarf_line_debug)
20753 {
20754 fprintf_unfiltered (gdb_stdlog,
20755 "Recording line %u, file %s, address %s\n",
20756 line, lbasename (subfile->name),
20757 paddress (gdbarch, address));
20758 }
20759
804d2729 20760 if (cu != nullptr)
8c95582d 20761 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20762}
20763
20764/* Subroutine of dwarf_decode_lines_1 to simplify it.
20765 Mark the end of a set of line number records.
d9b3de22 20766 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20767 If SUBFILE is NULL the request is ignored. */
20768
20769static void
20770dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20771 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20772{
27e0867f
DE
20773 if (subfile == NULL)
20774 return;
20775
20776 if (dwarf_line_debug)
20777 {
20778 fprintf_unfiltered (gdb_stdlog,
20779 "Finishing current line, file %s, address %s\n",
20780 lbasename (subfile->name),
20781 paddress (gdbarch, address));
20782 }
20783
8c95582d 20784 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20785}
20786
6f77053d
PA
20787void
20788lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20789{
d9b3de22
DE
20790 if (dwarf_line_debug)
20791 {
20792 fprintf_unfiltered (gdb_stdlog,
20793 "Processing actual line %u: file %u,"
94a72be7 20794 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20795 m_line, m_file,
6f77053d 20796 paddress (m_gdbarch, m_address),
94a72be7
AB
20797 m_is_stmt, m_discriminator,
20798 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20799 }
20800
6f77053d 20801 file_entry *fe = current_file ();
8c43009f
PA
20802
20803 if (fe == NULL)
d9b3de22
DE
20804 dwarf2_debug_line_missing_file_complaint ();
20805 /* For now we ignore lines not starting on an instruction boundary.
20806 But not when processing end_sequence for compatibility with the
20807 previous version of the code. */
6f77053d 20808 else if (m_op_index == 0 || end_sequence)
d9b3de22 20809 {
8c43009f 20810 fe->included_p = 1;
8c95582d 20811 if (m_record_lines_p)
d9b3de22 20812 {
1313c56e
AB
20813 /* When we switch files we insert an end maker in the first file,
20814 switch to the second file and add a new line entry. The
20815 problem is that the end marker inserted in the first file will
20816 discard any previous line entries at the same address. If the
20817 line entries in the first file are marked as is-stmt, while
20818 the new line in the second file is non-stmt, then this means
20819 the end marker will discard is-stmt lines so we can have a
20820 non-stmt line. This means that there are less addresses at
20821 which the user can insert a breakpoint.
20822
20823 To improve this we track the last address in m_last_address,
20824 and whether we have seen an is-stmt at this address. Then
20825 when switching files, if we have seen a stmt at the current
20826 address, and we are switching to create a non-stmt line, then
20827 discard the new line. */
20828 bool file_changed
20829 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
20830 bool ignore_this_line
876518dd
TV
20831 = ((file_changed && !end_sequence && m_last_address == m_address
20832 && !m_is_stmt && m_stmt_at_address)
20833 || (!end_sequence && m_line == 0));
1313c56e
AB
20834
20835 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 20836 {
804d2729
TT
20837 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20838 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20839 }
20840
1313c56e 20841 if (!end_sequence && !ignore_this_line)
d9b3de22 20842 {
8c95582d
AB
20843 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20844
804d2729 20845 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20846 m_line_has_non_zero_discriminator,
20847 m_last_subfile))
d9b3de22 20848 {
c24bdb02 20849 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20850 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20851 builder->get_current_subfile (),
8c95582d 20852 m_line, m_address, is_stmt,
804d2729 20853 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20854 }
c24bdb02 20855 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20856 m_last_line = m_line;
d9b3de22
DE
20857 }
20858 }
20859 }
1313c56e
AB
20860
20861 /* Track whether we have seen any m_is_stmt true at m_address in case we
20862 have multiple line table entries all at m_address. */
20863 if (m_last_address != m_address)
20864 {
20865 m_stmt_at_address = false;
20866 m_last_address = m_address;
20867 }
20868 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
20869}
20870
804d2729
TT
20871lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20872 line_header *lh, bool record_lines_p)
d9b3de22 20873{
804d2729 20874 m_cu = cu;
6f77053d
PA
20875 m_gdbarch = arch;
20876 m_record_lines_p = record_lines_p;
20877 m_line_header = lh;
d9b3de22 20878
804d2729 20879 m_currently_recording_lines = true;
d9b3de22 20880
d9b3de22
DE
20881 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20882 was a line entry for it so that the backend has a chance to adjust it
20883 and also record it in case it needs it. This is currently used by MIPS
20884 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20885 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20886 m_is_stmt = lh->default_is_stmt;
20887 m_discriminator = 0;
1313c56e
AB
20888
20889 m_last_address = m_address;
20890 m_stmt_at_address = false;
252a6764
DE
20891}
20892
6f77053d
PA
20893void
20894lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20895 const gdb_byte *line_ptr,
7ab6656f 20896 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20897{
a8caed5d
FS
20898 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
20899 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
20900 located at 0x0. In this case, additionally check that if
20901 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 20902
a8caed5d
FS
20903 if ((address == 0 && address < unrelocated_lowpc)
20904 || address == (CORE_ADDR) -1)
924c2928
DE
20905 {
20906 /* This line table is for a function which has been
20907 GCd by the linker. Ignore it. PR gdb/12528 */
20908
5e22e966 20909 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20910 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20911
b98664d3 20912 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20913 line_offset, objfile_name (objfile));
804d2729
TT
20914 m_currently_recording_lines = false;
20915 /* Note: m_currently_recording_lines is left as false until we see
20916 DW_LNE_end_sequence. */
924c2928
DE
20917 }
20918}
20919
f3f5162e 20920/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20921 Process the line number information in LH.
20922 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20923 program in order to set included_p for every referenced header. */
debd256d 20924
c906108c 20925static void
43f3e411
DE
20926dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20927 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20928{
d521ce57
TT
20929 const gdb_byte *line_ptr, *extended_end;
20930 const gdb_byte *line_end;
a8c50c1f 20931 unsigned int bytes_read, extended_len;
699ca60a 20932 unsigned char op_code, extended_op;
e142c38c 20933 CORE_ADDR baseaddr;
5e22e966 20934 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20935 bfd *abfd = objfile->obfd;
08feed99 20936 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20937 /* True if we're recording line info (as opposed to building partial
20938 symtabs and just interested in finding include files mentioned by
20939 the line number program). */
20940 bool record_lines_p = !decode_for_pst_p;
e142c38c 20941
b3b3bada 20942 baseaddr = objfile->text_section_offset ();
c906108c 20943
debd256d
JB
20944 line_ptr = lh->statement_program_start;
20945 line_end = lh->statement_program_end;
c906108c
SS
20946
20947 /* Read the statement sequences until there's nothing left. */
20948 while (line_ptr < line_end)
20949 {
6f77053d
PA
20950 /* The DWARF line number program state machine. Reset the state
20951 machine at the start of each sequence. */
804d2729 20952 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20953 bool end_sequence = false;
d9b3de22 20954
8c43009f 20955 if (record_lines_p)
c906108c 20956 {
8c43009f
PA
20957 /* Start a subfile for the current file of the state
20958 machine. */
20959 const file_entry *fe = state_machine.current_file ();
20960
20961 if (fe != NULL)
804d2729 20962 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20963 }
20964
a738430d 20965 /* Decode the table. */
d9b3de22 20966 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20967 {
20968 op_code = read_1_byte (abfd, line_ptr);
20969 line_ptr += 1;
9aa1fe7e 20970
debd256d 20971 if (op_code >= lh->opcode_base)
6e70227d 20972 {
8e07a239 20973 /* Special opcode. */
6f77053d 20974 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20975 }
20976 else switch (op_code)
c906108c
SS
20977 {
20978 case DW_LNS_extended_op:
3e43a32a
MS
20979 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20980 &bytes_read);
473b7be6 20981 line_ptr += bytes_read;
a8c50c1f 20982 extended_end = line_ptr + extended_len;
c906108c
SS
20983 extended_op = read_1_byte (abfd, line_ptr);
20984 line_ptr += 1;
8f34b746
TV
20985 if (DW_LNE_lo_user <= extended_op
20986 && extended_op <= DW_LNE_hi_user)
20987 {
20988 /* Vendor extension, ignore. */
20989 line_ptr = extended_end;
20990 break;
20991 }
c906108c
SS
20992 switch (extended_op)
20993 {
20994 case DW_LNE_end_sequence:
6f77053d
PA
20995 state_machine.handle_end_sequence ();
20996 end_sequence = true;
c906108c
SS
20997 break;
20998 case DW_LNE_set_address:
d9b3de22
DE
20999 {
21000 CORE_ADDR address
c8a7a66f 21001 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21002 line_ptr += bytes_read;
6f77053d
PA
21003
21004 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21005 lowpc - baseaddr, address);
6f77053d 21006 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21007 }
c906108c
SS
21008 break;
21009 case DW_LNE_define_file:
debd256d 21010 {
d521ce57 21011 const char *cur_file;
ecfb656c
PA
21012 unsigned int mod_time, length;
21013 dir_index dindex;
6e70227d 21014
3e43a32a
MS
21015 cur_file = read_direct_string (abfd, line_ptr,
21016 &bytes_read);
debd256d 21017 line_ptr += bytes_read;
ecfb656c 21018 dindex = (dir_index)
debd256d
JB
21019 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21020 line_ptr += bytes_read;
21021 mod_time =
21022 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21023 line_ptr += bytes_read;
21024 length =
21025 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21026 line_ptr += bytes_read;
ecfb656c 21027 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21028 }
c906108c 21029 break;
d0c6ba3d 21030 case DW_LNE_set_discriminator:
6f77053d
PA
21031 {
21032 /* The discriminator is not interesting to the
21033 debugger; just ignore it. We still need to
21034 check its value though:
21035 if there are consecutive entries for the same
21036 (non-prologue) line we want to coalesce them.
21037 PR 17276. */
21038 unsigned int discr
21039 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21040 line_ptr += bytes_read;
21041
21042 state_machine.handle_set_discriminator (discr);
21043 }
d0c6ba3d 21044 break;
c906108c 21045 default:
b98664d3 21046 complaint (_("mangled .debug_line section"));
debd256d 21047 return;
c906108c 21048 }
a8c50c1f
DJ
21049 /* Make sure that we parsed the extended op correctly. If e.g.
21050 we expected a different address size than the producer used,
21051 we may have read the wrong number of bytes. */
21052 if (line_ptr != extended_end)
21053 {
b98664d3 21054 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21055 return;
21056 }
c906108c
SS
21057 break;
21058 case DW_LNS_copy:
6f77053d 21059 state_machine.handle_copy ();
c906108c
SS
21060 break;
21061 case DW_LNS_advance_pc:
2dc7f7b3
TT
21062 {
21063 CORE_ADDR adjust
21064 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21065 line_ptr += bytes_read;
6f77053d
PA
21066
21067 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21068 }
c906108c
SS
21069 break;
21070 case DW_LNS_advance_line:
a05a36a5
DE
21071 {
21072 int line_delta
21073 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21074 line_ptr += bytes_read;
6f77053d
PA
21075
21076 state_machine.handle_advance_line (line_delta);
a05a36a5 21077 }
c906108c
SS
21078 break;
21079 case DW_LNS_set_file:
d9b3de22 21080 {
6f77053d 21081 file_name_index file
ecfb656c
PA
21082 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21083 &bytes_read);
d9b3de22 21084 line_ptr += bytes_read;
8c43009f 21085
6f77053d 21086 state_machine.handle_set_file (file);
d9b3de22 21087 }
c906108c
SS
21088 break;
21089 case DW_LNS_set_column:
0ad93d4f 21090 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21091 line_ptr += bytes_read;
21092 break;
21093 case DW_LNS_negate_stmt:
6f77053d 21094 state_machine.handle_negate_stmt ();
c906108c
SS
21095 break;
21096 case DW_LNS_set_basic_block:
c906108c 21097 break;
c2c6d25f
JM
21098 /* Add to the address register of the state machine the
21099 address increment value corresponding to special opcode
a738430d
MK
21100 255. I.e., this value is scaled by the minimum
21101 instruction length since special opcode 255 would have
b021a221 21102 scaled the increment. */
c906108c 21103 case DW_LNS_const_add_pc:
6f77053d 21104 state_machine.handle_const_add_pc ();
c906108c
SS
21105 break;
21106 case DW_LNS_fixed_advance_pc:
3e29f34a 21107 {
6f77053d 21108 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21109 line_ptr += 2;
6f77053d
PA
21110
21111 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21112 }
c906108c 21113 break;
9aa1fe7e 21114 default:
a738430d
MK
21115 {
21116 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21117 int i;
a738430d 21118
debd256d 21119 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21120 {
21121 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21122 line_ptr += bytes_read;
21123 }
21124 }
c906108c
SS
21125 }
21126 }
d9b3de22
DE
21127
21128 if (!end_sequence)
21129 dwarf2_debug_line_missing_end_sequence_complaint ();
21130
21131 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21132 in which case we still finish recording the last line). */
6f77053d 21133 state_machine.record_line (true);
c906108c 21134 }
f3f5162e
DE
21135}
21136
21137/* Decode the Line Number Program (LNP) for the given line_header
21138 structure and CU. The actual information extracted and the type
21139 of structures created from the LNP depends on the value of PST.
21140
21141 1. If PST is NULL, then this procedure uses the data from the program
21142 to create all necessary symbol tables, and their linetables.
21143
21144 2. If PST is not NULL, this procedure reads the program to determine
21145 the list of files included by the unit represented by PST, and
21146 builds all the associated partial symbol tables.
21147
21148 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21149 It is used for relative paths in the line table.
21150 NOTE: When processing partial symtabs (pst != NULL),
21151 comp_dir == pst->dirname.
21152
21153 NOTE: It is important that psymtabs have the same file name (via strcmp)
21154 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21155 symtab we don't use it in the name of the psymtabs we create.
21156 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21157 A good testcase for this is mb-inline.exp.
21158
527f3840
JK
21159 LOWPC is the lowest address in CU (or 0 if not known).
21160
21161 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21162 for its PC<->lines mapping information. Otherwise only the filename
21163 table is read in. */
f3f5162e
DE
21164
21165static void
21166dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21167 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21168 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21169{
5e22e966 21170 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21171 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21172
527f3840
JK
21173 if (decode_mapping)
21174 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21175
21176 if (decode_for_pst_p)
21177 {
aaa75496
JB
21178 /* Now that we're done scanning the Line Header Program, we can
21179 create the psymtab of each included file. */
7ba99d21
AT
21180 for (auto &file_entry : lh->file_names ())
21181 if (file_entry.included_p == 1)
aaa75496 21182 {
c89b44cd 21183 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21184 const char *include_name =
7ba99d21
AT
21185 psymtab_include_file_name (lh, file_entry, pst,
21186 comp_dir, &name_holder);
c6da4cef 21187 if (include_name != NULL)
aaa75496
JB
21188 dwarf2_create_include_psymtab (include_name, pst, objfile);
21189 }
21190 }
cb1df416
DJ
21191 else
21192 {
21193 /* Make sure a symtab is created for every file, even files
21194 which contain only variables (i.e. no code with associated
21195 line numbers). */
c24bdb02
KS
21196 buildsym_compunit *builder = cu->get_builder ();
21197 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21198
7ba99d21 21199 for (auto &fe : lh->file_names ())
cb1df416 21200 {
804d2729 21201 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21202 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21203 {
c24bdb02 21204 builder->get_current_subfile ()->symtab
804d2729 21205 = allocate_symtab (cust,
c24bdb02 21206 builder->get_current_subfile ()->name);
43f3e411 21207 }
c24bdb02 21208 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21209 }
21210 }
c906108c
SS
21211}
21212
21213/* Start a subfile for DWARF. FILENAME is the name of the file and
21214 DIRNAME the name of the source directory which contains FILENAME
4d663531 21215 or NULL if not known.
c906108c
SS
21216 This routine tries to keep line numbers from identical absolute and
21217 relative file names in a common subfile.
21218
21219 Using the `list' example from the GDB testsuite, which resides in
21220 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21221 of /srcdir/list0.c yields the following debugging information for list0.c:
21222
c5aa993b 21223 DW_AT_name: /srcdir/list0.c
4d663531 21224 DW_AT_comp_dir: /compdir
357e46e7 21225 files.files[0].name: list0.h
c5aa993b 21226 files.files[0].dir: /srcdir
357e46e7 21227 files.files[1].name: list0.c
c5aa993b 21228 files.files[1].dir: /srcdir
c906108c
SS
21229
21230 The line number information for list0.c has to end up in a single
4f1520fb
FR
21231 subfile, so that `break /srcdir/list0.c:1' works as expected.
21232 start_subfile will ensure that this happens provided that we pass the
21233 concatenation of files.files[1].dir and files.files[1].name as the
21234 subfile's name. */
c906108c
SS
21235
21236static void
804d2729
TT
21237dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21238 const char *dirname)
c906108c 21239{
43816ebc 21240 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21241
4d663531 21242 /* In order not to lose the line information directory,
4f1520fb
FR
21243 we concatenate it to the filename when it makes sense.
21244 Note that the Dwarf3 standard says (speaking of filenames in line
21245 information): ``The directory index is ignored for file names
21246 that represent full path names''. Thus ignoring dirname in the
21247 `else' branch below isn't an issue. */
c906108c 21248
d5166ae1 21249 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21250 {
43816ebc
TT
21251 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21252 filename = copy.get ();
d521ce57 21253 }
c906108c 21254
c24bdb02 21255 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21256}
21257
804d2729
TT
21258/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21259 buildsym_compunit constructor. */
f4dc4d17 21260
c24bdb02
KS
21261struct compunit_symtab *
21262dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21263 CORE_ADDR low_pc)
f4dc4d17 21264{
c24bdb02 21265 gdb_assert (m_builder == nullptr);
43f3e411 21266
c24bdb02 21267 m_builder.reset (new struct buildsym_compunit
f6e649dd 21268 (this->per_objfile->objfile,
c24bdb02 21269 name, comp_dir, language, low_pc));
93b8bea4 21270
c24bdb02 21271 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21272
c24bdb02
KS
21273 get_builder ()->record_debugformat ("DWARF 2");
21274 get_builder ()->record_producer (producer);
f4dc4d17 21275
c24bdb02 21276 processing_has_namespace_info = false;
43f3e411 21277
c24bdb02 21278 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21279}
21280
4c2df51b
DJ
21281static void
21282var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21283 struct dwarf2_cu *cu)
4c2df51b 21284{
5e22e966 21285 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21286 struct comp_unit_head *cu_header = &cu->header;
21287
4c2df51b
DJ
21288 /* NOTE drow/2003-01-30: There used to be a comment and some special
21289 code here to turn a symbol with DW_AT_external and a
21290 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21291 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21292 with some versions of binutils) where shared libraries could have
21293 relocations against symbols in their debug information - the
21294 minimal symbol would have the right address, but the debug info
21295 would not. It's no longer necessary, because we will explicitly
21296 apply relocations when we read in the debug information now. */
21297
21298 /* A DW_AT_location attribute with no contents indicates that a
21299 variable has been optimized away. */
9d2246fc 21300 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21301 {
f1e6e072 21302 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21303 return;
21304 }
21305
21306 /* Handle one degenerate form of location expression specially, to
21307 preserve GDB's previous behavior when section offsets are
336d760d
AT
21308 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21309 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21310
9d2246fc
TT
21311 if (attr->form_is_block ())
21312 {
21313 struct dwarf_block *block = attr->as_block ();
21314
21315 if ((block->data[0] == DW_OP_addr
21316 && block->size == 1 + cu_header->addr_size)
21317 || ((block->data[0] == DW_OP_GNU_addr_index
21318 || block->data[0] == DW_OP_addrx)
21319 && (block->size
21320 == 1 + leb128_size (&block->data[1]))))
21321 {
21322 unsigned int dummy;
21323
21324 if (block->data[0] == DW_OP_addr)
21325 SET_SYMBOL_VALUE_ADDRESS
21326 (sym, cu->header.read_address (objfile->obfd,
21327 block->data + 1,
38583298 21328 &dummy));
9d2246fc
TT
21329 else
21330 SET_SYMBOL_VALUE_ADDRESS
21331 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21332 &dummy));
21333 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21334 fixup_symbol_section (sym, objfile);
21335 SET_SYMBOL_VALUE_ADDRESS
21336 (sym,
21337 SYMBOL_VALUE_ADDRESS (sym)
21338 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21339 return;
21340 }
4c2df51b
DJ
21341 }
21342
21343 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21344 expression evaluator, and use LOC_COMPUTED only when necessary
21345 (i.e. when the value of a register or memory location is
21346 referenced, or a thread-local block, etc.). Then again, it might
21347 not be worthwhile. I'm assuming that it isn't unless performance
21348 or memory numbers show me otherwise. */
21349
f1e6e072 21350 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21351
f1e6e072 21352 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21353 cu->has_loclist = true;
4c2df51b
DJ
21354}
21355
c906108c
SS
21356/* Given a pointer to a DWARF information entry, figure out if we need
21357 to make a symbol table entry for it, and if so, create a new entry
21358 and return a pointer to it.
21359 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21360 used the passed type.
21361 If SPACE is not NULL, use it to hold the new symbol. If it is
21362 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21363
21364static struct symbol *
5e2db402
TT
21365new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21366 struct symbol *space)
c906108c 21367{
976ca316
SM
21368 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21369 struct objfile *objfile = per_objfile->objfile;
08feed99 21370 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21371 struct symbol *sym = NULL;
15d034d0 21372 const char *name;
c906108c
SS
21373 struct attribute *attr = NULL;
21374 struct attribute *attr2 = NULL;
e142c38c 21375 CORE_ADDR baseaddr;
e37fd15a
SW
21376 struct pending **list_to_add = NULL;
21377
edb3359d 21378 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21379
b3b3bada 21380 baseaddr = objfile->text_section_offset ();
c906108c 21381
94af9270 21382 name = dwarf2_name (die, cu);
c906108c
SS
21383 if (name)
21384 {
34eaf542 21385 int suppress_add = 0;
94af9270 21386
34eaf542
TT
21387 if (space)
21388 sym = space;
21389 else
8c14c3a3 21390 sym = new (&objfile->objfile_obstack) symbol;
c906108c 21391 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21392
21393 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 21394 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
21395 /* Fortran does not have mangling standard and the mangling does differ
21396 between gfortran, iFort etc. */
bcfe6157
TT
21397 const char *physname
21398 = (cu->language == language_fortran
21399 ? dwarf2_full_name (name, die, cu)
21400 : dwarf2_physname (name, die, cu));
21401 const char *linkagename = dw2_linkage_name (die, cu);
21402
21403 if (linkagename == nullptr || cu->language == language_ada)
21404 sym->set_linkage_name (physname);
21405 else
21406 {
21407 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21408 sym->set_linkage_name (linkagename);
21409 }
f55ee35c 21410
c906108c 21411 /* Default assumptions.
c5aa993b 21412 Use the passed type or decode it from the die. */
176620f1 21413 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21414 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21415 if (type != NULL)
21416 SYMBOL_TYPE (sym) = type;
21417 else
e7c27a73 21418 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21419 attr = dwarf2_attr (die,
21420 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21421 cu);
435d3d88 21422 if (attr != nullptr)
c906108c
SS
21423 {
21424 SYMBOL_LINE (sym) = DW_UNSND (attr);
21425 }
cb1df416 21426
edb3359d
DJ
21427 attr = dwarf2_attr (die,
21428 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21429 cu);
435d3d88 21430 if (attr != nullptr)
cb1df416 21431 {
ecfb656c 21432 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21433 struct file_entry *fe;
9a619af0 21434
ecfb656c
PA
21435 if (cu->line_header != NULL)
21436 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21437 else
21438 fe = NULL;
21439
21440 if (fe == NULL)
b98664d3 21441 complaint (_("file index out of range"));
8c43009f
PA
21442 else
21443 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21444 }
21445
c906108c
SS
21446 switch (die->tag)
21447 {
21448 case DW_TAG_label:
e142c38c 21449 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 21450 if (attr != nullptr)
3e29f34a
MR
21451 {
21452 CORE_ADDR addr;
21453
95f982e5 21454 addr = attr->as_address ();
3e29f34a 21455 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21456 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 21457 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 21458 }
8f5c6526
TV
21459 else
21460 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
21461 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21462 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 21463 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21464 break;
21465 case DW_TAG_subprogram:
21466 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21467 finish_block. */
f1e6e072 21468 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21469 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21470 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21471 || cu->language == language_ada
21472 || cu->language == language_fortran)
c906108c 21473 {
2cfa0c8d 21474 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21475 Ada and Fortran subprograms, whether marked external or
21476 not, are always stored as a global symbol, because we want
21477 to be able to access them globally. For instance, we want
21478 to be able to break on a nested subprogram without having
21479 to specify the context. */
c24bdb02 21480 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21481 }
21482 else
21483 {
e37fd15a 21484 list_to_add = cu->list_in_scope;
c906108c
SS
21485 }
21486 break;
edb3359d
DJ
21487 case DW_TAG_inlined_subroutine:
21488 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21489 finish_block. */
f1e6e072 21490 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21491 SYMBOL_INLINED (sym) = 1;
481860b3 21492 list_to_add = cu->list_in_scope;
edb3359d 21493 break;
34eaf542
TT
21494 case DW_TAG_template_value_param:
21495 suppress_add = 1;
21496 /* Fall through. */
72929c62 21497 case DW_TAG_constant:
c906108c 21498 case DW_TAG_variable:
254e6b9e 21499 case DW_TAG_member:
0963b4bd
MS
21500 /* Compilation with minimal debug info may result in
21501 variables with missing type entries. Change the
21502 misleading `void' type to something sensible. */
78134374 21503 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 21504 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21505
e142c38c 21506 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21507 /* In the case of DW_TAG_member, we should only be called for
21508 static const members. */
21509 if (die->tag == DW_TAG_member)
21510 {
3863f96c
DE
21511 /* dwarf2_add_field uses die_is_declaration,
21512 so we do the same. */
254e6b9e
DE
21513 gdb_assert (die_is_declaration (die, cu));
21514 gdb_assert (attr);
21515 }
435d3d88 21516 if (attr != nullptr)
c906108c 21517 {
e7c27a73 21518 dwarf2_const_value (attr, sym, cu);
e142c38c 21519 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21520 if (!suppress_add)
34eaf542
TT
21521 {
21522 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21523 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21524 else
e37fd15a 21525 list_to_add = cu->list_in_scope;
34eaf542 21526 }
c906108c
SS
21527 break;
21528 }
e142c38c 21529 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21530 if (attr != nullptr)
c906108c 21531 {
e7c27a73 21532 var_decode_location (attr, sym, cu);
e142c38c 21533 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21534
21535 /* Fortran explicitly imports any global symbols to the local
21536 scope by DW_TAG_common_block. */
21537 if (cu->language == language_fortran && die->parent
21538 && die->parent->tag == DW_TAG_common_block)
21539 attr2 = NULL;
21540
caac4577
JG
21541 if (SYMBOL_CLASS (sym) == LOC_STATIC
21542 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 21543 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
21544 {
21545 /* When a static variable is eliminated by the linker,
21546 the corresponding debug information is not stripped
21547 out, but the variable address is set to null;
21548 do not add such variables into symbol table. */
21549 }
21550 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21551 {
4b610737
TT
21552 if (SYMBOL_CLASS (sym) == LOC_STATIC
21553 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 21554 && per_objfile->per_bfd->can_copy)
4b610737
TT
21555 {
21556 /* A global static variable might be subject to
21557 copy relocation. We first check for a local
21558 minsym, though, because maybe the symbol was
21559 marked hidden, in which case this would not
21560 apply. */
21561 bound_minimal_symbol found
21562 = (lookup_minimal_symbol_linkage
987012b8 21563 (sym->linkage_name (), objfile));
4b610737
TT
21564 if (found.minsym != nullptr)
21565 sym->maybe_copied = 1;
21566 }
f55ee35c 21567
1c809c68
TT
21568 /* A variable with DW_AT_external is never static,
21569 but it may be block-scoped. */
804d2729 21570 list_to_add
c24bdb02
KS
21571 = ((cu->list_in_scope
21572 == cu->get_builder ()->get_file_symbols ())
21573 ? cu->get_builder ()->get_global_symbols ()
804d2729 21574 : cu->list_in_scope);
1c809c68 21575 }
c906108c 21576 else
e37fd15a 21577 list_to_add = cu->list_in_scope;
c906108c
SS
21578 }
21579 else
21580 {
21581 /* We do not know the address of this symbol.
c5aa993b
JM
21582 If it is an external symbol and we have type information
21583 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21584 The address of the variable will then be determined from
21585 the minimal symbol table whenever the variable is
21586 referenced. */
e142c38c 21587 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21588
21589 /* Fortran explicitly imports any global symbols to the local
21590 scope by DW_TAG_common_block. */
21591 if (cu->language == language_fortran && die->parent
21592 && die->parent->tag == DW_TAG_common_block)
21593 {
21594 /* SYMBOL_CLASS doesn't matter here because
21595 read_common_block is going to reset it. */
21596 if (!suppress_add)
21597 list_to_add = cu->list_in_scope;
21598 }
21599 else if (attr2 && (DW_UNSND (attr2) != 0)
21600 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21601 {
0fe7935b
DJ
21602 /* A variable with DW_AT_external is never static, but it
21603 may be block-scoped. */
804d2729 21604 list_to_add
c24bdb02
KS
21605 = ((cu->list_in_scope
21606 == cu->get_builder ()->get_file_symbols ())
21607 ? cu->get_builder ()->get_global_symbols ()
804d2729 21608 : cu->list_in_scope);
0fe7935b 21609
f1e6e072 21610 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21611 }
442ddf59
JK
21612 else if (!die_is_declaration (die, cu))
21613 {
21614 /* Use the default LOC_OPTIMIZED_OUT class. */
21615 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21616 if (!suppress_add)
21617 list_to_add = cu->list_in_scope;
442ddf59 21618 }
c906108c
SS
21619 }
21620 break;
21621 case DW_TAG_formal_parameter:
a60f3166
TT
21622 {
21623 /* If we are inside a function, mark this as an argument. If
21624 not, we might be looking at an argument to an inlined function
21625 when we do not have enough information to show inlined frames;
21626 pretend it's a local variable in that case so that the user can
21627 still see it. */
804d2729 21628 struct context_stack *curr
c24bdb02 21629 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21630 if (curr != nullptr && curr->name != nullptr)
21631 SYMBOL_IS_ARGUMENT (sym) = 1;
21632 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 21633 if (attr != nullptr)
a60f3166
TT
21634 {
21635 var_decode_location (attr, sym, cu);
21636 }
21637 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21638 if (attr != nullptr)
a60f3166
TT
21639 {
21640 dwarf2_const_value (attr, sym, cu);
21641 }
f346a30d 21642
a60f3166
TT
21643 list_to_add = cu->list_in_scope;
21644 }
c906108c
SS
21645 break;
21646 case DW_TAG_unspecified_parameters:
21647 /* From varargs functions; gdb doesn't seem to have any
21648 interest in this information, so just ignore it for now.
21649 (FIXME?) */
21650 break;
34eaf542
TT
21651 case DW_TAG_template_type_param:
21652 suppress_add = 1;
21653 /* Fall through. */
c906108c 21654 case DW_TAG_class_type:
680b30c7 21655 case DW_TAG_interface_type:
c906108c
SS
21656 case DW_TAG_structure_type:
21657 case DW_TAG_union_type:
72019c9c 21658 case DW_TAG_set_type:
c906108c 21659 case DW_TAG_enumeration_type:
f1e6e072 21660 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21661 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21662
63d06c5c 21663 {
9c37b5ae 21664 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21665 really ever be static objects: otherwise, if you try
21666 to, say, break of a class's method and you're in a file
21667 which doesn't mention that class, it won't work unless
21668 the check for all static symbols in lookup_symbol_aux
21669 saves you. See the OtherFileClass tests in
21670 gdb.c++/namespace.exp. */
21671
e37fd15a 21672 if (!suppress_add)
34eaf542 21673 {
c24bdb02 21674 buildsym_compunit *builder = cu->get_builder ();
804d2729 21675 list_to_add
c24bdb02 21676 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21677 && cu->language == language_cplus
c24bdb02 21678 ? builder->get_global_symbols ()
804d2729 21679 : cu->list_in_scope);
63d06c5c 21680
64382290 21681 /* The semantics of C++ state that "struct foo {
9c37b5ae 21682 ... }" also defines a typedef for "foo". */
64382290 21683 if (cu->language == language_cplus
45280282 21684 || cu->language == language_ada
c44af4eb
TT
21685 || cu->language == language_d
21686 || cu->language == language_rust)
64382290
TT
21687 {
21688 /* The symbol's name is already allocated along
21689 with this objfile, so we don't need to
21690 duplicate it for the type. */
7d93a1e0 21691 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21692 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21693 }
63d06c5c
DC
21694 }
21695 }
c906108c
SS
21696 break;
21697 case DW_TAG_typedef:
f1e6e072 21698 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21699 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21700 list_to_add = cu->list_in_scope;
63d06c5c 21701 break;
c906108c 21702 case DW_TAG_base_type:
a02abb62 21703 case DW_TAG_subrange_type:
f1e6e072 21704 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21705 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21706 list_to_add = cu->list_in_scope;
c906108c
SS
21707 break;
21708 case DW_TAG_enumerator:
e142c38c 21709 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21710 if (attr != nullptr)
c906108c 21711 {
e7c27a73 21712 dwarf2_const_value (attr, sym, cu);
c906108c 21713 }
63d06c5c
DC
21714 {
21715 /* NOTE: carlton/2003-11-10: See comment above in the
21716 DW_TAG_class_type, etc. block. */
21717
804d2729 21718 list_to_add
c24bdb02 21719 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21720 && cu->language == language_cplus
c24bdb02 21721 ? cu->get_builder ()->get_global_symbols ()
804d2729 21722 : cu->list_in_scope);
63d06c5c 21723 }
c906108c 21724 break;
74921315 21725 case DW_TAG_imported_declaration:
5c4e30ca 21726 case DW_TAG_namespace:
f1e6e072 21727 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21728 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21729 break;
530e8392
KB
21730 case DW_TAG_module:
21731 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21732 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21733 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21734 break;
4357ac6c 21735 case DW_TAG_common_block:
f1e6e072 21736 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21737 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21738 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21739 break;
c906108c
SS
21740 default:
21741 /* Not a tag we recognize. Hopefully we aren't processing
21742 trash data, but since we must specifically ignore things
21743 we don't recognize, there is nothing else we should do at
0963b4bd 21744 this point. */
b98664d3 21745 complaint (_("unsupported tag: '%s'"),
4d3c2250 21746 dwarf_tag_name (die->tag));
c906108c
SS
21747 break;
21748 }
df8a16a1 21749
e37fd15a
SW
21750 if (suppress_add)
21751 {
21752 sym->hash_next = objfile->template_symbols;
21753 objfile->template_symbols = sym;
21754 list_to_add = NULL;
21755 }
21756
21757 if (list_to_add != NULL)
d3cb6808 21758 add_symbol_to_list (sym, list_to_add);
e37fd15a 21759
df8a16a1
DJ
21760 /* For the benefit of old versions of GCC, check for anonymous
21761 namespaces based on the demangled name. */
4d4ec4e5 21762 if (!cu->processing_has_namespace_info
94af9270 21763 && cu->language == language_cplus)
c24bdb02 21764 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21765 }
21766 return (sym);
21767}
21768
98bfdba5
PA
21769/* Given an attr with a DW_FORM_dataN value in host byte order,
21770 zero-extend it as appropriate for the symbol's type. The DWARF
21771 standard (v4) is not entirely clear about the meaning of using
21772 DW_FORM_dataN for a constant with a signed type, where the type is
21773 wider than the data. The conclusion of a discussion on the DWARF
21774 list was that this is unspecified. We choose to always zero-extend
21775 because that is the interpretation long in use by GCC. */
c906108c 21776
98bfdba5 21777static gdb_byte *
ff39bb5e 21778dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21779 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21780{
5e22e966 21781 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21782 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21783 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21784 LONGEST l = DW_UNSND (attr);
21785
21786 if (bits < sizeof (*value) * 8)
21787 {
21788 l &= ((LONGEST) 1 << bits) - 1;
21789 *value = l;
21790 }
21791 else if (bits == sizeof (*value) * 8)
21792 *value = l;
21793 else
21794 {
224c3ddb 21795 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21796 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21797 return bytes;
21798 }
21799
21800 return NULL;
21801}
21802
21803/* Read a constant value from an attribute. Either set *VALUE, or if
21804 the value does not fit in *VALUE, set *BYTES - either already
21805 allocated on the objfile obstack, or newly allocated on OBSTACK,
21806 or, set *BATON, if we translated the constant to a location
21807 expression. */
21808
21809static void
ff39bb5e 21810dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21811 const char *name, struct obstack *obstack,
21812 struct dwarf2_cu *cu,
d521ce57 21813 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21814 struct dwarf2_locexpr_baton **baton)
21815{
5e22e966 21816 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21817 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21818 struct comp_unit_head *cu_header = &cu->header;
c906108c 21819 struct dwarf_block *blk;
98bfdba5
PA
21820 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21821 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21822
21823 *value = 0;
21824 *bytes = NULL;
21825 *baton = NULL;
c906108c
SS
21826
21827 switch (attr->form)
21828 {
21829 case DW_FORM_addr:
336d760d 21830 case DW_FORM_addrx:
3019eac3 21831 case DW_FORM_GNU_addr_index:
ac56253d 21832 {
ac56253d
TT
21833 gdb_byte *data;
21834
98bfdba5
PA
21835 if (TYPE_LENGTH (type) != cu_header->addr_size)
21836 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21837 cu_header->addr_size,
98bfdba5 21838 TYPE_LENGTH (type));
ac56253d
TT
21839 /* Symbols of this form are reasonably rare, so we just
21840 piggyback on the existing location code rather than writing
21841 a new implementation of symbol_computed_ops. */
8d749320 21842 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21843 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21844 (*baton)->per_cu = cu->per_cu;
21845 gdb_assert ((*baton)->per_cu);
ac56253d 21846
98bfdba5 21847 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21848 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21849 (*baton)->data = data;
ac56253d
TT
21850
21851 data[0] = DW_OP_addr;
21852 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 21853 byte_order, attr->as_address ());
ac56253d 21854 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21855 }
c906108c 21856 break;
4ac36638 21857 case DW_FORM_string:
93b5768b 21858 case DW_FORM_strp:
cf532bd1 21859 case DW_FORM_strx:
3019eac3 21860 case DW_FORM_GNU_str_index:
36586728 21861 case DW_FORM_GNU_strp_alt:
c6481205 21862 /* The string is already allocated on the objfile obstack, point
98bfdba5 21863 directly to it. */
2c830f54 21864 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 21865 break;
c906108c
SS
21866 case DW_FORM_block1:
21867 case DW_FORM_block2:
21868 case DW_FORM_block4:
21869 case DW_FORM_block:
2dc7f7b3 21870 case DW_FORM_exprloc:
0224619f 21871 case DW_FORM_data16:
9d2246fc 21872 blk = attr->as_block ();
98bfdba5
PA
21873 if (TYPE_LENGTH (type) != blk->size)
21874 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21875 TYPE_LENGTH (type));
21876 *bytes = blk->data;
c906108c 21877 break;
2df3850c
JM
21878
21879 /* The DW_AT_const_value attributes are supposed to carry the
21880 symbol's value "represented as it would be on the target
21881 architecture." By the time we get here, it's already been
21882 converted to host endianness, so we just need to sign- or
21883 zero-extend it as appropriate. */
21884 case DW_FORM_data1:
3aef2284 21885 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21886 break;
c906108c 21887 case DW_FORM_data2:
3aef2284 21888 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21889 break;
c906108c 21890 case DW_FORM_data4:
3aef2284 21891 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21892 break;
c906108c 21893 case DW_FORM_data8:
3aef2284 21894 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21895 break;
21896
c906108c 21897 case DW_FORM_sdata:
663c44ac 21898 case DW_FORM_implicit_const:
1bc397c5 21899 *value = attr->as_signed ();
2df3850c
JM
21900 break;
21901
c906108c 21902 case DW_FORM_udata:
98bfdba5 21903 *value = DW_UNSND (attr);
c906108c 21904 break;
2df3850c 21905
c906108c 21906 default:
b98664d3 21907 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21908 dwarf_form_name (attr->form));
98bfdba5 21909 *value = 0;
c906108c
SS
21910 break;
21911 }
21912}
21913
2df3850c 21914
98bfdba5
PA
21915/* Copy constant value from an attribute to a symbol. */
21916
2df3850c 21917static void
ff39bb5e 21918dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21919 struct dwarf2_cu *cu)
2df3850c 21920{
5e22e966 21921 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21922 LONGEST value;
d521ce57 21923 const gdb_byte *bytes;
98bfdba5 21924 struct dwarf2_locexpr_baton *baton;
2df3850c 21925
98bfdba5 21926 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21927 sym->print_name (),
98bfdba5
PA
21928 &objfile->objfile_obstack, cu,
21929 &value, &bytes, &baton);
2df3850c 21930
98bfdba5
PA
21931 if (baton != NULL)
21932 {
98bfdba5 21933 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21934 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21935 }
21936 else if (bytes != NULL)
21937 {
21938 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21939 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21940 }
21941 else
21942 {
21943 SYMBOL_VALUE (sym) = value;
f1e6e072 21944 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21945 }
2df3850c
JM
21946}
21947
c906108c
SS
21948/* Return the type of the die in question using its DW_AT_type attribute. */
21949
21950static struct type *
e7c27a73 21951die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21952{
c906108c 21953 struct attribute *type_attr;
c906108c 21954
e142c38c 21955 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21956 if (!type_attr)
21957 {
5e22e966 21958 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21959 /* A missing DW_AT_type represents a void type. */
518817b3 21960 return objfile_type (objfile)->builtin_void;
c906108c 21961 }
348e048f 21962
673bfd45 21963 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21964}
21965
b4ba55a1
JB
21966/* True iff CU's producer generates GNAT Ada auxiliary information
21967 that allows to find parallel types through that information instead
21968 of having to do expensive parallel lookups by type name. */
21969
21970static int
21971need_gnat_info (struct dwarf2_cu *cu)
21972{
de4cb04a
JB
21973 /* Assume that the Ada compiler was GNAT, which always produces
21974 the auxiliary information. */
21975 return (cu->language == language_ada);
b4ba55a1
JB
21976}
21977
b4ba55a1
JB
21978/* Return the auxiliary type of the die in question using its
21979 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21980 attribute is not present. */
21981
21982static struct type *
21983die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21984{
b4ba55a1 21985 struct attribute *type_attr;
b4ba55a1
JB
21986
21987 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21988 if (!type_attr)
21989 return NULL;
21990
673bfd45 21991 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21992}
21993
21994/* If DIE has a descriptive_type attribute, then set the TYPE's
21995 descriptive type accordingly. */
21996
21997static void
21998set_descriptive_type (struct type *type, struct die_info *die,
21999 struct dwarf2_cu *cu)
22000{
22001 struct type *descriptive_type = die_descriptive_type (die, cu);
22002
22003 if (descriptive_type)
22004 {
22005 ALLOCATE_GNAT_AUX_TYPE (type);
22006 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22007 }
22008}
22009
c906108c
SS
22010/* Return the containing type of the die in question using its
22011 DW_AT_containing_type attribute. */
22012
22013static struct type *
e7c27a73 22014die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22015{
c906108c 22016 struct attribute *type_attr;
5e22e966 22017 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22018
e142c38c 22019 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22020 if (!type_attr)
22021 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22022 "[in module %s]"), objfile_name (objfile));
33ac96f0 22023
673bfd45 22024 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22025}
22026
ac9ec31b
DE
22027/* Return an error marker type to use for the ill formed type in DIE/CU. */
22028
22029static struct type *
22030build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22031{
976ca316
SM
22032 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22033 struct objfile *objfile = per_objfile->objfile;
528e1572 22034 char *saved;
ac9ec31b 22035
528e1572
SM
22036 std::string message
22037 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22038 objfile_name (objfile),
22039 sect_offset_str (cu->header.sect_off),
22040 sect_offset_str (die->sect_off));
efba19b0 22041 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22042
19f392bc 22043 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22044}
22045
673bfd45 22046/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22047 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22048 DW_AT_containing_type.
673bfd45
DE
22049 If there is no type substitute an error marker. */
22050
c906108c 22051static struct type *
ff39bb5e 22052lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22053 struct dwarf2_cu *cu)
c906108c 22054{
976ca316
SM
22055 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22056 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22057 struct type *this_type;
22058
ac9ec31b
DE
22059 gdb_assert (attr->name == DW_AT_type
22060 || attr->name == DW_AT_GNAT_descriptive_type
22061 || attr->name == DW_AT_containing_type);
22062
673bfd45
DE
22063 /* First see if we have it cached. */
22064
36586728
TT
22065 if (attr->form == DW_FORM_GNU_ref_alt)
22066 {
22067 struct dwarf2_per_cu_data *per_cu;
0826b30a 22068 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22069
976ca316
SM
22070 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22071 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22072 }
cd6c91b4 22073 else if (attr->form_is_ref ())
673bfd45 22074 {
0826b30a 22075 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22076
976ca316 22077 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22078 }
55f1336d 22079 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22080 {
630ed6b9 22081 ULONGEST signature = attr->as_signature ();
673bfd45 22082
ac9ec31b 22083 return get_signatured_type (die, signature, cu);
673bfd45
DE
22084 }
22085 else
22086 {
b98664d3 22087 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22088 " at %s [in module %s]"),
22089 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22090 objfile_name (objfile));
ac9ec31b 22091 return build_error_marker_type (cu, die);
673bfd45
DE
22092 }
22093
22094 /* If not cached we need to read it in. */
22095
22096 if (this_type == NULL)
22097 {
ac9ec31b 22098 struct die_info *type_die = NULL;
673bfd45
DE
22099 struct dwarf2_cu *type_cu = cu;
22100
cd6c91b4 22101 if (attr->form_is_ref ())
ac9ec31b
DE
22102 type_die = follow_die_ref (die, attr, &type_cu);
22103 if (type_die == NULL)
22104 return build_error_marker_type (cu, die);
22105 /* If we find the type now, it's probably because the type came
3019eac3
DE
22106 from an inter-CU reference and the type's CU got expanded before
22107 ours. */
ac9ec31b 22108 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22109 }
22110
22111 /* If we still don't have a type use an error marker. */
22112
22113 if (this_type == NULL)
ac9ec31b 22114 return build_error_marker_type (cu, die);
673bfd45 22115
f792889a 22116 return this_type;
c906108c
SS
22117}
22118
673bfd45
DE
22119/* Return the type in DIE, CU.
22120 Returns NULL for invalid types.
22121
02142a6c 22122 This first does a lookup in die_type_hash,
673bfd45
DE
22123 and only reads the die in if necessary.
22124
22125 NOTE: This can be called when reading in partial or full symbols. */
22126
f792889a 22127static struct type *
e7c27a73 22128read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22129{
f792889a
DJ
22130 struct type *this_type;
22131
22132 this_type = get_die_type (die, cu);
22133 if (this_type)
22134 return this_type;
22135
673bfd45
DE
22136 return read_type_die_1 (die, cu);
22137}
22138
22139/* Read the type in DIE, CU.
22140 Returns NULL for invalid types. */
22141
22142static struct type *
22143read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22144{
22145 struct type *this_type = NULL;
22146
c906108c
SS
22147 switch (die->tag)
22148 {
22149 case DW_TAG_class_type:
680b30c7 22150 case DW_TAG_interface_type:
c906108c
SS
22151 case DW_TAG_structure_type:
22152 case DW_TAG_union_type:
f792889a 22153 this_type = read_structure_type (die, cu);
c906108c
SS
22154 break;
22155 case DW_TAG_enumeration_type:
f792889a 22156 this_type = read_enumeration_type (die, cu);
c906108c
SS
22157 break;
22158 case DW_TAG_subprogram:
22159 case DW_TAG_subroutine_type:
edb3359d 22160 case DW_TAG_inlined_subroutine:
f792889a 22161 this_type = read_subroutine_type (die, cu);
c906108c
SS
22162 break;
22163 case DW_TAG_array_type:
f792889a 22164 this_type = read_array_type (die, cu);
c906108c 22165 break;
72019c9c 22166 case DW_TAG_set_type:
f792889a 22167 this_type = read_set_type (die, cu);
72019c9c 22168 break;
c906108c 22169 case DW_TAG_pointer_type:
f792889a 22170 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22171 break;
22172 case DW_TAG_ptr_to_member_type:
f792889a 22173 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22174 break;
22175 case DW_TAG_reference_type:
4297a3f0
AV
22176 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22177 break;
22178 case DW_TAG_rvalue_reference_type:
22179 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22180 break;
22181 case DW_TAG_const_type:
f792889a 22182 this_type = read_tag_const_type (die, cu);
c906108c
SS
22183 break;
22184 case DW_TAG_volatile_type:
f792889a 22185 this_type = read_tag_volatile_type (die, cu);
c906108c 22186 break;
06d66ee9
TT
22187 case DW_TAG_restrict_type:
22188 this_type = read_tag_restrict_type (die, cu);
22189 break;
c906108c 22190 case DW_TAG_string_type:
f792889a 22191 this_type = read_tag_string_type (die, cu);
c906108c
SS
22192 break;
22193 case DW_TAG_typedef:
f792889a 22194 this_type = read_typedef (die, cu);
c906108c 22195 break;
a02abb62 22196 case DW_TAG_subrange_type:
f792889a 22197 this_type = read_subrange_type (die, cu);
a02abb62 22198 break;
c906108c 22199 case DW_TAG_base_type:
f792889a 22200 this_type = read_base_type (die, cu);
c906108c 22201 break;
81a17f79 22202 case DW_TAG_unspecified_type:
f792889a 22203 this_type = read_unspecified_type (die, cu);
81a17f79 22204 break;
0114d602
DJ
22205 case DW_TAG_namespace:
22206 this_type = read_namespace_type (die, cu);
22207 break;
f55ee35c
JK
22208 case DW_TAG_module:
22209 this_type = read_module_type (die, cu);
22210 break;
a2c2acaf
MW
22211 case DW_TAG_atomic_type:
22212 this_type = read_tag_atomic_type (die, cu);
22213 break;
c906108c 22214 default:
b98664d3 22215 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22216 dwarf_tag_name (die->tag));
c906108c
SS
22217 break;
22218 }
63d06c5c 22219
f792889a 22220 return this_type;
63d06c5c
DC
22221}
22222
abc72ce4
DE
22223/* See if we can figure out if the class lives in a namespace. We do
22224 this by looking for a member function; its demangled name will
22225 contain namespace info, if there is any.
22226 Return the computed name or NULL.
22227 Space for the result is allocated on the objfile's obstack.
22228 This is the full-die version of guess_partial_die_structure_name.
22229 In this case we know DIE has no useful parent. */
22230
43816ebc 22231static const char *
abc72ce4
DE
22232guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22233{
22234 struct die_info *spec_die;
22235 struct dwarf2_cu *spec_cu;
22236 struct die_info *child;
5e22e966 22237 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22238
22239 spec_cu = cu;
22240 spec_die = die_specification (die, &spec_cu);
22241 if (spec_die != NULL)
22242 {
22243 die = spec_die;
22244 cu = spec_cu;
22245 }
22246
22247 for (child = die->child;
22248 child != NULL;
22249 child = child->sibling)
22250 {
22251 if (child->tag == DW_TAG_subprogram)
22252 {
73b9be8b 22253 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22254
7d45c7c3 22255 if (linkage_name != NULL)
abc72ce4 22256 {
43816ebc 22257 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22258 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22259 const char *name = NULL;
abc72ce4
DE
22260
22261 if (actual_name != NULL)
22262 {
15d034d0 22263 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22264
22265 if (die_name != NULL
43816ebc 22266 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22267 {
22268 /* Strip off the class name from the full name.
22269 We want the prefix. */
22270 int die_name_len = strlen (die_name);
43816ebc
TT
22271 int actual_name_len = strlen (actual_name.get ());
22272 const char *ptr = actual_name.get ();
abc72ce4
DE
22273
22274 /* Test for '::' as a sanity check. */
22275 if (actual_name_len > die_name_len + 2
43816ebc 22276 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22277 name = obstack_strndup (
e3b94546 22278 &objfile->per_bfd->storage_obstack,
43816ebc 22279 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22280 }
22281 }
abc72ce4
DE
22282 return name;
22283 }
22284 }
22285 }
22286
22287 return NULL;
22288}
22289
96408a79
SA
22290/* GCC might emit a nameless typedef that has a linkage name. Determine the
22291 prefix part in such case. See
22292 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22293
a121b7c1 22294static const char *
96408a79
SA
22295anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22296{
22297 struct attribute *attr;
e6a959d6 22298 const char *base;
96408a79
SA
22299
22300 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22301 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22302 return NULL;
22303
7d45c7c3 22304 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22305 return NULL;
22306
73b9be8b 22307 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22308 const char *attr_name = attr->as_string ();
22309 if (attr == NULL || attr_name == NULL)
96408a79
SA
22310 return NULL;
22311
22312 /* dwarf2_name had to be already called. */
3b64bf15 22313 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22314
22315 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22316 base = strrchr (attr_name, ':');
22317 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22318 return "";
22319
5e22e966 22320 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22321 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22322 attr_name,
22323 &base[-1] - attr_name);
96408a79
SA
22324}
22325
fdde2d81 22326/* Return the name of the namespace/class that DIE is defined within,
0114d602 22327 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22328
0114d602
DJ
22329 For example, if we're within the method foo() in the following
22330 code:
22331
22332 namespace N {
22333 class C {
22334 void foo () {
22335 }
22336 };
22337 }
22338
22339 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22340
0d5cff50 22341static const char *
e142c38c 22342determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22343{
976ca316 22344 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22345 struct die_info *parent, *spec_die;
22346 struct dwarf2_cu *spec_cu;
22347 struct type *parent_type;
a121b7c1 22348 const char *retval;
63d06c5c 22349
9c37b5ae 22350 if (cu->language != language_cplus
c44af4eb
TT
22351 && cu->language != language_fortran && cu->language != language_d
22352 && cu->language != language_rust)
0114d602
DJ
22353 return "";
22354
96408a79
SA
22355 retval = anonymous_struct_prefix (die, cu);
22356 if (retval)
22357 return retval;
22358
0114d602
DJ
22359 /* We have to be careful in the presence of DW_AT_specification.
22360 For example, with GCC 3.4, given the code
22361
22362 namespace N {
22363 void foo() {
22364 // Definition of N::foo.
22365 }
22366 }
22367
22368 then we'll have a tree of DIEs like this:
22369
22370 1: DW_TAG_compile_unit
22371 2: DW_TAG_namespace // N
22372 3: DW_TAG_subprogram // declaration of N::foo
22373 4: DW_TAG_subprogram // definition of N::foo
22374 DW_AT_specification // refers to die #3
22375
22376 Thus, when processing die #4, we have to pretend that we're in
22377 the context of its DW_AT_specification, namely the contex of die
22378 #3. */
22379 spec_cu = cu;
22380 spec_die = die_specification (die, &spec_cu);
22381 if (spec_die == NULL)
22382 parent = die->parent;
22383 else
63d06c5c 22384 {
0114d602
DJ
22385 parent = spec_die->parent;
22386 cu = spec_cu;
63d06c5c 22387 }
0114d602
DJ
22388
22389 if (parent == NULL)
22390 return "";
98bfdba5
PA
22391 else if (parent->building_fullname)
22392 {
22393 const char *name;
22394 const char *parent_name;
22395
22396 /* It has been seen on RealView 2.2 built binaries,
22397 DW_TAG_template_type_param types actually _defined_ as
22398 children of the parent class:
22399
22400 enum E {};
22401 template class <class Enum> Class{};
22402 Class<enum E> class_e;
22403
22404 1: DW_TAG_class_type (Class)
22405 2: DW_TAG_enumeration_type (E)
22406 3: DW_TAG_enumerator (enum1:0)
22407 3: DW_TAG_enumerator (enum2:1)
22408 ...
22409 2: DW_TAG_template_type_param
22410 DW_AT_type DW_FORM_ref_udata (E)
22411
22412 Besides being broken debug info, it can put GDB into an
22413 infinite loop. Consider:
22414
22415 When we're building the full name for Class<E>, we'll start
22416 at Class, and go look over its template type parameters,
22417 finding E. We'll then try to build the full name of E, and
22418 reach here. We're now trying to build the full name of E,
22419 and look over the parent DIE for containing scope. In the
22420 broken case, if we followed the parent DIE of E, we'd again
22421 find Class, and once again go look at its template type
22422 arguments, etc., etc. Simply don't consider such parent die
22423 as source-level parent of this die (it can't be, the language
22424 doesn't allow it), and break the loop here. */
22425 name = dwarf2_name (die, cu);
22426 parent_name = dwarf2_name (parent, cu);
b98664d3 22427 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22428 name ? name : "<unknown>",
22429 parent_name ? parent_name : "<unknown>");
22430 return "";
22431 }
63d06c5c 22432 else
0114d602
DJ
22433 switch (parent->tag)
22434 {
63d06c5c 22435 case DW_TAG_namespace:
0114d602 22436 parent_type = read_type_die (parent, cu);
acebe513
UW
22437 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22438 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22439 Work around this problem here. */
22440 if (cu->language == language_cplus
7d93a1e0 22441 && strcmp (parent_type->name (), "::") == 0)
acebe513 22442 return "";
0114d602 22443 /* We give a name to even anonymous namespaces. */
7d93a1e0 22444 return parent_type->name ();
63d06c5c 22445 case DW_TAG_class_type:
680b30c7 22446 case DW_TAG_interface_type:
63d06c5c 22447 case DW_TAG_structure_type:
0114d602 22448 case DW_TAG_union_type:
f55ee35c 22449 case DW_TAG_module:
0114d602 22450 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
22451 if (parent_type->name () != NULL)
22452 return parent_type->name ();
0114d602
DJ
22453 else
22454 /* An anonymous structure is only allowed non-static data
22455 members; no typedefs, no member functions, et cetera.
22456 So it does not need a prefix. */
22457 return "";
abc72ce4 22458 case DW_TAG_compile_unit:
95554aad 22459 case DW_TAG_partial_unit:
abc72ce4
DE
22460 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22461 if (cu->language == language_cplus
976ca316 22462 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
22463 && die->child != NULL
22464 && (die->tag == DW_TAG_class_type
22465 || die->tag == DW_TAG_structure_type
22466 || die->tag == DW_TAG_union_type))
22467 {
43816ebc 22468 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
22469 if (name != NULL)
22470 return name;
22471 }
22472 return "";
0a4b0913
AB
22473 case DW_TAG_subprogram:
22474 /* Nested subroutines in Fortran get a prefix with the name
22475 of the parent's subroutine. */
22476 if (cu->language == language_fortran)
22477 {
22478 if ((die->tag == DW_TAG_subprogram)
22479 && (dwarf2_name (parent, cu) != NULL))
22480 return dwarf2_name (parent, cu);
22481 }
22482 return determine_prefix (parent, cu);
3d567982
TT
22483 case DW_TAG_enumeration_type:
22484 parent_type = read_type_die (parent, cu);
22485 if (TYPE_DECLARED_CLASS (parent_type))
22486 {
7d93a1e0
SM
22487 if (parent_type->name () != NULL)
22488 return parent_type->name ();
3d567982
TT
22489 return "";
22490 }
22491 /* Fall through. */
63d06c5c 22492 default:
8176b9b8 22493 return determine_prefix (parent, cu);
63d06c5c 22494 }
63d06c5c
DC
22495}
22496
3e43a32a
MS
22497/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22498 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22499 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22500 an obconcat, otherwise allocate storage for the result. The CU argument is
22501 used to determine the language and hence, the appropriate separator. */
987504bb 22502
f55ee35c 22503#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22504
22505static char *
f55ee35c
JK
22506typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22507 int physname, struct dwarf2_cu *cu)
63d06c5c 22508{
f55ee35c 22509 const char *lead = "";
5c315b68 22510 const char *sep;
63d06c5c 22511
3e43a32a
MS
22512 if (suffix == NULL || suffix[0] == '\0'
22513 || prefix == NULL || prefix[0] == '\0')
987504bb 22514 sep = "";
45280282
IB
22515 else if (cu->language == language_d)
22516 {
22517 /* For D, the 'main' function could be defined in any module, but it
22518 should never be prefixed. */
22519 if (strcmp (suffix, "D main") == 0)
22520 {
22521 prefix = "";
22522 sep = "";
22523 }
22524 else
22525 sep = ".";
22526 }
f55ee35c
JK
22527 else if (cu->language == language_fortran && physname)
22528 {
22529 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22530 DW_AT_MIPS_linkage_name is preferred and used instead. */
22531
22532 lead = "__";
22533 sep = "_MOD_";
22534 }
987504bb
JJ
22535 else
22536 sep = "::";
63d06c5c 22537
6dd47d34
DE
22538 if (prefix == NULL)
22539 prefix = "";
22540 if (suffix == NULL)
22541 suffix = "";
22542
987504bb
JJ
22543 if (obs == NULL)
22544 {
3e43a32a 22545 char *retval
224c3ddb
SM
22546 = ((char *)
22547 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22548
f55ee35c
JK
22549 strcpy (retval, lead);
22550 strcat (retval, prefix);
6dd47d34
DE
22551 strcat (retval, sep);
22552 strcat (retval, suffix);
63d06c5c
DC
22553 return retval;
22554 }
987504bb
JJ
22555 else
22556 {
22557 /* We have an obstack. */
f55ee35c 22558 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22559 }
63d06c5c
DC
22560}
22561
71c25dea
TT
22562/* Get name of a die, return NULL if not found. */
22563
15d034d0
TT
22564static const char *
22565dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 22566 struct objfile *objfile)
71c25dea
TT
22567{
22568 if (name && cu->language == language_cplus)
22569 {
596dc4ad
TT
22570 gdb::unique_xmalloc_ptr<char> canon_name
22571 = cp_canonicalize_string (name);
71c25dea 22572
596dc4ad
TT
22573 if (canon_name != nullptr)
22574 name = objfile->intern (canon_name.get ());
71c25dea
TT
22575 }
22576
22577 return name;
c906108c
SS
22578}
22579
96553a0c
DE
22580/* Get name of a die, return NULL if not found.
22581 Anonymous namespaces are converted to their magic string. */
9219021c 22582
15d034d0 22583static const char *
e142c38c 22584dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22585{
22586 struct attribute *attr;
5e22e966 22587 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 22588
e142c38c 22589 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
22590 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
22591 if (attr_name == nullptr
96553a0c 22592 && die->tag != DW_TAG_namespace
53832f31
TT
22593 && die->tag != DW_TAG_class_type
22594 && die->tag != DW_TAG_interface_type
22595 && die->tag != DW_TAG_structure_type
22596 && die->tag != DW_TAG_union_type)
71c25dea
TT
22597 return NULL;
22598
22599 switch (die->tag)
22600 {
22601 case DW_TAG_compile_unit:
95554aad 22602 case DW_TAG_partial_unit:
71c25dea
TT
22603 /* Compilation units have a DW_AT_name that is a filename, not
22604 a source language identifier. */
22605 case DW_TAG_enumeration_type:
22606 case DW_TAG_enumerator:
22607 /* These tags always have simple identifiers already; no need
22608 to canonicalize them. */
2c830f54 22609 return attr_name;
907af001 22610
96553a0c 22611 case DW_TAG_namespace:
2c830f54
TT
22612 if (attr_name != nullptr)
22613 return attr_name;
96553a0c
DE
22614 return CP_ANONYMOUS_NAMESPACE_STR;
22615
907af001
UW
22616 case DW_TAG_class_type:
22617 case DW_TAG_interface_type:
22618 case DW_TAG_structure_type:
22619 case DW_TAG_union_type:
22620 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22621 structures or unions. These were of the form "._%d" in GCC 4.1,
22622 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22623 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
22624 if (attr_name != nullptr
22625 && (startswith (attr_name, "._")
22626 || startswith (attr_name, "<anonymous")))
907af001 22627 return NULL;
53832f31
TT
22628
22629 /* GCC might emit a nameless typedef that has a linkage name. See
22630 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 22631 if (!attr || attr_name == NULL)
53832f31 22632 {
73b9be8b 22633 attr = dw2_linkage_name_attr (die, cu);
2c830f54 22634 if (attr == NULL || attr_name == NULL)
53832f31
TT
22635 return NULL;
22636
2c830f54 22637 /* Avoid demangling attr_name the second time on a second
df5c6c50 22638 call for the same DIE. */
3b64bf15 22639 if (!attr->canonical_string_p ())
53832f31 22640 {
43816ebc 22641 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 22642 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
22643 if (demangled == nullptr)
22644 return nullptr;
43816ebc 22645
c6481205 22646 attr->set_string_canonical (objfile->intern (demangled.get ()));
53832f31 22647 }
67430cd0 22648
2c830f54
TT
22649 /* Strip any leading namespaces/classes, keep only the
22650 base name. DW_AT_name for named DIEs does not
22651 contain the prefixes. */
22652 const char *base = strrchr (attr_name, ':');
22653 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
22654 return &base[1];
22655 else
2c830f54 22656 return attr_name;
53832f31 22657 }
907af001
UW
22658 break;
22659
71c25dea 22660 default:
907af001
UW
22661 break;
22662 }
22663
3b64bf15 22664 if (!attr->canonical_string_p ())
c6481205
TT
22665 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
22666 objfile));
2c830f54 22667 return attr->as_string ();
9219021c
DC
22668}
22669
22670/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22671 is none. *EXT_CU is the CU containing DIE on input, and the CU
22672 containing the return value on output. */
9219021c
DC
22673
22674static struct die_info *
f2f0e013 22675dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22676{
22677 struct attribute *attr;
9219021c 22678
f2f0e013 22679 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22680 if (attr == NULL)
22681 return NULL;
22682
f2f0e013 22683 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22684}
22685
f9aca02d 22686static void
d97bc12b 22687dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22688{
22689 unsigned int i;
22690
d97bc12b 22691 print_spaces (indent, f);
9d8780f0 22692 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22693 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22694 sect_offset_str (die->sect_off));
d97bc12b
DE
22695
22696 if (die->parent != NULL)
22697 {
22698 print_spaces (indent, f);
9d8780f0
SM
22699 fprintf_unfiltered (f, " parent at offset: %s\n",
22700 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22701 }
22702
22703 print_spaces (indent, f);
22704 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22705 dwarf_bool_name (die->child != NULL));
c906108c 22706
d97bc12b
DE
22707 print_spaces (indent, f);
22708 fprintf_unfiltered (f, " attributes:\n");
22709
c906108c
SS
22710 for (i = 0; i < die->num_attrs; ++i)
22711 {
d97bc12b
DE
22712 print_spaces (indent, f);
22713 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22714 dwarf_attr_name (die->attrs[i].name),
22715 dwarf_form_name (die->attrs[i].form));
d97bc12b 22716
c906108c
SS
22717 switch (die->attrs[i].form)
22718 {
c906108c 22719 case DW_FORM_addr:
336d760d 22720 case DW_FORM_addrx:
3019eac3 22721 case DW_FORM_GNU_addr_index:
d97bc12b 22722 fprintf_unfiltered (f, "address: ");
36d378cf 22723 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
22724 break;
22725 case DW_FORM_block2:
22726 case DW_FORM_block4:
22727 case DW_FORM_block:
22728 case DW_FORM_block1:
56eb65bd 22729 fprintf_unfiltered (f, "block: size %s",
9d2246fc 22730 pulongest (die->attrs[i].as_block ()->size));
c906108c 22731 break;
2dc7f7b3 22732 case DW_FORM_exprloc:
56eb65bd 22733 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 22734 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 22735 break;
0224619f
JK
22736 case DW_FORM_data16:
22737 fprintf_unfiltered (f, "constant of 16 bytes");
22738 break;
4568ecf9
DE
22739 case DW_FORM_ref_addr:
22740 fprintf_unfiltered (f, "ref address: ");
22741 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22742 break;
36586728
TT
22743 case DW_FORM_GNU_ref_alt:
22744 fprintf_unfiltered (f, "alt ref address: ");
22745 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22746 break;
10b3939b
DJ
22747 case DW_FORM_ref1:
22748 case DW_FORM_ref2:
22749 case DW_FORM_ref4:
4568ecf9
DE
22750 case DW_FORM_ref8:
22751 case DW_FORM_ref_udata:
d97bc12b 22752 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22753 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22754 break;
c906108c
SS
22755 case DW_FORM_data1:
22756 case DW_FORM_data2:
22757 case DW_FORM_data4:
ce5d95e1 22758 case DW_FORM_data8:
c906108c 22759 case DW_FORM_udata:
43bbcdc2
PH
22760 fprintf_unfiltered (f, "constant: %s",
22761 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22762 break;
2dc7f7b3
TT
22763 case DW_FORM_sec_offset:
22764 fprintf_unfiltered (f, "section offset: %s",
22765 pulongest (DW_UNSND (&die->attrs[i])));
22766 break;
55f1336d 22767 case DW_FORM_ref_sig8:
ac9ec31b 22768 fprintf_unfiltered (f, "signature: %s",
630ed6b9 22769 hex_string (die->attrs[i].as_signature ()));
348e048f 22770 break;
c906108c 22771 case DW_FORM_string:
4bdf3d34 22772 case DW_FORM_strp:
43988095 22773 case DW_FORM_line_strp:
cf532bd1 22774 case DW_FORM_strx:
3019eac3 22775 case DW_FORM_GNU_str_index:
36586728 22776 case DW_FORM_GNU_strp_alt:
8285870a 22777 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
22778 die->attrs[i].as_string ()
22779 ? die->attrs[i].as_string () : "",
22780 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
22781 break;
22782 case DW_FORM_flag:
22783 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22784 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22785 else
d97bc12b 22786 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22787 break;
2dc7f7b3
TT
22788 case DW_FORM_flag_present:
22789 fprintf_unfiltered (f, "flag: TRUE");
22790 break;
a8329558 22791 case DW_FORM_indirect:
0963b4bd
MS
22792 /* The reader will have reduced the indirect form to
22793 the "base form" so this form should not occur. */
5f48f8f3 22794 fprintf_unfiltered (f,
3e43a32a 22795 "unexpected attribute form: DW_FORM_indirect");
a8329558 22796 break;
1bc397c5 22797 case DW_FORM_sdata:
663c44ac
JK
22798 case DW_FORM_implicit_const:
22799 fprintf_unfiltered (f, "constant: %s",
1bc397c5 22800 plongest (die->attrs[i].as_signed ()));
663c44ac 22801 break;
c906108c 22802 default:
d97bc12b 22803 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22804 die->attrs[i].form);
d97bc12b 22805 break;
c906108c 22806 }
d97bc12b 22807 fprintf_unfiltered (f, "\n");
c906108c
SS
22808 }
22809}
22810
f9aca02d 22811static void
d97bc12b 22812dump_die_for_error (struct die_info *die)
c906108c 22813{
d97bc12b
DE
22814 dump_die_shallow (gdb_stderr, 0, die);
22815}
22816
22817static void
22818dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22819{
22820 int indent = level * 4;
22821
22822 gdb_assert (die != NULL);
22823
22824 if (level >= max_level)
22825 return;
22826
22827 dump_die_shallow (f, indent, die);
22828
22829 if (die->child != NULL)
c906108c 22830 {
d97bc12b
DE
22831 print_spaces (indent, f);
22832 fprintf_unfiltered (f, " Children:");
22833 if (level + 1 < max_level)
22834 {
22835 fprintf_unfiltered (f, "\n");
22836 dump_die_1 (f, level + 1, max_level, die->child);
22837 }
22838 else
22839 {
3e43a32a
MS
22840 fprintf_unfiltered (f,
22841 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22842 }
22843 }
22844
22845 if (die->sibling != NULL && level > 0)
22846 {
22847 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22848 }
22849}
22850
d97bc12b
DE
22851/* This is called from the pdie macro in gdbinit.in.
22852 It's not static so gcc will keep a copy callable from gdb. */
22853
22854void
22855dump_die (struct die_info *die, int max_level)
22856{
22857 dump_die_1 (gdb_stdlog, 0, max_level, die);
22858}
22859
f9aca02d 22860static void
51545339 22861store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22862{
51545339 22863 void **slot;
c906108c 22864
9c541725
PA
22865 slot = htab_find_slot_with_hash (cu->die_hash, die,
22866 to_underlying (die->sect_off),
b64f50a1 22867 INSERT);
51545339
DJ
22868
22869 *slot = die;
c906108c
SS
22870}
22871
348e048f
DE
22872/* Follow reference or signature attribute ATTR of SRC_DIE.
22873 On entry *REF_CU is the CU of SRC_DIE.
22874 On exit *REF_CU is the CU of the result. */
22875
22876static struct die_info *
ff39bb5e 22877follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22878 struct dwarf2_cu **ref_cu)
22879{
22880 struct die_info *die;
22881
cd6c91b4 22882 if (attr->form_is_ref ())
348e048f 22883 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22884 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22885 die = follow_die_sig (src_die, attr, ref_cu);
22886 else
22887 {
22888 dump_die_for_error (src_die);
22889 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22890 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22891 }
22892
22893 return die;
03dd20cc
DJ
22894}
22895
5c631832 22896/* Follow reference OFFSET.
673bfd45
DE
22897 On entry *REF_CU is the CU of the source die referencing OFFSET.
22898 On exit *REF_CU is the CU of the result.
22899 Returns NULL if OFFSET is invalid. */
f504f079 22900
f9aca02d 22901static struct die_info *
9c541725 22902follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22903 struct dwarf2_cu **ref_cu)
c906108c 22904{
10b3939b 22905 struct die_info temp_die;
f2f0e013 22906 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 22907 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 22908
348e048f
DE
22909 gdb_assert (cu->per_cu != NULL);
22910
98bfdba5
PA
22911 target_cu = cu;
22912
3019eac3 22913 if (cu->per_cu->is_debug_types)
348e048f
DE
22914 {
22915 /* .debug_types CUs cannot reference anything outside their CU.
22916 If they need to, they have to reference a signatured type via
55f1336d 22917 DW_FORM_ref_sig8. */
4057dfde 22918 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22919 return NULL;
348e048f 22920 }
36586728 22921 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22922 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22923 {
22924 struct dwarf2_per_cu_data *per_cu;
9a619af0 22925
9c541725 22926 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 22927 per_objfile);
03dd20cc
DJ
22928
22929 /* If necessary, add it to the queue and load its DIEs. */
976ca316
SM
22930 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
22931 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
03dd20cc 22932
976ca316 22933 target_cu = per_objfile->get_cu (per_cu);
10b3939b 22934 }
98bfdba5
PA
22935 else if (cu->dies == NULL)
22936 {
22937 /* We're loading full DIEs during partial symbol reading. */
976ca316
SM
22938 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
22939 load_full_comp_unit (cu->per_cu, per_objfile, false, language_minimal);
98bfdba5 22940 }
c906108c 22941
f2f0e013 22942 *ref_cu = target_cu;
9c541725 22943 temp_die.sect_off = sect_off;
c24bdb02
KS
22944
22945 if (target_cu != cu)
22946 target_cu->ancestor = cu;
22947
9a3c8263 22948 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22949 &temp_die,
22950 to_underlying (sect_off));
5c631832 22951}
10b3939b 22952
5c631832
JK
22953/* Follow reference attribute ATTR of SRC_DIE.
22954 On entry *REF_CU is the CU of SRC_DIE.
22955 On exit *REF_CU is the CU of the result. */
22956
22957static struct die_info *
ff39bb5e 22958follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22959 struct dwarf2_cu **ref_cu)
22960{
0826b30a 22961 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22962 struct dwarf2_cu *cu = *ref_cu;
22963 struct die_info *die;
22964
9c541725 22965 die = follow_die_offset (sect_off,
36586728
TT
22966 (attr->form == DW_FORM_GNU_ref_alt
22967 || cu->per_cu->is_dwz),
22968 ref_cu);
5c631832 22969 if (!die)
9d8780f0
SM
22970 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22971 "at %s [in module %s]"),
22972 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22973 objfile_name (cu->per_objfile->objfile));
348e048f 22974
5c631832
JK
22975 return die;
22976}
22977
d4c9a4f8 22978/* See read.h. */
5c631832
JK
22979
22980struct dwarf2_locexpr_baton
9c541725 22981dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22982 dwarf2_per_cu_data *per_cu,
976ca316 22983 dwarf2_per_objfile *per_objfile,
041d9819
SM
22984 gdb::function_view<CORE_ADDR ()> get_frame_pc,
22985 bool resolve_abstract_p)
5c631832 22986{
5c631832
JK
22987 struct die_info *die;
22988 struct attribute *attr;
22989 struct dwarf2_locexpr_baton retval;
976ca316 22990 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 22991
976ca316 22992 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 22993 if (cu == nullptr)
976ca316 22994 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
22995
22996 if (cu == nullptr)
cc12ce38
DE
22997 {
22998 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22999 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23000 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23001 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23002 }
918dd910 23003
9c541725 23004 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23005 if (!die)
9d8780f0
SM
23006 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23007 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23008
23009 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23010 if (!attr && resolve_abstract_p
976ca316
SM
23011 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23012 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23013 {
041d9819 23014 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23015 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23016 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23017
3360b6e7 23018 for (const auto &cand_off
976ca316 23019 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23020 {
3360b6e7
TV
23021 struct dwarf2_cu *cand_cu = cu;
23022 struct die_info *cand
23023 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23024 if (!cand
23025 || !cand->parent
e4a62c65
TV
23026 || cand->parent->tag != DW_TAG_subprogram)
23027 continue;
23028
23029 CORE_ADDR pc_low, pc_high;
23030 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23031 if (pc_low == ((CORE_ADDR) -1))
23032 continue;
23033 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23034 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23035 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23036 continue;
23037
23038 die = cand;
23039 attr = dwarf2_attr (die, DW_AT_location, cu);
23040 break;
23041 }
23042 }
23043
5c631832
JK
23044 if (!attr)
23045 {
e103e986
JK
23046 /* DWARF: "If there is no such attribute, then there is no effect.".
23047 DATA is ignored if SIZE is 0. */
5c631832 23048
e103e986 23049 retval.data = NULL;
5c631832
JK
23050 retval.size = 0;
23051 }
cd6c91b4 23052 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23053 {
23054 struct dwarf2_loclist_baton loclist_baton;
041d9819 23055 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23056 size_t size;
23057
23058 fill_in_loclist_baton (cu, &loclist_baton, attr);
23059
23060 retval.data = dwarf2_find_location_expression (&loclist_baton,
23061 &size, pc);
23062 retval.size = size;
23063 }
5c631832
JK
23064 else
23065 {
4fc6c0d5 23066 if (!attr->form_is_block ())
9d8780f0 23067 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23068 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23069 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23070
9d2246fc
TT
23071 struct dwarf_block *block = attr->as_block ();
23072 retval.data = block->data;
23073 retval.size = block->size;
5c631832 23074 }
976ca316 23075 retval.per_objfile = per_objfile;
5c631832 23076 retval.per_cu = cu->per_cu;
918dd910 23077
976ca316 23078 per_objfile->age_comp_units ();
918dd910 23079
5c631832 23080 return retval;
348e048f
DE
23081}
23082
d4c9a4f8 23083/* See read.h. */
8b9737bf
TT
23084
23085struct dwarf2_locexpr_baton
23086dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23087 dwarf2_per_cu_data *per_cu,
14095eb3 23088 dwarf2_per_objfile *per_objfile,
041d9819 23089 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23090{
9c541725 23091 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23092
14095eb3 23093 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23094 get_frame_pc);
8b9737bf
TT
23095}
23096
b6807d98
TT
23097/* Write a constant of a given type as target-ordered bytes into
23098 OBSTACK. */
23099
23100static const gdb_byte *
23101write_constant_as_bytes (struct obstack *obstack,
23102 enum bfd_endian byte_order,
23103 struct type *type,
23104 ULONGEST value,
23105 LONGEST *len)
23106{
23107 gdb_byte *result;
23108
23109 *len = TYPE_LENGTH (type);
224c3ddb 23110 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23111 store_unsigned_integer (result, *len, byte_order, value);
23112
23113 return result;
23114}
23115
d4c9a4f8 23116/* See read.h. */
b6807d98
TT
23117
23118const gdb_byte *
9c541725 23119dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23120 dwarf2_per_cu_data *per_cu,
14095eb3 23121 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23122 obstack *obstack,
b6807d98
TT
23123 LONGEST *len)
23124{
b6807d98
TT
23125 struct die_info *die;
23126 struct attribute *attr;
23127 const gdb_byte *result = NULL;
23128 struct type *type;
23129 LONGEST value;
23130 enum bfd_endian byte_order;
14095eb3 23131 struct objfile *objfile = per_objfile->objfile;
b6807d98 23132
7188ed02 23133 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23134 if (cu == nullptr)
23135 cu = load_cu (per_cu, per_objfile, false);
23136
23137 if (cu == nullptr)
cc12ce38
DE
23138 {
23139 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23140 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23141 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23142 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23143 }
b6807d98 23144
9c541725 23145 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23146 if (!die)
9d8780f0
SM
23147 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23148 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23149
23150 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23151 if (attr == NULL)
23152 return NULL;
23153
e3b94546 23154 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23155 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23156
23157 switch (attr->form)
23158 {
23159 case DW_FORM_addr:
336d760d 23160 case DW_FORM_addrx:
b6807d98
TT
23161 case DW_FORM_GNU_addr_index:
23162 {
23163 gdb_byte *tem;
23164
23165 *len = cu->header.addr_size;
224c3ddb 23166 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23167 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23168 result = tem;
23169 }
23170 break;
23171 case DW_FORM_string:
23172 case DW_FORM_strp:
cf532bd1 23173 case DW_FORM_strx:
b6807d98
TT
23174 case DW_FORM_GNU_str_index:
23175 case DW_FORM_GNU_strp_alt:
c6481205 23176 /* The string is already allocated on the objfile obstack, point
b6807d98 23177 directly to it. */
2c830f54
TT
23178 {
23179 const char *attr_name = attr->as_string ();
23180 result = (const gdb_byte *) attr_name;
23181 *len = strlen (attr_name);
23182 }
b6807d98
TT
23183 break;
23184 case DW_FORM_block1:
23185 case DW_FORM_block2:
23186 case DW_FORM_block4:
23187 case DW_FORM_block:
23188 case DW_FORM_exprloc:
0224619f 23189 case DW_FORM_data16:
9d2246fc
TT
23190 {
23191 struct dwarf_block *block = attr->as_block ();
23192 result = block->data;
23193 *len = block->size;
23194 }
b6807d98
TT
23195 break;
23196
23197 /* The DW_AT_const_value attributes are supposed to carry the
23198 symbol's value "represented as it would be on the target
23199 architecture." By the time we get here, it's already been
23200 converted to host endianness, so we just need to sign- or
23201 zero-extend it as appropriate. */
23202 case DW_FORM_data1:
23203 type = die_type (die, cu);
23204 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23205 if (result == NULL)
23206 result = write_constant_as_bytes (obstack, byte_order,
23207 type, value, len);
23208 break;
23209 case DW_FORM_data2:
23210 type = die_type (die, cu);
23211 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23212 if (result == NULL)
23213 result = write_constant_as_bytes (obstack, byte_order,
23214 type, value, len);
23215 break;
23216 case DW_FORM_data4:
23217 type = die_type (die, cu);
23218 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23219 if (result == NULL)
23220 result = write_constant_as_bytes (obstack, byte_order,
23221 type, value, len);
23222 break;
23223 case DW_FORM_data8:
23224 type = die_type (die, cu);
23225 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23226 if (result == NULL)
23227 result = write_constant_as_bytes (obstack, byte_order,
23228 type, value, len);
23229 break;
23230
23231 case DW_FORM_sdata:
663c44ac 23232 case DW_FORM_implicit_const:
b6807d98
TT
23233 type = die_type (die, cu);
23234 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23235 type, attr->as_signed (), len);
b6807d98
TT
23236 break;
23237
23238 case DW_FORM_udata:
23239 type = die_type (die, cu);
23240 result = write_constant_as_bytes (obstack, byte_order,
23241 type, DW_UNSND (attr), len);
23242 break;
23243
23244 default:
b98664d3 23245 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23246 dwarf_form_name (attr->form));
23247 break;
23248 }
23249
23250 return result;
23251}
23252
d4c9a4f8 23253/* See read.h. */
7942e96e
AA
23254
23255struct type *
9c541725 23256dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23257 dwarf2_per_cu_data *per_cu,
23258 dwarf2_per_objfile *per_objfile)
7942e96e 23259{
7942e96e
AA
23260 struct die_info *die;
23261
7188ed02 23262 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23263 if (cu == nullptr)
23264 cu = load_cu (per_cu, per_objfile, false);
23265
23266 if (cu == nullptr)
23267 return nullptr;
7942e96e 23268
9c541725 23269 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23270 if (!die)
23271 return NULL;
23272
23273 return die_type (die, cu);
23274}
23275
8cb5117c 23276/* See read.h. */
8a9b8146
TT
23277
23278struct type *
b64f50a1 23279dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23280 dwarf2_per_cu_data *per_cu,
23281 dwarf2_per_objfile *per_objfile)
8a9b8146 23282{
9c541725 23283 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23284 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23285}
23286
ac9ec31b 23287/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23288 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23289 On exit *REF_CU is the CU of the result.
23290 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23291
23292static struct die_info *
ac9ec31b
DE
23293follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23294 struct dwarf2_cu **ref_cu)
348e048f 23295{
348e048f 23296 struct die_info temp_die;
c24bdb02 23297 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23298 struct die_info *die;
976ca316 23299 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23300
348e048f 23301
ac9ec31b
DE
23302 /* While it might be nice to assert sig_type->type == NULL here,
23303 we can get here for DW_AT_imported_declaration where we need
23304 the DIE not the type. */
348e048f
DE
23305
23306 /* If necessary, add it to the queue and load its DIEs. */
23307
976ca316 23308 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 23309 language_minimal))
976ca316 23310 read_signatured_type (sig_type, per_objfile);
348e048f 23311
976ca316 23312 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23313 gdb_assert (sig_cu != NULL);
9c541725
PA
23314 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23315 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23316 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23317 to_underlying (temp_die.sect_off));
348e048f
DE
23318 if (die)
23319 {
796a7ff8
DE
23320 /* For .gdb_index version 7 keep track of included TUs.
23321 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23322 if (per_objfile->per_bfd->index_table != NULL
23323 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23324 {
ae640021 23325 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23326 }
23327
348e048f 23328 *ref_cu = sig_cu;
c24bdb02
KS
23329 if (sig_cu != cu)
23330 sig_cu->ancestor = cu;
23331
348e048f
DE
23332 return die;
23333 }
23334
ac9ec31b
DE
23335 return NULL;
23336}
23337
23338/* Follow signatured type referenced by ATTR in SRC_DIE.
23339 On entry *REF_CU is the CU of SRC_DIE.
23340 On exit *REF_CU is the CU of the result.
23341 The result is the DIE of the type.
23342 If the referenced type cannot be found an error is thrown. */
23343
23344static struct die_info *
ff39bb5e 23345follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23346 struct dwarf2_cu **ref_cu)
23347{
630ed6b9 23348 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23349 struct signatured_type *sig_type;
23350 struct die_info *die;
23351
23352 gdb_assert (attr->form == DW_FORM_ref_sig8);
23353
a2ce51a0 23354 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23355 /* sig_type will be NULL if the signatured type is missing from
23356 the debug info. */
23357 if (sig_type == NULL)
23358 {
23359 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23360 " from DIE at %s [in module %s]"),
23361 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23362 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23363 }
23364
23365 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23366 if (die == NULL)
23367 {
23368 dump_die_for_error (src_die);
23369 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23370 " from DIE at %s [in module %s]"),
23371 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23372 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23373 }
23374
23375 return die;
23376}
23377
23378/* Get the type specified by SIGNATURE referenced in DIE/CU,
23379 reading in and processing the type unit if necessary. */
23380
23381static struct type *
23382get_signatured_type (struct die_info *die, ULONGEST signature,
23383 struct dwarf2_cu *cu)
23384{
976ca316 23385 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
23386 struct signatured_type *sig_type;
23387 struct dwarf2_cu *type_cu;
23388 struct die_info *type_die;
23389 struct type *type;
23390
a2ce51a0 23391 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23392 /* sig_type will be NULL if the signatured type is missing from
23393 the debug info. */
23394 if (sig_type == NULL)
23395 {
b98664d3 23396 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23397 " from DIE at %s [in module %s]"),
23398 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23399 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23400 return build_error_marker_type (cu, die);
23401 }
23402
23403 /* If we already know the type we're done. */
976ca316 23404 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
23405 if (type != nullptr)
23406 return type;
ac9ec31b
DE
23407
23408 type_cu = cu;
23409 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23410 if (type_die != NULL)
23411 {
23412 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23413 is created. This is important, for example, because for c++ classes
23414 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23415 type = read_type_die (type_die, type_cu);
23416 if (type == NULL)
23417 {
b98664d3 23418 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23419 " referenced from DIE at %s [in module %s]"),
23420 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23421 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23422 type = build_error_marker_type (cu, die);
23423 }
23424 }
23425 else
23426 {
b98664d3 23427 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23428 " from DIE at %s [in module %s]"),
23429 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 23430 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23431 type = build_error_marker_type (cu, die);
23432 }
e286671b 23433
976ca316 23434 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
23435
23436 return type;
23437}
23438
23439/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23440 reading in and processing the type unit if necessary. */
23441
23442static struct type *
ff39bb5e 23443get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23444 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23445{
23446 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 23447 if (attr->form_is_ref ())
ac9ec31b
DE
23448 {
23449 struct dwarf2_cu *type_cu = cu;
23450 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23451
23452 return read_type_die (type_die, type_cu);
23453 }
23454 else if (attr->form == DW_FORM_ref_sig8)
23455 {
630ed6b9 23456 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
23457 }
23458 else
23459 {
976ca316 23460 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23461
b98664d3 23462 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23463 " at %s [in module %s]"),
23464 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 23465 objfile_name (per_objfile->objfile));
ac9ec31b
DE
23466 return build_error_marker_type (cu, die);
23467 }
348e048f
DE
23468}
23469
e5fe5e75 23470/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23471
23472static void
ab432490
SM
23473load_full_type_unit (dwarf2_per_cu_data *per_cu,
23474 dwarf2_per_objfile *per_objfile)
348e048f 23475{
52dc124a 23476 struct signatured_type *sig_type;
348e048f 23477
f4dc4d17 23478 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 23479 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 23480
6721b2ec
DE
23481 /* We have the per_cu, but we need the signatured_type.
23482 Fortunately this is an easy translation. */
23483 gdb_assert (per_cu->is_debug_types);
23484 sig_type = (struct signatured_type *) per_cu;
348e048f 23485
7188ed02 23486 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23487
ab432490 23488 read_signatured_type (sig_type, per_objfile);
348e048f 23489
7188ed02 23490 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
23491}
23492
3019eac3
DE
23493/* Read in a signatured type and build its CU and DIEs.
23494 If the type is a stub for the real type in a DWO file,
23495 read in the real type from the DWO file as well. */
dee91e82
DE
23496
23497static void
ab432490
SM
23498read_signatured_type (signatured_type *sig_type,
23499 dwarf2_per_objfile *per_objfile)
dee91e82
DE
23500{
23501 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23502
3019eac3 23503 gdb_assert (per_cu->is_debug_types);
7188ed02 23504 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 23505
2e671100 23506 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
23507
23508 if (!reader.dummy_p)
23509 {
23510 struct dwarf2_cu *cu = reader.cu;
23511 const gdb_byte *info_ptr = reader.info_ptr;
23512
23513 gdb_assert (cu->die_hash == NULL);
23514 cu->die_hash =
23515 htab_create_alloc_ex (cu->header.length / 12,
23516 die_hash,
23517 die_eq,
23518 NULL,
23519 &cu->comp_unit_obstack,
23520 hashtab_obstack_allocate,
23521 dummy_obstack_deallocate);
23522
3e225074 23523 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
23524 reader.comp_unit_die->child
23525 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23526 reader.comp_unit_die);
23527 cu->dies = reader.comp_unit_die;
23528 /* comp_unit_die is not stored in die_hash, no need. */
23529
23530 /* We try not to read any attributes in this function, because
23531 not all CUs needed for references have been loaded yet, and
23532 symbol table processing isn't initialized. But we have to
23533 set the CU language, or we won't be able to build types
23534 correctly. Similarly, if we do not read the producer, we can
23535 not apply producer-specific interpretation. */
23536 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
23537
23538 reader.keep ();
c0ab21c2
TT
23539 }
23540
7ee85ab1 23541 sig_type->per_cu.tu_read = 1;
c906108c
SS
23542}
23543
c906108c
SS
23544/* Decode simple location descriptions.
23545 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
23546 the location and return the value. If COMPUTED is non-null, it is
23547 set to true to indicate that decoding was successful, and false
23548 otherwise. If COMPUTED is null, then this function may emit a
23549 complaint. */
c906108c
SS
23550
23551static CORE_ADDR
7d79de9a 23552decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 23553{
5e22e966 23554 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
23555 size_t i;
23556 size_t size = blk->size;
d521ce57 23557 const gdb_byte *data = blk->data;
21ae7a4d
JK
23558 CORE_ADDR stack[64];
23559 int stacki;
23560 unsigned int bytes_read, unsnd;
23561 gdb_byte op;
c906108c 23562
7d79de9a
TT
23563 if (computed != nullptr)
23564 *computed = false;
23565
21ae7a4d
JK
23566 i = 0;
23567 stacki = 0;
23568 stack[stacki] = 0;
23569 stack[++stacki] = 0;
23570
23571 while (i < size)
23572 {
23573 op = data[i++];
23574 switch (op)
23575 {
23576 case DW_OP_lit0:
23577 case DW_OP_lit1:
23578 case DW_OP_lit2:
23579 case DW_OP_lit3:
23580 case DW_OP_lit4:
23581 case DW_OP_lit5:
23582 case DW_OP_lit6:
23583 case DW_OP_lit7:
23584 case DW_OP_lit8:
23585 case DW_OP_lit9:
23586 case DW_OP_lit10:
23587 case DW_OP_lit11:
23588 case DW_OP_lit12:
23589 case DW_OP_lit13:
23590 case DW_OP_lit14:
23591 case DW_OP_lit15:
23592 case DW_OP_lit16:
23593 case DW_OP_lit17:
23594 case DW_OP_lit18:
23595 case DW_OP_lit19:
23596 case DW_OP_lit20:
23597 case DW_OP_lit21:
23598 case DW_OP_lit22:
23599 case DW_OP_lit23:
23600 case DW_OP_lit24:
23601 case DW_OP_lit25:
23602 case DW_OP_lit26:
23603 case DW_OP_lit27:
23604 case DW_OP_lit28:
23605 case DW_OP_lit29:
23606 case DW_OP_lit30:
23607 case DW_OP_lit31:
23608 stack[++stacki] = op - DW_OP_lit0;
23609 break;
f1bea926 23610
21ae7a4d
JK
23611 case DW_OP_reg0:
23612 case DW_OP_reg1:
23613 case DW_OP_reg2:
23614 case DW_OP_reg3:
23615 case DW_OP_reg4:
23616 case DW_OP_reg5:
23617 case DW_OP_reg6:
23618 case DW_OP_reg7:
23619 case DW_OP_reg8:
23620 case DW_OP_reg9:
23621 case DW_OP_reg10:
23622 case DW_OP_reg11:
23623 case DW_OP_reg12:
23624 case DW_OP_reg13:
23625 case DW_OP_reg14:
23626 case DW_OP_reg15:
23627 case DW_OP_reg16:
23628 case DW_OP_reg17:
23629 case DW_OP_reg18:
23630 case DW_OP_reg19:
23631 case DW_OP_reg20:
23632 case DW_OP_reg21:
23633 case DW_OP_reg22:
23634 case DW_OP_reg23:
23635 case DW_OP_reg24:
23636 case DW_OP_reg25:
23637 case DW_OP_reg26:
23638 case DW_OP_reg27:
23639 case DW_OP_reg28:
23640 case DW_OP_reg29:
23641 case DW_OP_reg30:
23642 case DW_OP_reg31:
23643 stack[++stacki] = op - DW_OP_reg0;
23644 if (i < size)
7d79de9a
TT
23645 {
23646 if (computed == nullptr)
23647 dwarf2_complex_location_expr_complaint ();
23648 else
23649 return 0;
23650 }
21ae7a4d 23651 break;
c906108c 23652
21ae7a4d
JK
23653 case DW_OP_regx:
23654 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23655 i += bytes_read;
23656 stack[++stacki] = unsnd;
23657 if (i < size)
7d79de9a
TT
23658 {
23659 if (computed == nullptr)
23660 dwarf2_complex_location_expr_complaint ();
23661 else
23662 return 0;
23663 }
21ae7a4d 23664 break;
c906108c 23665
21ae7a4d 23666 case DW_OP_addr:
c8a7a66f
TT
23667 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23668 &bytes_read);
21ae7a4d
JK
23669 i += bytes_read;
23670 break;
d53d4ac5 23671
21ae7a4d
JK
23672 case DW_OP_const1u:
23673 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23674 i += 1;
23675 break;
23676
23677 case DW_OP_const1s:
23678 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23679 i += 1;
23680 break;
23681
23682 case DW_OP_const2u:
23683 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23684 i += 2;
23685 break;
23686
23687 case DW_OP_const2s:
23688 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23689 i += 2;
23690 break;
d53d4ac5 23691
21ae7a4d
JK
23692 case DW_OP_const4u:
23693 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23694 i += 4;
23695 break;
23696
23697 case DW_OP_const4s:
23698 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23699 i += 4;
23700 break;
23701
585861ea
JK
23702 case DW_OP_const8u:
23703 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23704 i += 8;
23705 break;
23706
21ae7a4d
JK
23707 case DW_OP_constu:
23708 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23709 &bytes_read);
23710 i += bytes_read;
23711 break;
23712
23713 case DW_OP_consts:
23714 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23715 i += bytes_read;
23716 break;
23717
23718 case DW_OP_dup:
23719 stack[stacki + 1] = stack[stacki];
23720 stacki++;
23721 break;
23722
23723 case DW_OP_plus:
23724 stack[stacki - 1] += stack[stacki];
23725 stacki--;
23726 break;
23727
23728 case DW_OP_plus_uconst:
23729 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23730 &bytes_read);
23731 i += bytes_read;
23732 break;
23733
23734 case DW_OP_minus:
23735 stack[stacki - 1] -= stack[stacki];
23736 stacki--;
23737 break;
23738
23739 case DW_OP_deref:
23740 /* If we're not the last op, then we definitely can't encode
23741 this using GDB's address_class enum. This is valid for partial
23742 global symbols, although the variable's address will be bogus
23743 in the psymtab. */
23744 if (i < size)
7d79de9a
TT
23745 {
23746 if (computed == nullptr)
23747 dwarf2_complex_location_expr_complaint ();
23748 else
23749 return 0;
23750 }
21ae7a4d
JK
23751 break;
23752
23753 case DW_OP_GNU_push_tls_address:
4aa4e28b 23754 case DW_OP_form_tls_address:
21ae7a4d
JK
23755 /* The top of the stack has the offset from the beginning
23756 of the thread control block at which the variable is located. */
23757 /* Nothing should follow this operator, so the top of stack would
23758 be returned. */
23759 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23760 address will be bogus in the psymtab. Make it always at least
23761 non-zero to not look as a variable garbage collected by linker
23762 which have DW_OP_addr 0. */
21ae7a4d 23763 if (i < size)
7d79de9a
TT
23764 {
23765 if (computed == nullptr)
23766 dwarf2_complex_location_expr_complaint ();
23767 else
23768 return 0;
23769 }
585861ea 23770 stack[stacki]++;
21ae7a4d
JK
23771 break;
23772
23773 case DW_OP_GNU_uninit:
7d79de9a
TT
23774 if (computed != nullptr)
23775 return 0;
21ae7a4d
JK
23776 break;
23777
336d760d 23778 case DW_OP_addrx:
3019eac3 23779 case DW_OP_GNU_addr_index:
49f6c839 23780 case DW_OP_GNU_const_index:
3019eac3
DE
23781 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23782 &bytes_read);
23783 i += bytes_read;
23784 break;
23785
21ae7a4d 23786 default:
7d79de9a
TT
23787 if (computed == nullptr)
23788 {
23789 const char *name = get_DW_OP_name (op);
21ae7a4d 23790
7d79de9a
TT
23791 if (name)
23792 complaint (_("unsupported stack op: '%s'"),
23793 name);
23794 else
23795 complaint (_("unsupported stack op: '%02x'"),
23796 op);
23797 }
21ae7a4d
JK
23798
23799 return (stack[stacki]);
d53d4ac5 23800 }
3c6e0cb3 23801
21ae7a4d
JK
23802 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23803 outside of the allocated space. Also enforce minimum>0. */
23804 if (stacki >= ARRAY_SIZE (stack) - 1)
23805 {
7d79de9a
TT
23806 if (computed == nullptr)
23807 complaint (_("location description stack overflow"));
21ae7a4d
JK
23808 return 0;
23809 }
23810
23811 if (stacki <= 0)
23812 {
7d79de9a
TT
23813 if (computed == nullptr)
23814 complaint (_("location description stack underflow"));
21ae7a4d
JK
23815 return 0;
23816 }
23817 }
7d79de9a
TT
23818
23819 if (computed != nullptr)
23820 *computed = true;
21ae7a4d 23821 return (stack[stacki]);
c906108c
SS
23822}
23823
23824/* memory allocation interface */
23825
c906108c 23826static struct dwarf_block *
7b5a2f43 23827dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23828{
8d749320 23829 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23830}
23831
c906108c 23832static struct die_info *
b60c80d6 23833dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23834{
23835 struct die_info *die;
b60c80d6
DJ
23836 size_t size = sizeof (struct die_info);
23837
23838 if (num_attrs > 1)
23839 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23840
b60c80d6 23841 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23842 memset (die, 0, sizeof (struct die_info));
23843 return (die);
23844}
2e276125
JB
23845
23846\f
a036ba48 23847
c90ec28a 23848/* Macro support. */
cf2c3c16 23849
9eac9650
TT
23850/* An overload of dwarf_decode_macros that finds the correct section
23851 and ensures it is read in before calling the other overload. */
23852
23853static void
23854dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23855 int section_is_gnu)
23856{
976ca316
SM
23857 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23858 struct objfile *objfile = per_objfile->objfile;
5a0e026f 23859 const struct line_header *lh = cu->line_header;
9eac9650
TT
23860 unsigned int offset_size = cu->header.offset_size;
23861 struct dwarf2_section_info *section;
23862 const char *section_name;
23863
23864 if (cu->dwo_unit != nullptr)
23865 {
23866 if (section_is_gnu)
23867 {
23868 section = &cu->dwo_unit->dwo_file->sections.macro;
23869 section_name = ".debug_macro.dwo";
23870 }
23871 else
23872 {
23873 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23874 section_name = ".debug_macinfo.dwo";
23875 }
23876 }
23877 else
23878 {
23879 if (section_is_gnu)
23880 {
976ca316 23881 section = &per_objfile->per_bfd->macro;
9eac9650
TT
23882 section_name = ".debug_macro";
23883 }
23884 else
23885 {
976ca316 23886 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
23887 section_name = ".debug_macinfo";
23888 }
23889 }
23890
23891 section->read (objfile);
23892 if (section->buffer == nullptr)
23893 {
23894 complaint (_("missing %s section"), section_name);
23895 return;
23896 }
23897
23898 buildsym_compunit *builder = cu->get_builder ();
23899
976ca316 23900 dwarf_decode_macros (per_objfile, builder, section, lh,
9eac9650
TT
23901 offset_size, offset, section_is_gnu);
23902}
23903
3019eac3
DE
23904/* Return the .debug_loc section to use for CU.
23905 For DWO files use .debug_loc.dwo. */
23906
23907static struct dwarf2_section_info *
23908cu_debug_loc_section (struct dwarf2_cu *cu)
23909{
976ca316 23910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 23911
3019eac3 23912 if (cu->dwo_unit)
43988095
JK
23913 {
23914 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23915
43988095
JK
23916 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23917 }
976ca316
SM
23918 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
23919 : &per_objfile->per_bfd->loc);
3019eac3
DE
23920}
23921
d0ce17d8
CT
23922/* Return the .debug_rnglists section to use for CU. */
23923static struct dwarf2_section_info *
23924cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
23925{
23926 if (cu->header.version < 5)
23927 error (_(".debug_rnglists section cannot be used in DWARF %d"),
23928 cu->header.version);
23929 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23930
23931 /* Make sure we read the .debug_rnglists section from the file that
23932 contains the DW_AT_ranges attribute we are reading. Normally that
23933 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
23934 or DW_TAG_skeleton unit, we always want to read from objfile/linked
23935 program. */
23936 if (cu->dwo_unit != nullptr
23937 && tag != DW_TAG_compile_unit
23938 && tag != DW_TAG_skeleton_unit)
23939 {
23940 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23941
23942 if (sections->rnglists.size > 0)
23943 return &sections->rnglists;
23944 else
23945 error (_(".debug_rnglists section is missing from .dwo file."));
23946 }
23947 return &dwarf2_per_objfile->per_bfd->rnglists;
23948}
23949
8cf6f0b1
TT
23950/* A helper function that fills in a dwarf2_loclist_baton. */
23951
23952static void
23953fill_in_loclist_baton (struct dwarf2_cu *cu,
23954 struct dwarf2_loclist_baton *baton,
ff39bb5e 23955 const struct attribute *attr)
8cf6f0b1 23956{
976ca316 23957 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
23958 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23959
976ca316 23960 section->read (per_objfile->objfile);
8cf6f0b1 23961
976ca316 23962 baton->per_objfile = per_objfile;
8cf6f0b1
TT
23963 baton->per_cu = cu->per_cu;
23964 gdb_assert (baton->per_cu);
23965 /* We don't know how long the location list is, but make sure we
23966 don't run off the edge of the section. */
d4df075e
TT
23967 baton->size = section->size - attr->as_unsigned ();
23968 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
23969 if (cu->base_address.has_value ())
23970 baton->base_address = *cu->base_address;
23971 else
23972 baton->base_address = 0;
f664829e 23973 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23974}
23975
4c2df51b 23976static void
ff39bb5e 23977dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23978 struct dwarf2_cu *cu, int is_block)
4c2df51b 23979{
976ca316
SM
23980 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23981 struct objfile *objfile = per_objfile->objfile;
3019eac3 23982 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23983
cd6c91b4 23984 if (attr->form_is_section_offset ()
3019eac3 23985 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23986 the section. If so, fall through to the complaint in the
23987 other branch. */
d4df075e 23988 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 23989 {
0d53c4c4 23990 struct dwarf2_loclist_baton *baton;
4c2df51b 23991
8d749320 23992 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23993
8cf6f0b1 23994 fill_in_loclist_baton (cu, baton, attr);
be391dca 23995
2b24b6e4 23996 if (!cu->base_address.has_value ())
b98664d3 23997 complaint (_("Location list used without "
3e43a32a 23998 "specifying the CU base address."));
4c2df51b 23999
f1e6e072
TT
24000 SYMBOL_ACLASS_INDEX (sym) = (is_block
24001 ? dwarf2_loclist_block_index
24002 : dwarf2_loclist_index);
0d53c4c4
DJ
24003 SYMBOL_LOCATION_BATON (sym) = baton;
24004 }
24005 else
24006 {
24007 struct dwarf2_locexpr_baton *baton;
24008
8d749320 24009 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24010 baton->per_objfile = per_objfile;
ae0d2f24
UW
24011 baton->per_cu = cu->per_cu;
24012 gdb_assert (baton->per_cu);
0d53c4c4 24013
4fc6c0d5 24014 if (attr->form_is_block ())
0d53c4c4
DJ
24015 {
24016 /* Note that we're just copying the block's data pointer
24017 here, not the actual data. We're still pointing into the
6502dd73
DJ
24018 info_buffer for SYM's objfile; right now we never release
24019 that buffer, but when we do clean up properly this may
24020 need to change. */
9d2246fc
TT
24021 struct dwarf_block *block = attr->as_block ();
24022 baton->size = block->size;
24023 baton->data = block->data;
0d53c4c4
DJ
24024 }
24025 else
24026 {
24027 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24028 sym->natural_name ());
0d53c4c4 24029 baton->size = 0;
0d53c4c4 24030 }
6e70227d 24031
f1e6e072
TT
24032 SYMBOL_ACLASS_INDEX (sym) = (is_block
24033 ? dwarf2_locexpr_block_index
24034 : dwarf2_locexpr_index);
0d53c4c4
DJ
24035 SYMBOL_LOCATION_BATON (sym) = baton;
24036 }
4c2df51b 24037}
6502dd73 24038
2e6a9f79 24039/* See read.h. */
96408a79 24040
2e6a9f79
SM
24041const comp_unit_head *
24042dwarf2_per_cu_data::get_header () const
96408a79 24043{
2e6a9f79
SM
24044 if (!m_header_read_in)
24045 {
24046 const gdb_byte *info_ptr
24047 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24048
2e6a9f79 24049 memset (&m_header, 0, sizeof (m_header));
96408a79 24050
2e6a9f79
SM
24051 read_comp_unit_head (&m_header, info_ptr, this->section,
24052 rcuh_kind::COMPILE);
24053 }
96408a79 24054
2e6a9f79 24055 return &m_header;
96408a79
SA
24056}
24057
09ba997f 24058/* See read.h. */
ae0d2f24 24059
98714339 24060int
09ba997f 24061dwarf2_per_cu_data::addr_size () const
ae0d2f24 24062{
2e6a9f79 24063 return this->get_header ()->addr_size;
ae0d2f24
UW
24064}
24065
09ba997f 24066/* See read.h. */
9eae7c52
TT
24067
24068int
09ba997f 24069dwarf2_per_cu_data::offset_size () const
9eae7c52 24070{
2e6a9f79 24071 return this->get_header ()->offset_size;
96408a79
SA
24072}
24073
09ba997f 24074/* See read.h. */
96408a79
SA
24075
24076int
09ba997f 24077dwarf2_per_cu_data::ref_addr_size () const
96408a79 24078{
2e6a9f79 24079 const comp_unit_head *header = this->get_header ();
96408a79 24080
2e6a9f79
SM
24081 if (header->version == 2)
24082 return header->addr_size;
96408a79 24083 else
2e6a9f79 24084 return header->offset_size;
181cebd4
JK
24085}
24086
09ba997f 24087/* See read.h. */
9aa1f1e3 24088
09ba997f 24089struct type *
293e7e51 24090dwarf2_cu::addr_type () const
9a49df9d 24091{
293e7e51 24092 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24093 struct type *void_type = objfile_type (objfile)->builtin_void;
24094 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24095 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24096
24097 if (TYPE_LENGTH (addr_type) == addr_size)
24098 return addr_type;
24099
c6d940a9 24100 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24101 return addr_type;
24102}
24103
22b6cd70
TT
24104/* A helper function for dwarf2_find_containing_comp_unit that returns
24105 the index of the result, and that searches a vector. It will
24106 return a result even if the offset in question does not actually
24107 occur in any CU. This is separate so that it can be unit
24108 tested. */
ae038cb0 24109
22b6cd70
TT
24110static int
24111dwarf2_find_containing_comp_unit
24112 (sect_offset sect_off,
24113 unsigned int offset_in_dwz,
24114 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24115{
ae038cb0
DJ
24116 int low, high;
24117
ae038cb0 24118 low = 0;
22b6cd70 24119 high = all_comp_units.size () - 1;
ae038cb0
DJ
24120 while (high > low)
24121 {
36586728 24122 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24123 int mid = low + (high - low) / 2;
9a619af0 24124
22b6cd70 24125 mid_cu = all_comp_units[mid];
36586728 24126 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24127 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24128 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24129 high = mid;
24130 else
24131 low = mid + 1;
24132 }
24133 gdb_assert (low == high);
22b6cd70
TT
24134 return low;
24135}
24136
24137/* Locate the .debug_info compilation unit from CU's objfile which contains
24138 the DIE at OFFSET. Raises an error on failure. */
24139
24140static struct dwarf2_per_cu_data *
24141dwarf2_find_containing_comp_unit (sect_offset sect_off,
24142 unsigned int offset_in_dwz,
976ca316 24143 dwarf2_per_objfile *per_objfile)
22b6cd70 24144{
976ca316
SM
24145 int low = dwarf2_find_containing_comp_unit
24146 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24147 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24148
45b8ae0c 24149 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24150 {
36586728 24151 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24152 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24153 "offset %s [in module %s]"),
24154 sect_offset_str (sect_off),
976ca316 24155 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24156
976ca316 24157 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24158 <= sect_off);
976ca316 24159 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24160 }
24161 else
24162 {
976ca316 24163 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24164 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24165 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24166 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24167 return this_cu;
24168 }
24169}
24170
22b6cd70
TT
24171#if GDB_SELF_TEST
24172
24173namespace selftests {
24174namespace find_containing_comp_unit {
24175
24176static void
24177run_test ()
24178{
24179 struct dwarf2_per_cu_data one {};
24180 struct dwarf2_per_cu_data two {};
24181 struct dwarf2_per_cu_data three {};
24182 struct dwarf2_per_cu_data four {};
24183
24184 one.length = 5;
24185 two.sect_off = sect_offset (one.length);
24186 two.length = 7;
24187
24188 three.length = 5;
24189 three.is_dwz = 1;
24190 four.sect_off = sect_offset (three.length);
24191 four.length = 7;
24192 four.is_dwz = 1;
24193
24194 std::vector<dwarf2_per_cu_data *> units;
24195 units.push_back (&one);
24196 units.push_back (&two);
24197 units.push_back (&three);
24198 units.push_back (&four);
24199
24200 int result;
24201
24202 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24203 SELF_CHECK (units[result] == &one);
24204 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24205 SELF_CHECK (units[result] == &one);
24206 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24207 SELF_CHECK (units[result] == &two);
24208
24209 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24210 SELF_CHECK (units[result] == &three);
24211 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24212 SELF_CHECK (units[result] == &three);
24213 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24214 SELF_CHECK (units[result] == &four);
24215}
24216
24217}
24218}
24219
24220#endif /* GDB_SELF_TEST */
24221
9e021579 24222/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24223
9e021579
SM
24224dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24225 dwarf2_per_objfile *per_objfile)
24226 : per_cu (per_cu),
24227 per_objfile (per_objfile),
9068261f
AB
24228 mark (false),
24229 has_loclist (false),
24230 checked_producer (false),
24231 producer_is_gxx_lt_4_6 (false),
24232 producer_is_gcc_lt_4_3 (false),
eb77c9df 24233 producer_is_icc (false),
9068261f 24234 producer_is_icc_lt_14 (false),
c258c396 24235 producer_is_codewarrior (false),
9068261f 24236 processing_has_namespace_info (false)
93311388 24237{
9816fde3
JK
24238}
24239
24240/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24241
24242static void
95554aad
TT
24243prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24244 enum language pretend_language)
9816fde3
JK
24245{
24246 struct attribute *attr;
24247
24248 /* Set the language we're debugging. */
24249 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24250 if (attr != nullptr)
9816fde3
JK
24251 set_cu_language (DW_UNSND (attr), cu);
24252 else
9cded63f 24253 {
95554aad 24254 cu->language = pretend_language;
9cded63f
TT
24255 cu->language_defn = language_def (cu->language);
24256 }
dee91e82 24257
7d45c7c3 24258 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24259}
24260
7188ed02 24261/* See read.h. */
ae038cb0 24262
7188ed02
SM
24263dwarf2_cu *
24264dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24265{
7188ed02
SM
24266 auto it = m_dwarf2_cus.find (per_cu);
24267 if (it == m_dwarf2_cus.end ())
24268 return nullptr;
ae038cb0 24269
7188ed02
SM
24270 return it->second;
24271}
24272
24273/* See read.h. */
24274
24275void
24276dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24277{
24278 gdb_assert (this->get_cu (per_cu) == nullptr);
24279
24280 m_dwarf2_cus[per_cu] = cu;
24281}
24282
24283/* See read.h. */
24284
24285void
24286dwarf2_per_objfile::age_comp_units ()
24287{
24288 /* Start by clearing all marks. */
24289 for (auto pair : m_dwarf2_cus)
24290 pair.second->mark = false;
24291
24292 /* Traverse all CUs, mark them and their dependencies if used recently
24293 enough. */
24294 for (auto pair : m_dwarf2_cus)
ae038cb0 24295 {
7188ed02
SM
24296 dwarf2_cu *cu = pair.second;
24297
24298 cu->last_used++;
24299 if (cu->last_used <= dwarf_max_cache_age)
24300 dwarf2_mark (cu);
ae038cb0
DJ
24301 }
24302
7188ed02
SM
24303 /* Delete all CUs still not marked. */
24304 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24305 {
7188ed02 24306 dwarf2_cu *cu = it->second;
ae038cb0 24307
7188ed02 24308 if (!cu->mark)
ae038cb0 24309 {
7188ed02
SM
24310 delete cu;
24311 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24312 }
24313 else
7188ed02 24314 it++;
ae038cb0
DJ
24315 }
24316}
24317
7188ed02 24318/* See read.h. */
ae038cb0 24319
7188ed02
SM
24320void
24321dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24322{
7188ed02
SM
24323 auto it = m_dwarf2_cus.find (per_cu);
24324 if (it == m_dwarf2_cus.end ())
24325 return;
ae038cb0 24326
7188ed02 24327 delete it->second;
ae038cb0 24328
7188ed02
SM
24329 m_dwarf2_cus.erase (it);
24330}
ae038cb0 24331
7188ed02
SM
24332dwarf2_per_objfile::~dwarf2_per_objfile ()
24333{
24334 remove_all_cus ();
ae038cb0
DJ
24335}
24336
dee91e82
DE
24337/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24338 We store these in a hash table separate from the DIEs, and preserve them
24339 when the DIEs are flushed out of cache.
24340
24341 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24342 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24343 or the type may come from a DWO file. Furthermore, while it's more logical
24344 to use per_cu->section+offset, with Fission the section with the data is in
24345 the DWO file but we don't know that section at the point we need it.
24346 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24347 because we can enter the lookup routine, get_die_type_at_offset, from
24348 outside this file, and thus won't necessarily have PER_CU->cu.
24349 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24350
dee91e82 24351struct dwarf2_per_cu_offset_and_type
1c379e20 24352{
dee91e82 24353 const struct dwarf2_per_cu_data *per_cu;
9c541725 24354 sect_offset sect_off;
1c379e20
DJ
24355 struct type *type;
24356};
24357
dee91e82 24358/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24359
24360static hashval_t
dee91e82 24361per_cu_offset_and_type_hash (const void *item)
1c379e20 24362{
9a3c8263
SM
24363 const struct dwarf2_per_cu_offset_and_type *ofs
24364 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24365
9c541725 24366 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
24367}
24368
dee91e82 24369/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24370
24371static int
dee91e82 24372per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 24373{
9a3c8263
SM
24374 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24375 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24376 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24377 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 24378
dee91e82 24379 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 24380 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
24381}
24382
24383/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
24384 table if necessary. For convenience, return TYPE.
24385
24386 The DIEs reading must have careful ordering to:
85102364 24387 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
24388 reading current DIE.
24389 * Not trying to dereference contents of still incompletely read in types
24390 while reading in other DIEs.
24391 * Enable referencing still incompletely read in types just by a pointer to
24392 the type without accessing its fields.
24393
24394 Therefore caller should follow these rules:
24395 * Try to fetch any prerequisite types we may need to build this DIE type
24396 before building the type and calling set_die_type.
e71ec853 24397 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
24398 possible before fetching more types to complete the current type.
24399 * Make the type as complete as possible before fetching more types. */
1c379e20 24400
f792889a 24401static struct type *
1c379e20
DJ
24402set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
24403{
976ca316 24404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 24405 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 24406 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
24407 struct attribute *attr;
24408 struct dynamic_prop prop;
1c379e20 24409
b4ba55a1
JB
24410 /* For Ada types, make sure that the gnat-specific data is always
24411 initialized (if not already set). There are a few types where
24412 we should not be doing so, because the type-specific area is
24413 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24414 where the type-specific area is used to store the floatformat).
24415 But this is not a problem, because the gnat-specific information
24416 is actually not needed for these types. */
24417 if (need_gnat_info (cu)
78134374
SM
24418 && type->code () != TYPE_CODE_FUNC
24419 && type->code () != TYPE_CODE_FLT
24420 && type->code () != TYPE_CODE_METHODPTR
24421 && type->code () != TYPE_CODE_MEMBERPTR
24422 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
24423 && !HAVE_GNAT_AUX_INFO (type))
24424 INIT_GNAT_SPECIFIC (type);
24425
3f2f83dd
KB
24426 /* Read DW_AT_allocated and set in type. */
24427 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 24428 if (attr != NULL)
3f2f83dd 24429 {
293e7e51 24430 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24431 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 24432 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 24433 }
3f2f83dd
KB
24434
24435 /* Read DW_AT_associated and set in type. */
24436 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 24437 if (attr != NULL)
3f2f83dd 24438 {
293e7e51 24439 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 24440 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 24441 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 24442 }
3f2f83dd 24443
3cdcd0ce
JB
24444 /* Read DW_AT_data_location and set in type. */
24445 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 24446 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 24447 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 24448
976ca316
SM
24449 if (per_objfile->die_type_hash == NULL)
24450 per_objfile->die_type_hash
0335378b
TT
24451 = htab_up (htab_create_alloc (127,
24452 per_cu_offset_and_type_hash,
24453 per_cu_offset_and_type_eq,
24454 NULL, xcalloc, xfree));
1c379e20 24455
dee91e82 24456 ofs.per_cu = cu->per_cu;
9c541725 24457 ofs.sect_off = die->sect_off;
1c379e20 24458 ofs.type = type;
dee91e82 24459 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 24460 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 24461 if (*slot)
b98664d3 24462 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 24463 sect_offset_str (die->sect_off));
8d749320
SM
24464 *slot = XOBNEW (&objfile->objfile_obstack,
24465 struct dwarf2_per_cu_offset_and_type);
1c379e20 24466 **slot = ofs;
f792889a 24467 return type;
1c379e20
DJ
24468}
24469
9c541725 24470/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 24471 or return NULL if the die does not have a saved type. */
1c379e20
DJ
24472
24473static struct type *
9c541725 24474get_die_type_at_offset (sect_offset sect_off,
aa66c379 24475 dwarf2_per_cu_data *per_cu,
976ca316 24476 dwarf2_per_objfile *per_objfile)
1c379e20 24477{
dee91e82 24478 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 24479
976ca316 24480 if (per_objfile->die_type_hash == NULL)
f792889a 24481 return NULL;
1c379e20 24482
dee91e82 24483 ofs.per_cu = per_cu;
9c541725 24484 ofs.sect_off = sect_off;
9a3c8263 24485 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 24486 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
24487 if (slot)
24488 return slot->type;
24489 else
24490 return NULL;
24491}
24492
02142a6c 24493/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
24494 or return NULL if DIE does not have a saved type. */
24495
24496static struct type *
24497get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24498{
aa66c379 24499 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
24500}
24501
10b3939b
DJ
24502/* Add a dependence relationship from CU to REF_PER_CU. */
24503
24504static void
24505dwarf2_add_dependence (struct dwarf2_cu *cu,
24506 struct dwarf2_per_cu_data *ref_per_cu)
24507{
24508 void **slot;
24509
24510 if (cu->dependencies == NULL)
24511 cu->dependencies
24512 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24513 NULL, &cu->comp_unit_obstack,
24514 hashtab_obstack_allocate,
24515 dummy_obstack_deallocate);
24516
24517 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24518 if (*slot == NULL)
24519 *slot = ref_per_cu;
24520}
1c379e20 24521
f504f079
DE
24522/* Subroutine of dwarf2_mark to pass to htab_traverse.
24523 Set the mark field in every compilation unit in the
7188ed02
SM
24524 cache that we must keep because we are keeping CU.
24525
24526 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 24527
10b3939b
DJ
24528static int
24529dwarf2_mark_helper (void **slot, void *data)
24530{
7188ed02
SM
24531 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
24532 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
24533 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
24534
24535 /* cu->dependencies references may not yet have been ever read if QUIT aborts
24536 reading of the chain. As such dependencies remain valid it is not much
24537 useful to track and undo them during QUIT cleanups. */
7188ed02 24538 if (cu == nullptr)
d07ed419
JK
24539 return 1;
24540
7188ed02 24541 if (cu->mark)
10b3939b 24542 return 1;
10b3939b 24543
7188ed02
SM
24544 cu->mark = true;
24545
24546 if (cu->dependencies != nullptr)
24547 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
24548
24549 return 1;
24550}
24551
f504f079
DE
24552/* Set the mark field in CU and in every other compilation unit in the
24553 cache that we must keep because we are keeping CU. */
24554
ae038cb0
DJ
24555static void
24556dwarf2_mark (struct dwarf2_cu *cu)
24557{
24558 if (cu->mark)
24559 return;
7188ed02 24560
9068261f 24561 cu->mark = true;
ae038cb0 24562
7188ed02
SM
24563 if (cu->dependencies != nullptr)
24564 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
24565}
24566
72bf9492
DJ
24567/* Trivial hash function for partial_die_info: the hash value of a DIE
24568 is its offset in .debug_info for this objfile. */
24569
24570static hashval_t
24571partial_die_hash (const void *item)
24572{
9a3c8263
SM
24573 const struct partial_die_info *part_die
24574 = (const struct partial_die_info *) item;
9a619af0 24575
9c541725 24576 return to_underlying (part_die->sect_off);
72bf9492
DJ
24577}
24578
24579/* Trivial comparison function for partial_die_info structures: two DIEs
24580 are equal if they have the same offset. */
24581
24582static int
24583partial_die_eq (const void *item_lhs, const void *item_rhs)
24584{
9a3c8263
SM
24585 const struct partial_die_info *part_die_lhs
24586 = (const struct partial_die_info *) item_lhs;
24587 const struct partial_die_info *part_die_rhs
24588 = (const struct partial_die_info *) item_rhs;
9a619af0 24589
9c541725 24590 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
24591}
24592
3c3bb058
AB
24593struct cmd_list_element *set_dwarf_cmdlist;
24594struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 24595
9291a0cd 24596static void
cd4fb1b2
SM
24597show_check_physname (struct ui_file *file, int from_tty,
24598 struct cmd_list_element *c, const char *value)
9291a0cd 24599{
cd4fb1b2
SM
24600 fprintf_filtered (file,
24601 _("Whether to check \"physname\" is %s.\n"),
24602 value);
9291a0cd
TT
24603}
24604
6c265988 24605void _initialize_dwarf2_read ();
cd4fb1b2 24606void
6c265988 24607_initialize_dwarf2_read ()
9291a0cd 24608{
0743fc83 24609 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 24610Set DWARF specific variables.\n\
590042fc 24611Configure DWARF variables such as the cache size."),
0743fc83
TT
24612 &set_dwarf_cmdlist, "maintenance set dwarf ",
24613 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 24614
0743fc83 24615 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
24616Show DWARF specific variables.\n\
24617Show DWARF variables such as the cache size."),
0743fc83
TT
24618 &show_dwarf_cmdlist, "maintenance show dwarf ",
24619 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 24620
cd4fb1b2
SM
24621 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
24622 &dwarf_max_cache_age, _("\
24623Set the upper bound on the age of cached DWARF compilation units."), _("\
24624Show the upper bound on the age of cached DWARF compilation units."), _("\
24625A higher limit means that cached compilation units will be stored\n\
24626in memory longer, and more total memory will be used. Zero disables\n\
24627caching, which can slow down startup."),
24628 NULL,
24629 show_dwarf_max_cache_age,
24630 &set_dwarf_cmdlist,
24631 &show_dwarf_cmdlist);
156942c7 24632
cd4fb1b2
SM
24633 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
24634Set debugging of the DWARF reader."), _("\
24635Show debugging of the DWARF reader."), _("\
24636When enabled (non-zero), debugging messages are printed during DWARF\n\
24637reading and symtab expansion. A value of 1 (one) provides basic\n\
24638information. A value greater than 1 provides more verbose information."),
24639 NULL,
24640 NULL,
24641 &setdebuglist, &showdebuglist);
9291a0cd 24642
cd4fb1b2
SM
24643 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
24644Set debugging of the DWARF DIE reader."), _("\
24645Show debugging of the DWARF DIE reader."), _("\
24646When enabled (non-zero), DIEs are dumped after they are read in.\n\
24647The value is the maximum depth to print."),
24648 NULL,
24649 NULL,
24650 &setdebuglist, &showdebuglist);
9291a0cd 24651
cd4fb1b2
SM
24652 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
24653Set debugging of the dwarf line reader."), _("\
24654Show debugging of the dwarf line reader."), _("\
24655When enabled (non-zero), line number entries are dumped as they are read in.\n\
24656A value of 1 (one) provides basic information.\n\
24657A value greater than 1 provides more verbose information."),
24658 NULL,
24659 NULL,
24660 &setdebuglist, &showdebuglist);
437afbb8 24661
cd4fb1b2
SM
24662 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
24663Set cross-checking of \"physname\" code against demangler."), _("\
24664Show cross-checking of \"physname\" code against demangler."), _("\
24665When enabled, GDB's internal \"physname\" code is checked against\n\
24666the demangler."),
24667 NULL, show_check_physname,
24668 &setdebuglist, &showdebuglist);
900e11f9 24669
e615022a
DE
24670 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24671 no_class, &use_deprecated_index_sections, _("\
24672Set whether to use deprecated gdb_index sections."), _("\
24673Show whether to use deprecated gdb_index sections."), _("\
24674When enabled, deprecated .gdb_index sections are used anyway.\n\
24675Normally they are ignored either because of a missing feature or\n\
24676performance issue.\n\
24677Warning: This option must be enabled before gdb reads the file."),
24678 NULL,
24679 NULL,
24680 &setlist, &showlist);
24681
f1e6e072
TT
24682 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24683 &dwarf2_locexpr_funcs);
24684 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24685 &dwarf2_loclist_funcs);
24686
24687 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24688 &dwarf2_block_frame_base_locexpr_funcs);
24689 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24690 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
24691
24692#if GDB_SELF_TEST
24693 selftests::register_test ("dw2_expand_symtabs_matching",
24694 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
24695 selftests::register_test ("dwarf2_find_containing_comp_unit",
24696 selftests::find_containing_comp_unit::run_test);
c62446b1 24697#endif
6502dd73 24698}
This page took 6.010383 seconds and 4 git commands to generate.