Introduce dwarf2/public.h
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
3666a048 3 Copyright (C) 1994-2021 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"
c2a62a3d 43#include "dwarf2/sect-names.h"
2b2558bf 44#include "dwarf2/stringify.h"
70182375 45#include "dwarf2/public.h"
4de283e4
TT
46#include "bfd.h"
47#include "elf-bfd.h"
48#include "symtab.h"
49#include "gdbtypes.h"
50#include "objfiles.h"
d55e5aa6 51#include "dwarf2.h"
4de283e4
TT
52#include "buildsym.h"
53#include "demangle.h"
54#include "gdb-demangle.h"
4de283e4 55#include "filenames.h" /* for DOSish file names */
4de283e4
TT
56#include "language.h"
57#include "complaints.h"
82ca8957
TT
58#include "dwarf2/expr.h"
59#include "dwarf2/loc.h"
4de283e4
TT
60#include "cp-support.h"
61#include "hashtab.h"
62#include "command.h"
d55e5aa6 63#include "gdbcmd.h"
4de283e4
TT
64#include "block.h"
65#include "addrmap.h"
66#include "typeprint.h"
67#include "psympriv.h"
4de283e4 68#include "c-lang.h"
d55e5aa6 69#include "go-lang.h"
4de283e4
TT
70#include "valprint.h"
71#include "gdbcore.h" /* for gnutarget */
72#include "gdb/gdb-index.h"
4de283e4
TT
73#include "gdb_bfd.h"
74#include "f-lang.h"
75#include "source.h"
4de283e4 76#include "build-id.h"
d55e5aa6 77#include "namespace.h"
268a13a5
TT
78#include "gdbsupport/function-view.h"
79#include "gdbsupport/gdb_optional.h"
80#include "gdbsupport/underlying.h"
268a13a5 81#include "gdbsupport/hash_enum.h"
4de283e4 82#include "filename-seen-cache.h"
b32b108a 83#include "producer.h"
4de283e4 84#include <fcntl.h>
4de283e4 85#include <algorithm>
4de283e4 86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
c9317f21 88#include "rust-lang.h"
268a13a5 89#include "gdbsupport/pathstuff.h"
edd45eb0 90#include "count-one-bits.h"
437afbb8 91
73be47f5
DE
92/* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
b4f54984
DE
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95static unsigned int dwarf_read_debug = 0;
45cfd468 96
6f738b01
SM
97/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99#define dwarf_read_debug_printf(fmt, ...) \
74b773fc
SM
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
6f738b01
SM
102
103/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
6f738b01 108
d97bc12b 109/* When non-zero, dump DIEs after they are read in. */
b4f54984 110static unsigned int dwarf_die_debug = 0;
d97bc12b 111
27e0867f 112/* When non-zero, dump line number entries as they are read in. */
8fdd972c 113unsigned int dwarf_line_debug = 0;
27e0867f 114
491144b5
CB
115/* When true, cross-check physname against demangler. */
116static bool check_physname = false;
900e11f9 117
491144b5
CB
118/* When true, do not reject deprecated .gdb_index sections. */
119static bool use_deprecated_index_sections = false;
481860b3 120
17ee85fc
TT
121/* This is used to store the data that is always per objfile. */
122static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124/* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 134
f1e6e072
TT
135/* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137static int dwarf2_locexpr_index;
138static int dwarf2_loclist_index;
139static int dwarf2_locexpr_block_index;
140static int dwarf2_loclist_block_index;
141
41144253 142/* Size of .debug_loclists section header for 32-bit DWARF format. */
143#define LOCLIST_HEADER_SIZE32 12
144
145/* Size of .debug_loclists section header for 64-bit DWARF format. */
146#define LOCLIST_HEADER_SIZE64 20
147
d0ce17d8
CT
148/* Size of .debug_rnglists section header for 32-bit DWARF format. */
149#define RNGLIST_HEADER_SIZE32 12
150
151/* Size of .debug_rnglists section header for 64-bit DWARF format. */
152#define RNGLIST_HEADER_SIZE64 20
153
3f563c84
PA
154/* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167struct name_component
168{
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177};
178
44ed8f3e
PA
179/* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182struct mapped_index_base
183{
22ca247e
TT
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
44ed8f3e
PA
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
fcf23d5b 210 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
3b00ef10 217 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
220
221 /* Prevent deleting/destroying via a base class pointer. */
222protected:
223 ~mapped_index_base() = default;
224};
225
9291a0cd
TT
226/* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
fc898b42 228struct mapped_index final : public mapped_index_base
9291a0cd 229{
f00a2de2
PA
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
559a7a62 237 /* Index data format version. */
3063847f 238 int version = 0;
559a7a62 239
f00a2de2
PA
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
b11b1f88 242
3876f04e 243 /* The symbol table, implemented as a hash table. */
f00a2de2 244 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 245
9291a0cd 246 /* A pointer to the constant pool. */
3063847f 247 const char *constant_pool = nullptr;
3f563c84 248
44ed8f3e
PA
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
9ab08412 252 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 253 }
5c58de74 254
3f563c84
PA
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
fcf23d5b
SM
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 260
44ed8f3e
PA
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
9291a0cd
TT
263};
264
927aa2e7
JK
265/* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
fc898b42 267struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
268{
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
fcf23d5b
SM
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
fcf23d5b
SM
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
927aa2e7
JK
312};
313
cd4fb1b2 314/* See dwarf2read.h. */
ed2dc618 315
cd4fb1b2 316dwarf2_per_objfile *
ed2dc618
SM
317get_dwarf2_per_objfile (struct objfile *objfile)
318{
5bfd760d 319 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 320}
c906108c 321
251d32d9 322/* Default names of the debugging sections. */
c906108c 323
233a11ab
CS
324/* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
9938d15a 327const struct dwarf2_debug_sections dwarf2_elf_names =
9cdd5dbd 328{
251d32d9
TG
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
43988095 333 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 334 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 335 { ".debug_macro", ".zdebug_macro" },
251d32d9 336 { ".debug_str", ".zdebug_str" },
18a8505e 337 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 338 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 339 { ".debug_ranges", ".zdebug_ranges" },
43988095 340 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 341 { ".debug_types", ".zdebug_types" },
3019eac3 342 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
24d3216f 345 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 348 23
251d32d9 349};
c906108c 350
80626a55 351/* List of DWO/DWP sections. */
3019eac3 352
80626a55 353static const struct dwop_section_names
3019eac3
DE
354{
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
43988095 359 struct dwarf2_section_names loclists_dwo;
09262596
DE
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
d0ce17d8 362 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
80626a55
DE
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
3019eac3 368}
80626a55 369dwop_section_names =
3019eac3
DE
370{
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
384};
385
c906108c
SS
386/* local data types */
387
d0ce17d8
CT
388/* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390struct loclists_rnglists_header
41144253 391{
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410};
411
3da10d80
KS
412/* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414struct delayed_method_info
415{
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430};
431
e7c27a73
DJ
432/* Internal state when decoding a particular compilation unit. */
433struct dwarf2_cu
434{
9e021579
SM
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
c24bdb02
KS
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
293e7e51
SM
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
d00adf39 466 /* The header of the compilation unit. */
fcd3b13d 467 struct comp_unit_head header {};
e142c38c 468
d00adf39 469 /* Base address of this compilation unit. */
2b24b6e4 470 gdb::optional<CORE_ADDR> base_address;
d00adf39 471
e142c38c 472 /* The language we are debugging. */
fcd3b13d
SM
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
e142c38c 475
fcd3b13d 476 const char *producer = nullptr;
b0f35d58 477
c24bdb02 478private:
804d2729
TT
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
c24bdb02 481 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 482
c24bdb02 483public:
e142c38c
DJ
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
fcd3b13d 493 struct pending **list_in_scope = nullptr;
e142c38c 494
b64f50a1
JK
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 497 htab_t partial_dies = nullptr;
72bf9492
DJ
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
fcd3b13d 501 auto_obstack comp_unit_obstack;
72bf9492 502
69d751e3 503 /* Backlink to our per_cu entry. */
ae038cb0
DJ
504 struct dwarf2_per_cu_data *per_cu;
505
9e021579 506 /* The dwarf2_per_objfile that owns this. */
976ca316 507 dwarf2_per_objfile *per_objfile;
9e021579 508
ae038cb0 509 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 510 int last_used = 0;
ae038cb0 511
b64f50a1
JK
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
fcd3b13d 514 htab_t die_hash = nullptr;
10b3939b
DJ
515
516 /* Full DIEs if read in. */
fcd3b13d 517 struct die_info *dies = nullptr;
10b3939b
DJ
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
fcd3b13d 522 htab_t dependencies = nullptr;
10b3939b 523
cb1df416 524 /* Header data from the line table, during full symbol processing. */
fcd3b13d 525 struct line_header *line_header = nullptr;
4c8aa72d 526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
fcd3b13d 531 die_info *line_header_die_owner = nullptr;
cb1df416 532
3da10d80
KS
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
c89b44cd 535 std::vector<delayed_method_info> method_list;
3da10d80 536
96408a79 537 /* To be copied to symtab->call_site_htab. */
fcd3b13d 538 htab_t call_site_htab = nullptr;
96408a79 539
034e5797
DE
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
18a8505e 544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
fcd3b13d 549 struct dwo_unit *dwo_unit = nullptr;
3019eac3 550
18a8505e 551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 552 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 553 gdb::optional<ULONGEST> addr_base;
3019eac3 554
2b0c7f41
SM
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
2e3cf129 590
41144253 591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
c9317f21
TT
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
18a8505e
AT
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
ae038cb0 608 /* Mark used when releasing cached dies. */
9068261f 609 bool mark : 1;
ae038cb0 610
8be455d7
JK
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 615 bool has_loclist : 1;
ba919b58 616
9068261f 617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
9068261f
AB
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 624 bool producer_is_icc : 1;
9068261f 625 bool producer_is_icc_lt_14 : 1;
c258c396 626 bool producer_is_codewarrior : 1;
4d4ec4e5 627
9068261f 628 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
9068261f 632 bool processing_has_namespace_info : 1;
d590ff25
YQ
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
e7c27a73
DJ
653};
654
094b34ac
DE
655/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658struct stmt_list_hash
659{
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 664 sect_offset line_sect_off;
094b34ac
DE
665};
666
5989a64e 667/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
f4dc4d17
DE
671
672struct type_unit_group
673{
0186c6a7 674 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
197400e8 677 a "per_cu" handle on the symtab. */
094b34ac
DE
678 struct dwarf2_per_cu_data per_cu;
679
0186c6a7
DE
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
a8b3b8e9 683 std::vector<signatured_type *> *tus;
f4dc4d17 684
094b34ac
DE
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
f4dc4d17
DE
687};
688
73869dc2 689/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
690
691struct dwo_sections
692{
693 struct dwarf2_section_info abbrev;
3019eac3
DE
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
43988095 696 struct dwarf2_section_info loclists;
09262596
DE
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
d0ce17d8 699 struct dwarf2_section_info rnglists;
3019eac3
DE
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
80626a55
DE
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
fd5866f6 704 std::vector<dwarf2_section_info> types;
3019eac3
DE
705};
706
c88ee1f0 707/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
708
709struct dwo_unit
710{
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 720 struct dwarf2_section_info *section;
3019eac3 721
9c541725
PA
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
3019eac3
DE
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728};
729
73869dc2
DE
730/* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734enum dwp_v2_section_ids
735{
736 DW_SECT_MIN = 1
737};
738
80626a55 739/* Data for one DWO file.
57d63ce2
DE
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
3019eac3
DE
749
750struct dwo_file
751{
51ac9db5
SM
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
18a8505e 755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
51ac9db5 759 const char *dwo_name = nullptr;
0ac5b59e
DE
760
761 /* The DW_AT_comp_dir attribute. */
51ac9db5 762 const char *comp_dir = nullptr;
3019eac3 763
80626a55
DE
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 766 gdb_bfd_ref_ptr dbfd;
3019eac3 767
73869dc2 768 /* The sections that make up this DWO file.
d2854d8d 769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 770 sections (for lack of a better name). */
51ac9db5 771 struct dwo_sections sections {};
3019eac3 772
33c5cd75
DB
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 777 htab_up cus;
3019eac3
DE
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
b0b6a987 781 htab_up tus;
3019eac3
DE
782};
783
80626a55
DE
784/* These sections are what may appear in a DWP file. */
785
786struct dwp_sections
787{
d2854d8d 788 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
73869dc2 792
d2854d8d 793 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
73869dc2
DE
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
d2854d8d 805 struct dwarf2_section_info loclists;
73869dc2
DE
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
d2854d8d 808 struct dwarf2_section_info rnglists;
73869dc2
DE
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
80626a55
DE
811};
812
73869dc2
DE
813/* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 815
73869dc2 816struct virtual_v1_dwo_sections
80626a55
DE
817{
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 825 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
826 struct dwarf2_section_info info_or_types;
827};
828
d2854d8d 829/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
d2854d8d 834struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
835{
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
d2854d8d
CT
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
73869dc2
DE
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
d2854d8d
CT
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
73869dc2
DE
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864};
865
80626a55
DE
866/* Contents of DWP hash tables. */
867
868struct dwp_hash_table
869{
73869dc2 870 uint32_t version, nr_columns;
80626a55 871 uint32_t nr_units, nr_slots;
73869dc2
DE
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883#define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
d2854d8d
CT
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898#define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
73869dc2 910 } section_pool;
80626a55
DE
911};
912
913/* Data for one DWP file. */
914
915struct dwp_file
916{
400174b1
TT
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
80626a55
DE
923 /* Name of the file. */
924 const char *name;
925
73869dc2 926 /* File format version. */
400174b1 927 int version = 0;
73869dc2 928
93417882 929 /* The bfd. */
400174b1 930 gdb_bfd_ref_ptr dbfd;
80626a55
DE
931
932 /* Section info for this file. */
400174b1 933 struct dwp_sections sections {};
80626a55 934
57d63ce2 935 /* Table of CUs in the file. */
400174b1 936 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
937
938 /* Table of TUs in the file. */
400174b1 939 const struct dwp_hash_table *tus = nullptr;
80626a55 940
19ac8c2e 941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
942 htab_up loaded_cus;
943 htab_up loaded_tus;
80626a55 944
73869dc2
DE
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
400174b1
TT
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
80626a55
DE
949};
950
0963b4bd
MS
951/* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
dee91e82 954 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
955
956struct die_reader_specs
957{
a32a8923 958 /* The bfd of die_section. */
0280fdcc 959 bfd *abfd;
93311388
DE
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
80626a55 964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
965 struct dwo_file *dwo_file;
966
dee91e82 967 /* The section the die comes from.
3019eac3 968 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
d521ce57 972 const gdb_byte *buffer;
f664829e
DE
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
a2ce51a0 976
685af9cd
TT
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
93311388
DE
979};
980
c0ab21c2
TT
981/* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984class cutu_reader : public die_reader_specs
985{
986public:
987
ab432490
SM
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
c0ab21c2 990 struct abbrev_table *abbrev_table,
2e671100 991 dwarf2_cu *existing_cu,
c0ab21c2
TT
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 995 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
c0ab21c2
TT
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
1003 bool dummy_p = false;
1004
6751ebae
TT
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
c0ab21c2 1009private:
9e021579
SM
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
2e671100 1012 dwarf2_cu *existing_cu);
c0ab21c2
TT
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022};
dee91e82 1023
c906108c 1024/* When we construct a partial symbol table entry we only
0963b4bd 1025 need this much information. */
6f06d47b 1026struct partial_die_info : public allocate_on_obstack
c906108c 1027 {
7c32eebb 1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
6f06d47b
YQ
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
52356b79
YQ
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
48fbe735
YQ
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
7d00ffec
TT
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
72bf9492 1049 /* Offset of this DIE. */
6f06d47b 1050 const sect_offset sect_off;
72bf9492
DJ
1051
1052 /* DWARF-2 tag for this DIE. */
6f06d47b 1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1054
72bf9492 1055 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1056 const unsigned int has_children : 1;
1057
72bf9492
DJ
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
481860b3 1063 unsigned int may_be_inlined : 1;
72bf9492 1064
0c1b455e
TT
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
72bf9492
DJ
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
fa4028e9
JB
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
ff908ebf
AW
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
98bfdba5
PA
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
52356b79 1081 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1082 unsigned int fixup_called : 1;
1083
36586728
TT
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
7d00ffec
TT
1090 unsigned int canonical_name : 1;
1091
72bf9492 1092 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1093 sometimes a default name for unnamed DIEs. */
7d00ffec 1094 const char *raw_name = nullptr;
72bf9492 1095
abc72ce4 1096 /* The linkage name, if present. */
6f06d47b 1097 const char *linkage_name = nullptr;
abc72ce4 1098
72bf9492
DJ
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
6f06d47b 1102 const char *scope = nullptr;
72bf9492 1103
95554aad
TT
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1111 sect_offset sect_off;
6f06d47b 1112 } d {};
72bf9492
DJ
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
72bf9492 1117
93311388 1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1119 DW_AT_sibling, if any. */
48fbe735
YQ
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1122 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
6f06d47b 1127 sect_offset spec_offset {};
72bf9492
DJ
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
6f06d47b
YQ
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
7d00ffec 1163 canonical_name = 0;
6f06d47b 1164 }
c906108c
SS
1165 };
1166
c906108c
SS
1167/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170static int bits_per_byte = 8;
1171
9c6a1327
TT
1172struct variant_part_builder;
1173
1174/* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
2ddeaf8a
TT
1176
1177struct variant_field
1178{
9c6a1327
TT
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
2ddeaf8a
TT
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
9c6a1327
TT
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194};
1195
1196/* This represents a DW_TAG_variant_part. */
1197
1198struct variant_part_builder
1199{
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
2ddeaf8a
TT
1208};
1209
52059ffd
TT
1210struct nextfield
1211{
be2daae6
TT
1212 int accessibility = 0;
1213 int virtuality = 0;
9c6a1327
TT
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
be2daae6 1218 struct field field {};
52059ffd
TT
1219};
1220
1221struct fnfieldlist
1222{
be2daae6
TT
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
52059ffd
TT
1225};
1226
c906108c
SS
1227/* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230struct field_info
2de01bdb
SM
1231{
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1237 bool non_public_fields = false;
2de01bdb
SM
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
c5aa993b 1261 {
2de01bdb
SM
1262 return fields.size () + baseclasses.size ();
1263 }
1264};
c906108c 1265
ae038cb0
DJ
1266/* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1271static int dwarf_max_cache_age = 5;
920d2a44 1272static void
b4f54984
DE
1273show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
920d2a44 1275{
3e43a32a 1276 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1277 "DWARF compilation units is %s.\n"),
920d2a44
AC
1278 value);
1279}
4390d890 1280\f
c906108c
SS
1281/* local function prototypes */
1282
918dd910
JK
1283static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
891813be 1286static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
0018ea6f 1289
f1902523
JK
1290static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
3e225074 1292 struct die_info *type_unit_die);
f1902523 1293
976ca316 1294static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1295
72bf9492
DJ
1296static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1298 int, struct dwarf2_cu *);
c906108c 1299
72bf9492
DJ
1300static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
63d06c5c 1302
72bf9492
DJ
1303static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1305 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1306
5d7cb8df 1307static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1308 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1309 struct dwarf2_cu *cu);
1310
72bf9492
DJ
1311static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
91c24f0a 1313
bc30ff58
JB
1314static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1316 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1317
d521ce57 1318static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1319
dee91e82 1320static struct partial_die_info *load_partial_dies
d521ce57 1321 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1322
fb816e8b
TV
1323/* A pair of partial_die_info and compilation unit. */
1324struct cu_partial_die_info
1325{
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
122cf0f2
AB
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
405feb71 1334 { /* Nothing. */ }
122cf0f2
AB
1335
1336private:
1337 cu_partial_die_info () = delete;
fb816e8b
TV
1338};
1339
122cf0f2
AB
1340static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
72bf9492 1342
d521ce57 1343static const gdb_byte *read_attribute (const struct die_reader_specs *,
4444f407
TT
1344 struct attribute *,
1345 const struct attr_abbrev *,
7a5f294d 1346 const gdb_byte *);
18a8505e
AT
1347
1348static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1349 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1350
1351static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1352
976ca316
SM
1353static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
f4dc4d17 1355
ed2dc618 1356static const char *read_indirect_string
976ca316 1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1358 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1359
ed2dc618 1360static const char *read_indirect_string_at_offset
976ca316 1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1362
d521ce57
TT
1363static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
3019eac3
DE
1365 unsigned int *);
1366
18a8505e
AT
1367static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
3019eac3 1372
e142c38c 1373static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1374
e142c38c
DJ
1375static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
c906108c 1377
7d45c7c3 1378static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1379 struct dwarf2_cu *cu);
7d45c7c3 1380
a084a2a6
AT
1381static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
05cf31d1 1383static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1384 struct dwarf2_cu *cu);
05cf31d1 1385
e142c38c 1386static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1387
e142c38c 1388static struct die_info *die_specification (struct die_info *die,
f2f0e013 1389 struct dwarf2_cu **);
63d06c5c 1390
9c541725 1391static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1392 struct dwarf2_cu *cu);
debd256d 1393
f3f5162e 1394static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1395 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1396 CORE_ADDR, int decode_mapping);
c906108c 1397
804d2729
TT
1398static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
c906108c 1400
a14ed312 1401static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1402 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1403
ff39bb5e 1404static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1405 struct dwarf2_cu *);
c906108c 1406
ff39bb5e 1407static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
12df843f 1411 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1412 const gdb_byte **bytes,
98bfdba5 1413 struct dwarf2_locexpr_baton **baton);
2df3850c 1414
57567375
TT
1415static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
e7c27a73 1418static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1419
b4ba55a1
JB
1420static int need_gnat_info (struct dwarf2_cu *);
1421
3e43a32a
MS
1422static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
b4ba55a1
JB
1424
1425static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
e7c27a73
DJ
1428static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
c906108c 1430
ff39bb5e 1431static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1432 struct dwarf2_cu *);
c906108c 1433
f792889a 1434static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1435
673bfd45
DE
1436static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
0d5cff50 1438static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1439
6e70227d 1440static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
63d06c5c 1443
e7c27a73 1444static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1445
348e048f
DE
1446static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
e7c27a73 1448static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1449
e7c27a73 1450static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1451
96408a79
SA
1452static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
71a3c369
TT
1454static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
41144253 1456/* Return the .debug_loclists section to use for cu. */
1457static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
d0ce17d8
CT
1459/* Return the .debug_rnglists section to use for cu. */
1460static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
3a2b436a 1463/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1464 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1465enum pc_bounds_kind
1466{
e385593e 1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1468 PC_BOUNDS_NOT_PRESENT,
1469
e385593e
JK
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
3a2b436a
JK
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479};
1480
1481static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
891813be 1484 dwarf2_psymtab *);
c906108c 1485
fae299cd
DC
1486static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
801e3a5b 1490static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1491 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1492
a14ed312 1493static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1494 struct dwarf2_cu *);
c906108c 1495
a14ed312 1496static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1497 struct type *, struct dwarf2_cu *);
c906108c 1498
a14ed312 1499static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1500 struct die_info *, struct type *,
e7c27a73 1501 struct dwarf2_cu *);
c906108c 1502
a14ed312 1503static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1504 struct type *,
1505 struct dwarf2_cu *);
c906108c 1506
134d01f1 1507static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1508
e7c27a73 1509static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1510
e7c27a73 1511static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1512
5d7cb8df
JK
1513static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
804d2729 1515static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1516
27aa8d6a
SW
1517static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
74921315
KS
1519static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
f55ee35c
JK
1521static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
38d518c9 1524static const char *namespace_name (struct die_info *die,
e142c38c 1525 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1526
134d01f1 1527static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1528
7d79de9a
TT
1529static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
c906108c 1531
6e70227d 1532static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1533 struct dwarf2_cu *);
1534
bf6af496 1535static struct die_info *read_die_and_siblings_1
d521ce57 1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1537 struct die_info *);
639d11d3 1538
dee91e82 1539static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
639d11d3
DC
1542 struct die_info *parent);
1543
d521ce57
TT
1544static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
3e225074 1546 int);
3019eac3 1547
d521ce57 1548static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1549 struct die_info **, const gdb_byte *);
93311388 1550
e7c27a73 1551static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1552
15d034d0 1553static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1554 struct objfile *);
71c25dea 1555
15d034d0 1556static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1557
15d034d0 1558static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
ca69b9e6
DE
1562static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
e142c38c 1565static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1566 struct dwarf2_cu **);
9219021c 1567
d97bc12b
DE
1568static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570static void dump_die_for_error (struct die_info *);
1571
1572static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
c906108c 1574
d97bc12b 1575/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1576
51545339 1577static void store_in_ref_table (struct die_info *,
10b3939b 1578 struct dwarf2_cu *);
c906108c 1579
348e048f 1580static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1581 const struct attribute *,
348e048f
DE
1582 struct dwarf2_cu **);
1583
10b3939b 1584static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1585 const struct attribute *,
f2f0e013 1586 struct dwarf2_cu **);
c906108c 1587
348e048f 1588static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1589 const struct attribute *,
348e048f
DE
1590 struct dwarf2_cu **);
1591
ac9ec31b
DE
1592static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1596 const struct attribute *,
ac9ec31b
DE
1597 struct dwarf2_cu *);
1598
ab432490
SM
1599static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
348e048f 1601
ab432490
SM
1602static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
348e048f 1604
63e43d3a
PMR
1605static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1607 struct dynamic_prop *prop, struct type *type);
63e43d3a 1608
c906108c
SS
1609/* memory allocation interface */
1610
7b5a2f43 1611static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1612
b60c80d6 1613static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1614
43f3e411 1615static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1616
8cf6f0b1
TT
1617static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
ff39bb5e 1619 const struct attribute *attr);
8cf6f0b1 1620
ff39bb5e 1621static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1622 struct symbol *sym,
f1e6e072
TT
1623 struct dwarf2_cu *cu,
1624 int is_block);
4c2df51b 1625
d521ce57
TT
1626static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
7c32eebb 1628 const struct abbrev_info *abbrev);
4bb7a0a7 1629
72bf9492
DJ
1630static hashval_t partial_die_hash (const void *item);
1631
1632static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
ae038cb0 1634static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1635 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1636 dwarf2_per_objfile *per_objfile);
ae038cb0 1637
9816fde3 1638static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
93311388 1641
f792889a 1642static struct type *set_die_type (struct die_info *, struct type *,
57567375 1643 struct dwarf2_cu *, bool = false);
1c379e20 1644
976ca316 1645static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1646
976ca316 1647static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1648
ab432490
SM
1649static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
4a636814 1651 dwarf2_cu *existing_cu,
ab432490
SM
1652 bool skip_partial,
1653 enum language pretend_language);
10b3939b 1654
8fc0b21d 1655static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1656 enum language pretend_language);
10b3939b 1657
8fc0b21d 1658static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1659 enum language pretend_language);
f4dc4d17 1660
10b3939b
DJ
1661static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
ae038cb0
DJ
1664static void dwarf2_mark (struct dwarf2_cu *);
1665
b64f50a1 1666static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
673bfd45 1669
f792889a 1670static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1671
120ce1b5
SM
1672static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
95554aad
TT
1674 enum language pretend_language);
1675
976ca316 1676static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1677
b303c6f6
AB
1678/* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683class dwarf2_queue_guard
1684{
1685public:
39856def
TT
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
08ac5771
SM
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
39856def 1692 }
b303c6f6
AB
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
08ac5771
SM
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
39856def 1701 }
b303c6f6 1702
39856def 1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1704
39856def
TT
1705private:
1706 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1707};
1708
39856def
TT
1709dwarf2_queue_item::~dwarf2_queue_item ()
1710{
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
7188ed02 1715 per_objfile->remove_cu (per_cu);
39856def
TT
1716 per_cu->queued = 0;
1717 }
1718}
1719
d721ba37
PA
1720/* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723struct file_and_directory
1724{
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737};
1738
1739static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
9291a0cd 1741
298e9637 1742static htab_up allocate_signatured_type_table ();
1fd400ff 1743
298e9637 1744static htab_up allocate_dwo_unit_table ();
3019eac3 1745
57d63ce2 1746static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1749
976ca316 1750static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1751
3019eac3 1752static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
3019eac3
DE
1755
1756static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1758
1b555f17 1759static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1760
263db9a1
TT
1761/* A unique pointer to a dwo_file. */
1762
51ac9db5 1763typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1764
976ca316 1765static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1766
1b80a9fa 1767static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1768
1769static void free_line_header_voidp (void *arg);
4390d890
DE
1770\f
1771/* Various complaints about symbol reading that don't abort the process. */
1772
4390d890
DE
1773static void
1774dwarf2_debug_line_missing_file_complaint (void)
1775{
b98664d3 1776 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1777}
1778
1779static void
1780dwarf2_debug_line_missing_end_sequence_complaint (void)
1781{
b98664d3 1782 complaint (_(".debug_line section has line "
4390d890
DE
1783 "program sequence without an end"));
1784}
1785
1786static void
1787dwarf2_complex_location_expr_complaint (void)
1788{
b98664d3 1789 complaint (_("location expression too complex"));
4390d890
DE
1790}
1791
1792static void
1793dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795{
b98664d3 1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1797 arg1, arg2, arg3);
1798}
1799
4390d890
DE
1800static void
1801dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802{
b98664d3 1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1804 arg1, arg2);
1805}
527f3840
JK
1806
1807/* Hash function for line_header_hash. */
1808
1809static hashval_t
1810line_header_hash (const struct line_header *ofs)
1811{
9c541725 1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1813}
1814
1815/* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817static hashval_t
1818line_header_hash_voidp (const void *item)
1819{
9a3c8263 1820 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1821
1822 return line_header_hash (ofs);
1823}
1824
1825/* Equality function for line_header_hash. */
1826
1827static int
1828line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829{
9a3c8263
SM
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1832
9c541725 1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835}
1836
4390d890 1837\f
9291a0cd 1838
330cdd98
PA
1839/* See declaration. */
1840
5989a64e
SM
1841dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
c3699833
SM
1843 : obfd (obfd),
1844 can_copy (can_copy_)
330cdd98
PA
1845{
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
330cdd98
PA
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851}
1852
5989a64e 1853dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1854{
b76e467d 1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1856 per_cu->imported_symtabs_free ();
fc8e7e75 1857
b2bdb8cf 1858 for (signatured_type *sig_type : all_type_units)
ae640021 1859 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1860
5989a64e 1861 /* Everything else should be on this->obstack. */
330cdd98
PA
1862}
1863
7188ed02 1864/* See read.h. */
330cdd98
PA
1865
1866void
7188ed02 1867dwarf2_per_objfile::remove_all_cus ()
330cdd98 1868{
08ac5771
SM
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
7188ed02
SM
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
330cdd98 1873
7188ed02 1874 m_dwarf2_cus.clear ();
330cdd98
PA
1875}
1876
11ed8cad
TT
1877/* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880class free_cached_comp_units
1881{
1882public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
7188ed02 1891 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899};
1900
af758d11
SM
1901/* See read.h. */
1902
1903bool
1904dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905{
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909}
1910
1911/* See read.h. */
1912
1913compunit_symtab *
1914dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915{
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919}
1920
1921/* See read.h. */
1922
1923void
1924dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926{
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931}
1932
c906108c 1933/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
c906108c
SS
1939
1940int
251d32d9 1941dwarf2_has_info (struct objfile *objfile,
dda83cd7 1942 const struct dwarf2_debug_sections *names,
4b610737 1943 bool can_copy)
c906108c 1944{
97cbe998
SDJ
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
976ca316 1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1949
976ca316 1950 if (per_objfile == NULL)
5989a64e 1951 {
17ee85fc
TT
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1955 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1956 from some other reader. */
1957 if (!objfile_has_partial_symbols (objfile)
1958 && !gdb_bfd_requires_relocations (objfile->obfd))
1959 {
1960 /* See if one has been created for this BFD yet. */
1961 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1962
1963 if (per_bfd == nullptr)
1964 {
1965 /* No, create it now. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1968 }
1969 }
1970 else
1971 {
1972 /* No sharing possible, create one specifically for this objfile. */
1973 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1974 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1975 }
5989a64e 1976
976ca316 1977 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1978 }
5bfd760d 1979
976ca316
SM
1980 return (!per_objfile->per_bfd->info.is_virtual
1981 && per_objfile->per_bfd->info.s.section != NULL
1982 && !per_objfile->per_bfd->abbrev.is_virtual
1983 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1984}
1985
330cdd98 1986/* See declaration. */
c906108c 1987
330cdd98 1988void
5989a64e
SM
1989dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1990 const dwarf2_debug_sections &names)
c906108c 1991{
fd361982 1992 flagword aflag = bfd_section_flags (sectp);
251d32d9 1993
dc7650b8
JK
1994 if ((aflag & SEC_HAS_CONTENTS) == 0)
1995 {
1996 }
950b7495
KS
1997 else if (elf_section_data (sectp)->this_hdr.sh_size
1998 > bfd_get_file_size (abfd))
1999 {
2000 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2001 warning (_("Discarding section %s which has a section size (%s"
2002 ") larger than the file size [in module %s]"),
2003 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2004 bfd_get_filename (abfd));
2005 }
fbedd546 2006 else if (names.info.matches (sectp->name))
c906108c 2007 {
330cdd98 2008 this->info.s.section = sectp;
fd361982 2009 this->info.size = bfd_section_size (sectp);
c906108c 2010 }
fbedd546 2011 else if (names.abbrev.matches (sectp->name))
c906108c 2012 {
330cdd98 2013 this->abbrev.s.section = sectp;
fd361982 2014 this->abbrev.size = bfd_section_size (sectp);
c906108c 2015 }
fbedd546 2016 else if (names.line.matches (sectp->name))
c906108c 2017 {
330cdd98 2018 this->line.s.section = sectp;
fd361982 2019 this->line.size = bfd_section_size (sectp);
c906108c 2020 }
fbedd546 2021 else if (names.loc.matches (sectp->name))
c906108c 2022 {
330cdd98 2023 this->loc.s.section = sectp;
fd361982 2024 this->loc.size = bfd_section_size (sectp);
c906108c 2025 }
fbedd546 2026 else if (names.loclists.matches (sectp->name))
43988095 2027 {
330cdd98 2028 this->loclists.s.section = sectp;
fd361982 2029 this->loclists.size = bfd_section_size (sectp);
43988095 2030 }
fbedd546 2031 else if (names.macinfo.matches (sectp->name))
c906108c 2032 {
330cdd98 2033 this->macinfo.s.section = sectp;
fd361982 2034 this->macinfo.size = bfd_section_size (sectp);
c906108c 2035 }
fbedd546 2036 else if (names.macro.matches (sectp->name))
cf2c3c16 2037 {
330cdd98 2038 this->macro.s.section = sectp;
fd361982 2039 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2040 }
fbedd546 2041 else if (names.str.matches (sectp->name))
c906108c 2042 {
330cdd98 2043 this->str.s.section = sectp;
fd361982 2044 this->str.size = bfd_section_size (sectp);
c906108c 2045 }
fbedd546 2046 else if (names.str_offsets.matches (sectp->name))
18a8505e
AT
2047 {
2048 this->str_offsets.s.section = sectp;
2049 this->str_offsets.size = bfd_section_size (sectp);
2050 }
fbedd546 2051 else if (names.line_str.matches (sectp->name))
43988095 2052 {
330cdd98 2053 this->line_str.s.section = sectp;
fd361982 2054 this->line_str.size = bfd_section_size (sectp);
43988095 2055 }
fbedd546 2056 else if (names.addr.matches (sectp->name))
3019eac3 2057 {
330cdd98 2058 this->addr.s.section = sectp;
fd361982 2059 this->addr.size = bfd_section_size (sectp);
3019eac3 2060 }
fbedd546 2061 else if (names.frame.matches (sectp->name))
b6af0555 2062 {
330cdd98 2063 this->frame.s.section = sectp;
fd361982 2064 this->frame.size = bfd_section_size (sectp);
b6af0555 2065 }
fbedd546 2066 else if (names.eh_frame.matches (sectp->name))
b6af0555 2067 {
330cdd98 2068 this->eh_frame.s.section = sectp;
fd361982 2069 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2070 }
fbedd546 2071 else if (names.ranges.matches (sectp->name))
af34e669 2072 {
330cdd98 2073 this->ranges.s.section = sectp;
fd361982 2074 this->ranges.size = bfd_section_size (sectp);
af34e669 2075 }
fbedd546 2076 else if (names.rnglists.matches (sectp->name))
43988095 2077 {
330cdd98 2078 this->rnglists.s.section = sectp;
fd361982 2079 this->rnglists.size = bfd_section_size (sectp);
43988095 2080 }
fbedd546 2081 else if (names.types.matches (sectp->name))
348e048f 2082 {
8b70b953
TT
2083 struct dwarf2_section_info type_section;
2084
2085 memset (&type_section, 0, sizeof (type_section));
049412e3 2086 type_section.s.section = sectp;
fd361982 2087 type_section.size = bfd_section_size (sectp);
8b70b953 2088
fd5866f6 2089 this->types.push_back (type_section);
348e048f 2090 }
fbedd546 2091 else if (names.gdb_index.matches (sectp->name))
9291a0cd 2092 {
330cdd98 2093 this->gdb_index.s.section = sectp;
fd361982 2094 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2095 }
fbedd546 2096 else if (names.debug_names.matches (sectp->name))
927aa2e7
JK
2097 {
2098 this->debug_names.s.section = sectp;
fd361982 2099 this->debug_names.size = bfd_section_size (sectp);
927aa2e7 2100 }
fbedd546 2101 else if (names.debug_aranges.matches (sectp->name))
927aa2e7
JK
2102 {
2103 this->debug_aranges.s.section = sectp;
fd361982 2104 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2105 }
dce234bc 2106
fd361982
AM
2107 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2108 && bfd_section_vma (sectp) == 0)
330cdd98 2109 this->has_section_at_zero = true;
c906108c
SS
2110}
2111
dce234bc 2112/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2113 SECTION_NAME. */
af34e669 2114
dce234bc 2115void
3017a003 2116dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2117 enum dwarf2_section_enum sect,
2118 asection **sectp, const gdb_byte **bufp,
2119 bfd_size_type *sizep)
dce234bc 2120{
976ca316 2121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2122 struct dwarf2_section_info *info;
a3b2a86b
TT
2123
2124 /* We may see an objfile without any DWARF, in which case we just
2125 return nothing. */
976ca316 2126 if (per_objfile == NULL)
a3b2a86b
TT
2127 {
2128 *sectp = NULL;
2129 *bufp = NULL;
2130 *sizep = 0;
2131 return;
2132 }
3017a003
TG
2133 switch (sect)
2134 {
2135 case DWARF2_DEBUG_FRAME:
976ca316 2136 info = &per_objfile->per_bfd->frame;
3017a003
TG
2137 break;
2138 case DWARF2_EH_FRAME:
976ca316 2139 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2140 break;
2141 default:
2142 gdb_assert_not_reached ("unexpected section");
2143 }
dce234bc 2144
96b79293 2145 info->read (objfile);
dce234bc 2146
96b79293 2147 *sectp = info->get_bfd_section ();
dce234bc
PP
2148 *bufp = info->buffer;
2149 *sizep = info->size;
2150}
2151
9291a0cd 2152\f
7b9f3c50
DE
2153/* DWARF quick_symbols_functions support. */
2154
2155/* TUs can share .debug_line entries, and there can be a lot more TUs than
2156 unique line tables, so we maintain a separate table of all .debug_line
2157 derived entries to support the sharing.
2158 All the quick functions need is the list of file names. We discard the
2159 line_header when we're done and don't need to record it here. */
2160struct quick_file_names
2161{
094b34ac
DE
2162 /* The data used to construct the hash key. */
2163 struct stmt_list_hash hash;
7b9f3c50
DE
2164
2165 /* The number of entries in file_names, real_names. */
2166 unsigned int num_file_names;
2167
2168 /* The file names from the line table, after being run through
2169 file_full_name. */
2170 const char **file_names;
2171
2172 /* The file names from the line table after being run through
2173 gdb_realpath. These are computed lazily. */
2174 const char **real_names;
2175};
2176
2177/* When using the index (and thus not using psymtabs), each CU has an
2178 object of this type. This is used to hold information needed by
2179 the various "quick" methods. */
2180struct dwarf2_per_cu_quick_data
2181{
2182 /* The file table. This can be NULL if there was no file table
2183 or it's currently not read in.
5989a64e 2184 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2185 struct quick_file_names *file_names;
2186
7b9f3c50
DE
2187 /* A temporary mark bit used when iterating over all CUs in
2188 expand_symtabs_matching. */
2189 unsigned int mark : 1;
2190
2191 /* True if we've tried to read the file table and found there isn't one.
2192 There will be no point in trying to read it again next time. */
2193 unsigned int no_file_data : 1;
2194};
2195
094b34ac
DE
2196/* Utility hash function for a stmt_list_hash. */
2197
2198static hashval_t
2199hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2200{
2201 hashval_t v = 0;
2202
2203 if (stmt_list_hash->dwo_unit != NULL)
2204 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2205 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2206 return v;
2207}
2208
2209/* Utility equality function for a stmt_list_hash. */
2210
2211static int
2212eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2213 const struct stmt_list_hash *rhs)
2214{
2215 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2216 return 0;
2217 if (lhs->dwo_unit != NULL
2218 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2219 return 0;
2220
9c541725 2221 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2222}
2223
7b9f3c50
DE
2224/* Hash function for a quick_file_names. */
2225
2226static hashval_t
2227hash_file_name_entry (const void *e)
2228{
9a3c8263
SM
2229 const struct quick_file_names *file_data
2230 = (const struct quick_file_names *) e;
7b9f3c50 2231
094b34ac 2232 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2233}
2234
2235/* Equality function for a quick_file_names. */
2236
2237static int
2238eq_file_name_entry (const void *a, const void *b)
2239{
9a3c8263
SM
2240 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2241 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2242
094b34ac 2243 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2244}
2245
2246/* Delete function for a quick_file_names. */
2247
2248static void
2249delete_file_name_entry (void *e)
2250{
9a3c8263 2251 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2252 int i;
2253
2254 for (i = 0; i < file_data->num_file_names; ++i)
2255 {
2256 xfree ((void*) file_data->file_names[i]);
2257 if (file_data->real_names)
2258 xfree ((void*) file_data->real_names[i]);
2259 }
2260
45940949
TT
2261 /* The space for the struct itself lives on the obstack, so we don't
2262 free it here. */
7b9f3c50
DE
2263}
2264
2265/* Create a quick_file_names hash table. */
2266
5895093f 2267static htab_up
7b9f3c50
DE
2268create_quick_file_names_table (unsigned int nr_initial_entries)
2269{
5895093f
TT
2270 return htab_up (htab_create_alloc (nr_initial_entries,
2271 hash_file_name_entry, eq_file_name_entry,
2272 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2273}
9291a0cd 2274
ab432490
SM
2275/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2276 function is unrelated to symtabs, symtab would have to be created afterwards.
2277 You should call age_cached_comp_units after processing the CU. */
918dd910 2278
1b555f17 2279static dwarf2_cu *
ab432490
SM
2280load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2281 bool skip_partial)
918dd910 2282{
3019eac3 2283 if (per_cu->is_debug_types)
ab432490 2284 load_full_type_unit (per_cu, per_objfile);
918dd910 2285 else
4a636814
SM
2286 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2287 skip_partial, language_minimal);
918dd910 2288
7188ed02
SM
2289 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2290 if (cu == nullptr)
1b555f17 2291 return nullptr; /* Dummy CU. */
2dc860c0 2292
7188ed02 2293 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2294
7188ed02 2295 return cu;
918dd910
JK
2296}
2297
1350c3b4 2298/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2299
9291a0cd 2300static void
97a1449a 2301dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2302 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2303{
f4dc4d17
DE
2304 /* Skip type_unit_groups, reading the type units they contain
2305 is handled elsewhere. */
197400e8 2306 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2307 return;
2308
08ac5771
SM
2309 {
2310 /* The destructor of dwarf2_queue_guard frees any entries left on
2311 the queue. After this point we're guaranteed to leave this function
2312 with the dwarf queue empty. */
2313 dwarf2_queue_guard q_guard (per_objfile);
89e63ee4 2314
08ac5771
SM
2315 if (!per_objfile->symtab_set_p (per_cu))
2316 {
2317 queue_comp_unit (per_cu, per_objfile, language_minimal);
2318 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2319
2320 /* If we just loaded a CU from a DWO, and we're working with an index
2321 that may badly handle TUs, load all the TUs in that DWO as well.
2322 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2323 if (!per_cu->is_debug_types
2324 && cu != NULL
2325 && cu->dwo_unit != NULL
2326 && per_objfile->per_bfd->index_table != NULL
2327 && per_objfile->per_bfd->index_table->version <= 7
2328 /* DWP files aren't supported yet. */
2329 && get_dwp_file (per_objfile) == NULL)
2330 queue_and_load_all_dwo_tus (cu);
2331 }
9291a0cd 2332
08ac5771
SM
2333 process_queue (per_objfile);
2334 }
9291a0cd
TT
2335
2336 /* Age the cache, releasing compilation units that have not
2337 been used recently. */
976ca316 2338 per_objfile->age_comp_units ();
9291a0cd
TT
2339}
2340
97a1449a
SM
2341/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2342 the per-objfile for which this symtab is instantiated.
2343
2344 Returns the resulting symbol table. */
2fdf6df6 2345
43f3e411 2346static struct compunit_symtab *
97a1449a 2347dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2348 dwarf2_per_objfile *per_objfile,
97a1449a 2349 bool skip_partial)
9291a0cd 2350{
976ca316 2351 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2352
976ca316 2353 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2354 {
976ca316 2355 free_cached_comp_units freer (per_objfile);
c83dd867 2356 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2357 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2358 process_cu_includes (per_objfile);
9291a0cd 2359 }
f194fefb 2360
976ca316 2361 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2362}
2363
ff4c9fec 2364/* See declaration. */
f4dc4d17 2365
ff4c9fec 2366dwarf2_per_cu_data *
5989a64e 2367dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2368{
b76e467d 2369 if (index >= this->all_comp_units.size ())
ff4c9fec 2370 {
b76e467d 2371 index -= this->all_comp_units.size ();
b2bdb8cf 2372 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2373 return &this->all_type_units[index]->per_cu;
2374 }
f4dc4d17 2375
ff4c9fec
SM
2376 return this->all_comp_units[index];
2377}
f4dc4d17 2378
ff4c9fec 2379/* See declaration. */
2fdf6df6 2380
ff4c9fec 2381dwarf2_per_cu_data *
5989a64e 2382dwarf2_per_bfd::get_cu (int index)
1fd400ff 2383{
b76e467d 2384 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2385
ff4c9fec 2386 return this->all_comp_units[index];
f4dc4d17
DE
2387}
2388
ff4c9fec 2389/* See declaration. */
f4dc4d17 2390
ff4c9fec 2391signatured_type *
5989a64e 2392dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2393{
b2bdb8cf 2394 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2395
ff4c9fec 2396 return this->all_type_units[index];
1fd400ff
TT
2397}
2398
d3473f0c
TT
2399/* See read.h. */
2400
2401dwarf2_per_cu_data *
5989a64e 2402dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2403{
2404 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2405 result->per_bfd = this;
d3473f0c
TT
2406 result->index = m_num_psymtabs++;
2407 return result;
2408}
2409
2410/* See read.h. */
2411
2412signatured_type *
5989a64e 2413dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2414{
2415 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2416 result->per_cu.per_bfd = this;
d3473f0c
TT
2417 result->per_cu.index = m_num_psymtabs++;
2418 return result;
2419}
2420
168c9250 2421/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2422 obstack, and constructed with the specified field values. */
4b514bc8
JK
2423
2424static dwarf2_per_cu_data *
168c9250
SM
2425create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2426 struct dwarf2_section_info *section,
2427 int is_dwz,
2428 sect_offset sect_off, ULONGEST length)
4b514bc8 2429{
168c9250 2430 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2431 the_cu->sect_off = sect_off;
2432 the_cu->length = length;
4b514bc8 2433 the_cu->section = section;
168c9250 2434 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2435 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2436 the_cu->is_dwz = is_dwz;
2437 return the_cu;
2438}
2439
2ec9a5e0
TT
2440/* A helper for create_cus_from_index that handles a given list of
2441 CUs. */
2fdf6df6 2442
74a0d9f6 2443static void
168c9250 2444create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2445 const gdb_byte *cu_list, offset_type n_elements,
2446 struct dwarf2_section_info *section,
b76e467d 2447 int is_dwz)
9291a0cd 2448{
12359b5e 2449 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2450 {
74a0d9f6 2451 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2452
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2455 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2456 cu_list += 2 * 8;
2457
b76e467d 2458 dwarf2_per_cu_data *per_cu
168c9250
SM
2459 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2460 length);
2461 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2462 }
9291a0cd
TT
2463}
2464
2ec9a5e0 2465/* Read the CU list from the mapped index, and use it to create all
168c9250 2466 the CU objects for PER_BFD. */
2ec9a5e0 2467
74a0d9f6 2468static void
168c9250 2469create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2470 const gdb_byte *cu_list, offset_type cu_list_elements,
2471 const gdb_byte *dwz_list, offset_type dwz_elements)
2472{
168c9250
SM
2473 gdb_assert (per_bfd->all_comp_units.empty ());
2474 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2475
168c9250
SM
2476 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2477 &per_bfd->info, 0);
2ec9a5e0
TT
2478
2479 if (dwz_elements == 0)
74a0d9f6 2480 return;
2ec9a5e0 2481
168c9250
SM
2482 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2483 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2484 &dwz->info, 1);
2ec9a5e0
TT
2485}
2486
1fd400ff 2487/* Create the signatured type hash table from the index. */
673bfd45 2488
74a0d9f6 2489static void
12359b5e 2490create_signatured_type_table_from_index
168c9250
SM
2491 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2492 const gdb_byte *bytes, offset_type elements)
1fd400ff 2493{
168c9250
SM
2494 gdb_assert (per_bfd->all_type_units.empty ());
2495 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2496
298e9637 2497 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2498
12359b5e 2499 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2500 {
52dc124a 2501 struct signatured_type *sig_type;
9c541725 2502 ULONGEST signature;
1fd400ff 2503 void **slot;
9c541725 2504 cu_offset type_offset_in_tu;
1fd400ff 2505
74a0d9f6 2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
168c9250 2515 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2516 sig_type->signature = signature;
9c541725 2517 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2518 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2519 sig_type->per_cu.section = section;
9c541725 2520 sig_type->per_cu.sect_off = sect_off;
52dc124a 2521 sig_type->per_cu.v.quick
168c9250 2522 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2523 struct dwarf2_per_cu_quick_data);
2524
b0b6a987 2525 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2526 *slot = sig_type;
1fd400ff 2527
168c9250 2528 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2529 }
2530
168c9250 2531 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2532}
2533
927aa2e7
JK
2534/* Create the signatured type hash table from .debug_names. */
2535
2536static void
2537create_signatured_type_table_from_debug_names
976ca316 2538 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2539 const mapped_debug_names &map,
2540 struct dwarf2_section_info *section,
2541 struct dwarf2_section_info *abbrev_section)
2542{
976ca316 2543 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2544
96b79293
TT
2545 section->read (objfile);
2546 abbrev_section->read (objfile);
927aa2e7 2547
976ca316
SM
2548 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2549 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2550
298e9637 2551 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2552
2553 for (uint32_t i = 0; i < map.tu_count; ++i)
2554 {
2555 struct signatured_type *sig_type;
927aa2e7 2556 void **slot;
927aa2e7
JK
2557
2558 sect_offset sect_off
2559 = (sect_offset) (extract_unsigned_integer
2560 (map.tu_table_reordered + i * map.offset_size,
2561 map.offset_size,
2562 map.dwarf5_byte_order));
2563
2564 comp_unit_head cu_header;
976ca316 2565 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2566 abbrev_section,
927aa2e7
JK
2567 section->buffer + to_underlying (sect_off),
2568 rcuh_kind::TYPE);
2569
976ca316 2570 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2571 sig_type->signature = cu_header.signature;
2572 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2573 sig_type->per_cu.is_debug_types = 1;
2574 sig_type->per_cu.section = section;
2575 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2576 sig_type->per_cu.v.quick
976ca316 2577 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2578 struct dwarf2_per_cu_quick_data);
2579
b0b6a987 2580 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2581 *slot = sig_type;
2582
976ca316 2583 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2584 }
2585
976ca316 2586 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2587}
2588
9291a0cd
TT
2589/* Read the address map data from the mapped index, and use it to
2590 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2591
9291a0cd 2592static void
976ca316 2593create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2594 struct mapped_index *index)
9291a0cd 2595{
976ca316 2596 struct objfile *objfile = per_objfile->objfile;
08feed99 2597 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2598 const gdb_byte *iter, *end;
9291a0cd 2599 struct addrmap *mutable_map;
9291a0cd
TT
2600 CORE_ADDR baseaddr;
2601
8268c778
PA
2602 auto_obstack temp_obstack;
2603
9291a0cd
TT
2604 mutable_map = addrmap_create_mutable (&temp_obstack);
2605
f00a2de2
PA
2606 iter = index->address_table.data ();
2607 end = iter + index->address_table.size ();
9291a0cd 2608
b3b3bada 2609 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2610
2611 while (iter < end)
2612 {
2613 ULONGEST hi, lo, cu_index;
2614 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2615 iter += 8;
2616 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2617 iter += 8;
2618 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2619 iter += 4;
f652bce2 2620
24a55014 2621 if (lo > hi)
f652bce2 2622 {
b98664d3 2623 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2624 hex_string (lo), hex_string (hi));
24a55014 2625 continue;
f652bce2 2626 }
24a55014 2627
976ca316 2628 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2629 {
b98664d3 2630 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2631 (unsigned) cu_index);
24a55014 2632 continue;
f652bce2 2633 }
24a55014 2634
79748972
TT
2635 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2636 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2637 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2638 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2639 }
2640
d320c2b5 2641 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2642 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2643}
2644
927aa2e7
JK
2645/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2646 populate the objfile's psymtabs_addrmap. */
2647
2648static void
976ca316 2649create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2650 struct dwarf2_section_info *section)
2651{
976ca316 2652 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2653 bfd *abfd = objfile->obfd;
08feed99 2654 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2655 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2656
2657 auto_obstack temp_obstack;
2658 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2659
2660 std::unordered_map<sect_offset,
2661 dwarf2_per_cu_data *,
2662 gdb::hash_enum<sect_offset>>
2663 debug_info_offset_to_per_cu;
976ca316 2664 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2665 {
927aa2e7
JK
2666 const auto insertpair
2667 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2668 if (!insertpair.second)
2669 {
2670 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2671 "debug_info_offset %s, ignoring .debug_aranges."),
2672 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2673 return;
2674 }
2675 }
2676
96b79293 2677 section->read (objfile);
927aa2e7
JK
2678
2679 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2680
2681 const gdb_byte *addr = section->buffer;
2682
2683 while (addr < section->buffer + section->size)
2684 {
2685 const gdb_byte *const entry_addr = addr;
2686 unsigned int bytes_read;
2687
2688 const LONGEST entry_length = read_initial_length (abfd, addr,
2689 &bytes_read);
2690 addr += bytes_read;
2691
2692 const gdb_byte *const entry_end = addr + entry_length;
2693 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2694 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2695 if (addr + entry_length > section->buffer + section->size)
2696 {
47e3f474 2697 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2698 "length %s exceeds section length %s, "
927aa2e7 2699 "ignoring .debug_aranges."),
47e3f474
TV
2700 objfile_name (objfile),
2701 plongest (entry_addr - section->buffer),
927aa2e7
JK
2702 plongest (bytes_read + entry_length),
2703 pulongest (section->size));
2704 return;
2705 }
2706
2707 /* The version number. */
2708 const uint16_t version = read_2_bytes (abfd, addr);
2709 addr += 2;
2710 if (version != 2)
2711 {
47e3f474 2712 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2713 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2714 objfile_name (objfile),
2715 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2716 return;
2717 }
2718
2719 const uint64_t debug_info_offset
2720 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2721 addr += offset_size;
2722 const auto per_cu_it
2723 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2724 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2725 {
47e3f474 2726 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2727 "debug_info_offset %s does not exists, "
2728 "ignoring .debug_aranges."),
47e3f474
TV
2729 objfile_name (objfile),
2730 plongest (entry_addr - section->buffer),
927aa2e7
JK
2731 pulongest (debug_info_offset));
2732 return;
2733 }
2734 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2735
2736 const uint8_t address_size = *addr++;
2737 if (address_size < 1 || address_size > 8)
2738 {
47e3f474 2739 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2740 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2741 objfile_name (objfile),
2742 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2743 return;
2744 }
2745
2746 const uint8_t segment_selector_size = *addr++;
2747 if (segment_selector_size != 0)
2748 {
47e3f474 2749 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2750 "segment_selector_size %u is not supported, "
2751 "ignoring .debug_aranges."),
47e3f474
TV
2752 objfile_name (objfile),
2753 plongest (entry_addr - section->buffer),
927aa2e7
JK
2754 segment_selector_size);
2755 return;
2756 }
2757
2758 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2759 size. It is undocumented by the DWARF standard but GCC does
2760 use it. */
927aa2e7
JK
2761 for (size_t padding = ((-(addr - section->buffer))
2762 & (2 * address_size - 1));
dda83cd7 2763 padding > 0; padding--)
927aa2e7
JK
2764 if (*addr++ != 0)
2765 {
47e3f474 2766 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2767 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2768 objfile_name (objfile),
2769 plongest (entry_addr - section->buffer));
927aa2e7
JK
2770 return;
2771 }
2772
2773 for (;;)
2774 {
2775 if (addr + 2 * address_size > entry_end)
2776 {
47e3f474 2777 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2778 "address list is not properly terminated, "
2779 "ignoring .debug_aranges."),
47e3f474
TV
2780 objfile_name (objfile),
2781 plongest (entry_addr - section->buffer));
927aa2e7
JK
2782 return;
2783 }
2784 ULONGEST start = extract_unsigned_integer (addr, address_size,
2785 dwarf5_byte_order);
2786 addr += address_size;
2787 ULONGEST length = extract_unsigned_integer (addr, address_size,
2788 dwarf5_byte_order);
2789 addr += address_size;
2790 if (start == 0 && length == 0)
2791 break;
976ca316 2792 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2793 {
2794 /* Symbol was eliminated due to a COMDAT group. */
2795 continue;
2796 }
2797 ULONGEST end = start + length;
79748972
TT
2798 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2799 - baseaddr);
2800 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2801 - baseaddr);
927aa2e7
JK
2802 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2803 }
2804 }
2805
d320c2b5 2806 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2807 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2808}
2809
9291a0cd
TT
2810/* Find a slot in the mapped index INDEX for the object named NAME.
2811 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2812 constant pool and return true. If NAME cannot be found, return
2813 false. */
2fdf6df6 2814
109483d9 2815static bool
9291a0cd
TT
2816find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2817 offset_type **vec_out)
2818{
0cf03b49 2819 offset_type hash;
9291a0cd 2820 offset_type slot, step;
559a7a62 2821 int (*cmp) (const char *, const char *);
9291a0cd 2822
791afaa2 2823 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2824 if (current_language->la_language == language_cplus
45280282
IB
2825 || current_language->la_language == language_fortran
2826 || current_language->la_language == language_d)
0cf03b49
JK
2827 {
2828 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2829 not contain any. */
a8719064 2830
72998fb3 2831 if (strchr (name, '(') != NULL)
0cf03b49 2832 {
109483d9 2833 without_params = cp_remove_params (name);
0cf03b49 2834
72998fb3 2835 if (without_params != NULL)
791afaa2 2836 name = without_params.get ();
0cf03b49
JK
2837 }
2838 }
2839
559a7a62 2840 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2841 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2842 simulate our NAME being searched is also lowercased. */
2843 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 2844 && case_sensitivity == case_sensitive_off
559a7a62
JK
2845 ? 5 : index->version),
2846 name);
2847
f00a2de2
PA
2848 slot = hash & (index->symbol_table.size () - 1);
2849 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2850 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2851
2852 for (;;)
2853 {
9291a0cd 2854 const char *str;
f00a2de2
PA
2855
2856 const auto &bucket = index->symbol_table[slot];
2857 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2858 return false;
9291a0cd 2859
f00a2de2 2860 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2861 if (!cmp (name, str))
9291a0cd
TT
2862 {
2863 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2864 + MAYBE_SWAP (bucket.vec));
109483d9 2865 return true;
9291a0cd
TT
2866 }
2867
f00a2de2 2868 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2869 }
2870}
2871
4485a1c1
SM
2872/* A helper function that reads the .gdb_index from BUFFER and fills
2873 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2874 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2875 ok to use deprecated sections.
2876
2877 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2878 out parameters that are filled in with information about the CU and
2879 TU lists in the section.
2880
4485a1c1 2881 Returns true if all went well, false otherwise. */
2fdf6df6 2882
d33bc52e 2883static bool
3810f182 2884read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2885 bool deprecated_ok,
2886 gdb::array_view<const gdb_byte> buffer,
2887 struct mapped_index *map,
2888 const gdb_byte **cu_list,
2889 offset_type *cu_list_elements,
2890 const gdb_byte **types_list,
2891 offset_type *types_list_elements)
2892{
2893 const gdb_byte *addr = &buffer[0];
82430852 2894
9291a0cd 2895 /* Version check. */
4485a1c1 2896 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2897 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2898 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2899 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2900 indices. */
831adc1f 2901 if (version < 4)
481860b3
GB
2902 {
2903 static int warning_printed = 0;
2904 if (!warning_printed)
2905 {
2906 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2907 filename);
481860b3
GB
2908 warning_printed = 1;
2909 }
2910 return 0;
2911 }
2912 /* Index version 4 uses a different hash function than index version
2913 5 and later.
2914
2915 Versions earlier than 6 did not emit psymbols for inlined
2916 functions. Using these files will cause GDB not to be able to
2917 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2918 indices unless the user has done
2919 "set use-deprecated-index-sections on". */
2ec9a5e0 2920 if (version < 6 && !deprecated_ok)
481860b3
GB
2921 {
2922 static int warning_printed = 0;
2923 if (!warning_printed)
2924 {
e615022a
DE
2925 warning (_("\
2926Skipping deprecated .gdb_index section in %s.\n\
2927Do \"set use-deprecated-index-sections on\" before the file is read\n\
2928to use the section anyway."),
2ec9a5e0 2929 filename);
481860b3
GB
2930 warning_printed = 1;
2931 }
2932 return 0;
2933 }
796a7ff8 2934 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2935 of the TU (for symbols coming from TUs),
2936 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2937 Plus gold-generated indices can have duplicate entries for global symbols,
2938 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2939 These are just performance bugs, and we can't distinguish gdb-generated
2940 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2941
481860b3 2942 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2943 longer backward compatible. */
796a7ff8 2944 if (version > 8)
594e8718 2945 return 0;
9291a0cd 2946
559a7a62 2947 map->version = version;
9291a0cd 2948
4485a1c1 2949 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2950
4485a1c1 2951 int i = 0;
2ec9a5e0
TT
2952 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2953 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2954 / 8);
1fd400ff
TT
2955 ++i;
2956
2ec9a5e0
TT
2957 *types_list = addr + MAYBE_SWAP (metadata[i]);
2958 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2959 - MAYBE_SWAP (metadata[i]))
2960 / 8);
987d643c 2961 ++i;
1fd400ff 2962
f00a2de2
PA
2963 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2964 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2965 map->address_table
2966 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2967 ++i;
2968
f00a2de2
PA
2969 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2970 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2971 map->symbol_table
2972 = gdb::array_view<mapped_index::symbol_table_slot>
2973 ((mapped_index::symbol_table_slot *) symbol_table,
2974 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2975
f00a2de2 2976 ++i;
f9d83a0b 2977 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2978
2ec9a5e0
TT
2979 return 1;
2980}
2981
4485a1c1
SM
2982/* Callback types for dwarf2_read_gdb_index. */
2983
2984typedef gdb::function_view
5989a64e 2985 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
2986 get_gdb_index_contents_ftype;
2987typedef gdb::function_view
2988 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2989 get_gdb_index_contents_dwz_ftype;
2990
927aa2e7 2991/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2992 elements of all the CUs and return 1. Otherwise, return 0. */
2993
2994static int
4485a1c1 2995dwarf2_read_gdb_index
976ca316 2996 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
2997 get_gdb_index_contents_ftype get_gdb_index_contents,
2998 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2999{
2ec9a5e0
TT
3000 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3001 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3002 struct dwz_file *dwz;
976ca316 3003 struct objfile *objfile = per_objfile->objfile;
f8c41851 3004 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3005
4485a1c1 3006 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3007 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3008
3009 if (main_index_contents.empty ())
3010 return 0;
3011
3063847f 3012 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3013 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3014 use_deprecated_index_sections,
3015 main_index_contents, map.get (), &cu_list,
3016 &cu_list_elements, &types_list,
3017 &types_list_elements))
2ec9a5e0
TT
3018 return 0;
3019
0fefef59 3020 /* Don't use the index if it's empty. */
3063847f 3021 if (map->symbol_table.empty ())
0fefef59
DE
3022 return 0;
3023
2ec9a5e0
TT
3024 /* If there is a .dwz file, read it so we can get its CU list as
3025 well. */
f8c41851 3026 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3027 if (dwz != NULL)
2ec9a5e0 3028 {
2ec9a5e0
TT
3029 struct mapped_index dwz_map;
3030 const gdb_byte *dwz_types_ignore;
3031 offset_type dwz_types_elements_ignore;
3032
4485a1c1
SM
3033 gdb::array_view<const gdb_byte> dwz_index_content
3034 = get_gdb_index_contents_dwz (objfile, dwz);
3035
3036 if (dwz_index_content.empty ())
3037 return 0;
3038
3810f182 3039 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3040 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3041 &dwz_list, &dwz_list_elements,
3042 &dwz_types_ignore,
3043 &dwz_types_elements_ignore))
2ec9a5e0
TT
3044 {
3045 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3046 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3047 return 0;
3048 }
3049 }
3050
f8c41851
SM
3051 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3052 dwz_list_elements);
1fd400ff 3053
8b70b953
TT
3054 if (types_list_elements)
3055 {
8b70b953
TT
3056 /* We can only handle a single .debug_types when we have an
3057 index. */
f8c41851 3058 if (per_bfd->types.size () != 1)
8b70b953
TT
3059 return 0;
3060
f8c41851 3061 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3062
f8c41851 3063 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3064 types_list_elements);
8b70b953 3065 }
9291a0cd 3066
976ca316 3067 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3068
f8c41851
SM
3069 per_bfd->index_table = std::move (map);
3070 per_bfd->using_index = 1;
3071 per_bfd->quick_file_names_table =
3072 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3073
3074 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3075 objfiles using the same BFD. */
3076 gdb_assert (per_bfd->partial_symtabs == nullptr);
3077 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3078
3079 return 1;
3080}
3081
dee91e82 3082/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3083
dee91e82
DE
3084static void
3085dw2_get_file_names_reader (const struct die_reader_specs *reader,
3e225074 3086 struct die_info *comp_unit_die)
9291a0cd 3087{
dee91e82 3088 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3089 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3090 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3091 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3092 struct attribute *attr;
7b9f3c50
DE
3093 void **slot;
3094 struct quick_file_names *qfn;
9291a0cd 3095
0186c6a7
DE
3096 gdb_assert (! this_cu->is_debug_types);
3097
07261596
TT
3098 /* Our callers never want to match partial units -- instead they
3099 will match the enclosing full CU. */
3100 if (comp_unit_die->tag == DW_TAG_partial_unit)
3101 {
3102 this_cu->v.quick->no_file_data = 1;
3103 return;
3104 }
3105
0186c6a7 3106 lh_cu = this_cu;
7b9f3c50 3107 slot = NULL;
dee91e82 3108
fff8551c 3109 line_header_up lh;
9c541725 3110 sect_offset line_offset {};
fff8551c 3111
dee91e82 3112 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3113 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3114 {
7b9f3c50
DE
3115 struct quick_file_names find_entry;
3116
d4df075e 3117 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3118
3119 /* We may have already read in this line header (TU line header sharing).
3120 If we have we're done. */
094b34ac 3121 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3122 find_entry.hash.line_sect_off = line_offset;
976ca316 3123 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3124 &find_entry, INSERT);
3125 if (*slot != NULL)
3126 {
9a3c8263 3127 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3128 return;
7b9f3c50
DE
3129 }
3130
3019eac3 3131 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3132 }
3133 if (lh == NULL)
3134 {
094b34ac 3135 lh_cu->v.quick->no_file_data = 1;
dee91e82 3136 return;
9291a0cd
TT
3137 }
3138
976ca316 3139 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3140 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3141 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3142 gdb_assert (slot != NULL);
3143 *slot = qfn;
9291a0cd 3144
d721ba37 3145 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3146
aa391654
TT
3147 int offset = 0;
3148 if (strcmp (fnd.name, "<unknown>") != 0)
3149 ++offset;
3150
7ba99d21 3151 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3152 qfn->file_names =
976ca316 3153 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3154 qfn->num_file_names);
aa391654
TT
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3157 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
7b9f3c50 3160 qfn->real_names = NULL;
9291a0cd 3161
094b34ac 3162 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3163}
3164
3165/* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168static struct quick_file_names *
ab432490
SM
3169dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3170 dwarf2_per_objfile *per_objfile)
dee91e82 3171{
0186c6a7
DE
3172 /* This should never be called for TUs. */
3173 gdb_assert (! this_cu->is_debug_types);
3174 /* Nor type unit groups. */
197400e8 3175 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3176
dee91e82
DE
3177 if (this_cu->v.quick->file_names != NULL)
3178 return this_cu->v.quick->file_names;
3179 /* If we know there is no line data, no point in looking again. */
3180 if (this_cu->v.quick->no_file_data)
3181 return NULL;
3182
ab432490 3183 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3184 if (!reader.dummy_p)
eb5dd737 3185 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
dee91e82
DE
3186
3187 if (this_cu->v.quick->no_file_data)
3188 return NULL;
3189 return this_cu->v.quick->file_names;
9291a0cd
TT
3190}
3191
3192/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3193 real path for a given file name from the line table. */
2fdf6df6 3194
9291a0cd 3195static const char *
976ca316 3196dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3197 struct quick_file_names *qfn, int index)
9291a0cd 3198{
7b9f3c50 3199 if (qfn->real_names == NULL)
976ca316 3200 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3201 qfn->num_file_names, const char *);
9291a0cd 3202
7b9f3c50 3203 if (qfn->real_names[index] == NULL)
14278e1f 3204 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3205
7b9f3c50 3206 return qfn->real_names[index];
9291a0cd
TT
3207}
3208
3209static struct symtab *
3210dw2_find_last_source_symtab (struct objfile *objfile)
3211{
976ca316
SM
3212 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3213 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3215
43f3e411
DE
3216 if (cust == NULL)
3217 return NULL;
ed2dc618 3218
43f3e411 3219 return compunit_primary_filetab (cust);
9291a0cd
TT
3220}
3221
7b9f3c50
DE
3222/* Traversal function for dw2_forget_cached_source_info. */
3223
3224static int
3225dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3226{
7b9f3c50 3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3228
7b9f3c50 3229 if (file_data->real_names)
9291a0cd 3230 {
7b9f3c50 3231 int i;
9291a0cd 3232
7b9f3c50 3233 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3234 {
7b9f3c50
DE
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
9291a0cd
TT
3237 }
3238 }
7b9f3c50
DE
3239
3240 return 1;
3241}
3242
3243static void
3244dw2_forget_cached_source_info (struct objfile *objfile)
3245{
976ca316 3246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3247
976ca316 3248 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3249 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3250}
3251
f8eba3c6
TT
3252/* Helper function for dw2_map_symtabs_matching_filename that expands
3253 the symtabs and calls the iterator. */
3254
3255static int
3256dw2_map_expand_apply (struct objfile *objfile,
3257 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3258 const char *name, const char *real_path,
14bc53a8 3259 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3260{
43f3e411 3261 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3262
3263 /* Don't visit already-expanded CUs. */
af758d11
SM
3264 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3265 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
97a1449a 3270 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3271
14bc53a8
PA
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
f8eba3c6
TT
3274}
3275
3276/* Implementation of the map_symtabs_matching_filename method. */
3277
14bc53a8
PA
3278static bool
3279dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3282{
c011a4f4 3283 const char *name_basename = lbasename (name);
976ca316 3284 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3285
848e3e78
DE
3286 /* The rule is CUs specify all the files, including those used by
3287 any TU, so there's no need to scan TUs here. */
f4dc4d17 3288
976ca316 3289 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3290 {
3d7bb9d9 3291 /* We only need to look at symtabs not already expanded. */
976ca316 3292 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3293 continue;
3294
976ca316 3295 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3296 if (file_data == NULL)
9291a0cd
TT
3297 continue;
3298
b76e467d 3299 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3300 {
7b9f3c50 3301 const char *this_name = file_data->file_names[j];
da235a7c 3302 const char *this_real_name;
9291a0cd 3303
af529f8f 3304 if (compare_filenames_for_search (this_name, name))
9291a0cd 3305 {
f5b95b50 3306 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3307 callback))
3308 return true;
288e77a7 3309 continue;
4aac40c8 3310 }
9291a0cd 3311
c011a4f4
DE
3312 /* Before we invoke realpath, which can get expensive when many
3313 files are involved, do a quick comparison of the basenames. */
3314 if (! basenames_may_differ
3315 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3316 continue;
3317
976ca316 3318 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3319 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3320 {
da235a7c 3321 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3322 callback))
3323 return true;
288e77a7 3324 continue;
da235a7c 3325 }
9291a0cd 3326
da235a7c
JK
3327 if (real_path != NULL)
3328 {
af529f8f
JK
3329 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3330 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3331 if (this_real_name != NULL
af529f8f 3332 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3333 {
f5b95b50 3334 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3335 callback))
3336 return true;
288e77a7 3337 continue;
9291a0cd
TT
3338 }
3339 }
3340 }
3341 }
3342
14bc53a8 3343 return false;
9291a0cd
TT
3344}
3345
da51c347
DE
3346/* Struct used to manage iterating over all CUs looking for a symbol. */
3347
3348struct dw2_symtab_iterator
9291a0cd 3349{
ed2dc618 3350 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3351 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3352 /* If set, only look for symbols that match that block. Valid values are
3353 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3354 gdb::optional<block_enum> block_index;
da51c347
DE
3355 /* The kind of symbol we're looking for. */
3356 domain_enum domain;
3357 /* The list of CUs from the index entry of the symbol,
3358 or NULL if not found. */
3359 offset_type *vec;
3360 /* The next element in VEC to look at. */
3361 int next;
3362 /* The number of elements in VEC, or zero if there is no match. */
3363 int length;
8943b874
DE
3364 /* Have we seen a global version of the symbol?
3365 If so we can ignore all further global instances.
3366 This is to work around gold/15646, inefficient gold-generated
3367 indices. */
3368 int global_seen;
da51c347 3369};
9291a0cd 3370
e5f3ece2 3371/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3372
9291a0cd 3373static void
e5f3ece2
TV
3374dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3375 dwarf2_per_objfile *per_objfile,
3376 gdb::optional<block_enum> block_index,
3377 domain_enum domain)
da51c347 3378{
976ca316 3379 iter->per_objfile = per_objfile;
da51c347
DE
3380 iter->block_index = block_index;
3381 iter->domain = domain;
3382 iter->next = 0;
8943b874 3383 iter->global_seen = 0;
e5f3ece2
TV
3384 iter->vec = NULL;
3385 iter->length = 0;
3386}
da51c347 3387
e5f3ece2
TV
3388/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3389
3390static void
3391dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3392 dwarf2_per_objfile *per_objfile,
3393 gdb::optional<block_enum> block_index,
3394 domain_enum domain,
3395 const char *name)
3396{
3397 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3398
e5f3ece2 3399 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3400 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3401 if (index == NULL)
3402 return;
3403
3404 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3405 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3406}
3407
3408/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3409
3410static void
3411dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3412 dwarf2_per_objfile *per_objfile,
3413 gdb::optional<block_enum> block_index,
3414 domain_enum domain, offset_type namei)
3415{
3416 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3417
3418 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3419 /* index is NULL if OBJF_READNOW. */
3420 if (index == NULL)
3421 return;
3422
3423 gdb_assert (!index->symbol_name_slot_invalid (namei));
3424 const auto &bucket = index->symbol_table[namei];
3425
3426 iter->vec = (offset_type *) (index->constant_pool
3427 + MAYBE_SWAP (bucket.vec));
3428 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3429}
3430
3431/* Return the next matching CU or NULL if there are no more. */
3432
3433static struct dwarf2_per_cu_data *
3434dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3435{
976ca316 3436 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3437
da51c347
DE
3438 for ( ; iter->next < iter->length; ++iter->next)
3439 {
3440 offset_type cu_index_and_attrs =
3441 MAYBE_SWAP (iter->vec[iter->next + 1]);
3442 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3443 gdb_index_symbol_kind symbol_kind =
3444 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3445 /* Only check the symbol attributes if they're present.
3446 Indices prior to version 7 don't record them,
3447 and indices >= 7 may elide them for certain symbols
3448 (gold does this). */
3449 int attrs_valid =
976ca316 3450 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3451 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3452
3190f0c6 3453 /* Don't crash on bad data. */
976ca316
SM
3454 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3455 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3456 {
b98664d3 3457 complaint (_(".gdb_index entry has bad CU index"
976ca316 3458 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3459 continue;
3460 }
3461
976ca316 3462 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3463
da51c347 3464 /* Skip if already read in. */
976ca316 3465 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3466 continue;
3467
8943b874
DE
3468 /* Check static vs global. */
3469 if (attrs_valid)
3470 {
2b79f376
SM
3471 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3472
3473 if (iter->block_index.has_value ())
3474 {
3475 bool want_static = *iter->block_index == STATIC_BLOCK;
3476
3477 if (is_static != want_static)
3478 continue;
3479 }
3480
8943b874 3481 /* Work around gold/15646. */
f030440d
TV
3482 if (!is_static
3483 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3484 {
3485 if (iter->global_seen)
3486 continue;
3487
3488 iter->global_seen = 1;
3489 }
8943b874 3490 }
da51c347
DE
3491
3492 /* Only check the symbol's kind if it has one. */
3493 if (attrs_valid)
3494 {
3495 switch (iter->domain)
3496 {
3497 case VAR_DOMAIN:
3498 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3499 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3500 /* Some types are also in VAR_DOMAIN. */
3501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3502 continue;
3503 break;
3504 case STRUCT_DOMAIN:
3505 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3506 continue;
3507 break;
3508 case LABEL_DOMAIN:
3509 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3510 continue;
3511 break;
59c35742
AB
3512 case MODULE_DOMAIN:
3513 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3514 continue;
3515 break;
da51c347
DE
3516 default:
3517 break;
3518 }
3519 }
3520
3521 ++iter->next;
3522 return per_cu;
3523 }
3524
3525 return NULL;
3526}
3527
43f3e411 3528static struct compunit_symtab *
c7f839cb 3529dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3530 const char *name, domain_enum domain)
9291a0cd 3531{
43f3e411 3532 struct compunit_symtab *stab_best = NULL;
976ca316 3533 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3534
b5ec771e
PA
3535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3536
ed2dc618
SM
3537 struct dw2_symtab_iterator iter;
3538 struct dwarf2_per_cu_data *per_cu;
da51c347 3539
976ca316 3540 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3541
ed2dc618
SM
3542 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3543 {
3544 struct symbol *sym, *with_opaque = NULL;
97a1449a 3545 struct compunit_symtab *stab
976ca316 3546 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3547 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3548 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3549
ed2dc618
SM
3550 sym = block_find_symbol (block, name, domain,
3551 block_find_non_opaque_type_preferred,
3552 &with_opaque);
b2e2f908 3553
ed2dc618
SM
3554 /* Some caution must be observed with overloaded functions
3555 and methods, since the index will not contain any overload
3556 information (but NAME might contain it). */
da51c347 3557
ed2dc618
SM
3558 if (sym != NULL
3559 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3560 return stab;
3561 if (with_opaque != NULL
3562 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3563 stab_best = stab;
da51c347 3564
ed2dc618 3565 /* Keep looking through other CUs. */
9291a0cd 3566 }
9291a0cd 3567
da51c347 3568 return stab_best;
9291a0cd
TT
3569}
3570
3571static void
3572dw2_print_stats (struct objfile *objfile)
3573{
976ca316
SM
3574 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3575 int total = (per_objfile->per_bfd->all_comp_units.size ()
3576 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3577 int count = 0;
9291a0cd 3578
ed2dc618 3579 for (int i = 0; i < total; ++i)
9291a0cd 3580 {
976ca316 3581 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3582
976ca316 3583 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3584 ++count;
3585 }
e4a48d9d 3586 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3587 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3588}
3589
779bd270
DE
3590/* This dumps minimal information about the index.
3591 It is called via "mt print objfiles".
3592 One use is to verify .gdb_index has been loaded by the
3593 gdb.dwarf2/gdb-index.exp testcase. */
3594
9291a0cd
TT
3595static void
3596dw2_dump (struct objfile *objfile)
3597{
976ca316 3598 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3599
976ca316 3600 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3601 printf_filtered (".gdb_index:");
976ca316 3602 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3603 {
3604 printf_filtered (" version %d\n",
976ca316 3605 per_objfile->per_bfd->index_table->version);
779bd270
DE
3606 }
3607 else
3608 printf_filtered (" faked for \"readnow\"\n");
3609 printf_filtered ("\n");
9291a0cd
TT
3610}
3611
9291a0cd
TT
3612static void
3613dw2_expand_symtabs_for_function (struct objfile *objfile,
3614 const char *func_name)
3615{
976ca316 3616 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3617
ed2dc618
SM
3618 struct dw2_symtab_iterator iter;
3619 struct dwarf2_per_cu_data *per_cu;
da51c347 3620
976ca316 3621 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3622
ed2dc618 3623 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3624 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3625
9291a0cd
TT
3626}
3627
3628static void
3629dw2_expand_all_symtabs (struct objfile *objfile)
3630{
976ca316
SM
3631 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3632 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3633 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3634
ed2dc618 3635 for (int i = 0; i < total_units; ++i)
9291a0cd 3636 {
976ca316 3637 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3638
58f0c718
TT
3639 /* We don't want to directly expand a partial CU, because if we
3640 read it with the wrong language, then assertion failures can
3641 be triggered later on. See PR symtab/23010. So, tell
3642 dw2_instantiate_symtab to skip partial CUs -- any important
3643 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3644 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3645 }
3646}
3647
3648static void
652a8996
JK
3649dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3650 const char *fullname)
9291a0cd 3651{
976ca316 3652 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3653
3654 /* We don't need to consider type units here.
3655 This is only called for examining code, e.g. expand_line_sal.
3656 There can be an order of magnitude (or more) more type units
3657 than comp units, and we avoid them if we can. */
3658
976ca316 3659 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3660 {
3d7bb9d9 3661 /* We only need to look at symtabs not already expanded. */
976ca316 3662 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3663 continue;
3664
976ca316 3665 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3666 if (file_data == NULL)
9291a0cd
TT
3667 continue;
3668
b76e467d 3669 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3670 {
652a8996
JK
3671 const char *this_fullname = file_data->file_names[j];
3672
3673 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3674 {
976ca316 3675 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3676 break;
3677 }
3678 }
3679 }
3680}
3681
9a0bacfb
TV
3682static void
3683dw2_expand_symtabs_matching_symbol
3684 (mapped_index_base &index,
3685 const lookup_name_info &lookup_name_in,
3686 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3687 enum search_domain kind,
fcf23d5b
SM
3688 gdb::function_view<bool (offset_type)> match_callback,
3689 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3690
3691static void
3692dw2_expand_symtabs_matching_one
97a1449a
SM
3693 (dwarf2_per_cu_data *per_cu,
3694 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3695 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3696 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3697
9291a0cd 3698static void
199b4314
TT
3699dw2_map_matching_symbols
3700 (struct objfile *objfile,
b054970d 3701 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3702 int global,
3703 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3704 symbol_compare_ftype *ordered_compare)
9291a0cd 3705{
1aa98955 3706 /* Used for Ada. */
976ca316 3707 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3708
9a0bacfb
TV
3709 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3710
976ca316 3711 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3712 {
976ca316 3713 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3714
9a0bacfb
TV
3715 const char *match_name = name.ada ().lookup_name ().c_str ();
3716 auto matcher = [&] (const char *symname)
3717 {
3718 if (ordered_compare == nullptr)
3719 return true;
3720 return ordered_compare (symname, match_name) == 0;
3721 };
3722
3723 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3724 [&] (offset_type namei)
3725 {
3726 struct dw2_symtab_iterator iter;
3727 struct dwarf2_per_cu_data *per_cu;
3728
976ca316 3729 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3730 namei);
9a0bacfb 3731 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3732 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3733 nullptr);
9a0bacfb 3734 return true;
976ca316 3735 }, per_objfile);
9a0bacfb
TV
3736 }
3737 else
3738 {
3739 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3740 proceed assuming all symtabs have been read in. */
3741 }
1aa98955
TV
3742
3743 for (compunit_symtab *cust : objfile->compunits ())
3744 {
3745 const struct block *block;
3746
3747 if (cust == NULL)
3748 continue;
3749 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3750 if (!iterate_over_symbols_terminated (block, name,
3751 domain, callback))
3752 return;
3753 }
9291a0cd
TT
3754}
3755
e1ef7d7a
PA
3756/* Starting from a search name, return the string that finds the upper
3757 bound of all strings that start with SEARCH_NAME in a sorted name
3758 list. Returns the empty string to indicate that the upper bound is
3759 the end of the list. */
3760
3761static std::string
3762make_sort_after_prefix_name (const char *search_name)
3763{
3764 /* When looking to complete "func", we find the upper bound of all
3765 symbols that start with "func" by looking for where we'd insert
3766 the closest string that would follow "func" in lexicographical
3767 order. Usually, that's "func"-with-last-character-incremented,
3768 i.e. "fund". Mind non-ASCII characters, though. Usually those
3769 will be UTF-8 multi-byte sequences, but we can't be certain.
3770 Especially mind the 0xff character, which is a valid character in
3771 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3772 rule out compilers allowing it in identifiers. Note that
3773 conveniently, strcmp/strcasecmp are specified to compare
3774 characters interpreted as unsigned char. So what we do is treat
3775 the whole string as a base 256 number composed of a sequence of
3776 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3777 to 0, and carries 1 to the following more-significant position.
3778 If the very first character in SEARCH_NAME ends up incremented
3779 and carries/overflows, then the upper bound is the end of the
3780 list. The string after the empty string is also the empty
3781 string.
3782
3783 Some examples of this operation:
3784
3785 SEARCH_NAME => "+1" RESULT
3786
3787 "abc" => "abd"
3788 "ab\xff" => "ac"
3789 "\xff" "a" "\xff" => "\xff" "b"
3790 "\xff" => ""
3791 "\xff\xff" => ""
3792 "" => ""
3793
3794 Then, with these symbols for example:
3795
3796 func
3797 func1
3798 fund
3799
3800 completing "func" looks for symbols between "func" and
3801 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3802 which finds "func" and "func1", but not "fund".
3803
3804 And with:
3805
3806 funcÿ (Latin1 'ÿ' [0xff])
3807 funcÿ1
3808 fund
3809
3810 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3811 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3812
3813 And with:
3814
3815 ÿÿ (Latin1 'ÿ' [0xff])
3816 ÿÿ1
3817
3818 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3819 the end of the list.
3820 */
3821 std::string after = search_name;
3822 while (!after.empty () && (unsigned char) after.back () == 0xff)
3823 after.pop_back ();
3824 if (!after.empty ())
3825 after.back () = (unsigned char) after.back () + 1;
3826 return after;
3827}
3828
5c58de74 3829/* See declaration. */
61d96d7e 3830
5c58de74
PA
3831std::pair<std::vector<name_component>::const_iterator,
3832 std::vector<name_component>::const_iterator>
44ed8f3e 3833mapped_index_base::find_name_components_bounds
fcf23d5b
SM
3834 (const lookup_name_info &lookup_name_without_params, language lang,
3835 dwarf2_per_objfile *per_objfile) const
3f563c84 3836{
5c58de74
PA
3837 auto *name_cmp
3838 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3839
3b00ef10 3840 const char *lang_name
e0802d59 3841 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3842
3f563c84
PA
3843 /* Comparison function object for lower_bound that matches against a
3844 given symbol name. */
3845 auto lookup_compare_lower = [&] (const name_component &elem,
3846 const char *name)
3847 {
fcf23d5b 3848 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3849 const char *elem_name = elem_qualified + elem.name_offset;
3850 return name_cmp (elem_name, name) < 0;
3851 };
3852
3853 /* Comparison function object for upper_bound that matches against a
3854 given symbol name. */
3855 auto lookup_compare_upper = [&] (const char *name,
3856 const name_component &elem)
3857 {
fcf23d5b 3858 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
3859 const char *elem_name = elem_qualified + elem.name_offset;
3860 return name_cmp (name, elem_name) < 0;
3861 };
3862
5c58de74
PA
3863 auto begin = this->name_components.begin ();
3864 auto end = this->name_components.end ();
3f563c84
PA
3865
3866 /* Find the lower bound. */
3867 auto lower = [&] ()
3868 {
3b00ef10 3869 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3870 return begin;
3871 else
3b00ef10 3872 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3873 } ();
3874
3875 /* Find the upper bound. */
3876 auto upper = [&] ()
3877 {
5c58de74 3878 if (lookup_name_without_params.completion_mode ())
3f563c84 3879 {
e1ef7d7a
PA
3880 /* In completion mode, we want UPPER to point past all
3881 symbols names that have the same prefix. I.e., with
3882 these symbols, and completing "func":
3883
3884 function << lower bound
3885 function1
3886 other_function << upper bound
3887
3888 We find the upper bound by looking for the insertion
3889 point of "func"-with-last-character-incremented,
3890 i.e. "fund". */
3b00ef10 3891 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3892 if (after.empty ())
3f563c84 3893 return end;
e6b2f5ef
PA
3894 return std::lower_bound (lower, end, after.c_str (),
3895 lookup_compare_lower);
3f563c84
PA
3896 }
3897 else
3b00ef10 3898 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3899 } ();
3900
5c58de74
PA
3901 return {lower, upper};
3902}
3903
3904/* See declaration. */
3905
3906void
fcf23d5b 3907mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
3908{
3909 if (!this->name_components.empty ())
3910 return;
3911
3912 this->name_components_casing = case_sensitivity;
3913 auto *name_cmp
3914 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3915
3916 /* The code below only knows how to break apart components of C++
3917 symbol names (and other languages that use '::' as
3b00ef10 3918 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3919 auto count = this->symbol_name_count ();
3920 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3921 {
44ed8f3e 3922 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3923 continue;
3924
fcf23d5b 3925 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
3926
3927 /* Add each name component to the name component table. */
3928 unsigned int previous_len = 0;
3b00ef10
TT
3929
3930 if (strstr (name, "::") != nullptr)
3931 {
3932 for (unsigned int current_len = cp_find_first_component (name);
3933 name[current_len] != '\0';
3934 current_len += cp_find_first_component (name + current_len))
3935 {
3936 gdb_assert (name[current_len] == ':');
3937 this->name_components.push_back ({previous_len, idx});
3938 /* Skip the '::'. */
3939 current_len += 2;
3940 previous_len = current_len;
3941 }
3942 }
3943 else
5c58de74 3944 {
3b00ef10
TT
3945 /* Handle the Ada encoded (aka mangled) form here. */
3946 for (const char *iter = strstr (name, "__");
3947 iter != nullptr;
3948 iter = strstr (iter, "__"))
3949 {
3950 this->name_components.push_back ({previous_len, idx});
3951 iter += 2;
3952 previous_len = iter - name;
3953 }
5c58de74 3954 }
3b00ef10 3955
5c58de74
PA
3956 this->name_components.push_back ({previous_len, idx});
3957 }
3958
3959 /* Sort name_components elements by name. */
3960 auto name_comp_compare = [&] (const name_component &left,
3961 const name_component &right)
3962 {
fcf23d5b
SM
3963 const char *left_qualified
3964 = this->symbol_name_at (left.idx, per_objfile);
3965 const char *right_qualified
3966 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
3967
3968 const char *left_name = left_qualified + left.name_offset;
3969 const char *right_name = right_qualified + right.name_offset;
3970
3971 return name_cmp (left_name, right_name) < 0;
3972 };
3973
3974 std::sort (this->name_components.begin (),
3975 this->name_components.end (),
3976 name_comp_compare);
3977}
3978
3979/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3980 mapped_index_base instead of the containing objfile. This is split
3981 to a separate function in order to be able to unit test the
3982 name_components matching using a mock mapped_index_base. For each
5c58de74 3983 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3984 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3985
3986static void
3987dw2_expand_symtabs_matching_symbol
44ed8f3e 3988 (mapped_index_base &index,
5c58de74
PA
3989 const lookup_name_info &lookup_name_in,
3990 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3991 enum search_domain kind,
fcf23d5b
SM
3992 gdb::function_view<bool (offset_type)> match_callback,
3993 dwarf2_per_objfile *per_objfile)
5c58de74
PA
3994{
3995 lookup_name_info lookup_name_without_params
3996 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3997
3998 /* Build the symbol name component sorted vector, if we haven't
3999 yet. */
fcf23d5b 4000 index.build_name_components (per_objfile);
5c58de74 4001
3f563c84
PA
4002 /* The same symbol may appear more than once in the range though.
4003 E.g., if we're looking for symbols that complete "w", and we have
4004 a symbol named "w1::w2", we'll find the two name components for
4005 that same symbol in the range. To be sure we only call the
4006 callback once per symbol, we first collect the symbol name
4007 indexes that matched in a temporary vector and ignore
4008 duplicates. */
4009 std::vector<offset_type> matches;
3f563c84 4010
3b00ef10
TT
4011 struct name_and_matcher
4012 {
4013 symbol_name_matcher_ftype *matcher;
ecc6c606 4014 const char *name;
3b00ef10
TT
4015
4016 bool operator== (const name_and_matcher &other) const
3f563c84 4017 {
ecc6c606 4018 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4019 }
4020 };
4021
4022 /* A vector holding all the different symbol name matchers, for all
4023 languages. */
4024 std::vector<name_and_matcher> matchers;
4025
4026 for (int i = 0; i < nr_languages; i++)
4027 {
4028 enum language lang_e = (enum language) i;
4029
4030 const language_defn *lang = language_def (lang_e);
4031 symbol_name_matcher_ftype *name_matcher
c9debfb9 4032 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4033
3b00ef10 4034 name_and_matcher key {
dda83cd7 4035 name_matcher,
3b00ef10
TT
4036 lookup_name_without_params.language_lookup_name (lang_e)
4037 };
4038
4039 /* Don't insert the same comparison routine more than once.
4040 Note that we do this linear walk. This is not a problem in
4041 practice because the number of supported languages is
4042 low. */
4043 if (std::find (matchers.begin (), matchers.end (), key)
4044 != matchers.end ())
9291a0cd 4045 continue;
3b00ef10
TT
4046 matchers.push_back (std::move (key));
4047
4048 auto bounds
4049 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4050 lang_e, per_objfile);
3b00ef10
TT
4051
4052 /* Now for each symbol name in range, check to see if we have a name
4053 match, and if so, call the MATCH_CALLBACK callback. */
4054
4055 for (; bounds.first != bounds.second; ++bounds.first)
4056 {
fcf23d5b
SM
4057 const char *qualified
4058 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4059
4060 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4061 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4062 continue;
9291a0cd 4063
3b00ef10
TT
4064 matches.push_back (bounds.first->idx);
4065 }
3f563c84
PA
4066 }
4067
4068 std::sort (matches.begin (), matches.end ());
4069
4070 /* Finally call the callback, once per match. */
4071 ULONGEST prev = -1;
4072 for (offset_type idx : matches)
4073 {
4074 if (prev != idx)
4075 {
3b00ef10
TT
4076 if (!match_callback (idx))
4077 break;
3f563c84
PA
4078 prev = idx;
4079 }
4080 }
4081
4082 /* Above we use a type wider than idx's for 'prev', since 0 and
4083 (offset_type)-1 are both possible values. */
4084 static_assert (sizeof (prev) > sizeof (offset_type), "");
4085}
4086
c62446b1
PA
4087#if GDB_SELF_TEST
4088
4089namespace selftests { namespace dw2_expand_symtabs_matching {
4090
a3c5fafd
PA
4091/* A mock .gdb_index/.debug_names-like name index table, enough to
4092 exercise dw2_expand_symtabs_matching_symbol, which works with the
4093 mapped_index_base interface. Builds an index from the symbol list
4094 passed as parameter to the constructor. */
4095class mock_mapped_index : public mapped_index_base
c62446b1
PA
4096{
4097public:
a3c5fafd
PA
4098 mock_mapped_index (gdb::array_view<const char *> symbols)
4099 : m_symbol_table (symbols)
c62446b1
PA
4100 {}
4101
a3c5fafd 4102 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4103
a3c5fafd 4104 /* Return the number of names in the symbol table. */
632e107b 4105 size_t symbol_name_count () const override
c62446b1 4106 {
a3c5fafd 4107 return m_symbol_table.size ();
c62446b1
PA
4108 }
4109
a3c5fafd 4110 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4111 const char *symbol_name_at
4112 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4113 {
4114 return m_symbol_table[idx];
4115 }
c62446b1 4116
a3c5fafd
PA
4117private:
4118 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4119};
4120
4121/* Convenience function that converts a NULL pointer to a "<null>"
4122 string, to pass to print routines. */
4123
4124static const char *
4125string_or_null (const char *str)
4126{
4127 return str != NULL ? str : "<null>";
4128}
4129
4130/* Check if a lookup_name_info built from
4131 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4132 index. EXPECTED_LIST is the list of expected matches, in expected
4133 matching order. If no match expected, then an empty list is
4134 specified. Returns true on success. On failure prints a warning
4135 indicating the file:line that failed, and returns false. */
4136
4137static bool
4138check_match (const char *file, int line,
4139 mock_mapped_index &mock_index,
4140 const char *name, symbol_name_match_type match_type,
4141 bool completion_mode,
fcf23d5b
SM
4142 std::initializer_list<const char *> expected_list,
4143 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4144{
4145 lookup_name_info lookup_name (name, match_type, completion_mode);
4146
4147 bool matched = true;
4148
4149 auto mismatch = [&] (const char *expected_str,
4150 const char *got)
4151 {
4152 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4153 "expected=\"%s\", got=\"%s\"\n"),
4154 file, line,
4155 (match_type == symbol_name_match_type::FULL
4156 ? "FULL" : "WILD"),
4157 name, string_or_null (expected_str), string_or_null (got));
4158 matched = false;
4159 };
4160
4161 auto expected_it = expected_list.begin ();
4162 auto expected_end = expected_list.end ();
4163
a3c5fafd 4164 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4165 NULL, ALL_DOMAIN,
4166 [&] (offset_type idx)
4167 {
fcf23d5b 4168 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4169 const char *expected_str
4170 = expected_it == expected_end ? NULL : *expected_it++;
4171
4172 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4173 mismatch (expected_str, matched_name);
3b00ef10 4174 return true;
fcf23d5b 4175 }, per_objfile);
c62446b1
PA
4176
4177 const char *expected_str
4178 = expected_it == expected_end ? NULL : *expected_it++;
4179 if (expected_str != NULL)
4180 mismatch (expected_str, NULL);
4181
4182 return matched;
4183}
4184
4185/* The symbols added to the mock mapped_index for testing (in
4186 canonical form). */
4187static const char *test_symbols[] = {
4188 "function",
4189 "std::bar",
4190 "std::zfunction",
4191 "std::zfunction2",
4192 "w1::w2",
4193 "ns::foo<char*>",
4194 "ns::foo<int>",
4195 "ns::foo<long>",
a20714ff
PA
4196 "ns2::tmpl<int>::foo2",
4197 "(anonymous namespace)::A::B::C",
c62446b1 4198
e1ef7d7a
PA
4199 /* These are used to check that the increment-last-char in the
4200 matching algorithm for completion doesn't match "t1_fund" when
4201 completing "t1_func". */
4202 "t1_func",
4203 "t1_func1",
4204 "t1_fund",
4205 "t1_fund1",
4206
4207 /* A UTF-8 name with multi-byte sequences to make sure that
4208 cp-name-parser understands this as a single identifier ("função"
4209 is "function" in PT). */
4210 u8"u8função",
4211
4212 /* \377 (0xff) is Latin1 'ÿ'. */
4213 "yfunc\377",
4214
4215 /* \377 (0xff) is Latin1 'ÿ'. */
4216 "\377",
4217 "\377\377123",
4218
c62446b1
PA
4219 /* A name with all sorts of complications. Starts with "z" to make
4220 it easier for the completion tests below. */
4221#define Z_SYM_NAME \
4222 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4223 "::tuple<(anonymous namespace)::ui*, " \
4224 "std::default_delete<(anonymous namespace)::ui>, void>"
4225
4226 Z_SYM_NAME
4227};
4228
a3c5fafd
PA
4229/* Returns true if the mapped_index_base::find_name_component_bounds
4230 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4231 in completion mode. */
5c58de74
PA
4232
4233static bool
a3c5fafd 4234check_find_bounds_finds (mapped_index_base &index,
5c58de74 4235 const char *search_name,
fcf23d5b
SM
4236 gdb::array_view<const char *> expected_syms,
4237 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4238{
4239 lookup_name_info lookup_name (search_name,
4240 symbol_name_match_type::FULL, true);
4241
3b00ef10 4242 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4243 language_cplus,
4244 per_objfile);
5c58de74
PA
4245
4246 size_t distance = std::distance (bounds.first, bounds.second);
4247 if (distance != expected_syms.size ())
4248 return false;
4249
4250 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4251 {
4252 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4253 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4254 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4255 return false;
4256 }
4257
4258 return true;
4259}
4260
4261/* Test the lower-level mapped_index::find_name_component_bounds
4262 method. */
4263
c62446b1 4264static void
5c58de74
PA
4265test_mapped_index_find_name_component_bounds ()
4266{
4267 mock_mapped_index mock_index (test_symbols);
4268
fcf23d5b 4269 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4270
4271 /* Test the lower-level mapped_index::find_name_component_bounds
4272 method in completion mode. */
4273 {
4274 static const char *expected_syms[] = {
4275 "t1_func",
4276 "t1_func1",
5c58de74
PA
4277 };
4278
fcf23d5b
SM
4279 SELF_CHECK (check_find_bounds_finds
4280 (mock_index, "t1_func", expected_syms,
4281 NULL /* per_objfile */));
5c58de74
PA
4282 }
4283
4284 /* Check that the increment-last-char in the name matching algorithm
4285 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4286 {
4287 static const char *expected_syms1[] = {
4288 "\377",
4289 "\377\377123",
4290 };
fcf23d5b
SM
4291 SELF_CHECK (check_find_bounds_finds
4292 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4293
4294 static const char *expected_syms2[] = {
4295 "\377\377123",
4296 };
fcf23d5b
SM
4297 SELF_CHECK (check_find_bounds_finds
4298 (mock_index, "\377\377", expected_syms2,
4299 NULL /* per_objfile */));
5c58de74
PA
4300 }
4301}
4302
4303/* Test dw2_expand_symtabs_matching_symbol. */
4304
4305static void
4306test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4307{
4308 mock_mapped_index mock_index (test_symbols);
4309
4310 /* We let all tests run until the end even if some fails, for debug
4311 convenience. */
4312 bool any_mismatch = false;
4313
4314 /* Create the expected symbols list (an initializer_list). Needed
4315 because lists have commas, and we need to pass them to CHECK,
4316 which is a macro. */
4317#define EXPECT(...) { __VA_ARGS__ }
4318
4319 /* Wrapper for check_match that passes down the current
4320 __FILE__/__LINE__. */
4321#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4322 any_mismatch |= !check_match (__FILE__, __LINE__, \
4323 mock_index, \
4324 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4325 EXPECTED_LIST, NULL)
c62446b1
PA
4326
4327 /* Identity checks. */
4328 for (const char *sym : test_symbols)
4329 {
4330 /* Should be able to match all existing symbols. */
4331 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4332 EXPECT (sym));
4333
4334 /* Should be able to match all existing symbols with
4335 parameters. */
4336 std::string with_params = std::string (sym) + "(int)";
4337 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4338 EXPECT (sym));
4339
4340 /* Should be able to match all existing symbols with
4341 parameters and qualifiers. */
4342 with_params = std::string (sym) + " ( int ) const";
4343 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4344 EXPECT (sym));
4345
4346 /* This should really find sym, but cp-name-parser.y doesn't
4347 know about lvalue/rvalue qualifiers yet. */
4348 with_params = std::string (sym) + " ( int ) &&";
4349 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4350 {});
4351 }
4352
e1ef7d7a
PA
4353 /* Check that the name matching algorithm for completion doesn't get
4354 confused with Latin1 'ÿ' / 0xff. */
4355 {
4356 static const char str[] = "\377";
4357 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4358 EXPECT ("\377", "\377\377123"));
4359 }
4360
4361 /* Check that the increment-last-char in the matching algorithm for
4362 completion doesn't match "t1_fund" when completing "t1_func". */
4363 {
4364 static const char str[] = "t1_func";
4365 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4366 EXPECT ("t1_func", "t1_func1"));
4367 }
4368
c62446b1
PA
4369 /* Check that completion mode works at each prefix of the expected
4370 symbol name. */
4371 {
4372 static const char str[] = "function(int)";
4373 size_t len = strlen (str);
4374 std::string lookup;
4375
4376 for (size_t i = 1; i < len; i++)
4377 {
4378 lookup.assign (str, i);
4379 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4380 EXPECT ("function"));
4381 }
4382 }
4383
4384 /* While "w" is a prefix of both components, the match function
4385 should still only be called once. */
4386 {
4387 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4388 EXPECT ("w1::w2"));
a20714ff
PA
4389 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4390 EXPECT ("w1::w2"));
c62446b1
PA
4391 }
4392
4393 /* Same, with a "complicated" symbol. */
4394 {
4395 static const char str[] = Z_SYM_NAME;
4396 size_t len = strlen (str);
4397 std::string lookup;
4398
4399 for (size_t i = 1; i < len; i++)
4400 {
4401 lookup.assign (str, i);
4402 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4403 EXPECT (Z_SYM_NAME));
4404 }
4405 }
4406
4407 /* In FULL mode, an incomplete symbol doesn't match. */
4408 {
4409 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4410 {});
4411 }
4412
4413 /* A complete symbol with parameters matches any overload, since the
4414 index has no overload info. */
4415 {
4416 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4417 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4418 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4419 EXPECT ("std::zfunction", "std::zfunction2"));
4420 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4421 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4422 }
4423
4424 /* Check that whitespace is ignored appropriately. A symbol with a
4425 template argument list. */
4426 {
4427 static const char expected[] = "ns::foo<int>";
4428 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4429 EXPECT (expected));
a20714ff
PA
4430 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4431 EXPECT (expected));
c62446b1
PA
4432 }
4433
4434 /* Check that whitespace is ignored appropriately. A symbol with a
4435 template argument list that includes a pointer. */
4436 {
4437 static const char expected[] = "ns::foo<char*>";
4438 /* Try both completion and non-completion modes. */
4439 static const bool completion_mode[2] = {false, true};
4440 for (size_t i = 0; i < 2; i++)
4441 {
4442 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4443 completion_mode[i], EXPECT (expected));
a20714ff
PA
4444 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4445 completion_mode[i], EXPECT (expected));
c62446b1
PA
4446
4447 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4448 completion_mode[i], EXPECT (expected));
a20714ff
PA
4449 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4450 completion_mode[i], EXPECT (expected));
c62446b1
PA
4451 }
4452 }
4453
4454 {
4455 /* Check method qualifiers are ignored. */
4456 static const char expected[] = "ns::foo<char*>";
4457 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4458 symbol_name_match_type::FULL, true, EXPECT (expected));
4459 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4460 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4461 CHECK_MATCH ("foo < char * > ( int ) const",
4462 symbol_name_match_type::WILD, true, EXPECT (expected));
4463 CHECK_MATCH ("foo < char * > ( int ) &&",
4464 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4465 }
4466
4467 /* Test lookup names that don't match anything. */
4468 {
a20714ff
PA
4469 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4470 {});
4471
c62446b1
PA
4472 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4473 {});
4474 }
4475
a20714ff
PA
4476 /* Some wild matching tests, exercising "(anonymous namespace)",
4477 which should not be confused with a parameter list. */
4478 {
4479 static const char *syms[] = {
4480 "A::B::C",
4481 "B::C",
4482 "C",
4483 "A :: B :: C ( int )",
4484 "B :: C ( int )",
4485 "C ( int )",
4486 };
4487
4488 for (const char *s : syms)
4489 {
4490 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4491 EXPECT ("(anonymous namespace)::A::B::C"));
4492 }
4493 }
4494
4495 {
4496 static const char expected[] = "ns2::tmpl<int>::foo2";
4497 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4498 EXPECT (expected));
4499 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4500 EXPECT (expected));
4501 }
4502
c62446b1
PA
4503 SELF_CHECK (!any_mismatch);
4504
4505#undef EXPECT
4506#undef CHECK_MATCH
4507}
4508
5c58de74
PA
4509static void
4510run_test ()
4511{
4512 test_mapped_index_find_name_component_bounds ();
4513 test_dw2_expand_symtabs_matching_symbol ();
4514}
4515
c62446b1
PA
4516}} // namespace selftests::dw2_expand_symtabs_matching
4517
4518#endif /* GDB_SELF_TEST */
4519
4b514bc8
JK
4520/* If FILE_MATCHER is NULL or if PER_CU has
4521 dwarf2_per_cu_quick_data::MARK set (see
4522 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4523 EXPANSION_NOTIFY on it. */
4524
4525static void
4526dw2_expand_symtabs_matching_one
97a1449a
SM
4527 (dwarf2_per_cu_data *per_cu,
4528 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4529 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4530 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4531{
4532 if (file_matcher == NULL || per_cu->v.quick->mark)
4533 {
af758d11 4534 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4535
97a1449a
SM
4536 compunit_symtab *symtab
4537 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4538 gdb_assert (symtab != nullptr);
4b514bc8 4539
af758d11
SM
4540 if (expansion_notify != NULL && symtab_was_null)
4541 expansion_notify (symtab);
4b514bc8
JK
4542 }
4543}
4544
3f563c84
PA
4545/* Helper for dw2_expand_matching symtabs. Called on each symbol
4546 matched, to expand corresponding CUs that were marked. IDX is the
4547 index of the symbol name that matched. */
4548
4549static void
4550dw2_expand_marked_cus
976ca316 4551 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4552 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4553 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4554 search_domain kind)
4555{
3f563c84
PA
4556 offset_type *vec, vec_len, vec_idx;
4557 bool global_seen = false;
976ca316 4558 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4559
61920122 4560 vec = (offset_type *) (index.constant_pool
f00a2de2 4561 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4562 vec_len = MAYBE_SWAP (vec[0]);
4563 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4564 {
61920122
PA
4565 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4566 /* This value is only valid for index versions >= 7. */
4567 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4568 gdb_index_symbol_kind symbol_kind =
4569 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4570 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4571 /* Only check the symbol attributes if they're present.
4572 Indices prior to version 7 don't record them,
4573 and indices >= 7 may elide them for certain symbols
4574 (gold does this). */
4575 int attrs_valid =
4576 (index.version >= 7
4577 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4578
4579 /* Work around gold/15646. */
f030440d
TV
4580 if (attrs_valid
4581 && !is_static
4582 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4583 {
f030440d 4584 if (global_seen)
61920122 4585 continue;
f030440d
TV
4586
4587 global_seen = true;
61920122 4588 }
3190f0c6 4589
61920122
PA
4590 /* Only check the symbol's kind if it has one. */
4591 if (attrs_valid)
4592 {
4593 switch (kind)
8943b874 4594 {
61920122
PA
4595 case VARIABLES_DOMAIN:
4596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4597 continue;
4598 break;
4599 case FUNCTIONS_DOMAIN:
4600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4601 continue;
61920122
PA
4602 break;
4603 case TYPES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4605 continue;
4606 break;
59c35742
AB
4607 case MODULES_DOMAIN:
4608 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4609 continue;
4610 break;
61920122
PA
4611 default:
4612 break;
8943b874 4613 }
61920122 4614 }
8943b874 4615
61920122 4616 /* Don't crash on bad data. */
976ca316
SM
4617 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4618 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4619 {
b98664d3 4620 complaint (_(".gdb_index entry has bad CU index"
976ca316 4621 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4622 continue;
4623 }
4624
976ca316
SM
4625 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4626 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4627 expansion_notify);
61920122
PA
4628 }
4629}
4630
4b514bc8
JK
4631/* If FILE_MATCHER is non-NULL, set all the
4632 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4633 that match FILE_MATCHER. */
4634
61920122 4635static void
4b514bc8 4636dw_expand_symtabs_matching_file_matcher
976ca316 4637 (dwarf2_per_objfile *per_objfile,
ed2dc618 4638 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4639{
4b514bc8 4640 if (file_matcher == NULL)
61920122
PA
4641 return;
4642
4b514bc8
JK
4643 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4644 htab_eq_pointer,
4645 NULL, xcalloc, xfree));
4646 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4647 htab_eq_pointer,
4648 NULL, xcalloc, xfree));
61920122 4649
4b514bc8
JK
4650 /* The rule is CUs specify all the files, including those used by
4651 any TU, so there's no need to scan TUs here. */
61920122 4652
976ca316 4653 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4654 {
927aa2e7
JK
4655 QUIT;
4656
4657 per_cu->v.quick->mark = 0;
4658
4659 /* We only need to look at symtabs not already expanded. */
976ca316 4660 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4661 continue;
4662
976ca316 4663 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4664 if (file_data == NULL)
4665 continue;
4666
4667 if (htab_find (visited_not_found.get (), file_data) != NULL)
4668 continue;
4669 else if (htab_find (visited_found.get (), file_data) != NULL)
4670 {
4671 per_cu->v.quick->mark = 1;
4672 continue;
4673 }
4674
b76e467d 4675 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4676 {
4677 const char *this_real_name;
4678
4679 if (file_matcher (file_data->file_names[j], false))
4680 {
4681 per_cu->v.quick->mark = 1;
4682 break;
4683 }
4684
4685 /* Before we invoke realpath, which can get expensive when many
4686 files are involved, do a quick comparison of the basenames. */
4687 if (!basenames_may_differ
4688 && !file_matcher (lbasename (file_data->file_names[j]),
4689 true))
4690 continue;
4691
976ca316 4692 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4693 if (file_matcher (this_real_name, false))
4694 {
4695 per_cu->v.quick->mark = 1;
4696 break;
4697 }
4698 }
4699
b76e467d
SM
4700 void **slot = htab_find_slot (per_cu->v.quick->mark
4701 ? visited_found.get ()
4702 : visited_not_found.get (),
4703 file_data, INSERT);
927aa2e7
JK
4704 *slot = file_data;
4705 }
4706}
4707
4708static void
4709dw2_expand_symtabs_matching
4710 (struct objfile *objfile,
4711 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4712 const lookup_name_info *lookup_name,
927aa2e7
JK
4713 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4714 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4715 enum search_domain kind)
4716{
976ca316 4717 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4718
4719 /* index_table is NULL if OBJF_READNOW. */
976ca316 4720 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4721 return;
4722
976ca316 4723 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4724
c1a66c06
TV
4725 if (symbol_matcher == NULL && lookup_name == NULL)
4726 {
976ca316 4727 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4728 {
4729 QUIT;
4730
976ca316 4731 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4732 file_matcher, expansion_notify);
c1a66c06
TV
4733 }
4734 return;
4735 }
4736
976ca316 4737 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4738
c1a66c06 4739 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4740 symbol_matcher,
4741 kind, [&] (offset_type idx)
4742 {
976ca316
SM
4743 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4744 kind);
3b00ef10 4745 return true;
976ca316 4746 }, per_objfile);
927aa2e7
JK
4747}
4748
4749/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4750 symtab. */
4751
4752static struct compunit_symtab *
4753recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4754 CORE_ADDR pc)
4755{
4756 int i;
4757
4758 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4759 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4760 return cust;
4761
4762 if (cust->includes == NULL)
4763 return NULL;
4764
4765 for (i = 0; cust->includes[i]; ++i)
4766 {
4767 struct compunit_symtab *s = cust->includes[i];
4768
4769 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4770 if (s != NULL)
4771 return s;
4772 }
4773
4774 return NULL;
4775}
4776
4777static struct compunit_symtab *
4778dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4779 struct bound_minimal_symbol msymbol,
4780 CORE_ADDR pc,
4781 struct obj_section *section,
4782 int warn_if_readin)
4783{
4784 struct dwarf2_per_cu_data *data;
4785 struct compunit_symtab *result;
4786
d320c2b5 4787 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4788 return NULL;
4789
b3b3bada 4790 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4791 data = (struct dwarf2_per_cu_data *) addrmap_find
4792 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4793 if (!data)
4794 return NULL;
4795
af758d11
SM
4796 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4797 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4799 paddress (objfile->arch (), pc));
927aa2e7 4800
97a1449a
SM
4801 result = recursively_find_pc_sect_compunit_symtab
4802 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4803
927aa2e7
JK
4804 gdb_assert (result != NULL);
4805 return result;
4806}
4807
4808static void
4809dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4810 void *data, int need_fullname)
4811{
976ca316 4812 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 4813
976ca316 4814 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 4815 {
976ca316 4816 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4817
4818 htab_up visited (htab_create_alloc (10,
4819 htab_hash_pointer, htab_eq_pointer,
4820 NULL, xcalloc, xfree));
4821
4822 /* The rule is CUs specify all the files, including those used
4823 by any TU, so there's no need to scan TUs here. We can
4824 ignore file names coming from already-expanded CUs. */
4825
976ca316 4826 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4827 {
976ca316 4828 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4829 {
4830 void **slot = htab_find_slot (visited.get (),
4831 per_cu->v.quick->file_names,
4832 INSERT);
4833
4834 *slot = per_cu->v.quick->file_names;
4835 }
4836 }
4837
976ca316 4838 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4839 {
927aa2e7 4840 /* We only need to look at symtabs not already expanded. */
976ca316 4841 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4842 continue;
4843
ab432490 4844 quick_file_names *file_data
976ca316 4845 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4846 if (file_data == NULL)
4847 continue;
4848
b76e467d 4849 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4850 if (*slot)
4851 {
4852 /* Already visited. */
4853 continue;
4854 }
4855 *slot = file_data;
4856
4857 for (int j = 0; j < file_data->num_file_names; ++j)
4858 {
4859 const char *filename = file_data->file_names[j];
976ca316 4860 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4861 }
4862 }
4863 }
4864
976ca316 4865 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4866 {
4867 gdb::unique_xmalloc_ptr<char> this_real_name;
4868
4869 if (need_fullname)
4870 this_real_name = gdb_realpath (filename);
4871 (*fun) (filename, this_real_name.get (), data);
4872 });
4873}
4874
4875static int
4876dw2_has_symbols (struct objfile *objfile)
4877{
4878 return 1;
4879}
4880
4881const struct quick_symbol_functions dwarf2_gdb_index_functions =
4882{
4883 dw2_has_symbols,
4884 dw2_find_last_source_symtab,
4885 dw2_forget_cached_source_info,
4886 dw2_map_symtabs_matching_filename,
4887 dw2_lookup_symbol,
d3214198 4888 NULL,
927aa2e7
JK
4889 dw2_print_stats,
4890 dw2_dump,
927aa2e7
JK
4891 dw2_expand_symtabs_for_function,
4892 dw2_expand_all_symtabs,
4893 dw2_expand_symtabs_with_fullname,
4894 dw2_map_matching_symbols,
4895 dw2_expand_symtabs_matching,
4896 dw2_find_pc_sect_compunit_symtab,
4897 NULL,
4898 dw2_map_symbol_filenames
4899};
4900
4901/* DWARF-5 debug_names reader. */
4902
4903/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4904static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4905
4906/* A helper function that reads the .debug_names section in SECTION
4907 and fills in MAP. FILENAME is the name of the file containing the
4908 section; it is used for error reporting.
4909
4910 Returns true if all went well, false otherwise. */
4911
4912static bool
4913read_debug_names_from_section (struct objfile *objfile,
4914 const char *filename,
4915 struct dwarf2_section_info *section,
4916 mapped_debug_names &map)
4917{
96b79293 4918 if (section->empty ())
927aa2e7
JK
4919 return false;
4920
4921 /* Older elfutils strip versions could keep the section in the main
4922 executable while splitting it for the separate debug info file. */
96b79293 4923 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4924 return false;
4925
96b79293 4926 section->read (objfile);
927aa2e7 4927
08feed99 4928 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4929
4930 const gdb_byte *addr = section->buffer;
4931
96b79293 4932 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4933
4934 unsigned int bytes_read;
4935 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4936 addr += bytes_read;
4937
4938 map.dwarf5_is_dwarf64 = bytes_read != 4;
4939 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4940 if (bytes_read + length != section->size)
4941 {
4942 /* There may be multiple per-CU indices. */
4943 warning (_("Section .debug_names in %s length %s does not match "
4944 "section length %s, ignoring .debug_names."),
4945 filename, plongest (bytes_read + length),
4946 pulongest (section->size));
4947 return false;
4948 }
4949
4950 /* The version number. */
4951 uint16_t version = read_2_bytes (abfd, addr);
4952 addr += 2;
4953 if (version != 5)
4954 {
4955 warning (_("Section .debug_names in %s has unsupported version %d, "
4956 "ignoring .debug_names."),
4957 filename, version);
4958 return false;
4959 }
4960
4961 /* Padding. */
4962 uint16_t padding = read_2_bytes (abfd, addr);
4963 addr += 2;
4964 if (padding != 0)
4965 {
4966 warning (_("Section .debug_names in %s has unsupported padding %d, "
4967 "ignoring .debug_names."),
4968 filename, padding);
4969 return false;
4970 }
4971
4972 /* comp_unit_count - The number of CUs in the CU list. */
4973 map.cu_count = read_4_bytes (abfd, addr);
4974 addr += 4;
4975
4976 /* local_type_unit_count - The number of TUs in the local TU
4977 list. */
4978 map.tu_count = read_4_bytes (abfd, addr);
4979 addr += 4;
4980
4981 /* foreign_type_unit_count - The number of TUs in the foreign TU
4982 list. */
4983 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4984 addr += 4;
4985 if (foreign_tu_count != 0)
4986 {
4987 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4988 "ignoring .debug_names."),
4989 filename, static_cast<unsigned long> (foreign_tu_count));
4990 return false;
4991 }
4992
4993 /* bucket_count - The number of hash buckets in the hash lookup
4994 table. */
4995 map.bucket_count = read_4_bytes (abfd, addr);
4996 addr += 4;
4997
4998 /* name_count - The number of unique names in the index. */
4999 map.name_count = read_4_bytes (abfd, addr);
5000 addr += 4;
5001
5002 /* abbrev_table_size - The size in bytes of the abbreviations
5003 table. */
5004 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5005 addr += 4;
5006
5007 /* augmentation_string_size - The size in bytes of the augmentation
5008 string. This value is rounded up to a multiple of 4. */
5009 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5010 addr += 4;
5011 map.augmentation_is_gdb = ((augmentation_string_size
5012 == sizeof (dwarf5_augmentation))
5013 && memcmp (addr, dwarf5_augmentation,
5014 sizeof (dwarf5_augmentation)) == 0);
5015 augmentation_string_size += (-augmentation_string_size) & 3;
5016 addr += augmentation_string_size;
5017
5018 /* List of CUs */
5019 map.cu_table_reordered = addr;
5020 addr += map.cu_count * map.offset_size;
5021
5022 /* List of Local TUs */
5023 map.tu_table_reordered = addr;
5024 addr += map.tu_count * map.offset_size;
5025
5026 /* Hash Lookup Table */
5027 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5028 addr += map.bucket_count * 4;
5029 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5030 addr += map.name_count * 4;
5031
5032 /* Name Table */
5033 map.name_table_string_offs_reordered = addr;
5034 addr += map.name_count * map.offset_size;
5035 map.name_table_entry_offs_reordered = addr;
5036 addr += map.name_count * map.offset_size;
5037
5038 const gdb_byte *abbrev_table_start = addr;
5039 for (;;)
5040 {
927aa2e7
JK
5041 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5042 addr += bytes_read;
5043 if (index_num == 0)
5044 break;
5045
5046 const auto insertpair
5047 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5048 if (!insertpair.second)
5049 {
5050 warning (_("Section .debug_names in %s has duplicate index %s, "
5051 "ignoring .debug_names."),
5052 filename, pulongest (index_num));
5053 return false;
5054 }
5055 mapped_debug_names::index_val &indexval = insertpair.first->second;
5056 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5057 addr += bytes_read;
5058
5059 for (;;)
5060 {
5061 mapped_debug_names::index_val::attr attr;
5062 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5063 addr += bytes_read;
5064 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5065 addr += bytes_read;
5066 if (attr.form == DW_FORM_implicit_const)
5067 {
5068 attr.implicit_const = read_signed_leb128 (abfd, addr,
5069 &bytes_read);
5070 addr += bytes_read;
5071 }
5072 if (attr.dw_idx == 0 && attr.form == 0)
5073 break;
5074 indexval.attr_vec.push_back (std::move (attr));
5075 }
5076 }
5077 if (addr != abbrev_table_start + abbrev_table_size)
5078 {
5079 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5080 "of size %s vs. written as %u, ignoring .debug_names."),
5081 filename, plongest (addr - abbrev_table_start),
5082 abbrev_table_size);
927aa2e7
JK
5083 return false;
5084 }
5085 map.entry_pool = addr;
5086
5087 return true;
5088}
5089
5090/* A helper for create_cus_from_debug_names that handles the MAP's CU
5091 list. */
5092
5093static void
168c9250 5094create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5095 const mapped_debug_names &map,
5096 dwarf2_section_info &section,
b76e467d 5097 bool is_dwz)
927aa2e7 5098{
3ee6bb11
TV
5099 if (!map.augmentation_is_gdb)
5100 {
fc9a13fb
TV
5101 for (uint32_t i = 0; i < map.cu_count; ++i)
5102 {
5103 sect_offset sect_off
5104 = (sect_offset) (extract_unsigned_integer
5105 (map.cu_table_reordered + i * map.offset_size,
5106 map.offset_size,
5107 map.dwarf5_byte_order));
5108 /* We don't know the length of the CU, because the CU list in a
5109 .debug_names index can be incomplete, so we can't use the start
5110 of the next CU as end of this CU. We create the CUs here with
5111 length 0, and in cutu_reader::cutu_reader we'll fill in the
5112 actual length. */
5113 dwarf2_per_cu_data *per_cu
5114 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5115 sect_off, 0);
5116 per_bfd->all_comp_units.push_back (per_cu);
5117 }
d3b54e63 5118 return;
3ee6bb11
TV
5119 }
5120
927aa2e7
JK
5121 sect_offset sect_off_prev;
5122 for (uint32_t i = 0; i <= map.cu_count; ++i)
5123 {
5124 sect_offset sect_off_next;
5125 if (i < map.cu_count)
5126 {
5127 sect_off_next
5128 = (sect_offset) (extract_unsigned_integer
5129 (map.cu_table_reordered + i * map.offset_size,
5130 map.offset_size,
5131 map.dwarf5_byte_order));
5132 }
5133 else
5134 sect_off_next = (sect_offset) section.size;
5135 if (i >= 1)
5136 {
5137 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5138 dwarf2_per_cu_data *per_cu
168c9250 5139 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5140 sect_off_prev, length);
168c9250 5141 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5142 }
5143 sect_off_prev = sect_off_next;
5144 }
5145}
5146
5147/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5148 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5149
5150static void
168c9250 5151create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5152 const mapped_debug_names &map,
5153 const mapped_debug_names &dwz_map)
5154{
168c9250
SM
5155 gdb_assert (per_bfd->all_comp_units.empty ());
5156 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5157
168c9250 5158 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5159 false /* is_dwz */);
927aa2e7
JK
5160
5161 if (dwz_map.cu_count == 0)
5162 return;
5163
168c9250
SM
5164 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5165 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5166 true /* is_dwz */);
927aa2e7
JK
5167}
5168
5169/* Read .debug_names. If everything went ok, initialize the "quick"
5170 elements of all the CUs and return true. Otherwise, return false. */
5171
5172static bool
976ca316 5173dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5174{
fcf23d5b
SM
5175 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5176 mapped_debug_names dwz_map;
976ca316 5177 struct objfile *objfile = per_objfile->objfile;
f8c41851 5178 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5179
5180 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5181 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5182 return false;
5183
5184 /* Don't use the index if it's empty. */
22ca247e 5185 if (map->name_count == 0)
927aa2e7
JK
5186 return false;
5187
5188 /* If there is a .dwz file, read it so we can get its CU list as
5189 well. */
f8c41851 5190 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5191 if (dwz != NULL)
5192 {
5193 if (!read_debug_names_from_section (objfile,
00f93c44 5194 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5195 &dwz->debug_names, dwz_map))
5196 {
5197 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5198 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5199 return false;
5200 }
5201 }
5202
f8c41851 5203 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5204
22ca247e 5205 if (map->tu_count != 0)
927aa2e7
JK
5206 {
5207 /* We can only handle a single .debug_types when we have an
5208 index. */
f8c41851 5209 if (per_bfd->types.size () != 1)
927aa2e7
JK
5210 return false;
5211
f8c41851 5212 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5213
5214 create_signatured_type_table_from_debug_names
f8c41851 5215 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5216 }
5217
f8c41851 5218 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5219
f8c41851
SM
5220 per_bfd->debug_names_table = std::move (map);
5221 per_bfd->using_index = 1;
5222 per_bfd->quick_file_names_table =
976ca316 5223 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5224
f8c41851
SM
5225 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5226 objfiles using the same BFD. */
5227 gdb_assert (per_bfd->partial_symtabs == nullptr);
5228 per_bfd->partial_symtabs = objfile->partial_symtabs;
5229
927aa2e7
JK
5230 return true;
5231}
5232
927aa2e7
JK
5233/* Type used to manage iterating over all CUs looking for a symbol for
5234 .debug_names. */
5235
5236class dw2_debug_names_iterator
5237{
5238public:
927aa2e7 5239 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5240 gdb::optional<block_enum> block_index,
5241 domain_enum domain,
fcf23d5b 5242 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5243 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5244 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5245 m_per_objfile (per_objfile)
927aa2e7
JK
5246 {}
5247
5248 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5249 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5250 : m_map (map),
5251 m_search (search),
fcf23d5b
SM
5252 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5253 m_per_objfile (per_objfile)
927aa2e7
JK
5254 {}
5255
3b00ef10
TT
5256 dw2_debug_names_iterator (const mapped_debug_names &map,
5257 block_enum block_index, domain_enum domain,
fcf23d5b 5258 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5259 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5260 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5261 m_per_objfile (per_objfile)
3b00ef10
TT
5262 {}
5263
927aa2e7
JK
5264 /* Return the next matching CU or NULL if there are no more. */
5265 dwarf2_per_cu_data *next ();
5266
5267private:
5268 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5269 const char *name,
5270 dwarf2_per_objfile *per_objfile);
927aa2e7 5271 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5272 uint32_t namei,
5273 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5274
5275 /* The internalized form of .debug_names. */
5276 const mapped_debug_names &m_map;
5277
2b79f376
SM
5278 /* If set, only look for symbols that match that block. Valid values are
5279 GLOBAL_BLOCK and STATIC_BLOCK. */
5280 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5281
5282 /* The kind of symbol we're looking for. */
5283 const domain_enum m_domain = UNDEF_DOMAIN;
5284 const search_domain m_search = ALL_DOMAIN;
5285
5286 /* The list of CUs from the index entry of the symbol, or NULL if
5287 not found. */
5288 const gdb_byte *m_addr;
fcf23d5b
SM
5289
5290 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5291};
5292
5293const char *
fcf23d5b 5294mapped_debug_names::namei_to_name
976ca316 5295 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5296{
5297 const ULONGEST namei_string_offs
5298 = extract_unsigned_integer ((name_table_string_offs_reordered
5299 + namei * offset_size),
5300 offset_size,
5301 dwarf5_byte_order);
976ca316 5302 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5303}
5304
5305/* Find a slot in .debug_names for the object named NAME. If NAME is
5306 found, return pointer to its pool data. If NAME cannot be found,
5307 return NULL. */
5308
5309const gdb_byte *
5310dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5311 (const mapped_debug_names &map, const char *name,
5312 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5313{
5314 int (*cmp) (const char *, const char *);
5315
54ee4252 5316 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5317 if (current_language->la_language == language_cplus
5318 || current_language->la_language == language_fortran
5319 || current_language->la_language == language_d)
5320 {
5321 /* NAME is already canonical. Drop any qualifiers as
5322 .debug_names does not contain any. */
5323
5324 if (strchr (name, '(') != NULL)
5325 {
54ee4252 5326 without_params = cp_remove_params (name);
927aa2e7 5327 if (without_params != NULL)
54ee4252 5328 name = without_params.get ();
927aa2e7
JK
5329 }
5330 }
5331
5332 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5333
5334 const uint32_t full_hash = dwarf5_djb_hash (name);
5335 uint32_t namei
5336 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5337 (map.bucket_table_reordered
5338 + (full_hash % map.bucket_count)), 4,
5339 map.dwarf5_byte_order);
5340 if (namei == 0)
5341 return NULL;
5342 --namei;
5343 if (namei >= map.name_count)
5344 {
b98664d3 5345 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5346 "[in module %s]"),
5347 namei, map.name_count,
fcf23d5b 5348 objfile_name (per_objfile->objfile));
927aa2e7
JK
5349 return NULL;
5350 }
5351
5352 for (;;)
5353 {
5354 const uint32_t namei_full_hash
5355 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5356 (map.hash_table_reordered + namei), 4,
5357 map.dwarf5_byte_order);
5358 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5359 return NULL;
5360
5361 if (full_hash == namei_full_hash)
5362 {
fcf23d5b 5363 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5364
5365#if 0 /* An expensive sanity check. */
5366 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5367 {
b98664d3 5368 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5369 "[in module %s]"),
5370 namei, objfile_name (dwarf2_per_objfile->objfile));
5371 return NULL;
5372 }
5373#endif
5374
5375 if (cmp (namei_string, name) == 0)
5376 {
5377 const ULONGEST namei_entry_offs
5378 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5379 + namei * map.offset_size),
5380 map.offset_size, map.dwarf5_byte_order);
5381 return map.entry_pool + namei_entry_offs;
5382 }
5383 }
5384
5385 ++namei;
5386 if (namei >= map.name_count)
5387 return NULL;
5388 }
5389}
5390
5391const gdb_byte *
5392dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5393 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5394{
5395 if (namei >= map.name_count)
5396 {
b98664d3 5397 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5398 "[in module %s]"),
5399 namei, map.name_count,
fcf23d5b 5400 objfile_name (per_objfile->objfile));
927aa2e7
JK
5401 return NULL;
5402 }
5403
5404 const ULONGEST namei_entry_offs
5405 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5406 + namei * map.offset_size),
5407 map.offset_size, map.dwarf5_byte_order);
5408 return map.entry_pool + namei_entry_offs;
5409}
5410
5411/* See dw2_debug_names_iterator. */
5412
5413dwarf2_per_cu_data *
5414dw2_debug_names_iterator::next ()
5415{
5416 if (m_addr == NULL)
5417 return NULL;
5418
fcf23d5b
SM
5419 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5420 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5421 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5422
5423 again:
5424
5425 unsigned int bytes_read;
5426 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5427 m_addr += bytes_read;
5428 if (abbrev == 0)
5429 return NULL;
5430
5431 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5432 if (indexval_it == m_map.abbrev_map.cend ())
5433 {
b98664d3 5434 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5435 "[in module %s]"),
ed2dc618 5436 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5437 return NULL;
5438 }
5439 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5440 enum class symbol_linkage {
5441 unknown,
5442 static_,
5443 extern_,
23c13d42 5444 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5445 dwarf2_per_cu_data *per_cu = NULL;
5446 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5447 {
5448 ULONGEST ull;
5449 switch (attr.form)
5450 {
5451 case DW_FORM_implicit_const:
5452 ull = attr.implicit_const;
5453 break;
5454 case DW_FORM_flag_present:
5455 ull = 1;
5456 break;
5457 case DW_FORM_udata:
5458 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5459 m_addr += bytes_read;
5460 break;
6dc55ce9 5461 case DW_FORM_ref4:
5462 ull = read_4_bytes (abfd, m_addr);
5463 m_addr += 4;
5464 break;
5465 case DW_FORM_ref8:
5466 ull = read_8_bytes (abfd, m_addr);
5467 m_addr += 8;
5468 break;
5469 case DW_FORM_ref_sig8:
5470 ull = read_8_bytes (abfd, m_addr);
5471 m_addr += 8;
5472 break;
927aa2e7 5473 default:
b98664d3 5474 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5475 dwarf_form_name (attr.form),
ed2dc618 5476 objfile_name (objfile));
927aa2e7
JK
5477 return NULL;
5478 }
5479 switch (attr.dw_idx)
5480 {
5481 case DW_IDX_compile_unit:
5482 /* Don't crash on bad data. */
fcf23d5b 5483 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5484 {
b98664d3 5485 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5486 " [in module %s]"),
5487 pulongest (ull),
fcf23d5b 5488 objfile_name (objfile));
927aa2e7
JK
5489 continue;
5490 }
fcf23d5b 5491 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5492 break;
8af5c486
JK
5493 case DW_IDX_type_unit:
5494 /* Don't crash on bad data. */
fcf23d5b 5495 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5496 {
b98664d3 5497 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5498 " [in module %s]"),
5499 pulongest (ull),
fcf23d5b 5500 objfile_name (objfile));
8af5c486
JK
5501 continue;
5502 }
fcf23d5b 5503 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5504 break;
6dc55ce9 5505 case DW_IDX_die_offset:
5506 /* In a per-CU index (as opposed to a per-module index), index
5507 entries without CU attribute implicitly refer to the single CU. */
5508 if (per_cu == NULL)
fcf23d5b 5509 per_cu = per_bfd->get_cu (0);
6dc55ce9 5510 break;
927aa2e7
JK
5511 case DW_IDX_GNU_internal:
5512 if (!m_map.augmentation_is_gdb)
5513 break;
23c13d42 5514 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5515 break;
5516 case DW_IDX_GNU_external:
5517 if (!m_map.augmentation_is_gdb)
5518 break;
23c13d42 5519 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5520 break;
5521 }
5522 }
5523
5524 /* Skip if already read in. */
fcf23d5b 5525 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5526 goto again;
5527
5528 /* Check static vs global. */
23c13d42 5529 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5530 {
2b79f376 5531 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5532 const bool symbol_is_static =
5533 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5534 if (want_static != symbol_is_static)
2b79f376 5535 goto again;
927aa2e7
JK
5536 }
5537
5538 /* Match dw2_symtab_iter_next, symbol_kind
5539 and debug_names::psymbol_tag. */
5540 switch (m_domain)
5541 {
5542 case VAR_DOMAIN:
5543 switch (indexval.dwarf_tag)
5544 {
5545 case DW_TAG_variable:
5546 case DW_TAG_subprogram:
5547 /* Some types are also in VAR_DOMAIN. */
5548 case DW_TAG_typedef:
5549 case DW_TAG_structure_type:
5550 break;
5551 default:
5552 goto again;
5553 }
5554 break;
5555 case STRUCT_DOMAIN:
5556 switch (indexval.dwarf_tag)
5557 {
5558 case DW_TAG_typedef:
5559 case DW_TAG_structure_type:
5560 break;
5561 default:
5562 goto again;
5563 }
5564 break;
5565 case LABEL_DOMAIN:
5566 switch (indexval.dwarf_tag)
5567 {
5568 case 0:
5569 case DW_TAG_variable:
5570 break;
5571 default:
5572 goto again;
5573 }
5574 break;
59c35742
AB
5575 case MODULE_DOMAIN:
5576 switch (indexval.dwarf_tag)
5577 {
5578 case DW_TAG_module:
5579 break;
5580 default:
5581 goto again;
5582 }
5583 break;
927aa2e7
JK
5584 default:
5585 break;
5586 }
5587
5588 /* Match dw2_expand_symtabs_matching, symbol_kind and
5589 debug_names::psymbol_tag. */
5590 switch (m_search)
4b514bc8 5591 {
927aa2e7
JK
5592 case VARIABLES_DOMAIN:
5593 switch (indexval.dwarf_tag)
4b514bc8 5594 {
927aa2e7
JK
5595 case DW_TAG_variable:
5596 break;
5597 default:
5598 goto again;
4b514bc8 5599 }
927aa2e7
JK
5600 break;
5601 case FUNCTIONS_DOMAIN:
5602 switch (indexval.dwarf_tag)
4b514bc8 5603 {
927aa2e7
JK
5604 case DW_TAG_subprogram:
5605 break;
5606 default:
5607 goto again;
4b514bc8 5608 }
927aa2e7
JK
5609 break;
5610 case TYPES_DOMAIN:
5611 switch (indexval.dwarf_tag)
5612 {
5613 case DW_TAG_typedef:
5614 case DW_TAG_structure_type:
5615 break;
5616 default:
5617 goto again;
5618 }
5619 break;
59c35742
AB
5620 case MODULES_DOMAIN:
5621 switch (indexval.dwarf_tag)
5622 {
5623 case DW_TAG_module:
5624 break;
5625 default:
5626 goto again;
5627 }
927aa2e7
JK
5628 default:
5629 break;
4b514bc8 5630 }
927aa2e7
JK
5631
5632 return per_cu;
4b514bc8 5633}
61920122 5634
927aa2e7 5635static struct compunit_symtab *
c7f839cb 5636dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5637 const char *name, domain_enum domain)
4b514bc8 5638{
976ca316 5639 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5640
976ca316 5641 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5642 if (!mapp)
61920122 5643 {
927aa2e7
JK
5644 /* index is NULL if OBJF_READNOW. */
5645 return NULL;
5646 }
5647 const auto &map = *mapp;
9291a0cd 5648
976ca316 5649 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5650
927aa2e7
JK
5651 struct compunit_symtab *stab_best = NULL;
5652 struct dwarf2_per_cu_data *per_cu;
5653 while ((per_cu = iter.next ()) != NULL)
5654 {
5655 struct symbol *sym, *with_opaque = NULL;
97a1449a 5656 compunit_symtab *stab
976ca316 5657 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5658 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5659 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5660
927aa2e7
JK
5661 sym = block_find_symbol (block, name, domain,
5662 block_find_non_opaque_type_preferred,
5663 &with_opaque);
9703b513 5664
927aa2e7
JK
5665 /* Some caution must be observed with overloaded functions and
5666 methods, since the index will not contain any overload
5667 information (but NAME might contain it). */
a3ec0bb1 5668
927aa2e7 5669 if (sym != NULL
987012b8 5670 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5671 return stab;
5672 if (with_opaque != NULL
987012b8 5673 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5674 stab_best = stab;
9703b513 5675
927aa2e7 5676 /* Keep looking through other CUs. */
9703b513
TT
5677 }
5678
927aa2e7 5679 return stab_best;
9703b513
TT
5680}
5681
927aa2e7
JK
5682/* This dumps minimal information about .debug_names. It is called
5683 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5684 uses this to verify that .debug_names has been loaded. */
9291a0cd 5685
927aa2e7
JK
5686static void
5687dw2_debug_names_dump (struct objfile *objfile)
5688{
976ca316 5689 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5690
976ca316 5691 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5692 printf_filtered (".debug_names:");
976ca316 5693 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5694 printf_filtered (" exists\n");
5695 else
5696 printf_filtered (" faked for \"readnow\"\n");
5697 printf_filtered ("\n");
9291a0cd
TT
5698}
5699
9291a0cd 5700static void
927aa2e7
JK
5701dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5702 const char *func_name)
9291a0cd 5703{
976ca316 5704 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5705
976ca316
SM
5706 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5707 if (per_objfile->per_bfd->debug_names_table)
24c79950 5708 {
976ca316 5709 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5710
fcf23d5b 5711 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5712 per_objfile);
24c79950 5713
927aa2e7
JK
5714 struct dwarf2_per_cu_data *per_cu;
5715 while ((per_cu = iter.next ()) != NULL)
976ca316 5716 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5717 }
5718}
24c79950 5719
3b00ef10
TT
5720static void
5721dw2_debug_names_map_matching_symbols
5722 (struct objfile *objfile,
5723 const lookup_name_info &name, domain_enum domain,
5724 int global,
5725 gdb::function_view<symbol_found_callback_ftype> callback,
5726 symbol_compare_ftype *ordered_compare)
5727{
976ca316 5728 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5729
5730 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5731 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5732 return;
5733
976ca316 5734 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5735 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5736
5737 const char *match_name = name.ada ().lookup_name ().c_str ();
5738 auto matcher = [&] (const char *symname)
5739 {
5740 if (ordered_compare == nullptr)
5741 return true;
5742 return ordered_compare (symname, match_name) == 0;
5743 };
5744
5745 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5746 [&] (offset_type namei)
5747 {
5748 /* The name was matched, now expand corresponding CUs that were
5749 marked. */
fcf23d5b 5750 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5751 per_objfile);
3b00ef10
TT
5752
5753 struct dwarf2_per_cu_data *per_cu;
5754 while ((per_cu = iter.next ()) != NULL)
976ca316 5755 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5756 nullptr);
3b00ef10 5757 return true;
976ca316 5758 }, per_objfile);
3b00ef10
TT
5759
5760 /* It's a shame we couldn't do this inside the
5761 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5762 that have already been expanded. Instead, this loop matches what
5763 the psymtab code does. */
976ca316 5764 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5765 {
976ca316 5766 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5767 if (symtab != nullptr)
3b00ef10
TT
5768 {
5769 const struct block *block
af758d11 5770 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5771 if (!iterate_over_symbols_terminated (block, name,
5772 domain, callback))
5773 break;
5774 }
5775 }
5776}
5777
927aa2e7
JK
5778static void
5779dw2_debug_names_expand_symtabs_matching
5780 (struct objfile *objfile,
5781 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5782 const lookup_name_info *lookup_name,
927aa2e7
JK
5783 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5784 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5785 enum search_domain kind)
5786{
976ca316 5787 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5788
927aa2e7 5789 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5790 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5791 return;
9291a0cd 5792
976ca316 5793 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 5794
c1a66c06
TV
5795 if (symbol_matcher == NULL && lookup_name == NULL)
5796 {
976ca316 5797 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5798 {
5799 QUIT;
5800
976ca316
SM
5801 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5802 expansion_notify);
c1a66c06
TV
5803 }
5804 return;
5805 }
5806
976ca316 5807 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 5808
c1a66c06 5809 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5810 symbol_matcher,
5811 kind, [&] (offset_type namei)
927aa2e7 5812 {
927aa2e7
JK
5813 /* The name was matched, now expand corresponding CUs that were
5814 marked. */
976ca316 5815 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 5816
927aa2e7
JK
5817 struct dwarf2_per_cu_data *per_cu;
5818 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
5819 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
5820 expansion_notify);
3b00ef10 5821 return true;
976ca316 5822 }, per_objfile);
9291a0cd
TT
5823}
5824
927aa2e7 5825const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5826{
5827 dw2_has_symbols,
5828 dw2_find_last_source_symtab,
5829 dw2_forget_cached_source_info,
f8eba3c6 5830 dw2_map_symtabs_matching_filename,
927aa2e7 5831 dw2_debug_names_lookup_symbol,
d3214198 5832 NULL,
9291a0cd 5833 dw2_print_stats,
927aa2e7 5834 dw2_debug_names_dump,
927aa2e7 5835 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5836 dw2_expand_all_symtabs,
652a8996 5837 dw2_expand_symtabs_with_fullname,
3b00ef10 5838 dw2_debug_names_map_matching_symbols,
927aa2e7 5839 dw2_debug_names_expand_symtabs_matching,
43f3e411 5840 dw2_find_pc_sect_compunit_symtab,
71a3c369 5841 NULL,
9291a0cd
TT
5842 dw2_map_symbol_filenames
5843};
5844
4485a1c1 5845/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5846 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5847
5848template <typename T>
5849static gdb::array_view<const gdb_byte>
5850get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5851{
5852 dwarf2_section_info *section = &section_owner->gdb_index;
5853
96b79293 5854 if (section->empty ())
4485a1c1
SM
5855 return {};
5856
5857 /* Older elfutils strip versions could keep the section in the main
5858 executable while splitting it for the separate debug info file. */
96b79293 5859 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5860 return {};
5861
96b79293 5862 section->read (obj);
4485a1c1 5863
8bebfcda
PA
5864 /* dwarf2_section_info::size is a bfd_size_type, while
5865 gdb::array_view works with size_t. On 32-bit hosts, with
5866 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5867 is 32-bit. So we need an explicit narrowing conversion here.
5868 This is fine, because it's impossible to allocate or mmap an
5869 array/buffer larger than what size_t can represent. */
5870 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5871}
5872
87d6a7aa
SM
5873/* Lookup the index cache for the contents of the index associated to
5874 DWARF2_OBJ. */
5875
5876static gdb::array_view<const gdb_byte>
5989a64e 5877get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5878{
5879 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5880 if (build_id == nullptr)
5881 return {};
5882
5883 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5884 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5885}
5886
5887/* Same as the above, but for DWZ. */
5888
5889static gdb::array_view<const gdb_byte>
5890get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5891{
5892 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5893 if (build_id == nullptr)
5894 return {};
5895
5896 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5897}
5898
3c0aa29a 5899/* See symfile.h. */
9291a0cd 5900
3c0aa29a
PA
5901bool
5902dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5903{
976ca316
SM
5904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5905 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 5906
850ed749
SM
5907 dwarf_read_debug_printf ("called");
5908
9291a0cd
TT
5909 /* If we're about to read full symbols, don't bother with the
5910 indices. In this case we also don't care if some other debug
5911 format is making psymtabs, because they are all about to be
5912 expanded anyway. */
5913 if ((objfile->flags & OBJF_READNOW))
5914 {
850ed749
SM
5915 dwarf_read_debug_printf ("readnow requested");
5916
17ee85fc
TT
5917 /* When using READNOW, the using_index flag (set below) indicates that
5918 PER_BFD was already initialized, when we loaded some other objfile. */
5919 if (per_bfd->using_index)
5920 {
850ed749 5921 dwarf_read_debug_printf ("using_index already set");
17ee85fc 5922 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5923 per_objfile->resize_symtabs ();
17ee85fc
TT
5924 return true;
5925 }
5926
5927 per_bfd->using_index = 1;
976ca316
SM
5928 create_all_comp_units (per_objfile);
5929 create_all_type_units (per_objfile);
17ee85fc
TT
5930 per_bfd->quick_file_names_table
5931 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 5932 per_objfile->resize_symtabs ();
9291a0cd 5933
17ee85fc
TT
5934 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5935 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 5936 {
17ee85fc 5937 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 5938
17ee85fc 5939 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 5940 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5941 }
5942
5943 /* Return 1 so that gdb sees the "quick" functions. However,
5944 these functions will be no-ops because we will have expanded
5945 all symtabs. */
3c0aa29a
PA
5946 *index_kind = dw_index_kind::GDB_INDEX;
5947 return true;
9291a0cd
TT
5948 }
5949
17ee85fc
TT
5950 /* Was a debug names index already read when we processed an objfile sharing
5951 PER_BFD? */
5952 if (per_bfd->debug_names_table != nullptr)
5953 {
850ed749 5954 dwarf_read_debug_printf ("re-using shared debug names table");
17ee85fc 5955 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 5956 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 5957 per_objfile->resize_symtabs ();
17ee85fc
TT
5958 return true;
5959 }
5960
5961 /* Was a GDB index already read when we processed an objfile sharing
5962 PER_BFD? */
5963 if (per_bfd->index_table != nullptr)
5964 {
850ed749 5965 dwarf_read_debug_printf ("re-using shared index table");
17ee85fc 5966 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 5967 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 5968 per_objfile->resize_symtabs ();
17ee85fc
TT
5969 return true;
5970 }
5971
efb763a5
SM
5972 /* There might already be partial symtabs built for this BFD. This happens
5973 when loading the same binary twice with the index-cache enabled. If so,
5974 don't try to read an index. The objfile / per_objfile initialization will
5975 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5976 code path. */
5977 if (per_bfd->partial_symtabs != nullptr)
850ed749
SM
5978 {
5979 dwarf_read_debug_printf ("re-using shared partial symtabs");
5980 return false;
5981 }
efb763a5 5982
976ca316 5983 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a 5984 {
850ed749 5985 dwarf_read_debug_printf ("found debug names");
3c0aa29a 5986 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 5987 per_objfile->resize_symtabs ();
3c0aa29a
PA
5988 return true;
5989 }
927aa2e7 5990
976ca316 5991 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 5992 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5993 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a 5994 {
850ed749 5995 dwarf_read_debug_printf ("found gdb index from file");
3c0aa29a 5996 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 5997 per_objfile->resize_symtabs ();
3c0aa29a
PA
5998 return true;
5999 }
9291a0cd 6000
87d6a7aa 6001 /* ... otherwise, try to find the index in the index cache. */
976ca316 6002 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6003 get_gdb_index_contents_from_cache,
6004 get_gdb_index_contents_from_cache_dwz))
6005 {
850ed749 6006 dwarf_read_debug_printf ("found gdb index from cache");
87d6a7aa
SM
6007 global_index_cache.hit ();
6008 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6009 per_objfile->resize_symtabs ();
87d6a7aa
SM
6010 return true;
6011 }
6012
6013 global_index_cache.miss ();
3c0aa29a 6014 return false;
9291a0cd
TT
6015}
6016
6017\f
6018
dce234bc
PP
6019/* Build a partial symbol table. */
6020
6021void
f29dff0a 6022dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6023{
976ca316
SM
6024 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6025 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6026
6027 if (per_bfd->partial_symtabs != nullptr)
6028 {
6029 /* Partial symbols were already read, so now we can simply
6030 attach them. */
6031 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6032 per_objfile->resize_symtabs ();
17ee85fc
TT
6033 return;
6034 }
c9bf0622 6035
a70b8144 6036 try
c9bf0622
TT
6037 {
6038 /* This isn't really ideal: all the data we allocate on the
6039 objfile's obstack is still uselessly kept around. However,
6040 freeing it seems unsafe. */
906768f9 6041 psymtab_discarder psymtabs (objfile);
976ca316 6042 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6043 psymtabs.keep ();
87d6a7aa 6044
976ca316 6045 per_objfile->resize_symtabs ();
af758d11 6046
87d6a7aa 6047 /* (maybe) store an index in the cache. */
976ca316 6048 global_index_cache.store (per_objfile);
c9bf0622 6049 }
230d2906 6050 catch (const gdb_exception_error &except)
492d29ea
PA
6051 {
6052 exception_print (gdb_stderr, except);
6053 }
17ee85fc
TT
6054
6055 /* Finish by setting the local reference to partial symtabs, so that
6056 we don't try to read them again if reading another objfile with the same
6057 BFD. If we can't in fact share, this won't make a difference anyway as
6058 the dwarf2_per_bfd object won't be shared. */
6059 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6060}
c906108c 6061
3b80fe9b
DE
6062/* Find the base address of the compilation unit for range lists and
6063 location lists. It will normally be specified by DW_AT_low_pc.
6064 In DWARF-3 draft 4, the base address could be overridden by
6065 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6066 compilation units with discontinuous ranges. */
6067
6068static void
6069dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6070{
6071 struct attribute *attr;
6072
2b24b6e4 6073 cu->base_address.reset ();
3b80fe9b
DE
6074
6075 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6076 if (attr != nullptr)
95f982e5 6077 cu->base_address = attr->as_address ();
3b80fe9b
DE
6078 else
6079 {
6080 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6081 if (attr != nullptr)
95f982e5 6082 cu->base_address = attr->as_address ();
3b80fe9b
DE
6083 }
6084}
6085
36586728
TT
6086/* Helper function that returns the proper abbrev section for
6087 THIS_CU. */
6088
6089static struct dwarf2_section_info *
6090get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6091{
6092 struct dwarf2_section_info *abbrev;
c3699833 6093 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6094
6095 if (this_cu->is_dwz)
a7308ce0 6096 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
36586728 6097 else
c3699833 6098 abbrev = &per_bfd->abbrev;
36586728
TT
6099
6100 return abbrev;
6101}
6102
f4dc4d17
DE
6103/* Fetch the abbreviation table offset from a comp or type unit header. */
6104
6105static sect_offset
976ca316 6106read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6107 struct dwarf2_section_info *section,
9c541725 6108 sect_offset sect_off)
f4dc4d17 6109{
96b79293 6110 bfd *abfd = section->get_bfd_owner ();
d521ce57 6111 const gdb_byte *info_ptr;
ac298888 6112 unsigned int initial_length_size, offset_size;
43988095 6113 uint16_t version;
f4dc4d17 6114
976ca316 6115 section->read (per_objfile->objfile);
9c541725 6116 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6117 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6118 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6119 info_ptr += initial_length_size;
6120
6121 version = read_2_bytes (abfd, info_ptr);
6122 info_ptr += 2;
6123 if (version >= 5)
6124 {
6125 /* Skip unit type and address size. */
6126 info_ptr += 2;
6127 }
6128
24aa364d 6129 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6130}
6131
b83470bf
TT
6132/* A partial symtab that is used only for include files. */
6133struct dwarf2_include_psymtab : public partial_symtab
6134{
6135 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6136 : partial_symtab (filename, objfile)
6137 {
6138 }
6139
6140 void read_symtab (struct objfile *objfile) override
6141 {
194d088f
TV
6142 /* It's an include file, no symbols to read for it.
6143 Everything is in the includer symtab. */
6144
6145 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6146 expansion of the includer psymtab. We use the dependencies[0] field to
6147 model the includer. But if we go the regular route of calling
6148 expand_psymtab here, and having expand_psymtab call expand_dependencies
6149 to expand the includer, we'll only use expand_psymtab on the includer
6150 (making it a non-toplevel psymtab), while if we expand the includer via
6151 another path, we'll use read_symtab (making it a toplevel psymtab).
6152 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6153 psymtab, and trigger read_symtab on the includer here directly. */
6154 includer ()->read_symtab (objfile);
b83470bf
TT
6155 }
6156
6157 void expand_psymtab (struct objfile *objfile) override
6158 {
194d088f
TV
6159 /* This is not called by read_symtab, and should not be called by any
6160 expand_dependencies. */
6161 gdb_assert (false);
b83470bf
TT
6162 }
6163
5717c425 6164 bool readin_p (struct objfile *objfile) const override
b83470bf 6165 {
5717c425 6166 return includer ()->readin_p (objfile);
b83470bf
TT
6167 }
6168
5717c425 6169 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6170 {
6171 return nullptr;
6172 }
6173
6174private:
194d088f
TV
6175 partial_symtab *includer () const
6176 {
6177 /* An include psymtab has exactly one dependency: the psymtab that
6178 includes it. */
6179 gdb_assert (this->number_of_dependencies == 1);
6180 return this->dependencies[0];
6181 }
b83470bf
TT
6182};
6183
aaa75496
JB
6184/* Allocate a new partial symtab for file named NAME and mark this new
6185 partial symtab as being an include of PST. */
6186
6187static void
891813be 6188dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6189 struct objfile *objfile)
aaa75496 6190{
b83470bf 6191 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6192
fbd9ab74 6193 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6194 subpst->dirname = pst->dirname;
fbd9ab74 6195
a9342b62 6196 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6197 subpst->dependencies[0] = pst;
6198 subpst->number_of_dependencies = 1;
aaa75496
JB
6199}
6200
6201/* Read the Line Number Program data and extract the list of files
6202 included by the source file represented by PST. Build an include
d85a05f0 6203 partial symtab for each of these included files. */
aaa75496
JB
6204
6205static void
6206dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6207 struct die_info *die,
891813be 6208 dwarf2_psymtab *pst)
aaa75496 6209{
fff8551c 6210 line_header_up lh;
d85a05f0 6211 struct attribute *attr;
aaa75496 6212
d85a05f0 6213 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6214 if (attr != nullptr && attr->form_is_unsigned ())
6215 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6216 if (lh == NULL)
6217 return; /* No linetable, so no includes. */
6218
79748972
TT
6219 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6220 that we pass in the raw text_low here; that is ok because we're
6221 only decoding the line table to make include partial symtabs, and
6222 so the addresses aren't really used. */
4ae976d1 6223 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6224 pst->raw_text_low (), 1);
aaa75496
JB
6225}
6226
348e048f 6227static hashval_t
52dc124a 6228hash_signatured_type (const void *item)
348e048f 6229{
9a3c8263
SM
6230 const struct signatured_type *sig_type
6231 = (const struct signatured_type *) item;
9a619af0 6232
348e048f 6233 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6234 return sig_type->signature;
348e048f
DE
6235}
6236
6237static int
52dc124a 6238eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6239{
9a3c8263
SM
6240 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6241 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6242
348e048f
DE
6243 return lhs->signature == rhs->signature;
6244}
6245
1fd400ff
TT
6246/* Allocate a hash table for signatured types. */
6247
b0b6a987 6248static htab_up
298e9637 6249allocate_signatured_type_table ()
1fd400ff 6250{
b0b6a987
TT
6251 return htab_up (htab_create_alloc (41,
6252 hash_signatured_type,
6253 eq_signatured_type,
6254 NULL, xcalloc, xfree));
1fd400ff
TT
6255}
6256
d467dd73 6257/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6258
6259static int
d467dd73 6260add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6261{
9a3c8263 6262 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6263 std::vector<signatured_type *> *all_type_units
6264 = (std::vector<signatured_type *> *) datum;
1fd400ff 6265
b2bdb8cf 6266 all_type_units->push_back (sigt);
1fd400ff
TT
6267
6268 return 1;
6269}
6270
78d4d2c5 6271/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6272 and fill them into TYPES_HTAB. It will process only type units,
6273 therefore DW_UT_type. */
c88ee1f0 6274
78d4d2c5 6275static void
976ca316 6276create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6277 struct dwo_file *dwo_file,
b0b6a987 6278 dwarf2_section_info *section, htab_up &types_htab,
43988095 6279 rcuh_kind section_kind)
348e048f 6280{
976ca316 6281 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6282 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6283 bfd *abfd;
6284 const gdb_byte *info_ptr, *end_ptr;
348e048f 6285
4bdcc0c1
DE
6286 abbrev_section = (dwo_file != NULL
6287 ? &dwo_file->sections.abbrev
976ca316 6288 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6289
4800761a 6290 dwarf_read_debug_printf ("Reading %s for %s",
6f738b01
SM
6291 section->get_name (),
6292 abbrev_section->get_file_name ());
09406207 6293
96b79293 6294 section->read (objfile);
78d4d2c5 6295 info_ptr = section->buffer;
348e048f 6296
78d4d2c5
JK
6297 if (info_ptr == NULL)
6298 return;
348e048f 6299
78d4d2c5
JK
6300 /* We can't set abfd until now because the section may be empty or
6301 not present, in which case the bfd is unknown. */
96b79293 6302 abfd = section->get_bfd_owner ();
348e048f 6303
c0ab21c2
TT
6304 /* We don't use cutu_reader here because we don't need to read
6305 any dies: the signature is in the header. */
3019eac3 6306
78d4d2c5
JK
6307 end_ptr = info_ptr + section->size;
6308 while (info_ptr < end_ptr)
6309 {
78d4d2c5
JK
6310 struct signatured_type *sig_type;
6311 struct dwo_unit *dwo_tu;
6312 void **slot;
6313 const gdb_byte *ptr = info_ptr;
6314 struct comp_unit_head header;
6315 unsigned int length;
8b70b953 6316
9c541725 6317 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6318
a49dd8dd
JK
6319 /* Initialize it due to a false compiler warning. */
6320 header.signature = -1;
9c541725 6321 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6322
78d4d2c5
JK
6323 /* We need to read the type's signature in order to build the hash
6324 table, but we don't need anything else just yet. */
348e048f 6325
976ca316 6326 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6327 abbrev_section, ptr, section_kind);
348e048f 6328
4057dfde 6329 length = header.get_length ();
6caca83c 6330
78d4d2c5
JK
6331 /* Skip dummy type units. */
6332 if (ptr >= info_ptr + length
43988095 6333 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6334 || (header.unit_type != DW_UT_type
6335 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6336 {
6337 info_ptr += length;
6338 continue;
6339 }
dee91e82 6340
78d4d2c5
JK
6341 if (types_htab == NULL)
6342 {
6343 if (dwo_file)
298e9637 6344 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6345 else
298e9637 6346 types_htab = allocate_signatured_type_table ();
78d4d2c5 6347 }
8b70b953 6348
78d4d2c5
JK
6349 if (dwo_file)
6350 {
6351 sig_type = NULL;
976ca316 6352 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6353 dwo_tu->dwo_file = dwo_file;
43988095 6354 dwo_tu->signature = header.signature;
9c541725 6355 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6356 dwo_tu->section = section;
9c541725 6357 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6358 dwo_tu->length = length;
6359 }
6360 else
6361 {
6362 /* N.B.: type_offset is not usable if this type uses a DWO file.
6363 The real type_offset is in the DWO file. */
6364 dwo_tu = NULL;
976ca316 6365 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6366 sig_type->signature = header.signature;
9c541725 6367 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6368 sig_type->per_cu.is_debug_types = 1;
6369 sig_type->per_cu.section = section;
9c541725 6370 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6371 sig_type->per_cu.length = length;
6372 }
6373
b0b6a987 6374 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6375 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6376 INSERT);
6377 gdb_assert (slot != NULL);
6378 if (*slot != NULL)
6379 {
9c541725 6380 sect_offset dup_sect_off;
0349ea22 6381
3019eac3
DE
6382 if (dwo_file)
6383 {
78d4d2c5
JK
6384 const struct dwo_unit *dup_tu
6385 = (const struct dwo_unit *) *slot;
6386
9c541725 6387 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6388 }
6389 else
6390 {
78d4d2c5
JK
6391 const struct signatured_type *dup_tu
6392 = (const struct signatured_type *) *slot;
6393
9c541725 6394 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6395 }
8b70b953 6396
b98664d3 6397 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6398 " the entry at offset %s, signature %s"),
6399 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6400 hex_string (header.signature));
78d4d2c5
JK
6401 }
6402 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6403
6f738b01
SM
6404 dwarf_read_debug_printf_v (" offset %s, signature %s",
6405 sect_offset_str (sect_off),
6406 hex_string (header.signature));
3019eac3 6407
78d4d2c5
JK
6408 info_ptr += length;
6409 }
6410}
3019eac3 6411
78d4d2c5
JK
6412/* Create the hash table of all entries in the .debug_types
6413 (or .debug_types.dwo) section(s).
6414 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6415 otherwise it is NULL.
b3c8eb43 6416
78d4d2c5 6417 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6418
78d4d2c5 6419 Note: This function processes DWO files only, not DWP files. */
348e048f 6420
78d4d2c5 6421static void
976ca316 6422create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6423 struct dwo_file *dwo_file,
fd5866f6 6424 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6425 htab_up &types_htab)
78d4d2c5 6426{
fd5866f6 6427 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6428 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6429 rcuh_kind::TYPE);
3019eac3
DE
6430}
6431
6432/* Create the hash table of all entries in the .debug_types section,
6433 and initialize all_type_units.
6434 The result is zero if there is an error (e.g. missing .debug_types section),
6435 otherwise non-zero. */
6436
6437static int
976ca316 6438create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6439{
b0b6a987 6440 htab_up types_htab;
3019eac3 6441
976ca316
SM
6442 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6443 types_htab, rcuh_kind::COMPILE);
6444 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6445 types_htab);
3019eac3
DE
6446 if (types_htab == NULL)
6447 {
976ca316 6448 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6449 return 0;
6450 }
6451
976ca316 6452 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6453
976ca316
SM
6454 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6455 per_objfile->per_bfd->all_type_units.reserve
6456 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6457
976ca316 6458 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6459 add_signatured_type_cu_to_table,
976ca316 6460 &per_objfile->per_bfd->all_type_units);
1fd400ff 6461
348e048f
DE
6462 return 1;
6463}
6464
5989a64e 6465/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6466 If SLOT is non-NULL, it is the entry to use in the hash table.
6467 Otherwise we find one. */
6468
6469static struct signatured_type *
976ca316 6470add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6471{
976ca316
SM
6472 if (per_objfile->per_bfd->all_type_units.size ()
6473 == per_objfile->per_bfd->all_type_units.capacity ())
6474 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6475
976ca316 6476 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6477
976ca316 6478 per_objfile->resize_symtabs ();
af758d11 6479
976ca316 6480 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6481 sig_type->signature = sig;
6482 sig_type->per_cu.is_debug_types = 1;
976ca316 6483 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6484 {
6485 sig_type->per_cu.v.quick =
976ca316 6486 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6487 struct dwarf2_per_cu_quick_data);
6488 }
6489
6490 if (slot == NULL)
6491 {
976ca316 6492 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6493 sig_type, INSERT);
6494 }
6495 gdb_assert (*slot == NULL);
6496 *slot = sig_type;
6497 /* The rest of sig_type must be filled in by the caller. */
6498 return sig_type;
6499}
6500
a2ce51a0
DE
6501/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6502 Fill in SIG_ENTRY with DWO_ENTRY. */
6503
6504static void
976ca316 6505fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6506 struct signatured_type *sig_entry,
6507 struct dwo_unit *dwo_entry)
6508{
976ca316 6509 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6510
7ee85ab1 6511 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6512 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6513 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6514 if (per_bfd->using_index)
6aa5f3a6
DE
6515 {
6516 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6517 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6518 }
6519 else
6520 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6521 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6522 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6523 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6524 gdb_assert (sig_entry->dwo_unit == NULL);
6525
6526 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6527 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6528 sig_entry->per_cu.length = dwo_entry->length;
6529 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6530 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6531 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6532 sig_entry->dwo_unit = dwo_entry;
6533}
6534
6535/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6536 If we haven't read the TU yet, create the signatured_type data structure
6537 for a TU to be read in directly from a DWO file, bypassing the stub.
6538 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6539 using .gdb_index, then when reading a CU we want to stay in the DWO file
6540 containing that CU. Otherwise we could end up reading several other DWO
6541 files (due to comdat folding) to process the transitive closure of all the
6542 mentioned TUs, and that can be slow. The current DWO file will have every
6543 type signature that it needs.
a2ce51a0
DE
6544 We only do this for .gdb_index because in the psymtab case we already have
6545 to read all the DWOs to build the type unit groups. */
6546
6547static struct signatured_type *
6548lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6549{
976ca316 6550 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6551 struct dwo_file *dwo_file;
6552 struct dwo_unit find_dwo_entry, *dwo_entry;
6553 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6554 void **slot;
a2ce51a0 6555
976ca316 6556 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6557
6aa5f3a6
DE
6558 /* If TU skeletons have been removed then we may not have read in any
6559 TUs yet. */
976ca316
SM
6560 if (per_objfile->per_bfd->signatured_types == NULL)
6561 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6562
6563 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6564 Use the global signatured_types array to do our own comdat-folding
6565 of types. If this is the first time we're reading this TU, and
6566 the TU has an entry in .gdb_index, replace the recorded data from
6567 .gdb_index with this TU. */
a2ce51a0 6568
a2ce51a0 6569 find_sig_entry.signature = sig;
976ca316 6570 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6571 &find_sig_entry, INSERT);
9a3c8263 6572 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6573
6574 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6575 read. Don't reassign the global entry to point to this DWO if that's
6576 the case. Also note that if the TU is already being read, it may not
6577 have come from a DWO, the program may be a mix of Fission-compiled
6578 code and non-Fission-compiled code. */
6579
6580 /* Have we already tried to read this TU?
6581 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6582 needn't exist in the global table yet). */
6583 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6584 return sig_entry;
6585
6aa5f3a6
DE
6586 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6587 dwo_unit of the TU itself. */
6588 dwo_file = cu->dwo_unit->dwo_file;
6589
a2ce51a0
DE
6590 /* Ok, this is the first time we're reading this TU. */
6591 if (dwo_file->tus == NULL)
6592 return NULL;
6593 find_dwo_entry.signature = sig;
b0b6a987
TT
6594 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6595 &find_dwo_entry);
a2ce51a0
DE
6596 if (dwo_entry == NULL)
6597 return NULL;
6598
6aa5f3a6
DE
6599 /* If the global table doesn't have an entry for this TU, add one. */
6600 if (sig_entry == NULL)
976ca316 6601 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6602
976ca316 6603 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6604 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6605 return sig_entry;
6606}
6607
a2ce51a0
DE
6608/* Subroutine of lookup_signatured_type.
6609 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6610 then try the DWP file. If the TU stub (skeleton) has been removed then
6611 it won't be in .gdb_index. */
a2ce51a0
DE
6612
6613static struct signatured_type *
6614lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6615{
976ca316
SM
6616 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6617 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6618 struct dwo_unit *dwo_entry;
6619 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6620 void **slot;
a2ce51a0 6621
976ca316 6622 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6623 gdb_assert (dwp_file != NULL);
6624
6aa5f3a6
DE
6625 /* If TU skeletons have been removed then we may not have read in any
6626 TUs yet. */
976ca316
SM
6627 if (per_objfile->per_bfd->signatured_types == NULL)
6628 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6629
6aa5f3a6 6630 find_sig_entry.signature = sig;
976ca316 6631 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6632 &find_sig_entry, INSERT);
9a3c8263 6633 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6634
6635 /* Have we already tried to read this TU?
6636 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6637 needn't exist in the global table yet). */
6638 if (sig_entry != NULL)
6639 return sig_entry;
6640
a2ce51a0
DE
6641 if (dwp_file->tus == NULL)
6642 return NULL;
976ca316
SM
6643 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6644 1 /* is_debug_types */);
a2ce51a0
DE
6645 if (dwo_entry == NULL)
6646 return NULL;
6647
976ca316
SM
6648 sig_entry = add_type_unit (per_objfile, sig, slot);
6649 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6650
a2ce51a0
DE
6651 return sig_entry;
6652}
6653
380bca97 6654/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6655 Returns NULL if signature SIG is not present in the table.
6656 It is up to the caller to complain about this. */
348e048f
DE
6657
6658static struct signatured_type *
a2ce51a0 6659lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6660{
976ca316 6661 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6662
976ca316 6663 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6664 {
6665 /* We're in a DWO/DWP file, and we're using .gdb_index.
6666 These cases require special processing. */
976ca316 6667 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6668 return lookup_dwo_signatured_type (cu, sig);
6669 else
6670 return lookup_dwp_signatured_type (cu, sig);
6671 }
6672 else
6673 {
6674 struct signatured_type find_entry, *entry;
348e048f 6675
976ca316 6676 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6677 return NULL;
6678 find_entry.signature = sig;
9a3c8263 6679 entry = ((struct signatured_type *)
976ca316 6680 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6681 &find_entry));
a2ce51a0
DE
6682 return entry;
6683 }
348e048f 6684}
18a8505e 6685
42e7ad6c 6686/* Low level DIE reading support. */
348e048f 6687
d85a05f0
DJ
6688/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6689
6690static void
6691init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6692 struct dwarf2_cu *cu,
3019eac3 6693 struct dwarf2_section_info *section,
685af9cd
TT
6694 struct dwo_file *dwo_file,
6695 struct abbrev_table *abbrev_table)
d85a05f0 6696{
fceca515 6697 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6698 reader->abfd = section->get_bfd_owner ();
d85a05f0 6699 reader->cu = cu;
3019eac3 6700 reader->dwo_file = dwo_file;
dee91e82
DE
6701 reader->die_section = section;
6702 reader->buffer = section->buffer;
f664829e 6703 reader->buffer_end = section->buffer + section->size;
685af9cd 6704 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6705}
6706
c0ab21c2 6707/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6708 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6709 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6710 already.
6711
6712 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6713 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6714 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6715 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6716 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6717 STUB_COMP_DIR may be non-NULL.
3e225074 6718 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6719 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6720 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6721 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6722 kept around for at least as long as *RESULT_READER.
6723
b0c7bfa9
DE
6724 The result is non-zero if a valid (non-dummy) DIE was found. */
6725
6726static int
4ab09049 6727read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6728 struct dwo_unit *dwo_unit,
b0c7bfa9 6729 struct die_info *stub_comp_unit_die,
a2ce51a0 6730 const char *stub_comp_dir,
b0c7bfa9 6731 struct die_reader_specs *result_reader,
d521ce57 6732 const gdb_byte **result_info_ptr,
b0c7bfa9 6733 struct die_info **result_comp_unit_die,
685af9cd 6734 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6735{
976ca316 6736 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6737 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6738 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6739 bfd *abfd;
d521ce57 6740 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6741 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6742 int i,num_extra_attrs;
6743 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6744 struct die_info *comp_unit_die;
6745
b0aeadb3
DE
6746 /* At most one of these may be provided. */
6747 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6748
b0c7bfa9
DE
6749 /* These attributes aren't processed until later:
6750 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6751 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6752 referenced later. However, these attributes are found in the stub
6753 which we won't have later. In order to not impose this complication
6754 on the rest of the code, we read them here and copy them to the
6755 DWO CU/TU die. */
b0c7bfa9
DE
6756
6757 stmt_list = NULL;
6758 low_pc = NULL;
6759 high_pc = NULL;
6760 ranges = NULL;
6761 comp_dir = NULL;
6762
6763 if (stub_comp_unit_die != NULL)
6764 {
6765 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6766 DWO file. */
4ab09049 6767 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6768 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6769 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6770 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6771 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6772 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6773
a39fdb41 6774 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6775
2b0c7f41
SM
6776 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6777 We need the value before we can process DW_AT_ranges values from the
6778 DWO. */
6779 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6780
6781 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6782 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6783 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6784 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6785 section. */
6786 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
b0c7bfa9 6787 }
a2ce51a0
DE
6788 else if (stub_comp_dir != NULL)
6789 {
6790 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6791 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6792 comp_dir->name = DW_AT_comp_dir;
6793 comp_dir->form = DW_FORM_string;
c6481205 6794 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6795 }
b0c7bfa9
DE
6796
6797 /* Set up for reading the DWO CU/TU. */
6798 cu->dwo_unit = dwo_unit;
685af9cd 6799 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6800 section->read (objfile);
6801 abfd = section->get_bfd_owner ();
9c541725
PA
6802 begin_info_ptr = info_ptr = (section->buffer
6803 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6804 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6805
4ab09049 6806 if (per_cu->is_debug_types)
b0c7bfa9 6807 {
4ab09049 6808 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6809
976ca316
SM
6810 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6811 section, dwo_abbrev_section,
43988095 6812 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6813 /* This is not an assert because it can be caused by bad debug info. */
43988095 6814 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6815 {
6816 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6817 " TU at offset %s [in module %s]"),
a2ce51a0 6818 hex_string (sig_type->signature),
43988095 6819 hex_string (cu->header.signature),
9d8780f0 6820 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6821 bfd_get_filename (abfd));
6822 }
9c541725 6823 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6824 /* For DWOs coming from DWP files, we don't know the CU length
6825 nor the type's offset in the TU until now. */
4057dfde 6826 dwo_unit->length = cu->header.get_length ();
9c541725 6827 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6828
6829 /* Establish the type offset that can be used to lookup the type.
6830 For DWO files, we don't know it until now. */
9c541725
PA
6831 sig_type->type_offset_in_section
6832 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6833 }
6834 else
6835 {
976ca316
SM
6836 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6837 section, dwo_abbrev_section,
43988095 6838 info_ptr, rcuh_kind::COMPILE);
9c541725 6839 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6840 /* For DWOs coming from DWP files, we don't know the CU length
6841 until now. */
4057dfde 6842 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6843 }
6844
606decb2 6845 dwo_abbrev_section->read (objfile);
685af9cd 6846 *result_dwo_abbrev_table
606decb2 6847 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
6848 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6849 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6850
6851 /* Read in the die, but leave space to copy over the attributes
6852 from the stub. This has the benefit of simplifying the rest of
6853 the code - all the work to maintain the illusion of a single
6854 DW_TAG_{compile,type}_unit DIE is done here. */
6855 num_extra_attrs = ((stmt_list != NULL)
6856 + (low_pc != NULL)
6857 + (high_pc != NULL)
6858 + (ranges != NULL)
6859 + (comp_dir != NULL));
6860 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6861 num_extra_attrs);
b0c7bfa9
DE
6862
6863 /* Copy over the attributes from the stub to the DIE we just read in. */
6864 comp_unit_die = *result_comp_unit_die;
6865 i = comp_unit_die->num_attrs;
6866 if (stmt_list != NULL)
6867 comp_unit_die->attrs[i++] = *stmt_list;
6868 if (low_pc != NULL)
6869 comp_unit_die->attrs[i++] = *low_pc;
6870 if (high_pc != NULL)
6871 comp_unit_die->attrs[i++] = *high_pc;
6872 if (ranges != NULL)
6873 comp_unit_die->attrs[i++] = *ranges;
6874 if (comp_dir != NULL)
6875 comp_unit_die->attrs[i++] = *comp_dir;
6876 comp_unit_die->num_attrs += num_extra_attrs;
6877
b4f54984 6878 if (dwarf_die_debug)
bf6af496
DE
6879 {
6880 fprintf_unfiltered (gdb_stdlog,
6881 "Read die from %s@0x%x of %s:\n",
96b79293 6882 section->get_name (),
bf6af496
DE
6883 (unsigned) (begin_info_ptr - section->buffer),
6884 bfd_get_filename (abfd));
b4f54984 6885 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6886 }
6887
b0c7bfa9
DE
6888 /* Skip dummy compilation units. */
6889 if (info_ptr >= begin_info_ptr + dwo_unit->length
6890 || peek_abbrev_code (abfd, info_ptr) == 0)
6891 return 0;
6892
6893 *result_info_ptr = info_ptr;
6894 return 1;
6895}
6896
a084a2a6
AT
6897/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6898 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6899 signature is part of the header. */
6900static gdb::optional<ULONGEST>
6901lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6902{
6903 if (cu->header.version >= 5)
6904 return cu->header.signature;
6905 struct attribute *attr;
6906 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 6907 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 6908 return gdb::optional<ULONGEST> ();
529908cb 6909 return attr->as_unsigned ();
a084a2a6
AT
6910}
6911
c0ab21c2 6912/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6913 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6914 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6915
6916static struct dwo_unit *
4ab09049 6917lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6918{
4ab09049 6919 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6920 struct dwo_unit *dwo_unit;
c0ab21c2 6921 const char *comp_dir;
b0c7bfa9 6922
a2ce51a0
DE
6923 gdb_assert (cu != NULL);
6924
b0c7bfa9 6925 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6926 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6927 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6928
4ab09049
SM
6929 if (per_cu->is_debug_types)
6930 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6931 else
6932 {
a084a2a6 6933 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6934
a084a2a6 6935 if (!signature.has_value ())
b0c7bfa9
DE
6936 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6937 " [in module %s]"),
4ab09049
SM
6938 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6939
6940 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6941 }
6942
b0c7bfa9
DE
6943 return dwo_unit;
6944}
6945
c0ab21c2 6946/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6947 See it for a description of the parameters.
fcd3b13d 6948 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6949
c0ab21c2 6950void
9e021579
SM
6951cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6952 dwarf2_per_objfile *per_objfile,
2e671100 6953 dwarf2_cu *existing_cu)
a2ce51a0 6954{
a2ce51a0 6955 struct signatured_type *sig_type;
a2ce51a0
DE
6956
6957 /* Verify we can do the following downcast, and that we have the
6958 data we need. */
6959 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6960 sig_type = (struct signatured_type *) this_cu;
6961 gdb_assert (sig_type->dwo_unit != NULL);
6962
2e671100
SM
6963 dwarf2_cu *cu;
6964
6965 if (existing_cu != nullptr)
6aa5f3a6 6966 {
2e671100
SM
6967 cu = existing_cu;
6968 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6969 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6970 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6971 }
6972 else
6973 {
7188ed02 6974 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 6975 in per_objfile yet. */
7188ed02 6976 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6977 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6978 cu = m_new_cu.get ();
6aa5f3a6
DE
6979 }
6980
6981 /* A future optimization, if needed, would be to use an existing
6982 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6983 could share abbrev tables. */
a2ce51a0 6984
2e671100 6985 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6986 NULL /* stub_comp_unit_die */,
6987 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6988 this, &info_ptr,
3e225074 6989 &comp_unit_die,
c0ab21c2 6990 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6991 {
6992 /* Dummy die. */
c0ab21c2 6993 dummy_p = true;
a2ce51a0 6994 }
a2ce51a0
DE
6995}
6996
fd820528 6997/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6998 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6999
f4dc4d17
DE
7000 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7001 Otherwise the table specified in the comp unit header is read in and used.
7002 This is an optimization for when we already have the abbrev table.
7003
2e671100
SM
7004 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7005 allocated. */
aaa75496 7006
ab432490 7007cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7008 dwarf2_per_objfile *per_objfile,
c0ab21c2 7009 struct abbrev_table *abbrev_table,
2e671100 7010 dwarf2_cu *existing_cu,
c0ab21c2
TT
7011 bool skip_partial)
7012 : die_reader_specs {},
6751ebae 7013 m_this_cu (this_cu)
c906108c 7014{
976ca316 7015 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7016 struct dwarf2_section_info *section = this_cu->section;
96b79293 7017 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7018 const gdb_byte *begin_info_ptr;
dee91e82 7019 struct signatured_type *sig_type = NULL;
4bdcc0c1 7020 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7021 /* Non-zero if CU currently points to a DWO file and we need to
7022 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7023 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7024 int rereading_dwo_cu = 0;
c906108c 7025
b4f54984 7026 if (dwarf_die_debug)
9d8780f0 7027 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7028 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7029 sect_offset_str (this_cu->sect_off));
09406207 7030
a2ce51a0
DE
7031 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7032 file (instead of going through the stub), short-circuit all of this. */
7033 if (this_cu->reading_dwo_directly)
7034 {
7035 /* Narrow down the scope of possibilities to have to understand. */
7036 gdb_assert (this_cu->is_debug_types);
7037 gdb_assert (abbrev_table == NULL);
976ca316 7038 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7039 return;
7040 }
7041
dee91e82 7042 /* This is cheap if the section is already read in. */
96b79293 7043 section->read (objfile);
dee91e82 7044
9c541725 7045 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7046
7047 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7048
2e671100
SM
7049 dwarf2_cu *cu;
7050
7051 if (existing_cu != nullptr)
dee91e82 7052 {
2e671100 7053 cu = existing_cu;
42e7ad6c
DE
7054 /* If this CU is from a DWO file we need to start over, we need to
7055 refetch the attributes from the skeleton CU.
7056 This could be optimized by retrieving those attributes from when we
7057 were here the first time: the previous comp_unit_die was stored in
7058 comp_unit_obstack. But there's no data yet that we need this
7059 optimization. */
7060 if (cu->dwo_unit != NULL)
7061 rereading_dwo_cu = 1;
dee91e82
DE
7062 }
7063 else
7064 {
7188ed02 7065 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7066 in per_objfile yet. */
976ca316
SM
7067 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7068 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7069 cu = m_new_cu.get ();
42e7ad6c 7070 }
dee91e82 7071
b0c7bfa9 7072 /* Get the header. */
9c541725 7073 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7074 {
7075 /* We already have the header, there's no need to read it in again. */
9c541725 7076 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7077 }
7078 else
7079 {
3019eac3 7080 if (this_cu->is_debug_types)
dee91e82 7081 {
976ca316
SM
7082 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7083 section, abbrev_section,
7084 info_ptr, rcuh_kind::TYPE);
dee91e82 7085
42e7ad6c
DE
7086 /* Since per_cu is the first member of struct signatured_type,
7087 we can go from a pointer to one to a pointer to the other. */
7088 sig_type = (struct signatured_type *) this_cu;
43988095 7089 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7090 gdb_assert (sig_type->type_offset_in_tu
7091 == cu->header.type_cu_offset_in_tu);
7092 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7093
42e7ad6c
DE
7094 /* LENGTH has not been set yet for type units if we're
7095 using .gdb_index. */
4057dfde 7096 this_cu->length = cu->header.get_length ();
3019eac3
DE
7097
7098 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7099 sig_type->type_offset_in_section =
7100 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7101
7102 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7103 }
7104 else
7105 {
976ca316
SM
7106 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7107 section, abbrev_section,
43988095
JK
7108 info_ptr,
7109 rcuh_kind::COMPILE);
dee91e82 7110
9c541725 7111 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7112 if (this_cu->length == 0)
7113 this_cu->length = cu->header.get_length ();
7114 else
7115 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7116 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7117 }
7118 }
10b3939b 7119
6caca83c 7120 /* Skip dummy compilation units. */
dee91e82 7121 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7122 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7123 {
7124 dummy_p = true;
7125 return;
7126 }
6caca83c 7127
433df2d4
DE
7128 /* If we don't have them yet, read the abbrevs for this compilation unit.
7129 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7130 done. */
f4dc4d17 7131 if (abbrev_table != NULL)
685af9cd
TT
7132 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7133 else
f4dc4d17 7134 {
606decb2 7135 abbrev_section->read (objfile);
c0ab21c2 7136 m_abbrev_table_holder
606decb2 7137 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7138 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7139 }
af703f96 7140
dee91e82 7141 /* Read the top level CU/TU die. */
c0ab21c2 7142 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7143 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7144
58f0c718 7145 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7146 {
7147 dummy_p = true;
7148 return;
7149 }
58f0c718 7150
b0c7bfa9 7151 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7152 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7153 table from the DWO file and pass the ownership over to us. It will be
7154 referenced from READER, so we must make sure to free it after we're done
7155 with READER.
7156
b0c7bfa9
DE
7157 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7158 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7159 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7160 if (dwo_name != nullptr)
3019eac3 7161 {
3019eac3 7162 struct dwo_unit *dwo_unit;
b0c7bfa9 7163 struct die_info *dwo_comp_unit_die;
3019eac3 7164
3e225074 7165 if (comp_unit_die->has_children)
6a506a2d 7166 {
b98664d3 7167 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7168 " has children (offset %s) [in module %s]"),
7169 sect_offset_str (this_cu->sect_off),
7170 bfd_get_filename (abfd));
6a506a2d 7171 }
4ab09049 7172 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7173 if (dwo_unit != NULL)
3019eac3 7174 {
4ab09049 7175 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7176 comp_unit_die, NULL,
c0ab21c2 7177 this, &info_ptr,
3e225074 7178 &dwo_comp_unit_die,
c0ab21c2 7179 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7180 {
7181 /* Dummy die. */
c0ab21c2 7182 dummy_p = true;
6a506a2d
DE
7183 return;
7184 }
7185 comp_unit_die = dwo_comp_unit_die;
7186 }
7187 else
7188 {
7189 /* Yikes, we couldn't find the rest of the DIE, we only have
7190 the stub. A complaint has already been logged. There's
7191 not much more we can do except pass on the stub DIE to
7192 die_reader_func. We don't want to throw an error on bad
7193 debug info. */
3019eac3
DE
7194 }
7195 }
c0ab21c2 7196}
3019eac3 7197
6751ebae
TT
7198void
7199cutu_reader::keep ()
c0ab21c2 7200{
b0c7bfa9 7201 /* Done, clean up. */
6751ebae
TT
7202 gdb_assert (!dummy_p);
7203 if (m_new_cu != NULL)
348e048f 7204 {
7188ed02 7205 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7206 now. */
7188ed02
SM
7207 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7208 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7209 }
dee91e82
DE
7210}
7211
18a8505e
AT
7212/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7213 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7214 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7215
7216 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7217 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7218
7219 We fill in THIS_CU->length.
7220
dee91e82 7221 THIS_CU->cu is always freed when done.
3019eac3 7222 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7223 to care whether it refers to the "main" CU or the DWO CU.
7224
7225 When parent_cu is passed, it is used to provide a default value for
7226 str_offsets_base and addr_base from the parent. */
dee91e82 7227
ab432490 7228cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7229 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7230 struct dwarf2_cu *parent_cu,
7231 struct dwo_file *dwo_file)
7232 : die_reader_specs {},
7233 m_this_cu (this_cu)
dee91e82 7234{
976ca316 7235 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7236 struct dwarf2_section_info *section = this_cu->section;
96b79293 7237 bfd *abfd = section->get_bfd_owner ();
33e80786 7238 struct dwarf2_section_info *abbrev_section;
d521ce57 7239 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7240
b4f54984 7241 if (dwarf_die_debug)
9d8780f0 7242 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7243 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7244 sect_offset_str (this_cu->sect_off));
09406207 7245
976ca316 7246 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7247
33e80786
DE
7248 abbrev_section = (dwo_file != NULL
7249 ? &dwo_file->sections.abbrev
7250 : get_abbrev_section_for_cu (this_cu));
7251
dee91e82 7252 /* This is cheap if the section is already read in. */
96b79293 7253 section->read (objfile);
dee91e82 7254
976ca316 7255 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7256
9c541725 7257 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7258 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7259 section, abbrev_section, info_ptr,
43988095
JK
7260 (this_cu->is_debug_types
7261 ? rcuh_kind::TYPE
7262 : rcuh_kind::COMPILE));
dee91e82 7263
18a8505e
AT
7264 if (parent_cu != nullptr)
7265 {
c0ab21c2
TT
7266 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7267 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7268 }
4057dfde 7269 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7270
7271 /* Skip dummy compilation units. */
7272 if (info_ptr >= begin_info_ptr + this_cu->length
7273 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7274 {
7275 dummy_p = true;
7276 return;
7277 }
72bf9492 7278
606decb2 7279 abbrev_section->read (objfile);
c0ab21c2 7280 m_abbrev_table_holder
606decb2 7281 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7282
c0ab21c2
TT
7283 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7284 m_abbrev_table_holder.get ());
3e225074 7285 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7286}
7287
0018ea6f
DE
7288\f
7289/* Type Unit Groups.
dee91e82 7290
0018ea6f
DE
7291 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7292 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7293 so that all types coming from the same compilation (.o file) are grouped
7294 together. A future step could be to put the types in the same symtab as
7295 the CU the types ultimately came from. */
ff013f42 7296
f4dc4d17
DE
7297static hashval_t
7298hash_type_unit_group (const void *item)
7299{
9a3c8263
SM
7300 const struct type_unit_group *tu_group
7301 = (const struct type_unit_group *) item;
f4dc4d17 7302
094b34ac 7303 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7304}
348e048f
DE
7305
7306static int
f4dc4d17 7307eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7308{
9a3c8263
SM
7309 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7310 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7311
094b34ac 7312 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7313}
348e048f 7314
f4dc4d17
DE
7315/* Allocate a hash table for type unit groups. */
7316
eaa5fa8b 7317static htab_up
298e9637 7318allocate_type_unit_groups_table ()
f4dc4d17 7319{
eaa5fa8b
TT
7320 return htab_up (htab_create_alloc (3,
7321 hash_type_unit_group,
7322 eq_type_unit_group,
7323 NULL, xcalloc, xfree));
f4dc4d17 7324}
dee91e82 7325
f4dc4d17
DE
7326/* Type units that don't have DW_AT_stmt_list are grouped into their own
7327 partial symtabs. We combine several TUs per psymtab to not let the size
7328 of any one psymtab grow too big. */
7329#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7330#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7331
094b34ac 7332/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7333 Create the type_unit_group object used to hold one or more TUs. */
7334
7335static struct type_unit_group *
094b34ac 7336create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7337{
976ca316
SM
7338 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7339 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7340 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7341 struct type_unit_group *tu_group;
f4dc4d17 7342
976ca316 7343 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7344 per_cu = &tu_group->per_cu;
1859c670 7345 per_cu->per_bfd = per_bfd;
f4dc4d17 7346
1859c670 7347 if (per_bfd->using_index)
094b34ac 7348 {
1859c670 7349 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7350 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7351 }
7352 else
7353 {
9c541725 7354 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7355 dwarf2_psymtab *pst;
528e1572 7356 std::string name;
094b34ac
DE
7357
7358 /* Give the symtab a useful name for debug purposes. */
7359 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7360 name = string_printf ("<type_units_%d>",
7361 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7362 else
528e1572 7363 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7364
976ca316 7365 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7366 pst->anonymous = true;
094b34ac 7367 }
f4dc4d17 7368
094b34ac 7369 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7370 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7371
7372 return tu_group;
7373}
7374
094b34ac
DE
7375/* Look up the type_unit_group for type unit CU, and create it if necessary.
7376 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7377
7378static struct type_unit_group *
ff39bb5e 7379get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7380{
976ca316
SM
7381 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7382 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7383 struct type_unit_group *tu_group;
7384 void **slot;
7385 unsigned int line_offset;
7386 struct type_unit_group type_unit_group_for_lookup;
7387
976ca316
SM
7388 if (per_objfile->per_bfd->type_unit_groups == NULL)
7389 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7390
7391 /* Do we need to create a new group, or can we use an existing one? */
7392
529908cb 7393 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7394 {
529908cb 7395 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7396 ++tu_stats->nr_symtab_sharers;
7397 }
7398 else
7399 {
7400 /* Ugh, no stmt_list. Rare, but we have to handle it.
7401 We can do various things here like create one group per TU or
7402 spread them over multiple groups to split up the expansion work.
7403 To avoid worst case scenarios (too many groups or too large groups)
7404 we, umm, group them in bunches. */
7405 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7406 | (tu_stats->nr_stmt_less_type_units
7407 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7408 ++tu_stats->nr_stmt_less_type_units;
7409 }
7410
094b34ac 7411 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7412 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7413 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7414 &type_unit_group_for_lookup, INSERT);
7415 if (*slot != NULL)
7416 {
9a3c8263 7417 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7418 gdb_assert (tu_group != NULL);
7419 }
7420 else
7421 {
9c541725 7422 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7423 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7424 *slot = tu_group;
7425 ++tu_stats->nr_symtabs;
7426 }
7427
7428 return tu_group;
7429}
0018ea6f
DE
7430\f
7431/* Partial symbol tables. */
7432
7433/* Create a psymtab named NAME and assign it to PER_CU.
7434
7435 The caller must fill in the following details:
7436 dirname, textlow, texthigh. */
7437
891813be 7438static dwarf2_psymtab *
7aa104c4
SM
7439create_partial_symtab (dwarf2_per_cu_data *per_cu,
7440 dwarf2_per_objfile *per_objfile,
7441 const char *name)
0018ea6f 7442{
7aa104c4 7443 struct objfile *objfile = per_objfile->objfile;
891813be 7444 dwarf2_psymtab *pst;
0018ea6f 7445
9f4e76a4 7446 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7447
6d94535f 7448 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7449
7450 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7451 per_cu->v.psymtab = pst;
7452
7453 return pst;
7454}
7455
c0ab21c2 7456/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7457
7458static void
7459process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7460 const gdb_byte *info_ptr,
0018ea6f 7461 struct die_info *comp_unit_die,
c0ab21c2 7462 enum language pretend_language)
0018ea6f
DE
7463{
7464 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7466 struct objfile *objfile = per_objfile->objfile;
08feed99 7467 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7468 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7469 CORE_ADDR baseaddr;
7470 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7471 dwarf2_psymtab *pst;
3a2b436a 7472 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7473 const char *filename;
0018ea6f 7474
0018ea6f
DE
7475 gdb_assert (! per_cu->is_debug_types);
7476
c0ab21c2 7477 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7478
0018ea6f 7479 /* Allocate a new partial symbol table structure. */
2e927613
TV
7480 gdb::unique_xmalloc_ptr<char> debug_filename;
7481 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7482 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7483 if (filename == NULL)
0018ea6f 7484 filename = "";
2e927613
TV
7485 else if (strcmp (filename, artificial) == 0)
7486 {
7487 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7488 sect_offset_str (per_cu->sect_off),
7489 (char *) NULL));
2e927613
TV
7490 filename = debug_filename.get ();
7491 }
0018ea6f 7492
7aa104c4 7493 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7494
7495 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7496 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7497
b3b3bada 7498 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7499
7500 dwarf2_find_base_address (comp_unit_die, cu);
7501
7502 /* Possibly set the default values of LOWPC and HIGHPC from
7503 `DW_AT_ranges'. */
3a2b436a
JK
7504 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7505 &best_highpc, cu, pst);
7506 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7507 {
7508 CORE_ADDR low
7509 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7510 - baseaddr);
7511 CORE_ADDR high
7512 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7513 - baseaddr - 1);
7514 /* Store the contiguous range if it is not empty; it can be
7515 empty for CUs with no code. */
d320c2b5
TT
7516 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7517 low, high, pst);
79748972 7518 }
0018ea6f
DE
7519
7520 /* Check if comp unit has_children.
7521 If so, read the rest of the partial symbols from this comp unit.
7522 If not, there's no more debug_info for this comp unit. */
3e225074 7523 if (comp_unit_die->has_children)
0018ea6f
DE
7524 {
7525 struct partial_die_info *first_die;
7526 CORE_ADDR lowpc, highpc;
7527
7528 lowpc = ((CORE_ADDR) -1);
7529 highpc = ((CORE_ADDR) 0);
7530
7531 first_die = load_partial_dies (reader, info_ptr, 1);
7532
7533 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7534 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7535
7536 /* If we didn't find a lowpc, set it to highpc to avoid
7537 complaints from `maint check'. */
7538 if (lowpc == ((CORE_ADDR) -1))
7539 lowpc = highpc;
7540
7541 /* If the compilation unit didn't have an explicit address range,
7542 then use the information extracted from its child dies. */
e385593e 7543 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7544 {
7545 best_lowpc = lowpc;
7546 best_highpc = highpc;
7547 }
7548 }
4ae976d1 7549 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7550 best_lowpc + baseaddr)
7551 - baseaddr);
4ae976d1 7552 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7553 best_highpc + baseaddr)
7554 - baseaddr);
0018ea6f 7555
ae7754b2 7556 pst->end ();
0018ea6f 7557
ae640021 7558 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7559 {
7560 int i;
ae640021 7561 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7562
7563 /* Fill in 'dependencies' here; we fill in 'users' in a
7564 post-pass. */
7565 pst->number_of_dependencies = len;
a9342b62
TT
7566 pst->dependencies
7567 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7568 for (i = 0; i < len; ++i)
7569 {
7570 pst->dependencies[i]
7571 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7572 }
0018ea6f 7573
ae640021 7574 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7575 }
7576
7577 /* Get the list of files included in the current compilation unit,
7578 and build a psymtab for each of them. */
7579 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7580
6f738b01
SM
7581 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7582 ", %d global, %d static syms",
7583 per_cu->is_debug_types ? "type" : "comp",
7584 sect_offset_str (per_cu->sect_off),
7585 paddress (gdbarch, pst->text_low (objfile)),
7586 paddress (gdbarch, pst->text_high (objfile)),
7587 (int) pst->global_psymbols.size (),
7588 (int) pst->static_psymbols.size ());
0018ea6f
DE
7589}
7590
7591/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7592 Process compilation unit THIS_CU for a psymtab. */
7593
7594static void
ab432490
SM
7595process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7596 dwarf2_per_objfile *per_objfile,
135f5437 7597 bool want_partial_unit,
b93601f3 7598 enum language pretend_language)
0018ea6f
DE
7599{
7600 /* If this compilation unit was already read in, free the
7601 cached copy in order to read it in again. This is
7602 necessary because we skipped some symbols when we first
7603 read in the compilation unit (see load_partial_dies).
7604 This problem could be avoided, but the benefit is unclear. */
7188ed02 7605 per_objfile->remove_cu (this_cu);
0018ea6f 7606
2e671100 7607 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7608
58990295
TV
7609 switch (reader.comp_unit_die->tag)
7610 {
7611 case DW_TAG_compile_unit:
7612 this_cu->unit_type = DW_UT_compile;
7613 break;
7614 case DW_TAG_partial_unit:
7615 this_cu->unit_type = DW_UT_partial;
7616 break;
e77b0004
TV
7617 case DW_TAG_type_unit:
7618 this_cu->unit_type = DW_UT_type;
7619 break;
58990295
TV
7620 default:
7621 abort ();
7622 }
7623
c0ab21c2 7624 if (reader.dummy_p)
f1902523 7625 {
c0ab21c2 7626 /* Nothing. */
f1902523 7627 }
c0ab21c2 7628 else if (this_cu->is_debug_types)
3e225074
TT
7629 build_type_psymtabs_reader (&reader, reader.info_ptr,
7630 reader.comp_unit_die);
135f5437
TT
7631 else if (want_partial_unit
7632 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7633 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7634 reader.comp_unit_die,
c0ab21c2 7635 pretend_language);
0018ea6f 7636
7188ed02 7637 this_cu->lang = reader.cu->language;
58990295 7638
0018ea6f 7639 /* Age out any secondary CUs. */
7188ed02 7640 per_objfile->age_comp_units ();
0018ea6f 7641}
f4dc4d17
DE
7642
7643/* Reader function for build_type_psymtabs. */
7644
7645static void
7646build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7647 const gdb_byte *info_ptr,
3e225074 7648 struct die_info *type_unit_die)
f4dc4d17 7649{
976ca316 7650 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7651 struct dwarf2_cu *cu = reader->cu;
7652 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7653 struct signatured_type *sig_type;
f4dc4d17
DE
7654 struct type_unit_group *tu_group;
7655 struct attribute *attr;
7656 struct partial_die_info *first_die;
7657 CORE_ADDR lowpc, highpc;
891813be 7658 dwarf2_psymtab *pst;
f4dc4d17 7659
0186c6a7
DE
7660 gdb_assert (per_cu->is_debug_types);
7661 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7662
3e225074 7663 if (! type_unit_die->has_children)
f4dc4d17
DE
7664 return;
7665
052c8bb8 7666 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7667 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7668
df07e2c7 7669 if (tu_group->tus == nullptr)
a8b3b8e9 7670 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7671 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7672
7673 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7674 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7675 pst->anonymous = true;
f4dc4d17
DE
7676
7677 first_die = load_partial_dies (reader, info_ptr, 1);
7678
7679 lowpc = (CORE_ADDR) -1;
7680 highpc = (CORE_ADDR) 0;
7681 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7682
ae7754b2 7683 pst->end ();
f4dc4d17
DE
7684}
7685
73051182
DE
7686/* Struct used to sort TUs by their abbreviation table offset. */
7687
7688struct tu_abbrev_offset
7689{
b2bdb8cf
SM
7690 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7691 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7692 {}
7693
7694 signatured_type *sig_type;
73051182
DE
7695 sect_offset abbrev_offset;
7696};
7697
484cf504 7698/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7699
484cf504
TT
7700static bool
7701sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7702 const struct tu_abbrev_offset &b)
73051182 7703{
484cf504 7704 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7705}
7706
7707/* Efficiently read all the type units.
7708 This does the bulk of the work for build_type_psymtabs.
7709
7710 The efficiency is because we sort TUs by the abbrev table they use and
7711 only read each abbrev table once. In one program there are 200K TUs
7712 sharing 8K abbrev tables.
7713
7714 The main purpose of this function is to support building the
5989a64e 7715 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7716 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7717 can collapse the search space by grouping them by stmt_list.
7718 The savings can be significant, in the same program from above the 200K TUs
7719 share 8K stmt_list tables.
7720
7721 FUNC is expected to call get_type_unit_group, which will create the
7722 struct type_unit_group if necessary and add it to
5989a64e 7723 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7724
7725static void
976ca316 7726build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7727{
976ca316 7728 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7729 abbrev_table_up abbrev_table;
73051182 7730 sect_offset abbrev_offset;
73051182
DE
7731
7732 /* It's up to the caller to not call us multiple times. */
976ca316 7733 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7734
976ca316 7735 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7736 return;
7737
7738 /* TUs typically share abbrev tables, and there can be way more TUs than
7739 abbrev tables. Sort by abbrev table to reduce the number of times we
7740 read each abbrev table in.
7741 Alternatives are to punt or to maintain a cache of abbrev tables.
7742 This is simpler and efficient enough for now.
7743
7744 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7745 symtab to use). Typically TUs with the same abbrev offset have the same
7746 stmt_list value too so in practice this should work well.
7747
7748 The basic algorithm here is:
7749
7750 sort TUs by abbrev table
7751 for each TU with same abbrev table:
7752 read abbrev table if first user
7753 read TU top level DIE
7754 [IWBN if DWO skeletons had DW_AT_stmt_list]
7755 call FUNC */
7756
6f738b01 7757 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7758
7759 /* Sort in a separate table to maintain the order of all_type_units
7760 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7761 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7762 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7763
976ca316 7764 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7765 sorted_by_abbrev.emplace_back
976ca316 7766 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7767 sig_type->per_cu.sect_off));
73051182 7768
484cf504
TT
7769 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7770 sort_tu_by_abbrev_offset);
73051182 7771
9c541725 7772 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7773
b2bdb8cf 7774 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7775 {
73051182
DE
7776 /* Switch to the next abbrev table if necessary. */
7777 if (abbrev_table == NULL
b2bdb8cf 7778 || tu.abbrev_offset != abbrev_offset)
73051182 7779 {
b2bdb8cf 7780 abbrev_offset = tu.abbrev_offset;
606decb2 7781 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7782 abbrev_table =
606decb2 7783 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7784 ++tu_stats->nr_uniq_abbrev_tables;
7785 }
7786
976ca316 7787 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7788 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7789 if (!reader.dummy_p)
7790 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7791 reader.comp_unit_die);
73051182 7792 }
6aa5f3a6 7793}
73051182 7794
6aa5f3a6
DE
7795/* Print collected type unit statistics. */
7796
7797static void
976ca316 7798print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7799{
976ca316 7800 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7801
6f738b01
SM
7802 dwarf_read_debug_printf ("Type unit statistics:");
7803 dwarf_read_debug_printf (" %zu TUs",
7804 per_objfile->per_bfd->all_type_units.size ());
7805 dwarf_read_debug_printf (" %d uniq abbrev tables",
7806 tu_stats->nr_uniq_abbrev_tables);
7807 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7808 tu_stats->nr_symtabs);
7809 dwarf_read_debug_printf (" %d symtab sharers",
7810 tu_stats->nr_symtab_sharers);
7811 dwarf_read_debug_printf (" %d type units without a stmt_list",
7812 tu_stats->nr_stmt_less_type_units);
7813 dwarf_read_debug_printf (" %d all_type_units reallocs",
7814 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7815}
7816
f4dc4d17
DE
7817/* Traversal function for build_type_psymtabs. */
7818
7819static int
7820build_type_psymtab_dependencies (void **slot, void *info)
7821{
976ca316
SM
7822 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7823 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 7824 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7825 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7826 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7827 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7828 int i;
7829
7830 gdb_assert (len > 0);
197400e8 7831 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7832
7833 pst->number_of_dependencies = len;
a9342b62 7834 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7835 for (i = 0; i < len; ++i)
f4dc4d17 7836 {
df07e2c7 7837 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7838 gdb_assert (iter->per_cu.is_debug_types);
7839 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7840 iter->type_unit_group = tu_group;
f4dc4d17
DE
7841 }
7842
df07e2c7
AB
7843 delete tu_group->tus;
7844 tu_group->tus = nullptr;
348e048f
DE
7845
7846 return 1;
7847}
7848
7849/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7850 Build partial symbol tables for the .debug_types comp-units. */
7851
7852static void
976ca316 7853build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 7854{
976ca316 7855 if (! create_all_type_units (per_objfile))
348e048f
DE
7856 return;
7857
976ca316 7858 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 7859}
f4dc4d17 7860
6aa5f3a6
DE
7861/* Traversal function for process_skeletonless_type_unit.
7862 Read a TU in a DWO file and build partial symbols for it. */
7863
7864static int
7865process_skeletonless_type_unit (void **slot, void *info)
7866{
7867 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 7868 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
7869 struct signatured_type find_entry, *entry;
7870
7871 /* If this TU doesn't exist in the global table, add it and read it in. */
7872
976ca316
SM
7873 if (per_objfile->per_bfd->signatured_types == NULL)
7874 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7875
7876 find_entry.signature = dwo_unit->signature;
976ca316 7877 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7878 &find_entry, INSERT);
6aa5f3a6
DE
7879 /* If we've already seen this type there's nothing to do. What's happening
7880 is we're doing our own version of comdat-folding here. */
7881 if (*slot != NULL)
7882 return 1;
7883
7884 /* This does the job that create_all_type_units would have done for
7885 this TU. */
976ca316
SM
7886 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7887 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7888 *slot = entry;
7889
7890 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 7891 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
7892 if (!reader.dummy_p)
7893 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7894 reader.comp_unit_die);
6aa5f3a6
DE
7895
7896 return 1;
7897}
7898
7899/* Traversal function for process_skeletonless_type_units. */
7900
7901static int
7902process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7903{
7904 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7905
7906 if (dwo_file->tus != NULL)
b0b6a987
TT
7907 htab_traverse_noresize (dwo_file->tus.get (),
7908 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7909
7910 return 1;
7911}
7912
7913/* Scan all TUs of DWO files, verifying we've processed them.
7914 This is needed in case a TU was emitted without its skeleton.
7915 Note: This can't be done until we know what all the DWO files are. */
7916
7917static void
976ca316 7918process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
7919{
7920 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
7921 if (get_dwp_file (per_objfile) == NULL
7922 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7923 {
976ca316 7924 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7925 process_dwo_file_for_skeletonless_type_units,
976ca316 7926 per_objfile);
6aa5f3a6 7927 }
348e048f
DE
7928}
7929
ed2dc618 7930/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7931
7932static void
976ca316 7933set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 7934{
976ca316 7935 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 7936 {
891813be 7937 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7938
36586728
TT
7939 if (pst == NULL)
7940 continue;
7941
b76e467d 7942 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7943 {
7944 /* Set the 'user' field only if it is not already set. */
7945 if (pst->dependencies[j]->user == NULL)
7946 pst->dependencies[j]->user = pst;
7947 }
7948 }
7949}
7950
93311388
DE
7951/* Build the partial symbol table by doing a quick pass through the
7952 .debug_info and .debug_abbrev sections. */
72bf9492 7953
93311388 7954static void
976ca316 7955dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 7956{
976ca316 7957 struct objfile *objfile = per_objfile->objfile;
93311388 7958
6f738b01
SM
7959 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7960 objfile_name (objfile));
45cfd468 7961
76935768 7962 scoped_restore restore_reading_psyms
976ca316 7963 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 7964 true);
98bfdba5 7965
976ca316 7966 per_objfile->per_bfd->info.read (objfile);
91c24f0a 7967
93311388
DE
7968 /* Any cached compilation units will be linked by the per-objfile
7969 read_in_chain. Make sure to free them when we're done. */
976ca316 7970 free_cached_comp_units freer (per_objfile);
72bf9492 7971
976ca316 7972 build_type_psymtabs (per_objfile);
348e048f 7973
976ca316 7974 create_all_comp_units (per_objfile);
c906108c 7975
60606b2c
TT
7976 /* Create a temporary address map on a temporary obstack. We later
7977 copy this to the final obstack. */
8268c778 7978 auto_obstack temp_obstack;
791afaa2
TT
7979
7980 scoped_restore save_psymtabs_addrmap
d320c2b5 7981 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7982 addrmap_create_mutable (&temp_obstack));
72bf9492 7983
976ca316 7984 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7985 {
7986 if (per_cu->v.psymtab != NULL)
7987 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7988 continue;
976ca316 7989 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 7990 language_minimal);
3d5afab3 7991 }
ff013f42 7992
6aa5f3a6 7993 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 7994 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
7995
7996 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 7997 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7998 {
976ca316
SM
7999 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8000 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8001 }
8002
6f738b01 8003 if (dwarf_read_debug > 0)
976ca316 8004 print_tu_stats (per_objfile);
6aa5f3a6 8005
976ca316 8006 set_partial_user (per_objfile);
95554aad 8007
d320c2b5
TT
8008 objfile->partial_symtabs->psymtabs_addrmap
8009 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8010 objfile->partial_symtabs->obstack ());
791afaa2
TT
8011 /* At this point we want to keep the address map. */
8012 save_psymtabs_addrmap.release ();
ff013f42 8013
6f738b01
SM
8014 dwarf_read_debug_printf ("Done building psymtabs of %s",
8015 objfile_name (objfile));
ae038cb0
DJ
8016}
8017
dee91e82
DE
8018/* Load the partial DIEs for a secondary CU into memory.
8019 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8020
dee91e82 8021static void
ab432490 8022load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8023 dwarf2_per_objfile *per_objfile,
8024 dwarf2_cu *existing_cu)
dee91e82 8025{
2e671100 8026 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8027
8028 if (!reader.dummy_p)
8029 {
8030 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8031 language_minimal);
8032
8033 /* Check if comp unit has_children.
8034 If so, read the rest of the partial symbols from this comp unit.
8035 If not, there's no more debug_info for this comp unit. */
3e225074 8036 if (reader.comp_unit_die->has_children)
c0ab21c2 8037 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8038
8039 reader.keep ();
c0ab21c2 8040 }
ae038cb0
DJ
8041}
8042
ae038cb0 8043static void
976ca316 8044read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8045 struct dwarf2_section_info *section,
f1902523 8046 struct dwarf2_section_info *abbrev_section,
b76e467d 8047 unsigned int is_dwz)
ae038cb0 8048{
d521ce57 8049 const gdb_byte *info_ptr;
976ca316 8050 struct objfile *objfile = per_objfile->objfile;
be391dca 8051
6f738b01
SM
8052 dwarf_read_debug_printf ("Reading %s for %s",
8053 section->get_name (),
8054 section->get_file_name ());
bf6af496 8055
96b79293 8056 section->read (objfile);
ae038cb0 8057
36586728 8058 info_ptr = section->buffer;
6e70227d 8059
36586728 8060 while (info_ptr < section->buffer + section->size)
ae038cb0 8061 {
ae038cb0 8062 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8063
9c541725 8064 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8065
f1902523 8066 comp_unit_head cu_header;
976ca316 8067 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8068 abbrev_section, info_ptr,
8069 rcuh_kind::COMPILE);
ae038cb0
DJ
8070
8071 /* Save the compilation unit for later lookup. */
f1902523 8072 if (cu_header.unit_type != DW_UT_type)
976ca316 8073 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8074 else
8075 {
976ca316 8076 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8077 sig_type->signature = cu_header.signature;
8078 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8079 this_cu = &sig_type->per_cu;
8080 }
8081 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8082 this_cu->sect_off = sect_off;
f1902523 8083 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8084 this_cu->is_dwz = is_dwz;
8a0459fd 8085 this_cu->section = section;
ae038cb0 8086
976ca316 8087 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8088
8089 info_ptr = info_ptr + this_cu->length;
8090 }
36586728
TT
8091}
8092
8093/* Create a list of all compilation units in OBJFILE.
8094 This is only done for -readnow and building partial symtabs. */
8095
8096static void
976ca316 8097create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8098{
976ca316
SM
8099 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8100 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8101 &per_objfile->per_bfd->abbrev, 0);
36586728 8102
976ca316 8103 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8104 if (dwz != NULL)
976ca316 8105 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8106}
8107
5734ee8b 8108/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8109 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8110 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8111 DW_AT_ranges). See the comments of add_partial_subprogram on how
8112 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8113
72bf9492
DJ
8114static void
8115scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8116 CORE_ADDR *highpc, int set_addrmap,
8117 struct dwarf2_cu *cu)
c906108c 8118{
72bf9492 8119 struct partial_die_info *pdi;
c906108c 8120
91c24f0a
DC
8121 /* Now, march along the PDI's, descending into ones which have
8122 interesting children but skipping the children of the other ones,
8123 until we reach the end of the compilation unit. */
c906108c 8124
72bf9492 8125 pdi = first_die;
91c24f0a 8126
72bf9492
DJ
8127 while (pdi != NULL)
8128 {
52356b79 8129 pdi->fixup (cu);
c906108c 8130
f55ee35c 8131 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8132 children, so we need to look at them. Ditto for anonymous
8133 enums. */
933c6fe4 8134
7d00ffec 8135 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8136 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8137 || pdi->tag == DW_TAG_imported_unit
8138 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8139 {
72bf9492 8140 switch (pdi->tag)
c906108c
SS
8141 {
8142 case DW_TAG_subprogram:
b1dc1806 8143 case DW_TAG_inlined_subroutine:
cdc07690 8144 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8145 if (cu->language == language_cplus)
8146 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8147 set_addrmap, cu);
c906108c 8148 break;
72929c62 8149 case DW_TAG_constant:
c906108c
SS
8150 case DW_TAG_variable:
8151 case DW_TAG_typedef:
91c24f0a 8152 case DW_TAG_union_type:
317d2668
TV
8153 if (!pdi->is_declaration
8154 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8155 {
72bf9492 8156 add_partial_symbol (pdi, cu);
63d06c5c
DC
8157 }
8158 break;
c906108c 8159 case DW_TAG_class_type:
680b30c7 8160 case DW_TAG_interface_type:
c906108c 8161 case DW_TAG_structure_type:
72bf9492 8162 if (!pdi->is_declaration)
c906108c 8163 {
72bf9492 8164 add_partial_symbol (pdi, cu);
c906108c 8165 }
b7fee5a3
KS
8166 if ((cu->language == language_rust
8167 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8168 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8169 set_addrmap, cu);
c906108c 8170 break;
91c24f0a 8171 case DW_TAG_enumeration_type:
72bf9492
DJ
8172 if (!pdi->is_declaration)
8173 add_partial_enumeration (pdi, cu);
c906108c
SS
8174 break;
8175 case DW_TAG_base_type:
dda83cd7 8176 case DW_TAG_subrange_type:
c906108c 8177 /* File scope base type definitions are added to the partial
dda83cd7 8178 symbol table. */
72bf9492 8179 add_partial_symbol (pdi, cu);
c906108c 8180 break;
d9fa45fe 8181 case DW_TAG_namespace:
cdc07690 8182 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8183 break;
5d7cb8df 8184 case DW_TAG_module:
59c35742
AB
8185 if (!pdi->is_declaration)
8186 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8187 break;
95554aad
TT
8188 case DW_TAG_imported_unit:
8189 {
8190 struct dwarf2_per_cu_data *per_cu;
8191
f4dc4d17
DE
8192 /* For now we don't handle imported units in type units. */
8193 if (cu->per_cu->is_debug_types)
8194 {
8195 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8196 " supported in type units [in module %s]"),
5e22e966 8197 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8198 }
8199
e3b94546 8200 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8201 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8202
8203 /* Go read the partial unit, if needed. */
8204 if (per_cu->v.psymtab == NULL)
ab432490
SM
8205 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8206 cu->language);
95554aad 8207
ae640021 8208 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8209 }
8210 break;
74921315
KS
8211 case DW_TAG_imported_declaration:
8212 add_partial_symbol (pdi, cu);
8213 break;
c906108c
SS
8214 default:
8215 break;
8216 }
8217 }
8218
72bf9492
DJ
8219 /* If the die has a sibling, skip to the sibling. */
8220
8221 pdi = pdi->die_sibling;
8222 }
8223}
8224
8225/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8226
72bf9492 8227 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8228 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8229 Enumerators are an exception; they use the scope of their parent
8230 enumeration type, i.e. the name of the enumeration type is not
8231 prepended to the enumerator.
91c24f0a 8232
72bf9492
DJ
8233 There are two complexities. One is DW_AT_specification; in this
8234 case "parent" means the parent of the target of the specification,
8235 instead of the direct parent of the DIE. The other is compilers
8236 which do not emit DW_TAG_namespace; in this case we try to guess
8237 the fully qualified name of structure types from their members'
8238 linkage names. This must be done using the DIE's children rather
8239 than the children of any DW_AT_specification target. We only need
8240 to do this for structures at the top level, i.e. if the target of
8241 any DW_AT_specification (if any; otherwise the DIE itself) does not
8242 have a parent. */
8243
8244/* Compute the scope prefix associated with PDI's parent, in
8245 compilation unit CU. The result will be allocated on CU's
8246 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8247 field. NULL is returned if no prefix is necessary. */
15d034d0 8248static const char *
72bf9492
DJ
8249partial_die_parent_scope (struct partial_die_info *pdi,
8250 struct dwarf2_cu *cu)
8251{
15d034d0 8252 const char *grandparent_scope;
72bf9492 8253 struct partial_die_info *parent, *real_pdi;
91c24f0a 8254
72bf9492
DJ
8255 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8256 then this means the parent of the specification DIE. */
8257
8258 real_pdi = pdi;
72bf9492 8259 while (real_pdi->has_specification)
fb816e8b 8260 {
122cf0f2
AB
8261 auto res = find_partial_die (real_pdi->spec_offset,
8262 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8263 real_pdi = res.pdi;
8264 cu = res.cu;
8265 }
72bf9492
DJ
8266
8267 parent = real_pdi->die_parent;
8268 if (parent == NULL)
8269 return NULL;
8270
8271 if (parent->scope_set)
8272 return parent->scope;
8273
52356b79 8274 parent->fixup (cu);
72bf9492 8275
10b3939b 8276 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8277
acebe513
UW
8278 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8279 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8280 Work around this problem here. */
8281 if (cu->language == language_cplus
6e70227d 8282 && parent->tag == DW_TAG_namespace
7d00ffec 8283 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8284 && grandparent_scope == NULL)
8285 {
8286 parent->scope = NULL;
8287 parent->scope_set = 1;
8288 return NULL;
8289 }
8290
0a4b0913 8291 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8292 if (pdi->tag == DW_TAG_enumerator)
8293 /* Enumerators should not get the name of the enumeration as a prefix. */
8294 parent->scope = grandparent_scope;
8295 else if (parent->tag == DW_TAG_namespace
f55ee35c 8296 || parent->tag == DW_TAG_module
72bf9492
DJ
8297 || parent->tag == DW_TAG_structure_type
8298 || parent->tag == DW_TAG_class_type
680b30c7 8299 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8300 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8301 || parent->tag == DW_TAG_enumeration_type
8302 || (cu->language == language_fortran
8303 && parent->tag == DW_TAG_subprogram
8304 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8305 {
8306 if (grandparent_scope == NULL)
7d00ffec 8307 parent->scope = parent->name (cu);
72bf9492 8308 else
3e43a32a
MS
8309 parent->scope = typename_concat (&cu->comp_unit_obstack,
8310 grandparent_scope,
7d00ffec 8311 parent->name (cu), 0, cu);
72bf9492 8312 }
72bf9492
DJ
8313 else
8314 {
8315 /* FIXME drow/2004-04-01: What should we be doing with
8316 function-local names? For partial symbols, we should probably be
8317 ignoring them. */
fa9c3fa0
TT
8318 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8319 dwarf_tag_name (parent->tag),
8320 sect_offset_str (pdi->sect_off));
72bf9492 8321 parent->scope = grandparent_scope;
c906108c
SS
8322 }
8323
72bf9492
DJ
8324 parent->scope_set = 1;
8325 return parent->scope;
8326}
8327
8328/* Return the fully scoped name associated with PDI, from compilation unit
8329 CU. The result will be allocated with malloc. */
4568ecf9 8330
43816ebc 8331static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8332partial_die_full_name (struct partial_die_info *pdi,
8333 struct dwarf2_cu *cu)
8334{
15d034d0 8335 const char *parent_scope;
72bf9492 8336
98bfdba5
PA
8337 /* If this is a template instantiation, we can not work out the
8338 template arguments from partial DIEs. So, unfortunately, we have
8339 to go through the full DIEs. At least any work we do building
8340 types here will be reused if full symbols are loaded later. */
8341 if (pdi->has_template_arguments)
8342 {
52356b79 8343 pdi->fixup (cu);
98bfdba5 8344
7d00ffec 8345 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8346 {
8347 struct die_info *die;
8348 struct attribute attr;
8349 struct dwarf2_cu *ref_cu = cu;
8350
b64f50a1 8351 /* DW_FORM_ref_addr is using section offset. */
b4069958 8352 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8353 attr.form = DW_FORM_ref_addr;
9c541725 8354 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8355 die = follow_die_ref (NULL, &attr, &ref_cu);
8356
43816ebc 8357 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8358 }
8359 }
8360
72bf9492
DJ
8361 parent_scope = partial_die_parent_scope (pdi, cu);
8362 if (parent_scope == NULL)
8363 return NULL;
8364 else
43816ebc 8365 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8366 pdi->name (cu),
8367 0, cu));
c906108c
SS
8368}
8369
8370static void
72bf9492 8371add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8372{
976ca316
SM
8373 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8374 struct objfile *objfile = per_objfile->objfile;
08feed99 8375 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8376 CORE_ADDR addr = 0;
15d034d0 8377 const char *actual_name = NULL;
e142c38c
DJ
8378 CORE_ADDR baseaddr;
8379
b3b3bada 8380 baseaddr = objfile->text_section_offset ();
c906108c 8381
43816ebc
TT
8382 gdb::unique_xmalloc_ptr<char> built_actual_name
8383 = partial_die_full_name (pdi, cu);
15d034d0 8384 if (built_actual_name != NULL)
43816ebc 8385 actual_name = built_actual_name.get ();
63d06c5c 8386
72bf9492 8387 if (actual_name == NULL)
7d00ffec 8388 actual_name = pdi->name (cu);
72bf9492 8389
76e288d1
TT
8390 partial_symbol psymbol;
8391 memset (&psymbol, 0, sizeof (psymbol));
8392 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
a52d653e 8393 psymbol.ginfo.set_section_index (-1);
76e288d1
TT
8394
8395 /* The code below indicates that the psymbol should be installed by
8396 setting this. */
8397 gdb::optional<psymbol_placement> where;
8398
c906108c
SS
8399 switch (pdi->tag)
8400 {
b1dc1806 8401 case DW_TAG_inlined_subroutine:
c906108c 8402 case DW_TAG_subprogram:
79748972
TT
8403 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8404 - baseaddr);
0a4b0913
AB
8405 if (pdi->is_external
8406 || cu->language == language_ada
8407 || (cu->language == language_fortran
8408 && pdi->die_parent != NULL
8409 && pdi->die_parent->tag == DW_TAG_subprogram))
8410 {
dda83cd7
SM
8411 /* Normally, only "external" DIEs are part of the global scope.
8412 But in Ada and Fortran, we want to be able to access nested
8413 procedures globally. So all Ada and Fortran subprograms are
8414 stored in the global scope. */
76e288d1 8415 where = psymbol_placement::GLOBAL;
c906108c
SS
8416 }
8417 else
76e288d1
TT
8418 where = psymbol_placement::STATIC;
8419
8420 psymbol.domain = VAR_DOMAIN;
8421 psymbol.aclass = LOC_BLOCK;
a52d653e 8422 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1 8423 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8424
8425 if (pdi->main_subprogram && actual_name != NULL)
8426 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8427 break;
72929c62 8428 case DW_TAG_constant:
76e288d1
TT
8429 psymbol.domain = VAR_DOMAIN;
8430 psymbol.aclass = LOC_STATIC;
8431 where = (pdi->is_external
8432 ? psymbol_placement::GLOBAL
8433 : psymbol_placement::STATIC);
72929c62 8434 break;
c906108c 8435 case DW_TAG_variable:
95554aad
TT
8436 if (pdi->d.locdesc)
8437 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8438
95554aad 8439 if (pdi->d.locdesc
caac4577 8440 && addr == 0
976ca316 8441 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8442 {
8443 /* A global or static variable may also have been stripped
8444 out by the linker if unused, in which case its address
8445 will be nullified; do not add such variables into partial
8446 symbol table then. */
8447 }
8448 else if (pdi->is_external)
c906108c
SS
8449 {
8450 /* Global Variable.
8451 Don't enter into the minimal symbol tables as there is
8452 a minimal symbol table entry from the ELF symbols already.
8453 Enter into partial symbol table if it has a location
8454 descriptor or a type.
8455 If the location descriptor is missing, new_symbol will create
8456 a LOC_UNRESOLVED symbol, the address of the variable will then
8457 be determined from the minimal symbol table whenever the variable
8458 is referenced.
8459 The address for the partial symbol table entry is not
8460 used by GDB, but it comes in handy for debugging partial symbol
8461 table building. */
8462
95554aad 8463 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8464 {
8465 psymbol.domain = VAR_DOMAIN;
8466 psymbol.aclass = LOC_STATIC;
a52d653e 8467 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8468 psymbol.ginfo.value.address = addr;
8469 where = psymbol_placement::GLOBAL;
8470 }
c906108c
SS
8471 }
8472 else
8473 {
ff908ebf
AW
8474 int has_loc = pdi->d.locdesc != NULL;
8475
8476 /* Static Variable. Skip symbols whose value we cannot know (those
8477 without location descriptors or constant values). */
8478 if (!has_loc && !pdi->has_const_value)
43816ebc 8479 return;
ff908ebf 8480
76e288d1
TT
8481 psymbol.domain = VAR_DOMAIN;
8482 psymbol.aclass = LOC_STATIC;
a52d653e 8483 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
76e288d1
TT
8484 if (has_loc)
8485 psymbol.ginfo.value.address = addr;
8486 where = psymbol_placement::STATIC;
c906108c
SS
8487 }
8488 break;
d8f62e84 8489 case DW_TAG_array_type:
c906108c
SS
8490 case DW_TAG_typedef:
8491 case DW_TAG_base_type:
a02abb62 8492 case DW_TAG_subrange_type:
76e288d1
TT
8493 psymbol.domain = VAR_DOMAIN;
8494 psymbol.aclass = LOC_TYPEDEF;
8495 where = psymbol_placement::STATIC;
c906108c 8496 break;
74921315 8497 case DW_TAG_imported_declaration:
72bf9492 8498 case DW_TAG_namespace:
76e288d1
TT
8499 psymbol.domain = VAR_DOMAIN;
8500 psymbol.aclass = LOC_TYPEDEF;
8501 where = psymbol_placement::GLOBAL;
72bf9492 8502 break;
530e8392 8503 case DW_TAG_module:
a5fd13a9 8504 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8505 available without any name. If so, we skip the module as it
8506 doesn't bring any value. */
a5fd13a9 8507 if (actual_name != nullptr)
76e288d1
TT
8508 {
8509 psymbol.domain = MODULE_DOMAIN;
8510 psymbol.aclass = LOC_TYPEDEF;
8511 where = psymbol_placement::GLOBAL;
8512 }
530e8392 8513 break;
c906108c 8514 case DW_TAG_class_type:
680b30c7 8515 case DW_TAG_interface_type:
c906108c
SS
8516 case DW_TAG_structure_type:
8517 case DW_TAG_union_type:
8518 case DW_TAG_enumeration_type:
fa4028e9 8519 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8520 about "Structure, Union, and Class Type Entries": "An incomplete
8521 structure, union or class type is represented by a structure,
8522 union or class entry that does not have a byte size attribute
8523 and that has a DW_AT_declaration attribute." */
fa4028e9 8524 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8525 return;
fa4028e9 8526
63d06c5c
DC
8527 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8528 static vs. global. */
76e288d1
TT
8529 psymbol.domain = STRUCT_DOMAIN;
8530 psymbol.aclass = LOC_TYPEDEF;
8531 where = (cu->language == language_cplus
8532 ? psymbol_placement::GLOBAL
8533 : psymbol_placement::STATIC);
c906108c
SS
8534 break;
8535 case DW_TAG_enumerator:
76e288d1
TT
8536 psymbol.domain = VAR_DOMAIN;
8537 psymbol.aclass = LOC_CONST;
8538 where = (cu->language == language_cplus
8539 ? psymbol_placement::GLOBAL
8540 : psymbol_placement::STATIC);
c906108c
SS
8541 break;
8542 default:
8543 break;
8544 }
76e288d1
TT
8545
8546 if (where.has_value ())
8547 {
f049a313
TT
8548 if (built_actual_name != nullptr)
8549 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8550 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8551 psymbol.ginfo.set_linkage_name (actual_name);
8552 else
8553 {
8554 psymbol.ginfo.set_demangled_name (actual_name,
8555 &objfile->objfile_obstack);
8556 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8557 }
932539d7 8558 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8559 }
c906108c
SS
8560}
8561
5c4e30ca
DC
8562/* Read a partial die corresponding to a namespace; also, add a symbol
8563 corresponding to that namespace to the symbol table. NAMESPACE is
8564 the name of the enclosing namespace. */
91c24f0a 8565
72bf9492
DJ
8566static void
8567add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8568 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8569 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8570{
72bf9492 8571 /* Add a symbol for the namespace. */
e7c27a73 8572
72bf9492 8573 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8574
8575 /* Now scan partial symbols in that namespace. */
8576
91c24f0a 8577 if (pdi->has_children)
cdc07690 8578 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8579}
8580
5d7cb8df
JK
8581/* Read a partial die corresponding to a Fortran module. */
8582
8583static void
8584add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8585 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8586{
530e8392
KB
8587 /* Add a symbol for the namespace. */
8588
8589 add_partial_symbol (pdi, cu);
8590
f55ee35c 8591 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8592
8593 if (pdi->has_children)
cdc07690 8594 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8595}
8596
b1dc1806
XR
8597/* Read a partial die corresponding to a subprogram or an inlined
8598 subprogram and create a partial symbol for that subprogram.
8599 When the CU language allows it, this routine also defines a partial
8600 symbol for each nested subprogram that this subprogram contains.
8601 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8602 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8603
cdc07690
YQ
8604 PDI may also be a lexical block, in which case we simply search
8605 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8606 Again, this is only performed when the CU language allows this
8607 type of definitions. */
8608
8609static void
8610add_partial_subprogram (struct partial_die_info *pdi,
8611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8612 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8613{
b1dc1806 8614 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8615 {
8616 if (pdi->has_pc_info)
dda83cd7
SM
8617 {
8618 if (pdi->lowpc < *lowpc)
8619 *lowpc = pdi->lowpc;
8620 if (pdi->highpc > *highpc)
8621 *highpc = pdi->highpc;
cdc07690 8622 if (set_addrmap)
5734ee8b 8623 {
5e22e966 8624 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8625 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8626 CORE_ADDR baseaddr;
b926417a
TT
8627 CORE_ADDR this_highpc;
8628 CORE_ADDR this_lowpc;
5734ee8b 8629
b3b3bada 8630 baseaddr = objfile->text_section_offset ();
b926417a
TT
8631 this_lowpc
8632 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8633 pdi->lowpc + baseaddr)
8634 - baseaddr);
8635 this_highpc
8636 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8637 pdi->highpc + baseaddr)
8638 - baseaddr);
d320c2b5 8639 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8640 this_lowpc, this_highpc - 1,
9291a0cd 8641 cu->per_cu->v.psymtab);
5734ee8b 8642 }
dda83cd7 8643 }
481860b3
GB
8644
8645 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8646 {
dda83cd7 8647 if (!pdi->is_declaration)
e8d05480
JB
8648 /* Ignore subprogram DIEs that do not have a name, they are
8649 illegal. Do not emit a complaint at this point, we will
8650 do so when we convert this psymtab into a symtab. */
7d00ffec 8651 if (pdi->name (cu))
e8d05480 8652 add_partial_symbol (pdi, cu);
dda83cd7 8653 }
bc30ff58 8654 }
6e70227d 8655
bc30ff58
JB
8656 if (! pdi->has_children)
8657 return;
8658
0a4b0913 8659 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8660 {
8661 pdi = pdi->die_child;
8662 while (pdi != NULL)
8663 {
52356b79 8664 pdi->fixup (cu);
bc30ff58 8665 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8666 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8667 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8668 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8669 pdi = pdi->die_sibling;
8670 }
8671 }
8672}
8673
91c24f0a
DC
8674/* Read a partial die corresponding to an enumeration type. */
8675
72bf9492
DJ
8676static void
8677add_partial_enumeration (struct partial_die_info *enum_pdi,
8678 struct dwarf2_cu *cu)
91c24f0a 8679{
72bf9492 8680 struct partial_die_info *pdi;
91c24f0a 8681
7d00ffec 8682 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8683 add_partial_symbol (enum_pdi, cu);
8684
8685 pdi = enum_pdi->die_child;
8686 while (pdi)
91c24f0a 8687 {
7d00ffec 8688 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8689 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8690 else
72bf9492
DJ
8691 add_partial_symbol (pdi, cu);
8692 pdi = pdi->die_sibling;
91c24f0a 8693 }
91c24f0a
DC
8694}
8695
6caca83c
CC
8696/* Return the initial uleb128 in the die at INFO_PTR. */
8697
8698static unsigned int
d521ce57 8699peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8700{
8701 unsigned int bytes_read;
8702
8703 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8704}
8705
685af9cd
TT
8706/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8707 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8708
4bb7a0a7
DJ
8709 Return the corresponding abbrev, or NULL if the number is zero (indicating
8710 an empty DIE). In either case *BYTES_READ will be set to the length of
8711 the initial number. */
8712
7c32eebb 8713static const struct abbrev_info *
685af9cd
TT
8714peek_die_abbrev (const die_reader_specs &reader,
8715 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8716{
685af9cd 8717 dwarf2_cu *cu = reader.cu;
0280fdcc 8718 bfd *abfd = reader.abfd;
685af9cd
TT
8719 unsigned int abbrev_number
8720 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8721
8722 if (abbrev_number == 0)
8723 return NULL;
8724
7c32eebb
TT
8725 const abbrev_info *abbrev
8726 = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8727 if (!abbrev)
8728 {
422b9917 8729 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8730 " at offset %s [in module %s]"),
422b9917 8731 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8732 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8733 }
8734
8735 return abbrev;
8736}
8737
93311388
DE
8738/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8739 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8740 DIE. Any children of the skipped DIEs will also be skipped. */
8741
d521ce57
TT
8742static const gdb_byte *
8743skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8744{
4bb7a0a7
DJ
8745 while (1)
8746 {
685af9cd 8747 unsigned int bytes_read;
7c32eebb
TT
8748 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8749 &bytes_read);
685af9cd 8750
4bb7a0a7
DJ
8751 if (abbrev == NULL)
8752 return info_ptr + bytes_read;
8753 else
dee91e82 8754 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8755 }
8756}
8757
93311388
DE
8758/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8759 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8760 abbrev corresponding to that skipped uleb128 should be passed in
8761 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8762 children. */
8763
d521ce57
TT
8764static const gdb_byte *
8765skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
7c32eebb 8766 const struct abbrev_info *abbrev)
4bb7a0a7
DJ
8767{
8768 unsigned int bytes_read;
8769 struct attribute attr;
dee91e82
DE
8770 bfd *abfd = reader->abfd;
8771 struct dwarf2_cu *cu = reader->cu;
d521ce57 8772 const gdb_byte *buffer = reader->buffer;
f664829e 8773 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8774 unsigned int form, i;
8775
8776 for (i = 0; i < abbrev->num_attrs; i++)
8777 {
8778 /* The only abbrev we care about is DW_AT_sibling. */
8779 if (abbrev->attrs[i].name == DW_AT_sibling)
8780 {
7a5f294d 8781 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8782 if (attr.form == DW_FORM_ref_addr)
b98664d3 8783 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8784 else
b9502d3f 8785 {
0826b30a 8786 sect_offset off = attr.get_ref_die_offset ();
9c541725 8787 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8788
8789 if (sibling_ptr < info_ptr)
b98664d3 8790 complaint (_("DW_AT_sibling points backwards"));
22869d73 8791 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8792 reader->die_section->overflow_complaint ();
b9502d3f
WN
8793 else
8794 return sibling_ptr;
8795 }
4bb7a0a7
DJ
8796 }
8797
8798 /* If it isn't DW_AT_sibling, skip this attribute. */
8799 form = abbrev->attrs[i].form;
8800 skip_attribute:
8801 switch (form)
8802 {
4bb7a0a7 8803 case DW_FORM_ref_addr:
ae411497
TT
8804 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8805 and later it is offset sized. */
8806 if (cu->header.version == 2)
8807 info_ptr += cu->header.addr_size;
8808 else
8809 info_ptr += cu->header.offset_size;
8810 break;
36586728
TT
8811 case DW_FORM_GNU_ref_alt:
8812 info_ptr += cu->header.offset_size;
8813 break;
ae411497 8814 case DW_FORM_addr:
4bb7a0a7
DJ
8815 info_ptr += cu->header.addr_size;
8816 break;
8817 case DW_FORM_data1:
8818 case DW_FORM_ref1:
8819 case DW_FORM_flag:
8fe0f950 8820 case DW_FORM_strx1:
4bb7a0a7
DJ
8821 info_ptr += 1;
8822 break;
2dc7f7b3 8823 case DW_FORM_flag_present:
43988095 8824 case DW_FORM_implicit_const:
2dc7f7b3 8825 break;
4bb7a0a7
DJ
8826 case DW_FORM_data2:
8827 case DW_FORM_ref2:
8fe0f950 8828 case DW_FORM_strx2:
4bb7a0a7
DJ
8829 info_ptr += 2;
8830 break;
8fe0f950
AT
8831 case DW_FORM_strx3:
8832 info_ptr += 3;
8833 break;
4bb7a0a7
DJ
8834 case DW_FORM_data4:
8835 case DW_FORM_ref4:
8fe0f950 8836 case DW_FORM_strx4:
4bb7a0a7
DJ
8837 info_ptr += 4;
8838 break;
8839 case DW_FORM_data8:
8840 case DW_FORM_ref8:
55f1336d 8841 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8842 info_ptr += 8;
8843 break;
0224619f
JK
8844 case DW_FORM_data16:
8845 info_ptr += 16;
8846 break;
4bb7a0a7 8847 case DW_FORM_string:
9b1c24c8 8848 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8849 info_ptr += bytes_read;
8850 break;
2dc7f7b3 8851 case DW_FORM_sec_offset:
4bb7a0a7 8852 case DW_FORM_strp:
36586728 8853 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8854 info_ptr += cu->header.offset_size;
8855 break;
2dc7f7b3 8856 case DW_FORM_exprloc:
4bb7a0a7
DJ
8857 case DW_FORM_block:
8858 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8859 info_ptr += bytes_read;
8860 break;
8861 case DW_FORM_block1:
8862 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8863 break;
8864 case DW_FORM_block2:
8865 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8866 break;
8867 case DW_FORM_block4:
8868 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8869 break;
336d760d 8870 case DW_FORM_addrx:
cf532bd1 8871 case DW_FORM_strx:
4bb7a0a7
DJ
8872 case DW_FORM_sdata:
8873 case DW_FORM_udata:
8874 case DW_FORM_ref_udata:
3019eac3
DE
8875 case DW_FORM_GNU_addr_index:
8876 case DW_FORM_GNU_str_index:
18a8505e 8877 case DW_FORM_rnglistx:
41144253 8878 case DW_FORM_loclistx:
d521ce57 8879 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8880 break;
8881 case DW_FORM_indirect:
8882 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8883 info_ptr += bytes_read;
8884 /* We need to continue parsing from here, so just go back to
8885 the top. */
8886 goto skip_attribute;
8887
8888 default:
3e43a32a
MS
8889 error (_("Dwarf Error: Cannot handle %s "
8890 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8891 dwarf_form_name (form),
8892 bfd_get_filename (abfd));
8893 }
8894 }
8895
8896 if (abbrev->has_children)
dee91e82 8897 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8898 else
8899 return info_ptr;
8900}
8901
93311388 8902/* Locate ORIG_PDI's sibling.
dee91e82 8903 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8904
d521ce57 8905static const gdb_byte *
dee91e82
DE
8906locate_pdi_sibling (const struct die_reader_specs *reader,
8907 struct partial_die_info *orig_pdi,
d521ce57 8908 const gdb_byte *info_ptr)
91c24f0a
DC
8909{
8910 /* Do we know the sibling already? */
72bf9492 8911
91c24f0a
DC
8912 if (orig_pdi->sibling)
8913 return orig_pdi->sibling;
8914
8915 /* Are there any children to deal with? */
8916
8917 if (!orig_pdi->has_children)
8918 return info_ptr;
8919
4bb7a0a7 8920 /* Skip the children the long way. */
91c24f0a 8921
dee91e82 8922 return skip_children (reader, info_ptr);
91c24f0a
DC
8923}
8924
257e7a09 8925/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8926 not NULL. */
c906108c 8927
891813be
TT
8928void
8929dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8930{
976ca316 8931 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 8932
976ca316 8933 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 8934
077cbab2
TT
8935 /* If this psymtab is constructed from a debug-only objfile, the
8936 has_section_at_zero flag will not necessarily be correct. We
8937 can get the correct value for this flag by looking at the data
8938 associated with the (presumably stripped) associated objfile. */
8939 if (objfile->separate_debug_objfile_backlink)
c906108c 8940 {
976ca316 8941 dwarf2_per_objfile *per_objfile_backlink
077cbab2 8942 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8943
976ca316
SM
8944 per_objfile->per_bfd->has_section_at_zero
8945 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 8946 }
98bfdba5 8947
8566b89b 8948 expand_psymtab (objfile);
95554aad 8949
976ca316 8950 process_cu_includes (per_objfile);
c906108c 8951}
9cdd5dbd
DE
8952\f
8953/* Reading in full CUs. */
c906108c 8954
10b3939b
DJ
8955/* Add PER_CU to the queue. */
8956
8957static void
120ce1b5
SM
8958queue_comp_unit (dwarf2_per_cu_data *per_cu,
8959 dwarf2_per_objfile *per_objfile,
95554aad 8960 enum language pretend_language)
10b3939b 8961{
10b3939b 8962 per_cu->queued = 1;
08ac5771
SM
8963
8964 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8965 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8966}
8967
616c069a
SM
8968/* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8969
89e63ee4
DE
8970 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8971 dependency.
69d751e3 8972
616c069a
SM
8973 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8974 DIEs, false otherwise.
8975
8976 Explanation: there is an invariant that if a CU is queued for expansion
8977 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8978 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8979 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8980 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8981 invariant is respected.
8982
8983 The caller is therefore not required to load the CU's DIEs (we return false)
8984 if:
8985
8986 - the CU is already expanded, and therefore does not get enqueued
8987 - the CU gets enqueued for expansion, but its DIEs are already loaded
8988
8989 Note that the caller should not use this function's return value as an
8990 indicator of whether the CU's DIEs are loaded right now, it should check
8991 that by calling `dwarf2_per_objfile::get_cu` instead. */
0907af0c
DE
8992
8993static int
89e63ee4 8994maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8995 dwarf2_per_cu_data *per_cu,
8996 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8997 enum language pretend_language)
8998{
8999 /* We may arrive here during partial symbol reading, if we need full
9000 DIEs to process an unusual case (e.g. template arguments). Do
9001 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9002 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9003 {
7188ed02
SM
9004 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9005
9006 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9007 return 1;
9008 return 0;
9009 }
9010
9011 /* Mark the dependence relation so that we don't flush PER_CU
9012 too early. */
89e63ee4
DE
9013 if (dependent_cu != NULL)
9014 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9015
9016 /* If it's already on the queue, we have nothing to do. */
9017 if (per_cu->queued)
de53369b
SM
9018 {
9019 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9020 loaded. */
9021 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
616c069a
SM
9022
9023 /* If the CU is queued for expansion, it should not already be
9024 expanded. */
9025 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9026
9027 /* The DIEs are already loaded, the caller doesn't need to do it. */
de53369b
SM
9028 return 0;
9029 }
0907af0c 9030
616c069a
SM
9031 bool queued = false;
9032 if (!per_objfile->symtab_set_p (per_cu))
9033 {
9034 /* Add it to the queue. */
9035 queue_comp_unit (per_cu, per_objfile, pretend_language);
9036 queued = true;
9037 }
9038
0907af0c
DE
9039 /* If the compilation unit is already loaded, just mark it as
9040 used. */
7188ed02
SM
9041 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9042 if (cu != nullptr)
616c069a 9043 cu->last_used = 0;
0907af0c 9044
616c069a
SM
9045 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9046 and the DIEs are not already loaded. */
9047 return queued && cu == nullptr;
0907af0c
DE
9048}
9049
10b3939b
DJ
9050/* Process the queue. */
9051
9052static void
976ca316 9053process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9054{
6f738b01
SM
9055 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9056 objfile_name (per_objfile->objfile));
45cfd468 9057
03dd20cc
DJ
9058 /* The queue starts out with one item, but following a DIE reference
9059 may load a new CU, adding it to the end of the queue. */
08ac5771 9060 while (!per_objfile->per_bfd->queue->empty ())
10b3939b 9061 {
08ac5771 9062 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
7188ed02 9063 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9064
976ca316 9065 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9066 {
976ca316 9067 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9068
7188ed02
SM
9069 /* Skip dummy CUs. */
9070 if (cu != nullptr)
73be47f5 9071 {
7188ed02
SM
9072 unsigned int debug_print_threshold;
9073 char buf[100];
9074
9075 if (per_cu->is_debug_types)
9076 {
9077 struct signatured_type *sig_type =
9078 (struct signatured_type *) per_cu;
9079
9080 sprintf (buf, "TU %s at offset %s",
9081 hex_string (sig_type->signature),
9082 sect_offset_str (per_cu->sect_off));
9083 /* There can be 100s of TUs.
9084 Only print them in verbose mode. */
9085 debug_print_threshold = 2;
9086 }
9087 else
9088 {
9089 sprintf (buf, "CU at offset %s",
9090 sect_offset_str (per_cu->sect_off));
9091 debug_print_threshold = 1;
9092 }
247f5c4f 9093
7188ed02 9094 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9095 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9096
7188ed02
SM
9097 if (per_cu->is_debug_types)
9098 process_full_type_unit (cu, item.pretend_language);
9099 else
9100 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9101
7188ed02 9102 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9103 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9104 }
f4dc4d17 9105 }
10b3939b 9106
7188ed02 9107 per_cu->queued = 0;
08ac5771 9108 per_objfile->per_bfd->queue->pop ();
10b3939b
DJ
9109 }
9110
6f738b01
SM
9111 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9112 objfile_name (per_objfile->objfile));
10b3939b
DJ
9113}
9114
10b3939b
DJ
9115/* Read in full symbols for PST, and anything it depends on. */
9116
8566b89b
TT
9117void
9118dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9119{
af758d11 9120 gdb_assert (!readin_p (objfile));
95554aad 9121
17ee85fc
TT
9122 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9123 free_cached_comp_units freer (per_objfile);
48993951 9124 expand_dependencies (objfile);
aaa75496 9125
97a1449a 9126 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9127 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9128}
9129
af758d11
SM
9130/* See psympriv.h. */
9131
9132bool
9133dwarf2_psymtab::readin_p (struct objfile *objfile) const
9134{
9135 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9136 return per_objfile->symtab_set_p (per_cu_data);
9137}
9138
9139/* See psympriv.h. */
9140
9141compunit_symtab *
9142dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9143{
9144 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9145 return per_objfile->get_symtab (per_cu_data);
9146}
9147
dee91e82
DE
9148/* Trivial hash function for die_info: the hash value of a DIE
9149 is its offset in .debug_info for this objfile. */
10b3939b 9150
dee91e82
DE
9151static hashval_t
9152die_hash (const void *item)
10b3939b 9153{
9a3c8263 9154 const struct die_info *die = (const struct die_info *) item;
6502dd73 9155
9c541725 9156 return to_underlying (die->sect_off);
dee91e82 9157}
63d06c5c 9158
dee91e82
DE
9159/* Trivial comparison function for die_info structures: two DIEs
9160 are equal if they have the same offset. */
98bfdba5 9161
dee91e82
DE
9162static int
9163die_eq (const void *item_lhs, const void *item_rhs)
9164{
9a3c8263
SM
9165 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9166 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9167
9c541725 9168 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9169}
c906108c 9170
4a636814
SM
9171/* Load the DIEs associated with PER_CU into memory.
9172
9173 In some cases, the caller, while reading partial symbols, will need to load
9174 the full symbols for the CU for some reason. It will already have a
9175 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9176 rather than creating a new one. */
c906108c 9177
dee91e82 9178static void
ab432490
SM
9179load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9180 dwarf2_per_objfile *per_objfile,
4a636814 9181 dwarf2_cu *existing_cu,
c0ab21c2
TT
9182 bool skip_partial,
9183 enum language pretend_language)
dee91e82 9184{
c0ab21c2
TT
9185 gdb_assert (! this_cu->is_debug_types);
9186
7188ed02 9187 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9188 if (reader.dummy_p)
9189 return;
9190
9191 struct dwarf2_cu *cu = reader.cu;
9192 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9193
dee91e82
DE
9194 gdb_assert (cu->die_hash == NULL);
9195 cu->die_hash =
9196 htab_create_alloc_ex (cu->header.length / 12,
9197 die_hash,
9198 die_eq,
9199 NULL,
9200 &cu->comp_unit_obstack,
9201 hashtab_obstack_allocate,
9202 dummy_obstack_deallocate);
e142c38c 9203
3e225074 9204 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9205 reader.comp_unit_die->child
9206 = read_die_and_siblings (&reader, reader.info_ptr,
9207 &info_ptr, reader.comp_unit_die);
9208 cu->dies = reader.comp_unit_die;
dee91e82 9209 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9210
9211 /* We try not to read any attributes in this function, because not
9cdd5dbd 9212 all CUs needed for references have been loaded yet, and symbol
10b3939b 9213 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9214 or we won't be able to build types correctly.
9215 Similarly, if we do not read the producer, we can not apply
9216 producer-specific interpretation. */
c0ab21c2 9217 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9218
9219 reader.keep ();
10b3939b
DJ
9220}
9221
3da10d80
KS
9222/* Add a DIE to the delayed physname list. */
9223
9224static void
9225add_to_method_list (struct type *type, int fnfield_index, int index,
9226 const char *name, struct die_info *die,
9227 struct dwarf2_cu *cu)
9228{
9229 struct delayed_method_info mi;
9230 mi.type = type;
9231 mi.fnfield_index = fnfield_index;
9232 mi.index = index;
9233 mi.name = name;
9234 mi.die = die;
c89b44cd 9235 cu->method_list.push_back (mi);
3da10d80
KS
9236}
9237
3693fdb3
PA
9238/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9239 "const" / "volatile". If so, decrements LEN by the length of the
9240 modifier and return true. Otherwise return false. */
9241
9242template<size_t N>
9243static bool
9244check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9245{
9246 size_t mod_len = sizeof (mod) - 1;
9247 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9248 {
9249 len -= mod_len;
9250 return true;
9251 }
9252 return false;
9253}
9254
3da10d80
KS
9255/* Compute the physnames of any methods on the CU's method list.
9256
9257 The computation of method physnames is delayed in order to avoid the
9258 (bad) condition that one of the method's formal parameters is of an as yet
9259 incomplete type. */
9260
9261static void
9262compute_delayed_physnames (struct dwarf2_cu *cu)
9263{
3693fdb3 9264 /* Only C++ delays computing physnames. */
c89b44cd 9265 if (cu->method_list.empty ())
3693fdb3
PA
9266 return;
9267 gdb_assert (cu->language == language_cplus);
9268
52941706 9269 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9270 {
1d06ead6 9271 const char *physname;
3da10d80 9272 struct fn_fieldlist *fn_flp
c89b44cd
TT
9273 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9274 physname = dwarf2_physname (mi.name, mi.die, cu);
9275 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9276 = physname ? physname : "";
3693fdb3
PA
9277
9278 /* Since there's no tag to indicate whether a method is a
9279 const/volatile overload, extract that information out of the
9280 demangled name. */
9281 if (physname != NULL)
9282 {
9283 size_t len = strlen (physname);
9284
9285 while (1)
9286 {
9287 if (physname[len] == ')') /* shortcut */
9288 break;
9289 else if (check_modifier (physname, len, " const"))
c89b44cd 9290 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9291 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9292 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9293 else
9294 break;
9295 }
9296 }
3da10d80 9297 }
c89b44cd
TT
9298
9299 /* The list is no longer needed. */
9300 cu->method_list.clear ();
3da10d80
KS
9301}
9302
a766d390
DE
9303/* Go objects should be embedded in a DW_TAG_module DIE,
9304 and it's not clear if/how imported objects will appear.
9305 To keep Go support simple until that's worked out,
9306 go back through what we've read and create something usable.
9307 We could do this while processing each DIE, and feels kinda cleaner,
9308 but that way is more invasive.
9309 This is to, for example, allow the user to type "p var" or "b main"
9310 without having to specify the package name, and allow lookups
9311 of module.object to work in contexts that use the expression
9312 parser. */
9313
9314static void
9315fixup_go_packaging (struct dwarf2_cu *cu)
9316{
421d1616 9317 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9318 struct pending *list;
9319 int i;
9320
c24bdb02 9321 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9322 list != NULL;
9323 list = list->next)
a766d390
DE
9324 {
9325 for (i = 0; i < list->nsyms; ++i)
9326 {
9327 struct symbol *sym = list->symbol[i];
9328
c1b5c1eb 9329 if (sym->language () == language_go
a766d390
DE
9330 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9331 {
421d1616
TT
9332 gdb::unique_xmalloc_ptr<char> this_package_name
9333 (go_symbol_package_name (sym));
a766d390
DE
9334
9335 if (this_package_name == NULL)
9336 continue;
9337 if (package_name == NULL)
421d1616 9338 package_name = std::move (this_package_name);
a766d390
DE
9339 else
9340 {
5e22e966 9341 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9342 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9343 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9344 (symbol_symtab (sym) != NULL
9345 ? symtab_to_filename_for_display
9346 (symbol_symtab (sym))
e3b94546 9347 : objfile_name (objfile)),
421d1616 9348 this_package_name.get (), package_name.get ());
a766d390
DE
9349 }
9350 }
9351 }
9352 }
9353
9354 if (package_name != NULL)
9355 {
5e22e966 9356 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9357 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9358 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9359 saved_package_name);
a766d390
DE
9360 struct symbol *sym;
9361
8c14c3a3 9362 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9363 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9364 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9365 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9366 e.g., "main" finds the "main" module and not C's main(). */
9367 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9368 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9369 SYMBOL_TYPE (sym) = type;
9370
c24bdb02 9371 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9372 }
9373}
9374
c9317f21
TT
9375/* Allocate a fully-qualified name consisting of the two parts on the
9376 obstack. */
9377
9378static const char *
9379rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9380{
9381 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9382}
9383
9c6a1327
TT
9384/* A helper that allocates a variant part to attach to a Rust enum
9385 type. OBSTACK is where the results should be allocated. TYPE is
9386 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9387 discriminant. It must be the index of one of the fields of TYPE,
9388 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9389 DEFAULT_INDEX is the index of the default field; or -1 if there is
9390 no default. RANGES is indexed by "effective" field number (the
9391 field index, but omitting the discriminant and default fields) and
9392 must hold the discriminant values used by the variants. Note that
9393 RANGES must have a lifetime at least as long as OBSTACK -- either
9394 already allocated on it, or static. */
c9317f21 9395
9c6a1327
TT
9396static void
9397alloc_rust_variant (struct obstack *obstack, struct type *type,
9398 int discriminant_index, int default_index,
9399 gdb::array_view<discriminant_range> ranges)
9400{
57d02173
TT
9401 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9402 gdb_assert (discriminant_index == -1
9403 || (discriminant_index >= 0
9404 && discriminant_index < type->num_fields ()));
c9317f21 9405 gdb_assert (default_index == -1
1f704f76 9406 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9407
9c6a1327 9408 /* We have one variant for each non-discriminant field. */
57d02173
TT
9409 int n_variants = type->num_fields ();
9410 if (discriminant_index != -1)
9411 --n_variants;
c9317f21 9412
9c6a1327
TT
9413 variant *variants = new (obstack) variant[n_variants];
9414 int var_idx = 0;
9415 int range_idx = 0;
1f704f76 9416 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9417 {
9418 if (i == discriminant_index)
9419 continue;
c9317f21 9420
9c6a1327
TT
9421 variants[var_idx].first_field = i;
9422 variants[var_idx].last_field = i + 1;
9423
9424 /* The default field does not need a range, but other fields do.
9425 We skipped the discriminant above. */
9426 if (i != default_index)
9427 {
9428 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9429 ++range_idx;
9430 }
c9317f21 9431
9c6a1327
TT
9432 ++var_idx;
9433 }
9434
9435 gdb_assert (range_idx == ranges.size ());
9436 gdb_assert (var_idx == n_variants);
9437
9438 variant_part *part = new (obstack) variant_part;
9439 part->discriminant_index = discriminant_index;
57d02173
TT
9440 /* If there is no discriminant, then whether it is signed is of no
9441 consequence. */
9442 part->is_unsigned
9443 = (discriminant_index == -1
9444 ? false
c6d940a9 9445 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9446 part->variants = gdb::array_view<variant> (variants, n_variants);
9447
9448 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9449 gdb::array_view<variant_part> *prop_value
9450 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9451
9c6a1327 9452 struct dynamic_prop prop;
8c2e4e06 9453 prop.set_variant_parts (prop_value);
9c6a1327 9454
5c54719c 9455 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9456}
9457
9458/* Some versions of rustc emitted enums in an unusual way.
9459
9460 Ordinary enums were emitted as unions. The first element of each
9461 structure in the union was named "RUST$ENUM$DISR". This element
9462 held the discriminant.
9463
9464 These versions of Rust also implemented the "non-zero"
9465 optimization. When the enum had two values, and one is empty and
9466 the other holds a pointer that cannot be zero, the pointer is used
9467 as the discriminant, with a zero value meaning the empty variant.
9468 Here, the union's first member is of the form
9469 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9470 where the fieldnos are the indices of the fields that should be
9471 traversed in order to find the field (which may be several fields deep)
9472 and the variantname is the name of the variant of the case when the
9473 field is zero.
9474
9475 This function recognizes whether TYPE is of one of these forms,
9476 and, if so, smashes it to be a variant type. */
9477
9478static void
9479quirk_rust_enum (struct type *type, struct objfile *objfile)
9480{
78134374 9481 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9482
9483 /* We don't need to deal with empty enums. */
1f704f76 9484 if (type->num_fields () == 0)
c9317f21
TT
9485 return;
9486
9487#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9488 if (type->num_fields () == 1
c9317f21
TT
9489 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9490 {
9491 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9492
9493 /* Decode the field name to find the offset of the
9494 discriminant. */
9495 ULONGEST bit_offset = 0;
940da03e 9496 struct type *field_type = type->field (0).type ();
c9317f21
TT
9497 while (name[0] >= '0' && name[0] <= '9')
9498 {
9499 char *tail;
9500 unsigned long index = strtoul (name, &tail, 10);
9501 name = tail;
9502 if (*name != '$'
1f704f76 9503 || index >= field_type->num_fields ()
c9317f21
TT
9504 || (TYPE_FIELD_LOC_KIND (field_type, index)
9505 != FIELD_LOC_KIND_BITPOS))
9506 {
b98664d3 9507 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9508 "[in module %s]"),
9509 TYPE_FIELD_NAME (type, 0),
9510 objfile_name (objfile));
9511 return;
9512 }
9513 ++name;
9514
9515 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9516 field_type = field_type->field (index).type ();
c9317f21
TT
9517 }
9518
9c6a1327
TT
9519 /* Smash this type to be a structure type. We have to do this
9520 because the type has already been recorded. */
67607e24 9521 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9522 type->set_num_fields (3);
9c6a1327 9523 /* Save the field we care about. */
ceacbf6e 9524 struct field saved_field = type->field (0);
3cabb6b0
SM
9525 type->set_fields
9526 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9527
9c6a1327 9528 /* Put the discriminant at index 0. */
5d14b6e5 9529 type->field (0).set_type (field_type);
9c6a1327
TT
9530 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9531 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9532 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9533
9534 /* The order of fields doesn't really matter, so put the real
9535 field at index 1 and the data-less field at index 2. */
ceacbf6e 9536 type->field (1) = saved_field;
9c6a1327 9537 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9538 = rust_last_path_segment (type->field (1).type ()->name ());
9539 type->field (1).type ()->set_name
7d93a1e0 9540 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9541 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9542
9543 const char *dataless_name
7d93a1e0 9544 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9545 name);
9546 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9547 dataless_name);
5d14b6e5 9548 type->field (2).set_type (dataless_type);
c9317f21
TT
9549 /* NAME points into the original discriminant name, which
9550 already has the correct lifetime. */
9c6a1327 9551 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9552 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9553
9c6a1327
TT
9554 /* Indicate that this is a variant type. */
9555 static discriminant_range ranges[1] = { { 0, 0 } };
9556 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9557 }
77c2dba3
TT
9558 /* A union with a single anonymous field is probably an old-style
9559 univariant enum. */
1f704f76 9560 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9561 {
c9317f21
TT
9562 /* Smash this type to be a structure type. We have to do this
9563 because the type has already been recorded. */
67607e24 9564 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9565
940da03e 9566 struct type *field_type = type->field (0).type ();
c9317f21 9567 const char *variant_name
7d93a1e0 9568 = rust_last_path_segment (field_type->name ());
9c6a1327 9569 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9570 field_type->set_name
9571 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9572 type->name (), variant_name));
57d02173
TT
9573
9574 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9575 }
9576 else
9577 {
9578 struct type *disr_type = nullptr;
1f704f76 9579 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9580 {
940da03e 9581 disr_type = type->field (i).type ();
c9317f21 9582
78134374 9583 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9584 {
9585 /* All fields of a true enum will be structs. */
9586 return;
9587 }
1f704f76 9588 else if (disr_type->num_fields () == 0)
c9317f21
TT
9589 {
9590 /* Could be data-less variant, so keep going. */
a037790e 9591 disr_type = nullptr;
c9317f21
TT
9592 }
9593 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9594 "RUST$ENUM$DISR") != 0)
9595 {
9596 /* Not a Rust enum. */
9597 return;
9598 }
9599 else
9600 {
9601 /* Found one. */
9602 break;
9603 }
9604 }
9605
9606 /* If we got here without a discriminant, then it's probably
9607 just a union. */
9608 if (disr_type == nullptr)
9609 return;
9610
9611 /* Smash this type to be a structure type. We have to do this
9612 because the type has already been recorded. */
67607e24 9613 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9614
9c6a1327 9615 /* Make space for the discriminant field. */
ceacbf6e 9616 struct field *disr_field = &disr_type->field (0);
9c6a1327 9617 field *new_fields
1f704f76 9618 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9619 * sizeof (struct field)));
80fc5e77 9620 memcpy (new_fields + 1, type->fields (),
1f704f76 9621 type->num_fields () * sizeof (struct field));
3cabb6b0 9622 type->set_fields (new_fields);
1f704f76 9623 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9624
9625 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9626 type->field (0) = *disr_field;
9c6a1327
TT
9627 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9628 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9629
9630 /* We need a way to find the correct discriminant given a
9631 variant name. For convenience we build a map here. */
b6cdac4b 9632 struct type *enum_type = disr_field->type ();
c9317f21 9633 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9634 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9635 {
9636 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9637 {
9638 const char *name
9639 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9640 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9641 }
9642 }
9643
1f704f76 9644 int n_fields = type->num_fields ();
9c6a1327
TT
9645 /* We don't need a range entry for the discriminant, but we do
9646 need one for every other field, as there is no default
9647 variant. */
9648 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9649 discriminant_range,
9650 n_fields - 1);
c9317f21
TT
9651 /* Skip the discriminant here. */
9652 for (int i = 1; i < n_fields; ++i)
9653 {
9654 /* Find the final word in the name of this variant's type.
9655 That name can be used to look up the correct
9656 discriminant. */
9657 const char *variant_name
940da03e 9658 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9659
9660 auto iter = discriminant_map.find (variant_name);
9661 if (iter != discriminant_map.end ())
9c6a1327 9662 {
57d02173
TT
9663 ranges[i - 1].low = iter->second;
9664 ranges[i - 1].high = iter->second;
9c6a1327 9665 }
c9317f21 9666
57d02173
TT
9667 /* In Rust, each element should have the size of the
9668 enclosing enum. */
9669 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9670
bedda9ac 9671 /* Remove the discriminant field, if it exists. */
940da03e 9672 struct type *sub_type = type->field (i).type ();
1f704f76 9673 if (sub_type->num_fields () > 0)
bedda9ac 9674 {
5e33d5f4 9675 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9676 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9677 }
9c6a1327 9678 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9679 sub_type->set_name
9680 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9681 type->name (), variant_name));
c9317f21 9682 }
9c6a1327
TT
9683
9684 /* Indicate that this is a variant type. */
a1520ad8 9685 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9686 gdb::array_view<discriminant_range> (ranges,
9687 n_fields - 1));
c9317f21
TT
9688 }
9689}
9690
9691/* Rewrite some Rust unions to be structures with variants parts. */
9692
9693static void
9694rust_union_quirks (struct dwarf2_cu *cu)
9695{
9696 gdb_assert (cu->language == language_rust);
52941706 9697 for (type *type_ : cu->rust_unions)
5e22e966 9698 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9699 /* We don't need this any more. */
9700 cu->rust_unions.clear ();
c9317f21
TT
9701}
9702
8adb8487
TT
9703/* See read.h. */
9704
9705type_unit_group_unshareable *
9706dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9707{
9708 auto iter = this->m_type_units.find (tu_group);
9709 if (iter != this->m_type_units.end ())
9710 return iter->second.get ();
9711
9712 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9713 type_unit_group_unshareable *result = uniq.get ();
9714 this->m_type_units[tu_group] = std::move (uniq);
9715 return result;
9716}
9717
e286671b
TT
9718struct type *
9719dwarf2_per_objfile::get_type_for_signatured_type
9720 (signatured_type *sig_type) const
9721{
9722 auto iter = this->m_type_map.find (sig_type);
9723 if (iter == this->m_type_map.end ())
9724 return nullptr;
9725
9726 return iter->second;
9727}
9728
9729void dwarf2_per_objfile::set_type_for_signatured_type
9730 (signatured_type *sig_type, struct type *type)
9731{
9732 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9733
9734 this->m_type_map[sig_type] = type;
9735}
9736
95554aad
TT
9737/* A helper function for computing the list of all symbol tables
9738 included by PER_CU. */
9739
9740static void
4c39bc03 9741recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9742 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9743 dwarf2_per_cu_data *per_cu,
9744 dwarf2_per_objfile *per_objfile,
43f3e411 9745 struct compunit_symtab *immediate_parent)
95554aad 9746{
af758d11 9747 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9748 if (*slot != NULL)
9749 {
9750 /* This inclusion and its children have been processed. */
9751 return;
9752 }
9753
9754 *slot = per_cu;
af758d11 9755
95554aad 9756 /* Only add a CU if it has a symbol table. */
43182c09 9757 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9758 if (cust != NULL)
ec94af83
DE
9759 {
9760 /* If this is a type unit only add its symbol table if we haven't
9761 seen it yet (type unit per_cu's can share symtabs). */
9762 if (per_cu->is_debug_types)
9763 {
43f3e411 9764 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9765 if (*slot == NULL)
9766 {
43f3e411 9767 *slot = cust;
4c39bc03 9768 result->push_back (cust);
43f3e411
DE
9769 if (cust->user == NULL)
9770 cust->user = immediate_parent;
ec94af83
DE
9771 }
9772 }
9773 else
f9125b6c 9774 {
4c39bc03 9775 result->push_back (cust);
43f3e411
DE
9776 if (cust->user == NULL)
9777 cust->user = immediate_parent;
f9125b6c 9778 }
ec94af83 9779 }
95554aad 9780
ae640021
AB
9781 if (!per_cu->imported_symtabs_empty ())
9782 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9783 {
9784 recursively_compute_inclusions (result, all_children,
43182c09
SM
9785 all_type_symtabs, ptr, per_objfile,
9786 cust);
ae640021 9787 }
95554aad
TT
9788}
9789
43f3e411 9790/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9791 PER_CU. */
9792
9793static void
43182c09
SM
9794compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9795 dwarf2_per_objfile *per_objfile)
95554aad 9796{
f4dc4d17
DE
9797 gdb_assert (! per_cu->is_debug_types);
9798
ae640021 9799 if (!per_cu->imported_symtabs_empty ())
95554aad 9800 {
ae640021 9801 int len;
4c39bc03 9802 std::vector<compunit_symtab *> result_symtabs;
43182c09 9803 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9804
9805 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9806 if (cust == NULL)
95554aad
TT
9807 return;
9808
280a9412
TT
9809 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9810 htab_eq_pointer,
9811 NULL, xcalloc, xfree));
9812 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9813 htab_eq_pointer,
9814 NULL, xcalloc, xfree));
95554aad 9815
ae640021 9816 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9817 {
280a9412
TT
9818 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9819 all_type_symtabs.get (), ptr,
9820 per_objfile, cust);
ec94af83 9821 }
95554aad 9822
ec94af83 9823 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9824 len = result_symtabs.size ();
43f3e411 9825 cust->includes
f6e649dd 9826 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9827 struct compunit_symtab *, len + 1);
4c39bc03
TT
9828 memcpy (cust->includes, result_symtabs.data (),
9829 len * sizeof (compunit_symtab *));
43f3e411 9830 cust->includes[len] = NULL;
95554aad
TT
9831 }
9832}
9833
9834/* Compute the 'includes' field for the symtabs of all the CUs we just
9835 read. */
9836
9837static void
976ca316 9838process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9839{
976ca316 9840 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9841 {
9842 if (! iter->is_debug_types)
976ca316 9843 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9844 }
95554aad 9845
976ca316 9846 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9847}
9848
8fc0b21d 9849/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9850 already been loaded into memory. */
9851
9852static void
8fc0b21d 9853process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9854{
976ca316
SM
9855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9856 struct objfile *objfile = per_objfile->objfile;
08feed99 9857 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9858 CORE_ADDR lowpc, highpc;
43f3e411 9859 struct compunit_symtab *cust;
10b3939b 9860 CORE_ADDR baseaddr;
4359dff1 9861 struct block *static_block;
3e29f34a 9862 CORE_ADDR addr;
10b3939b 9863
b3b3bada 9864 baseaddr = objfile->text_section_offset ();
10b3939b 9865
c89b44cd
TT
9866 /* Clear the list here in case something was left over. */
9867 cu->method_list.clear ();
10b3939b 9868
95554aad
TT
9869 cu->language = pretend_language;
9870 cu->language_defn = language_def (cu->language);
9871
1c47ec3e
TV
9872 dwarf2_find_base_address (cu->dies, cu);
9873
c906108c 9874 /* Do line number decoding in read_file_scope () */
10b3939b 9875 process_die (cu->dies, cu);
c906108c 9876
a766d390
DE
9877 /* For now fudge the Go package. */
9878 if (cu->language == language_go)
9879 fixup_go_packaging (cu);
9880
5f48f8f3 9881 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9882 should be complete, and it should now be safe to compute all of the
9883 physnames. */
9884 compute_delayed_physnames (cu);
3da10d80 9885
c9317f21
TT
9886 if (cu->language == language_rust)
9887 rust_union_quirks (cu);
9888
fae299cd
DC
9889 /* Some compilers don't define a DW_AT_high_pc attribute for the
9890 compilation unit. If the DW_AT_high_pc is missing, synthesize
9891 it, by scanning the DIE's below the compilation unit. */
10b3939b 9892 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9893
3e29f34a 9894 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9895 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9896
9897 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9898 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9899 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9900 addrmap to help ensure it has an accurate map of pc values belonging to
9901 this comp unit. */
9902 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9903
c24bdb02 9904 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9905 SECT_OFF_TEXT (objfile),
9906 0);
c906108c 9907
43f3e411 9908 if (cust != NULL)
c906108c 9909 {
df15bd07 9910 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9911
8be455d7
JK
9912 /* Set symtab language to language from DW_AT_language. If the
9913 compilation is from a C file generated by language preprocessors, do
9914 not set the language if it was already deduced by start_subfile. */
43f3e411 9915 if (!(cu->language == language_c
40e3ad0e 9916 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9917 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9918
9919 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9920 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9921 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9922 there were bugs in prologue debug info, fixed later in GCC-4.5
9923 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9924
9925 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9926 needed, it would be wrong due to missing DW_AT_producer there.
9927
9928 Still one can confuse GDB by using non-standard GCC compilation
9929 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9930 */
ab260dad 9931 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9932 cust->locations_valid = 1;
e0d00bc7
JK
9933
9934 if (gcc_4_minor >= 5)
43f3e411 9935 cust->epilogue_unwind_valid = 1;
96408a79 9936
43f3e411 9937 cust->call_site_htab = cu->call_site_htab;
c906108c 9938 }
9291a0cd 9939
976ca316 9940 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9941
95554aad 9942 /* Push it for inclusion processing later. */
976ca316 9943 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9944
9945 /* Not needed any more. */
c24bdb02 9946 cu->reset_builder ();
f4dc4d17 9947}
45cfd468 9948
8fc0b21d 9949/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9950 already been loaded into memory. */
9951
9952static void
8fc0b21d 9953process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9954 enum language pretend_language)
9955{
976ca316
SM
9956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9957 struct objfile *objfile = per_objfile->objfile;
43f3e411 9958 struct compunit_symtab *cust;
0186c6a7
DE
9959 struct signatured_type *sig_type;
9960
8fc0b21d
SM
9961 gdb_assert (cu->per_cu->is_debug_types);
9962 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9963
c89b44cd
TT
9964 /* Clear the list here in case something was left over. */
9965 cu->method_list.clear ();
f4dc4d17 9966
f4dc4d17
DE
9967 cu->language = pretend_language;
9968 cu->language_defn = language_def (cu->language);
9969
9970 /* The symbol tables are set up in read_type_unit_scope. */
9971 process_die (cu->dies, cu);
9972
9973 /* For now fudge the Go package. */
9974 if (cu->language == language_go)
9975 fixup_go_packaging (cu);
9976
5f48f8f3 9977 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9978 should be complete, and it should now be safe to compute all of the
9979 physnames. */
9980 compute_delayed_physnames (cu);
f4dc4d17 9981
c9317f21
TT
9982 if (cu->language == language_rust)
9983 rust_union_quirks (cu);
9984
f4dc4d17
DE
9985 /* TUs share symbol tables.
9986 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9987 of it with end_expandable_symtab. Otherwise, complete the addition of
9988 this TU's symbols to the existing symtab. */
8adb8487 9989 type_unit_group_unshareable *tug_unshare =
976ca316 9990 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 9991 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9992 {
c24bdb02
KS
9993 buildsym_compunit *builder = cu->get_builder ();
9994 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9995 tug_unshare->compunit_symtab = cust;
f4dc4d17 9996
43f3e411 9997 if (cust != NULL)
f4dc4d17
DE
9998 {
9999 /* Set symtab language to language from DW_AT_language. If the
10000 compilation is from a C file generated by language preprocessors,
10001 do not set the language if it was already deduced by
10002 start_subfile. */
43f3e411
DE
10003 if (!(cu->language == language_c
10004 && COMPUNIT_FILETABS (cust)->language != language_c))
10005 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10006 }
10007 }
10008 else
10009 {
c24bdb02 10010 cu->get_builder ()->augment_type_symtab ();
8adb8487 10011 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10012 }
10013
976ca316 10014 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10015
10016 /* Not needed any more. */
c24bdb02 10017 cu->reset_builder ();
c906108c
SS
10018}
10019
95554aad
TT
10020/* Process an imported unit DIE. */
10021
10022static void
10023process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10024{
10025 struct attribute *attr;
10026
f4dc4d17
DE
10027 /* For now we don't handle imported units in type units. */
10028 if (cu->per_cu->is_debug_types)
10029 {
10030 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10031 " supported in type units [in module %s]"),
5e22e966 10032 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10033 }
10034
95554aad
TT
10035 attr = dwarf2_attr (die, DW_AT_import, cu);
10036 if (attr != NULL)
10037 {
0826b30a 10038 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10039 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10040 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10041 dwarf2_per_cu_data *per_cu
ab432490 10042 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10043
58990295
TV
10044 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10045 into another compilation unit, at root level. Regard this as a hint,
10046 and ignore it. */
10047 if (die->parent && die->parent->parent == NULL
10048 && per_cu->unit_type == DW_UT_compile
10049 && per_cu->lang == language_cplus)
10050 return;
10051
69d751e3 10052 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10053 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10054 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10055 false, cu->language);
95554aad 10056
ae640021 10057 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10058 }
10059}
10060
4c8aa72d
PA
10061/* RAII object that represents a process_die scope: i.e.,
10062 starts/finishes processing a DIE. */
10063class process_die_scope
adde2bff 10064{
4c8aa72d
PA
10065public:
10066 process_die_scope (die_info *die, dwarf2_cu *cu)
10067 : m_die (die), m_cu (cu)
10068 {
10069 /* We should only be processing DIEs not already in process. */
10070 gdb_assert (!m_die->in_process);
10071 m_die->in_process = true;
10072 }
8c3cb9fa 10073
4c8aa72d
PA
10074 ~process_die_scope ()
10075 {
10076 m_die->in_process = false;
10077
10078 /* If we're done processing the DIE for the CU that owns the line
10079 header, we don't need the line header anymore. */
10080 if (m_cu->line_header_die_owner == m_die)
10081 {
10082 delete m_cu->line_header;
10083 m_cu->line_header = NULL;
10084 m_cu->line_header_die_owner = NULL;
10085 }
10086 }
10087
10088private:
10089 die_info *m_die;
10090 dwarf2_cu *m_cu;
10091};
adde2bff 10092
c906108c
SS
10093/* Process a die and its children. */
10094
10095static void
e7c27a73 10096process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10097{
4c8aa72d 10098 process_die_scope scope (die, cu);
adde2bff 10099
c906108c
SS
10100 switch (die->tag)
10101 {
10102 case DW_TAG_padding:
10103 break;
10104 case DW_TAG_compile_unit:
95554aad 10105 case DW_TAG_partial_unit:
e7c27a73 10106 read_file_scope (die, cu);
c906108c 10107 break;
348e048f
DE
10108 case DW_TAG_type_unit:
10109 read_type_unit_scope (die, cu);
10110 break;
c906108c 10111 case DW_TAG_subprogram:
0a4b0913
AB
10112 /* Nested subprograms in Fortran get a prefix. */
10113 if (cu->language == language_fortran
10114 && die->parent != NULL
10115 && die->parent->tag == DW_TAG_subprogram)
10116 cu->processing_has_namespace_info = true;
10117 /* Fall through. */
c906108c 10118 case DW_TAG_inlined_subroutine:
edb3359d 10119 read_func_scope (die, cu);
c906108c
SS
10120 break;
10121 case DW_TAG_lexical_block:
14898363
L
10122 case DW_TAG_try_block:
10123 case DW_TAG_catch_block:
e7c27a73 10124 read_lexical_block_scope (die, cu);
c906108c 10125 break;
216f72a1 10126 case DW_TAG_call_site:
96408a79
SA
10127 case DW_TAG_GNU_call_site:
10128 read_call_site_scope (die, cu);
10129 break;
c906108c 10130 case DW_TAG_class_type:
680b30c7 10131 case DW_TAG_interface_type:
c906108c
SS
10132 case DW_TAG_structure_type:
10133 case DW_TAG_union_type:
134d01f1 10134 process_structure_scope (die, cu);
c906108c
SS
10135 break;
10136 case DW_TAG_enumeration_type:
134d01f1 10137 process_enumeration_scope (die, cu);
c906108c 10138 break;
134d01f1 10139
f792889a
DJ
10140 /* These dies have a type, but processing them does not create
10141 a symbol or recurse to process the children. Therefore we can
10142 read them on-demand through read_type_die. */
c906108c 10143 case DW_TAG_subroutine_type:
72019c9c 10144 case DW_TAG_set_type:
c906108c 10145 case DW_TAG_pointer_type:
c906108c 10146 case DW_TAG_ptr_to_member_type:
c906108c 10147 case DW_TAG_reference_type:
4297a3f0 10148 case DW_TAG_rvalue_reference_type:
c906108c 10149 case DW_TAG_string_type:
c906108c 10150 break;
134d01f1 10151
d8f62e84
TT
10152 case DW_TAG_array_type:
10153 /* We only need to handle this case for Ada -- in other
10154 languages, it's normal for the compiler to emit a typedef
10155 instead. */
10156 if (cu->language != language_ada)
10157 break;
10158 /* FALLTHROUGH */
c906108c 10159 case DW_TAG_base_type:
a02abb62 10160 case DW_TAG_subrange_type:
cb249c71 10161 case DW_TAG_typedef:
134d01f1 10162 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10163 DW_AT_name. */
f792889a 10164 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10165 break;
c906108c 10166 case DW_TAG_common_block:
e7c27a73 10167 read_common_block (die, cu);
c906108c
SS
10168 break;
10169 case DW_TAG_common_inclusion:
10170 break;
d9fa45fe 10171 case DW_TAG_namespace:
9068261f 10172 cu->processing_has_namespace_info = true;
e7c27a73 10173 read_namespace (die, cu);
d9fa45fe 10174 break;
5d7cb8df 10175 case DW_TAG_module:
9068261f 10176 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10177 read_module (die, cu);
10178 break;
d9fa45fe 10179 case DW_TAG_imported_declaration:
9068261f 10180 cu->processing_has_namespace_info = true;
74921315
KS
10181 if (read_namespace_alias (die, cu))
10182 break;
86a73007
TT
10183 /* The declaration is not a global namespace alias. */
10184 /* Fall through. */
d9fa45fe 10185 case DW_TAG_imported_module:
9068261f 10186 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10187 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10188 || cu->language != language_fortran))
b98664d3 10189 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10190 dwarf_tag_name (die->tag));
10191 read_import_statement (die, cu);
d9fa45fe 10192 break;
95554aad
TT
10193
10194 case DW_TAG_imported_unit:
10195 process_imported_unit_die (die, cu);
10196 break;
10197
71a3c369
TT
10198 case DW_TAG_variable:
10199 read_variable (die, cu);
10200 break;
10201
c906108c 10202 default:
e7c27a73 10203 new_symbol (die, NULL, cu);
c906108c
SS
10204 break;
10205 }
10206}
ca69b9e6
DE
10207\f
10208/* DWARF name computation. */
c906108c 10209
94af9270
KS
10210/* A helper function for dwarf2_compute_name which determines whether DIE
10211 needs to have the name of the scope prepended to the name listed in the
10212 die. */
10213
10214static int
10215die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10216{
1c809c68
TT
10217 struct attribute *attr;
10218
94af9270
KS
10219 switch (die->tag)
10220 {
10221 case DW_TAG_namespace:
10222 case DW_TAG_typedef:
10223 case DW_TAG_class_type:
10224 case DW_TAG_interface_type:
10225 case DW_TAG_structure_type:
10226 case DW_TAG_union_type:
10227 case DW_TAG_enumeration_type:
10228 case DW_TAG_enumerator:
10229 case DW_TAG_subprogram:
08a76f8a 10230 case DW_TAG_inlined_subroutine:
94af9270 10231 case DW_TAG_member:
74921315 10232 case DW_TAG_imported_declaration:
94af9270
KS
10233 return 1;
10234
10235 case DW_TAG_variable:
c2b0a229 10236 case DW_TAG_constant:
94af9270
KS
10237 /* We only need to prefix "globally" visible variables. These include
10238 any variable marked with DW_AT_external or any variable that
10239 lives in a namespace. [Variables in anonymous namespaces
10240 require prefixing, but they are not DW_AT_external.] */
10241
10242 if (dwarf2_attr (die, DW_AT_specification, cu))
10243 {
10244 struct dwarf2_cu *spec_cu = cu;
9a619af0 10245
94af9270
KS
10246 return die_needs_namespace (die_specification (die, &spec_cu),
10247 spec_cu);
10248 }
10249
1c809c68 10250 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10251 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10252 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10253 return 0;
10254 /* A variable in a lexical block of some kind does not need a
10255 namespace, even though in C++ such variables may be external
10256 and have a mangled name. */
10257 if (die->parent->tag == DW_TAG_lexical_block
10258 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10259 || die->parent->tag == DW_TAG_catch_block
10260 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10261 return 0;
10262 return 1;
94af9270
KS
10263
10264 default:
10265 return 0;
10266 }
10267}
10268
73b9be8b
KS
10269/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10270 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10271 defined for the given DIE. */
10272
10273static struct attribute *
10274dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10275{
10276 struct attribute *attr;
10277
10278 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10279 if (attr == NULL)
10280 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10281
10282 return attr;
10283}
10284
10285/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10286 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10287 defined for the given DIE. */
10288
10289static const char *
10290dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10291{
10292 const char *linkage_name;
10293
10294 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10295 if (linkage_name == NULL)
10296 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10297
787de330
TT
10298 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10299 See https://github.com/rust-lang/rust/issues/32925. */
10300 if (cu->language == language_rust && linkage_name != NULL
10301 && strchr (linkage_name, '{') != NULL)
10302 linkage_name = NULL;
10303
73b9be8b
KS
10304 return linkage_name;
10305}
10306
94af9270 10307/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10308 compute the physname for the object, which include a method's:
9c37b5ae 10309 - formal parameters (C++),
a766d390 10310 - receiver type (Go),
a766d390
DE
10311
10312 The term "physname" is a bit confusing.
10313 For C++, for example, it is the demangled name.
10314 For Go, for example, it's the mangled name.
94af9270 10315
af6b7be1
JB
10316 For Ada, return the DIE's linkage name rather than the fully qualified
10317 name. PHYSNAME is ignored..
10318
5989a64e 10319 The result is allocated on the objfile->per_bfd's obstack and
45940949 10320 canonicalized. */
94af9270
KS
10321
10322static const char *
15d034d0
TT
10323dwarf2_compute_name (const char *name,
10324 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10325 int physname)
10326{
5e22e966 10327 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10328
94af9270
KS
10329 if (name == NULL)
10330 name = dwarf2_name (die, cu);
10331
2ee7123e
DE
10332 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10333 but otherwise compute it by typename_concat inside GDB.
10334 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10335 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10336 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10337 will set the demangled name to the result of dwarf2_full_name, and it is
10338 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10339 if (cu->language == language_ada
10340 || (cu->language == language_fortran && physname))
10341 {
10342 /* For Ada unit, we prefer the linkage name over the name, as
10343 the former contains the exported name, which the user expects
10344 to be able to reference. Ideally, we want the user to be able
10345 to reference this entity using either natural or linkage name,
10346 but we haven't started looking at this enhancement yet. */
73b9be8b 10347 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10348
2ee7123e
DE
10349 if (linkage_name != NULL)
10350 return linkage_name;
f55ee35c
JK
10351 }
10352
94af9270
KS
10353 /* These are the only languages we know how to qualify names in. */
10354 if (name != NULL
9c37b5ae 10355 && (cu->language == language_cplus
c44af4eb
TT
10356 || cu->language == language_fortran || cu->language == language_d
10357 || cu->language == language_rust))
94af9270
KS
10358 {
10359 if (die_needs_namespace (die, cu))
10360 {
0d5cff50 10361 const char *prefix;
34a68019 10362 const char *canonical_name = NULL;
94af9270 10363
d7e74731
PA
10364 string_file buf;
10365
94af9270 10366 prefix = determine_prefix (die, cu);
94af9270
KS
10367 if (*prefix != '\0')
10368 {
43816ebc
TT
10369 gdb::unique_xmalloc_ptr<char> prefixed_name
10370 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10371
43816ebc 10372 buf.puts (prefixed_name.get ());
94af9270
KS
10373 }
10374 else
d7e74731 10375 buf.puts (name);
94af9270 10376
98bfdba5
PA
10377 /* Template parameters may be specified in the DIE's DW_AT_name, or
10378 as children with DW_TAG_template_type_param or
10379 DW_TAG_value_type_param. If the latter, add them to the name
10380 here. If the name already has template parameters, then
10381 skip this step; some versions of GCC emit both, and
10382 it is more efficient to use the pre-computed name.
10383
10384 Something to keep in mind about this process: it is very
10385 unlikely, or in some cases downright impossible, to produce
10386 something that will match the mangled name of a function.
10387 If the definition of the function has the same debug info,
10388 we should be able to match up with it anyway. But fallbacks
10389 using the minimal symbol, for instance to find a method
10390 implemented in a stripped copy of libstdc++, will not work.
10391 If we do not have debug info for the definition, we will have to
10392 match them up some other way.
10393
10394 When we do name matching there is a related problem with function
10395 templates; two instantiated function templates are allowed to
10396 differ only by their return types, which we do not add here. */
10397
10398 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10399 {
10400 struct attribute *attr;
10401 struct die_info *child;
10402 int first = 1;
2c75ccb2 10403 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10404
10405 die->building_fullname = 1;
10406
10407 for (child = die->child; child != NULL; child = child->sibling)
10408 {
10409 struct type *type;
12df843f 10410 LONGEST value;
d521ce57 10411 const gdb_byte *bytes;
98bfdba5
PA
10412 struct dwarf2_locexpr_baton *baton;
10413 struct value *v;
10414
10415 if (child->tag != DW_TAG_template_type_param
10416 && child->tag != DW_TAG_template_value_param)
10417 continue;
10418
10419 if (first)
10420 {
d7e74731 10421 buf.puts ("<");
98bfdba5
PA
10422 first = 0;
10423 }
10424 else
d7e74731 10425 buf.puts (", ");
98bfdba5
PA
10426
10427 attr = dwarf2_attr (child, DW_AT_type, cu);
10428 if (attr == NULL)
10429 {
b98664d3 10430 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10431 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10432 continue;
10433 }
10434 type = die_type (child, cu);
10435
10436 if (child->tag == DW_TAG_template_type_param)
10437 {
2c75ccb2
AB
10438 cplus_lang->print_type (type, "", &buf, -1, 0,
10439 &type_print_raw_options);
98bfdba5
PA
10440 continue;
10441 }
10442
10443 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10444 if (attr == NULL)
10445 {
b98664d3 10446 complaint (_("template parameter missing "
3e43a32a 10447 "DW_AT_const_value"));
d7e74731 10448 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10449 continue;
10450 }
10451
10452 dwarf2_const_value_attr (attr, type, name,
10453 &cu->comp_unit_obstack, cu,
10454 &value, &bytes, &baton);
10455
20ce4123 10456 if (type->has_no_signedness ())
98bfdba5
PA
10457 /* GDB prints characters as NUMBER 'CHAR'. If that's
10458 changed, this can use value_print instead. */
2c75ccb2 10459 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10460 else
10461 {
10462 struct value_print_options opts;
10463
10464 if (baton != NULL)
10465 v = dwarf2_evaluate_loc_desc (type, NULL,
10466 baton->data,
10467 baton->size,
9f47c707
SM
10468 baton->per_cu,
10469 baton->per_objfile);
98bfdba5
PA
10470 else if (bytes != NULL)
10471 {
10472 v = allocate_value (type);
10473 memcpy (value_contents_writeable (v), bytes,
10474 TYPE_LENGTH (type));
10475 }
10476 else
10477 v = value_from_longest (type, value);
10478
3e43a32a
MS
10479 /* Specify decimal so that we do not depend on
10480 the radix. */
98bfdba5
PA
10481 get_formatted_print_options (&opts, 'd');
10482 opts.raw = 1;
d7e74731 10483 value_print (v, &buf, &opts);
98bfdba5 10484 release_value (v);
98bfdba5
PA
10485 }
10486 }
10487
10488 die->building_fullname = 0;
10489
10490 if (!first)
10491 {
10492 /* Close the argument list, with a space if necessary
10493 (nested templates). */
d7e74731
PA
10494 if (!buf.empty () && buf.string ().back () == '>')
10495 buf.puts (" >");
98bfdba5 10496 else
d7e74731 10497 buf.puts (">");
98bfdba5
PA
10498 }
10499 }
10500
9c37b5ae 10501 /* For C++ methods, append formal parameter type
94af9270 10502 information, if PHYSNAME. */
6e70227d 10503
94af9270 10504 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10505 && cu->language == language_cplus)
94af9270
KS
10506 {
10507 struct type *type = read_type_die (die, cu);
10508
d7e74731 10509 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10510 &type_print_raw_options);
94af9270 10511
9c37b5ae 10512 if (cu->language == language_cplus)
94af9270 10513 {
60430eff
DJ
10514 /* Assume that an artificial first parameter is
10515 "this", but do not crash if it is not. RealView
10516 marks unnamed (and thus unused) parameters as
10517 artificial; there is no way to differentiate
10518 the two cases. */
1f704f76 10519 if (type->num_fields () > 0
94af9270 10520 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10521 && type->field (0).type ()->code () == TYPE_CODE_PTR
10522 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10523 buf.puts (" const");
94af9270
KS
10524 }
10525 }
10526
d7e74731 10527 const std::string &intermediate_name = buf.string ();
94af9270
KS
10528
10529 if (cu->language == language_cplus)
34a68019 10530 canonical_name
322a8516 10531 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10532 objfile);
34a68019
TT
10533
10534 /* If we only computed INTERMEDIATE_NAME, or if
10535 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10536 intern it. */
322a8516 10537 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10538 name = objfile->intern (intermediate_name);
34a68019
TT
10539 else
10540 name = canonical_name;
94af9270
KS
10541 }
10542 }
10543
10544 return name;
10545}
10546
0114d602
DJ
10547/* Return the fully qualified name of DIE, based on its DW_AT_name.
10548 If scope qualifiers are appropriate they will be added. The result
34a68019 10549 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10550 not have a name. NAME may either be from a previous call to
10551 dwarf2_name or NULL.
10552
9c37b5ae 10553 The output string will be canonicalized (if C++). */
0114d602
DJ
10554
10555static const char *
15d034d0 10556dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10557{
94af9270
KS
10558 return dwarf2_compute_name (name, die, cu, 0);
10559}
0114d602 10560
94af9270
KS
10561/* Construct a physname for the given DIE in CU. NAME may either be
10562 from a previous call to dwarf2_name or NULL. The result will be
10563 allocated on the objfile_objstack or NULL if the DIE does not have a
10564 name.
0114d602 10565
9c37b5ae 10566 The output string will be canonicalized (if C++). */
0114d602 10567
94af9270 10568static const char *
15d034d0 10569dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10570{
5e22e966 10571 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10572 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10573 int need_copy = 1;
10574
10575 /* In this case dwarf2_compute_name is just a shortcut not building anything
10576 on its own. */
10577 if (!die_needs_namespace (die, cu))
10578 return dwarf2_compute_name (name, die, cu, 1);
10579
906bb4c5
TT
10580 if (cu->language != language_rust)
10581 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10582
10583 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10584 has computed. */
791afaa2 10585 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10586 if (mangled != NULL)
900e11f9 10587 {
900e11f9 10588
d3355e4d 10589 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10590 {
10591 /* Do nothing (do not demangle the symbol name). */
10592 }
a766d390
DE
10593 else
10594 {
0eb876f5
JB
10595 /* Use DMGL_RET_DROP for C++ template functions to suppress
10596 their return type. It is easier for GDB users to search
10597 for such functions as `name(params)' than `long name(params)'.
10598 In such case the minimal symbol names do not match the full
10599 symbol names but for template functions there is never a need
10600 to look up their definition from their declaration so
10601 the only disadvantage remains the minimal symbol variant
10602 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10603 demangled.reset (gdb_demangle (mangled,
10604 (DMGL_PARAMS | DMGL_ANSI
10605 | DMGL_RET_DROP)));
a766d390 10606 }
900e11f9 10607 if (demangled)
791afaa2 10608 canon = demangled.get ();
900e11f9
JK
10609 else
10610 {
10611 canon = mangled;
10612 need_copy = 0;
10613 }
10614 }
10615
10616 if (canon == NULL || check_physname)
10617 {
10618 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10619
10620 if (canon != NULL && strcmp (physname, canon) != 0)
10621 {
10622 /* It may not mean a bug in GDB. The compiler could also
10623 compute DW_AT_linkage_name incorrectly. But in such case
10624 GDB would need to be bug-to-bug compatible. */
10625
b98664d3 10626 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10627 "(from linkage <%s>) - DIE at %s [in module %s]"),
10628 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10629 objfile_name (objfile));
900e11f9
JK
10630
10631 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10632 is available here - over computed PHYSNAME. It is safer
10633 against both buggy GDB and buggy compilers. */
10634
10635 retval = canon;
10636 }
10637 else
10638 {
10639 retval = physname;
10640 need_copy = 0;
10641 }
10642 }
10643 else
10644 retval = canon;
10645
10646 if (need_copy)
be1e3d3e 10647 retval = objfile->intern (retval);
900e11f9 10648
900e11f9 10649 return retval;
0114d602
DJ
10650}
10651
74921315
KS
10652/* Inspect DIE in CU for a namespace alias. If one exists, record
10653 a new symbol for it.
10654
10655 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10656
10657static int
10658read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10659{
10660 struct attribute *attr;
10661
10662 /* If the die does not have a name, this is not a namespace
10663 alias. */
10664 attr = dwarf2_attr (die, DW_AT_name, cu);
10665 if (attr != NULL)
10666 {
10667 int num;
10668 struct die_info *d = die;
10669 struct dwarf2_cu *imported_cu = cu;
10670
10671 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10672 keep inspecting DIEs until we hit the underlying import. */
10673#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10674 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10675 {
10676 attr = dwarf2_attr (d, DW_AT_import, cu);
10677 if (attr == NULL)
10678 break;
10679
10680 d = follow_die_ref (d, attr, &imported_cu);
10681 if (d->tag != DW_TAG_imported_declaration)
10682 break;
10683 }
10684
10685 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10686 {
b98664d3 10687 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10688 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10689 return 0;
10690 }
10691
10692 if (attr != NULL)
10693 {
10694 struct type *type;
0826b30a 10695 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10696
aa66c379 10697 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10698 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10699 {
10700 /* This declaration is a global namespace alias. Add
10701 a symbol for it whose type is the aliased namespace. */
10702 new_symbol (die, type, cu);
10703 return 1;
10704 }
10705 }
10706 }
10707
10708 return 0;
10709}
10710
22cee43f 10711/* Return the using directives repository (global or local?) to use in the
804d2729 10712 current context for CU.
22cee43f
PMR
10713
10714 For Ada, imported declarations can materialize renamings, which *may* be
10715 global. However it is impossible (for now?) in DWARF to distinguish
10716 "external" imported declarations and "static" ones. As all imported
10717 declarations seem to be static in all other languages, make them all CU-wide
10718 global only in Ada. */
10719
10720static struct using_direct **
804d2729 10721using_directives (struct dwarf2_cu *cu)
22cee43f 10722{
c24bdb02
KS
10723 if (cu->language == language_ada
10724 && cu->get_builder ()->outermost_context_p ())
10725 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10726 else
c24bdb02 10727 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10728}
10729
27aa8d6a
SW
10730/* Read the import statement specified by the given die and record it. */
10731
10732static void
10733read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10734{
5e22e966 10735 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10736 struct attribute *import_attr;
32019081 10737 struct die_info *imported_die, *child_die;
de4affc9 10738 struct dwarf2_cu *imported_cu;
27aa8d6a 10739 const char *imported_name;
794684b6 10740 const char *imported_name_prefix;
13387711
SW
10741 const char *canonical_name;
10742 const char *import_alias;
10743 const char *imported_declaration = NULL;
794684b6 10744 const char *import_prefix;
eb1e02fd 10745 std::vector<const char *> excludes;
13387711 10746
27aa8d6a
SW
10747 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10748 if (import_attr == NULL)
10749 {
b98664d3 10750 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10751 dwarf_tag_name (die->tag));
10752 return;
10753 }
10754
de4affc9
CC
10755 imported_cu = cu;
10756 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10757 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10758 if (imported_name == NULL)
10759 {
10760 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10761
dda83cd7
SM
10762 The import in the following code:
10763 namespace A
10764 {
10765 typedef int B;
10766 }
10767
10768 int main ()
10769 {
10770 using A::B;
10771 B b;
10772 return b;
10773 }
10774
10775 ...
10776 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10777 <52> DW_AT_decl_file : 1
10778 <53> DW_AT_decl_line : 6
10779 <54> DW_AT_import : <0x75>
10780 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10781 <59> DW_AT_name : B
10782 <5b> DW_AT_decl_file : 1
10783 <5c> DW_AT_decl_line : 2
10784 <5d> DW_AT_type : <0x6e>
10785 ...
10786 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10787 <76> DW_AT_byte_size : 4
10788 <77> DW_AT_encoding : 5 (signed)
10789
10790 imports the wrong die ( 0x75 instead of 0x58 ).
10791 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10792 return;
10793 }
10794
82856980
SW
10795 /* Figure out the local name after import. */
10796 import_alias = dwarf2_name (die, cu);
27aa8d6a 10797
794684b6
SW
10798 /* Figure out where the statement is being imported to. */
10799 import_prefix = determine_prefix (die, cu);
10800
10801 /* Figure out what the scope of the imported die is and prepend it
10802 to the name of the imported die. */
de4affc9 10803 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10804
f55ee35c
JK
10805 if (imported_die->tag != DW_TAG_namespace
10806 && imported_die->tag != DW_TAG_module)
794684b6 10807 {
13387711
SW
10808 imported_declaration = imported_name;
10809 canonical_name = imported_name_prefix;
794684b6 10810 }
13387711 10811 else if (strlen (imported_name_prefix) > 0)
12aaed36 10812 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10813 imported_name_prefix,
10814 (cu->language == language_d ? "." : "::"),
10815 imported_name, (char *) NULL);
13387711
SW
10816 else
10817 canonical_name = imported_name;
794684b6 10818
32019081
JK
10819 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10820 for (child_die = die->child; child_die && child_die->tag;
436c571c 10821 child_die = child_die->sibling)
32019081
JK
10822 {
10823 /* DWARF-4: A Fortran use statement with a “rename list” may be
10824 represented by an imported module entry with an import attribute
10825 referring to the module and owned entries corresponding to those
10826 entities that are renamed as part of being imported. */
10827
10828 if (child_die->tag != DW_TAG_imported_declaration)
10829 {
b98664d3 10830 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10831 "- DIE at %s [in module %s]"),
10832 sect_offset_str (child_die->sect_off),
10833 objfile_name (objfile));
32019081
JK
10834 continue;
10835 }
10836
10837 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10838 if (import_attr == NULL)
10839 {
b98664d3 10840 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10841 dwarf_tag_name (child_die->tag));
10842 continue;
10843 }
10844
10845 imported_cu = cu;
10846 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10847 &imported_cu);
10848 imported_name = dwarf2_name (imported_die, imported_cu);
10849 if (imported_name == NULL)
10850 {
b98664d3 10851 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10852 "imported name - DIE at %s [in module %s]"),
10853 sect_offset_str (child_die->sect_off),
10854 objfile_name (objfile));
32019081
JK
10855 continue;
10856 }
10857
eb1e02fd 10858 excludes.push_back (imported_name);
32019081
JK
10859
10860 process_die (child_die, cu);
10861 }
10862
804d2729 10863 add_using_directive (using_directives (cu),
22cee43f
PMR
10864 import_prefix,
10865 canonical_name,
10866 import_alias,
10867 imported_declaration,
10868 excludes,
10869 0,
10870 &objfile->objfile_obstack);
27aa8d6a
SW
10871}
10872
5230b05a
WT
10873/* ICC<14 does not output the required DW_AT_declaration on incomplete
10874 types, but gives them a size of zero. Starting with version 14,
10875 ICC is compatible with GCC. */
10876
9068261f 10877static bool
5230b05a
WT
10878producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10879{
10880 if (!cu->checked_producer)
10881 check_producer (cu);
10882
10883 return cu->producer_is_icc_lt_14;
10884}
10885
eb77c9df
AB
10886/* ICC generates a DW_AT_type for C void functions. This was observed on
10887 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10888 which says that void functions should not have a DW_AT_type. */
10889
10890static bool
10891producer_is_icc (struct dwarf2_cu *cu)
10892{
10893 if (!cu->checked_producer)
10894 check_producer (cu);
10895
10896 return cu->producer_is_icc;
10897}
10898
1b80a9fa
JK
10899/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10900 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10901 this, it was first present in GCC release 4.3.0. */
10902
9068261f 10903static bool
1b80a9fa
JK
10904producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10905{
10906 if (!cu->checked_producer)
10907 check_producer (cu);
10908
10909 return cu->producer_is_gcc_lt_4_3;
10910}
10911
d721ba37
PA
10912static file_and_directory
10913find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10914{
d721ba37
PA
10915 file_and_directory res;
10916
9291a0cd
TT
10917 /* Find the filename. Do not use dwarf2_name here, since the filename
10918 is not a source language identifier. */
d721ba37
PA
10919 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10920 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10921
d721ba37
PA
10922 if (res.comp_dir == NULL
10923 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10924 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10925 {
d721ba37
PA
10926 res.comp_dir_storage = ldirname (res.name);
10927 if (!res.comp_dir_storage.empty ())
10928 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10929 }
d721ba37 10930 if (res.comp_dir != NULL)
9291a0cd
TT
10931 {
10932 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10933 directory, get rid of it. */
d721ba37 10934 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10935
d721ba37
PA
10936 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10937 res.comp_dir = cp + 1;
9291a0cd
TT
10938 }
10939
d721ba37
PA
10940 if (res.name == NULL)
10941 res.name = "<unknown>";
10942
10943 return res;
9291a0cd
TT
10944}
10945
f4dc4d17
DE
10946/* Handle DW_AT_stmt_list for a compilation unit.
10947 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10948 COMP_DIR is the compilation directory. LOWPC is passed to
10949 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10950
10951static void
10952handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10953 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10954{
976ca316 10955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 10956 struct attribute *attr;
527f3840
JK
10957 struct line_header line_header_local;
10958 hashval_t line_header_local_hash;
527f3840
JK
10959 void **slot;
10960 int decode_mapping;
2ab95328 10961
f4dc4d17
DE
10962 gdb_assert (! cu->per_cu->is_debug_types);
10963
2ab95328 10964 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 10965 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
10966 return;
10967
d4df075e 10968 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
10969
10970 /* The line header hash table is only created if needed (it exists to
10971 prevent redundant reading of the line table for partial_units).
10972 If we're given a partial_unit, we'll need it. If we're given a
10973 compile_unit, then use the line header hash table if it's already
10974 created, but don't create one just yet. */
10975
976ca316 10976 if (per_objfile->line_header_hash == NULL
527f3840 10977 && die->tag == DW_TAG_partial_unit)
2ab95328 10978 {
976ca316 10979 per_objfile->line_header_hash
d15acc42
TT
10980 .reset (htab_create_alloc (127, line_header_hash_voidp,
10981 line_header_eq_voidp,
10982 free_line_header_voidp,
10983 xcalloc, xfree));
527f3840 10984 }
2ab95328 10985
9c541725 10986 line_header_local.sect_off = line_offset;
527f3840
JK
10987 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10988 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 10989 if (per_objfile->line_header_hash != NULL)
527f3840 10990 {
976ca316 10991 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
10992 &line_header_local,
10993 line_header_local_hash, NO_INSERT);
10994
10995 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10996 is not present in *SLOT (since if there is something in *SLOT then
10997 it will be for a partial_unit). */
10998 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10999 {
527f3840 11000 gdb_assert (*slot != NULL);
9a3c8263 11001 cu->line_header = (struct line_header *) *slot;
527f3840 11002 return;
dee91e82 11003 }
2ab95328 11004 }
527f3840
JK
11005
11006 /* dwarf_decode_line_header does not yet provide sufficient information.
11007 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11008 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11009 if (lh == NULL)
527f3840 11010 return;
4c8aa72d
PA
11011
11012 cu->line_header = lh.release ();
11013 cu->line_header_die_owner = die;
527f3840 11014
976ca316 11015 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11016 slot = NULL;
11017 else
11018 {
976ca316 11019 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11020 &line_header_local,
11021 line_header_local_hash, INSERT);
11022 gdb_assert (slot != NULL);
11023 }
11024 if (slot != NULL && *slot == NULL)
11025 {
11026 /* This newly decoded line number information unit will be owned
11027 by line_header_hash hash table. */
11028 *slot = cu->line_header;
4c8aa72d 11029 cu->line_header_die_owner = NULL;
527f3840
JK
11030 }
11031 else
11032 {
11033 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11034 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11035 line_header for this CU - it may happen at most once for each line
11036 number information unit. And if we're not using line_header_hash
11037 then this is what we want as well. */
11038 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11039 }
11040 decode_mapping = (die->tag != DW_TAG_partial_unit);
11041 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11042 decode_mapping);
fff8551c 11043
2ab95328
TT
11044}
11045
95554aad 11046/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11047
c906108c 11048static void
e7c27a73 11049read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11050{
976ca316
SM
11051 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11052 struct objfile *objfile = per_objfile->objfile;
08feed99 11053 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11054 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11055 CORE_ADDR highpc = ((CORE_ADDR) 0);
11056 struct attribute *attr;
c906108c 11057 struct die_info *child_die;
e142c38c 11058 CORE_ADDR baseaddr;
6e70227d 11059
380618d6 11060 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11061 baseaddr = objfile->text_section_offset ();
c906108c 11062
fae299cd 11063 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11064
11065 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11066 from finish_block. */
2acceee2 11067 if (lowpc == ((CORE_ADDR) -1))
c906108c 11068 lowpc = highpc;
3e29f34a 11069 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11070
d721ba37 11071 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11072
f4b8a18d
KW
11073 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11074 standardised yet. As a workaround for the language detection we fall
11075 back to the DW_AT_producer string. */
11076 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11077 cu->language = language_opencl;
11078
3019eac3
DE
11079 /* Similar hack for Go. */
11080 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11081 set_cu_language (DW_LANG_Go, cu);
11082
c24bdb02 11083 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11084
11085 /* Decode line number information if present. We do this before
11086 processing child DIEs, so that the line header table is available
11087 for DW_AT_decl_file. */
d721ba37 11088 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11089
11090 /* Process all dies in compilation unit. */
11091 if (die->child != NULL)
11092 {
11093 child_die = die->child;
11094 while (child_die && child_die->tag)
11095 {
11096 process_die (child_die, cu);
436c571c 11097 child_die = child_die->sibling;
3019eac3
DE
11098 }
11099 }
11100
11101 /* Decode macro information, if present. Dwarf 2 macro information
11102 refers to information in the line number info statement program
11103 header, so we can only read it if we've read the header
11104 successfully. */
0af92d60
JK
11105 attr = dwarf2_attr (die, DW_AT_macros, cu);
11106 if (attr == NULL)
11107 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11108 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11109 {
11110 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11111 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11112
529908cb 11113 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11114 }
11115 else
11116 {
11117 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11118 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11119 {
529908cb 11120 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11121
43f3e411 11122 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11123 }
11124 }
3019eac3
DE
11125}
11126
c24bdb02
KS
11127void
11128dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11129{
f4dc4d17
DE
11130 struct type_unit_group *tu_group;
11131 int first_time;
3019eac3 11132 struct attribute *attr;
9c541725 11133 unsigned int i;
0186c6a7 11134 struct signatured_type *sig_type;
3019eac3 11135
f4dc4d17 11136 gdb_assert (per_cu->is_debug_types);
0186c6a7 11137 sig_type = (struct signatured_type *) per_cu;
3019eac3 11138
c24bdb02 11139 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11140
f4dc4d17 11141 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11142 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11143 if (sig_type->type_unit_group == NULL)
c24bdb02 11144 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11145 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11146
11147 /* If we've already processed this stmt_list there's no real need to
11148 do it again, we could fake it and just recreate the part we need
11149 (file name,index -> symtab mapping). If data shows this optimization
11150 is useful we can do it then. */
8adb8487
TT
11151 type_unit_group_unshareable *tug_unshare
11152 = per_objfile->get_type_unit_group_unshareable (tu_group);
11153 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11154
11155 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11156 debug info. */
fff8551c 11157 line_header_up lh;
d4df075e 11158 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11159 {
d4df075e 11160 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11161 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11162 }
11163 if (lh == NULL)
11164 {
11165 if (first_time)
c24bdb02 11166 start_symtab ("", NULL, 0);
f4dc4d17
DE
11167 else
11168 {
8adb8487 11169 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11170 gdb_assert (m_builder == nullptr);
8adb8487 11171 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11172 m_builder.reset (new struct buildsym_compunit
11173 (COMPUNIT_OBJFILE (cust), "",
11174 COMPUNIT_DIRNAME (cust),
11175 compunit_language (cust),
11176 0, cust));
770479f2 11177 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11178 }
f4dc4d17 11179 return;
3019eac3
DE
11180 }
11181
c24bdb02
KS
11182 line_header = lh.release ();
11183 line_header_die_owner = die;
3019eac3 11184
f4dc4d17
DE
11185 if (first_time)
11186 {
c24bdb02 11187 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11188
1fd60fc0
DE
11189 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11190 still initializing it, and our caller (a few levels up)
11191 process_full_type_unit still needs to know if this is the first
11192 time. */
11193
8adb8487 11194 tug_unshare->symtabs
4ac93832
TT
11195 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11196 struct symtab *, line_header->file_names_size ());
3019eac3 11197
7ba99d21
AT
11198 auto &file_names = line_header->file_names ();
11199 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11200 {
7ba99d21 11201 file_entry &fe = file_names[i];
c24bdb02
KS
11202 dwarf2_start_subfile (this, fe.name,
11203 fe.include_dir (line_header));
11204 buildsym_compunit *b = get_builder ();
11205 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11206 {
4c8aa72d
PA
11207 /* NOTE: start_subfile will recognize when it's been
11208 passed a file it has already seen. So we can't
11209 assume there's a simple mapping from
11210 cu->line_header->file_names to subfiles, plus
11211 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11212 b->get_current_subfile ()->symtab
11213 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11214 }
11215
c24bdb02 11216 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11217 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11218 }
11219 }
11220 else
3019eac3 11221 {
c24bdb02 11222 gdb_assert (m_builder == nullptr);
8adb8487 11223 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11224 m_builder.reset (new struct buildsym_compunit
11225 (COMPUNIT_OBJFILE (cust), "",
11226 COMPUNIT_DIRNAME (cust),
11227 compunit_language (cust),
11228 0, cust));
770479f2 11229 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11230
7ba99d21
AT
11231 auto &file_names = line_header->file_names ();
11232 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11233 {
7ba99d21 11234 file_entry &fe = file_names[i];
8adb8487 11235 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11236 }
3019eac3
DE
11237 }
11238
f4dc4d17
DE
11239 /* The main symtab is allocated last. Type units don't have DW_AT_name
11240 so they don't have a "real" (so to speak) symtab anyway.
11241 There is later code that will assign the main symtab to all symbols
11242 that don't have one. We need to handle the case of a symbol with a
11243 missing symtab (DW_AT_decl_file) anyway. */
11244}
3019eac3 11245
f4dc4d17
DE
11246/* Process DW_TAG_type_unit.
11247 For TUs we want to skip the first top level sibling if it's not the
11248 actual type being defined by this TU. In this case the first top
11249 level sibling is there to provide context only. */
3019eac3 11250
f4dc4d17
DE
11251static void
11252read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11253{
11254 struct die_info *child_die;
3019eac3 11255
f4dc4d17
DE
11256 prepare_one_comp_unit (cu, die, language_minimal);
11257
11258 /* Initialize (or reinitialize) the machinery for building symtabs.
11259 We do this before processing child DIEs, so that the line header table
11260 is available for DW_AT_decl_file. */
c24bdb02 11261 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11262
11263 if (die->child != NULL)
11264 {
11265 child_die = die->child;
11266 while (child_die && child_die->tag)
11267 {
11268 process_die (child_die, cu);
436c571c 11269 child_die = child_die->sibling;
f4dc4d17
DE
11270 }
11271 }
3019eac3
DE
11272}
11273\f
80626a55
DE
11274/* DWO/DWP files.
11275
11276 http://gcc.gnu.org/wiki/DebugFission
11277 http://gcc.gnu.org/wiki/DebugFissionDWP
11278
11279 To simplify handling of both DWO files ("object" files with the DWARF info)
11280 and DWP files (a file with the DWOs packaged up into one file), we treat
11281 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11282
11283static hashval_t
11284hash_dwo_file (const void *item)
11285{
9a3c8263 11286 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11287 hashval_t hash;
3019eac3 11288
a2ce51a0
DE
11289 hash = htab_hash_string (dwo_file->dwo_name);
11290 if (dwo_file->comp_dir != NULL)
11291 hash += htab_hash_string (dwo_file->comp_dir);
11292 return hash;
3019eac3
DE
11293}
11294
11295static int
11296eq_dwo_file (const void *item_lhs, const void *item_rhs)
11297{
9a3c8263
SM
11298 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11299 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11300
a2ce51a0
DE
11301 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11302 return 0;
11303 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11304 return lhs->comp_dir == rhs->comp_dir;
11305 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11306}
11307
11308/* Allocate a hash table for DWO files. */
11309
51ac9db5 11310static htab_up
298e9637 11311allocate_dwo_file_hash_table ()
3019eac3 11312{
51ac9db5
SM
11313 auto delete_dwo_file = [] (void *item)
11314 {
11315 struct dwo_file *dwo_file = (struct dwo_file *) item;
11316
11317 delete dwo_file;
11318 };
11319
bc68fb19
TT
11320 return htab_up (htab_create_alloc (41,
11321 hash_dwo_file,
11322 eq_dwo_file,
11323 delete_dwo_file,
11324 xcalloc, xfree));
3019eac3
DE
11325}
11326
80626a55
DE
11327/* Lookup DWO file DWO_NAME. */
11328
11329static void **
976ca316 11330lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11331 const char *dwo_name,
11332 const char *comp_dir)
80626a55
DE
11333{
11334 struct dwo_file find_entry;
11335 void **slot;
11336
976ca316
SM
11337 if (per_objfile->per_bfd->dwo_files == NULL)
11338 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11339
0ac5b59e
DE
11340 find_entry.dwo_name = dwo_name;
11341 find_entry.comp_dir = comp_dir;
976ca316 11342 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11343 INSERT);
80626a55
DE
11344
11345 return slot;
11346}
11347
3019eac3
DE
11348static hashval_t
11349hash_dwo_unit (const void *item)
11350{
9a3c8263 11351 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11352
11353 /* This drops the top 32 bits of the id, but is ok for a hash. */
11354 return dwo_unit->signature;
11355}
11356
11357static int
11358eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11359{
9a3c8263
SM
11360 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11361 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11362
11363 /* The signature is assumed to be unique within the DWO file.
11364 So while object file CU dwo_id's always have the value zero,
11365 that's OK, assuming each object file DWO file has only one CU,
11366 and that's the rule for now. */
11367 return lhs->signature == rhs->signature;
11368}
11369
11370/* Allocate a hash table for DWO CUs,TUs.
11371 There is one of these tables for each of CUs,TUs for each DWO file. */
11372
b0b6a987 11373static htab_up
298e9637 11374allocate_dwo_unit_table ()
3019eac3
DE
11375{
11376 /* Start out with a pretty small number.
11377 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11378 return htab_up (htab_create_alloc (3,
11379 hash_dwo_unit,
11380 eq_dwo_unit,
11381 NULL, xcalloc, xfree));
3019eac3
DE
11382}
11383
19c3d4c9 11384/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11385
11386static void
19c3d4c9
DE
11387create_dwo_cu_reader (const struct die_reader_specs *reader,
11388 const gdb_byte *info_ptr,
11389 struct die_info *comp_unit_die,
c0ab21c2
TT
11390 struct dwo_file *dwo_file,
11391 struct dwo_unit *dwo_unit)
3019eac3
DE
11392{
11393 struct dwarf2_cu *cu = reader->cu;
9c541725 11394 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11395 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11396
a084a2a6
AT
11397 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11398 if (!signature.has_value ())
3019eac3 11399 {
b98664d3 11400 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11401 " its dwo_id [in module %s]"),
9d8780f0 11402 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11403 return;
11404 }
11405
3019eac3 11406 dwo_unit->dwo_file = dwo_file;
a084a2a6 11407 dwo_unit->signature = *signature;
8a0459fd 11408 dwo_unit->section = section;
9c541725 11409 dwo_unit->sect_off = sect_off;
3019eac3
DE
11410 dwo_unit->length = cu->per_cu->length;
11411
6f738b01
SM
11412 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11413 sect_offset_str (sect_off),
11414 hex_string (dwo_unit->signature));
3019eac3
DE
11415}
11416
33c5cd75 11417/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11418 Note: This function processes DWO files only, not DWP files. */
3019eac3 11419
33c5cd75 11420static void
976ca316 11421create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11422 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11423 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11424{
976ca316
SM
11425 struct objfile *objfile = per_objfile->objfile;
11426 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11427 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11428
96b79293 11429 section.read (objfile);
33c5cd75 11430 info_ptr = section.buffer;
3019eac3
DE
11431
11432 if (info_ptr == NULL)
33c5cd75 11433 return;
3019eac3 11434
6f738b01
SM
11435 dwarf_read_debug_printf ("Reading %s for %s:",
11436 section.get_name (),
11437 section.get_file_name ());
3019eac3 11438
33c5cd75 11439 end_ptr = info_ptr + section.size;
3019eac3
DE
11440 while (info_ptr < end_ptr)
11441 {
11442 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11443 struct dwo_unit read_unit {};
33c5cd75
DB
11444 struct dwo_unit *dwo_unit;
11445 void **slot;
11446 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11447
11448 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11449 per_cu.per_bfd = per_bfd;
3019eac3 11450 per_cu.is_debug_types = 0;
33c5cd75
DB
11451 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11452 per_cu.section = &section;
11453
976ca316 11454 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11455 if (!reader.dummy_p)
11456 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11457 &dwo_file, &read_unit);
33c5cd75
DB
11458 info_ptr += per_cu.length;
11459
11460 // If the unit could not be parsed, skip it.
c0ab21c2 11461 if (read_unit.dwo_file == NULL)
33c5cd75 11462 continue;
3019eac3 11463
33c5cd75 11464 if (cus_htab == NULL)
298e9637 11465 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11466
1859c670 11467 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11468 struct dwo_unit);
c0ab21c2 11469 *dwo_unit = read_unit;
b0b6a987 11470 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11471 gdb_assert (slot != NULL);
11472 if (*slot != NULL)
19c3d4c9 11473 {
33c5cd75
DB
11474 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11475 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11476
b98664d3 11477 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11478 " the entry at offset %s, signature %s"),
11479 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11480 hex_string (dwo_unit->signature));
19c3d4c9 11481 }
33c5cd75 11482 *slot = (void *)dwo_unit;
3019eac3 11483 }
3019eac3
DE
11484}
11485
80626a55
DE
11486/* DWP file .debug_{cu,tu}_index section format:
11487 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11488 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11489
11490 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11491 officially standard DWP format was published with DWARF v5 and is called
11492 Version 5. There are no versions 3 or 4.
80626a55 11493
d2415c6c
DE
11494 DWP Version 1:
11495
80626a55
DE
11496 Both index sections have the same format, and serve to map a 64-bit
11497 signature to a set of section numbers. Each section begins with a header,
11498 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11499 indexes, and a pool of 32-bit section numbers. The index sections will be
11500 aligned at 8-byte boundaries in the file.
11501
d2415c6c
DE
11502 The index section header consists of:
11503
11504 V, 32 bit version number
11505 -, 32 bits unused
11506 N, 32 bit number of compilation units or type units in the index
11507 M, 32 bit number of slots in the hash table
80626a55 11508
d2415c6c 11509 Numbers are recorded using the byte order of the application binary.
80626a55 11510
d2415c6c
DE
11511 The hash table begins at offset 16 in the section, and consists of an array
11512 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11513 order of the application binary). Unused slots in the hash table are 0.
11514 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11515
d2415c6c
DE
11516 The parallel table begins immediately after the hash table
11517 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11518 array of 32-bit indexes (using the byte order of the application binary),
11519 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11520 table contains a 32-bit index into the pool of section numbers. For unused
11521 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11522
73869dc2
DE
11523 The pool of section numbers begins immediately following the hash table
11524 (at offset 16 + 12 * M from the beginning of the section). The pool of
11525 section numbers consists of an array of 32-bit words (using the byte order
11526 of the application binary). Each item in the array is indexed starting
11527 from 0. The hash table entry provides the index of the first section
11528 number in the set. Additional section numbers in the set follow, and the
11529 set is terminated by a 0 entry (section number 0 is not used in ELF).
11530
11531 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11532 section must be the first entry in the set, and the .debug_abbrev.dwo must
11533 be the second entry. Other members of the set may follow in any order.
11534
11535 ---
11536
d2854d8d 11537 DWP Versions 2 and 5:
73869dc2 11538
d2854d8d 11539 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11540 and the entries in the index tables are now offsets into these sections.
11541 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11542 section.
11543
11544 Index Section Contents:
11545 Header
11546 Hash Table of Signatures dwp_hash_table.hash_table
11547 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11548 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11549 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11550
11551 The index section header consists of:
11552
11553 V, 32 bit version number
11554 L, 32 bit number of columns in the table of section offsets
11555 N, 32 bit number of compilation units or type units in the index
11556 M, 32 bit number of slots in the hash table
11557
11558 Numbers are recorded using the byte order of the application binary.
11559
11560 The hash table has the same format as version 1.
11561 The parallel table of indices has the same format as version 1,
11562 except that the entries are origin-1 indices into the table of sections
11563 offsets and the table of section sizes.
11564
11565 The table of offsets begins immediately following the parallel table
11566 (at offset 16 + 12 * M from the beginning of the section). The table is
11567 a two-dimensional array of 32-bit words (using the byte order of the
11568 application binary), with L columns and N+1 rows, in row-major order.
11569 Each row in the array is indexed starting from 0. The first row provides
11570 a key to the remaining rows: each column in this row provides an identifier
11571 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11572 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11573
11574 DW_SECT_INFO 1 .debug_info.dwo
11575 DW_SECT_TYPES 2 .debug_types.dwo
11576 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11577 DW_SECT_LINE 4 .debug_line.dwo
11578 DW_SECT_LOC 5 .debug_loc.dwo
11579 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11580 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11581 DW_SECT_MACRO 8 .debug_macro.dwo
11582
d2854d8d
CT
11583 The section identifiers for Version 5 are:
11584
11585 DW_SECT_INFO_V5 1 .debug_info.dwo
11586 DW_SECT_RESERVED_V5 2 --
11587 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11588 DW_SECT_LINE_V5 4 .debug_line.dwo
11589 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11590 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11591 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11592 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11593
73869dc2
DE
11594 The offsets provided by the CU and TU index sections are the base offsets
11595 for the contributions made by each CU or TU to the corresponding section
11596 in the package file. Each CU and TU header contains an abbrev_offset
11597 field, used to find the abbreviations table for that CU or TU within the
11598 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11599 be interpreted as relative to the base offset given in the index section.
11600 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11601 should be interpreted as relative to the base offset for .debug_line.dwo,
11602 and offsets into other debug sections obtained from DWARF attributes should
11603 also be interpreted as relative to the corresponding base offset.
11604
11605 The table of sizes begins immediately following the table of offsets.
11606 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11607 with L columns and N rows, in row-major order. Each row in the array is
11608 indexed starting from 1 (row 0 is shared by the two tables).
11609
11610 ---
11611
11612 Hash table lookup is handled the same in version 1 and 2:
11613
11614 We assume that N and M will not exceed 2^32 - 1.
11615 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11616
d2415c6c
DE
11617 Given a 64-bit compilation unit signature or a type signature S, an entry
11618 in the hash table is located as follows:
80626a55 11619
d2415c6c
DE
11620 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11621 the low-order k bits all set to 1.
80626a55 11622
d2415c6c 11623 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11624
d2415c6c
DE
11625 3) If the hash table entry at index H matches the signature, use that
11626 entry. If the hash table entry at index H is unused (all zeroes),
11627 terminate the search: the signature is not present in the table.
80626a55 11628
d2415c6c 11629 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11630
d2415c6c 11631 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11632 to stop at an unused slot or find the match. */
80626a55
DE
11633
11634/* Create a hash table to map DWO IDs to their CU/TU entry in
11635 .debug_{info,types}.dwo in DWP_FILE.
11636 Returns NULL if there isn't one.
11637 Note: This function processes DWP files only, not DWO files. */
11638
11639static struct dwp_hash_table *
976ca316 11640create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11641 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11642{
976ca316 11643 struct objfile *objfile = per_objfile->objfile;
400174b1 11644 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11645 const gdb_byte *index_ptr, *index_end;
80626a55 11646 struct dwarf2_section_info *index;
73869dc2 11647 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11648 struct dwp_hash_table *htab;
11649
11650 if (is_debug_types)
11651 index = &dwp_file->sections.tu_index;
11652 else
11653 index = &dwp_file->sections.cu_index;
11654
96b79293 11655 if (index->empty ())
80626a55 11656 return NULL;
96b79293 11657 index->read (objfile);
80626a55
DE
11658
11659 index_ptr = index->buffer;
11660 index_end = index_ptr + index->size;
11661
d2854d8d
CT
11662 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11663 For now it's safe to just read 4 bytes (particularly as it's difficult to
11664 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11665 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11666 index_ptr += 4;
d2854d8d 11667 if (version == 2 || version == 5)
73869dc2
DE
11668 nr_columns = read_4_bytes (dbfd, index_ptr);
11669 else
11670 nr_columns = 0;
11671 index_ptr += 4;
80626a55
DE
11672 nr_units = read_4_bytes (dbfd, index_ptr);
11673 index_ptr += 4;
11674 nr_slots = read_4_bytes (dbfd, index_ptr);
11675 index_ptr += 4;
11676
d2854d8d 11677 if (version != 1 && version != 2 && version != 5)
80626a55 11678 {
21aa081e 11679 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11680 " [in module %s]"),
21aa081e 11681 pulongest (version), dwp_file->name);
80626a55
DE
11682 }
11683 if (nr_slots != (nr_slots & -nr_slots))
11684 {
21aa081e 11685 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11686 " is not power of 2 [in module %s]"),
21aa081e 11687 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11688 }
11689
976ca316 11690 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11691 htab->version = version;
11692 htab->nr_columns = nr_columns;
80626a55
DE
11693 htab->nr_units = nr_units;
11694 htab->nr_slots = nr_slots;
11695 htab->hash_table = index_ptr;
11696 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11697
11698 /* Exit early if the table is empty. */
11699 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11700 || (version == 2 && nr_columns == 0)
11701 || (version == 5 && nr_columns == 0))
73869dc2
DE
11702 {
11703 /* All must be zero. */
11704 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11705 || (version == 2 && nr_columns != 0)
11706 || (version == 5 && nr_columns != 0))
73869dc2 11707 {
b98664d3 11708 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11709 " all zero [in modules %s]"),
11710 dwp_file->name);
11711 }
11712 return htab;
11713 }
11714
11715 if (version == 1)
11716 {
11717 htab->section_pool.v1.indices =
11718 htab->unit_table + sizeof (uint32_t) * nr_slots;
11719 /* It's harder to decide whether the section is too small in v1.
11720 V1 is deprecated anyway so we punt. */
11721 }
d2854d8d 11722 else if (version == 2)
73869dc2
DE
11723 {
11724 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11725 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11726 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11727 /* Reverse map for error checking. */
11728 int ids_seen[DW_SECT_MAX + 1];
11729 int i;
11730
11731 if (nr_columns < 2)
11732 {
11733 error (_("Dwarf Error: bad DWP hash table, too few columns"
11734 " in section table [in module %s]"),
11735 dwp_file->name);
11736 }
11737 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11738 {
11739 error (_("Dwarf Error: bad DWP hash table, too many columns"
11740 " in section table [in module %s]"),
11741 dwp_file->name);
11742 }
04fd5eed
GB
11743 memset (ids, 255, sizeof_ids);
11744 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11745 for (i = 0; i < nr_columns; ++i)
11746 {
11747 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11748
11749 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11750 {
11751 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11752 " in section table [in module %s]"),
11753 id, dwp_file->name);
11754 }
11755 if (ids_seen[id] != -1)
11756 {
11757 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11758 " id %d in section table [in module %s]"),
11759 id, dwp_file->name);
11760 }
11761 ids_seen[id] = i;
11762 ids[i] = id;
11763 }
11764 /* Must have exactly one info or types section. */
11765 if (((ids_seen[DW_SECT_INFO] != -1)
11766 + (ids_seen[DW_SECT_TYPES] != -1))
11767 != 1)
11768 {
11769 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11770 " DWO info/types section [in module %s]"),
11771 dwp_file->name);
11772 }
11773 /* Must have an abbrev section. */
11774 if (ids_seen[DW_SECT_ABBREV] == -1)
11775 {
11776 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11777 " section [in module %s]"),
11778 dwp_file->name);
11779 }
11780 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11781 htab->section_pool.v2.sizes =
11782 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11783 * nr_units * nr_columns);
11784 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11785 * nr_units * nr_columns))
11786 > index_end)
11787 {
11788 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11789 " [in module %s]"),
11790 dwp_file->name);
11791 }
11792 }
d2854d8d
CT
11793 else /* version == 5 */
11794 {
11795 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11796 int *ids = htab->section_pool.v5.section_ids;
11797 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11798 /* Reverse map for error checking. */
11799 int ids_seen[DW_SECT_MAX_V5 + 1];
11800
11801 if (nr_columns < 2)
11802 {
11803 error (_("Dwarf Error: bad DWP hash table, too few columns"
11804 " in section table [in module %s]"),
11805 dwp_file->name);
11806 }
11807 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11808 {
11809 error (_("Dwarf Error: bad DWP hash table, too many columns"
11810 " in section table [in module %s]"),
11811 dwp_file->name);
11812 }
11813 memset (ids, 255, sizeof_ids);
11814 memset (ids_seen, 255, sizeof (ids_seen));
11815 for (int i = 0; i < nr_columns; ++i)
11816 {
11817 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11818
11819 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11820 {
11821 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11822 " in section table [in module %s]"),
11823 id, dwp_file->name);
11824 }
11825 if (ids_seen[id] != -1)
11826 {
11827 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11828 " id %d in section table [in module %s]"),
11829 id, dwp_file->name);
11830 }
11831 ids_seen[id] = i;
11832 ids[i] = id;
11833 }
11834 /* Must have seen an info section. */
11835 if (ids_seen[DW_SECT_INFO_V5] == -1)
11836 {
11837 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11838 " DWO info/types section [in module %s]"),
11839 dwp_file->name);
11840 }
11841 /* Must have an abbrev section. */
11842 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11843 {
11844 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11845 " section [in module %s]"),
11846 dwp_file->name);
11847 }
11848 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11849 htab->section_pool.v5.sizes
11850 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11851 * nr_units * nr_columns);
11852 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11853 * nr_units * nr_columns))
11854 > index_end)
11855 {
11856 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11857 " [in module %s]"),
11858 dwp_file->name);
11859 }
11860 }
80626a55
DE
11861
11862 return htab;
11863}
11864
11865/* Update SECTIONS with the data from SECTP.
11866
5bb6e9dd
TT
11867 This function is like the other "locate" section routines, but in
11868 this context the sections to read comes from the DWP V1 hash table,
11869 not the full ELF section table.
80626a55
DE
11870
11871 The result is non-zero for success, or zero if an error was found. */
11872
11873static int
73869dc2
DE
11874locate_v1_virtual_dwo_sections (asection *sectp,
11875 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11876{
11877 const struct dwop_section_names *names = &dwop_section_names;
11878
fbedd546 11879 if (names->abbrev_dwo.matches (sectp->name))
80626a55
DE
11880 {
11881 /* There can be only one. */
049412e3 11882 if (sections->abbrev.s.section != NULL)
80626a55 11883 return 0;
049412e3 11884 sections->abbrev.s.section = sectp;
fd361982 11885 sections->abbrev.size = bfd_section_size (sectp);
80626a55 11886 }
fbedd546
TT
11887 else if (names->info_dwo.matches (sectp->name)
11888 || names->types_dwo.matches (sectp->name))
80626a55
DE
11889 {
11890 /* There can be only one. */
049412e3 11891 if (sections->info_or_types.s.section != NULL)
80626a55 11892 return 0;
049412e3 11893 sections->info_or_types.s.section = sectp;
fd361982 11894 sections->info_or_types.size = bfd_section_size (sectp);
80626a55 11895 }
fbedd546 11896 else if (names->line_dwo.matches (sectp->name))
80626a55
DE
11897 {
11898 /* There can be only one. */
049412e3 11899 if (sections->line.s.section != NULL)
80626a55 11900 return 0;
049412e3 11901 sections->line.s.section = sectp;
fd361982 11902 sections->line.size = bfd_section_size (sectp);
80626a55 11903 }
fbedd546 11904 else if (names->loc_dwo.matches (sectp->name))
80626a55
DE
11905 {
11906 /* There can be only one. */
049412e3 11907 if (sections->loc.s.section != NULL)
80626a55 11908 return 0;
049412e3 11909 sections->loc.s.section = sectp;
fd361982 11910 sections->loc.size = bfd_section_size (sectp);
80626a55 11911 }
fbedd546 11912 else if (names->macinfo_dwo.matches (sectp->name))
80626a55
DE
11913 {
11914 /* There can be only one. */
049412e3 11915 if (sections->macinfo.s.section != NULL)
80626a55 11916 return 0;
049412e3 11917 sections->macinfo.s.section = sectp;
fd361982 11918 sections->macinfo.size = bfd_section_size (sectp);
80626a55 11919 }
fbedd546 11920 else if (names->macro_dwo.matches (sectp->name))
80626a55
DE
11921 {
11922 /* There can be only one. */
049412e3 11923 if (sections->macro.s.section != NULL)
80626a55 11924 return 0;
049412e3 11925 sections->macro.s.section = sectp;
fd361982 11926 sections->macro.size = bfd_section_size (sectp);
80626a55 11927 }
fbedd546 11928 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55
DE
11929 {
11930 /* There can be only one. */
049412e3 11931 if (sections->str_offsets.s.section != NULL)
80626a55 11932 return 0;
049412e3 11933 sections->str_offsets.s.section = sectp;
fd361982 11934 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11935 }
11936 else
11937 {
11938 /* No other kind of section is valid. */
11939 return 0;
11940 }
11941
11942 return 1;
11943}
11944
73869dc2
DE
11945/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11946 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11947 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11948 This is for DWP version 1 files. */
80626a55
DE
11949
11950static struct dwo_unit *
976ca316 11951create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 11952 struct dwp_file *dwp_file,
73869dc2
DE
11953 uint32_t unit_index,
11954 const char *comp_dir,
11955 ULONGEST signature, int is_debug_types)
80626a55 11956{
73869dc2
DE
11957 const struct dwp_hash_table *dwp_htab =
11958 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11959 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11960 const char *kind = is_debug_types ? "TU" : "CU";
11961 struct dwo_file *dwo_file;
11962 struct dwo_unit *dwo_unit;
73869dc2 11963 struct virtual_v1_dwo_sections sections;
80626a55 11964 void **dwo_file_slot;
80626a55
DE
11965 int i;
11966
73869dc2
DE
11967 gdb_assert (dwp_file->version == 1);
11968
6f738b01
SM
11969 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11970 kind, pulongest (unit_index), hex_string (signature),
11971 dwp_file->name);
80626a55 11972
19ac8c2e 11973 /* Fetch the sections of this DWO unit.
80626a55
DE
11974 Put a limit on the number of sections we look for so that bad data
11975 doesn't cause us to loop forever. */
11976
73869dc2 11977#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11978 (1 /* .debug_info or .debug_types */ \
11979 + 1 /* .debug_abbrev */ \
11980 + 1 /* .debug_line */ \
11981 + 1 /* .debug_loc */ \
11982 + 1 /* .debug_str_offsets */ \
19ac8c2e 11983 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11984 + 1 /* trailing zero */)
11985
11986 memset (&sections, 0, sizeof (sections));
80626a55 11987
73869dc2 11988 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11989 {
11990 asection *sectp;
11991 uint32_t section_nr =
11992 read_4_bytes (dbfd,
73869dc2
DE
11993 dwp_htab->section_pool.v1.indices
11994 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11995
11996 if (section_nr == 0)
11997 break;
11998 if (section_nr >= dwp_file->num_sections)
11999 {
12000 error (_("Dwarf Error: bad DWP hash table, section number too large"
12001 " [in module %s]"),
12002 dwp_file->name);
12003 }
12004
12005 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12006 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12007 {
12008 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12009 " [in module %s]"),
12010 dwp_file->name);
12011 }
12012 }
12013
12014 if (i < 2
96b79293
TT
12015 || sections.info_or_types.empty ()
12016 || sections.abbrev.empty ())
80626a55
DE
12017 {
12018 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12019 " [in module %s]"),
12020 dwp_file->name);
12021 }
73869dc2 12022 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12023 {
12024 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12025 " [in module %s]"),
12026 dwp_file->name);
12027 }
12028
12029 /* It's easier for the rest of the code if we fake a struct dwo_file and
12030 have dwo_unit "live" in that. At least for now.
12031
12032 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12033 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12034 file, we can combine them back into a virtual DWO file to save space
12035 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12036 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12037
791afaa2
TT
12038 std::string virtual_dwo_name =
12039 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12040 sections.abbrev.get_id (),
12041 sections.line.get_id (),
12042 sections.loc.get_id (),
12043 sections.str_offsets.get_id ());
80626a55 12044 /* Can we use an existing virtual DWO file? */
976ca316 12045 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12046 comp_dir);
80626a55
DE
12047 /* Create one if necessary. */
12048 if (*dwo_file_slot == NULL)
12049 {
6f738b01
SM
12050 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12051 virtual_dwo_name.c_str ());
12052
51ac9db5 12053 dwo_file = new struct dwo_file;
976ca316 12054 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12055 dwo_file->comp_dir = comp_dir;
80626a55
DE
12056 dwo_file->sections.abbrev = sections.abbrev;
12057 dwo_file->sections.line = sections.line;
12058 dwo_file->sections.loc = sections.loc;
12059 dwo_file->sections.macinfo = sections.macinfo;
12060 dwo_file->sections.macro = sections.macro;
12061 dwo_file->sections.str_offsets = sections.str_offsets;
12062 /* The "str" section is global to the entire DWP file. */
12063 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12064 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12065 there's no need to record it in dwo_file.
12066 Also, we can't simply record type sections in dwo_file because
12067 we record a pointer into the vector in dwo_unit. As we collect more
12068 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12069 for it, invalidating all copies of pointers into the previous
12070 contents. */
80626a55
DE
12071 *dwo_file_slot = dwo_file;
12072 }
12073 else
12074 {
6f738b01
SM
12075 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12076 virtual_dwo_name.c_str ());
12077
9a3c8263 12078 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12079 }
80626a55 12080
976ca316 12081 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12082 dwo_unit->dwo_file = dwo_file;
12083 dwo_unit->signature = signature;
8d749320 12084 dwo_unit->section =
976ca316 12085 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12086 *dwo_unit->section = sections.info_or_types;
57d63ce2 12087 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12088
12089 return dwo_unit;
12090}
12091
d2854d8d
CT
12092/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12093 simplify them. Given a pointer to the containing section SECTION, and
12094 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12095 virtual section of just that piece. */
73869dc2
DE
12096
12097static struct dwarf2_section_info
d2854d8d
CT
12098create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12099 struct dwarf2_section_info *section,
12100 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12101{
12102 struct dwarf2_section_info result;
12103 asection *sectp;
12104
12105 gdb_assert (section != NULL);
12106 gdb_assert (!section->is_virtual);
12107
12108 memset (&result, 0, sizeof (result));
12109 result.s.containing_section = section;
dc4ccb6f 12110 result.is_virtual = true;
73869dc2
DE
12111
12112 if (size == 0)
12113 return result;
12114
96b79293 12115 sectp = section->get_bfd_section ();
73869dc2
DE
12116
12117 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12118 bounds of the real section. This is a pretty-rare event, so just
12119 flag an error (easier) instead of a warning and trying to cope. */
12120 if (sectp == NULL
fd361982 12121 || offset + size > bfd_section_size (sectp))
73869dc2 12122 {
d2854d8d 12123 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12124 " in section %s [in module %s]"),
fd361982 12125 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12126 objfile_name (per_objfile->objfile));
73869dc2
DE
12127 }
12128
12129 result.virtual_offset = offset;
12130 result.size = size;
12131 return result;
12132}
12133
12134/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12135 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12136 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12137 This is for DWP version 2 files. */
12138
12139static struct dwo_unit *
976ca316 12140create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12141 struct dwp_file *dwp_file,
73869dc2
DE
12142 uint32_t unit_index,
12143 const char *comp_dir,
12144 ULONGEST signature, int is_debug_types)
12145{
73869dc2
DE
12146 const struct dwp_hash_table *dwp_htab =
12147 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12148 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12149 const char *kind = is_debug_types ? "TU" : "CU";
12150 struct dwo_file *dwo_file;
12151 struct dwo_unit *dwo_unit;
d2854d8d 12152 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12153 void **dwo_file_slot;
73869dc2
DE
12154 int i;
12155
12156 gdb_assert (dwp_file->version == 2);
12157
6f738b01
SM
12158 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12159 kind, pulongest (unit_index), hex_string (signature),
12160 dwp_file->name);
73869dc2
DE
12161
12162 /* Fetch the section offsets of this DWO unit. */
12163
12164 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12165
12166 for (i = 0; i < dwp_htab->nr_columns; ++i)
12167 {
12168 uint32_t offset = read_4_bytes (dbfd,
12169 dwp_htab->section_pool.v2.offsets
12170 + (((unit_index - 1) * dwp_htab->nr_columns
12171 + i)
12172 * sizeof (uint32_t)));
12173 uint32_t size = read_4_bytes (dbfd,
12174 dwp_htab->section_pool.v2.sizes
12175 + (((unit_index - 1) * dwp_htab->nr_columns
12176 + i)
12177 * sizeof (uint32_t)));
12178
12179 switch (dwp_htab->section_pool.v2.section_ids[i])
12180 {
12181 case DW_SECT_INFO:
12182 case DW_SECT_TYPES:
12183 sections.info_or_types_offset = offset;
12184 sections.info_or_types_size = size;
12185 break;
12186 case DW_SECT_ABBREV:
12187 sections.abbrev_offset = offset;
12188 sections.abbrev_size = size;
12189 break;
12190 case DW_SECT_LINE:
12191 sections.line_offset = offset;
12192 sections.line_size = size;
12193 break;
12194 case DW_SECT_LOC:
12195 sections.loc_offset = offset;
12196 sections.loc_size = size;
12197 break;
12198 case DW_SECT_STR_OFFSETS:
12199 sections.str_offsets_offset = offset;
12200 sections.str_offsets_size = size;
12201 break;
12202 case DW_SECT_MACINFO:
12203 sections.macinfo_offset = offset;
12204 sections.macinfo_size = size;
12205 break;
12206 case DW_SECT_MACRO:
12207 sections.macro_offset = offset;
12208 sections.macro_size = size;
12209 break;
12210 }
12211 }
12212
12213 /* It's easier for the rest of the code if we fake a struct dwo_file and
12214 have dwo_unit "live" in that. At least for now.
12215
12216 The DWP file can be made up of a random collection of CUs and TUs.
12217 However, for each CU + set of TUs that came from the same original DWO
12218 file, we can combine them back into a virtual DWO file to save space
12219 (fewer struct dwo_file objects to allocate). Remember that for really
12220 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12221
791afaa2
TT
12222 std::string virtual_dwo_name =
12223 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12224 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12225 (long) (sections.line_size ? sections.line_offset : 0),
12226 (long) (sections.loc_size ? sections.loc_offset : 0),
12227 (long) (sections.str_offsets_size
12228 ? sections.str_offsets_offset : 0));
73869dc2 12229 /* Can we use an existing virtual DWO file? */
976ca316 12230 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12231 comp_dir);
73869dc2
DE
12232 /* Create one if necessary. */
12233 if (*dwo_file_slot == NULL)
12234 {
6f738b01
SM
12235 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12236 virtual_dwo_name.c_str ());
12237
51ac9db5 12238 dwo_file = new struct dwo_file;
976ca316 12239 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12240 dwo_file->comp_dir = comp_dir;
12241 dwo_file->sections.abbrev =
d2854d8d
CT
12242 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12243 sections.abbrev_offset,
12244 sections.abbrev_size);
73869dc2 12245 dwo_file->sections.line =
d2854d8d
CT
12246 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12247 sections.line_offset,
12248 sections.line_size);
73869dc2 12249 dwo_file->sections.loc =
d2854d8d
CT
12250 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12251 sections.loc_offset, sections.loc_size);
73869dc2 12252 dwo_file->sections.macinfo =
d2854d8d
CT
12253 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12254 sections.macinfo_offset,
12255 sections.macinfo_size);
73869dc2 12256 dwo_file->sections.macro =
d2854d8d
CT
12257 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12258 sections.macro_offset,
12259 sections.macro_size);
73869dc2 12260 dwo_file->sections.str_offsets =
d2854d8d
CT
12261 create_dwp_v2_or_v5_section (per_objfile,
12262 &dwp_file->sections.str_offsets,
12263 sections.str_offsets_offset,
12264 sections.str_offsets_size);
73869dc2
DE
12265 /* The "str" section is global to the entire DWP file. */
12266 dwo_file->sections.str = dwp_file->sections.str;
12267 /* The info or types section is assigned below to dwo_unit,
12268 there's no need to record it in dwo_file.
12269 Also, we can't simply record type sections in dwo_file because
12270 we record a pointer into the vector in dwo_unit. As we collect more
12271 types we'll grow the vector and eventually have to reallocate space
12272 for it, invalidating all copies of pointers into the previous
12273 contents. */
12274 *dwo_file_slot = dwo_file;
12275 }
12276 else
12277 {
6f738b01
SM
12278 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12279 virtual_dwo_name.c_str ());
12280
9a3c8263 12281 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12282 }
73869dc2 12283
976ca316 12284 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12285 dwo_unit->dwo_file = dwo_file;
12286 dwo_unit->signature = signature;
8d749320 12287 dwo_unit->section =
976ca316 12288 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12289 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12290 (per_objfile,
d2854d8d
CT
12291 is_debug_types
12292 ? &dwp_file->sections.types
12293 : &dwp_file->sections.info,
12294 sections.info_or_types_offset,
12295 sections.info_or_types_size);
12296 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12297
12298 return dwo_unit;
12299}
12300
12301/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12302 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12303 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12304 This is for DWP version 5 files. */
12305
12306static struct dwo_unit *
12307create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12308 struct dwp_file *dwp_file,
12309 uint32_t unit_index,
12310 const char *comp_dir,
12311 ULONGEST signature, int is_debug_types)
12312{
12313 const struct dwp_hash_table *dwp_htab
12314 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12315 bfd *dbfd = dwp_file->dbfd.get ();
12316 const char *kind = is_debug_types ? "TU" : "CU";
12317 struct dwo_file *dwo_file;
12318 struct dwo_unit *dwo_unit;
12319 struct virtual_v2_or_v5_dwo_sections sections {};
12320 void **dwo_file_slot;
12321
12322 gdb_assert (dwp_file->version == 5);
12323
6f738b01
SM
12324 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12325 kind, pulongest (unit_index), hex_string (signature),
12326 dwp_file->name);
d2854d8d
CT
12327
12328 /* Fetch the section offsets of this DWO unit. */
12329
12330 /* memset (&sections, 0, sizeof (sections)); */
12331
12332 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12333 {
12334 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12335 dwp_htab->section_pool.v5.offsets
12336 + (((unit_index - 1)
12337 * dwp_htab->nr_columns
12338 + i)
12339 * sizeof (uint32_t)));
d2854d8d 12340 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12341 dwp_htab->section_pool.v5.sizes
12342 + (((unit_index - 1) * dwp_htab->nr_columns
12343 + i)
12344 * sizeof (uint32_t)));
d2854d8d
CT
12345
12346 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12347 {
12348 case DW_SECT_ABBREV_V5:
12349 sections.abbrev_offset = offset;
12350 sections.abbrev_size = size;
12351 break;
12352 case DW_SECT_INFO_V5:
12353 sections.info_or_types_offset = offset;
12354 sections.info_or_types_size = size;
12355 break;
12356 case DW_SECT_LINE_V5:
12357 sections.line_offset = offset;
12358 sections.line_size = size;
12359 break;
12360 case DW_SECT_LOCLISTS_V5:
12361 sections.loclists_offset = offset;
12362 sections.loclists_size = size;
12363 break;
12364 case DW_SECT_MACRO_V5:
12365 sections.macro_offset = offset;
12366 sections.macro_size = size;
12367 break;
12368 case DW_SECT_RNGLISTS_V5:
12369 sections.rnglists_offset = offset;
12370 sections.rnglists_size = size;
12371 break;
12372 case DW_SECT_STR_OFFSETS_V5:
12373 sections.str_offsets_offset = offset;
12374 sections.str_offsets_size = size;
12375 break;
12376 case DW_SECT_RESERVED_V5:
12377 default:
12378 break;
12379 }
d2854d8d
CT
12380 }
12381
12382 /* It's easier for the rest of the code if we fake a struct dwo_file and
12383 have dwo_unit "live" in that. At least for now.
12384
12385 The DWP file can be made up of a random collection of CUs and TUs.
12386 However, for each CU + set of TUs that came from the same original DWO
12387 file, we can combine them back into a virtual DWO file to save space
12388 (fewer struct dwo_file objects to allocate). Remember that for really
12389 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12390
12391 std::string virtual_dwo_name =
12392 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12393 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12394 (long) (sections.line_size ? sections.line_offset : 0),
12395 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12396 (long) (sections.str_offsets_size
12397 ? sections.str_offsets_offset : 0),
12398 (long) (sections.macro_size ? sections.macro_offset : 0),
12399 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12400 /* Can we use an existing virtual DWO file? */
12401 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12402 virtual_dwo_name.c_str (),
12403 comp_dir);
d2854d8d
CT
12404 /* Create one if necessary. */
12405 if (*dwo_file_slot == NULL)
12406 {
6f738b01
SM
12407 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12408 virtual_dwo_name.c_str ());
12409
d2854d8d
CT
12410 dwo_file = new struct dwo_file;
12411 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12412 dwo_file->comp_dir = comp_dir;
12413 dwo_file->sections.abbrev =
dda83cd7
SM
12414 create_dwp_v2_or_v5_section (per_objfile,
12415 &dwp_file->sections.abbrev,
12416 sections.abbrev_offset,
12417 sections.abbrev_size);
d2854d8d 12418 dwo_file->sections.line =
dda83cd7
SM
12419 create_dwp_v2_or_v5_section (per_objfile,
12420 &dwp_file->sections.line,
12421 sections.line_offset, sections.line_size);
d2854d8d 12422 dwo_file->sections.macro =
dda83cd7
SM
12423 create_dwp_v2_or_v5_section (per_objfile,
12424 &dwp_file->sections.macro,
12425 sections.macro_offset,
12426 sections.macro_size);
d2854d8d 12427 dwo_file->sections.loclists =
dda83cd7
SM
12428 create_dwp_v2_or_v5_section (per_objfile,
12429 &dwp_file->sections.loclists,
12430 sections.loclists_offset,
12431 sections.loclists_size);
d2854d8d 12432 dwo_file->sections.rnglists =
dda83cd7
SM
12433 create_dwp_v2_or_v5_section (per_objfile,
12434 &dwp_file->sections.rnglists,
12435 sections.rnglists_offset,
12436 sections.rnglists_size);
d2854d8d 12437 dwo_file->sections.str_offsets =
dda83cd7
SM
12438 create_dwp_v2_or_v5_section (per_objfile,
12439 &dwp_file->sections.str_offsets,
12440 sections.str_offsets_offset,
12441 sections.str_offsets_size);
d2854d8d
CT
12442 /* The "str" section is global to the entire DWP file. */
12443 dwo_file->sections.str = dwp_file->sections.str;
12444 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12445 there's no need to record it in dwo_file.
12446 Also, we can't simply record type sections in dwo_file because
12447 we record a pointer into the vector in dwo_unit. As we collect more
12448 types we'll grow the vector and eventually have to reallocate space
12449 for it, invalidating all copies of pointers into the previous
12450 contents. */
d2854d8d
CT
12451 *dwo_file_slot = dwo_file;
12452 }
12453 else
12454 {
6f738b01
SM
12455 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12456 virtual_dwo_name.c_str ());
12457
d2854d8d
CT
12458 dwo_file = (struct dwo_file *) *dwo_file_slot;
12459 }
12460
12461 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12462 dwo_unit->dwo_file = dwo_file;
12463 dwo_unit->signature = signature;
12464 dwo_unit->section
12465 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12466 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12467 &dwp_file->sections.info,
12468 sections.info_or_types_offset,
12469 sections.info_or_types_size);
73869dc2
DE
12470 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12471
12472 return dwo_unit;
12473}
12474
57d63ce2
DE
12475/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12476 Returns NULL if the signature isn't found. */
80626a55
DE
12477
12478static struct dwo_unit *
976ca316 12479lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12480 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12481 ULONGEST signature, int is_debug_types)
80626a55 12482{
57d63ce2
DE
12483 const struct dwp_hash_table *dwp_htab =
12484 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12485 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12486 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12487 uint32_t hash = signature & mask;
12488 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12489 unsigned int i;
12490 void **slot;
870f88f7 12491 struct dwo_unit find_dwo_cu;
80626a55
DE
12492
12493 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12494 find_dwo_cu.signature = signature;
19ac8c2e 12495 slot = htab_find_slot (is_debug_types
48b490f2
TT
12496 ? dwp_file->loaded_tus.get ()
12497 : dwp_file->loaded_cus.get (),
19ac8c2e 12498 &find_dwo_cu, INSERT);
80626a55
DE
12499
12500 if (*slot != NULL)
9a3c8263 12501 return (struct dwo_unit *) *slot;
80626a55
DE
12502
12503 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12504 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12505 {
12506 ULONGEST signature_in_table;
12507
12508 signature_in_table =
57d63ce2 12509 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12510 if (signature_in_table == signature)
12511 {
57d63ce2
DE
12512 uint32_t unit_index =
12513 read_4_bytes (dbfd,
12514 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12515
73869dc2
DE
12516 if (dwp_file->version == 1)
12517 {
976ca316
SM
12518 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12519 unit_index, comp_dir,
12520 signature, is_debug_types);
73869dc2 12521 }
d2854d8d 12522 else if (dwp_file->version == 2)
73869dc2 12523 {
976ca316
SM
12524 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12525 unit_index, comp_dir,
12526 signature, is_debug_types);
73869dc2 12527 }
d2854d8d
CT
12528 else /* version == 5 */
12529 {
12530 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12531 unit_index, comp_dir,
12532 signature, is_debug_types);
12533 }
9a3c8263 12534 return (struct dwo_unit *) *slot;
80626a55
DE
12535 }
12536 if (signature_in_table == 0)
12537 return NULL;
12538 hash = (hash + hash2) & mask;
12539 }
12540
12541 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12542 " [in module %s]"),
12543 dwp_file->name);
12544}
12545
ab5088bf 12546/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12547 Open the file specified by FILE_NAME and hand it off to BFD for
12548 preliminary analysis. Return a newly initialized bfd *, which
12549 includes a canonicalized copy of FILE_NAME.
80626a55 12550 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12551 SEARCH_CWD is true if the current directory is to be searched.
12552 It will be searched before debug-file-directory.
13aaf454
DE
12553 If successful, the file is added to the bfd include table of the
12554 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12555 If unable to find/open the file, return NULL.
3019eac3
DE
12556 NOTE: This function is derived from symfile_bfd_open. */
12557
192b62ce 12558static gdb_bfd_ref_ptr
976ca316 12559try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12560 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12561{
24b9144d 12562 int desc;
9c02c129
DE
12563 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12564 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12565 to debug_file_directory. */
e0cc99a6 12566 const char *search_path;
9c02c129
DE
12567 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12568
e0cc99a6 12569 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12570 if (search_cwd)
12571 {
12572 if (*debug_file_directory != '\0')
e0cc99a6
TT
12573 {
12574 search_path_holder.reset (concat (".", dirname_separator_string,
12575 debug_file_directory,
12576 (char *) NULL));
12577 search_path = search_path_holder.get ();
12578 }
6ac97d4c 12579 else
e0cc99a6 12580 search_path = ".";
6ac97d4c 12581 }
9c02c129 12582 else
e0cc99a6 12583 search_path = debug_file_directory;
3019eac3 12584
24b9144d 12585 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12586 if (is_dwp)
12587 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12588
12589 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12590 desc = openp (search_path, flags, file_name,
3019eac3
DE
12591 O_RDONLY | O_BINARY, &absolute_name);
12592 if (desc < 0)
12593 return NULL;
12594
e0cc99a6
TT
12595 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12596 gnutarget, desc));
9c02c129
DE
12597 if (sym_bfd == NULL)
12598 return NULL;
192b62ce 12599 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12600
192b62ce
TT
12601 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12602 return NULL;
3019eac3 12603
13aaf454
DE
12604 /* Success. Record the bfd as having been included by the objfile's bfd.
12605 This is important because things like demangled_names_hash lives in the
12606 objfile's per_bfd space and may have references to things like symbol
12607 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12608 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12609
3019eac3
DE
12610 return sym_bfd;
12611}
12612
ab5088bf 12613/* Try to open DWO file FILE_NAME.
3019eac3
DE
12614 COMP_DIR is the DW_AT_comp_dir attribute.
12615 The result is the bfd handle of the file.
12616 If there is a problem finding or opening the file, return NULL.
12617 Upon success, the canonicalized path of the file is stored in the bfd,
12618 same as symfile_bfd_open. */
12619
192b62ce 12620static gdb_bfd_ref_ptr
976ca316 12621open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12622 const char *file_name, const char *comp_dir)
3019eac3 12623{
80626a55 12624 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12625 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12626 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12627
12628 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12629
12630 if (comp_dir != NULL)
12631 {
43816ebc
TT
12632 gdb::unique_xmalloc_ptr<char> path_to_try
12633 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12634
12635 /* NOTE: If comp_dir is a relative path, this will also try the
12636 search path, which seems useful. */
976ca316 12637 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12638 0 /*is_dwp*/,
192b62ce 12639 1 /*search_cwd*/));
3019eac3
DE
12640 if (abfd != NULL)
12641 return abfd;
12642 }
12643
12644 /* That didn't work, try debug-file-directory, which, despite its name,
12645 is a list of paths. */
12646
12647 if (*debug_file_directory == '\0')
12648 return NULL;
12649
976ca316 12650 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12651 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12652}
12653
80626a55
DE
12654/* This function is mapped across the sections and remembers the offset and
12655 size of each of the DWO debugging sections we are interested in. */
12656
12657static void
5bb6e9dd
TT
12658dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12659 dwo_sections *dwo_sections)
80626a55 12660{
80626a55
DE
12661 const struct dwop_section_names *names = &dwop_section_names;
12662
fbedd546 12663 if (names->abbrev_dwo.matches (sectp->name))
80626a55 12664 {
049412e3 12665 dwo_sections->abbrev.s.section = sectp;
fd361982 12666 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55 12667 }
fbedd546 12668 else if (names->info_dwo.matches (sectp->name))
80626a55 12669 {
049412e3 12670 dwo_sections->info.s.section = sectp;
fd361982 12671 dwo_sections->info.size = bfd_section_size (sectp);
80626a55 12672 }
fbedd546 12673 else if (names->line_dwo.matches (sectp->name))
80626a55 12674 {
049412e3 12675 dwo_sections->line.s.section = sectp;
fd361982 12676 dwo_sections->line.size = bfd_section_size (sectp);
80626a55 12677 }
fbedd546 12678 else if (names->loc_dwo.matches (sectp->name))
80626a55 12679 {
049412e3 12680 dwo_sections->loc.s.section = sectp;
fd361982 12681 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12682 }
fbedd546 12683 else if (names->loclists_dwo.matches (sectp->name))
41144253 12684 {
12685 dwo_sections->loclists.s.section = sectp;
12686 dwo_sections->loclists.size = bfd_section_size (sectp);
12687 }
fbedd546 12688 else if (names->macinfo_dwo.matches (sectp->name))
80626a55 12689 {
049412e3 12690 dwo_sections->macinfo.s.section = sectp;
fd361982 12691 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55 12692 }
fbedd546 12693 else if (names->macro_dwo.matches (sectp->name))
80626a55 12694 {
049412e3 12695 dwo_sections->macro.s.section = sectp;
fd361982 12696 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12697 }
fbedd546 12698 else if (names->rnglists_dwo.matches (sectp->name))
d0ce17d8
CT
12699 {
12700 dwo_sections->rnglists.s.section = sectp;
12701 dwo_sections->rnglists.size = bfd_section_size (sectp);
12702 }
fbedd546 12703 else if (names->str_dwo.matches (sectp->name))
80626a55 12704 {
049412e3 12705 dwo_sections->str.s.section = sectp;
fd361982 12706 dwo_sections->str.size = bfd_section_size (sectp);
80626a55 12707 }
fbedd546 12708 else if (names->str_offsets_dwo.matches (sectp->name))
80626a55 12709 {
049412e3 12710 dwo_sections->str_offsets.s.section = sectp;
fd361982 12711 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55 12712 }
fbedd546 12713 else if (names->types_dwo.matches (sectp->name))
80626a55
DE
12714 {
12715 struct dwarf2_section_info type_section;
12716
12717 memset (&type_section, 0, sizeof (type_section));
049412e3 12718 type_section.s.section = sectp;
fd361982 12719 type_section.size = bfd_section_size (sectp);
fd5866f6 12720 dwo_sections->types.push_back (type_section);
80626a55
DE
12721 }
12722}
12723
ab5088bf 12724/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12725 by PER_CU. This is for the non-DWP case.
80626a55 12726 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12727
12728static struct dwo_file *
4ab09049
SM
12729open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12730 const char *comp_dir)
3019eac3 12731{
976ca316 12732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12733
976ca316 12734 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12735 if (dbfd == NULL)
12736 {
6f738b01
SM
12737 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12738
80626a55
DE
12739 return NULL;
12740 }
263db9a1 12741
51ac9db5 12742 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12743 dwo_file->dwo_name = dwo_name;
12744 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12745 dwo_file->dbfd = std::move (dbfd);
3019eac3 12746
5bb6e9dd
TT
12747 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12748 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12749 &dwo_file->sections);
3019eac3 12750
976ca316
SM
12751 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12752 dwo_file->cus);
3019eac3 12753
d2854d8d
CT
12754 if (cu->per_cu->dwarf_version < 5)
12755 {
12756 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12757 dwo_file->sections.types, dwo_file->tus);
12758 }
12759 else
12760 {
12761 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12762 &dwo_file->sections.info, dwo_file->tus,
30c80d88 12763 rcuh_kind::COMPILE);
d2854d8d 12764 }
3019eac3 12765
6f738b01 12766 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12767
263db9a1 12768 return dwo_file.release ();
3019eac3
DE
12769}
12770
80626a55 12771/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12772 size of each of the DWP debugging sections common to version 1 and 2 that
12773 we are interested in. */
3019eac3 12774
80626a55 12775static void
73869dc2 12776dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12777 dwp_file *dwp_file)
3019eac3 12778{
80626a55
DE
12779 const struct dwop_section_names *names = &dwop_section_names;
12780 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12781
80626a55 12782 /* Record the ELF section number for later lookup: this is what the
73869dc2 12783 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12784 gdb_assert (elf_section_nr < dwp_file->num_sections);
12785 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12786
80626a55 12787 /* Look for specific sections that we need. */
fbedd546 12788 if (names->str_dwo.matches (sectp->name))
80626a55 12789 {
049412e3 12790 dwp_file->sections.str.s.section = sectp;
fd361982 12791 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55 12792 }
fbedd546 12793 else if (names->cu_index.matches (sectp->name))
80626a55 12794 {
049412e3 12795 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12796 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55 12797 }
fbedd546 12798 else if (names->tu_index.matches (sectp->name))
80626a55 12799 {
049412e3 12800 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12801 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12802 }
12803}
3019eac3 12804
73869dc2
DE
12805/* This function is mapped across the sections and remembers the offset and
12806 size of each of the DWP version 2 debugging sections that we are interested
12807 in. This is split into a separate function because we don't know if we
d2854d8d 12808 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12809
12810static void
12811dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12812{
9a3c8263 12813 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12814 const struct dwop_section_names *names = &dwop_section_names;
12815 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12816
12817 /* Record the ELF section number for later lookup: this is what the
12818 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12819 gdb_assert (elf_section_nr < dwp_file->num_sections);
12820 dwp_file->elf_sections[elf_section_nr] = sectp;
12821
12822 /* Look for specific sections that we need. */
fbedd546 12823 if (names->abbrev_dwo.matches (sectp->name))
73869dc2 12824 {
049412e3 12825 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12826 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2 12827 }
fbedd546 12828 else if (names->info_dwo.matches (sectp->name))
73869dc2 12829 {
049412e3 12830 dwp_file->sections.info.s.section = sectp;
fd361982 12831 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2 12832 }
fbedd546 12833 else if (names->line_dwo.matches (sectp->name))
73869dc2 12834 {
049412e3 12835 dwp_file->sections.line.s.section = sectp;
fd361982 12836 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2 12837 }
fbedd546 12838 else if (names->loc_dwo.matches (sectp->name))
73869dc2 12839 {
049412e3 12840 dwp_file->sections.loc.s.section = sectp;
fd361982 12841 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2 12842 }
fbedd546 12843 else if (names->macinfo_dwo.matches (sectp->name))
73869dc2 12844 {
049412e3 12845 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12846 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2 12847 }
fbedd546 12848 else if (names->macro_dwo.matches (sectp->name))
73869dc2 12849 {
049412e3 12850 dwp_file->sections.macro.s.section = sectp;
fd361982 12851 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2 12852 }
fbedd546 12853 else if (names->str_offsets_dwo.matches (sectp->name))
73869dc2 12854 {
049412e3 12855 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12856 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2 12857 }
fbedd546 12858 else if (names->types_dwo.matches (sectp->name))
73869dc2 12859 {
049412e3 12860 dwp_file->sections.types.s.section = sectp;
fd361982 12861 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12862 }
12863}
12864
d2854d8d
CT
12865/* This function is mapped across the sections and remembers the offset and
12866 size of each of the DWP version 5 debugging sections that we are interested
12867 in. This is split into a separate function because we don't know if we
12868 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12869
12870static void
12871dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12872{
12873 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12874 const struct dwop_section_names *names = &dwop_section_names;
12875 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12876
12877 /* Record the ELF section number for later lookup: this is what the
12878 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12879 gdb_assert (elf_section_nr < dwp_file->num_sections);
12880 dwp_file->elf_sections[elf_section_nr] = sectp;
12881
12882 /* Look for specific sections that we need. */
fbedd546 12883 if (names->abbrev_dwo.matches (sectp->name))
d2854d8d
CT
12884 {
12885 dwp_file->sections.abbrev.s.section = sectp;
12886 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12887 }
fbedd546 12888 else if (names->info_dwo.matches (sectp->name))
d2854d8d
CT
12889 {
12890 dwp_file->sections.info.s.section = sectp;
12891 dwp_file->sections.info.size = bfd_section_size (sectp);
12892 }
fbedd546 12893 else if (names->line_dwo.matches (sectp->name))
d2854d8d
CT
12894 {
12895 dwp_file->sections.line.s.section = sectp;
12896 dwp_file->sections.line.size = bfd_section_size (sectp);
12897 }
fbedd546 12898 else if (names->loclists_dwo.matches (sectp->name))
d2854d8d
CT
12899 {
12900 dwp_file->sections.loclists.s.section = sectp;
12901 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12902 }
fbedd546 12903 else if (names->macro_dwo.matches (sectp->name))
d2854d8d
CT
12904 {
12905 dwp_file->sections.macro.s.section = sectp;
12906 dwp_file->sections.macro.size = bfd_section_size (sectp);
12907 }
fbedd546 12908 else if (names->rnglists_dwo.matches (sectp->name))
d2854d8d
CT
12909 {
12910 dwp_file->sections.rnglists.s.section = sectp;
12911 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12912 }
fbedd546 12913 else if (names->str_offsets_dwo.matches (sectp->name))
d2854d8d
CT
12914 {
12915 dwp_file->sections.str_offsets.s.section = sectp;
12916 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12917 }
12918}
12919
80626a55 12920/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12921
80626a55
DE
12922static hashval_t
12923hash_dwp_loaded_cutus (const void *item)
12924{
9a3c8263 12925 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12926
80626a55
DE
12927 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12928 return dwo_unit->signature;
3019eac3
DE
12929}
12930
80626a55 12931/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12932
80626a55
DE
12933static int
12934eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12935{
9a3c8263
SM
12936 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12937 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12938
80626a55
DE
12939 return dua->signature == dub->signature;
12940}
3019eac3 12941
80626a55 12942/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12943
48b490f2 12944static htab_up
298e9637 12945allocate_dwp_loaded_cutus_table ()
80626a55 12946{
48b490f2
TT
12947 return htab_up (htab_create_alloc (3,
12948 hash_dwp_loaded_cutus,
12949 eq_dwp_loaded_cutus,
12950 NULL, xcalloc, xfree));
80626a55 12951}
3019eac3 12952
ab5088bf
DE
12953/* Try to open DWP file FILE_NAME.
12954 The result is the bfd handle of the file.
12955 If there is a problem finding or opening the file, return NULL.
12956 Upon success, the canonicalized path of the file is stored in the bfd,
12957 same as symfile_bfd_open. */
12958
192b62ce 12959static gdb_bfd_ref_ptr
976ca316 12960open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 12961{
976ca316 12962 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 12963 1 /*is_dwp*/,
192b62ce 12964 1 /*search_cwd*/));
6ac97d4c
DE
12965 if (abfd != NULL)
12966 return abfd;
12967
12968 /* Work around upstream bug 15652.
12969 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12970 [Whether that's a "bug" is debatable, but it is getting in our way.]
12971 We have no real idea where the dwp file is, because gdb's realpath-ing
12972 of the executable's path may have discarded the needed info.
12973 [IWBN if the dwp file name was recorded in the executable, akin to
12974 .gnu_debuglink, but that doesn't exist yet.]
12975 Strip the directory from FILE_NAME and search again. */
12976 if (*debug_file_directory != '\0')
12977 {
12978 /* Don't implicitly search the current directory here.
12979 If the user wants to search "." to handle this case,
12980 it must be added to debug-file-directory. */
976ca316
SM
12981 return try_open_dwop_file (per_objfile, lbasename (file_name),
12982 1 /*is_dwp*/,
6ac97d4c
DE
12983 0 /*search_cwd*/);
12984 }
12985
12986 return NULL;
ab5088bf
DE
12987}
12988
80626a55
DE
12989/* Initialize the use of the DWP file for the current objfile.
12990 By convention the name of the DWP file is ${objfile}.dwp.
12991 The result is NULL if it can't be found. */
a766d390 12992
400174b1 12993static std::unique_ptr<struct dwp_file>
976ca316 12994open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 12995{
976ca316 12996 struct objfile *objfile = per_objfile->objfile;
80626a55 12997
82bf32bc
JK
12998 /* Try to find first .dwp for the binary file before any symbolic links
12999 resolving. */
6c447423
DE
13000
13001 /* If the objfile is a debug file, find the name of the real binary
13002 file and get the name of dwp file from there. */
d721ba37 13003 std::string dwp_name;
6c447423
DE
13004 if (objfile->separate_debug_objfile_backlink != NULL)
13005 {
13006 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13007 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13008
d721ba37 13009 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13010 }
13011 else
d721ba37
PA
13012 dwp_name = objfile->original_name;
13013
13014 dwp_name += ".dwp";
80626a55 13015
976ca316 13016 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13017 if (dbfd == NULL
13018 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13019 {
13020 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13021 dwp_name = objfile_name (objfile);
13022 dwp_name += ".dwp";
976ca316 13023 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13024 }
13025
80626a55
DE
13026 if (dbfd == NULL)
13027 {
6f738b01
SM
13028 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13029
400174b1 13030 return std::unique_ptr<dwp_file> ();
3019eac3 13031 }
400174b1
TT
13032
13033 const char *name = bfd_get_filename (dbfd.get ());
13034 std::unique_ptr<struct dwp_file> dwp_file
13035 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13036
0a0f4c01 13037 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13038 dwp_file->elf_sections =
976ca316 13039 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13040 dwp_file->num_sections, asection *);
13041
5bb6e9dd
TT
13042 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13043 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13044 dwp_file.get ());
80626a55 13045
976ca316 13046 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13047
976ca316 13048 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13049
73869dc2 13050 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13051 if (dwp_file->cus && dwp_file->tus
13052 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13053 {
13054 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13055 pretty bizarre. We use pulongest here because that's the established
4d65956b 13056 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13057 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13058 " TU version %s [in DWP file %s]"),
13059 pulongest (dwp_file->cus->version),
d721ba37 13060 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13061 }
08302ed2
DE
13062
13063 if (dwp_file->cus)
13064 dwp_file->version = dwp_file->cus->version;
13065 else if (dwp_file->tus)
13066 dwp_file->version = dwp_file->tus->version;
13067 else
13068 dwp_file->version = 2;
73869dc2 13069
5bb6e9dd
TT
13070 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13071 {
13072 if (dwp_file->version == 2)
13073 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13074 dwp_file.get ());
13075 else
13076 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13077 dwp_file.get ());
13078 }
73869dc2 13079
298e9637
SM
13080 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13081 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13082
6f738b01
SM
13083 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13084 dwarf_read_debug_printf (" %s CUs, %s TUs",
13085 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13086 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13087
13088 return dwp_file;
3019eac3 13089}
c906108c 13090
ab5088bf
DE
13091/* Wrapper around open_and_init_dwp_file, only open it once. */
13092
13093static struct dwp_file *
976ca316 13094get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13095{
976ca316 13096 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13097 {
976ca316
SM
13098 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13099 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13100 }
976ca316 13101 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13102}
13103
80626a55
DE
13104/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13105 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13106 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13107 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13108 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13109
13110 This is called, for example, when wanting to read a variable with a
13111 complex location. Therefore we don't want to do file i/o for every call.
13112 Therefore we don't want to look for a DWO file on every call.
13113 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13114 then we check if we've already seen DWO_NAME, and only THEN do we check
13115 for a DWO file.
13116
1c658ad5 13117 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13118 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13119
3019eac3 13120static struct dwo_unit *
4ab09049 13121lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13122 ULONGEST signature, int is_debug_types)
3019eac3 13123{
976ca316
SM
13124 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13125 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13126 const char *kind = is_debug_types ? "TU" : "CU";
13127 void **dwo_file_slot;
3019eac3 13128 struct dwo_file *dwo_file;
80626a55 13129 struct dwp_file *dwp_file;
cb1df416 13130
6a506a2d
DE
13131 /* First see if there's a DWP file.
13132 If we have a DWP file but didn't find the DWO inside it, don't
13133 look for the original DWO file. It makes gdb behave differently
13134 depending on whether one is debugging in the build tree. */
cf2c3c16 13135
976ca316 13136 dwp_file = get_dwp_file (per_objfile);
80626a55 13137 if (dwp_file != NULL)
cf2c3c16 13138 {
80626a55
DE
13139 const struct dwp_hash_table *dwp_htab =
13140 is_debug_types ? dwp_file->tus : dwp_file->cus;
13141
13142 if (dwp_htab != NULL)
13143 {
13144 struct dwo_unit *dwo_cutu =
976ca316
SM
13145 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13146 is_debug_types);
80626a55
DE
13147
13148 if (dwo_cutu != NULL)
13149 {
6f738b01
SM
13150 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13151 kind, hex_string (signature),
13152 host_address_to_string (dwo_cutu));
13153
80626a55
DE
13154 return dwo_cutu;
13155 }
13156 }
13157 }
6a506a2d 13158 else
80626a55 13159 {
6a506a2d 13160 /* No DWP file, look for the DWO file. */
80626a55 13161
976ca316 13162 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13163 if (*dwo_file_slot == NULL)
80626a55 13164 {
6a506a2d 13165 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13166 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13167 }
6a506a2d 13168 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13169 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13170
6a506a2d 13171 if (dwo_file != NULL)
19c3d4c9 13172 {
6a506a2d
DE
13173 struct dwo_unit *dwo_cutu = NULL;
13174
13175 if (is_debug_types && dwo_file->tus)
13176 {
13177 struct dwo_unit find_dwo_cutu;
13178
13179 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13180 find_dwo_cutu.signature = signature;
9a3c8263 13181 dwo_cutu
b0b6a987
TT
13182 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13183 &find_dwo_cutu);
6a506a2d 13184 }
33c5cd75 13185 else if (!is_debug_types && dwo_file->cus)
80626a55 13186 {
33c5cd75
DB
13187 struct dwo_unit find_dwo_cutu;
13188
13189 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13190 find_dwo_cutu.signature = signature;
b0b6a987 13191 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13192 &find_dwo_cutu);
6a506a2d
DE
13193 }
13194
13195 if (dwo_cutu != NULL)
13196 {
6f738b01
SM
13197 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13198 kind, dwo_name, hex_string (signature),
13199 host_address_to_string (dwo_cutu));
13200
6a506a2d 13201 return dwo_cutu;
80626a55
DE
13202 }
13203 }
2e276125 13204 }
9cdd5dbd 13205
80626a55
DE
13206 /* We didn't find it. This could mean a dwo_id mismatch, or
13207 someone deleted the DWO/DWP file, or the search path isn't set up
13208 correctly to find the file. */
13209
6f738b01
SM
13210 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13211 kind, dwo_name, hex_string (signature));
3019eac3 13212
6656a72d
DE
13213 /* This is a warning and not a complaint because it can be caused by
13214 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13215 {
13216 /* Print the name of the DWP file if we looked there, helps the user
13217 better diagnose the problem. */
791afaa2 13218 std::string dwp_text;
43942612
DE
13219
13220 if (dwp_file != NULL)
791afaa2
TT
13221 dwp_text = string_printf (" [in DWP file %s]",
13222 lbasename (dwp_file->name));
43942612 13223
9d8780f0 13224 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13225 " [in module %s]"),
4ab09049
SM
13226 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13227 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13228 }
3019eac3 13229 return NULL;
5fb290d7
DJ
13230}
13231
80626a55
DE
13232/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13233 See lookup_dwo_cutu_unit for details. */
13234
13235static struct dwo_unit *
4ab09049 13236lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13237 ULONGEST signature)
13238{
4ab09049
SM
13239 gdb_assert (!cu->per_cu->is_debug_types);
13240
13241 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13242}
13243
13244/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13245 See lookup_dwo_cutu_unit for details. */
13246
13247static struct dwo_unit *
4ab09049 13248lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13249{
4ab09049
SM
13250 gdb_assert (cu->per_cu->is_debug_types);
13251
13252 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13253
13254 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13255}
13256
89e63ee4
DE
13257/* Traversal function for queue_and_load_all_dwo_tus. */
13258
13259static int
13260queue_and_load_dwo_tu (void **slot, void *info)
13261{
13262 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13263 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13264 ULONGEST signature = dwo_unit->signature;
d460f660 13265 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13266
13267 if (sig_type != NULL)
13268 {
13269 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13270
13271 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13272 a real dependency of PER_CU on SIG_TYPE. That is detected later
13273 while processing PER_CU. */
120ce1b5 13274 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13275 load_full_type_unit (sig_cu, cu->per_objfile);
13276 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13277 }
13278
13279 return 1;
13280}
13281
1b555f17 13282/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13283 The DWO may have the only definition of the type, though it may not be
13284 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13285 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13286
13287static void
1b555f17 13288queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13289{
13290 struct dwo_unit *dwo_unit;
13291 struct dwo_file *dwo_file;
13292
1b555f17
SM
13293 gdb_assert (cu != nullptr);
13294 gdb_assert (!cu->per_cu->is_debug_types);
13295 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13296
1b555f17 13297 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13298 gdb_assert (dwo_unit != NULL);
13299
13300 dwo_file = dwo_unit->dwo_file;
13301 if (dwo_file->tus != NULL)
1b555f17 13302 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13303}
13304
3019eac3 13305/* Read in various DIEs. */
348e048f 13306
d389af10 13307/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13308 Inherit only the children of the DW_AT_abstract_origin DIE not being
13309 already referenced by DW_AT_abstract_origin from the children of the
13310 current DIE. */
d389af10
JK
13311
13312static void
13313inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13314{
13315 struct die_info *child_die;
791afaa2 13316 sect_offset *offsetp;
d389af10
JK
13317 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13318 struct die_info *origin_die;
13319 /* Iterator of the ORIGIN_DIE children. */
13320 struct die_info *origin_child_die;
d389af10 13321 struct attribute *attr;
cd02d79d
PA
13322 struct dwarf2_cu *origin_cu;
13323 struct pending **origin_previous_list_in_scope;
d389af10
JK
13324
13325 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13326 if (!attr)
13327 return;
13328
cd02d79d
PA
13329 /* Note that following die references may follow to a die in a
13330 different cu. */
13331
13332 origin_cu = cu;
13333 origin_die = follow_die_ref (die, attr, &origin_cu);
13334
13335 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13336 symbols in. */
13337 origin_previous_list_in_scope = origin_cu->list_in_scope;
13338 origin_cu->list_in_scope = cu->list_in_scope;
13339
edb3359d
DJ
13340 if (die->tag != origin_die->tag
13341 && !(die->tag == DW_TAG_inlined_subroutine
13342 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13343 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13344 sect_offset_str (die->sect_off),
13345 sect_offset_str (origin_die->sect_off));
d389af10 13346
791afaa2 13347 std::vector<sect_offset> offsets;
d389af10 13348
3ea89b92
PMR
13349 for (child_die = die->child;
13350 child_die && child_die->tag;
436c571c 13351 child_die = child_die->sibling)
3ea89b92
PMR
13352 {
13353 struct die_info *child_origin_die;
13354 struct dwarf2_cu *child_origin_cu;
13355
13356 /* We are trying to process concrete instance entries:
216f72a1 13357 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13358 it's not relevant to our analysis here. i.e. detecting DIEs that are
13359 present in the abstract instance but not referenced in the concrete
13360 one. */
216f72a1 13361 if (child_die->tag == DW_TAG_call_site
dda83cd7 13362 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13363 continue;
13364
c38f313d
DJ
13365 /* For each CHILD_DIE, find the corresponding child of
13366 ORIGIN_DIE. If there is more than one layer of
13367 DW_AT_abstract_origin, follow them all; there shouldn't be,
13368 but GCC versions at least through 4.4 generate this (GCC PR
13369 40573). */
3ea89b92
PMR
13370 child_origin_die = child_die;
13371 child_origin_cu = cu;
c38f313d
DJ
13372 while (1)
13373 {
cd02d79d
PA
13374 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13375 child_origin_cu);
c38f313d
DJ
13376 if (attr == NULL)
13377 break;
cd02d79d
PA
13378 child_origin_die = follow_die_ref (child_origin_die, attr,
13379 &child_origin_cu);
c38f313d
DJ
13380 }
13381
d389af10
JK
13382 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13383 counterpart may exist. */
c38f313d 13384 if (child_origin_die != child_die)
d389af10 13385 {
edb3359d
DJ
13386 if (child_die->tag != child_origin_die->tag
13387 && !(child_die->tag == DW_TAG_inlined_subroutine
13388 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13389 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13390 "different tags"),
9d8780f0
SM
13391 sect_offset_str (child_die->sect_off),
13392 sect_offset_str (child_origin_die->sect_off));
c38f313d 13393 if (child_origin_die->parent != origin_die)
b98664d3 13394 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13395 "different parents"),
9d8780f0
SM
13396 sect_offset_str (child_die->sect_off),
13397 sect_offset_str (child_origin_die->sect_off));
c38f313d 13398 else
791afaa2 13399 offsets.push_back (child_origin_die->sect_off);
d389af10 13400 }
d389af10 13401 }
791afaa2
TT
13402 std::sort (offsets.begin (), offsets.end ());
13403 sect_offset *offsets_end = offsets.data () + offsets.size ();
13404 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13405 if (offsetp[-1] == *offsetp)
b98664d3 13406 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13407 "to DIE %s as their abstract origin"),
13408 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13409
791afaa2 13410 offsetp = offsets.data ();
d389af10
JK
13411 origin_child_die = origin_die->child;
13412 while (origin_child_die && origin_child_die->tag)
13413 {
13414 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13415 while (offsetp < offsets_end
9c541725 13416 && *offsetp < origin_child_die->sect_off)
d389af10 13417 offsetp++;
b64f50a1 13418 if (offsetp >= offsets_end
9c541725 13419 || *offsetp > origin_child_die->sect_off)
d389af10 13420 {
adde2bff
DE
13421 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13422 Check whether we're already processing ORIGIN_CHILD_DIE.
13423 This can happen with mutually referenced abstract_origins.
13424 PR 16581. */
13425 if (!origin_child_die->in_process)
13426 process_die (origin_child_die, origin_cu);
d389af10 13427 }
436c571c 13428 origin_child_die = origin_child_die->sibling;
d389af10 13429 }
cd02d79d 13430 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13431
13432 if (cu != origin_cu)
13433 compute_delayed_physnames (origin_cu);
d389af10
JK
13434}
13435
c906108c 13436static void
e7c27a73 13437read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13438{
5e22e966 13439 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13440 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13441 struct context_stack *newobj;
c906108c
SS
13442 CORE_ADDR lowpc;
13443 CORE_ADDR highpc;
13444 struct die_info *child_die;
edb3359d 13445 struct attribute *attr, *call_line, *call_file;
15d034d0 13446 const char *name;
e142c38c 13447 CORE_ADDR baseaddr;
801e3a5b 13448 struct block *block;
edb3359d 13449 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13450 std::vector<struct symbol *> template_args;
34eaf542 13451 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13452
13453 if (inlined_func)
13454 {
13455 /* If we do not have call site information, we can't show the
13456 caller of this inlined function. That's too confusing, so
13457 only use the scope for local variables. */
13458 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13459 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13460 if (call_line == NULL || call_file == NULL)
13461 {
13462 read_lexical_block_scope (die, cu);
13463 return;
13464 }
13465 }
c906108c 13466
b3b3bada 13467 baseaddr = objfile->text_section_offset ();
e142c38c 13468
94af9270 13469 name = dwarf2_name (die, cu);
c906108c 13470
e8d05480
JB
13471 /* Ignore functions with missing or empty names. These are actually
13472 illegal according to the DWARF standard. */
13473 if (name == NULL)
13474 {
b98664d3 13475 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13476 sect_offset_str (die->sect_off));
e8d05480
JB
13477 return;
13478 }
13479
13480 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13481 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13482 <= PC_BOUNDS_INVALID)
e8d05480 13483 {
ae4d0c03 13484 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13485 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13486 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13487 "for subprogram DIE at %s"),
13488 sect_offset_str (die->sect_off));
e8d05480
JB
13489 return;
13490 }
c906108c 13491
3e29f34a
MR
13492 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13493 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13494
34eaf542
TT
13495 /* If we have any template arguments, then we must allocate a
13496 different sort of symbol. */
436c571c 13497 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13498 {
13499 if (child_die->tag == DW_TAG_template_type_param
13500 || child_die->tag == DW_TAG_template_value_param)
13501 {
8c14c3a3 13502 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13503 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13504 break;
13505 }
13506 }
13507
c24bdb02 13508 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13509 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13510 (struct symbol *) templ_func);
4c2df51b 13511
81873cc8 13512 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13513 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13514 cu->language);
13515
4cecd739
DJ
13516 /* If there is a location expression for DW_AT_frame_base, record
13517 it. */
e142c38c 13518 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13519 if (attr != nullptr)
fe978cb0 13520 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13521
63e43d3a
PMR
13522 /* If there is a location for the static link, record it. */
13523 newobj->static_link = NULL;
13524 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13525 if (attr != nullptr)
63e43d3a 13526 {
224c3ddb
SM
13527 newobj->static_link
13528 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13529 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13530 cu->addr_type ());
63e43d3a
PMR
13531 }
13532
c24bdb02 13533 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13534
639d11d3 13535 if (die->child != NULL)
c906108c 13536 {
639d11d3 13537 child_die = die->child;
c906108c
SS
13538 while (child_die && child_die->tag)
13539 {
34eaf542
TT
13540 if (child_die->tag == DW_TAG_template_type_param
13541 || child_die->tag == DW_TAG_template_value_param)
13542 {
13543 struct symbol *arg = new_symbol (child_die, NULL, cu);
13544
f1078f66 13545 if (arg != NULL)
2f4732b0 13546 template_args.push_back (arg);
34eaf542
TT
13547 }
13548 else
13549 process_die (child_die, cu);
436c571c 13550 child_die = child_die->sibling;
c906108c
SS
13551 }
13552 }
13553
d389af10
JK
13554 inherit_abstract_dies (die, cu);
13555
4a811a97
UW
13556 /* If we have a DW_AT_specification, we might need to import using
13557 directives from the context of the specification DIE. See the
13558 comment in determine_prefix. */
13559 if (cu->language == language_cplus
13560 && dwarf2_attr (die, DW_AT_specification, cu))
13561 {
13562 struct dwarf2_cu *spec_cu = cu;
13563 struct die_info *spec_die = die_specification (die, &spec_cu);
13564
13565 while (spec_die)
13566 {
13567 child_die = spec_die->child;
13568 while (child_die && child_die->tag)
13569 {
13570 if (child_die->tag == DW_TAG_imported_module)
13571 process_die (child_die, spec_cu);
436c571c 13572 child_die = child_die->sibling;
4a811a97
UW
13573 }
13574
13575 /* In some cases, GCC generates specification DIEs that
13576 themselves contain DW_AT_specification attributes. */
13577 spec_die = die_specification (spec_die, &spec_cu);
13578 }
13579 }
13580
c24bdb02 13581 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13582 /* Make a block for the local symbols within. */
c24bdb02 13583 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13584 cstk.static_link, lowpc, highpc);
801e3a5b 13585
df8a16a1 13586 /* For C++, set the block's scope. */
45280282
IB
13587 if ((cu->language == language_cplus
13588 || cu->language == language_fortran
c44af4eb
TT
13589 || cu->language == language_d
13590 || cu->language == language_rust)
4d4ec4e5 13591 && cu->processing_has_namespace_info)
195a3f6c
TT
13592 block_set_scope (block, determine_prefix (die, cu),
13593 &objfile->objfile_obstack);
df8a16a1 13594
801e3a5b
JB
13595 /* If we have address ranges, record them. */
13596 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13597
a60f3166 13598 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13599
34eaf542 13600 /* Attach template arguments to function. */
2f4732b0 13601 if (!template_args.empty ())
34eaf542
TT
13602 {
13603 gdb_assert (templ_func != NULL);
13604
2f4732b0 13605 templ_func->n_template_arguments = template_args.size ();
34eaf542 13606 templ_func->template_arguments
dda83cd7 13607 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13608 templ_func->n_template_arguments);
34eaf542 13609 memcpy (templ_func->template_arguments,
2f4732b0 13610 template_args.data (),
34eaf542 13611 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13612
13613 /* Make sure that the symtab is set on the new symbols. Even
13614 though they don't appear in this symtab directly, other parts
13615 of gdb assume that symbols do, and this is reasonably
13616 true. */
8634679f 13617 for (symbol *sym : template_args)
3e1d3d8c 13618 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13619 }
13620
208d8187
JB
13621 /* In C++, we can have functions nested inside functions (e.g., when
13622 a function declares a class that has methods). This means that
13623 when we finish processing a function scope, we may need to go
13624 back to building a containing block's symbol lists. */
c24bdb02
KS
13625 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13626 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13627
921e78cf
JB
13628 /* If we've finished processing a top-level function, subsequent
13629 symbols go in the file symbol list. */
c24bdb02
KS
13630 if (cu->get_builder ()->outermost_context_p ())
13631 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13632}
13633
13634/* Process all the DIES contained within a lexical block scope. Start
13635 a new scope, process the dies, and then close the scope. */
13636
13637static void
e7c27a73 13638read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13639{
5e22e966 13640 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13641 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13642 CORE_ADDR lowpc, highpc;
13643 struct die_info *child_die;
e142c38c
DJ
13644 CORE_ADDR baseaddr;
13645
b3b3bada 13646 baseaddr = objfile->text_section_offset ();
c906108c
SS
13647
13648 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13649 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13650 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13651 be nasty. Might be easier to properly extend generic blocks to
af34e669 13652 describe ranges. */
e385593e
JK
13653 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13654 {
13655 case PC_BOUNDS_NOT_PRESENT:
13656 /* DW_TAG_lexical_block has no attributes, process its children as if
13657 there was no wrapping by that DW_TAG_lexical_block.
13658 GCC does no longer produces such DWARF since GCC r224161. */
13659 for (child_die = die->child;
13660 child_die != NULL && child_die->tag;
436c571c 13661 child_die = child_die->sibling)
4f7bc5ed
TT
13662 {
13663 /* We might already be processing this DIE. This can happen
13664 in an unusual circumstance -- where a subroutine A
13665 appears lexically in another subroutine B, but A actually
13666 inlines B. The recursion is broken here, rather than in
13667 inherit_abstract_dies, because it seems better to simply
13668 drop concrete children here. */
13669 if (!child_die->in_process)
13670 process_die (child_die, cu);
13671 }
e385593e
JK
13672 return;
13673 case PC_BOUNDS_INVALID:
13674 return;
13675 }
3e29f34a
MR
13676 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13677 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13678
c24bdb02 13679 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13680 if (die->child != NULL)
c906108c 13681 {
639d11d3 13682 child_die = die->child;
c906108c
SS
13683 while (child_die && child_die->tag)
13684 {
e7c27a73 13685 process_die (child_die, cu);
436c571c 13686 child_die = child_die->sibling;
c906108c
SS
13687 }
13688 }
3ea89b92 13689 inherit_abstract_dies (die, cu);
c24bdb02 13690 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13691
c24bdb02
KS
13692 if (*cu->get_builder ()->get_local_symbols () != NULL
13693 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13694 {
801e3a5b 13695 struct block *block
dda83cd7 13696 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13697 cstk.start_addr, highpc);
801e3a5b
JB
13698
13699 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13700 do here, means that recording a parent's ranges entails
13701 walking across all its children's ranges as they appear in
13702 the address map, which is quadratic behavior.
13703
13704 It would be nicer to record the parent's ranges before
13705 traversing its children, simply overriding whatever you find
13706 there. But since we don't even decide whether to create a
13707 block until after we've traversed its children, that's hard
13708 to do. */
801e3a5b 13709 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13710 }
c24bdb02
KS
13711 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13712 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13713}
13714
216f72a1 13715/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13716
13717static void
13718read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13719{
5e22e966 13720 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13721 struct objfile *objfile = per_objfile->objfile;
08feed99 13722 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13723 CORE_ADDR pc, baseaddr;
13724 struct attribute *attr;
13725 struct call_site *call_site, call_site_local;
13726 void **slot;
13727 int nparams;
13728 struct die_info *child_die;
13729
b3b3bada 13730 baseaddr = objfile->text_section_offset ();
96408a79 13731
216f72a1
JK
13732 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13733 if (attr == NULL)
13734 {
13735 /* This was a pre-DWARF-5 GNU extension alias
13736 for DW_AT_call_return_pc. */
13737 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13738 }
96408a79
SA
13739 if (!attr)
13740 {
b98664d3 13741 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13742 "DIE %s [in module %s]"),
13743 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13744 return;
13745 }
95f982e5 13746 pc = attr->as_address () + baseaddr;
3e29f34a 13747 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13748
13749 if (cu->call_site_htab == NULL)
13750 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13751 NULL, &objfile->objfile_obstack,
13752 hashtab_obstack_allocate, NULL);
13753 call_site_local.pc = pc;
13754 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13755 if (*slot != NULL)
13756 {
b98664d3 13757 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13758 "DIE %s [in module %s]"),
13759 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13760 objfile_name (objfile));
96408a79
SA
13761 return;
13762 }
13763
13764 /* Count parameters at the caller. */
13765
13766 nparams = 0;
13767 for (child_die = die->child; child_die && child_die->tag;
436c571c 13768 child_die = child_die->sibling)
96408a79 13769 {
216f72a1 13770 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13771 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13772 {
b98664d3 13773 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13774 "DW_TAG_call_site child DIE %s [in module %s]"),
13775 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13776 objfile_name (objfile));
96408a79
SA
13777 continue;
13778 }
13779
13780 nparams++;
13781 }
13782
224c3ddb
SM
13783 call_site
13784 = ((struct call_site *)
13785 obstack_alloc (&objfile->objfile_obstack,
13786 sizeof (*call_site)
13787 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13788 *slot = call_site;
13789 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13790 call_site->pc = pc;
13791
216f72a1
JK
13792 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13793 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13794 {
13795 struct die_info *func_die;
13796
13797 /* Skip also over DW_TAG_inlined_subroutine. */
13798 for (func_die = die->parent;
13799 func_die && func_die->tag != DW_TAG_subprogram
13800 && func_die->tag != DW_TAG_subroutine_type;
13801 func_die = func_die->parent);
13802
216f72a1
JK
13803 /* DW_AT_call_all_calls is a superset
13804 of DW_AT_call_all_tail_calls. */
96408a79 13805 if (func_die
dda83cd7
SM
13806 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13807 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13808 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13809 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13810 {
13811 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13812 not complete. But keep CALL_SITE for look ups via call_site_htab,
13813 both the initial caller containing the real return address PC and
13814 the final callee containing the current PC of a chain of tail
13815 calls do not need to have the tail call list complete. But any
13816 function candidate for a virtual tail call frame searched via
13817 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13818 determined unambiguously. */
13819 }
13820 else
13821 {
13822 struct type *func_type = NULL;
13823
13824 if (func_die)
13825 func_type = get_die_type (func_die, cu);
13826 if (func_type != NULL)
13827 {
78134374 13828 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13829
13830 /* Enlist this call site to the function. */
13831 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13832 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13833 }
13834 else
b98664d3 13835 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13836 "DIE %s [in module %s]"),
13837 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13838 }
13839 }
13840
216f72a1
JK
13841 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13842 if (attr == NULL)
13843 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13844 if (attr == NULL)
13845 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13846 if (attr == NULL)
216f72a1
JK
13847 {
13848 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13849 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13850 }
96408a79 13851 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 13852 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 13853 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13854 else if (attr->form_is_block ())
96408a79
SA
13855 {
13856 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 13857 struct dwarf_block *block = attr->as_block ();
96408a79 13858
8d749320 13859 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
13860 dlbaton->data = block->data;
13861 dlbaton->size = block->size;
a50264ba 13862 dlbaton->per_objfile = per_objfile;
96408a79
SA
13863 dlbaton->per_cu = cu->per_cu;
13864
13865 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13866 }
cd6c91b4 13867 else if (attr->form_is_ref ())
96408a79 13868 {
96408a79
SA
13869 struct dwarf2_cu *target_cu = cu;
13870 struct die_info *target_die;
13871
ac9ec31b 13872 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13873 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13874 if (die_is_declaration (target_die, target_cu))
13875 {
7d45c7c3 13876 const char *target_physname;
9112db09
JK
13877
13878 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13879 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13880 if (target_physname == NULL)
9112db09 13881 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13882 if (target_physname == NULL)
b98664d3 13883 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13884 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 13885 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13886 else
7d455152 13887 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13888 }
13889 else
13890 {
13891 CORE_ADDR lowpc;
13892
13893 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13894 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13895 <= PC_BOUNDS_INVALID)
b98664d3 13896 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 13897 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 13898 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13899 else
3e29f34a
MR
13900 {
13901 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13902 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13903 }
96408a79
SA
13904 }
13905 }
13906 else
b98664d3 13907 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13908 "block nor reference, for DIE %s [in module %s]"),
13909 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13910
13911 call_site->per_cu = cu->per_cu;
9f47c707 13912 call_site->per_objfile = per_objfile;
96408a79
SA
13913
13914 for (child_die = die->child;
13915 child_die && child_die->tag;
436c571c 13916 child_die = child_die->sibling)
96408a79 13917 {
96408a79 13918 struct call_site_parameter *parameter;
1788b2d3 13919 struct attribute *loc, *origin;
96408a79 13920
216f72a1 13921 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13922 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13923 {
13924 /* Already printed the complaint above. */
13925 continue;
13926 }
13927
13928 gdb_assert (call_site->parameter_count < nparams);
13929 parameter = &call_site->parameter[call_site->parameter_count];
13930
1788b2d3
JK
13931 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13932 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13933 register is contained in DW_AT_call_value. */
96408a79 13934
24c5c679 13935 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13936 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13937 if (origin == NULL)
13938 {
13939 /* This was a pre-DWARF-5 GNU extension alias
13940 for DW_AT_call_parameter. */
13941 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13942 }
cd6c91b4 13943 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13944 {
1788b2d3 13945 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13946
0826b30a 13947 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13948 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13949 {
13950 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13951 binding can be done only inside one CU. Such referenced DIE
13952 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13953 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13954 "DW_TAG_call_site child DIE %s [in module %s]"),
13955 sect_offset_str (child_die->sect_off),
9c541725 13956 objfile_name (objfile));
d76b7dbc
JK
13957 continue;
13958 }
9c541725
PA
13959 parameter->u.param_cu_off
13960 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13961 }
4fc6c0d5 13962 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13963 {
b98664d3 13964 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13965 "DW_TAG_call_site child DIE %s [in module %s]"),
13966 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13967 continue;
13968 }
24c5c679 13969 else
96408a79 13970 {
9d2246fc
TT
13971 struct dwarf_block *block = loc->as_block ();
13972
24c5c679 13973 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 13974 (block->data, &block->data[block->size]);
24c5c679
JK
13975 if (parameter->u.dwarf_reg != -1)
13976 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
13977 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13978 &block->data[block->size],
24c5c679
JK
13979 &parameter->u.fb_offset))
13980 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13981 else
13982 {
b98664d3 13983 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13984 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13985 "DW_TAG_call_site child DIE %s "
24c5c679 13986 "[in module %s]"),
9d8780f0 13987 sect_offset_str (child_die->sect_off),
9c541725 13988 objfile_name (objfile));
24c5c679
JK
13989 continue;
13990 }
96408a79
SA
13991 }
13992
216f72a1
JK
13993 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13994 if (attr == NULL)
13995 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13996 if (attr == NULL || !attr->form_is_block ())
96408a79 13997 {
b98664d3 13998 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13999 "DW_TAG_call_site child DIE %s [in module %s]"),
14000 sect_offset_str (child_die->sect_off),
9c541725 14001 objfile_name (objfile));
96408a79
SA
14002 continue;
14003 }
9d2246fc
TT
14004
14005 struct dwarf_block *block = attr->as_block ();
14006 parameter->value = block->data;
14007 parameter->value_size = block->size;
96408a79
SA
14008
14009 /* Parameters are not pre-cleared by memset above. */
14010 parameter->data_value = NULL;
14011 parameter->data_value_size = 0;
14012 call_site->parameter_count++;
14013
216f72a1
JK
14014 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14015 if (attr == NULL)
14016 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14017 if (attr != nullptr)
96408a79 14018 {
4fc6c0d5 14019 if (!attr->form_is_block ())
b98664d3 14020 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14021 "DW_TAG_call_site child DIE %s [in module %s]"),
14022 sect_offset_str (child_die->sect_off),
9c541725 14023 objfile_name (objfile));
96408a79
SA
14024 else
14025 {
9d2246fc
TT
14026 block = attr->as_block ();
14027 parameter->data_value = block->data;
14028 parameter->data_value_size = block->size;
96408a79
SA
14029 }
14030 }
14031 }
14032}
14033
71a3c369
TT
14034/* Helper function for read_variable. If DIE represents a virtual
14035 table, then return the type of the concrete object that is
14036 associated with the virtual table. Otherwise, return NULL. */
14037
14038static struct type *
14039rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14040{
14041 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14042 if (attr == NULL)
14043 return NULL;
14044
14045 /* Find the type DIE. */
14046 struct die_info *type_die = NULL;
14047 struct dwarf2_cu *type_cu = cu;
14048
cd6c91b4 14049 if (attr->form_is_ref ())
71a3c369
TT
14050 type_die = follow_die_ref (die, attr, &type_cu);
14051 if (type_die == NULL)
14052 return NULL;
14053
14054 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14055 return NULL;
14056 return die_containing_type (type_die, type_cu);
14057}
14058
14059/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14060
14061static void
14062read_variable (struct die_info *die, struct dwarf2_cu *cu)
14063{
14064 struct rust_vtable_symbol *storage = NULL;
14065
14066 if (cu->language == language_rust)
14067 {
14068 struct type *containing_type = rust_containing_type (die, cu);
14069
14070 if (containing_type != NULL)
14071 {
5e22e966 14072 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14073
8c14c3a3 14074 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14075 storage->concrete_type = containing_type;
cf724bc9 14076 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14077 }
14078 }
14079
e4a62c65
TV
14080 struct symbol *res = new_symbol (die, NULL, cu, storage);
14081 struct attribute *abstract_origin
14082 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14083 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14084 if (res == NULL && loc && abstract_origin)
14085 {
14086 /* We have a variable without a name, but with a location and an abstract
14087 origin. This may be a concrete instance of an abstract variable
14088 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14089 later. */
14090 struct dwarf2_cu *origin_cu = cu;
14091 struct die_info *origin_die
14092 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14094 per_objfile->per_bfd->abstract_to_concrete
14095 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14096 }
71a3c369
TT
14097}
14098
43988095
JK
14099/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14100 reading .debug_rnglists.
14101 Callback's type should be:
14102 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14103 Return true if the attributes are present and valid, otherwise,
14104 return false. */
14105
14106template <typename Callback>
14107static bool
14108dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14109 dwarf_tag tag, Callback &&callback)
43988095 14110{
976ca316
SM
14111 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14112 struct objfile *objfile = per_objfile->objfile;
43988095 14113 bfd *obfd = objfile->obfd;
43988095 14114 /* Base address selection entry. */
2b24b6e4 14115 gdb::optional<CORE_ADDR> base;
43988095 14116 const gdb_byte *buffer;
43988095
JK
14117 CORE_ADDR baseaddr;
14118 bool overflow = false;
d0ce17d8
CT
14119 ULONGEST addr_index;
14120 struct dwarf2_section_info *rnglists_section;
43988095 14121
43988095 14122 base = cu->base_address;
d0ce17d8
CT
14123 rnglists_section = cu_debug_rnglists_section (cu, tag);
14124 rnglists_section->read (objfile);
43988095 14125
d0ce17d8 14126 if (offset >= rnglists_section->size)
43988095 14127 {
b98664d3 14128 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14129 offset);
14130 return false;
14131 }
d0ce17d8 14132 buffer = rnglists_section->buffer + offset;
43988095 14133
b3b3bada 14134 baseaddr = objfile->text_section_offset ();
43988095
JK
14135
14136 while (1)
14137 {
7814882a
JK
14138 /* Initialize it due to a false compiler warning. */
14139 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14140 const gdb_byte *buf_end = (rnglists_section->buffer
14141 + rnglists_section->size);
43988095
JK
14142 unsigned int bytes_read;
14143
14144 if (buffer == buf_end)
14145 {
14146 overflow = true;
14147 break;
14148 }
14149 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14150 switch (rlet)
14151 {
14152 case DW_RLE_end_of_list:
14153 break;
14154 case DW_RLE_base_address:
14155 if (buffer + cu->header.addr_size > buf_end)
14156 {
14157 overflow = true;
14158 break;
14159 }
c8a7a66f 14160 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14161 buffer += bytes_read;
14162 break;
dda83cd7
SM
14163 case DW_RLE_base_addressx:
14164 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14165 buffer += bytes_read;
14166 base = read_addr_index (cu, addr_index);
14167 break;
43988095
JK
14168 case DW_RLE_start_length:
14169 if (buffer + cu->header.addr_size > buf_end)
14170 {
14171 overflow = true;
14172 break;
14173 }
c8a7a66f
TT
14174 range_beginning = cu->header.read_address (obfd, buffer,
14175 &bytes_read);
43988095
JK
14176 buffer += bytes_read;
14177 range_end = (range_beginning
14178 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14179 buffer += bytes_read;
14180 if (buffer > buf_end)
14181 {
14182 overflow = true;
14183 break;
14184 }
14185 break;
d0ce17d8 14186 case DW_RLE_startx_length:
dda83cd7
SM
14187 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14188 buffer += bytes_read;
14189 range_beginning = read_addr_index (cu, addr_index);
14190 if (buffer > buf_end)
14191 {
14192 overflow = true;
14193 break;
14194 }
14195 range_end = (range_beginning
14196 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14197 buffer += bytes_read;
14198 break;
43988095
JK
14199 case DW_RLE_offset_pair:
14200 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14201 buffer += bytes_read;
14202 if (buffer > buf_end)
14203 {
14204 overflow = true;
14205 break;
14206 }
14207 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14208 buffer += bytes_read;
14209 if (buffer > buf_end)
14210 {
14211 overflow = true;
14212 break;
14213 }
14214 break;
14215 case DW_RLE_start_end:
14216 if (buffer + 2 * cu->header.addr_size > buf_end)
14217 {
14218 overflow = true;
14219 break;
14220 }
c8a7a66f
TT
14221 range_beginning = cu->header.read_address (obfd, buffer,
14222 &bytes_read);
43988095 14223 buffer += bytes_read;
c8a7a66f 14224 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14225 buffer += bytes_read;
14226 break;
d0ce17d8 14227 case DW_RLE_startx_endx:
dda83cd7
SM
14228 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14229 buffer += bytes_read;
14230 range_beginning = read_addr_index (cu, addr_index);
14231 if (buffer > buf_end)
14232 {
14233 overflow = true;
14234 break;
14235 }
14236 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14237 buffer += bytes_read;
14238 range_end = read_addr_index (cu, addr_index);
14239 break;
43988095 14240 default:
b98664d3 14241 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14242 return false;
14243 }
14244 if (rlet == DW_RLE_end_of_list || overflow)
14245 break;
14246 if (rlet == DW_RLE_base_address)
14247 continue;
14248
43988095
JK
14249 if (range_beginning > range_end)
14250 {
14251 /* Inverted range entries are invalid. */
b98664d3 14252 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14253 return false;
14254 }
14255
14256 /* Empty range entries have no effect. */
14257 if (range_beginning == range_end)
14258 continue;
14259
d0ce17d8
CT
14260 /* Only DW_RLE_offset_pair needs the base address added. */
14261 if (rlet == DW_RLE_offset_pair)
14262 {
14263 if (!base.has_value ())
14264 {
14265 /* We have no valid base address for the DW_RLE_offset_pair. */
14266 complaint (_("Invalid .debug_rnglists data (no base address for "
14267 "DW_RLE_offset_pair)"));
14268 return false;
14269 }
14270
14271 range_beginning += *base;
14272 range_end += *base;
14273 }
43988095
JK
14274
14275 /* A not-uncommon case of bad debug info.
14276 Don't pollute the addrmap with bad data. */
14277 if (range_beginning + baseaddr == 0
976ca316 14278 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14279 {
b98664d3 14280 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14281 " [in module %s]"), objfile_name (objfile));
14282 continue;
14283 }
14284
14285 callback (range_beginning, range_end);
14286 }
14287
14288 if (overflow)
14289 {
b98664d3 14290 complaint (_("Offset %d is not terminated "
43988095
JK
14291 "for DW_AT_ranges attribute"),
14292 offset);
14293 return false;
14294 }
14295
14296 return true;
14297}
14298
14299/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14300 Callback's type should be:
14301 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14302 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14303
43988095 14304template <typename Callback>
43039443 14305static int
d0ce17d8 14306dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14307 Callback &&callback)
43039443 14308{
5e22e966
SM
14309 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14310 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14311 struct comp_unit_head *cu_header = &cu->header;
14312 bfd *obfd = objfile->obfd;
14313 unsigned int addr_size = cu_header->addr_size;
14314 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14315 /* Base address selection entry. */
2b24b6e4 14316 gdb::optional<CORE_ADDR> base;
43039443 14317 unsigned int dummy;
d521ce57 14318 const gdb_byte *buffer;
ff013f42 14319 CORE_ADDR baseaddr;
43039443 14320
43988095 14321 if (cu_header->version >= 5)
d0ce17d8 14322 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14323
d00adf39 14324 base = cu->base_address;
43039443 14325
5e22e966
SM
14326 per_objfile->per_bfd->ranges.read (objfile);
14327 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14328 {
b98664d3 14329 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14330 offset);
14331 return 0;
14332 }
5e22e966 14333 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14334
b3b3bada 14335 baseaddr = objfile->text_section_offset ();
ff013f42 14336
43039443
JK
14337 while (1)
14338 {
14339 CORE_ADDR range_beginning, range_end;
14340
c8a7a66f 14341 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14342 buffer += addr_size;
c8a7a66f 14343 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14344 buffer += addr_size;
14345 offset += 2 * addr_size;
14346
14347 /* An end of list marker is a pair of zero addresses. */
14348 if (range_beginning == 0 && range_end == 0)
14349 /* Found the end of list entry. */
14350 break;
14351
14352 /* Each base address selection entry is a pair of 2 values.
14353 The first is the largest possible address, the second is
14354 the base address. Check for a base address here. */
14355 if ((range_beginning & mask) == mask)
14356 {
28d2bfb9
AB
14357 /* If we found the largest possible address, then we already
14358 have the base address in range_end. */
14359 base = range_end;
43039443
JK
14360 continue;
14361 }
14362
2b24b6e4 14363 if (!base.has_value ())
43039443
JK
14364 {
14365 /* We have no valid base address for the ranges
14366 data. */
b98664d3 14367 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14368 return 0;
14369 }
14370
9277c30c
UW
14371 if (range_beginning > range_end)
14372 {
14373 /* Inverted range entries are invalid. */
b98664d3 14374 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14375 return 0;
14376 }
14377
14378 /* Empty range entries have no effect. */
14379 if (range_beginning == range_end)
14380 continue;
14381
2b24b6e4
TT
14382 range_beginning += *base;
14383 range_end += *base;
43039443 14384
01093045
DE
14385 /* A not-uncommon case of bad debug info.
14386 Don't pollute the addrmap with bad data. */
14387 if (range_beginning + baseaddr == 0
5e22e966 14388 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14389 {
b98664d3 14390 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14391 " [in module %s]"), objfile_name (objfile));
01093045
DE
14392 continue;
14393 }
14394
5f46c5a5
JK
14395 callback (range_beginning, range_end);
14396 }
14397
14398 return 1;
14399}
14400
14401/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14402 Return 1 if the attributes are present and valid, otherwise, return 0.
14403 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14404
14405static int
14406dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14407 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14408 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14409{
5e22e966 14410 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14411 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14412 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14413 int low_set = 0;
14414 CORE_ADDR low = 0;
14415 CORE_ADDR high = 0;
14416 int retval;
14417
d0ce17d8 14418 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14419 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14420 {
9277c30c 14421 if (ranges_pst != NULL)
3e29f34a
MR
14422 {
14423 CORE_ADDR lowpc;
14424 CORE_ADDR highpc;
14425
79748972
TT
14426 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14427 range_beginning + baseaddr)
14428 - baseaddr);
14429 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14430 range_end + baseaddr)
14431 - baseaddr);
d320c2b5
TT
14432 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14433 lowpc, highpc - 1, ranges_pst);
3e29f34a 14434 }
ff013f42 14435
43039443
JK
14436 /* FIXME: This is recording everything as a low-high
14437 segment of consecutive addresses. We should have a
14438 data structure for discontiguous block ranges
14439 instead. */
14440 if (! low_set)
14441 {
14442 low = range_beginning;
14443 high = range_end;
14444 low_set = 1;
14445 }
14446 else
14447 {
14448 if (range_beginning < low)
14449 low = range_beginning;
14450 if (range_end > high)
14451 high = range_end;
14452 }
5f46c5a5
JK
14453 });
14454 if (!retval)
14455 return 0;
43039443
JK
14456
14457 if (! low_set)
14458 /* If the first entry is an end-of-list marker, the range
14459 describes an empty scope, i.e. no instructions. */
14460 return 0;
14461
14462 if (low_return)
14463 *low_return = low;
14464 if (high_return)
14465 *high_return = high;
14466 return 1;
14467}
14468
3a2b436a
JK
14469/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14470 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14471 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14472
3a2b436a 14473static enum pc_bounds_kind
af34e669 14474dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14475 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14476 dwarf2_psymtab *pst)
c906108c 14477{
976ca316 14478 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14479 struct attribute *attr;
91da1414 14480 struct attribute *attr_high;
af34e669
DJ
14481 CORE_ADDR low = 0;
14482 CORE_ADDR high = 0;
e385593e 14483 enum pc_bounds_kind ret;
c906108c 14484
91da1414
MW
14485 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14486 if (attr_high)
af34e669 14487 {
e142c38c 14488 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14489 if (attr != nullptr)
dda83cd7 14490 {
95f982e5
TT
14491 low = attr->as_address ();
14492 high = attr_high->as_address ();
cd6c91b4 14493 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14494 high += low;
91da1414 14495 }
af34e669
DJ
14496 else
14497 /* Found high w/o low attribute. */
e385593e 14498 return PC_BOUNDS_INVALID;
af34e669
DJ
14499
14500 /* Found consecutive range of addresses. */
3a2b436a 14501 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14502 }
c906108c 14503 else
af34e669 14504 {
e142c38c 14505 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14506 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14507 {
2b0c7f41
SM
14508 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14509 on DWARF version). */
14510 ULONGEST ranges_offset = attr->as_unsigned ();
14511
14512 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14513 this value. */
14514 if (die->tag != DW_TAG_compile_unit)
14515 ranges_offset += cu->gnu_ranges_base;
2e3cf129 14516
af34e669 14517 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14518 .debug_ranges section. */
d0ce17d8
CT
14519 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14520 die->tag))
e385593e 14521 return PC_BOUNDS_INVALID;
43039443 14522 /* Found discontinuous range of addresses. */
3a2b436a 14523 ret = PC_BOUNDS_RANGES;
af34e669 14524 }
e385593e
JK
14525 else
14526 return PC_BOUNDS_NOT_PRESENT;
af34e669 14527 }
c906108c 14528
48fbe735 14529 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14530 if (high <= low)
e385593e 14531 return PC_BOUNDS_INVALID;
c906108c
SS
14532
14533 /* When using the GNU linker, .gnu.linkonce. sections are used to
14534 eliminate duplicate copies of functions and vtables and such.
14535 The linker will arbitrarily choose one and discard the others.
14536 The AT_*_pc values for such functions refer to local labels in
14537 these sections. If the section from that file was discarded, the
14538 labels are not in the output, so the relocs get a value of 0.
14539 If this is a discarded function, mark the pc bounds as invalid,
14540 so that GDB will ignore it. */
976ca316 14541 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14542 return PC_BOUNDS_INVALID;
c906108c
SS
14543
14544 *lowpc = low;
96408a79
SA
14545 if (highpc)
14546 *highpc = high;
af34e669 14547 return ret;
c906108c
SS
14548}
14549
b084d499
JB
14550/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14551 its low and high PC addresses. Do nothing if these addresses could not
14552 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14553 and HIGHPC to the high address if greater than HIGHPC. */
14554
14555static void
14556dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14557 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14558 struct dwarf2_cu *cu)
b084d499
JB
14559{
14560 CORE_ADDR low, high;
14561 struct die_info *child = die->child;
14562
e385593e 14563 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14564 {
325fac50
PA
14565 *lowpc = std::min (*lowpc, low);
14566 *highpc = std::max (*highpc, high);
b084d499
JB
14567 }
14568
14569 /* If the language does not allow nested subprograms (either inside
14570 subprograms or lexical blocks), we're done. */
14571 if (cu->language != language_ada)
14572 return;
6e70227d 14573
b084d499
JB
14574 /* Check all the children of the given DIE. If it contains nested
14575 subprograms, then check their pc bounds. Likewise, we need to
14576 check lexical blocks as well, as they may also contain subprogram
14577 definitions. */
14578 while (child && child->tag)
14579 {
14580 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14581 || child->tag == DW_TAG_lexical_block)
14582 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14583 child = child->sibling;
b084d499
JB
14584 }
14585}
14586
fae299cd
DC
14587/* Get the low and high pc's represented by the scope DIE, and store
14588 them in *LOWPC and *HIGHPC. If the correct values can't be
14589 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14590
14591static void
14592get_scope_pc_bounds (struct die_info *die,
14593 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14594 struct dwarf2_cu *cu)
14595{
14596 CORE_ADDR best_low = (CORE_ADDR) -1;
14597 CORE_ADDR best_high = (CORE_ADDR) 0;
14598 CORE_ADDR current_low, current_high;
14599
3a2b436a 14600 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14601 >= PC_BOUNDS_RANGES)
fae299cd
DC
14602 {
14603 best_low = current_low;
14604 best_high = current_high;
14605 }
14606 else
14607 {
14608 struct die_info *child = die->child;
14609
14610 while (child && child->tag)
14611 {
14612 switch (child->tag) {
14613 case DW_TAG_subprogram:
dda83cd7 14614 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14615 break;
14616 case DW_TAG_namespace:
f55ee35c 14617 case DW_TAG_module:
fae299cd
DC
14618 /* FIXME: carlton/2004-01-16: Should we do this for
14619 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14620 that current GCC's always emit the DIEs corresponding
14621 to definitions of methods of classes as children of a
14622 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14623 the DIEs giving the declarations, which could be
14624 anywhere). But I don't see any reason why the
14625 standards says that they have to be there. */
14626 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14627
14628 if (current_low != ((CORE_ADDR) -1))
14629 {
325fac50
PA
14630 best_low = std::min (best_low, current_low);
14631 best_high = std::max (best_high, current_high);
fae299cd
DC
14632 }
14633 break;
14634 default:
0963b4bd 14635 /* Ignore. */
fae299cd
DC
14636 break;
14637 }
14638
436c571c 14639 child = child->sibling;
fae299cd
DC
14640 }
14641 }
14642
14643 *lowpc = best_low;
14644 *highpc = best_high;
14645}
14646
801e3a5b
JB
14647/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14648 in DIE. */
380bca97 14649
801e3a5b
JB
14650static void
14651dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14652 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14653{
5e22e966 14654 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14655 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14656 struct attribute *attr;
91da1414 14657 struct attribute *attr_high;
801e3a5b 14658
91da1414
MW
14659 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14660 if (attr_high)
801e3a5b 14661 {
801e3a5b 14662 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14663 if (attr != nullptr)
dda83cd7 14664 {
95f982e5
TT
14665 CORE_ADDR low = attr->as_address ();
14666 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14667
cd6c91b4 14668 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14669 high += low;
9a619af0 14670
3e29f34a
MR
14671 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14672 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14673 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14674 }
801e3a5b
JB
14675 }
14676
14677 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14678 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14679 {
2b0c7f41
SM
14680 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14681 on DWARF version). */
14682 ULONGEST ranges_offset = attr->as_unsigned ();
801e3a5b 14683
2b0c7f41
SM
14684 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14685 this value. */
14686 if (die->tag != DW_TAG_compile_unit)
14687 ranges_offset += cu->gnu_ranges_base;
801e3a5b 14688
2d5f09ec 14689 std::vector<blockrange> blockvec;
2b0c7f41 14690 dwarf2_ranges_process (ranges_offset, cu, die->tag,
5f46c5a5
JK
14691 [&] (CORE_ADDR start, CORE_ADDR end)
14692 {
58fdfd2c
JK
14693 start += baseaddr;
14694 end += baseaddr;
5f46c5a5
JK
14695 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14696 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14697 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14698 blockvec.emplace_back (start, end);
5f46c5a5 14699 });
2d5f09ec
KB
14700
14701 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14702 }
14703}
14704
685b1105
JK
14705/* Check whether the producer field indicates either of GCC < 4.6, or the
14706 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14707
685b1105
JK
14708static void
14709check_producer (struct dwarf2_cu *cu)
60d5a603 14710{
38360086 14711 int major, minor;
60d5a603
JK
14712
14713 if (cu->producer == NULL)
14714 {
14715 /* For unknown compilers expect their behavior is DWARF version
14716 compliant.
14717
14718 GCC started to support .debug_types sections by -gdwarf-4 since
14719 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14720 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14721 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14722 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14723 }
b1ffba5a 14724 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14725 {
38360086
MW
14726 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14727 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14728 }
5230b05a 14729 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14730 {
14731 cu->producer_is_icc = true;
14732 cu->producer_is_icc_lt_14 = major < 14;
14733 }
c258c396
JD
14734 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14735 cu->producer_is_codewarrior = true;
685b1105
JK
14736 else
14737 {
14738 /* For other non-GCC compilers, expect their behavior is DWARF version
14739 compliant. */
60d5a603
JK
14740 }
14741
9068261f 14742 cu->checked_producer = true;
685b1105 14743}
ba919b58 14744
685b1105
JK
14745/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14746 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14747 during 4.6.0 experimental. */
14748
9068261f 14749static bool
685b1105
JK
14750producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14751{
14752 if (!cu->checked_producer)
14753 check_producer (cu);
14754
14755 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14756}
14757
c258c396
JD
14758
14759/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14760 with incorrect is_stmt attributes. */
14761
14762static bool
14763producer_is_codewarrior (struct dwarf2_cu *cu)
14764{
14765 if (!cu->checked_producer)
14766 check_producer (cu);
14767
14768 return cu->producer_is_codewarrior;
14769}
14770
bf23a268
TT
14771/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14772 If that attribute is not available, return the appropriate
14773 default. */
60d5a603
JK
14774
14775static enum dwarf_access_attribute
bf23a268 14776dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14777{
bf23a268
TT
14778 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14779 if (attr != nullptr)
14780 {
14781 LONGEST value = attr->constant_value (-1);
14782 if (value == DW_ACCESS_public
14783 || value == DW_ACCESS_protected
14784 || value == DW_ACCESS_private)
14785 return (dwarf_access_attribute) value;
14786 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14787 plongest (value));
14788 }
14789
60d5a603
JK
14790 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14791 {
14792 /* The default DWARF 2 accessibility for members is public, the default
14793 accessibility for inheritance is private. */
14794
14795 if (die->tag != DW_TAG_inheritance)
14796 return DW_ACCESS_public;
14797 else
14798 return DW_ACCESS_private;
14799 }
14800 else
14801 {
14802 /* DWARF 3+ defines the default accessibility a different way. The same
14803 rules apply now for DW_TAG_inheritance as for the members and it only
14804 depends on the container kind. */
14805
14806 if (die->parent->tag == DW_TAG_class_type)
14807 return DW_ACCESS_private;
14808 else
14809 return DW_ACCESS_public;
14810 }
14811}
14812
74ac6d43
TT
14813/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14814 offset. If the attribute was not found return 0, otherwise return
14815 1. If it was found but could not properly be handled, set *OFFSET
14816 to 0. */
14817
14818static int
14819handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14820 LONGEST *offset)
14821{
14822 struct attribute *attr;
14823
14824 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14825 if (attr != NULL)
14826 {
14827 *offset = 0;
14828
14829 /* Note that we do not check for a section offset first here.
14830 This is because DW_AT_data_member_location is new in DWARF 4,
14831 so if we see it, we can assume that a constant form is really
14832 a constant and not a section offset. */
cd6c91b4 14833 if (attr->form_is_constant ())
0826b30a 14834 *offset = attr->constant_value (0);
cd6c91b4 14835 else if (attr->form_is_section_offset ())
74ac6d43 14836 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14837 else if (attr->form_is_block ())
9d2246fc 14838 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
14839 else
14840 dwarf2_complex_location_expr_complaint ();
14841
14842 return 1;
14843 }
14844
14845 return 0;
14846}
14847
7d79de9a
TT
14848/* Look for DW_AT_data_member_location and store the results in FIELD. */
14849
14850static void
14851handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14852 struct field *field)
14853{
14854 struct attribute *attr;
14855
14856 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14857 if (attr != NULL)
14858 {
14859 if (attr->form_is_constant ())
14860 {
14861 LONGEST offset = attr->constant_value (0);
14862 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14863 }
14864 else if (attr->form_is_section_offset ())
14865 dwarf2_complex_location_expr_complaint ();
14866 else if (attr->form_is_block ())
14867 {
14868 bool handled;
9d2246fc 14869 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
14870 if (handled)
14871 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14872 else
14873 {
5e22e966
SM
14874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14875 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14876 struct dwarf2_locexpr_baton *dlbaton
14877 = XOBNEW (&objfile->objfile_obstack,
14878 struct dwarf2_locexpr_baton);
9d2246fc
TT
14879 dlbaton->data = attr->as_block ()->data;
14880 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
14881 /* When using this baton, we want to compute the address
14882 of the field, not the value. This is why
14883 is_reference is set to false here. */
14884 dlbaton->is_reference = false;
5e22e966 14885 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14886 dlbaton->per_cu = cu->per_cu;
14887
14888 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14889 }
14890 }
14891 else
14892 dwarf2_complex_location_expr_complaint ();
14893 }
14894}
14895
c906108c
SS
14896/* Add an aggregate field to the field list. */
14897
14898static void
107d2387 14899dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14900 struct dwarf2_cu *cu)
6e70227d 14901{
5e22e966 14902 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14903 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14904 struct nextfield *new_field;
14905 struct attribute *attr;
14906 struct field *fp;
15d034d0 14907 const char *fieldname = "";
c906108c 14908
7d0ccb61
DJ
14909 if (die->tag == DW_TAG_inheritance)
14910 {
be2daae6
TT
14911 fip->baseclasses.emplace_back ();
14912 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14913 }
14914 else
14915 {
be2daae6
TT
14916 fip->fields.emplace_back ();
14917 new_field = &fip->fields.back ();
7d0ccb61 14918 }
be2daae6 14919
9c6a1327
TT
14920 new_field->offset = die->sect_off;
14921
bf23a268 14922 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 14923 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 14924 fip->non_public_fields = true;
60d5a603 14925
e142c38c 14926 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14927 if (attr != nullptr)
23dca5c3 14928 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
14929 else
14930 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14931
14932 fp = &new_field->field;
a9a9bd0f 14933
e142c38c 14934 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14935 {
a9a9bd0f 14936 /* Data member other than a C++ static data member. */
6e70227d 14937
c906108c 14938 /* Get type of field. */
5d14b6e5 14939 fp->set_type (die_type (die, cu));
c906108c 14940
d6a843b5 14941 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14942
c906108c 14943 /* Get bit size of field (zero if none). */
e142c38c 14944 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14945 if (attr != nullptr)
c906108c 14946 {
529908cb 14947 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
14948 }
14949 else
14950 {
14951 FIELD_BITSIZE (*fp) = 0;
14952 }
14953
14954 /* Get bit offset of field. */
7d79de9a 14955 handle_data_member_location (die, cu, fp);
e142c38c 14956 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 14957 if (attr != nullptr && attr->form_is_constant ())
c906108c 14958 {
d5a22e77 14959 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14960 {
14961 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
14962 additional bit offset from the MSB of the containing
14963 anonymous object to the MSB of the field. We don't
14964 have to do anything special since we don't need to
14965 know the size of the anonymous object. */
529908cb 14966 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 14967 + attr->constant_value (0)));
c906108c
SS
14968 }
14969 else
14970 {
14971 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
14972 MSB of the anonymous object, subtract off the number of
14973 bits from the MSB of the field to the MSB of the
14974 object, and then subtract off the number of bits of
14975 the field itself. The result is the bit offset of
14976 the LSB of the field. */
c906108c 14977 int anonymous_size;
7c184d33 14978 int bit_offset = attr->constant_value (0);
c906108c 14979
e142c38c 14980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 14981 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
14982 {
14983 /* The size of the anonymous object containing
14984 the bit field is explicit, so use the
14985 indicated size (in bytes). */
7c184d33 14986 anonymous_size = attr->constant_value (0);
c906108c
SS
14987 }
14988 else
14989 {
14990 /* The size of the anonymous object containing
14991 the bit field must be inferred from the type
14992 attribute of the data member containing the
14993 bit field. */
5d14b6e5 14994 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 14995 }
f41f5e61
PA
14996 SET_FIELD_BITPOS (*fp,
14997 (FIELD_BITPOS (*fp)
14998 + anonymous_size * bits_per_byte
14999 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15000 }
15001 }
da5b30da
AA
15002 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15003 if (attr != NULL)
15004 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15005 + attr->constant_value (0)));
c906108c
SS
15006
15007 /* Get name of field. */
39cbfefa
DJ
15008 fieldname = dwarf2_name (die, cu);
15009 if (fieldname == NULL)
15010 fieldname = "";
d8151005
DJ
15011
15012 /* The name is already allocated along with this objfile, so we don't
15013 need to duplicate it for the type. */
15014 fp->name = fieldname;
c906108c
SS
15015
15016 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15017 pointer or virtual base class pointer) to private. */
e142c38c 15018 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15019 {
d48cc9dd 15020 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15021 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15022 fip->non_public_fields = true;
c906108c
SS
15023 }
15024 }
a9a9bd0f 15025 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15026 {
a9a9bd0f
DC
15027 /* C++ static member. */
15028
15029 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15030 is a declaration, but all versions of G++ as of this writing
15031 (so through at least 3.2.1) incorrectly generate
15032 DW_TAG_variable tags. */
6e70227d 15033
ff355380 15034 const char *physname;
c906108c 15035
a9a9bd0f 15036 /* Get name of field. */
39cbfefa
DJ
15037 fieldname = dwarf2_name (die, cu);
15038 if (fieldname == NULL)
c906108c
SS
15039 return;
15040
254e6b9e 15041 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15042 if (attr
15043 /* Only create a symbol if this is an external value.
15044 new_symbol checks this and puts the value in the global symbol
15045 table, which we want. If it is not external, new_symbol
15046 will try to put the value in cu->list_in_scope which is wrong. */
15047 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15048 {
15049 /* A static const member, not much different than an enum as far as
15050 we're concerned, except that we can support more types. */
15051 new_symbol (die, NULL, cu);
15052 }
15053
2df3850c 15054 /* Get physical name. */
ff355380 15055 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15056
d8151005
DJ
15057 /* The name is already allocated along with this objfile, so we don't
15058 need to duplicate it for the type. */
15059 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15060 fp->set_type (die_type (die, cu));
d8151005 15061 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15062 }
15063 else if (die->tag == DW_TAG_inheritance)
15064 {
74ac6d43 15065 /* C++ base class field. */
7d79de9a 15066 handle_data_member_location (die, cu, fp);
c906108c 15067 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15068 fp->set_type (die_type (die, cu));
15069 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15070 }
2ddeaf8a
TT
15071 else
15072 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15073}
15074
883fd55a
KS
15075/* Can the type given by DIE define another type? */
15076
15077static bool
15078type_can_define_types (const struct die_info *die)
15079{
15080 switch (die->tag)
15081 {
15082 case DW_TAG_typedef:
15083 case DW_TAG_class_type:
15084 case DW_TAG_structure_type:
15085 case DW_TAG_union_type:
15086 case DW_TAG_enumeration_type:
15087 return true;
15088
15089 default:
15090 return false;
15091 }
15092}
15093
15094/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15095
15096static void
883fd55a
KS
15097dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15098 struct dwarf2_cu *cu)
6e70227d 15099{
be2daae6
TT
15100 struct decl_field fp;
15101 memset (&fp, 0, sizeof (fp));
98751a41 15102
883fd55a 15103 gdb_assert (type_can_define_types (die));
98751a41 15104
883fd55a 15105 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15106 fp.name = dwarf2_name (die, cu);
15107 fp.type = read_type_die (die, cu);
98751a41 15108
c191a687 15109 /* Save accessibility. */
bf23a268 15110 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15111 switch (accessibility)
15112 {
15113 case DW_ACCESS_public:
15114 /* The assumed value if neither private nor protected. */
15115 break;
15116 case DW_ACCESS_private:
be2daae6 15117 fp.is_private = 1;
c191a687
KS
15118 break;
15119 case DW_ACCESS_protected:
be2daae6 15120 fp.is_protected = 1;
c191a687 15121 break;
c191a687
KS
15122 }
15123
883fd55a 15124 if (die->tag == DW_TAG_typedef)
be2daae6 15125 fip->typedef_field_list.push_back (fp);
883fd55a 15126 else
be2daae6 15127 fip->nested_types_list.push_back (fp);
98751a41
JK
15128}
15129
9c6a1327
TT
15130/* A convenience typedef that's used when finding the discriminant
15131 field for a variant part. */
1b95cdb7
SM
15132typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15133 offset_map_type;
9c6a1327
TT
15134
15135/* Compute the discriminant range for a given variant. OBSTACK is
15136 where the results will be stored. VARIANT is the variant to
15137 process. IS_UNSIGNED indicates whether the discriminant is signed
15138 or unsigned. */
15139
15140static const gdb::array_view<discriminant_range>
15141convert_variant_range (struct obstack *obstack, const variant_field &variant,
15142 bool is_unsigned)
15143{
15144 std::vector<discriminant_range> ranges;
15145
15146 if (variant.default_branch)
15147 return {};
15148
15149 if (variant.discr_list_data == nullptr)
15150 {
15151 discriminant_range r
15152 = {variant.discriminant_value, variant.discriminant_value};
15153 ranges.push_back (r);
15154 }
15155 else
15156 {
15157 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15158 variant.discr_list_data->size);
15159 while (!data.empty ())
15160 {
15161 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15162 {
15163 complaint (_("invalid discriminant marker: %d"), data[0]);
15164 break;
15165 }
15166 bool is_range = data[0] == DW_DSC_range;
15167 data = data.slice (1);
15168
15169 ULONGEST low, high;
15170 unsigned int bytes_read;
15171
15172 if (data.empty ())
15173 {
15174 complaint (_("DW_AT_discr_list missing low value"));
15175 break;
15176 }
15177 if (is_unsigned)
15178 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15179 else
15180 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15181 &bytes_read);
15182 data = data.slice (bytes_read);
15183
15184 if (is_range)
15185 {
15186 if (data.empty ())
15187 {
15188 complaint (_("DW_AT_discr_list missing high value"));
15189 break;
15190 }
15191 if (is_unsigned)
15192 high = read_unsigned_leb128 (nullptr, data.data (),
15193 &bytes_read);
15194 else
15195 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15196 &bytes_read);
15197 data = data.slice (bytes_read);
15198 }
15199 else
15200 high = low;
15201
15202 ranges.push_back ({ low, high });
15203 }
15204 }
15205
15206 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15207 ranges.size ());
15208 std::copy (ranges.begin (), ranges.end (), result);
15209 return gdb::array_view<discriminant_range> (result, ranges.size ());
15210}
15211
15212static const gdb::array_view<variant_part> create_variant_parts
15213 (struct obstack *obstack,
15214 const offset_map_type &offset_map,
15215 struct field_info *fi,
15216 const std::vector<variant_part_builder> &variant_parts);
15217
15218/* Fill in a "struct variant" for a given variant field. RESULT is
15219 the variant to fill in. OBSTACK is where any needed allocations
15220 will be done. OFFSET_MAP holds the mapping from section offsets to
15221 fields for the type. FI describes the fields of the type we're
15222 processing. FIELD is the variant field we're converting. */
15223
15224static void
15225create_one_variant (variant &result, struct obstack *obstack,
15226 const offset_map_type &offset_map,
15227 struct field_info *fi, const variant_field &field)
15228{
15229 result.discriminants = convert_variant_range (obstack, field, false);
15230 result.first_field = field.first_field + fi->baseclasses.size ();
15231 result.last_field = field.last_field + fi->baseclasses.size ();
15232 result.parts = create_variant_parts (obstack, offset_map, fi,
15233 field.variant_parts);
15234}
15235
15236/* Fill in a "struct variant_part" for a given variant part. RESULT
15237 is the variant part to fill in. OBSTACK is where any needed
15238 allocations will be done. OFFSET_MAP holds the mapping from
15239 section offsets to fields for the type. FI describes the fields of
15240 the type we're processing. BUILDER is the variant part to be
15241 converted. */
15242
15243static void
15244create_one_variant_part (variant_part &result,
15245 struct obstack *obstack,
15246 const offset_map_type &offset_map,
15247 struct field_info *fi,
15248 const variant_part_builder &builder)
15249{
15250 auto iter = offset_map.find (builder.discriminant_offset);
15251 if (iter == offset_map.end ())
15252 {
15253 result.discriminant_index = -1;
15254 /* Doesn't matter. */
15255 result.is_unsigned = false;
15256 }
15257 else
15258 {
15259 result.discriminant_index = iter->second;
15260 result.is_unsigned
c6d940a9 15261 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15262 }
15263
15264 size_t n = builder.variants.size ();
15265 variant *output = new (obstack) variant[n];
15266 for (size_t i = 0; i < n; ++i)
15267 create_one_variant (output[i], obstack, offset_map, fi,
15268 builder.variants[i]);
15269
15270 result.variants = gdb::array_view<variant> (output, n);
15271}
15272
15273/* Create a vector of variant parts that can be attached to a type.
15274 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15275 holds the mapping from section offsets to fields for the type. FI
15276 describes the fields of the type we're processing. VARIANT_PARTS
15277 is the vector to convert. */
15278
15279static const gdb::array_view<variant_part>
15280create_variant_parts (struct obstack *obstack,
15281 const offset_map_type &offset_map,
15282 struct field_info *fi,
15283 const std::vector<variant_part_builder> &variant_parts)
15284{
15285 if (variant_parts.empty ())
15286 return {};
15287
15288 size_t n = variant_parts.size ();
15289 variant_part *result = new (obstack) variant_part[n];
15290 for (size_t i = 0; i < n; ++i)
15291 create_one_variant_part (result[i], obstack, offset_map, fi,
15292 variant_parts[i]);
15293
15294 return gdb::array_view<variant_part> (result, n);
15295}
15296
15297/* Compute the variant part vector for FIP, attaching it to TYPE when
15298 done. */
15299
15300static void
15301add_variant_property (struct field_info *fip, struct type *type,
15302 struct dwarf2_cu *cu)
15303{
15304 /* Map section offsets of fields to their field index. Note the
15305 field index here does not take the number of baseclasses into
15306 account. */
15307 offset_map_type offset_map;
15308 for (int i = 0; i < fip->fields.size (); ++i)
15309 offset_map[fip->fields[i].offset] = i;
15310
5e22e966 15311 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15312 gdb::array_view<variant_part> parts
15313 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15314 fip->variant_parts);
15315
15316 struct dynamic_prop prop;
8c2e4e06
SM
15317 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15318 obstack_copy (&objfile->objfile_obstack, &parts,
15319 sizeof (parts)));
9c6a1327 15320
5c54719c 15321 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15322}
15323
c906108c
SS
15324/* Create the vector of fields, and attach it to the type. */
15325
15326static void
fba45db2 15327dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15328 struct dwarf2_cu *cu)
c906108c 15329{
317f7127 15330 int nfields = fip->nfields ();
c906108c
SS
15331
15332 /* Record the field count, allocate space for the array of fields,
15333 and create blank accessibility bitfields if necessary. */
5e33d5f4 15334 type->set_num_fields (nfields);
3cabb6b0
SM
15335 type->set_fields
15336 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15337
b4ba55a1 15338 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15339 {
15340 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15341
15342 TYPE_FIELD_PRIVATE_BITS (type) =
15343 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15344 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15345
15346 TYPE_FIELD_PROTECTED_BITS (type) =
15347 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15348 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15349
774b6a14
TT
15350 TYPE_FIELD_IGNORE_BITS (type) =
15351 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15352 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15353 }
15354
15355 /* If the type has baseclasses, allocate and clear a bit vector for
15356 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15357 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15358 {
be2daae6 15359 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15360 unsigned char *pointer;
c906108c
SS
15361
15362 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15363 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15364 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15365 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15366 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15367 }
15368
9c6a1327
TT
15369 if (!fip->variant_parts.empty ())
15370 add_variant_property (fip, type, cu);
2ddeaf8a 15371
be2daae6
TT
15372 /* Copy the saved-up fields into the field vector. */
15373 for (int i = 0; i < nfields; ++i)
c906108c 15374 {
be2daae6
TT
15375 struct nextfield &field
15376 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15377 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15378
ceacbf6e 15379 type->field (i) = field.field;
be2daae6 15380 switch (field.accessibility)
c906108c 15381 {
c5aa993b 15382 case DW_ACCESS_private:
b4ba55a1 15383 if (cu->language != language_ada)
be2daae6 15384 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15385 break;
c906108c 15386
c5aa993b 15387 case DW_ACCESS_protected:
b4ba55a1 15388 if (cu->language != language_ada)
be2daae6 15389 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15390 break;
c906108c 15391
c5aa993b
JM
15392 case DW_ACCESS_public:
15393 break;
c906108c 15394
c5aa993b
JM
15395 default:
15396 /* Unknown accessibility. Complain and treat it as public. */
15397 {
b98664d3 15398 complaint (_("unsupported accessibility %d"),
be2daae6 15399 field.accessibility);
c5aa993b
JM
15400 }
15401 break;
c906108c 15402 }
be2daae6 15403 if (i < fip->baseclasses.size ())
c906108c 15404 {
be2daae6 15405 switch (field.virtuality)
c906108c 15406 {
c5aa993b
JM
15407 case DW_VIRTUALITY_virtual:
15408 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15409 if (cu->language == language_ada)
a73c6dcd 15410 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15411 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15412 break;
c906108c
SS
15413 }
15414 }
c906108c
SS
15415 }
15416}
15417
7d27a96d
TT
15418/* Return true if this member function is a constructor, false
15419 otherwise. */
15420
15421static int
15422dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15423{
15424 const char *fieldname;
fe978cb0 15425 const char *type_name;
7d27a96d
TT
15426 int len;
15427
15428 if (die->parent == NULL)
15429 return 0;
15430
15431 if (die->parent->tag != DW_TAG_structure_type
15432 && die->parent->tag != DW_TAG_union_type
15433 && die->parent->tag != DW_TAG_class_type)
15434 return 0;
15435
15436 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15437 type_name = dwarf2_name (die->parent, cu);
15438 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15439 return 0;
15440
15441 len = strlen (fieldname);
fe978cb0
PA
15442 return (strncmp (fieldname, type_name, len) == 0
15443 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15444}
15445
c906108c
SS
15446/* Add a member function to the proper fieldlist. */
15447
15448static void
107d2387 15449dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15450 struct type *type, struct dwarf2_cu *cu)
c906108c 15451{
5e22e966 15452 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15453 struct attribute *attr;
c906108c 15454 int i;
be2daae6 15455 struct fnfieldlist *flp = nullptr;
c906108c 15456 struct fn_field *fnp;
15d034d0 15457 const char *fieldname;
f792889a 15458 struct type *this_type;
c906108c 15459
b4ba55a1 15460 if (cu->language == language_ada)
a73c6dcd 15461 error (_("unexpected member function in Ada type"));
b4ba55a1 15462
2df3850c 15463 /* Get name of member function. */
39cbfefa
DJ
15464 fieldname = dwarf2_name (die, cu);
15465 if (fieldname == NULL)
2df3850c 15466 return;
c906108c 15467
c906108c 15468 /* Look up member function name in fieldlist. */
be2daae6 15469 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15470 {
27bfe10e 15471 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15472 {
15473 flp = &fip->fnfieldlists[i];
15474 break;
15475 }
c906108c
SS
15476 }
15477
be2daae6
TT
15478 /* Create a new fnfieldlist if necessary. */
15479 if (flp == nullptr)
c906108c 15480 {
be2daae6
TT
15481 fip->fnfieldlists.emplace_back ();
15482 flp = &fip->fnfieldlists.back ();
c906108c 15483 flp->name = fieldname;
be2daae6 15484 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15485 }
15486
be2daae6
TT
15487 /* Create a new member function field and add it to the vector of
15488 fnfieldlists. */
15489 flp->fnfields.emplace_back ();
15490 fnp = &flp->fnfields.back ();
3da10d80
KS
15491
15492 /* Delay processing of the physname until later. */
9c37b5ae 15493 if (cu->language == language_cplus)
be2daae6
TT
15494 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15495 die, cu);
3da10d80
KS
15496 else
15497 {
1d06ead6 15498 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15499 fnp->physname = physname ? physname : "";
15500 }
15501
c906108c 15502 fnp->type = alloc_type (objfile);
f792889a 15503 this_type = read_type_die (die, cu);
78134374 15504 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15505 {
1f704f76 15506 int nparams = this_type->num_fields ();
c906108c 15507
f792889a 15508 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15509 of the method itself (TYPE_CODE_METHOD). */
15510 smash_to_method_type (fnp->type, type,
f792889a 15511 TYPE_TARGET_TYPE (this_type),
80fc5e77 15512 this_type->fields (),
1f704f76 15513 this_type->num_fields (),
a409645d 15514 this_type->has_varargs ());
c906108c
SS
15515
15516 /* Handle static member functions.
dda83cd7
SM
15517 Dwarf2 has no clean way to discern C++ static and non-static
15518 member functions. G++ helps GDB by marking the first
15519 parameter for non-static member functions (which is the this
15520 pointer) as artificial. We obtain this information from
15521 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15522 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15523 fnp->voffset = VOFFSET_STATIC;
15524 }
15525 else
b98664d3 15526 complaint (_("member function type missing for '%s'"),
3da10d80 15527 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15528
15529 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15530 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15531 fnp->fcontext = die_containing_type (die, cu);
c906108c 15532
3e43a32a
MS
15533 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15534 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15535
15536 /* Get accessibility. */
bf23a268 15537 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15538 switch (accessibility)
c906108c 15539 {
60d5a603
JK
15540 case DW_ACCESS_private:
15541 fnp->is_private = 1;
15542 break;
15543 case DW_ACCESS_protected:
15544 fnp->is_protected = 1;
15545 break;
c906108c
SS
15546 }
15547
b02dede2 15548 /* Check for artificial methods. */
e142c38c 15549 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15550 if (attr && attr->as_boolean ())
b02dede2
DJ
15551 fnp->is_artificial = 1;
15552
e35000a7
TBA
15553 /* Check for defaulted methods. */
15554 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15555 if (attr != nullptr)
15556 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15557
15558 /* Check for deleted methods. */
15559 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15560 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15561 fnp->is_deleted = 1;
15562
7d27a96d
TT
15563 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15564
0d564a31 15565 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15566 function. For older versions of GCC, this is an offset in the
15567 appropriate virtual table, as specified by DW_AT_containing_type.
15568 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15569 to the object address. */
15570
e142c38c 15571 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15572 if (attr != nullptr)
8e19ed76 15573 {
9d2246fc 15574 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15575 {
9d2246fc
TT
15576 struct dwarf_block *block = attr->as_block ();
15577
15578 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15579 {
15580 /* Old-style GCC. */
9d2246fc 15581 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15582 }
9d2246fc
TT
15583 else if (block->data[0] == DW_OP_deref
15584 || (block->size > 1
15585 && block->data[0] == DW_OP_deref_size
15586 && block->data[1] == cu->header.addr_size))
aec5aa8b 15587 {
9d2246fc 15588 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15589 if ((fnp->voffset % cu->header.addr_size) != 0)
15590 dwarf2_complex_location_expr_complaint ();
15591 else
15592 fnp->voffset /= cu->header.addr_size;
15593 fnp->voffset += 2;
15594 }
15595 else
15596 dwarf2_complex_location_expr_complaint ();
15597
15598 if (!fnp->fcontext)
7e993ebf
KS
15599 {
15600 /* If there is no `this' field and no DW_AT_containing_type,
15601 we cannot actually find a base class context for the
15602 vtable! */
1f704f76 15603 if (this_type->num_fields () == 0
7e993ebf
KS
15604 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15605 {
b98664d3 15606 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15607 "function \"%s\" (offset %s)"),
15608 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15609 }
15610 else
15611 {
15612 fnp->fcontext
940da03e 15613 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15614 }
15615 }
aec5aa8b 15616 }
cd6c91b4 15617 else if (attr->form_is_section_offset ())
dda83cd7 15618 {
4d3c2250 15619 dwarf2_complex_location_expr_complaint ();
dda83cd7 15620 }
8e19ed76 15621 else
dda83cd7 15622 {
4d3c2250
KB
15623 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15624 fieldname);
dda83cd7 15625 }
0d564a31 15626 }
d48cc9dd
DJ
15627 else
15628 {
15629 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15630 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15631 {
15632 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15633 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15634 "but the vtable offset is not specified"),
9d8780f0 15635 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15636 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15637 TYPE_CPLUS_DYNAMIC (type) = 1;
15638 }
15639 }
c906108c
SS
15640}
15641
15642/* Create the vector of member function fields, and attach it to the type. */
15643
15644static void
fba45db2 15645dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15646 struct dwarf2_cu *cu)
c906108c 15647{
b4ba55a1 15648 if (cu->language == language_ada)
a73c6dcd 15649 error (_("unexpected member functions in Ada type"));
b4ba55a1 15650
c906108c
SS
15651 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15652 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15653 TYPE_ALLOC (type,
15654 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15655
be2daae6 15656 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15657 {
be2daae6 15658 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15659 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15660
be2daae6
TT
15661 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15662 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15663 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15664 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15665
15666 for (int k = 0; k < nf.fnfields.size (); ++k)
15667 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15668 }
15669
be2daae6 15670 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15671}
15672
1168df01
JB
15673/* Returns non-zero if NAME is the name of a vtable member in CU's
15674 language, zero otherwise. */
15675static int
15676is_vtable_name (const char *name, struct dwarf2_cu *cu)
15677{
15678 static const char vptr[] = "_vptr";
15679
9c37b5ae
TT
15680 /* Look for the C++ form of the vtable. */
15681 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15682 return 1;
15683
15684 return 0;
15685}
15686
c0dd20ea 15687/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15688 functions, with the ABI-specified layout. If TYPE describes
15689 such a structure, smash it into a member function type.
61049d3b
DJ
15690
15691 GCC shouldn't do this; it should just output pointer to member DIEs.
15692 This is GCC PR debug/28767. */
c0dd20ea 15693
0b92b5bb
TT
15694static void
15695quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15696{
09e2d7c7 15697 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15698
15699 /* Check for a structure with no name and two children. */
1f704f76 15700 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15701 return;
c0dd20ea
DJ
15702
15703 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15704 if (TYPE_FIELD_NAME (type, 0) == NULL
15705 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15706 || TYPE_FIELD_NAME (type, 1) == NULL
15707 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15708 return;
c0dd20ea
DJ
15709
15710 /* Find the type of the method. */
940da03e 15711 pfn_type = type->field (0).type ();
c0dd20ea 15712 if (pfn_type == NULL
78134374
SM
15713 || pfn_type->code () != TYPE_CODE_PTR
15714 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15715 return;
c0dd20ea
DJ
15716
15717 /* Look for the "this" argument. */
15718 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15719 if (pfn_type->num_fields () == 0
940da03e
SM
15720 /* || pfn_type->field (0).type () == NULL */
15721 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15722 return;
c0dd20ea 15723
940da03e 15724 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15725 new_type = alloc_type (objfile);
09e2d7c7 15726 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15727 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15728 pfn_type->has_varargs ());
0b92b5bb 15729 smash_to_methodptr_type (type, new_type);
c0dd20ea 15730}
1168df01 15731
e26624c6
TT
15732/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15733 requires rewriting, then copy it and return the updated copy.
15734 Otherwise return nullptr. */
15735
15736static struct type *
15737rewrite_array_type (struct type *type)
15738{
15739 if (type->code () != TYPE_CODE_ARRAY)
15740 return nullptr;
15741
15742 struct type *index_type = type->index_type ();
15743 range_bounds *current_bounds = index_type->bounds ();
15744
15745 /* Handle multi-dimensional arrays. */
15746 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15747 if (new_target == nullptr)
15748 {
15749 /* Maybe we don't need to rewrite this array. */
15750 if (current_bounds->low.kind () == PROP_CONST
15751 && current_bounds->high.kind () == PROP_CONST)
15752 return nullptr;
15753 }
15754
15755 /* Either the target type was rewritten, or the bounds have to be
15756 updated. Either way we want to copy the type and update
15757 everything. */
15758 struct type *copy = copy_type (type);
15759 int nfields = copy->num_fields ();
15760 field *new_fields
15761 = ((struct field *) TYPE_ZALLOC (copy,
15762 nfields * sizeof (struct field)));
15763 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15764 copy->set_fields (new_fields);
15765 if (new_target != nullptr)
15766 TYPE_TARGET_TYPE (copy) = new_target;
15767
15768 struct type *index_copy = copy_type (index_type);
15769 range_bounds *bounds
15770 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15771 sizeof (range_bounds));
15772 *bounds = *current_bounds;
15773 bounds->low.set_const_val (1);
15774 bounds->high.set_const_val (0);
15775 index_copy->set_bounds (bounds);
15776 copy->set_index_type (index_copy);
15777
15778 return copy;
15779}
15780
57567375
TT
15781/* While some versions of GCC will generate complicated DWARF for an
15782 array (see quirk_ada_thick_pointer), more recent versions were
15783 modified to emit an explicit thick pointer structure. However, in
15784 this case, the array still has DWARF expressions for its ranges,
15785 and these must be ignored. */
15786
15787static void
15788quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15789 struct type *type)
15790{
15791 gdb_assert (cu->language == language_ada);
15792
15793 /* Check for a structure with two children. */
15794 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15795 return;
15796
15797 /* Check for P_ARRAY and P_BOUNDS members. */
15798 if (TYPE_FIELD_NAME (type, 0) == NULL
15799 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15800 || TYPE_FIELD_NAME (type, 1) == NULL
15801 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15802 return;
15803
15804 /* Make sure we're looking at a pointer to an array. */
15805 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15806 return;
57567375 15807
e26624c6
TT
15808 /* The Ada code already knows how to handle these types, so all that
15809 we need to do is turn the bounds into static bounds. However, we
15810 don't want to rewrite existing array or index types in-place,
15811 because those may be referenced in other contexts where this
15812 rewriting is undesirable. */
15813 struct type *new_ary_type
15814 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15815 if (new_ary_type != nullptr)
15816 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15817}
15818
2b4424c3
TT
15819/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15820 appropriate error checking and issuing complaints if there is a
15821 problem. */
15822
15823static ULONGEST
15824get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15825{
15826 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15827
15828 if (attr == nullptr)
15829 return 0;
15830
cd6c91b4 15831 if (!attr->form_is_constant ())
2b4424c3 15832 {
b98664d3 15833 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15834 " - DIE at %s [in module %s]"),
15835 sect_offset_str (die->sect_off),
5e22e966 15836 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15837 return 0;
15838 }
15839
529908cb
TT
15840 LONGEST val = attr->constant_value (0);
15841 if (val < 0)
2b4424c3 15842 {
529908cb
TT
15843 complaint (_("DW_AT_alignment value must not be negative"
15844 " - DIE at %s [in module %s]"),
15845 sect_offset_str (die->sect_off),
15846 objfile_name (cu->per_objfile->objfile));
15847 return 0;
2b4424c3 15848 }
529908cb 15849 ULONGEST align = val;
2b4424c3
TT
15850
15851 if (align == 0)
15852 {
b98664d3 15853 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15854 " - DIE at %s [in module %s]"),
15855 sect_offset_str (die->sect_off),
5e22e966 15856 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15857 return 0;
15858 }
15859 if ((align & (align - 1)) != 0)
15860 {
b98664d3 15861 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15862 " - DIE at %s [in module %s]"),
15863 sect_offset_str (die->sect_off),
5e22e966 15864 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15865 return 0;
15866 }
15867
15868 return align;
15869}
15870
15871/* If the DIE has a DW_AT_alignment attribute, use its value to set
15872 the alignment for TYPE. */
15873
15874static void
15875maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15876 struct type *type)
15877{
15878 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15879 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15880 " - DIE at %s [in module %s]"),
15881 sect_offset_str (die->sect_off),
5e22e966 15882 objfile_name (cu->per_objfile->objfile));
2b4424c3 15883}
685b1105 15884
e35000a7
TBA
15885/* Check if the given VALUE is a valid enum dwarf_calling_convention
15886 constant for a type, according to DWARF5 spec, Table 5.5. */
15887
15888static bool
15889is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15890{
15891 switch (value)
15892 {
15893 case DW_CC_normal:
15894 case DW_CC_pass_by_reference:
15895 case DW_CC_pass_by_value:
15896 return true;
15897
15898 default:
15899 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15900 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15901 return false;
15902 }
15903}
15904
d0922fcf
TBA
15905/* Check if the given VALUE is a valid enum dwarf_calling_convention
15906 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15907 also according to GNU-specific values (see include/dwarf2.h). */
15908
15909static bool
15910is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15911{
15912 switch (value)
15913 {
15914 case DW_CC_normal:
15915 case DW_CC_program:
15916 case DW_CC_nocall:
15917 return true;
15918
15919 case DW_CC_GNU_renesas_sh:
15920 case DW_CC_GNU_borland_fastcall_i386:
15921 case DW_CC_GDB_IBM_OpenCL:
15922 return true;
15923
15924 default:
15925 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15926 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15927 return false;
15928 }
15929}
15930
c906108c 15931/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15932 (definition) to create a type for the structure or union. Fill in
15933 the type's name and general properties; the members will not be
83655187
DE
15934 processed until process_structure_scope. A symbol table entry for
15935 the type will also not be done until process_structure_scope (assuming
15936 the type has a name).
c906108c 15937
c767944b
DJ
15938 NOTE: we need to call these functions regardless of whether or not the
15939 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15940 structure or union. This gets the type entered into our set of
83655187 15941 user defined types. */
c906108c 15942
f792889a 15943static struct type *
134d01f1 15944read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15945{
5e22e966 15946 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15947 struct type *type;
15948 struct attribute *attr;
15d034d0 15949 const char *name;
c906108c 15950
348e048f
DE
15951 /* If the definition of this type lives in .debug_types, read that type.
15952 Don't follow DW_AT_specification though, that will take us back up
15953 the chain and we want to go down. */
052c8bb8 15954 attr = die->attr (DW_AT_signature);
435d3d88 15955 if (attr != nullptr)
348e048f 15956 {
ac9ec31b 15957 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15958
ac9ec31b 15959 /* The type's CU may not be the same as CU.
02142a6c 15960 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15961 return set_die_type (die, type, cu);
15962 }
15963
c0dd20ea 15964 type = alloc_type (objfile);
c906108c 15965 INIT_CPLUS_SPECIFIC (type);
93311388 15966
39cbfefa
DJ
15967 name = dwarf2_name (die, cu);
15968 if (name != NULL)
c906108c 15969 {
987504bb 15970 if (cu->language == language_cplus
c44af4eb
TT
15971 || cu->language == language_d
15972 || cu->language == language_rust)
63d06c5c 15973 {
15d034d0 15974 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15975
15976 /* dwarf2_full_name might have already finished building the DIE's
15977 type. If so, there is no need to continue. */
15978 if (get_die_type (die, cu) != NULL)
15979 return get_die_type (die, cu);
15980
d0e39ea2 15981 type->set_name (full_name);
63d06c5c
DC
15982 }
15983 else
15984 {
d8151005
DJ
15985 /* The name is already allocated along with this objfile, so
15986 we don't need to duplicate it for the type. */
d0e39ea2 15987 type->set_name (name);
63d06c5c 15988 }
c906108c
SS
15989 }
15990
15991 if (die->tag == DW_TAG_structure_type)
15992 {
67607e24 15993 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15994 }
15995 else if (die->tag == DW_TAG_union_type)
15996 {
67607e24 15997 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15998 }
15999 else
16000 {
67607e24 16001 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16002 }
16003
0cc2414c
TT
16004 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16005 TYPE_DECLARED_CLASS (type) = 1;
16006
e35000a7
TBA
16007 /* Store the calling convention in the type if it's available in
16008 the die. Otherwise the calling convention remains set to
16009 the default value DW_CC_normal. */
16010 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16011 if (attr != nullptr
529908cb 16012 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16013 {
16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16015 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16016 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16017 }
16018
e142c38c 16019 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16020 if (attr != nullptr)
c906108c 16021 {
cd6c91b4 16022 if (attr->form_is_constant ())
dda83cd7 16023 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16024 else
16025 {
f8e89861 16026 struct dynamic_prop prop;
293e7e51 16027 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16028 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16029 TYPE_LENGTH (type) = 0;
155bfbd3 16030 }
c906108c
SS
16031 }
16032 else
16033 {
16034 TYPE_LENGTH (type) = 0;
16035 }
16036
2b4424c3
TT
16037 maybe_set_alignment (cu, die, type);
16038
5230b05a 16039 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16040 {
5230b05a
WT
16041 /* ICC<14 does not output the required DW_AT_declaration on
16042 incomplete types, but gives them a size of zero. */
b4b73759 16043 type->set_is_stub (true);
685b1105
JK
16044 }
16045 else
9baccff6 16046 type->set_stub_is_supported (true);
685b1105 16047
dc718098 16048 if (die_is_declaration (die, cu))
b4b73759 16049 type->set_is_stub (true);
a6c727b2
DJ
16050 else if (attr == NULL && die->child == NULL
16051 && producer_is_realview (cu->producer))
16052 /* RealView does not output the required DW_AT_declaration
16053 on incomplete types. */
b4b73759 16054 type->set_is_stub (true);
dc718098 16055
c906108c
SS
16056 /* We need to add the type field to the die immediately so we don't
16057 infinitely recurse when dealing with pointers to the structure
0963b4bd 16058 type within the structure itself. */
1c379e20 16059 set_die_type (die, type, cu);
c906108c 16060
7e314c57
JK
16061 /* set_die_type should be already done. */
16062 set_descriptive_type (type, die, cu);
16063
c767944b
DJ
16064 return type;
16065}
16066
9c6a1327
TT
16067static void handle_struct_member_die
16068 (struct die_info *child_die,
16069 struct type *type,
16070 struct field_info *fi,
16071 std::vector<struct symbol *> *template_args,
16072 struct dwarf2_cu *cu);
16073
16074/* A helper for handle_struct_member_die that handles
16075 DW_TAG_variant_part. */
16076
16077static void
16078handle_variant_part (struct die_info *die, struct type *type,
16079 struct field_info *fi,
16080 std::vector<struct symbol *> *template_args,
16081 struct dwarf2_cu *cu)
16082{
16083 variant_part_builder *new_part;
16084 if (fi->current_variant_part == nullptr)
16085 {
16086 fi->variant_parts.emplace_back ();
16087 new_part = &fi->variant_parts.back ();
16088 }
16089 else if (!fi->current_variant_part->processing_variant)
16090 {
16091 complaint (_("nested DW_TAG_variant_part seen "
16092 "- DIE at %s [in module %s]"),
16093 sect_offset_str (die->sect_off),
5e22e966 16094 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16095 return;
16096 }
16097 else
16098 {
16099 variant_field &current = fi->current_variant_part->variants.back ();
16100 current.variant_parts.emplace_back ();
16101 new_part = &current.variant_parts.back ();
16102 }
16103
16104 /* When we recurse, we want callees to add to this new variant
16105 part. */
16106 scoped_restore save_current_variant_part
16107 = make_scoped_restore (&fi->current_variant_part, new_part);
16108
16109 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16110 if (discr == NULL)
16111 {
16112 /* It's a univariant form, an extension we support. */
16113 }
16114 else if (discr->form_is_ref ())
16115 {
16116 struct dwarf2_cu *target_cu = cu;
16117 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16118
16119 new_part->discriminant_offset = target_die->sect_off;
16120 }
16121 else
16122 {
16123 complaint (_("DW_AT_discr does not have DIE reference form"
16124 " - DIE at %s [in module %s]"),
16125 sect_offset_str (die->sect_off),
5e22e966 16126 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16127 }
16128
16129 for (die_info *child_die = die->child;
16130 child_die != NULL;
16131 child_die = child_die->sibling)
16132 handle_struct_member_die (child_die, type, fi, template_args, cu);
16133}
16134
16135/* A helper for handle_struct_member_die that handles
16136 DW_TAG_variant. */
16137
16138static void
16139handle_variant (struct die_info *die, struct type *type,
16140 struct field_info *fi,
16141 std::vector<struct symbol *> *template_args,
16142 struct dwarf2_cu *cu)
16143{
16144 if (fi->current_variant_part == nullptr)
16145 {
16146 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16147 "- DIE at %s [in module %s]"),
16148 sect_offset_str (die->sect_off),
5e22e966 16149 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16150 return;
16151 }
16152 if (fi->current_variant_part->processing_variant)
16153 {
16154 complaint (_("nested DW_TAG_variant seen "
16155 "- DIE at %s [in module %s]"),
16156 sect_offset_str (die->sect_off),
5e22e966 16157 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16158 return;
16159 }
16160
16161 scoped_restore save_processing_variant
16162 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16163 true);
16164
16165 fi->current_variant_part->variants.emplace_back ();
16166 variant_field &variant = fi->current_variant_part->variants.back ();
16167 variant.first_field = fi->fields.size ();
16168
16169 /* In a variant we want to get the discriminant and also add a
16170 field for our sole member child. */
16171 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16172 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16173 {
16174 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16175 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16176 variant.default_branch = true;
16177 else
9d2246fc 16178 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16179 }
16180 else
cae21f8e 16181 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16182
16183 for (die_info *variant_child = die->child;
16184 variant_child != NULL;
16185 variant_child = variant_child->sibling)
16186 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16187
16188 variant.last_field = fi->fields.size ();
16189}
16190
2ddeaf8a
TT
16191/* A helper for process_structure_scope that handles a single member
16192 DIE. */
16193
16194static void
16195handle_struct_member_die (struct die_info *child_die, struct type *type,
16196 struct field_info *fi,
16197 std::vector<struct symbol *> *template_args,
16198 struct dwarf2_cu *cu)
16199{
16200 if (child_die->tag == DW_TAG_member
9c6a1327 16201 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16202 {
16203 /* NOTE: carlton/2002-11-05: A C++ static data member
16204 should be a DW_TAG_member that is a declaration, but
16205 all versions of G++ as of this writing (so through at
16206 least 3.2.1) incorrectly generate DW_TAG_variable
16207 tags for them instead. */
16208 dwarf2_add_field (fi, child_die, cu);
16209 }
16210 else if (child_die->tag == DW_TAG_subprogram)
16211 {
16212 /* Rust doesn't have member functions in the C++ sense.
16213 However, it does emit ordinary functions as children
16214 of a struct DIE. */
16215 if (cu->language == language_rust)
16216 read_func_scope (child_die, cu);
16217 else
16218 {
16219 /* C++ member function. */
16220 dwarf2_add_member_fn (fi, child_die, type, cu);
16221 }
16222 }
16223 else if (child_die->tag == DW_TAG_inheritance)
16224 {
16225 /* C++ base class field. */
16226 dwarf2_add_field (fi, child_die, cu);
16227 }
16228 else if (type_can_define_types (child_die))
16229 dwarf2_add_type_defn (fi, child_die, cu);
16230 else if (child_die->tag == DW_TAG_template_type_param
16231 || child_die->tag == DW_TAG_template_value_param)
16232 {
16233 struct symbol *arg = new_symbol (child_die, NULL, cu);
16234
16235 if (arg != NULL)
16236 template_args->push_back (arg);
16237 }
9c6a1327
TT
16238 else if (child_die->tag == DW_TAG_variant_part)
16239 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16240 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16241 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16242}
16243
c767944b
DJ
16244/* Finish creating a structure or union type, including filling in
16245 its members and creating a symbol for it. */
16246
16247static void
16248process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16249{
5e22e966 16250 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16251 struct die_info *child_die;
c767944b
DJ
16252 struct type *type;
16253
16254 type = get_die_type (die, cu);
16255 if (type == NULL)
16256 type = read_structure_type (die, cu);
16257
3e1d3d8c 16258 bool has_template_parameters = false;
e142c38c 16259 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16260 {
16261 struct field_info fi;
2f4732b0 16262 std::vector<struct symbol *> template_args;
c906108c 16263
639d11d3 16264 child_die = die->child;
c906108c
SS
16265
16266 while (child_die && child_die->tag)
16267 {
2ddeaf8a 16268 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16269 child_die = child_die->sibling;
c906108c
SS
16270 }
16271
34eaf542 16272 /* Attach template arguments to type. */
2f4732b0 16273 if (!template_args.empty ())
34eaf542 16274 {
3e1d3d8c 16275 has_template_parameters = true;
34eaf542 16276 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16277 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16278 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16279 = XOBNEWVEC (&objfile->objfile_obstack,
16280 struct symbol *,
16281 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16282 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16283 template_args.data (),
34eaf542
TT
16284 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16285 * sizeof (struct symbol *)));
34eaf542
TT
16286 }
16287
c906108c 16288 /* Attach fields and member functions to the type. */
317f7127 16289 if (fi.nfields () > 0)
e7c27a73 16290 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16291 if (!fi.fnfieldlists.empty ())
c906108c 16292 {
e7c27a73 16293 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16294
c5aa993b 16295 /* Get the type which refers to the base class (possibly this
c906108c 16296 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16297 class from the DW_AT_containing_type attribute. This use of
16298 DW_AT_containing_type is a GNU extension. */
c906108c 16299
e142c38c 16300 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16301 {
e7c27a73 16302 struct type *t = die_containing_type (die, cu);
c906108c 16303
ae6ae975 16304 set_type_vptr_basetype (type, t);
c906108c
SS
16305 if (type == t)
16306 {
c906108c
SS
16307 int i;
16308
16309 /* Our own class provides vtbl ptr. */
1f704f76 16310 for (i = t->num_fields () - 1;
c906108c
SS
16311 i >= TYPE_N_BASECLASSES (t);
16312 --i)
16313 {
0d5cff50 16314 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16315
dda83cd7 16316 if (is_vtable_name (fieldname, cu))
c906108c 16317 {
ae6ae975 16318 set_type_vptr_fieldno (type, i);
c906108c
SS
16319 break;
16320 }
16321 }
16322
16323 /* Complain if virtual function table field not found. */
16324 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16325 complaint (_("virtual function table pointer "
3e43a32a 16326 "not found when defining class '%s'"),
7d93a1e0 16327 type->name () ? type->name () : "");
c906108c
SS
16328 }
16329 else
16330 {
ae6ae975 16331 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16332 }
16333 }
f6235d4c 16334 else if (cu->producer
61012eef 16335 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16336 {
16337 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16338 of the containing type, but the vtable pointer is
16339 always named __vfp. */
f6235d4c
EZ
16340
16341 int i;
16342
1f704f76 16343 for (i = type->num_fields () - 1;
f6235d4c
EZ
16344 i >= TYPE_N_BASECLASSES (type);
16345 --i)
16346 {
16347 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16348 {
ae6ae975
DE
16349 set_type_vptr_fieldno (type, i);
16350 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16351 break;
16352 }
16353 }
16354 }
c906108c 16355 }
98751a41
JK
16356
16357 /* Copy fi.typedef_field_list linked list elements content into the
16358 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16359 if (!fi.typedef_field_list.empty ())
98751a41 16360 {
be2daae6 16361 int count = fi.typedef_field_list.size ();
98751a41 16362
a0d7a4ff 16363 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16364 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16365 = ((struct decl_field *)
be2daae6
TT
16366 TYPE_ALLOC (type,
16367 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16368 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16369
be2daae6
TT
16370 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16371 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16372 }
c767944b 16373
883fd55a
KS
16374 /* Copy fi.nested_types_list linked list elements content into the
16375 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16376 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16377 {
be2daae6 16378 int count = fi.nested_types_list.size ();
883fd55a
KS
16379
16380 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16381 TYPE_NESTED_TYPES_ARRAY (type)
16382 = ((struct decl_field *)
be2daae6
TT
16383 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16384 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16385
be2daae6
TT
16386 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16387 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16388 }
c906108c 16389 }
63d06c5c 16390
bb5ed363 16391 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16392 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16393 cu->rust_unions.push_back (type);
57567375
TT
16394 else if (cu->language == language_ada)
16395 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16396
90aeadfc
DC
16397 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16398 snapshots) has been known to create a die giving a declaration
16399 for a class that has, as a child, a die giving a definition for a
16400 nested class. So we have to process our children even if the
16401 current die is a declaration. Normally, of course, a declaration
16402 won't have any children at all. */
134d01f1 16403
ca040673
DE
16404 child_die = die->child;
16405
90aeadfc
DC
16406 while (child_die != NULL && child_die->tag)
16407 {
16408 if (child_die->tag == DW_TAG_member
16409 || child_die->tag == DW_TAG_variable
34eaf542
TT
16410 || child_die->tag == DW_TAG_inheritance
16411 || child_die->tag == DW_TAG_template_value_param
16412 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16413 {
90aeadfc 16414 /* Do nothing. */
134d01f1 16415 }
90aeadfc
DC
16416 else
16417 process_die (child_die, cu);
134d01f1 16418
436c571c 16419 child_die = child_die->sibling;
134d01f1
DJ
16420 }
16421
fa4028e9
JB
16422 /* Do not consider external references. According to the DWARF standard,
16423 these DIEs are identified by the fact that they have no byte_size
16424 attribute, and a declaration attribute. */
16425 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16426 || !die_is_declaration (die, cu)
16427 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16428 {
16429 struct symbol *sym = new_symbol (die, type, cu);
16430
16431 if (has_template_parameters)
16432 {
a776957c
TT
16433 struct symtab *symtab;
16434 if (sym != nullptr)
16435 symtab = symbol_symtab (sym);
16436 else if (cu->line_header != nullptr)
16437 {
16438 /* Any related symtab will do. */
16439 symtab
7ba99d21 16440 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16441 }
16442 else
16443 {
16444 symtab = nullptr;
16445 complaint (_("could not find suitable "
16446 "symtab for template parameter"
16447 " - DIE at %s [in module %s]"),
16448 sect_offset_str (die->sect_off),
16449 objfile_name (objfile));
16450 }
16451
16452 if (symtab != nullptr)
16453 {
16454 /* Make sure that the symtab is set on the new symbols.
16455 Even though they don't appear in this symtab directly,
16456 other parts of gdb assume that symbols do, and this is
16457 reasonably true. */
16458 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16459 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16460 }
3e1d3d8c
TT
16461 }
16462 }
134d01f1
DJ
16463}
16464
ed6acedd
TT
16465/* Assuming DIE is an enumeration type, and TYPE is its associated
16466 type, update TYPE using some information only available in DIE's
16467 children. In particular, the fields are computed. */
55426c9d
JB
16468
16469static void
16470update_enumeration_type_from_children (struct die_info *die,
16471 struct type *type,
16472 struct dwarf2_cu *cu)
16473{
60f7655a 16474 struct die_info *child_die;
55426c9d
JB
16475 int unsigned_enum = 1;
16476 int flag_enum = 1;
55426c9d 16477
8268c778 16478 auto_obstack obstack;
ed6acedd 16479 std::vector<struct field> fields;
55426c9d 16480
60f7655a
DE
16481 for (child_die = die->child;
16482 child_die != NULL && child_die->tag;
436c571c 16483 child_die = child_die->sibling)
55426c9d
JB
16484 {
16485 struct attribute *attr;
16486 LONGEST value;
16487 const gdb_byte *bytes;
16488 struct dwarf2_locexpr_baton *baton;
16489 const char *name;
60f7655a 16490
55426c9d
JB
16491 if (child_die->tag != DW_TAG_enumerator)
16492 continue;
16493
16494 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16495 if (attr == NULL)
16496 continue;
16497
16498 name = dwarf2_name (child_die, cu);
16499 if (name == NULL)
16500 name = "<anonymous enumerator>";
16501
16502 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16503 &value, &bytes, &baton);
16504 if (value < 0)
16505 {
16506 unsigned_enum = 0;
16507 flag_enum = 0;
16508 }
55426c9d 16509 else
edd45eb0
SM
16510 {
16511 if (count_one_bits_ll (value) >= 2)
16512 flag_enum = 0;
edd45eb0 16513 }
55426c9d 16514
ed6acedd
TT
16515 fields.emplace_back ();
16516 struct field &field = fields.back ();
16517 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16518 SET_FIELD_ENUMVAL (field, value);
16519 }
16520
16521 if (!fields.empty ())
16522 {
5e33d5f4 16523 type->set_num_fields (fields.size ());
3cabb6b0
SM
16524 type->set_fields
16525 ((struct field *)
16526 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16527 memcpy (type->fields (), fields.data (),
ed6acedd 16528 sizeof (struct field) * fields.size ());
55426c9d
JB
16529 }
16530
16531 if (unsigned_enum)
653223d3
SM
16532 type->set_is_unsigned (true);
16533
55426c9d
JB
16534 if (flag_enum)
16535 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16536}
16537
134d01f1
DJ
16538/* Given a DW_AT_enumeration_type die, set its type. We do not
16539 complete the type's fields yet, or create any symbols. */
c906108c 16540
f792889a 16541static struct type *
134d01f1 16542read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16543{
5e22e966 16544 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16545 struct type *type;
c906108c 16546 struct attribute *attr;
0114d602 16547 const char *name;
134d01f1 16548
348e048f
DE
16549 /* If the definition of this type lives in .debug_types, read that type.
16550 Don't follow DW_AT_specification though, that will take us back up
16551 the chain and we want to go down. */
052c8bb8 16552 attr = die->attr (DW_AT_signature);
435d3d88 16553 if (attr != nullptr)
348e048f 16554 {
ac9ec31b 16555 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16556
ac9ec31b 16557 /* The type's CU may not be the same as CU.
02142a6c 16558 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16559 return set_die_type (die, type, cu);
16560 }
16561
c906108c
SS
16562 type = alloc_type (objfile);
16563
67607e24 16564 type->set_code (TYPE_CODE_ENUM);
94af9270 16565 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16566 if (name != NULL)
d0e39ea2 16567 type->set_name (name);
c906108c 16568
0626fc76
TT
16569 attr = dwarf2_attr (die, DW_AT_type, cu);
16570 if (attr != NULL)
16571 {
16572 struct type *underlying_type = die_type (die, cu);
16573
16574 TYPE_TARGET_TYPE (type) = underlying_type;
16575 }
16576
e142c38c 16577 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16578 if (attr != nullptr)
c906108c 16579 {
529908cb 16580 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16581 }
16582 else
16583 {
16584 TYPE_LENGTH (type) = 0;
16585 }
16586
2b4424c3
TT
16587 maybe_set_alignment (cu, die, type);
16588
137033e9
JB
16589 /* The enumeration DIE can be incomplete. In Ada, any type can be
16590 declared as private in the package spec, and then defined only
16591 inside the package body. Such types are known as Taft Amendment
16592 Types. When another package uses such a type, an incomplete DIE
16593 may be generated by the compiler. */
02eb380e 16594 if (die_is_declaration (die, cu))
b4b73759 16595 type->set_is_stub (true);
02eb380e 16596
0626fc76
TT
16597 /* If this type has an underlying type that is not a stub, then we
16598 may use its attributes. We always use the "unsigned" attribute
16599 in this situation, because ordinarily we guess whether the type
16600 is unsigned -- but the guess can be wrong and the underlying type
16601 can tell us the reality. However, we defer to a local size
16602 attribute if one exists, because this lets the compiler override
16603 the underlying type if needed. */
e46d3488 16604 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16605 {
9e7c9a03
HD
16606 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16607 underlying_type = check_typedef (underlying_type);
653223d3
SM
16608
16609 type->set_is_unsigned (underlying_type->is_unsigned ());
16610
0626fc76 16611 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16612 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16613
2b4424c3 16614 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16615 && TYPE_RAW_ALIGN (underlying_type) != 0)
16616 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16617 }
16618
3d567982
TT
16619 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16620
ed6acedd
TT
16621 set_die_type (die, type, cu);
16622
16623 /* Finish the creation of this type by using the enum's children.
16624 Note that, as usual, this must come after set_die_type to avoid
16625 infinite recursion when trying to compute the names of the
16626 enumerators. */
16627 update_enumeration_type_from_children (die, type, cu);
16628
16629 return type;
134d01f1
DJ
16630}
16631
16632/* Given a pointer to a die which begins an enumeration, process all
16633 the dies that define the members of the enumeration, and create the
16634 symbol for the enumeration type.
16635
16636 NOTE: We reverse the order of the element list. */
16637
16638static void
16639process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16640{
f792889a 16641 struct type *this_type;
134d01f1 16642
f792889a
DJ
16643 this_type = get_die_type (die, cu);
16644 if (this_type == NULL)
16645 this_type = read_enumeration_type (die, cu);
9dc481d3 16646
639d11d3 16647 if (die->child != NULL)
c906108c 16648 {
9dc481d3 16649 struct die_info *child_die;
15d034d0 16650 const char *name;
9dc481d3 16651
639d11d3 16652 child_die = die->child;
c906108c
SS
16653 while (child_die && child_die->tag)
16654 {
16655 if (child_die->tag != DW_TAG_enumerator)
16656 {
e7c27a73 16657 process_die (child_die, cu);
c906108c
SS
16658 }
16659 else
16660 {
39cbfefa
DJ
16661 name = dwarf2_name (child_die, cu);
16662 if (name)
ed6acedd 16663 new_symbol (child_die, this_type, cu);
c906108c
SS
16664 }
16665
436c571c 16666 child_die = child_die->sibling;
c906108c 16667 }
c906108c 16668 }
134d01f1 16669
6c83ed52
TT
16670 /* If we are reading an enum from a .debug_types unit, and the enum
16671 is a declaration, and the enum is not the signatured type in the
16672 unit, then we do not want to add a symbol for it. Adding a
16673 symbol would in some cases obscure the true definition of the
16674 enum, giving users an incomplete type when the definition is
16675 actually available. Note that we do not want to do this for all
16676 enums which are just declarations, because C++0x allows forward
16677 enum declarations. */
3019eac3 16678 if (cu->per_cu->is_debug_types
6c83ed52
TT
16679 && die_is_declaration (die, cu))
16680 {
52dc124a 16681 struct signatured_type *sig_type;
6c83ed52 16682
c0f78cd4 16683 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16684 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16685 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16686 return;
16687 }
16688
f792889a 16689 new_symbol (die, this_type, cu);
c906108c
SS
16690}
16691
57567375
TT
16692/* Helper function for quirk_ada_thick_pointer that examines a bounds
16693 expression for an index type and finds the corresponding field
16694 offset in the hidden "P_BOUNDS" structure. Returns true on success
16695 and updates *FIELD, false if it fails to recognize an
16696 expression. */
16697
16698static bool
16699recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16700 int *bounds_offset, struct field *field,
16701 struct dwarf2_cu *cu)
16702{
16703 struct attribute *attr = dwarf2_attr (die, name, cu);
16704 if (attr == nullptr || !attr->form_is_block ())
16705 return false;
16706
16707 const struct dwarf_block *block = attr->as_block ();
16708 const gdb_byte *start = block->data;
16709 const gdb_byte *end = block->data + block->size;
16710
16711 /* The expression to recognize generally looks like:
16712
16713 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16714 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16715
16716 However, the second "plus_uconst" may be missing:
16717
16718 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16719 DW_OP_deref_size: 4)
16720
16721 This happens when the field is at the start of the structure.
16722
16723 Also, the final deref may not be sized:
16724
16725 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16726 DW_OP_deref)
16727
16728 This happens when the size of the index type happens to be the
16729 same as the architecture's word size. This can occur with or
16730 without the second plus_uconst. */
16731
16732 if (end - start < 2)
16733 return false;
16734 if (*start++ != DW_OP_push_object_address)
16735 return false;
16736 if (*start++ != DW_OP_plus_uconst)
16737 return false;
16738
16739 uint64_t this_bound_off;
16740 start = gdb_read_uleb128 (start, end, &this_bound_off);
16741 if (start == nullptr || (int) this_bound_off != this_bound_off)
16742 return false;
16743 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16744 is consistent among all bounds. */
16745 if (*bounds_offset == -1)
16746 *bounds_offset = this_bound_off;
16747 else if (*bounds_offset != this_bound_off)
16748 return false;
16749
16750 if (start == end || *start++ != DW_OP_deref)
16751 return false;
16752
16753 int offset = 0;
16754 if (start ==end)
16755 return false;
16756 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16757 {
16758 /* This means an offset of 0. */
16759 }
16760 else if (*start++ != DW_OP_plus_uconst)
16761 return false;
16762 else
16763 {
16764 /* The size is the parameter to DW_OP_plus_uconst. */
16765 uint64_t val;
16766 start = gdb_read_uleb128 (start, end, &val);
16767 if (start == nullptr)
16768 return false;
16769 if ((int) val != val)
16770 return false;
16771 offset = val;
16772 }
16773
16774 if (start == end)
16775 return false;
16776
16777 uint64_t size;
16778 if (*start == DW_OP_deref_size)
16779 {
16780 start = gdb_read_uleb128 (start + 1, end, &size);
16781 if (start == nullptr)
16782 return false;
16783 }
16784 else if (*start == DW_OP_deref)
16785 {
16786 size = cu->header.addr_size;
16787 ++start;
16788 }
16789 else
16790 return false;
16791
16792 SET_FIELD_BITPOS (*field, 8 * offset);
16793 if (size != TYPE_LENGTH (field->type ()))
16794 FIELD_BITSIZE (*field) = 8 * size;
16795
16796 return true;
16797}
16798
16799/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16800 some kinds of Ada arrays:
16801
16802 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16803 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16804 <11e0> DW_AT_data_location: 2 byte block: 97 6
16805 (DW_OP_push_object_address; DW_OP_deref)
16806 <11e3> DW_AT_type : <0x1173>
16807 <11e7> DW_AT_sibling : <0x1201>
16808 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16809 <11ec> DW_AT_type : <0x1206>
16810 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16811 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16812 DW_OP_deref_size: 4)
16813 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16814 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16815 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16816
16817 This actually represents a "thick pointer", which is a structure
16818 with two elements: one that is a pointer to the array data, and one
16819 that is a pointer to another structure; this second structure holds
16820 the array bounds.
16821
16822 This returns a new type on success, or nullptr if this didn't
16823 recognize the type. */
16824
16825static struct type *
16826quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16827 struct type *type)
16828{
16829 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16830 /* So far we've only seen this with block form. */
16831 if (attr == nullptr || !attr->form_is_block ())
16832 return nullptr;
16833
16834 /* Note that this will fail if the structure layout is changed by
16835 the compiler. However, we have no good way to recognize some
16836 other layout, because we don't know what expression the compiler
16837 might choose to emit should this happen. */
16838 struct dwarf_block *blk = attr->as_block ();
16839 if (blk->size != 2
16840 || blk->data[0] != DW_OP_push_object_address
16841 || blk->data[1] != DW_OP_deref)
16842 return nullptr;
16843
16844 int bounds_offset = -1;
16845 int max_align = -1;
16846 std::vector<struct field> range_fields;
16847 for (struct die_info *child_die = die->child;
16848 child_die;
16849 child_die = child_die->sibling)
16850 {
16851 if (child_die->tag == DW_TAG_subrange_type)
16852 {
16853 struct type *underlying = read_subrange_index_type (child_die, cu);
16854
16855 int this_align = type_align (underlying);
16856 if (this_align > max_align)
16857 max_align = this_align;
16858
16859 range_fields.emplace_back ();
16860 range_fields.emplace_back ();
16861
16862 struct field &lower = range_fields[range_fields.size () - 2];
16863 struct field &upper = range_fields[range_fields.size () - 1];
16864
16865 lower.set_type (underlying);
16866 FIELD_ARTIFICIAL (lower) = 1;
16867
16868 upper.set_type (underlying);
16869 FIELD_ARTIFICIAL (upper) = 1;
16870
16871 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16872 &bounds_offset, &lower, cu)
16873 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16874 &bounds_offset, &upper, cu))
16875 return nullptr;
16876 }
16877 }
16878
16879 /* This shouldn't really happen, but double-check that we found
16880 where the bounds are stored. */
16881 if (bounds_offset == -1)
16882 return nullptr;
16883
16884 struct objfile *objfile = cu->per_objfile->objfile;
16885 for (int i = 0; i < range_fields.size (); i += 2)
16886 {
16887 char name[20];
16888
16889 /* Set the name of each field in the bounds. */
16890 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16891 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16892 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16893 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16894 }
16895
16896 struct type *bounds = alloc_type (objfile);
16897 bounds->set_code (TYPE_CODE_STRUCT);
16898
16899 bounds->set_num_fields (range_fields.size ());
16900 bounds->set_fields
16901 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16902 * sizeof (struct field))));
16903 memcpy (bounds->fields (), range_fields.data (),
16904 bounds->num_fields () * sizeof (struct field));
16905
16906 int last_fieldno = range_fields.size () - 1;
16907 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16908 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16909 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16910
16911 /* Rewrite the existing array type in place. Specifically, we
16912 remove any dynamic properties we might have read, and we replace
16913 the index types. */
16914 struct type *iter = type;
16915 for (int i = 0; i < range_fields.size (); i += 2)
16916 {
16917 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16918 iter->main_type->dyn_prop_list = nullptr;
16919 iter->set_index_type
16920 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16921 iter = TYPE_TARGET_TYPE (iter);
16922 }
16923
16924 struct type *result = alloc_type (objfile);
16925 result->set_code (TYPE_CODE_STRUCT);
16926
16927 result->set_num_fields (2);
16928 result->set_fields
16929 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16930 * sizeof (struct field))));
16931
16932 /* The names are chosen to coincide with what the compiler does with
16933 -fgnat-encodings=all, which the Ada code in gdb already
16934 understands. */
16935 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16936 result->field (0).set_type (lookup_pointer_type (type));
16937
16938 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16939 result->field (1).set_type (lookup_pointer_type (bounds));
16940 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16941
16942 result->set_name (type->name ());
16943 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16944 + TYPE_LENGTH (result->field (1).type ()));
16945
16946 return result;
16947}
16948
c906108c
SS
16949/* Extract all information from a DW_TAG_array_type DIE and put it in
16950 the DIE's type field. For now, this only handles one dimensional
16951 arrays. */
16952
f792889a 16953static struct type *
e7c27a73 16954read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16955{
5e22e966 16956 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16957 struct die_info *child_die;
7e314c57 16958 struct type *type;
c906108c 16959 struct type *element_type, *range_type, *index_type;
c906108c 16960 struct attribute *attr;
15d034d0 16961 const char *name;
a405673c 16962 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16963 unsigned int bit_stride = 0;
c906108c 16964
e7c27a73 16965 element_type = die_type (die, cu);
c906108c 16966
7e314c57
JK
16967 /* The die_type call above may have already set the type for this DIE. */
16968 type = get_die_type (die, cu);
16969 if (type)
16970 return type;
16971
dc53a7ad
JB
16972 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16973 if (attr != NULL)
a405673c
JB
16974 {
16975 int stride_ok;
293e7e51 16976 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16977
16978 byte_stride_prop
16979 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16980 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16981 prop_type);
a405673c
JB
16982 if (!stride_ok)
16983 {
b98664d3 16984 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16985 " - DIE at %s [in module %s]"),
16986 sect_offset_str (die->sect_off),
5e22e966 16987 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16988 /* Ignore this attribute. We will likely not be able to print
16989 arrays of this type correctly, but there is little we can do
16990 to help if we cannot read the attribute's value. */
16991 byte_stride_prop = NULL;
16992 }
16993 }
dc53a7ad
JB
16994
16995 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16996 if (attr != NULL)
529908cb 16997 bit_stride = attr->constant_value (0);
dc53a7ad 16998
c906108c
SS
16999 /* Irix 6.2 native cc creates array types without children for
17000 arrays with unspecified length. */
639d11d3 17001 if (die->child == NULL)
c906108c 17002 {
46bf5051 17003 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17004 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17005 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17006 byte_stride_prop, bit_stride);
f792889a 17007 return set_die_type (die, type, cu);
c906108c
SS
17008 }
17009
791afaa2 17010 std::vector<struct type *> range_types;
639d11d3 17011 child_die = die->child;
c906108c
SS
17012 while (child_die && child_die->tag)
17013 {
17014 if (child_die->tag == DW_TAG_subrange_type)
17015 {
f792889a 17016 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17017
dda83cd7
SM
17018 if (child_type != NULL)
17019 {
0963b4bd 17020 /* The range type was succesfully read. Save it for the
dda83cd7 17021 array type creation. */
791afaa2 17022 range_types.push_back (child_type);
dda83cd7 17023 }
c906108c 17024 }
436c571c 17025 child_die = child_die->sibling;
c906108c
SS
17026 }
17027
cf2b2075
TV
17028 if (range_types.empty ())
17029 {
17030 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17031 sect_offset_str (die->sect_off),
17032 objfile_name (cu->per_objfile->objfile));
17033 return NULL;
17034 }
17035
c906108c
SS
17036 /* Dwarf2 dimensions are output from left to right, create the
17037 necessary array types in backwards order. */
7ca2d3a3 17038
c906108c 17039 type = element_type;
7ca2d3a3
DL
17040
17041 if (read_array_order (die, cu) == DW_ORD_col_major)
17042 {
17043 int i = 0;
9a619af0 17044
791afaa2 17045 while (i < range_types.size ())
10f6a3ad
TT
17046 {
17047 type = create_array_type_with_stride (NULL, type, range_types[i++],
17048 byte_stride_prop, bit_stride);
17049 bit_stride = 0;
17050 byte_stride_prop = nullptr;
17051 }
7ca2d3a3
DL
17052 }
17053 else
17054 {
791afaa2 17055 size_t ndim = range_types.size ();
7ca2d3a3 17056 while (ndim-- > 0)
10f6a3ad
TT
17057 {
17058 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17059 byte_stride_prop, bit_stride);
17060 bit_stride = 0;
17061 byte_stride_prop = nullptr;
17062 }
7ca2d3a3 17063 }
c906108c 17064
cf2b2075
TV
17065 gdb_assert (type != element_type);
17066
f5f8a009
EZ
17067 /* Understand Dwarf2 support for vector types (like they occur on
17068 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17069 array type. This is not part of the Dwarf2/3 standard yet, but a
17070 custom vendor extension. The main difference between a regular
17071 array and the vector variant is that vectors are passed by value
17072 to functions. */
e142c38c 17073 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17074 if (attr != nullptr)
ea37ba09 17075 make_vector_type (type);
f5f8a009 17076
dbc98a8b
KW
17077 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17078 implementation may choose to implement triple vectors using this
17079 attribute. */
17080 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17081 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17082 {
529908cb
TT
17083 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17084 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17085 else
b98664d3 17086 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17087 "than the total size of elements"));
dbc98a8b
KW
17088 }
17089
39cbfefa
DJ
17090 name = dwarf2_name (die, cu);
17091 if (name)
d0e39ea2 17092 type->set_name (name);
6e70227d 17093
2b4424c3
TT
17094 maybe_set_alignment (cu, die, type);
17095
57567375
TT
17096 struct type *replacement_type = nullptr;
17097 if (cu->language == language_ada)
17098 {
17099 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17100 if (replacement_type != nullptr)
17101 type = replacement_type;
17102 }
17103
0963b4bd 17104 /* Install the type in the die. */
57567375 17105 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17106
17107 /* set_die_type should be already done. */
b4ba55a1
JB
17108 set_descriptive_type (type, die, cu);
17109
7e314c57 17110 return type;
c906108c
SS
17111}
17112
7ca2d3a3 17113static enum dwarf_array_dim_ordering
6e70227d 17114read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17115{
17116 struct attribute *attr;
17117
17118 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17119
435d3d88 17120 if (attr != nullptr)
1bc397c5
TT
17121 {
17122 LONGEST val = attr->constant_value (-1);
17123 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17124 return (enum dwarf_array_dim_ordering) val;
17125 }
7ca2d3a3 17126
0963b4bd
MS
17127 /* GNU F77 is a special case, as at 08/2004 array type info is the
17128 opposite order to the dwarf2 specification, but data is still
17129 laid out as per normal fortran.
7ca2d3a3 17130
0963b4bd
MS
17131 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17132 version checking. */
7ca2d3a3 17133
905e0470
PM
17134 if (cu->language == language_fortran
17135 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17136 {
17137 return DW_ORD_row_major;
17138 }
17139
3a3440fb 17140 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17141 {
17142 case array_column_major:
17143 return DW_ORD_col_major;
17144 case array_row_major:
17145 default:
17146 return DW_ORD_row_major;
17147 };
17148}
17149
72019c9c 17150/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17151 the DIE's type field. */
72019c9c 17152
f792889a 17153static struct type *
72019c9c
GM
17154read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17155{
7e314c57
JK
17156 struct type *domain_type, *set_type;
17157 struct attribute *attr;
f792889a 17158
7e314c57
JK
17159 domain_type = die_type (die, cu);
17160
17161 /* The die_type call above may have already set the type for this DIE. */
17162 set_type = get_die_type (die, cu);
17163 if (set_type)
17164 return set_type;
17165
17166 set_type = create_set_type (NULL, domain_type);
17167
17168 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17169 if (attr != nullptr && attr->form_is_unsigned ())
17170 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17171
2b4424c3
TT
17172 maybe_set_alignment (cu, die, set_type);
17173
f792889a 17174 return set_die_type (die, set_type, cu);
72019c9c 17175}
7ca2d3a3 17176
0971de02
TT
17177/* A helper for read_common_block that creates a locexpr baton.
17178 SYM is the symbol which we are marking as computed.
17179 COMMON_DIE is the DIE for the common block.
17180 COMMON_LOC is the location expression attribute for the common
17181 block itself.
17182 MEMBER_LOC is the location expression attribute for the particular
17183 member of the common block that we are processing.
17184 CU is the CU from which the above come. */
17185
17186static void
17187mark_common_block_symbol_computed (struct symbol *sym,
17188 struct die_info *common_die,
17189 struct attribute *common_loc,
17190 struct attribute *member_loc,
17191 struct dwarf2_cu *cu)
17192{
5e22e966 17193 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17194 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17195 struct dwarf2_locexpr_baton *baton;
17196 gdb_byte *ptr;
17197 unsigned int cu_off;
08feed99 17198 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17199 LONGEST offset = 0;
17200
17201 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17202 gdb_assert (common_loc->form_is_block ());
17203 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17204 || member_loc->form_is_constant ());
0971de02 17205
8d749320 17206 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17207 baton->per_objfile = per_objfile;
0971de02
TT
17208 baton->per_cu = cu->per_cu;
17209 gdb_assert (baton->per_cu);
17210
17211 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17212
cd6c91b4 17213 if (member_loc->form_is_constant ())
0971de02 17214 {
0826b30a 17215 offset = member_loc->constant_value (0);
0971de02
TT
17216 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17217 }
17218 else
9d2246fc 17219 baton->size += member_loc->as_block ()->size;
0971de02 17220
224c3ddb 17221 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17222 baton->data = ptr;
17223
17224 *ptr++ = DW_OP_call4;
9c541725 17225 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17226 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17227 ptr += 4;
17228
cd6c91b4 17229 if (member_loc->form_is_constant ())
0971de02
TT
17230 {
17231 *ptr++ = DW_OP_addr;
17232 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17233 ptr += cu->header.addr_size;
17234 }
17235 else
17236 {
17237 /* We have to copy the data here, because DW_OP_call4 will only
17238 use a DW_AT_location attribute. */
9d2246fc
TT
17239 struct dwarf_block *block = member_loc->as_block ();
17240 memcpy (ptr, block->data, block->size);
17241 ptr += block->size;
0971de02
TT
17242 }
17243
17244 *ptr++ = DW_OP_plus;
17245 gdb_assert (ptr - baton->data == baton->size);
17246
0971de02 17247 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17248 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17249}
17250
4357ac6c
TT
17251/* Create appropriate locally-scoped variables for all the
17252 DW_TAG_common_block entries. Also create a struct common_block
17253 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17254 is used to separate the common blocks name namespace from regular
4357ac6c 17255 variable names. */
c906108c
SS
17256
17257static void
e7c27a73 17258read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17259{
0971de02
TT
17260 struct attribute *attr;
17261
17262 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17263 if (attr != nullptr)
0971de02
TT
17264 {
17265 /* Support the .debug_loc offsets. */
4fc6c0d5 17266 if (attr->form_is_block ())
dda83cd7 17267 {
0971de02 17268 /* Ok. */
dda83cd7 17269 }
cd6c91b4 17270 else if (attr->form_is_section_offset ())
dda83cd7 17271 {
0971de02
TT
17272 dwarf2_complex_location_expr_complaint ();
17273 attr = NULL;
dda83cd7 17274 }
0971de02 17275 else
dda83cd7 17276 {
0971de02
TT
17277 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17278 "common block member");
17279 attr = NULL;
dda83cd7 17280 }
0971de02
TT
17281 }
17282
639d11d3 17283 if (die->child != NULL)
c906108c 17284 {
5e22e966 17285 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17286 struct die_info *child_die;
17287 size_t n_entries = 0, size;
17288 struct common_block *common_block;
17289 struct symbol *sym;
74ac6d43 17290
4357ac6c
TT
17291 for (child_die = die->child;
17292 child_die && child_die->tag;
436c571c 17293 child_die = child_die->sibling)
4357ac6c
TT
17294 ++n_entries;
17295
17296 size = (sizeof (struct common_block)
17297 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17298 common_block
17299 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17300 size);
4357ac6c
TT
17301 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17302 common_block->n_entries = 0;
17303
17304 for (child_die = die->child;
17305 child_die && child_die->tag;
436c571c 17306 child_die = child_die->sibling)
4357ac6c
TT
17307 {
17308 /* Create the symbol in the DW_TAG_common_block block in the current
17309 symbol scope. */
e7c27a73 17310 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17311 if (sym != NULL)
17312 {
17313 struct attribute *member_loc;
17314
17315 common_block->contents[common_block->n_entries++] = sym;
17316
17317 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17318 cu);
17319 if (member_loc)
17320 {
17321 /* GDB has handled this for a long time, but it is
17322 not specified by DWARF. It seems to have been
17323 emitted by gfortran at least as recently as:
17324 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17325 complaint (_("Variable in common block has "
0971de02 17326 "DW_AT_data_member_location "
9d8780f0
SM
17327 "- DIE at %s [in module %s]"),
17328 sect_offset_str (child_die->sect_off),
518817b3 17329 objfile_name (objfile));
0971de02 17330
cd6c91b4 17331 if (member_loc->form_is_section_offset ())
0971de02 17332 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17333 else if (member_loc->form_is_constant ()
4fc6c0d5 17334 || member_loc->form_is_block ())
0971de02 17335 {
435d3d88 17336 if (attr != nullptr)
0971de02
TT
17337 mark_common_block_symbol_computed (sym, die, attr,
17338 member_loc, cu);
17339 }
17340 else
17341 dwarf2_complex_location_expr_complaint ();
17342 }
17343 }
c906108c 17344 }
4357ac6c
TT
17345
17346 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17347 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17348 }
17349}
17350
0114d602 17351/* Create a type for a C++ namespace. */
d9fa45fe 17352
0114d602
DJ
17353static struct type *
17354read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17355{
5e22e966 17356 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17357 const char *previous_prefix, *name;
9219021c 17358 int is_anonymous;
0114d602
DJ
17359 struct type *type;
17360
17361 /* For extensions, reuse the type of the original namespace. */
17362 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17363 {
17364 struct die_info *ext_die;
17365 struct dwarf2_cu *ext_cu = cu;
9a619af0 17366
0114d602
DJ
17367 ext_die = dwarf2_extension (die, &ext_cu);
17368 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17369
17370 /* EXT_CU may not be the same as CU.
02142a6c 17371 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17372 return set_die_type (die, type, cu);
17373 }
9219021c 17374
e142c38c 17375 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17376
17377 /* Now build the name of the current namespace. */
17378
0114d602
DJ
17379 previous_prefix = determine_prefix (die, cu);
17380 if (previous_prefix[0] != '\0')
17381 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17382 previous_prefix, name, 0, cu);
0114d602
DJ
17383
17384 /* Create the type. */
19f392bc 17385 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17386
60531b24 17387 return set_die_type (die, type, cu);
0114d602
DJ
17388}
17389
22cee43f 17390/* Read a namespace scope. */
0114d602
DJ
17391
17392static void
17393read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17394{
5e22e966 17395 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17396 int is_anonymous;
9219021c 17397
5c4e30ca
DC
17398 /* Add a symbol associated to this if we haven't seen the namespace
17399 before. Also, add a using directive if it's an anonymous
17400 namespace. */
9219021c 17401
f2f0e013 17402 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17403 {
17404 struct type *type;
17405
0114d602 17406 type = read_type_die (die, cu);
e7c27a73 17407 new_symbol (die, type, cu);
5c4e30ca 17408
e8e80198 17409 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17410 if (is_anonymous)
0114d602
DJ
17411 {
17412 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17413
eb1e02fd 17414 std::vector<const char *> excludes;
804d2729 17415 add_using_directive (using_directives (cu),
7d93a1e0 17416 previous_prefix, type->name (), NULL,
eb1e02fd 17417 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17418 }
5c4e30ca 17419 }
9219021c 17420
639d11d3 17421 if (die->child != NULL)
d9fa45fe 17422 {
639d11d3 17423 struct die_info *child_die = die->child;
6e70227d 17424
d9fa45fe
DC
17425 while (child_die && child_die->tag)
17426 {
e7c27a73 17427 process_die (child_die, cu);
436c571c 17428 child_die = child_die->sibling;
d9fa45fe
DC
17429 }
17430 }
38d518c9
EZ
17431}
17432
f55ee35c
JK
17433/* Read a Fortran module as type. This DIE can be only a declaration used for
17434 imported module. Still we need that type as local Fortran "use ... only"
17435 declaration imports depend on the created type in determine_prefix. */
17436
17437static struct type *
17438read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17439{
5e22e966 17440 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17441 const char *module_name;
f55ee35c
JK
17442 struct type *type;
17443
17444 module_name = dwarf2_name (die, cu);
19f392bc 17445 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17446
f55ee35c
JK
17447 return set_die_type (die, type, cu);
17448}
17449
5d7cb8df
JK
17450/* Read a Fortran module. */
17451
17452static void
17453read_module (struct die_info *die, struct dwarf2_cu *cu)
17454{
17455 struct die_info *child_die = die->child;
530e8392
KB
17456 struct type *type;
17457
17458 type = read_type_die (die, cu);
17459 new_symbol (die, type, cu);
5d7cb8df 17460
5d7cb8df
JK
17461 while (child_die && child_die->tag)
17462 {
17463 process_die (child_die, cu);
436c571c 17464 child_die = child_die->sibling;
5d7cb8df
JK
17465 }
17466}
17467
38d518c9
EZ
17468/* Return the name of the namespace represented by DIE. Set
17469 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17470 namespace. */
17471
17472static const char *
e142c38c 17473namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17474{
17475 struct die_info *current_die;
17476 const char *name = NULL;
17477
17478 /* Loop through the extensions until we find a name. */
17479
17480 for (current_die = die;
17481 current_die != NULL;
f2f0e013 17482 current_die = dwarf2_extension (die, &cu))
38d518c9 17483 {
96553a0c
DE
17484 /* We don't use dwarf2_name here so that we can detect the absence
17485 of a name -> anonymous namespace. */
7d45c7c3 17486 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17487
38d518c9
EZ
17488 if (name != NULL)
17489 break;
17490 }
17491
17492 /* Is it an anonymous namespace? */
17493
17494 *is_anonymous = (name == NULL);
17495 if (*is_anonymous)
2b1dbab0 17496 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17497
17498 return name;
d9fa45fe
DC
17499}
17500
c906108c
SS
17501/* Extract all information from a DW_TAG_pointer_type DIE and add to
17502 the user defined type vector. */
17503
f792889a 17504static struct type *
e7c27a73 17505read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17506{
5e22e966 17507 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17508 struct comp_unit_head *cu_header = &cu->header;
c906108c 17509 struct type *type;
8b2dbe47
KB
17510 struct attribute *attr_byte_size;
17511 struct attribute *attr_address_class;
17512 int byte_size, addr_class;
7e314c57
JK
17513 struct type *target_type;
17514
17515 target_type = die_type (die, cu);
c906108c 17516
7e314c57
JK
17517 /* The die_type call above may have already set the type for this DIE. */
17518 type = get_die_type (die, cu);
17519 if (type)
17520 return type;
17521
17522 type = lookup_pointer_type (target_type);
8b2dbe47 17523
e142c38c 17524 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17525 if (attr_byte_size)
529908cb 17526 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17527 else
8b2dbe47
KB
17528 byte_size = cu_header->addr_size;
17529
e142c38c 17530 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17531 if (attr_address_class)
529908cb 17532 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17533 else
17534 addr_class = DW_ADDR_none;
17535
2b4424c3
TT
17536 ULONGEST alignment = get_alignment (cu, die);
17537
17538 /* If the pointer size, alignment, or address class is different
17539 than the default, create a type variant marked as such and set
17540 the length accordingly. */
17541 if (TYPE_LENGTH (type) != byte_size
17542 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17543 && alignment != TYPE_RAW_ALIGN (type))
17544 || addr_class != DW_ADDR_none)
c906108c 17545 {
5e2b427d 17546 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17547 {
314ad88d
PA
17548 type_instance_flags type_flags
17549 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17550 addr_class);
876cecd0
TT
17551 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17552 == 0);
8b2dbe47
KB
17553 type = make_type_with_address_space (type, type_flags);
17554 }
17555 else if (TYPE_LENGTH (type) != byte_size)
17556 {
b98664d3 17557 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17558 }
2b4424c3
TT
17559 else if (TYPE_RAW_ALIGN (type) != alignment)
17560 {
b98664d3 17561 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17562 " - DIE at %s [in module %s]"),
17563 sect_offset_str (die->sect_off),
5e22e966 17564 objfile_name (cu->per_objfile->objfile));
2b4424c3 17565 }
6e70227d 17566 else
9a619af0
MS
17567 {
17568 /* Should we also complain about unhandled address classes? */
17569 }
c906108c 17570 }
8b2dbe47
KB
17571
17572 TYPE_LENGTH (type) = byte_size;
2b4424c3 17573 set_type_align (type, alignment);
f792889a 17574 return set_die_type (die, type, cu);
c906108c
SS
17575}
17576
17577/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17578 the user defined type vector. */
17579
f792889a 17580static struct type *
e7c27a73 17581read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17582{
17583 struct type *type;
17584 struct type *to_type;
17585 struct type *domain;
17586
e7c27a73
DJ
17587 to_type = die_type (die, cu);
17588 domain = die_containing_type (die, cu);
0d5de010 17589
7e314c57
JK
17590 /* The calls above may have already set the type for this DIE. */
17591 type = get_die_type (die, cu);
17592 if (type)
17593 return type;
17594
78134374 17595 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17596 type = lookup_methodptr_type (to_type);
78134374 17597 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17598 {
5e22e966 17599 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17600
17601 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17602 to_type->fields (), to_type->num_fields (),
a409645d 17603 to_type->has_varargs ());
7078baeb
TT
17604 type = lookup_methodptr_type (new_type);
17605 }
0d5de010
DJ
17606 else
17607 type = lookup_memberptr_type (to_type, domain);
c906108c 17608
f792889a 17609 return set_die_type (die, type, cu);
c906108c
SS
17610}
17611
4297a3f0 17612/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17613 the user defined type vector. */
17614
f792889a 17615static struct type *
4297a3f0 17616read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17617 enum type_code refcode)
c906108c 17618{
e7c27a73 17619 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17620 struct type *type, *target_type;
c906108c
SS
17621 struct attribute *attr;
17622
4297a3f0
AV
17623 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17624
7e314c57
JK
17625 target_type = die_type (die, cu);
17626
17627 /* The die_type call above may have already set the type for this DIE. */
17628 type = get_die_type (die, cu);
17629 if (type)
17630 return type;
17631
4297a3f0 17632 type = lookup_reference_type (target_type, refcode);
e142c38c 17633 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17634 if (attr != nullptr)
c906108c 17635 {
529908cb 17636 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17637 }
17638 else
17639 {
107d2387 17640 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17641 }
2b4424c3 17642 maybe_set_alignment (cu, die, type);
f792889a 17643 return set_die_type (die, type, cu);
c906108c
SS
17644}
17645
cf363f18
MW
17646/* Add the given cv-qualifiers to the element type of the array. GCC
17647 outputs DWARF type qualifiers that apply to an array, not the
17648 element type. But GDB relies on the array element type to carry
17649 the cv-qualifiers. This mimics section 6.7.3 of the C99
17650 specification. */
17651
17652static struct type *
17653add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17654 struct type *base_type, int cnst, int voltl)
17655{
17656 struct type *el_type, *inner_array;
17657
17658 base_type = copy_type (base_type);
17659 inner_array = base_type;
17660
78134374 17661 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17662 {
17663 TYPE_TARGET_TYPE (inner_array) =
17664 copy_type (TYPE_TARGET_TYPE (inner_array));
17665 inner_array = TYPE_TARGET_TYPE (inner_array);
17666 }
17667
17668 el_type = TYPE_TARGET_TYPE (inner_array);
17669 cnst |= TYPE_CONST (el_type);
17670 voltl |= TYPE_VOLATILE (el_type);
17671 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17672
17673 return set_die_type (die, base_type, cu);
17674}
17675
f792889a 17676static struct type *
e7c27a73 17677read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17678{
f792889a 17679 struct type *base_type, *cv_type;
c906108c 17680
e7c27a73 17681 base_type = die_type (die, cu);
7e314c57
JK
17682
17683 /* The die_type call above may have already set the type for this DIE. */
17684 cv_type = get_die_type (die, cu);
17685 if (cv_type)
17686 return cv_type;
17687
2f608a3a
KW
17688 /* In case the const qualifier is applied to an array type, the element type
17689 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17690 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17691 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17692
f792889a
DJ
17693 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17694 return set_die_type (die, cv_type, cu);
c906108c
SS
17695}
17696
f792889a 17697static struct type *
e7c27a73 17698read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17699{
f792889a 17700 struct type *base_type, *cv_type;
c906108c 17701
e7c27a73 17702 base_type = die_type (die, cu);
7e314c57
JK
17703
17704 /* The die_type call above may have already set the type for this DIE. */
17705 cv_type = get_die_type (die, cu);
17706 if (cv_type)
17707 return cv_type;
17708
cf363f18
MW
17709 /* In case the volatile qualifier is applied to an array type, the
17710 element type is so qualified, not the array type (section 6.7.3
17711 of C99). */
78134374 17712 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17713 return add_array_cv_type (die, cu, base_type, 0, 1);
17714
f792889a
DJ
17715 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17716 return set_die_type (die, cv_type, cu);
c906108c
SS
17717}
17718
06d66ee9
TT
17719/* Handle DW_TAG_restrict_type. */
17720
17721static struct type *
17722read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17723{
17724 struct type *base_type, *cv_type;
17725
17726 base_type = die_type (die, cu);
17727
17728 /* The die_type call above may have already set the type for this DIE. */
17729 cv_type = get_die_type (die, cu);
17730 if (cv_type)
17731 return cv_type;
17732
17733 cv_type = make_restrict_type (base_type);
17734 return set_die_type (die, cv_type, cu);
17735}
17736
a2c2acaf
MW
17737/* Handle DW_TAG_atomic_type. */
17738
17739static struct type *
17740read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17741{
17742 struct type *base_type, *cv_type;
17743
17744 base_type = die_type (die, cu);
17745
17746 /* The die_type call above may have already set the type for this DIE. */
17747 cv_type = get_die_type (die, cu);
17748 if (cv_type)
17749 return cv_type;
17750
17751 cv_type = make_atomic_type (base_type);
17752 return set_die_type (die, cv_type, cu);
17753}
17754
c906108c
SS
17755/* Extract all information from a DW_TAG_string_type DIE and add to
17756 the user defined type vector. It isn't really a user defined type,
17757 but it behaves like one, with other DIE's using an AT_user_def_type
17758 attribute to reference it. */
17759
f792889a 17760static struct type *
e7c27a73 17761read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17762{
5e22e966 17763 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17764 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17765 struct type *type, *range_type, *index_type, *char_type;
17766 struct attribute *attr;
216a7e6b
AB
17767 struct dynamic_prop prop;
17768 bool length_is_constant = true;
17769 LONGEST length;
17770
17771 /* There are a couple of places where bit sizes might be made use of
17772 when parsing a DW_TAG_string_type, however, no producer that we know
17773 of make use of these. Handling bit sizes that are a multiple of the
17774 byte size is easy enough, but what about other bit sizes? Lets deal
17775 with that problem when we have to. Warn about these attributes being
17776 unsupported, then parse the type and ignore them like we always
17777 have. */
17778 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17779 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17780 {
17781 static bool warning_printed = false;
17782 if (!warning_printed)
17783 {
17784 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17785 "currently supported on DW_TAG_string_type."));
17786 warning_printed = true;
17787 }
17788 }
c906108c 17789
e142c38c 17790 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17791 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17792 {
17793 /* The string length describes the location at which the length of
17794 the string can be found. The size of the length field can be
17795 specified with one of the attributes below. */
17796 struct type *prop_type;
17797 struct attribute *len
17798 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17799 if (len == nullptr)
17800 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17801 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17802 {
17803 /* Pass 0 as the default as we know this attribute is constant
17804 and the default value will not be returned. */
0826b30a 17805 LONGEST sz = len->constant_value (0);
293e7e51 17806 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17807 }
17808 else
17809 {
17810 /* If the size is not specified then we assume it is the size of
17811 an address on this target. */
293e7e51 17812 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17813 }
17814
17815 /* Convert the attribute into a dynamic property. */
17816 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17817 length = 1;
17818 else
17819 length_is_constant = false;
17820 }
17821 else if (attr != nullptr)
17822 {
17823 /* This DW_AT_string_length just contains the length with no
17824 indirection. There's no need to create a dynamic property in this
17825 case. Pass 0 for the default value as we know it will not be
17826 returned in this case. */
0826b30a 17827 length = attr->constant_value (0);
216a7e6b
AB
17828 }
17829 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17830 {
216a7e6b 17831 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17832 length = attr->constant_value (1);
c906108c
SS
17833 }
17834 else
17835 {
216a7e6b
AB
17836 /* Use 1 as a fallback length if we have nothing else. */
17837 length = 1;
c906108c 17838 }
6ccb9162 17839
46bf5051 17840 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17841 if (length_is_constant)
17842 range_type = create_static_range_type (NULL, index_type, 1, length);
17843 else
17844 {
17845 struct dynamic_prop low_bound;
17846
8c2e4e06 17847 low_bound.set_const_val (1);
216a7e6b
AB
17848 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17849 }
3b7538c0
UW
17850 char_type = language_string_char_type (cu->language_defn, gdbarch);
17851 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17852
f792889a 17853 return set_die_type (die, type, cu);
c906108c
SS
17854}
17855
4d804846
JB
17856/* Assuming that DIE corresponds to a function, returns nonzero
17857 if the function is prototyped. */
17858
17859static int
17860prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17861{
17862 struct attribute *attr;
17863
17864 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17865 if (attr && attr->as_boolean ())
4d804846
JB
17866 return 1;
17867
17868 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17869 is only meaningful for C, but the concept also extends to other
4d804846
JB
17870 languages that allow unprototyped functions (Eg: Objective C).
17871 For all other languages, assume that functions are always
17872 prototyped. */
17873 if (cu->language != language_c
17874 && cu->language != language_objc
17875 && cu->language != language_opencl)
17876 return 1;
17877
17878 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17879 prototyped and unprototyped functions; default to prototyped,
17880 since that is more common in modern code (and RealView warns
17881 about unprototyped functions). */
17882 if (producer_is_realview (cu->producer))
17883 return 1;
17884
17885 return 0;
17886}
17887
c906108c
SS
17888/* Handle DIES due to C code like:
17889
17890 struct foo
c5aa993b
JM
17891 {
17892 int (*funcp)(int a, long l);
17893 int b;
17894 };
c906108c 17895
0963b4bd 17896 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17897
f792889a 17898static struct type *
e7c27a73 17899read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17900{
5e22e966 17901 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17902 struct type *type; /* Type that this function returns. */
17903 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17904 struct attribute *attr;
17905
e7c27a73 17906 type = die_type (die, cu);
7e314c57
JK
17907
17908 /* The die_type call above may have already set the type for this DIE. */
17909 ftype = get_die_type (die, cu);
17910 if (ftype)
17911 return ftype;
17912
0c8b41f1 17913 ftype = lookup_function_type (type);
c906108c 17914
4d804846 17915 if (prototyped_function_p (die, cu))
27e69b7a 17916 ftype->set_is_prototyped (true);
c906108c 17917
c055b101
CV
17918 /* Store the calling convention in the type if it's available in
17919 the subroutine die. Otherwise set the calling convention to
17920 the default value DW_CC_normal. */
17921 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 17922 if (attr != nullptr
529908cb 17923 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 17924 TYPE_CALLING_CONVENTION (ftype)
529908cb 17925 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
17926 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17927 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17928 else
17929 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17930
743649fd
MW
17931 /* Record whether the function returns normally to its caller or not
17932 if the DWARF producer set that information. */
17933 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 17934 if (attr && attr->as_boolean ())
743649fd
MW
17935 TYPE_NO_RETURN (ftype) = 1;
17936
76c10ea2
GM
17937 /* We need to add the subroutine type to the die immediately so
17938 we don't infinitely recurse when dealing with parameters
0963b4bd 17939 declared as the same subroutine type. */
76c10ea2 17940 set_die_type (die, ftype, cu);
6e70227d 17941
639d11d3 17942 if (die->child != NULL)
c906108c 17943 {
bb5ed363 17944 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17945 struct die_info *child_die;
8072405b 17946 int nparams, iparams;
c906108c
SS
17947
17948 /* Count the number of parameters.
dda83cd7
SM
17949 FIXME: GDB currently ignores vararg functions, but knows about
17950 vararg member functions. */
8072405b 17951 nparams = 0;
639d11d3 17952 child_die = die->child;
c906108c
SS
17953 while (child_die && child_die->tag)
17954 {
17955 if (child_die->tag == DW_TAG_formal_parameter)
17956 nparams++;
17957 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
17958 ftype->set_has_varargs (true);
17959
436c571c 17960 child_die = child_die->sibling;
c906108c
SS
17961 }
17962
17963 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17964 ftype->set_num_fields (nparams);
3cabb6b0
SM
17965 ftype->set_fields
17966 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17967
8072405b
JK
17968 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17969 even if we error out during the parameters reading below. */
17970 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 17971 ftype->field (iparams).set_type (void_type);
8072405b
JK
17972
17973 iparams = 0;
639d11d3 17974 child_die = die->child;
c906108c
SS
17975 while (child_die && child_die->tag)
17976 {
17977 if (child_die->tag == DW_TAG_formal_parameter)
17978 {
3ce3b1ba
PA
17979 struct type *arg_type;
17980
17981 /* DWARF version 2 has no clean way to discern C++
17982 static and non-static member functions. G++ helps
17983 GDB by marking the first parameter for non-static
17984 member functions (which is the this pointer) as
17985 artificial. We pass this information to
17986 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17987
17988 DWARF version 3 added DW_AT_object_pointer, which GCC
17989 4.5 does not yet generate. */
e142c38c 17990 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17991 if (attr != nullptr)
c45bc3f8 17992 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 17993 else
9c37b5ae 17994 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17995 arg_type = die_type (child_die, cu);
17996
17997 /* RealView does not mark THIS as const, which the testsuite
17998 expects. GCC marks THIS as const in method definitions,
17999 but not in the class specifications (GCC PR 43053). */
18000 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18001 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18002 {
18003 int is_this = 0;
18004 struct dwarf2_cu *arg_cu = cu;
18005 const char *name = dwarf2_name (child_die, cu);
18006
18007 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18008 if (attr != nullptr)
3ce3b1ba
PA
18009 {
18010 /* If the compiler emits this, use it. */
18011 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18012 is_this = 1;
18013 }
18014 else if (name && strcmp (name, "this") == 0)
18015 /* Function definitions will have the argument names. */
18016 is_this = 1;
18017 else if (name == NULL && iparams == 0)
18018 /* Declarations may not have the names, so like
18019 elsewhere in GDB, assume an artificial first
18020 argument is "this". */
18021 is_this = 1;
18022
18023 if (is_this)
18024 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18025 arg_type, 0);
18026 }
18027
5d14b6e5 18028 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18029 iparams++;
18030 }
436c571c 18031 child_die = child_die->sibling;
c906108c
SS
18032 }
18033 }
18034
76c10ea2 18035 return ftype;
c906108c
SS
18036}
18037
f792889a 18038static struct type *
e7c27a73 18039read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18040{
5e22e966 18041 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18042 const char *name = NULL;
3c8e0968 18043 struct type *this_type, *target_type;
c906108c 18044
94af9270 18045 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18046 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18047 this_type->set_target_is_stub (true);
f792889a 18048 set_die_type (die, this_type, cu);
3c8e0968
DE
18049 target_type = die_type (die, cu);
18050 if (target_type != this_type)
18051 TYPE_TARGET_TYPE (this_type) = target_type;
18052 else
18053 {
18054 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18055 spec and cause infinite loops in GDB. */
b98664d3 18056 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18057 "- DIE at %s [in module %s]"),
18058 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18059 TYPE_TARGET_TYPE (this_type) = NULL;
18060 }
e4003a34
TV
18061 if (name == NULL)
18062 {
18063 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18064 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18065 Handle these by just returning the target type, rather than
18066 constructing an anonymous typedef type and trying to handle this
18067 elsewhere. */
18068 set_die_type (die, target_type, cu);
18069 return target_type;
18070 }
f792889a 18071 return this_type;
c906108c
SS
18072}
18073
a625a8c9
TT
18074/* Helper for get_dwarf2_rational_constant that computes the value of
18075 a given gmp_mpz given an attribute. */
18076
18077static void
18078get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18079{
18080 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18081 location expression that pushes an implicit value. */
18082 if (attr->form == DW_FORM_exprloc)
18083 {
18084 dwarf_block *blk = attr->as_block ();
18085 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18086 {
18087 uint64_t len;
18088 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18089 blk->data + blk->size,
18090 &len);
18091 if (ptr - blk->data + len <= blk->size)
18092 {
18093 mpz_import (value->val, len,
18094 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18095 1, 0, 0, ptr);
18096 return;
18097 }
18098 }
18099
18100 /* On failure set it to 1. */
18101 *value = gdb_mpz (1);
18102 }
18103 else if (attr->form_is_block ())
18104 {
18105 dwarf_block *blk = attr->as_block ();
18106 mpz_import (value->val, blk->size,
18107 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18108 1, 0, 0, blk->data);
18109 }
18110 else
18111 *value = gdb_mpz (attr->constant_value (1));
18112}
18113
09584414
JB
18114/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18115 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18116
18117 If the numerator and/or numerator attribute is missing,
18118 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18119 untouched. */
18120
18121static void
18122get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18123 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18124{
18125 struct attribute *num_attr, *denom_attr;
18126
18127 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18128 if (num_attr == nullptr)
18129 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18130 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18131
18132 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18133 if (denom_attr == nullptr)
18134 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18135 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18136
18137 if (num_attr == nullptr || denom_attr == nullptr)
18138 return;
18139
a625a8c9
TT
18140 get_mpz (cu, numerator, num_attr);
18141 get_mpz (cu, denominator, denom_attr);
09584414
JB
18142}
18143
18144/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18145 rational constant, rather than a signed one.
18146
18147 If the rational constant has a negative value, a complaint
18148 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18149
18150static void
18151get_dwarf2_unsigned_rational_constant (struct die_info *die,
18152 struct dwarf2_cu *cu,
5cde1d82
TT
18153 gdb_mpz *numerator,
18154 gdb_mpz *denominator)
09584414 18155{
5cde1d82
TT
18156 gdb_mpz num (1);
18157 gdb_mpz denom (1);
09584414
JB
18158
18159 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18160 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18161 {
5cde1d82
TT
18162 mpz_neg (num.val, num.val);
18163 mpz_neg (denom.val, denom.val);
09584414 18164 }
5cde1d82 18165 else if (mpz_sgn (num.val) == -1)
09584414
JB
18166 {
18167 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18168 " in DIE at %s"),
18169 sect_offset_str (die->sect_off));
18170 return;
18171 }
5cde1d82 18172 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18173 {
18174 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18175 " in DIE at %s"),
18176 sect_offset_str (die->sect_off));
18177 return;
18178 }
18179
5cde1d82
TT
18180 *numerator = std::move (num);
18181 *denominator = std::move (denom);
09584414
JB
18182}
18183
bbcdf9ab
TT
18184/* Assuming that ENCODING is a string whose contents starting at the
18185 K'th character is "_nn" where "nn" is a decimal number, scan that
18186 number and set RESULT to the value. K is updated to point to the
18187 character immediately following the number.
18188
18189 If the string does not conform to the format described above, false
18190 is returned, and K may or may not be changed. */
18191
18192static bool
18193ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18194{
18195 /* The next character should be an underscore ('_') followed
18196 by a digit. */
18197 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18198 return false;
18199
18200 /* Skip the underscore. */
18201 k++;
18202 int start = k;
18203
18204 /* Determine the number of digits for our number. */
18205 while (isdigit (encoding[k]))
18206 k++;
18207 if (k == start)
18208 return false;
18209
18210 std::string copy (&encoding[start], k - start);
18211 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18212 return false;
18213
18214 return true;
18215}
18216
18217/* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18218 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18219 DENOM, update OFFSET, and return true on success. Return false on
18220 failure. */
18221
18222static bool
18223ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18224 gdb_mpz *num, gdb_mpz *denom)
18225{
18226 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18227 return false;
18228 return ada_get_gnat_encoded_number (encoding, offset, denom);
18229}
18230
09584414 18231/* Assuming DIE corresponds to a fixed point type, finish the creation
bbcdf9ab
TT
18232 of the corresponding TYPE by setting its type-specific data. CU is
18233 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18234 encodings. It is nullptr if the GNAT encoding should be
18235 ignored. */
09584414
JB
18236
18237static void
bbcdf9ab
TT
18238finish_fixed_point_type (struct type *type, const char *suffix,
18239 struct die_info *die, struct dwarf2_cu *cu)
09584414 18240{
09584414
JB
18241 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18242 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18243
bbcdf9ab
TT
18244 /* If GNAT encodings are preferred, don't examine the
18245 attributes. */
18246 struct attribute *attr = nullptr;
18247 if (suffix == nullptr)
18248 {
18249 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18250 if (attr == nullptr)
18251 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18252 if (attr == nullptr)
18253 attr = dwarf2_attr (die, DW_AT_small, cu);
18254 }
09584414 18255
5cde1d82
TT
18256 /* Numerator and denominator of our fixed-point type's scaling factor.
18257 The default is a scaling factor of 1, which we use as a fallback
18258 when we are not able to decode it (problem with the debugging info,
18259 unsupported forms, bug in GDB, etc...). Using that as the default
18260 allows us to at least print the unscaled value, which might still
18261 be useful to a user. */
18262 gdb_mpz scale_num (1);
18263 gdb_mpz scale_denom (1);
18264
09584414
JB
18265 if (attr == nullptr)
18266 {
bbcdf9ab
TT
18267 int offset = 0;
18268 if (suffix != nullptr
18269 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18270 &scale_denom)
18271 /* The number might be encoded as _nn_dd_nn_dd, where the
18272 second ratio is the 'small value. In this situation, we
18273 want the second value. */
18274 && (suffix[offset] != '_'
18275 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18276 &scale_denom)))
18277 {
18278 /* Found it. */
18279 }
18280 else
18281 {
18282 /* Scaling factor not found. Assume a scaling factor of 1,
18283 and hope for the best. At least the user will be able to
18284 see the encoded value. */
18285 scale_num = 1;
18286 scale_denom = 1;
18287 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18288 sect_offset_str (die->sect_off));
18289 }
09584414
JB
18290 }
18291 else if (attr->name == DW_AT_binary_scale)
18292 {
18293 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18294 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18295
5cde1d82 18296 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18297 }
18298 else if (attr->name == DW_AT_decimal_scale)
18299 {
18300 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18301 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18302
5cde1d82 18303 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18304 }
18305 else if (attr->name == DW_AT_small)
18306 {
18307 struct die_info *scale_die;
18308 struct dwarf2_cu *scale_cu = cu;
18309
18310 scale_die = follow_die_ref (die, attr, &scale_cu);
18311 if (scale_die->tag == DW_TAG_constant)
18312 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18313 &scale_num, &scale_denom);
18314 else
18315 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18316 " (DIE at %s)"),
18317 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18318 }
18319 else
18320 {
18321 complaint (_("unsupported scale attribute %s for fixed-point type"
18322 " (DIE at %s)"),
18323 dwarf_attr_name (attr->name),
18324 sect_offset_str (die->sect_off));
18325 }
18326
2a12c336 18327 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18328 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18329 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18330 mpq_canonicalize (scaling_factor.val);
18331}
18332
bbcdf9ab
TT
18333/* The gnat-encoding suffix for fixed point. */
18334
18335#define GNAT_FIXED_POINT_SUFFIX "___XF_"
18336
18337/* If NAME encodes an Ada fixed-point type, return a pointer to the
18338 "XF" suffix of the name. The text after this is what encodes the
18339 'small and 'delta information. Otherwise, return nullptr. */
18340
18341static const char *
18342gnat_encoded_fixed_point_type_info (const char *name)
18343{
18344 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18345}
18346
9b790ce7
UW
18347/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18348 (which may be different from NAME) to the architecture back-end to allow
18349 it to guess the correct format if necessary. */
18350
18351static struct type *
18352dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18353 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18354{
08feed99 18355 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18356 const struct floatformat **format;
18357 struct type *type;
18358
18359 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18360 if (format)
103a685e 18361 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18362 else
77b7c781 18363 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18364
18365 return type;
18366}
18367
eb77c9df
AB
18368/* Allocate an integer type of size BITS and name NAME. */
18369
18370static struct type *
18371dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18372 int bits, int unsigned_p, const char *name)
18373{
18374 struct type *type;
18375
18376 /* Versions of Intel's C Compiler generate an integer type called "void"
18377 instead of using DW_TAG_unspecified_type. This has been seen on
18378 at least versions 14, 17, and 18. */
35ee2dc2
AB
18379 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18380 && strcmp (name, "void") == 0)
eb77c9df
AB
18381 type = objfile_type (objfile)->builtin_void;
18382 else
18383 type = init_integer_type (objfile, bits, unsigned_p, name);
18384
18385 return type;
18386}
18387
09584414
JB
18388/* Return true if DIE has a DW_AT_small attribute whose value is
18389 a constant rational, where both the numerator and denominator
18390 are equal to zero.
18391
18392 CU is the DIE's Compilation Unit. */
18393
18394static bool
18395has_zero_over_zero_small_attribute (struct die_info *die,
18396 struct dwarf2_cu *cu)
18397{
18398 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18399 if (attr == nullptr)
18400 return false;
18401
18402 struct dwarf2_cu *scale_cu = cu;
18403 struct die_info *scale_die
18404 = follow_die_ref (die, attr, &scale_cu);
18405
18406 if (scale_die->tag != DW_TAG_constant)
18407 return false;
18408
5cde1d82 18409 gdb_mpz num (1), denom (1);
09584414 18410 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18411 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18412}
18413
8bdc1658
AB
18414/* Initialise and return a floating point type of size BITS suitable for
18415 use as a component of a complex number. The NAME_HINT is passed through
18416 when initialising the floating point type and is the name of the complex
18417 type.
18418
18419 As DWARF doesn't currently provide an explicit name for the components
18420 of a complex number, but it can be helpful to have these components
18421 named, we try to select a suitable name based on the size of the
18422 component. */
18423static struct type *
18424dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18425 struct objfile *objfile,
103a685e
TT
18426 int bits, const char *name_hint,
18427 enum bfd_endian byte_order)
8bdc1658 18428{
08feed99 18429 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18430 struct type *tt = nullptr;
18431
35add35e
AB
18432 /* Try to find a suitable floating point builtin type of size BITS.
18433 We're going to use the name of this type as the name for the complex
18434 target type that we are about to create. */
1db455a7 18435 switch (cu->language)
8bdc1658 18436 {
1db455a7
AB
18437 case language_fortran:
18438 switch (bits)
18439 {
18440 case 32:
18441 tt = builtin_f_type (gdbarch)->builtin_real;
18442 break;
18443 case 64:
18444 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18445 break;
18446 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18447 case 128:
18448 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18449 break;
18450 }
8bdc1658 18451 break;
1db455a7
AB
18452 default:
18453 switch (bits)
18454 {
18455 case 32:
18456 tt = builtin_type (gdbarch)->builtin_float;
18457 break;
18458 case 64:
18459 tt = builtin_type (gdbarch)->builtin_double;
18460 break;
18461 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18462 case 128:
18463 tt = builtin_type (gdbarch)->builtin_long_double;
18464 break;
18465 }
8bdc1658
AB
18466 break;
18467 }
18468
35add35e
AB
18469 /* If the type we found doesn't match the size we were looking for, then
18470 pretend we didn't find a type at all, the complex target type we
18471 create will then be nameless. */
a12e5744 18472 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18473 tt = nullptr;
18474
7d93a1e0 18475 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18476 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18477}
18478
c906108c
SS
18479/* Find a representation of a given base type and install
18480 it in the TYPE field of the die. */
18481
f792889a 18482static struct type *
e7c27a73 18483read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18484{
5e22e966 18485 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18486 struct type *type;
18487 struct attribute *attr;
19f392bc 18488 int encoding = 0, bits = 0;
15d034d0 18489 const char *name;
34877895 18490 gdbarch *arch;
c906108c 18491
e142c38c 18492 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18493 if (attr != nullptr && attr->form_is_constant ())
18494 encoding = attr->constant_value (0);
e142c38c 18495 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18496 if (attr != nullptr)
529908cb 18497 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18498 name = dwarf2_name (die, cu);
6ccb9162 18499 if (!name)
34877895 18500 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18501
08feed99 18502 arch = objfile->arch ();
103a685e
TT
18503 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18504
34877895 18505 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18506 if (attr != nullptr && attr->form_is_constant ())
103a685e 18507 {
529908cb 18508 int endianity = attr->constant_value (0);
103a685e
TT
18509
18510 switch (endianity)
18511 {
18512 case DW_END_big:
18513 byte_order = BFD_ENDIAN_BIG;
18514 break;
18515 case DW_END_little:
18516 byte_order = BFD_ENDIAN_LITTLE;
18517 break;
18518 default:
18519 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18520 break;
18521 }
18522 }
6ccb9162 18523
09584414
JB
18524 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18525 && cu->language == language_ada
18526 && has_zero_over_zero_small_attribute (die, cu))
18527 {
18528 /* brobecker/2018-02-24: This is a fixed point type for which
18529 the scaling factor is represented as fraction whose value
18530 does not make sense (zero divided by zero), so we should
18531 normally never see these. However, there is a small category
18532 of fixed point types for which GNAT is unable to provide
18533 the scaling factor via the standard DWARF mechanisms, and
18534 for which the info is provided via the GNAT encodings instead.
bbcdf9ab 18535 This is likely what this DIE is about. */
09584414
JB
18536 encoding = (encoding == DW_ATE_signed_fixed
18537 ? DW_ATE_signed
18538 : DW_ATE_unsigned);
18539 }
18540
bbcdf9ab
TT
18541 /* With GNAT encodings, fixed-point information will be encoded in
18542 the type name. Note that this can also occur with the above
18543 zero-over-zero case, which is why this is a separate "if" rather
18544 than an "else if". */
18545 const char *gnat_encoding_suffix = nullptr;
18546 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18547 && cu->language == language_ada
18548 && name != nullptr)
18549 {
18550 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18551 if (gnat_encoding_suffix != nullptr)
18552 {
18553 gdb_assert (startswith (gnat_encoding_suffix,
18554 GNAT_FIXED_POINT_SUFFIX));
18555 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18556 name, gnat_encoding_suffix - name);
18557 /* Use -1 here so that SUFFIX points at the "_" after the
18558 "XF". */
18559 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18560
18561 encoding = (encoding == DW_ATE_signed
18562 ? DW_ATE_signed_fixed
18563 : DW_ATE_unsigned_fixed);
18564 }
18565 }
18566
6ccb9162 18567 switch (encoding)
c906108c 18568 {
6ccb9162
UW
18569 case DW_ATE_address:
18570 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18571 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18572 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18573 break;
18574 case DW_ATE_boolean:
19f392bc 18575 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18576 break;
18577 case DW_ATE_complex_float:
103a685e
TT
18578 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18579 byte_order);
78134374 18580 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18581 {
18582 if (name == nullptr)
18583 {
18584 struct obstack *obstack
5e22e966 18585 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18586 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18587 nullptr);
18588 }
18589 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18590 }
18591 else
18592 type = init_complex_type (name, type);
6ccb9162
UW
18593 break;
18594 case DW_ATE_decimal_float:
19f392bc 18595 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18596 break;
18597 case DW_ATE_float:
103a685e 18598 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18599 break;
18600 case DW_ATE_signed:
eb77c9df 18601 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18602 break;
18603 case DW_ATE_unsigned:
3b2b8fea
TT
18604 if (cu->language == language_fortran
18605 && name
61012eef 18606 && startswith (name, "character("))
19f392bc
UW
18607 type = init_character_type (objfile, bits, 1, name);
18608 else
eb77c9df 18609 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18610 break;
18611 case DW_ATE_signed_char:
6e70227d 18612 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18613 || cu->language == language_pascal
18614 || cu->language == language_fortran)
19f392bc
UW
18615 type = init_character_type (objfile, bits, 0, name);
18616 else
eb77c9df 18617 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18618 break;
18619 case DW_ATE_unsigned_char:
868a0084 18620 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18621 || cu->language == language_pascal
c44af4eb
TT
18622 || cu->language == language_fortran
18623 || cu->language == language_rust)
19f392bc
UW
18624 type = init_character_type (objfile, bits, 1, name);
18625 else
eb77c9df 18626 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18627 break;
75079b2b 18628 case DW_ATE_UTF:
53e710ac 18629 {
53e710ac
PA
18630 if (bits == 16)
18631 type = builtin_type (arch)->builtin_char16;
18632 else if (bits == 32)
18633 type = builtin_type (arch)->builtin_char32;
18634 else
18635 {
b98664d3 18636 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18637 bits);
eb77c9df 18638 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18639 }
18640 return set_die_type (die, type, cu);
18641 }
75079b2b 18642 break;
09584414
JB
18643 case DW_ATE_signed_fixed:
18644 type = init_fixed_point_type (objfile, bits, 0, name);
bbcdf9ab 18645 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414
JB
18646 break;
18647 case DW_ATE_unsigned_fixed:
18648 type = init_fixed_point_type (objfile, bits, 1, name);
bbcdf9ab 18649 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
09584414 18650 break;
75079b2b 18651
6ccb9162 18652 default:
b98664d3 18653 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18654 dwarf_type_encoding_name (encoding));
77b7c781 18655 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18656 break;
c906108c 18657 }
6ccb9162 18658
0114d602 18659 if (name && strcmp (name, "char") == 0)
15152a54 18660 type->set_has_no_signedness (true);
0114d602 18661
2b4424c3
TT
18662 maybe_set_alignment (cu, die, type);
18663
db558e34 18664 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18665
20a5fcbd
TT
18666 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18667 {
18668 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18669 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18670 {
529908cb 18671 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18672 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18673 /* Only use the attributes if they make sense together. */
18674 if (attr == nullptr
529908cb
TT
18675 || (attr->as_unsigned () + real_bit_size
18676 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18677 {
18678 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18679 = real_bit_size;
18680 if (attr != nullptr)
18681 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18682 = attr->as_unsigned ();
20a5fcbd
TT
18683 }
18684 }
18685 }
18686
f792889a 18687 return set_die_type (die, type, cu);
c906108c
SS
18688}
18689
80180f79
SA
18690/* Parse dwarf attribute if it's a block, reference or constant and put the
18691 resulting value of the attribute into struct bound_prop.
18692 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18693
18694static int
18695attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18696 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18697 struct type *default_type)
80180f79
SA
18698{
18699 struct dwarf2_property_baton *baton;
5e22e966 18700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18701 struct objfile *objfile = per_objfile->objfile;
18702 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18703
9a49df9d
AB
18704 gdb_assert (default_type != NULL);
18705
80180f79
SA
18706 if (attr == NULL || prop == NULL)
18707 return 0;
18708
4fc6c0d5 18709 if (attr->form_is_block ())
80180f79 18710 {
8d749320 18711 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18712 baton->property_type = default_type;
80180f79 18713 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18714 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18715
18716 struct dwarf_block *block = attr->as_block ();
18717 baton->locexpr.size = block->size;
18718 baton->locexpr.data = block->data;
216a7e6b
AB
18719 switch (attr->name)
18720 {
18721 case DW_AT_string_length:
18722 baton->locexpr.is_reference = true;
18723 break;
18724 default:
18725 baton->locexpr.is_reference = false;
18726 break;
18727 }
8c2e4e06
SM
18728
18729 prop->set_locexpr (baton);
18730 gdb_assert (prop->baton () != NULL);
80180f79 18731 }
cd6c91b4 18732 else if (attr->form_is_ref ())
80180f79
SA
18733 {
18734 struct dwarf2_cu *target_cu = cu;
18735 struct die_info *target_die;
18736 struct attribute *target_attr;
18737
18738 target_die = follow_die_ref (die, attr, &target_cu);
18739 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18740 if (target_attr == NULL)
18741 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18742 target_cu);
80180f79
SA
18743 if (target_attr == NULL)
18744 return 0;
18745
df25ebbd 18746 switch (target_attr->name)
80180f79 18747 {
df25ebbd 18748 case DW_AT_location:
cd6c91b4 18749 if (target_attr->form_is_section_offset ())
df25ebbd 18750 {
8d749320 18751 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18752 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18753 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18754 prop->set_loclist (baton);
18755 gdb_assert (prop->baton () != NULL);
df25ebbd 18756 }
4fc6c0d5 18757 else if (target_attr->form_is_block ())
df25ebbd 18758 {
8d749320 18759 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18760 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18761 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18762 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18763 struct dwarf_block *block = target_attr->as_block ();
18764 baton->locexpr.size = block->size;
18765 baton->locexpr.data = block->data;
9a49df9d 18766 baton->locexpr.is_reference = true;
8c2e4e06
SM
18767 prop->set_locexpr (baton);
18768 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18769 }
18770 else
18771 {
18772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18773 "dynamic property");
18774 return 0;
18775 }
18776 break;
18777 case DW_AT_data_member_location:
18778 {
18779 LONGEST offset;
18780
18781 if (!handle_data_member_location (target_die, target_cu,
18782 &offset))
18783 return 0;
18784
8d749320 18785 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18786 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18787 target_cu);
df25ebbd
JB
18788 baton->offset_info.offset = offset;
18789 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18790 prop->set_addr_offset (baton);
df25ebbd
JB
18791 break;
18792 }
80180f79
SA
18793 }
18794 }
cd6c91b4 18795 else if (attr->form_is_constant ())
8c2e4e06 18796 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18797 else
18798 {
18799 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18800 dwarf2_name (die, cu));
18801 return 0;
18802 }
18803
18804 return 1;
18805}
18806
09ba997f 18807/* See read.h. */
9a49df9d 18808
09ba997f 18809struct type *
293e7e51 18810dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18811{
9a49df9d
AB
18812 struct type *int_type;
18813
18814 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18815#define TRY_TYPE(F) \
18816 int_type = (unsigned_p \
18817 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18818 : objfile_type (objfile)->builtin_ ## F); \
18819 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18820 return int_type
18821
18822 TRY_TYPE (char);
18823 TRY_TYPE (short);
18824 TRY_TYPE (int);
18825 TRY_TYPE (long);
18826 TRY_TYPE (long_long);
18827
18828#undef TRY_TYPE
18829
18830 gdb_assert_not_reached ("unable to find suitable integer type");
18831}
18832
09ba997f 18833/* See read.h. */
11a8b164 18834
09ba997f 18835struct type *
293e7e51 18836dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18837{
293e7e51
SM
18838 int addr_size = this->per_cu->addr_size ();
18839 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18840}
18841
b86352cf
AB
18842/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18843 present (which is valid) then compute the default type based on the
18844 compilation units address size. */
18845
18846static struct type *
18847read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18848{
18849 struct type *index_type = die_type (die, cu);
18850
18851 /* Dwarf-2 specifications explicitly allows to create subrange types
18852 without specifying a base type.
18853 In that case, the base type must be set to the type of
18854 the lower bound, upper bound or count, in that order, if any of these
18855 three attributes references an object that has a type.
18856 If no base type is found, the Dwarf-2 specifications say that
18857 a signed integer type of size equal to the size of an address should
18858 be used.
18859 For the following C code: `extern char gdb_int [];'
18860 GCC produces an empty range DIE.
18861 FIXME: muller/2010-05-28: Possible references to object for low bound,
18862 high bound or count are not yet handled by this code. */
78134374 18863 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18864 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18865
18866 return index_type;
18867}
18868
a02abb62
JB
18869/* Read the given DW_AT_subrange DIE. */
18870
f792889a 18871static struct type *
a02abb62
JB
18872read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18873{
4c9ad8c2 18874 struct type *base_type, *orig_base_type;
a02abb62
JB
18875 struct type *range_type;
18876 struct attribute *attr;
729efb13 18877 struct dynamic_prop low, high;
4fae6e18 18878 int low_default_is_valid;
c451ebe5 18879 int high_bound_is_count = 0;
15d034d0 18880 const char *name;
d359392f 18881 ULONGEST negative_mask;
e77813c8 18882
b86352cf
AB
18883 orig_base_type = read_subrange_index_type (die, cu);
18884
4c9ad8c2
TT
18885 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18886 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18887 creating the range type, but we use the result of check_typedef
18888 when examining properties of the type. */
18889 base_type = check_typedef (orig_base_type);
a02abb62 18890
7e314c57
JK
18891 /* The die_type call above may have already set the type for this DIE. */
18892 range_type = get_die_type (die, cu);
18893 if (range_type)
18894 return range_type;
18895
8c2e4e06 18896 high.set_const_val (0);
729efb13 18897
4fae6e18
JK
18898 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18899 omitting DW_AT_lower_bound. */
18900 switch (cu->language)
6e70227d 18901 {
4fae6e18
JK
18902 case language_c:
18903 case language_cplus:
8c2e4e06 18904 low.set_const_val (0);
4fae6e18
JK
18905 low_default_is_valid = 1;
18906 break;
18907 case language_fortran:
8c2e4e06 18908 low.set_const_val (1);
4fae6e18
JK
18909 low_default_is_valid = 1;
18910 break;
18911 case language_d:
4fae6e18 18912 case language_objc:
c44af4eb 18913 case language_rust:
8c2e4e06 18914 low.set_const_val (0);
4fae6e18
JK
18915 low_default_is_valid = (cu->header.version >= 4);
18916 break;
18917 case language_ada:
18918 case language_m2:
18919 case language_pascal:
8c2e4e06 18920 low.set_const_val (1);
4fae6e18
JK
18921 low_default_is_valid = (cu->header.version >= 4);
18922 break;
18923 default:
8c2e4e06 18924 low.set_const_val (0);
4fae6e18
JK
18925 low_default_is_valid = 0;
18926 break;
a02abb62
JB
18927 }
18928
e142c38c 18929 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18930 if (attr != nullptr)
9a49df9d 18931 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18932 else if (!low_default_is_valid)
b98664d3 18933 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18934 "- DIE at %s [in module %s]"),
18935 sect_offset_str (die->sect_off),
5e22e966 18936 objfile_name (cu->per_objfile->objfile));
a02abb62 18937
506f5c41
TV
18938 struct attribute *attr_ub, *attr_count;
18939 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18940 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18941 {
506f5c41 18942 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18943 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18944 {
c451ebe5 18945 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18946 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18947 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18948 else
18949 high_bound_is_count = 1;
c2ff108b 18950 }
506f5c41
TV
18951 else
18952 {
18953 if (attr_ub != NULL)
18954 complaint (_("Unresolved DW_AT_upper_bound "
18955 "- DIE at %s [in module %s]"),
18956 sect_offset_str (die->sect_off),
5e22e966 18957 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18958 if (attr_count != NULL)
18959 complaint (_("Unresolved DW_AT_count "
18960 "- DIE at %s [in module %s]"),
18961 sect_offset_str (die->sect_off),
5e22e966 18962 objfile_name (cu->per_objfile->objfile));
506f5c41 18963 }
e77813c8 18964 }
a02abb62 18965
4e962e74
TT
18966 LONGEST bias = 0;
18967 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18968 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18969 bias = bias_attr->constant_value (0);
4e962e74 18970
dbb9c2b1
JB
18971 /* Normally, the DWARF producers are expected to use a signed
18972 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18973 But this is unfortunately not always the case, as witnessed
18974 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18975 is used instead. To work around that ambiguity, we treat
18976 the bounds as signed, and thus sign-extend their values, when
18977 the base type is signed. */
6e70227d 18978 negative_mask =
d359392f 18979 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18980 if (low.kind () == PROP_CONST
c6d940a9 18981 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18982 low.set_const_val (low.const_val () | negative_mask);
18983 if (high.kind () == PROP_CONST
c6d940a9 18984 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18985 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18986
5bbd8269
AB
18987 /* Check for bit and byte strides. */
18988 struct dynamic_prop byte_stride_prop;
18989 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18990 if (attr_byte_stride != nullptr)
18991 {
293e7e51 18992 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18993 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18994 prop_type);
18995 }
18996
18997 struct dynamic_prop bit_stride_prop;
18998 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18999 if (attr_bit_stride != nullptr)
19000 {
19001 /* It only makes sense to have either a bit or byte stride. */
19002 if (attr_byte_stride != nullptr)
19003 {
19004 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19005 "- DIE at %s [in module %s]"),
19006 sect_offset_str (die->sect_off),
5e22e966 19007 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19008 attr_bit_stride = nullptr;
19009 }
19010 else
19011 {
293e7e51 19012 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19013 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19014 prop_type);
19015 }
19016 }
19017
19018 if (attr_byte_stride != nullptr
19019 || attr_bit_stride != nullptr)
19020 {
19021 bool byte_stride_p = (attr_byte_stride != nullptr);
19022 struct dynamic_prop *stride
19023 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19024
19025 range_type
19026 = create_range_type_with_stride (NULL, orig_base_type, &low,
19027 &high, bias, stride, byte_stride_p);
19028 }
19029 else
19030 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19031
c451ebe5 19032 if (high_bound_is_count)
599088e3 19033 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19034
c2ff108b
JK
19035 /* Ada expects an empty array on no boundary attributes. */
19036 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19037 range_type->bounds ()->high.set_undefined ();
c2ff108b 19038
39cbfefa
DJ
19039 name = dwarf2_name (die, cu);
19040 if (name)
d0e39ea2 19041 range_type->set_name (name);
6e70227d 19042
e142c38c 19043 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19044 if (attr != nullptr)
529908cb 19045 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19046
2b4424c3
TT
19047 maybe_set_alignment (cu, die, range_type);
19048
7e314c57
JK
19049 set_die_type (die, range_type, cu);
19050
19051 /* set_die_type should be already done. */
b4ba55a1
JB
19052 set_descriptive_type (range_type, die, cu);
19053
7e314c57 19054 return range_type;
a02abb62 19055}
6e70227d 19056
f792889a 19057static struct type *
81a17f79
JB
19058read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19059{
19060 struct type *type;
81a17f79 19061
5e22e966 19062 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19063 type->set_name (dwarf2_name (die, cu));
81a17f79 19064
74a2f8ff 19065 /* In Ada, an unspecified type is typically used when the description
85102364 19066 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19067 such a type, we treat it as a stub, and try to resolve it later on,
19068 when needed. */
19069 if (cu->language == language_ada)
b4b73759 19070 type->set_is_stub (true);
74a2f8ff 19071
f792889a 19072 return set_die_type (die, type, cu);
81a17f79 19073}
a02abb62 19074
639d11d3
DC
19075/* Read a single die and all its descendents. Set the die's sibling
19076 field to NULL; set other fields in the die correctly, and set all
19077 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19078 location of the info_ptr after reading all of those dies. PARENT
19079 is the parent of the die in question. */
19080
19081static struct die_info *
dee91e82 19082read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19083 const gdb_byte *info_ptr,
19084 const gdb_byte **new_info_ptr,
dee91e82 19085 struct die_info *parent)
639d11d3
DC
19086{
19087 struct die_info *die;
d521ce57 19088 const gdb_byte *cur_ptr;
639d11d3 19089
3e225074 19090 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19091 if (die == NULL)
19092 {
19093 *new_info_ptr = cur_ptr;
19094 return NULL;
19095 }
93311388 19096 store_in_ref_table (die, reader->cu);
639d11d3 19097
3e225074 19098 if (die->has_children)
bf6af496 19099 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19100 else
19101 {
19102 die->child = NULL;
19103 *new_info_ptr = cur_ptr;
19104 }
19105
19106 die->sibling = NULL;
19107 die->parent = parent;
19108 return die;
19109}
19110
19111/* Read a die, all of its descendents, and all of its siblings; set
19112 all of the fields of all of the dies correctly. Arguments are as
19113 in read_die_and_children. */
19114
19115static struct die_info *
bf6af496 19116read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19117 const gdb_byte *info_ptr,
19118 const gdb_byte **new_info_ptr,
bf6af496 19119 struct die_info *parent)
639d11d3
DC
19120{
19121 struct die_info *first_die, *last_sibling;
d521ce57 19122 const gdb_byte *cur_ptr;
639d11d3 19123
c906108c 19124 cur_ptr = info_ptr;
639d11d3
DC
19125 first_die = last_sibling = NULL;
19126
19127 while (1)
c906108c 19128 {
639d11d3 19129 struct die_info *die
dee91e82 19130 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19131
1d325ec1 19132 if (die == NULL)
c906108c 19133 {
639d11d3
DC
19134 *new_info_ptr = cur_ptr;
19135 return first_die;
c906108c 19136 }
1d325ec1
DJ
19137
19138 if (!first_die)
19139 first_die = die;
c906108c 19140 else
1d325ec1
DJ
19141 last_sibling->sibling = die;
19142
19143 last_sibling = die;
c906108c 19144 }
c906108c
SS
19145}
19146
bf6af496
DE
19147/* Read a die, all of its descendents, and all of its siblings; set
19148 all of the fields of all of the dies correctly. Arguments are as
19149 in read_die_and_children.
19150 This the main entry point for reading a DIE and all its children. */
19151
19152static struct die_info *
19153read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19154 const gdb_byte *info_ptr,
19155 const gdb_byte **new_info_ptr,
bf6af496
DE
19156 struct die_info *parent)
19157{
19158 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19159 new_info_ptr, parent);
19160
b4f54984 19161 if (dwarf_die_debug)
bf6af496
DE
19162 {
19163 fprintf_unfiltered (gdb_stdlog,
19164 "Read die from %s@0x%x of %s:\n",
96b79293 19165 reader->die_section->get_name (),
bf6af496
DE
19166 (unsigned) (info_ptr - reader->die_section->buffer),
19167 bfd_get_filename (reader->abfd));
b4f54984 19168 dump_die (die, dwarf_die_debug);
bf6af496
DE
19169 }
19170
19171 return die;
19172}
19173
3019eac3
DE
19174/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19175 attributes.
19176 The caller is responsible for filling in the extra attributes
19177 and updating (*DIEP)->num_attrs.
19178 Set DIEP to point to a newly allocated die with its information,
3e225074 19179 except for its child, sibling, and parent fields. */
93311388 19180
d521ce57 19181static const gdb_byte *
3019eac3 19182read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19183 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19184 int num_extra_attrs)
93311388 19185{
b64f50a1 19186 unsigned int abbrev_number, bytes_read, i;
7c32eebb 19187 const struct abbrev_info *abbrev;
93311388
DE
19188 struct die_info *die;
19189 struct dwarf2_cu *cu = reader->cu;
19190 bfd *abfd = reader->abfd;
19191
9c541725 19192 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19193 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19194 info_ptr += bytes_read;
19195 if (!abbrev_number)
19196 {
19197 *diep = NULL;
93311388
DE
19198 return info_ptr;
19199 }
19200
685af9cd 19201 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19202 if (!abbrev)
348e048f
DE
19203 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19204 abbrev_number,
19205 bfd_get_filename (abfd));
19206
3019eac3 19207 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19208 die->sect_off = sect_off;
93311388
DE
19209 die->tag = abbrev->tag;
19210 die->abbrev = abbrev_number;
3e225074 19211 die->has_children = abbrev->has_children;
93311388 19212
3019eac3
DE
19213 /* Make the result usable.
19214 The caller needs to update num_attrs after adding the extra
19215 attributes. */
93311388
DE
19216 die->num_attrs = abbrev->num_attrs;
19217
7a5f294d 19218 bool any_need_reprocess = false;
93311388 19219 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19220 {
7a5f294d
TT
19221 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19222 info_ptr);
19223 if (die->attrs[i].requires_reprocessing_p ())
19224 any_need_reprocess = true;
18a8505e
AT
19225 }
19226
052c8bb8 19227 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19228 if (attr != nullptr && attr->form_is_unsigned ())
19229 cu->str_offsets_base = attr->as_unsigned ();
93311388 19230
41144253 19231 attr = die->attr (DW_AT_loclists_base);
19232 if (attr != nullptr)
529908cb 19233 cu->loclist_base = attr->as_unsigned ();
41144253 19234
a39fdb41 19235 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19236 if (maybe_addr_base.has_value ())
19237 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19238
19239 attr = die->attr (DW_AT_rnglists_base);
19240 if (attr != nullptr)
2b0c7f41 19241 cu->rnglists_base = attr->as_unsigned ();
d0ce17d8 19242
7a5f294d
TT
19243 if (any_need_reprocess)
19244 {
19245 for (i = 0; i < abbrev->num_attrs; ++i)
19246 {
19247 if (die->attrs[i].requires_reprocessing_p ())
19248 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19249 }
19250 }
93311388 19251 *diep = die;
93311388
DE
19252 return info_ptr;
19253}
19254
3019eac3
DE
19255/* Read a die and all its attributes.
19256 Set DIEP to point to a newly allocated die with its information,
3e225074 19257 except for its child, sibling, and parent fields. */
3019eac3 19258
d521ce57 19259static const gdb_byte *
3019eac3 19260read_full_die (const struct die_reader_specs *reader,
3e225074 19261 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19262{
d521ce57 19263 const gdb_byte *result;
bf6af496 19264
3e225074 19265 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19266
b4f54984 19267 if (dwarf_die_debug)
bf6af496
DE
19268 {
19269 fprintf_unfiltered (gdb_stdlog,
19270 "Read die from %s@0x%x of %s:\n",
96b79293 19271 reader->die_section->get_name (),
bf6af496
DE
19272 (unsigned) (info_ptr - reader->die_section->buffer),
19273 bfd_get_filename (reader->abfd));
b4f54984 19274 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19275 }
19276
19277 return result;
3019eac3 19278}
433df2d4 19279\f
c906108c 19280
72bf9492
DJ
19281/* Returns nonzero if TAG represents a type that we might generate a partial
19282 symbol for. */
19283
19284static int
d8f62e84 19285is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19286{
19287 switch (tag)
19288 {
19289#if 0
19290 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19291 that we don't at present. Note that normally this does not
19292 matter, mainly because C compilers don't give names to these
19293 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19294 case DW_TAG_file_type:
19295 case DW_TAG_ptr_to_member_type:
19296 case DW_TAG_set_type:
19297 case DW_TAG_string_type:
19298 case DW_TAG_subroutine_type:
19299#endif
d8f62e84
TT
19300
19301 /* GNAT may emit an array with a name, but no typedef, so we
19302 need to make a symbol in this case. */
19303 case DW_TAG_array_type:
19304 return lang == language_ada;
19305
72bf9492
DJ
19306 case DW_TAG_base_type:
19307 case DW_TAG_class_type:
680b30c7 19308 case DW_TAG_interface_type:
72bf9492
DJ
19309 case DW_TAG_enumeration_type:
19310 case DW_TAG_structure_type:
19311 case DW_TAG_subrange_type:
19312 case DW_TAG_typedef:
19313 case DW_TAG_union_type:
19314 return 1;
19315 default:
19316 return 0;
19317 }
19318}
19319
19320/* Load all DIEs that are interesting for partial symbols into memory. */
19321
19322static struct partial_die_info *
dee91e82 19323load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19324 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19325{
dee91e82 19326 struct dwarf2_cu *cu = reader->cu;
5e22e966 19327 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19328 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19329 unsigned int bytes_read;
5afb4e99 19330 unsigned int load_all = 0;
72bf9492
DJ
19331 int nesting_level = 1;
19332
19333 parent_die = NULL;
19334 last_die = NULL;
19335
7adf1e79
DE
19336 gdb_assert (cu->per_cu != NULL);
19337 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19338 load_all = 1;
19339
72bf9492
DJ
19340 cu->partial_dies
19341 = htab_create_alloc_ex (cu->header.length / 12,
19342 partial_die_hash,
19343 partial_die_eq,
19344 NULL,
19345 &cu->comp_unit_obstack,
19346 hashtab_obstack_allocate,
19347 dummy_obstack_deallocate);
19348
72bf9492
DJ
19349 while (1)
19350 {
7c32eebb
TT
19351 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19352 &bytes_read);
72bf9492
DJ
19353
19354 /* A NULL abbrev means the end of a series of children. */
19355 if (abbrev == NULL)
19356 {
19357 if (--nesting_level == 0)
cd9983dd
YQ
19358 return first_die;
19359
72bf9492
DJ
19360 info_ptr += bytes_read;
19361 last_die = parent_die;
19362 parent_die = parent_die->die_parent;
19363 continue;
19364 }
19365
98bfdba5
PA
19366 /* Check for template arguments. We never save these; if
19367 they're seen, we just mark the parent, and go on our way. */
19368 if (parent_die != NULL
19369 && cu->language == language_cplus
19370 && (abbrev->tag == DW_TAG_template_type_param
19371 || abbrev->tag == DW_TAG_template_value_param))
19372 {
19373 parent_die->has_template_arguments = 1;
19374
19375 if (!load_all)
19376 {
19377 /* We don't need a partial DIE for the template argument. */
dee91e82 19378 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19379 continue;
19380 }
19381 }
19382
0d99eb77 19383 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19384 Skip their other children. */
19385 if (!load_all
19386 && cu->language == language_cplus
19387 && parent_die != NULL
f9b5d5ea
TV
19388 && parent_die->tag == DW_TAG_subprogram
19389 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19390 {
dee91e82 19391 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19392 continue;
19393 }
19394
5afb4e99
DJ
19395 /* Check whether this DIE is interesting enough to save. Normally
19396 we would not be interested in members here, but there may be
19397 later variables referencing them via DW_AT_specification (for
19398 static members). */
19399 if (!load_all
d8f62e84 19400 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19401 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19402 && abbrev->tag != DW_TAG_enumerator
19403 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19404 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19405 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19406 && abbrev->tag != DW_TAG_variable
5afb4e99 19407 && abbrev->tag != DW_TAG_namespace
f55ee35c 19408 && abbrev->tag != DW_TAG_module
95554aad 19409 && abbrev->tag != DW_TAG_member
74921315
KS
19410 && abbrev->tag != DW_TAG_imported_unit
19411 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19412 {
19413 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19414 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19415 continue;
19416 }
19417
6f06d47b
YQ
19418 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19419 abbrev);
cd9983dd 19420
48fbe735 19421 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19422
19423 /* This two-pass algorithm for processing partial symbols has a
19424 high cost in cache pressure. Thus, handle some simple cases
19425 here which cover the majority of C partial symbols. DIEs
19426 which neither have specification tags in them, nor could have
19427 specification tags elsewhere pointing at them, can simply be
19428 processed and discarded.
19429
19430 This segment is also optional; scan_partial_symbols and
19431 add_partial_symbol will handle these DIEs if we chain
19432 them in normally. When compilers which do not emit large
19433 quantities of duplicate debug information are more common,
19434 this code can probably be removed. */
19435
19436 /* Any complete simple types at the top level (pretty much all
19437 of them, for a language without namespaces), can be processed
19438 directly. */
19439 if (parent_die == NULL
cd9983dd
YQ
19440 && pdi.has_specification == 0
19441 && pdi.is_declaration == 0
19442 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19443 || pdi.tag == DW_TAG_base_type
d8f62e84 19444 || pdi.tag == DW_TAG_array_type
cd9983dd 19445 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19446 {
7d00ffec 19447 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19448 add_partial_symbol (&pdi, cu);
19449
cd9983dd 19450 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19451 continue;
19452 }
19453
d8228535
JK
19454 /* The exception for DW_TAG_typedef with has_children above is
19455 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19456 type_name_or_error will error on such types later.
d8228535
JK
19457
19458 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19459 it could not find the child DIEs referenced later, this is checked
19460 above. In correct DWARF DW_TAG_typedef should have no children. */
19461
cd9983dd 19462 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19463 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19464 "- DIE at %s [in module %s]"),
cd9983dd 19465 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19466
72bf9492
DJ
19467 /* If we're at the second level, and we're an enumerator, and
19468 our parent has no specification (meaning possibly lives in a
19469 namespace elsewhere), then we can add the partial symbol now
19470 instead of queueing it. */
cd9983dd 19471 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19472 && parent_die != NULL
19473 && parent_die->die_parent == NULL
19474 && parent_die->tag == DW_TAG_enumeration_type
19475 && parent_die->has_specification == 0)
19476 {
7d00ffec 19477 if (pdi.raw_name == NULL)
b98664d3 19478 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19479 else if (building_psymtab)
f0fbb768 19480 add_partial_symbol (&pdi, cu);
72bf9492 19481
cd9983dd 19482 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19483 continue;
19484 }
19485
cd9983dd 19486 struct partial_die_info *part_die
6f06d47b 19487 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19488
72bf9492
DJ
19489 /* We'll save this DIE so link it in. */
19490 part_die->die_parent = parent_die;
19491 part_die->die_sibling = NULL;
19492 part_die->die_child = NULL;
19493
19494 if (last_die && last_die == parent_die)
19495 last_die->die_child = part_die;
19496 else if (last_die)
19497 last_die->die_sibling = part_die;
19498
19499 last_die = part_die;
19500
19501 if (first_die == NULL)
19502 first_die = part_die;
19503
19504 /* Maybe add the DIE to the hash table. Not all DIEs that we
19505 find interesting need to be in the hash table, because we
19506 also have the parent/sibling/child chains; only those that we
19507 might refer to by offset later during partial symbol reading.
19508
19509 For now this means things that might have be the target of a
19510 DW_AT_specification, DW_AT_abstract_origin, or
19511 DW_AT_extension. DW_AT_extension will refer only to
19512 namespaces; DW_AT_abstract_origin refers to functions (and
19513 many things under the function DIE, but we do not recurse
19514 into function DIEs during partial symbol reading) and
19515 possibly variables as well; DW_AT_specification refers to
19516 declarations. Declarations ought to have the DW_AT_declaration
19517 flag. It happens that GCC forgets to put it in sometimes, but
19518 only for functions, not for types.
19519
19520 Adding more things than necessary to the hash table is harmless
19521 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19522 wasted time in find_partial_die, when we reread the compilation
19523 unit with load_all_dies set. */
72bf9492 19524
5afb4e99 19525 if (load_all
72929c62 19526 || abbrev->tag == DW_TAG_constant
5afb4e99 19527 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19528 || abbrev->tag == DW_TAG_variable
19529 || abbrev->tag == DW_TAG_namespace
19530 || part_die->is_declaration)
19531 {
19532 void **slot;
19533
19534 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19535 to_underlying (part_die->sect_off),
19536 INSERT);
72bf9492
DJ
19537 *slot = part_die;
19538 }
19539
72bf9492 19540 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19541 we have no reason to follow the children of structures; for other
98bfdba5
PA
19542 languages we have to, so that we can get at method physnames
19543 to infer fully qualified class names, for DW_AT_specification,
19544 and for C++ template arguments. For C++, we also look one level
19545 inside functions to find template arguments (if the name of the
19546 function does not already contain the template arguments).
bc30ff58 19547
0a4b0913
AB
19548 For Ada and Fortran, we need to scan the children of subprograms
19549 and lexical blocks as well because these languages allow the
19550 definition of nested entities that could be interesting for the
19551 debugger, such as nested subprograms for instance. */
72bf9492 19552 if (last_die->has_children
5afb4e99
DJ
19553 && (load_all
19554 || last_die->tag == DW_TAG_namespace
f55ee35c 19555 || last_die->tag == DW_TAG_module
72bf9492 19556 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19557 || (cu->language == language_cplus
19558 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19559 && (last_die->raw_name == NULL
19560 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19561 || (cu->language != language_c
19562 && (last_die->tag == DW_TAG_class_type
680b30c7 19563 || last_die->tag == DW_TAG_interface_type
72bf9492 19564 || last_die->tag == DW_TAG_structure_type
bc30ff58 19565 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19566 || ((cu->language == language_ada
19567 || cu->language == language_fortran)
bc30ff58
JB
19568 && (last_die->tag == DW_TAG_subprogram
19569 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19570 {
19571 nesting_level++;
19572 parent_die = last_die;
19573 continue;
19574 }
19575
19576 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19577 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19578
19579 /* Back to the top, do it again. */
19580 }
19581}
19582
6f06d47b 19583partial_die_info::partial_die_info (sect_offset sect_off_,
7c32eebb 19584 const struct abbrev_info *abbrev)
6f06d47b
YQ
19585 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19586{
19587}
19588
7d00ffec
TT
19589/* See class definition. */
19590
19591const char *
19592partial_die_info::name (dwarf2_cu *cu)
19593{
19594 if (!canonical_name && raw_name != nullptr)
19595 {
19596 struct objfile *objfile = cu->per_objfile->objfile;
19597 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19598 canonical_name = 1;
19599 }
19600
19601 return raw_name;
19602}
19603
35cc7ed7
YQ
19604/* Read a minimal amount of information into the minimal die structure.
19605 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19606
48fbe735
YQ
19607const gdb_byte *
19608partial_die_info::read (const struct die_reader_specs *reader,
19609 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19610{
dee91e82 19611 struct dwarf2_cu *cu = reader->cu;
976ca316 19612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19613 unsigned int i;
c5aa993b 19614 int has_low_pc_attr = 0;
c906108c 19615 int has_high_pc_attr = 0;
91da1414 19616 int high_pc_relative = 0;
c906108c 19617
fd0a254f 19618 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19619 {
e7da7f8f 19620 attribute attr;
7a5f294d 19621 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19622 /* String and address offsets that need to do the reprocessing have
dda83cd7 19623 already been read at this point, so there is no need to wait until
18a8505e 19624 the loop terminates to do the reprocessing. */
7a5f294d 19625 if (attr.requires_reprocessing_p ())
d0ce17d8 19626 read_attribute_reprocess (reader, &attr, tag);
c906108c 19627 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19628 partial symbol table. */
c906108c
SS
19629 switch (attr.name)
19630 {
19631 case DW_AT_name:
48fbe735 19632 switch (tag)
71c25dea
TT
19633 {
19634 case DW_TAG_compile_unit:
95554aad 19635 case DW_TAG_partial_unit:
348e048f 19636 case DW_TAG_type_unit:
71c25dea
TT
19637 /* Compilation units have a DW_AT_name that is a filename, not
19638 a source language identifier. */
19639 case DW_TAG_enumeration_type:
19640 case DW_TAG_enumerator:
19641 /* These tags always have simple identifiers already; no need
19642 to canonicalize them. */
7d00ffec 19643 canonical_name = 1;
2c830f54 19644 raw_name = attr.as_string ();
71c25dea
TT
19645 break;
19646 default:
7d00ffec 19647 canonical_name = 0;
2c830f54 19648 raw_name = attr.as_string ();
71c25dea
TT
19649 break;
19650 }
c906108c 19651 break;
31ef98ae 19652 case DW_AT_linkage_name:
c906108c 19653 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19654 /* Note that both forms of linkage name might appear. We
19655 assume they will be the same, and we only store the last
19656 one we see. */
95f982e5 19657 linkage_name = attr.as_string ();
c906108c
SS
19658 break;
19659 case DW_AT_low_pc:
19660 has_low_pc_attr = 1;
95f982e5 19661 lowpc = attr.as_address ();
c906108c
SS
19662 break;
19663 case DW_AT_high_pc:
19664 has_high_pc_attr = 1;
95f982e5 19665 highpc = attr.as_address ();
cd6c91b4 19666 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19667 high_pc_relative = 1;
c906108c
SS
19668 break;
19669 case DW_AT_location:
dda83cd7
SM
19670 /* Support the .debug_loc offsets. */
19671 if (attr.form_is_block ())
19672 {
9d2246fc 19673 d.locdesc = attr.as_block ();
dda83cd7
SM
19674 }
19675 else if (attr.form_is_section_offset ())
19676 {
4d3c2250 19677 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19678 }
19679 else
19680 {
4d3c2250
KB
19681 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19682 "partial symbol information");
dda83cd7 19683 }
c906108c 19684 break;
c906108c 19685 case DW_AT_external:
c45bc3f8 19686 is_external = attr.as_boolean ();
c906108c
SS
19687 break;
19688 case DW_AT_declaration:
c45bc3f8 19689 is_declaration = attr.as_boolean ();
c906108c
SS
19690 break;
19691 case DW_AT_type:
48fbe735 19692 has_type = 1;
c906108c
SS
19693 break;
19694 case DW_AT_abstract_origin:
19695 case DW_AT_specification:
72bf9492 19696 case DW_AT_extension:
48fbe735 19697 has_specification = 1;
0826b30a 19698 spec_offset = attr.get_ref_die_offset ();
48fbe735 19699 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19700 || cu->per_cu->is_dwz);
c906108c
SS
19701 break;
19702 case DW_AT_sibling:
19703 /* Ignore absolute siblings, they might point outside of
19704 the current compile unit. */
19705 if (attr.form == DW_FORM_ref_addr)
b98664d3 19706 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19707 else
b9502d3f 19708 {
48fbe735 19709 const gdb_byte *buffer = reader->buffer;
0826b30a 19710 sect_offset off = attr.get_ref_die_offset ();
9c541725 19711 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19712
19713 if (sibling_ptr < info_ptr)
b98664d3 19714 complaint (_("DW_AT_sibling points backwards"));
22869d73 19715 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19716 reader->die_section->overflow_complaint ();
b9502d3f 19717 else
48fbe735 19718 sibling = sibling_ptr;
b9502d3f 19719 }
c906108c 19720 break;
dda83cd7
SM
19721 case DW_AT_byte_size:
19722 has_byte_size = 1;
19723 break;
19724 case DW_AT_const_value:
19725 has_const_value = 1;
19726 break;
68511cec
CES
19727 case DW_AT_calling_convention:
19728 /* DWARF doesn't provide a way to identify a program's source-level
19729 entry point. DW_AT_calling_convention attributes are only meant
19730 to describe functions' calling conventions.
19731
19732 However, because it's a necessary piece of information in
0c1b455e
TT
19733 Fortran, and before DWARF 4 DW_CC_program was the only
19734 piece of debugging information whose definition refers to
19735 a 'main program' at all, several compilers marked Fortran
19736 main programs with DW_CC_program --- even when those
19737 functions use the standard calling conventions.
19738
19739 Although DWARF now specifies a way to provide this
19740 information, we support this practice for backward
19741 compatibility. */
529908cb 19742 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19743 && cu->language == language_fortran)
48fbe735 19744 main_subprogram = 1;
68511cec 19745 break;
481860b3 19746 case DW_AT_inline:
529908cb
TT
19747 {
19748 LONGEST value = attr.constant_value (-1);
19749 if (value == DW_INL_inlined
19750 || value == DW_INL_declared_inlined)
19751 may_be_inlined = 1;
19752 }
481860b3 19753 break;
95554aad
TT
19754
19755 case DW_AT_import:
48fbe735 19756 if (tag == DW_TAG_imported_unit)
36586728 19757 {
0826b30a 19758 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19759 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19760 || cu->per_cu->is_dwz);
19761 }
95554aad
TT
19762 break;
19763
0c1b455e 19764 case DW_AT_main_subprogram:
c45bc3f8 19765 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19766 break;
19767
05caa1d2
TT
19768 case DW_AT_ranges:
19769 {
2b0c7f41
SM
19770 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19771 on DWARF version). */
19772 ULONGEST ranges_offset = attr.as_unsigned ();
19773
19774 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19775 this value. */
19776 if (tag != DW_TAG_compile_unit)
19777 ranges_offset += cu->gnu_ranges_base;
19778
05caa1d2 19779 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19780 nullptr, tag))
05caa1d2
TT
19781 has_pc_info = 1;
19782 }
19783 break;
19784
c906108c
SS
19785 default:
19786 break;
19787 }
19788 }
19789
10d06d82
TT
19790 /* For Ada, if both the name and the linkage name appear, we prefer
19791 the latter. This lets "catch exception" work better, regardless
19792 of the order in which the name and linkage name were emitted.
19793 Really, though, this is just a workaround for the fact that gdb
19794 doesn't store both the name and the linkage name. */
19795 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19796 raw_name = linkage_name;
10d06d82 19797
91da1414 19798 if (high_pc_relative)
48fbe735 19799 highpc += lowpc;
91da1414 19800
9373cf26
JK
19801 if (has_low_pc_attr && has_high_pc_attr)
19802 {
19803 /* When using the GNU linker, .gnu.linkonce. sections are used to
19804 eliminate duplicate copies of functions and vtables and such.
19805 The linker will arbitrarily choose one and discard the others.
19806 The AT_*_pc values for such functions refer to local labels in
19807 these sections. If the section from that file was discarded, the
19808 labels are not in the output, so the relocs get a value of 0.
19809 If this is a discarded function, mark the pc bounds as invalid,
19810 so that GDB will ignore it. */
976ca316 19811 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19812 {
976ca316 19813 struct objfile *objfile = per_objfile->objfile;
08feed99 19814 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19815
b98664d3 19816 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19817 "for DIE at %s [in module %s]"),
48fbe735
YQ
19818 paddress (gdbarch, lowpc),
19819 sect_offset_str (sect_off),
9d8780f0 19820 objfile_name (objfile));
9373cf26
JK
19821 }
19822 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19823 else if (lowpc >= highpc)
9373cf26 19824 {
976ca316 19825 struct objfile *objfile = per_objfile->objfile;
08feed99 19826 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19827
b98664d3 19828 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19829 "for DIE at %s [in module %s]"),
48fbe735
YQ
19830 paddress (gdbarch, lowpc),
19831 paddress (gdbarch, highpc),
19832 sect_offset_str (sect_off),
9c541725 19833 objfile_name (objfile));
9373cf26
JK
19834 }
19835 else
48fbe735 19836 has_pc_info = 1;
9373cf26 19837 }
85cbf3d3 19838
c906108c
SS
19839 return info_ptr;
19840}
19841
72bf9492
DJ
19842/* Find a cached partial DIE at OFFSET in CU. */
19843
d590ff25
YQ
19844struct partial_die_info *
19845dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19846{
19847 struct partial_die_info *lookup_die = NULL;
6f06d47b 19848 struct partial_die_info part_die (sect_off);
72bf9492 19849
9a3c8263 19850 lookup_die = ((struct partial_die_info *)
d590ff25 19851 htab_find_with_hash (partial_dies, &part_die,
9c541725 19852 to_underlying (sect_off)));
72bf9492 19853
72bf9492
DJ
19854 return lookup_die;
19855}
19856
348e048f
DE
19857/* Find a partial DIE at OFFSET, which may or may not be in CU,
19858 except in the case of .debug_types DIEs which do not reference
19859 outside their CU (they do however referencing other types via
55f1336d 19860 DW_FORM_ref_sig8). */
72bf9492 19861
122cf0f2 19862static const struct cu_partial_die_info
9c541725 19863find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19864{
976ca316
SM
19865 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19866 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19867 struct partial_die_info *pd = NULL;
72bf9492 19868
36586728 19869 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19870 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19871 {
d590ff25 19872 pd = cu->find_partial_die (sect_off);
5afb4e99 19873 if (pd != NULL)
fb816e8b 19874 return { cu, pd };
0d99eb77
DE
19875 /* We missed recording what we needed.
19876 Load all dies and try again. */
5afb4e99 19877 }
0d99eb77
DE
19878 else
19879 {
19880 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19881 if (cu->per_cu->is_debug_types)
0d99eb77 19882 {
9d8780f0
SM
19883 error (_("Dwarf Error: Type Unit at offset %s contains"
19884 " external reference to offset %s [in module %s].\n"),
19885 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19886 bfd_get_filename (objfile->obfd));
19887 }
7188ed02
SM
19888 dwarf2_per_cu_data *per_cu
19889 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19890 per_objfile);
72bf9492 19891
976ca316 19892 cu = per_objfile->get_cu (per_cu);
7188ed02 19893 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19894 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19895
976ca316 19896 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19897
19898 cu->last_used = 0;
19899 pd = cu->find_partial_die (sect_off);
0d99eb77 19900 }
5afb4e99 19901
dee91e82
DE
19902 /* If we didn't find it, and not all dies have been loaded,
19903 load them all and try again. */
19904
7188ed02 19905 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19906 {
7188ed02 19907 cu->per_cu->load_all_dies = 1;
fd820528
DE
19908
19909 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19910 THIS_CU->cu may already be in use. So we can't just free it and
19911 replace its DIEs with the ones we read in. Instead, we leave those
19912 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19913 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19914 set. */
976ca316 19915 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19916
7188ed02 19917 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19918 }
19919
19920 if (pd == NULL)
521894aa 19921 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19922 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19923 return { cu, pd };
72bf9492
DJ
19924}
19925
abc72ce4
DE
19926/* See if we can figure out if the class lives in a namespace. We do
19927 this by looking for a member function; its demangled name will
19928 contain namespace info, if there is any. */
19929
19930static void
19931guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19932 struct dwarf2_cu *cu)
19933{
19934 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19935 what template types look like, because the demangler
19936 frequently doesn't give the same name as the debug info. We
19937 could fix this by only using the demangled name to get the
19938 prefix (but see comment in read_structure_type). */
19939
19940 struct partial_die_info *real_pdi;
19941 struct partial_die_info *child_pdi;
19942
19943 /* If this DIE (this DIE's specification, if any) has a parent, then
19944 we should not do this. We'll prepend the parent's fully qualified
19945 name when we create the partial symbol. */
19946
19947 real_pdi = struct_pdi;
19948 while (real_pdi->has_specification)
fb816e8b 19949 {
122cf0f2
AB
19950 auto res = find_partial_die (real_pdi->spec_offset,
19951 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19952 real_pdi = res.pdi;
19953 cu = res.cu;
19954 }
abc72ce4
DE
19955
19956 if (real_pdi->die_parent != NULL)
19957 return;
19958
19959 for (child_pdi = struct_pdi->die_child;
19960 child_pdi != NULL;
19961 child_pdi = child_pdi->die_sibling)
19962 {
19963 if (child_pdi->tag == DW_TAG_subprogram
19964 && child_pdi->linkage_name != NULL)
19965 {
43816ebc 19966 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19967 (cu->language_defn->class_name_from_physname
19968 (child_pdi->linkage_name));
abc72ce4
DE
19969 if (actual_class_name != NULL)
19970 {
5e22e966 19971 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19972 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19973 struct_pdi->canonical_name = 1;
abc72ce4
DE
19974 }
19975 break;
19976 }
19977 }
19978}
19979
25c11aca
TV
19980/* Return true if a DIE with TAG may have the DW_AT_const_value
19981 attribute. */
19982
19983static bool
19984can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19985{
19986 switch (tag)
19987 {
19988 case DW_TAG_constant:
19989 case DW_TAG_enumerator:
19990 case DW_TAG_formal_parameter:
19991 case DW_TAG_template_value_param:
19992 case DW_TAG_variable:
19993 return true;
19994 }
19995
19996 return false;
19997}
19998
52356b79
YQ
19999void
20000partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20001{
abc72ce4
DE
20002 /* Once we've fixed up a die, there's no point in doing so again.
20003 This also avoids a memory leak if we were to call
20004 guess_partial_die_structure_name multiple times. */
52356b79 20005 if (fixup_called)
abc72ce4
DE
20006 return;
20007
72bf9492
DJ
20008 /* If we found a reference attribute and the DIE has no name, try
20009 to find a name in the referred to DIE. */
20010
7d00ffec 20011 if (raw_name == NULL && has_specification)
72bf9492
DJ
20012 {
20013 struct partial_die_info *spec_die;
72bf9492 20014
122cf0f2 20015 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20016 spec_die = res.pdi;
20017 cu = res.cu;
72bf9492 20018
52356b79 20019 spec_die->fixup (cu);
72bf9492 20020
7d00ffec 20021 if (spec_die->raw_name)
72bf9492 20022 {
7d00ffec
TT
20023 raw_name = spec_die->raw_name;
20024 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20025
20026 /* Copy DW_AT_external attribute if it is set. */
20027 if (spec_die->is_external)
52356b79 20028 is_external = spec_die->is_external;
72bf9492
DJ
20029 }
20030 }
20031
25c11aca
TV
20032 if (!has_const_value && has_specification
20033 && can_have_DW_AT_const_value_p (tag))
20034 {
20035 struct partial_die_info *spec_die;
20036
20037 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20038 spec_die = res.pdi;
20039 cu = res.cu;
20040
20041 spec_die->fixup (cu);
20042
20043 if (spec_die->has_const_value)
20044 {
20045 /* Copy DW_AT_const_value attribute if it is set. */
20046 has_const_value = spec_die->has_const_value;
20047 }
20048 }
20049
72bf9492 20050 /* Set default names for some unnamed DIEs. */
72bf9492 20051
7d00ffec
TT
20052 if (raw_name == NULL && tag == DW_TAG_namespace)
20053 {
20054 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20055 canonical_name = 1;
20056 }
72bf9492 20057
abc72ce4
DE
20058 /* If there is no parent die to provide a namespace, and there are
20059 children, see if we can determine the namespace from their linkage
122d1940 20060 name. */
abc72ce4 20061 if (cu->language == language_cplus
5e22e966 20062 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20063 && die_parent == NULL
20064 && has_children
20065 && (tag == DW_TAG_class_type
20066 || tag == DW_TAG_structure_type
20067 || tag == DW_TAG_union_type))
20068 guess_partial_die_structure_name (this, cu);
abc72ce4 20069
53832f31
TT
20070 /* GCC might emit a nameless struct or union that has a linkage
20071 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20072 if (raw_name == NULL
52356b79
YQ
20073 && (tag == DW_TAG_class_type
20074 || tag == DW_TAG_interface_type
20075 || tag == DW_TAG_structure_type
20076 || tag == DW_TAG_union_type)
20077 && linkage_name != NULL)
53832f31 20078 {
43816ebc
TT
20079 gdb::unique_xmalloc_ptr<char> demangled
20080 (gdb_demangle (linkage_name, DMGL_TYPES));
20081 if (demangled != nullptr)
53832f31 20082 {
96408a79
SA
20083 const char *base;
20084
20085 /* Strip any leading namespaces/classes, keep only the base name.
20086 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20087 base = strrchr (demangled.get (), ':');
20088 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20089 base++;
20090 else
43816ebc 20091 base = demangled.get ();
96408a79 20092
5e22e966 20093 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20094 raw_name = objfile->intern (base);
20095 canonical_name = 1;
53832f31
TT
20096 }
20097 }
20098
52356b79 20099 fixup_called = 1;
72bf9492
DJ
20100}
20101
d0ce17d8 20102/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20103 contents from the given SECTION in the HEADER.
20104
20105 HEADER_OFFSET is the offset of the header in the section. */
41144253 20106static void
d0ce17d8 20107read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20108 struct dwarf2_section_info *section,
20109 sect_offset header_offset)
41144253 20110{
20111 unsigned int bytes_read;
20112 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20113 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20114
41144253 20115 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20116 info_ptr += bytes_read;
a1c40103 20117
41144253 20118 header->version = read_2_bytes (abfd, info_ptr);
20119 info_ptr += 2;
a1c40103 20120
41144253 20121 header->addr_size = read_1_byte (abfd, info_ptr);
20122 info_ptr += 1;
a1c40103 20123
41144253 20124 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20125 info_ptr += 1;
a1c40103 20126
41144253 20127 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20128}
20129
20130/* Return the DW_AT_loclists_base value for the CU. */
20131static ULONGEST
20132lookup_loclist_base (struct dwarf2_cu *cu)
20133{
20134 /* For the .dwo unit, the loclist_base points to the first offset following
20135 the header. The header consists of the following entities-
20136 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20137 bit format)
20138 2. version (2 bytes)
20139 3. address size (1 byte)
20140 4. segment selector size (1 byte)
20141 5. offset entry count (4 bytes)
20142 These sizes are derived as per the DWARFv5 standard. */
20143 if (cu->dwo_unit != nullptr)
20144 {
20145 if (cu->header.initial_length_size == 4)
20146 return LOCLIST_HEADER_SIZE32;
20147 return LOCLIST_HEADER_SIZE64;
20148 }
20149 return cu->loclist_base;
20150}
20151
20152/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20153 array of offsets in the .debug_loclists section. */
e57933dc
SM
20154
20155static sect_offset
41144253 20156read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20157{
976ca316
SM
20158 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20159 struct objfile *objfile = per_objfile->objfile;
41144253 20160 bfd *abfd = objfile->obfd;
a1c40103
SM
20161 ULONGEST loclist_header_size =
20162 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20163 : LOCLIST_HEADER_SIZE64);
41144253 20164 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20165
20166 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20167 ULONGEST start_offset =
20168 loclist_base + loclist_index * cu->header.offset_size;
20169
a1c40103 20170 /* Get loclists section. */
41144253 20171 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20172
a1c40103 20173 /* Read the loclists section content. */
41144253 20174 section->read (objfile);
20175 if (section->buffer == NULL)
a0c1eeba
SM
20176 error (_("DW_FORM_loclistx used without .debug_loclists "
20177 "section [in module %s]"), objfile_name (objfile));
20178
a1c40103
SM
20179 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20180 so if loclist_base is smaller than the header size, we have a problem. */
20181 if (loclist_base < loclist_header_size)
20182 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20183 objfile_name (objfile));
20184
20185 /* Read the header of the loclists contribution. */
d0ce17d8 20186 struct loclists_rnglists_header header;
a1c40103
SM
20187 read_loclists_rnglists_header (&header, section,
20188 (sect_offset) (loclist_base - loclist_header_size));
20189
20190 /* Verify the loclist index is valid. */
41144253 20191 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20192 error (_("DW_FORM_loclistx pointing outside of "
20193 ".debug_loclists offset array [in module %s]"),
20194 objfile_name (objfile));
20195
05787bad
SM
20196 /* Validate that reading won't go beyond the end of the section. */
20197 if (start_offset + cu->header.offset_size > section->size)
20198 error (_("Reading DW_FORM_loclistx index beyond end of"
20199 ".debug_loclists section [in module %s]"),
20200 objfile_name (objfile));
20201
20202 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20203
20204 if (cu->header.offset_size == 4)
e57933dc 20205 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
41144253 20206 else
e57933dc 20207 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
41144253 20208}
20209
d0ce17d8
CT
20210/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20211 array of offsets in the .debug_rnglists section. */
e57933dc
SM
20212
20213static sect_offset
d0ce17d8
CT
20214read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20215 dwarf_tag tag)
20216{
20217 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20218 struct objfile *objfile = dwarf2_per_objfile->objfile;
20219 bfd *abfd = objfile->obfd;
20220 ULONGEST rnglist_header_size =
20221 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20222 : RNGLIST_HEADER_SIZE64);
2b0c7f41
SM
20223
20224 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20225 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20226 doesn't apply. */
d0ce17d8 20227 ULONGEST rnglist_base =
2b0c7f41 20228 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
5e4d9bbc
SM
20229
20230 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20231 ULONGEST start_offset =
20232 rnglist_base + rnglist_index * cu->header.offset_size;
20233
20234 /* Get rnglists section. */
20235 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20236
20237 /* Read the rnglists section content. */
20238 section->read (objfile);
20239 if (section->buffer == nullptr)
20240 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20241 "[in module %s]"),
20242 objfile_name (objfile));
20243
a1c40103
SM
20244 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20245 so if rnglist_base is smaller than the header size, we have a problem. */
20246 if (rnglist_base < rnglist_header_size)
20247 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20248 objfile_name (objfile));
20249
20250 /* Read the header of the rnglists contribution. */
d0ce17d8 20251 struct loclists_rnglists_header header;
a1c40103
SM
20252 read_loclists_rnglists_header (&header, section,
20253 (sect_offset) (rnglist_base - rnglist_header_size));
20254
20255 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20256 if (rnglist_index >= header.offset_entry_count)
20257 error (_("DW_FORM_rnglistx index pointing outside of "
20258 ".debug_rnglists offset array [in module %s]"),
20259 objfile_name (objfile));
20260
d0ce17d8 20261 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20262 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20263 error (_("Reading DW_FORM_rnglistx index beyond end of"
20264 ".debug_rnglists section [in module %s]"),
20265 objfile_name (objfile));
20266
20267 const gdb_byte *info_ptr = section->buffer + start_offset;
20268
20269 if (cu->header.offset_size == 4)
e57933dc 20270 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8 20271 else
e57933dc 20272 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
d0ce17d8
CT
20273}
20274
18a8505e
AT
20275/* Process the attributes that had to be skipped in the first round. These
20276 attributes are the ones that need str_offsets_base or addr_base attributes.
20277 They could not have been processed in the first round, because at the time
20278 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20279static void
20280read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20281 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20282{
20283 struct dwarf2_cu *cu = reader->cu;
20284 switch (attr->form)
20285 {
20286 case DW_FORM_addrx:
20287 case DW_FORM_GNU_addr_index:
36d378cf
TT
20288 attr->set_address (read_addr_index (cu,
20289 attr->as_unsigned_reprocess ()));
dda83cd7 20290 break;
41144253 20291 case DW_FORM_loclistx:
b1829e1b 20292 {
e57933dc 20293 sect_offset loclists_sect_off
b1829e1b
SM
20294 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20295
e57933dc 20296 attr->set_unsigned (to_underlying (loclists_sect_off));
b1829e1b
SM
20297 }
20298 break;
d0ce17d8 20299 case DW_FORM_rnglistx:
b1829e1b 20300 {
e57933dc 20301 sect_offset rnglists_sect_off
b1829e1b
SM
20302 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20303
e57933dc 20304 attr->set_unsigned (to_underlying (rnglists_sect_off));
b1829e1b 20305 }
dda83cd7 20306 break;
18a8505e
AT
20307 case DW_FORM_strx:
20308 case DW_FORM_strx1:
20309 case DW_FORM_strx2:
20310 case DW_FORM_strx3:
20311 case DW_FORM_strx4:
20312 case DW_FORM_GNU_str_index:
20313 {
fe56917a 20314 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20315 gdb_assert (!attr->canonical_string_p ());
18a8505e 20316 if (reader->dwo_file != NULL)
c6481205
TT
20317 attr->set_string_noncanonical (read_dwo_str_index (reader,
20318 str_index));
18a8505e 20319 else
c6481205
TT
20320 attr->set_string_noncanonical (read_stub_str_index (cu,
20321 str_index));
18a8505e
AT
20322 break;
20323 }
20324 default:
20325 gdb_assert_not_reached (_("Unexpected DWARF form."));
20326 }
20327}
20328
a8329558 20329/* Read an attribute value described by an attribute form. */
c906108c 20330
d521ce57 20331static const gdb_byte *
dee91e82
DE
20332read_attribute_value (const struct die_reader_specs *reader,
20333 struct attribute *attr, unsigned form,
7a5f294d 20334 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20335{
dee91e82 20336 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20337 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20338 struct objfile *objfile = per_objfile->objfile;
dee91e82 20339 bfd *abfd = reader->abfd;
e7c27a73 20340 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20341 unsigned int bytes_read;
20342 struct dwarf_block *blk;
20343
aead7601 20344 attr->form = (enum dwarf_form) form;
a8329558 20345 switch (form)
c906108c 20346 {
c906108c 20347 case DW_FORM_ref_addr:
7c290a04
TT
20348 if (cu_header->version == 2)
20349 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
414ad644 20350 &bytes_read));
ae411497 20351 else
7c290a04 20352 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20353 &bytes_read));
ae411497
TT
20354 info_ptr += bytes_read;
20355 break;
36586728 20356 case DW_FORM_GNU_ref_alt:
7c290a04 20357 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20358 &bytes_read));
36586728
TT
20359 info_ptr += bytes_read;
20360 break;
ae411497 20361 case DW_FORM_addr:
08feed99
TT
20362 {
20363 struct gdbarch *gdbarch = objfile->arch ();
7c290a04 20364 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
36d378cf
TT
20365 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20366 attr->set_address (addr);
08feed99
TT
20367 info_ptr += bytes_read;
20368 }
c906108c
SS
20369 break;
20370 case DW_FORM_block2:
7b5a2f43 20371 blk = dwarf_alloc_block (cu);
c906108c
SS
20372 blk->size = read_2_bytes (abfd, info_ptr);
20373 info_ptr += 2;
20374 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20375 info_ptr += blk->size;
9d2246fc 20376 attr->set_block (blk);
c906108c
SS
20377 break;
20378 case DW_FORM_block4:
7b5a2f43 20379 blk = dwarf_alloc_block (cu);
c906108c
SS
20380 blk->size = read_4_bytes (abfd, info_ptr);
20381 info_ptr += 4;
20382 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20383 info_ptr += blk->size;
9d2246fc 20384 attr->set_block (blk);
c906108c
SS
20385 break;
20386 case DW_FORM_data2:
414ad644 20387 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20388 info_ptr += 2;
20389 break;
20390 case DW_FORM_data4:
414ad644 20391 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20392 info_ptr += 4;
20393 break;
20394 case DW_FORM_data8:
414ad644 20395 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20396 info_ptr += 8;
20397 break;
0224619f
JK
20398 case DW_FORM_data16:
20399 blk = dwarf_alloc_block (cu);
20400 blk->size = 16;
20401 blk->data = read_n_bytes (abfd, info_ptr, 16);
20402 info_ptr += 16;
9d2246fc 20403 attr->set_block (blk);
0224619f 20404 break;
2dc7f7b3 20405 case DW_FORM_sec_offset:
7c290a04 20406 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
414ad644 20407 &bytes_read));
2dc7f7b3
TT
20408 info_ptr += bytes_read;
20409 break;
41144253 20410 case DW_FORM_loclistx:
20411 {
7a5f294d
TT
20412 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20413 &bytes_read));
20414 info_ptr += bytes_read;
41144253 20415 }
20416 break;
c906108c 20417 case DW_FORM_string:
c6481205
TT
20418 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20419 &bytes_read));
c906108c
SS
20420 info_ptr += bytes_read;
20421 break;
4bdf3d34 20422 case DW_FORM_strp:
36586728
TT
20423 if (!cu->per_cu->is_dwz)
20424 {
c6481205
TT
20425 attr->set_string_noncanonical
20426 (read_indirect_string (per_objfile,
20427 abfd, info_ptr, cu_header,
20428 &bytes_read));
36586728
TT
20429 info_ptr += bytes_read;
20430 break;
20431 }
20432 /* FALLTHROUGH */
43988095
JK
20433 case DW_FORM_line_strp:
20434 if (!cu->per_cu->is_dwz)
20435 {
c6481205
TT
20436 attr->set_string_noncanonical
20437 (per_objfile->read_line_string (info_ptr, cu_header,
20438 &bytes_read));
43988095
JK
20439 info_ptr += bytes_read;
20440 break;
20441 }
20442 /* FALLTHROUGH */
36586728
TT
20443 case DW_FORM_GNU_strp_alt:
20444 {
a7308ce0 20445 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
8266302d
TT
20446 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20447 &bytes_read);
36586728 20448
c6481205
TT
20449 attr->set_string_noncanonical
20450 (dwz->read_string (objfile, str_offset));
36586728
TT
20451 info_ptr += bytes_read;
20452 }
4bdf3d34 20453 break;
2dc7f7b3 20454 case DW_FORM_exprloc:
c906108c 20455 case DW_FORM_block:
7b5a2f43 20456 blk = dwarf_alloc_block (cu);
c906108c
SS
20457 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20458 info_ptr += bytes_read;
20459 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20460 info_ptr += blk->size;
9d2246fc 20461 attr->set_block (blk);
c906108c
SS
20462 break;
20463 case DW_FORM_block1:
7b5a2f43 20464 blk = dwarf_alloc_block (cu);
c906108c
SS
20465 blk->size = read_1_byte (abfd, info_ptr);
20466 info_ptr += 1;
20467 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20468 info_ptr += blk->size;
9d2246fc 20469 attr->set_block (blk);
c906108c
SS
20470 break;
20471 case DW_FORM_data1:
c906108c 20472 case DW_FORM_flag:
414ad644 20473 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20474 info_ptr += 1;
20475 break;
2dc7f7b3 20476 case DW_FORM_flag_present:
414ad644 20477 attr->set_unsigned (1);
2dc7f7b3 20478 break;
c906108c 20479 case DW_FORM_sdata:
1bc397c5 20480 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20481 info_ptr += bytes_read;
20482 break;
18a8505e 20483 case DW_FORM_rnglistx:
7a5f294d
TT
20484 {
20485 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20486 &bytes_read));
20487 info_ptr += bytes_read;
20488 }
20489 break;
d0ce17d8 20490 case DW_FORM_udata:
414ad644 20491 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20492 info_ptr += bytes_read;
20493 break;
20494 case DW_FORM_ref1:
7c290a04 20495 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20496 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20497 info_ptr += 1;
20498 break;
20499 case DW_FORM_ref2:
7c290a04 20500 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20501 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20502 info_ptr += 2;
20503 break;
20504 case DW_FORM_ref4:
7c290a04 20505 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20506 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20507 info_ptr += 4;
20508 break;
613e1657 20509 case DW_FORM_ref8:
7c290a04 20510 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644 20511 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20512 info_ptr += 8;
20513 break;
55f1336d 20514 case DW_FORM_ref_sig8:
630ed6b9 20515 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20516 info_ptr += 8;
20517 break;
c906108c 20518 case DW_FORM_ref_udata:
7c290a04 20519 attr->set_unsigned ((to_underlying (cu_header->sect_off)
414ad644
TT
20520 + read_unsigned_leb128 (abfd, info_ptr,
20521 &bytes_read)));
c906108c
SS
20522 info_ptr += bytes_read;
20523 break;
c906108c 20524 case DW_FORM_indirect:
a8329558
KW
20525 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20526 info_ptr += bytes_read;
43988095
JK
20527 if (form == DW_FORM_implicit_const)
20528 {
20529 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20530 info_ptr += bytes_read;
20531 }
20532 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20533 info_ptr);
43988095
JK
20534 break;
20535 case DW_FORM_implicit_const:
1bc397c5 20536 attr->set_signed (implicit_const);
a8329558 20537 break;
336d760d 20538 case DW_FORM_addrx:
3019eac3 20539 case DW_FORM_GNU_addr_index:
fe56917a
TT
20540 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20541 &bytes_read));
3019eac3
DE
20542 info_ptr += bytes_read;
20543 break;
cf532bd1 20544 case DW_FORM_strx:
15f18d14
AT
20545 case DW_FORM_strx1:
20546 case DW_FORM_strx2:
20547 case DW_FORM_strx3:
20548 case DW_FORM_strx4:
3019eac3 20549 case DW_FORM_GNU_str_index:
3019eac3 20550 {
15f18d14
AT
20551 ULONGEST str_index;
20552 if (form == DW_FORM_strx1)
20553 {
20554 str_index = read_1_byte (abfd, info_ptr);
20555 info_ptr += 1;
20556 }
20557 else if (form == DW_FORM_strx2)
20558 {
20559 str_index = read_2_bytes (abfd, info_ptr);
20560 info_ptr += 2;
20561 }
20562 else if (form == DW_FORM_strx3)
20563 {
20564 str_index = read_3_bytes (abfd, info_ptr);
20565 info_ptr += 3;
20566 }
20567 else if (form == DW_FORM_strx4)
20568 {
20569 str_index = read_4_bytes (abfd, info_ptr);
20570 info_ptr += 4;
20571 }
20572 else
20573 {
20574 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20575 info_ptr += bytes_read;
20576 }
fe56917a 20577 attr->set_unsigned_reprocess (str_index);
7a5f294d 20578 }
3019eac3 20579 break;
c906108c 20580 default:
8a3fe4f8 20581 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20582 dwarf_form_name (form),
20583 bfd_get_filename (abfd));
c906108c 20584 }
28e94949 20585
36586728 20586 /* Super hack. */
cd6c91b4 20587 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20588 attr->form = DW_FORM_GNU_ref_alt;
20589
28e94949
JB
20590 /* We have seen instances where the compiler tried to emit a byte
20591 size attribute of -1 which ended up being encoded as an unsigned
20592 0xffffffff. Although 0xffffffff is technically a valid size value,
20593 an object of this size seems pretty unlikely so we can relatively
20594 safely treat these cases as if the size attribute was invalid and
20595 treat them as zero by default. */
20596 if (attr->name == DW_AT_byte_size
20597 && form == DW_FORM_data4
529908cb 20598 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20599 {
20600 complaint
dda83cd7
SM
20601 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20602 hex_string (attr->as_unsigned ()));
414ad644 20603 attr->set_unsigned (0);
01c66ae6 20604 }
28e94949 20605
c906108c
SS
20606 return info_ptr;
20607}
20608
a8329558
KW
20609/* Read an attribute described by an abbreviated attribute. */
20610
d521ce57 20611static const gdb_byte *
dee91e82 20612read_attribute (const struct die_reader_specs *reader,
4444f407 20613 struct attribute *attr, const struct attr_abbrev *abbrev,
7a5f294d 20614 const gdb_byte *info_ptr)
a8329558
KW
20615{
20616 attr->name = abbrev->name;
c6481205 20617 attr->string_is_canonical = 0;
fe56917a 20618 attr->requires_reprocessing = 0;
43988095 20619 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20620 abbrev->implicit_const, info_ptr);
a8329558
KW
20621}
20622
43988095
JK
20623/* Return pointer to string at .debug_str offset STR_OFFSET. */
20624
20625static const char *
976ca316 20626read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20627 LONGEST str_offset)
43988095 20628{
976ca316
SM
20629 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20630 str_offset, "DW_FORM_strp");
c906108c
SS
20631}
20632
43988095
JK
20633/* Return pointer to string at .debug_str offset as read from BUF.
20634 BUF is assumed to be in a compilation unit described by CU_HEADER.
20635 Return *BYTES_READ_PTR count of bytes read from BUF. */
20636
d521ce57 20637static const char *
976ca316 20638read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20639 const gdb_byte *buf,
cf2c3c16
TT
20640 const struct comp_unit_head *cu_header,
20641 unsigned int *bytes_read_ptr)
20642{
8266302d 20643 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20644
976ca316 20645 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20646}
20647
86c0bb4c 20648/* See read.h. */
43988095 20649
86c0bb4c
TT
20650const char *
20651dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20652 const struct comp_unit_head *cu_header,
20653 unsigned int *bytes_read_ptr)
43988095 20654{
86c0bb4c 20655 bfd *abfd = objfile->obfd;
8266302d 20656 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20657
5989a64e 20658 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20659}
20660
3019eac3 20661/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20662 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20663 ADDR_SIZE is the size of addresses from the CU header. */
20664
20665static CORE_ADDR
976ca316
SM
20666read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20667 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20668{
976ca316 20669 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20670 bfd *abfd = objfile->obfd;
20671 const gdb_byte *info_ptr;
18a8505e 20672 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20673
976ca316
SM
20674 per_objfile->per_bfd->addr.read (objfile);
20675 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20676 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20677 objfile_name (objfile));
18a8505e 20678 if (addr_base_or_zero + addr_index * addr_size
976ca316 20679 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20680 error (_("DW_FORM_addr_index pointing outside of "
20681 ".debug_addr section [in module %s]"),
4262abfb 20682 objfile_name (objfile));
976ca316
SM
20683 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20684 + addr_index * addr_size);
3019eac3
DE
20685 if (addr_size == 4)
20686 return bfd_get_32 (abfd, info_ptr);
20687 else
20688 return bfd_get_64 (abfd, info_ptr);
20689}
20690
20691/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20692
20693static CORE_ADDR
20694read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20695{
5e22e966 20696 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20697 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20698}
20699
20700/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20701
20702static CORE_ADDR
d521ce57 20703read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20704 unsigned int *bytes_read)
20705{
5e22e966 20706 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20707 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20708
20709 return read_addr_index (cu, addr_index);
20710}
20711
450a1bfc 20712/* See read.h. */
3019eac3
DE
20713
20714CORE_ADDR
82ca3f51 20715dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20716 dwarf2_per_objfile *per_objfile,
82ca3f51 20717 unsigned int addr_index)
3019eac3 20718{
976ca316 20719 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20720 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20721 int addr_size;
20722
3019eac3
DE
20723 /* We need addr_base and addr_size.
20724 If we don't have PER_CU->cu, we have to get it.
20725 Nasty, but the alternative is storing the needed info in PER_CU,
20726 which at this point doesn't seem justified: it's not clear how frequently
20727 it would get used and it would increase the size of every PER_CU.
20728 Entry points like dwarf2_per_cu_addr_size do a similar thing
20729 so we're not in uncharted territory here.
20730 Alas we need to be a bit more complicated as addr_base is contained
20731 in the DIE.
20732
20733 We don't need to read the entire CU(/TU).
20734 We just need the header and top level die.
a1b64ce1 20735
3019eac3 20736 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20737 For now we skip this optimization. */
3019eac3
DE
20738
20739 if (cu != NULL)
20740 {
20741 addr_base = cu->addr_base;
20742 addr_size = cu->header.addr_size;
20743 }
20744 else
20745 {
976ca316 20746 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20747 addr_base = reader.cu->addr_base;
20748 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20749 }
20750
976ca316 20751 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20752}
20753
18a8505e
AT
20754/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20755 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20756 DWO file. */
3019eac3 20757
d521ce57 20758static const char *
18a8505e
AT
20759read_str_index (struct dwarf2_cu *cu,
20760 struct dwarf2_section_info *str_section,
20761 struct dwarf2_section_info *str_offsets_section,
20762 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20763{
976ca316
SM
20764 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20765 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20766 const char *objf_name = objfile_name (objfile);
3019eac3 20767 bfd *abfd = objfile->obfd;
d521ce57 20768 const gdb_byte *info_ptr;
3019eac3 20769 ULONGEST str_offset;
cf532bd1 20770 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20771
96b79293
TT
20772 str_section->read (objfile);
20773 str_offsets_section->read (objfile);
73869dc2 20774 if (str_section->buffer == NULL)
18a8505e 20775 error (_("%s used without %s section"
9d8780f0 20776 " in CU at offset %s [in module %s]"),
96b79293 20777 form_name, str_section->get_name (),
dda83cd7 20778 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20779 if (str_offsets_section->buffer == NULL)
18a8505e 20780 error (_("%s used without %s section"
9d8780f0 20781 " in CU at offset %s [in module %s]"),
96b79293 20782 form_name, str_section->get_name (),
dda83cd7 20783 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20784 info_ptr = (str_offsets_section->buffer
18a8505e 20785 + str_offsets_base
3019eac3
DE
20786 + str_index * cu->header.offset_size);
20787 if (cu->header.offset_size == 4)
20788 str_offset = bfd_get_32 (abfd, info_ptr);
20789 else
20790 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20791 if (str_offset >= str_section->size)
57d63ce2 20792 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20793 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20794 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20795 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20796}
20797
18a8505e
AT
20798/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20799
20800static const char *
20801read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20802{
20803 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20804 ? reader->cu->header.addr_size : 0;
20805 return read_str_index (reader->cu,
20806 &reader->dwo_file->sections.str,
20807 &reader->dwo_file->sections.str_offsets,
20808 str_offsets_base, str_index);
20809}
20810
20811/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20812
20813static const char *
20814read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20815{
5e22e966 20816 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20817 const char *objf_name = objfile_name (objfile);
20818 static const char form_name[] = "DW_FORM_GNU_str_index";
20819 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20820
20821 if (!cu->str_offsets_base.has_value ())
20822 error (_("%s used in Fission stub without %s"
20823 " in CU at offset 0x%lx [in module %s]"),
20824 form_name, str_offsets_attr_name,
20825 (long) cu->header.offset_size, objf_name);
20826
20827 return read_str_index (cu,
5e22e966
SM
20828 &cu->per_objfile->per_bfd->str,
20829 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20830 *cu->str_offsets_base, str_index);
20831}
20832
3019eac3
DE
20833/* Return the length of an LEB128 number in BUF. */
20834
20835static int
20836leb128_size (const gdb_byte *buf)
20837{
20838 const gdb_byte *begin = buf;
20839 gdb_byte byte;
20840
20841 while (1)
20842 {
20843 byte = *buf++;
20844 if ((byte & 128) == 0)
20845 return buf - begin;
20846 }
20847}
20848
c906108c 20849static void
e142c38c 20850set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20851{
20852 switch (lang)
20853 {
20854 case DW_LANG_C89:
76bee0cc 20855 case DW_LANG_C99:
0cfd832f 20856 case DW_LANG_C11:
c906108c 20857 case DW_LANG_C:
d1be3247 20858 case DW_LANG_UPC:
e142c38c 20859 cu->language = language_c;
c906108c 20860 break;
9c37b5ae 20861 case DW_LANG_Java:
c906108c 20862 case DW_LANG_C_plus_plus:
0cfd832f
MW
20863 case DW_LANG_C_plus_plus_11:
20864 case DW_LANG_C_plus_plus_14:
e142c38c 20865 cu->language = language_cplus;
c906108c 20866 break;
6aecb9c2
JB
20867 case DW_LANG_D:
20868 cu->language = language_d;
20869 break;
c906108c
SS
20870 case DW_LANG_Fortran77:
20871 case DW_LANG_Fortran90:
b21b22e0 20872 case DW_LANG_Fortran95:
f7de9aab
MW
20873 case DW_LANG_Fortran03:
20874 case DW_LANG_Fortran08:
e142c38c 20875 cu->language = language_fortran;
c906108c 20876 break;
a766d390
DE
20877 case DW_LANG_Go:
20878 cu->language = language_go;
20879 break;
c906108c 20880 case DW_LANG_Mips_Assembler:
e142c38c 20881 cu->language = language_asm;
c906108c
SS
20882 break;
20883 case DW_LANG_Ada83:
8aaf0b47 20884 case DW_LANG_Ada95:
bc5f45f8
JB
20885 cu->language = language_ada;
20886 break;
72019c9c
GM
20887 case DW_LANG_Modula2:
20888 cu->language = language_m2;
20889 break;
fe8e67fd
PM
20890 case DW_LANG_Pascal83:
20891 cu->language = language_pascal;
20892 break;
22566fbd
DJ
20893 case DW_LANG_ObjC:
20894 cu->language = language_objc;
20895 break;
c44af4eb
TT
20896 case DW_LANG_Rust:
20897 case DW_LANG_Rust_old:
20898 cu->language = language_rust;
20899 break;
c906108c
SS
20900 case DW_LANG_Cobol74:
20901 case DW_LANG_Cobol85:
c906108c 20902 default:
e142c38c 20903 cu->language = language_minimal;
c906108c
SS
20904 break;
20905 }
e142c38c 20906 cu->language_defn = language_def (cu->language);
c906108c
SS
20907}
20908
20909/* Return the named attribute or NULL if not there. */
20910
20911static struct attribute *
e142c38c 20912dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20913{
a48e046c 20914 for (;;)
c906108c 20915 {
a48e046c
TT
20916 unsigned int i;
20917 struct attribute *spec = NULL;
20918
20919 for (i = 0; i < die->num_attrs; ++i)
20920 {
20921 if (die->attrs[i].name == name)
20922 return &die->attrs[i];
20923 if (die->attrs[i].name == DW_AT_specification
20924 || die->attrs[i].name == DW_AT_abstract_origin)
20925 spec = &die->attrs[i];
20926 }
20927
20928 if (!spec)
20929 break;
c906108c 20930
f2f0e013 20931 die = follow_die_ref (die, spec, &cu);
f2f0e013 20932 }
c5aa993b 20933
c906108c
SS
20934 return NULL;
20935}
20936
7d45c7c3
KB
20937/* Return the string associated with a string-typed attribute, or NULL if it
20938 is either not found or is of an incorrect type. */
20939
20940static const char *
20941dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20942{
20943 struct attribute *attr;
20944 const char *str = NULL;
20945
20946 attr = dwarf2_attr (die, name, cu);
20947
20948 if (attr != NULL)
20949 {
95f982e5 20950 str = attr->as_string ();
e61108c9 20951 if (str == nullptr)
dda83cd7 20952 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20953 "DIE at %s in module %s"),
20954 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20955 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20956 }
20957
20958 return str;
20959}
20960
a084a2a6 20961/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20962 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20963static const char *
20964dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20965{
20966 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20967 if (dwo_name == nullptr)
20968 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20969 return dwo_name;
20970}
20971
05cf31d1
JB
20972/* Return non-zero iff the attribute NAME is defined for the given DIE,
20973 and holds a non-zero value. This function should only be used for
2dc7f7b3 20974 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20975
20976static int
20977dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20978{
20979 struct attribute *attr = dwarf2_attr (die, name, cu);
20980
c45bc3f8 20981 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20982}
20983
3ca72b44 20984static int
e142c38c 20985die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20986{
05cf31d1
JB
20987 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20988 which value is non-zero. However, we have to be careful with
20989 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20990 (via dwarf2_flag_true_p) follows this attribute. So we may
20991 end up accidently finding a declaration attribute that belongs
20992 to a different DIE referenced by the specification attribute,
20993 even though the given DIE does not have a declaration attribute. */
20994 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20995 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20996}
20997
63d06c5c 20998/* Return the die giving the specification for DIE, if there is
f2f0e013 20999 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21000 containing the return value on output. If there is no
21001 specification, but there is an abstract origin, that is
21002 returned. */
63d06c5c
DC
21003
21004static struct die_info *
f2f0e013 21005die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21006{
f2f0e013
DJ
21007 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21008 *spec_cu);
63d06c5c 21009
edb3359d
DJ
21010 if (spec_attr == NULL)
21011 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21012
63d06c5c
DC
21013 if (spec_attr == NULL)
21014 return NULL;
21015 else
f2f0e013 21016 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21017}
c906108c 21018
527f3840
JK
21019/* Stub for free_line_header to match void * callback types. */
21020
21021static void
21022free_line_header_voidp (void *arg)
21023{
9a3c8263 21024 struct line_header *lh = (struct line_header *) arg;
527f3840 21025
fff8551c 21026 delete lh;
527f3840
JK
21027}
21028
83769d0b 21029/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21030
21031static struct dwarf2_section_info *
21032get_debug_line_section (struct dwarf2_cu *cu)
21033{
21034 struct dwarf2_section_info *section;
976ca316 21035 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21036
21037 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21038 DWO file. */
21039 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21040 section = &cu->dwo_unit->dwo_file->sections.line;
21041 else if (cu->per_cu->is_dwz)
21042 {
a7308ce0 21043 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
36586728
TT
21044
21045 section = &dwz->line;
21046 }
21047 else
976ca316 21048 section = &per_objfile->per_bfd->line;
36586728
TT
21049
21050 return section;
21051}
21052
debd256d 21053/* Read the statement program header starting at OFFSET in
3019eac3 21054 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21055 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21056 Returns NULL if there is a problem reading the header, e.g., if it
21057 has a version we don't understand.
debd256d
JB
21058
21059 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21060 the returned object point into the dwarf line section buffer,
21061 and must not be freed. */
ae2de4f8 21062
fff8551c 21063static line_header_up
9c541725 21064dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21065{
3019eac3 21066 struct dwarf2_section_info *section;
976ca316 21067 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21068
36586728 21069 section = get_debug_line_section (cu);
976ca316 21070 section->read (per_objfile->objfile);
3019eac3 21071 if (section->buffer == NULL)
debd256d 21072 {
3019eac3 21073 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21074 complaint (_("missing .debug_line.dwo section"));
3019eac3 21075 else
b98664d3 21076 complaint (_("missing .debug_line section"));
debd256d
JB
21077 return 0;
21078 }
21079
0df7ad3a 21080 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21081 per_objfile, section, &cu->header);
debd256d 21082}
c906108c 21083
c6da4cef 21084/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21085 Return the file name of the psymtab for the given file_entry.
c6da4cef 21086 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21087 If space for the result is malloc'd, *NAME_HOLDER will be set.
21088 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21089
d521ce57 21090static const char *
7ba99d21 21091psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21092 const dwarf2_psymtab *pst,
c89b44cd
TT
21093 const char *comp_dir,
21094 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21095{
d521ce57
TT
21096 const char *include_name = fe.name;
21097 const char *include_name_to_compare = include_name;
72b9f47f 21098 const char *pst_filename;
c6da4cef
DE
21099 int file_is_pst;
21100
8c43009f 21101 const char *dir_name = fe.include_dir (lh);
c6da4cef 21102
c89b44cd 21103 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21104 if (!IS_ABSOLUTE_PATH (include_name)
21105 && (dir_name != NULL || comp_dir != NULL))
21106 {
21107 /* Avoid creating a duplicate psymtab for PST.
21108 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21109 Before we do the comparison, however, we need to account
21110 for DIR_NAME and COMP_DIR.
21111 First prepend dir_name (if non-NULL). If we still don't
21112 have an absolute path prepend comp_dir (if non-NULL).
21113 However, the directory we record in the include-file's
21114 psymtab does not contain COMP_DIR (to match the
21115 corresponding symtab(s)).
21116
21117 Example:
21118
21119 bash$ cd /tmp
21120 bash$ gcc -g ./hello.c
21121 include_name = "hello.c"
21122 dir_name = "."
21123 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21124 DW_AT_name = "./hello.c"
21125
21126 */
c6da4cef
DE
21127
21128 if (dir_name != NULL)
21129 {
c89b44cd
TT
21130 name_holder->reset (concat (dir_name, SLASH_STRING,
21131 include_name, (char *) NULL));
21132 include_name = name_holder->get ();
c6da4cef 21133 include_name_to_compare = include_name;
c6da4cef
DE
21134 }
21135 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21136 {
c89b44cd
TT
21137 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21138 include_name, (char *) NULL));
21139 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21140 }
21141 }
21142
21143 pst_filename = pst->filename;
c89b44cd 21144 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21145 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21146 {
c89b44cd
TT
21147 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21148 pst_filename, (char *) NULL));
21149 pst_filename = copied_name.get ();
c6da4cef
DE
21150 }
21151
1e3fad37 21152 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21153
c6da4cef
DE
21154 if (file_is_pst)
21155 return NULL;
21156 return include_name;
21157}
21158
d9b3de22
DE
21159/* State machine to track the state of the line number program. */
21160
6f77053d 21161class lnp_state_machine
d9b3de22 21162{
6f77053d
PA
21163public:
21164 /* Initialize a machine state for the start of a line number
21165 program. */
804d2729
TT
21166 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21167 bool record_lines_p);
6f77053d 21168
8c43009f
PA
21169 file_entry *current_file ()
21170 {
21171 /* lh->file_names is 0-based, but the file name numbers in the
21172 statement program are 1-based. */
6f77053d
PA
21173 return m_line_header->file_name_at (m_file);
21174 }
21175
21176 /* Record the line in the state machine. END_SEQUENCE is true if
21177 we're processing the end of a sequence. */
21178 void record_line (bool end_sequence);
21179
a8caed5d 21180 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21181 nop-out rest of the lines in this sequence. */
6f77053d
PA
21182 void check_line_address (struct dwarf2_cu *cu,
21183 const gdb_byte *line_ptr,
7ab6656f 21184 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21185
21186 void handle_set_discriminator (unsigned int discriminator)
21187 {
21188 m_discriminator = discriminator;
21189 m_line_has_non_zero_discriminator |= discriminator != 0;
21190 }
21191
21192 /* Handle DW_LNE_set_address. */
21193 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21194 {
21195 m_op_index = 0;
21196 address += baseaddr;
21197 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21198 }
21199
21200 /* Handle DW_LNS_advance_pc. */
21201 void handle_advance_pc (CORE_ADDR adjust);
21202
21203 /* Handle a special opcode. */
21204 void handle_special_opcode (unsigned char op_code);
21205
21206 /* Handle DW_LNS_advance_line. */
21207 void handle_advance_line (int line_delta)
21208 {
21209 advance_line (line_delta);
21210 }
21211
21212 /* Handle DW_LNS_set_file. */
21213 void handle_set_file (file_name_index file);
21214
21215 /* Handle DW_LNS_negate_stmt. */
21216 void handle_negate_stmt ()
21217 {
21218 m_is_stmt = !m_is_stmt;
21219 }
21220
21221 /* Handle DW_LNS_const_add_pc. */
21222 void handle_const_add_pc ();
21223
21224 /* Handle DW_LNS_fixed_advance_pc. */
21225 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21226 {
21227 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21228 m_op_index = 0;
21229 }
21230
21231 /* Handle DW_LNS_copy. */
21232 void handle_copy ()
21233 {
21234 record_line (false);
21235 m_discriminator = 0;
21236 }
21237
21238 /* Handle DW_LNE_end_sequence. */
21239 void handle_end_sequence ()
21240 {
804d2729 21241 m_currently_recording_lines = true;
6f77053d
PA
21242 }
21243
21244private:
21245 /* Advance the line by LINE_DELTA. */
21246 void advance_line (int line_delta)
21247 {
21248 m_line += line_delta;
21249
21250 if (line_delta != 0)
21251 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21252 }
21253
804d2729
TT
21254 struct dwarf2_cu *m_cu;
21255
6f77053d
PA
21256 gdbarch *m_gdbarch;
21257
21258 /* True if we're recording lines.
21259 Otherwise we're building partial symtabs and are just interested in
21260 finding include files mentioned by the line number program. */
21261 bool m_record_lines_p;
21262
8c43009f 21263 /* The line number header. */
6f77053d 21264 line_header *m_line_header;
8c43009f 21265
6f77053d
PA
21266 /* These are part of the standard DWARF line number state machine,
21267 and initialized according to the DWARF spec. */
d9b3de22 21268
6f77053d 21269 unsigned char m_op_index = 0;
7ba99d21
AT
21270 /* The line table index of the current file. */
21271 file_name_index m_file = 1;
6f77053d
PA
21272 unsigned int m_line = 1;
21273
21274 /* These are initialized in the constructor. */
21275
21276 CORE_ADDR m_address;
21277 bool m_is_stmt;
21278 unsigned int m_discriminator;
d9b3de22
DE
21279
21280 /* Additional bits of state we need to track. */
21281
21282 /* The last file that we called dwarf2_start_subfile for.
21283 This is only used for TLLs. */
6f77053d 21284 unsigned int m_last_file = 0;
d9b3de22 21285 /* The last file a line number was recorded for. */
6f77053d 21286 struct subfile *m_last_subfile = NULL;
d9b3de22 21287
1313c56e
AB
21288 /* The address of the last line entry. */
21289 CORE_ADDR m_last_address;
21290
21291 /* Set to true when a previous line at the same address (using
21292 m_last_address) had m_is_stmt true. This is reset to false when a
21293 line entry at a new address (m_address different to m_last_address) is
21294 processed. */
21295 bool m_stmt_at_address = false;
21296
804d2729
TT
21297 /* When true, record the lines we decode. */
21298 bool m_currently_recording_lines = false;
d9b3de22
DE
21299
21300 /* The last line number that was recorded, used to coalesce
21301 consecutive entries for the same line. This can happen, for
21302 example, when discriminators are present. PR 17276. */
6f77053d
PA
21303 unsigned int m_last_line = 0;
21304 bool m_line_has_non_zero_discriminator = false;
8c43009f 21305};
d9b3de22 21306
6f77053d
PA
21307void
21308lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21309{
21310 CORE_ADDR addr_adj = (((m_op_index + adjust)
21311 / m_line_header->maximum_ops_per_instruction)
21312 * m_line_header->minimum_instruction_length);
21313 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21314 m_op_index = ((m_op_index + adjust)
21315 % m_line_header->maximum_ops_per_instruction);
21316}
d9b3de22 21317
6f77053d
PA
21318void
21319lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21320{
6f77053d 21321 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21322 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21323 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21324 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21325 / m_line_header->maximum_ops_per_instruction)
21326 * m_line_header->minimum_instruction_length);
21327 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21328 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21329 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21330
258bf0ee 21331 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21332 advance_line (line_delta);
21333 record_line (false);
21334 m_discriminator = 0;
21335}
d9b3de22 21336
6f77053d
PA
21337void
21338lnp_state_machine::handle_set_file (file_name_index file)
21339{
21340 m_file = file;
21341
21342 const file_entry *fe = current_file ();
21343 if (fe == NULL)
21344 dwarf2_debug_line_missing_file_complaint ();
21345 else if (m_record_lines_p)
21346 {
21347 const char *dir = fe->include_dir (m_line_header);
21348
c24bdb02 21349 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21350 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21351 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21352 }
21353}
21354
21355void
21356lnp_state_machine::handle_const_add_pc ()
21357{
21358 CORE_ADDR adjust
21359 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21360
21361 CORE_ADDR addr_adj
21362 = (((m_op_index + adjust)
21363 / m_line_header->maximum_ops_per_instruction)
21364 * m_line_header->minimum_instruction_length);
21365
21366 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21367 m_op_index = ((m_op_index + adjust)
21368 % m_line_header->maximum_ops_per_instruction);
21369}
d9b3de22 21370
a05a36a5
DE
21371/* Return non-zero if we should add LINE to the line number table.
21372 LINE is the line to add, LAST_LINE is the last line that was added,
21373 LAST_SUBFILE is the subfile for LAST_LINE.
21374 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21375 had a non-zero discriminator.
21376
21377 We have to be careful in the presence of discriminators.
21378 E.g., for this line:
21379
21380 for (i = 0; i < 100000; i++);
21381
21382 clang can emit four line number entries for that one line,
21383 each with a different discriminator.
21384 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21385
21386 However, we want gdb to coalesce all four entries into one.
21387 Otherwise the user could stepi into the middle of the line and
21388 gdb would get confused about whether the pc really was in the
21389 middle of the line.
21390
21391 Things are further complicated by the fact that two consecutive
21392 line number entries for the same line is a heuristic used by gcc
21393 to denote the end of the prologue. So we can't just discard duplicate
21394 entries, we have to be selective about it. The heuristic we use is
21395 that we only collapse consecutive entries for the same line if at least
21396 one of those entries has a non-zero discriminator. PR 17276.
21397
21398 Note: Addresses in the line number state machine can never go backwards
21399 within one sequence, thus this coalescing is ok. */
21400
21401static int
804d2729
TT
21402dwarf_record_line_p (struct dwarf2_cu *cu,
21403 unsigned int line, unsigned int last_line,
a05a36a5
DE
21404 int line_has_non_zero_discriminator,
21405 struct subfile *last_subfile)
21406{
c24bdb02 21407 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21408 return 1;
21409 if (line != last_line)
21410 return 1;
21411 /* Same line for the same file that we've seen already.
21412 As a last check, for pr 17276, only record the line if the line
21413 has never had a non-zero discriminator. */
21414 if (!line_has_non_zero_discriminator)
21415 return 1;
21416 return 0;
21417}
21418
804d2729
TT
21419/* Use the CU's builder to record line number LINE beginning at
21420 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21421
21422static void
d9b3de22 21423dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21424 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21425 struct dwarf2_cu *cu)
252a6764
DE
21426{
21427 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21428
27e0867f
DE
21429 if (dwarf_line_debug)
21430 {
21431 fprintf_unfiltered (gdb_stdlog,
21432 "Recording line %u, file %s, address %s\n",
21433 line, lbasename (subfile->name),
21434 paddress (gdbarch, address));
21435 }
21436
804d2729 21437 if (cu != nullptr)
8c95582d 21438 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21439}
21440
21441/* Subroutine of dwarf_decode_lines_1 to simplify it.
21442 Mark the end of a set of line number records.
d9b3de22 21443 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21444 If SUBFILE is NULL the request is ignored. */
21445
21446static void
21447dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21448 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21449{
27e0867f
DE
21450 if (subfile == NULL)
21451 return;
21452
21453 if (dwarf_line_debug)
21454 {
21455 fprintf_unfiltered (gdb_stdlog,
21456 "Finishing current line, file %s, address %s\n",
21457 lbasename (subfile->name),
21458 paddress (gdbarch, address));
21459 }
21460
8c95582d 21461 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21462}
21463
6f77053d
PA
21464void
21465lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21466{
d9b3de22
DE
21467 if (dwarf_line_debug)
21468 {
21469 fprintf_unfiltered (gdb_stdlog,
21470 "Processing actual line %u: file %u,"
94a72be7 21471 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21472 m_line, m_file,
6f77053d 21473 paddress (m_gdbarch, m_address),
94a72be7
AB
21474 m_is_stmt, m_discriminator,
21475 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21476 }
21477
6f77053d 21478 file_entry *fe = current_file ();
8c43009f
PA
21479
21480 if (fe == NULL)
d9b3de22
DE
21481 dwarf2_debug_line_missing_file_complaint ();
21482 /* For now we ignore lines not starting on an instruction boundary.
21483 But not when processing end_sequence for compatibility with the
21484 previous version of the code. */
6f77053d 21485 else if (m_op_index == 0 || end_sequence)
d9b3de22 21486 {
8c43009f 21487 fe->included_p = 1;
8c95582d 21488 if (m_record_lines_p)
d9b3de22 21489 {
1313c56e
AB
21490 /* When we switch files we insert an end maker in the first file,
21491 switch to the second file and add a new line entry. The
21492 problem is that the end marker inserted in the first file will
21493 discard any previous line entries at the same address. If the
21494 line entries in the first file are marked as is-stmt, while
21495 the new line in the second file is non-stmt, then this means
21496 the end marker will discard is-stmt lines so we can have a
21497 non-stmt line. This means that there are less addresses at
21498 which the user can insert a breakpoint.
21499
21500 To improve this we track the last address in m_last_address,
21501 and whether we have seen an is-stmt at this address. Then
21502 when switching files, if we have seen a stmt at the current
21503 address, and we are switching to create a non-stmt line, then
21504 discard the new line. */
21505 bool file_changed
21506 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21507 bool ignore_this_line
dda83cd7
SM
21508 = ((file_changed && !end_sequence && m_last_address == m_address
21509 && !m_is_stmt && m_stmt_at_address)
21510 || (!end_sequence && m_line == 0));
1313c56e
AB
21511
21512 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21513 {
804d2729
TT
21514 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21515 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21516 }
21517
1313c56e 21518 if (!end_sequence && !ignore_this_line)
d9b3de22 21519 {
8c95582d
AB
21520 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21521
804d2729 21522 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21523 m_line_has_non_zero_discriminator,
21524 m_last_subfile))
d9b3de22 21525 {
c24bdb02 21526 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21527 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21528 builder->get_current_subfile (),
8c95582d 21529 m_line, m_address, is_stmt,
804d2729 21530 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21531 }
c24bdb02 21532 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21533 m_last_line = m_line;
d9b3de22
DE
21534 }
21535 }
21536 }
1313c56e
AB
21537
21538 /* Track whether we have seen any m_is_stmt true at m_address in case we
21539 have multiple line table entries all at m_address. */
21540 if (m_last_address != m_address)
21541 {
21542 m_stmt_at_address = false;
21543 m_last_address = m_address;
21544 }
21545 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21546}
21547
804d2729
TT
21548lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21549 line_header *lh, bool record_lines_p)
d9b3de22 21550{
804d2729 21551 m_cu = cu;
6f77053d
PA
21552 m_gdbarch = arch;
21553 m_record_lines_p = record_lines_p;
21554 m_line_header = lh;
d9b3de22 21555
804d2729 21556 m_currently_recording_lines = true;
d9b3de22 21557
d9b3de22
DE
21558 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21559 was a line entry for it so that the backend has a chance to adjust it
21560 and also record it in case it needs it. This is currently used by MIPS
21561 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21562 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21563 m_is_stmt = lh->default_is_stmt;
21564 m_discriminator = 0;
1313c56e
AB
21565
21566 m_last_address = m_address;
21567 m_stmt_at_address = false;
252a6764
DE
21568}
21569
6f77053d
PA
21570void
21571lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21572 const gdb_byte *line_ptr,
7ab6656f 21573 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21574{
a8caed5d
FS
21575 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21576 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21577 located at 0x0. In this case, additionally check that if
21578 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21579
a8caed5d
FS
21580 if ((address == 0 && address < unrelocated_lowpc)
21581 || address == (CORE_ADDR) -1)
924c2928
DE
21582 {
21583 /* This line table is for a function which has been
21584 GCd by the linker. Ignore it. PR gdb/12528 */
21585
5e22e966 21586 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21587 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21588
b98664d3 21589 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21590 line_offset, objfile_name (objfile));
804d2729
TT
21591 m_currently_recording_lines = false;
21592 /* Note: m_currently_recording_lines is left as false until we see
21593 DW_LNE_end_sequence. */
924c2928
DE
21594 }
21595}
21596
f3f5162e 21597/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21598 Process the line number information in LH.
21599 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21600 program in order to set included_p for every referenced header. */
debd256d 21601
c906108c 21602static void
43f3e411
DE
21603dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21604 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21605{
d521ce57
TT
21606 const gdb_byte *line_ptr, *extended_end;
21607 const gdb_byte *line_end;
a8c50c1f 21608 unsigned int bytes_read, extended_len;
699ca60a 21609 unsigned char op_code, extended_op;
e142c38c 21610 CORE_ADDR baseaddr;
5e22e966 21611 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21612 bfd *abfd = objfile->obfd;
08feed99 21613 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21614 /* True if we're recording line info (as opposed to building partial
21615 symtabs and just interested in finding include files mentioned by
21616 the line number program). */
21617 bool record_lines_p = !decode_for_pst_p;
e142c38c 21618
b3b3bada 21619 baseaddr = objfile->text_section_offset ();
c906108c 21620
debd256d
JB
21621 line_ptr = lh->statement_program_start;
21622 line_end = lh->statement_program_end;
c906108c
SS
21623
21624 /* Read the statement sequences until there's nothing left. */
21625 while (line_ptr < line_end)
21626 {
6f77053d
PA
21627 /* The DWARF line number program state machine. Reset the state
21628 machine at the start of each sequence. */
804d2729 21629 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21630 bool end_sequence = false;
d9b3de22 21631
8c43009f 21632 if (record_lines_p)
c906108c 21633 {
8c43009f
PA
21634 /* Start a subfile for the current file of the state
21635 machine. */
21636 const file_entry *fe = state_machine.current_file ();
21637
21638 if (fe != NULL)
804d2729 21639 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21640 }
21641
a738430d 21642 /* Decode the table. */
d9b3de22 21643 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21644 {
21645 op_code = read_1_byte (abfd, line_ptr);
21646 line_ptr += 1;
9aa1fe7e 21647
debd256d 21648 if (op_code >= lh->opcode_base)
6e70227d 21649 {
8e07a239 21650 /* Special opcode. */
6f77053d 21651 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21652 }
21653 else switch (op_code)
c906108c
SS
21654 {
21655 case DW_LNS_extended_op:
3e43a32a
MS
21656 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21657 &bytes_read);
473b7be6 21658 line_ptr += bytes_read;
a8c50c1f 21659 extended_end = line_ptr + extended_len;
c906108c
SS
21660 extended_op = read_1_byte (abfd, line_ptr);
21661 line_ptr += 1;
8f34b746
TV
21662 if (DW_LNE_lo_user <= extended_op
21663 && extended_op <= DW_LNE_hi_user)
21664 {
21665 /* Vendor extension, ignore. */
21666 line_ptr = extended_end;
21667 break;
21668 }
c906108c
SS
21669 switch (extended_op)
21670 {
21671 case DW_LNE_end_sequence:
6f77053d
PA
21672 state_machine.handle_end_sequence ();
21673 end_sequence = true;
c906108c
SS
21674 break;
21675 case DW_LNE_set_address:
d9b3de22
DE
21676 {
21677 CORE_ADDR address
c8a7a66f 21678 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21679 line_ptr += bytes_read;
6f77053d
PA
21680
21681 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21682 lowpc - baseaddr, address);
6f77053d 21683 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21684 }
c906108c
SS
21685 break;
21686 case DW_LNE_define_file:
dda83cd7
SM
21687 {
21688 const char *cur_file;
ecfb656c
PA
21689 unsigned int mod_time, length;
21690 dir_index dindex;
6e70227d 21691
dda83cd7 21692 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21693 &bytes_read);
dda83cd7
SM
21694 line_ptr += bytes_read;
21695 dindex = (dir_index)
21696 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21697 line_ptr += bytes_read;
21698 mod_time =
21699 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21700 line_ptr += bytes_read;
21701 length =
21702 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21703 line_ptr += bytes_read;
21704 lh->add_file_name (cur_file, dindex, mod_time, length);
21705 }
c906108c 21706 break;
d0c6ba3d 21707 case DW_LNE_set_discriminator:
6f77053d
PA
21708 {
21709 /* The discriminator is not interesting to the
21710 debugger; just ignore it. We still need to
21711 check its value though:
21712 if there are consecutive entries for the same
21713 (non-prologue) line we want to coalesce them.
21714 PR 17276. */
21715 unsigned int discr
21716 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21717 line_ptr += bytes_read;
21718
21719 state_machine.handle_set_discriminator (discr);
21720 }
d0c6ba3d 21721 break;
c906108c 21722 default:
b98664d3 21723 complaint (_("mangled .debug_line section"));
debd256d 21724 return;
c906108c 21725 }
a8c50c1f
DJ
21726 /* Make sure that we parsed the extended op correctly. If e.g.
21727 we expected a different address size than the producer used,
21728 we may have read the wrong number of bytes. */
21729 if (line_ptr != extended_end)
21730 {
b98664d3 21731 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21732 return;
21733 }
c906108c
SS
21734 break;
21735 case DW_LNS_copy:
6f77053d 21736 state_machine.handle_copy ();
c906108c
SS
21737 break;
21738 case DW_LNS_advance_pc:
2dc7f7b3
TT
21739 {
21740 CORE_ADDR adjust
21741 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21742 line_ptr += bytes_read;
6f77053d
PA
21743
21744 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21745 }
c906108c
SS
21746 break;
21747 case DW_LNS_advance_line:
a05a36a5
DE
21748 {
21749 int line_delta
21750 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21751 line_ptr += bytes_read;
6f77053d
PA
21752
21753 state_machine.handle_advance_line (line_delta);
a05a36a5 21754 }
c906108c
SS
21755 break;
21756 case DW_LNS_set_file:
d9b3de22 21757 {
6f77053d 21758 file_name_index file
ecfb656c
PA
21759 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21760 &bytes_read);
d9b3de22 21761 line_ptr += bytes_read;
8c43009f 21762
6f77053d 21763 state_machine.handle_set_file (file);
d9b3de22 21764 }
c906108c
SS
21765 break;
21766 case DW_LNS_set_column:
0ad93d4f 21767 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21768 line_ptr += bytes_read;
21769 break;
21770 case DW_LNS_negate_stmt:
6f77053d 21771 state_machine.handle_negate_stmt ();
c906108c
SS
21772 break;
21773 case DW_LNS_set_basic_block:
c906108c 21774 break;
c2c6d25f
JM
21775 /* Add to the address register of the state machine the
21776 address increment value corresponding to special opcode
a738430d
MK
21777 255. I.e., this value is scaled by the minimum
21778 instruction length since special opcode 255 would have
b021a221 21779 scaled the increment. */
c906108c 21780 case DW_LNS_const_add_pc:
6f77053d 21781 state_machine.handle_const_add_pc ();
c906108c
SS
21782 break;
21783 case DW_LNS_fixed_advance_pc:
3e29f34a 21784 {
6f77053d 21785 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21786 line_ptr += 2;
6f77053d
PA
21787
21788 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21789 }
c906108c 21790 break;
9aa1fe7e 21791 default:
a738430d
MK
21792 {
21793 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21794 int i;
a738430d 21795
debd256d 21796 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21797 {
21798 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21799 line_ptr += bytes_read;
21800 }
21801 }
c906108c
SS
21802 }
21803 }
d9b3de22
DE
21804
21805 if (!end_sequence)
21806 dwarf2_debug_line_missing_end_sequence_complaint ();
21807
21808 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21809 in which case we still finish recording the last line). */
6f77053d 21810 state_machine.record_line (true);
c906108c 21811 }
f3f5162e
DE
21812}
21813
21814/* Decode the Line Number Program (LNP) for the given line_header
21815 structure and CU. The actual information extracted and the type
21816 of structures created from the LNP depends on the value of PST.
21817
21818 1. If PST is NULL, then this procedure uses the data from the program
21819 to create all necessary symbol tables, and their linetables.
21820
21821 2. If PST is not NULL, this procedure reads the program to determine
21822 the list of files included by the unit represented by PST, and
21823 builds all the associated partial symbol tables.
21824
21825 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21826 It is used for relative paths in the line table.
21827 NOTE: When processing partial symtabs (pst != NULL),
21828 comp_dir == pst->dirname.
21829
21830 NOTE: It is important that psymtabs have the same file name (via strcmp)
21831 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21832 symtab we don't use it in the name of the psymtabs we create.
21833 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21834 A good testcase for this is mb-inline.exp.
21835
527f3840
JK
21836 LOWPC is the lowest address in CU (or 0 if not known).
21837
21838 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21839 for its PC<->lines mapping information. Otherwise only the filename
21840 table is read in. */
f3f5162e
DE
21841
21842static void
21843dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21844 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21845 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21846{
5e22e966 21847 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21848 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21849
527f3840
JK
21850 if (decode_mapping)
21851 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21852
21853 if (decode_for_pst_p)
21854 {
aaa75496 21855 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21856 create the psymtab of each included file. */
7ba99d21 21857 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21858 if (file_entry.included_p == 1)
21859 {
c89b44cd 21860 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21861 const char *include_name =
7ba99d21
AT
21862 psymtab_include_file_name (lh, file_entry, pst,
21863 comp_dir, &name_holder);
c6da4cef 21864 if (include_name != NULL)
dda83cd7
SM
21865 dwarf2_create_include_psymtab (include_name, pst, objfile);
21866 }
aaa75496 21867 }
cb1df416
DJ
21868 else
21869 {
21870 /* Make sure a symtab is created for every file, even files
21871 which contain only variables (i.e. no code with associated
21872 line numbers). */
c24bdb02
KS
21873 buildsym_compunit *builder = cu->get_builder ();
21874 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21875
7ba99d21 21876 for (auto &fe : lh->file_names ())
cb1df416 21877 {
804d2729 21878 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21879 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21880 {
c24bdb02 21881 builder->get_current_subfile ()->symtab
804d2729 21882 = allocate_symtab (cust,
c24bdb02 21883 builder->get_current_subfile ()->name);
43f3e411 21884 }
c24bdb02 21885 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21886 }
21887 }
c906108c
SS
21888}
21889
21890/* Start a subfile for DWARF. FILENAME is the name of the file and
21891 DIRNAME the name of the source directory which contains FILENAME
4d663531 21892 or NULL if not known.
c906108c
SS
21893 This routine tries to keep line numbers from identical absolute and
21894 relative file names in a common subfile.
21895
21896 Using the `list' example from the GDB testsuite, which resides in
21897 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21898 of /srcdir/list0.c yields the following debugging information for list0.c:
21899
c5aa993b 21900 DW_AT_name: /srcdir/list0.c
4d663531 21901 DW_AT_comp_dir: /compdir
357e46e7 21902 files.files[0].name: list0.h
c5aa993b 21903 files.files[0].dir: /srcdir
357e46e7 21904 files.files[1].name: list0.c
c5aa993b 21905 files.files[1].dir: /srcdir
c906108c
SS
21906
21907 The line number information for list0.c has to end up in a single
4f1520fb
FR
21908 subfile, so that `break /srcdir/list0.c:1' works as expected.
21909 start_subfile will ensure that this happens provided that we pass the
21910 concatenation of files.files[1].dir and files.files[1].name as the
21911 subfile's name. */
c906108c
SS
21912
21913static void
804d2729
TT
21914dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21915 const char *dirname)
c906108c 21916{
43816ebc 21917 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21918
4d663531 21919 /* In order not to lose the line information directory,
4f1520fb
FR
21920 we concatenate it to the filename when it makes sense.
21921 Note that the Dwarf3 standard says (speaking of filenames in line
21922 information): ``The directory index is ignored for file names
21923 that represent full path names''. Thus ignoring dirname in the
21924 `else' branch below isn't an issue. */
c906108c 21925
d5166ae1 21926 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21927 {
43816ebc
TT
21928 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21929 filename = copy.get ();
d521ce57 21930 }
c906108c 21931
c24bdb02 21932 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21933}
21934
804d2729
TT
21935/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21936 buildsym_compunit constructor. */
f4dc4d17 21937
c24bdb02
KS
21938struct compunit_symtab *
21939dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21940 CORE_ADDR low_pc)
f4dc4d17 21941{
c24bdb02 21942 gdb_assert (m_builder == nullptr);
43f3e411 21943
c24bdb02 21944 m_builder.reset (new struct buildsym_compunit
f6e649dd 21945 (this->per_objfile->objfile,
c24bdb02 21946 name, comp_dir, language, low_pc));
93b8bea4 21947
c24bdb02 21948 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21949
c24bdb02
KS
21950 get_builder ()->record_debugformat ("DWARF 2");
21951 get_builder ()->record_producer (producer);
f4dc4d17 21952
c24bdb02 21953 processing_has_namespace_info = false;
43f3e411 21954
c24bdb02 21955 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21956}
21957
4c2df51b
DJ
21958static void
21959var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21960 struct dwarf2_cu *cu)
4c2df51b 21961{
5e22e966 21962 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21963 struct comp_unit_head *cu_header = &cu->header;
21964
4c2df51b
DJ
21965 /* NOTE drow/2003-01-30: There used to be a comment and some special
21966 code here to turn a symbol with DW_AT_external and a
21967 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21968 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21969 with some versions of binutils) where shared libraries could have
21970 relocations against symbols in their debug information - the
21971 minimal symbol would have the right address, but the debug info
21972 would not. It's no longer necessary, because we will explicitly
21973 apply relocations when we read in the debug information now. */
21974
21975 /* A DW_AT_location attribute with no contents indicates that a
21976 variable has been optimized away. */
9d2246fc 21977 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21978 {
f1e6e072 21979 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21980 return;
21981 }
21982
21983 /* Handle one degenerate form of location expression specially, to
21984 preserve GDB's previous behavior when section offsets are
336d760d
AT
21985 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21986 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21987
9d2246fc
TT
21988 if (attr->form_is_block ())
21989 {
21990 struct dwarf_block *block = attr->as_block ();
21991
21992 if ((block->data[0] == DW_OP_addr
21993 && block->size == 1 + cu_header->addr_size)
21994 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21995 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21996 && (block->size
21997 == 1 + leb128_size (&block->data[1]))))
21998 {
21999 unsigned int dummy;
22000
22001 if (block->data[0] == DW_OP_addr)
22002 SET_SYMBOL_VALUE_ADDRESS
22003 (sym, cu->header.read_address (objfile->obfd,
22004 block->data + 1,
38583298 22005 &dummy));
9d2246fc
TT
22006 else
22007 SET_SYMBOL_VALUE_ADDRESS
22008 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22009 &dummy));
22010 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22011 fixup_symbol_section (sym, objfile);
22012 SET_SYMBOL_VALUE_ADDRESS
22013 (sym,
22014 SYMBOL_VALUE_ADDRESS (sym)
a52d653e 22015 + objfile->section_offsets[sym->section_index ()]);
9d2246fc
TT
22016 return;
22017 }
4c2df51b
DJ
22018 }
22019
22020 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22021 expression evaluator, and use LOC_COMPUTED only when necessary
22022 (i.e. when the value of a register or memory location is
22023 referenced, or a thread-local block, etc.). Then again, it might
22024 not be worthwhile. I'm assuming that it isn't unless performance
22025 or memory numbers show me otherwise. */
22026
f1e6e072 22027 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22028
f1e6e072 22029 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22030 cu->has_loclist = true;
4c2df51b
DJ
22031}
22032
c906108c
SS
22033/* Given a pointer to a DWARF information entry, figure out if we need
22034 to make a symbol table entry for it, and if so, create a new entry
22035 and return a pointer to it.
22036 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22037 used the passed type.
22038 If SPACE is not NULL, use it to hold the new symbol. If it is
22039 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22040
22041static struct symbol *
5e2db402
TT
22042new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22043 struct symbol *space)
c906108c 22044{
976ca316
SM
22045 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22046 struct objfile *objfile = per_objfile->objfile;
08feed99 22047 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22048 struct symbol *sym = NULL;
15d034d0 22049 const char *name;
c906108c
SS
22050 struct attribute *attr = NULL;
22051 struct attribute *attr2 = NULL;
e142c38c 22052 CORE_ADDR baseaddr;
e37fd15a
SW
22053 struct pending **list_to_add = NULL;
22054
edb3359d 22055 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22056
b3b3bada 22057 baseaddr = objfile->text_section_offset ();
c906108c 22058
94af9270 22059 name = dwarf2_name (die, cu);
c906108c
SS
22060 if (name)
22061 {
34eaf542 22062 int suppress_add = 0;
94af9270 22063
34eaf542
TT
22064 if (space)
22065 sym = space;
22066 else
8c14c3a3 22067 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22068 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22069
22070 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22071 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22072 /* Fortran does not have mangling standard and the mangling does differ
22073 between gfortran, iFort etc. */
bcfe6157
TT
22074 const char *physname
22075 = (cu->language == language_fortran
22076 ? dwarf2_full_name (name, die, cu)
22077 : dwarf2_physname (name, die, cu));
22078 const char *linkagename = dw2_linkage_name (die, cu);
22079
22080 if (linkagename == nullptr || cu->language == language_ada)
22081 sym->set_linkage_name (physname);
22082 else
22083 {
22084 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22085 sym->set_linkage_name (linkagename);
22086 }
f55ee35c 22087
c906108c 22088 /* Default assumptions.
dda83cd7 22089 Use the passed type or decode it from the die. */
176620f1 22090 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22091 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22092 if (type != NULL)
22093 SYMBOL_TYPE (sym) = type;
22094 else
e7c27a73 22095 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22096 attr = dwarf2_attr (die,
22097 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22098 cu);
435d3d88 22099 if (attr != nullptr)
529908cb 22100 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22101
edb3359d
DJ
22102 attr = dwarf2_attr (die,
22103 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22104 cu);
aa659cfa 22105 if (attr != nullptr && attr->is_nonnegative ())
cb1df416 22106 {
529908cb 22107 file_name_index file_index
aa659cfa 22108 = (file_name_index) attr->as_nonnegative ();
8c43009f 22109 struct file_entry *fe;
9a619af0 22110
ecfb656c
PA
22111 if (cu->line_header != NULL)
22112 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22113 else
22114 fe = NULL;
22115
22116 if (fe == NULL)
b98664d3 22117 complaint (_("file index out of range"));
8c43009f
PA
22118 else
22119 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22120 }
22121
c906108c
SS
22122 switch (die->tag)
22123 {
22124 case DW_TAG_label:
e142c38c 22125 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22126 if (attr != nullptr)
3e29f34a
MR
22127 {
22128 CORE_ADDR addr;
22129
95f982e5 22130 addr = attr->as_address ();
3e29f34a 22131 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22132 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22133 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22134 }
8f5c6526
TV
22135 else
22136 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22137 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22138 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22139 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22140 break;
22141 case DW_TAG_subprogram:
22142 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22143 finish_block. */
f1e6e072 22144 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22145 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22146 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22147 || cu->language == language_ada
22148 || cu->language == language_fortran)
c906108c 22149 {
dda83cd7
SM
22150 /* Subprograms marked external are stored as a global symbol.
22151 Ada and Fortran subprograms, whether marked external or
22152 not, are always stored as a global symbol, because we want
22153 to be able to access them globally. For instance, we want
22154 to be able to break on a nested subprogram without having
22155 to specify the context. */
c24bdb02 22156 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22157 }
22158 else
22159 {
e37fd15a 22160 list_to_add = cu->list_in_scope;
c906108c
SS
22161 }
22162 break;
edb3359d
DJ
22163 case DW_TAG_inlined_subroutine:
22164 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22165 finish_block. */
f1e6e072 22166 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22167 SYMBOL_INLINED (sym) = 1;
481860b3 22168 list_to_add = cu->list_in_scope;
edb3359d 22169 break;
34eaf542
TT
22170 case DW_TAG_template_value_param:
22171 suppress_add = 1;
22172 /* Fall through. */
72929c62 22173 case DW_TAG_constant:
c906108c 22174 case DW_TAG_variable:
254e6b9e 22175 case DW_TAG_member:
0963b4bd
MS
22176 /* Compilation with minimal debug info may result in
22177 variables with missing type entries. Change the
22178 misleading `void' type to something sensible. */
78134374 22179 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22180 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22181
e142c38c 22182 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22183 /* In the case of DW_TAG_member, we should only be called for
22184 static const members. */
22185 if (die->tag == DW_TAG_member)
22186 {
3863f96c
DE
22187 /* dwarf2_add_field uses die_is_declaration,
22188 so we do the same. */
254e6b9e
DE
22189 gdb_assert (die_is_declaration (die, cu));
22190 gdb_assert (attr);
22191 }
435d3d88 22192 if (attr != nullptr)
c906108c 22193 {
e7c27a73 22194 dwarf2_const_value (attr, sym, cu);
e142c38c 22195 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22196 if (!suppress_add)
34eaf542 22197 {
c45bc3f8 22198 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22199 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22200 else
e37fd15a 22201 list_to_add = cu->list_in_scope;
34eaf542 22202 }
c906108c
SS
22203 break;
22204 }
e142c38c 22205 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22206 if (attr != nullptr)
c906108c 22207 {
e7c27a73 22208 var_decode_location (attr, sym, cu);
e142c38c 22209 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22210
22211 /* Fortran explicitly imports any global symbols to the local
22212 scope by DW_TAG_common_block. */
22213 if (cu->language == language_fortran && die->parent
22214 && die->parent->tag == DW_TAG_common_block)
22215 attr2 = NULL;
22216
caac4577
JG
22217 if (SYMBOL_CLASS (sym) == LOC_STATIC
22218 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22219 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22220 {
22221 /* When a static variable is eliminated by the linker,
22222 the corresponding debug information is not stripped
22223 out, but the variable address is set to null;
22224 do not add such variables into symbol table. */
22225 }
c45bc3f8 22226 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22227 {
4b610737
TT
22228 if (SYMBOL_CLASS (sym) == LOC_STATIC
22229 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22230 && per_objfile->per_bfd->can_copy)
4b610737
TT
22231 {
22232 /* A global static variable might be subject to
22233 copy relocation. We first check for a local
22234 minsym, though, because maybe the symbol was
22235 marked hidden, in which case this would not
22236 apply. */
22237 bound_minimal_symbol found
22238 = (lookup_minimal_symbol_linkage
987012b8 22239 (sym->linkage_name (), objfile));
4b610737
TT
22240 if (found.minsym != nullptr)
22241 sym->maybe_copied = 1;
22242 }
f55ee35c 22243
1c809c68
TT
22244 /* A variable with DW_AT_external is never static,
22245 but it may be block-scoped. */
804d2729 22246 list_to_add
c24bdb02
KS
22247 = ((cu->list_in_scope
22248 == cu->get_builder ()->get_file_symbols ())
22249 ? cu->get_builder ()->get_global_symbols ()
804d2729 22250 : cu->list_in_scope);
1c809c68 22251 }
c906108c 22252 else
e37fd15a 22253 list_to_add = cu->list_in_scope;
c906108c
SS
22254 }
22255 else
22256 {
22257 /* We do not know the address of this symbol.
dda83cd7
SM
22258 If it is an external symbol and we have type information
22259 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22260 The address of the variable will then be determined from
22261 the minimal symbol table whenever the variable is
22262 referenced. */
e142c38c 22263 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22264
22265 /* Fortran explicitly imports any global symbols to the local
22266 scope by DW_TAG_common_block. */
22267 if (cu->language == language_fortran && die->parent
22268 && die->parent->tag == DW_TAG_common_block)
22269 {
22270 /* SYMBOL_CLASS doesn't matter here because
22271 read_common_block is going to reset it. */
22272 if (!suppress_add)
22273 list_to_add = cu->list_in_scope;
22274 }
c45bc3f8 22275 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22276 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22277 {
0fe7935b
DJ
22278 /* A variable with DW_AT_external is never static, but it
22279 may be block-scoped. */
804d2729 22280 list_to_add
c24bdb02
KS
22281 = ((cu->list_in_scope
22282 == cu->get_builder ()->get_file_symbols ())
22283 ? cu->get_builder ()->get_global_symbols ()
804d2729 22284 : cu->list_in_scope);
0fe7935b 22285
f1e6e072 22286 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22287 }
442ddf59
JK
22288 else if (!die_is_declaration (die, cu))
22289 {
22290 /* Use the default LOC_OPTIMIZED_OUT class. */
22291 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22292 if (!suppress_add)
22293 list_to_add = cu->list_in_scope;
442ddf59 22294 }
c906108c
SS
22295 }
22296 break;
22297 case DW_TAG_formal_parameter:
a60f3166
TT
22298 {
22299 /* If we are inside a function, mark this as an argument. If
22300 not, we might be looking at an argument to an inlined function
22301 when we do not have enough information to show inlined frames;
22302 pretend it's a local variable in that case so that the user can
22303 still see it. */
804d2729 22304 struct context_stack *curr
c24bdb02 22305 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22306 if (curr != nullptr && curr->name != nullptr)
22307 SYMBOL_IS_ARGUMENT (sym) = 1;
22308 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22309 if (attr != nullptr)
a60f3166
TT
22310 {
22311 var_decode_location (attr, sym, cu);
22312 }
22313 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22314 if (attr != nullptr)
a60f3166
TT
22315 {
22316 dwarf2_const_value (attr, sym, cu);
22317 }
f346a30d 22318
a60f3166
TT
22319 list_to_add = cu->list_in_scope;
22320 }
c906108c
SS
22321 break;
22322 case DW_TAG_unspecified_parameters:
22323 /* From varargs functions; gdb doesn't seem to have any
22324 interest in this information, so just ignore it for now.
22325 (FIXME?) */
22326 break;
34eaf542
TT
22327 case DW_TAG_template_type_param:
22328 suppress_add = 1;
22329 /* Fall through. */
c906108c 22330 case DW_TAG_class_type:
680b30c7 22331 case DW_TAG_interface_type:
c906108c
SS
22332 case DW_TAG_structure_type:
22333 case DW_TAG_union_type:
72019c9c 22334 case DW_TAG_set_type:
c906108c 22335 case DW_TAG_enumeration_type:
f1e6e072 22336 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22337 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22338
63d06c5c 22339 {
9c37b5ae 22340 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22341 really ever be static objects: otherwise, if you try
22342 to, say, break of a class's method and you're in a file
22343 which doesn't mention that class, it won't work unless
22344 the check for all static symbols in lookup_symbol_aux
22345 saves you. See the OtherFileClass tests in
22346 gdb.c++/namespace.exp. */
22347
e37fd15a 22348 if (!suppress_add)
34eaf542 22349 {
c24bdb02 22350 buildsym_compunit *builder = cu->get_builder ();
804d2729 22351 list_to_add
c24bdb02 22352 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22353 && cu->language == language_cplus
c24bdb02 22354 ? builder->get_global_symbols ()
804d2729 22355 : cu->list_in_scope);
63d06c5c 22356
64382290 22357 /* The semantics of C++ state that "struct foo {
9c37b5ae 22358 ... }" also defines a typedef for "foo". */
64382290 22359 if (cu->language == language_cplus
45280282 22360 || cu->language == language_ada
c44af4eb
TT
22361 || cu->language == language_d
22362 || cu->language == language_rust)
64382290
TT
22363 {
22364 /* The symbol's name is already allocated along
22365 with this objfile, so we don't need to
22366 duplicate it for the type. */
7d93a1e0 22367 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22368 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22369 }
63d06c5c
DC
22370 }
22371 }
c906108c
SS
22372 break;
22373 case DW_TAG_typedef:
f1e6e072 22374 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22375 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22376 list_to_add = cu->list_in_scope;
63d06c5c 22377 break;
d8f62e84 22378 case DW_TAG_array_type:
c906108c 22379 case DW_TAG_base_type:
dda83cd7 22380 case DW_TAG_subrange_type:
f1e6e072 22381 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22382 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22383 list_to_add = cu->list_in_scope;
c906108c
SS
22384 break;
22385 case DW_TAG_enumerator:
e142c38c 22386 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22387 if (attr != nullptr)
c906108c 22388 {
e7c27a73 22389 dwarf2_const_value (attr, sym, cu);
c906108c 22390 }
63d06c5c
DC
22391 {
22392 /* NOTE: carlton/2003-11-10: See comment above in the
22393 DW_TAG_class_type, etc. block. */
22394
804d2729 22395 list_to_add
c24bdb02 22396 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22397 && cu->language == language_cplus
c24bdb02 22398 ? cu->get_builder ()->get_global_symbols ()
804d2729 22399 : cu->list_in_scope);
63d06c5c 22400 }
c906108c 22401 break;
74921315 22402 case DW_TAG_imported_declaration:
5c4e30ca 22403 case DW_TAG_namespace:
f1e6e072 22404 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22405 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22406 break;
530e8392
KB
22407 case DW_TAG_module:
22408 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22409 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22410 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22411 break;
4357ac6c 22412 case DW_TAG_common_block:
f1e6e072 22413 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22414 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22415 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22416 break;
c906108c
SS
22417 default:
22418 /* Not a tag we recognize. Hopefully we aren't processing
22419 trash data, but since we must specifically ignore things
22420 we don't recognize, there is nothing else we should do at
0963b4bd 22421 this point. */
b98664d3 22422 complaint (_("unsupported tag: '%s'"),
4d3c2250 22423 dwarf_tag_name (die->tag));
c906108c
SS
22424 break;
22425 }
df8a16a1 22426
e37fd15a
SW
22427 if (suppress_add)
22428 {
22429 sym->hash_next = objfile->template_symbols;
22430 objfile->template_symbols = sym;
22431 list_to_add = NULL;
22432 }
22433
22434 if (list_to_add != NULL)
d3cb6808 22435 add_symbol_to_list (sym, list_to_add);
e37fd15a 22436
df8a16a1
DJ
22437 /* For the benefit of old versions of GCC, check for anonymous
22438 namespaces based on the demangled name. */
4d4ec4e5 22439 if (!cu->processing_has_namespace_info
94af9270 22440 && cu->language == language_cplus)
c24bdb02 22441 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22442 }
22443 return (sym);
22444}
22445
98bfdba5
PA
22446/* Given an attr with a DW_FORM_dataN value in host byte order,
22447 zero-extend it as appropriate for the symbol's type. The DWARF
22448 standard (v4) is not entirely clear about the meaning of using
22449 DW_FORM_dataN for a constant with a signed type, where the type is
22450 wider than the data. The conclusion of a discussion on the DWARF
22451 list was that this is unspecified. We choose to always zero-extend
22452 because that is the interpretation long in use by GCC. */
c906108c 22453
98bfdba5 22454static gdb_byte *
ff39bb5e 22455dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22456 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22457{
5e22e966 22458 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22459 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22460 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22461 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22462
22463 if (bits < sizeof (*value) * 8)
22464 {
22465 l &= ((LONGEST) 1 << bits) - 1;
22466 *value = l;
22467 }
22468 else if (bits == sizeof (*value) * 8)
22469 *value = l;
22470 else
22471 {
224c3ddb 22472 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22473 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22474 return bytes;
22475 }
22476
22477 return NULL;
22478}
22479
22480/* Read a constant value from an attribute. Either set *VALUE, or if
22481 the value does not fit in *VALUE, set *BYTES - either already
22482 allocated on the objfile obstack, or newly allocated on OBSTACK,
22483 or, set *BATON, if we translated the constant to a location
22484 expression. */
22485
22486static void
ff39bb5e 22487dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22488 const char *name, struct obstack *obstack,
22489 struct dwarf2_cu *cu,
d521ce57 22490 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22491 struct dwarf2_locexpr_baton **baton)
22492{
5e22e966 22493 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22494 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22495 struct comp_unit_head *cu_header = &cu->header;
c906108c 22496 struct dwarf_block *blk;
98bfdba5
PA
22497 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22498 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22499
22500 *value = 0;
22501 *bytes = NULL;
22502 *baton = NULL;
c906108c
SS
22503
22504 switch (attr->form)
22505 {
22506 case DW_FORM_addr:
336d760d 22507 case DW_FORM_addrx:
3019eac3 22508 case DW_FORM_GNU_addr_index:
ac56253d 22509 {
ac56253d
TT
22510 gdb_byte *data;
22511
98bfdba5
PA
22512 if (TYPE_LENGTH (type) != cu_header->addr_size)
22513 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22514 cu_header->addr_size,
98bfdba5 22515 TYPE_LENGTH (type));
ac56253d
TT
22516 /* Symbols of this form are reasonably rare, so we just
22517 piggyback on the existing location code rather than writing
22518 a new implementation of symbol_computed_ops. */
8d749320 22519 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22520 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22521 (*baton)->per_cu = cu->per_cu;
22522 gdb_assert ((*baton)->per_cu);
ac56253d 22523
98bfdba5 22524 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22525 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22526 (*baton)->data = data;
ac56253d
TT
22527
22528 data[0] = DW_OP_addr;
22529 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22530 byte_order, attr->as_address ());
ac56253d 22531 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22532 }
c906108c 22533 break;
4ac36638 22534 case DW_FORM_string:
93b5768b 22535 case DW_FORM_strp:
cf532bd1 22536 case DW_FORM_strx:
3019eac3 22537 case DW_FORM_GNU_str_index:
36586728 22538 case DW_FORM_GNU_strp_alt:
c6481205 22539 /* The string is already allocated on the objfile obstack, point
98bfdba5 22540 directly to it. */
2c830f54 22541 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22542 break;
c906108c
SS
22543 case DW_FORM_block1:
22544 case DW_FORM_block2:
22545 case DW_FORM_block4:
22546 case DW_FORM_block:
2dc7f7b3 22547 case DW_FORM_exprloc:
0224619f 22548 case DW_FORM_data16:
9d2246fc 22549 blk = attr->as_block ();
98bfdba5
PA
22550 if (TYPE_LENGTH (type) != blk->size)
22551 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22552 TYPE_LENGTH (type));
22553 *bytes = blk->data;
c906108c 22554 break;
2df3850c
JM
22555
22556 /* The DW_AT_const_value attributes are supposed to carry the
22557 symbol's value "represented as it would be on the target
22558 architecture." By the time we get here, it's already been
22559 converted to host endianness, so we just need to sign- or
22560 zero-extend it as appropriate. */
22561 case DW_FORM_data1:
3aef2284 22562 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22563 break;
c906108c 22564 case DW_FORM_data2:
3aef2284 22565 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22566 break;
c906108c 22567 case DW_FORM_data4:
3aef2284 22568 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22569 break;
c906108c 22570 case DW_FORM_data8:
3aef2284 22571 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22572 break;
22573
c906108c 22574 case DW_FORM_sdata:
663c44ac 22575 case DW_FORM_implicit_const:
1bc397c5 22576 *value = attr->as_signed ();
2df3850c
JM
22577 break;
22578
c906108c 22579 case DW_FORM_udata:
529908cb 22580 *value = attr->as_unsigned ();
c906108c 22581 break;
2df3850c 22582
c906108c 22583 default:
b98664d3 22584 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22585 dwarf_form_name (attr->form));
98bfdba5 22586 *value = 0;
c906108c
SS
22587 break;
22588 }
22589}
22590
2df3850c 22591
98bfdba5
PA
22592/* Copy constant value from an attribute to a symbol. */
22593
2df3850c 22594static void
ff39bb5e 22595dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22596 struct dwarf2_cu *cu)
2df3850c 22597{
5e22e966 22598 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22599 LONGEST value;
d521ce57 22600 const gdb_byte *bytes;
98bfdba5 22601 struct dwarf2_locexpr_baton *baton;
2df3850c 22602
98bfdba5 22603 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22604 sym->print_name (),
98bfdba5
PA
22605 &objfile->objfile_obstack, cu,
22606 &value, &bytes, &baton);
2df3850c 22607
98bfdba5
PA
22608 if (baton != NULL)
22609 {
98bfdba5 22610 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22611 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22612 }
22613 else if (bytes != NULL)
22614 {
22615 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22616 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22617 }
22618 else
22619 {
22620 SYMBOL_VALUE (sym) = value;
f1e6e072 22621 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22622 }
2df3850c
JM
22623}
22624
c906108c
SS
22625/* Return the type of the die in question using its DW_AT_type attribute. */
22626
22627static struct type *
e7c27a73 22628die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22629{
c906108c 22630 struct attribute *type_attr;
c906108c 22631
e142c38c 22632 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22633 if (!type_attr)
22634 {
5e22e966 22635 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22636 /* A missing DW_AT_type represents a void type. */
518817b3 22637 return objfile_type (objfile)->builtin_void;
c906108c 22638 }
348e048f 22639
673bfd45 22640 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22641}
22642
b4ba55a1
JB
22643/* True iff CU's producer generates GNAT Ada auxiliary information
22644 that allows to find parallel types through that information instead
22645 of having to do expensive parallel lookups by type name. */
22646
22647static int
22648need_gnat_info (struct dwarf2_cu *cu)
22649{
de4cb04a
JB
22650 /* Assume that the Ada compiler was GNAT, which always produces
22651 the auxiliary information. */
22652 return (cu->language == language_ada);
b4ba55a1
JB
22653}
22654
b4ba55a1
JB
22655/* Return the auxiliary type of the die in question using its
22656 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22657 attribute is not present. */
22658
22659static struct type *
22660die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22661{
b4ba55a1 22662 struct attribute *type_attr;
b4ba55a1
JB
22663
22664 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22665 if (!type_attr)
22666 return NULL;
22667
673bfd45 22668 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22669}
22670
22671/* If DIE has a descriptive_type attribute, then set the TYPE's
22672 descriptive type accordingly. */
22673
22674static void
22675set_descriptive_type (struct type *type, struct die_info *die,
22676 struct dwarf2_cu *cu)
22677{
22678 struct type *descriptive_type = die_descriptive_type (die, cu);
22679
22680 if (descriptive_type)
22681 {
22682 ALLOCATE_GNAT_AUX_TYPE (type);
22683 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22684 }
22685}
22686
c906108c
SS
22687/* Return the containing type of the die in question using its
22688 DW_AT_containing_type attribute. */
22689
22690static struct type *
e7c27a73 22691die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22692{
c906108c 22693 struct attribute *type_attr;
5e22e966 22694 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22695
e142c38c 22696 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22697 if (!type_attr)
22698 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22699 "[in module %s]"), objfile_name (objfile));
33ac96f0 22700
673bfd45 22701 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22702}
22703
ac9ec31b
DE
22704/* Return an error marker type to use for the ill formed type in DIE/CU. */
22705
22706static struct type *
22707build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22708{
976ca316
SM
22709 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22710 struct objfile *objfile = per_objfile->objfile;
528e1572 22711 char *saved;
ac9ec31b 22712
528e1572
SM
22713 std::string message
22714 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22715 objfile_name (objfile),
22716 sect_offset_str (cu->header.sect_off),
22717 sect_offset_str (die->sect_off));
efba19b0 22718 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22719
19f392bc 22720 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22721}
22722
673bfd45 22723/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22724 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22725 DW_AT_containing_type.
673bfd45
DE
22726 If there is no type substitute an error marker. */
22727
c906108c 22728static struct type *
ff39bb5e 22729lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22730 struct dwarf2_cu *cu)
c906108c 22731{
976ca316
SM
22732 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22733 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22734 struct type *this_type;
22735
ac9ec31b
DE
22736 gdb_assert (attr->name == DW_AT_type
22737 || attr->name == DW_AT_GNAT_descriptive_type
22738 || attr->name == DW_AT_containing_type);
22739
673bfd45
DE
22740 /* First see if we have it cached. */
22741
36586728
TT
22742 if (attr->form == DW_FORM_GNU_ref_alt)
22743 {
22744 struct dwarf2_per_cu_data *per_cu;
0826b30a 22745 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22746
976ca316
SM
22747 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22748 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22749 }
cd6c91b4 22750 else if (attr->form_is_ref ())
673bfd45 22751 {
0826b30a 22752 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22753
976ca316 22754 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22755 }
55f1336d 22756 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22757 {
630ed6b9 22758 ULONGEST signature = attr->as_signature ();
673bfd45 22759
ac9ec31b 22760 return get_signatured_type (die, signature, cu);
673bfd45
DE
22761 }
22762 else
22763 {
b98664d3 22764 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22765 " at %s [in module %s]"),
22766 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22767 objfile_name (objfile));
ac9ec31b 22768 return build_error_marker_type (cu, die);
673bfd45
DE
22769 }
22770
22771 /* If not cached we need to read it in. */
22772
22773 if (this_type == NULL)
22774 {
ac9ec31b 22775 struct die_info *type_die = NULL;
673bfd45
DE
22776 struct dwarf2_cu *type_cu = cu;
22777
cd6c91b4 22778 if (attr->form_is_ref ())
ac9ec31b
DE
22779 type_die = follow_die_ref (die, attr, &type_cu);
22780 if (type_die == NULL)
22781 return build_error_marker_type (cu, die);
22782 /* If we find the type now, it's probably because the type came
3019eac3
DE
22783 from an inter-CU reference and the type's CU got expanded before
22784 ours. */
ac9ec31b 22785 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22786 }
22787
22788 /* If we still don't have a type use an error marker. */
22789
22790 if (this_type == NULL)
ac9ec31b 22791 return build_error_marker_type (cu, die);
673bfd45 22792
f792889a 22793 return this_type;
c906108c
SS
22794}
22795
673bfd45
DE
22796/* Return the type in DIE, CU.
22797 Returns NULL for invalid types.
22798
02142a6c 22799 This first does a lookup in die_type_hash,
673bfd45
DE
22800 and only reads the die in if necessary.
22801
22802 NOTE: This can be called when reading in partial or full symbols. */
22803
f792889a 22804static struct type *
e7c27a73 22805read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22806{
f792889a
DJ
22807 struct type *this_type;
22808
22809 this_type = get_die_type (die, cu);
22810 if (this_type)
22811 return this_type;
22812
673bfd45
DE
22813 return read_type_die_1 (die, cu);
22814}
22815
22816/* Read the type in DIE, CU.
22817 Returns NULL for invalid types. */
22818
22819static struct type *
22820read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22821{
22822 struct type *this_type = NULL;
22823
c906108c
SS
22824 switch (die->tag)
22825 {
22826 case DW_TAG_class_type:
680b30c7 22827 case DW_TAG_interface_type:
c906108c
SS
22828 case DW_TAG_structure_type:
22829 case DW_TAG_union_type:
f792889a 22830 this_type = read_structure_type (die, cu);
c906108c
SS
22831 break;
22832 case DW_TAG_enumeration_type:
f792889a 22833 this_type = read_enumeration_type (die, cu);
c906108c
SS
22834 break;
22835 case DW_TAG_subprogram:
22836 case DW_TAG_subroutine_type:
edb3359d 22837 case DW_TAG_inlined_subroutine:
f792889a 22838 this_type = read_subroutine_type (die, cu);
c906108c
SS
22839 break;
22840 case DW_TAG_array_type:
f792889a 22841 this_type = read_array_type (die, cu);
c906108c 22842 break;
72019c9c 22843 case DW_TAG_set_type:
f792889a 22844 this_type = read_set_type (die, cu);
72019c9c 22845 break;
c906108c 22846 case DW_TAG_pointer_type:
f792889a 22847 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22848 break;
22849 case DW_TAG_ptr_to_member_type:
f792889a 22850 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22851 break;
22852 case DW_TAG_reference_type:
4297a3f0
AV
22853 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22854 break;
22855 case DW_TAG_rvalue_reference_type:
22856 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22857 break;
22858 case DW_TAG_const_type:
f792889a 22859 this_type = read_tag_const_type (die, cu);
c906108c
SS
22860 break;
22861 case DW_TAG_volatile_type:
f792889a 22862 this_type = read_tag_volatile_type (die, cu);
c906108c 22863 break;
06d66ee9
TT
22864 case DW_TAG_restrict_type:
22865 this_type = read_tag_restrict_type (die, cu);
22866 break;
c906108c 22867 case DW_TAG_string_type:
f792889a 22868 this_type = read_tag_string_type (die, cu);
c906108c
SS
22869 break;
22870 case DW_TAG_typedef:
f792889a 22871 this_type = read_typedef (die, cu);
c906108c 22872 break;
a02abb62 22873 case DW_TAG_subrange_type:
f792889a 22874 this_type = read_subrange_type (die, cu);
a02abb62 22875 break;
c906108c 22876 case DW_TAG_base_type:
f792889a 22877 this_type = read_base_type (die, cu);
c906108c 22878 break;
81a17f79 22879 case DW_TAG_unspecified_type:
f792889a 22880 this_type = read_unspecified_type (die, cu);
81a17f79 22881 break;
0114d602
DJ
22882 case DW_TAG_namespace:
22883 this_type = read_namespace_type (die, cu);
22884 break;
f55ee35c
JK
22885 case DW_TAG_module:
22886 this_type = read_module_type (die, cu);
22887 break;
a2c2acaf
MW
22888 case DW_TAG_atomic_type:
22889 this_type = read_tag_atomic_type (die, cu);
22890 break;
c906108c 22891 default:
b98664d3 22892 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22893 dwarf_tag_name (die->tag));
c906108c
SS
22894 break;
22895 }
63d06c5c 22896
f792889a 22897 return this_type;
63d06c5c
DC
22898}
22899
abc72ce4
DE
22900/* See if we can figure out if the class lives in a namespace. We do
22901 this by looking for a member function; its demangled name will
22902 contain namespace info, if there is any.
22903 Return the computed name or NULL.
22904 Space for the result is allocated on the objfile's obstack.
22905 This is the full-die version of guess_partial_die_structure_name.
22906 In this case we know DIE has no useful parent. */
22907
43816ebc 22908static const char *
abc72ce4
DE
22909guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22910{
22911 struct die_info *spec_die;
22912 struct dwarf2_cu *spec_cu;
22913 struct die_info *child;
5e22e966 22914 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22915
22916 spec_cu = cu;
22917 spec_die = die_specification (die, &spec_cu);
22918 if (spec_die != NULL)
22919 {
22920 die = spec_die;
22921 cu = spec_cu;
22922 }
22923
22924 for (child = die->child;
22925 child != NULL;
22926 child = child->sibling)
22927 {
22928 if (child->tag == DW_TAG_subprogram)
22929 {
73b9be8b 22930 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22931
7d45c7c3 22932 if (linkage_name != NULL)
abc72ce4 22933 {
43816ebc 22934 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22935 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22936 const char *name = NULL;
abc72ce4
DE
22937
22938 if (actual_name != NULL)
22939 {
15d034d0 22940 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22941
22942 if (die_name != NULL
43816ebc 22943 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22944 {
22945 /* Strip off the class name from the full name.
22946 We want the prefix. */
22947 int die_name_len = strlen (die_name);
43816ebc
TT
22948 int actual_name_len = strlen (actual_name.get ());
22949 const char *ptr = actual_name.get ();
abc72ce4
DE
22950
22951 /* Test for '::' as a sanity check. */
22952 if (actual_name_len > die_name_len + 2
43816ebc 22953 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22954 name = obstack_strndup (
e3b94546 22955 &objfile->per_bfd->storage_obstack,
43816ebc 22956 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22957 }
22958 }
abc72ce4
DE
22959 return name;
22960 }
22961 }
22962 }
22963
22964 return NULL;
22965}
22966
96408a79
SA
22967/* GCC might emit a nameless typedef that has a linkage name. Determine the
22968 prefix part in such case. See
22969 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22970
a121b7c1 22971static const char *
96408a79
SA
22972anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22973{
22974 struct attribute *attr;
e6a959d6 22975 const char *base;
96408a79
SA
22976
22977 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22978 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22979 return NULL;
22980
7d45c7c3 22981 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22982 return NULL;
22983
73b9be8b 22984 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22985 const char *attr_name = attr->as_string ();
22986 if (attr == NULL || attr_name == NULL)
96408a79
SA
22987 return NULL;
22988
22989 /* dwarf2_name had to be already called. */
3b64bf15 22990 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22991
22992 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22993 base = strrchr (attr_name, ':');
22994 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22995 return "";
22996
5e22e966 22997 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22998 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22999 attr_name,
23000 &base[-1] - attr_name);
96408a79
SA
23001}
23002
fdde2d81 23003/* Return the name of the namespace/class that DIE is defined within,
0114d602 23004 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23005
0114d602
DJ
23006 For example, if we're within the method foo() in the following
23007 code:
23008
23009 namespace N {
23010 class C {
23011 void foo () {
23012 }
23013 };
23014 }
23015
23016 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23017
0d5cff50 23018static const char *
e142c38c 23019determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23020{
976ca316 23021 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23022 struct die_info *parent, *spec_die;
23023 struct dwarf2_cu *spec_cu;
23024 struct type *parent_type;
a121b7c1 23025 const char *retval;
63d06c5c 23026
9c37b5ae 23027 if (cu->language != language_cplus
c44af4eb
TT
23028 && cu->language != language_fortran && cu->language != language_d
23029 && cu->language != language_rust)
0114d602
DJ
23030 return "";
23031
96408a79
SA
23032 retval = anonymous_struct_prefix (die, cu);
23033 if (retval)
23034 return retval;
23035
0114d602
DJ
23036 /* We have to be careful in the presence of DW_AT_specification.
23037 For example, with GCC 3.4, given the code
23038
23039 namespace N {
23040 void foo() {
23041 // Definition of N::foo.
23042 }
23043 }
23044
23045 then we'll have a tree of DIEs like this:
23046
23047 1: DW_TAG_compile_unit
23048 2: DW_TAG_namespace // N
23049 3: DW_TAG_subprogram // declaration of N::foo
23050 4: DW_TAG_subprogram // definition of N::foo
23051 DW_AT_specification // refers to die #3
23052
23053 Thus, when processing die #4, we have to pretend that we're in
23054 the context of its DW_AT_specification, namely the contex of die
23055 #3. */
23056 spec_cu = cu;
23057 spec_die = die_specification (die, &spec_cu);
23058 if (spec_die == NULL)
23059 parent = die->parent;
23060 else
63d06c5c 23061 {
0114d602
DJ
23062 parent = spec_die->parent;
23063 cu = spec_cu;
63d06c5c 23064 }
0114d602
DJ
23065
23066 if (parent == NULL)
23067 return "";
98bfdba5
PA
23068 else if (parent->building_fullname)
23069 {
23070 const char *name;
23071 const char *parent_name;
23072
23073 /* It has been seen on RealView 2.2 built binaries,
23074 DW_TAG_template_type_param types actually _defined_ as
23075 children of the parent class:
23076
23077 enum E {};
23078 template class <class Enum> Class{};
23079 Class<enum E> class_e;
23080
dda83cd7
SM
23081 1: DW_TAG_class_type (Class)
23082 2: DW_TAG_enumeration_type (E)
23083 3: DW_TAG_enumerator (enum1:0)
23084 3: DW_TAG_enumerator (enum2:1)
23085 ...
23086 2: DW_TAG_template_type_param
23087 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23088
23089 Besides being broken debug info, it can put GDB into an
23090 infinite loop. Consider:
23091
23092 When we're building the full name for Class<E>, we'll start
23093 at Class, and go look over its template type parameters,
23094 finding E. We'll then try to build the full name of E, and
23095 reach here. We're now trying to build the full name of E,
23096 and look over the parent DIE for containing scope. In the
23097 broken case, if we followed the parent DIE of E, we'd again
23098 find Class, and once again go look at its template type
23099 arguments, etc., etc. Simply don't consider such parent die
23100 as source-level parent of this die (it can't be, the language
23101 doesn't allow it), and break the loop here. */
23102 name = dwarf2_name (die, cu);
23103 parent_name = dwarf2_name (parent, cu);
b98664d3 23104 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23105 name ? name : "<unknown>",
23106 parent_name ? parent_name : "<unknown>");
23107 return "";
23108 }
63d06c5c 23109 else
0114d602
DJ
23110 switch (parent->tag)
23111 {
63d06c5c 23112 case DW_TAG_namespace:
0114d602 23113 parent_type = read_type_die (parent, cu);
acebe513
UW
23114 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23115 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23116 Work around this problem here. */
23117 if (cu->language == language_cplus
7d93a1e0 23118 && strcmp (parent_type->name (), "::") == 0)
acebe513 23119 return "";
0114d602 23120 /* We give a name to even anonymous namespaces. */
7d93a1e0 23121 return parent_type->name ();
63d06c5c 23122 case DW_TAG_class_type:
680b30c7 23123 case DW_TAG_interface_type:
63d06c5c 23124 case DW_TAG_structure_type:
0114d602 23125 case DW_TAG_union_type:
f55ee35c 23126 case DW_TAG_module:
0114d602 23127 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23128 if (parent_type->name () != NULL)
23129 return parent_type->name ();
0114d602
DJ
23130 else
23131 /* An anonymous structure is only allowed non-static data
23132 members; no typedefs, no member functions, et cetera.
23133 So it does not need a prefix. */
23134 return "";
abc72ce4 23135 case DW_TAG_compile_unit:
95554aad 23136 case DW_TAG_partial_unit:
abc72ce4
DE
23137 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23138 if (cu->language == language_cplus
976ca316 23139 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23140 && die->child != NULL
23141 && (die->tag == DW_TAG_class_type
23142 || die->tag == DW_TAG_structure_type
23143 || die->tag == DW_TAG_union_type))
23144 {
43816ebc 23145 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23146 if (name != NULL)
23147 return name;
23148 }
23149 return "";
0a4b0913
AB
23150 case DW_TAG_subprogram:
23151 /* Nested subroutines in Fortran get a prefix with the name
23152 of the parent's subroutine. */
23153 if (cu->language == language_fortran)
23154 {
23155 if ((die->tag == DW_TAG_subprogram)
23156 && (dwarf2_name (parent, cu) != NULL))
23157 return dwarf2_name (parent, cu);
23158 }
23159 return determine_prefix (parent, cu);
3d567982
TT
23160 case DW_TAG_enumeration_type:
23161 parent_type = read_type_die (parent, cu);
23162 if (TYPE_DECLARED_CLASS (parent_type))
23163 {
7d93a1e0
SM
23164 if (parent_type->name () != NULL)
23165 return parent_type->name ();
3d567982
TT
23166 return "";
23167 }
23168 /* Fall through. */
63d06c5c 23169 default:
8176b9b8 23170 return determine_prefix (parent, cu);
63d06c5c 23171 }
63d06c5c
DC
23172}
23173
3e43a32a
MS
23174/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23175 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23176 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23177 an obconcat, otherwise allocate storage for the result. The CU argument is
23178 used to determine the language and hence, the appropriate separator. */
987504bb 23179
f55ee35c 23180#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23181
23182static char *
f55ee35c 23183typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23184 int physname, struct dwarf2_cu *cu)
63d06c5c 23185{
f55ee35c 23186 const char *lead = "";
5c315b68 23187 const char *sep;
63d06c5c 23188
3e43a32a
MS
23189 if (suffix == NULL || suffix[0] == '\0'
23190 || prefix == NULL || prefix[0] == '\0')
987504bb 23191 sep = "";
45280282
IB
23192 else if (cu->language == language_d)
23193 {
23194 /* For D, the 'main' function could be defined in any module, but it
23195 should never be prefixed. */
23196 if (strcmp (suffix, "D main") == 0)
23197 {
23198 prefix = "";
23199 sep = "";
23200 }
23201 else
23202 sep = ".";
23203 }
f55ee35c
JK
23204 else if (cu->language == language_fortran && physname)
23205 {
23206 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23207 DW_AT_MIPS_linkage_name is preferred and used instead. */
23208
23209 lead = "__";
23210 sep = "_MOD_";
23211 }
987504bb
JJ
23212 else
23213 sep = "::";
63d06c5c 23214
6dd47d34
DE
23215 if (prefix == NULL)
23216 prefix = "";
23217 if (suffix == NULL)
23218 suffix = "";
23219
987504bb
JJ
23220 if (obs == NULL)
23221 {
3e43a32a 23222 char *retval
224c3ddb
SM
23223 = ((char *)
23224 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23225
f55ee35c
JK
23226 strcpy (retval, lead);
23227 strcat (retval, prefix);
6dd47d34
DE
23228 strcat (retval, sep);
23229 strcat (retval, suffix);
63d06c5c
DC
23230 return retval;
23231 }
987504bb
JJ
23232 else
23233 {
23234 /* We have an obstack. */
f55ee35c 23235 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23236 }
63d06c5c
DC
23237}
23238
71c25dea
TT
23239/* Get name of a die, return NULL if not found. */
23240
15d034d0
TT
23241static const char *
23242dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23243 struct objfile *objfile)
71c25dea
TT
23244{
23245 if (name && cu->language == language_cplus)
23246 {
596dc4ad
TT
23247 gdb::unique_xmalloc_ptr<char> canon_name
23248 = cp_canonicalize_string (name);
71c25dea 23249
596dc4ad
TT
23250 if (canon_name != nullptr)
23251 name = objfile->intern (canon_name.get ());
71c25dea
TT
23252 }
23253
23254 return name;
c906108c
SS
23255}
23256
96553a0c
DE
23257/* Get name of a die, return NULL if not found.
23258 Anonymous namespaces are converted to their magic string. */
9219021c 23259
15d034d0 23260static const char *
e142c38c 23261dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23262{
23263 struct attribute *attr;
5e22e966 23264 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23265
e142c38c 23266 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23267 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23268 if (attr_name == nullptr
96553a0c 23269 && die->tag != DW_TAG_namespace
53832f31
TT
23270 && die->tag != DW_TAG_class_type
23271 && die->tag != DW_TAG_interface_type
23272 && die->tag != DW_TAG_structure_type
23273 && die->tag != DW_TAG_union_type)
71c25dea
TT
23274 return NULL;
23275
23276 switch (die->tag)
23277 {
23278 case DW_TAG_compile_unit:
95554aad 23279 case DW_TAG_partial_unit:
71c25dea
TT
23280 /* Compilation units have a DW_AT_name that is a filename, not
23281 a source language identifier. */
23282 case DW_TAG_enumeration_type:
23283 case DW_TAG_enumerator:
23284 /* These tags always have simple identifiers already; no need
23285 to canonicalize them. */
2c830f54 23286 return attr_name;
907af001 23287
96553a0c 23288 case DW_TAG_namespace:
2c830f54
TT
23289 if (attr_name != nullptr)
23290 return attr_name;
96553a0c
DE
23291 return CP_ANONYMOUS_NAMESPACE_STR;
23292
907af001
UW
23293 case DW_TAG_class_type:
23294 case DW_TAG_interface_type:
23295 case DW_TAG_structure_type:
23296 case DW_TAG_union_type:
23297 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23298 structures or unions. These were of the form "._%d" in GCC 4.1,
23299 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23300 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23301 if (attr_name != nullptr
23302 && (startswith (attr_name, "._")
23303 || startswith (attr_name, "<anonymous")))
907af001 23304 return NULL;
53832f31
TT
23305
23306 /* GCC might emit a nameless typedef that has a linkage name. See
23307 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23308 if (!attr || attr_name == NULL)
53832f31 23309 {
73b9be8b 23310 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23311 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23312 if (attr == NULL || attr_name == NULL)
53832f31
TT
23313 return NULL;
23314
2c830f54 23315 /* Avoid demangling attr_name the second time on a second
df5c6c50 23316 call for the same DIE. */
3b64bf15 23317 if (!attr->canonical_string_p ())
53832f31 23318 {
43816ebc 23319 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23320 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23321 if (demangled == nullptr)
23322 return nullptr;
43816ebc 23323
c6481205 23324 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23325 attr_name = attr->as_string ();
53832f31 23326 }
67430cd0 23327
2c830f54
TT
23328 /* Strip any leading namespaces/classes, keep only the
23329 base name. DW_AT_name for named DIEs does not
23330 contain the prefixes. */
23331 const char *base = strrchr (attr_name, ':');
23332 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23333 return &base[1];
23334 else
2c830f54 23335 return attr_name;
53832f31 23336 }
907af001
UW
23337 break;
23338
71c25dea 23339 default:
907af001
UW
23340 break;
23341 }
23342
3b64bf15 23343 if (!attr->canonical_string_p ())
c6481205
TT
23344 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23345 objfile));
2c830f54 23346 return attr->as_string ();
9219021c
DC
23347}
23348
23349/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23350 is none. *EXT_CU is the CU containing DIE on input, and the CU
23351 containing the return value on output. */
9219021c
DC
23352
23353static struct die_info *
f2f0e013 23354dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23355{
23356 struct attribute *attr;
9219021c 23357
f2f0e013 23358 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23359 if (attr == NULL)
23360 return NULL;
23361
f2f0e013 23362 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23363}
23364
f9aca02d 23365static void
d97bc12b 23366dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23367{
23368 unsigned int i;
23369
d97bc12b 23370 print_spaces (indent, f);
9d8780f0 23371 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23372 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23373 sect_offset_str (die->sect_off));
d97bc12b
DE
23374
23375 if (die->parent != NULL)
23376 {
23377 print_spaces (indent, f);
9d8780f0
SM
23378 fprintf_unfiltered (f, " parent at offset: %s\n",
23379 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23380 }
23381
23382 print_spaces (indent, f);
23383 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23384 dwarf_bool_name (die->child != NULL));
c906108c 23385
d97bc12b
DE
23386 print_spaces (indent, f);
23387 fprintf_unfiltered (f, " attributes:\n");
23388
c906108c
SS
23389 for (i = 0; i < die->num_attrs; ++i)
23390 {
d97bc12b
DE
23391 print_spaces (indent, f);
23392 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23393 dwarf_attr_name (die->attrs[i].name),
23394 dwarf_form_name (die->attrs[i].form));
d97bc12b 23395
c906108c
SS
23396 switch (die->attrs[i].form)
23397 {
c906108c 23398 case DW_FORM_addr:
336d760d 23399 case DW_FORM_addrx:
3019eac3 23400 case DW_FORM_GNU_addr_index:
d97bc12b 23401 fprintf_unfiltered (f, "address: ");
36d378cf 23402 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23403 break;
23404 case DW_FORM_block2:
23405 case DW_FORM_block4:
23406 case DW_FORM_block:
23407 case DW_FORM_block1:
56eb65bd 23408 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23409 pulongest (die->attrs[i].as_block ()->size));
c906108c 23410 break;
2dc7f7b3 23411 case DW_FORM_exprloc:
56eb65bd 23412 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23413 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23414 break;
0224619f
JK
23415 case DW_FORM_data16:
23416 fprintf_unfiltered (f, "constant of 16 bytes");
23417 break;
4568ecf9
DE
23418 case DW_FORM_ref_addr:
23419 fprintf_unfiltered (f, "ref address: ");
529908cb 23420 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23421 break;
36586728
TT
23422 case DW_FORM_GNU_ref_alt:
23423 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23424 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23425 break;
10b3939b
DJ
23426 case DW_FORM_ref1:
23427 case DW_FORM_ref2:
23428 case DW_FORM_ref4:
4568ecf9
DE
23429 case DW_FORM_ref8:
23430 case DW_FORM_ref_udata:
d97bc12b 23431 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23432 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23433 break;
c906108c
SS
23434 case DW_FORM_data1:
23435 case DW_FORM_data2:
23436 case DW_FORM_data4:
ce5d95e1 23437 case DW_FORM_data8:
c906108c 23438 case DW_FORM_udata:
43bbcdc2 23439 fprintf_unfiltered (f, "constant: %s",
529908cb 23440 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23441 break;
2dc7f7b3
TT
23442 case DW_FORM_sec_offset:
23443 fprintf_unfiltered (f, "section offset: %s",
529908cb 23444 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23445 break;
55f1336d 23446 case DW_FORM_ref_sig8:
ac9ec31b 23447 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23448 hex_string (die->attrs[i].as_signature ()));
348e048f 23449 break;
c906108c 23450 case DW_FORM_string:
4bdf3d34 23451 case DW_FORM_strp:
43988095 23452 case DW_FORM_line_strp:
cf532bd1 23453 case DW_FORM_strx:
3019eac3 23454 case DW_FORM_GNU_str_index:
36586728 23455 case DW_FORM_GNU_strp_alt:
8285870a 23456 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23457 die->attrs[i].as_string ()
23458 ? die->attrs[i].as_string () : "",
23459 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23460 break;
23461 case DW_FORM_flag:
c45bc3f8 23462 if (die->attrs[i].as_boolean ())
d97bc12b 23463 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23464 else
d97bc12b 23465 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23466 break;
2dc7f7b3
TT
23467 case DW_FORM_flag_present:
23468 fprintf_unfiltered (f, "flag: TRUE");
23469 break;
a8329558 23470 case DW_FORM_indirect:
0963b4bd
MS
23471 /* The reader will have reduced the indirect form to
23472 the "base form" so this form should not occur. */
5f48f8f3 23473 fprintf_unfiltered (f,
3e43a32a 23474 "unexpected attribute form: DW_FORM_indirect");
a8329558 23475 break;
1bc397c5 23476 case DW_FORM_sdata:
663c44ac
JK
23477 case DW_FORM_implicit_const:
23478 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23479 plongest (die->attrs[i].as_signed ()));
663c44ac 23480 break;
c906108c 23481 default:
d97bc12b 23482 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23483 die->attrs[i].form);
d97bc12b 23484 break;
c906108c 23485 }
d97bc12b 23486 fprintf_unfiltered (f, "\n");
c906108c
SS
23487 }
23488}
23489
f9aca02d 23490static void
d97bc12b 23491dump_die_for_error (struct die_info *die)
c906108c 23492{
d97bc12b
DE
23493 dump_die_shallow (gdb_stderr, 0, die);
23494}
23495
23496static void
23497dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23498{
23499 int indent = level * 4;
23500
23501 gdb_assert (die != NULL);
23502
23503 if (level >= max_level)
23504 return;
23505
23506 dump_die_shallow (f, indent, die);
23507
23508 if (die->child != NULL)
c906108c 23509 {
d97bc12b
DE
23510 print_spaces (indent, f);
23511 fprintf_unfiltered (f, " Children:");
23512 if (level + 1 < max_level)
23513 {
23514 fprintf_unfiltered (f, "\n");
23515 dump_die_1 (f, level + 1, max_level, die->child);
23516 }
23517 else
23518 {
3e43a32a
MS
23519 fprintf_unfiltered (f,
23520 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23521 }
23522 }
23523
23524 if (die->sibling != NULL && level > 0)
23525 {
23526 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23527 }
23528}
23529
d97bc12b
DE
23530/* This is called from the pdie macro in gdbinit.in.
23531 It's not static so gcc will keep a copy callable from gdb. */
23532
23533void
23534dump_die (struct die_info *die, int max_level)
23535{
23536 dump_die_1 (gdb_stdlog, 0, max_level, die);
23537}
23538
f9aca02d 23539static void
51545339 23540store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23541{
51545339 23542 void **slot;
c906108c 23543
9c541725
PA
23544 slot = htab_find_slot_with_hash (cu->die_hash, die,
23545 to_underlying (die->sect_off),
b64f50a1 23546 INSERT);
51545339
DJ
23547
23548 *slot = die;
c906108c
SS
23549}
23550
348e048f
DE
23551/* Follow reference or signature attribute ATTR of SRC_DIE.
23552 On entry *REF_CU is the CU of SRC_DIE.
23553 On exit *REF_CU is the CU of the result. */
23554
23555static struct die_info *
ff39bb5e 23556follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23557 struct dwarf2_cu **ref_cu)
23558{
23559 struct die_info *die;
23560
cd6c91b4 23561 if (attr->form_is_ref ())
348e048f 23562 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23563 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23564 die = follow_die_sig (src_die, attr, ref_cu);
23565 else
23566 {
23567 dump_die_for_error (src_die);
23568 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23569 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23570 }
23571
23572 return die;
03dd20cc
DJ
23573}
23574
5c631832 23575/* Follow reference OFFSET.
673bfd45
DE
23576 On entry *REF_CU is the CU of the source die referencing OFFSET.
23577 On exit *REF_CU is the CU of the result.
23578 Returns NULL if OFFSET is invalid. */
f504f079 23579
f9aca02d 23580static struct die_info *
9c541725 23581follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23582 struct dwarf2_cu **ref_cu)
c906108c 23583{
10b3939b 23584 struct die_info temp_die;
f2f0e013 23585 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23586 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23587
348e048f
DE
23588 gdb_assert (cu->per_cu != NULL);
23589
98bfdba5
PA
23590 target_cu = cu;
23591
17e593e9
SM
23592 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23593 "source CU contains target offset: %d",
23594 sect_offset_str (cu->per_cu->sect_off),
23595 sect_offset_str (sect_off),
23596 cu->header.offset_in_cu_p (sect_off));
23597
3019eac3 23598 if (cu->per_cu->is_debug_types)
348e048f
DE
23599 {
23600 /* .debug_types CUs cannot reference anything outside their CU.
23601 If they need to, they have to reference a signatured type via
55f1336d 23602 DW_FORM_ref_sig8. */
4057dfde 23603 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23604 return NULL;
348e048f 23605 }
36586728 23606 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23607 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23608 {
23609 struct dwarf2_per_cu_data *per_cu;
9a619af0 23610
9c541725 23611 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23612 per_objfile);
03dd20cc 23613
17e593e9
SM
23614 dwarf_read_debug_printf_v ("target CU offset: %s, "
23615 "target CU DIEs loaded: %d",
23616 sect_offset_str (per_cu->sect_off),
23617 per_objfile->get_cu (per_cu) != nullptr);
23618
616c069a
SM
23619 /* If necessary, add it to the queue and load its DIEs.
23620
23621 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23622 it doesn't mean they are currently loaded. Since we require them
23623 to be loaded, we must check for ourselves. */
23624 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23625 || per_objfile->get_cu (per_cu) == nullptr)
4a636814
SM
23626 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23627 false, cu->language);
03dd20cc 23628
976ca316 23629 target_cu = per_objfile->get_cu (per_cu);
616c069a 23630 gdb_assert (target_cu != nullptr);
10b3939b 23631 }
98bfdba5
PA
23632 else if (cu->dies == NULL)
23633 {
23634 /* We're loading full DIEs during partial symbol reading. */
976ca316 23635 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23636 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23637 language_minimal);
98bfdba5 23638 }
c906108c 23639
f2f0e013 23640 *ref_cu = target_cu;
9c541725 23641 temp_die.sect_off = sect_off;
c24bdb02
KS
23642
23643 if (target_cu != cu)
23644 target_cu->ancestor = cu;
23645
9a3c8263 23646 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23647 &temp_die,
23648 to_underlying (sect_off));
5c631832 23649}
10b3939b 23650
5c631832
JK
23651/* Follow reference attribute ATTR of SRC_DIE.
23652 On entry *REF_CU is the CU of SRC_DIE.
23653 On exit *REF_CU is the CU of the result. */
23654
23655static struct die_info *
ff39bb5e 23656follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23657 struct dwarf2_cu **ref_cu)
23658{
0826b30a 23659 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23660 struct dwarf2_cu *cu = *ref_cu;
23661 struct die_info *die;
23662
9c541725 23663 die = follow_die_offset (sect_off,
36586728
TT
23664 (attr->form == DW_FORM_GNU_ref_alt
23665 || cu->per_cu->is_dwz),
23666 ref_cu);
5c631832 23667 if (!die)
9d8780f0
SM
23668 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23669 "at %s [in module %s]"),
23670 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23671 objfile_name (cu->per_objfile->objfile));
348e048f 23672
5c631832
JK
23673 return die;
23674}
23675
d4c9a4f8 23676/* See read.h. */
5c631832
JK
23677
23678struct dwarf2_locexpr_baton
9c541725 23679dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23680 dwarf2_per_cu_data *per_cu,
976ca316 23681 dwarf2_per_objfile *per_objfile,
041d9819
SM
23682 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23683 bool resolve_abstract_p)
5c631832 23684{
5c631832
JK
23685 struct die_info *die;
23686 struct attribute *attr;
23687 struct dwarf2_locexpr_baton retval;
976ca316 23688 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23689
976ca316 23690 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23691 if (cu == nullptr)
976ca316 23692 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23693
23694 if (cu == nullptr)
cc12ce38
DE
23695 {
23696 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23697 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23698 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23699 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23700 }
918dd910 23701
9c541725 23702 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23703 if (!die)
9d8780f0
SM
23704 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23705 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23706
23707 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23708 if (!attr && resolve_abstract_p
976ca316
SM
23709 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23710 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23711 {
041d9819 23712 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23713 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23714 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23715
3360b6e7 23716 for (const auto &cand_off
976ca316 23717 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23718 {
3360b6e7
TV
23719 struct dwarf2_cu *cand_cu = cu;
23720 struct die_info *cand
23721 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23722 if (!cand
23723 || !cand->parent
e4a62c65
TV
23724 || cand->parent->tag != DW_TAG_subprogram)
23725 continue;
23726
23727 CORE_ADDR pc_low, pc_high;
23728 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23729 if (pc_low == ((CORE_ADDR) -1))
23730 continue;
23731 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23732 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23733 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23734 continue;
23735
23736 die = cand;
23737 attr = dwarf2_attr (die, DW_AT_location, cu);
23738 break;
23739 }
23740 }
23741
5c631832
JK
23742 if (!attr)
23743 {
e103e986
JK
23744 /* DWARF: "If there is no such attribute, then there is no effect.".
23745 DATA is ignored if SIZE is 0. */
5c631832 23746
e103e986 23747 retval.data = NULL;
5c631832
JK
23748 retval.size = 0;
23749 }
cd6c91b4 23750 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23751 {
23752 struct dwarf2_loclist_baton loclist_baton;
041d9819 23753 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23754 size_t size;
23755
23756 fill_in_loclist_baton (cu, &loclist_baton, attr);
23757
23758 retval.data = dwarf2_find_location_expression (&loclist_baton,
23759 &size, pc);
23760 retval.size = size;
23761 }
5c631832
JK
23762 else
23763 {
4fc6c0d5 23764 if (!attr->form_is_block ())
9d8780f0 23765 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23766 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23767 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23768
9d2246fc
TT
23769 struct dwarf_block *block = attr->as_block ();
23770 retval.data = block->data;
23771 retval.size = block->size;
5c631832 23772 }
976ca316 23773 retval.per_objfile = per_objfile;
5c631832 23774 retval.per_cu = cu->per_cu;
918dd910 23775
976ca316 23776 per_objfile->age_comp_units ();
918dd910 23777
5c631832 23778 return retval;
348e048f
DE
23779}
23780
d4c9a4f8 23781/* See read.h. */
8b9737bf
TT
23782
23783struct dwarf2_locexpr_baton
23784dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23785 dwarf2_per_cu_data *per_cu,
14095eb3 23786 dwarf2_per_objfile *per_objfile,
041d9819 23787 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23788{
9c541725 23789 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23790
14095eb3 23791 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23792 get_frame_pc);
8b9737bf
TT
23793}
23794
b6807d98
TT
23795/* Write a constant of a given type as target-ordered bytes into
23796 OBSTACK. */
23797
23798static const gdb_byte *
23799write_constant_as_bytes (struct obstack *obstack,
23800 enum bfd_endian byte_order,
23801 struct type *type,
23802 ULONGEST value,
23803 LONGEST *len)
23804{
23805 gdb_byte *result;
23806
23807 *len = TYPE_LENGTH (type);
224c3ddb 23808 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23809 store_unsigned_integer (result, *len, byte_order, value);
23810
23811 return result;
23812}
23813
d4c9a4f8 23814/* See read.h. */
b6807d98
TT
23815
23816const gdb_byte *
9c541725 23817dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23818 dwarf2_per_cu_data *per_cu,
14095eb3 23819 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23820 obstack *obstack,
b6807d98
TT
23821 LONGEST *len)
23822{
b6807d98
TT
23823 struct die_info *die;
23824 struct attribute *attr;
23825 const gdb_byte *result = NULL;
23826 struct type *type;
23827 LONGEST value;
23828 enum bfd_endian byte_order;
14095eb3 23829 struct objfile *objfile = per_objfile->objfile;
b6807d98 23830
7188ed02 23831 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23832 if (cu == nullptr)
23833 cu = load_cu (per_cu, per_objfile, false);
23834
23835 if (cu == nullptr)
cc12ce38
DE
23836 {
23837 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23838 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23839 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23840 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23841 }
b6807d98 23842
9c541725 23843 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23844 if (!die)
9d8780f0
SM
23845 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23846 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23847
23848 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23849 if (attr == NULL)
23850 return NULL;
23851
e3b94546 23852 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23853 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23854
23855 switch (attr->form)
23856 {
23857 case DW_FORM_addr:
336d760d 23858 case DW_FORM_addrx:
b6807d98
TT
23859 case DW_FORM_GNU_addr_index:
23860 {
23861 gdb_byte *tem;
23862
23863 *len = cu->header.addr_size;
224c3ddb 23864 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23865 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23866 result = tem;
23867 }
23868 break;
23869 case DW_FORM_string:
23870 case DW_FORM_strp:
cf532bd1 23871 case DW_FORM_strx:
b6807d98
TT
23872 case DW_FORM_GNU_str_index:
23873 case DW_FORM_GNU_strp_alt:
c6481205 23874 /* The string is already allocated on the objfile obstack, point
b6807d98 23875 directly to it. */
2c830f54
TT
23876 {
23877 const char *attr_name = attr->as_string ();
23878 result = (const gdb_byte *) attr_name;
23879 *len = strlen (attr_name);
23880 }
b6807d98
TT
23881 break;
23882 case DW_FORM_block1:
23883 case DW_FORM_block2:
23884 case DW_FORM_block4:
23885 case DW_FORM_block:
23886 case DW_FORM_exprloc:
0224619f 23887 case DW_FORM_data16:
9d2246fc
TT
23888 {
23889 struct dwarf_block *block = attr->as_block ();
23890 result = block->data;
23891 *len = block->size;
23892 }
b6807d98
TT
23893 break;
23894
23895 /* The DW_AT_const_value attributes are supposed to carry the
23896 symbol's value "represented as it would be on the target
23897 architecture." By the time we get here, it's already been
23898 converted to host endianness, so we just need to sign- or
23899 zero-extend it as appropriate. */
23900 case DW_FORM_data1:
23901 type = die_type (die, cu);
23902 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23903 if (result == NULL)
23904 result = write_constant_as_bytes (obstack, byte_order,
23905 type, value, len);
23906 break;
23907 case DW_FORM_data2:
23908 type = die_type (die, cu);
23909 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23910 if (result == NULL)
23911 result = write_constant_as_bytes (obstack, byte_order,
23912 type, value, len);
23913 break;
23914 case DW_FORM_data4:
23915 type = die_type (die, cu);
23916 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23917 if (result == NULL)
23918 result = write_constant_as_bytes (obstack, byte_order,
23919 type, value, len);
23920 break;
23921 case DW_FORM_data8:
23922 type = die_type (die, cu);
23923 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23924 if (result == NULL)
23925 result = write_constant_as_bytes (obstack, byte_order,
23926 type, value, len);
23927 break;
23928
23929 case DW_FORM_sdata:
663c44ac 23930 case DW_FORM_implicit_const:
b6807d98
TT
23931 type = die_type (die, cu);
23932 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23933 type, attr->as_signed (), len);
b6807d98
TT
23934 break;
23935
23936 case DW_FORM_udata:
23937 type = die_type (die, cu);
23938 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23939 type, attr->as_unsigned (), len);
b6807d98
TT
23940 break;
23941
23942 default:
b98664d3 23943 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23944 dwarf_form_name (attr->form));
23945 break;
23946 }
23947
23948 return result;
23949}
23950
d4c9a4f8 23951/* See read.h. */
7942e96e
AA
23952
23953struct type *
9c541725 23954dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23955 dwarf2_per_cu_data *per_cu,
23956 dwarf2_per_objfile *per_objfile)
7942e96e 23957{
7942e96e
AA
23958 struct die_info *die;
23959
7188ed02 23960 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23961 if (cu == nullptr)
23962 cu = load_cu (per_cu, per_objfile, false);
23963
23964 if (cu == nullptr)
23965 return nullptr;
7942e96e 23966
9c541725 23967 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23968 if (!die)
23969 return NULL;
23970
23971 return die_type (die, cu);
23972}
23973
8cb5117c 23974/* See read.h. */
8a9b8146
TT
23975
23976struct type *
b64f50a1 23977dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23978 dwarf2_per_cu_data *per_cu,
23979 dwarf2_per_objfile *per_objfile)
8a9b8146 23980{
9c541725 23981 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23982 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23983}
23984
ac9ec31b 23985/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23986 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23987 On exit *REF_CU is the CU of the result.
23988 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23989
23990static struct die_info *
ac9ec31b
DE
23991follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23992 struct dwarf2_cu **ref_cu)
348e048f 23993{
348e048f 23994 struct die_info temp_die;
c24bdb02 23995 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23996 struct die_info *die;
976ca316 23997 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23998
348e048f 23999
ac9ec31b
DE
24000 /* While it might be nice to assert sig_type->type == NULL here,
24001 we can get here for DW_AT_imported_declaration where we need
24002 the DIE not the type. */
348e048f 24003
616c069a 24004 /* If necessary, add it to the queue and load its DIEs.
348e048f 24005
616c069a
SM
24006 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24007 it doesn't mean they are currently loaded. Since we require them
24008 to be loaded, we must check for ourselves. */
976ca316 24009 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
616c069a
SM
24010 language_minimal)
24011 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
976ca316 24012 read_signatured_type (sig_type, per_objfile);
348e048f 24013
976ca316 24014 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24015 gdb_assert (sig_cu != NULL);
9c541725
PA
24016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24017 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24018 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24019 to_underlying (temp_die.sect_off));
348e048f
DE
24020 if (die)
24021 {
796a7ff8
DE
24022 /* For .gdb_index version 7 keep track of included TUs.
24023 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24024 if (per_objfile->per_bfd->index_table != NULL
24025 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24026 {
ae640021 24027 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24028 }
24029
348e048f 24030 *ref_cu = sig_cu;
c24bdb02
KS
24031 if (sig_cu != cu)
24032 sig_cu->ancestor = cu;
24033
348e048f
DE
24034 return die;
24035 }
24036
ac9ec31b
DE
24037 return NULL;
24038}
24039
24040/* Follow signatured type referenced by ATTR in SRC_DIE.
24041 On entry *REF_CU is the CU of SRC_DIE.
24042 On exit *REF_CU is the CU of the result.
24043 The result is the DIE of the type.
24044 If the referenced type cannot be found an error is thrown. */
24045
24046static struct die_info *
ff39bb5e 24047follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24048 struct dwarf2_cu **ref_cu)
24049{
630ed6b9 24050 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24051 struct signatured_type *sig_type;
24052 struct die_info *die;
24053
24054 gdb_assert (attr->form == DW_FORM_ref_sig8);
24055
a2ce51a0 24056 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24057 /* sig_type will be NULL if the signatured type is missing from
24058 the debug info. */
24059 if (sig_type == NULL)
24060 {
24061 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24062 " from DIE at %s [in module %s]"),
24063 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24064 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24065 }
24066
24067 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24068 if (die == NULL)
24069 {
24070 dump_die_for_error (src_die);
24071 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24072 " from DIE at %s [in module %s]"),
24073 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24074 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24075 }
24076
24077 return die;
24078}
24079
24080/* Get the type specified by SIGNATURE referenced in DIE/CU,
24081 reading in and processing the type unit if necessary. */
24082
24083static struct type *
24084get_signatured_type (struct die_info *die, ULONGEST signature,
24085 struct dwarf2_cu *cu)
24086{
976ca316 24087 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24088 struct signatured_type *sig_type;
24089 struct dwarf2_cu *type_cu;
24090 struct die_info *type_die;
24091 struct type *type;
24092
a2ce51a0 24093 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24094 /* sig_type will be NULL if the signatured type is missing from
24095 the debug info. */
24096 if (sig_type == NULL)
24097 {
b98664d3 24098 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24099 " from DIE at %s [in module %s]"),
24100 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24101 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24102 return build_error_marker_type (cu, die);
24103 }
24104
24105 /* If we already know the type we're done. */
976ca316 24106 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24107 if (type != nullptr)
24108 return type;
ac9ec31b
DE
24109
24110 type_cu = cu;
24111 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24112 if (type_die != NULL)
24113 {
24114 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24115 is created. This is important, for example, because for c++ classes
24116 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24117 type = read_type_die (type_die, type_cu);
24118 if (type == NULL)
24119 {
b98664d3 24120 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24121 " referenced from DIE at %s [in module %s]"),
24122 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24123 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24124 type = build_error_marker_type (cu, die);
24125 }
24126 }
24127 else
24128 {
b98664d3 24129 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24130 " from DIE at %s [in module %s]"),
24131 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24132 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24133 type = build_error_marker_type (cu, die);
24134 }
e286671b 24135
976ca316 24136 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24137
24138 return type;
24139}
24140
24141/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24142 reading in and processing the type unit if necessary. */
24143
24144static struct type *
ff39bb5e 24145get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24146 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24147{
24148 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24149 if (attr->form_is_ref ())
ac9ec31b
DE
24150 {
24151 struct dwarf2_cu *type_cu = cu;
24152 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24153
24154 return read_type_die (type_die, type_cu);
24155 }
24156 else if (attr->form == DW_FORM_ref_sig8)
24157 {
630ed6b9 24158 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24159 }
24160 else
24161 {
976ca316 24162 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24163
b98664d3 24164 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24165 " at %s [in module %s]"),
24166 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24167 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24168 return build_error_marker_type (cu, die);
24169 }
348e048f
DE
24170}
24171
e5fe5e75 24172/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24173
24174static void
ab432490
SM
24175load_full_type_unit (dwarf2_per_cu_data *per_cu,
24176 dwarf2_per_objfile *per_objfile)
348e048f 24177{
52dc124a 24178 struct signatured_type *sig_type;
348e048f 24179
f4dc4d17 24180 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24181 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24182
6721b2ec
DE
24183 /* We have the per_cu, but we need the signatured_type.
24184 Fortunately this is an easy translation. */
24185 gdb_assert (per_cu->is_debug_types);
24186 sig_type = (struct signatured_type *) per_cu;
348e048f 24187
7188ed02 24188 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24189
ab432490 24190 read_signatured_type (sig_type, per_objfile);
348e048f 24191
7188ed02 24192 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24193}
24194
3019eac3
DE
24195/* Read in a signatured type and build its CU and DIEs.
24196 If the type is a stub for the real type in a DWO file,
24197 read in the real type from the DWO file as well. */
dee91e82
DE
24198
24199static void
ab432490
SM
24200read_signatured_type (signatured_type *sig_type,
24201 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24202{
24203 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24204
3019eac3 24205 gdb_assert (per_cu->is_debug_types);
7188ed02 24206 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24207
2e671100 24208 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24209
24210 if (!reader.dummy_p)
24211 {
24212 struct dwarf2_cu *cu = reader.cu;
24213 const gdb_byte *info_ptr = reader.info_ptr;
24214
24215 gdb_assert (cu->die_hash == NULL);
24216 cu->die_hash =
24217 htab_create_alloc_ex (cu->header.length / 12,
24218 die_hash,
24219 die_eq,
24220 NULL,
24221 &cu->comp_unit_obstack,
24222 hashtab_obstack_allocate,
24223 dummy_obstack_deallocate);
24224
3e225074 24225 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24226 reader.comp_unit_die->child
24227 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24228 reader.comp_unit_die);
24229 cu->dies = reader.comp_unit_die;
24230 /* comp_unit_die is not stored in die_hash, no need. */
24231
24232 /* We try not to read any attributes in this function, because
24233 not all CUs needed for references have been loaded yet, and
24234 symbol table processing isn't initialized. But we have to
24235 set the CU language, or we won't be able to build types
24236 correctly. Similarly, if we do not read the producer, we can
24237 not apply producer-specific interpretation. */
24238 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24239
24240 reader.keep ();
c0ab21c2
TT
24241 }
24242
7ee85ab1 24243 sig_type->per_cu.tu_read = 1;
c906108c
SS
24244}
24245
c906108c
SS
24246/* Decode simple location descriptions.
24247 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24248 the location and return the value. If COMPUTED is non-null, it is
24249 set to true to indicate that decoding was successful, and false
24250 otherwise. If COMPUTED is null, then this function may emit a
24251 complaint. */
c906108c
SS
24252
24253static CORE_ADDR
7d79de9a 24254decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24255{
5e22e966 24256 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24257 size_t i;
24258 size_t size = blk->size;
d521ce57 24259 const gdb_byte *data = blk->data;
21ae7a4d
JK
24260 CORE_ADDR stack[64];
24261 int stacki;
24262 unsigned int bytes_read, unsnd;
24263 gdb_byte op;
c906108c 24264
7d79de9a
TT
24265 if (computed != nullptr)
24266 *computed = false;
24267
21ae7a4d
JK
24268 i = 0;
24269 stacki = 0;
24270 stack[stacki] = 0;
24271 stack[++stacki] = 0;
24272
24273 while (i < size)
24274 {
24275 op = data[i++];
24276 switch (op)
24277 {
24278 case DW_OP_lit0:
24279 case DW_OP_lit1:
24280 case DW_OP_lit2:
24281 case DW_OP_lit3:
24282 case DW_OP_lit4:
24283 case DW_OP_lit5:
24284 case DW_OP_lit6:
24285 case DW_OP_lit7:
24286 case DW_OP_lit8:
24287 case DW_OP_lit9:
24288 case DW_OP_lit10:
24289 case DW_OP_lit11:
24290 case DW_OP_lit12:
24291 case DW_OP_lit13:
24292 case DW_OP_lit14:
24293 case DW_OP_lit15:
24294 case DW_OP_lit16:
24295 case DW_OP_lit17:
24296 case DW_OP_lit18:
24297 case DW_OP_lit19:
24298 case DW_OP_lit20:
24299 case DW_OP_lit21:
24300 case DW_OP_lit22:
24301 case DW_OP_lit23:
24302 case DW_OP_lit24:
24303 case DW_OP_lit25:
24304 case DW_OP_lit26:
24305 case DW_OP_lit27:
24306 case DW_OP_lit28:
24307 case DW_OP_lit29:
24308 case DW_OP_lit30:
24309 case DW_OP_lit31:
24310 stack[++stacki] = op - DW_OP_lit0;
24311 break;
f1bea926 24312
21ae7a4d
JK
24313 case DW_OP_reg0:
24314 case DW_OP_reg1:
24315 case DW_OP_reg2:
24316 case DW_OP_reg3:
24317 case DW_OP_reg4:
24318 case DW_OP_reg5:
24319 case DW_OP_reg6:
24320 case DW_OP_reg7:
24321 case DW_OP_reg8:
24322 case DW_OP_reg9:
24323 case DW_OP_reg10:
24324 case DW_OP_reg11:
24325 case DW_OP_reg12:
24326 case DW_OP_reg13:
24327 case DW_OP_reg14:
24328 case DW_OP_reg15:
24329 case DW_OP_reg16:
24330 case DW_OP_reg17:
24331 case DW_OP_reg18:
24332 case DW_OP_reg19:
24333 case DW_OP_reg20:
24334 case DW_OP_reg21:
24335 case DW_OP_reg22:
24336 case DW_OP_reg23:
24337 case DW_OP_reg24:
24338 case DW_OP_reg25:
24339 case DW_OP_reg26:
24340 case DW_OP_reg27:
24341 case DW_OP_reg28:
24342 case DW_OP_reg29:
24343 case DW_OP_reg30:
24344 case DW_OP_reg31:
24345 stack[++stacki] = op - DW_OP_reg0;
24346 if (i < size)
7d79de9a
TT
24347 {
24348 if (computed == nullptr)
24349 dwarf2_complex_location_expr_complaint ();
24350 else
24351 return 0;
24352 }
21ae7a4d 24353 break;
c906108c 24354
21ae7a4d
JK
24355 case DW_OP_regx:
24356 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24357 i += bytes_read;
24358 stack[++stacki] = unsnd;
24359 if (i < size)
7d79de9a
TT
24360 {
24361 if (computed == nullptr)
24362 dwarf2_complex_location_expr_complaint ();
24363 else
24364 return 0;
24365 }
21ae7a4d 24366 break;
c906108c 24367
21ae7a4d 24368 case DW_OP_addr:
c8a7a66f
TT
24369 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24370 &bytes_read);
21ae7a4d
JK
24371 i += bytes_read;
24372 break;
d53d4ac5 24373
21ae7a4d
JK
24374 case DW_OP_const1u:
24375 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24376 i += 1;
24377 break;
24378
24379 case DW_OP_const1s:
24380 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24381 i += 1;
24382 break;
24383
24384 case DW_OP_const2u:
24385 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24386 i += 2;
24387 break;
24388
24389 case DW_OP_const2s:
24390 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24391 i += 2;
24392 break;
d53d4ac5 24393
21ae7a4d
JK
24394 case DW_OP_const4u:
24395 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24396 i += 4;
24397 break;
24398
24399 case DW_OP_const4s:
24400 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24401 i += 4;
24402 break;
24403
585861ea
JK
24404 case DW_OP_const8u:
24405 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24406 i += 8;
24407 break;
24408
21ae7a4d
JK
24409 case DW_OP_constu:
24410 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24411 &bytes_read);
24412 i += bytes_read;
24413 break;
24414
24415 case DW_OP_consts:
24416 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24417 i += bytes_read;
24418 break;
24419
24420 case DW_OP_dup:
24421 stack[stacki + 1] = stack[stacki];
24422 stacki++;
24423 break;
24424
24425 case DW_OP_plus:
24426 stack[stacki - 1] += stack[stacki];
24427 stacki--;
24428 break;
24429
24430 case DW_OP_plus_uconst:
24431 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24432 &bytes_read);
24433 i += bytes_read;
24434 break;
24435
24436 case DW_OP_minus:
24437 stack[stacki - 1] -= stack[stacki];
24438 stacki--;
24439 break;
24440
24441 case DW_OP_deref:
24442 /* If we're not the last op, then we definitely can't encode
24443 this using GDB's address_class enum. This is valid for partial
24444 global symbols, although the variable's address will be bogus
24445 in the psymtab. */
24446 if (i < size)
7d79de9a
TT
24447 {
24448 if (computed == nullptr)
24449 dwarf2_complex_location_expr_complaint ();
24450 else
24451 return 0;
24452 }
21ae7a4d
JK
24453 break;
24454
dda83cd7 24455 case DW_OP_GNU_push_tls_address:
4aa4e28b 24456 case DW_OP_form_tls_address:
21ae7a4d
JK
24457 /* The top of the stack has the offset from the beginning
24458 of the thread control block at which the variable is located. */
24459 /* Nothing should follow this operator, so the top of stack would
24460 be returned. */
24461 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24462 address will be bogus in the psymtab. Make it always at least
24463 non-zero to not look as a variable garbage collected by linker
24464 which have DW_OP_addr 0. */
21ae7a4d 24465 if (i < size)
7d79de9a
TT
24466 {
24467 if (computed == nullptr)
24468 dwarf2_complex_location_expr_complaint ();
24469 else
24470 return 0;
24471 }
585861ea 24472 stack[stacki]++;
dda83cd7 24473 break;
21ae7a4d
JK
24474
24475 case DW_OP_GNU_uninit:
7d79de9a
TT
24476 if (computed != nullptr)
24477 return 0;
21ae7a4d
JK
24478 break;
24479
336d760d 24480 case DW_OP_addrx:
3019eac3 24481 case DW_OP_GNU_addr_index:
49f6c839 24482 case DW_OP_GNU_const_index:
3019eac3
DE
24483 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24484 &bytes_read);
24485 i += bytes_read;
24486 break;
24487
21ae7a4d 24488 default:
7d79de9a
TT
24489 if (computed == nullptr)
24490 {
24491 const char *name = get_DW_OP_name (op);
21ae7a4d 24492
7d79de9a
TT
24493 if (name)
24494 complaint (_("unsupported stack op: '%s'"),
24495 name);
24496 else
24497 complaint (_("unsupported stack op: '%02x'"),
24498 op);
24499 }
21ae7a4d
JK
24500
24501 return (stack[stacki]);
d53d4ac5 24502 }
3c6e0cb3 24503
21ae7a4d 24504 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24505 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24506 if (stacki >= ARRAY_SIZE (stack) - 1)
24507 {
7d79de9a
TT
24508 if (computed == nullptr)
24509 complaint (_("location description stack overflow"));
21ae7a4d
JK
24510 return 0;
24511 }
24512
24513 if (stacki <= 0)
24514 {
7d79de9a
TT
24515 if (computed == nullptr)
24516 complaint (_("location description stack underflow"));
21ae7a4d
JK
24517 return 0;
24518 }
24519 }
7d79de9a
TT
24520
24521 if (computed != nullptr)
24522 *computed = true;
21ae7a4d 24523 return (stack[stacki]);
c906108c
SS
24524}
24525
24526/* memory allocation interface */
24527
c906108c 24528static struct dwarf_block *
7b5a2f43 24529dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24530{
8d749320 24531 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24532}
24533
c906108c 24534static struct die_info *
b60c80d6 24535dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24536{
24537 struct die_info *die;
b60c80d6
DJ
24538 size_t size = sizeof (struct die_info);
24539
24540 if (num_attrs > 1)
24541 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24542
b60c80d6 24543 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24544 memset (die, 0, sizeof (struct die_info));
24545 return (die);
24546}
2e276125
JB
24547
24548\f
a036ba48 24549
c90ec28a 24550/* Macro support. */
cf2c3c16 24551
9eac9650
TT
24552/* An overload of dwarf_decode_macros that finds the correct section
24553 and ensures it is read in before calling the other overload. */
24554
24555static void
24556dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24557 int section_is_gnu)
24558{
976ca316
SM
24559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24560 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24561 const struct line_header *lh = cu->line_header;
9eac9650
TT
24562 unsigned int offset_size = cu->header.offset_size;
24563 struct dwarf2_section_info *section;
24564 const char *section_name;
24565
24566 if (cu->dwo_unit != nullptr)
24567 {
24568 if (section_is_gnu)
24569 {
24570 section = &cu->dwo_unit->dwo_file->sections.macro;
24571 section_name = ".debug_macro.dwo";
24572 }
24573 else
24574 {
24575 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24576 section_name = ".debug_macinfo.dwo";
24577 }
24578 }
24579 else
24580 {
24581 if (section_is_gnu)
24582 {
976ca316 24583 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24584 section_name = ".debug_macro";
24585 }
24586 else
24587 {
976ca316 24588 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24589 section_name = ".debug_macinfo";
24590 }
24591 }
24592
24593 section->read (objfile);
24594 if (section->buffer == nullptr)
24595 {
24596 complaint (_("missing %s section"), section_name);
24597 return;
24598 }
24599
24600 buildsym_compunit *builder = cu->get_builder ();
24601
048fde1e 24602 struct dwarf2_section_info *str_offsets_section;
24603 struct dwarf2_section_info *str_section;
24604 ULONGEST str_offsets_base;
24605
24606 if (cu->dwo_unit != nullptr)
24607 {
24608 str_offsets_section = &cu->dwo_unit->dwo_file
24609 ->sections.str_offsets;
24610 str_section = &cu->dwo_unit->dwo_file->sections.str;
24611 str_offsets_base = cu->header.addr_size;
24612 }
24613 else
24614 {
24615 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24616 str_section = &per_objfile->per_bfd->str;
24617 str_offsets_base = *cu->str_offsets_base;
24618 }
24619
976ca316 24620 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24621 offset_size, offset, str_section, str_offsets_section,
24622 str_offsets_base, section_is_gnu);
9eac9650
TT
24623}
24624
3019eac3
DE
24625/* Return the .debug_loc section to use for CU.
24626 For DWO files use .debug_loc.dwo. */
24627
24628static struct dwarf2_section_info *
24629cu_debug_loc_section (struct dwarf2_cu *cu)
24630{
976ca316 24631 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24632
3019eac3 24633 if (cu->dwo_unit)
43988095
JK
24634 {
24635 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24636
43988095
JK
24637 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24638 }
976ca316
SM
24639 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24640 : &per_objfile->per_bfd->loc);
3019eac3
DE
24641}
24642
d0ce17d8
CT
24643/* Return the .debug_rnglists section to use for CU. */
24644static struct dwarf2_section_info *
24645cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24646{
24647 if (cu->header.version < 5)
24648 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24649 cu->header.version);
24650 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24651
24652 /* Make sure we read the .debug_rnglists section from the file that
24653 contains the DW_AT_ranges attribute we are reading. Normally that
24654 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24655 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24656 program. */
24657 if (cu->dwo_unit != nullptr
24658 && tag != DW_TAG_compile_unit
24659 && tag != DW_TAG_skeleton_unit)
24660 {
24661 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24662
24663 if (sections->rnglists.size > 0)
24664 return &sections->rnglists;
24665 else
24666 error (_(".debug_rnglists section is missing from .dwo file."));
24667 }
24668 return &dwarf2_per_objfile->per_bfd->rnglists;
24669}
24670
8cf6f0b1
TT
24671/* A helper function that fills in a dwarf2_loclist_baton. */
24672
24673static void
24674fill_in_loclist_baton (struct dwarf2_cu *cu,
24675 struct dwarf2_loclist_baton *baton,
ff39bb5e 24676 const struct attribute *attr)
8cf6f0b1 24677{
976ca316 24678 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24679 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24680
976ca316 24681 section->read (per_objfile->objfile);
8cf6f0b1 24682
976ca316 24683 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24684 baton->per_cu = cu->per_cu;
24685 gdb_assert (baton->per_cu);
24686 /* We don't know how long the location list is, but make sure we
24687 don't run off the edge of the section. */
d4df075e
TT
24688 baton->size = section->size - attr->as_unsigned ();
24689 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24690 if (cu->base_address.has_value ())
24691 baton->base_address = *cu->base_address;
24692 else
24693 baton->base_address = 0;
f664829e 24694 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24695}
24696
4c2df51b 24697static void
ff39bb5e 24698dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24699 struct dwarf2_cu *cu, int is_block)
4c2df51b 24700{
976ca316
SM
24701 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24702 struct objfile *objfile = per_objfile->objfile;
3019eac3 24703 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24704
cd6c91b4 24705 if (attr->form_is_section_offset ()
3019eac3 24706 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24707 the section. If so, fall through to the complaint in the
24708 other branch. */
d4df075e 24709 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24710 {
0d53c4c4 24711 struct dwarf2_loclist_baton *baton;
4c2df51b 24712
8d749320 24713 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24714
8cf6f0b1 24715 fill_in_loclist_baton (cu, baton, attr);
be391dca 24716
2b24b6e4 24717 if (!cu->base_address.has_value ())
b98664d3 24718 complaint (_("Location list used without "
3e43a32a 24719 "specifying the CU base address."));
4c2df51b 24720
f1e6e072
TT
24721 SYMBOL_ACLASS_INDEX (sym) = (is_block
24722 ? dwarf2_loclist_block_index
24723 : dwarf2_loclist_index);
0d53c4c4
DJ
24724 SYMBOL_LOCATION_BATON (sym) = baton;
24725 }
24726 else
24727 {
24728 struct dwarf2_locexpr_baton *baton;
24729
8d749320 24730 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24731 baton->per_objfile = per_objfile;
ae0d2f24
UW
24732 baton->per_cu = cu->per_cu;
24733 gdb_assert (baton->per_cu);
0d53c4c4 24734
4fc6c0d5 24735 if (attr->form_is_block ())
0d53c4c4
DJ
24736 {
24737 /* Note that we're just copying the block's data pointer
24738 here, not the actual data. We're still pointing into the
6502dd73
DJ
24739 info_buffer for SYM's objfile; right now we never release
24740 that buffer, but when we do clean up properly this may
24741 need to change. */
9d2246fc
TT
24742 struct dwarf_block *block = attr->as_block ();
24743 baton->size = block->size;
24744 baton->data = block->data;
0d53c4c4
DJ
24745 }
24746 else
24747 {
24748 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24749 sym->natural_name ());
0d53c4c4 24750 baton->size = 0;
0d53c4c4 24751 }
6e70227d 24752
f1e6e072
TT
24753 SYMBOL_ACLASS_INDEX (sym) = (is_block
24754 ? dwarf2_locexpr_block_index
24755 : dwarf2_locexpr_index);
0d53c4c4
DJ
24756 SYMBOL_LOCATION_BATON (sym) = baton;
24757 }
4c2df51b 24758}
6502dd73 24759
2e6a9f79 24760/* See read.h. */
96408a79 24761
2e6a9f79
SM
24762const comp_unit_head *
24763dwarf2_per_cu_data::get_header () const
96408a79 24764{
2e6a9f79
SM
24765 if (!m_header_read_in)
24766 {
24767 const gdb_byte *info_ptr
24768 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24769
2e6a9f79 24770 memset (&m_header, 0, sizeof (m_header));
96408a79 24771
2e6a9f79
SM
24772 read_comp_unit_head (&m_header, info_ptr, this->section,
24773 rcuh_kind::COMPILE);
a9f172c6
TT
24774
24775 m_header_read_in = true;
2e6a9f79 24776 }
96408a79 24777
2e6a9f79 24778 return &m_header;
96408a79
SA
24779}
24780
09ba997f 24781/* See read.h. */
ae0d2f24 24782
98714339 24783int
09ba997f 24784dwarf2_per_cu_data::addr_size () const
ae0d2f24 24785{
2e6a9f79 24786 return this->get_header ()->addr_size;
ae0d2f24
UW
24787}
24788
09ba997f 24789/* See read.h. */
9eae7c52
TT
24790
24791int
09ba997f 24792dwarf2_per_cu_data::offset_size () const
9eae7c52 24793{
2e6a9f79 24794 return this->get_header ()->offset_size;
96408a79
SA
24795}
24796
09ba997f 24797/* See read.h. */
96408a79
SA
24798
24799int
09ba997f 24800dwarf2_per_cu_data::ref_addr_size () const
96408a79 24801{
2e6a9f79 24802 const comp_unit_head *header = this->get_header ();
96408a79 24803
2e6a9f79
SM
24804 if (header->version == 2)
24805 return header->addr_size;
96408a79 24806 else
2e6a9f79 24807 return header->offset_size;
181cebd4
JK
24808}
24809
09ba997f 24810/* See read.h. */
9aa1f1e3 24811
09ba997f 24812struct type *
293e7e51 24813dwarf2_cu::addr_type () const
9a49df9d 24814{
293e7e51 24815 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24816 struct type *void_type = objfile_type (objfile)->builtin_void;
24817 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24818 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24819
24820 if (TYPE_LENGTH (addr_type) == addr_size)
24821 return addr_type;
24822
c6d940a9 24823 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24824 return addr_type;
24825}
24826
22b6cd70
TT
24827/* A helper function for dwarf2_find_containing_comp_unit that returns
24828 the index of the result, and that searches a vector. It will
24829 return a result even if the offset in question does not actually
24830 occur in any CU. This is separate so that it can be unit
24831 tested. */
ae038cb0 24832
22b6cd70
TT
24833static int
24834dwarf2_find_containing_comp_unit
24835 (sect_offset sect_off,
24836 unsigned int offset_in_dwz,
24837 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24838{
ae038cb0
DJ
24839 int low, high;
24840
ae038cb0 24841 low = 0;
22b6cd70 24842 high = all_comp_units.size () - 1;
ae038cb0
DJ
24843 while (high > low)
24844 {
36586728 24845 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24846 int mid = low + (high - low) / 2;
9a619af0 24847
22b6cd70 24848 mid_cu = all_comp_units[mid];
36586728 24849 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24850 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24851 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24852 high = mid;
24853 else
24854 low = mid + 1;
24855 }
24856 gdb_assert (low == high);
22b6cd70
TT
24857 return low;
24858}
24859
24860/* Locate the .debug_info compilation unit from CU's objfile which contains
24861 the DIE at OFFSET. Raises an error on failure. */
24862
24863static struct dwarf2_per_cu_data *
24864dwarf2_find_containing_comp_unit (sect_offset sect_off,
24865 unsigned int offset_in_dwz,
976ca316 24866 dwarf2_per_objfile *per_objfile)
22b6cd70 24867{
976ca316
SM
24868 int low = dwarf2_find_containing_comp_unit
24869 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24870 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24871
45b8ae0c 24872 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24873 {
36586728 24874 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24875 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24876 "offset %s [in module %s]"),
24877 sect_offset_str (sect_off),
976ca316 24878 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24879
976ca316 24880 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24881 <= sect_off);
976ca316 24882 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24883 }
24884 else
24885 {
976ca316 24886 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24887 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24888 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24889 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24890 return this_cu;
24891 }
24892}
24893
22b6cd70
TT
24894#if GDB_SELF_TEST
24895
24896namespace selftests {
24897namespace find_containing_comp_unit {
24898
24899static void
24900run_test ()
24901{
24902 struct dwarf2_per_cu_data one {};
24903 struct dwarf2_per_cu_data two {};
24904 struct dwarf2_per_cu_data three {};
24905 struct dwarf2_per_cu_data four {};
24906
24907 one.length = 5;
24908 two.sect_off = sect_offset (one.length);
24909 two.length = 7;
24910
24911 three.length = 5;
24912 three.is_dwz = 1;
24913 four.sect_off = sect_offset (three.length);
24914 four.length = 7;
24915 four.is_dwz = 1;
24916
24917 std::vector<dwarf2_per_cu_data *> units;
24918 units.push_back (&one);
24919 units.push_back (&two);
24920 units.push_back (&three);
24921 units.push_back (&four);
24922
24923 int result;
24924
24925 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24926 SELF_CHECK (units[result] == &one);
24927 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24928 SELF_CHECK (units[result] == &one);
24929 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24930 SELF_CHECK (units[result] == &two);
24931
24932 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24933 SELF_CHECK (units[result] == &three);
24934 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24935 SELF_CHECK (units[result] == &three);
24936 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24937 SELF_CHECK (units[result] == &four);
24938}
24939
24940}
24941}
24942
24943#endif /* GDB_SELF_TEST */
24944
9e021579 24945/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24946
9e021579
SM
24947dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24948 dwarf2_per_objfile *per_objfile)
24949 : per_cu (per_cu),
24950 per_objfile (per_objfile),
9068261f
AB
24951 mark (false),
24952 has_loclist (false),
24953 checked_producer (false),
24954 producer_is_gxx_lt_4_6 (false),
24955 producer_is_gcc_lt_4_3 (false),
eb77c9df 24956 producer_is_icc (false),
9068261f 24957 producer_is_icc_lt_14 (false),
c258c396 24958 producer_is_codewarrior (false),
9068261f 24959 processing_has_namespace_info (false)
93311388 24960{
9816fde3
JK
24961}
24962
24963/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24964
24965static void
95554aad
TT
24966prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24967 enum language pretend_language)
9816fde3
JK
24968{
24969 struct attribute *attr;
24970
24971 /* Set the language we're debugging. */
24972 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24973 if (attr != nullptr)
529908cb 24974 set_cu_language (attr->constant_value (0), cu);
9816fde3 24975 else
9cded63f 24976 {
95554aad 24977 cu->language = pretend_language;
9cded63f
TT
24978 cu->language_defn = language_def (cu->language);
24979 }
dee91e82 24980
7d45c7c3 24981 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24982}
24983
7188ed02 24984/* See read.h. */
ae038cb0 24985
7188ed02
SM
24986dwarf2_cu *
24987dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24988{
7188ed02
SM
24989 auto it = m_dwarf2_cus.find (per_cu);
24990 if (it == m_dwarf2_cus.end ())
24991 return nullptr;
ae038cb0 24992
7188ed02
SM
24993 return it->second;
24994}
24995
24996/* See read.h. */
24997
24998void
24999dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25000{
25001 gdb_assert (this->get_cu (per_cu) == nullptr);
25002
25003 m_dwarf2_cus[per_cu] = cu;
25004}
25005
25006/* See read.h. */
25007
25008void
25009dwarf2_per_objfile::age_comp_units ()
25010{
17e593e9
SM
25011 dwarf_read_debug_printf_v ("running");
25012
08ac5771
SM
25013 /* This is not expected to be called in the middle of CU expansion. There is
25014 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
25015 loaded in memory. Calling age_comp_units while the queue is in use could
25016 make us free the DIEs for a CU that is in the queue and therefore break
25017 that invariant. */
25018 gdb_assert (!this->per_bfd->queue.has_value ());
25019
7188ed02
SM
25020 /* Start by clearing all marks. */
25021 for (auto pair : m_dwarf2_cus)
25022 pair.second->mark = false;
25023
25024 /* Traverse all CUs, mark them and their dependencies if used recently
25025 enough. */
25026 for (auto pair : m_dwarf2_cus)
ae038cb0 25027 {
7188ed02
SM
25028 dwarf2_cu *cu = pair.second;
25029
25030 cu->last_used++;
25031 if (cu->last_used <= dwarf_max_cache_age)
25032 dwarf2_mark (cu);
ae038cb0
DJ
25033 }
25034
7188ed02
SM
25035 /* Delete all CUs still not marked. */
25036 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25037 {
7188ed02 25038 dwarf2_cu *cu = it->second;
ae038cb0 25039
7188ed02 25040 if (!cu->mark)
ae038cb0 25041 {
17e593e9
SM
25042 dwarf_read_debug_printf_v ("deleting old CU %s",
25043 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25044 delete cu;
25045 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25046 }
25047 else
7188ed02 25048 it++;
ae038cb0
DJ
25049 }
25050}
25051
7188ed02 25052/* See read.h. */
ae038cb0 25053
7188ed02
SM
25054void
25055dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25056{
7188ed02
SM
25057 auto it = m_dwarf2_cus.find (per_cu);
25058 if (it == m_dwarf2_cus.end ())
25059 return;
ae038cb0 25060
7188ed02 25061 delete it->second;
ae038cb0 25062
7188ed02
SM
25063 m_dwarf2_cus.erase (it);
25064}
ae038cb0 25065
7188ed02
SM
25066dwarf2_per_objfile::~dwarf2_per_objfile ()
25067{
25068 remove_all_cus ();
ae038cb0
DJ
25069}
25070
dee91e82
DE
25071/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25072 We store these in a hash table separate from the DIEs, and preserve them
25073 when the DIEs are flushed out of cache.
25074
25075 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25076 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25077 or the type may come from a DWO file. Furthermore, while it's more logical
25078 to use per_cu->section+offset, with Fission the section with the data is in
25079 the DWO file but we don't know that section at the point we need it.
25080 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25081 because we can enter the lookup routine, get_die_type_at_offset, from
25082 outside this file, and thus won't necessarily have PER_CU->cu.
25083 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25084
dee91e82 25085struct dwarf2_per_cu_offset_and_type
1c379e20 25086{
dee91e82 25087 const struct dwarf2_per_cu_data *per_cu;
9c541725 25088 sect_offset sect_off;
1c379e20
DJ
25089 struct type *type;
25090};
25091
dee91e82 25092/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25093
25094static hashval_t
dee91e82 25095per_cu_offset_and_type_hash (const void *item)
1c379e20 25096{
9a3c8263
SM
25097 const struct dwarf2_per_cu_offset_and_type *ofs
25098 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25099
9c541725 25100 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25101}
25102
dee91e82 25103/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25104
25105static int
dee91e82 25106per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25107{
9a3c8263
SM
25108 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25109 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25110 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25111 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25112
dee91e82 25113 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25114 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25115}
25116
25117/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25118 table if necessary. For convenience, return TYPE.
25119
25120 The DIEs reading must have careful ordering to:
85102364 25121 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25122 reading current DIE.
25123 * Not trying to dereference contents of still incompletely read in types
25124 while reading in other DIEs.
25125 * Enable referencing still incompletely read in types just by a pointer to
25126 the type without accessing its fields.
25127
25128 Therefore caller should follow these rules:
25129 * Try to fetch any prerequisite types we may need to build this DIE type
25130 before building the type and calling set_die_type.
e71ec853 25131 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25132 possible before fetching more types to complete the current type.
25133 * Make the type as complete as possible before fetching more types. */
1c379e20 25134
f792889a 25135static struct type *
57567375
TT
25136set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25137 bool skip_data_location)
1c379e20 25138{
976ca316 25139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25140 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25141 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25142 struct attribute *attr;
25143 struct dynamic_prop prop;
1c379e20 25144
b4ba55a1
JB
25145 /* For Ada types, make sure that the gnat-specific data is always
25146 initialized (if not already set). There are a few types where
25147 we should not be doing so, because the type-specific area is
25148 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25149 where the type-specific area is used to store the floatformat).
25150 But this is not a problem, because the gnat-specific information
25151 is actually not needed for these types. */
25152 if (need_gnat_info (cu)
78134374
SM
25153 && type->code () != TYPE_CODE_FUNC
25154 && type->code () != TYPE_CODE_FLT
25155 && type->code () != TYPE_CODE_METHODPTR
25156 && type->code () != TYPE_CODE_MEMBERPTR
25157 && type->code () != TYPE_CODE_METHOD
09584414 25158 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25159 && !HAVE_GNAT_AUX_INFO (type))
25160 INIT_GNAT_SPECIFIC (type);
25161
3f2f83dd
KB
25162 /* Read DW_AT_allocated and set in type. */
25163 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25164 if (attr != NULL)
3f2f83dd 25165 {
293e7e51 25166 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25167 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25168 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25169 }
3f2f83dd
KB
25170
25171 /* Read DW_AT_associated and set in type. */
25172 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25173 if (attr != NULL)
3f2f83dd 25174 {
293e7e51 25175 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25176 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25177 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25178 }
3f2f83dd 25179
3cdcd0ce 25180 /* Read DW_AT_data_location and set in type. */
57567375
TT
25181 if (!skip_data_location)
25182 {
25183 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25184 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25185 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25186 }
3cdcd0ce 25187
976ca316
SM
25188 if (per_objfile->die_type_hash == NULL)
25189 per_objfile->die_type_hash
0335378b
TT
25190 = htab_up (htab_create_alloc (127,
25191 per_cu_offset_and_type_hash,
25192 per_cu_offset_and_type_eq,
25193 NULL, xcalloc, xfree));
1c379e20 25194
dee91e82 25195 ofs.per_cu = cu->per_cu;
9c541725 25196 ofs.sect_off = die->sect_off;
1c379e20 25197 ofs.type = type;
dee91e82 25198 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25199 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25200 if (*slot)
b98664d3 25201 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25202 sect_offset_str (die->sect_off));
8d749320
SM
25203 *slot = XOBNEW (&objfile->objfile_obstack,
25204 struct dwarf2_per_cu_offset_and_type);
1c379e20 25205 **slot = ofs;
f792889a 25206 return type;
1c379e20
DJ
25207}
25208
9c541725 25209/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25210 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25211
25212static struct type *
9c541725 25213get_die_type_at_offset (sect_offset sect_off,
aa66c379 25214 dwarf2_per_cu_data *per_cu,
976ca316 25215 dwarf2_per_objfile *per_objfile)
1c379e20 25216{
dee91e82 25217 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25218
976ca316 25219 if (per_objfile->die_type_hash == NULL)
f792889a 25220 return NULL;
1c379e20 25221
dee91e82 25222 ofs.per_cu = per_cu;
9c541725 25223 ofs.sect_off = sect_off;
9a3c8263 25224 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25225 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25226 if (slot)
25227 return slot->type;
25228 else
25229 return NULL;
25230}
25231
02142a6c 25232/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25233 or return NULL if DIE does not have a saved type. */
25234
25235static struct type *
25236get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25237{
aa66c379 25238 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25239}
25240
10b3939b
DJ
25241/* Add a dependence relationship from CU to REF_PER_CU. */
25242
25243static void
25244dwarf2_add_dependence (struct dwarf2_cu *cu,
25245 struct dwarf2_per_cu_data *ref_per_cu)
25246{
25247 void **slot;
25248
25249 if (cu->dependencies == NULL)
25250 cu->dependencies
25251 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25252 NULL, &cu->comp_unit_obstack,
25253 hashtab_obstack_allocate,
25254 dummy_obstack_deallocate);
25255
25256 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25257 if (*slot == NULL)
25258 *slot = ref_per_cu;
25259}
1c379e20 25260
f504f079
DE
25261/* Subroutine of dwarf2_mark to pass to htab_traverse.
25262 Set the mark field in every compilation unit in the
7188ed02
SM
25263 cache that we must keep because we are keeping CU.
25264
25265 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25266
10b3939b
DJ
25267static int
25268dwarf2_mark_helper (void **slot, void *data)
25269{
7188ed02
SM
25270 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25271 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25272 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25273
25274 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25275 reading of the chain. As such dependencies remain valid it is not much
25276 useful to track and undo them during QUIT cleanups. */
7188ed02 25277 if (cu == nullptr)
d07ed419
JK
25278 return 1;
25279
7188ed02 25280 if (cu->mark)
10b3939b 25281 return 1;
10b3939b 25282
7188ed02
SM
25283 cu->mark = true;
25284
25285 if (cu->dependencies != nullptr)
25286 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25287
25288 return 1;
25289}
25290
f504f079
DE
25291/* Set the mark field in CU and in every other compilation unit in the
25292 cache that we must keep because we are keeping CU. */
25293
ae038cb0
DJ
25294static void
25295dwarf2_mark (struct dwarf2_cu *cu)
25296{
25297 if (cu->mark)
25298 return;
7188ed02 25299
9068261f 25300 cu->mark = true;
ae038cb0 25301
7188ed02
SM
25302 if (cu->dependencies != nullptr)
25303 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25304}
25305
72bf9492
DJ
25306/* Trivial hash function for partial_die_info: the hash value of a DIE
25307 is its offset in .debug_info for this objfile. */
25308
25309static hashval_t
25310partial_die_hash (const void *item)
25311{
9a3c8263
SM
25312 const struct partial_die_info *part_die
25313 = (const struct partial_die_info *) item;
9a619af0 25314
9c541725 25315 return to_underlying (part_die->sect_off);
72bf9492
DJ
25316}
25317
25318/* Trivial comparison function for partial_die_info structures: two DIEs
25319 are equal if they have the same offset. */
25320
25321static int
25322partial_die_eq (const void *item_lhs, const void *item_rhs)
25323{
9a3c8263
SM
25324 const struct partial_die_info *part_die_lhs
25325 = (const struct partial_die_info *) item_lhs;
25326 const struct partial_die_info *part_die_rhs
25327 = (const struct partial_die_info *) item_rhs;
9a619af0 25328
9c541725 25329 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25330}
25331
3c3bb058
AB
25332struct cmd_list_element *set_dwarf_cmdlist;
25333struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25334
9291a0cd 25335static void
cd4fb1b2
SM
25336show_check_physname (struct ui_file *file, int from_tty,
25337 struct cmd_list_element *c, const char *value)
9291a0cd 25338{
cd4fb1b2
SM
25339 fprintf_filtered (file,
25340 _("Whether to check \"physname\" is %s.\n"),
25341 value);
9291a0cd
TT
25342}
25343
6c265988 25344void _initialize_dwarf2_read ();
cd4fb1b2 25345void
6c265988 25346_initialize_dwarf2_read ()
9291a0cd 25347{
0743fc83 25348 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25349Set DWARF specific variables.\n\
590042fc 25350Configure DWARF variables such as the cache size."),
0743fc83
TT
25351 &set_dwarf_cmdlist, "maintenance set dwarf ",
25352 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25353
0743fc83 25354 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25355Show DWARF specific variables.\n\
25356Show DWARF variables such as the cache size."),
0743fc83
TT
25357 &show_dwarf_cmdlist, "maintenance show dwarf ",
25358 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25359
cd4fb1b2
SM
25360 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25361 &dwarf_max_cache_age, _("\
25362Set the upper bound on the age of cached DWARF compilation units."), _("\
25363Show the upper bound on the age of cached DWARF compilation units."), _("\
25364A higher limit means that cached compilation units will be stored\n\
25365in memory longer, and more total memory will be used. Zero disables\n\
25366caching, which can slow down startup."),
25367 NULL,
25368 show_dwarf_max_cache_age,
25369 &set_dwarf_cmdlist,
25370 &show_dwarf_cmdlist);
156942c7 25371
cd4fb1b2
SM
25372 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25373Set debugging of the DWARF reader."), _("\
25374Show debugging of the DWARF reader."), _("\
25375When enabled (non-zero), debugging messages are printed during DWARF\n\
25376reading and symtab expansion. A value of 1 (one) provides basic\n\
25377information. A value greater than 1 provides more verbose information."),
25378 NULL,
25379 NULL,
25380 &setdebuglist, &showdebuglist);
9291a0cd 25381
cd4fb1b2
SM
25382 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25383Set debugging of the DWARF DIE reader."), _("\
25384Show debugging of the DWARF DIE reader."), _("\
25385When enabled (non-zero), DIEs are dumped after they are read in.\n\
25386The value is the maximum depth to print."),
25387 NULL,
25388 NULL,
25389 &setdebuglist, &showdebuglist);
9291a0cd 25390
cd4fb1b2
SM
25391 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25392Set debugging of the dwarf line reader."), _("\
25393Show debugging of the dwarf line reader."), _("\
25394When enabled (non-zero), line number entries are dumped as they are read in.\n\
25395A value of 1 (one) provides basic information.\n\
25396A value greater than 1 provides more verbose information."),
25397 NULL,
25398 NULL,
25399 &setdebuglist, &showdebuglist);
437afbb8 25400
cd4fb1b2
SM
25401 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25402Set cross-checking of \"physname\" code against demangler."), _("\
25403Show cross-checking of \"physname\" code against demangler."), _("\
25404When enabled, GDB's internal \"physname\" code is checked against\n\
25405the demangler."),
25406 NULL, show_check_physname,
25407 &setdebuglist, &showdebuglist);
900e11f9 25408
e615022a
DE
25409 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25410 no_class, &use_deprecated_index_sections, _("\
25411Set whether to use deprecated gdb_index sections."), _("\
25412Show whether to use deprecated gdb_index sections."), _("\
25413When enabled, deprecated .gdb_index sections are used anyway.\n\
25414Normally they are ignored either because of a missing feature or\n\
25415performance issue.\n\
25416Warning: This option must be enabled before gdb reads the file."),
25417 NULL,
25418 NULL,
25419 &setlist, &showlist);
25420
f1e6e072
TT
25421 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25422 &dwarf2_locexpr_funcs);
25423 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25424 &dwarf2_loclist_funcs);
25425
25426 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25427 &dwarf2_block_frame_base_locexpr_funcs);
25428 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25429 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25430
25431#if GDB_SELF_TEST
25432 selftests::register_test ("dw2_expand_symtabs_matching",
25433 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25434 selftests::register_test ("dwarf2_find_containing_comp_unit",
25435 selftests::find_containing_comp_unit::run_test);
c62446b1 25436#endif
6502dd73 25437}
This page took 6.458921 seconds and 4 git commands to generate.