40aeb298c14d70279413c90d33539ab7dac45310
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 Free Software Foundation, Inc.
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
10 support.
11
12 This file is part of GDB.
13
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
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
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.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
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
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "dwarf2/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static 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. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
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
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
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
167 struct 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
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
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. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
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. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
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>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* This is a view into the index that converts from bytes to an
227 offset_type, and allows indexing. Unaligned bytes are specifically
228 allowed here, and handled via unpacking. */
229
230 class offset_view
231 {
232 public:
233 offset_view () = default;
234
235 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
236 : m_bytes (bytes)
237 {
238 }
239
240 /* Extract the INDEXth offset_type from the array. */
241 offset_type operator[] (size_t index) const
242 {
243 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
244 return (offset_type) extract_unsigned_integer (bytes,
245 sizeof (offset_type),
246 BFD_ENDIAN_LITTLE);
247 }
248
249 /* Return the number of offset_types in this array. */
250 size_t size () const
251 {
252 return m_bytes.size () / sizeof (offset_type);
253 }
254
255 /* Return true if this view is empty. */
256 bool empty () const
257 {
258 return m_bytes.empty ();
259 }
260
261 private:
262 /* The underlying bytes. */
263 gdb::array_view<const gdb_byte> m_bytes;
264 };
265
266 /* A description of the mapped index. The file format is described in
267 a comment by the code that writes the index. */
268 struct mapped_index final : public mapped_index_base
269 {
270 /* Index data format version. */
271 int version = 0;
272
273 /* The address table data. */
274 gdb::array_view<const gdb_byte> address_table;
275
276 /* The symbol table, implemented as a hash table. */
277 offset_view symbol_table;
278
279 /* A pointer to the constant pool. */
280 gdb::array_view<const gdb_byte> constant_pool;
281
282 /* Return the index into the constant pool of the name of the IDXth
283 symbol in the symbol table. */
284 offset_type symbol_name_index (offset_type idx) const
285 {
286 return symbol_table[2 * idx];
287 }
288
289 /* Return the index into the constant pool of the CU vector of the
290 IDXth symbol in the symbol table. */
291 offset_type symbol_vec_index (offset_type idx) const
292 {
293 return symbol_table[2 * idx + 1];
294 }
295
296 bool symbol_name_slot_invalid (offset_type idx) const override
297 {
298 return (symbol_name_index (idx) == 0
299 && symbol_vec_index (idx) == 0);
300 }
301
302 /* Convenience method to get at the name of the symbol at IDX in the
303 symbol table. */
304 const char *symbol_name_at
305 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
306 {
307 return (const char *) (this->constant_pool.data ()
308 + symbol_name_index (idx));
309 }
310
311 size_t symbol_name_count () const override
312 { return this->symbol_table.size () / 2; }
313 };
314
315 /* A description of the mapped .debug_names.
316 Uninitialized map has CU_COUNT 0. */
317 struct mapped_debug_names final : public mapped_index_base
318 {
319 bfd_endian dwarf5_byte_order;
320 bool dwarf5_is_dwarf64;
321 bool augmentation_is_gdb;
322 uint8_t offset_size;
323 uint32_t cu_count = 0;
324 uint32_t tu_count, bucket_count, name_count;
325 const gdb_byte *cu_table_reordered, *tu_table_reordered;
326 const uint32_t *bucket_table_reordered, *hash_table_reordered;
327 const gdb_byte *name_table_string_offs_reordered;
328 const gdb_byte *name_table_entry_offs_reordered;
329 const gdb_byte *entry_pool;
330
331 struct index_val
332 {
333 ULONGEST dwarf_tag;
334 struct attr
335 {
336 /* Attribute name DW_IDX_*. */
337 ULONGEST dw_idx;
338
339 /* Attribute form DW_FORM_*. */
340 ULONGEST form;
341
342 /* Value if FORM is DW_FORM_implicit_const. */
343 LONGEST implicit_const;
344 };
345 std::vector<attr> attr_vec;
346 };
347
348 std::unordered_map<ULONGEST, index_val> abbrev_map;
349
350 const char *namei_to_name
351 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
352
353 /* Implementation of the mapped_index_base virtual interface, for
354 the name_components cache. */
355
356 const char *symbol_name_at
357 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
358 { return namei_to_name (idx, per_objfile); }
359
360 size_t symbol_name_count () const override
361 { return this->name_count; }
362 };
363
364 /* See dwarf2read.h. */
365
366 dwarf2_per_objfile *
367 get_dwarf2_per_objfile (struct objfile *objfile)
368 {
369 return dwarf2_objfile_data_key.get (objfile);
370 }
371
372 /* Default names of the debugging sections. */
373
374 /* Note that if the debugging section has been compressed, it might
375 have a name like .zdebug_info. */
376
377 const struct dwarf2_debug_sections dwarf2_elf_names =
378 {
379 { ".debug_info", ".zdebug_info" },
380 { ".debug_abbrev", ".zdebug_abbrev" },
381 { ".debug_line", ".zdebug_line" },
382 { ".debug_loc", ".zdebug_loc" },
383 { ".debug_loclists", ".zdebug_loclists" },
384 { ".debug_macinfo", ".zdebug_macinfo" },
385 { ".debug_macro", ".zdebug_macro" },
386 { ".debug_str", ".zdebug_str" },
387 { ".debug_str_offsets", ".zdebug_str_offsets" },
388 { ".debug_line_str", ".zdebug_line_str" },
389 { ".debug_ranges", ".zdebug_ranges" },
390 { ".debug_rnglists", ".zdebug_rnglists" },
391 { ".debug_types", ".zdebug_types" },
392 { ".debug_addr", ".zdebug_addr" },
393 { ".debug_frame", ".zdebug_frame" },
394 { ".eh_frame", NULL },
395 { ".gdb_index", ".zgdb_index" },
396 { ".debug_names", ".zdebug_names" },
397 { ".debug_aranges", ".zdebug_aranges" },
398 23
399 };
400
401 /* List of DWO/DWP sections. */
402
403 static const struct dwop_section_names
404 {
405 struct dwarf2_section_names abbrev_dwo;
406 struct dwarf2_section_names info_dwo;
407 struct dwarf2_section_names line_dwo;
408 struct dwarf2_section_names loc_dwo;
409 struct dwarf2_section_names loclists_dwo;
410 struct dwarf2_section_names macinfo_dwo;
411 struct dwarf2_section_names macro_dwo;
412 struct dwarf2_section_names rnglists_dwo;
413 struct dwarf2_section_names str_dwo;
414 struct dwarf2_section_names str_offsets_dwo;
415 struct dwarf2_section_names types_dwo;
416 struct dwarf2_section_names cu_index;
417 struct dwarf2_section_names tu_index;
418 }
419 dwop_section_names =
420 {
421 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
422 { ".debug_info.dwo", ".zdebug_info.dwo" },
423 { ".debug_line.dwo", ".zdebug_line.dwo" },
424 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
425 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
426 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
427 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
428 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
429 { ".debug_str.dwo", ".zdebug_str.dwo" },
430 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
431 { ".debug_types.dwo", ".zdebug_types.dwo" },
432 { ".debug_cu_index", ".zdebug_cu_index" },
433 { ".debug_tu_index", ".zdebug_tu_index" },
434 };
435
436 /* local data types */
437
438 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
439 begin with a header, which contains the following information. */
440 struct loclists_rnglists_header
441 {
442 /* A 4-byte or 12-byte length containing the length of the
443 set of entries for this compilation unit, not including the
444 length field itself. */
445 unsigned int length;
446
447 /* A 2-byte version identifier. */
448 short version;
449
450 /* A 1-byte unsigned integer containing the size in bytes of an address on
451 the target system. */
452 unsigned char addr_size;
453
454 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
455 on the target system. */
456 unsigned char segment_collector_size;
457
458 /* A 4-byte count of the number of offsets that follow the header. */
459 unsigned int offset_entry_count;
460 };
461
462 /* Type used for delaying computation of method physnames.
463 See comments for compute_delayed_physnames. */
464 struct delayed_method_info
465 {
466 /* The type to which the method is attached, i.e., its parent class. */
467 struct type *type;
468
469 /* The index of the method in the type's function fieldlists. */
470 int fnfield_index;
471
472 /* The index of the method in the fieldlist. */
473 int index;
474
475 /* The name of the DIE. */
476 const char *name;
477
478 /* The DIE associated with this method. */
479 struct die_info *die;
480 };
481
482 /* Internal state when decoding a particular compilation unit. */
483 struct dwarf2_cu
484 {
485 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
486 dwarf2_per_objfile *per_objfile);
487
488 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
489
490 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
491 Create the set of symtabs used by this TU, or if this TU is sharing
492 symtabs with another TU and the symtabs have already been created
493 then restore those symtabs in the line header.
494 We don't need the pc/line-number mapping for type units. */
495 void setup_type_unit_groups (struct die_info *die);
496
497 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
498 buildsym_compunit constructor. */
499 struct compunit_symtab *start_symtab (const char *name,
500 const char *comp_dir,
501 CORE_ADDR low_pc);
502
503 /* Reset the builder. */
504 void reset_builder () { m_builder.reset (); }
505
506 /* Return a type that is a generic pointer type, the size of which
507 matches the address size given in the compilation unit header for
508 this CU. */
509 struct type *addr_type () const;
510
511 /* Find an integer type the same size as the address size given in
512 the compilation unit header for this CU. UNSIGNED_P controls if
513 the integer is unsigned or not. */
514 struct type *addr_sized_int_type (bool unsigned_p) const;
515
516 /* The header of the compilation unit. */
517 struct comp_unit_head header {};
518
519 /* Base address of this compilation unit. */
520 gdb::optional<CORE_ADDR> base_address;
521
522 /* The language we are debugging. */
523 enum language language = language_unknown;
524 const struct language_defn *language_defn = nullptr;
525
526 const char *producer = nullptr;
527
528 private:
529 /* The symtab builder for this CU. This is only non-NULL when full
530 symbols are being read. */
531 std::unique_ptr<buildsym_compunit> m_builder;
532
533 public:
534 /* The generic symbol table building routines have separate lists for
535 file scope symbols and all all other scopes (local scopes). So
536 we need to select the right one to pass to add_symbol_to_list().
537 We do it by keeping a pointer to the correct list in list_in_scope.
538
539 FIXME: The original dwarf code just treated the file scope as the
540 first local scope, and all other local scopes as nested local
541 scopes, and worked fine. Check to see if we really need to
542 distinguish these in buildsym.c. */
543 struct pending **list_in_scope = nullptr;
544
545 /* Hash table holding all the loaded partial DIEs
546 with partial_die->offset.SECT_OFF as hash. */
547 htab_t partial_dies = nullptr;
548
549 /* Storage for things with the same lifetime as this read-in compilation
550 unit, including partial DIEs. */
551 auto_obstack comp_unit_obstack;
552
553 /* Backlink to our per_cu entry. */
554 struct dwarf2_per_cu_data *per_cu;
555
556 /* The dwarf2_per_objfile that owns this. */
557 dwarf2_per_objfile *per_objfile;
558
559 /* How many compilation units ago was this CU last referenced? */
560 int last_used = 0;
561
562 /* A hash table of DIE cu_offset for following references with
563 die_info->offset.sect_off as hash. */
564 htab_t die_hash = nullptr;
565
566 /* Full DIEs if read in. */
567 struct die_info *dies = nullptr;
568
569 /* A set of pointers to dwarf2_per_cu_data objects for compilation
570 units referenced by this one. Only set during full symbol processing;
571 partial symbol tables do not have dependencies. */
572 htab_t dependencies = nullptr;
573
574 /* Header data from the line table, during full symbol processing. */
575 struct line_header *line_header = nullptr;
576 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
577 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
578 this is the DW_TAG_compile_unit die for this CU. We'll hold on
579 to the line header as long as this DIE is being processed. See
580 process_die_scope. */
581 die_info *line_header_die_owner = nullptr;
582
583 /* A list of methods which need to have physnames computed
584 after all type information has been read. */
585 std::vector<delayed_method_info> method_list;
586
587 /* To be copied to symtab->call_site_htab. */
588 htab_t call_site_htab = nullptr;
589
590 /* Non-NULL if this CU came from a DWO file.
591 There is an invariant here that is important to remember:
592 Except for attributes copied from the top level DIE in the "main"
593 (or "stub") file in preparation for reading the DWO file
594 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
595 Either there isn't a DWO file (in which case this is NULL and the point
596 is moot), or there is and either we're not going to read it (in which
597 case this is NULL) or there is and we are reading it (in which case this
598 is non-NULL). */
599 struct dwo_unit *dwo_unit = nullptr;
600
601 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
602 Note this value comes from the Fission stub CU/TU's DIE. */
603 gdb::optional<ULONGEST> addr_base;
604
605 /* The DW_AT_GNU_ranges_base attribute, if present.
606
607 This is only relevant in the context of pre-DWARF 5 split units. In this
608 context, there is a .debug_ranges section in the linked executable,
609 containing all the ranges data for all the compilation units. Each
610 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
611 indicates the base of its contribution to that section. The DW_AT_ranges
612 attributes in the split-unit are of the form DW_FORM_sec_offset and point
613 into the .debug_ranges section of the linked file. However, they are not
614 "true" DW_FORM_sec_offset, because they are relative to the base of their
615 compilation unit's contribution, rather than relative to the beginning of
616 the section. The DW_AT_GNU_ranges_base value must be added to it to make
617 it relative to the beginning of the section.
618
619 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
620 case, so this value can be added without needing to know whether we are in
621 this case or not.
622
623 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
624 skeleton/stub, it must not have the base added, as it already points to the
625 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
626 have a DW_AT_ranges attribute, we can use the
627
628 die->tag != DW_AT_compile_unit
629
630 to determine whether the base should be added or not. */
631 ULONGEST gnu_ranges_base = 0;
632
633 /* The DW_AT_rnglists_base attribute, if present.
634
635 This is used when processing attributes of form DW_FORM_rnglistx in
636 non-split units. Attributes of this form found in a split unit don't
637 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
638 section. */
639 ULONGEST rnglists_base = 0;
640
641 /* The DW_AT_loclists_base attribute if present. */
642 ULONGEST loclist_base = 0;
643
644 /* When reading debug info generated by older versions of rustc, we
645 have to rewrite some union types to be struct types with a
646 variant part. This rewriting must be done after the CU is fully
647 read in, because otherwise at the point of rewriting some struct
648 type might not have been fully processed. So, we keep a list of
649 all such types here and process them after expansion. */
650 std::vector<struct type *> rust_unions;
651
652 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
653 files, the value is implicitly zero. For DWARF 5 version DWO files, the
654 value is often implicit and is the size of the header of
655 .debug_str_offsets section (8 or 4, depending on the address size). */
656 gdb::optional<ULONGEST> str_offsets_base;
657
658 /* Mark used when releasing cached dies. */
659 bool mark : 1;
660
661 /* This CU references .debug_loc. See the symtab->locations_valid field.
662 This test is imperfect as there may exist optimized debug code not using
663 any location list and still facing inlining issues if handled as
664 unoptimized code. For a future better test see GCC PR other/32998. */
665 bool has_loclist : 1;
666
667 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
668 if all the producer_is_* fields are valid. This information is cached
669 because profiling CU expansion showed excessive time spent in
670 producer_is_gxx_lt_4_6. */
671 bool checked_producer : 1;
672 bool producer_is_gxx_lt_4_6 : 1;
673 bool producer_is_gcc_lt_4_3 : 1;
674 bool producer_is_icc : 1;
675 bool producer_is_icc_lt_14 : 1;
676 bool producer_is_codewarrior : 1;
677
678 /* When true, the file that we're processing is known to have
679 debugging info for C++ namespaces. GCC 3.3.x did not produce
680 this information, but later versions do. */
681
682 bool processing_has_namespace_info : 1;
683
684 struct partial_die_info *find_partial_die (sect_offset sect_off);
685
686 /* If this CU was inherited by another CU (via specification,
687 abstract_origin, etc), this is the ancestor CU. */
688 dwarf2_cu *ancestor;
689
690 /* Get the buildsym_compunit for this CU. */
691 buildsym_compunit *get_builder ()
692 {
693 /* If this CU has a builder associated with it, use that. */
694 if (m_builder != nullptr)
695 return m_builder.get ();
696
697 /* Otherwise, search ancestors for a valid builder. */
698 if (ancestor != nullptr)
699 return ancestor->get_builder ();
700
701 return nullptr;
702 }
703 };
704
705 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
706 This includes type_unit_group and quick_file_names. */
707
708 struct stmt_list_hash
709 {
710 /* The DWO unit this table is from or NULL if there is none. */
711 struct dwo_unit *dwo_unit;
712
713 /* Offset in .debug_line or .debug_line.dwo. */
714 sect_offset line_sect_off;
715 };
716
717 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
718 an object of this type. This contains elements of type unit groups
719 that can be shared across objfiles. The non-shareable parts are in
720 type_unit_group_unshareable. */
721
722 struct type_unit_group
723 {
724 /* dwarf2read.c's main "handle" on a TU symtab.
725 To simplify things we create an artificial CU that "includes" all the
726 type units using this stmt_list so that the rest of the code still has
727 a "per_cu" handle on the symtab. */
728 struct dwarf2_per_cu_data per_cu;
729
730 /* The TUs that share this DW_AT_stmt_list entry.
731 This is added to while parsing type units to build partial symtabs,
732 and is deleted afterwards and not used again. */
733 std::vector<signatured_type *> *tus;
734
735 /* The data used to construct the hash key. */
736 struct stmt_list_hash hash;
737 };
738
739 /* These sections are what may appear in a (real or virtual) DWO file. */
740
741 struct dwo_sections
742 {
743 struct dwarf2_section_info abbrev;
744 struct dwarf2_section_info line;
745 struct dwarf2_section_info loc;
746 struct dwarf2_section_info loclists;
747 struct dwarf2_section_info macinfo;
748 struct dwarf2_section_info macro;
749 struct dwarf2_section_info rnglists;
750 struct dwarf2_section_info str;
751 struct dwarf2_section_info str_offsets;
752 /* In the case of a virtual DWO file, these two are unused. */
753 struct dwarf2_section_info info;
754 std::vector<dwarf2_section_info> types;
755 };
756
757 /* CUs/TUs in DWP/DWO files. */
758
759 struct dwo_unit
760 {
761 /* Backlink to the containing struct dwo_file. */
762 struct dwo_file *dwo_file;
763
764 /* The "id" that distinguishes this CU/TU.
765 .debug_info calls this "dwo_id", .debug_types calls this "signature".
766 Since signatures came first, we stick with it for consistency. */
767 ULONGEST signature;
768
769 /* The section this CU/TU lives in, in the DWO file. */
770 struct dwarf2_section_info *section;
771
772 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
773 sect_offset sect_off;
774 unsigned int length;
775
776 /* For types, offset in the type's DIE of the type defined by this TU. */
777 cu_offset type_offset_in_tu;
778 };
779
780 /* include/dwarf2.h defines the DWP section codes.
781 It defines a max value but it doesn't define a min value, which we
782 use for error checking, so provide one. */
783
784 enum dwp_v2_section_ids
785 {
786 DW_SECT_MIN = 1
787 };
788
789 /* Data for one DWO file.
790
791 This includes virtual DWO files (a virtual DWO file is a DWO file as it
792 appears in a DWP file). DWP files don't really have DWO files per se -
793 comdat folding of types "loses" the DWO file they came from, and from
794 a high level view DWP files appear to contain a mass of random types.
795 However, to maintain consistency with the non-DWP case we pretend DWP
796 files contain virtual DWO files, and we assign each TU with one virtual
797 DWO file (generally based on the line and abbrev section offsets -
798 a heuristic that seems to work in practice). */
799
800 struct dwo_file
801 {
802 dwo_file () = default;
803 DISABLE_COPY_AND_ASSIGN (dwo_file);
804
805 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
806 For virtual DWO files the name is constructed from the section offsets
807 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
808 from related CU+TUs. */
809 const char *dwo_name = nullptr;
810
811 /* The DW_AT_comp_dir attribute. */
812 const char *comp_dir = nullptr;
813
814 /* The bfd, when the file is open. Otherwise this is NULL.
815 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
816 gdb_bfd_ref_ptr dbfd;
817
818 /* The sections that make up this DWO file.
819 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
820 sections (for lack of a better name). */
821 struct dwo_sections sections {};
822
823 /* The CUs in the file.
824 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
825 an extension to handle LLVM's Link Time Optimization output (where
826 multiple source files may be compiled into a single object/dwo pair). */
827 htab_up cus;
828
829 /* Table of TUs in the file.
830 Each element is a struct dwo_unit. */
831 htab_up tus;
832 };
833
834 /* These sections are what may appear in a DWP file. */
835
836 struct dwp_sections
837 {
838 /* These are used by all DWP versions (1, 2 and 5). */
839 struct dwarf2_section_info str;
840 struct dwarf2_section_info cu_index;
841 struct dwarf2_section_info tu_index;
842
843 /* These are only used by DWP version 2 and version 5 files.
844 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
845 sections are referenced by section number, and are not recorded here.
846 In DWP version 2 or 5 there is at most one copy of all these sections,
847 each section being (effectively) comprised of the concatenation of all of
848 the individual sections that exist in the version 1 format.
849 To keep the code simple we treat each of these concatenated pieces as a
850 section itself (a virtual section?). */
851 struct dwarf2_section_info abbrev;
852 struct dwarf2_section_info info;
853 struct dwarf2_section_info line;
854 struct dwarf2_section_info loc;
855 struct dwarf2_section_info loclists;
856 struct dwarf2_section_info macinfo;
857 struct dwarf2_section_info macro;
858 struct dwarf2_section_info rnglists;
859 struct dwarf2_section_info str_offsets;
860 struct dwarf2_section_info types;
861 };
862
863 /* These sections are what may appear in a virtual DWO file in DWP version 1.
864 A virtual DWO file is a DWO file as it appears in a DWP file. */
865
866 struct virtual_v1_dwo_sections
867 {
868 struct dwarf2_section_info abbrev;
869 struct dwarf2_section_info line;
870 struct dwarf2_section_info loc;
871 struct dwarf2_section_info macinfo;
872 struct dwarf2_section_info macro;
873 struct dwarf2_section_info str_offsets;
874 /* Each DWP hash table entry records one CU or one TU.
875 That is recorded here, and copied to dwo_unit.section. */
876 struct dwarf2_section_info info_or_types;
877 };
878
879 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
880 In version 2, the sections of the DWO files are concatenated together
881 and stored in one section of that name. Thus each ELF section contains
882 several "virtual" sections. */
883
884 struct virtual_v2_or_v5_dwo_sections
885 {
886 bfd_size_type abbrev_offset;
887 bfd_size_type abbrev_size;
888
889 bfd_size_type line_offset;
890 bfd_size_type line_size;
891
892 bfd_size_type loc_offset;
893 bfd_size_type loc_size;
894
895 bfd_size_type loclists_offset;
896 bfd_size_type loclists_size;
897
898 bfd_size_type macinfo_offset;
899 bfd_size_type macinfo_size;
900
901 bfd_size_type macro_offset;
902 bfd_size_type macro_size;
903
904 bfd_size_type rnglists_offset;
905 bfd_size_type rnglists_size;
906
907 bfd_size_type str_offsets_offset;
908 bfd_size_type str_offsets_size;
909
910 /* Each DWP hash table entry records one CU or one TU.
911 That is recorded here, and copied to dwo_unit.section. */
912 bfd_size_type info_or_types_offset;
913 bfd_size_type info_or_types_size;
914 };
915
916 /* Contents of DWP hash tables. */
917
918 struct dwp_hash_table
919 {
920 uint32_t version, nr_columns;
921 uint32_t nr_units, nr_slots;
922 const gdb_byte *hash_table, *unit_table;
923 union
924 {
925 struct
926 {
927 const gdb_byte *indices;
928 } v1;
929 struct
930 {
931 /* This is indexed by column number and gives the id of the section
932 in that column. */
933 #define MAX_NR_V2_DWO_SECTIONS \
934 (1 /* .debug_info or .debug_types */ \
935 + 1 /* .debug_abbrev */ \
936 + 1 /* .debug_line */ \
937 + 1 /* .debug_loc */ \
938 + 1 /* .debug_str_offsets */ \
939 + 1 /* .debug_macro or .debug_macinfo */)
940 int section_ids[MAX_NR_V2_DWO_SECTIONS];
941 const gdb_byte *offsets;
942 const gdb_byte *sizes;
943 } v2;
944 struct
945 {
946 /* This is indexed by column number and gives the id of the section
947 in that column. */
948 #define MAX_NR_V5_DWO_SECTIONS \
949 (1 /* .debug_info */ \
950 + 1 /* .debug_abbrev */ \
951 + 1 /* .debug_line */ \
952 + 1 /* .debug_loclists */ \
953 + 1 /* .debug_str_offsets */ \
954 + 1 /* .debug_macro */ \
955 + 1 /* .debug_rnglists */)
956 int section_ids[MAX_NR_V5_DWO_SECTIONS];
957 const gdb_byte *offsets;
958 const gdb_byte *sizes;
959 } v5;
960 } section_pool;
961 };
962
963 /* Data for one DWP file. */
964
965 struct dwp_file
966 {
967 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
968 : name (name_),
969 dbfd (std::move (abfd))
970 {
971 }
972
973 /* Name of the file. */
974 const char *name;
975
976 /* File format version. */
977 int version = 0;
978
979 /* The bfd. */
980 gdb_bfd_ref_ptr dbfd;
981
982 /* Section info for this file. */
983 struct dwp_sections sections {};
984
985 /* Table of CUs in the file. */
986 const struct dwp_hash_table *cus = nullptr;
987
988 /* Table of TUs in the file. */
989 const struct dwp_hash_table *tus = nullptr;
990
991 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
992 htab_up loaded_cus;
993 htab_up loaded_tus;
994
995 /* Table to map ELF section numbers to their sections.
996 This is only needed for the DWP V1 file format. */
997 unsigned int num_sections = 0;
998 asection **elf_sections = nullptr;
999 };
1000
1001 /* Struct used to pass misc. parameters to read_die_and_children, et
1002 al. which are used for both .debug_info and .debug_types dies.
1003 All parameters here are unchanging for the life of the call. This
1004 struct exists to abstract away the constant parameters of die reading. */
1005
1006 struct die_reader_specs
1007 {
1008 /* The bfd of die_section. */
1009 bfd *abfd;
1010
1011 /* The CU of the DIE we are parsing. */
1012 struct dwarf2_cu *cu;
1013
1014 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1015 struct dwo_file *dwo_file;
1016
1017 /* The section the die comes from.
1018 This is either .debug_info or .debug_types, or the .dwo variants. */
1019 struct dwarf2_section_info *die_section;
1020
1021 /* die_section->buffer. */
1022 const gdb_byte *buffer;
1023
1024 /* The end of the buffer. */
1025 const gdb_byte *buffer_end;
1026
1027 /* The abbreviation table to use when reading the DIEs. */
1028 struct abbrev_table *abbrev_table;
1029 };
1030
1031 /* A subclass of die_reader_specs that holds storage and has complex
1032 constructor and destructor behavior. */
1033
1034 class cutu_reader : public die_reader_specs
1035 {
1036 public:
1037
1038 cutu_reader (dwarf2_per_cu_data *this_cu,
1039 dwarf2_per_objfile *per_objfile,
1040 struct abbrev_table *abbrev_table,
1041 dwarf2_cu *existing_cu,
1042 bool skip_partial);
1043
1044 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
1045 dwarf2_per_objfile *per_objfile,
1046 struct dwarf2_cu *parent_cu = nullptr,
1047 struct dwo_file *dwo_file = nullptr);
1048
1049 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1050
1051 const gdb_byte *info_ptr = nullptr;
1052 struct die_info *comp_unit_die = nullptr;
1053 bool dummy_p = false;
1054
1055 /* Release the new CU, putting it on the chain. This cannot be done
1056 for dummy CUs. */
1057 void keep ();
1058
1059 private:
1060 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1061 dwarf2_per_objfile *per_objfile,
1062 dwarf2_cu *existing_cu);
1063
1064 struct dwarf2_per_cu_data *m_this_cu;
1065 std::unique_ptr<dwarf2_cu> m_new_cu;
1066
1067 /* The ordinary abbreviation table. */
1068 abbrev_table_up m_abbrev_table_holder;
1069
1070 /* The DWO abbreviation table. */
1071 abbrev_table_up m_dwo_abbrev_table;
1072 };
1073
1074 /* When we construct a partial symbol table entry we only
1075 need this much information. */
1076 struct partial_die_info : public allocate_on_obstack
1077 {
1078 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1079
1080 /* Disable assign but still keep copy ctor, which is needed
1081 load_partial_dies. */
1082 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1083
1084 /* Adjust the partial die before generating a symbol for it. This
1085 function may set the is_external flag or change the DIE's
1086 name. */
1087 void fixup (struct dwarf2_cu *cu);
1088
1089 /* Read a minimal amount of information into the minimal die
1090 structure. */
1091 const gdb_byte *read (const struct die_reader_specs *reader,
1092 const struct abbrev_info &abbrev,
1093 const gdb_byte *info_ptr);
1094
1095 /* Compute the name of this partial DIE. This memoizes the
1096 result, so it is safe to call multiple times. */
1097 const char *name (dwarf2_cu *cu);
1098
1099 /* Offset of this DIE. */
1100 const sect_offset sect_off;
1101
1102 /* DWARF-2 tag for this DIE. */
1103 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1104
1105 /* Assorted flags describing the data found in this DIE. */
1106 const unsigned int has_children : 1;
1107
1108 unsigned int is_external : 1;
1109 unsigned int is_declaration : 1;
1110 unsigned int has_type : 1;
1111 unsigned int has_specification : 1;
1112 unsigned int has_pc_info : 1;
1113 unsigned int may_be_inlined : 1;
1114
1115 /* This DIE has been marked DW_AT_main_subprogram. */
1116 unsigned int main_subprogram : 1;
1117
1118 /* Flag set if the SCOPE field of this structure has been
1119 computed. */
1120 unsigned int scope_set : 1;
1121
1122 /* Flag set if the DIE has a byte_size attribute. */
1123 unsigned int has_byte_size : 1;
1124
1125 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1126 unsigned int has_const_value : 1;
1127
1128 /* Flag set if any of the DIE's children are template arguments. */
1129 unsigned int has_template_arguments : 1;
1130
1131 /* Flag set if fixup has been called on this die. */
1132 unsigned int fixup_called : 1;
1133
1134 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1135 unsigned int is_dwz : 1;
1136
1137 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1138 unsigned int spec_is_dwz : 1;
1139
1140 unsigned int canonical_name : 1;
1141
1142 /* The name of this DIE. Normally the value of DW_AT_name, but
1143 sometimes a default name for unnamed DIEs. */
1144 const char *raw_name = nullptr;
1145
1146 /* The linkage name, if present. */
1147 const char *linkage_name = nullptr;
1148
1149 /* The scope to prepend to our children. This is generally
1150 allocated on the comp_unit_obstack, so will disappear
1151 when this compilation unit leaves the cache. */
1152 const char *scope = nullptr;
1153
1154 /* Some data associated with the partial DIE. The tag determines
1155 which field is live. */
1156 union
1157 {
1158 /* The location description associated with this DIE, if any. */
1159 struct dwarf_block *locdesc;
1160 /* The offset of an import, for DW_TAG_imported_unit. */
1161 sect_offset sect_off;
1162 } d {};
1163
1164 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1165 CORE_ADDR lowpc = 0;
1166 CORE_ADDR highpc = 0;
1167
1168 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1169 DW_AT_sibling, if any. */
1170 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1171 could return DW_AT_sibling values to its caller load_partial_dies. */
1172 const gdb_byte *sibling = nullptr;
1173
1174 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1175 DW_AT_specification (or DW_AT_abstract_origin or
1176 DW_AT_extension). */
1177 sect_offset spec_offset {};
1178
1179 /* Pointers to this DIE's parent, first child, and next sibling,
1180 if any. */
1181 struct partial_die_info *die_parent = nullptr;
1182 struct partial_die_info *die_child = nullptr;
1183 struct partial_die_info *die_sibling = nullptr;
1184
1185 friend struct partial_die_info *
1186 dwarf2_cu::find_partial_die (sect_offset sect_off);
1187
1188 private:
1189 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1190 partial_die_info (sect_offset sect_off)
1191 : partial_die_info (sect_off, DW_TAG_padding, 0)
1192 {
1193 }
1194
1195 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1196 int has_children_)
1197 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1198 {
1199 is_external = 0;
1200 is_declaration = 0;
1201 has_type = 0;
1202 has_specification = 0;
1203 has_pc_info = 0;
1204 may_be_inlined = 0;
1205 main_subprogram = 0;
1206 scope_set = 0;
1207 has_byte_size = 0;
1208 has_const_value = 0;
1209 has_template_arguments = 0;
1210 fixup_called = 0;
1211 is_dwz = 0;
1212 spec_is_dwz = 0;
1213 canonical_name = 0;
1214 }
1215 };
1216
1217 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1218 but this would require a corresponding change in unpack_field_as_long
1219 and friends. */
1220 static int bits_per_byte = 8;
1221
1222 struct variant_part_builder;
1223
1224 /* When reading a variant, we track a bit more information about the
1225 field, and store it in an object of this type. */
1226
1227 struct variant_field
1228 {
1229 int first_field = -1;
1230 int last_field = -1;
1231
1232 /* A variant can contain other variant parts. */
1233 std::vector<variant_part_builder> variant_parts;
1234
1235 /* If we see a DW_TAG_variant, then this will be set if this is the
1236 default branch. */
1237 bool default_branch = false;
1238 /* If we see a DW_AT_discr_value, then this will be the discriminant
1239 value. */
1240 ULONGEST discriminant_value = 0;
1241 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1242 data. */
1243 struct dwarf_block *discr_list_data = nullptr;
1244 };
1245
1246 /* This represents a DW_TAG_variant_part. */
1247
1248 struct variant_part_builder
1249 {
1250 /* The offset of the discriminant field. */
1251 sect_offset discriminant_offset {};
1252
1253 /* Variants that are direct children of this variant part. */
1254 std::vector<variant_field> variants;
1255
1256 /* True if we're currently reading a variant. */
1257 bool processing_variant = false;
1258 };
1259
1260 struct nextfield
1261 {
1262 int accessibility = 0;
1263 int virtuality = 0;
1264 /* Variant parts need to find the discriminant, which is a DIE
1265 reference. We track the section offset of each field to make
1266 this link. */
1267 sect_offset offset;
1268 struct field field {};
1269 };
1270
1271 struct fnfieldlist
1272 {
1273 const char *name = nullptr;
1274 std::vector<struct fn_field> fnfields;
1275 };
1276
1277 /* The routines that read and process dies for a C struct or C++ class
1278 pass lists of data member fields and lists of member function fields
1279 in an instance of a field_info structure, as defined below. */
1280 struct field_info
1281 {
1282 /* List of data member and baseclasses fields. */
1283 std::vector<struct nextfield> fields;
1284 std::vector<struct nextfield> baseclasses;
1285
1286 /* Set if the accessibility of one of the fields is not public. */
1287 bool non_public_fields = false;
1288
1289 /* Member function fieldlist array, contains name of possibly overloaded
1290 member function, number of overloaded member functions and a pointer
1291 to the head of the member function field chain. */
1292 std::vector<struct fnfieldlist> fnfieldlists;
1293
1294 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1295 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1296 std::vector<struct decl_field> typedef_field_list;
1297
1298 /* Nested types defined by this class and the number of elements in this
1299 list. */
1300 std::vector<struct decl_field> nested_types_list;
1301
1302 /* If non-null, this is the variant part we are currently
1303 reading. */
1304 variant_part_builder *current_variant_part = nullptr;
1305 /* This holds all the top-level variant parts attached to the type
1306 we're reading. */
1307 std::vector<variant_part_builder> variant_parts;
1308
1309 /* Return the total number of fields (including baseclasses). */
1310 int nfields () const
1311 {
1312 return fields.size () + baseclasses.size ();
1313 }
1314 };
1315
1316 /* Loaded secondary compilation units are kept in memory until they
1317 have not been referenced for the processing of this many
1318 compilation units. Set this to zero to disable caching. Cache
1319 sizes of up to at least twenty will improve startup time for
1320 typical inter-CU-reference binaries, at an obvious memory cost. */
1321 static int dwarf_max_cache_age = 5;
1322 static void
1323 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1324 struct cmd_list_element *c, const char *value)
1325 {
1326 fprintf_filtered (file, _("The upper bound on the age of cached "
1327 "DWARF compilation units is %s.\n"),
1328 value);
1329 }
1330 \f
1331 /* local function prototypes */
1332
1333 static void dwarf2_find_base_address (struct die_info *die,
1334 struct dwarf2_cu *cu);
1335
1336 static dwarf2_psymtab *create_partial_symtab
1337 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1338 const char *name);
1339
1340 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1341 const gdb_byte *info_ptr,
1342 struct die_info *type_unit_die);
1343
1344 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1345
1346 static void scan_partial_symbols (struct partial_die_info *,
1347 CORE_ADDR *, CORE_ADDR *,
1348 int, struct dwarf2_cu *);
1349
1350 static void add_partial_symbol (struct partial_die_info *,
1351 struct dwarf2_cu *);
1352
1353 static void add_partial_namespace (struct partial_die_info *pdi,
1354 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1355 int set_addrmap, struct dwarf2_cu *cu);
1356
1357 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1358 CORE_ADDR *highpc, int set_addrmap,
1359 struct dwarf2_cu *cu);
1360
1361 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1362 struct dwarf2_cu *cu);
1363
1364 static void add_partial_subprogram (struct partial_die_info *pdi,
1365 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1366 int need_pc, struct dwarf2_cu *cu);
1367
1368 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1369
1370 static struct partial_die_info *load_partial_dies
1371 (const struct die_reader_specs *, const gdb_byte *, int);
1372
1373 /* A pair of partial_die_info and compilation unit. */
1374 struct cu_partial_die_info
1375 {
1376 /* The compilation unit of the partial_die_info. */
1377 struct dwarf2_cu *cu;
1378 /* A partial_die_info. */
1379 struct partial_die_info *pdi;
1380
1381 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1382 : cu (cu),
1383 pdi (pdi)
1384 { /* Nothing. */ }
1385
1386 private:
1387 cu_partial_die_info () = delete;
1388 };
1389
1390 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1391 struct dwarf2_cu *);
1392
1393 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1394 struct attribute *,
1395 const struct attr_abbrev *,
1396 const gdb_byte *);
1397
1398 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1399 struct attribute *attr, dwarf_tag tag);
1400
1401 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1402
1403 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1404 dwarf2_section_info *, sect_offset);
1405
1406 static const char *read_indirect_string
1407 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1408 const struct comp_unit_head *, unsigned int *);
1409
1410 static const char *read_indirect_string_at_offset
1411 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1412
1413 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1414 const gdb_byte *,
1415 unsigned int *);
1416
1417 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1418 ULONGEST str_index);
1419
1420 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1421 ULONGEST str_index);
1422
1423 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1424
1425 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1426 struct dwarf2_cu *);
1427
1428 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1429 struct dwarf2_cu *cu);
1430
1431 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1432
1433 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1434 struct dwarf2_cu *cu);
1435
1436 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1437
1438 static struct die_info *die_specification (struct die_info *die,
1439 struct dwarf2_cu **);
1440
1441 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1442 struct dwarf2_cu *cu);
1443
1444 static void dwarf_decode_lines (struct line_header *, const char *,
1445 struct dwarf2_cu *, dwarf2_psymtab *,
1446 CORE_ADDR, int decode_mapping);
1447
1448 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1449 const char *);
1450
1451 static struct symbol *new_symbol (struct die_info *, struct type *,
1452 struct dwarf2_cu *, struct symbol * = NULL);
1453
1454 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1455 struct dwarf2_cu *);
1456
1457 static void dwarf2_const_value_attr (const struct attribute *attr,
1458 struct type *type,
1459 const char *name,
1460 struct obstack *obstack,
1461 struct dwarf2_cu *cu, LONGEST *value,
1462 const gdb_byte **bytes,
1463 struct dwarf2_locexpr_baton **baton);
1464
1465 static struct type *read_subrange_index_type (struct die_info *die,
1466 struct dwarf2_cu *cu);
1467
1468 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1469
1470 static int need_gnat_info (struct dwarf2_cu *);
1471
1472 static struct type *die_descriptive_type (struct die_info *,
1473 struct dwarf2_cu *);
1474
1475 static void set_descriptive_type (struct type *, struct die_info *,
1476 struct dwarf2_cu *);
1477
1478 static struct type *die_containing_type (struct die_info *,
1479 struct dwarf2_cu *);
1480
1481 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1482 struct dwarf2_cu *);
1483
1484 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1485
1486 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1487
1488 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1489
1490 static char *typename_concat (struct obstack *obs, const char *prefix,
1491 const char *suffix, int physname,
1492 struct dwarf2_cu *cu);
1493
1494 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1495
1496 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1497
1498 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1499
1500 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1501
1502 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1503
1504 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1505
1506 /* Return the .debug_loclists section to use for cu. */
1507 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1508
1509 /* Return the .debug_rnglists section to use for cu. */
1510 static struct dwarf2_section_info *cu_debug_rnglists_section
1511 (struct dwarf2_cu *cu, dwarf_tag tag);
1512
1513 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1514 values. Keep the items ordered with increasing constraints compliance. */
1515 enum pc_bounds_kind
1516 {
1517 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1518 PC_BOUNDS_NOT_PRESENT,
1519
1520 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1521 were present but they do not form a valid range of PC addresses. */
1522 PC_BOUNDS_INVALID,
1523
1524 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1525 PC_BOUNDS_RANGES,
1526
1527 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1528 PC_BOUNDS_HIGH_LOW,
1529 };
1530
1531 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1532 CORE_ADDR *, CORE_ADDR *,
1533 struct dwarf2_cu *,
1534 dwarf2_psymtab *);
1535
1536 static void get_scope_pc_bounds (struct die_info *,
1537 CORE_ADDR *, CORE_ADDR *,
1538 struct dwarf2_cu *);
1539
1540 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1541 CORE_ADDR, struct dwarf2_cu *);
1542
1543 static void dwarf2_add_field (struct field_info *, struct die_info *,
1544 struct dwarf2_cu *);
1545
1546 static void dwarf2_attach_fields_to_type (struct field_info *,
1547 struct type *, struct dwarf2_cu *);
1548
1549 static void dwarf2_add_member_fn (struct field_info *,
1550 struct die_info *, struct type *,
1551 struct dwarf2_cu *);
1552
1553 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1554 struct type *,
1555 struct dwarf2_cu *);
1556
1557 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1558
1559 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1560
1561 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1562
1563 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1564
1565 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1566
1567 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1568
1569 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1570
1571 static struct type *read_module_type (struct die_info *die,
1572 struct dwarf2_cu *cu);
1573
1574 static const char *namespace_name (struct die_info *die,
1575 int *is_anonymous, struct dwarf2_cu *);
1576
1577 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1578
1579 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1580 bool * = nullptr);
1581
1582 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1583 struct dwarf2_cu *);
1584
1585 static struct die_info *read_die_and_siblings_1
1586 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1587 struct die_info *);
1588
1589 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1590 const gdb_byte *info_ptr,
1591 const gdb_byte **new_info_ptr,
1592 struct die_info *parent);
1593
1594 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1595 struct die_info **, const gdb_byte *,
1596 int);
1597
1598 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1599 struct die_info **, const gdb_byte *);
1600
1601 static void process_die (struct die_info *, struct dwarf2_cu *);
1602
1603 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1604 struct objfile *);
1605
1606 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1607
1608 static const char *dwarf2_full_name (const char *name,
1609 struct die_info *die,
1610 struct dwarf2_cu *cu);
1611
1612 static const char *dwarf2_physname (const char *name, struct die_info *die,
1613 struct dwarf2_cu *cu);
1614
1615 static struct die_info *dwarf2_extension (struct die_info *die,
1616 struct dwarf2_cu **);
1617
1618 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1619
1620 static void dump_die_for_error (struct die_info *);
1621
1622 static void dump_die_1 (struct ui_file *, int level, int max_level,
1623 struct die_info *);
1624
1625 /*static*/ void dump_die (struct die_info *, int max_level);
1626
1627 static void store_in_ref_table (struct die_info *,
1628 struct dwarf2_cu *);
1629
1630 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1631 const struct attribute *,
1632 struct dwarf2_cu **);
1633
1634 static struct die_info *follow_die_ref (struct die_info *,
1635 const struct attribute *,
1636 struct dwarf2_cu **);
1637
1638 static struct die_info *follow_die_sig (struct die_info *,
1639 const struct attribute *,
1640 struct dwarf2_cu **);
1641
1642 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1643 struct dwarf2_cu *);
1644
1645 static struct type *get_DW_AT_signature_type (struct die_info *,
1646 const struct attribute *,
1647 struct dwarf2_cu *);
1648
1649 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile);
1651
1652 static void read_signatured_type (signatured_type *sig_type,
1653 dwarf2_per_objfile *per_objfile);
1654
1655 static int attr_to_dynamic_prop (const struct attribute *attr,
1656 struct die_info *die, struct dwarf2_cu *cu,
1657 struct dynamic_prop *prop, struct type *type);
1658
1659 /* memory allocation interface */
1660
1661 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1662
1663 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1664
1665 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1666
1667 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1668 struct dwarf2_loclist_baton *baton,
1669 const struct attribute *attr);
1670
1671 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1672 struct symbol *sym,
1673 struct dwarf2_cu *cu,
1674 int is_block);
1675
1676 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1677 const gdb_byte *info_ptr,
1678 const struct abbrev_info *abbrev);
1679
1680 static hashval_t partial_die_hash (const void *item);
1681
1682 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1683
1684 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1685 (sect_offset sect_off, unsigned int offset_in_dwz,
1686 dwarf2_per_objfile *per_objfile);
1687
1688 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1689 struct die_info *comp_unit_die,
1690 enum language pretend_language);
1691
1692 static struct type *set_die_type (struct die_info *, struct type *,
1693 struct dwarf2_cu *, bool = false);
1694
1695 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1696
1697 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1698
1699 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1700 dwarf2_per_objfile *per_objfile,
1701 dwarf2_cu *existing_cu,
1702 bool skip_partial,
1703 enum language pretend_language);
1704
1705 static void process_full_comp_unit (dwarf2_cu *cu,
1706 enum language pretend_language);
1707
1708 static void process_full_type_unit (dwarf2_cu *cu,
1709 enum language pretend_language);
1710
1711 static void dwarf2_add_dependence (struct dwarf2_cu *,
1712 struct dwarf2_per_cu_data *);
1713
1714 static void dwarf2_mark (struct dwarf2_cu *);
1715
1716 static struct type *get_die_type_at_offset (sect_offset,
1717 dwarf2_per_cu_data *per_cu,
1718 dwarf2_per_objfile *per_objfile);
1719
1720 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1721
1722 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1723 dwarf2_per_objfile *per_objfile,
1724 enum language pretend_language);
1725
1726 static void process_queue (dwarf2_per_objfile *per_objfile);
1727
1728 /* Class, the destructor of which frees all allocated queue entries. This
1729 will only have work to do if an error was thrown while processing the
1730 dwarf. If no error was thrown then the queue entries should have all
1731 been processed, and freed, as we went along. */
1732
1733 class dwarf2_queue_guard
1734 {
1735 public:
1736 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1737 : m_per_objfile (per_objfile)
1738 {
1739 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1740
1741 m_per_objfile->per_bfd->queue.emplace ();
1742 }
1743
1744 /* Free any entries remaining on the queue. There should only be
1745 entries left if we hit an error while processing the dwarf. */
1746 ~dwarf2_queue_guard ()
1747 {
1748 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1749
1750 m_per_objfile->per_bfd->queue.reset ();
1751 }
1752
1753 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1754
1755 private:
1756 dwarf2_per_objfile *m_per_objfile;
1757 };
1758
1759 dwarf2_queue_item::~dwarf2_queue_item ()
1760 {
1761 /* Anything still marked queued is likely to be in an
1762 inconsistent state, so discard it. */
1763 if (per_cu->queued)
1764 {
1765 per_objfile->remove_cu (per_cu);
1766 per_cu->queued = 0;
1767 }
1768 }
1769
1770 /* The return type of find_file_and_directory. Note, the enclosed
1771 string pointers are only valid while this object is valid. */
1772
1773 struct file_and_directory
1774 {
1775 /* The filename. This is never NULL. */
1776 const char *name;
1777
1778 /* The compilation directory. NULL if not known. If we needed to
1779 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1780 points directly to the DW_AT_comp_dir string attribute owned by
1781 the obstack that owns the DIE. */
1782 const char *comp_dir;
1783
1784 /* If we needed to build a new string for comp_dir, this is what
1785 owns the storage. */
1786 std::string comp_dir_storage;
1787 };
1788
1789 static file_and_directory find_file_and_directory (struct die_info *die,
1790 struct dwarf2_cu *cu);
1791
1792 static htab_up allocate_signatured_type_table ();
1793
1794 static htab_up allocate_dwo_unit_table ();
1795
1796 static struct dwo_unit *lookup_dwo_unit_in_dwp
1797 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1798 const char *comp_dir, ULONGEST signature, int is_debug_types);
1799
1800 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1801
1802 static struct dwo_unit *lookup_dwo_comp_unit
1803 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1804 ULONGEST signature);
1805
1806 static struct dwo_unit *lookup_dwo_type_unit
1807 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1808
1809 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1810
1811 /* A unique pointer to a dwo_file. */
1812
1813 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1814
1815 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1816
1817 static void check_producer (struct dwarf2_cu *cu);
1818
1819 static void free_line_header_voidp (void *arg);
1820 \f
1821 /* Various complaints about symbol reading that don't abort the process. */
1822
1823 static void
1824 dwarf2_debug_line_missing_file_complaint (void)
1825 {
1826 complaint (_(".debug_line section has line data without a file"));
1827 }
1828
1829 static void
1830 dwarf2_debug_line_missing_end_sequence_complaint (void)
1831 {
1832 complaint (_(".debug_line section has line "
1833 "program sequence without an end"));
1834 }
1835
1836 static void
1837 dwarf2_complex_location_expr_complaint (void)
1838 {
1839 complaint (_("location expression too complex"));
1840 }
1841
1842 static void
1843 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1844 int arg3)
1845 {
1846 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1847 arg1, arg2, arg3);
1848 }
1849
1850 static void
1851 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1852 {
1853 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1854 arg1, arg2);
1855 }
1856
1857 /* Hash function for line_header_hash. */
1858
1859 static hashval_t
1860 line_header_hash (const struct line_header *ofs)
1861 {
1862 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1863 }
1864
1865 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1866
1867 static hashval_t
1868 line_header_hash_voidp (const void *item)
1869 {
1870 const struct line_header *ofs = (const struct line_header *) item;
1871
1872 return line_header_hash (ofs);
1873 }
1874
1875 /* Equality function for line_header_hash. */
1876
1877 static int
1878 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1879 {
1880 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1881 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1882
1883 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1884 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1885 }
1886
1887 \f
1888
1889 /* See declaration. */
1890
1891 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1892 bool can_copy_)
1893 : obfd (obfd),
1894 can_copy (can_copy_)
1895 {
1896 if (names == NULL)
1897 names = &dwarf2_elf_names;
1898
1899 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1900 locate_sections (obfd, sec, *names);
1901 }
1902
1903 dwarf2_per_bfd::~dwarf2_per_bfd ()
1904 {
1905 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1906 per_cu->imported_symtabs_free ();
1907
1908 for (signatured_type *sig_type : all_type_units)
1909 sig_type->per_cu.imported_symtabs_free ();
1910
1911 /* Everything else should be on this->obstack. */
1912 }
1913
1914 /* See read.h. */
1915
1916 void
1917 dwarf2_per_objfile::remove_all_cus ()
1918 {
1919 gdb_assert (!this->per_bfd->queue.has_value ());
1920
1921 for (auto pair : m_dwarf2_cus)
1922 delete pair.second;
1923
1924 m_dwarf2_cus.clear ();
1925 }
1926
1927 /* A helper class that calls free_cached_comp_units on
1928 destruction. */
1929
1930 class free_cached_comp_units
1931 {
1932 public:
1933
1934 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1935 : m_per_objfile (per_objfile)
1936 {
1937 }
1938
1939 ~free_cached_comp_units ()
1940 {
1941 m_per_objfile->remove_all_cus ();
1942 }
1943
1944 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1945
1946 private:
1947
1948 dwarf2_per_objfile *m_per_objfile;
1949 };
1950
1951 /* See read.h. */
1952
1953 bool
1954 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1955 {
1956 gdb_assert (per_cu->index < this->m_symtabs.size ());
1957
1958 return this->m_symtabs[per_cu->index] != nullptr;
1959 }
1960
1961 /* See read.h. */
1962
1963 compunit_symtab *
1964 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1965 {
1966 gdb_assert (per_cu->index < this->m_symtabs.size ());
1967
1968 return this->m_symtabs[per_cu->index];
1969 }
1970
1971 /* See read.h. */
1972
1973 void
1974 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1975 compunit_symtab *symtab)
1976 {
1977 gdb_assert (per_cu->index < this->m_symtabs.size ());
1978 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1979
1980 this->m_symtabs[per_cu->index] = symtab;
1981 }
1982
1983 /* Try to locate the sections we need for DWARF 2 debugging
1984 information and return true if we have enough to do something.
1985 NAMES points to the dwarf2 section names, or is NULL if the standard
1986 ELF names are used. CAN_COPY is true for formats where symbol
1987 interposition is possible and so symbol values must follow copy
1988 relocation rules. */
1989
1990 int
1991 dwarf2_has_info (struct objfile *objfile,
1992 const struct dwarf2_debug_sections *names,
1993 bool can_copy)
1994 {
1995 if (objfile->flags & OBJF_READNEVER)
1996 return 0;
1997
1998 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1999
2000 if (per_objfile == NULL)
2001 {
2002 dwarf2_per_bfd *per_bfd;
2003
2004 /* We can share a "dwarf2_per_bfd" with other objfiles if the
2005 BFD doesn't require relocations.
2006
2007 We don't share with objfiles for which -readnow was requested,
2008 because it would complicate things when loading the same BFD with
2009 -readnow and then without -readnow. */
2010 if (!gdb_bfd_requires_relocations (objfile->obfd)
2011 && (objfile->flags & OBJF_READNOW) == 0)
2012 {
2013 /* See if one has been created for this BFD yet. */
2014 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
2015
2016 if (per_bfd == nullptr)
2017 {
2018 /* No, create it now. */
2019 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2020 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2021 }
2022 }
2023 else
2024 {
2025 /* No sharing possible, create one specifically for this objfile. */
2026 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2027 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2028 }
2029
2030 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
2031 }
2032
2033 return (!per_objfile->per_bfd->info.is_virtual
2034 && per_objfile->per_bfd->info.s.section != NULL
2035 && !per_objfile->per_bfd->abbrev.is_virtual
2036 && per_objfile->per_bfd->abbrev.s.section != NULL);
2037 }
2038
2039 /* See declaration. */
2040
2041 void
2042 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2043 const dwarf2_debug_sections &names)
2044 {
2045 flagword aflag = bfd_section_flags (sectp);
2046
2047 if ((aflag & SEC_HAS_CONTENTS) == 0)
2048 {
2049 }
2050 else if (elf_section_data (sectp)->this_hdr.sh_size
2051 > bfd_get_file_size (abfd))
2052 {
2053 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2054 warning (_("Discarding section %s which has a section size (%s"
2055 ") larger than the file size [in module %s]"),
2056 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2057 bfd_get_filename (abfd));
2058 }
2059 else if (names.info.matches (sectp->name))
2060 {
2061 this->info.s.section = sectp;
2062 this->info.size = bfd_section_size (sectp);
2063 }
2064 else if (names.abbrev.matches (sectp->name))
2065 {
2066 this->abbrev.s.section = sectp;
2067 this->abbrev.size = bfd_section_size (sectp);
2068 }
2069 else if (names.line.matches (sectp->name))
2070 {
2071 this->line.s.section = sectp;
2072 this->line.size = bfd_section_size (sectp);
2073 }
2074 else if (names.loc.matches (sectp->name))
2075 {
2076 this->loc.s.section = sectp;
2077 this->loc.size = bfd_section_size (sectp);
2078 }
2079 else if (names.loclists.matches (sectp->name))
2080 {
2081 this->loclists.s.section = sectp;
2082 this->loclists.size = bfd_section_size (sectp);
2083 }
2084 else if (names.macinfo.matches (sectp->name))
2085 {
2086 this->macinfo.s.section = sectp;
2087 this->macinfo.size = bfd_section_size (sectp);
2088 }
2089 else if (names.macro.matches (sectp->name))
2090 {
2091 this->macro.s.section = sectp;
2092 this->macro.size = bfd_section_size (sectp);
2093 }
2094 else if (names.str.matches (sectp->name))
2095 {
2096 this->str.s.section = sectp;
2097 this->str.size = bfd_section_size (sectp);
2098 }
2099 else if (names.str_offsets.matches (sectp->name))
2100 {
2101 this->str_offsets.s.section = sectp;
2102 this->str_offsets.size = bfd_section_size (sectp);
2103 }
2104 else if (names.line_str.matches (sectp->name))
2105 {
2106 this->line_str.s.section = sectp;
2107 this->line_str.size = bfd_section_size (sectp);
2108 }
2109 else if (names.addr.matches (sectp->name))
2110 {
2111 this->addr.s.section = sectp;
2112 this->addr.size = bfd_section_size (sectp);
2113 }
2114 else if (names.frame.matches (sectp->name))
2115 {
2116 this->frame.s.section = sectp;
2117 this->frame.size = bfd_section_size (sectp);
2118 }
2119 else if (names.eh_frame.matches (sectp->name))
2120 {
2121 this->eh_frame.s.section = sectp;
2122 this->eh_frame.size = bfd_section_size (sectp);
2123 }
2124 else if (names.ranges.matches (sectp->name))
2125 {
2126 this->ranges.s.section = sectp;
2127 this->ranges.size = bfd_section_size (sectp);
2128 }
2129 else if (names.rnglists.matches (sectp->name))
2130 {
2131 this->rnglists.s.section = sectp;
2132 this->rnglists.size = bfd_section_size (sectp);
2133 }
2134 else if (names.types.matches (sectp->name))
2135 {
2136 struct dwarf2_section_info type_section;
2137
2138 memset (&type_section, 0, sizeof (type_section));
2139 type_section.s.section = sectp;
2140 type_section.size = bfd_section_size (sectp);
2141
2142 this->types.push_back (type_section);
2143 }
2144 else if (names.gdb_index.matches (sectp->name))
2145 {
2146 this->gdb_index.s.section = sectp;
2147 this->gdb_index.size = bfd_section_size (sectp);
2148 }
2149 else if (names.debug_names.matches (sectp->name))
2150 {
2151 this->debug_names.s.section = sectp;
2152 this->debug_names.size = bfd_section_size (sectp);
2153 }
2154 else if (names.debug_aranges.matches (sectp->name))
2155 {
2156 this->debug_aranges.s.section = sectp;
2157 this->debug_aranges.size = bfd_section_size (sectp);
2158 }
2159
2160 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2161 && bfd_section_vma (sectp) == 0)
2162 this->has_section_at_zero = true;
2163 }
2164
2165 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2166 SECTION_NAME. */
2167
2168 void
2169 dwarf2_get_section_info (struct objfile *objfile,
2170 enum dwarf2_section_enum sect,
2171 asection **sectp, const gdb_byte **bufp,
2172 bfd_size_type *sizep)
2173 {
2174 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2175 struct dwarf2_section_info *info;
2176
2177 /* We may see an objfile without any DWARF, in which case we just
2178 return nothing. */
2179 if (per_objfile == NULL)
2180 {
2181 *sectp = NULL;
2182 *bufp = NULL;
2183 *sizep = 0;
2184 return;
2185 }
2186 switch (sect)
2187 {
2188 case DWARF2_DEBUG_FRAME:
2189 info = &per_objfile->per_bfd->frame;
2190 break;
2191 case DWARF2_EH_FRAME:
2192 info = &per_objfile->per_bfd->eh_frame;
2193 break;
2194 default:
2195 gdb_assert_not_reached ("unexpected section");
2196 }
2197
2198 info->read (objfile);
2199
2200 *sectp = info->get_bfd_section ();
2201 *bufp = info->buffer;
2202 *sizep = info->size;
2203 }
2204
2205 \f
2206 /* DWARF quick_symbol_functions support. */
2207
2208 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2209 unique line tables, so we maintain a separate table of all .debug_line
2210 derived entries to support the sharing.
2211 All the quick functions need is the list of file names. We discard the
2212 line_header when we're done and don't need to record it here. */
2213 struct quick_file_names
2214 {
2215 /* The data used to construct the hash key. */
2216 struct stmt_list_hash hash;
2217
2218 /* The number of entries in file_names, real_names. */
2219 unsigned int num_file_names;
2220
2221 /* The file names from the line table, after being run through
2222 file_full_name. */
2223 const char **file_names;
2224
2225 /* The file names from the line table after being run through
2226 gdb_realpath. These are computed lazily. */
2227 const char **real_names;
2228 };
2229
2230 /* When using the index (and thus not using psymtabs), each CU has an
2231 object of this type. This is used to hold information needed by
2232 the various "quick" methods. */
2233 struct dwarf2_per_cu_quick_data
2234 {
2235 /* The file table. This can be NULL if there was no file table
2236 or it's currently not read in.
2237 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2238 struct quick_file_names *file_names;
2239
2240 /* A temporary mark bit used when iterating over all CUs in
2241 expand_symtabs_matching. */
2242 unsigned int mark : 1;
2243
2244 /* True if we've tried to read the file table and found there isn't one.
2245 There will be no point in trying to read it again next time. */
2246 unsigned int no_file_data : 1;
2247 };
2248
2249 /* A subclass of psymbol_functions that arranges to read the DWARF
2250 partial symbols when needed. */
2251 struct lazy_dwarf_reader : public psymbol_functions
2252 {
2253 using psymbol_functions::psymbol_functions;
2254
2255 bool can_lazily_read_symbols () override
2256 {
2257 return true;
2258 }
2259
2260 void read_partial_symbols (struct objfile *objfile) override
2261 {
2262 if (dwarf2_has_info (objfile, nullptr))
2263 dwarf2_build_psymtabs (objfile, this);
2264 }
2265 };
2266
2267 static quick_symbol_functions_up
2268 make_lazy_dwarf_reader ()
2269 {
2270 return quick_symbol_functions_up (new lazy_dwarf_reader);
2271 }
2272
2273 struct dwarf2_base_index_functions : public quick_symbol_functions
2274 {
2275 bool has_symbols (struct objfile *objfile) override;
2276
2277 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2278
2279 void forget_cached_source_info (struct objfile *objfile) override;
2280
2281 enum language lookup_global_symbol_language (struct objfile *objfile,
2282 const char *name,
2283 domain_enum domain,
2284 bool *symbol_found_p) override
2285 {
2286 *symbol_found_p = false;
2287 return language_unknown;
2288 }
2289
2290 void print_stats (struct objfile *objfile, bool print_bcache) override;
2291
2292 void expand_all_symtabs (struct objfile *objfile) override;
2293
2294 struct compunit_symtab *find_pc_sect_compunit_symtab
2295 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2296 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2297
2298 struct compunit_symtab *find_compunit_symtab_by_address
2299 (struct objfile *objfile, CORE_ADDR address) override
2300 {
2301 return nullptr;
2302 }
2303
2304 void map_symbol_filenames (struct objfile *objfile,
2305 gdb::function_view<symbol_filename_ftype> fun,
2306 bool need_fullname) override;
2307 };
2308
2309 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2310 {
2311 void dump (struct objfile *objfile) override;
2312
2313 void expand_matching_symbols
2314 (struct objfile *,
2315 const lookup_name_info &lookup_name,
2316 domain_enum domain,
2317 int global,
2318 symbol_compare_ftype *ordered_compare) override;
2319
2320 bool expand_symtabs_matching
2321 (struct objfile *objfile,
2322 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2323 const lookup_name_info *lookup_name,
2324 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2325 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2326 block_search_flags search_flags,
2327 domain_enum domain,
2328 enum search_domain kind) override;
2329 };
2330
2331 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2332 {
2333 void dump (struct objfile *objfile) override;
2334
2335 void expand_matching_symbols
2336 (struct objfile *,
2337 const lookup_name_info &lookup_name,
2338 domain_enum domain,
2339 int global,
2340 symbol_compare_ftype *ordered_compare) override;
2341
2342 bool expand_symtabs_matching
2343 (struct objfile *objfile,
2344 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2345 const lookup_name_info *lookup_name,
2346 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2347 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2348 block_search_flags search_flags,
2349 domain_enum domain,
2350 enum search_domain kind) override;
2351 };
2352
2353 static quick_symbol_functions_up
2354 make_dwarf_gdb_index ()
2355 {
2356 return quick_symbol_functions_up (new dwarf2_gdb_index);
2357 }
2358
2359 static quick_symbol_functions_up
2360 make_dwarf_debug_names ()
2361 {
2362 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2363 }
2364
2365 /* Utility hash function for a stmt_list_hash. */
2366
2367 static hashval_t
2368 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2369 {
2370 hashval_t v = 0;
2371
2372 if (stmt_list_hash->dwo_unit != NULL)
2373 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2374 v += to_underlying (stmt_list_hash->line_sect_off);
2375 return v;
2376 }
2377
2378 /* Utility equality function for a stmt_list_hash. */
2379
2380 static int
2381 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2382 const struct stmt_list_hash *rhs)
2383 {
2384 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2385 return 0;
2386 if (lhs->dwo_unit != NULL
2387 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2388 return 0;
2389
2390 return lhs->line_sect_off == rhs->line_sect_off;
2391 }
2392
2393 /* Hash function for a quick_file_names. */
2394
2395 static hashval_t
2396 hash_file_name_entry (const void *e)
2397 {
2398 const struct quick_file_names *file_data
2399 = (const struct quick_file_names *) e;
2400
2401 return hash_stmt_list_entry (&file_data->hash);
2402 }
2403
2404 /* Equality function for a quick_file_names. */
2405
2406 static int
2407 eq_file_name_entry (const void *a, const void *b)
2408 {
2409 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2410 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2411
2412 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2413 }
2414
2415 /* Delete function for a quick_file_names. */
2416
2417 static void
2418 delete_file_name_entry (void *e)
2419 {
2420 struct quick_file_names *file_data = (struct quick_file_names *) e;
2421 int i;
2422
2423 for (i = 0; i < file_data->num_file_names; ++i)
2424 {
2425 xfree ((void*) file_data->file_names[i]);
2426 if (file_data->real_names)
2427 xfree ((void*) file_data->real_names[i]);
2428 }
2429
2430 /* The space for the struct itself lives on the obstack, so we don't
2431 free it here. */
2432 }
2433
2434 /* Create a quick_file_names hash table. */
2435
2436 static htab_up
2437 create_quick_file_names_table (unsigned int nr_initial_entries)
2438 {
2439 return htab_up (htab_create_alloc (nr_initial_entries,
2440 hash_file_name_entry, eq_file_name_entry,
2441 delete_file_name_entry, xcalloc, xfree));
2442 }
2443
2444 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2445 function is unrelated to symtabs, symtab would have to be created afterwards.
2446 You should call age_cached_comp_units after processing the CU. */
2447
2448 static dwarf2_cu *
2449 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2450 bool skip_partial)
2451 {
2452 if (per_cu->is_debug_types)
2453 load_full_type_unit (per_cu, per_objfile);
2454 else
2455 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2456 skip_partial, language_minimal);
2457
2458 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2459 if (cu == nullptr)
2460 return nullptr; /* Dummy CU. */
2461
2462 dwarf2_find_base_address (cu->dies, cu);
2463
2464 return cu;
2465 }
2466
2467 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2468
2469 static void
2470 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2471 dwarf2_per_objfile *per_objfile, bool skip_partial)
2472 {
2473 /* Skip type_unit_groups, reading the type units they contain
2474 is handled elsewhere. */
2475 if (per_cu->type_unit_group_p ())
2476 return;
2477
2478 {
2479 /* The destructor of dwarf2_queue_guard frees any entries left on
2480 the queue. After this point we're guaranteed to leave this function
2481 with the dwarf queue empty. */
2482 dwarf2_queue_guard q_guard (per_objfile);
2483
2484 if (!per_objfile->symtab_set_p (per_cu))
2485 {
2486 queue_comp_unit (per_cu, per_objfile, language_minimal);
2487 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2488
2489 /* If we just loaded a CU from a DWO, and we're working with an index
2490 that may badly handle TUs, load all the TUs in that DWO as well.
2491 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2492 if (!per_cu->is_debug_types
2493 && cu != NULL
2494 && cu->dwo_unit != NULL
2495 && per_objfile->per_bfd->index_table != NULL
2496 && per_objfile->per_bfd->index_table->version <= 7
2497 /* DWP files aren't supported yet. */
2498 && get_dwp_file (per_objfile) == NULL)
2499 queue_and_load_all_dwo_tus (cu);
2500 }
2501
2502 process_queue (per_objfile);
2503 }
2504
2505 /* Age the cache, releasing compilation units that have not
2506 been used recently. */
2507 per_objfile->age_comp_units ();
2508 }
2509
2510 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2511 the per-objfile for which this symtab is instantiated.
2512
2513 Returns the resulting symbol table. */
2514
2515 static struct compunit_symtab *
2516 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2517 dwarf2_per_objfile *per_objfile,
2518 bool skip_partial)
2519 {
2520 gdb_assert (per_objfile->per_bfd->using_index);
2521
2522 if (!per_objfile->symtab_set_p (per_cu))
2523 {
2524 free_cached_comp_units freer (per_objfile);
2525 scoped_restore decrementer = increment_reading_symtab ();
2526 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2527 process_cu_includes (per_objfile);
2528 }
2529
2530 return per_objfile->get_symtab (per_cu);
2531 }
2532
2533 /* See declaration. */
2534
2535 dwarf2_per_cu_data *
2536 dwarf2_per_bfd::get_cutu (int index)
2537 {
2538 if (index >= this->all_comp_units.size ())
2539 {
2540 index -= this->all_comp_units.size ();
2541 gdb_assert (index < this->all_type_units.size ());
2542 return &this->all_type_units[index]->per_cu;
2543 }
2544
2545 return this->all_comp_units[index];
2546 }
2547
2548 /* See declaration. */
2549
2550 dwarf2_per_cu_data *
2551 dwarf2_per_bfd::get_cu (int index)
2552 {
2553 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2554
2555 return this->all_comp_units[index];
2556 }
2557
2558 /* See declaration. */
2559
2560 signatured_type *
2561 dwarf2_per_bfd::get_tu (int index)
2562 {
2563 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2564
2565 return this->all_type_units[index];
2566 }
2567
2568 /* See read.h. */
2569
2570 dwarf2_per_cu_data *
2571 dwarf2_per_bfd::allocate_per_cu ()
2572 {
2573 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2574 result->per_bfd = this;
2575 result->index = m_num_psymtabs++;
2576 return result;
2577 }
2578
2579 /* See read.h. */
2580
2581 signatured_type *
2582 dwarf2_per_bfd::allocate_signatured_type ()
2583 {
2584 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2585 result->per_cu.per_bfd = this;
2586 result->per_cu.index = m_num_psymtabs++;
2587 return result;
2588 }
2589
2590 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2591 obstack, and constructed with the specified field values. */
2592
2593 static dwarf2_per_cu_data *
2594 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2595 struct dwarf2_section_info *section,
2596 int is_dwz,
2597 sect_offset sect_off, ULONGEST length)
2598 {
2599 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2600 the_cu->sect_off = sect_off;
2601 the_cu->length = length;
2602 the_cu->section = section;
2603 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2604 struct dwarf2_per_cu_quick_data);
2605 the_cu->is_dwz = is_dwz;
2606 return the_cu;
2607 }
2608
2609 /* A helper for create_cus_from_index that handles a given list of
2610 CUs. */
2611
2612 static void
2613 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2614 const gdb_byte *cu_list, offset_type n_elements,
2615 struct dwarf2_section_info *section,
2616 int is_dwz)
2617 {
2618 for (offset_type i = 0; i < n_elements; i += 2)
2619 {
2620 gdb_static_assert (sizeof (ULONGEST) >= 8);
2621
2622 sect_offset sect_off
2623 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2624 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2625 cu_list += 2 * 8;
2626
2627 dwarf2_per_cu_data *per_cu
2628 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2629 length);
2630 per_bfd->all_comp_units.push_back (per_cu);
2631 }
2632 }
2633
2634 /* Read the CU list from the mapped index, and use it to create all
2635 the CU objects for PER_BFD. */
2636
2637 static void
2638 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2639 const gdb_byte *cu_list, offset_type cu_list_elements,
2640 const gdb_byte *dwz_list, offset_type dwz_elements)
2641 {
2642 gdb_assert (per_bfd->all_comp_units.empty ());
2643 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2644
2645 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2646 &per_bfd->info, 0);
2647
2648 if (dwz_elements == 0)
2649 return;
2650
2651 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2652 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2653 &dwz->info, 1);
2654 }
2655
2656 /* Create the signatured type hash table from the index. */
2657
2658 static void
2659 create_signatured_type_table_from_index
2660 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2661 const gdb_byte *bytes, offset_type elements)
2662 {
2663 gdb_assert (per_bfd->all_type_units.empty ());
2664 per_bfd->all_type_units.reserve (elements / 3);
2665
2666 htab_up sig_types_hash = allocate_signatured_type_table ();
2667
2668 for (offset_type i = 0; i < elements; i += 3)
2669 {
2670 struct signatured_type *sig_type;
2671 ULONGEST signature;
2672 void **slot;
2673 cu_offset type_offset_in_tu;
2674
2675 gdb_static_assert (sizeof (ULONGEST) >= 8);
2676 sect_offset sect_off
2677 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2678 type_offset_in_tu
2679 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2680 BFD_ENDIAN_LITTLE);
2681 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2682 bytes += 3 * 8;
2683
2684 sig_type = per_bfd->allocate_signatured_type ();
2685 sig_type->signature = signature;
2686 sig_type->type_offset_in_tu = type_offset_in_tu;
2687 sig_type->per_cu.is_debug_types = 1;
2688 sig_type->per_cu.section = section;
2689 sig_type->per_cu.sect_off = sect_off;
2690 sig_type->per_cu.v.quick
2691 = OBSTACK_ZALLOC (&per_bfd->obstack,
2692 struct dwarf2_per_cu_quick_data);
2693
2694 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2695 *slot = sig_type;
2696
2697 per_bfd->all_type_units.push_back (sig_type);
2698 }
2699
2700 per_bfd->signatured_types = std::move (sig_types_hash);
2701 }
2702
2703 /* Create the signatured type hash table from .debug_names. */
2704
2705 static void
2706 create_signatured_type_table_from_debug_names
2707 (dwarf2_per_objfile *per_objfile,
2708 const mapped_debug_names &map,
2709 struct dwarf2_section_info *section,
2710 struct dwarf2_section_info *abbrev_section)
2711 {
2712 struct objfile *objfile = per_objfile->objfile;
2713
2714 section->read (objfile);
2715 abbrev_section->read (objfile);
2716
2717 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2718 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2719
2720 htab_up sig_types_hash = allocate_signatured_type_table ();
2721
2722 for (uint32_t i = 0; i < map.tu_count; ++i)
2723 {
2724 struct signatured_type *sig_type;
2725 void **slot;
2726
2727 sect_offset sect_off
2728 = (sect_offset) (extract_unsigned_integer
2729 (map.tu_table_reordered + i * map.offset_size,
2730 map.offset_size,
2731 map.dwarf5_byte_order));
2732
2733 comp_unit_head cu_header;
2734 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2735 abbrev_section,
2736 section->buffer + to_underlying (sect_off),
2737 rcuh_kind::TYPE);
2738
2739 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2740 sig_type->signature = cu_header.signature;
2741 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2742 sig_type->per_cu.is_debug_types = 1;
2743 sig_type->per_cu.section = section;
2744 sig_type->per_cu.sect_off = sect_off;
2745 sig_type->per_cu.v.quick
2746 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2747 struct dwarf2_per_cu_quick_data);
2748
2749 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2750 *slot = sig_type;
2751
2752 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2753 }
2754
2755 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2756 }
2757
2758 /* Read the address map data from the mapped index, and use it to
2759 populate the psymtabs_addrmap. */
2760
2761 static void
2762 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2763 struct mapped_index *index)
2764 {
2765 struct objfile *objfile = per_objfile->objfile;
2766 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2767 struct gdbarch *gdbarch = objfile->arch ();
2768 const gdb_byte *iter, *end;
2769 struct addrmap *mutable_map;
2770 CORE_ADDR baseaddr;
2771
2772 auto_obstack temp_obstack;
2773
2774 mutable_map = addrmap_create_mutable (&temp_obstack);
2775
2776 iter = index->address_table.data ();
2777 end = iter + index->address_table.size ();
2778
2779 baseaddr = objfile->text_section_offset ();
2780
2781 while (iter < end)
2782 {
2783 ULONGEST hi, lo, cu_index;
2784 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2785 iter += 8;
2786 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2787 iter += 8;
2788 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2789 iter += 4;
2790
2791 if (lo > hi)
2792 {
2793 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2794 hex_string (lo), hex_string (hi));
2795 continue;
2796 }
2797
2798 if (cu_index >= per_bfd->all_comp_units.size ())
2799 {
2800 complaint (_(".gdb_index address table has invalid CU number %u"),
2801 (unsigned) cu_index);
2802 continue;
2803 }
2804
2805 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2806 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2807 addrmap_set_empty (mutable_map, lo, hi - 1,
2808 per_bfd->get_cu (cu_index));
2809 }
2810
2811 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2812 &per_bfd->obstack);
2813 }
2814
2815 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2816 populate the psymtabs_addrmap. */
2817
2818 static void
2819 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2820 struct dwarf2_section_info *section)
2821 {
2822 struct objfile *objfile = per_objfile->objfile;
2823 bfd *abfd = objfile->obfd;
2824 struct gdbarch *gdbarch = objfile->arch ();
2825 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2826 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2827
2828 auto_obstack temp_obstack;
2829 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2830
2831 std::unordered_map<sect_offset,
2832 dwarf2_per_cu_data *,
2833 gdb::hash_enum<sect_offset>>
2834 debug_info_offset_to_per_cu;
2835 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2836 {
2837 const auto insertpair
2838 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2839 if (!insertpair.second)
2840 {
2841 warning (_("Section .debug_aranges in %s has duplicate "
2842 "debug_info_offset %s, ignoring .debug_aranges."),
2843 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2844 return;
2845 }
2846 }
2847
2848 section->read (objfile);
2849
2850 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2851
2852 const gdb_byte *addr = section->buffer;
2853
2854 while (addr < section->buffer + section->size)
2855 {
2856 const gdb_byte *const entry_addr = addr;
2857 unsigned int bytes_read;
2858
2859 const LONGEST entry_length = read_initial_length (abfd, addr,
2860 &bytes_read);
2861 addr += bytes_read;
2862
2863 const gdb_byte *const entry_end = addr + entry_length;
2864 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2865 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2866 if (addr + entry_length > section->buffer + section->size)
2867 {
2868 warning (_("Section .debug_aranges in %s entry at offset %s "
2869 "length %s exceeds section length %s, "
2870 "ignoring .debug_aranges."),
2871 objfile_name (objfile),
2872 plongest (entry_addr - section->buffer),
2873 plongest (bytes_read + entry_length),
2874 pulongest (section->size));
2875 return;
2876 }
2877
2878 /* The version number. */
2879 const uint16_t version = read_2_bytes (abfd, addr);
2880 addr += 2;
2881 if (version != 2)
2882 {
2883 warning (_("Section .debug_aranges in %s entry at offset %s "
2884 "has unsupported version %d, ignoring .debug_aranges."),
2885 objfile_name (objfile),
2886 plongest (entry_addr - section->buffer), version);
2887 return;
2888 }
2889
2890 const uint64_t debug_info_offset
2891 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2892 addr += offset_size;
2893 const auto per_cu_it
2894 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2895 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2896 {
2897 warning (_("Section .debug_aranges in %s entry at offset %s "
2898 "debug_info_offset %s does not exists, "
2899 "ignoring .debug_aranges."),
2900 objfile_name (objfile),
2901 plongest (entry_addr - section->buffer),
2902 pulongest (debug_info_offset));
2903 return;
2904 }
2905 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2906
2907 const uint8_t address_size = *addr++;
2908 if (address_size < 1 || address_size > 8)
2909 {
2910 warning (_("Section .debug_aranges in %s entry at offset %s "
2911 "address_size %u is invalid, ignoring .debug_aranges."),
2912 objfile_name (objfile),
2913 plongest (entry_addr - section->buffer), address_size);
2914 return;
2915 }
2916
2917 const uint8_t segment_selector_size = *addr++;
2918 if (segment_selector_size != 0)
2919 {
2920 warning (_("Section .debug_aranges in %s entry at offset %s "
2921 "segment_selector_size %u is not supported, "
2922 "ignoring .debug_aranges."),
2923 objfile_name (objfile),
2924 plongest (entry_addr - section->buffer),
2925 segment_selector_size);
2926 return;
2927 }
2928
2929 /* Must pad to an alignment boundary that is twice the address
2930 size. It is undocumented by the DWARF standard but GCC does
2931 use it. */
2932 for (size_t padding = ((-(addr - section->buffer))
2933 & (2 * address_size - 1));
2934 padding > 0; padding--)
2935 if (*addr++ != 0)
2936 {
2937 warning (_("Section .debug_aranges in %s entry at offset %s "
2938 "padding is not zero, ignoring .debug_aranges."),
2939 objfile_name (objfile),
2940 plongest (entry_addr - section->buffer));
2941 return;
2942 }
2943
2944 for (;;)
2945 {
2946 if (addr + 2 * address_size > entry_end)
2947 {
2948 warning (_("Section .debug_aranges in %s entry at offset %s "
2949 "address list is not properly terminated, "
2950 "ignoring .debug_aranges."),
2951 objfile_name (objfile),
2952 plongest (entry_addr - section->buffer));
2953 return;
2954 }
2955 ULONGEST start = extract_unsigned_integer (addr, address_size,
2956 dwarf5_byte_order);
2957 addr += address_size;
2958 ULONGEST length = extract_unsigned_integer (addr, address_size,
2959 dwarf5_byte_order);
2960 addr += address_size;
2961 if (start == 0 && length == 0)
2962 break;
2963 if (start == 0 && !per_bfd->has_section_at_zero)
2964 {
2965 /* Symbol was eliminated due to a COMDAT group. */
2966 continue;
2967 }
2968 ULONGEST end = start + length;
2969 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2970 - baseaddr);
2971 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2972 - baseaddr);
2973 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2974 }
2975 }
2976
2977 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2978 &per_bfd->obstack);
2979 }
2980
2981 /* A helper function that reads the .gdb_index from BUFFER and fills
2982 in MAP. FILENAME is the name of the file containing the data;
2983 it is used for error reporting. DEPRECATED_OK is true if it is
2984 ok to use deprecated sections.
2985
2986 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2987 out parameters that are filled in with information about the CU and
2988 TU lists in the section.
2989
2990 Returns true if all went well, false otherwise. */
2991
2992 static bool
2993 read_gdb_index_from_buffer (const char *filename,
2994 bool deprecated_ok,
2995 gdb::array_view<const gdb_byte> buffer,
2996 struct mapped_index *map,
2997 const gdb_byte **cu_list,
2998 offset_type *cu_list_elements,
2999 const gdb_byte **types_list,
3000 offset_type *types_list_elements)
3001 {
3002 const gdb_byte *addr = &buffer[0];
3003 offset_view metadata (buffer);
3004
3005 /* Version check. */
3006 offset_type version = metadata[0];
3007 /* Versions earlier than 3 emitted every copy of a psymbol. This
3008 causes the index to behave very poorly for certain requests. Version 3
3009 contained incomplete addrmap. So, it seems better to just ignore such
3010 indices. */
3011 if (version < 4)
3012 {
3013 static int warning_printed = 0;
3014 if (!warning_printed)
3015 {
3016 warning (_("Skipping obsolete .gdb_index section in %s."),
3017 filename);
3018 warning_printed = 1;
3019 }
3020 return 0;
3021 }
3022 /* Index version 4 uses a different hash function than index version
3023 5 and later.
3024
3025 Versions earlier than 6 did not emit psymbols for inlined
3026 functions. Using these files will cause GDB not to be able to
3027 set breakpoints on inlined functions by name, so we ignore these
3028 indices unless the user has done
3029 "set use-deprecated-index-sections on". */
3030 if (version < 6 && !deprecated_ok)
3031 {
3032 static int warning_printed = 0;
3033 if (!warning_printed)
3034 {
3035 warning (_("\
3036 Skipping deprecated .gdb_index section in %s.\n\
3037 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3038 to use the section anyway."),
3039 filename);
3040 warning_printed = 1;
3041 }
3042 return 0;
3043 }
3044 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3045 of the TU (for symbols coming from TUs),
3046 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3047 Plus gold-generated indices can have duplicate entries for global symbols,
3048 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3049 These are just performance bugs, and we can't distinguish gdb-generated
3050 indices from gold-generated ones, so issue no warning here. */
3051
3052 /* Indexes with higher version than the one supported by GDB may be no
3053 longer backward compatible. */
3054 if (version > 8)
3055 return 0;
3056
3057 map->version = version;
3058
3059 int i = 1;
3060 *cu_list = addr + metadata[i];
3061 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3062 ++i;
3063
3064 *types_list = addr + metadata[i];
3065 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3066 ++i;
3067
3068 const gdb_byte *address_table = addr + metadata[i];
3069 const gdb_byte *address_table_end = addr + metadata[i + 1];
3070 map->address_table
3071 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3072 ++i;
3073
3074 const gdb_byte *symbol_table = addr + metadata[i];
3075 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
3076 map->symbol_table
3077 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3078 symbol_table_end));
3079
3080 ++i;
3081 map->constant_pool = buffer.slice (metadata[i]);
3082
3083 return 1;
3084 }
3085
3086 /* Callback types for dwarf2_read_gdb_index. */
3087
3088 typedef gdb::function_view
3089 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3090 get_gdb_index_contents_ftype;
3091 typedef gdb::function_view
3092 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3093 get_gdb_index_contents_dwz_ftype;
3094
3095 /* Read .gdb_index. If everything went ok, initialize the "quick"
3096 elements of all the CUs and return 1. Otherwise, return 0. */
3097
3098 static int
3099 dwarf2_read_gdb_index
3100 (dwarf2_per_objfile *per_objfile,
3101 get_gdb_index_contents_ftype get_gdb_index_contents,
3102 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3103 {
3104 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3105 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3106 struct dwz_file *dwz;
3107 struct objfile *objfile = per_objfile->objfile;
3108 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3109
3110 gdb::array_view<const gdb_byte> main_index_contents
3111 = get_gdb_index_contents (objfile, per_bfd);
3112
3113 if (main_index_contents.empty ())
3114 return 0;
3115
3116 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3117 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3118 use_deprecated_index_sections,
3119 main_index_contents, map.get (), &cu_list,
3120 &cu_list_elements, &types_list,
3121 &types_list_elements))
3122 return 0;
3123
3124 /* Don't use the index if it's empty. */
3125 if (map->symbol_table.empty ())
3126 return 0;
3127
3128 /* If there is a .dwz file, read it so we can get its CU list as
3129 well. */
3130 dwz = dwarf2_get_dwz_file (per_bfd);
3131 if (dwz != NULL)
3132 {
3133 struct mapped_index dwz_map;
3134 const gdb_byte *dwz_types_ignore;
3135 offset_type dwz_types_elements_ignore;
3136
3137 gdb::array_view<const gdb_byte> dwz_index_content
3138 = get_gdb_index_contents_dwz (objfile, dwz);
3139
3140 if (dwz_index_content.empty ())
3141 return 0;
3142
3143 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3144 1, dwz_index_content, &dwz_map,
3145 &dwz_list, &dwz_list_elements,
3146 &dwz_types_ignore,
3147 &dwz_types_elements_ignore))
3148 {
3149 warning (_("could not read '.gdb_index' section from %s; skipping"),
3150 bfd_get_filename (dwz->dwz_bfd.get ()));
3151 return 0;
3152 }
3153 }
3154
3155 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3156 dwz_list_elements);
3157
3158 if (types_list_elements)
3159 {
3160 /* We can only handle a single .debug_types when we have an
3161 index. */
3162 if (per_bfd->types.size () != 1)
3163 return 0;
3164
3165 dwarf2_section_info *section = &per_bfd->types[0];
3166
3167 create_signatured_type_table_from_index (per_bfd, section, types_list,
3168 types_list_elements);
3169 }
3170
3171 create_addrmap_from_index (per_objfile, map.get ());
3172
3173 per_bfd->index_table = std::move (map);
3174 per_bfd->using_index = 1;
3175 per_bfd->quick_file_names_table =
3176 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3177
3178 return 1;
3179 }
3180
3181 /* die_reader_func for dw2_get_file_names. */
3182
3183 static void
3184 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3185 struct die_info *comp_unit_die)
3186 {
3187 struct dwarf2_cu *cu = reader->cu;
3188 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3189 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3190 struct dwarf2_per_cu_data *lh_cu;
3191 struct attribute *attr;
3192 void **slot;
3193 struct quick_file_names *qfn;
3194
3195 gdb_assert (! this_cu->is_debug_types);
3196
3197 /* Our callers never want to match partial units -- instead they
3198 will match the enclosing full CU. */
3199 if (comp_unit_die->tag == DW_TAG_partial_unit)
3200 {
3201 this_cu->v.quick->no_file_data = 1;
3202 return;
3203 }
3204
3205 lh_cu = this_cu;
3206 slot = NULL;
3207
3208 line_header_up lh;
3209 sect_offset line_offset {};
3210
3211 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3212 if (attr != nullptr && attr->form_is_unsigned ())
3213 {
3214 struct quick_file_names find_entry;
3215
3216 line_offset = (sect_offset) attr->as_unsigned ();
3217
3218 /* We may have already read in this line header (TU line header sharing).
3219 If we have we're done. */
3220 find_entry.hash.dwo_unit = cu->dwo_unit;
3221 find_entry.hash.line_sect_off = line_offset;
3222 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3223 &find_entry, INSERT);
3224 if (*slot != NULL)
3225 {
3226 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3227 return;
3228 }
3229
3230 lh = dwarf_decode_line_header (line_offset, cu);
3231 }
3232 if (lh == NULL)
3233 {
3234 lh_cu->v.quick->no_file_data = 1;
3235 return;
3236 }
3237
3238 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3239 qfn->hash.dwo_unit = cu->dwo_unit;
3240 qfn->hash.line_sect_off = line_offset;
3241 gdb_assert (slot != NULL);
3242 *slot = qfn;
3243
3244 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3245
3246 int offset = 0;
3247 if (strcmp (fnd.name, "<unknown>") != 0)
3248 ++offset;
3249
3250 qfn->num_file_names = offset + lh->file_names_size ();
3251 qfn->file_names =
3252 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3253 qfn->num_file_names);
3254 if (offset != 0)
3255 qfn->file_names[0] = xstrdup (fnd.name);
3256 for (int i = 0; i < lh->file_names_size (); ++i)
3257 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3258 fnd.comp_dir).release ();
3259 qfn->real_names = NULL;
3260
3261 lh_cu->v.quick->file_names = qfn;
3262 }
3263
3264 /* A helper for the "quick" functions which attempts to read the line
3265 table for THIS_CU. */
3266
3267 static struct quick_file_names *
3268 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3269 dwarf2_per_objfile *per_objfile)
3270 {
3271 /* This should never be called for TUs. */
3272 gdb_assert (! this_cu->is_debug_types);
3273 /* Nor type unit groups. */
3274 gdb_assert (! this_cu->type_unit_group_p ());
3275
3276 if (this_cu->v.quick->file_names != NULL)
3277 return this_cu->v.quick->file_names;
3278 /* If we know there is no line data, no point in looking again. */
3279 if (this_cu->v.quick->no_file_data)
3280 return NULL;
3281
3282 cutu_reader reader (this_cu, per_objfile);
3283 if (!reader.dummy_p)
3284 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3285
3286 if (this_cu->v.quick->no_file_data)
3287 return NULL;
3288 return this_cu->v.quick->file_names;
3289 }
3290
3291 /* A helper for the "quick" functions which computes and caches the
3292 real path for a given file name from the line table. */
3293
3294 static const char *
3295 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3296 struct quick_file_names *qfn, int index)
3297 {
3298 if (qfn->real_names == NULL)
3299 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3300 qfn->num_file_names, const char *);
3301
3302 if (qfn->real_names[index] == NULL)
3303 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3304
3305 return qfn->real_names[index];
3306 }
3307
3308 struct symtab *
3309 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3310 {
3311 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3312 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3313 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3314
3315 if (cust == NULL)
3316 return NULL;
3317
3318 return compunit_primary_filetab (cust);
3319 }
3320
3321 /* Traversal function for dw2_forget_cached_source_info. */
3322
3323 static int
3324 dw2_free_cached_file_names (void **slot, void *info)
3325 {
3326 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3327
3328 if (file_data->real_names)
3329 {
3330 int i;
3331
3332 for (i = 0; i < file_data->num_file_names; ++i)
3333 {
3334 xfree ((void*) file_data->real_names[i]);
3335 file_data->real_names[i] = NULL;
3336 }
3337 }
3338
3339 return 1;
3340 }
3341
3342 void
3343 dwarf2_base_index_functions::forget_cached_source_info
3344 (struct objfile *objfile)
3345 {
3346 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3347
3348 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3349 dw2_free_cached_file_names, NULL);
3350 }
3351
3352 /* Struct used to manage iterating over all CUs looking for a symbol. */
3353
3354 struct dw2_symtab_iterator
3355 {
3356 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3357 dwarf2_per_objfile *per_objfile;
3358 /* If set, only look for symbols that match that block. Valid values are
3359 GLOBAL_BLOCK and STATIC_BLOCK. */
3360 gdb::optional<block_enum> block_index;
3361 /* The kind of symbol we're looking for. */
3362 domain_enum domain;
3363 /* The list of CUs from the index entry of the symbol,
3364 or NULL if not found. */
3365 offset_view vec;
3366 /* The next element in VEC to look at. */
3367 int next;
3368 /* The number of elements in VEC, or zero if there is no match. */
3369 int length;
3370 /* Have we seen a global version of the symbol?
3371 If so we can ignore all further global instances.
3372 This is to work around gold/15646, inefficient gold-generated
3373 indices. */
3374 int global_seen;
3375 };
3376
3377 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3378
3379 static void
3380 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3381 dwarf2_per_objfile *per_objfile,
3382 gdb::optional<block_enum> block_index,
3383 domain_enum domain, offset_type namei)
3384 {
3385 iter->per_objfile = per_objfile;
3386 iter->block_index = block_index;
3387 iter->domain = domain;
3388 iter->next = 0;
3389 iter->global_seen = 0;
3390 iter->vec = {};
3391 iter->length = 0;
3392
3393 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3394 /* index is NULL if OBJF_READNOW. */
3395 if (index == NULL)
3396 return;
3397
3398 gdb_assert (!index->symbol_name_slot_invalid (namei));
3399 offset_type vec_idx = index->symbol_vec_index (namei);
3400
3401 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3402 iter->length = iter->vec[0];
3403 }
3404
3405 /* Return the next matching CU or NULL if there are no more. */
3406
3407 static struct dwarf2_per_cu_data *
3408 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3409 {
3410 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3411
3412 for ( ; iter->next < iter->length; ++iter->next)
3413 {
3414 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3415 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3416 gdb_index_symbol_kind symbol_kind =
3417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3418 /* Only check the symbol attributes if they're present.
3419 Indices prior to version 7 don't record them,
3420 and indices >= 7 may elide them for certain symbols
3421 (gold does this). */
3422 int attrs_valid =
3423 (per_objfile->per_bfd->index_table->version >= 7
3424 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3425
3426 /* Don't crash on bad data. */
3427 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3428 + per_objfile->per_bfd->all_type_units.size ()))
3429 {
3430 complaint (_(".gdb_index entry has bad CU index"
3431 " [in module %s]"), objfile_name (per_objfile->objfile));
3432 continue;
3433 }
3434
3435 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3436
3437 /* Skip if already read in. */
3438 if (per_objfile->symtab_set_p (per_cu))
3439 continue;
3440
3441 /* Check static vs global. */
3442 if (attrs_valid)
3443 {
3444 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3445
3446 if (iter->block_index.has_value ())
3447 {
3448 bool want_static = *iter->block_index == STATIC_BLOCK;
3449
3450 if (is_static != want_static)
3451 continue;
3452 }
3453
3454 /* Work around gold/15646. */
3455 if (!is_static
3456 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3457 {
3458 if (iter->global_seen)
3459 continue;
3460
3461 iter->global_seen = 1;
3462 }
3463 }
3464
3465 /* Only check the symbol's kind if it has one. */
3466 if (attrs_valid)
3467 {
3468 switch (iter->domain)
3469 {
3470 case VAR_DOMAIN:
3471 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3472 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3473 /* Some types are also in VAR_DOMAIN. */
3474 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3475 continue;
3476 break;
3477 case STRUCT_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3479 continue;
3480 break;
3481 case LABEL_DOMAIN:
3482 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3483 continue;
3484 break;
3485 case MODULE_DOMAIN:
3486 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3487 continue;
3488 break;
3489 default:
3490 break;
3491 }
3492 }
3493
3494 ++iter->next;
3495 return per_cu;
3496 }
3497
3498 return NULL;
3499 }
3500
3501 void
3502 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3503 bool print_bcache)
3504 {
3505 if (print_bcache)
3506 return;
3507
3508 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3509 int total = (per_objfile->per_bfd->all_comp_units.size ()
3510 + per_objfile->per_bfd->all_type_units.size ());
3511 int count = 0;
3512
3513 for (int i = 0; i < total; ++i)
3514 {
3515 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3516
3517 if (!per_objfile->symtab_set_p (per_cu))
3518 ++count;
3519 }
3520 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3521 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3522 }
3523
3524 /* This dumps minimal information about the index.
3525 It is called via "mt print objfiles".
3526 One use is to verify .gdb_index has been loaded by the
3527 gdb.dwarf2/gdb-index.exp testcase. */
3528
3529 void
3530 dwarf2_gdb_index::dump (struct objfile *objfile)
3531 {
3532 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3533
3534 gdb_assert (per_objfile->per_bfd->using_index);
3535 printf_filtered (".gdb_index:");
3536 if (per_objfile->per_bfd->index_table != NULL)
3537 {
3538 printf_filtered (" version %d\n",
3539 per_objfile->per_bfd->index_table->version);
3540 }
3541 else
3542 printf_filtered (" faked for \"readnow\"\n");
3543 printf_filtered ("\n");
3544 }
3545
3546 void
3547 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3548 {
3549 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3550 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3551 + per_objfile->per_bfd->all_type_units.size ());
3552
3553 for (int i = 0; i < total_units; ++i)
3554 {
3555 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3556
3557 /* We don't want to directly expand a partial CU, because if we
3558 read it with the wrong language, then assertion failures can
3559 be triggered later on. See PR symtab/23010. So, tell
3560 dw2_instantiate_symtab to skip partial CUs -- any important
3561 partial CU will be read via DW_TAG_imported_unit anyway. */
3562 dw2_instantiate_symtab (per_cu, per_objfile, true);
3563 }
3564 }
3565
3566 static bool
3567 dw2_expand_symtabs_matching_symbol
3568 (mapped_index_base &index,
3569 const lookup_name_info &lookup_name_in,
3570 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3571 gdb::function_view<bool (offset_type)> match_callback,
3572 dwarf2_per_objfile *per_objfile);
3573
3574 static bool
3575 dw2_expand_symtabs_matching_one
3576 (dwarf2_per_cu_data *per_cu,
3577 dwarf2_per_objfile *per_objfile,
3578 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3579 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3580
3581 void
3582 dwarf2_gdb_index::expand_matching_symbols
3583 (struct objfile *objfile,
3584 const lookup_name_info &name, domain_enum domain,
3585 int global,
3586 symbol_compare_ftype *ordered_compare)
3587 {
3588 /* Used for Ada. */
3589 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3590
3591 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3592
3593 if (per_objfile->per_bfd->index_table != nullptr)
3594 {
3595 mapped_index &index = *per_objfile->per_bfd->index_table;
3596
3597 const char *match_name = name.ada ().lookup_name ().c_str ();
3598 auto matcher = [&] (const char *symname)
3599 {
3600 if (ordered_compare == nullptr)
3601 return true;
3602 return ordered_compare (symname, match_name) == 0;
3603 };
3604
3605 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3606 [&] (offset_type namei)
3607 {
3608 struct dw2_symtab_iterator iter;
3609 struct dwarf2_per_cu_data *per_cu;
3610
3611 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3612 namei);
3613 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3614 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3615 nullptr);
3616 return true;
3617 }, per_objfile);
3618 }
3619 else
3620 {
3621 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3622 proceed assuming all symtabs have been read in. */
3623 }
3624 }
3625
3626 /* Starting from a search name, return the string that finds the upper
3627 bound of all strings that start with SEARCH_NAME in a sorted name
3628 list. Returns the empty string to indicate that the upper bound is
3629 the end of the list. */
3630
3631 static std::string
3632 make_sort_after_prefix_name (const char *search_name)
3633 {
3634 /* When looking to complete "func", we find the upper bound of all
3635 symbols that start with "func" by looking for where we'd insert
3636 the closest string that would follow "func" in lexicographical
3637 order. Usually, that's "func"-with-last-character-incremented,
3638 i.e. "fund". Mind non-ASCII characters, though. Usually those
3639 will be UTF-8 multi-byte sequences, but we can't be certain.
3640 Especially mind the 0xff character, which is a valid character in
3641 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3642 rule out compilers allowing it in identifiers. Note that
3643 conveniently, strcmp/strcasecmp are specified to compare
3644 characters interpreted as unsigned char. So what we do is treat
3645 the whole string as a base 256 number composed of a sequence of
3646 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3647 to 0, and carries 1 to the following more-significant position.
3648 If the very first character in SEARCH_NAME ends up incremented
3649 and carries/overflows, then the upper bound is the end of the
3650 list. The string after the empty string is also the empty
3651 string.
3652
3653 Some examples of this operation:
3654
3655 SEARCH_NAME => "+1" RESULT
3656
3657 "abc" => "abd"
3658 "ab\xff" => "ac"
3659 "\xff" "a" "\xff" => "\xff" "b"
3660 "\xff" => ""
3661 "\xff\xff" => ""
3662 "" => ""
3663
3664 Then, with these symbols for example:
3665
3666 func
3667 func1
3668 fund
3669
3670 completing "func" looks for symbols between "func" and
3671 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3672 which finds "func" and "func1", but not "fund".
3673
3674 And with:
3675
3676 funcÿ (Latin1 'ÿ' [0xff])
3677 funcÿ1
3678 fund
3679
3680 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3681 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3682
3683 And with:
3684
3685 ÿÿ (Latin1 'ÿ' [0xff])
3686 ÿÿ1
3687
3688 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3689 the end of the list.
3690 */
3691 std::string after = search_name;
3692 while (!after.empty () && (unsigned char) after.back () == 0xff)
3693 after.pop_back ();
3694 if (!after.empty ())
3695 after.back () = (unsigned char) after.back () + 1;
3696 return after;
3697 }
3698
3699 /* See declaration. */
3700
3701 std::pair<std::vector<name_component>::const_iterator,
3702 std::vector<name_component>::const_iterator>
3703 mapped_index_base::find_name_components_bounds
3704 (const lookup_name_info &lookup_name_without_params, language lang,
3705 dwarf2_per_objfile *per_objfile) const
3706 {
3707 auto *name_cmp
3708 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3709
3710 const char *lang_name
3711 = lookup_name_without_params.language_lookup_name (lang);
3712
3713 /* Comparison function object for lower_bound that matches against a
3714 given symbol name. */
3715 auto lookup_compare_lower = [&] (const name_component &elem,
3716 const char *name)
3717 {
3718 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3719 const char *elem_name = elem_qualified + elem.name_offset;
3720 return name_cmp (elem_name, name) < 0;
3721 };
3722
3723 /* Comparison function object for upper_bound that matches against a
3724 given symbol name. */
3725 auto lookup_compare_upper = [&] (const char *name,
3726 const name_component &elem)
3727 {
3728 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3729 const char *elem_name = elem_qualified + elem.name_offset;
3730 return name_cmp (name, elem_name) < 0;
3731 };
3732
3733 auto begin = this->name_components.begin ();
3734 auto end = this->name_components.end ();
3735
3736 /* Find the lower bound. */
3737 auto lower = [&] ()
3738 {
3739 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3740 return begin;
3741 else
3742 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3743 } ();
3744
3745 /* Find the upper bound. */
3746 auto upper = [&] ()
3747 {
3748 if (lookup_name_without_params.completion_mode ())
3749 {
3750 /* In completion mode, we want UPPER to point past all
3751 symbols names that have the same prefix. I.e., with
3752 these symbols, and completing "func":
3753
3754 function << lower bound
3755 function1
3756 other_function << upper bound
3757
3758 We find the upper bound by looking for the insertion
3759 point of "func"-with-last-character-incremented,
3760 i.e. "fund". */
3761 std::string after = make_sort_after_prefix_name (lang_name);
3762 if (after.empty ())
3763 return end;
3764 return std::lower_bound (lower, end, after.c_str (),
3765 lookup_compare_lower);
3766 }
3767 else
3768 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3769 } ();
3770
3771 return {lower, upper};
3772 }
3773
3774 /* See declaration. */
3775
3776 void
3777 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3778 {
3779 if (!this->name_components.empty ())
3780 return;
3781
3782 this->name_components_casing = case_sensitivity;
3783 auto *name_cmp
3784 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3785
3786 /* The code below only knows how to break apart components of C++
3787 symbol names (and other languages that use '::' as
3788 namespace/module separator) and Ada symbol names. */
3789 auto count = this->symbol_name_count ();
3790 for (offset_type idx = 0; idx < count; idx++)
3791 {
3792 if (this->symbol_name_slot_invalid (idx))
3793 continue;
3794
3795 const char *name = this->symbol_name_at (idx, per_objfile);
3796
3797 /* Add each name component to the name component table. */
3798 unsigned int previous_len = 0;
3799
3800 if (strstr (name, "::") != nullptr)
3801 {
3802 for (unsigned int current_len = cp_find_first_component (name);
3803 name[current_len] != '\0';
3804 current_len += cp_find_first_component (name + current_len))
3805 {
3806 gdb_assert (name[current_len] == ':');
3807 this->name_components.push_back ({previous_len, idx});
3808 /* Skip the '::'. */
3809 current_len += 2;
3810 previous_len = current_len;
3811 }
3812 }
3813 else
3814 {
3815 /* Handle the Ada encoded (aka mangled) form here. */
3816 for (const char *iter = strstr (name, "__");
3817 iter != nullptr;
3818 iter = strstr (iter, "__"))
3819 {
3820 this->name_components.push_back ({previous_len, idx});
3821 iter += 2;
3822 previous_len = iter - name;
3823 }
3824 }
3825
3826 this->name_components.push_back ({previous_len, idx});
3827 }
3828
3829 /* Sort name_components elements by name. */
3830 auto name_comp_compare = [&] (const name_component &left,
3831 const name_component &right)
3832 {
3833 const char *left_qualified
3834 = this->symbol_name_at (left.idx, per_objfile);
3835 const char *right_qualified
3836 = this->symbol_name_at (right.idx, per_objfile);
3837
3838 const char *left_name = left_qualified + left.name_offset;
3839 const char *right_name = right_qualified + right.name_offset;
3840
3841 return name_cmp (left_name, right_name) < 0;
3842 };
3843
3844 std::sort (this->name_components.begin (),
3845 this->name_components.end (),
3846 name_comp_compare);
3847 }
3848
3849 /* Helper for dw2_expand_symtabs_matching that works with a
3850 mapped_index_base instead of the containing objfile. This is split
3851 to a separate function in order to be able to unit test the
3852 name_components matching using a mock mapped_index_base. For each
3853 symbol name that matches, calls MATCH_CALLBACK, passing it the
3854 symbol's index in the mapped_index_base symbol table. */
3855
3856 static bool
3857 dw2_expand_symtabs_matching_symbol
3858 (mapped_index_base &index,
3859 const lookup_name_info &lookup_name_in,
3860 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3861 gdb::function_view<bool (offset_type)> match_callback,
3862 dwarf2_per_objfile *per_objfile)
3863 {
3864 lookup_name_info lookup_name_without_params
3865 = lookup_name_in.make_ignore_params ();
3866
3867 /* Build the symbol name component sorted vector, if we haven't
3868 yet. */
3869 index.build_name_components (per_objfile);
3870
3871 /* The same symbol may appear more than once in the range though.
3872 E.g., if we're looking for symbols that complete "w", and we have
3873 a symbol named "w1::w2", we'll find the two name components for
3874 that same symbol in the range. To be sure we only call the
3875 callback once per symbol, we first collect the symbol name
3876 indexes that matched in a temporary vector and ignore
3877 duplicates. */
3878 std::vector<offset_type> matches;
3879
3880 struct name_and_matcher
3881 {
3882 symbol_name_matcher_ftype *matcher;
3883 const char *name;
3884
3885 bool operator== (const name_and_matcher &other) const
3886 {
3887 return matcher == other.matcher && strcmp (name, other.name) == 0;
3888 }
3889 };
3890
3891 /* A vector holding all the different symbol name matchers, for all
3892 languages. */
3893 std::vector<name_and_matcher> matchers;
3894
3895 for (int i = 0; i < nr_languages; i++)
3896 {
3897 enum language lang_e = (enum language) i;
3898
3899 const language_defn *lang = language_def (lang_e);
3900 symbol_name_matcher_ftype *name_matcher
3901 = lang->get_symbol_name_matcher (lookup_name_without_params);
3902
3903 name_and_matcher key {
3904 name_matcher,
3905 lookup_name_without_params.language_lookup_name (lang_e)
3906 };
3907
3908 /* Don't insert the same comparison routine more than once.
3909 Note that we do this linear walk. This is not a problem in
3910 practice because the number of supported languages is
3911 low. */
3912 if (std::find (matchers.begin (), matchers.end (), key)
3913 != matchers.end ())
3914 continue;
3915 matchers.push_back (std::move (key));
3916
3917 auto bounds
3918 = index.find_name_components_bounds (lookup_name_without_params,
3919 lang_e, per_objfile);
3920
3921 /* Now for each symbol name in range, check to see if we have a name
3922 match, and if so, call the MATCH_CALLBACK callback. */
3923
3924 for (; bounds.first != bounds.second; ++bounds.first)
3925 {
3926 const char *qualified
3927 = index.symbol_name_at (bounds.first->idx, per_objfile);
3928
3929 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3930 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3931 continue;
3932
3933 matches.push_back (bounds.first->idx);
3934 }
3935 }
3936
3937 std::sort (matches.begin (), matches.end ());
3938
3939 /* Finally call the callback, once per match. */
3940 ULONGEST prev = -1;
3941 bool result = true;
3942 for (offset_type idx : matches)
3943 {
3944 if (prev != idx)
3945 {
3946 if (!match_callback (idx))
3947 {
3948 result = false;
3949 break;
3950 }
3951 prev = idx;
3952 }
3953 }
3954
3955 /* Above we use a type wider than idx's for 'prev', since 0 and
3956 (offset_type)-1 are both possible values. */
3957 static_assert (sizeof (prev) > sizeof (offset_type), "");
3958
3959 return result;
3960 }
3961
3962 #if GDB_SELF_TEST
3963
3964 namespace selftests { namespace dw2_expand_symtabs_matching {
3965
3966 /* A mock .gdb_index/.debug_names-like name index table, enough to
3967 exercise dw2_expand_symtabs_matching_symbol, which works with the
3968 mapped_index_base interface. Builds an index from the symbol list
3969 passed as parameter to the constructor. */
3970 class mock_mapped_index : public mapped_index_base
3971 {
3972 public:
3973 mock_mapped_index (gdb::array_view<const char *> symbols)
3974 : m_symbol_table (symbols)
3975 {}
3976
3977 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3978
3979 /* Return the number of names in the symbol table. */
3980 size_t symbol_name_count () const override
3981 {
3982 return m_symbol_table.size ();
3983 }
3984
3985 /* Get the name of the symbol at IDX in the symbol table. */
3986 const char *symbol_name_at
3987 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3988 {
3989 return m_symbol_table[idx];
3990 }
3991
3992 private:
3993 gdb::array_view<const char *> m_symbol_table;
3994 };
3995
3996 /* Convenience function that converts a NULL pointer to a "<null>"
3997 string, to pass to print routines. */
3998
3999 static const char *
4000 string_or_null (const char *str)
4001 {
4002 return str != NULL ? str : "<null>";
4003 }
4004
4005 /* Check if a lookup_name_info built from
4006 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4007 index. EXPECTED_LIST is the list of expected matches, in expected
4008 matching order. If no match expected, then an empty list is
4009 specified. Returns true on success. On failure prints a warning
4010 indicating the file:line that failed, and returns false. */
4011
4012 static bool
4013 check_match (const char *file, int line,
4014 mock_mapped_index &mock_index,
4015 const char *name, symbol_name_match_type match_type,
4016 bool completion_mode,
4017 std::initializer_list<const char *> expected_list,
4018 dwarf2_per_objfile *per_objfile)
4019 {
4020 lookup_name_info lookup_name (name, match_type, completion_mode);
4021
4022 bool matched = true;
4023
4024 auto mismatch = [&] (const char *expected_str,
4025 const char *got)
4026 {
4027 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4028 "expected=\"%s\", got=\"%s\"\n"),
4029 file, line,
4030 (match_type == symbol_name_match_type::FULL
4031 ? "FULL" : "WILD"),
4032 name, string_or_null (expected_str), string_or_null (got));
4033 matched = false;
4034 };
4035
4036 auto expected_it = expected_list.begin ();
4037 auto expected_end = expected_list.end ();
4038
4039 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4040 nullptr,
4041 [&] (offset_type idx)
4042 {
4043 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4044 const char *expected_str
4045 = expected_it == expected_end ? NULL : *expected_it++;
4046
4047 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4048 mismatch (expected_str, matched_name);
4049 return true;
4050 }, per_objfile);
4051
4052 const char *expected_str
4053 = expected_it == expected_end ? NULL : *expected_it++;
4054 if (expected_str != NULL)
4055 mismatch (expected_str, NULL);
4056
4057 return matched;
4058 }
4059
4060 /* The symbols added to the mock mapped_index for testing (in
4061 canonical form). */
4062 static const char *test_symbols[] = {
4063 "function",
4064 "std::bar",
4065 "std::zfunction",
4066 "std::zfunction2",
4067 "w1::w2",
4068 "ns::foo<char*>",
4069 "ns::foo<int>",
4070 "ns::foo<long>",
4071 "ns2::tmpl<int>::foo2",
4072 "(anonymous namespace)::A::B::C",
4073
4074 /* These are used to check that the increment-last-char in the
4075 matching algorithm for completion doesn't match "t1_fund" when
4076 completing "t1_func". */
4077 "t1_func",
4078 "t1_func1",
4079 "t1_fund",
4080 "t1_fund1",
4081
4082 /* A UTF-8 name with multi-byte sequences to make sure that
4083 cp-name-parser understands this as a single identifier ("função"
4084 is "function" in PT). */
4085 u8"u8função",
4086
4087 /* \377 (0xff) is Latin1 'ÿ'. */
4088 "yfunc\377",
4089
4090 /* \377 (0xff) is Latin1 'ÿ'. */
4091 "\377",
4092 "\377\377123",
4093
4094 /* A name with all sorts of complications. Starts with "z" to make
4095 it easier for the completion tests below. */
4096 #define Z_SYM_NAME \
4097 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4098 "::tuple<(anonymous namespace)::ui*, " \
4099 "std::default_delete<(anonymous namespace)::ui>, void>"
4100
4101 Z_SYM_NAME
4102 };
4103
4104 /* Returns true if the mapped_index_base::find_name_component_bounds
4105 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4106 in completion mode. */
4107
4108 static bool
4109 check_find_bounds_finds (mapped_index_base &index,
4110 const char *search_name,
4111 gdb::array_view<const char *> expected_syms,
4112 dwarf2_per_objfile *per_objfile)
4113 {
4114 lookup_name_info lookup_name (search_name,
4115 symbol_name_match_type::FULL, true);
4116
4117 auto bounds = index.find_name_components_bounds (lookup_name,
4118 language_cplus,
4119 per_objfile);
4120
4121 size_t distance = std::distance (bounds.first, bounds.second);
4122 if (distance != expected_syms.size ())
4123 return false;
4124
4125 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4126 {
4127 auto nc_elem = bounds.first + exp_elem;
4128 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4129 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4130 return false;
4131 }
4132
4133 return true;
4134 }
4135
4136 /* Test the lower-level mapped_index::find_name_component_bounds
4137 method. */
4138
4139 static void
4140 test_mapped_index_find_name_component_bounds ()
4141 {
4142 mock_mapped_index mock_index (test_symbols);
4143
4144 mock_index.build_name_components (NULL /* per_objfile */);
4145
4146 /* Test the lower-level mapped_index::find_name_component_bounds
4147 method in completion mode. */
4148 {
4149 static const char *expected_syms[] = {
4150 "t1_func",
4151 "t1_func1",
4152 };
4153
4154 SELF_CHECK (check_find_bounds_finds
4155 (mock_index, "t1_func", expected_syms,
4156 NULL /* per_objfile */));
4157 }
4158
4159 /* Check that the increment-last-char in the name matching algorithm
4160 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4161 {
4162 static const char *expected_syms1[] = {
4163 "\377",
4164 "\377\377123",
4165 };
4166 SELF_CHECK (check_find_bounds_finds
4167 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4168
4169 static const char *expected_syms2[] = {
4170 "\377\377123",
4171 };
4172 SELF_CHECK (check_find_bounds_finds
4173 (mock_index, "\377\377", expected_syms2,
4174 NULL /* per_objfile */));
4175 }
4176 }
4177
4178 /* Test dw2_expand_symtabs_matching_symbol. */
4179
4180 static void
4181 test_dw2_expand_symtabs_matching_symbol ()
4182 {
4183 mock_mapped_index mock_index (test_symbols);
4184
4185 /* We let all tests run until the end even if some fails, for debug
4186 convenience. */
4187 bool any_mismatch = false;
4188
4189 /* Create the expected symbols list (an initializer_list). Needed
4190 because lists have commas, and we need to pass them to CHECK,
4191 which is a macro. */
4192 #define EXPECT(...) { __VA_ARGS__ }
4193
4194 /* Wrapper for check_match that passes down the current
4195 __FILE__/__LINE__. */
4196 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4197 any_mismatch |= !check_match (__FILE__, __LINE__, \
4198 mock_index, \
4199 NAME, MATCH_TYPE, COMPLETION_MODE, \
4200 EXPECTED_LIST, NULL)
4201
4202 /* Identity checks. */
4203 for (const char *sym : test_symbols)
4204 {
4205 /* Should be able to match all existing symbols. */
4206 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4207 EXPECT (sym));
4208
4209 /* Should be able to match all existing symbols with
4210 parameters. */
4211 std::string with_params = std::string (sym) + "(int)";
4212 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4213 EXPECT (sym));
4214
4215 /* Should be able to match all existing symbols with
4216 parameters and qualifiers. */
4217 with_params = std::string (sym) + " ( int ) const";
4218 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4219 EXPECT (sym));
4220
4221 /* This should really find sym, but cp-name-parser.y doesn't
4222 know about lvalue/rvalue qualifiers yet. */
4223 with_params = std::string (sym) + " ( int ) &&";
4224 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4225 {});
4226 }
4227
4228 /* Check that the name matching algorithm for completion doesn't get
4229 confused with Latin1 'ÿ' / 0xff. */
4230 {
4231 static const char str[] = "\377";
4232 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4233 EXPECT ("\377", "\377\377123"));
4234 }
4235
4236 /* Check that the increment-last-char in the matching algorithm for
4237 completion doesn't match "t1_fund" when completing "t1_func". */
4238 {
4239 static const char str[] = "t1_func";
4240 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4241 EXPECT ("t1_func", "t1_func1"));
4242 }
4243
4244 /* Check that completion mode works at each prefix of the expected
4245 symbol name. */
4246 {
4247 static const char str[] = "function(int)";
4248 size_t len = strlen (str);
4249 std::string lookup;
4250
4251 for (size_t i = 1; i < len; i++)
4252 {
4253 lookup.assign (str, i);
4254 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4255 EXPECT ("function"));
4256 }
4257 }
4258
4259 /* While "w" is a prefix of both components, the match function
4260 should still only be called once. */
4261 {
4262 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4263 EXPECT ("w1::w2"));
4264 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4265 EXPECT ("w1::w2"));
4266 }
4267
4268 /* Same, with a "complicated" symbol. */
4269 {
4270 static const char str[] = Z_SYM_NAME;
4271 size_t len = strlen (str);
4272 std::string lookup;
4273
4274 for (size_t i = 1; i < len; i++)
4275 {
4276 lookup.assign (str, i);
4277 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4278 EXPECT (Z_SYM_NAME));
4279 }
4280 }
4281
4282 /* In FULL mode, an incomplete symbol doesn't match. */
4283 {
4284 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4285 {});
4286 }
4287
4288 /* A complete symbol with parameters matches any overload, since the
4289 index has no overload info. */
4290 {
4291 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4292 EXPECT ("std::zfunction", "std::zfunction2"));
4293 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4294 EXPECT ("std::zfunction", "std::zfunction2"));
4295 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4296 EXPECT ("std::zfunction", "std::zfunction2"));
4297 }
4298
4299 /* Check that whitespace is ignored appropriately. A symbol with a
4300 template argument list. */
4301 {
4302 static const char expected[] = "ns::foo<int>";
4303 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4304 EXPECT (expected));
4305 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4306 EXPECT (expected));
4307 }
4308
4309 /* Check that whitespace is ignored appropriately. A symbol with a
4310 template argument list that includes a pointer. */
4311 {
4312 static const char expected[] = "ns::foo<char*>";
4313 /* Try both completion and non-completion modes. */
4314 static const bool completion_mode[2] = {false, true};
4315 for (size_t i = 0; i < 2; i++)
4316 {
4317 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4318 completion_mode[i], EXPECT (expected));
4319 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4320 completion_mode[i], EXPECT (expected));
4321
4322 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4323 completion_mode[i], EXPECT (expected));
4324 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4325 completion_mode[i], EXPECT (expected));
4326 }
4327 }
4328
4329 {
4330 /* Check method qualifiers are ignored. */
4331 static const char expected[] = "ns::foo<char*>";
4332 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4333 symbol_name_match_type::FULL, true, EXPECT (expected));
4334 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4335 symbol_name_match_type::FULL, true, EXPECT (expected));
4336 CHECK_MATCH ("foo < char * > ( int ) const",
4337 symbol_name_match_type::WILD, true, EXPECT (expected));
4338 CHECK_MATCH ("foo < char * > ( int ) &&",
4339 symbol_name_match_type::WILD, true, EXPECT (expected));
4340 }
4341
4342 /* Test lookup names that don't match anything. */
4343 {
4344 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4345 {});
4346
4347 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4348 {});
4349 }
4350
4351 /* Some wild matching tests, exercising "(anonymous namespace)",
4352 which should not be confused with a parameter list. */
4353 {
4354 static const char *syms[] = {
4355 "A::B::C",
4356 "B::C",
4357 "C",
4358 "A :: B :: C ( int )",
4359 "B :: C ( int )",
4360 "C ( int )",
4361 };
4362
4363 for (const char *s : syms)
4364 {
4365 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4366 EXPECT ("(anonymous namespace)::A::B::C"));
4367 }
4368 }
4369
4370 {
4371 static const char expected[] = "ns2::tmpl<int>::foo2";
4372 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4373 EXPECT (expected));
4374 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4375 EXPECT (expected));
4376 }
4377
4378 SELF_CHECK (!any_mismatch);
4379
4380 #undef EXPECT
4381 #undef CHECK_MATCH
4382 }
4383
4384 static void
4385 run_test ()
4386 {
4387 test_mapped_index_find_name_component_bounds ();
4388 test_dw2_expand_symtabs_matching_symbol ();
4389 }
4390
4391 }} // namespace selftests::dw2_expand_symtabs_matching
4392
4393 #endif /* GDB_SELF_TEST */
4394
4395 /* If FILE_MATCHER is NULL or if PER_CU has
4396 dwarf2_per_cu_quick_data::MARK set (see
4397 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4398 EXPANSION_NOTIFY on it. */
4399
4400 static bool
4401 dw2_expand_symtabs_matching_one
4402 (dwarf2_per_cu_data *per_cu,
4403 dwarf2_per_objfile *per_objfile,
4404 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4405 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4406 {
4407 if (file_matcher == NULL || per_cu->v.quick->mark)
4408 {
4409 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4410
4411 compunit_symtab *symtab
4412 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4413 gdb_assert (symtab != nullptr);
4414
4415 if (expansion_notify != NULL && symtab_was_null)
4416 return expansion_notify (symtab);
4417 }
4418 return true;
4419 }
4420
4421 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4422 matched, to expand corresponding CUs that were marked. IDX is the
4423 index of the symbol name that matched. */
4424
4425 static bool
4426 dw2_expand_marked_cus
4427 (dwarf2_per_objfile *per_objfile, offset_type idx,
4428 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4429 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4430 block_search_flags search_flags,
4431 search_domain kind)
4432 {
4433 offset_type vec_len, vec_idx;
4434 bool global_seen = false;
4435 mapped_index &index = *per_objfile->per_bfd->index_table;
4436
4437 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4438 vec_len = vec[0];
4439 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4440 {
4441 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4442 /* This value is only valid for index versions >= 7. */
4443 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4444 gdb_index_symbol_kind symbol_kind =
4445 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4446 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4447 /* Only check the symbol attributes if they're present.
4448 Indices prior to version 7 don't record them,
4449 and indices >= 7 may elide them for certain symbols
4450 (gold does this). */
4451 int attrs_valid =
4452 (index.version >= 7
4453 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4454
4455 /* Work around gold/15646. */
4456 if (attrs_valid
4457 && !is_static
4458 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4459 {
4460 if (global_seen)
4461 continue;
4462
4463 global_seen = true;
4464 }
4465
4466 /* Only check the symbol's kind if it has one. */
4467 if (attrs_valid)
4468 {
4469 if (is_static)
4470 {
4471 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4472 continue;
4473 }
4474 else
4475 {
4476 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4477 continue;
4478 }
4479
4480 switch (kind)
4481 {
4482 case VARIABLES_DOMAIN:
4483 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4484 continue;
4485 break;
4486 case FUNCTIONS_DOMAIN:
4487 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4488 continue;
4489 break;
4490 case TYPES_DOMAIN:
4491 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4492 continue;
4493 break;
4494 case MODULES_DOMAIN:
4495 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4496 continue;
4497 break;
4498 default:
4499 break;
4500 }
4501 }
4502
4503 /* Don't crash on bad data. */
4504 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4505 + per_objfile->per_bfd->all_type_units.size ()))
4506 {
4507 complaint (_(".gdb_index entry has bad CU index"
4508 " [in module %s]"), objfile_name (per_objfile->objfile));
4509 continue;
4510 }
4511
4512 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4513 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4514 expansion_notify))
4515 return false;
4516 }
4517
4518 return true;
4519 }
4520
4521 /* If FILE_MATCHER is non-NULL, set all the
4522 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4523 that match FILE_MATCHER. */
4524
4525 static void
4526 dw_expand_symtabs_matching_file_matcher
4527 (dwarf2_per_objfile *per_objfile,
4528 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4529 {
4530 if (file_matcher == NULL)
4531 return;
4532
4533 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4534 htab_eq_pointer,
4535 NULL, xcalloc, xfree));
4536 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4537 htab_eq_pointer,
4538 NULL, xcalloc, xfree));
4539
4540 /* The rule is CUs specify all the files, including those used by
4541 any TU, so there's no need to scan TUs here. */
4542
4543 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4544 {
4545 QUIT;
4546
4547 per_cu->v.quick->mark = 0;
4548
4549 /* We only need to look at symtabs not already expanded. */
4550 if (per_objfile->symtab_set_p (per_cu))
4551 continue;
4552
4553 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4554 if (file_data == NULL)
4555 continue;
4556
4557 if (htab_find (visited_not_found.get (), file_data) != NULL)
4558 continue;
4559 else if (htab_find (visited_found.get (), file_data) != NULL)
4560 {
4561 per_cu->v.quick->mark = 1;
4562 continue;
4563 }
4564
4565 for (int j = 0; j < file_data->num_file_names; ++j)
4566 {
4567 const char *this_real_name;
4568
4569 if (file_matcher (file_data->file_names[j], false))
4570 {
4571 per_cu->v.quick->mark = 1;
4572 break;
4573 }
4574
4575 /* Before we invoke realpath, which can get expensive when many
4576 files are involved, do a quick comparison of the basenames. */
4577 if (!basenames_may_differ
4578 && !file_matcher (lbasename (file_data->file_names[j]),
4579 true))
4580 continue;
4581
4582 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4583 if (file_matcher (this_real_name, false))
4584 {
4585 per_cu->v.quick->mark = 1;
4586 break;
4587 }
4588 }
4589
4590 void **slot = htab_find_slot (per_cu->v.quick->mark
4591 ? visited_found.get ()
4592 : visited_not_found.get (),
4593 file_data, INSERT);
4594 *slot = file_data;
4595 }
4596 }
4597
4598 bool
4599 dwarf2_gdb_index::expand_symtabs_matching
4600 (struct objfile *objfile,
4601 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4602 const lookup_name_info *lookup_name,
4603 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4604 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4605 block_search_flags search_flags,
4606 domain_enum domain,
4607 enum search_domain kind)
4608 {
4609 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4610
4611 /* index_table is NULL if OBJF_READNOW. */
4612 if (!per_objfile->per_bfd->index_table)
4613 return true;
4614
4615 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4616
4617 if (symbol_matcher == NULL && lookup_name == NULL)
4618 {
4619 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4620 {
4621 QUIT;
4622
4623 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4624 file_matcher,
4625 expansion_notify))
4626 return false;
4627 }
4628 return true;
4629 }
4630
4631 mapped_index &index = *per_objfile->per_bfd->index_table;
4632
4633 bool result
4634 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4635 symbol_matcher,
4636 [&] (offset_type idx)
4637 {
4638 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4639 expansion_notify, search_flags, kind))
4640 return false;
4641 return true;
4642 }, per_objfile);
4643
4644 return result;
4645 }
4646
4647 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4648 symtab. */
4649
4650 static struct compunit_symtab *
4651 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4652 CORE_ADDR pc)
4653 {
4654 int i;
4655
4656 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4657 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4658 return cust;
4659
4660 if (cust->includes == NULL)
4661 return NULL;
4662
4663 for (i = 0; cust->includes[i]; ++i)
4664 {
4665 struct compunit_symtab *s = cust->includes[i];
4666
4667 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4668 if (s != NULL)
4669 return s;
4670 }
4671
4672 return NULL;
4673 }
4674
4675 struct compunit_symtab *
4676 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4677 (struct objfile *objfile,
4678 struct bound_minimal_symbol msymbol,
4679 CORE_ADDR pc,
4680 struct obj_section *section,
4681 int warn_if_readin)
4682 {
4683 struct dwarf2_per_cu_data *data;
4684 struct compunit_symtab *result;
4685
4686 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4687 if (per_objfile->per_bfd->index_addrmap == nullptr)
4688 return NULL;
4689
4690 CORE_ADDR baseaddr = objfile->text_section_offset ();
4691 data = ((struct dwarf2_per_cu_data *)
4692 addrmap_find (per_objfile->per_bfd->index_addrmap,
4693 pc - baseaddr));
4694 if (!data)
4695 return NULL;
4696
4697 if (warn_if_readin && per_objfile->symtab_set_p (data))
4698 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4699 paddress (objfile->arch (), pc));
4700
4701 result = recursively_find_pc_sect_compunit_symtab
4702 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4703
4704 gdb_assert (result != NULL);
4705 return result;
4706 }
4707
4708 void
4709 dwarf2_base_index_functions::map_symbol_filenames
4710 (struct objfile *objfile,
4711 gdb::function_view<symbol_filename_ftype> fun,
4712 bool need_fullname)
4713 {
4714 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4715
4716 if (!per_objfile->per_bfd->filenames_cache)
4717 {
4718 per_objfile->per_bfd->filenames_cache.emplace ();
4719
4720 htab_up visited (htab_create_alloc (10,
4721 htab_hash_pointer, htab_eq_pointer,
4722 NULL, xcalloc, xfree));
4723
4724 /* The rule is CUs specify all the files, including those used
4725 by any TU, so there's no need to scan TUs here. We can
4726 ignore file names coming from already-expanded CUs. */
4727
4728 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4729 {
4730 if (per_objfile->symtab_set_p (per_cu))
4731 {
4732 void **slot = htab_find_slot (visited.get (),
4733 per_cu->v.quick->file_names,
4734 INSERT);
4735
4736 *slot = per_cu->v.quick->file_names;
4737 }
4738 }
4739
4740 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4741 {
4742 /* We only need to look at symtabs not already expanded. */
4743 if (per_objfile->symtab_set_p (per_cu))
4744 continue;
4745
4746 quick_file_names *file_data
4747 = dw2_get_file_names (per_cu, per_objfile);
4748 if (file_data == NULL)
4749 continue;
4750
4751 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4752 if (*slot)
4753 {
4754 /* Already visited. */
4755 continue;
4756 }
4757 *slot = file_data;
4758
4759 for (int j = 0; j < file_data->num_file_names; ++j)
4760 {
4761 const char *filename = file_data->file_names[j];
4762 per_objfile->per_bfd->filenames_cache->seen (filename);
4763 }
4764 }
4765 }
4766
4767 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4768 {
4769 gdb::unique_xmalloc_ptr<char> this_real_name;
4770
4771 if (need_fullname)
4772 this_real_name = gdb_realpath (filename);
4773 fun (filename, this_real_name.get ());
4774 });
4775 }
4776
4777 bool
4778 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4779 {
4780 return true;
4781 }
4782
4783 /* DWARF-5 debug_names reader. */
4784
4785 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4786 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4787
4788 /* A helper function that reads the .debug_names section in SECTION
4789 and fills in MAP. FILENAME is the name of the file containing the
4790 section; it is used for error reporting.
4791
4792 Returns true if all went well, false otherwise. */
4793
4794 static bool
4795 read_debug_names_from_section (struct objfile *objfile,
4796 const char *filename,
4797 struct dwarf2_section_info *section,
4798 mapped_debug_names &map)
4799 {
4800 if (section->empty ())
4801 return false;
4802
4803 /* Older elfutils strip versions could keep the section in the main
4804 executable while splitting it for the separate debug info file. */
4805 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4806 return false;
4807
4808 section->read (objfile);
4809
4810 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4811
4812 const gdb_byte *addr = section->buffer;
4813
4814 bfd *const abfd = section->get_bfd_owner ();
4815
4816 unsigned int bytes_read;
4817 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4818 addr += bytes_read;
4819
4820 map.dwarf5_is_dwarf64 = bytes_read != 4;
4821 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4822 if (bytes_read + length != section->size)
4823 {
4824 /* There may be multiple per-CU indices. */
4825 warning (_("Section .debug_names in %s length %s does not match "
4826 "section length %s, ignoring .debug_names."),
4827 filename, plongest (bytes_read + length),
4828 pulongest (section->size));
4829 return false;
4830 }
4831
4832 /* The version number. */
4833 uint16_t version = read_2_bytes (abfd, addr);
4834 addr += 2;
4835 if (version != 5)
4836 {
4837 warning (_("Section .debug_names in %s has unsupported version %d, "
4838 "ignoring .debug_names."),
4839 filename, version);
4840 return false;
4841 }
4842
4843 /* Padding. */
4844 uint16_t padding = read_2_bytes (abfd, addr);
4845 addr += 2;
4846 if (padding != 0)
4847 {
4848 warning (_("Section .debug_names in %s has unsupported padding %d, "
4849 "ignoring .debug_names."),
4850 filename, padding);
4851 return false;
4852 }
4853
4854 /* comp_unit_count - The number of CUs in the CU list. */
4855 map.cu_count = read_4_bytes (abfd, addr);
4856 addr += 4;
4857
4858 /* local_type_unit_count - The number of TUs in the local TU
4859 list. */
4860 map.tu_count = read_4_bytes (abfd, addr);
4861 addr += 4;
4862
4863 /* foreign_type_unit_count - The number of TUs in the foreign TU
4864 list. */
4865 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4866 addr += 4;
4867 if (foreign_tu_count != 0)
4868 {
4869 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4870 "ignoring .debug_names."),
4871 filename, static_cast<unsigned long> (foreign_tu_count));
4872 return false;
4873 }
4874
4875 /* bucket_count - The number of hash buckets in the hash lookup
4876 table. */
4877 map.bucket_count = read_4_bytes (abfd, addr);
4878 addr += 4;
4879
4880 /* name_count - The number of unique names in the index. */
4881 map.name_count = read_4_bytes (abfd, addr);
4882 addr += 4;
4883
4884 /* abbrev_table_size - The size in bytes of the abbreviations
4885 table. */
4886 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4887 addr += 4;
4888
4889 /* augmentation_string_size - The size in bytes of the augmentation
4890 string. This value is rounded up to a multiple of 4. */
4891 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4892 addr += 4;
4893 map.augmentation_is_gdb = ((augmentation_string_size
4894 == sizeof (dwarf5_augmentation))
4895 && memcmp (addr, dwarf5_augmentation,
4896 sizeof (dwarf5_augmentation)) == 0);
4897 augmentation_string_size += (-augmentation_string_size) & 3;
4898 addr += augmentation_string_size;
4899
4900 /* List of CUs */
4901 map.cu_table_reordered = addr;
4902 addr += map.cu_count * map.offset_size;
4903
4904 /* List of Local TUs */
4905 map.tu_table_reordered = addr;
4906 addr += map.tu_count * map.offset_size;
4907
4908 /* Hash Lookup Table */
4909 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4910 addr += map.bucket_count * 4;
4911 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4912 addr += map.name_count * 4;
4913
4914 /* Name Table */
4915 map.name_table_string_offs_reordered = addr;
4916 addr += map.name_count * map.offset_size;
4917 map.name_table_entry_offs_reordered = addr;
4918 addr += map.name_count * map.offset_size;
4919
4920 const gdb_byte *abbrev_table_start = addr;
4921 for (;;)
4922 {
4923 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4924 addr += bytes_read;
4925 if (index_num == 0)
4926 break;
4927
4928 const auto insertpair
4929 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4930 if (!insertpair.second)
4931 {
4932 warning (_("Section .debug_names in %s has duplicate index %s, "
4933 "ignoring .debug_names."),
4934 filename, pulongest (index_num));
4935 return false;
4936 }
4937 mapped_debug_names::index_val &indexval = insertpair.first->second;
4938 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4939 addr += bytes_read;
4940
4941 for (;;)
4942 {
4943 mapped_debug_names::index_val::attr attr;
4944 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4945 addr += bytes_read;
4946 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4947 addr += bytes_read;
4948 if (attr.form == DW_FORM_implicit_const)
4949 {
4950 attr.implicit_const = read_signed_leb128 (abfd, addr,
4951 &bytes_read);
4952 addr += bytes_read;
4953 }
4954 if (attr.dw_idx == 0 && attr.form == 0)
4955 break;
4956 indexval.attr_vec.push_back (std::move (attr));
4957 }
4958 }
4959 if (addr != abbrev_table_start + abbrev_table_size)
4960 {
4961 warning (_("Section .debug_names in %s has abbreviation_table "
4962 "of size %s vs. written as %u, ignoring .debug_names."),
4963 filename, plongest (addr - abbrev_table_start),
4964 abbrev_table_size);
4965 return false;
4966 }
4967 map.entry_pool = addr;
4968
4969 return true;
4970 }
4971
4972 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4973 list. */
4974
4975 static void
4976 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4977 const mapped_debug_names &map,
4978 dwarf2_section_info &section,
4979 bool is_dwz)
4980 {
4981 if (!map.augmentation_is_gdb)
4982 {
4983 for (uint32_t i = 0; i < map.cu_count; ++i)
4984 {
4985 sect_offset sect_off
4986 = (sect_offset) (extract_unsigned_integer
4987 (map.cu_table_reordered + i * map.offset_size,
4988 map.offset_size,
4989 map.dwarf5_byte_order));
4990 /* We don't know the length of the CU, because the CU list in a
4991 .debug_names index can be incomplete, so we can't use the start
4992 of the next CU as end of this CU. We create the CUs here with
4993 length 0, and in cutu_reader::cutu_reader we'll fill in the
4994 actual length. */
4995 dwarf2_per_cu_data *per_cu
4996 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4997 sect_off, 0);
4998 per_bfd->all_comp_units.push_back (per_cu);
4999 }
5000 return;
5001 }
5002
5003 sect_offset sect_off_prev;
5004 for (uint32_t i = 0; i <= map.cu_count; ++i)
5005 {
5006 sect_offset sect_off_next;
5007 if (i < map.cu_count)
5008 {
5009 sect_off_next
5010 = (sect_offset) (extract_unsigned_integer
5011 (map.cu_table_reordered + i * map.offset_size,
5012 map.offset_size,
5013 map.dwarf5_byte_order));
5014 }
5015 else
5016 sect_off_next = (sect_offset) section.size;
5017 if (i >= 1)
5018 {
5019 const ULONGEST length = sect_off_next - sect_off_prev;
5020 dwarf2_per_cu_data *per_cu
5021 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5022 sect_off_prev, length);
5023 per_bfd->all_comp_units.push_back (per_cu);
5024 }
5025 sect_off_prev = sect_off_next;
5026 }
5027 }
5028
5029 /* Read the CU list from the mapped index, and use it to create all
5030 the CU objects for this dwarf2_per_objfile. */
5031
5032 static void
5033 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5034 const mapped_debug_names &map,
5035 const mapped_debug_names &dwz_map)
5036 {
5037 gdb_assert (per_bfd->all_comp_units.empty ());
5038 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5039
5040 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5041 false /* is_dwz */);
5042
5043 if (dwz_map.cu_count == 0)
5044 return;
5045
5046 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5047 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5048 true /* is_dwz */);
5049 }
5050
5051 /* Read .debug_names. If everything went ok, initialize the "quick"
5052 elements of all the CUs and return true. Otherwise, return false. */
5053
5054 static bool
5055 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5056 {
5057 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5058 mapped_debug_names dwz_map;
5059 struct objfile *objfile = per_objfile->objfile;
5060 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5061
5062 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5063 &per_bfd->debug_names, *map))
5064 return false;
5065
5066 /* Don't use the index if it's empty. */
5067 if (map->name_count == 0)
5068 return false;
5069
5070 /* If there is a .dwz file, read it so we can get its CU list as
5071 well. */
5072 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5073 if (dwz != NULL)
5074 {
5075 if (!read_debug_names_from_section (objfile,
5076 bfd_get_filename (dwz->dwz_bfd.get ()),
5077 &dwz->debug_names, dwz_map))
5078 {
5079 warning (_("could not read '.debug_names' section from %s; skipping"),
5080 bfd_get_filename (dwz->dwz_bfd.get ()));
5081 return false;
5082 }
5083 }
5084
5085 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5086
5087 if (map->tu_count != 0)
5088 {
5089 /* We can only handle a single .debug_types when we have an
5090 index. */
5091 if (per_bfd->types.size () != 1)
5092 return false;
5093
5094 dwarf2_section_info *section = &per_bfd->types[0];
5095
5096 create_signatured_type_table_from_debug_names
5097 (per_objfile, *map, section, &per_bfd->abbrev);
5098 }
5099
5100 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5101
5102 per_bfd->debug_names_table = std::move (map);
5103 per_bfd->using_index = 1;
5104 per_bfd->quick_file_names_table =
5105 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5106
5107 return true;
5108 }
5109
5110 /* Type used to manage iterating over all CUs looking for a symbol for
5111 .debug_names. */
5112
5113 class dw2_debug_names_iterator
5114 {
5115 public:
5116 dw2_debug_names_iterator (const mapped_debug_names &map,
5117 block_search_flags block_index,
5118 domain_enum domain,
5119 const char *name, dwarf2_per_objfile *per_objfile)
5120 : m_map (map), m_block_index (block_index), m_domain (domain),
5121 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5122 m_per_objfile (per_objfile)
5123 {}
5124
5125 dw2_debug_names_iterator (const mapped_debug_names &map,
5126 search_domain search, uint32_t namei,
5127 dwarf2_per_objfile *per_objfile,
5128 domain_enum domain = UNDEF_DOMAIN)
5129 : m_map (map),
5130 m_domain (domain),
5131 m_search (search),
5132 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5133 m_per_objfile (per_objfile)
5134 {}
5135
5136 dw2_debug_names_iterator (const mapped_debug_names &map,
5137 block_search_flags block_index, domain_enum domain,
5138 uint32_t namei, dwarf2_per_objfile *per_objfile)
5139 : m_map (map), m_block_index (block_index), m_domain (domain),
5140 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5141 m_per_objfile (per_objfile)
5142 {}
5143
5144 /* Return the next matching CU or NULL if there are no more. */
5145 dwarf2_per_cu_data *next ();
5146
5147 private:
5148 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5149 const char *name,
5150 dwarf2_per_objfile *per_objfile);
5151 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5152 uint32_t namei,
5153 dwarf2_per_objfile *per_objfile);
5154
5155 /* The internalized form of .debug_names. */
5156 const mapped_debug_names &m_map;
5157
5158 /* Restrict the search to these blocks. */
5159 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5160 | SEARCH_STATIC_BLOCK);
5161
5162 /* The kind of symbol we're looking for. */
5163 const domain_enum m_domain = UNDEF_DOMAIN;
5164 const search_domain m_search = ALL_DOMAIN;
5165
5166 /* The list of CUs from the index entry of the symbol, or NULL if
5167 not found. */
5168 const gdb_byte *m_addr;
5169
5170 dwarf2_per_objfile *m_per_objfile;
5171 };
5172
5173 const char *
5174 mapped_debug_names::namei_to_name
5175 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5176 {
5177 const ULONGEST namei_string_offs
5178 = extract_unsigned_integer ((name_table_string_offs_reordered
5179 + namei * offset_size),
5180 offset_size,
5181 dwarf5_byte_order);
5182 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5183 }
5184
5185 /* Find a slot in .debug_names for the object named NAME. If NAME is
5186 found, return pointer to its pool data. If NAME cannot be found,
5187 return NULL. */
5188
5189 const gdb_byte *
5190 dw2_debug_names_iterator::find_vec_in_debug_names
5191 (const mapped_debug_names &map, const char *name,
5192 dwarf2_per_objfile *per_objfile)
5193 {
5194 int (*cmp) (const char *, const char *);
5195
5196 gdb::unique_xmalloc_ptr<char> without_params;
5197 if (current_language->la_language == language_cplus
5198 || current_language->la_language == language_fortran
5199 || current_language->la_language == language_d)
5200 {
5201 /* NAME is already canonical. Drop any qualifiers as
5202 .debug_names does not contain any. */
5203
5204 if (strchr (name, '(') != NULL)
5205 {
5206 without_params = cp_remove_params (name);
5207 if (without_params != NULL)
5208 name = without_params.get ();
5209 }
5210 }
5211
5212 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5213
5214 const uint32_t full_hash = dwarf5_djb_hash (name);
5215 uint32_t namei
5216 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5217 (map.bucket_table_reordered
5218 + (full_hash % map.bucket_count)), 4,
5219 map.dwarf5_byte_order);
5220 if (namei == 0)
5221 return NULL;
5222 --namei;
5223 if (namei >= map.name_count)
5224 {
5225 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5226 "[in module %s]"),
5227 namei, map.name_count,
5228 objfile_name (per_objfile->objfile));
5229 return NULL;
5230 }
5231
5232 for (;;)
5233 {
5234 const uint32_t namei_full_hash
5235 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5236 (map.hash_table_reordered + namei), 4,
5237 map.dwarf5_byte_order);
5238 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5239 return NULL;
5240
5241 if (full_hash == namei_full_hash)
5242 {
5243 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5244
5245 #if 0 /* An expensive sanity check. */
5246 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5247 {
5248 complaint (_("Wrong .debug_names hash for string at index %u "
5249 "[in module %s]"),
5250 namei, objfile_name (dwarf2_per_objfile->objfile));
5251 return NULL;
5252 }
5253 #endif
5254
5255 if (cmp (namei_string, name) == 0)
5256 {
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263 }
5264
5265 ++namei;
5266 if (namei >= map.name_count)
5267 return NULL;
5268 }
5269 }
5270
5271 const gdb_byte *
5272 dw2_debug_names_iterator::find_vec_in_debug_names
5273 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5274 {
5275 if (namei >= map.name_count)
5276 {
5277 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5278 "[in module %s]"),
5279 namei, map.name_count,
5280 objfile_name (per_objfile->objfile));
5281 return NULL;
5282 }
5283
5284 const ULONGEST namei_entry_offs
5285 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5286 + namei * map.offset_size),
5287 map.offset_size, map.dwarf5_byte_order);
5288 return map.entry_pool + namei_entry_offs;
5289 }
5290
5291 /* See dw2_debug_names_iterator. */
5292
5293 dwarf2_per_cu_data *
5294 dw2_debug_names_iterator::next ()
5295 {
5296 if (m_addr == NULL)
5297 return NULL;
5298
5299 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5300 struct objfile *objfile = m_per_objfile->objfile;
5301 bfd *const abfd = objfile->obfd;
5302
5303 again:
5304
5305 unsigned int bytes_read;
5306 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5307 m_addr += bytes_read;
5308 if (abbrev == 0)
5309 return NULL;
5310
5311 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5312 if (indexval_it == m_map.abbrev_map.cend ())
5313 {
5314 complaint (_("Wrong .debug_names undefined abbrev code %s "
5315 "[in module %s]"),
5316 pulongest (abbrev), objfile_name (objfile));
5317 return NULL;
5318 }
5319 const mapped_debug_names::index_val &indexval = indexval_it->second;
5320 enum class symbol_linkage {
5321 unknown,
5322 static_,
5323 extern_,
5324 } symbol_linkage_ = symbol_linkage::unknown;
5325 dwarf2_per_cu_data *per_cu = NULL;
5326 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5327 {
5328 ULONGEST ull;
5329 switch (attr.form)
5330 {
5331 case DW_FORM_implicit_const:
5332 ull = attr.implicit_const;
5333 break;
5334 case DW_FORM_flag_present:
5335 ull = 1;
5336 break;
5337 case DW_FORM_udata:
5338 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5339 m_addr += bytes_read;
5340 break;
5341 case DW_FORM_ref4:
5342 ull = read_4_bytes (abfd, m_addr);
5343 m_addr += 4;
5344 break;
5345 case DW_FORM_ref8:
5346 ull = read_8_bytes (abfd, m_addr);
5347 m_addr += 8;
5348 break;
5349 case DW_FORM_ref_sig8:
5350 ull = read_8_bytes (abfd, m_addr);
5351 m_addr += 8;
5352 break;
5353 default:
5354 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5355 dwarf_form_name (attr.form),
5356 objfile_name (objfile));
5357 return NULL;
5358 }
5359 switch (attr.dw_idx)
5360 {
5361 case DW_IDX_compile_unit:
5362 /* Don't crash on bad data. */
5363 if (ull >= per_bfd->all_comp_units.size ())
5364 {
5365 complaint (_(".debug_names entry has bad CU index %s"
5366 " [in module %s]"),
5367 pulongest (ull),
5368 objfile_name (objfile));
5369 continue;
5370 }
5371 per_cu = per_bfd->get_cutu (ull);
5372 break;
5373 case DW_IDX_type_unit:
5374 /* Don't crash on bad data. */
5375 if (ull >= per_bfd->all_type_units.size ())
5376 {
5377 complaint (_(".debug_names entry has bad TU index %s"
5378 " [in module %s]"),
5379 pulongest (ull),
5380 objfile_name (objfile));
5381 continue;
5382 }
5383 per_cu = &per_bfd->get_tu (ull)->per_cu;
5384 break;
5385 case DW_IDX_die_offset:
5386 /* In a per-CU index (as opposed to a per-module index), index
5387 entries without CU attribute implicitly refer to the single CU. */
5388 if (per_cu == NULL)
5389 per_cu = per_bfd->get_cu (0);
5390 break;
5391 case DW_IDX_GNU_internal:
5392 if (!m_map.augmentation_is_gdb)
5393 break;
5394 symbol_linkage_ = symbol_linkage::static_;
5395 break;
5396 case DW_IDX_GNU_external:
5397 if (!m_map.augmentation_is_gdb)
5398 break;
5399 symbol_linkage_ = symbol_linkage::extern_;
5400 break;
5401 }
5402 }
5403
5404 /* Skip if already read in. */
5405 if (m_per_objfile->symtab_set_p (per_cu))
5406 goto again;
5407
5408 /* Check static vs global. */
5409 if (symbol_linkage_ != symbol_linkage::unknown)
5410 {
5411 if (symbol_linkage_ == symbol_linkage::static_)
5412 {
5413 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5414 goto again;
5415 }
5416 else
5417 {
5418 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5419 goto again;
5420 }
5421 }
5422
5423 /* Match dw2_symtab_iter_next, symbol_kind
5424 and debug_names::psymbol_tag. */
5425 switch (m_domain)
5426 {
5427 case VAR_DOMAIN:
5428 switch (indexval.dwarf_tag)
5429 {
5430 case DW_TAG_variable:
5431 case DW_TAG_subprogram:
5432 /* Some types are also in VAR_DOMAIN. */
5433 case DW_TAG_typedef:
5434 case DW_TAG_structure_type:
5435 break;
5436 default:
5437 goto again;
5438 }
5439 break;
5440 case STRUCT_DOMAIN:
5441 switch (indexval.dwarf_tag)
5442 {
5443 case DW_TAG_typedef:
5444 case DW_TAG_structure_type:
5445 break;
5446 default:
5447 goto again;
5448 }
5449 break;
5450 case LABEL_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case 0:
5454 case DW_TAG_variable:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 break;
5460 case MODULE_DOMAIN:
5461 switch (indexval.dwarf_tag)
5462 {
5463 case DW_TAG_module:
5464 break;
5465 default:
5466 goto again;
5467 }
5468 break;
5469 default:
5470 break;
5471 }
5472
5473 /* Match dw2_expand_symtabs_matching, symbol_kind and
5474 debug_names::psymbol_tag. */
5475 switch (m_search)
5476 {
5477 case VARIABLES_DOMAIN:
5478 switch (indexval.dwarf_tag)
5479 {
5480 case DW_TAG_variable:
5481 break;
5482 default:
5483 goto again;
5484 }
5485 break;
5486 case FUNCTIONS_DOMAIN:
5487 switch (indexval.dwarf_tag)
5488 {
5489 case DW_TAG_subprogram:
5490 break;
5491 default:
5492 goto again;
5493 }
5494 break;
5495 case TYPES_DOMAIN:
5496 switch (indexval.dwarf_tag)
5497 {
5498 case DW_TAG_typedef:
5499 case DW_TAG_structure_type:
5500 break;
5501 default:
5502 goto again;
5503 }
5504 break;
5505 case MODULES_DOMAIN:
5506 switch (indexval.dwarf_tag)
5507 {
5508 case DW_TAG_module:
5509 break;
5510 default:
5511 goto again;
5512 }
5513 default:
5514 break;
5515 }
5516
5517 return per_cu;
5518 }
5519
5520 /* This dumps minimal information about .debug_names. It is called
5521 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5522 uses this to verify that .debug_names has been loaded. */
5523
5524 void
5525 dwarf2_debug_names_index::dump (struct objfile *objfile)
5526 {
5527 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5528
5529 gdb_assert (per_objfile->per_bfd->using_index);
5530 printf_filtered (".debug_names:");
5531 if (per_objfile->per_bfd->debug_names_table)
5532 printf_filtered (" exists\n");
5533 else
5534 printf_filtered (" faked for \"readnow\"\n");
5535 printf_filtered ("\n");
5536 }
5537
5538 void
5539 dwarf2_debug_names_index::expand_matching_symbols
5540 (struct objfile *objfile,
5541 const lookup_name_info &name, domain_enum domain,
5542 int global,
5543 symbol_compare_ftype *ordered_compare)
5544 {
5545 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5546
5547 /* debug_names_table is NULL if OBJF_READNOW. */
5548 if (!per_objfile->per_bfd->debug_names_table)
5549 return;
5550
5551 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5552 const block_search_flags block_flags
5553 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5554
5555 const char *match_name = name.ada ().lookup_name ().c_str ();
5556 auto matcher = [&] (const char *symname)
5557 {
5558 if (ordered_compare == nullptr)
5559 return true;
5560 return ordered_compare (symname, match_name) == 0;
5561 };
5562
5563 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5564 [&] (offset_type namei)
5565 {
5566 /* The name was matched, now expand corresponding CUs that were
5567 marked. */
5568 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5569 per_objfile);
5570
5571 struct dwarf2_per_cu_data *per_cu;
5572 while ((per_cu = iter.next ()) != NULL)
5573 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5574 nullptr);
5575 return true;
5576 }, per_objfile);
5577 }
5578
5579 bool
5580 dwarf2_debug_names_index::expand_symtabs_matching
5581 (struct objfile *objfile,
5582 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5583 const lookup_name_info *lookup_name,
5584 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5585 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5586 block_search_flags search_flags,
5587 domain_enum domain,
5588 enum search_domain kind)
5589 {
5590 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5591
5592 /* debug_names_table is NULL if OBJF_READNOW. */
5593 if (!per_objfile->per_bfd->debug_names_table)
5594 return true;
5595
5596 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5597
5598 if (symbol_matcher == NULL && lookup_name == NULL)
5599 {
5600 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5601 {
5602 QUIT;
5603
5604 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5605 file_matcher,
5606 expansion_notify))
5607 return false;
5608 }
5609 return true;
5610 }
5611
5612 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5613
5614 bool result
5615 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5616 symbol_matcher,
5617 [&] (offset_type namei)
5618 {
5619 /* The name was matched, now expand corresponding CUs that were
5620 marked. */
5621 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5622
5623 struct dwarf2_per_cu_data *per_cu;
5624 while ((per_cu = iter.next ()) != NULL)
5625 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5626 file_matcher,
5627 expansion_notify))
5628 return false;
5629 return true;
5630 }, per_objfile);
5631
5632 return result;
5633 }
5634
5635 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5636 to either a dwarf2_per_bfd or dwz_file object. */
5637
5638 template <typename T>
5639 static gdb::array_view<const gdb_byte>
5640 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5641 {
5642 dwarf2_section_info *section = &section_owner->gdb_index;
5643
5644 if (section->empty ())
5645 return {};
5646
5647 /* Older elfutils strip versions could keep the section in the main
5648 executable while splitting it for the separate debug info file. */
5649 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5650 return {};
5651
5652 section->read (obj);
5653
5654 /* dwarf2_section_info::size is a bfd_size_type, while
5655 gdb::array_view works with size_t. On 32-bit hosts, with
5656 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5657 is 32-bit. So we need an explicit narrowing conversion here.
5658 This is fine, because it's impossible to allocate or mmap an
5659 array/buffer larger than what size_t can represent. */
5660 return gdb::make_array_view (section->buffer, section->size);
5661 }
5662
5663 /* Lookup the index cache for the contents of the index associated to
5664 DWARF2_OBJ. */
5665
5666 static gdb::array_view<const gdb_byte>
5667 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5668 {
5669 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5670 if (build_id == nullptr)
5671 return {};
5672
5673 return global_index_cache.lookup_gdb_index (build_id,
5674 &dwarf2_per_bfd->index_cache_res);
5675 }
5676
5677 /* Same as the above, but for DWZ. */
5678
5679 static gdb::array_view<const gdb_byte>
5680 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5681 {
5682 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5683 if (build_id == nullptr)
5684 return {};
5685
5686 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5687 }
5688
5689 /* See dwarf2/public.h. */
5690
5691 void
5692 dwarf2_initialize_objfile (struct objfile *objfile)
5693 {
5694 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5695 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5696
5697 dwarf_read_debug_printf ("called");
5698
5699 /* If we're about to read full symbols, don't bother with the
5700 indices. In this case we also don't care if some other debug
5701 format is making psymtabs, because they are all about to be
5702 expanded anyway. */
5703 if ((objfile->flags & OBJF_READNOW))
5704 {
5705 dwarf_read_debug_printf ("readnow requested");
5706
5707 /* When using READNOW, the using_index flag (set below) indicates that
5708 PER_BFD was already initialized, when we loaded some other objfile. */
5709 if (per_bfd->using_index)
5710 {
5711 dwarf_read_debug_printf ("using_index already set");
5712 per_objfile->resize_symtabs ();
5713 objfile->qf.push_front (make_dwarf_gdb_index ());
5714 return;
5715 }
5716
5717 per_bfd->using_index = 1;
5718 create_all_comp_units (per_objfile);
5719 create_all_type_units (per_objfile);
5720 per_bfd->quick_file_names_table
5721 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5722 per_objfile->resize_symtabs ();
5723
5724 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5725 + per_bfd->all_type_units.size ()); ++i)
5726 {
5727 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5728
5729 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5730 struct dwarf2_per_cu_quick_data);
5731 }
5732
5733 /* Arrange for gdb to see the "quick" functions. However, these
5734 functions will be no-ops because we will have expanded all
5735 symtabs. */
5736 objfile->qf.push_front (make_dwarf_gdb_index ());
5737 return;
5738 }
5739
5740 /* Was a debug names index already read when we processed an objfile sharing
5741 PER_BFD? */
5742 if (per_bfd->debug_names_table != nullptr)
5743 {
5744 dwarf_read_debug_printf ("re-using shared debug names table");
5745 per_objfile->resize_symtabs ();
5746 objfile->qf.push_front (make_dwarf_debug_names ());
5747 return;
5748 }
5749
5750 /* Was a GDB index already read when we processed an objfile sharing
5751 PER_BFD? */
5752 if (per_bfd->index_table != nullptr)
5753 {
5754 dwarf_read_debug_printf ("re-using shared index table");
5755 per_objfile->resize_symtabs ();
5756 objfile->qf.push_front (make_dwarf_gdb_index ());
5757 return;
5758 }
5759
5760 /* There might already be partial symtabs built for this BFD. This happens
5761 when loading the same binary twice with the index-cache enabled. If so,
5762 don't try to read an index. The objfile / per_objfile initialization will
5763 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5764 code path. */
5765 if (per_bfd->partial_symtabs != nullptr)
5766 {
5767 dwarf_read_debug_printf ("re-using shared partial symtabs");
5768 objfile->qf.push_front (make_lazy_dwarf_reader ());
5769 return;
5770 }
5771
5772 if (dwarf2_read_debug_names (per_objfile))
5773 {
5774 dwarf_read_debug_printf ("found debug names");
5775 per_objfile->resize_symtabs ();
5776 objfile->qf.push_front (make_dwarf_debug_names ());
5777 return;
5778 }
5779
5780 if (dwarf2_read_gdb_index (per_objfile,
5781 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5782 get_gdb_index_contents_from_section<dwz_file>))
5783 {
5784 dwarf_read_debug_printf ("found gdb index from file");
5785 per_objfile->resize_symtabs ();
5786 objfile->qf.push_front (make_dwarf_gdb_index ());
5787 return;
5788 }
5789
5790 /* ... otherwise, try to find the index in the index cache. */
5791 if (dwarf2_read_gdb_index (per_objfile,
5792 get_gdb_index_contents_from_cache,
5793 get_gdb_index_contents_from_cache_dwz))
5794 {
5795 dwarf_read_debug_printf ("found gdb index from cache");
5796 global_index_cache.hit ();
5797 per_objfile->resize_symtabs ();
5798 objfile->qf.push_front (make_dwarf_gdb_index ());
5799 return;
5800 }
5801
5802 global_index_cache.miss ();
5803 objfile->qf.push_front (make_lazy_dwarf_reader ());
5804 }
5805
5806 \f
5807
5808 /* Build a partial symbol table. */
5809
5810 void
5811 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5812 {
5813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5814 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5815
5816 if (per_bfd->partial_symtabs != nullptr)
5817 {
5818 /* Partial symbols were already read, so now we can simply
5819 attach them. */
5820 if (psf == nullptr)
5821 {
5822 psf = new psymbol_functions (per_bfd->partial_symtabs);
5823 objfile->qf.emplace_front (psf);
5824 }
5825 else
5826 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5827 per_objfile->resize_symtabs ();
5828 return;
5829 }
5830
5831 if (psf == nullptr)
5832 {
5833 psf = new psymbol_functions;
5834 objfile->qf.emplace_front (psf);
5835 }
5836 const std::shared_ptr<psymtab_storage> &partial_symtabs
5837 = psf->get_partial_symtabs ();
5838
5839 /* Set the local reference to partial symtabs, so that we don't try
5840 to read them again if reading another objfile with the same BFD.
5841 If we can't in fact share, this won't make a difference anyway as
5842 the dwarf2_per_bfd object won't be shared. */
5843 per_bfd->partial_symtabs = partial_symtabs;
5844
5845 try
5846 {
5847 /* This isn't really ideal: all the data we allocate on the
5848 objfile's obstack is still uselessly kept around. However,
5849 freeing it seems unsafe. */
5850 psymtab_discarder psymtabs (partial_symtabs.get ());
5851 dwarf2_build_psymtabs_hard (per_objfile);
5852 psymtabs.keep ();
5853
5854 per_objfile->resize_symtabs ();
5855
5856 /* (maybe) store an index in the cache. */
5857 global_index_cache.store (per_objfile);
5858 }
5859 catch (const gdb_exception_error &except)
5860 {
5861 exception_print (gdb_stderr, except);
5862 }
5863 }
5864
5865 /* Find the base address of the compilation unit for range lists and
5866 location lists. It will normally be specified by DW_AT_low_pc.
5867 In DWARF-3 draft 4, the base address could be overridden by
5868 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5869 compilation units with discontinuous ranges. */
5870
5871 static void
5872 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5873 {
5874 struct attribute *attr;
5875
5876 cu->base_address.reset ();
5877
5878 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5879 if (attr != nullptr)
5880 cu->base_address = attr->as_address ();
5881 else
5882 {
5883 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5884 if (attr != nullptr)
5885 cu->base_address = attr->as_address ();
5886 }
5887 }
5888
5889 /* Helper function that returns the proper abbrev section for
5890 THIS_CU. */
5891
5892 static struct dwarf2_section_info *
5893 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5894 {
5895 struct dwarf2_section_info *abbrev;
5896 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5897
5898 if (this_cu->is_dwz)
5899 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5900 else
5901 abbrev = &per_bfd->abbrev;
5902
5903 return abbrev;
5904 }
5905
5906 /* Fetch the abbreviation table offset from a comp or type unit header. */
5907
5908 static sect_offset
5909 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5910 struct dwarf2_section_info *section,
5911 sect_offset sect_off)
5912 {
5913 bfd *abfd = section->get_bfd_owner ();
5914 const gdb_byte *info_ptr;
5915 unsigned int initial_length_size, offset_size;
5916 uint16_t version;
5917
5918 section->read (per_objfile->objfile);
5919 info_ptr = section->buffer + to_underlying (sect_off);
5920 read_initial_length (abfd, info_ptr, &initial_length_size);
5921 offset_size = initial_length_size == 4 ? 4 : 8;
5922 info_ptr += initial_length_size;
5923
5924 version = read_2_bytes (abfd, info_ptr);
5925 info_ptr += 2;
5926 if (version >= 5)
5927 {
5928 /* Skip unit type and address size. */
5929 info_ptr += 2;
5930 }
5931
5932 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5933 }
5934
5935 /* A partial symtab that is used only for include files. */
5936 struct dwarf2_include_psymtab : public partial_symtab
5937 {
5938 dwarf2_include_psymtab (const char *filename,
5939 psymtab_storage *partial_symtabs,
5940 objfile_per_bfd_storage *objfile_per_bfd)
5941 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5942 {
5943 }
5944
5945 void read_symtab (struct objfile *objfile) override
5946 {
5947 /* It's an include file, no symbols to read for it.
5948 Everything is in the includer symtab. */
5949
5950 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5951 expansion of the includer psymtab. We use the dependencies[0] field to
5952 model the includer. But if we go the regular route of calling
5953 expand_psymtab here, and having expand_psymtab call expand_dependencies
5954 to expand the includer, we'll only use expand_psymtab on the includer
5955 (making it a non-toplevel psymtab), while if we expand the includer via
5956 another path, we'll use read_symtab (making it a toplevel psymtab).
5957 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5958 psymtab, and trigger read_symtab on the includer here directly. */
5959 includer ()->read_symtab (objfile);
5960 }
5961
5962 void expand_psymtab (struct objfile *objfile) override
5963 {
5964 /* This is not called by read_symtab, and should not be called by any
5965 expand_dependencies. */
5966 gdb_assert (false);
5967 }
5968
5969 bool readin_p (struct objfile *objfile) const override
5970 {
5971 return includer ()->readin_p (objfile);
5972 }
5973
5974 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5975 {
5976 return nullptr;
5977 }
5978
5979 private:
5980 partial_symtab *includer () const
5981 {
5982 /* An include psymtab has exactly one dependency: the psymtab that
5983 includes it. */
5984 gdb_assert (this->number_of_dependencies == 1);
5985 return this->dependencies[0];
5986 }
5987 };
5988
5989 /* Allocate a new partial symtab for file named NAME and mark this new
5990 partial symtab as being an include of PST. */
5991
5992 static void
5993 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5994 const char *name,
5995 dwarf2_psymtab *pst,
5996 psymtab_storage *partial_symtabs,
5997 objfile_per_bfd_storage *objfile_per_bfd)
5998 {
5999 dwarf2_include_psymtab *subpst
6000 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
6001
6002 if (!IS_ABSOLUTE_PATH (subpst->filename))
6003 subpst->dirname = pst->dirname;
6004
6005 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
6006 subpst->dependencies[0] = pst;
6007 subpst->number_of_dependencies = 1;
6008 }
6009
6010 /* Read the Line Number Program data and extract the list of files
6011 included by the source file represented by PST. Build an include
6012 partial symtab for each of these included files. */
6013
6014 static void
6015 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6016 struct die_info *die,
6017 dwarf2_psymtab *pst)
6018 {
6019 line_header_up lh;
6020 struct attribute *attr;
6021
6022 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6023 if (attr != nullptr && attr->form_is_unsigned ())
6024 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6025 if (lh == NULL)
6026 return; /* No linetable, so no includes. */
6027
6028 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6029 that we pass in the raw text_low here; that is ok because we're
6030 only decoding the line table to make include partial symtabs, and
6031 so the addresses aren't really used. */
6032 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6033 pst->raw_text_low (), 1);
6034 }
6035
6036 static hashval_t
6037 hash_signatured_type (const void *item)
6038 {
6039 const struct signatured_type *sig_type
6040 = (const struct signatured_type *) item;
6041
6042 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6043 return sig_type->signature;
6044 }
6045
6046 static int
6047 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6048 {
6049 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6050 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6051
6052 return lhs->signature == rhs->signature;
6053 }
6054
6055 /* Allocate a hash table for signatured types. */
6056
6057 static htab_up
6058 allocate_signatured_type_table ()
6059 {
6060 return htab_up (htab_create_alloc (41,
6061 hash_signatured_type,
6062 eq_signatured_type,
6063 NULL, xcalloc, xfree));
6064 }
6065
6066 /* A helper function to add a signatured type CU to a table. */
6067
6068 static int
6069 add_signatured_type_cu_to_table (void **slot, void *datum)
6070 {
6071 struct signatured_type *sigt = (struct signatured_type *) *slot;
6072 std::vector<signatured_type *> *all_type_units
6073 = (std::vector<signatured_type *> *) datum;
6074
6075 all_type_units->push_back (sigt);
6076
6077 return 1;
6078 }
6079
6080 /* A helper for create_debug_types_hash_table. Read types from SECTION
6081 and fill them into TYPES_HTAB. It will process only type units,
6082 therefore DW_UT_type. */
6083
6084 static void
6085 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6086 struct dwo_file *dwo_file,
6087 dwarf2_section_info *section, htab_up &types_htab,
6088 rcuh_kind section_kind)
6089 {
6090 struct objfile *objfile = per_objfile->objfile;
6091 struct dwarf2_section_info *abbrev_section;
6092 bfd *abfd;
6093 const gdb_byte *info_ptr, *end_ptr;
6094
6095 abbrev_section = (dwo_file != NULL
6096 ? &dwo_file->sections.abbrev
6097 : &per_objfile->per_bfd->abbrev);
6098
6099 dwarf_read_debug_printf ("Reading %s for %s",
6100 section->get_name (),
6101 abbrev_section->get_file_name ());
6102
6103 section->read (objfile);
6104 info_ptr = section->buffer;
6105
6106 if (info_ptr == NULL)
6107 return;
6108
6109 /* We can't set abfd until now because the section may be empty or
6110 not present, in which case the bfd is unknown. */
6111 abfd = section->get_bfd_owner ();
6112
6113 /* We don't use cutu_reader here because we don't need to read
6114 any dies: the signature is in the header. */
6115
6116 end_ptr = info_ptr + section->size;
6117 while (info_ptr < end_ptr)
6118 {
6119 struct signatured_type *sig_type;
6120 struct dwo_unit *dwo_tu;
6121 void **slot;
6122 const gdb_byte *ptr = info_ptr;
6123 struct comp_unit_head header;
6124 unsigned int length;
6125
6126 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6127
6128 /* Initialize it due to a false compiler warning. */
6129 header.signature = -1;
6130 header.type_cu_offset_in_tu = (cu_offset) -1;
6131
6132 /* We need to read the type's signature in order to build the hash
6133 table, but we don't need anything else just yet. */
6134
6135 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6136 abbrev_section, ptr, section_kind);
6137
6138 length = header.get_length ();
6139
6140 /* Skip dummy type units. */
6141 if (ptr >= info_ptr + length
6142 || peek_abbrev_code (abfd, ptr) == 0
6143 || (header.unit_type != DW_UT_type
6144 && header.unit_type != DW_UT_split_type))
6145 {
6146 info_ptr += length;
6147 continue;
6148 }
6149
6150 if (types_htab == NULL)
6151 {
6152 if (dwo_file)
6153 types_htab = allocate_dwo_unit_table ();
6154 else
6155 types_htab = allocate_signatured_type_table ();
6156 }
6157
6158 if (dwo_file)
6159 {
6160 sig_type = NULL;
6161 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6162 dwo_tu->dwo_file = dwo_file;
6163 dwo_tu->signature = header.signature;
6164 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6165 dwo_tu->section = section;
6166 dwo_tu->sect_off = sect_off;
6167 dwo_tu->length = length;
6168 }
6169 else
6170 {
6171 /* N.B.: type_offset is not usable if this type uses a DWO file.
6172 The real type_offset is in the DWO file. */
6173 dwo_tu = NULL;
6174 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6175 sig_type->signature = header.signature;
6176 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6177 sig_type->per_cu.is_debug_types = 1;
6178 sig_type->per_cu.section = section;
6179 sig_type->per_cu.sect_off = sect_off;
6180 sig_type->per_cu.length = length;
6181 }
6182
6183 slot = htab_find_slot (types_htab.get (),
6184 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6185 INSERT);
6186 gdb_assert (slot != NULL);
6187 if (*slot != NULL)
6188 {
6189 sect_offset dup_sect_off;
6190
6191 if (dwo_file)
6192 {
6193 const struct dwo_unit *dup_tu
6194 = (const struct dwo_unit *) *slot;
6195
6196 dup_sect_off = dup_tu->sect_off;
6197 }
6198 else
6199 {
6200 const struct signatured_type *dup_tu
6201 = (const struct signatured_type *) *slot;
6202
6203 dup_sect_off = dup_tu->per_cu.sect_off;
6204 }
6205
6206 complaint (_("debug type entry at offset %s is duplicate to"
6207 " the entry at offset %s, signature %s"),
6208 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6209 hex_string (header.signature));
6210 }
6211 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6212
6213 dwarf_read_debug_printf_v (" offset %s, signature %s",
6214 sect_offset_str (sect_off),
6215 hex_string (header.signature));
6216
6217 info_ptr += length;
6218 }
6219 }
6220
6221 /* Create the hash table of all entries in the .debug_types
6222 (or .debug_types.dwo) section(s).
6223 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6224 otherwise it is NULL.
6225
6226 The result is a pointer to the hash table or NULL if there are no types.
6227
6228 Note: This function processes DWO files only, not DWP files. */
6229
6230 static void
6231 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6232 struct dwo_file *dwo_file,
6233 gdb::array_view<dwarf2_section_info> type_sections,
6234 htab_up &types_htab)
6235 {
6236 for (dwarf2_section_info &section : type_sections)
6237 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6238 rcuh_kind::TYPE);
6239 }
6240
6241 /* Create the hash table of all entries in the .debug_types section,
6242 and initialize all_type_units.
6243 The result is zero if there is an error (e.g. missing .debug_types section),
6244 otherwise non-zero. */
6245
6246 static int
6247 create_all_type_units (dwarf2_per_objfile *per_objfile)
6248 {
6249 htab_up types_htab;
6250
6251 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6252 types_htab, rcuh_kind::COMPILE);
6253 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6254 types_htab);
6255 if (types_htab == NULL)
6256 {
6257 per_objfile->per_bfd->signatured_types = NULL;
6258 return 0;
6259 }
6260
6261 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6262
6263 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6264 per_objfile->per_bfd->all_type_units.reserve
6265 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6266
6267 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6268 add_signatured_type_cu_to_table,
6269 &per_objfile->per_bfd->all_type_units);
6270
6271 return 1;
6272 }
6273
6274 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6275 If SLOT is non-NULL, it is the entry to use in the hash table.
6276 Otherwise we find one. */
6277
6278 static struct signatured_type *
6279 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6280 {
6281 if (per_objfile->per_bfd->all_type_units.size ()
6282 == per_objfile->per_bfd->all_type_units.capacity ())
6283 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6284
6285 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6286
6287 per_objfile->resize_symtabs ();
6288
6289 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6290 sig_type->signature = sig;
6291 sig_type->per_cu.is_debug_types = 1;
6292 if (per_objfile->per_bfd->using_index)
6293 {
6294 sig_type->per_cu.v.quick =
6295 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6296 struct dwarf2_per_cu_quick_data);
6297 }
6298
6299 if (slot == NULL)
6300 {
6301 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6302 sig_type, INSERT);
6303 }
6304 gdb_assert (*slot == NULL);
6305 *slot = sig_type;
6306 /* The rest of sig_type must be filled in by the caller. */
6307 return sig_type;
6308 }
6309
6310 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6311 Fill in SIG_ENTRY with DWO_ENTRY. */
6312
6313 static void
6314 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6315 struct signatured_type *sig_entry,
6316 struct dwo_unit *dwo_entry)
6317 {
6318 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6319
6320 /* Make sure we're not clobbering something we don't expect to. */
6321 gdb_assert (! sig_entry->per_cu.queued);
6322 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6323 if (per_bfd->using_index)
6324 {
6325 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6326 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6327 }
6328 else
6329 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6330 gdb_assert (sig_entry->signature == dwo_entry->signature);
6331 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6332 gdb_assert (sig_entry->type_unit_group == NULL);
6333 gdb_assert (sig_entry->dwo_unit == NULL);
6334
6335 sig_entry->per_cu.section = dwo_entry->section;
6336 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6337 sig_entry->per_cu.length = dwo_entry->length;
6338 sig_entry->per_cu.reading_dwo_directly = 1;
6339 sig_entry->per_cu.per_bfd = per_bfd;
6340 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6341 sig_entry->dwo_unit = dwo_entry;
6342 }
6343
6344 /* Subroutine of lookup_signatured_type.
6345 If we haven't read the TU yet, create the signatured_type data structure
6346 for a TU to be read in directly from a DWO file, bypassing the stub.
6347 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6348 using .gdb_index, then when reading a CU we want to stay in the DWO file
6349 containing that CU. Otherwise we could end up reading several other DWO
6350 files (due to comdat folding) to process the transitive closure of all the
6351 mentioned TUs, and that can be slow. The current DWO file will have every
6352 type signature that it needs.
6353 We only do this for .gdb_index because in the psymtab case we already have
6354 to read all the DWOs to build the type unit groups. */
6355
6356 static struct signatured_type *
6357 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6358 {
6359 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6360 struct dwo_file *dwo_file;
6361 struct dwo_unit find_dwo_entry, *dwo_entry;
6362 struct signatured_type find_sig_entry, *sig_entry;
6363 void **slot;
6364
6365 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6366
6367 /* If TU skeletons have been removed then we may not have read in any
6368 TUs yet. */
6369 if (per_objfile->per_bfd->signatured_types == NULL)
6370 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6371
6372 /* We only ever need to read in one copy of a signatured type.
6373 Use the global signatured_types array to do our own comdat-folding
6374 of types. If this is the first time we're reading this TU, and
6375 the TU has an entry in .gdb_index, replace the recorded data from
6376 .gdb_index with this TU. */
6377
6378 find_sig_entry.signature = sig;
6379 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6380 &find_sig_entry, INSERT);
6381 sig_entry = (struct signatured_type *) *slot;
6382
6383 /* We can get here with the TU already read, *or* in the process of being
6384 read. Don't reassign the global entry to point to this DWO if that's
6385 the case. Also note that if the TU is already being read, it may not
6386 have come from a DWO, the program may be a mix of Fission-compiled
6387 code and non-Fission-compiled code. */
6388
6389 /* Have we already tried to read this TU?
6390 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6391 needn't exist in the global table yet). */
6392 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6393 return sig_entry;
6394
6395 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6396 dwo_unit of the TU itself. */
6397 dwo_file = cu->dwo_unit->dwo_file;
6398
6399 /* Ok, this is the first time we're reading this TU. */
6400 if (dwo_file->tus == NULL)
6401 return NULL;
6402 find_dwo_entry.signature = sig;
6403 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6404 &find_dwo_entry);
6405 if (dwo_entry == NULL)
6406 return NULL;
6407
6408 /* If the global table doesn't have an entry for this TU, add one. */
6409 if (sig_entry == NULL)
6410 sig_entry = add_type_unit (per_objfile, sig, slot);
6411
6412 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6413 sig_entry->per_cu.tu_read = 1;
6414 return sig_entry;
6415 }
6416
6417 /* Subroutine of lookup_signatured_type.
6418 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6419 then try the DWP file. If the TU stub (skeleton) has been removed then
6420 it won't be in .gdb_index. */
6421
6422 static struct signatured_type *
6423 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6424 {
6425 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6426 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6427 struct dwo_unit *dwo_entry;
6428 struct signatured_type find_sig_entry, *sig_entry;
6429 void **slot;
6430
6431 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6432 gdb_assert (dwp_file != NULL);
6433
6434 /* If TU skeletons have been removed then we may not have read in any
6435 TUs yet. */
6436 if (per_objfile->per_bfd->signatured_types == NULL)
6437 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6438
6439 find_sig_entry.signature = sig;
6440 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6441 &find_sig_entry, INSERT);
6442 sig_entry = (struct signatured_type *) *slot;
6443
6444 /* Have we already tried to read this TU?
6445 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6446 needn't exist in the global table yet). */
6447 if (sig_entry != NULL)
6448 return sig_entry;
6449
6450 if (dwp_file->tus == NULL)
6451 return NULL;
6452 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6453 1 /* is_debug_types */);
6454 if (dwo_entry == NULL)
6455 return NULL;
6456
6457 sig_entry = add_type_unit (per_objfile, sig, slot);
6458 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6459
6460 return sig_entry;
6461 }
6462
6463 /* Lookup a signature based type for DW_FORM_ref_sig8.
6464 Returns NULL if signature SIG is not present in the table.
6465 It is up to the caller to complain about this. */
6466
6467 static struct signatured_type *
6468 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6469 {
6470 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6471
6472 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6473 {
6474 /* We're in a DWO/DWP file, and we're using .gdb_index.
6475 These cases require special processing. */
6476 if (get_dwp_file (per_objfile) == NULL)
6477 return lookup_dwo_signatured_type (cu, sig);
6478 else
6479 return lookup_dwp_signatured_type (cu, sig);
6480 }
6481 else
6482 {
6483 struct signatured_type find_entry, *entry;
6484
6485 if (per_objfile->per_bfd->signatured_types == NULL)
6486 return NULL;
6487 find_entry.signature = sig;
6488 entry = ((struct signatured_type *)
6489 htab_find (per_objfile->per_bfd->signatured_types.get (),
6490 &find_entry));
6491 return entry;
6492 }
6493 }
6494
6495 /* Low level DIE reading support. */
6496
6497 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6498
6499 static void
6500 init_cu_die_reader (struct die_reader_specs *reader,
6501 struct dwarf2_cu *cu,
6502 struct dwarf2_section_info *section,
6503 struct dwo_file *dwo_file,
6504 struct abbrev_table *abbrev_table)
6505 {
6506 gdb_assert (section->readin && section->buffer != NULL);
6507 reader->abfd = section->get_bfd_owner ();
6508 reader->cu = cu;
6509 reader->dwo_file = dwo_file;
6510 reader->die_section = section;
6511 reader->buffer = section->buffer;
6512 reader->buffer_end = section->buffer + section->size;
6513 reader->abbrev_table = abbrev_table;
6514 }
6515
6516 /* Subroutine of cutu_reader to simplify it.
6517 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6518 There's just a lot of work to do, and cutu_reader is big enough
6519 already.
6520
6521 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6522 from it to the DIE in the DWO. If NULL we are skipping the stub.
6523 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6524 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6525 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6526 STUB_COMP_DIR may be non-NULL.
6527 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6528 are filled in with the info of the DIE from the DWO file.
6529 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6530 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6531 kept around for at least as long as *RESULT_READER.
6532
6533 The result is non-zero if a valid (non-dummy) DIE was found. */
6534
6535 static int
6536 read_cutu_die_from_dwo (dwarf2_cu *cu,
6537 struct dwo_unit *dwo_unit,
6538 struct die_info *stub_comp_unit_die,
6539 const char *stub_comp_dir,
6540 struct die_reader_specs *result_reader,
6541 const gdb_byte **result_info_ptr,
6542 struct die_info **result_comp_unit_die,
6543 abbrev_table_up *result_dwo_abbrev_table)
6544 {
6545 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6546 dwarf2_per_cu_data *per_cu = cu->per_cu;
6547 struct objfile *objfile = per_objfile->objfile;
6548 bfd *abfd;
6549 const gdb_byte *begin_info_ptr, *info_ptr;
6550 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6551 int i,num_extra_attrs;
6552 struct dwarf2_section_info *dwo_abbrev_section;
6553 struct die_info *comp_unit_die;
6554
6555 /* At most one of these may be provided. */
6556 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6557
6558 /* These attributes aren't processed until later:
6559 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6560 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6561 referenced later. However, these attributes are found in the stub
6562 which we won't have later. In order to not impose this complication
6563 on the rest of the code, we read them here and copy them to the
6564 DWO CU/TU die. */
6565
6566 stmt_list = NULL;
6567 low_pc = NULL;
6568 high_pc = NULL;
6569 ranges = NULL;
6570 comp_dir = NULL;
6571
6572 if (stub_comp_unit_die != NULL)
6573 {
6574 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6575 DWO file. */
6576 if (!per_cu->is_debug_types)
6577 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6578 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6579 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6580 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6581 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6582
6583 cu->addr_base = stub_comp_unit_die->addr_base ();
6584
6585 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6586 We need the value before we can process DW_AT_ranges values from the
6587 DWO. */
6588 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6589
6590 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6591 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6592 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6593 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6594 section. */
6595 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6596 }
6597 else if (stub_comp_dir != NULL)
6598 {
6599 /* Reconstruct the comp_dir attribute to simplify the code below. */
6600 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6601 comp_dir->name = DW_AT_comp_dir;
6602 comp_dir->form = DW_FORM_string;
6603 comp_dir->set_string_noncanonical (stub_comp_dir);
6604 }
6605
6606 /* Set up for reading the DWO CU/TU. */
6607 cu->dwo_unit = dwo_unit;
6608 dwarf2_section_info *section = dwo_unit->section;
6609 section->read (objfile);
6610 abfd = section->get_bfd_owner ();
6611 begin_info_ptr = info_ptr = (section->buffer
6612 + to_underlying (dwo_unit->sect_off));
6613 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6614
6615 if (per_cu->is_debug_types)
6616 {
6617 signatured_type *sig_type = (struct signatured_type *) per_cu;
6618
6619 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6620 section, dwo_abbrev_section,
6621 info_ptr, rcuh_kind::TYPE);
6622 /* This is not an assert because it can be caused by bad debug info. */
6623 if (sig_type->signature != cu->header.signature)
6624 {
6625 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6626 " TU at offset %s [in module %s]"),
6627 hex_string (sig_type->signature),
6628 hex_string (cu->header.signature),
6629 sect_offset_str (dwo_unit->sect_off),
6630 bfd_get_filename (abfd));
6631 }
6632 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6633 /* For DWOs coming from DWP files, we don't know the CU length
6634 nor the type's offset in the TU until now. */
6635 dwo_unit->length = cu->header.get_length ();
6636 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6637
6638 /* Establish the type offset that can be used to lookup the type.
6639 For DWO files, we don't know it until now. */
6640 sig_type->type_offset_in_section
6641 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6642 }
6643 else
6644 {
6645 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6646 section, dwo_abbrev_section,
6647 info_ptr, rcuh_kind::COMPILE);
6648 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6649 /* For DWOs coming from DWP files, we don't know the CU length
6650 until now. */
6651 dwo_unit->length = cu->header.get_length ();
6652 }
6653
6654 dwo_abbrev_section->read (objfile);
6655 *result_dwo_abbrev_table
6656 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6657 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6658 result_dwo_abbrev_table->get ());
6659
6660 /* Read in the die, but leave space to copy over the attributes
6661 from the stub. This has the benefit of simplifying the rest of
6662 the code - all the work to maintain the illusion of a single
6663 DW_TAG_{compile,type}_unit DIE is done here. */
6664 num_extra_attrs = ((stmt_list != NULL)
6665 + (low_pc != NULL)
6666 + (high_pc != NULL)
6667 + (ranges != NULL)
6668 + (comp_dir != NULL));
6669 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6670 num_extra_attrs);
6671
6672 /* Copy over the attributes from the stub to the DIE we just read in. */
6673 comp_unit_die = *result_comp_unit_die;
6674 i = comp_unit_die->num_attrs;
6675 if (stmt_list != NULL)
6676 comp_unit_die->attrs[i++] = *stmt_list;
6677 if (low_pc != NULL)
6678 comp_unit_die->attrs[i++] = *low_pc;
6679 if (high_pc != NULL)
6680 comp_unit_die->attrs[i++] = *high_pc;
6681 if (ranges != NULL)
6682 comp_unit_die->attrs[i++] = *ranges;
6683 if (comp_dir != NULL)
6684 comp_unit_die->attrs[i++] = *comp_dir;
6685 comp_unit_die->num_attrs += num_extra_attrs;
6686
6687 if (dwarf_die_debug)
6688 {
6689 fprintf_unfiltered (gdb_stdlog,
6690 "Read die from %s@0x%x of %s:\n",
6691 section->get_name (),
6692 (unsigned) (begin_info_ptr - section->buffer),
6693 bfd_get_filename (abfd));
6694 dump_die (comp_unit_die, dwarf_die_debug);
6695 }
6696
6697 /* Skip dummy compilation units. */
6698 if (info_ptr >= begin_info_ptr + dwo_unit->length
6699 || peek_abbrev_code (abfd, info_ptr) == 0)
6700 return 0;
6701
6702 *result_info_ptr = info_ptr;
6703 return 1;
6704 }
6705
6706 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6707 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6708 signature is part of the header. */
6709 static gdb::optional<ULONGEST>
6710 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6711 {
6712 if (cu->header.version >= 5)
6713 return cu->header.signature;
6714 struct attribute *attr;
6715 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6716 if (attr == nullptr || !attr->form_is_unsigned ())
6717 return gdb::optional<ULONGEST> ();
6718 return attr->as_unsigned ();
6719 }
6720
6721 /* Subroutine of cutu_reader to simplify it.
6722 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6723 Returns NULL if the specified DWO unit cannot be found. */
6724
6725 static struct dwo_unit *
6726 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6727 {
6728 dwarf2_per_cu_data *per_cu = cu->per_cu;
6729 struct dwo_unit *dwo_unit;
6730 const char *comp_dir;
6731
6732 gdb_assert (cu != NULL);
6733
6734 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6735 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6736 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6737
6738 if (per_cu->is_debug_types)
6739 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6740 else
6741 {
6742 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6743
6744 if (!signature.has_value ())
6745 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6746 " [in module %s]"),
6747 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6748
6749 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6750 }
6751
6752 return dwo_unit;
6753 }
6754
6755 /* Subroutine of cutu_reader to simplify it.
6756 See it for a description of the parameters.
6757 Read a TU directly from a DWO file, bypassing the stub. */
6758
6759 void
6760 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6761 dwarf2_per_objfile *per_objfile,
6762 dwarf2_cu *existing_cu)
6763 {
6764 struct signatured_type *sig_type;
6765
6766 /* Verify we can do the following downcast, and that we have the
6767 data we need. */
6768 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6769 sig_type = (struct signatured_type *) this_cu;
6770 gdb_assert (sig_type->dwo_unit != NULL);
6771
6772 dwarf2_cu *cu;
6773
6774 if (existing_cu != nullptr)
6775 {
6776 cu = existing_cu;
6777 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6778 /* There's no need to do the rereading_dwo_cu handling that
6779 cutu_reader does since we don't read the stub. */
6780 }
6781 else
6782 {
6783 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6784 in per_objfile yet. */
6785 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6786 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6787 cu = m_new_cu.get ();
6788 }
6789
6790 /* A future optimization, if needed, would be to use an existing
6791 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6792 could share abbrev tables. */
6793
6794 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6795 NULL /* stub_comp_unit_die */,
6796 sig_type->dwo_unit->dwo_file->comp_dir,
6797 this, &info_ptr,
6798 &comp_unit_die,
6799 &m_dwo_abbrev_table) == 0)
6800 {
6801 /* Dummy die. */
6802 dummy_p = true;
6803 }
6804 }
6805
6806 /* Initialize a CU (or TU) and read its DIEs.
6807 If the CU defers to a DWO file, read the DWO file as well.
6808
6809 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6810 Otherwise the table specified in the comp unit header is read in and used.
6811 This is an optimization for when we already have the abbrev table.
6812
6813 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6814 allocated. */
6815
6816 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6817 dwarf2_per_objfile *per_objfile,
6818 struct abbrev_table *abbrev_table,
6819 dwarf2_cu *existing_cu,
6820 bool skip_partial)
6821 : die_reader_specs {},
6822 m_this_cu (this_cu)
6823 {
6824 struct objfile *objfile = per_objfile->objfile;
6825 struct dwarf2_section_info *section = this_cu->section;
6826 bfd *abfd = section->get_bfd_owner ();
6827 const gdb_byte *begin_info_ptr;
6828 struct signatured_type *sig_type = NULL;
6829 struct dwarf2_section_info *abbrev_section;
6830 /* Non-zero if CU currently points to a DWO file and we need to
6831 reread it. When this happens we need to reread the skeleton die
6832 before we can reread the DWO file (this only applies to CUs, not TUs). */
6833 int rereading_dwo_cu = 0;
6834
6835 if (dwarf_die_debug)
6836 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6837 this_cu->is_debug_types ? "type" : "comp",
6838 sect_offset_str (this_cu->sect_off));
6839
6840 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6841 file (instead of going through the stub), short-circuit all of this. */
6842 if (this_cu->reading_dwo_directly)
6843 {
6844 /* Narrow down the scope of possibilities to have to understand. */
6845 gdb_assert (this_cu->is_debug_types);
6846 gdb_assert (abbrev_table == NULL);
6847 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6848 return;
6849 }
6850
6851 /* This is cheap if the section is already read in. */
6852 section->read (objfile);
6853
6854 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6855
6856 abbrev_section = get_abbrev_section_for_cu (this_cu);
6857
6858 dwarf2_cu *cu;
6859
6860 if (existing_cu != nullptr)
6861 {
6862 cu = existing_cu;
6863 /* If this CU is from a DWO file we need to start over, we need to
6864 refetch the attributes from the skeleton CU.
6865 This could be optimized by retrieving those attributes from when we
6866 were here the first time: the previous comp_unit_die was stored in
6867 comp_unit_obstack. But there's no data yet that we need this
6868 optimization. */
6869 if (cu->dwo_unit != NULL)
6870 rereading_dwo_cu = 1;
6871 }
6872 else
6873 {
6874 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6875 in per_objfile yet. */
6876 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6877 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6878 cu = m_new_cu.get ();
6879 }
6880
6881 /* Get the header. */
6882 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6883 {
6884 /* We already have the header, there's no need to read it in again. */
6885 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6886 }
6887 else
6888 {
6889 if (this_cu->is_debug_types)
6890 {
6891 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6892 section, abbrev_section,
6893 info_ptr, rcuh_kind::TYPE);
6894
6895 /* Since per_cu is the first member of struct signatured_type,
6896 we can go from a pointer to one to a pointer to the other. */
6897 sig_type = (struct signatured_type *) this_cu;
6898 gdb_assert (sig_type->signature == cu->header.signature);
6899 gdb_assert (sig_type->type_offset_in_tu
6900 == cu->header.type_cu_offset_in_tu);
6901 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6902
6903 /* LENGTH has not been set yet for type units if we're
6904 using .gdb_index. */
6905 this_cu->length = cu->header.get_length ();
6906
6907 /* Establish the type offset that can be used to lookup the type. */
6908 sig_type->type_offset_in_section =
6909 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6910
6911 this_cu->dwarf_version = cu->header.version;
6912 }
6913 else
6914 {
6915 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6916 section, abbrev_section,
6917 info_ptr,
6918 rcuh_kind::COMPILE);
6919
6920 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6921 if (this_cu->length == 0)
6922 this_cu->length = cu->header.get_length ();
6923 else
6924 gdb_assert (this_cu->length == cu->header.get_length ());
6925 this_cu->dwarf_version = cu->header.version;
6926 }
6927 }
6928
6929 /* Skip dummy compilation units. */
6930 if (info_ptr >= begin_info_ptr + this_cu->length
6931 || peek_abbrev_code (abfd, info_ptr) == 0)
6932 {
6933 dummy_p = true;
6934 return;
6935 }
6936
6937 /* If we don't have them yet, read the abbrevs for this compilation unit.
6938 And if we need to read them now, make sure they're freed when we're
6939 done. */
6940 if (abbrev_table != NULL)
6941 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6942 else
6943 {
6944 abbrev_section->read (objfile);
6945 m_abbrev_table_holder
6946 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6947 abbrev_table = m_abbrev_table_holder.get ();
6948 }
6949
6950 /* Read the top level CU/TU die. */
6951 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6952 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6953
6954 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6955 {
6956 dummy_p = true;
6957 return;
6958 }
6959
6960 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6961 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6962 table from the DWO file and pass the ownership over to us. It will be
6963 referenced from READER, so we must make sure to free it after we're done
6964 with READER.
6965
6966 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6967 DWO CU, that this test will fail (the attribute will not be present). */
6968 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6969 if (dwo_name != nullptr)
6970 {
6971 struct dwo_unit *dwo_unit;
6972 struct die_info *dwo_comp_unit_die;
6973
6974 if (comp_unit_die->has_children)
6975 {
6976 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6977 " has children (offset %s) [in module %s]"),
6978 sect_offset_str (this_cu->sect_off),
6979 bfd_get_filename (abfd));
6980 }
6981 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6982 if (dwo_unit != NULL)
6983 {
6984 if (read_cutu_die_from_dwo (cu, dwo_unit,
6985 comp_unit_die, NULL,
6986 this, &info_ptr,
6987 &dwo_comp_unit_die,
6988 &m_dwo_abbrev_table) == 0)
6989 {
6990 /* Dummy die. */
6991 dummy_p = true;
6992 return;
6993 }
6994 comp_unit_die = dwo_comp_unit_die;
6995 }
6996 else
6997 {
6998 /* Yikes, we couldn't find the rest of the DIE, we only have
6999 the stub. A complaint has already been logged. There's
7000 not much more we can do except pass on the stub DIE to
7001 die_reader_func. We don't want to throw an error on bad
7002 debug info. */
7003 }
7004 }
7005 }
7006
7007 void
7008 cutu_reader::keep ()
7009 {
7010 /* Done, clean up. */
7011 gdb_assert (!dummy_p);
7012 if (m_new_cu != NULL)
7013 {
7014 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7015 now. */
7016 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7017 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7018 }
7019 }
7020
7021 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7022 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7023 assumed to have already done the lookup to find the DWO file).
7024
7025 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7026 THIS_CU->is_debug_types, but nothing else.
7027
7028 We fill in THIS_CU->length.
7029
7030 THIS_CU->cu is always freed when done.
7031 This is done in order to not leave THIS_CU->cu in a state where we have
7032 to care whether it refers to the "main" CU or the DWO CU.
7033
7034 When parent_cu is passed, it is used to provide a default value for
7035 str_offsets_base and addr_base from the parent. */
7036
7037 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7038 dwarf2_per_objfile *per_objfile,
7039 struct dwarf2_cu *parent_cu,
7040 struct dwo_file *dwo_file)
7041 : die_reader_specs {},
7042 m_this_cu (this_cu)
7043 {
7044 struct objfile *objfile = per_objfile->objfile;
7045 struct dwarf2_section_info *section = this_cu->section;
7046 bfd *abfd = section->get_bfd_owner ();
7047 struct dwarf2_section_info *abbrev_section;
7048 const gdb_byte *begin_info_ptr, *info_ptr;
7049
7050 if (dwarf_die_debug)
7051 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7052 this_cu->is_debug_types ? "type" : "comp",
7053 sect_offset_str (this_cu->sect_off));
7054
7055 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7056
7057 abbrev_section = (dwo_file != NULL
7058 ? &dwo_file->sections.abbrev
7059 : get_abbrev_section_for_cu (this_cu));
7060
7061 /* This is cheap if the section is already read in. */
7062 section->read (objfile);
7063
7064 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7065
7066 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7067 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7068 section, abbrev_section, info_ptr,
7069 (this_cu->is_debug_types
7070 ? rcuh_kind::TYPE
7071 : rcuh_kind::COMPILE));
7072
7073 if (parent_cu != nullptr)
7074 {
7075 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7076 m_new_cu->addr_base = parent_cu->addr_base;
7077 }
7078 this_cu->length = m_new_cu->header.get_length ();
7079
7080 /* Skip dummy compilation units. */
7081 if (info_ptr >= begin_info_ptr + this_cu->length
7082 || peek_abbrev_code (abfd, info_ptr) == 0)
7083 {
7084 dummy_p = true;
7085 return;
7086 }
7087
7088 abbrev_section->read (objfile);
7089 m_abbrev_table_holder
7090 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7091
7092 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7093 m_abbrev_table_holder.get ());
7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7095 }
7096
7097 \f
7098 /* Type Unit Groups.
7099
7100 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7101 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7102 so that all types coming from the same compilation (.o file) are grouped
7103 together. A future step could be to put the types in the same symtab as
7104 the CU the types ultimately came from. */
7105
7106 static hashval_t
7107 hash_type_unit_group (const void *item)
7108 {
7109 const struct type_unit_group *tu_group
7110 = (const struct type_unit_group *) item;
7111
7112 return hash_stmt_list_entry (&tu_group->hash);
7113 }
7114
7115 static int
7116 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7117 {
7118 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7119 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7120
7121 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7122 }
7123
7124 /* Allocate a hash table for type unit groups. */
7125
7126 static htab_up
7127 allocate_type_unit_groups_table ()
7128 {
7129 return htab_up (htab_create_alloc (3,
7130 hash_type_unit_group,
7131 eq_type_unit_group,
7132 NULL, xcalloc, xfree));
7133 }
7134
7135 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7136 partial symtabs. We combine several TUs per psymtab to not let the size
7137 of any one psymtab grow too big. */
7138 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7139 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7140
7141 /* Helper routine for get_type_unit_group.
7142 Create the type_unit_group object used to hold one or more TUs. */
7143
7144 static struct type_unit_group *
7145 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7146 {
7147 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7148 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7149 struct dwarf2_per_cu_data *per_cu;
7150 struct type_unit_group *tu_group;
7151
7152 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7153 per_cu = &tu_group->per_cu;
7154 per_cu->per_bfd = per_bfd;
7155
7156 if (per_bfd->using_index)
7157 {
7158 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7159 struct dwarf2_per_cu_quick_data);
7160 }
7161 else
7162 {
7163 unsigned int line_offset = to_underlying (line_offset_struct);
7164 dwarf2_psymtab *pst;
7165 std::string name;
7166
7167 /* Give the symtab a useful name for debug purposes. */
7168 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7169 name = string_printf ("<type_units_%d>",
7170 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7171 else
7172 name = string_printf ("<type_units_at_0x%x>", line_offset);
7173
7174 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7175 pst->anonymous = true;
7176 }
7177
7178 tu_group->hash.dwo_unit = cu->dwo_unit;
7179 tu_group->hash.line_sect_off = line_offset_struct;
7180
7181 return tu_group;
7182 }
7183
7184 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7185 STMT_LIST is a DW_AT_stmt_list attribute. */
7186
7187 static struct type_unit_group *
7188 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7189 {
7190 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7191 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7192 struct type_unit_group *tu_group;
7193 void **slot;
7194 unsigned int line_offset;
7195 struct type_unit_group type_unit_group_for_lookup;
7196
7197 if (per_objfile->per_bfd->type_unit_groups == NULL)
7198 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7199
7200 /* Do we need to create a new group, or can we use an existing one? */
7201
7202 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7203 {
7204 line_offset = stmt_list->as_unsigned ();
7205 ++tu_stats->nr_symtab_sharers;
7206 }
7207 else
7208 {
7209 /* Ugh, no stmt_list. Rare, but we have to handle it.
7210 We can do various things here like create one group per TU or
7211 spread them over multiple groups to split up the expansion work.
7212 To avoid worst case scenarios (too many groups or too large groups)
7213 we, umm, group them in bunches. */
7214 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7215 | (tu_stats->nr_stmt_less_type_units
7216 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7217 ++tu_stats->nr_stmt_less_type_units;
7218 }
7219
7220 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7221 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7222 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7223 &type_unit_group_for_lookup, INSERT);
7224 if (*slot != NULL)
7225 {
7226 tu_group = (struct type_unit_group *) *slot;
7227 gdb_assert (tu_group != NULL);
7228 }
7229 else
7230 {
7231 sect_offset line_offset_struct = (sect_offset) line_offset;
7232 tu_group = create_type_unit_group (cu, line_offset_struct);
7233 *slot = tu_group;
7234 ++tu_stats->nr_symtabs;
7235 }
7236
7237 return tu_group;
7238 }
7239 \f
7240 /* Partial symbol tables. */
7241
7242 /* Create a psymtab named NAME and assign it to PER_CU.
7243
7244 The caller must fill in the following details:
7245 dirname, textlow, texthigh. */
7246
7247 static dwarf2_psymtab *
7248 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7249 dwarf2_per_objfile *per_objfile,
7250 const char *name)
7251 {
7252 dwarf2_psymtab *pst
7253 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7254 per_objfile->objfile->per_bfd, per_cu);
7255
7256 pst->psymtabs_addrmap_supported = true;
7257
7258 /* This is the glue that links PST into GDB's symbol API. */
7259 per_cu->v.psymtab = pst;
7260
7261 return pst;
7262 }
7263
7264 /* DIE reader function for process_psymtab_comp_unit. */
7265
7266 static void
7267 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7268 const gdb_byte *info_ptr,
7269 struct die_info *comp_unit_die,
7270 enum language pretend_language)
7271 {
7272 struct dwarf2_cu *cu = reader->cu;
7273 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7274 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7275 struct objfile *objfile = per_objfile->objfile;
7276 struct gdbarch *gdbarch = objfile->arch ();
7277 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7278 CORE_ADDR baseaddr;
7279 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7280 dwarf2_psymtab *pst;
7281 enum pc_bounds_kind cu_bounds_kind;
7282 const char *filename;
7283
7284 gdb_assert (! per_cu->is_debug_types);
7285
7286 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7287
7288 /* Allocate a new partial symbol table structure. */
7289 gdb::unique_xmalloc_ptr<char> debug_filename;
7290 static const char artificial[] = "<artificial>";
7291 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7292 if (filename == NULL)
7293 filename = "";
7294 else if (strcmp (filename, artificial) == 0)
7295 {
7296 debug_filename.reset (concat (artificial, "@",
7297 sect_offset_str (per_cu->sect_off),
7298 (char *) NULL));
7299 filename = debug_filename.get ();
7300 }
7301
7302 pst = create_partial_symtab (per_cu, per_objfile, filename);
7303
7304 /* This must be done before calling dwarf2_build_include_psymtabs. */
7305 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7306
7307 baseaddr = objfile->text_section_offset ();
7308
7309 dwarf2_find_base_address (comp_unit_die, cu);
7310
7311 /* Possibly set the default values of LOWPC and HIGHPC from
7312 `DW_AT_ranges'. */
7313 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7314 &best_highpc, cu, pst);
7315 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7316 {
7317 CORE_ADDR low
7318 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7319 - baseaddr);
7320 CORE_ADDR high
7321 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7322 - baseaddr - 1);
7323 /* Store the contiguous range if it is not empty; it can be
7324 empty for CUs with no code. */
7325 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7326 low, high, pst);
7327 }
7328
7329 /* Check if comp unit has_children.
7330 If so, read the rest of the partial symbols from this comp unit.
7331 If not, there's no more debug_info for this comp unit. */
7332 if (comp_unit_die->has_children)
7333 {
7334 struct partial_die_info *first_die;
7335 CORE_ADDR lowpc, highpc;
7336
7337 lowpc = ((CORE_ADDR) -1);
7338 highpc = ((CORE_ADDR) 0);
7339
7340 first_die = load_partial_dies (reader, info_ptr, 1);
7341
7342 scan_partial_symbols (first_die, &lowpc, &highpc,
7343 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7344
7345 /* If we didn't find a lowpc, set it to highpc to avoid
7346 complaints from `maint check'. */
7347 if (lowpc == ((CORE_ADDR) -1))
7348 lowpc = highpc;
7349
7350 /* If the compilation unit didn't have an explicit address range,
7351 then use the information extracted from its child dies. */
7352 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7353 {
7354 best_lowpc = lowpc;
7355 best_highpc = highpc;
7356 }
7357 }
7358 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7359 best_lowpc + baseaddr)
7360 - baseaddr);
7361 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7362 best_highpc + baseaddr)
7363 - baseaddr);
7364
7365 pst->end ();
7366
7367 if (!cu->per_cu->imported_symtabs_empty ())
7368 {
7369 int i;
7370 int len = cu->per_cu->imported_symtabs_size ();
7371
7372 /* Fill in 'dependencies' here; we fill in 'users' in a
7373 post-pass. */
7374 pst->number_of_dependencies = len;
7375 pst->dependencies
7376 = per_bfd->partial_symtabs->allocate_dependencies (len);
7377 for (i = 0; i < len; ++i)
7378 {
7379 pst->dependencies[i]
7380 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7381 }
7382
7383 cu->per_cu->imported_symtabs_free ();
7384 }
7385
7386 /* Get the list of files included in the current compilation unit,
7387 and build a psymtab for each of them. */
7388 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7389
7390 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7391 ", %d global, %d static syms",
7392 per_cu->is_debug_types ? "type" : "comp",
7393 sect_offset_str (per_cu->sect_off),
7394 paddress (gdbarch, pst->text_low (objfile)),
7395 paddress (gdbarch, pst->text_high (objfile)),
7396 (int) pst->global_psymbols.size (),
7397 (int) pst->static_psymbols.size ());
7398 }
7399
7400 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7401 Process compilation unit THIS_CU for a psymtab. */
7402
7403 static void
7404 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7405 dwarf2_per_objfile *per_objfile,
7406 bool want_partial_unit,
7407 enum language pretend_language)
7408 {
7409 /* If this compilation unit was already read in, free the
7410 cached copy in order to read it in again. This is
7411 necessary because we skipped some symbols when we first
7412 read in the compilation unit (see load_partial_dies).
7413 This problem could be avoided, but the benefit is unclear. */
7414 per_objfile->remove_cu (this_cu);
7415
7416 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7417
7418 switch (reader.comp_unit_die->tag)
7419 {
7420 case DW_TAG_compile_unit:
7421 this_cu->unit_type = DW_UT_compile;
7422 break;
7423 case DW_TAG_partial_unit:
7424 this_cu->unit_type = DW_UT_partial;
7425 break;
7426 case DW_TAG_type_unit:
7427 this_cu->unit_type = DW_UT_type;
7428 break;
7429 default:
7430 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7431 dwarf_tag_name (reader.comp_unit_die->tag),
7432 sect_offset_str (reader.cu->per_cu->sect_off),
7433 objfile_name (per_objfile->objfile));
7434 }
7435
7436 if (reader.dummy_p)
7437 {
7438 /* Nothing. */
7439 }
7440 else if (this_cu->is_debug_types)
7441 build_type_psymtabs_reader (&reader, reader.info_ptr,
7442 reader.comp_unit_die);
7443 else if (want_partial_unit
7444 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7445 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7446 reader.comp_unit_die,
7447 pretend_language);
7448
7449 this_cu->lang = reader.cu->language;
7450
7451 /* Age out any secondary CUs. */
7452 per_objfile->age_comp_units ();
7453 }
7454
7455 /* Reader function for build_type_psymtabs. */
7456
7457 static void
7458 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7459 const gdb_byte *info_ptr,
7460 struct die_info *type_unit_die)
7461 {
7462 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7463 struct dwarf2_cu *cu = reader->cu;
7464 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7465 struct signatured_type *sig_type;
7466 struct type_unit_group *tu_group;
7467 struct attribute *attr;
7468 struct partial_die_info *first_die;
7469 CORE_ADDR lowpc, highpc;
7470 dwarf2_psymtab *pst;
7471
7472 gdb_assert (per_cu->is_debug_types);
7473 sig_type = (struct signatured_type *) per_cu;
7474
7475 if (! type_unit_die->has_children)
7476 return;
7477
7478 attr = type_unit_die->attr (DW_AT_stmt_list);
7479 tu_group = get_type_unit_group (cu, attr);
7480
7481 if (tu_group->tus == nullptr)
7482 tu_group->tus = new std::vector<signatured_type *>;
7483 tu_group->tus->push_back (sig_type);
7484
7485 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7486 pst = create_partial_symtab (per_cu, per_objfile, "");
7487 pst->anonymous = true;
7488
7489 first_die = load_partial_dies (reader, info_ptr, 1);
7490
7491 lowpc = (CORE_ADDR) -1;
7492 highpc = (CORE_ADDR) 0;
7493 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7494
7495 pst->end ();
7496 }
7497
7498 /* Struct used to sort TUs by their abbreviation table offset. */
7499
7500 struct tu_abbrev_offset
7501 {
7502 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7503 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7504 {}
7505
7506 signatured_type *sig_type;
7507 sect_offset abbrev_offset;
7508 };
7509
7510 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7511
7512 static bool
7513 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7514 const struct tu_abbrev_offset &b)
7515 {
7516 return a.abbrev_offset < b.abbrev_offset;
7517 }
7518
7519 /* Efficiently read all the type units.
7520 This does the bulk of the work for build_type_psymtabs.
7521
7522 The efficiency is because we sort TUs by the abbrev table they use and
7523 only read each abbrev table once. In one program there are 200K TUs
7524 sharing 8K abbrev tables.
7525
7526 The main purpose of this function is to support building the
7527 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7528 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7529 can collapse the search space by grouping them by stmt_list.
7530 The savings can be significant, in the same program from above the 200K TUs
7531 share 8K stmt_list tables.
7532
7533 FUNC is expected to call get_type_unit_group, which will create the
7534 struct type_unit_group if necessary and add it to
7535 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7536
7537 static void
7538 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7539 {
7540 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7541 abbrev_table_up abbrev_table;
7542 sect_offset abbrev_offset;
7543
7544 /* It's up to the caller to not call us multiple times. */
7545 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7546
7547 if (per_objfile->per_bfd->all_type_units.empty ())
7548 return;
7549
7550 /* TUs typically share abbrev tables, and there can be way more TUs than
7551 abbrev tables. Sort by abbrev table to reduce the number of times we
7552 read each abbrev table in.
7553 Alternatives are to punt or to maintain a cache of abbrev tables.
7554 This is simpler and efficient enough for now.
7555
7556 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7557 symtab to use). Typically TUs with the same abbrev offset have the same
7558 stmt_list value too so in practice this should work well.
7559
7560 The basic algorithm here is:
7561
7562 sort TUs by abbrev table
7563 for each TU with same abbrev table:
7564 read abbrev table if first user
7565 read TU top level DIE
7566 [IWBN if DWO skeletons had DW_AT_stmt_list]
7567 call FUNC */
7568
7569 dwarf_read_debug_printf ("Building type unit groups ...");
7570
7571 /* Sort in a separate table to maintain the order of all_type_units
7572 for .gdb_index: TU indices directly index all_type_units. */
7573 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7574 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7575
7576 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7577 sorted_by_abbrev.emplace_back
7578 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7579 sig_type->per_cu.sect_off));
7580
7581 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7582 sort_tu_by_abbrev_offset);
7583
7584 abbrev_offset = (sect_offset) ~(unsigned) 0;
7585
7586 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7587 {
7588 /* Switch to the next abbrev table if necessary. */
7589 if (abbrev_table == NULL
7590 || tu.abbrev_offset != abbrev_offset)
7591 {
7592 abbrev_offset = tu.abbrev_offset;
7593 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7594 abbrev_table =
7595 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7596 ++tu_stats->nr_uniq_abbrev_tables;
7597 }
7598
7599 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7600 abbrev_table.get (), nullptr, false);
7601 if (!reader.dummy_p)
7602 build_type_psymtabs_reader (&reader, reader.info_ptr,
7603 reader.comp_unit_die);
7604 }
7605 }
7606
7607 /* Print collected type unit statistics. */
7608
7609 static void
7610 print_tu_stats (dwarf2_per_objfile *per_objfile)
7611 {
7612 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7613
7614 dwarf_read_debug_printf ("Type unit statistics:");
7615 dwarf_read_debug_printf (" %zu TUs",
7616 per_objfile->per_bfd->all_type_units.size ());
7617 dwarf_read_debug_printf (" %d uniq abbrev tables",
7618 tu_stats->nr_uniq_abbrev_tables);
7619 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7620 tu_stats->nr_symtabs);
7621 dwarf_read_debug_printf (" %d symtab sharers",
7622 tu_stats->nr_symtab_sharers);
7623 dwarf_read_debug_printf (" %d type units without a stmt_list",
7624 tu_stats->nr_stmt_less_type_units);
7625 dwarf_read_debug_printf (" %d all_type_units reallocs",
7626 tu_stats->nr_all_type_units_reallocs);
7627 }
7628
7629 /* Traversal function for build_type_psymtabs. */
7630
7631 static int
7632 build_type_psymtab_dependencies (void **slot, void *info)
7633 {
7634 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7635 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7636 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7637 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7638 dwarf2_psymtab *pst = per_cu->v.psymtab;
7639 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7640 int i;
7641
7642 gdb_assert (len > 0);
7643 gdb_assert (per_cu->type_unit_group_p ());
7644
7645 pst->number_of_dependencies = len;
7646 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7647 for (i = 0; i < len; ++i)
7648 {
7649 struct signatured_type *iter = tu_group->tus->at (i);
7650 gdb_assert (iter->per_cu.is_debug_types);
7651 pst->dependencies[i] = iter->per_cu.v.psymtab;
7652 iter->type_unit_group = tu_group;
7653 }
7654
7655 delete tu_group->tus;
7656 tu_group->tus = nullptr;
7657
7658 return 1;
7659 }
7660
7661 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7662 Build partial symbol tables for the .debug_types comp-units. */
7663
7664 static void
7665 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7666 {
7667 if (! create_all_type_units (per_objfile))
7668 return;
7669
7670 build_type_psymtabs_1 (per_objfile);
7671 }
7672
7673 /* Traversal function for process_skeletonless_type_unit.
7674 Read a TU in a DWO file and build partial symbols for it. */
7675
7676 static int
7677 process_skeletonless_type_unit (void **slot, void *info)
7678 {
7679 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7680 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7681 struct signatured_type find_entry, *entry;
7682
7683 /* If this TU doesn't exist in the global table, add it and read it in. */
7684
7685 if (per_objfile->per_bfd->signatured_types == NULL)
7686 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7687
7688 find_entry.signature = dwo_unit->signature;
7689 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7690 &find_entry, INSERT);
7691 /* If we've already seen this type there's nothing to do. What's happening
7692 is we're doing our own version of comdat-folding here. */
7693 if (*slot != NULL)
7694 return 1;
7695
7696 /* This does the job that create_all_type_units would have done for
7697 this TU. */
7698 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7699 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7700 *slot = entry;
7701
7702 /* This does the job that build_type_psymtabs_1 would have done. */
7703 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7704 if (!reader.dummy_p)
7705 build_type_psymtabs_reader (&reader, reader.info_ptr,
7706 reader.comp_unit_die);
7707
7708 return 1;
7709 }
7710
7711 /* Traversal function for process_skeletonless_type_units. */
7712
7713 static int
7714 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7715 {
7716 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7717
7718 if (dwo_file->tus != NULL)
7719 htab_traverse_noresize (dwo_file->tus.get (),
7720 process_skeletonless_type_unit, info);
7721
7722 return 1;
7723 }
7724
7725 /* Scan all TUs of DWO files, verifying we've processed them.
7726 This is needed in case a TU was emitted without its skeleton.
7727 Note: This can't be done until we know what all the DWO files are. */
7728
7729 static void
7730 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7731 {
7732 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7733 if (get_dwp_file (per_objfile) == NULL
7734 && per_objfile->per_bfd->dwo_files != NULL)
7735 {
7736 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7737 process_dwo_file_for_skeletonless_type_units,
7738 per_objfile);
7739 }
7740 }
7741
7742 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7743
7744 static void
7745 set_partial_user (dwarf2_per_objfile *per_objfile)
7746 {
7747 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7748 {
7749 dwarf2_psymtab *pst = per_cu->v.psymtab;
7750
7751 if (pst == NULL)
7752 continue;
7753
7754 for (int j = 0; j < pst->number_of_dependencies; ++j)
7755 {
7756 /* Set the 'user' field only if it is not already set. */
7757 if (pst->dependencies[j]->user == NULL)
7758 pst->dependencies[j]->user = pst;
7759 }
7760 }
7761 }
7762
7763 /* Build the partial symbol table by doing a quick pass through the
7764 .debug_info and .debug_abbrev sections. */
7765
7766 static void
7767 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7768 {
7769 struct objfile *objfile = per_objfile->objfile;
7770 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7771
7772 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7773 objfile_name (objfile));
7774
7775 scoped_restore restore_reading_psyms
7776 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7777
7778 per_bfd->info.read (objfile);
7779
7780 /* Any cached compilation units will be linked by the per-objfile
7781 read_in_chain. Make sure to free them when we're done. */
7782 free_cached_comp_units freer (per_objfile);
7783
7784 build_type_psymtabs (per_objfile);
7785
7786 create_all_comp_units (per_objfile);
7787
7788 /* Create a temporary address map on a temporary obstack. We later
7789 copy this to the final obstack. */
7790 auto_obstack temp_obstack;
7791
7792 scoped_restore save_psymtabs_addrmap
7793 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7794 addrmap_create_mutable (&temp_obstack));
7795
7796 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
7797 {
7798 if (per_cu->v.psymtab != NULL)
7799 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7800 continue;
7801 process_psymtab_comp_unit (per_cu, per_objfile, false,
7802 language_minimal);
7803 }
7804
7805 /* This has to wait until we read the CUs, we need the list of DWOs. */
7806 process_skeletonless_type_units (per_objfile);
7807
7808 /* Now that all TUs have been processed we can fill in the dependencies. */
7809 if (per_bfd->type_unit_groups != NULL)
7810 {
7811 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7812 build_type_psymtab_dependencies, per_objfile);
7813 }
7814
7815 if (dwarf_read_debug > 0)
7816 print_tu_stats (per_objfile);
7817
7818 set_partial_user (per_objfile);
7819
7820 per_bfd->partial_symtabs->psymtabs_addrmap
7821 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7822 per_bfd->partial_symtabs->obstack ());
7823 /* At this point we want to keep the address map. */
7824 save_psymtabs_addrmap.release ();
7825
7826 dwarf_read_debug_printf ("Done building psymtabs of %s",
7827 objfile_name (objfile));
7828 }
7829
7830 /* Load the partial DIEs for a secondary CU into memory.
7831 This is also used when rereading a primary CU with load_all_dies. */
7832
7833 static void
7834 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7835 dwarf2_per_objfile *per_objfile,
7836 dwarf2_cu *existing_cu)
7837 {
7838 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7839
7840 if (!reader.dummy_p)
7841 {
7842 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7843 language_minimal);
7844
7845 /* Check if comp unit has_children.
7846 If so, read the rest of the partial symbols from this comp unit.
7847 If not, there's no more debug_info for this comp unit. */
7848 if (reader.comp_unit_die->has_children)
7849 load_partial_dies (&reader, reader.info_ptr, 0);
7850
7851 reader.keep ();
7852 }
7853 }
7854
7855 static void
7856 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7857 struct dwarf2_section_info *section,
7858 struct dwarf2_section_info *abbrev_section,
7859 unsigned int is_dwz)
7860 {
7861 const gdb_byte *info_ptr;
7862 struct objfile *objfile = per_objfile->objfile;
7863
7864 dwarf_read_debug_printf ("Reading %s for %s",
7865 section->get_name (),
7866 section->get_file_name ());
7867
7868 section->read (objfile);
7869
7870 info_ptr = section->buffer;
7871
7872 while (info_ptr < section->buffer + section->size)
7873 {
7874 struct dwarf2_per_cu_data *this_cu;
7875
7876 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7877
7878 comp_unit_head cu_header;
7879 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7880 abbrev_section, info_ptr,
7881 rcuh_kind::COMPILE);
7882
7883 /* Save the compilation unit for later lookup. */
7884 if (cu_header.unit_type != DW_UT_type)
7885 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7886 else
7887 {
7888 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7889 sig_type->signature = cu_header.signature;
7890 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7891 this_cu = &sig_type->per_cu;
7892 }
7893 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7894 this_cu->sect_off = sect_off;
7895 this_cu->length = cu_header.length + cu_header.initial_length_size;
7896 this_cu->is_dwz = is_dwz;
7897 this_cu->section = section;
7898
7899 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
7900
7901 info_ptr = info_ptr + this_cu->length;
7902 }
7903 }
7904
7905 /* Create a list of all compilation units in OBJFILE.
7906 This is only done for -readnow and building partial symtabs. */
7907
7908 static void
7909 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7910 {
7911 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7912 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7913 &per_objfile->per_bfd->abbrev, 0);
7914
7915 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7916 if (dwz != NULL)
7917 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
7918 }
7919
7920 /* Process all loaded DIEs for compilation unit CU, starting at
7921 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7922 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7923 DW_AT_ranges). See the comments of add_partial_subprogram on how
7924 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7925
7926 static void
7927 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7928 CORE_ADDR *highpc, int set_addrmap,
7929 struct dwarf2_cu *cu)
7930 {
7931 struct partial_die_info *pdi;
7932
7933 /* Now, march along the PDI's, descending into ones which have
7934 interesting children but skipping the children of the other ones,
7935 until we reach the end of the compilation unit. */
7936
7937 pdi = first_die;
7938
7939 while (pdi != NULL)
7940 {
7941 pdi->fixup (cu);
7942
7943 /* Anonymous namespaces or modules have no name but have interesting
7944 children, so we need to look at them. Ditto for anonymous
7945 enums. */
7946
7947 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7948 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7949 || pdi->tag == DW_TAG_imported_unit
7950 || pdi->tag == DW_TAG_inlined_subroutine)
7951 {
7952 switch (pdi->tag)
7953 {
7954 case DW_TAG_subprogram:
7955 case DW_TAG_inlined_subroutine:
7956 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7957 if (cu->language == language_cplus)
7958 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7959 set_addrmap, cu);
7960 break;
7961 case DW_TAG_constant:
7962 case DW_TAG_variable:
7963 case DW_TAG_typedef:
7964 case DW_TAG_union_type:
7965 if (!pdi->is_declaration
7966 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7967 {
7968 add_partial_symbol (pdi, cu);
7969 }
7970 break;
7971 case DW_TAG_class_type:
7972 case DW_TAG_interface_type:
7973 case DW_TAG_structure_type:
7974 if (!pdi->is_declaration)
7975 {
7976 add_partial_symbol (pdi, cu);
7977 }
7978 if ((cu->language == language_rust
7979 || cu->language == language_cplus) && pdi->has_children)
7980 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7981 set_addrmap, cu);
7982 break;
7983 case DW_TAG_enumeration_type:
7984 if (!pdi->is_declaration)
7985 add_partial_enumeration (pdi, cu);
7986 break;
7987 case DW_TAG_base_type:
7988 case DW_TAG_subrange_type:
7989 /* File scope base type definitions are added to the partial
7990 symbol table. */
7991 add_partial_symbol (pdi, cu);
7992 break;
7993 case DW_TAG_namespace:
7994 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7995 break;
7996 case DW_TAG_module:
7997 if (!pdi->is_declaration)
7998 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7999 break;
8000 case DW_TAG_imported_unit:
8001 {
8002 struct dwarf2_per_cu_data *per_cu;
8003
8004 /* For now we don't handle imported units in type units. */
8005 if (cu->per_cu->is_debug_types)
8006 {
8007 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8008 " supported in type units [in module %s]"),
8009 objfile_name (cu->per_objfile->objfile));
8010 }
8011
8012 per_cu = dwarf2_find_containing_comp_unit
8013 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8014
8015 /* Go read the partial unit, if needed. */
8016 if (per_cu->v.psymtab == NULL)
8017 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8018 cu->language);
8019
8020 cu->per_cu->imported_symtabs_push (per_cu);
8021 }
8022 break;
8023 case DW_TAG_imported_declaration:
8024 add_partial_symbol (pdi, cu);
8025 break;
8026 default:
8027 break;
8028 }
8029 }
8030
8031 /* If the die has a sibling, skip to the sibling. */
8032
8033 pdi = pdi->die_sibling;
8034 }
8035 }
8036
8037 /* Functions used to compute the fully scoped name of a partial DIE.
8038
8039 Normally, this is simple. For C++, the parent DIE's fully scoped
8040 name is concatenated with "::" and the partial DIE's name.
8041 Enumerators are an exception; they use the scope of their parent
8042 enumeration type, i.e. the name of the enumeration type is not
8043 prepended to the enumerator.
8044
8045 There are two complexities. One is DW_AT_specification; in this
8046 case "parent" means the parent of the target of the specification,
8047 instead of the direct parent of the DIE. The other is compilers
8048 which do not emit DW_TAG_namespace; in this case we try to guess
8049 the fully qualified name of structure types from their members'
8050 linkage names. This must be done using the DIE's children rather
8051 than the children of any DW_AT_specification target. We only need
8052 to do this for structures at the top level, i.e. if the target of
8053 any DW_AT_specification (if any; otherwise the DIE itself) does not
8054 have a parent. */
8055
8056 /* Compute the scope prefix associated with PDI's parent, in
8057 compilation unit CU. The result will be allocated on CU's
8058 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8059 field. NULL is returned if no prefix is necessary. */
8060 static const char *
8061 partial_die_parent_scope (struct partial_die_info *pdi,
8062 struct dwarf2_cu *cu)
8063 {
8064 const char *grandparent_scope;
8065 struct partial_die_info *parent, *real_pdi;
8066
8067 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8068 then this means the parent of the specification DIE. */
8069
8070 real_pdi = pdi;
8071 while (real_pdi->has_specification)
8072 {
8073 auto res = find_partial_die (real_pdi->spec_offset,
8074 real_pdi->spec_is_dwz, cu);
8075 real_pdi = res.pdi;
8076 cu = res.cu;
8077 }
8078
8079 parent = real_pdi->die_parent;
8080 if (parent == NULL)
8081 return NULL;
8082
8083 if (parent->scope_set)
8084 return parent->scope;
8085
8086 parent->fixup (cu);
8087
8088 grandparent_scope = partial_die_parent_scope (parent, cu);
8089
8090 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8091 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8092 Work around this problem here. */
8093 if (cu->language == language_cplus
8094 && parent->tag == DW_TAG_namespace
8095 && strcmp (parent->name (cu), "::") == 0
8096 && grandparent_scope == NULL)
8097 {
8098 parent->scope = NULL;
8099 parent->scope_set = 1;
8100 return NULL;
8101 }
8102
8103 /* Nested subroutines in Fortran get a prefix. */
8104 if (pdi->tag == DW_TAG_enumerator)
8105 /* Enumerators should not get the name of the enumeration as a prefix. */
8106 parent->scope = grandparent_scope;
8107 else if (parent->tag == DW_TAG_namespace
8108 || parent->tag == DW_TAG_module
8109 || parent->tag == DW_TAG_structure_type
8110 || parent->tag == DW_TAG_class_type
8111 || parent->tag == DW_TAG_interface_type
8112 || parent->tag == DW_TAG_union_type
8113 || parent->tag == DW_TAG_enumeration_type
8114 || (cu->language == language_fortran
8115 && parent->tag == DW_TAG_subprogram
8116 && pdi->tag == DW_TAG_subprogram))
8117 {
8118 if (grandparent_scope == NULL)
8119 parent->scope = parent->name (cu);
8120 else
8121 parent->scope = typename_concat (&cu->comp_unit_obstack,
8122 grandparent_scope,
8123 parent->name (cu), 0, cu);
8124 }
8125 else
8126 {
8127 /* FIXME drow/2004-04-01: What should we be doing with
8128 function-local names? For partial symbols, we should probably be
8129 ignoring them. */
8130 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8131 dwarf_tag_name (parent->tag),
8132 sect_offset_str (pdi->sect_off));
8133 parent->scope = grandparent_scope;
8134 }
8135
8136 parent->scope_set = 1;
8137 return parent->scope;
8138 }
8139
8140 /* Return the fully scoped name associated with PDI, from compilation unit
8141 CU. The result will be allocated with malloc. */
8142
8143 static gdb::unique_xmalloc_ptr<char>
8144 partial_die_full_name (struct partial_die_info *pdi,
8145 struct dwarf2_cu *cu)
8146 {
8147 const char *parent_scope;
8148
8149 /* If this is a template instantiation, we can not work out the
8150 template arguments from partial DIEs. So, unfortunately, we have
8151 to go through the full DIEs. At least any work we do building
8152 types here will be reused if full symbols are loaded later. */
8153 if (pdi->has_template_arguments)
8154 {
8155 pdi->fixup (cu);
8156
8157 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8158 {
8159 struct die_info *die;
8160 struct attribute attr;
8161 struct dwarf2_cu *ref_cu = cu;
8162
8163 /* DW_FORM_ref_addr is using section offset. */
8164 attr.name = (enum dwarf_attribute) 0;
8165 attr.form = DW_FORM_ref_addr;
8166 attr.u.unsnd = to_underlying (pdi->sect_off);
8167 die = follow_die_ref (NULL, &attr, &ref_cu);
8168
8169 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8170 }
8171 }
8172
8173 parent_scope = partial_die_parent_scope (pdi, cu);
8174 if (parent_scope == NULL)
8175 return NULL;
8176 else
8177 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8178 pdi->name (cu),
8179 0, cu));
8180 }
8181
8182 static void
8183 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8184 {
8185 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8186 struct objfile *objfile = per_objfile->objfile;
8187 struct gdbarch *gdbarch = objfile->arch ();
8188 CORE_ADDR addr = 0;
8189 const char *actual_name = NULL;
8190 CORE_ADDR baseaddr;
8191
8192 baseaddr = objfile->text_section_offset ();
8193
8194 gdb::unique_xmalloc_ptr<char> built_actual_name
8195 = partial_die_full_name (pdi, cu);
8196 if (built_actual_name != NULL)
8197 actual_name = built_actual_name.get ();
8198
8199 if (actual_name == NULL)
8200 actual_name = pdi->name (cu);
8201
8202 partial_symbol psymbol;
8203 memset (&psymbol, 0, sizeof (psymbol));
8204 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8205 psymbol.ginfo.set_section_index (-1);
8206
8207 /* The code below indicates that the psymbol should be installed by
8208 setting this. */
8209 gdb::optional<psymbol_placement> where;
8210
8211 switch (pdi->tag)
8212 {
8213 case DW_TAG_inlined_subroutine:
8214 case DW_TAG_subprogram:
8215 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8216 - baseaddr);
8217 if (pdi->is_external
8218 || cu->language == language_ada
8219 || (cu->language == language_fortran
8220 && pdi->die_parent != NULL
8221 && pdi->die_parent->tag == DW_TAG_subprogram))
8222 {
8223 /* Normally, only "external" DIEs are part of the global scope.
8224 But in Ada and Fortran, we want to be able to access nested
8225 procedures globally. So all Ada and Fortran subprograms are
8226 stored in the global scope. */
8227 where = psymbol_placement::GLOBAL;
8228 }
8229 else
8230 where = psymbol_placement::STATIC;
8231
8232 psymbol.domain = VAR_DOMAIN;
8233 psymbol.aclass = LOC_BLOCK;
8234 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8235 psymbol.ginfo.value.address = addr;
8236
8237 if (pdi->main_subprogram && actual_name != NULL)
8238 set_objfile_main_name (objfile, actual_name, cu->language);
8239 break;
8240 case DW_TAG_constant:
8241 psymbol.domain = VAR_DOMAIN;
8242 psymbol.aclass = LOC_STATIC;
8243 where = (pdi->is_external
8244 ? psymbol_placement::GLOBAL
8245 : psymbol_placement::STATIC);
8246 break;
8247 case DW_TAG_variable:
8248 if (pdi->d.locdesc)
8249 addr = decode_locdesc (pdi->d.locdesc, cu);
8250
8251 if (pdi->d.locdesc
8252 && addr == 0
8253 && !per_objfile->per_bfd->has_section_at_zero)
8254 {
8255 /* A global or static variable may also have been stripped
8256 out by the linker if unused, in which case its address
8257 will be nullified; do not add such variables into partial
8258 symbol table then. */
8259 }
8260 else if (pdi->is_external)
8261 {
8262 /* Global Variable.
8263 Don't enter into the minimal symbol tables as there is
8264 a minimal symbol table entry from the ELF symbols already.
8265 Enter into partial symbol table if it has a location
8266 descriptor or a type.
8267 If the location descriptor is missing, new_symbol will create
8268 a LOC_UNRESOLVED symbol, the address of the variable will then
8269 be determined from the minimal symbol table whenever the variable
8270 is referenced.
8271 The address for the partial symbol table entry is not
8272 used by GDB, but it comes in handy for debugging partial symbol
8273 table building. */
8274
8275 if (pdi->d.locdesc || pdi->has_type)
8276 {
8277 psymbol.domain = VAR_DOMAIN;
8278 psymbol.aclass = LOC_STATIC;
8279 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8280 psymbol.ginfo.value.address = addr;
8281 where = psymbol_placement::GLOBAL;
8282 }
8283 }
8284 else
8285 {
8286 int has_loc = pdi->d.locdesc != NULL;
8287
8288 /* Static Variable. Skip symbols whose value we cannot know (those
8289 without location descriptors or constant values). */
8290 if (!has_loc && !pdi->has_const_value)
8291 return;
8292
8293 psymbol.domain = VAR_DOMAIN;
8294 psymbol.aclass = LOC_STATIC;
8295 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8296 if (has_loc)
8297 psymbol.ginfo.value.address = addr;
8298 where = psymbol_placement::STATIC;
8299 }
8300 break;
8301 case DW_TAG_array_type:
8302 case DW_TAG_typedef:
8303 case DW_TAG_base_type:
8304 case DW_TAG_subrange_type:
8305 psymbol.domain = VAR_DOMAIN;
8306 psymbol.aclass = LOC_TYPEDEF;
8307 where = psymbol_placement::STATIC;
8308 break;
8309 case DW_TAG_imported_declaration:
8310 case DW_TAG_namespace:
8311 psymbol.domain = VAR_DOMAIN;
8312 psymbol.aclass = LOC_TYPEDEF;
8313 where = psymbol_placement::GLOBAL;
8314 break;
8315 case DW_TAG_module:
8316 /* With Fortran 77 there might be a "BLOCK DATA" module
8317 available without any name. If so, we skip the module as it
8318 doesn't bring any value. */
8319 if (actual_name != nullptr)
8320 {
8321 psymbol.domain = MODULE_DOMAIN;
8322 psymbol.aclass = LOC_TYPEDEF;
8323 where = psymbol_placement::GLOBAL;
8324 }
8325 break;
8326 case DW_TAG_class_type:
8327 case DW_TAG_interface_type:
8328 case DW_TAG_structure_type:
8329 case DW_TAG_union_type:
8330 case DW_TAG_enumeration_type:
8331 /* Skip external references. The DWARF standard says in the section
8332 about "Structure, Union, and Class Type Entries": "An incomplete
8333 structure, union or class type is represented by a structure,
8334 union or class entry that does not have a byte size attribute
8335 and that has a DW_AT_declaration attribute." */
8336 if (!pdi->has_byte_size && pdi->is_declaration)
8337 return;
8338
8339 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8340 static vs. global. */
8341 psymbol.domain = STRUCT_DOMAIN;
8342 psymbol.aclass = LOC_TYPEDEF;
8343 where = (cu->language == language_cplus
8344 ? psymbol_placement::GLOBAL
8345 : psymbol_placement::STATIC);
8346 break;
8347 case DW_TAG_enumerator:
8348 psymbol.domain = VAR_DOMAIN;
8349 psymbol.aclass = LOC_CONST;
8350 where = (cu->language == language_cplus
8351 ? psymbol_placement::GLOBAL
8352 : psymbol_placement::STATIC);
8353 break;
8354 default:
8355 break;
8356 }
8357
8358 if (where.has_value ())
8359 {
8360 if (built_actual_name != nullptr)
8361 actual_name = objfile->intern (actual_name);
8362 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8363 psymbol.ginfo.set_linkage_name (actual_name);
8364 else
8365 {
8366 psymbol.ginfo.set_demangled_name (actual_name,
8367 &objfile->objfile_obstack);
8368 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8369 }
8370 cu->per_cu->v.psymtab->add_psymbol
8371 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8372 objfile);
8373 }
8374 }
8375
8376 /* Read a partial die corresponding to a namespace; also, add a symbol
8377 corresponding to that namespace to the symbol table. NAMESPACE is
8378 the name of the enclosing namespace. */
8379
8380 static void
8381 add_partial_namespace (struct partial_die_info *pdi,
8382 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8383 int set_addrmap, struct dwarf2_cu *cu)
8384 {
8385 /* Add a symbol for the namespace. */
8386
8387 add_partial_symbol (pdi, cu);
8388
8389 /* Now scan partial symbols in that namespace. */
8390
8391 if (pdi->has_children)
8392 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8393 }
8394
8395 /* Read a partial die corresponding to a Fortran module. */
8396
8397 static void
8398 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8399 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8400 {
8401 /* Add a symbol for the namespace. */
8402
8403 add_partial_symbol (pdi, cu);
8404
8405 /* Now scan partial symbols in that module. */
8406
8407 if (pdi->has_children)
8408 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8409 }
8410
8411 /* Read a partial die corresponding to a subprogram or an inlined
8412 subprogram and create a partial symbol for that subprogram.
8413 When the CU language allows it, this routine also defines a partial
8414 symbol for each nested subprogram that this subprogram contains.
8415 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8416 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8417
8418 PDI may also be a lexical block, in which case we simply search
8419 recursively for subprograms defined inside that lexical block.
8420 Again, this is only performed when the CU language allows this
8421 type of definitions. */
8422
8423 static void
8424 add_partial_subprogram (struct partial_die_info *pdi,
8425 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8426 int set_addrmap, struct dwarf2_cu *cu)
8427 {
8428 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8429 {
8430 if (pdi->has_pc_info)
8431 {
8432 if (pdi->lowpc < *lowpc)
8433 *lowpc = pdi->lowpc;
8434 if (pdi->highpc > *highpc)
8435 *highpc = pdi->highpc;
8436 if (set_addrmap)
8437 {
8438 struct objfile *objfile = cu->per_objfile->objfile;
8439 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8440 struct gdbarch *gdbarch = objfile->arch ();
8441 CORE_ADDR baseaddr;
8442 CORE_ADDR this_highpc;
8443 CORE_ADDR this_lowpc;
8444
8445 baseaddr = objfile->text_section_offset ();
8446 this_lowpc
8447 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8448 pdi->lowpc + baseaddr)
8449 - baseaddr);
8450 this_highpc
8451 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8452 pdi->highpc + baseaddr)
8453 - baseaddr);
8454 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8455 this_lowpc, this_highpc - 1,
8456 cu->per_cu->v.psymtab);
8457 }
8458 }
8459
8460 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8461 {
8462 if (!pdi->is_declaration)
8463 /* Ignore subprogram DIEs that do not have a name, they are
8464 illegal. Do not emit a complaint at this point, we will
8465 do so when we convert this psymtab into a symtab. */
8466 if (pdi->name (cu))
8467 add_partial_symbol (pdi, cu);
8468 }
8469 }
8470
8471 if (! pdi->has_children)
8472 return;
8473
8474 if (cu->language == language_ada || cu->language == language_fortran)
8475 {
8476 pdi = pdi->die_child;
8477 while (pdi != NULL)
8478 {
8479 pdi->fixup (cu);
8480 if (pdi->tag == DW_TAG_subprogram
8481 || pdi->tag == DW_TAG_inlined_subroutine
8482 || pdi->tag == DW_TAG_lexical_block)
8483 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8484 pdi = pdi->die_sibling;
8485 }
8486 }
8487 }
8488
8489 /* Read a partial die corresponding to an enumeration type. */
8490
8491 static void
8492 add_partial_enumeration (struct partial_die_info *enum_pdi,
8493 struct dwarf2_cu *cu)
8494 {
8495 struct partial_die_info *pdi;
8496
8497 if (enum_pdi->name (cu) != NULL)
8498 add_partial_symbol (enum_pdi, cu);
8499
8500 pdi = enum_pdi->die_child;
8501 while (pdi)
8502 {
8503 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8504 complaint (_("malformed enumerator DIE ignored"));
8505 else
8506 add_partial_symbol (pdi, cu);
8507 pdi = pdi->die_sibling;
8508 }
8509 }
8510
8511 /* Return the initial uleb128 in the die at INFO_PTR. */
8512
8513 static unsigned int
8514 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8515 {
8516 unsigned int bytes_read;
8517
8518 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8519 }
8520
8521 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8522 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8523
8524 Return the corresponding abbrev, or NULL if the number is zero (indicating
8525 an empty DIE). In either case *BYTES_READ will be set to the length of
8526 the initial number. */
8527
8528 static const struct abbrev_info *
8529 peek_die_abbrev (const die_reader_specs &reader,
8530 const gdb_byte *info_ptr, unsigned int *bytes_read)
8531 {
8532 dwarf2_cu *cu = reader.cu;
8533 bfd *abfd = reader.abfd;
8534 unsigned int abbrev_number
8535 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8536
8537 if (abbrev_number == 0)
8538 return NULL;
8539
8540 const abbrev_info *abbrev
8541 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8542 if (!abbrev)
8543 {
8544 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8545 " at offset %s [in module %s]"),
8546 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8547 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8548 }
8549
8550 return abbrev;
8551 }
8552
8553 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8554 Returns a pointer to the end of a series of DIEs, terminated by an empty
8555 DIE. Any children of the skipped DIEs will also be skipped. */
8556
8557 static const gdb_byte *
8558 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8559 {
8560 while (1)
8561 {
8562 unsigned int bytes_read;
8563 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8564 &bytes_read);
8565
8566 if (abbrev == NULL)
8567 return info_ptr + bytes_read;
8568 else
8569 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8570 }
8571 }
8572
8573 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8574 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8575 abbrev corresponding to that skipped uleb128 should be passed in
8576 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8577 children. */
8578
8579 static const gdb_byte *
8580 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8581 const struct abbrev_info *abbrev)
8582 {
8583 unsigned int bytes_read;
8584 struct attribute attr;
8585 bfd *abfd = reader->abfd;
8586 struct dwarf2_cu *cu = reader->cu;
8587 const gdb_byte *buffer = reader->buffer;
8588 const gdb_byte *buffer_end = reader->buffer_end;
8589 unsigned int form, i;
8590
8591 for (i = 0; i < abbrev->num_attrs; i++)
8592 {
8593 /* The only abbrev we care about is DW_AT_sibling. */
8594 if (abbrev->attrs[i].name == DW_AT_sibling)
8595 {
8596 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8597 if (attr.form == DW_FORM_ref_addr)
8598 complaint (_("ignoring absolute DW_AT_sibling"));
8599 else
8600 {
8601 sect_offset off = attr.get_ref_die_offset ();
8602 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8603
8604 if (sibling_ptr < info_ptr)
8605 complaint (_("DW_AT_sibling points backwards"));
8606 else if (sibling_ptr > reader->buffer_end)
8607 reader->die_section->overflow_complaint ();
8608 else
8609 return sibling_ptr;
8610 }
8611 }
8612
8613 /* If it isn't DW_AT_sibling, skip this attribute. */
8614 form = abbrev->attrs[i].form;
8615 skip_attribute:
8616 switch (form)
8617 {
8618 case DW_FORM_ref_addr:
8619 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8620 and later it is offset sized. */
8621 if (cu->header.version == 2)
8622 info_ptr += cu->header.addr_size;
8623 else
8624 info_ptr += cu->header.offset_size;
8625 break;
8626 case DW_FORM_GNU_ref_alt:
8627 info_ptr += cu->header.offset_size;
8628 break;
8629 case DW_FORM_addr:
8630 info_ptr += cu->header.addr_size;
8631 break;
8632 case DW_FORM_data1:
8633 case DW_FORM_ref1:
8634 case DW_FORM_flag:
8635 case DW_FORM_strx1:
8636 info_ptr += 1;
8637 break;
8638 case DW_FORM_flag_present:
8639 case DW_FORM_implicit_const:
8640 break;
8641 case DW_FORM_data2:
8642 case DW_FORM_ref2:
8643 case DW_FORM_strx2:
8644 info_ptr += 2;
8645 break;
8646 case DW_FORM_strx3:
8647 info_ptr += 3;
8648 break;
8649 case DW_FORM_data4:
8650 case DW_FORM_ref4:
8651 case DW_FORM_strx4:
8652 info_ptr += 4;
8653 break;
8654 case DW_FORM_data8:
8655 case DW_FORM_ref8:
8656 case DW_FORM_ref_sig8:
8657 info_ptr += 8;
8658 break;
8659 case DW_FORM_data16:
8660 info_ptr += 16;
8661 break;
8662 case DW_FORM_string:
8663 read_direct_string (abfd, info_ptr, &bytes_read);
8664 info_ptr += bytes_read;
8665 break;
8666 case DW_FORM_sec_offset:
8667 case DW_FORM_strp:
8668 case DW_FORM_GNU_strp_alt:
8669 info_ptr += cu->header.offset_size;
8670 break;
8671 case DW_FORM_exprloc:
8672 case DW_FORM_block:
8673 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8674 info_ptr += bytes_read;
8675 break;
8676 case DW_FORM_block1:
8677 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8678 break;
8679 case DW_FORM_block2:
8680 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8681 break;
8682 case DW_FORM_block4:
8683 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8684 break;
8685 case DW_FORM_addrx:
8686 case DW_FORM_strx:
8687 case DW_FORM_sdata:
8688 case DW_FORM_udata:
8689 case DW_FORM_ref_udata:
8690 case DW_FORM_GNU_addr_index:
8691 case DW_FORM_GNU_str_index:
8692 case DW_FORM_rnglistx:
8693 case DW_FORM_loclistx:
8694 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8695 break;
8696 case DW_FORM_indirect:
8697 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8698 info_ptr += bytes_read;
8699 /* We need to continue parsing from here, so just go back to
8700 the top. */
8701 goto skip_attribute;
8702
8703 default:
8704 error (_("Dwarf Error: Cannot handle %s "
8705 "in DWARF reader [in module %s]"),
8706 dwarf_form_name (form),
8707 bfd_get_filename (abfd));
8708 }
8709 }
8710
8711 if (abbrev->has_children)
8712 return skip_children (reader, info_ptr);
8713 else
8714 return info_ptr;
8715 }
8716
8717 /* Locate ORIG_PDI's sibling.
8718 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8719
8720 static const gdb_byte *
8721 locate_pdi_sibling (const struct die_reader_specs *reader,
8722 struct partial_die_info *orig_pdi,
8723 const gdb_byte *info_ptr)
8724 {
8725 /* Do we know the sibling already? */
8726
8727 if (orig_pdi->sibling)
8728 return orig_pdi->sibling;
8729
8730 /* Are there any children to deal with? */
8731
8732 if (!orig_pdi->has_children)
8733 return info_ptr;
8734
8735 /* Skip the children the long way. */
8736
8737 return skip_children (reader, info_ptr);
8738 }
8739
8740 /* Expand this partial symbol table into a full symbol table. SELF is
8741 not NULL. */
8742
8743 void
8744 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8745 {
8746 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8747
8748 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8749
8750 /* If this psymtab is constructed from a debug-only objfile, the
8751 has_section_at_zero flag will not necessarily be correct. We
8752 can get the correct value for this flag by looking at the data
8753 associated with the (presumably stripped) associated objfile. */
8754 if (objfile->separate_debug_objfile_backlink)
8755 {
8756 dwarf2_per_objfile *per_objfile_backlink
8757 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8758
8759 per_objfile->per_bfd->has_section_at_zero
8760 = per_objfile_backlink->per_bfd->has_section_at_zero;
8761 }
8762
8763 expand_psymtab (objfile);
8764
8765 process_cu_includes (per_objfile);
8766 }
8767 \f
8768 /* Reading in full CUs. */
8769
8770 /* Add PER_CU to the queue. */
8771
8772 static void
8773 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8774 dwarf2_per_objfile *per_objfile,
8775 enum language pretend_language)
8776 {
8777 per_cu->queued = 1;
8778
8779 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8780 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8781 }
8782
8783 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8784
8785 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8786 dependency.
8787
8788 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8789 DIEs, false otherwise.
8790
8791 Explanation: there is an invariant that if a CU is queued for expansion
8792 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8793 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8794 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8795 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8796 invariant is respected.
8797
8798 The caller is therefore not required to load the CU's DIEs (we return false)
8799 if:
8800
8801 - the CU is already expanded, and therefore does not get enqueued
8802 - the CU gets enqueued for expansion, but its DIEs are already loaded
8803
8804 Note that the caller should not use this function's return value as an
8805 indicator of whether the CU's DIEs are loaded right now, it should check
8806 that by calling `dwarf2_per_objfile::get_cu` instead. */
8807
8808 static int
8809 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8810 dwarf2_per_cu_data *per_cu,
8811 dwarf2_per_objfile *per_objfile,
8812 enum language pretend_language)
8813 {
8814 /* We may arrive here during partial symbol reading, if we need full
8815 DIEs to process an unusual case (e.g. template arguments). Do
8816 not queue PER_CU, just tell our caller to load its DIEs. */
8817 if (per_cu->per_bfd->reading_partial_symbols)
8818 {
8819 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8820
8821 if (cu == NULL || cu->dies == NULL)
8822 return 1;
8823 return 0;
8824 }
8825
8826 /* Mark the dependence relation so that we don't flush PER_CU
8827 too early. */
8828 if (dependent_cu != NULL)
8829 dwarf2_add_dependence (dependent_cu, per_cu);
8830
8831 /* If it's already on the queue, we have nothing to do. */
8832 if (per_cu->queued)
8833 {
8834 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8835 loaded. */
8836 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8837
8838 /* If the CU is queued for expansion, it should not already be
8839 expanded. */
8840 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8841
8842 /* The DIEs are already loaded, the caller doesn't need to do it. */
8843 return 0;
8844 }
8845
8846 bool queued = false;
8847 if (!per_objfile->symtab_set_p (per_cu))
8848 {
8849 /* Add it to the queue. */
8850 queue_comp_unit (per_cu, per_objfile, pretend_language);
8851 queued = true;
8852 }
8853
8854 /* If the compilation unit is already loaded, just mark it as
8855 used. */
8856 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8857 if (cu != nullptr)
8858 cu->last_used = 0;
8859
8860 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8861 and the DIEs are not already loaded. */
8862 return queued && cu == nullptr;
8863 }
8864
8865 /* Process the queue. */
8866
8867 static void
8868 process_queue (dwarf2_per_objfile *per_objfile)
8869 {
8870 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8871 objfile_name (per_objfile->objfile));
8872
8873 /* The queue starts out with one item, but following a DIE reference
8874 may load a new CU, adding it to the end of the queue. */
8875 while (!per_objfile->per_bfd->queue->empty ())
8876 {
8877 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8878 dwarf2_per_cu_data *per_cu = item.per_cu;
8879
8880 if (!per_objfile->symtab_set_p (per_cu))
8881 {
8882 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8883
8884 /* Skip dummy CUs. */
8885 if (cu != nullptr)
8886 {
8887 unsigned int debug_print_threshold;
8888 char buf[100];
8889
8890 if (per_cu->is_debug_types)
8891 {
8892 struct signatured_type *sig_type =
8893 (struct signatured_type *) per_cu;
8894
8895 sprintf (buf, "TU %s at offset %s",
8896 hex_string (sig_type->signature),
8897 sect_offset_str (per_cu->sect_off));
8898 /* There can be 100s of TUs.
8899 Only print them in verbose mode. */
8900 debug_print_threshold = 2;
8901 }
8902 else
8903 {
8904 sprintf (buf, "CU at offset %s",
8905 sect_offset_str (per_cu->sect_off));
8906 debug_print_threshold = 1;
8907 }
8908
8909 if (dwarf_read_debug >= debug_print_threshold)
8910 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8911
8912 if (per_cu->is_debug_types)
8913 process_full_type_unit (cu, item.pretend_language);
8914 else
8915 process_full_comp_unit (cu, item.pretend_language);
8916
8917 if (dwarf_read_debug >= debug_print_threshold)
8918 dwarf_read_debug_printf ("Done expanding %s", buf);
8919 }
8920 }
8921
8922 per_cu->queued = 0;
8923 per_objfile->per_bfd->queue->pop ();
8924 }
8925
8926 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8927 objfile_name (per_objfile->objfile));
8928 }
8929
8930 /* Read in full symbols for PST, and anything it depends on. */
8931
8932 void
8933 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8934 {
8935 gdb_assert (!readin_p (objfile));
8936
8937 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8938 free_cached_comp_units freer (per_objfile);
8939 expand_dependencies (objfile);
8940
8941 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8942 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8943 }
8944
8945 /* See psympriv.h. */
8946
8947 bool
8948 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8949 {
8950 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8951 return per_objfile->symtab_set_p (per_cu_data);
8952 }
8953
8954 /* See psympriv.h. */
8955
8956 compunit_symtab *
8957 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8958 {
8959 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8960 return per_objfile->get_symtab (per_cu_data);
8961 }
8962
8963 /* Trivial hash function for die_info: the hash value of a DIE
8964 is its offset in .debug_info for this objfile. */
8965
8966 static hashval_t
8967 die_hash (const void *item)
8968 {
8969 const struct die_info *die = (const struct die_info *) item;
8970
8971 return to_underlying (die->sect_off);
8972 }
8973
8974 /* Trivial comparison function for die_info structures: two DIEs
8975 are equal if they have the same offset. */
8976
8977 static int
8978 die_eq (const void *item_lhs, const void *item_rhs)
8979 {
8980 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8981 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8982
8983 return die_lhs->sect_off == die_rhs->sect_off;
8984 }
8985
8986 /* Load the DIEs associated with PER_CU into memory.
8987
8988 In some cases, the caller, while reading partial symbols, will need to load
8989 the full symbols for the CU for some reason. It will already have a
8990 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8991 rather than creating a new one. */
8992
8993 static void
8994 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8995 dwarf2_per_objfile *per_objfile,
8996 dwarf2_cu *existing_cu,
8997 bool skip_partial,
8998 enum language pretend_language)
8999 {
9000 gdb_assert (! this_cu->is_debug_types);
9001
9002 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9003 if (reader.dummy_p)
9004 return;
9005
9006 struct dwarf2_cu *cu = reader.cu;
9007 const gdb_byte *info_ptr = reader.info_ptr;
9008
9009 gdb_assert (cu->die_hash == NULL);
9010 cu->die_hash =
9011 htab_create_alloc_ex (cu->header.length / 12,
9012 die_hash,
9013 die_eq,
9014 NULL,
9015 &cu->comp_unit_obstack,
9016 hashtab_obstack_allocate,
9017 dummy_obstack_deallocate);
9018
9019 if (reader.comp_unit_die->has_children)
9020 reader.comp_unit_die->child
9021 = read_die_and_siblings (&reader, reader.info_ptr,
9022 &info_ptr, reader.comp_unit_die);
9023 cu->dies = reader.comp_unit_die;
9024 /* comp_unit_die is not stored in die_hash, no need. */
9025
9026 /* We try not to read any attributes in this function, because not
9027 all CUs needed for references have been loaded yet, and symbol
9028 table processing isn't initialized. But we have to set the CU language,
9029 or we won't be able to build types correctly.
9030 Similarly, if we do not read the producer, we can not apply
9031 producer-specific interpretation. */
9032 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9033
9034 reader.keep ();
9035 }
9036
9037 /* Add a DIE to the delayed physname list. */
9038
9039 static void
9040 add_to_method_list (struct type *type, int fnfield_index, int index,
9041 const char *name, struct die_info *die,
9042 struct dwarf2_cu *cu)
9043 {
9044 struct delayed_method_info mi;
9045 mi.type = type;
9046 mi.fnfield_index = fnfield_index;
9047 mi.index = index;
9048 mi.name = name;
9049 mi.die = die;
9050 cu->method_list.push_back (mi);
9051 }
9052
9053 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9054 "const" / "volatile". If so, decrements LEN by the length of the
9055 modifier and return true. Otherwise return false. */
9056
9057 template<size_t N>
9058 static bool
9059 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9060 {
9061 size_t mod_len = sizeof (mod) - 1;
9062 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9063 {
9064 len -= mod_len;
9065 return true;
9066 }
9067 return false;
9068 }
9069
9070 /* Compute the physnames of any methods on the CU's method list.
9071
9072 The computation of method physnames is delayed in order to avoid the
9073 (bad) condition that one of the method's formal parameters is of an as yet
9074 incomplete type. */
9075
9076 static void
9077 compute_delayed_physnames (struct dwarf2_cu *cu)
9078 {
9079 /* Only C++ delays computing physnames. */
9080 if (cu->method_list.empty ())
9081 return;
9082 gdb_assert (cu->language == language_cplus);
9083
9084 for (const delayed_method_info &mi : cu->method_list)
9085 {
9086 const char *physname;
9087 struct fn_fieldlist *fn_flp
9088 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9089 physname = dwarf2_physname (mi.name, mi.die, cu);
9090 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9091 = physname ? physname : "";
9092
9093 /* Since there's no tag to indicate whether a method is a
9094 const/volatile overload, extract that information out of the
9095 demangled name. */
9096 if (physname != NULL)
9097 {
9098 size_t len = strlen (physname);
9099
9100 while (1)
9101 {
9102 if (physname[len] == ')') /* shortcut */
9103 break;
9104 else if (check_modifier (physname, len, " const"))
9105 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9106 else if (check_modifier (physname, len, " volatile"))
9107 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9108 else
9109 break;
9110 }
9111 }
9112 }
9113
9114 /* The list is no longer needed. */
9115 cu->method_list.clear ();
9116 }
9117
9118 /* Go objects should be embedded in a DW_TAG_module DIE,
9119 and it's not clear if/how imported objects will appear.
9120 To keep Go support simple until that's worked out,
9121 go back through what we've read and create something usable.
9122 We could do this while processing each DIE, and feels kinda cleaner,
9123 but that way is more invasive.
9124 This is to, for example, allow the user to type "p var" or "b main"
9125 without having to specify the package name, and allow lookups
9126 of module.object to work in contexts that use the expression
9127 parser. */
9128
9129 static void
9130 fixup_go_packaging (struct dwarf2_cu *cu)
9131 {
9132 gdb::unique_xmalloc_ptr<char> package_name;
9133 struct pending *list;
9134 int i;
9135
9136 for (list = *cu->get_builder ()->get_global_symbols ();
9137 list != NULL;
9138 list = list->next)
9139 {
9140 for (i = 0; i < list->nsyms; ++i)
9141 {
9142 struct symbol *sym = list->symbol[i];
9143
9144 if (sym->language () == language_go
9145 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9146 {
9147 gdb::unique_xmalloc_ptr<char> this_package_name
9148 (go_symbol_package_name (sym));
9149
9150 if (this_package_name == NULL)
9151 continue;
9152 if (package_name == NULL)
9153 package_name = std::move (this_package_name);
9154 else
9155 {
9156 struct objfile *objfile = cu->per_objfile->objfile;
9157 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9158 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9159 (symbol_symtab (sym) != NULL
9160 ? symtab_to_filename_for_display
9161 (symbol_symtab (sym))
9162 : objfile_name (objfile)),
9163 this_package_name.get (), package_name.get ());
9164 }
9165 }
9166 }
9167 }
9168
9169 if (package_name != NULL)
9170 {
9171 struct objfile *objfile = cu->per_objfile->objfile;
9172 const char *saved_package_name = objfile->intern (package_name.get ());
9173 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9174 saved_package_name);
9175 struct symbol *sym;
9176
9177 sym = new (&objfile->objfile_obstack) symbol;
9178 sym->set_language (language_go, &objfile->objfile_obstack);
9179 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9180 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9181 e.g., "main" finds the "main" module and not C's main(). */
9182 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9183 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9184 SYMBOL_TYPE (sym) = type;
9185
9186 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9187 }
9188 }
9189
9190 /* Allocate a fully-qualified name consisting of the two parts on the
9191 obstack. */
9192
9193 static const char *
9194 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9195 {
9196 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9197 }
9198
9199 /* A helper that allocates a variant part to attach to a Rust enum
9200 type. OBSTACK is where the results should be allocated. TYPE is
9201 the type we're processing. DISCRIMINANT_INDEX is the index of the
9202 discriminant. It must be the index of one of the fields of TYPE,
9203 or -1 to mean there is no discriminant (univariant enum).
9204 DEFAULT_INDEX is the index of the default field; or -1 if there is
9205 no default. RANGES is indexed by "effective" field number (the
9206 field index, but omitting the discriminant and default fields) and
9207 must hold the discriminant values used by the variants. Note that
9208 RANGES must have a lifetime at least as long as OBSTACK -- either
9209 already allocated on it, or static. */
9210
9211 static void
9212 alloc_rust_variant (struct obstack *obstack, struct type *type,
9213 int discriminant_index, int default_index,
9214 gdb::array_view<discriminant_range> ranges)
9215 {
9216 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9217 gdb_assert (discriminant_index == -1
9218 || (discriminant_index >= 0
9219 && discriminant_index < type->num_fields ()));
9220 gdb_assert (default_index == -1
9221 || (default_index >= 0 && default_index < type->num_fields ()));
9222
9223 /* We have one variant for each non-discriminant field. */
9224 int n_variants = type->num_fields ();
9225 if (discriminant_index != -1)
9226 --n_variants;
9227
9228 variant *variants = new (obstack) variant[n_variants];
9229 int var_idx = 0;
9230 int range_idx = 0;
9231 for (int i = 0; i < type->num_fields (); ++i)
9232 {
9233 if (i == discriminant_index)
9234 continue;
9235
9236 variants[var_idx].first_field = i;
9237 variants[var_idx].last_field = i + 1;
9238
9239 /* The default field does not need a range, but other fields do.
9240 We skipped the discriminant above. */
9241 if (i != default_index)
9242 {
9243 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9244 ++range_idx;
9245 }
9246
9247 ++var_idx;
9248 }
9249
9250 gdb_assert (range_idx == ranges.size ());
9251 gdb_assert (var_idx == n_variants);
9252
9253 variant_part *part = new (obstack) variant_part;
9254 part->discriminant_index = discriminant_index;
9255 /* If there is no discriminant, then whether it is signed is of no
9256 consequence. */
9257 part->is_unsigned
9258 = (discriminant_index == -1
9259 ? false
9260 : type->field (discriminant_index).type ()->is_unsigned ());
9261 part->variants = gdb::array_view<variant> (variants, n_variants);
9262
9263 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9264 gdb::array_view<variant_part> *prop_value
9265 = new (storage) gdb::array_view<variant_part> (part, 1);
9266
9267 struct dynamic_prop prop;
9268 prop.set_variant_parts (prop_value);
9269
9270 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9271 }
9272
9273 /* Some versions of rustc emitted enums in an unusual way.
9274
9275 Ordinary enums were emitted as unions. The first element of each
9276 structure in the union was named "RUST$ENUM$DISR". This element
9277 held the discriminant.
9278
9279 These versions of Rust also implemented the "non-zero"
9280 optimization. When the enum had two values, and one is empty and
9281 the other holds a pointer that cannot be zero, the pointer is used
9282 as the discriminant, with a zero value meaning the empty variant.
9283 Here, the union's first member is of the form
9284 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9285 where the fieldnos are the indices of the fields that should be
9286 traversed in order to find the field (which may be several fields deep)
9287 and the variantname is the name of the variant of the case when the
9288 field is zero.
9289
9290 This function recognizes whether TYPE is of one of these forms,
9291 and, if so, smashes it to be a variant type. */
9292
9293 static void
9294 quirk_rust_enum (struct type *type, struct objfile *objfile)
9295 {
9296 gdb_assert (type->code () == TYPE_CODE_UNION);
9297
9298 /* We don't need to deal with empty enums. */
9299 if (type->num_fields () == 0)
9300 return;
9301
9302 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9303 if (type->num_fields () == 1
9304 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9305 {
9306 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9307
9308 /* Decode the field name to find the offset of the
9309 discriminant. */
9310 ULONGEST bit_offset = 0;
9311 struct type *field_type = type->field (0).type ();
9312 while (name[0] >= '0' && name[0] <= '9')
9313 {
9314 char *tail;
9315 unsigned long index = strtoul (name, &tail, 10);
9316 name = tail;
9317 if (*name != '$'
9318 || index >= field_type->num_fields ()
9319 || (TYPE_FIELD_LOC_KIND (field_type, index)
9320 != FIELD_LOC_KIND_BITPOS))
9321 {
9322 complaint (_("Could not parse Rust enum encoding string \"%s\""
9323 "[in module %s]"),
9324 TYPE_FIELD_NAME (type, 0),
9325 objfile_name (objfile));
9326 return;
9327 }
9328 ++name;
9329
9330 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9331 field_type = field_type->field (index).type ();
9332 }
9333
9334 /* Smash this type to be a structure type. We have to do this
9335 because the type has already been recorded. */
9336 type->set_code (TYPE_CODE_STRUCT);
9337 type->set_num_fields (3);
9338 /* Save the field we care about. */
9339 struct field saved_field = type->field (0);
9340 type->set_fields
9341 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9342
9343 /* Put the discriminant at index 0. */
9344 type->field (0).set_type (field_type);
9345 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9346 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9347 SET_FIELD_BITPOS (type->field (0), bit_offset);
9348
9349 /* The order of fields doesn't really matter, so put the real
9350 field at index 1 and the data-less field at index 2. */
9351 type->field (1) = saved_field;
9352 TYPE_FIELD_NAME (type, 1)
9353 = rust_last_path_segment (type->field (1).type ()->name ());
9354 type->field (1).type ()->set_name
9355 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9356 TYPE_FIELD_NAME (type, 1)));
9357
9358 const char *dataless_name
9359 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9360 name);
9361 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9362 dataless_name);
9363 type->field (2).set_type (dataless_type);
9364 /* NAME points into the original discriminant name, which
9365 already has the correct lifetime. */
9366 TYPE_FIELD_NAME (type, 2) = name;
9367 SET_FIELD_BITPOS (type->field (2), 0);
9368
9369 /* Indicate that this is a variant type. */
9370 static discriminant_range ranges[1] = { { 0, 0 } };
9371 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9372 }
9373 /* A union with a single anonymous field is probably an old-style
9374 univariant enum. */
9375 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9376 {
9377 /* Smash this type to be a structure type. We have to do this
9378 because the type has already been recorded. */
9379 type->set_code (TYPE_CODE_STRUCT);
9380
9381 struct type *field_type = type->field (0).type ();
9382 const char *variant_name
9383 = rust_last_path_segment (field_type->name ());
9384 TYPE_FIELD_NAME (type, 0) = variant_name;
9385 field_type->set_name
9386 (rust_fully_qualify (&objfile->objfile_obstack,
9387 type->name (), variant_name));
9388
9389 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9390 }
9391 else
9392 {
9393 struct type *disr_type = nullptr;
9394 for (int i = 0; i < type->num_fields (); ++i)
9395 {
9396 disr_type = type->field (i).type ();
9397
9398 if (disr_type->code () != TYPE_CODE_STRUCT)
9399 {
9400 /* All fields of a true enum will be structs. */
9401 return;
9402 }
9403 else if (disr_type->num_fields () == 0)
9404 {
9405 /* Could be data-less variant, so keep going. */
9406 disr_type = nullptr;
9407 }
9408 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9409 "RUST$ENUM$DISR") != 0)
9410 {
9411 /* Not a Rust enum. */
9412 return;
9413 }
9414 else
9415 {
9416 /* Found one. */
9417 break;
9418 }
9419 }
9420
9421 /* If we got here without a discriminant, then it's probably
9422 just a union. */
9423 if (disr_type == nullptr)
9424 return;
9425
9426 /* Smash this type to be a structure type. We have to do this
9427 because the type has already been recorded. */
9428 type->set_code (TYPE_CODE_STRUCT);
9429
9430 /* Make space for the discriminant field. */
9431 struct field *disr_field = &disr_type->field (0);
9432 field *new_fields
9433 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9434 * sizeof (struct field)));
9435 memcpy (new_fields + 1, type->fields (),
9436 type->num_fields () * sizeof (struct field));
9437 type->set_fields (new_fields);
9438 type->set_num_fields (type->num_fields () + 1);
9439
9440 /* Install the discriminant at index 0 in the union. */
9441 type->field (0) = *disr_field;
9442 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9443 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9444
9445 /* We need a way to find the correct discriminant given a
9446 variant name. For convenience we build a map here. */
9447 struct type *enum_type = disr_field->type ();
9448 std::unordered_map<std::string, ULONGEST> discriminant_map;
9449 for (int i = 0; i < enum_type->num_fields (); ++i)
9450 {
9451 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9452 {
9453 const char *name
9454 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9455 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9456 }
9457 }
9458
9459 int n_fields = type->num_fields ();
9460 /* We don't need a range entry for the discriminant, but we do
9461 need one for every other field, as there is no default
9462 variant. */
9463 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9464 discriminant_range,
9465 n_fields - 1);
9466 /* Skip the discriminant here. */
9467 for (int i = 1; i < n_fields; ++i)
9468 {
9469 /* Find the final word in the name of this variant's type.
9470 That name can be used to look up the correct
9471 discriminant. */
9472 const char *variant_name
9473 = rust_last_path_segment (type->field (i).type ()->name ());
9474
9475 auto iter = discriminant_map.find (variant_name);
9476 if (iter != discriminant_map.end ())
9477 {
9478 ranges[i - 1].low = iter->second;
9479 ranges[i - 1].high = iter->second;
9480 }
9481
9482 /* In Rust, each element should have the size of the
9483 enclosing enum. */
9484 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9485
9486 /* Remove the discriminant field, if it exists. */
9487 struct type *sub_type = type->field (i).type ();
9488 if (sub_type->num_fields () > 0)
9489 {
9490 sub_type->set_num_fields (sub_type->num_fields () - 1);
9491 sub_type->set_fields (sub_type->fields () + 1);
9492 }
9493 TYPE_FIELD_NAME (type, i) = variant_name;
9494 sub_type->set_name
9495 (rust_fully_qualify (&objfile->objfile_obstack,
9496 type->name (), variant_name));
9497 }
9498
9499 /* Indicate that this is a variant type. */
9500 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9501 gdb::array_view<discriminant_range> (ranges,
9502 n_fields - 1));
9503 }
9504 }
9505
9506 /* Rewrite some Rust unions to be structures with variants parts. */
9507
9508 static void
9509 rust_union_quirks (struct dwarf2_cu *cu)
9510 {
9511 gdb_assert (cu->language == language_rust);
9512 for (type *type_ : cu->rust_unions)
9513 quirk_rust_enum (type_, cu->per_objfile->objfile);
9514 /* We don't need this any more. */
9515 cu->rust_unions.clear ();
9516 }
9517
9518 /* See read.h. */
9519
9520 type_unit_group_unshareable *
9521 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9522 {
9523 auto iter = this->m_type_units.find (tu_group);
9524 if (iter != this->m_type_units.end ())
9525 return iter->second.get ();
9526
9527 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9528 type_unit_group_unshareable *result = uniq.get ();
9529 this->m_type_units[tu_group] = std::move (uniq);
9530 return result;
9531 }
9532
9533 struct type *
9534 dwarf2_per_objfile::get_type_for_signatured_type
9535 (signatured_type *sig_type) const
9536 {
9537 auto iter = this->m_type_map.find (sig_type);
9538 if (iter == this->m_type_map.end ())
9539 return nullptr;
9540
9541 return iter->second;
9542 }
9543
9544 void dwarf2_per_objfile::set_type_for_signatured_type
9545 (signatured_type *sig_type, struct type *type)
9546 {
9547 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9548
9549 this->m_type_map[sig_type] = type;
9550 }
9551
9552 /* A helper function for computing the list of all symbol tables
9553 included by PER_CU. */
9554
9555 static void
9556 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9557 htab_t all_children, htab_t all_type_symtabs,
9558 dwarf2_per_cu_data *per_cu,
9559 dwarf2_per_objfile *per_objfile,
9560 struct compunit_symtab *immediate_parent)
9561 {
9562 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9563 if (*slot != NULL)
9564 {
9565 /* This inclusion and its children have been processed. */
9566 return;
9567 }
9568
9569 *slot = per_cu;
9570
9571 /* Only add a CU if it has a symbol table. */
9572 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9573 if (cust != NULL)
9574 {
9575 /* If this is a type unit only add its symbol table if we haven't
9576 seen it yet (type unit per_cu's can share symtabs). */
9577 if (per_cu->is_debug_types)
9578 {
9579 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9580 if (*slot == NULL)
9581 {
9582 *slot = cust;
9583 result->push_back (cust);
9584 if (cust->user == NULL)
9585 cust->user = immediate_parent;
9586 }
9587 }
9588 else
9589 {
9590 result->push_back (cust);
9591 if (cust->user == NULL)
9592 cust->user = immediate_parent;
9593 }
9594 }
9595
9596 if (!per_cu->imported_symtabs_empty ())
9597 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9598 {
9599 recursively_compute_inclusions (result, all_children,
9600 all_type_symtabs, ptr, per_objfile,
9601 cust);
9602 }
9603 }
9604
9605 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9606 PER_CU. */
9607
9608 static void
9609 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9610 dwarf2_per_objfile *per_objfile)
9611 {
9612 gdb_assert (! per_cu->is_debug_types);
9613
9614 if (!per_cu->imported_symtabs_empty ())
9615 {
9616 int len;
9617 std::vector<compunit_symtab *> result_symtabs;
9618 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9619
9620 /* If we don't have a symtab, we can just skip this case. */
9621 if (cust == NULL)
9622 return;
9623
9624 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9625 htab_eq_pointer,
9626 NULL, xcalloc, xfree));
9627 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9628 htab_eq_pointer,
9629 NULL, xcalloc, xfree));
9630
9631 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9632 {
9633 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9634 all_type_symtabs.get (), ptr,
9635 per_objfile, cust);
9636 }
9637
9638 /* Now we have a transitive closure of all the included symtabs. */
9639 len = result_symtabs.size ();
9640 cust->includes
9641 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9642 struct compunit_symtab *, len + 1);
9643 memcpy (cust->includes, result_symtabs.data (),
9644 len * sizeof (compunit_symtab *));
9645 cust->includes[len] = NULL;
9646 }
9647 }
9648
9649 /* Compute the 'includes' field for the symtabs of all the CUs we just
9650 read. */
9651
9652 static void
9653 process_cu_includes (dwarf2_per_objfile *per_objfile)
9654 {
9655 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9656 {
9657 if (! iter->is_debug_types)
9658 compute_compunit_symtab_includes (iter, per_objfile);
9659 }
9660
9661 per_objfile->per_bfd->just_read_cus.clear ();
9662 }
9663
9664 /* Generate full symbol information for CU, whose DIEs have
9665 already been loaded into memory. */
9666
9667 static void
9668 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9669 {
9670 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9671 struct objfile *objfile = per_objfile->objfile;
9672 struct gdbarch *gdbarch = objfile->arch ();
9673 CORE_ADDR lowpc, highpc;
9674 struct compunit_symtab *cust;
9675 CORE_ADDR baseaddr;
9676 struct block *static_block;
9677 CORE_ADDR addr;
9678
9679 baseaddr = objfile->text_section_offset ();
9680
9681 /* Clear the list here in case something was left over. */
9682 cu->method_list.clear ();
9683
9684 cu->language = pretend_language;
9685 cu->language_defn = language_def (cu->language);
9686
9687 dwarf2_find_base_address (cu->dies, cu);
9688
9689 /* Before we start reading the top-level DIE, ensure it has a valid tag
9690 type. */
9691 switch (cu->dies->tag)
9692 {
9693 case DW_TAG_compile_unit:
9694 case DW_TAG_partial_unit:
9695 case DW_TAG_type_unit:
9696 break;
9697 default:
9698 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9699 dwarf_tag_name (cu->dies->tag),
9700 sect_offset_str (cu->per_cu->sect_off),
9701 objfile_name (per_objfile->objfile));
9702 }
9703
9704 /* Do line number decoding in read_file_scope () */
9705 process_die (cu->dies, cu);
9706
9707 /* For now fudge the Go package. */
9708 if (cu->language == language_go)
9709 fixup_go_packaging (cu);
9710
9711 /* Now that we have processed all the DIEs in the CU, all the types
9712 should be complete, and it should now be safe to compute all of the
9713 physnames. */
9714 compute_delayed_physnames (cu);
9715
9716 if (cu->language == language_rust)
9717 rust_union_quirks (cu);
9718
9719 /* Some compilers don't define a DW_AT_high_pc attribute for the
9720 compilation unit. If the DW_AT_high_pc is missing, synthesize
9721 it, by scanning the DIE's below the compilation unit. */
9722 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9723
9724 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9725 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9726
9727 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9728 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9729 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9730 addrmap to help ensure it has an accurate map of pc values belonging to
9731 this comp unit. */
9732 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9733
9734 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9735 SECT_OFF_TEXT (objfile),
9736 0);
9737
9738 if (cust != NULL)
9739 {
9740 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9741
9742 /* Set symtab language to language from DW_AT_language. If the
9743 compilation is from a C file generated by language preprocessors, do
9744 not set the language if it was already deduced by start_subfile. */
9745 if (!(cu->language == language_c
9746 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9747 COMPUNIT_FILETABS (cust)->language = cu->language;
9748
9749 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9750 produce DW_AT_location with location lists but it can be possibly
9751 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9752 there were bugs in prologue debug info, fixed later in GCC-4.5
9753 by "unwind info for epilogues" patch (which is not directly related).
9754
9755 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9756 needed, it would be wrong due to missing DW_AT_producer there.
9757
9758 Still one can confuse GDB by using non-standard GCC compilation
9759 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9760 */
9761 if (cu->has_loclist && gcc_4_minor >= 5)
9762 cust->locations_valid = 1;
9763
9764 if (gcc_4_minor >= 5)
9765 cust->epilogue_unwind_valid = 1;
9766
9767 cust->call_site_htab = cu->call_site_htab;
9768 }
9769
9770 per_objfile->set_symtab (cu->per_cu, cust);
9771
9772 /* Push it for inclusion processing later. */
9773 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9774
9775 /* Not needed any more. */
9776 cu->reset_builder ();
9777 }
9778
9779 /* Generate full symbol information for type unit CU, whose DIEs have
9780 already been loaded into memory. */
9781
9782 static void
9783 process_full_type_unit (dwarf2_cu *cu,
9784 enum language pretend_language)
9785 {
9786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9787 struct objfile *objfile = per_objfile->objfile;
9788 struct compunit_symtab *cust;
9789 struct signatured_type *sig_type;
9790
9791 gdb_assert (cu->per_cu->is_debug_types);
9792 sig_type = (struct signatured_type *) cu->per_cu;
9793
9794 /* Clear the list here in case something was left over. */
9795 cu->method_list.clear ();
9796
9797 cu->language = pretend_language;
9798 cu->language_defn = language_def (cu->language);
9799
9800 /* The symbol tables are set up in read_type_unit_scope. */
9801 process_die (cu->dies, cu);
9802
9803 /* For now fudge the Go package. */
9804 if (cu->language == language_go)
9805 fixup_go_packaging (cu);
9806
9807 /* Now that we have processed all the DIEs in the CU, all the types
9808 should be complete, and it should now be safe to compute all of the
9809 physnames. */
9810 compute_delayed_physnames (cu);
9811
9812 if (cu->language == language_rust)
9813 rust_union_quirks (cu);
9814
9815 /* TUs share symbol tables.
9816 If this is the first TU to use this symtab, complete the construction
9817 of it with end_expandable_symtab. Otherwise, complete the addition of
9818 this TU's symbols to the existing symtab. */
9819 type_unit_group_unshareable *tug_unshare =
9820 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9821 if (tug_unshare->compunit_symtab == NULL)
9822 {
9823 buildsym_compunit *builder = cu->get_builder ();
9824 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9825 tug_unshare->compunit_symtab = cust;
9826
9827 if (cust != NULL)
9828 {
9829 /* Set symtab language to language from DW_AT_language. If the
9830 compilation is from a C file generated by language preprocessors,
9831 do not set the language if it was already deduced by
9832 start_subfile. */
9833 if (!(cu->language == language_c
9834 && COMPUNIT_FILETABS (cust)->language != language_c))
9835 COMPUNIT_FILETABS (cust)->language = cu->language;
9836 }
9837 }
9838 else
9839 {
9840 cu->get_builder ()->augment_type_symtab ();
9841 cust = tug_unshare->compunit_symtab;
9842 }
9843
9844 per_objfile->set_symtab (cu->per_cu, cust);
9845
9846 /* Not needed any more. */
9847 cu->reset_builder ();
9848 }
9849
9850 /* Process an imported unit DIE. */
9851
9852 static void
9853 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9854 {
9855 struct attribute *attr;
9856
9857 /* For now we don't handle imported units in type units. */
9858 if (cu->per_cu->is_debug_types)
9859 {
9860 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9861 " supported in type units [in module %s]"),
9862 objfile_name (cu->per_objfile->objfile));
9863 }
9864
9865 attr = dwarf2_attr (die, DW_AT_import, cu);
9866 if (attr != NULL)
9867 {
9868 sect_offset sect_off = attr->get_ref_die_offset ();
9869 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9871 dwarf2_per_cu_data *per_cu
9872 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9873
9874 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9875 into another compilation unit, at root level. Regard this as a hint,
9876 and ignore it. */
9877 if (die->parent && die->parent->parent == NULL
9878 && per_cu->unit_type == DW_UT_compile
9879 && per_cu->lang == language_cplus)
9880 return;
9881
9882 /* If necessary, add it to the queue and load its DIEs. */
9883 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9884 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9885 false, cu->language);
9886
9887 cu->per_cu->imported_symtabs_push (per_cu);
9888 }
9889 }
9890
9891 /* RAII object that represents a process_die scope: i.e.,
9892 starts/finishes processing a DIE. */
9893 class process_die_scope
9894 {
9895 public:
9896 process_die_scope (die_info *die, dwarf2_cu *cu)
9897 : m_die (die), m_cu (cu)
9898 {
9899 /* We should only be processing DIEs not already in process. */
9900 gdb_assert (!m_die->in_process);
9901 m_die->in_process = true;
9902 }
9903
9904 ~process_die_scope ()
9905 {
9906 m_die->in_process = false;
9907
9908 /* If we're done processing the DIE for the CU that owns the line
9909 header, we don't need the line header anymore. */
9910 if (m_cu->line_header_die_owner == m_die)
9911 {
9912 delete m_cu->line_header;
9913 m_cu->line_header = NULL;
9914 m_cu->line_header_die_owner = NULL;
9915 }
9916 }
9917
9918 private:
9919 die_info *m_die;
9920 dwarf2_cu *m_cu;
9921 };
9922
9923 /* Process a die and its children. */
9924
9925 static void
9926 process_die (struct die_info *die, struct dwarf2_cu *cu)
9927 {
9928 process_die_scope scope (die, cu);
9929
9930 switch (die->tag)
9931 {
9932 case DW_TAG_padding:
9933 break;
9934 case DW_TAG_compile_unit:
9935 case DW_TAG_partial_unit:
9936 read_file_scope (die, cu);
9937 break;
9938 case DW_TAG_type_unit:
9939 read_type_unit_scope (die, cu);
9940 break;
9941 case DW_TAG_subprogram:
9942 /* Nested subprograms in Fortran get a prefix. */
9943 if (cu->language == language_fortran
9944 && die->parent != NULL
9945 && die->parent->tag == DW_TAG_subprogram)
9946 cu->processing_has_namespace_info = true;
9947 /* Fall through. */
9948 case DW_TAG_inlined_subroutine:
9949 read_func_scope (die, cu);
9950 break;
9951 case DW_TAG_lexical_block:
9952 case DW_TAG_try_block:
9953 case DW_TAG_catch_block:
9954 read_lexical_block_scope (die, cu);
9955 break;
9956 case DW_TAG_call_site:
9957 case DW_TAG_GNU_call_site:
9958 read_call_site_scope (die, cu);
9959 break;
9960 case DW_TAG_class_type:
9961 case DW_TAG_interface_type:
9962 case DW_TAG_structure_type:
9963 case DW_TAG_union_type:
9964 process_structure_scope (die, cu);
9965 break;
9966 case DW_TAG_enumeration_type:
9967 process_enumeration_scope (die, cu);
9968 break;
9969
9970 /* These dies have a type, but processing them does not create
9971 a symbol or recurse to process the children. Therefore we can
9972 read them on-demand through read_type_die. */
9973 case DW_TAG_subroutine_type:
9974 case DW_TAG_set_type:
9975 case DW_TAG_pointer_type:
9976 case DW_TAG_ptr_to_member_type:
9977 case DW_TAG_reference_type:
9978 case DW_TAG_rvalue_reference_type:
9979 case DW_TAG_string_type:
9980 break;
9981
9982 case DW_TAG_array_type:
9983 /* We only need to handle this case for Ada -- in other
9984 languages, it's normal for the compiler to emit a typedef
9985 instead. */
9986 if (cu->language != language_ada)
9987 break;
9988 /* FALLTHROUGH */
9989 case DW_TAG_base_type:
9990 case DW_TAG_subrange_type:
9991 case DW_TAG_typedef:
9992 /* Add a typedef symbol for the type definition, if it has a
9993 DW_AT_name. */
9994 new_symbol (die, read_type_die (die, cu), cu);
9995 break;
9996 case DW_TAG_common_block:
9997 read_common_block (die, cu);
9998 break;
9999 case DW_TAG_common_inclusion:
10000 break;
10001 case DW_TAG_namespace:
10002 cu->processing_has_namespace_info = true;
10003 read_namespace (die, cu);
10004 break;
10005 case DW_TAG_module:
10006 cu->processing_has_namespace_info = true;
10007 read_module (die, cu);
10008 break;
10009 case DW_TAG_imported_declaration:
10010 cu->processing_has_namespace_info = true;
10011 if (read_namespace_alias (die, cu))
10012 break;
10013 /* The declaration is not a global namespace alias. */
10014 /* Fall through. */
10015 case DW_TAG_imported_module:
10016 cu->processing_has_namespace_info = true;
10017 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10018 || cu->language != language_fortran))
10019 complaint (_("Tag '%s' has unexpected children"),
10020 dwarf_tag_name (die->tag));
10021 read_import_statement (die, cu);
10022 break;
10023
10024 case DW_TAG_imported_unit:
10025 process_imported_unit_die (die, cu);
10026 break;
10027
10028 case DW_TAG_variable:
10029 read_variable (die, cu);
10030 break;
10031
10032 default:
10033 new_symbol (die, NULL, cu);
10034 break;
10035 }
10036 }
10037 \f
10038 /* DWARF name computation. */
10039
10040 /* A helper function for dwarf2_compute_name which determines whether DIE
10041 needs to have the name of the scope prepended to the name listed in the
10042 die. */
10043
10044 static int
10045 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10046 {
10047 struct attribute *attr;
10048
10049 switch (die->tag)
10050 {
10051 case DW_TAG_namespace:
10052 case DW_TAG_typedef:
10053 case DW_TAG_class_type:
10054 case DW_TAG_interface_type:
10055 case DW_TAG_structure_type:
10056 case DW_TAG_union_type:
10057 case DW_TAG_enumeration_type:
10058 case DW_TAG_enumerator:
10059 case DW_TAG_subprogram:
10060 case DW_TAG_inlined_subroutine:
10061 case DW_TAG_member:
10062 case DW_TAG_imported_declaration:
10063 return 1;
10064
10065 case DW_TAG_variable:
10066 case DW_TAG_constant:
10067 /* We only need to prefix "globally" visible variables. These include
10068 any variable marked with DW_AT_external or any variable that
10069 lives in a namespace. [Variables in anonymous namespaces
10070 require prefixing, but they are not DW_AT_external.] */
10071
10072 if (dwarf2_attr (die, DW_AT_specification, cu))
10073 {
10074 struct dwarf2_cu *spec_cu = cu;
10075
10076 return die_needs_namespace (die_specification (die, &spec_cu),
10077 spec_cu);
10078 }
10079
10080 attr = dwarf2_attr (die, DW_AT_external, cu);
10081 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10082 && die->parent->tag != DW_TAG_module)
10083 return 0;
10084 /* A variable in a lexical block of some kind does not need a
10085 namespace, even though in C++ such variables may be external
10086 and have a mangled name. */
10087 if (die->parent->tag == DW_TAG_lexical_block
10088 || die->parent->tag == DW_TAG_try_block
10089 || die->parent->tag == DW_TAG_catch_block
10090 || die->parent->tag == DW_TAG_subprogram)
10091 return 0;
10092 return 1;
10093
10094 default:
10095 return 0;
10096 }
10097 }
10098
10099 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10100 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10101 defined for the given DIE. */
10102
10103 static struct attribute *
10104 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10105 {
10106 struct attribute *attr;
10107
10108 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10109 if (attr == NULL)
10110 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10111
10112 return attr;
10113 }
10114
10115 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10116 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10117 defined for the given DIE. */
10118
10119 static const char *
10120 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10121 {
10122 const char *linkage_name;
10123
10124 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10125 if (linkage_name == NULL)
10126 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10127
10128 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10129 See https://github.com/rust-lang/rust/issues/32925. */
10130 if (cu->language == language_rust && linkage_name != NULL
10131 && strchr (linkage_name, '{') != NULL)
10132 linkage_name = NULL;
10133
10134 return linkage_name;
10135 }
10136
10137 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10138 compute the physname for the object, which include a method's:
10139 - formal parameters (C++),
10140 - receiver type (Go),
10141
10142 The term "physname" is a bit confusing.
10143 For C++, for example, it is the demangled name.
10144 For Go, for example, it's the mangled name.
10145
10146 For Ada, return the DIE's linkage name rather than the fully qualified
10147 name. PHYSNAME is ignored..
10148
10149 The result is allocated on the objfile->per_bfd's obstack and
10150 canonicalized. */
10151
10152 static const char *
10153 dwarf2_compute_name (const char *name,
10154 struct die_info *die, struct dwarf2_cu *cu,
10155 int physname)
10156 {
10157 struct objfile *objfile = cu->per_objfile->objfile;
10158
10159 if (name == NULL)
10160 name = dwarf2_name (die, cu);
10161
10162 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10163 but otherwise compute it by typename_concat inside GDB.
10164 FIXME: Actually this is not really true, or at least not always true.
10165 It's all very confusing. compute_and_set_names doesn't try to demangle
10166 Fortran names because there is no mangling standard. So new_symbol
10167 will set the demangled name to the result of dwarf2_full_name, and it is
10168 the demangled name that GDB uses if it exists. */
10169 if (cu->language == language_ada
10170 || (cu->language == language_fortran && physname))
10171 {
10172 /* For Ada unit, we prefer the linkage name over the name, as
10173 the former contains the exported name, which the user expects
10174 to be able to reference. Ideally, we want the user to be able
10175 to reference this entity using either natural or linkage name,
10176 but we haven't started looking at this enhancement yet. */
10177 const char *linkage_name = dw2_linkage_name (die, cu);
10178
10179 if (linkage_name != NULL)
10180 return linkage_name;
10181 }
10182
10183 /* These are the only languages we know how to qualify names in. */
10184 if (name != NULL
10185 && (cu->language == language_cplus
10186 || cu->language == language_fortran || cu->language == language_d
10187 || cu->language == language_rust))
10188 {
10189 if (die_needs_namespace (die, cu))
10190 {
10191 const char *prefix;
10192 const char *canonical_name = NULL;
10193
10194 string_file buf;
10195
10196 prefix = determine_prefix (die, cu);
10197 if (*prefix != '\0')
10198 {
10199 gdb::unique_xmalloc_ptr<char> prefixed_name
10200 (typename_concat (NULL, prefix, name, physname, cu));
10201
10202 buf.puts (prefixed_name.get ());
10203 }
10204 else
10205 buf.puts (name);
10206
10207 /* Template parameters may be specified in the DIE's DW_AT_name, or
10208 as children with DW_TAG_template_type_param or
10209 DW_TAG_value_type_param. If the latter, add them to the name
10210 here. If the name already has template parameters, then
10211 skip this step; some versions of GCC emit both, and
10212 it is more efficient to use the pre-computed name.
10213
10214 Something to keep in mind about this process: it is very
10215 unlikely, or in some cases downright impossible, to produce
10216 something that will match the mangled name of a function.
10217 If the definition of the function has the same debug info,
10218 we should be able to match up with it anyway. But fallbacks
10219 using the minimal symbol, for instance to find a method
10220 implemented in a stripped copy of libstdc++, will not work.
10221 If we do not have debug info for the definition, we will have to
10222 match them up some other way.
10223
10224 When we do name matching there is a related problem with function
10225 templates; two instantiated function templates are allowed to
10226 differ only by their return types, which we do not add here. */
10227
10228 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10229 {
10230 struct attribute *attr;
10231 struct die_info *child;
10232 int first = 1;
10233 const language_defn *cplus_lang = language_def (cu->language);
10234
10235 die->building_fullname = 1;
10236
10237 for (child = die->child; child != NULL; child = child->sibling)
10238 {
10239 struct type *type;
10240 LONGEST value;
10241 const gdb_byte *bytes;
10242 struct dwarf2_locexpr_baton *baton;
10243 struct value *v;
10244
10245 if (child->tag != DW_TAG_template_type_param
10246 && child->tag != DW_TAG_template_value_param)
10247 continue;
10248
10249 if (first)
10250 {
10251 buf.puts ("<");
10252 first = 0;
10253 }
10254 else
10255 buf.puts (", ");
10256
10257 attr = dwarf2_attr (child, DW_AT_type, cu);
10258 if (attr == NULL)
10259 {
10260 complaint (_("template parameter missing DW_AT_type"));
10261 buf.puts ("UNKNOWN_TYPE");
10262 continue;
10263 }
10264 type = die_type (child, cu);
10265
10266 if (child->tag == DW_TAG_template_type_param)
10267 {
10268 cplus_lang->print_type (type, "", &buf, -1, 0,
10269 &type_print_raw_options);
10270 continue;
10271 }
10272
10273 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10274 if (attr == NULL)
10275 {
10276 complaint (_("template parameter missing "
10277 "DW_AT_const_value"));
10278 buf.puts ("UNKNOWN_VALUE");
10279 continue;
10280 }
10281
10282 dwarf2_const_value_attr (attr, type, name,
10283 &cu->comp_unit_obstack, cu,
10284 &value, &bytes, &baton);
10285
10286 if (type->has_no_signedness ())
10287 /* GDB prints characters as NUMBER 'CHAR'. If that's
10288 changed, this can use value_print instead. */
10289 cplus_lang->printchar (value, type, &buf);
10290 else
10291 {
10292 struct value_print_options opts;
10293
10294 if (baton != NULL)
10295 v = dwarf2_evaluate_loc_desc (type, NULL,
10296 baton->data,
10297 baton->size,
10298 baton->per_cu,
10299 baton->per_objfile);
10300 else if (bytes != NULL)
10301 {
10302 v = allocate_value (type);
10303 memcpy (value_contents_writeable (v), bytes,
10304 TYPE_LENGTH (type));
10305 }
10306 else
10307 v = value_from_longest (type, value);
10308
10309 /* Specify decimal so that we do not depend on
10310 the radix. */
10311 get_formatted_print_options (&opts, 'd');
10312 opts.raw = 1;
10313 value_print (v, &buf, &opts);
10314 release_value (v);
10315 }
10316 }
10317
10318 die->building_fullname = 0;
10319
10320 if (!first)
10321 {
10322 /* Close the argument list, with a space if necessary
10323 (nested templates). */
10324 if (!buf.empty () && buf.string ().back () == '>')
10325 buf.puts (" >");
10326 else
10327 buf.puts (">");
10328 }
10329 }
10330
10331 /* For C++ methods, append formal parameter type
10332 information, if PHYSNAME. */
10333
10334 if (physname && die->tag == DW_TAG_subprogram
10335 && cu->language == language_cplus)
10336 {
10337 struct type *type = read_type_die (die, cu);
10338
10339 c_type_print_args (type, &buf, 1, cu->language,
10340 &type_print_raw_options);
10341
10342 if (cu->language == language_cplus)
10343 {
10344 /* Assume that an artificial first parameter is
10345 "this", but do not crash if it is not. RealView
10346 marks unnamed (and thus unused) parameters as
10347 artificial; there is no way to differentiate
10348 the two cases. */
10349 if (type->num_fields () > 0
10350 && TYPE_FIELD_ARTIFICIAL (type, 0)
10351 && type->field (0).type ()->code () == TYPE_CODE_PTR
10352 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10353 buf.puts (" const");
10354 }
10355 }
10356
10357 const std::string &intermediate_name = buf.string ();
10358
10359 if (cu->language == language_cplus)
10360 canonical_name
10361 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10362 objfile);
10363
10364 /* If we only computed INTERMEDIATE_NAME, or if
10365 INTERMEDIATE_NAME is already canonical, then we need to
10366 intern it. */
10367 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10368 name = objfile->intern (intermediate_name);
10369 else
10370 name = canonical_name;
10371 }
10372 }
10373
10374 return name;
10375 }
10376
10377 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10378 If scope qualifiers are appropriate they will be added. The result
10379 will be allocated on the storage_obstack, or NULL if the DIE does
10380 not have a name. NAME may either be from a previous call to
10381 dwarf2_name or NULL.
10382
10383 The output string will be canonicalized (if C++). */
10384
10385 static const char *
10386 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10387 {
10388 return dwarf2_compute_name (name, die, cu, 0);
10389 }
10390
10391 /* Construct a physname for the given DIE in CU. NAME may either be
10392 from a previous call to dwarf2_name or NULL. The result will be
10393 allocated on the objfile_objstack or NULL if the DIE does not have a
10394 name.
10395
10396 The output string will be canonicalized (if C++). */
10397
10398 static const char *
10399 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10400 {
10401 struct objfile *objfile = cu->per_objfile->objfile;
10402 const char *retval, *mangled = NULL, *canon = NULL;
10403 int need_copy = 1;
10404
10405 /* In this case dwarf2_compute_name is just a shortcut not building anything
10406 on its own. */
10407 if (!die_needs_namespace (die, cu))
10408 return dwarf2_compute_name (name, die, cu, 1);
10409
10410 if (cu->language != language_rust)
10411 mangled = dw2_linkage_name (die, cu);
10412
10413 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10414 has computed. */
10415 gdb::unique_xmalloc_ptr<char> demangled;
10416 if (mangled != NULL)
10417 {
10418
10419 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10420 {
10421 /* Do nothing (do not demangle the symbol name). */
10422 }
10423 else
10424 {
10425 /* Use DMGL_RET_DROP for C++ template functions to suppress
10426 their return type. It is easier for GDB users to search
10427 for such functions as `name(params)' than `long name(params)'.
10428 In such case the minimal symbol names do not match the full
10429 symbol names but for template functions there is never a need
10430 to look up their definition from their declaration so
10431 the only disadvantage remains the minimal symbol variant
10432 `long name(params)' does not have the proper inferior type. */
10433 demangled.reset (gdb_demangle (mangled,
10434 (DMGL_PARAMS | DMGL_ANSI
10435 | DMGL_RET_DROP)));
10436 }
10437 if (demangled)
10438 canon = demangled.get ();
10439 else
10440 {
10441 canon = mangled;
10442 need_copy = 0;
10443 }
10444 }
10445
10446 if (canon == NULL || check_physname)
10447 {
10448 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10449
10450 if (canon != NULL && strcmp (physname, canon) != 0)
10451 {
10452 /* It may not mean a bug in GDB. The compiler could also
10453 compute DW_AT_linkage_name incorrectly. But in such case
10454 GDB would need to be bug-to-bug compatible. */
10455
10456 complaint (_("Computed physname <%s> does not match demangled <%s> "
10457 "(from linkage <%s>) - DIE at %s [in module %s]"),
10458 physname, canon, mangled, sect_offset_str (die->sect_off),
10459 objfile_name (objfile));
10460
10461 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10462 is available here - over computed PHYSNAME. It is safer
10463 against both buggy GDB and buggy compilers. */
10464
10465 retval = canon;
10466 }
10467 else
10468 {
10469 retval = physname;
10470 need_copy = 0;
10471 }
10472 }
10473 else
10474 retval = canon;
10475
10476 if (need_copy)
10477 retval = objfile->intern (retval);
10478
10479 return retval;
10480 }
10481
10482 /* Inspect DIE in CU for a namespace alias. If one exists, record
10483 a new symbol for it.
10484
10485 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10486
10487 static int
10488 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10489 {
10490 struct attribute *attr;
10491
10492 /* If the die does not have a name, this is not a namespace
10493 alias. */
10494 attr = dwarf2_attr (die, DW_AT_name, cu);
10495 if (attr != NULL)
10496 {
10497 int num;
10498 struct die_info *d = die;
10499 struct dwarf2_cu *imported_cu = cu;
10500
10501 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10502 keep inspecting DIEs until we hit the underlying import. */
10503 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10504 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10505 {
10506 attr = dwarf2_attr (d, DW_AT_import, cu);
10507 if (attr == NULL)
10508 break;
10509
10510 d = follow_die_ref (d, attr, &imported_cu);
10511 if (d->tag != DW_TAG_imported_declaration)
10512 break;
10513 }
10514
10515 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10516 {
10517 complaint (_("DIE at %s has too many recursively imported "
10518 "declarations"), sect_offset_str (d->sect_off));
10519 return 0;
10520 }
10521
10522 if (attr != NULL)
10523 {
10524 struct type *type;
10525 sect_offset sect_off = attr->get_ref_die_offset ();
10526
10527 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10528 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10529 {
10530 /* This declaration is a global namespace alias. Add
10531 a symbol for it whose type is the aliased namespace. */
10532 new_symbol (die, type, cu);
10533 return 1;
10534 }
10535 }
10536 }
10537
10538 return 0;
10539 }
10540
10541 /* Return the using directives repository (global or local?) to use in the
10542 current context for CU.
10543
10544 For Ada, imported declarations can materialize renamings, which *may* be
10545 global. However it is impossible (for now?) in DWARF to distinguish
10546 "external" imported declarations and "static" ones. As all imported
10547 declarations seem to be static in all other languages, make them all CU-wide
10548 global only in Ada. */
10549
10550 static struct using_direct **
10551 using_directives (struct dwarf2_cu *cu)
10552 {
10553 if (cu->language == language_ada
10554 && cu->get_builder ()->outermost_context_p ())
10555 return cu->get_builder ()->get_global_using_directives ();
10556 else
10557 return cu->get_builder ()->get_local_using_directives ();
10558 }
10559
10560 /* Read the import statement specified by the given die and record it. */
10561
10562 static void
10563 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10564 {
10565 struct objfile *objfile = cu->per_objfile->objfile;
10566 struct attribute *import_attr;
10567 struct die_info *imported_die, *child_die;
10568 struct dwarf2_cu *imported_cu;
10569 const char *imported_name;
10570 const char *imported_name_prefix;
10571 const char *canonical_name;
10572 const char *import_alias;
10573 const char *imported_declaration = NULL;
10574 const char *import_prefix;
10575 std::vector<const char *> excludes;
10576
10577 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10578 if (import_attr == NULL)
10579 {
10580 complaint (_("Tag '%s' has no DW_AT_import"),
10581 dwarf_tag_name (die->tag));
10582 return;
10583 }
10584
10585 imported_cu = cu;
10586 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10587 imported_name = dwarf2_name (imported_die, imported_cu);
10588 if (imported_name == NULL)
10589 {
10590 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10591
10592 The import in the following code:
10593 namespace A
10594 {
10595 typedef int B;
10596 }
10597
10598 int main ()
10599 {
10600 using A::B;
10601 B b;
10602 return b;
10603 }
10604
10605 ...
10606 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10607 <52> DW_AT_decl_file : 1
10608 <53> DW_AT_decl_line : 6
10609 <54> DW_AT_import : <0x75>
10610 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10611 <59> DW_AT_name : B
10612 <5b> DW_AT_decl_file : 1
10613 <5c> DW_AT_decl_line : 2
10614 <5d> DW_AT_type : <0x6e>
10615 ...
10616 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10617 <76> DW_AT_byte_size : 4
10618 <77> DW_AT_encoding : 5 (signed)
10619
10620 imports the wrong die ( 0x75 instead of 0x58 ).
10621 This case will be ignored until the gcc bug is fixed. */
10622 return;
10623 }
10624
10625 /* Figure out the local name after import. */
10626 import_alias = dwarf2_name (die, cu);
10627
10628 /* Figure out where the statement is being imported to. */
10629 import_prefix = determine_prefix (die, cu);
10630
10631 /* Figure out what the scope of the imported die is and prepend it
10632 to the name of the imported die. */
10633 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10634
10635 if (imported_die->tag != DW_TAG_namespace
10636 && imported_die->tag != DW_TAG_module)
10637 {
10638 imported_declaration = imported_name;
10639 canonical_name = imported_name_prefix;
10640 }
10641 else if (strlen (imported_name_prefix) > 0)
10642 canonical_name = obconcat (&objfile->objfile_obstack,
10643 imported_name_prefix,
10644 (cu->language == language_d ? "." : "::"),
10645 imported_name, (char *) NULL);
10646 else
10647 canonical_name = imported_name;
10648
10649 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10650 for (child_die = die->child; child_die && child_die->tag;
10651 child_die = child_die->sibling)
10652 {
10653 /* DWARF-4: A Fortran use statement with a “rename list” may be
10654 represented by an imported module entry with an import attribute
10655 referring to the module and owned entries corresponding to those
10656 entities that are renamed as part of being imported. */
10657
10658 if (child_die->tag != DW_TAG_imported_declaration)
10659 {
10660 complaint (_("child DW_TAG_imported_declaration expected "
10661 "- DIE at %s [in module %s]"),
10662 sect_offset_str (child_die->sect_off),
10663 objfile_name (objfile));
10664 continue;
10665 }
10666
10667 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10668 if (import_attr == NULL)
10669 {
10670 complaint (_("Tag '%s' has no DW_AT_import"),
10671 dwarf_tag_name (child_die->tag));
10672 continue;
10673 }
10674
10675 imported_cu = cu;
10676 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10677 &imported_cu);
10678 imported_name = dwarf2_name (imported_die, imported_cu);
10679 if (imported_name == NULL)
10680 {
10681 complaint (_("child DW_TAG_imported_declaration has unknown "
10682 "imported name - DIE at %s [in module %s]"),
10683 sect_offset_str (child_die->sect_off),
10684 objfile_name (objfile));
10685 continue;
10686 }
10687
10688 excludes.push_back (imported_name);
10689
10690 process_die (child_die, cu);
10691 }
10692
10693 add_using_directive (using_directives (cu),
10694 import_prefix,
10695 canonical_name,
10696 import_alias,
10697 imported_declaration,
10698 excludes,
10699 0,
10700 &objfile->objfile_obstack);
10701 }
10702
10703 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10704 types, but gives them a size of zero. Starting with version 14,
10705 ICC is compatible with GCC. */
10706
10707 static bool
10708 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10709 {
10710 if (!cu->checked_producer)
10711 check_producer (cu);
10712
10713 return cu->producer_is_icc_lt_14;
10714 }
10715
10716 /* ICC generates a DW_AT_type for C void functions. This was observed on
10717 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10718 which says that void functions should not have a DW_AT_type. */
10719
10720 static bool
10721 producer_is_icc (struct dwarf2_cu *cu)
10722 {
10723 if (!cu->checked_producer)
10724 check_producer (cu);
10725
10726 return cu->producer_is_icc;
10727 }
10728
10729 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10730 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10731 this, it was first present in GCC release 4.3.0. */
10732
10733 static bool
10734 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10735 {
10736 if (!cu->checked_producer)
10737 check_producer (cu);
10738
10739 return cu->producer_is_gcc_lt_4_3;
10740 }
10741
10742 static file_and_directory
10743 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10744 {
10745 file_and_directory res;
10746
10747 /* Find the filename. Do not use dwarf2_name here, since the filename
10748 is not a source language identifier. */
10749 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10750 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10751
10752 if (res.comp_dir == NULL
10753 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10754 && IS_ABSOLUTE_PATH (res.name))
10755 {
10756 res.comp_dir_storage = ldirname (res.name);
10757 if (!res.comp_dir_storage.empty ())
10758 res.comp_dir = res.comp_dir_storage.c_str ();
10759 }
10760 if (res.comp_dir != NULL)
10761 {
10762 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10763 directory, get rid of it. */
10764 const char *cp = strchr (res.comp_dir, ':');
10765
10766 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10767 res.comp_dir = cp + 1;
10768 }
10769
10770 if (res.name == NULL)
10771 res.name = "<unknown>";
10772
10773 return res;
10774 }
10775
10776 /* Handle DW_AT_stmt_list for a compilation unit.
10777 DIE is the DW_TAG_compile_unit die for CU.
10778 COMP_DIR is the compilation directory. LOWPC is passed to
10779 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10780
10781 static void
10782 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10783 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10784 {
10785 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10786 struct attribute *attr;
10787 struct line_header line_header_local;
10788 hashval_t line_header_local_hash;
10789 void **slot;
10790 int decode_mapping;
10791
10792 gdb_assert (! cu->per_cu->is_debug_types);
10793
10794 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10795 if (attr == NULL || !attr->form_is_unsigned ())
10796 return;
10797
10798 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10799
10800 /* The line header hash table is only created if needed (it exists to
10801 prevent redundant reading of the line table for partial_units).
10802 If we're given a partial_unit, we'll need it. If we're given a
10803 compile_unit, then use the line header hash table if it's already
10804 created, but don't create one just yet. */
10805
10806 if (per_objfile->line_header_hash == NULL
10807 && die->tag == DW_TAG_partial_unit)
10808 {
10809 per_objfile->line_header_hash
10810 .reset (htab_create_alloc (127, line_header_hash_voidp,
10811 line_header_eq_voidp,
10812 free_line_header_voidp,
10813 xcalloc, xfree));
10814 }
10815
10816 line_header_local.sect_off = line_offset;
10817 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10818 line_header_local_hash = line_header_hash (&line_header_local);
10819 if (per_objfile->line_header_hash != NULL)
10820 {
10821 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10822 &line_header_local,
10823 line_header_local_hash, NO_INSERT);
10824
10825 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10826 is not present in *SLOT (since if there is something in *SLOT then
10827 it will be for a partial_unit). */
10828 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10829 {
10830 gdb_assert (*slot != NULL);
10831 cu->line_header = (struct line_header *) *slot;
10832 return;
10833 }
10834 }
10835
10836 /* dwarf_decode_line_header does not yet provide sufficient information.
10837 We always have to call also dwarf_decode_lines for it. */
10838 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10839 if (lh == NULL)
10840 return;
10841
10842 cu->line_header = lh.release ();
10843 cu->line_header_die_owner = die;
10844
10845 if (per_objfile->line_header_hash == NULL)
10846 slot = NULL;
10847 else
10848 {
10849 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10850 &line_header_local,
10851 line_header_local_hash, INSERT);
10852 gdb_assert (slot != NULL);
10853 }
10854 if (slot != NULL && *slot == NULL)
10855 {
10856 /* This newly decoded line number information unit will be owned
10857 by line_header_hash hash table. */
10858 *slot = cu->line_header;
10859 cu->line_header_die_owner = NULL;
10860 }
10861 else
10862 {
10863 /* We cannot free any current entry in (*slot) as that struct line_header
10864 may be already used by multiple CUs. Create only temporary decoded
10865 line_header for this CU - it may happen at most once for each line
10866 number information unit. And if we're not using line_header_hash
10867 then this is what we want as well. */
10868 gdb_assert (die->tag != DW_TAG_partial_unit);
10869 }
10870 decode_mapping = (die->tag != DW_TAG_partial_unit);
10871 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10872 decode_mapping);
10873
10874 }
10875
10876 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10877
10878 static void
10879 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10880 {
10881 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10882 struct objfile *objfile = per_objfile->objfile;
10883 struct gdbarch *gdbarch = objfile->arch ();
10884 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10885 CORE_ADDR highpc = ((CORE_ADDR) 0);
10886 struct attribute *attr;
10887 struct die_info *child_die;
10888 CORE_ADDR baseaddr;
10889
10890 prepare_one_comp_unit (cu, die, cu->language);
10891 baseaddr = objfile->text_section_offset ();
10892
10893 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10894
10895 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10896 from finish_block. */
10897 if (lowpc == ((CORE_ADDR) -1))
10898 lowpc = highpc;
10899 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10900
10901 file_and_directory fnd = find_file_and_directory (die, cu);
10902
10903 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10904 standardised yet. As a workaround for the language detection we fall
10905 back to the DW_AT_producer string. */
10906 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10907 cu->language = language_opencl;
10908
10909 /* Similar hack for Go. */
10910 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10911 set_cu_language (DW_LANG_Go, cu);
10912
10913 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10914
10915 /* Decode line number information if present. We do this before
10916 processing child DIEs, so that the line header table is available
10917 for DW_AT_decl_file. */
10918 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10919
10920 /* Process all dies in compilation unit. */
10921 if (die->child != NULL)
10922 {
10923 child_die = die->child;
10924 while (child_die && child_die->tag)
10925 {
10926 process_die (child_die, cu);
10927 child_die = child_die->sibling;
10928 }
10929 }
10930
10931 /* Decode macro information, if present. Dwarf 2 macro information
10932 refers to information in the line number info statement program
10933 header, so we can only read it if we've read the header
10934 successfully. */
10935 attr = dwarf2_attr (die, DW_AT_macros, cu);
10936 if (attr == NULL)
10937 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10938 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10939 {
10940 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10941 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10942
10943 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10944 }
10945 else
10946 {
10947 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10948 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10949 {
10950 unsigned int macro_offset = attr->as_unsigned ();
10951
10952 dwarf_decode_macros (cu, macro_offset, 0);
10953 }
10954 }
10955 }
10956
10957 void
10958 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10959 {
10960 struct type_unit_group *tu_group;
10961 int first_time;
10962 struct attribute *attr;
10963 unsigned int i;
10964 struct signatured_type *sig_type;
10965
10966 gdb_assert (per_cu->is_debug_types);
10967 sig_type = (struct signatured_type *) per_cu;
10968
10969 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10970
10971 /* If we're using .gdb_index (includes -readnow) then
10972 per_cu->type_unit_group may not have been set up yet. */
10973 if (sig_type->type_unit_group == NULL)
10974 sig_type->type_unit_group = get_type_unit_group (this, attr);
10975 tu_group = sig_type->type_unit_group;
10976
10977 /* If we've already processed this stmt_list there's no real need to
10978 do it again, we could fake it and just recreate the part we need
10979 (file name,index -> symtab mapping). If data shows this optimization
10980 is useful we can do it then. */
10981 type_unit_group_unshareable *tug_unshare
10982 = per_objfile->get_type_unit_group_unshareable (tu_group);
10983 first_time = tug_unshare->compunit_symtab == NULL;
10984
10985 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10986 debug info. */
10987 line_header_up lh;
10988 if (attr != NULL && attr->form_is_unsigned ())
10989 {
10990 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10991 lh = dwarf_decode_line_header (line_offset, this);
10992 }
10993 if (lh == NULL)
10994 {
10995 if (first_time)
10996 start_symtab ("", NULL, 0);
10997 else
10998 {
10999 gdb_assert (tug_unshare->symtabs == NULL);
11000 gdb_assert (m_builder == nullptr);
11001 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11002 m_builder.reset (new struct buildsym_compunit
11003 (COMPUNIT_OBJFILE (cust), "",
11004 COMPUNIT_DIRNAME (cust),
11005 compunit_language (cust),
11006 0, cust));
11007 list_in_scope = get_builder ()->get_file_symbols ();
11008 }
11009 return;
11010 }
11011
11012 line_header = lh.release ();
11013 line_header_die_owner = die;
11014
11015 if (first_time)
11016 {
11017 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11018
11019 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11020 still initializing it, and our caller (a few levels up)
11021 process_full_type_unit still needs to know if this is the first
11022 time. */
11023
11024 tug_unshare->symtabs
11025 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11026 struct symtab *, line_header->file_names_size ());
11027
11028 auto &file_names = line_header->file_names ();
11029 for (i = 0; i < file_names.size (); ++i)
11030 {
11031 file_entry &fe = file_names[i];
11032 dwarf2_start_subfile (this, fe.name,
11033 fe.include_dir (line_header));
11034 buildsym_compunit *b = get_builder ();
11035 if (b->get_current_subfile ()->symtab == NULL)
11036 {
11037 /* NOTE: start_subfile will recognize when it's been
11038 passed a file it has already seen. So we can't
11039 assume there's a simple mapping from
11040 cu->line_header->file_names to subfiles, plus
11041 cu->line_header->file_names may contain dups. */
11042 b->get_current_subfile ()->symtab
11043 = allocate_symtab (cust, b->get_current_subfile ()->name);
11044 }
11045
11046 fe.symtab = b->get_current_subfile ()->symtab;
11047 tug_unshare->symtabs[i] = fe.symtab;
11048 }
11049 }
11050 else
11051 {
11052 gdb_assert (m_builder == nullptr);
11053 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11054 m_builder.reset (new struct buildsym_compunit
11055 (COMPUNIT_OBJFILE (cust), "",
11056 COMPUNIT_DIRNAME (cust),
11057 compunit_language (cust),
11058 0, cust));
11059 list_in_scope = get_builder ()->get_file_symbols ();
11060
11061 auto &file_names = line_header->file_names ();
11062 for (i = 0; i < file_names.size (); ++i)
11063 {
11064 file_entry &fe = file_names[i];
11065 fe.symtab = tug_unshare->symtabs[i];
11066 }
11067 }
11068
11069 /* The main symtab is allocated last. Type units don't have DW_AT_name
11070 so they don't have a "real" (so to speak) symtab anyway.
11071 There is later code that will assign the main symtab to all symbols
11072 that don't have one. We need to handle the case of a symbol with a
11073 missing symtab (DW_AT_decl_file) anyway. */
11074 }
11075
11076 /* Process DW_TAG_type_unit.
11077 For TUs we want to skip the first top level sibling if it's not the
11078 actual type being defined by this TU. In this case the first top
11079 level sibling is there to provide context only. */
11080
11081 static void
11082 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11083 {
11084 struct die_info *child_die;
11085
11086 prepare_one_comp_unit (cu, die, language_minimal);
11087
11088 /* Initialize (or reinitialize) the machinery for building symtabs.
11089 We do this before processing child DIEs, so that the line header table
11090 is available for DW_AT_decl_file. */
11091 cu->setup_type_unit_groups (die);
11092
11093 if (die->child != NULL)
11094 {
11095 child_die = die->child;
11096 while (child_die && child_die->tag)
11097 {
11098 process_die (child_die, cu);
11099 child_die = child_die->sibling;
11100 }
11101 }
11102 }
11103 \f
11104 /* DWO/DWP files.
11105
11106 http://gcc.gnu.org/wiki/DebugFission
11107 http://gcc.gnu.org/wiki/DebugFissionDWP
11108
11109 To simplify handling of both DWO files ("object" files with the DWARF info)
11110 and DWP files (a file with the DWOs packaged up into one file), we treat
11111 DWP files as having a collection of virtual DWO files. */
11112
11113 static hashval_t
11114 hash_dwo_file (const void *item)
11115 {
11116 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11117 hashval_t hash;
11118
11119 hash = htab_hash_string (dwo_file->dwo_name);
11120 if (dwo_file->comp_dir != NULL)
11121 hash += htab_hash_string (dwo_file->comp_dir);
11122 return hash;
11123 }
11124
11125 static int
11126 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11127 {
11128 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11129 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11130
11131 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11132 return 0;
11133 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11134 return lhs->comp_dir == rhs->comp_dir;
11135 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11136 }
11137
11138 /* Allocate a hash table for DWO files. */
11139
11140 static htab_up
11141 allocate_dwo_file_hash_table ()
11142 {
11143 auto delete_dwo_file = [] (void *item)
11144 {
11145 struct dwo_file *dwo_file = (struct dwo_file *) item;
11146
11147 delete dwo_file;
11148 };
11149
11150 return htab_up (htab_create_alloc (41,
11151 hash_dwo_file,
11152 eq_dwo_file,
11153 delete_dwo_file,
11154 xcalloc, xfree));
11155 }
11156
11157 /* Lookup DWO file DWO_NAME. */
11158
11159 static void **
11160 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11161 const char *dwo_name,
11162 const char *comp_dir)
11163 {
11164 struct dwo_file find_entry;
11165 void **slot;
11166
11167 if (per_objfile->per_bfd->dwo_files == NULL)
11168 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11169
11170 find_entry.dwo_name = dwo_name;
11171 find_entry.comp_dir = comp_dir;
11172 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11173 INSERT);
11174
11175 return slot;
11176 }
11177
11178 static hashval_t
11179 hash_dwo_unit (const void *item)
11180 {
11181 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11182
11183 /* This drops the top 32 bits of the id, but is ok for a hash. */
11184 return dwo_unit->signature;
11185 }
11186
11187 static int
11188 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11189 {
11190 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11191 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11192
11193 /* The signature is assumed to be unique within the DWO file.
11194 So while object file CU dwo_id's always have the value zero,
11195 that's OK, assuming each object file DWO file has only one CU,
11196 and that's the rule for now. */
11197 return lhs->signature == rhs->signature;
11198 }
11199
11200 /* Allocate a hash table for DWO CUs,TUs.
11201 There is one of these tables for each of CUs,TUs for each DWO file. */
11202
11203 static htab_up
11204 allocate_dwo_unit_table ()
11205 {
11206 /* Start out with a pretty small number.
11207 Generally DWO files contain only one CU and maybe some TUs. */
11208 return htab_up (htab_create_alloc (3,
11209 hash_dwo_unit,
11210 eq_dwo_unit,
11211 NULL, xcalloc, xfree));
11212 }
11213
11214 /* die_reader_func for create_dwo_cu. */
11215
11216 static void
11217 create_dwo_cu_reader (const struct die_reader_specs *reader,
11218 const gdb_byte *info_ptr,
11219 struct die_info *comp_unit_die,
11220 struct dwo_file *dwo_file,
11221 struct dwo_unit *dwo_unit)
11222 {
11223 struct dwarf2_cu *cu = reader->cu;
11224 sect_offset sect_off = cu->per_cu->sect_off;
11225 struct dwarf2_section_info *section = cu->per_cu->section;
11226
11227 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11228 if (!signature.has_value ())
11229 {
11230 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11231 " its dwo_id [in module %s]"),
11232 sect_offset_str (sect_off), dwo_file->dwo_name);
11233 return;
11234 }
11235
11236 dwo_unit->dwo_file = dwo_file;
11237 dwo_unit->signature = *signature;
11238 dwo_unit->section = section;
11239 dwo_unit->sect_off = sect_off;
11240 dwo_unit->length = cu->per_cu->length;
11241
11242 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11243 sect_offset_str (sect_off),
11244 hex_string (dwo_unit->signature));
11245 }
11246
11247 /* Create the dwo_units for the CUs in a DWO_FILE.
11248 Note: This function processes DWO files only, not DWP files. */
11249
11250 static void
11251 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11252 dwarf2_cu *cu, struct dwo_file &dwo_file,
11253 dwarf2_section_info &section, htab_up &cus_htab)
11254 {
11255 struct objfile *objfile = per_objfile->objfile;
11256 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11257 const gdb_byte *info_ptr, *end_ptr;
11258
11259 section.read (objfile);
11260 info_ptr = section.buffer;
11261
11262 if (info_ptr == NULL)
11263 return;
11264
11265 dwarf_read_debug_printf ("Reading %s for %s:",
11266 section.get_name (),
11267 section.get_file_name ());
11268
11269 end_ptr = info_ptr + section.size;
11270 while (info_ptr < end_ptr)
11271 {
11272 struct dwarf2_per_cu_data per_cu;
11273 struct dwo_unit read_unit {};
11274 struct dwo_unit *dwo_unit;
11275 void **slot;
11276 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11277
11278 memset (&per_cu, 0, sizeof (per_cu));
11279 per_cu.per_bfd = per_bfd;
11280 per_cu.is_debug_types = 0;
11281 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11282 per_cu.section = &section;
11283
11284 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11285 if (!reader.dummy_p)
11286 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11287 &dwo_file, &read_unit);
11288 info_ptr += per_cu.length;
11289
11290 // If the unit could not be parsed, skip it.
11291 if (read_unit.dwo_file == NULL)
11292 continue;
11293
11294 if (cus_htab == NULL)
11295 cus_htab = allocate_dwo_unit_table ();
11296
11297 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11298 struct dwo_unit);
11299 *dwo_unit = read_unit;
11300 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11301 gdb_assert (slot != NULL);
11302 if (*slot != NULL)
11303 {
11304 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11305 sect_offset dup_sect_off = dup_cu->sect_off;
11306
11307 complaint (_("debug cu entry at offset %s is duplicate to"
11308 " the entry at offset %s, signature %s"),
11309 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11310 hex_string (dwo_unit->signature));
11311 }
11312 *slot = (void *)dwo_unit;
11313 }
11314 }
11315
11316 /* DWP file .debug_{cu,tu}_index section format:
11317 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11318 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11319
11320 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11321 officially standard DWP format was published with DWARF v5 and is called
11322 Version 5. There are no versions 3 or 4.
11323
11324 DWP Version 1:
11325
11326 Both index sections have the same format, and serve to map a 64-bit
11327 signature to a set of section numbers. Each section begins with a header,
11328 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11329 indexes, and a pool of 32-bit section numbers. The index sections will be
11330 aligned at 8-byte boundaries in the file.
11331
11332 The index section header consists of:
11333
11334 V, 32 bit version number
11335 -, 32 bits unused
11336 N, 32 bit number of compilation units or type units in the index
11337 M, 32 bit number of slots in the hash table
11338
11339 Numbers are recorded using the byte order of the application binary.
11340
11341 The hash table begins at offset 16 in the section, and consists of an array
11342 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11343 order of the application binary). Unused slots in the hash table are 0.
11344 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11345
11346 The parallel table begins immediately after the hash table
11347 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11348 array of 32-bit indexes (using the byte order of the application binary),
11349 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11350 table contains a 32-bit index into the pool of section numbers. For unused
11351 hash table slots, the corresponding entry in the parallel table will be 0.
11352
11353 The pool of section numbers begins immediately following the hash table
11354 (at offset 16 + 12 * M from the beginning of the section). The pool of
11355 section numbers consists of an array of 32-bit words (using the byte order
11356 of the application binary). Each item in the array is indexed starting
11357 from 0. The hash table entry provides the index of the first section
11358 number in the set. Additional section numbers in the set follow, and the
11359 set is terminated by a 0 entry (section number 0 is not used in ELF).
11360
11361 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11362 section must be the first entry in the set, and the .debug_abbrev.dwo must
11363 be the second entry. Other members of the set may follow in any order.
11364
11365 ---
11366
11367 DWP Versions 2 and 5:
11368
11369 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11370 and the entries in the index tables are now offsets into these sections.
11371 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11372 section.
11373
11374 Index Section Contents:
11375 Header
11376 Hash Table of Signatures dwp_hash_table.hash_table
11377 Parallel Table of Indices dwp_hash_table.unit_table
11378 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11379 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11380
11381 The index section header consists of:
11382
11383 V, 32 bit version number
11384 L, 32 bit number of columns in the table of section offsets
11385 N, 32 bit number of compilation units or type units in the index
11386 M, 32 bit number of slots in the hash table
11387
11388 Numbers are recorded using the byte order of the application binary.
11389
11390 The hash table has the same format as version 1.
11391 The parallel table of indices has the same format as version 1,
11392 except that the entries are origin-1 indices into the table of sections
11393 offsets and the table of section sizes.
11394
11395 The table of offsets begins immediately following the parallel table
11396 (at offset 16 + 12 * M from the beginning of the section). The table is
11397 a two-dimensional array of 32-bit words (using the byte order of the
11398 application binary), with L columns and N+1 rows, in row-major order.
11399 Each row in the array is indexed starting from 0. The first row provides
11400 a key to the remaining rows: each column in this row provides an identifier
11401 for a debug section, and the offsets in the same column of subsequent rows
11402 refer to that section. The section identifiers for Version 2 are:
11403
11404 DW_SECT_INFO 1 .debug_info.dwo
11405 DW_SECT_TYPES 2 .debug_types.dwo
11406 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11407 DW_SECT_LINE 4 .debug_line.dwo
11408 DW_SECT_LOC 5 .debug_loc.dwo
11409 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11410 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11411 DW_SECT_MACRO 8 .debug_macro.dwo
11412
11413 The section identifiers for Version 5 are:
11414
11415 DW_SECT_INFO_V5 1 .debug_info.dwo
11416 DW_SECT_RESERVED_V5 2 --
11417 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11418 DW_SECT_LINE_V5 4 .debug_line.dwo
11419 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11420 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11421 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11422 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11423
11424 The offsets provided by the CU and TU index sections are the base offsets
11425 for the contributions made by each CU or TU to the corresponding section
11426 in the package file. Each CU and TU header contains an abbrev_offset
11427 field, used to find the abbreviations table for that CU or TU within the
11428 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11429 be interpreted as relative to the base offset given in the index section.
11430 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11431 should be interpreted as relative to the base offset for .debug_line.dwo,
11432 and offsets into other debug sections obtained from DWARF attributes should
11433 also be interpreted as relative to the corresponding base offset.
11434
11435 The table of sizes begins immediately following the table of offsets.
11436 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11437 with L columns and N rows, in row-major order. Each row in the array is
11438 indexed starting from 1 (row 0 is shared by the two tables).
11439
11440 ---
11441
11442 Hash table lookup is handled the same in version 1 and 2:
11443
11444 We assume that N and M will not exceed 2^32 - 1.
11445 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11446
11447 Given a 64-bit compilation unit signature or a type signature S, an entry
11448 in the hash table is located as follows:
11449
11450 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11451 the low-order k bits all set to 1.
11452
11453 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11454
11455 3) If the hash table entry at index H matches the signature, use that
11456 entry. If the hash table entry at index H is unused (all zeroes),
11457 terminate the search: the signature is not present in the table.
11458
11459 4) Let H = (H + H') modulo M. Repeat at Step 3.
11460
11461 Because M > N and H' and M are relatively prime, the search is guaranteed
11462 to stop at an unused slot or find the match. */
11463
11464 /* Create a hash table to map DWO IDs to their CU/TU entry in
11465 .debug_{info,types}.dwo in DWP_FILE.
11466 Returns NULL if there isn't one.
11467 Note: This function processes DWP files only, not DWO files. */
11468
11469 static struct dwp_hash_table *
11470 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11471 struct dwp_file *dwp_file, int is_debug_types)
11472 {
11473 struct objfile *objfile = per_objfile->objfile;
11474 bfd *dbfd = dwp_file->dbfd.get ();
11475 const gdb_byte *index_ptr, *index_end;
11476 struct dwarf2_section_info *index;
11477 uint32_t version, nr_columns, nr_units, nr_slots;
11478 struct dwp_hash_table *htab;
11479
11480 if (is_debug_types)
11481 index = &dwp_file->sections.tu_index;
11482 else
11483 index = &dwp_file->sections.cu_index;
11484
11485 if (index->empty ())
11486 return NULL;
11487 index->read (objfile);
11488
11489 index_ptr = index->buffer;
11490 index_end = index_ptr + index->size;
11491
11492 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11493 For now it's safe to just read 4 bytes (particularly as it's difficult to
11494 tell if you're dealing with Version 5 before you've read the version). */
11495 version = read_4_bytes (dbfd, index_ptr);
11496 index_ptr += 4;
11497 if (version == 2 || version == 5)
11498 nr_columns = read_4_bytes (dbfd, index_ptr);
11499 else
11500 nr_columns = 0;
11501 index_ptr += 4;
11502 nr_units = read_4_bytes (dbfd, index_ptr);
11503 index_ptr += 4;
11504 nr_slots = read_4_bytes (dbfd, index_ptr);
11505 index_ptr += 4;
11506
11507 if (version != 1 && version != 2 && version != 5)
11508 {
11509 error (_("Dwarf Error: unsupported DWP file version (%s)"
11510 " [in module %s]"),
11511 pulongest (version), dwp_file->name);
11512 }
11513 if (nr_slots != (nr_slots & -nr_slots))
11514 {
11515 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11516 " is not power of 2 [in module %s]"),
11517 pulongest (nr_slots), dwp_file->name);
11518 }
11519
11520 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11521 htab->version = version;
11522 htab->nr_columns = nr_columns;
11523 htab->nr_units = nr_units;
11524 htab->nr_slots = nr_slots;
11525 htab->hash_table = index_ptr;
11526 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11527
11528 /* Exit early if the table is empty. */
11529 if (nr_slots == 0 || nr_units == 0
11530 || (version == 2 && nr_columns == 0)
11531 || (version == 5 && nr_columns == 0))
11532 {
11533 /* All must be zero. */
11534 if (nr_slots != 0 || nr_units != 0
11535 || (version == 2 && nr_columns != 0)
11536 || (version == 5 && nr_columns != 0))
11537 {
11538 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11539 " all zero [in modules %s]"),
11540 dwp_file->name);
11541 }
11542 return htab;
11543 }
11544
11545 if (version == 1)
11546 {
11547 htab->section_pool.v1.indices =
11548 htab->unit_table + sizeof (uint32_t) * nr_slots;
11549 /* It's harder to decide whether the section is too small in v1.
11550 V1 is deprecated anyway so we punt. */
11551 }
11552 else if (version == 2)
11553 {
11554 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11555 int *ids = htab->section_pool.v2.section_ids;
11556 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11557 /* Reverse map for error checking. */
11558 int ids_seen[DW_SECT_MAX + 1];
11559 int i;
11560
11561 if (nr_columns < 2)
11562 {
11563 error (_("Dwarf Error: bad DWP hash table, too few columns"
11564 " in section table [in module %s]"),
11565 dwp_file->name);
11566 }
11567 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11568 {
11569 error (_("Dwarf Error: bad DWP hash table, too many columns"
11570 " in section table [in module %s]"),
11571 dwp_file->name);
11572 }
11573 memset (ids, 255, sizeof_ids);
11574 memset (ids_seen, 255, sizeof (ids_seen));
11575 for (i = 0; i < nr_columns; ++i)
11576 {
11577 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11578
11579 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11580 {
11581 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11582 " in section table [in module %s]"),
11583 id, dwp_file->name);
11584 }
11585 if (ids_seen[id] != -1)
11586 {
11587 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11588 " id %d in section table [in module %s]"),
11589 id, dwp_file->name);
11590 }
11591 ids_seen[id] = i;
11592 ids[i] = id;
11593 }
11594 /* Must have exactly one info or types section. */
11595 if (((ids_seen[DW_SECT_INFO] != -1)
11596 + (ids_seen[DW_SECT_TYPES] != -1))
11597 != 1)
11598 {
11599 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11600 " DWO info/types section [in module %s]"),
11601 dwp_file->name);
11602 }
11603 /* Must have an abbrev section. */
11604 if (ids_seen[DW_SECT_ABBREV] == -1)
11605 {
11606 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11607 " section [in module %s]"),
11608 dwp_file->name);
11609 }
11610 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11611 htab->section_pool.v2.sizes =
11612 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11613 * nr_units * nr_columns);
11614 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11615 * nr_units * nr_columns))
11616 > index_end)
11617 {
11618 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11619 " [in module %s]"),
11620 dwp_file->name);
11621 }
11622 }
11623 else /* version == 5 */
11624 {
11625 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11626 int *ids = htab->section_pool.v5.section_ids;
11627 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11628 /* Reverse map for error checking. */
11629 int ids_seen[DW_SECT_MAX_V5 + 1];
11630
11631 if (nr_columns < 2)
11632 {
11633 error (_("Dwarf Error: bad DWP hash table, too few columns"
11634 " in section table [in module %s]"),
11635 dwp_file->name);
11636 }
11637 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11638 {
11639 error (_("Dwarf Error: bad DWP hash table, too many columns"
11640 " in section table [in module %s]"),
11641 dwp_file->name);
11642 }
11643 memset (ids, 255, sizeof_ids);
11644 memset (ids_seen, 255, sizeof (ids_seen));
11645 for (int i = 0; i < nr_columns; ++i)
11646 {
11647 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11648
11649 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11650 {
11651 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11652 " in section table [in module %s]"),
11653 id, dwp_file->name);
11654 }
11655 if (ids_seen[id] != -1)
11656 {
11657 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11658 " id %d in section table [in module %s]"),
11659 id, dwp_file->name);
11660 }
11661 ids_seen[id] = i;
11662 ids[i] = id;
11663 }
11664 /* Must have seen an info section. */
11665 if (ids_seen[DW_SECT_INFO_V5] == -1)
11666 {
11667 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11668 " DWO info/types section [in module %s]"),
11669 dwp_file->name);
11670 }
11671 /* Must have an abbrev section. */
11672 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11673 {
11674 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11675 " section [in module %s]"),
11676 dwp_file->name);
11677 }
11678 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11679 htab->section_pool.v5.sizes
11680 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11681 * nr_units * nr_columns);
11682 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11683 * nr_units * nr_columns))
11684 > index_end)
11685 {
11686 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11687 " [in module %s]"),
11688 dwp_file->name);
11689 }
11690 }
11691
11692 return htab;
11693 }
11694
11695 /* Update SECTIONS with the data from SECTP.
11696
11697 This function is like the other "locate" section routines, but in
11698 this context the sections to read comes from the DWP V1 hash table,
11699 not the full ELF section table.
11700
11701 The result is non-zero for success, or zero if an error was found. */
11702
11703 static int
11704 locate_v1_virtual_dwo_sections (asection *sectp,
11705 struct virtual_v1_dwo_sections *sections)
11706 {
11707 const struct dwop_section_names *names = &dwop_section_names;
11708
11709 if (names->abbrev_dwo.matches (sectp->name))
11710 {
11711 /* There can be only one. */
11712 if (sections->abbrev.s.section != NULL)
11713 return 0;
11714 sections->abbrev.s.section = sectp;
11715 sections->abbrev.size = bfd_section_size (sectp);
11716 }
11717 else if (names->info_dwo.matches (sectp->name)
11718 || names->types_dwo.matches (sectp->name))
11719 {
11720 /* There can be only one. */
11721 if (sections->info_or_types.s.section != NULL)
11722 return 0;
11723 sections->info_or_types.s.section = sectp;
11724 sections->info_or_types.size = bfd_section_size (sectp);
11725 }
11726 else if (names->line_dwo.matches (sectp->name))
11727 {
11728 /* There can be only one. */
11729 if (sections->line.s.section != NULL)
11730 return 0;
11731 sections->line.s.section = sectp;
11732 sections->line.size = bfd_section_size (sectp);
11733 }
11734 else if (names->loc_dwo.matches (sectp->name))
11735 {
11736 /* There can be only one. */
11737 if (sections->loc.s.section != NULL)
11738 return 0;
11739 sections->loc.s.section = sectp;
11740 sections->loc.size = bfd_section_size (sectp);
11741 }
11742 else if (names->macinfo_dwo.matches (sectp->name))
11743 {
11744 /* There can be only one. */
11745 if (sections->macinfo.s.section != NULL)
11746 return 0;
11747 sections->macinfo.s.section = sectp;
11748 sections->macinfo.size = bfd_section_size (sectp);
11749 }
11750 else if (names->macro_dwo.matches (sectp->name))
11751 {
11752 /* There can be only one. */
11753 if (sections->macro.s.section != NULL)
11754 return 0;
11755 sections->macro.s.section = sectp;
11756 sections->macro.size = bfd_section_size (sectp);
11757 }
11758 else if (names->str_offsets_dwo.matches (sectp->name))
11759 {
11760 /* There can be only one. */
11761 if (sections->str_offsets.s.section != NULL)
11762 return 0;
11763 sections->str_offsets.s.section = sectp;
11764 sections->str_offsets.size = bfd_section_size (sectp);
11765 }
11766 else
11767 {
11768 /* No other kind of section is valid. */
11769 return 0;
11770 }
11771
11772 return 1;
11773 }
11774
11775 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11776 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11777 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11778 This is for DWP version 1 files. */
11779
11780 static struct dwo_unit *
11781 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11782 struct dwp_file *dwp_file,
11783 uint32_t unit_index,
11784 const char *comp_dir,
11785 ULONGEST signature, int is_debug_types)
11786 {
11787 const struct dwp_hash_table *dwp_htab =
11788 is_debug_types ? dwp_file->tus : dwp_file->cus;
11789 bfd *dbfd = dwp_file->dbfd.get ();
11790 const char *kind = is_debug_types ? "TU" : "CU";
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit *dwo_unit;
11793 struct virtual_v1_dwo_sections sections;
11794 void **dwo_file_slot;
11795 int i;
11796
11797 gdb_assert (dwp_file->version == 1);
11798
11799 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11800 kind, pulongest (unit_index), hex_string (signature),
11801 dwp_file->name);
11802
11803 /* Fetch the sections of this DWO unit.
11804 Put a limit on the number of sections we look for so that bad data
11805 doesn't cause us to loop forever. */
11806
11807 #define MAX_NR_V1_DWO_SECTIONS \
11808 (1 /* .debug_info or .debug_types */ \
11809 + 1 /* .debug_abbrev */ \
11810 + 1 /* .debug_line */ \
11811 + 1 /* .debug_loc */ \
11812 + 1 /* .debug_str_offsets */ \
11813 + 1 /* .debug_macro or .debug_macinfo */ \
11814 + 1 /* trailing zero */)
11815
11816 memset (&sections, 0, sizeof (sections));
11817
11818 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11819 {
11820 asection *sectp;
11821 uint32_t section_nr =
11822 read_4_bytes (dbfd,
11823 dwp_htab->section_pool.v1.indices
11824 + (unit_index + i) * sizeof (uint32_t));
11825
11826 if (section_nr == 0)
11827 break;
11828 if (section_nr >= dwp_file->num_sections)
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, section number too large"
11831 " [in module %s]"),
11832 dwp_file->name);
11833 }
11834
11835 sectp = dwp_file->elf_sections[section_nr];
11836 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11837 {
11838 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11839 " [in module %s]"),
11840 dwp_file->name);
11841 }
11842 }
11843
11844 if (i < 2
11845 || sections.info_or_types.empty ()
11846 || sections.abbrev.empty ())
11847 {
11848 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11849 " [in module %s]"),
11850 dwp_file->name);
11851 }
11852 if (i == MAX_NR_V1_DWO_SECTIONS)
11853 {
11854 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11855 " [in module %s]"),
11856 dwp_file->name);
11857 }
11858
11859 /* It's easier for the rest of the code if we fake a struct dwo_file and
11860 have dwo_unit "live" in that. At least for now.
11861
11862 The DWP file can be made up of a random collection of CUs and TUs.
11863 However, for each CU + set of TUs that came from the same original DWO
11864 file, we can combine them back into a virtual DWO file to save space
11865 (fewer struct dwo_file objects to allocate). Remember that for really
11866 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11867
11868 std::string virtual_dwo_name =
11869 string_printf ("virtual-dwo/%d-%d-%d-%d",
11870 sections.abbrev.get_id (),
11871 sections.line.get_id (),
11872 sections.loc.get_id (),
11873 sections.str_offsets.get_id ());
11874 /* Can we use an existing virtual DWO file? */
11875 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11876 comp_dir);
11877 /* Create one if necessary. */
11878 if (*dwo_file_slot == NULL)
11879 {
11880 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11881 virtual_dwo_name.c_str ());
11882
11883 dwo_file = new struct dwo_file;
11884 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11885 dwo_file->comp_dir = comp_dir;
11886 dwo_file->sections.abbrev = sections.abbrev;
11887 dwo_file->sections.line = sections.line;
11888 dwo_file->sections.loc = sections.loc;
11889 dwo_file->sections.macinfo = sections.macinfo;
11890 dwo_file->sections.macro = sections.macro;
11891 dwo_file->sections.str_offsets = sections.str_offsets;
11892 /* The "str" section is global to the entire DWP file. */
11893 dwo_file->sections.str = dwp_file->sections.str;
11894 /* The info or types section is assigned below to dwo_unit,
11895 there's no need to record it in dwo_file.
11896 Also, we can't simply record type sections in dwo_file because
11897 we record a pointer into the vector in dwo_unit. As we collect more
11898 types we'll grow the vector and eventually have to reallocate space
11899 for it, invalidating all copies of pointers into the previous
11900 contents. */
11901 *dwo_file_slot = dwo_file;
11902 }
11903 else
11904 {
11905 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11906 virtual_dwo_name.c_str ());
11907
11908 dwo_file = (struct dwo_file *) *dwo_file_slot;
11909 }
11910
11911 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11912 dwo_unit->dwo_file = dwo_file;
11913 dwo_unit->signature = signature;
11914 dwo_unit->section =
11915 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11916 *dwo_unit->section = sections.info_or_types;
11917 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11918
11919 return dwo_unit;
11920 }
11921
11922 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11923 simplify them. Given a pointer to the containing section SECTION, and
11924 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11925 virtual section of just that piece. */
11926
11927 static struct dwarf2_section_info
11928 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11929 struct dwarf2_section_info *section,
11930 bfd_size_type offset, bfd_size_type size)
11931 {
11932 struct dwarf2_section_info result;
11933 asection *sectp;
11934
11935 gdb_assert (section != NULL);
11936 gdb_assert (!section->is_virtual);
11937
11938 memset (&result, 0, sizeof (result));
11939 result.s.containing_section = section;
11940 result.is_virtual = true;
11941
11942 if (size == 0)
11943 return result;
11944
11945 sectp = section->get_bfd_section ();
11946
11947 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11948 bounds of the real section. This is a pretty-rare event, so just
11949 flag an error (easier) instead of a warning and trying to cope. */
11950 if (sectp == NULL
11951 || offset + size > bfd_section_size (sectp))
11952 {
11953 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11954 " in section %s [in module %s]"),
11955 sectp ? bfd_section_name (sectp) : "<unknown>",
11956 objfile_name (per_objfile->objfile));
11957 }
11958
11959 result.virtual_offset = offset;
11960 result.size = size;
11961 return result;
11962 }
11963
11964 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11965 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11966 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11967 This is for DWP version 2 files. */
11968
11969 static struct dwo_unit *
11970 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11971 struct dwp_file *dwp_file,
11972 uint32_t unit_index,
11973 const char *comp_dir,
11974 ULONGEST signature, int is_debug_types)
11975 {
11976 const struct dwp_hash_table *dwp_htab =
11977 is_debug_types ? dwp_file->tus : dwp_file->cus;
11978 bfd *dbfd = dwp_file->dbfd.get ();
11979 const char *kind = is_debug_types ? "TU" : "CU";
11980 struct dwo_file *dwo_file;
11981 struct dwo_unit *dwo_unit;
11982 struct virtual_v2_or_v5_dwo_sections sections;
11983 void **dwo_file_slot;
11984 int i;
11985
11986 gdb_assert (dwp_file->version == 2);
11987
11988 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11989 kind, pulongest (unit_index), hex_string (signature),
11990 dwp_file->name);
11991
11992 /* Fetch the section offsets of this DWO unit. */
11993
11994 memset (&sections, 0, sizeof (sections));
11995
11996 for (i = 0; i < dwp_htab->nr_columns; ++i)
11997 {
11998 uint32_t offset = read_4_bytes (dbfd,
11999 dwp_htab->section_pool.v2.offsets
12000 + (((unit_index - 1) * dwp_htab->nr_columns
12001 + i)
12002 * sizeof (uint32_t)));
12003 uint32_t size = read_4_bytes (dbfd,
12004 dwp_htab->section_pool.v2.sizes
12005 + (((unit_index - 1) * dwp_htab->nr_columns
12006 + i)
12007 * sizeof (uint32_t)));
12008
12009 switch (dwp_htab->section_pool.v2.section_ids[i])
12010 {
12011 case DW_SECT_INFO:
12012 case DW_SECT_TYPES:
12013 sections.info_or_types_offset = offset;
12014 sections.info_or_types_size = size;
12015 break;
12016 case DW_SECT_ABBREV:
12017 sections.abbrev_offset = offset;
12018 sections.abbrev_size = size;
12019 break;
12020 case DW_SECT_LINE:
12021 sections.line_offset = offset;
12022 sections.line_size = size;
12023 break;
12024 case DW_SECT_LOC:
12025 sections.loc_offset = offset;
12026 sections.loc_size = size;
12027 break;
12028 case DW_SECT_STR_OFFSETS:
12029 sections.str_offsets_offset = offset;
12030 sections.str_offsets_size = size;
12031 break;
12032 case DW_SECT_MACINFO:
12033 sections.macinfo_offset = offset;
12034 sections.macinfo_size = size;
12035 break;
12036 case DW_SECT_MACRO:
12037 sections.macro_offset = offset;
12038 sections.macro_size = size;
12039 break;
12040 }
12041 }
12042
12043 /* It's easier for the rest of the code if we fake a struct dwo_file and
12044 have dwo_unit "live" in that. At least for now.
12045
12046 The DWP file can be made up of a random collection of CUs and TUs.
12047 However, for each CU + set of TUs that came from the same original DWO
12048 file, we can combine them back into a virtual DWO file to save space
12049 (fewer struct dwo_file objects to allocate). Remember that for really
12050 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12051
12052 std::string virtual_dwo_name =
12053 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12054 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12055 (long) (sections.line_size ? sections.line_offset : 0),
12056 (long) (sections.loc_size ? sections.loc_offset : 0),
12057 (long) (sections.str_offsets_size
12058 ? sections.str_offsets_offset : 0));
12059 /* Can we use an existing virtual DWO file? */
12060 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12061 comp_dir);
12062 /* Create one if necessary. */
12063 if (*dwo_file_slot == NULL)
12064 {
12065 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12066 virtual_dwo_name.c_str ());
12067
12068 dwo_file = new struct dwo_file;
12069 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12070 dwo_file->comp_dir = comp_dir;
12071 dwo_file->sections.abbrev =
12072 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12073 sections.abbrev_offset,
12074 sections.abbrev_size);
12075 dwo_file->sections.line =
12076 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12077 sections.line_offset,
12078 sections.line_size);
12079 dwo_file->sections.loc =
12080 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12081 sections.loc_offset, sections.loc_size);
12082 dwo_file->sections.macinfo =
12083 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12084 sections.macinfo_offset,
12085 sections.macinfo_size);
12086 dwo_file->sections.macro =
12087 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12088 sections.macro_offset,
12089 sections.macro_size);
12090 dwo_file->sections.str_offsets =
12091 create_dwp_v2_or_v5_section (per_objfile,
12092 &dwp_file->sections.str_offsets,
12093 sections.str_offsets_offset,
12094 sections.str_offsets_size);
12095 /* The "str" section is global to the entire DWP file. */
12096 dwo_file->sections.str = dwp_file->sections.str;
12097 /* The info or types section is assigned below to dwo_unit,
12098 there's no need to record it in dwo_file.
12099 Also, we can't simply record type sections in dwo_file because
12100 we record a pointer into the vector in dwo_unit. As we collect more
12101 types we'll grow the vector and eventually have to reallocate space
12102 for it, invalidating all copies of pointers into the previous
12103 contents. */
12104 *dwo_file_slot = dwo_file;
12105 }
12106 else
12107 {
12108 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12109 virtual_dwo_name.c_str ());
12110
12111 dwo_file = (struct dwo_file *) *dwo_file_slot;
12112 }
12113
12114 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12115 dwo_unit->dwo_file = dwo_file;
12116 dwo_unit->signature = signature;
12117 dwo_unit->section =
12118 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12119 *dwo_unit->section = create_dwp_v2_or_v5_section
12120 (per_objfile,
12121 is_debug_types
12122 ? &dwp_file->sections.types
12123 : &dwp_file->sections.info,
12124 sections.info_or_types_offset,
12125 sections.info_or_types_size);
12126 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12127
12128 return dwo_unit;
12129 }
12130
12131 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12132 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12133 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12134 This is for DWP version 5 files. */
12135
12136 static struct dwo_unit *
12137 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12138 struct dwp_file *dwp_file,
12139 uint32_t unit_index,
12140 const char *comp_dir,
12141 ULONGEST signature, int is_debug_types)
12142 {
12143 const struct dwp_hash_table *dwp_htab
12144 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12145 bfd *dbfd = dwp_file->dbfd.get ();
12146 const char *kind = is_debug_types ? "TU" : "CU";
12147 struct dwo_file *dwo_file;
12148 struct dwo_unit *dwo_unit;
12149 struct virtual_v2_or_v5_dwo_sections sections {};
12150 void **dwo_file_slot;
12151
12152 gdb_assert (dwp_file->version == 5);
12153
12154 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12155 kind, pulongest (unit_index), hex_string (signature),
12156 dwp_file->name);
12157
12158 /* Fetch the section offsets of this DWO unit. */
12159
12160 /* memset (&sections, 0, sizeof (sections)); */
12161
12162 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12163 {
12164 uint32_t offset = read_4_bytes (dbfd,
12165 dwp_htab->section_pool.v5.offsets
12166 + (((unit_index - 1)
12167 * dwp_htab->nr_columns
12168 + i)
12169 * sizeof (uint32_t)));
12170 uint32_t size = read_4_bytes (dbfd,
12171 dwp_htab->section_pool.v5.sizes
12172 + (((unit_index - 1) * dwp_htab->nr_columns
12173 + i)
12174 * sizeof (uint32_t)));
12175
12176 switch (dwp_htab->section_pool.v5.section_ids[i])
12177 {
12178 case DW_SECT_ABBREV_V5:
12179 sections.abbrev_offset = offset;
12180 sections.abbrev_size = size;
12181 break;
12182 case DW_SECT_INFO_V5:
12183 sections.info_or_types_offset = offset;
12184 sections.info_or_types_size = size;
12185 break;
12186 case DW_SECT_LINE_V5:
12187 sections.line_offset = offset;
12188 sections.line_size = size;
12189 break;
12190 case DW_SECT_LOCLISTS_V5:
12191 sections.loclists_offset = offset;
12192 sections.loclists_size = size;
12193 break;
12194 case DW_SECT_MACRO_V5:
12195 sections.macro_offset = offset;
12196 sections.macro_size = size;
12197 break;
12198 case DW_SECT_RNGLISTS_V5:
12199 sections.rnglists_offset = offset;
12200 sections.rnglists_size = size;
12201 break;
12202 case DW_SECT_STR_OFFSETS_V5:
12203 sections.str_offsets_offset = offset;
12204 sections.str_offsets_size = size;
12205 break;
12206 case DW_SECT_RESERVED_V5:
12207 default:
12208 break;
12209 }
12210 }
12211
12212 /* It's easier for the rest of the code if we fake a struct dwo_file and
12213 have dwo_unit "live" in that. At least for now.
12214
12215 The DWP file can be made up of a random collection of CUs and TUs.
12216 However, for each CU + set of TUs that came from the same original DWO
12217 file, we can combine them back into a virtual DWO file to save space
12218 (fewer struct dwo_file objects to allocate). Remember that for really
12219 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12220
12221 std::string virtual_dwo_name =
12222 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12223 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12224 (long) (sections.line_size ? sections.line_offset : 0),
12225 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12226 (long) (sections.str_offsets_size
12227 ? sections.str_offsets_offset : 0),
12228 (long) (sections.macro_size ? sections.macro_offset : 0),
12229 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12230 /* Can we use an existing virtual DWO file? */
12231 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12232 virtual_dwo_name.c_str (),
12233 comp_dir);
12234 /* Create one if necessary. */
12235 if (*dwo_file_slot == NULL)
12236 {
12237 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12238 virtual_dwo_name.c_str ());
12239
12240 dwo_file = new struct dwo_file;
12241 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12242 dwo_file->comp_dir = comp_dir;
12243 dwo_file->sections.abbrev =
12244 create_dwp_v2_or_v5_section (per_objfile,
12245 &dwp_file->sections.abbrev,
12246 sections.abbrev_offset,
12247 sections.abbrev_size);
12248 dwo_file->sections.line =
12249 create_dwp_v2_or_v5_section (per_objfile,
12250 &dwp_file->sections.line,
12251 sections.line_offset, sections.line_size);
12252 dwo_file->sections.macro =
12253 create_dwp_v2_or_v5_section (per_objfile,
12254 &dwp_file->sections.macro,
12255 sections.macro_offset,
12256 sections.macro_size);
12257 dwo_file->sections.loclists =
12258 create_dwp_v2_or_v5_section (per_objfile,
12259 &dwp_file->sections.loclists,
12260 sections.loclists_offset,
12261 sections.loclists_size);
12262 dwo_file->sections.rnglists =
12263 create_dwp_v2_or_v5_section (per_objfile,
12264 &dwp_file->sections.rnglists,
12265 sections.rnglists_offset,
12266 sections.rnglists_size);
12267 dwo_file->sections.str_offsets =
12268 create_dwp_v2_or_v5_section (per_objfile,
12269 &dwp_file->sections.str_offsets,
12270 sections.str_offsets_offset,
12271 sections.str_offsets_size);
12272 /* The "str" section is global to the entire DWP file. */
12273 dwo_file->sections.str = dwp_file->sections.str;
12274 /* The info or types section is assigned below to dwo_unit,
12275 there's no need to record it in dwo_file.
12276 Also, we can't simply record type sections in dwo_file because
12277 we record a pointer into the vector in dwo_unit. As we collect more
12278 types we'll grow the vector and eventually have to reallocate space
12279 for it, invalidating all copies of pointers into the previous
12280 contents. */
12281 *dwo_file_slot = dwo_file;
12282 }
12283 else
12284 {
12285 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12286 virtual_dwo_name.c_str ());
12287
12288 dwo_file = (struct dwo_file *) *dwo_file_slot;
12289 }
12290
12291 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12292 dwo_unit->dwo_file = dwo_file;
12293 dwo_unit->signature = signature;
12294 dwo_unit->section
12295 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12296 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12297 &dwp_file->sections.info,
12298 sections.info_or_types_offset,
12299 sections.info_or_types_size);
12300 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12301
12302 return dwo_unit;
12303 }
12304
12305 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12306 Returns NULL if the signature isn't found. */
12307
12308 static struct dwo_unit *
12309 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12310 struct dwp_file *dwp_file, 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 uint32_t mask = dwp_htab->nr_slots - 1;
12317 uint32_t hash = signature & mask;
12318 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12319 unsigned int i;
12320 void **slot;
12321 struct dwo_unit find_dwo_cu;
12322
12323 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12324 find_dwo_cu.signature = signature;
12325 slot = htab_find_slot (is_debug_types
12326 ? dwp_file->loaded_tus.get ()
12327 : dwp_file->loaded_cus.get (),
12328 &find_dwo_cu, INSERT);
12329
12330 if (*slot != NULL)
12331 return (struct dwo_unit *) *slot;
12332
12333 /* Use a for loop so that we don't loop forever on bad debug info. */
12334 for (i = 0; i < dwp_htab->nr_slots; ++i)
12335 {
12336 ULONGEST signature_in_table;
12337
12338 signature_in_table =
12339 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12340 if (signature_in_table == signature)
12341 {
12342 uint32_t unit_index =
12343 read_4_bytes (dbfd,
12344 dwp_htab->unit_table + hash * sizeof (uint32_t));
12345
12346 if (dwp_file->version == 1)
12347 {
12348 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12349 unit_index, comp_dir,
12350 signature, is_debug_types);
12351 }
12352 else if (dwp_file->version == 2)
12353 {
12354 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12355 unit_index, comp_dir,
12356 signature, is_debug_types);
12357 }
12358 else /* version == 5 */
12359 {
12360 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12361 unit_index, comp_dir,
12362 signature, is_debug_types);
12363 }
12364 return (struct dwo_unit *) *slot;
12365 }
12366 if (signature_in_table == 0)
12367 return NULL;
12368 hash = (hash + hash2) & mask;
12369 }
12370
12371 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12372 " [in module %s]"),
12373 dwp_file->name);
12374 }
12375
12376 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12377 Open the file specified by FILE_NAME and hand it off to BFD for
12378 preliminary analysis. Return a newly initialized bfd *, which
12379 includes a canonicalized copy of FILE_NAME.
12380 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12381 SEARCH_CWD is true if the current directory is to be searched.
12382 It will be searched before debug-file-directory.
12383 If successful, the file is added to the bfd include table of the
12384 objfile's bfd (see gdb_bfd_record_inclusion).
12385 If unable to find/open the file, return NULL.
12386 NOTE: This function is derived from symfile_bfd_open. */
12387
12388 static gdb_bfd_ref_ptr
12389 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12390 const char *file_name, int is_dwp, int search_cwd)
12391 {
12392 int desc;
12393 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12394 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12395 to debug_file_directory. */
12396 const char *search_path;
12397 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12398
12399 gdb::unique_xmalloc_ptr<char> search_path_holder;
12400 if (search_cwd)
12401 {
12402 if (*debug_file_directory != '\0')
12403 {
12404 search_path_holder.reset (concat (".", dirname_separator_string,
12405 debug_file_directory,
12406 (char *) NULL));
12407 search_path = search_path_holder.get ();
12408 }
12409 else
12410 search_path = ".";
12411 }
12412 else
12413 search_path = debug_file_directory;
12414
12415 /* Add the path for the executable binary to the list of search paths. */
12416 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12417 search_path_holder.reset (concat (objfile_dir.c_str (),
12418 dirname_separator_string,
12419 search_path, nullptr));
12420 search_path = search_path_holder.get ();
12421
12422 openp_flags flags = OPF_RETURN_REALPATH;
12423 if (is_dwp)
12424 flags |= OPF_SEARCH_IN_PATH;
12425
12426 gdb::unique_xmalloc_ptr<char> absolute_name;
12427 desc = openp (search_path, flags, file_name,
12428 O_RDONLY | O_BINARY, &absolute_name);
12429 if (desc < 0)
12430 return NULL;
12431
12432 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12433 gnutarget, desc));
12434 if (sym_bfd == NULL)
12435 return NULL;
12436 bfd_set_cacheable (sym_bfd.get (), 1);
12437
12438 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12439 return NULL;
12440
12441 /* Success. Record the bfd as having been included by the objfile's bfd.
12442 This is important because things like demangled_names_hash lives in the
12443 objfile's per_bfd space and may have references to things like symbol
12444 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12445 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12446
12447 return sym_bfd;
12448 }
12449
12450 /* Try to open DWO file FILE_NAME.
12451 COMP_DIR is the DW_AT_comp_dir attribute.
12452 The result is the bfd handle of the file.
12453 If there is a problem finding or opening the file, return NULL.
12454 Upon success, the canonicalized path of the file is stored in the bfd,
12455 same as symfile_bfd_open. */
12456
12457 static gdb_bfd_ref_ptr
12458 open_dwo_file (dwarf2_per_objfile *per_objfile,
12459 const char *file_name, const char *comp_dir)
12460 {
12461 if (IS_ABSOLUTE_PATH (file_name))
12462 return try_open_dwop_file (per_objfile, file_name,
12463 0 /*is_dwp*/, 0 /*search_cwd*/);
12464
12465 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12466
12467 if (comp_dir != NULL)
12468 {
12469 gdb::unique_xmalloc_ptr<char> path_to_try
12470 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12471
12472 /* NOTE: If comp_dir is a relative path, this will also try the
12473 search path, which seems useful. */
12474 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12475 0 /*is_dwp*/,
12476 1 /*search_cwd*/));
12477 if (abfd != NULL)
12478 return abfd;
12479 }
12480
12481 /* That didn't work, try debug-file-directory, which, despite its name,
12482 is a list of paths. */
12483
12484 if (*debug_file_directory == '\0')
12485 return NULL;
12486
12487 return try_open_dwop_file (per_objfile, file_name,
12488 0 /*is_dwp*/, 1 /*search_cwd*/);
12489 }
12490
12491 /* This function is mapped across the sections and remembers the offset and
12492 size of each of the DWO debugging sections we are interested in. */
12493
12494 static void
12495 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12496 dwo_sections *dwo_sections)
12497 {
12498 const struct dwop_section_names *names = &dwop_section_names;
12499
12500 if (names->abbrev_dwo.matches (sectp->name))
12501 {
12502 dwo_sections->abbrev.s.section = sectp;
12503 dwo_sections->abbrev.size = bfd_section_size (sectp);
12504 }
12505 else if (names->info_dwo.matches (sectp->name))
12506 {
12507 dwo_sections->info.s.section = sectp;
12508 dwo_sections->info.size = bfd_section_size (sectp);
12509 }
12510 else if (names->line_dwo.matches (sectp->name))
12511 {
12512 dwo_sections->line.s.section = sectp;
12513 dwo_sections->line.size = bfd_section_size (sectp);
12514 }
12515 else if (names->loc_dwo.matches (sectp->name))
12516 {
12517 dwo_sections->loc.s.section = sectp;
12518 dwo_sections->loc.size = bfd_section_size (sectp);
12519 }
12520 else if (names->loclists_dwo.matches (sectp->name))
12521 {
12522 dwo_sections->loclists.s.section = sectp;
12523 dwo_sections->loclists.size = bfd_section_size (sectp);
12524 }
12525 else if (names->macinfo_dwo.matches (sectp->name))
12526 {
12527 dwo_sections->macinfo.s.section = sectp;
12528 dwo_sections->macinfo.size = bfd_section_size (sectp);
12529 }
12530 else if (names->macro_dwo.matches (sectp->name))
12531 {
12532 dwo_sections->macro.s.section = sectp;
12533 dwo_sections->macro.size = bfd_section_size (sectp);
12534 }
12535 else if (names->rnglists_dwo.matches (sectp->name))
12536 {
12537 dwo_sections->rnglists.s.section = sectp;
12538 dwo_sections->rnglists.size = bfd_section_size (sectp);
12539 }
12540 else if (names->str_dwo.matches (sectp->name))
12541 {
12542 dwo_sections->str.s.section = sectp;
12543 dwo_sections->str.size = bfd_section_size (sectp);
12544 }
12545 else if (names->str_offsets_dwo.matches (sectp->name))
12546 {
12547 dwo_sections->str_offsets.s.section = sectp;
12548 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12549 }
12550 else if (names->types_dwo.matches (sectp->name))
12551 {
12552 struct dwarf2_section_info type_section;
12553
12554 memset (&type_section, 0, sizeof (type_section));
12555 type_section.s.section = sectp;
12556 type_section.size = bfd_section_size (sectp);
12557 dwo_sections->types.push_back (type_section);
12558 }
12559 }
12560
12561 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12562 by PER_CU. This is for the non-DWP case.
12563 The result is NULL if DWO_NAME can't be found. */
12564
12565 static struct dwo_file *
12566 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12567 const char *comp_dir)
12568 {
12569 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12570
12571 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12572 if (dbfd == NULL)
12573 {
12574 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12575
12576 return NULL;
12577 }
12578
12579 dwo_file_up dwo_file (new struct dwo_file);
12580 dwo_file->dwo_name = dwo_name;
12581 dwo_file->comp_dir = comp_dir;
12582 dwo_file->dbfd = std::move (dbfd);
12583
12584 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12585 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12586 &dwo_file->sections);
12587
12588 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12589 dwo_file->cus);
12590
12591 if (cu->per_cu->dwarf_version < 5)
12592 {
12593 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12594 dwo_file->sections.types, dwo_file->tus);
12595 }
12596 else
12597 {
12598 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12599 &dwo_file->sections.info, dwo_file->tus,
12600 rcuh_kind::COMPILE);
12601 }
12602
12603 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12604
12605 return dwo_file.release ();
12606 }
12607
12608 /* This function is mapped across the sections and remembers the offset and
12609 size of each of the DWP debugging sections common to version 1 and 2 that
12610 we are interested in. */
12611
12612 static void
12613 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12614 dwp_file *dwp_file)
12615 {
12616 const struct dwop_section_names *names = &dwop_section_names;
12617 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12618
12619 /* Record the ELF section number for later lookup: this is what the
12620 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12621 gdb_assert (elf_section_nr < dwp_file->num_sections);
12622 dwp_file->elf_sections[elf_section_nr] = sectp;
12623
12624 /* Look for specific sections that we need. */
12625 if (names->str_dwo.matches (sectp->name))
12626 {
12627 dwp_file->sections.str.s.section = sectp;
12628 dwp_file->sections.str.size = bfd_section_size (sectp);
12629 }
12630 else if (names->cu_index.matches (sectp->name))
12631 {
12632 dwp_file->sections.cu_index.s.section = sectp;
12633 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12634 }
12635 else if (names->tu_index.matches (sectp->name))
12636 {
12637 dwp_file->sections.tu_index.s.section = sectp;
12638 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12639 }
12640 }
12641
12642 /* This function is mapped across the sections and remembers the offset and
12643 size of each of the DWP version 2 debugging sections that we are interested
12644 in. This is split into a separate function because we don't know if we
12645 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12646
12647 static void
12648 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12649 {
12650 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12651 const struct dwop_section_names *names = &dwop_section_names;
12652 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12653
12654 /* Record the ELF section number for later lookup: this is what the
12655 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12656 gdb_assert (elf_section_nr < dwp_file->num_sections);
12657 dwp_file->elf_sections[elf_section_nr] = sectp;
12658
12659 /* Look for specific sections that we need. */
12660 if (names->abbrev_dwo.matches (sectp->name))
12661 {
12662 dwp_file->sections.abbrev.s.section = sectp;
12663 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12664 }
12665 else if (names->info_dwo.matches (sectp->name))
12666 {
12667 dwp_file->sections.info.s.section = sectp;
12668 dwp_file->sections.info.size = bfd_section_size (sectp);
12669 }
12670 else if (names->line_dwo.matches (sectp->name))
12671 {
12672 dwp_file->sections.line.s.section = sectp;
12673 dwp_file->sections.line.size = bfd_section_size (sectp);
12674 }
12675 else if (names->loc_dwo.matches (sectp->name))
12676 {
12677 dwp_file->sections.loc.s.section = sectp;
12678 dwp_file->sections.loc.size = bfd_section_size (sectp);
12679 }
12680 else if (names->macinfo_dwo.matches (sectp->name))
12681 {
12682 dwp_file->sections.macinfo.s.section = sectp;
12683 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12684 }
12685 else if (names->macro_dwo.matches (sectp->name))
12686 {
12687 dwp_file->sections.macro.s.section = sectp;
12688 dwp_file->sections.macro.size = bfd_section_size (sectp);
12689 }
12690 else if (names->str_offsets_dwo.matches (sectp->name))
12691 {
12692 dwp_file->sections.str_offsets.s.section = sectp;
12693 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12694 }
12695 else if (names->types_dwo.matches (sectp->name))
12696 {
12697 dwp_file->sections.types.s.section = sectp;
12698 dwp_file->sections.types.size = bfd_section_size (sectp);
12699 }
12700 }
12701
12702 /* This function is mapped across the sections and remembers the offset and
12703 size of each of the DWP version 5 debugging sections that we are interested
12704 in. This is split into a separate function because we don't know if we
12705 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12706
12707 static void
12708 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12709 {
12710 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12711 const struct dwop_section_names *names = &dwop_section_names;
12712 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12713
12714 /* Record the ELF section number for later lookup: this is what the
12715 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12716 gdb_assert (elf_section_nr < dwp_file->num_sections);
12717 dwp_file->elf_sections[elf_section_nr] = sectp;
12718
12719 /* Look for specific sections that we need. */
12720 if (names->abbrev_dwo.matches (sectp->name))
12721 {
12722 dwp_file->sections.abbrev.s.section = sectp;
12723 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12724 }
12725 else if (names->info_dwo.matches (sectp->name))
12726 {
12727 dwp_file->sections.info.s.section = sectp;
12728 dwp_file->sections.info.size = bfd_section_size (sectp);
12729 }
12730 else if (names->line_dwo.matches (sectp->name))
12731 {
12732 dwp_file->sections.line.s.section = sectp;
12733 dwp_file->sections.line.size = bfd_section_size (sectp);
12734 }
12735 else if (names->loclists_dwo.matches (sectp->name))
12736 {
12737 dwp_file->sections.loclists.s.section = sectp;
12738 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12739 }
12740 else if (names->macro_dwo.matches (sectp->name))
12741 {
12742 dwp_file->sections.macro.s.section = sectp;
12743 dwp_file->sections.macro.size = bfd_section_size (sectp);
12744 }
12745 else if (names->rnglists_dwo.matches (sectp->name))
12746 {
12747 dwp_file->sections.rnglists.s.section = sectp;
12748 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12749 }
12750 else if (names->str_offsets_dwo.matches (sectp->name))
12751 {
12752 dwp_file->sections.str_offsets.s.section = sectp;
12753 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12754 }
12755 }
12756
12757 /* Hash function for dwp_file loaded CUs/TUs. */
12758
12759 static hashval_t
12760 hash_dwp_loaded_cutus (const void *item)
12761 {
12762 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12763
12764 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12765 return dwo_unit->signature;
12766 }
12767
12768 /* Equality function for dwp_file loaded CUs/TUs. */
12769
12770 static int
12771 eq_dwp_loaded_cutus (const void *a, const void *b)
12772 {
12773 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12774 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12775
12776 return dua->signature == dub->signature;
12777 }
12778
12779 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12780
12781 static htab_up
12782 allocate_dwp_loaded_cutus_table ()
12783 {
12784 return htab_up (htab_create_alloc (3,
12785 hash_dwp_loaded_cutus,
12786 eq_dwp_loaded_cutus,
12787 NULL, xcalloc, xfree));
12788 }
12789
12790 /* Try to open DWP file FILE_NAME.
12791 The result is the bfd handle of the file.
12792 If there is a problem finding or opening the file, return NULL.
12793 Upon success, the canonicalized path of the file is stored in the bfd,
12794 same as symfile_bfd_open. */
12795
12796 static gdb_bfd_ref_ptr
12797 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12798 {
12799 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12800 1 /*is_dwp*/,
12801 1 /*search_cwd*/));
12802 if (abfd != NULL)
12803 return abfd;
12804
12805 /* Work around upstream bug 15652.
12806 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12807 [Whether that's a "bug" is debatable, but it is getting in our way.]
12808 We have no real idea where the dwp file is, because gdb's realpath-ing
12809 of the executable's path may have discarded the needed info.
12810 [IWBN if the dwp file name was recorded in the executable, akin to
12811 .gnu_debuglink, but that doesn't exist yet.]
12812 Strip the directory from FILE_NAME and search again. */
12813 if (*debug_file_directory != '\0')
12814 {
12815 /* Don't implicitly search the current directory here.
12816 If the user wants to search "." to handle this case,
12817 it must be added to debug-file-directory. */
12818 return try_open_dwop_file (per_objfile, lbasename (file_name),
12819 1 /*is_dwp*/,
12820 0 /*search_cwd*/);
12821 }
12822
12823 return NULL;
12824 }
12825
12826 /* Initialize the use of the DWP file for the current objfile.
12827 By convention the name of the DWP file is ${objfile}.dwp.
12828 The result is NULL if it can't be found. */
12829
12830 static std::unique_ptr<struct dwp_file>
12831 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12832 {
12833 struct objfile *objfile = per_objfile->objfile;
12834
12835 /* Try to find first .dwp for the binary file before any symbolic links
12836 resolving. */
12837
12838 /* If the objfile is a debug file, find the name of the real binary
12839 file and get the name of dwp file from there. */
12840 std::string dwp_name;
12841 if (objfile->separate_debug_objfile_backlink != NULL)
12842 {
12843 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12844 const char *backlink_basename = lbasename (backlink->original_name);
12845
12846 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12847 }
12848 else
12849 dwp_name = objfile->original_name;
12850
12851 dwp_name += ".dwp";
12852
12853 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12854 if (dbfd == NULL
12855 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12856 {
12857 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12858 dwp_name = objfile_name (objfile);
12859 dwp_name += ".dwp";
12860 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12861 }
12862
12863 if (dbfd == NULL)
12864 {
12865 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12866
12867 return std::unique_ptr<dwp_file> ();
12868 }
12869
12870 const char *name = bfd_get_filename (dbfd.get ());
12871 std::unique_ptr<struct dwp_file> dwp_file
12872 (new struct dwp_file (name, std::move (dbfd)));
12873
12874 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12875 dwp_file->elf_sections =
12876 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12877 dwp_file->num_sections, asection *);
12878
12879 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12880 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12881 dwp_file.get ());
12882
12883 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12884
12885 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12886
12887 /* The DWP file version is stored in the hash table. Oh well. */
12888 if (dwp_file->cus && dwp_file->tus
12889 && dwp_file->cus->version != dwp_file->tus->version)
12890 {
12891 /* Technically speaking, we should try to limp along, but this is
12892 pretty bizarre. We use pulongest here because that's the established
12893 portability solution (e.g, we cannot use %u for uint32_t). */
12894 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12895 " TU version %s [in DWP file %s]"),
12896 pulongest (dwp_file->cus->version),
12897 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12898 }
12899
12900 if (dwp_file->cus)
12901 dwp_file->version = dwp_file->cus->version;
12902 else if (dwp_file->tus)
12903 dwp_file->version = dwp_file->tus->version;
12904 else
12905 dwp_file->version = 2;
12906
12907 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12908 {
12909 if (dwp_file->version == 2)
12910 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12911 dwp_file.get ());
12912 else
12913 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12914 dwp_file.get ());
12915 }
12916
12917 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12918 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12919
12920 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12921 dwarf_read_debug_printf (" %s CUs, %s TUs",
12922 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12923 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12924
12925 return dwp_file;
12926 }
12927
12928 /* Wrapper around open_and_init_dwp_file, only open it once. */
12929
12930 static struct dwp_file *
12931 get_dwp_file (dwarf2_per_objfile *per_objfile)
12932 {
12933 if (!per_objfile->per_bfd->dwp_checked)
12934 {
12935 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12936 per_objfile->per_bfd->dwp_checked = 1;
12937 }
12938 return per_objfile->per_bfd->dwp_file.get ();
12939 }
12940
12941 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12942 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12943 or in the DWP file for the objfile, referenced by THIS_UNIT.
12944 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12945 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12946
12947 This is called, for example, when wanting to read a variable with a
12948 complex location. Therefore we don't want to do file i/o for every call.
12949 Therefore we don't want to look for a DWO file on every call.
12950 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12951 then we check if we've already seen DWO_NAME, and only THEN do we check
12952 for a DWO file.
12953
12954 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12955 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12956
12957 static struct dwo_unit *
12958 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12959 ULONGEST signature, int is_debug_types)
12960 {
12961 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12962 struct objfile *objfile = per_objfile->objfile;
12963 const char *kind = is_debug_types ? "TU" : "CU";
12964 void **dwo_file_slot;
12965 struct dwo_file *dwo_file;
12966 struct dwp_file *dwp_file;
12967
12968 /* First see if there's a DWP file.
12969 If we have a DWP file but didn't find the DWO inside it, don't
12970 look for the original DWO file. It makes gdb behave differently
12971 depending on whether one is debugging in the build tree. */
12972
12973 dwp_file = get_dwp_file (per_objfile);
12974 if (dwp_file != NULL)
12975 {
12976 const struct dwp_hash_table *dwp_htab =
12977 is_debug_types ? dwp_file->tus : dwp_file->cus;
12978
12979 if (dwp_htab != NULL)
12980 {
12981 struct dwo_unit *dwo_cutu =
12982 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12983 is_debug_types);
12984
12985 if (dwo_cutu != NULL)
12986 {
12987 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12988 kind, hex_string (signature),
12989 host_address_to_string (dwo_cutu));
12990
12991 return dwo_cutu;
12992 }
12993 }
12994 }
12995 else
12996 {
12997 /* No DWP file, look for the DWO file. */
12998
12999 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13000 if (*dwo_file_slot == NULL)
13001 {
13002 /* Read in the file and build a table of the CUs/TUs it contains. */
13003 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13004 }
13005 /* NOTE: This will be NULL if unable to open the file. */
13006 dwo_file = (struct dwo_file *) *dwo_file_slot;
13007
13008 if (dwo_file != NULL)
13009 {
13010 struct dwo_unit *dwo_cutu = NULL;
13011
13012 if (is_debug_types && dwo_file->tus)
13013 {
13014 struct dwo_unit find_dwo_cutu;
13015
13016 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13017 find_dwo_cutu.signature = signature;
13018 dwo_cutu
13019 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13020 &find_dwo_cutu);
13021 }
13022 else if (!is_debug_types && dwo_file->cus)
13023 {
13024 struct dwo_unit find_dwo_cutu;
13025
13026 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13027 find_dwo_cutu.signature = signature;
13028 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13029 &find_dwo_cutu);
13030 }
13031
13032 if (dwo_cutu != NULL)
13033 {
13034 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13035 kind, dwo_name, hex_string (signature),
13036 host_address_to_string (dwo_cutu));
13037
13038 return dwo_cutu;
13039 }
13040 }
13041 }
13042
13043 /* We didn't find it. This could mean a dwo_id mismatch, or
13044 someone deleted the DWO/DWP file, or the search path isn't set up
13045 correctly to find the file. */
13046
13047 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13048 kind, dwo_name, hex_string (signature));
13049
13050 /* This is a warning and not a complaint because it can be caused by
13051 pilot error (e.g., user accidentally deleting the DWO). */
13052 {
13053 /* Print the name of the DWP file if we looked there, helps the user
13054 better diagnose the problem. */
13055 std::string dwp_text;
13056
13057 if (dwp_file != NULL)
13058 dwp_text = string_printf (" [in DWP file %s]",
13059 lbasename (dwp_file->name));
13060
13061 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13062 " [in module %s]"),
13063 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13064 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13065 }
13066 return NULL;
13067 }
13068
13069 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13070 See lookup_dwo_cutu_unit for details. */
13071
13072 static struct dwo_unit *
13073 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13074 ULONGEST signature)
13075 {
13076 gdb_assert (!cu->per_cu->is_debug_types);
13077
13078 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13079 }
13080
13081 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13082 See lookup_dwo_cutu_unit for details. */
13083
13084 static struct dwo_unit *
13085 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13086 {
13087 gdb_assert (cu->per_cu->is_debug_types);
13088
13089 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13090
13091 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13092 }
13093
13094 /* Traversal function for queue_and_load_all_dwo_tus. */
13095
13096 static int
13097 queue_and_load_dwo_tu (void **slot, void *info)
13098 {
13099 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13100 dwarf2_cu *cu = (dwarf2_cu *) info;
13101 ULONGEST signature = dwo_unit->signature;
13102 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13103
13104 if (sig_type != NULL)
13105 {
13106 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13107
13108 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13109 a real dependency of PER_CU on SIG_TYPE. That is detected later
13110 while processing PER_CU. */
13111 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13112 load_full_type_unit (sig_cu, cu->per_objfile);
13113 cu->per_cu->imported_symtabs_push (sig_cu);
13114 }
13115
13116 return 1;
13117 }
13118
13119 /* Queue all TUs contained in the DWO of CU to be read in.
13120 The DWO may have the only definition of the type, though it may not be
13121 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13122 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13123
13124 static void
13125 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13126 {
13127 struct dwo_unit *dwo_unit;
13128 struct dwo_file *dwo_file;
13129
13130 gdb_assert (cu != nullptr);
13131 gdb_assert (!cu->per_cu->is_debug_types);
13132 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13133
13134 dwo_unit = cu->dwo_unit;
13135 gdb_assert (dwo_unit != NULL);
13136
13137 dwo_file = dwo_unit->dwo_file;
13138 if (dwo_file->tus != NULL)
13139 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13140 }
13141
13142 /* Read in various DIEs. */
13143
13144 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13145 Inherit only the children of the DW_AT_abstract_origin DIE not being
13146 already referenced by DW_AT_abstract_origin from the children of the
13147 current DIE. */
13148
13149 static void
13150 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13151 {
13152 struct die_info *child_die;
13153 sect_offset *offsetp;
13154 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13155 struct die_info *origin_die;
13156 /* Iterator of the ORIGIN_DIE children. */
13157 struct die_info *origin_child_die;
13158 struct attribute *attr;
13159 struct dwarf2_cu *origin_cu;
13160 struct pending **origin_previous_list_in_scope;
13161
13162 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13163 if (!attr)
13164 return;
13165
13166 /* Note that following die references may follow to a die in a
13167 different cu. */
13168
13169 origin_cu = cu;
13170 origin_die = follow_die_ref (die, attr, &origin_cu);
13171
13172 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13173 symbols in. */
13174 origin_previous_list_in_scope = origin_cu->list_in_scope;
13175 origin_cu->list_in_scope = cu->list_in_scope;
13176
13177 if (die->tag != origin_die->tag
13178 && !(die->tag == DW_TAG_inlined_subroutine
13179 && origin_die->tag == DW_TAG_subprogram))
13180 complaint (_("DIE %s and its abstract origin %s have different tags"),
13181 sect_offset_str (die->sect_off),
13182 sect_offset_str (origin_die->sect_off));
13183
13184 /* Find if the concrete and abstract trees are structurally the
13185 same. This is a shallow traversal and it is not bullet-proof;
13186 the compiler can trick the debugger into believing that the trees
13187 are isomorphic, whereas they actually are not. However, the
13188 likelyhood of this happening is pretty low, and a full-fledged
13189 check would be an overkill. */
13190 bool are_isomorphic = true;
13191 die_info *concrete_child = die->child;
13192 die_info *abstract_child = origin_die->child;
13193 while (concrete_child != nullptr || abstract_child != nullptr)
13194 {
13195 if (concrete_child == nullptr
13196 || abstract_child == nullptr
13197 || concrete_child->tag != abstract_child->tag)
13198 {
13199 are_isomorphic = false;
13200 break;
13201 }
13202
13203 concrete_child = concrete_child->sibling;
13204 abstract_child = abstract_child->sibling;
13205 }
13206
13207 /* Walk the origin's children in parallel to the concrete children.
13208 This helps match an origin child in case the debug info misses
13209 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13210 origin tree may not have the same tree structure as the concrete
13211 DIE, though. */
13212 die_info *corresponding_abstract_child
13213 = are_isomorphic ? origin_die->child : nullptr;
13214
13215 std::vector<sect_offset> offsets;
13216
13217 for (child_die = die->child;
13218 child_die && child_die->tag;
13219 child_die = child_die->sibling)
13220 {
13221 struct die_info *child_origin_die;
13222 struct dwarf2_cu *child_origin_cu;
13223
13224 /* We are trying to process concrete instance entries:
13225 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13226 it's not relevant to our analysis here. i.e. detecting DIEs that are
13227 present in the abstract instance but not referenced in the concrete
13228 one. */
13229 if (child_die->tag == DW_TAG_call_site
13230 || child_die->tag == DW_TAG_GNU_call_site)
13231 {
13232 if (are_isomorphic)
13233 corresponding_abstract_child
13234 = corresponding_abstract_child->sibling;
13235 continue;
13236 }
13237
13238 /* For each CHILD_DIE, find the corresponding child of
13239 ORIGIN_DIE. If there is more than one layer of
13240 DW_AT_abstract_origin, follow them all; there shouldn't be,
13241 but GCC versions at least through 4.4 generate this (GCC PR
13242 40573). */
13243 child_origin_die = child_die;
13244 child_origin_cu = cu;
13245 while (1)
13246 {
13247 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13248 child_origin_cu);
13249 if (attr == NULL)
13250 break;
13251 child_origin_die = follow_die_ref (child_origin_die, attr,
13252 &child_origin_cu);
13253 }
13254
13255 /* If missing DW_AT_abstract_origin, try the corresponding child
13256 of the origin. Clang emits such lexical scopes. */
13257 if (child_origin_die == child_die
13258 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13259 && are_isomorphic
13260 && child_die->tag == DW_TAG_lexical_block)
13261 child_origin_die = corresponding_abstract_child;
13262
13263 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13264 counterpart may exist. */
13265 if (child_origin_die != child_die)
13266 {
13267 if (child_die->tag != child_origin_die->tag
13268 && !(child_die->tag == DW_TAG_inlined_subroutine
13269 && child_origin_die->tag == DW_TAG_subprogram))
13270 complaint (_("Child DIE %s and its abstract origin %s have "
13271 "different tags"),
13272 sect_offset_str (child_die->sect_off),
13273 sect_offset_str (child_origin_die->sect_off));
13274 if (child_origin_die->parent != origin_die)
13275 complaint (_("Child DIE %s and its abstract origin %s have "
13276 "different parents"),
13277 sect_offset_str (child_die->sect_off),
13278 sect_offset_str (child_origin_die->sect_off));
13279 else
13280 offsets.push_back (child_origin_die->sect_off);
13281 }
13282
13283 if (are_isomorphic)
13284 corresponding_abstract_child = corresponding_abstract_child->sibling;
13285 }
13286 std::sort (offsets.begin (), offsets.end ());
13287 sect_offset *offsets_end = offsets.data () + offsets.size ();
13288 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13289 if (offsetp[-1] == *offsetp)
13290 complaint (_("Multiple children of DIE %s refer "
13291 "to DIE %s as their abstract origin"),
13292 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13293
13294 offsetp = offsets.data ();
13295 origin_child_die = origin_die->child;
13296 while (origin_child_die && origin_child_die->tag)
13297 {
13298 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13299 while (offsetp < offsets_end
13300 && *offsetp < origin_child_die->sect_off)
13301 offsetp++;
13302 if (offsetp >= offsets_end
13303 || *offsetp > origin_child_die->sect_off)
13304 {
13305 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13306 Check whether we're already processing ORIGIN_CHILD_DIE.
13307 This can happen with mutually referenced abstract_origins.
13308 PR 16581. */
13309 if (!origin_child_die->in_process)
13310 process_die (origin_child_die, origin_cu);
13311 }
13312 origin_child_die = origin_child_die->sibling;
13313 }
13314 origin_cu->list_in_scope = origin_previous_list_in_scope;
13315
13316 if (cu != origin_cu)
13317 compute_delayed_physnames (origin_cu);
13318 }
13319
13320 static void
13321 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13322 {
13323 struct objfile *objfile = cu->per_objfile->objfile;
13324 struct gdbarch *gdbarch = objfile->arch ();
13325 struct context_stack *newobj;
13326 CORE_ADDR lowpc;
13327 CORE_ADDR highpc;
13328 struct die_info *child_die;
13329 struct attribute *attr, *call_line, *call_file;
13330 const char *name;
13331 CORE_ADDR baseaddr;
13332 struct block *block;
13333 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13334 std::vector<struct symbol *> template_args;
13335 struct template_symbol *templ_func = NULL;
13336
13337 if (inlined_func)
13338 {
13339 /* If we do not have call site information, we can't show the
13340 caller of this inlined function. That's too confusing, so
13341 only use the scope for local variables. */
13342 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13343 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13344 if (call_line == NULL || call_file == NULL)
13345 {
13346 read_lexical_block_scope (die, cu);
13347 return;
13348 }
13349 }
13350
13351 baseaddr = objfile->text_section_offset ();
13352
13353 name = dwarf2_name (die, cu);
13354
13355 /* Ignore functions with missing or empty names. These are actually
13356 illegal according to the DWARF standard. */
13357 if (name == NULL)
13358 {
13359 complaint (_("missing name for subprogram DIE at %s"),
13360 sect_offset_str (die->sect_off));
13361 return;
13362 }
13363
13364 /* Ignore functions with missing or invalid low and high pc attributes. */
13365 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13366 <= PC_BOUNDS_INVALID)
13367 {
13368 attr = dwarf2_attr (die, DW_AT_external, cu);
13369 if (attr == nullptr || !attr->as_boolean ())
13370 complaint (_("cannot get low and high bounds "
13371 "for subprogram DIE at %s"),
13372 sect_offset_str (die->sect_off));
13373 return;
13374 }
13375
13376 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13377 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13378
13379 /* If we have any template arguments, then we must allocate a
13380 different sort of symbol. */
13381 for (child_die = die->child; child_die; child_die = child_die->sibling)
13382 {
13383 if (child_die->tag == DW_TAG_template_type_param
13384 || child_die->tag == DW_TAG_template_value_param)
13385 {
13386 templ_func = new (&objfile->objfile_obstack) template_symbol;
13387 templ_func->subclass = SYMBOL_TEMPLATE;
13388 break;
13389 }
13390 }
13391
13392 gdb_assert (cu->get_builder () != nullptr);
13393 newobj = cu->get_builder ()->push_context (0, lowpc);
13394 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13395 (struct symbol *) templ_func);
13396
13397 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13398 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13399 cu->language);
13400
13401 /* If there is a location expression for DW_AT_frame_base, record
13402 it. */
13403 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13404 if (attr != nullptr)
13405 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13406
13407 /* If there is a location for the static link, record it. */
13408 newobj->static_link = NULL;
13409 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13410 if (attr != nullptr)
13411 {
13412 newobj->static_link
13413 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13414 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13415 cu->addr_type ());
13416 }
13417
13418 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13419
13420 if (die->child != NULL)
13421 {
13422 child_die = die->child;
13423 while (child_die && child_die->tag)
13424 {
13425 if (child_die->tag == DW_TAG_template_type_param
13426 || child_die->tag == DW_TAG_template_value_param)
13427 {
13428 struct symbol *arg = new_symbol (child_die, NULL, cu);
13429
13430 if (arg != NULL)
13431 template_args.push_back (arg);
13432 }
13433 else
13434 process_die (child_die, cu);
13435 child_die = child_die->sibling;
13436 }
13437 }
13438
13439 inherit_abstract_dies (die, cu);
13440
13441 /* If we have a DW_AT_specification, we might need to import using
13442 directives from the context of the specification DIE. See the
13443 comment in determine_prefix. */
13444 if (cu->language == language_cplus
13445 && dwarf2_attr (die, DW_AT_specification, cu))
13446 {
13447 struct dwarf2_cu *spec_cu = cu;
13448 struct die_info *spec_die = die_specification (die, &spec_cu);
13449
13450 while (spec_die)
13451 {
13452 child_die = spec_die->child;
13453 while (child_die && child_die->tag)
13454 {
13455 if (child_die->tag == DW_TAG_imported_module)
13456 process_die (child_die, spec_cu);
13457 child_die = child_die->sibling;
13458 }
13459
13460 /* In some cases, GCC generates specification DIEs that
13461 themselves contain DW_AT_specification attributes. */
13462 spec_die = die_specification (spec_die, &spec_cu);
13463 }
13464 }
13465
13466 struct context_stack cstk = cu->get_builder ()->pop_context ();
13467 /* Make a block for the local symbols within. */
13468 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13469 cstk.static_link, lowpc, highpc);
13470
13471 /* For C++, set the block's scope. */
13472 if ((cu->language == language_cplus
13473 || cu->language == language_fortran
13474 || cu->language == language_d
13475 || cu->language == language_rust)
13476 && cu->processing_has_namespace_info)
13477 block_set_scope (block, determine_prefix (die, cu),
13478 &objfile->objfile_obstack);
13479
13480 /* If we have address ranges, record them. */
13481 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13482
13483 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13484
13485 /* Attach template arguments to function. */
13486 if (!template_args.empty ())
13487 {
13488 gdb_assert (templ_func != NULL);
13489
13490 templ_func->n_template_arguments = template_args.size ();
13491 templ_func->template_arguments
13492 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13493 templ_func->n_template_arguments);
13494 memcpy (templ_func->template_arguments,
13495 template_args.data (),
13496 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13497
13498 /* Make sure that the symtab is set on the new symbols. Even
13499 though they don't appear in this symtab directly, other parts
13500 of gdb assume that symbols do, and this is reasonably
13501 true. */
13502 for (symbol *sym : template_args)
13503 symbol_set_symtab (sym, symbol_symtab (templ_func));
13504 }
13505
13506 /* In C++, we can have functions nested inside functions (e.g., when
13507 a function declares a class that has methods). This means that
13508 when we finish processing a function scope, we may need to go
13509 back to building a containing block's symbol lists. */
13510 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13511 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13512
13513 /* If we've finished processing a top-level function, subsequent
13514 symbols go in the file symbol list. */
13515 if (cu->get_builder ()->outermost_context_p ())
13516 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13517 }
13518
13519 /* Process all the DIES contained within a lexical block scope. Start
13520 a new scope, process the dies, and then close the scope. */
13521
13522 static void
13523 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13524 {
13525 struct objfile *objfile = cu->per_objfile->objfile;
13526 struct gdbarch *gdbarch = objfile->arch ();
13527 CORE_ADDR lowpc, highpc;
13528 struct die_info *child_die;
13529 CORE_ADDR baseaddr;
13530
13531 baseaddr = objfile->text_section_offset ();
13532
13533 /* Ignore blocks with missing or invalid low and high pc attributes. */
13534 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13535 as multiple lexical blocks? Handling children in a sane way would
13536 be nasty. Might be easier to properly extend generic blocks to
13537 describe ranges. */
13538 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13539 {
13540 case PC_BOUNDS_NOT_PRESENT:
13541 /* DW_TAG_lexical_block has no attributes, process its children as if
13542 there was no wrapping by that DW_TAG_lexical_block.
13543 GCC does no longer produces such DWARF since GCC r224161. */
13544 for (child_die = die->child;
13545 child_die != NULL && child_die->tag;
13546 child_die = child_die->sibling)
13547 {
13548 /* We might already be processing this DIE. This can happen
13549 in an unusual circumstance -- where a subroutine A
13550 appears lexically in another subroutine B, but A actually
13551 inlines B. The recursion is broken here, rather than in
13552 inherit_abstract_dies, because it seems better to simply
13553 drop concrete children here. */
13554 if (!child_die->in_process)
13555 process_die (child_die, cu);
13556 }
13557 return;
13558 case PC_BOUNDS_INVALID:
13559 return;
13560 }
13561 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13562 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13563
13564 cu->get_builder ()->push_context (0, lowpc);
13565 if (die->child != NULL)
13566 {
13567 child_die = die->child;
13568 while (child_die && child_die->tag)
13569 {
13570 process_die (child_die, cu);
13571 child_die = child_die->sibling;
13572 }
13573 }
13574 inherit_abstract_dies (die, cu);
13575 struct context_stack cstk = cu->get_builder ()->pop_context ();
13576
13577 if (*cu->get_builder ()->get_local_symbols () != NULL
13578 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13579 {
13580 struct block *block
13581 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13582 cstk.start_addr, highpc);
13583
13584 /* Note that recording ranges after traversing children, as we
13585 do here, means that recording a parent's ranges entails
13586 walking across all its children's ranges as they appear in
13587 the address map, which is quadratic behavior.
13588
13589 It would be nicer to record the parent's ranges before
13590 traversing its children, simply overriding whatever you find
13591 there. But since we don't even decide whether to create a
13592 block until after we've traversed its children, that's hard
13593 to do. */
13594 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13595 }
13596 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13597 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13598 }
13599
13600 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13601
13602 static void
13603 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13604 {
13605 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13606 struct objfile *objfile = per_objfile->objfile;
13607 struct gdbarch *gdbarch = objfile->arch ();
13608 CORE_ADDR pc, baseaddr;
13609 struct attribute *attr;
13610 struct call_site *call_site, call_site_local;
13611 void **slot;
13612 int nparams;
13613 struct die_info *child_die;
13614
13615 baseaddr = objfile->text_section_offset ();
13616
13617 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13618 if (attr == NULL)
13619 {
13620 /* This was a pre-DWARF-5 GNU extension alias
13621 for DW_AT_call_return_pc. */
13622 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13623 }
13624 if (!attr)
13625 {
13626 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13627 "DIE %s [in module %s]"),
13628 sect_offset_str (die->sect_off), objfile_name (objfile));
13629 return;
13630 }
13631 pc = attr->as_address () + baseaddr;
13632 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13633
13634 if (cu->call_site_htab == NULL)
13635 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13636 NULL, &objfile->objfile_obstack,
13637 hashtab_obstack_allocate, NULL);
13638 call_site_local.pc = pc;
13639 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13640 if (*slot != NULL)
13641 {
13642 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13643 "DIE %s [in module %s]"),
13644 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13645 objfile_name (objfile));
13646 return;
13647 }
13648
13649 /* Count parameters at the caller. */
13650
13651 nparams = 0;
13652 for (child_die = die->child; child_die && child_die->tag;
13653 child_die = child_die->sibling)
13654 {
13655 if (child_die->tag != DW_TAG_call_site_parameter
13656 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13657 {
13658 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13659 "DW_TAG_call_site child DIE %s [in module %s]"),
13660 child_die->tag, sect_offset_str (child_die->sect_off),
13661 objfile_name (objfile));
13662 continue;
13663 }
13664
13665 nparams++;
13666 }
13667
13668 call_site
13669 = ((struct call_site *)
13670 obstack_alloc (&objfile->objfile_obstack,
13671 sizeof (*call_site)
13672 + (sizeof (*call_site->parameter) * (nparams - 1))));
13673 *slot = call_site;
13674 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13675 call_site->pc = pc;
13676
13677 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13678 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13679 {
13680 struct die_info *func_die;
13681
13682 /* Skip also over DW_TAG_inlined_subroutine. */
13683 for (func_die = die->parent;
13684 func_die && func_die->tag != DW_TAG_subprogram
13685 && func_die->tag != DW_TAG_subroutine_type;
13686 func_die = func_die->parent);
13687
13688 /* DW_AT_call_all_calls is a superset
13689 of DW_AT_call_all_tail_calls. */
13690 if (func_die
13691 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13692 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13693 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13694 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13695 {
13696 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13697 not complete. But keep CALL_SITE for look ups via call_site_htab,
13698 both the initial caller containing the real return address PC and
13699 the final callee containing the current PC of a chain of tail
13700 calls do not need to have the tail call list complete. But any
13701 function candidate for a virtual tail call frame searched via
13702 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13703 determined unambiguously. */
13704 }
13705 else
13706 {
13707 struct type *func_type = NULL;
13708
13709 if (func_die)
13710 func_type = get_die_type (func_die, cu);
13711 if (func_type != NULL)
13712 {
13713 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13714
13715 /* Enlist this call site to the function. */
13716 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13717 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13718 }
13719 else
13720 complaint (_("Cannot find function owning DW_TAG_call_site "
13721 "DIE %s [in module %s]"),
13722 sect_offset_str (die->sect_off), objfile_name (objfile));
13723 }
13724 }
13725
13726 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13727 if (attr == NULL)
13728 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13729 if (attr == NULL)
13730 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13731 if (attr == NULL)
13732 {
13733 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13734 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13735 }
13736 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13737 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13738 /* Keep NULL DWARF_BLOCK. */;
13739 else if (attr->form_is_block ())
13740 {
13741 struct dwarf2_locexpr_baton *dlbaton;
13742 struct dwarf_block *block = attr->as_block ();
13743
13744 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13745 dlbaton->data = block->data;
13746 dlbaton->size = block->size;
13747 dlbaton->per_objfile = per_objfile;
13748 dlbaton->per_cu = cu->per_cu;
13749
13750 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13751 }
13752 else if (attr->form_is_ref ())
13753 {
13754 struct dwarf2_cu *target_cu = cu;
13755 struct die_info *target_die;
13756
13757 target_die = follow_die_ref (die, attr, &target_cu);
13758 gdb_assert (target_cu->per_objfile->objfile == objfile);
13759 if (die_is_declaration (target_die, target_cu))
13760 {
13761 const char *target_physname;
13762
13763 /* Prefer the mangled name; otherwise compute the demangled one. */
13764 target_physname = dw2_linkage_name (target_die, target_cu);
13765 if (target_physname == NULL)
13766 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13767 if (target_physname == NULL)
13768 complaint (_("DW_AT_call_target target DIE has invalid "
13769 "physname, for referencing DIE %s [in module %s]"),
13770 sect_offset_str (die->sect_off), objfile_name (objfile));
13771 else
13772 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13773 }
13774 else
13775 {
13776 CORE_ADDR lowpc;
13777
13778 /* DW_AT_entry_pc should be preferred. */
13779 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13780 <= PC_BOUNDS_INVALID)
13781 complaint (_("DW_AT_call_target target DIE has invalid "
13782 "low pc, for referencing DIE %s [in module %s]"),
13783 sect_offset_str (die->sect_off), objfile_name (objfile));
13784 else
13785 {
13786 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13787 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13788 }
13789 }
13790 }
13791 else
13792 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13793 "block nor reference, for DIE %s [in module %s]"),
13794 sect_offset_str (die->sect_off), objfile_name (objfile));
13795
13796 call_site->per_cu = cu->per_cu;
13797 call_site->per_objfile = per_objfile;
13798
13799 for (child_die = die->child;
13800 child_die && child_die->tag;
13801 child_die = child_die->sibling)
13802 {
13803 struct call_site_parameter *parameter;
13804 struct attribute *loc, *origin;
13805
13806 if (child_die->tag != DW_TAG_call_site_parameter
13807 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13808 {
13809 /* Already printed the complaint above. */
13810 continue;
13811 }
13812
13813 gdb_assert (call_site->parameter_count < nparams);
13814 parameter = &call_site->parameter[call_site->parameter_count];
13815
13816 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13817 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13818 register is contained in DW_AT_call_value. */
13819
13820 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13821 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13822 if (origin == NULL)
13823 {
13824 /* This was a pre-DWARF-5 GNU extension alias
13825 for DW_AT_call_parameter. */
13826 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13827 }
13828 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13829 {
13830 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13831
13832 sect_offset sect_off = origin->get_ref_die_offset ();
13833 if (!cu->header.offset_in_cu_p (sect_off))
13834 {
13835 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13836 binding can be done only inside one CU. Such referenced DIE
13837 therefore cannot be even moved to DW_TAG_partial_unit. */
13838 complaint (_("DW_AT_call_parameter offset is not in CU for "
13839 "DW_TAG_call_site child DIE %s [in module %s]"),
13840 sect_offset_str (child_die->sect_off),
13841 objfile_name (objfile));
13842 continue;
13843 }
13844 parameter->u.param_cu_off
13845 = (cu_offset) (sect_off - cu->header.sect_off);
13846 }
13847 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13848 {
13849 complaint (_("No DW_FORM_block* DW_AT_location for "
13850 "DW_TAG_call_site child DIE %s [in module %s]"),
13851 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13852 continue;
13853 }
13854 else
13855 {
13856 struct dwarf_block *block = loc->as_block ();
13857
13858 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13859 (block->data, &block->data[block->size]);
13860 if (parameter->u.dwarf_reg != -1)
13861 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13862 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13863 &block->data[block->size],
13864 &parameter->u.fb_offset))
13865 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13866 else
13867 {
13868 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13869 "for DW_FORM_block* DW_AT_location is supported for "
13870 "DW_TAG_call_site child DIE %s "
13871 "[in module %s]"),
13872 sect_offset_str (child_die->sect_off),
13873 objfile_name (objfile));
13874 continue;
13875 }
13876 }
13877
13878 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13879 if (attr == NULL)
13880 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13881 if (attr == NULL || !attr->form_is_block ())
13882 {
13883 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13884 "DW_TAG_call_site child DIE %s [in module %s]"),
13885 sect_offset_str (child_die->sect_off),
13886 objfile_name (objfile));
13887 continue;
13888 }
13889
13890 struct dwarf_block *block = attr->as_block ();
13891 parameter->value = block->data;
13892 parameter->value_size = block->size;
13893
13894 /* Parameters are not pre-cleared by memset above. */
13895 parameter->data_value = NULL;
13896 parameter->data_value_size = 0;
13897 call_site->parameter_count++;
13898
13899 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13900 if (attr == NULL)
13901 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13902 if (attr != nullptr)
13903 {
13904 if (!attr->form_is_block ())
13905 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13906 "DW_TAG_call_site child DIE %s [in module %s]"),
13907 sect_offset_str (child_die->sect_off),
13908 objfile_name (objfile));
13909 else
13910 {
13911 block = attr->as_block ();
13912 parameter->data_value = block->data;
13913 parameter->data_value_size = block->size;
13914 }
13915 }
13916 }
13917 }
13918
13919 /* Helper function for read_variable. If DIE represents a virtual
13920 table, then return the type of the concrete object that is
13921 associated with the virtual table. Otherwise, return NULL. */
13922
13923 static struct type *
13924 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13925 {
13926 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13927 if (attr == NULL)
13928 return NULL;
13929
13930 /* Find the type DIE. */
13931 struct die_info *type_die = NULL;
13932 struct dwarf2_cu *type_cu = cu;
13933
13934 if (attr->form_is_ref ())
13935 type_die = follow_die_ref (die, attr, &type_cu);
13936 if (type_die == NULL)
13937 return NULL;
13938
13939 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13940 return NULL;
13941 return die_containing_type (type_die, type_cu);
13942 }
13943
13944 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13945
13946 static void
13947 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13948 {
13949 struct rust_vtable_symbol *storage = NULL;
13950
13951 if (cu->language == language_rust)
13952 {
13953 struct type *containing_type = rust_containing_type (die, cu);
13954
13955 if (containing_type != NULL)
13956 {
13957 struct objfile *objfile = cu->per_objfile->objfile;
13958
13959 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13960 storage->concrete_type = containing_type;
13961 storage->subclass = SYMBOL_RUST_VTABLE;
13962 }
13963 }
13964
13965 struct symbol *res = new_symbol (die, NULL, cu, storage);
13966 struct attribute *abstract_origin
13967 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13968 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13969 if (res == NULL && loc && abstract_origin)
13970 {
13971 /* We have a variable without a name, but with a location and an abstract
13972 origin. This may be a concrete instance of an abstract variable
13973 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13974 later. */
13975 struct dwarf2_cu *origin_cu = cu;
13976 struct die_info *origin_die
13977 = follow_die_ref (die, abstract_origin, &origin_cu);
13978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13979 per_objfile->per_bfd->abstract_to_concrete
13980 [origin_die->sect_off].push_back (die->sect_off);
13981 }
13982 }
13983
13984 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13985 reading .debug_rnglists.
13986 Callback's type should be:
13987 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13988 Return true if the attributes are present and valid, otherwise,
13989 return false. */
13990
13991 template <typename Callback>
13992 static bool
13993 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13994 dwarf_tag tag, Callback &&callback)
13995 {
13996 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13997 struct objfile *objfile = per_objfile->objfile;
13998 bfd *obfd = objfile->obfd;
13999 /* Base address selection entry. */
14000 gdb::optional<CORE_ADDR> base;
14001 const gdb_byte *buffer;
14002 CORE_ADDR baseaddr;
14003 bool overflow = false;
14004 ULONGEST addr_index;
14005 struct dwarf2_section_info *rnglists_section;
14006
14007 base = cu->base_address;
14008 rnglists_section = cu_debug_rnglists_section (cu, tag);
14009 rnglists_section->read (objfile);
14010
14011 if (offset >= rnglists_section->size)
14012 {
14013 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14014 offset);
14015 return false;
14016 }
14017 buffer = rnglists_section->buffer + offset;
14018
14019 baseaddr = objfile->text_section_offset ();
14020
14021 while (1)
14022 {
14023 /* Initialize it due to a false compiler warning. */
14024 CORE_ADDR range_beginning = 0, range_end = 0;
14025 const gdb_byte *buf_end = (rnglists_section->buffer
14026 + rnglists_section->size);
14027 unsigned int bytes_read;
14028
14029 if (buffer == buf_end)
14030 {
14031 overflow = true;
14032 break;
14033 }
14034 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14035 switch (rlet)
14036 {
14037 case DW_RLE_end_of_list:
14038 break;
14039 case DW_RLE_base_address:
14040 if (buffer + cu->header.addr_size > buf_end)
14041 {
14042 overflow = true;
14043 break;
14044 }
14045 base = cu->header.read_address (obfd, buffer, &bytes_read);
14046 buffer += bytes_read;
14047 break;
14048 case DW_RLE_base_addressx:
14049 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14050 buffer += bytes_read;
14051 base = read_addr_index (cu, addr_index);
14052 break;
14053 case DW_RLE_start_length:
14054 if (buffer + cu->header.addr_size > buf_end)
14055 {
14056 overflow = true;
14057 break;
14058 }
14059 range_beginning = cu->header.read_address (obfd, buffer,
14060 &bytes_read);
14061 buffer += bytes_read;
14062 range_end = (range_beginning
14063 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14064 buffer += bytes_read;
14065 if (buffer > buf_end)
14066 {
14067 overflow = true;
14068 break;
14069 }
14070 break;
14071 case DW_RLE_startx_length:
14072 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14073 buffer += bytes_read;
14074 range_beginning = read_addr_index (cu, addr_index);
14075 if (buffer > buf_end)
14076 {
14077 overflow = true;
14078 break;
14079 }
14080 range_end = (range_beginning
14081 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14082 buffer += bytes_read;
14083 break;
14084 case DW_RLE_offset_pair:
14085 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14086 buffer += bytes_read;
14087 if (buffer > buf_end)
14088 {
14089 overflow = true;
14090 break;
14091 }
14092 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14093 buffer += bytes_read;
14094 if (buffer > buf_end)
14095 {
14096 overflow = true;
14097 break;
14098 }
14099 break;
14100 case DW_RLE_start_end:
14101 if (buffer + 2 * cu->header.addr_size > buf_end)
14102 {
14103 overflow = true;
14104 break;
14105 }
14106 range_beginning = cu->header.read_address (obfd, buffer,
14107 &bytes_read);
14108 buffer += bytes_read;
14109 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14110 buffer += bytes_read;
14111 break;
14112 case DW_RLE_startx_endx:
14113 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14114 buffer += bytes_read;
14115 range_beginning = read_addr_index (cu, addr_index);
14116 if (buffer > buf_end)
14117 {
14118 overflow = true;
14119 break;
14120 }
14121 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14122 buffer += bytes_read;
14123 range_end = read_addr_index (cu, addr_index);
14124 break;
14125 default:
14126 complaint (_("Invalid .debug_rnglists data (no base address)"));
14127 return false;
14128 }
14129 if (rlet == DW_RLE_end_of_list || overflow)
14130 break;
14131 if (rlet == DW_RLE_base_address)
14132 continue;
14133
14134 if (range_beginning > range_end)
14135 {
14136 /* Inverted range entries are invalid. */
14137 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14138 return false;
14139 }
14140
14141 /* Empty range entries have no effect. */
14142 if (range_beginning == range_end)
14143 continue;
14144
14145 /* Only DW_RLE_offset_pair needs the base address added. */
14146 if (rlet == DW_RLE_offset_pair)
14147 {
14148 if (!base.has_value ())
14149 {
14150 /* We have no valid base address for the DW_RLE_offset_pair. */
14151 complaint (_("Invalid .debug_rnglists data (no base address for "
14152 "DW_RLE_offset_pair)"));
14153 return false;
14154 }
14155
14156 range_beginning += *base;
14157 range_end += *base;
14158 }
14159
14160 /* A not-uncommon case of bad debug info.
14161 Don't pollute the addrmap with bad data. */
14162 if (range_beginning + baseaddr == 0
14163 && !per_objfile->per_bfd->has_section_at_zero)
14164 {
14165 complaint (_(".debug_rnglists entry has start address of zero"
14166 " [in module %s]"), objfile_name (objfile));
14167 continue;
14168 }
14169
14170 callback (range_beginning, range_end);
14171 }
14172
14173 if (overflow)
14174 {
14175 complaint (_("Offset %d is not terminated "
14176 "for DW_AT_ranges attribute"),
14177 offset);
14178 return false;
14179 }
14180
14181 return true;
14182 }
14183
14184 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14185 Callback's type should be:
14186 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14187 Return 1 if the attributes are present and valid, otherwise, return 0. */
14188
14189 template <typename Callback>
14190 static int
14191 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14192 Callback &&callback)
14193 {
14194 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14195 struct objfile *objfile = per_objfile->objfile;
14196 struct comp_unit_head *cu_header = &cu->header;
14197 bfd *obfd = objfile->obfd;
14198 unsigned int addr_size = cu_header->addr_size;
14199 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14200 /* Base address selection entry. */
14201 gdb::optional<CORE_ADDR> base;
14202 unsigned int dummy;
14203 const gdb_byte *buffer;
14204 CORE_ADDR baseaddr;
14205
14206 if (cu_header->version >= 5)
14207 return dwarf2_rnglists_process (offset, cu, tag, callback);
14208
14209 base = cu->base_address;
14210
14211 per_objfile->per_bfd->ranges.read (objfile);
14212 if (offset >= per_objfile->per_bfd->ranges.size)
14213 {
14214 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14215 offset);
14216 return 0;
14217 }
14218 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14219
14220 baseaddr = objfile->text_section_offset ();
14221
14222 while (1)
14223 {
14224 CORE_ADDR range_beginning, range_end;
14225
14226 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14227 buffer += addr_size;
14228 range_end = cu->header.read_address (obfd, buffer, &dummy);
14229 buffer += addr_size;
14230 offset += 2 * addr_size;
14231
14232 /* An end of list marker is a pair of zero addresses. */
14233 if (range_beginning == 0 && range_end == 0)
14234 /* Found the end of list entry. */
14235 break;
14236
14237 /* Each base address selection entry is a pair of 2 values.
14238 The first is the largest possible address, the second is
14239 the base address. Check for a base address here. */
14240 if ((range_beginning & mask) == mask)
14241 {
14242 /* If we found the largest possible address, then we already
14243 have the base address in range_end. */
14244 base = range_end;
14245 continue;
14246 }
14247
14248 if (!base.has_value ())
14249 {
14250 /* We have no valid base address for the ranges
14251 data. */
14252 complaint (_("Invalid .debug_ranges data (no base address)"));
14253 return 0;
14254 }
14255
14256 if (range_beginning > range_end)
14257 {
14258 /* Inverted range entries are invalid. */
14259 complaint (_("Invalid .debug_ranges data (inverted range)"));
14260 return 0;
14261 }
14262
14263 /* Empty range entries have no effect. */
14264 if (range_beginning == range_end)
14265 continue;
14266
14267 range_beginning += *base;
14268 range_end += *base;
14269
14270 /* A not-uncommon case of bad debug info.
14271 Don't pollute the addrmap with bad data. */
14272 if (range_beginning + baseaddr == 0
14273 && !per_objfile->per_bfd->has_section_at_zero)
14274 {
14275 complaint (_(".debug_ranges entry has start address of zero"
14276 " [in module %s]"), objfile_name (objfile));
14277 continue;
14278 }
14279
14280 callback (range_beginning, range_end);
14281 }
14282
14283 return 1;
14284 }
14285
14286 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14287 Return 1 if the attributes are present and valid, otherwise, return 0.
14288 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14289
14290 static int
14291 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14292 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14293 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14294 {
14295 struct objfile *objfile = cu->per_objfile->objfile;
14296 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14297 struct gdbarch *gdbarch = objfile->arch ();
14298 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14299 int low_set = 0;
14300 CORE_ADDR low = 0;
14301 CORE_ADDR high = 0;
14302 int retval;
14303
14304 retval = dwarf2_ranges_process (offset, cu, tag,
14305 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14306 {
14307 if (ranges_pst != NULL)
14308 {
14309 CORE_ADDR lowpc;
14310 CORE_ADDR highpc;
14311
14312 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14313 range_beginning + baseaddr)
14314 - baseaddr);
14315 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14316 range_end + baseaddr)
14317 - baseaddr);
14318 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14319 lowpc, highpc - 1, ranges_pst);
14320 }
14321
14322 /* FIXME: This is recording everything as a low-high
14323 segment of consecutive addresses. We should have a
14324 data structure for discontiguous block ranges
14325 instead. */
14326 if (! low_set)
14327 {
14328 low = range_beginning;
14329 high = range_end;
14330 low_set = 1;
14331 }
14332 else
14333 {
14334 if (range_beginning < low)
14335 low = range_beginning;
14336 if (range_end > high)
14337 high = range_end;
14338 }
14339 });
14340 if (!retval)
14341 return 0;
14342
14343 if (! low_set)
14344 /* If the first entry is an end-of-list marker, the range
14345 describes an empty scope, i.e. no instructions. */
14346 return 0;
14347
14348 if (low_return)
14349 *low_return = low;
14350 if (high_return)
14351 *high_return = high;
14352 return 1;
14353 }
14354
14355 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14356 definition for the return value. *LOWPC and *HIGHPC are set iff
14357 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14358
14359 static enum pc_bounds_kind
14360 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14361 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14362 dwarf2_psymtab *pst)
14363 {
14364 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14365 struct attribute *attr;
14366 struct attribute *attr_high;
14367 CORE_ADDR low = 0;
14368 CORE_ADDR high = 0;
14369 enum pc_bounds_kind ret;
14370
14371 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14372 if (attr_high)
14373 {
14374 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14375 if (attr != nullptr)
14376 {
14377 low = attr->as_address ();
14378 high = attr_high->as_address ();
14379 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14380 high += low;
14381 }
14382 else
14383 /* Found high w/o low attribute. */
14384 return PC_BOUNDS_INVALID;
14385
14386 /* Found consecutive range of addresses. */
14387 ret = PC_BOUNDS_HIGH_LOW;
14388 }
14389 else
14390 {
14391 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14392 if (attr != nullptr && attr->form_is_unsigned ())
14393 {
14394 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14395 on DWARF version). */
14396 ULONGEST ranges_offset = attr->as_unsigned ();
14397
14398 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14399 this value. */
14400 if (die->tag != DW_TAG_compile_unit)
14401 ranges_offset += cu->gnu_ranges_base;
14402
14403 /* Value of the DW_AT_ranges attribute is the offset in the
14404 .debug_ranges section. */
14405 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14406 die->tag))
14407 return PC_BOUNDS_INVALID;
14408 /* Found discontinuous range of addresses. */
14409 ret = PC_BOUNDS_RANGES;
14410 }
14411 else
14412 return PC_BOUNDS_NOT_PRESENT;
14413 }
14414
14415 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14416 if (high <= low)
14417 return PC_BOUNDS_INVALID;
14418
14419 /* When using the GNU linker, .gnu.linkonce. sections are used to
14420 eliminate duplicate copies of functions and vtables and such.
14421 The linker will arbitrarily choose one and discard the others.
14422 The AT_*_pc values for such functions refer to local labels in
14423 these sections. If the section from that file was discarded, the
14424 labels are not in the output, so the relocs get a value of 0.
14425 If this is a discarded function, mark the pc bounds as invalid,
14426 so that GDB will ignore it. */
14427 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14428 return PC_BOUNDS_INVALID;
14429
14430 *lowpc = low;
14431 if (highpc)
14432 *highpc = high;
14433 return ret;
14434 }
14435
14436 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14437 its low and high PC addresses. Do nothing if these addresses could not
14438 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14439 and HIGHPC to the high address if greater than HIGHPC. */
14440
14441 static void
14442 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14443 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14444 struct dwarf2_cu *cu)
14445 {
14446 CORE_ADDR low, high;
14447 struct die_info *child = die->child;
14448
14449 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14450 {
14451 *lowpc = std::min (*lowpc, low);
14452 *highpc = std::max (*highpc, high);
14453 }
14454
14455 /* If the language does not allow nested subprograms (either inside
14456 subprograms or lexical blocks), we're done. */
14457 if (cu->language != language_ada)
14458 return;
14459
14460 /* Check all the children of the given DIE. If it contains nested
14461 subprograms, then check their pc bounds. Likewise, we need to
14462 check lexical blocks as well, as they may also contain subprogram
14463 definitions. */
14464 while (child && child->tag)
14465 {
14466 if (child->tag == DW_TAG_subprogram
14467 || child->tag == DW_TAG_lexical_block)
14468 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14469 child = child->sibling;
14470 }
14471 }
14472
14473 /* Get the low and high pc's represented by the scope DIE, and store
14474 them in *LOWPC and *HIGHPC. If the correct values can't be
14475 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14476
14477 static void
14478 get_scope_pc_bounds (struct die_info *die,
14479 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14480 struct dwarf2_cu *cu)
14481 {
14482 CORE_ADDR best_low = (CORE_ADDR) -1;
14483 CORE_ADDR best_high = (CORE_ADDR) 0;
14484 CORE_ADDR current_low, current_high;
14485
14486 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14487 >= PC_BOUNDS_RANGES)
14488 {
14489 best_low = current_low;
14490 best_high = current_high;
14491 }
14492 else
14493 {
14494 struct die_info *child = die->child;
14495
14496 while (child && child->tag)
14497 {
14498 switch (child->tag) {
14499 case DW_TAG_subprogram:
14500 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14501 break;
14502 case DW_TAG_namespace:
14503 case DW_TAG_module:
14504 /* FIXME: carlton/2004-01-16: Should we do this for
14505 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14506 that current GCC's always emit the DIEs corresponding
14507 to definitions of methods of classes as children of a
14508 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14509 the DIEs giving the declarations, which could be
14510 anywhere). But I don't see any reason why the
14511 standards says that they have to be there. */
14512 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14513
14514 if (current_low != ((CORE_ADDR) -1))
14515 {
14516 best_low = std::min (best_low, current_low);
14517 best_high = std::max (best_high, current_high);
14518 }
14519 break;
14520 default:
14521 /* Ignore. */
14522 break;
14523 }
14524
14525 child = child->sibling;
14526 }
14527 }
14528
14529 *lowpc = best_low;
14530 *highpc = best_high;
14531 }
14532
14533 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14534 in DIE. */
14535
14536 static void
14537 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14538 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14539 {
14540 struct objfile *objfile = cu->per_objfile->objfile;
14541 struct gdbarch *gdbarch = objfile->arch ();
14542 struct attribute *attr;
14543 struct attribute *attr_high;
14544
14545 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14546 if (attr_high)
14547 {
14548 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14549 if (attr != nullptr)
14550 {
14551 CORE_ADDR low = attr->as_address ();
14552 CORE_ADDR high = attr_high->as_address ();
14553
14554 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14555 high += low;
14556
14557 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14558 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14559 cu->get_builder ()->record_block_range (block, low, high - 1);
14560 }
14561 }
14562
14563 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14564 if (attr != nullptr && attr->form_is_unsigned ())
14565 {
14566 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14567 on DWARF version). */
14568 ULONGEST ranges_offset = attr->as_unsigned ();
14569
14570 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14571 this value. */
14572 if (die->tag != DW_TAG_compile_unit)
14573 ranges_offset += cu->gnu_ranges_base;
14574
14575 std::vector<blockrange> blockvec;
14576 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14577 [&] (CORE_ADDR start, CORE_ADDR end)
14578 {
14579 start += baseaddr;
14580 end += baseaddr;
14581 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14582 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14583 cu->get_builder ()->record_block_range (block, start, end - 1);
14584 blockvec.emplace_back (start, end);
14585 });
14586
14587 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14588 }
14589 }
14590
14591 /* Check whether the producer field indicates either of GCC < 4.6, or the
14592 Intel C/C++ compiler, and cache the result in CU. */
14593
14594 static void
14595 check_producer (struct dwarf2_cu *cu)
14596 {
14597 int major, minor;
14598
14599 if (cu->producer == NULL)
14600 {
14601 /* For unknown compilers expect their behavior is DWARF version
14602 compliant.
14603
14604 GCC started to support .debug_types sections by -gdwarf-4 since
14605 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14606 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14607 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14608 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14609 }
14610 else if (producer_is_gcc (cu->producer, &major, &minor))
14611 {
14612 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14613 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14614 }
14615 else if (producer_is_icc (cu->producer, &major, &minor))
14616 {
14617 cu->producer_is_icc = true;
14618 cu->producer_is_icc_lt_14 = major < 14;
14619 }
14620 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14621 cu->producer_is_codewarrior = true;
14622 else
14623 {
14624 /* For other non-GCC compilers, expect their behavior is DWARF version
14625 compliant. */
14626 }
14627
14628 cu->checked_producer = true;
14629 }
14630
14631 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14632 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14633 during 4.6.0 experimental. */
14634
14635 static bool
14636 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14637 {
14638 if (!cu->checked_producer)
14639 check_producer (cu);
14640
14641 return cu->producer_is_gxx_lt_4_6;
14642 }
14643
14644
14645 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14646 with incorrect is_stmt attributes. */
14647
14648 static bool
14649 producer_is_codewarrior (struct dwarf2_cu *cu)
14650 {
14651 if (!cu->checked_producer)
14652 check_producer (cu);
14653
14654 return cu->producer_is_codewarrior;
14655 }
14656
14657 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14658 If that attribute is not available, return the appropriate
14659 default. */
14660
14661 static enum dwarf_access_attribute
14662 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14663 {
14664 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14665 if (attr != nullptr)
14666 {
14667 LONGEST value = attr->constant_value (-1);
14668 if (value == DW_ACCESS_public
14669 || value == DW_ACCESS_protected
14670 || value == DW_ACCESS_private)
14671 return (dwarf_access_attribute) value;
14672 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14673 plongest (value));
14674 }
14675
14676 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14677 {
14678 /* The default DWARF 2 accessibility for members is public, the default
14679 accessibility for inheritance is private. */
14680
14681 if (die->tag != DW_TAG_inheritance)
14682 return DW_ACCESS_public;
14683 else
14684 return DW_ACCESS_private;
14685 }
14686 else
14687 {
14688 /* DWARF 3+ defines the default accessibility a different way. The same
14689 rules apply now for DW_TAG_inheritance as for the members and it only
14690 depends on the container kind. */
14691
14692 if (die->parent->tag == DW_TAG_class_type)
14693 return DW_ACCESS_private;
14694 else
14695 return DW_ACCESS_public;
14696 }
14697 }
14698
14699 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14700 offset. If the attribute was not found return 0, otherwise return
14701 1. If it was found but could not properly be handled, set *OFFSET
14702 to 0. */
14703
14704 static int
14705 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14706 LONGEST *offset)
14707 {
14708 struct attribute *attr;
14709
14710 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14711 if (attr != NULL)
14712 {
14713 *offset = 0;
14714
14715 /* Note that we do not check for a section offset first here.
14716 This is because DW_AT_data_member_location is new in DWARF 4,
14717 so if we see it, we can assume that a constant form is really
14718 a constant and not a section offset. */
14719 if (attr->form_is_constant ())
14720 *offset = attr->constant_value (0);
14721 else if (attr->form_is_section_offset ())
14722 dwarf2_complex_location_expr_complaint ();
14723 else if (attr->form_is_block ())
14724 *offset = decode_locdesc (attr->as_block (), cu);
14725 else
14726 dwarf2_complex_location_expr_complaint ();
14727
14728 return 1;
14729 }
14730
14731 return 0;
14732 }
14733
14734 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14735
14736 static void
14737 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14738 struct field *field)
14739 {
14740 struct attribute *attr;
14741
14742 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14743 if (attr != NULL)
14744 {
14745 if (attr->form_is_constant ())
14746 {
14747 LONGEST offset = attr->constant_value (0);
14748 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14749 }
14750 else if (attr->form_is_section_offset ())
14751 dwarf2_complex_location_expr_complaint ();
14752 else if (attr->form_is_block ())
14753 {
14754 bool handled;
14755 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14756 if (handled)
14757 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14758 else
14759 {
14760 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14761 struct objfile *objfile = per_objfile->objfile;
14762 struct dwarf2_locexpr_baton *dlbaton
14763 = XOBNEW (&objfile->objfile_obstack,
14764 struct dwarf2_locexpr_baton);
14765 dlbaton->data = attr->as_block ()->data;
14766 dlbaton->size = attr->as_block ()->size;
14767 /* When using this baton, we want to compute the address
14768 of the field, not the value. This is why
14769 is_reference is set to false here. */
14770 dlbaton->is_reference = false;
14771 dlbaton->per_objfile = per_objfile;
14772 dlbaton->per_cu = cu->per_cu;
14773
14774 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14775 }
14776 }
14777 else
14778 dwarf2_complex_location_expr_complaint ();
14779 }
14780 }
14781
14782 /* Add an aggregate field to the field list. */
14783
14784 static void
14785 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14786 struct dwarf2_cu *cu)
14787 {
14788 struct objfile *objfile = cu->per_objfile->objfile;
14789 struct gdbarch *gdbarch = objfile->arch ();
14790 struct nextfield *new_field;
14791 struct attribute *attr;
14792 struct field *fp;
14793 const char *fieldname = "";
14794
14795 if (die->tag == DW_TAG_inheritance)
14796 {
14797 fip->baseclasses.emplace_back ();
14798 new_field = &fip->baseclasses.back ();
14799 }
14800 else
14801 {
14802 fip->fields.emplace_back ();
14803 new_field = &fip->fields.back ();
14804 }
14805
14806 new_field->offset = die->sect_off;
14807
14808 new_field->accessibility = dwarf2_access_attribute (die, cu);
14809 if (new_field->accessibility != DW_ACCESS_public)
14810 fip->non_public_fields = true;
14811
14812 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14813 if (attr != nullptr)
14814 new_field->virtuality = attr->as_virtuality ();
14815 else
14816 new_field->virtuality = DW_VIRTUALITY_none;
14817
14818 fp = &new_field->field;
14819
14820 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14821 {
14822 /* Data member other than a C++ static data member. */
14823
14824 /* Get type of field. */
14825 fp->set_type (die_type (die, cu));
14826
14827 SET_FIELD_BITPOS (*fp, 0);
14828
14829 /* Get bit size of field (zero if none). */
14830 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14831 if (attr != nullptr)
14832 {
14833 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14834 }
14835 else
14836 {
14837 FIELD_BITSIZE (*fp) = 0;
14838 }
14839
14840 /* Get bit offset of field. */
14841 handle_data_member_location (die, cu, fp);
14842 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14843 if (attr != nullptr && attr->form_is_constant ())
14844 {
14845 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14846 {
14847 /* For big endian bits, the DW_AT_bit_offset gives the
14848 additional bit offset from the MSB of the containing
14849 anonymous object to the MSB of the field. We don't
14850 have to do anything special since we don't need to
14851 know the size of the anonymous object. */
14852 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14853 + attr->constant_value (0)));
14854 }
14855 else
14856 {
14857 /* For little endian bits, compute the bit offset to the
14858 MSB of the anonymous object, subtract off the number of
14859 bits from the MSB of the field to the MSB of the
14860 object, and then subtract off the number of bits of
14861 the field itself. The result is the bit offset of
14862 the LSB of the field. */
14863 int anonymous_size;
14864 int bit_offset = attr->constant_value (0);
14865
14866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14867 if (attr != nullptr && attr->form_is_constant ())
14868 {
14869 /* The size of the anonymous object containing
14870 the bit field is explicit, so use the
14871 indicated size (in bytes). */
14872 anonymous_size = attr->constant_value (0);
14873 }
14874 else
14875 {
14876 /* The size of the anonymous object containing
14877 the bit field must be inferred from the type
14878 attribute of the data member containing the
14879 bit field. */
14880 anonymous_size = TYPE_LENGTH (fp->type ());
14881 }
14882 SET_FIELD_BITPOS (*fp,
14883 (FIELD_BITPOS (*fp)
14884 + anonymous_size * bits_per_byte
14885 - bit_offset - FIELD_BITSIZE (*fp)));
14886 }
14887 }
14888 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14889 if (attr != NULL)
14890 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14891 + attr->constant_value (0)));
14892
14893 /* Get name of field. */
14894 fieldname = dwarf2_name (die, cu);
14895 if (fieldname == NULL)
14896 fieldname = "";
14897
14898 /* The name is already allocated along with this objfile, so we don't
14899 need to duplicate it for the type. */
14900 fp->name = fieldname;
14901
14902 /* Change accessibility for artificial fields (e.g. virtual table
14903 pointer or virtual base class pointer) to private. */
14904 if (dwarf2_attr (die, DW_AT_artificial, cu))
14905 {
14906 FIELD_ARTIFICIAL (*fp) = 1;
14907 new_field->accessibility = DW_ACCESS_private;
14908 fip->non_public_fields = true;
14909 }
14910 }
14911 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14912 {
14913 /* C++ static member. */
14914
14915 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14916 is a declaration, but all versions of G++ as of this writing
14917 (so through at least 3.2.1) incorrectly generate
14918 DW_TAG_variable tags. */
14919
14920 const char *physname;
14921
14922 /* Get name of field. */
14923 fieldname = dwarf2_name (die, cu);
14924 if (fieldname == NULL)
14925 return;
14926
14927 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14928 if (attr
14929 /* Only create a symbol if this is an external value.
14930 new_symbol checks this and puts the value in the global symbol
14931 table, which we want. If it is not external, new_symbol
14932 will try to put the value in cu->list_in_scope which is wrong. */
14933 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14934 {
14935 /* A static const member, not much different than an enum as far as
14936 we're concerned, except that we can support more types. */
14937 new_symbol (die, NULL, cu);
14938 }
14939
14940 /* Get physical name. */
14941 physname = dwarf2_physname (fieldname, die, cu);
14942
14943 /* The name is already allocated along with this objfile, so we don't
14944 need to duplicate it for the type. */
14945 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14946 fp->set_type (die_type (die, cu));
14947 FIELD_NAME (*fp) = fieldname;
14948 }
14949 else if (die->tag == DW_TAG_inheritance)
14950 {
14951 /* C++ base class field. */
14952 handle_data_member_location (die, cu, fp);
14953 FIELD_BITSIZE (*fp) = 0;
14954 fp->set_type (die_type (die, cu));
14955 FIELD_NAME (*fp) = fp->type ()->name ();
14956 }
14957 else
14958 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14959 }
14960
14961 /* Can the type given by DIE define another type? */
14962
14963 static bool
14964 type_can_define_types (const struct die_info *die)
14965 {
14966 switch (die->tag)
14967 {
14968 case DW_TAG_typedef:
14969 case DW_TAG_class_type:
14970 case DW_TAG_structure_type:
14971 case DW_TAG_union_type:
14972 case DW_TAG_enumeration_type:
14973 return true;
14974
14975 default:
14976 return false;
14977 }
14978 }
14979
14980 /* Add a type definition defined in the scope of the FIP's class. */
14981
14982 static void
14983 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14984 struct dwarf2_cu *cu)
14985 {
14986 struct decl_field fp;
14987 memset (&fp, 0, sizeof (fp));
14988
14989 gdb_assert (type_can_define_types (die));
14990
14991 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14992 fp.name = dwarf2_name (die, cu);
14993 fp.type = read_type_die (die, cu);
14994
14995 /* Save accessibility. */
14996 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14997 switch (accessibility)
14998 {
14999 case DW_ACCESS_public:
15000 /* The assumed value if neither private nor protected. */
15001 break;
15002 case DW_ACCESS_private:
15003 fp.is_private = 1;
15004 break;
15005 case DW_ACCESS_protected:
15006 fp.is_protected = 1;
15007 break;
15008 }
15009
15010 if (die->tag == DW_TAG_typedef)
15011 fip->typedef_field_list.push_back (fp);
15012 else
15013 fip->nested_types_list.push_back (fp);
15014 }
15015
15016 /* A convenience typedef that's used when finding the discriminant
15017 field for a variant part. */
15018 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15019 offset_map_type;
15020
15021 /* Compute the discriminant range for a given variant. OBSTACK is
15022 where the results will be stored. VARIANT is the variant to
15023 process. IS_UNSIGNED indicates whether the discriminant is signed
15024 or unsigned. */
15025
15026 static const gdb::array_view<discriminant_range>
15027 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15028 bool is_unsigned)
15029 {
15030 std::vector<discriminant_range> ranges;
15031
15032 if (variant.default_branch)
15033 return {};
15034
15035 if (variant.discr_list_data == nullptr)
15036 {
15037 discriminant_range r
15038 = {variant.discriminant_value, variant.discriminant_value};
15039 ranges.push_back (r);
15040 }
15041 else
15042 {
15043 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15044 variant.discr_list_data->size);
15045 while (!data.empty ())
15046 {
15047 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15048 {
15049 complaint (_("invalid discriminant marker: %d"), data[0]);
15050 break;
15051 }
15052 bool is_range = data[0] == DW_DSC_range;
15053 data = data.slice (1);
15054
15055 ULONGEST low, high;
15056 unsigned int bytes_read;
15057
15058 if (data.empty ())
15059 {
15060 complaint (_("DW_AT_discr_list missing low value"));
15061 break;
15062 }
15063 if (is_unsigned)
15064 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15065 else
15066 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15067 &bytes_read);
15068 data = data.slice (bytes_read);
15069
15070 if (is_range)
15071 {
15072 if (data.empty ())
15073 {
15074 complaint (_("DW_AT_discr_list missing high value"));
15075 break;
15076 }
15077 if (is_unsigned)
15078 high = read_unsigned_leb128 (nullptr, data.data (),
15079 &bytes_read);
15080 else
15081 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15082 &bytes_read);
15083 data = data.slice (bytes_read);
15084 }
15085 else
15086 high = low;
15087
15088 ranges.push_back ({ low, high });
15089 }
15090 }
15091
15092 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15093 ranges.size ());
15094 std::copy (ranges.begin (), ranges.end (), result);
15095 return gdb::array_view<discriminant_range> (result, ranges.size ());
15096 }
15097
15098 static const gdb::array_view<variant_part> create_variant_parts
15099 (struct obstack *obstack,
15100 const offset_map_type &offset_map,
15101 struct field_info *fi,
15102 const std::vector<variant_part_builder> &variant_parts);
15103
15104 /* Fill in a "struct variant" for a given variant field. RESULT is
15105 the variant to fill in. OBSTACK is where any needed allocations
15106 will be done. OFFSET_MAP holds the mapping from section offsets to
15107 fields for the type. FI describes the fields of the type we're
15108 processing. FIELD is the variant field we're converting. */
15109
15110 static void
15111 create_one_variant (variant &result, struct obstack *obstack,
15112 const offset_map_type &offset_map,
15113 struct field_info *fi, const variant_field &field)
15114 {
15115 result.discriminants = convert_variant_range (obstack, field, false);
15116 result.first_field = field.first_field + fi->baseclasses.size ();
15117 result.last_field = field.last_field + fi->baseclasses.size ();
15118 result.parts = create_variant_parts (obstack, offset_map, fi,
15119 field.variant_parts);
15120 }
15121
15122 /* Fill in a "struct variant_part" for a given variant part. RESULT
15123 is the variant part to fill in. OBSTACK is where any needed
15124 allocations will be done. OFFSET_MAP holds the mapping from
15125 section offsets to fields for the type. FI describes the fields of
15126 the type we're processing. BUILDER is the variant part to be
15127 converted. */
15128
15129 static void
15130 create_one_variant_part (variant_part &result,
15131 struct obstack *obstack,
15132 const offset_map_type &offset_map,
15133 struct field_info *fi,
15134 const variant_part_builder &builder)
15135 {
15136 auto iter = offset_map.find (builder.discriminant_offset);
15137 if (iter == offset_map.end ())
15138 {
15139 result.discriminant_index = -1;
15140 /* Doesn't matter. */
15141 result.is_unsigned = false;
15142 }
15143 else
15144 {
15145 result.discriminant_index = iter->second;
15146 result.is_unsigned
15147 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15148 }
15149
15150 size_t n = builder.variants.size ();
15151 variant *output = new (obstack) variant[n];
15152 for (size_t i = 0; i < n; ++i)
15153 create_one_variant (output[i], obstack, offset_map, fi,
15154 builder.variants[i]);
15155
15156 result.variants = gdb::array_view<variant> (output, n);
15157 }
15158
15159 /* Create a vector of variant parts that can be attached to a type.
15160 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15161 holds the mapping from section offsets to fields for the type. FI
15162 describes the fields of the type we're processing. VARIANT_PARTS
15163 is the vector to convert. */
15164
15165 static const gdb::array_view<variant_part>
15166 create_variant_parts (struct obstack *obstack,
15167 const offset_map_type &offset_map,
15168 struct field_info *fi,
15169 const std::vector<variant_part_builder> &variant_parts)
15170 {
15171 if (variant_parts.empty ())
15172 return {};
15173
15174 size_t n = variant_parts.size ();
15175 variant_part *result = new (obstack) variant_part[n];
15176 for (size_t i = 0; i < n; ++i)
15177 create_one_variant_part (result[i], obstack, offset_map, fi,
15178 variant_parts[i]);
15179
15180 return gdb::array_view<variant_part> (result, n);
15181 }
15182
15183 /* Compute the variant part vector for FIP, attaching it to TYPE when
15184 done. */
15185
15186 static void
15187 add_variant_property (struct field_info *fip, struct type *type,
15188 struct dwarf2_cu *cu)
15189 {
15190 /* Map section offsets of fields to their field index. Note the
15191 field index here does not take the number of baseclasses into
15192 account. */
15193 offset_map_type offset_map;
15194 for (int i = 0; i < fip->fields.size (); ++i)
15195 offset_map[fip->fields[i].offset] = i;
15196
15197 struct objfile *objfile = cu->per_objfile->objfile;
15198 gdb::array_view<variant_part> parts
15199 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15200 fip->variant_parts);
15201
15202 struct dynamic_prop prop;
15203 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15204 obstack_copy (&objfile->objfile_obstack, &parts,
15205 sizeof (parts)));
15206
15207 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15208 }
15209
15210 /* Create the vector of fields, and attach it to the type. */
15211
15212 static void
15213 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15214 struct dwarf2_cu *cu)
15215 {
15216 int nfields = fip->nfields ();
15217
15218 /* Record the field count, allocate space for the array of fields,
15219 and create blank accessibility bitfields if necessary. */
15220 type->set_num_fields (nfields);
15221 type->set_fields
15222 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15223
15224 if (fip->non_public_fields && cu->language != language_ada)
15225 {
15226 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15227
15228 TYPE_FIELD_PRIVATE_BITS (type) =
15229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15230 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15231
15232 TYPE_FIELD_PROTECTED_BITS (type) =
15233 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15234 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15235
15236 TYPE_FIELD_IGNORE_BITS (type) =
15237 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15238 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15239 }
15240
15241 /* If the type has baseclasses, allocate and clear a bit vector for
15242 TYPE_FIELD_VIRTUAL_BITS. */
15243 if (!fip->baseclasses.empty () && cu->language != language_ada)
15244 {
15245 int num_bytes = B_BYTES (fip->baseclasses.size ());
15246 unsigned char *pointer;
15247
15248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15249 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15250 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15251 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15252 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15253 }
15254
15255 if (!fip->variant_parts.empty ())
15256 add_variant_property (fip, type, cu);
15257
15258 /* Copy the saved-up fields into the field vector. */
15259 for (int i = 0; i < nfields; ++i)
15260 {
15261 struct nextfield &field
15262 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15263 : fip->fields[i - fip->baseclasses.size ()]);
15264
15265 type->field (i) = field.field;
15266 switch (field.accessibility)
15267 {
15268 case DW_ACCESS_private:
15269 if (cu->language != language_ada)
15270 SET_TYPE_FIELD_PRIVATE (type, i);
15271 break;
15272
15273 case DW_ACCESS_protected:
15274 if (cu->language != language_ada)
15275 SET_TYPE_FIELD_PROTECTED (type, i);
15276 break;
15277
15278 case DW_ACCESS_public:
15279 break;
15280
15281 default:
15282 /* Unknown accessibility. Complain and treat it as public. */
15283 {
15284 complaint (_("unsupported accessibility %d"),
15285 field.accessibility);
15286 }
15287 break;
15288 }
15289 if (i < fip->baseclasses.size ())
15290 {
15291 switch (field.virtuality)
15292 {
15293 case DW_VIRTUALITY_virtual:
15294 case DW_VIRTUALITY_pure_virtual:
15295 if (cu->language == language_ada)
15296 error (_("unexpected virtuality in component of Ada type"));
15297 SET_TYPE_FIELD_VIRTUAL (type, i);
15298 break;
15299 }
15300 }
15301 }
15302 }
15303
15304 /* Return true if this member function is a constructor, false
15305 otherwise. */
15306
15307 static int
15308 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15309 {
15310 const char *fieldname;
15311 const char *type_name;
15312 int len;
15313
15314 if (die->parent == NULL)
15315 return 0;
15316
15317 if (die->parent->tag != DW_TAG_structure_type
15318 && die->parent->tag != DW_TAG_union_type
15319 && die->parent->tag != DW_TAG_class_type)
15320 return 0;
15321
15322 fieldname = dwarf2_name (die, cu);
15323 type_name = dwarf2_name (die->parent, cu);
15324 if (fieldname == NULL || type_name == NULL)
15325 return 0;
15326
15327 len = strlen (fieldname);
15328 return (strncmp (fieldname, type_name, len) == 0
15329 && (type_name[len] == '\0' || type_name[len] == '<'));
15330 }
15331
15332 /* Add a member function to the proper fieldlist. */
15333
15334 static void
15335 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15336 struct type *type, struct dwarf2_cu *cu)
15337 {
15338 struct objfile *objfile = cu->per_objfile->objfile;
15339 struct attribute *attr;
15340 int i;
15341 struct fnfieldlist *flp = nullptr;
15342 struct fn_field *fnp;
15343 const char *fieldname;
15344 struct type *this_type;
15345
15346 if (cu->language == language_ada)
15347 error (_("unexpected member function in Ada type"));
15348
15349 /* Get name of member function. */
15350 fieldname = dwarf2_name (die, cu);
15351 if (fieldname == NULL)
15352 return;
15353
15354 /* Look up member function name in fieldlist. */
15355 for (i = 0; i < fip->fnfieldlists.size (); i++)
15356 {
15357 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15358 {
15359 flp = &fip->fnfieldlists[i];
15360 break;
15361 }
15362 }
15363
15364 /* Create a new fnfieldlist if necessary. */
15365 if (flp == nullptr)
15366 {
15367 fip->fnfieldlists.emplace_back ();
15368 flp = &fip->fnfieldlists.back ();
15369 flp->name = fieldname;
15370 i = fip->fnfieldlists.size () - 1;
15371 }
15372
15373 /* Create a new member function field and add it to the vector of
15374 fnfieldlists. */
15375 flp->fnfields.emplace_back ();
15376 fnp = &flp->fnfields.back ();
15377
15378 /* Delay processing of the physname until later. */
15379 if (cu->language == language_cplus)
15380 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15381 die, cu);
15382 else
15383 {
15384 const char *physname = dwarf2_physname (fieldname, die, cu);
15385 fnp->physname = physname ? physname : "";
15386 }
15387
15388 fnp->type = alloc_type (objfile);
15389 this_type = read_type_die (die, cu);
15390 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15391 {
15392 int nparams = this_type->num_fields ();
15393
15394 /* TYPE is the domain of this method, and THIS_TYPE is the type
15395 of the method itself (TYPE_CODE_METHOD). */
15396 smash_to_method_type (fnp->type, type,
15397 TYPE_TARGET_TYPE (this_type),
15398 this_type->fields (),
15399 this_type->num_fields (),
15400 this_type->has_varargs ());
15401
15402 /* Handle static member functions.
15403 Dwarf2 has no clean way to discern C++ static and non-static
15404 member functions. G++ helps GDB by marking the first
15405 parameter for non-static member functions (which is the this
15406 pointer) as artificial. We obtain this information from
15407 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15408 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15409 fnp->voffset = VOFFSET_STATIC;
15410 }
15411 else
15412 complaint (_("member function type missing for '%s'"),
15413 dwarf2_full_name (fieldname, die, cu));
15414
15415 /* Get fcontext from DW_AT_containing_type if present. */
15416 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15417 fnp->fcontext = die_containing_type (die, cu);
15418
15419 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15420 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15421
15422 /* Get accessibility. */
15423 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15424 switch (accessibility)
15425 {
15426 case DW_ACCESS_private:
15427 fnp->is_private = 1;
15428 break;
15429 case DW_ACCESS_protected:
15430 fnp->is_protected = 1;
15431 break;
15432 }
15433
15434 /* Check for artificial methods. */
15435 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15436 if (attr && attr->as_boolean ())
15437 fnp->is_artificial = 1;
15438
15439 /* Check for defaulted methods. */
15440 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15441 if (attr != nullptr)
15442 fnp->defaulted = attr->defaulted ();
15443
15444 /* Check for deleted methods. */
15445 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15446 if (attr != nullptr && attr->as_boolean ())
15447 fnp->is_deleted = 1;
15448
15449 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15450
15451 /* Get index in virtual function table if it is a virtual member
15452 function. For older versions of GCC, this is an offset in the
15453 appropriate virtual table, as specified by DW_AT_containing_type.
15454 For everyone else, it is an expression to be evaluated relative
15455 to the object address. */
15456
15457 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15458 if (attr != nullptr)
15459 {
15460 if (attr->form_is_block () && attr->as_block ()->size > 0)
15461 {
15462 struct dwarf_block *block = attr->as_block ();
15463
15464 if (block->data[0] == DW_OP_constu)
15465 {
15466 /* Old-style GCC. */
15467 fnp->voffset = decode_locdesc (block, cu) + 2;
15468 }
15469 else if (block->data[0] == DW_OP_deref
15470 || (block->size > 1
15471 && block->data[0] == DW_OP_deref_size
15472 && block->data[1] == cu->header.addr_size))
15473 {
15474 fnp->voffset = decode_locdesc (block, cu);
15475 if ((fnp->voffset % cu->header.addr_size) != 0)
15476 dwarf2_complex_location_expr_complaint ();
15477 else
15478 fnp->voffset /= cu->header.addr_size;
15479 fnp->voffset += 2;
15480 }
15481 else
15482 dwarf2_complex_location_expr_complaint ();
15483
15484 if (!fnp->fcontext)
15485 {
15486 /* If there is no `this' field and no DW_AT_containing_type,
15487 we cannot actually find a base class context for the
15488 vtable! */
15489 if (this_type->num_fields () == 0
15490 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15491 {
15492 complaint (_("cannot determine context for virtual member "
15493 "function \"%s\" (offset %s)"),
15494 fieldname, sect_offset_str (die->sect_off));
15495 }
15496 else
15497 {
15498 fnp->fcontext
15499 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15500 }
15501 }
15502 }
15503 else if (attr->form_is_section_offset ())
15504 {
15505 dwarf2_complex_location_expr_complaint ();
15506 }
15507 else
15508 {
15509 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15510 fieldname);
15511 }
15512 }
15513 else
15514 {
15515 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15516 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15517 {
15518 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15519 complaint (_("Member function \"%s\" (offset %s) is virtual "
15520 "but the vtable offset is not specified"),
15521 fieldname, sect_offset_str (die->sect_off));
15522 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15523 TYPE_CPLUS_DYNAMIC (type) = 1;
15524 }
15525 }
15526 }
15527
15528 /* Create the vector of member function fields, and attach it to the type. */
15529
15530 static void
15531 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15532 struct dwarf2_cu *cu)
15533 {
15534 if (cu->language == language_ada)
15535 error (_("unexpected member functions in Ada type"));
15536
15537 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15538 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15539 TYPE_ALLOC (type,
15540 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15541
15542 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15543 {
15544 struct fnfieldlist &nf = fip->fnfieldlists[i];
15545 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15546
15547 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15548 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15549 fn_flp->fn_fields = (struct fn_field *)
15550 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15551
15552 for (int k = 0; k < nf.fnfields.size (); ++k)
15553 fn_flp->fn_fields[k] = nf.fnfields[k];
15554 }
15555
15556 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15557 }
15558
15559 /* Returns non-zero if NAME is the name of a vtable member in CU's
15560 language, zero otherwise. */
15561 static int
15562 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15563 {
15564 static const char vptr[] = "_vptr";
15565
15566 /* Look for the C++ form of the vtable. */
15567 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15568 return 1;
15569
15570 return 0;
15571 }
15572
15573 /* GCC outputs unnamed structures that are really pointers to member
15574 functions, with the ABI-specified layout. If TYPE describes
15575 such a structure, smash it into a member function type.
15576
15577 GCC shouldn't do this; it should just output pointer to member DIEs.
15578 This is GCC PR debug/28767. */
15579
15580 static void
15581 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15582 {
15583 struct type *pfn_type, *self_type, *new_type;
15584
15585 /* Check for a structure with no name and two children. */
15586 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15587 return;
15588
15589 /* Check for __pfn and __delta members. */
15590 if (TYPE_FIELD_NAME (type, 0) == NULL
15591 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15592 || TYPE_FIELD_NAME (type, 1) == NULL
15593 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15594 return;
15595
15596 /* Find the type of the method. */
15597 pfn_type = type->field (0).type ();
15598 if (pfn_type == NULL
15599 || pfn_type->code () != TYPE_CODE_PTR
15600 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15601 return;
15602
15603 /* Look for the "this" argument. */
15604 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15605 if (pfn_type->num_fields () == 0
15606 /* || pfn_type->field (0).type () == NULL */
15607 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15608 return;
15609
15610 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15611 new_type = alloc_type (objfile);
15612 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15613 pfn_type->fields (), pfn_type->num_fields (),
15614 pfn_type->has_varargs ());
15615 smash_to_methodptr_type (type, new_type);
15616 }
15617
15618 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15619 requires rewriting, then copy it and return the updated copy.
15620 Otherwise return nullptr. */
15621
15622 static struct type *
15623 rewrite_array_type (struct type *type)
15624 {
15625 if (type->code () != TYPE_CODE_ARRAY)
15626 return nullptr;
15627
15628 struct type *index_type = type->index_type ();
15629 range_bounds *current_bounds = index_type->bounds ();
15630
15631 /* Handle multi-dimensional arrays. */
15632 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15633 if (new_target == nullptr)
15634 {
15635 /* Maybe we don't need to rewrite this array. */
15636 if (current_bounds->low.kind () == PROP_CONST
15637 && current_bounds->high.kind () == PROP_CONST)
15638 return nullptr;
15639 }
15640
15641 /* Either the target type was rewritten, or the bounds have to be
15642 updated. Either way we want to copy the type and update
15643 everything. */
15644 struct type *copy = copy_type (type);
15645 int nfields = copy->num_fields ();
15646 field *new_fields
15647 = ((struct field *) TYPE_ZALLOC (copy,
15648 nfields * sizeof (struct field)));
15649 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15650 copy->set_fields (new_fields);
15651 if (new_target != nullptr)
15652 TYPE_TARGET_TYPE (copy) = new_target;
15653
15654 struct type *index_copy = copy_type (index_type);
15655 range_bounds *bounds
15656 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15657 sizeof (range_bounds));
15658 *bounds = *current_bounds;
15659 bounds->low.set_const_val (1);
15660 bounds->high.set_const_val (0);
15661 index_copy->set_bounds (bounds);
15662 copy->set_index_type (index_copy);
15663
15664 return copy;
15665 }
15666
15667 /* While some versions of GCC will generate complicated DWARF for an
15668 array (see quirk_ada_thick_pointer), more recent versions were
15669 modified to emit an explicit thick pointer structure. However, in
15670 this case, the array still has DWARF expressions for its ranges,
15671 and these must be ignored. */
15672
15673 static void
15674 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15675 struct type *type)
15676 {
15677 gdb_assert (cu->language == language_ada);
15678
15679 /* Check for a structure with two children. */
15680 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15681 return;
15682
15683 /* Check for P_ARRAY and P_BOUNDS members. */
15684 if (TYPE_FIELD_NAME (type, 0) == NULL
15685 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15686 || TYPE_FIELD_NAME (type, 1) == NULL
15687 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15688 return;
15689
15690 /* Make sure we're looking at a pointer to an array. */
15691 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15692 return;
15693
15694 /* The Ada code already knows how to handle these types, so all that
15695 we need to do is turn the bounds into static bounds. However, we
15696 don't want to rewrite existing array or index types in-place,
15697 because those may be referenced in other contexts where this
15698 rewriting is undesirable. */
15699 struct type *new_ary_type
15700 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15701 if (new_ary_type != nullptr)
15702 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15703 }
15704
15705 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15706 appropriate error checking and issuing complaints if there is a
15707 problem. */
15708
15709 static ULONGEST
15710 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15711 {
15712 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15713
15714 if (attr == nullptr)
15715 return 0;
15716
15717 if (!attr->form_is_constant ())
15718 {
15719 complaint (_("DW_AT_alignment must have constant form"
15720 " - DIE at %s [in module %s]"),
15721 sect_offset_str (die->sect_off),
15722 objfile_name (cu->per_objfile->objfile));
15723 return 0;
15724 }
15725
15726 LONGEST val = attr->constant_value (0);
15727 if (val < 0)
15728 {
15729 complaint (_("DW_AT_alignment value must not be negative"
15730 " - DIE at %s [in module %s]"),
15731 sect_offset_str (die->sect_off),
15732 objfile_name (cu->per_objfile->objfile));
15733 return 0;
15734 }
15735 ULONGEST align = val;
15736
15737 if (align == 0)
15738 {
15739 complaint (_("DW_AT_alignment value must not be zero"
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_objfile->objfile));
15743 return 0;
15744 }
15745 if ((align & (align - 1)) != 0)
15746 {
15747 complaint (_("DW_AT_alignment value must be a power of 2"
15748 " - DIE at %s [in module %s]"),
15749 sect_offset_str (die->sect_off),
15750 objfile_name (cu->per_objfile->objfile));
15751 return 0;
15752 }
15753
15754 return align;
15755 }
15756
15757 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15758 the alignment for TYPE. */
15759
15760 static void
15761 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15762 struct type *type)
15763 {
15764 if (!set_type_align (type, get_alignment (cu, die)))
15765 complaint (_("DW_AT_alignment value too large"
15766 " - DIE at %s [in module %s]"),
15767 sect_offset_str (die->sect_off),
15768 objfile_name (cu->per_objfile->objfile));
15769 }
15770
15771 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15772 constant for a type, according to DWARF5 spec, Table 5.5. */
15773
15774 static bool
15775 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15776 {
15777 switch (value)
15778 {
15779 case DW_CC_normal:
15780 case DW_CC_pass_by_reference:
15781 case DW_CC_pass_by_value:
15782 return true;
15783
15784 default:
15785 complaint (_("unrecognized DW_AT_calling_convention value "
15786 "(%s) for a type"), pulongest (value));
15787 return false;
15788 }
15789 }
15790
15791 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15792 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15793 also according to GNU-specific values (see include/dwarf2.h). */
15794
15795 static bool
15796 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15797 {
15798 switch (value)
15799 {
15800 case DW_CC_normal:
15801 case DW_CC_program:
15802 case DW_CC_nocall:
15803 return true;
15804
15805 case DW_CC_GNU_renesas_sh:
15806 case DW_CC_GNU_borland_fastcall_i386:
15807 case DW_CC_GDB_IBM_OpenCL:
15808 return true;
15809
15810 default:
15811 complaint (_("unrecognized DW_AT_calling_convention value "
15812 "(%s) for a subroutine"), pulongest (value));
15813 return false;
15814 }
15815 }
15816
15817 /* Called when we find the DIE that starts a structure or union scope
15818 (definition) to create a type for the structure or union. Fill in
15819 the type's name and general properties; the members will not be
15820 processed until process_structure_scope. A symbol table entry for
15821 the type will also not be done until process_structure_scope (assuming
15822 the type has a name).
15823
15824 NOTE: we need to call these functions regardless of whether or not the
15825 DIE has a DW_AT_name attribute, since it might be an anonymous
15826 structure or union. This gets the type entered into our set of
15827 user defined types. */
15828
15829 static struct type *
15830 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15831 {
15832 struct objfile *objfile = cu->per_objfile->objfile;
15833 struct type *type;
15834 struct attribute *attr;
15835 const char *name;
15836
15837 /* If the definition of this type lives in .debug_types, read that type.
15838 Don't follow DW_AT_specification though, that will take us back up
15839 the chain and we want to go down. */
15840 attr = die->attr (DW_AT_signature);
15841 if (attr != nullptr)
15842 {
15843 type = get_DW_AT_signature_type (die, attr, cu);
15844
15845 /* The type's CU may not be the same as CU.
15846 Ensure TYPE is recorded with CU in die_type_hash. */
15847 return set_die_type (die, type, cu);
15848 }
15849
15850 type = alloc_type (objfile);
15851 INIT_CPLUS_SPECIFIC (type);
15852
15853 name = dwarf2_name (die, cu);
15854 if (name != NULL)
15855 {
15856 if (cu->language == language_cplus
15857 || cu->language == language_d
15858 || cu->language == language_rust)
15859 {
15860 const char *full_name = dwarf2_full_name (name, die, cu);
15861
15862 /* dwarf2_full_name might have already finished building the DIE's
15863 type. If so, there is no need to continue. */
15864 if (get_die_type (die, cu) != NULL)
15865 return get_die_type (die, cu);
15866
15867 type->set_name (full_name);
15868 }
15869 else
15870 {
15871 /* The name is already allocated along with this objfile, so
15872 we don't need to duplicate it for the type. */
15873 type->set_name (name);
15874 }
15875 }
15876
15877 if (die->tag == DW_TAG_structure_type)
15878 {
15879 type->set_code (TYPE_CODE_STRUCT);
15880 }
15881 else if (die->tag == DW_TAG_union_type)
15882 {
15883 type->set_code (TYPE_CODE_UNION);
15884 }
15885 else
15886 {
15887 type->set_code (TYPE_CODE_STRUCT);
15888 }
15889
15890 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15891 type->set_is_declared_class (true);
15892
15893 /* Store the calling convention in the type if it's available in
15894 the die. Otherwise the calling convention remains set to
15895 the default value DW_CC_normal. */
15896 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15897 if (attr != nullptr
15898 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15899 {
15900 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15901 TYPE_CPLUS_CALLING_CONVENTION (type)
15902 = (enum dwarf_calling_convention) (attr->constant_value (0));
15903 }
15904
15905 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15906 if (attr != nullptr)
15907 {
15908 if (attr->form_is_constant ())
15909 TYPE_LENGTH (type) = attr->constant_value (0);
15910 else
15911 {
15912 struct dynamic_prop prop;
15913 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15914 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15915 TYPE_LENGTH (type) = 0;
15916 }
15917 }
15918 else
15919 {
15920 TYPE_LENGTH (type) = 0;
15921 }
15922
15923 maybe_set_alignment (cu, die, type);
15924
15925 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15926 {
15927 /* ICC<14 does not output the required DW_AT_declaration on
15928 incomplete types, but gives them a size of zero. */
15929 type->set_is_stub (true);
15930 }
15931 else
15932 type->set_stub_is_supported (true);
15933
15934 if (die_is_declaration (die, cu))
15935 type->set_is_stub (true);
15936 else if (attr == NULL && die->child == NULL
15937 && producer_is_realview (cu->producer))
15938 /* RealView does not output the required DW_AT_declaration
15939 on incomplete types. */
15940 type->set_is_stub (true);
15941
15942 /* We need to add the type field to the die immediately so we don't
15943 infinitely recurse when dealing with pointers to the structure
15944 type within the structure itself. */
15945 set_die_type (die, type, cu);
15946
15947 /* set_die_type should be already done. */
15948 set_descriptive_type (type, die, cu);
15949
15950 return type;
15951 }
15952
15953 static void handle_struct_member_die
15954 (struct die_info *child_die,
15955 struct type *type,
15956 struct field_info *fi,
15957 std::vector<struct symbol *> *template_args,
15958 struct dwarf2_cu *cu);
15959
15960 /* A helper for handle_struct_member_die that handles
15961 DW_TAG_variant_part. */
15962
15963 static void
15964 handle_variant_part (struct die_info *die, struct type *type,
15965 struct field_info *fi,
15966 std::vector<struct symbol *> *template_args,
15967 struct dwarf2_cu *cu)
15968 {
15969 variant_part_builder *new_part;
15970 if (fi->current_variant_part == nullptr)
15971 {
15972 fi->variant_parts.emplace_back ();
15973 new_part = &fi->variant_parts.back ();
15974 }
15975 else if (!fi->current_variant_part->processing_variant)
15976 {
15977 complaint (_("nested DW_TAG_variant_part seen "
15978 "- DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
15980 objfile_name (cu->per_objfile->objfile));
15981 return;
15982 }
15983 else
15984 {
15985 variant_field &current = fi->current_variant_part->variants.back ();
15986 current.variant_parts.emplace_back ();
15987 new_part = &current.variant_parts.back ();
15988 }
15989
15990 /* When we recurse, we want callees to add to this new variant
15991 part. */
15992 scoped_restore save_current_variant_part
15993 = make_scoped_restore (&fi->current_variant_part, new_part);
15994
15995 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15996 if (discr == NULL)
15997 {
15998 /* It's a univariant form, an extension we support. */
15999 }
16000 else if (discr->form_is_ref ())
16001 {
16002 struct dwarf2_cu *target_cu = cu;
16003 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16004
16005 new_part->discriminant_offset = target_die->sect_off;
16006 }
16007 else
16008 {
16009 complaint (_("DW_AT_discr does not have DIE reference form"
16010 " - DIE at %s [in module %s]"),
16011 sect_offset_str (die->sect_off),
16012 objfile_name (cu->per_objfile->objfile));
16013 }
16014
16015 for (die_info *child_die = die->child;
16016 child_die != NULL;
16017 child_die = child_die->sibling)
16018 handle_struct_member_die (child_die, type, fi, template_args, cu);
16019 }
16020
16021 /* A helper for handle_struct_member_die that handles
16022 DW_TAG_variant. */
16023
16024 static void
16025 handle_variant (struct die_info *die, struct type *type,
16026 struct field_info *fi,
16027 std::vector<struct symbol *> *template_args,
16028 struct dwarf2_cu *cu)
16029 {
16030 if (fi->current_variant_part == nullptr)
16031 {
16032 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16033 "- DIE at %s [in module %s]"),
16034 sect_offset_str (die->sect_off),
16035 objfile_name (cu->per_objfile->objfile));
16036 return;
16037 }
16038 if (fi->current_variant_part->processing_variant)
16039 {
16040 complaint (_("nested DW_TAG_variant seen "
16041 "- DIE at %s [in module %s]"),
16042 sect_offset_str (die->sect_off),
16043 objfile_name (cu->per_objfile->objfile));
16044 return;
16045 }
16046
16047 scoped_restore save_processing_variant
16048 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16049 true);
16050
16051 fi->current_variant_part->variants.emplace_back ();
16052 variant_field &variant = fi->current_variant_part->variants.back ();
16053 variant.first_field = fi->fields.size ();
16054
16055 /* In a variant we want to get the discriminant and also add a
16056 field for our sole member child. */
16057 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16058 if (discr == nullptr || !discr->form_is_constant ())
16059 {
16060 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16061 if (discr == nullptr || discr->as_block ()->size == 0)
16062 variant.default_branch = true;
16063 else
16064 variant.discr_list_data = discr->as_block ();
16065 }
16066 else
16067 variant.discriminant_value = discr->constant_value (0);
16068
16069 for (die_info *variant_child = die->child;
16070 variant_child != NULL;
16071 variant_child = variant_child->sibling)
16072 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16073
16074 variant.last_field = fi->fields.size ();
16075 }
16076
16077 /* A helper for process_structure_scope that handles a single member
16078 DIE. */
16079
16080 static void
16081 handle_struct_member_die (struct die_info *child_die, struct type *type,
16082 struct field_info *fi,
16083 std::vector<struct symbol *> *template_args,
16084 struct dwarf2_cu *cu)
16085 {
16086 if (child_die->tag == DW_TAG_member
16087 || child_die->tag == DW_TAG_variable)
16088 {
16089 /* NOTE: carlton/2002-11-05: A C++ static data member
16090 should be a DW_TAG_member that is a declaration, but
16091 all versions of G++ as of this writing (so through at
16092 least 3.2.1) incorrectly generate DW_TAG_variable
16093 tags for them instead. */
16094 dwarf2_add_field (fi, child_die, cu);
16095 }
16096 else if (child_die->tag == DW_TAG_subprogram)
16097 {
16098 /* Rust doesn't have member functions in the C++ sense.
16099 However, it does emit ordinary functions as children
16100 of a struct DIE. */
16101 if (cu->language == language_rust)
16102 read_func_scope (child_die, cu);
16103 else
16104 {
16105 /* C++ member function. */
16106 dwarf2_add_member_fn (fi, child_die, type, cu);
16107 }
16108 }
16109 else if (child_die->tag == DW_TAG_inheritance)
16110 {
16111 /* C++ base class field. */
16112 dwarf2_add_field (fi, child_die, cu);
16113 }
16114 else if (type_can_define_types (child_die))
16115 dwarf2_add_type_defn (fi, child_die, cu);
16116 else if (child_die->tag == DW_TAG_template_type_param
16117 || child_die->tag == DW_TAG_template_value_param)
16118 {
16119 struct symbol *arg = new_symbol (child_die, NULL, cu);
16120
16121 if (arg != NULL)
16122 template_args->push_back (arg);
16123 }
16124 else if (child_die->tag == DW_TAG_variant_part)
16125 handle_variant_part (child_die, type, fi, template_args, cu);
16126 else if (child_die->tag == DW_TAG_variant)
16127 handle_variant (child_die, type, fi, template_args, cu);
16128 }
16129
16130 /* Finish creating a structure or union type, including filling in
16131 its members and creating a symbol for it. */
16132
16133 static void
16134 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16135 {
16136 struct objfile *objfile = cu->per_objfile->objfile;
16137 struct die_info *child_die;
16138 struct type *type;
16139
16140 type = get_die_type (die, cu);
16141 if (type == NULL)
16142 type = read_structure_type (die, cu);
16143
16144 bool has_template_parameters = false;
16145 if (die->child != NULL && ! die_is_declaration (die, cu))
16146 {
16147 struct field_info fi;
16148 std::vector<struct symbol *> template_args;
16149
16150 child_die = die->child;
16151
16152 while (child_die && child_die->tag)
16153 {
16154 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16155 child_die = child_die->sibling;
16156 }
16157
16158 /* Attach template arguments to type. */
16159 if (!template_args.empty ())
16160 {
16161 has_template_parameters = true;
16162 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16163 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16164 TYPE_TEMPLATE_ARGUMENTS (type)
16165 = XOBNEWVEC (&objfile->objfile_obstack,
16166 struct symbol *,
16167 TYPE_N_TEMPLATE_ARGUMENTS (type));
16168 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16169 template_args.data (),
16170 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16171 * sizeof (struct symbol *)));
16172 }
16173
16174 /* Attach fields and member functions to the type. */
16175 if (fi.nfields () > 0)
16176 dwarf2_attach_fields_to_type (&fi, type, cu);
16177 if (!fi.fnfieldlists.empty ())
16178 {
16179 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16180
16181 /* Get the type which refers to the base class (possibly this
16182 class itself) which contains the vtable pointer for the current
16183 class from the DW_AT_containing_type attribute. This use of
16184 DW_AT_containing_type is a GNU extension. */
16185
16186 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16187 {
16188 struct type *t = die_containing_type (die, cu);
16189
16190 set_type_vptr_basetype (type, t);
16191 if (type == t)
16192 {
16193 int i;
16194
16195 /* Our own class provides vtbl ptr. */
16196 for (i = t->num_fields () - 1;
16197 i >= TYPE_N_BASECLASSES (t);
16198 --i)
16199 {
16200 const char *fieldname = TYPE_FIELD_NAME (t, i);
16201
16202 if (is_vtable_name (fieldname, cu))
16203 {
16204 set_type_vptr_fieldno (type, i);
16205 break;
16206 }
16207 }
16208
16209 /* Complain if virtual function table field not found. */
16210 if (i < TYPE_N_BASECLASSES (t))
16211 complaint (_("virtual function table pointer "
16212 "not found when defining class '%s'"),
16213 type->name () ? type->name () : "");
16214 }
16215 else
16216 {
16217 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16218 }
16219 }
16220 else if (cu->producer
16221 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16222 {
16223 /* The IBM XLC compiler does not provide direct indication
16224 of the containing type, but the vtable pointer is
16225 always named __vfp. */
16226
16227 int i;
16228
16229 for (i = type->num_fields () - 1;
16230 i >= TYPE_N_BASECLASSES (type);
16231 --i)
16232 {
16233 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16234 {
16235 set_type_vptr_fieldno (type, i);
16236 set_type_vptr_basetype (type, type);
16237 break;
16238 }
16239 }
16240 }
16241 }
16242
16243 /* Copy fi.typedef_field_list linked list elements content into the
16244 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16245 if (!fi.typedef_field_list.empty ())
16246 {
16247 int count = fi.typedef_field_list.size ();
16248
16249 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16250 TYPE_TYPEDEF_FIELD_ARRAY (type)
16251 = ((struct decl_field *)
16252 TYPE_ALLOC (type,
16253 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16254 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16255
16256 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16257 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16258 }
16259
16260 /* Copy fi.nested_types_list linked list elements content into the
16261 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16262 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16263 {
16264 int count = fi.nested_types_list.size ();
16265
16266 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16267 TYPE_NESTED_TYPES_ARRAY (type)
16268 = ((struct decl_field *)
16269 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16270 TYPE_NESTED_TYPES_COUNT (type) = count;
16271
16272 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16273 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16274 }
16275 }
16276
16277 quirk_gcc_member_function_pointer (type, objfile);
16278 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16279 cu->rust_unions.push_back (type);
16280 else if (cu->language == language_ada)
16281 quirk_ada_thick_pointer_struct (die, cu, type);
16282
16283 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16284 snapshots) has been known to create a die giving a declaration
16285 for a class that has, as a child, a die giving a definition for a
16286 nested class. So we have to process our children even if the
16287 current die is a declaration. Normally, of course, a declaration
16288 won't have any children at all. */
16289
16290 child_die = die->child;
16291
16292 while (child_die != NULL && child_die->tag)
16293 {
16294 if (child_die->tag == DW_TAG_member
16295 || child_die->tag == DW_TAG_variable
16296 || child_die->tag == DW_TAG_inheritance
16297 || child_die->tag == DW_TAG_template_value_param
16298 || child_die->tag == DW_TAG_template_type_param)
16299 {
16300 /* Do nothing. */
16301 }
16302 else
16303 process_die (child_die, cu);
16304
16305 child_die = child_die->sibling;
16306 }
16307
16308 /* Do not consider external references. According to the DWARF standard,
16309 these DIEs are identified by the fact that they have no byte_size
16310 attribute, and a declaration attribute. */
16311 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16312 || !die_is_declaration (die, cu)
16313 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16314 {
16315 struct symbol *sym = new_symbol (die, type, cu);
16316
16317 if (has_template_parameters)
16318 {
16319 struct symtab *symtab;
16320 if (sym != nullptr)
16321 symtab = symbol_symtab (sym);
16322 else if (cu->line_header != nullptr)
16323 {
16324 /* Any related symtab will do. */
16325 symtab
16326 = cu->line_header->file_names ()[0].symtab;
16327 }
16328 else
16329 {
16330 symtab = nullptr;
16331 complaint (_("could not find suitable "
16332 "symtab for template parameter"
16333 " - DIE at %s [in module %s]"),
16334 sect_offset_str (die->sect_off),
16335 objfile_name (objfile));
16336 }
16337
16338 if (symtab != nullptr)
16339 {
16340 /* Make sure that the symtab is set on the new symbols.
16341 Even though they don't appear in this symtab directly,
16342 other parts of gdb assume that symbols do, and this is
16343 reasonably true. */
16344 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16345 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16346 }
16347 }
16348 }
16349 }
16350
16351 /* Assuming DIE is an enumeration type, and TYPE is its associated
16352 type, update TYPE using some information only available in DIE's
16353 children. In particular, the fields are computed. */
16354
16355 static void
16356 update_enumeration_type_from_children (struct die_info *die,
16357 struct type *type,
16358 struct dwarf2_cu *cu)
16359 {
16360 struct die_info *child_die;
16361 int unsigned_enum = 1;
16362 int flag_enum = 1;
16363
16364 auto_obstack obstack;
16365 std::vector<struct field> fields;
16366
16367 for (child_die = die->child;
16368 child_die != NULL && child_die->tag;
16369 child_die = child_die->sibling)
16370 {
16371 struct attribute *attr;
16372 LONGEST value;
16373 const gdb_byte *bytes;
16374 struct dwarf2_locexpr_baton *baton;
16375 const char *name;
16376
16377 if (child_die->tag != DW_TAG_enumerator)
16378 continue;
16379
16380 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16381 if (attr == NULL)
16382 continue;
16383
16384 name = dwarf2_name (child_die, cu);
16385 if (name == NULL)
16386 name = "<anonymous enumerator>";
16387
16388 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16389 &value, &bytes, &baton);
16390 if (value < 0)
16391 {
16392 unsigned_enum = 0;
16393 flag_enum = 0;
16394 }
16395 else
16396 {
16397 if (count_one_bits_ll (value) >= 2)
16398 flag_enum = 0;
16399 }
16400
16401 fields.emplace_back ();
16402 struct field &field = fields.back ();
16403 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16404 SET_FIELD_ENUMVAL (field, value);
16405 }
16406
16407 if (!fields.empty ())
16408 {
16409 type->set_num_fields (fields.size ());
16410 type->set_fields
16411 ((struct field *)
16412 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16413 memcpy (type->fields (), fields.data (),
16414 sizeof (struct field) * fields.size ());
16415 }
16416
16417 if (unsigned_enum)
16418 type->set_is_unsigned (true);
16419
16420 if (flag_enum)
16421 type->set_is_flag_enum (true);
16422 }
16423
16424 /* Given a DW_AT_enumeration_type die, set its type. We do not
16425 complete the type's fields yet, or create any symbols. */
16426
16427 static struct type *
16428 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16429 {
16430 struct objfile *objfile = cu->per_objfile->objfile;
16431 struct type *type;
16432 struct attribute *attr;
16433 const char *name;
16434
16435 /* If the definition of this type lives in .debug_types, read that type.
16436 Don't follow DW_AT_specification though, that will take us back up
16437 the chain and we want to go down. */
16438 attr = die->attr (DW_AT_signature);
16439 if (attr != nullptr)
16440 {
16441 type = get_DW_AT_signature_type (die, attr, cu);
16442
16443 /* The type's CU may not be the same as CU.
16444 Ensure TYPE is recorded with CU in die_type_hash. */
16445 return set_die_type (die, type, cu);
16446 }
16447
16448 type = alloc_type (objfile);
16449
16450 type->set_code (TYPE_CODE_ENUM);
16451 name = dwarf2_full_name (NULL, die, cu);
16452 if (name != NULL)
16453 type->set_name (name);
16454
16455 attr = dwarf2_attr (die, DW_AT_type, cu);
16456 if (attr != NULL)
16457 {
16458 struct type *underlying_type = die_type (die, cu);
16459
16460 TYPE_TARGET_TYPE (type) = underlying_type;
16461 }
16462
16463 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16464 if (attr != nullptr)
16465 {
16466 TYPE_LENGTH (type) = attr->constant_value (0);
16467 }
16468 else
16469 {
16470 TYPE_LENGTH (type) = 0;
16471 }
16472
16473 maybe_set_alignment (cu, die, type);
16474
16475 /* The enumeration DIE can be incomplete. In Ada, any type can be
16476 declared as private in the package spec, and then defined only
16477 inside the package body. Such types are known as Taft Amendment
16478 Types. When another package uses such a type, an incomplete DIE
16479 may be generated by the compiler. */
16480 if (die_is_declaration (die, cu))
16481 type->set_is_stub (true);
16482
16483 /* If this type has an underlying type that is not a stub, then we
16484 may use its attributes. We always use the "unsigned" attribute
16485 in this situation, because ordinarily we guess whether the type
16486 is unsigned -- but the guess can be wrong and the underlying type
16487 can tell us the reality. However, we defer to a local size
16488 attribute if one exists, because this lets the compiler override
16489 the underlying type if needed. */
16490 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16491 {
16492 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16493 underlying_type = check_typedef (underlying_type);
16494
16495 type->set_is_unsigned (underlying_type->is_unsigned ());
16496
16497 if (TYPE_LENGTH (type) == 0)
16498 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16499
16500 if (TYPE_RAW_ALIGN (type) == 0
16501 && TYPE_RAW_ALIGN (underlying_type) != 0)
16502 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16503 }
16504
16505 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16506
16507 set_die_type (die, type, cu);
16508
16509 /* Finish the creation of this type by using the enum's children.
16510 Note that, as usual, this must come after set_die_type to avoid
16511 infinite recursion when trying to compute the names of the
16512 enumerators. */
16513 update_enumeration_type_from_children (die, type, cu);
16514
16515 return type;
16516 }
16517
16518 /* Given a pointer to a die which begins an enumeration, process all
16519 the dies that define the members of the enumeration, and create the
16520 symbol for the enumeration type.
16521
16522 NOTE: We reverse the order of the element list. */
16523
16524 static void
16525 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16526 {
16527 struct type *this_type;
16528
16529 this_type = get_die_type (die, cu);
16530 if (this_type == NULL)
16531 this_type = read_enumeration_type (die, cu);
16532
16533 if (die->child != NULL)
16534 {
16535 struct die_info *child_die;
16536 const char *name;
16537
16538 child_die = die->child;
16539 while (child_die && child_die->tag)
16540 {
16541 if (child_die->tag != DW_TAG_enumerator)
16542 {
16543 process_die (child_die, cu);
16544 }
16545 else
16546 {
16547 name = dwarf2_name (child_die, cu);
16548 if (name)
16549 new_symbol (child_die, this_type, cu);
16550 }
16551
16552 child_die = child_die->sibling;
16553 }
16554 }
16555
16556 /* If we are reading an enum from a .debug_types unit, and the enum
16557 is a declaration, and the enum is not the signatured type in the
16558 unit, then we do not want to add a symbol for it. Adding a
16559 symbol would in some cases obscure the true definition of the
16560 enum, giving users an incomplete type when the definition is
16561 actually available. Note that we do not want to do this for all
16562 enums which are just declarations, because C++0x allows forward
16563 enum declarations. */
16564 if (cu->per_cu->is_debug_types
16565 && die_is_declaration (die, cu))
16566 {
16567 struct signatured_type *sig_type;
16568
16569 sig_type = (struct signatured_type *) cu->per_cu;
16570 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16571 if (sig_type->type_offset_in_section != die->sect_off)
16572 return;
16573 }
16574
16575 new_symbol (die, this_type, cu);
16576 }
16577
16578 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16579 expression for an index type and finds the corresponding field
16580 offset in the hidden "P_BOUNDS" structure. Returns true on success
16581 and updates *FIELD, false if it fails to recognize an
16582 expression. */
16583
16584 static bool
16585 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16586 int *bounds_offset, struct field *field,
16587 struct dwarf2_cu *cu)
16588 {
16589 struct attribute *attr = dwarf2_attr (die, name, cu);
16590 if (attr == nullptr || !attr->form_is_block ())
16591 return false;
16592
16593 const struct dwarf_block *block = attr->as_block ();
16594 const gdb_byte *start = block->data;
16595 const gdb_byte *end = block->data + block->size;
16596
16597 /* The expression to recognize generally looks like:
16598
16599 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16600 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16601
16602 However, the second "plus_uconst" may be missing:
16603
16604 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16605 DW_OP_deref_size: 4)
16606
16607 This happens when the field is at the start of the structure.
16608
16609 Also, the final deref may not be sized:
16610
16611 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16612 DW_OP_deref)
16613
16614 This happens when the size of the index type happens to be the
16615 same as the architecture's word size. This can occur with or
16616 without the second plus_uconst. */
16617
16618 if (end - start < 2)
16619 return false;
16620 if (*start++ != DW_OP_push_object_address)
16621 return false;
16622 if (*start++ != DW_OP_plus_uconst)
16623 return false;
16624
16625 uint64_t this_bound_off;
16626 start = gdb_read_uleb128 (start, end, &this_bound_off);
16627 if (start == nullptr || (int) this_bound_off != this_bound_off)
16628 return false;
16629 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16630 is consistent among all bounds. */
16631 if (*bounds_offset == -1)
16632 *bounds_offset = this_bound_off;
16633 else if (*bounds_offset != this_bound_off)
16634 return false;
16635
16636 if (start == end || *start++ != DW_OP_deref)
16637 return false;
16638
16639 int offset = 0;
16640 if (start ==end)
16641 return false;
16642 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16643 {
16644 /* This means an offset of 0. */
16645 }
16646 else if (*start++ != DW_OP_plus_uconst)
16647 return false;
16648 else
16649 {
16650 /* The size is the parameter to DW_OP_plus_uconst. */
16651 uint64_t val;
16652 start = gdb_read_uleb128 (start, end, &val);
16653 if (start == nullptr)
16654 return false;
16655 if ((int) val != val)
16656 return false;
16657 offset = val;
16658 }
16659
16660 if (start == end)
16661 return false;
16662
16663 uint64_t size;
16664 if (*start == DW_OP_deref_size)
16665 {
16666 start = gdb_read_uleb128 (start + 1, end, &size);
16667 if (start == nullptr)
16668 return false;
16669 }
16670 else if (*start == DW_OP_deref)
16671 {
16672 size = cu->header.addr_size;
16673 ++start;
16674 }
16675 else
16676 return false;
16677
16678 SET_FIELD_BITPOS (*field, 8 * offset);
16679 if (size != TYPE_LENGTH (field->type ()))
16680 FIELD_BITSIZE (*field) = 8 * size;
16681
16682 return true;
16683 }
16684
16685 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16686 some kinds of Ada arrays:
16687
16688 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16689 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16690 <11e0> DW_AT_data_location: 2 byte block: 97 6
16691 (DW_OP_push_object_address; DW_OP_deref)
16692 <11e3> DW_AT_type : <0x1173>
16693 <11e7> DW_AT_sibling : <0x1201>
16694 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16695 <11ec> DW_AT_type : <0x1206>
16696 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16697 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16698 DW_OP_deref_size: 4)
16699 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16700 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16701 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16702
16703 This actually represents a "thick pointer", which is a structure
16704 with two elements: one that is a pointer to the array data, and one
16705 that is a pointer to another structure; this second structure holds
16706 the array bounds.
16707
16708 This returns a new type on success, or nullptr if this didn't
16709 recognize the type. */
16710
16711 static struct type *
16712 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16713 struct type *type)
16714 {
16715 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16716 /* So far we've only seen this with block form. */
16717 if (attr == nullptr || !attr->form_is_block ())
16718 return nullptr;
16719
16720 /* Note that this will fail if the structure layout is changed by
16721 the compiler. However, we have no good way to recognize some
16722 other layout, because we don't know what expression the compiler
16723 might choose to emit should this happen. */
16724 struct dwarf_block *blk = attr->as_block ();
16725 if (blk->size != 2
16726 || blk->data[0] != DW_OP_push_object_address
16727 || blk->data[1] != DW_OP_deref)
16728 return nullptr;
16729
16730 int bounds_offset = -1;
16731 int max_align = -1;
16732 std::vector<struct field> range_fields;
16733 for (struct die_info *child_die = die->child;
16734 child_die;
16735 child_die = child_die->sibling)
16736 {
16737 if (child_die->tag == DW_TAG_subrange_type)
16738 {
16739 struct type *underlying = read_subrange_index_type (child_die, cu);
16740
16741 int this_align = type_align (underlying);
16742 if (this_align > max_align)
16743 max_align = this_align;
16744
16745 range_fields.emplace_back ();
16746 range_fields.emplace_back ();
16747
16748 struct field &lower = range_fields[range_fields.size () - 2];
16749 struct field &upper = range_fields[range_fields.size () - 1];
16750
16751 lower.set_type (underlying);
16752 FIELD_ARTIFICIAL (lower) = 1;
16753
16754 upper.set_type (underlying);
16755 FIELD_ARTIFICIAL (upper) = 1;
16756
16757 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16758 &bounds_offset, &lower, cu)
16759 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16760 &bounds_offset, &upper, cu))
16761 return nullptr;
16762 }
16763 }
16764
16765 /* This shouldn't really happen, but double-check that we found
16766 where the bounds are stored. */
16767 if (bounds_offset == -1)
16768 return nullptr;
16769
16770 struct objfile *objfile = cu->per_objfile->objfile;
16771 for (int i = 0; i < range_fields.size (); i += 2)
16772 {
16773 char name[20];
16774
16775 /* Set the name of each field in the bounds. */
16776 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16777 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16778 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16779 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16780 }
16781
16782 struct type *bounds = alloc_type (objfile);
16783 bounds->set_code (TYPE_CODE_STRUCT);
16784
16785 bounds->set_num_fields (range_fields.size ());
16786 bounds->set_fields
16787 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16788 * sizeof (struct field))));
16789 memcpy (bounds->fields (), range_fields.data (),
16790 bounds->num_fields () * sizeof (struct field));
16791
16792 int last_fieldno = range_fields.size () - 1;
16793 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16794 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16795 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16796
16797 /* Rewrite the existing array type in place. Specifically, we
16798 remove any dynamic properties we might have read, and we replace
16799 the index types. */
16800 struct type *iter = type;
16801 for (int i = 0; i < range_fields.size (); i += 2)
16802 {
16803 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16804 iter->main_type->dyn_prop_list = nullptr;
16805 iter->set_index_type
16806 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16807 iter = TYPE_TARGET_TYPE (iter);
16808 }
16809
16810 struct type *result = alloc_type (objfile);
16811 result->set_code (TYPE_CODE_STRUCT);
16812
16813 result->set_num_fields (2);
16814 result->set_fields
16815 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16816 * sizeof (struct field))));
16817
16818 /* The names are chosen to coincide with what the compiler does with
16819 -fgnat-encodings=all, which the Ada code in gdb already
16820 understands. */
16821 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16822 result->field (0).set_type (lookup_pointer_type (type));
16823
16824 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16825 result->field (1).set_type (lookup_pointer_type (bounds));
16826 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16827
16828 result->set_name (type->name ());
16829 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16830 + TYPE_LENGTH (result->field (1).type ()));
16831
16832 return result;
16833 }
16834
16835 /* Extract all information from a DW_TAG_array_type DIE and put it in
16836 the DIE's type field. For now, this only handles one dimensional
16837 arrays. */
16838
16839 static struct type *
16840 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16841 {
16842 struct objfile *objfile = cu->per_objfile->objfile;
16843 struct die_info *child_die;
16844 struct type *type;
16845 struct type *element_type, *range_type, *index_type;
16846 struct attribute *attr;
16847 const char *name;
16848 struct dynamic_prop *byte_stride_prop = NULL;
16849 unsigned int bit_stride = 0;
16850
16851 element_type = die_type (die, cu);
16852
16853 /* The die_type call above may have already set the type for this DIE. */
16854 type = get_die_type (die, cu);
16855 if (type)
16856 return type;
16857
16858 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16859 if (attr != NULL)
16860 {
16861 int stride_ok;
16862 struct type *prop_type = cu->addr_sized_int_type (false);
16863
16864 byte_stride_prop
16865 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16866 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16867 prop_type);
16868 if (!stride_ok)
16869 {
16870 complaint (_("unable to read array DW_AT_byte_stride "
16871 " - DIE at %s [in module %s]"),
16872 sect_offset_str (die->sect_off),
16873 objfile_name (cu->per_objfile->objfile));
16874 /* Ignore this attribute. We will likely not be able to print
16875 arrays of this type correctly, but there is little we can do
16876 to help if we cannot read the attribute's value. */
16877 byte_stride_prop = NULL;
16878 }
16879 }
16880
16881 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16882 if (attr != NULL)
16883 bit_stride = attr->constant_value (0);
16884
16885 /* Irix 6.2 native cc creates array types without children for
16886 arrays with unspecified length. */
16887 if (die->child == NULL)
16888 {
16889 index_type = objfile_type (objfile)->builtin_int;
16890 range_type = create_static_range_type (NULL, index_type, 0, -1);
16891 type = create_array_type_with_stride (NULL, element_type, range_type,
16892 byte_stride_prop, bit_stride);
16893 return set_die_type (die, type, cu);
16894 }
16895
16896 std::vector<struct type *> range_types;
16897 child_die = die->child;
16898 while (child_die && child_die->tag)
16899 {
16900 if (child_die->tag == DW_TAG_subrange_type)
16901 {
16902 struct type *child_type = read_type_die (child_die, cu);
16903
16904 if (child_type != NULL)
16905 {
16906 /* The range type was succesfully read. Save it for the
16907 array type creation. */
16908 range_types.push_back (child_type);
16909 }
16910 }
16911 child_die = child_die->sibling;
16912 }
16913
16914 if (range_types.empty ())
16915 {
16916 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16917 sect_offset_str (die->sect_off),
16918 objfile_name (cu->per_objfile->objfile));
16919 return NULL;
16920 }
16921
16922 /* Dwarf2 dimensions are output from left to right, create the
16923 necessary array types in backwards order. */
16924
16925 type = element_type;
16926
16927 if (read_array_order (die, cu) == DW_ORD_col_major)
16928 {
16929 int i = 0;
16930
16931 while (i < range_types.size ())
16932 {
16933 type = create_array_type_with_stride (NULL, type, range_types[i++],
16934 byte_stride_prop, bit_stride);
16935 bit_stride = 0;
16936 byte_stride_prop = nullptr;
16937 }
16938 }
16939 else
16940 {
16941 size_t ndim = range_types.size ();
16942 while (ndim-- > 0)
16943 {
16944 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16945 byte_stride_prop, bit_stride);
16946 bit_stride = 0;
16947 byte_stride_prop = nullptr;
16948 }
16949 }
16950
16951 gdb_assert (type != element_type);
16952
16953 /* Understand Dwarf2 support for vector types (like they occur on
16954 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16955 array type. This is not part of the Dwarf2/3 standard yet, but a
16956 custom vendor extension. The main difference between a regular
16957 array and the vector variant is that vectors are passed by value
16958 to functions. */
16959 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16960 if (attr != nullptr)
16961 make_vector_type (type);
16962
16963 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16964 implementation may choose to implement triple vectors using this
16965 attribute. */
16966 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16967 if (attr != nullptr && attr->form_is_unsigned ())
16968 {
16969 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16970 TYPE_LENGTH (type) = attr->as_unsigned ();
16971 else
16972 complaint (_("DW_AT_byte_size for array type smaller "
16973 "than the total size of elements"));
16974 }
16975
16976 name = dwarf2_name (die, cu);
16977 if (name)
16978 type->set_name (name);
16979
16980 maybe_set_alignment (cu, die, type);
16981
16982 struct type *replacement_type = nullptr;
16983 if (cu->language == language_ada)
16984 {
16985 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16986 if (replacement_type != nullptr)
16987 type = replacement_type;
16988 }
16989
16990 /* Install the type in the die. */
16991 set_die_type (die, type, cu, replacement_type != nullptr);
16992
16993 /* set_die_type should be already done. */
16994 set_descriptive_type (type, die, cu);
16995
16996 return type;
16997 }
16998
16999 static enum dwarf_array_dim_ordering
17000 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17001 {
17002 struct attribute *attr;
17003
17004 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17005
17006 if (attr != nullptr)
17007 {
17008 LONGEST val = attr->constant_value (-1);
17009 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17010 return (enum dwarf_array_dim_ordering) val;
17011 }
17012
17013 /* GNU F77 is a special case, as at 08/2004 array type info is the
17014 opposite order to the dwarf2 specification, but data is still
17015 laid out as per normal fortran.
17016
17017 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17018 version checking. */
17019
17020 if (cu->language == language_fortran
17021 && cu->producer && strstr (cu->producer, "GNU F77"))
17022 {
17023 return DW_ORD_row_major;
17024 }
17025
17026 switch (cu->language_defn->array_ordering ())
17027 {
17028 case array_column_major:
17029 return DW_ORD_col_major;
17030 case array_row_major:
17031 default:
17032 return DW_ORD_row_major;
17033 };
17034 }
17035
17036 /* Extract all information from a DW_TAG_set_type DIE and put it in
17037 the DIE's type field. */
17038
17039 static struct type *
17040 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17041 {
17042 struct type *domain_type, *set_type;
17043 struct attribute *attr;
17044
17045 domain_type = die_type (die, cu);
17046
17047 /* The die_type call above may have already set the type for this DIE. */
17048 set_type = get_die_type (die, cu);
17049 if (set_type)
17050 return set_type;
17051
17052 set_type = create_set_type (NULL, domain_type);
17053
17054 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17055 if (attr != nullptr && attr->form_is_unsigned ())
17056 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17057
17058 maybe_set_alignment (cu, die, set_type);
17059
17060 return set_die_type (die, set_type, cu);
17061 }
17062
17063 /* A helper for read_common_block that creates a locexpr baton.
17064 SYM is the symbol which we are marking as computed.
17065 COMMON_DIE is the DIE for the common block.
17066 COMMON_LOC is the location expression attribute for the common
17067 block itself.
17068 MEMBER_LOC is the location expression attribute for the particular
17069 member of the common block that we are processing.
17070 CU is the CU from which the above come. */
17071
17072 static void
17073 mark_common_block_symbol_computed (struct symbol *sym,
17074 struct die_info *common_die,
17075 struct attribute *common_loc,
17076 struct attribute *member_loc,
17077 struct dwarf2_cu *cu)
17078 {
17079 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17080 struct objfile *objfile = per_objfile->objfile;
17081 struct dwarf2_locexpr_baton *baton;
17082 gdb_byte *ptr;
17083 unsigned int cu_off;
17084 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17085 LONGEST offset = 0;
17086
17087 gdb_assert (common_loc && member_loc);
17088 gdb_assert (common_loc->form_is_block ());
17089 gdb_assert (member_loc->form_is_block ()
17090 || member_loc->form_is_constant ());
17091
17092 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17093 baton->per_objfile = per_objfile;
17094 baton->per_cu = cu->per_cu;
17095 gdb_assert (baton->per_cu);
17096
17097 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17098
17099 if (member_loc->form_is_constant ())
17100 {
17101 offset = member_loc->constant_value (0);
17102 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17103 }
17104 else
17105 baton->size += member_loc->as_block ()->size;
17106
17107 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17108 baton->data = ptr;
17109
17110 *ptr++ = DW_OP_call4;
17111 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17112 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17113 ptr += 4;
17114
17115 if (member_loc->form_is_constant ())
17116 {
17117 *ptr++ = DW_OP_addr;
17118 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17119 ptr += cu->header.addr_size;
17120 }
17121 else
17122 {
17123 /* We have to copy the data here, because DW_OP_call4 will only
17124 use a DW_AT_location attribute. */
17125 struct dwarf_block *block = member_loc->as_block ();
17126 memcpy (ptr, block->data, block->size);
17127 ptr += block->size;
17128 }
17129
17130 *ptr++ = DW_OP_plus;
17131 gdb_assert (ptr - baton->data == baton->size);
17132
17133 SYMBOL_LOCATION_BATON (sym) = baton;
17134 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17135 }
17136
17137 /* Create appropriate locally-scoped variables for all the
17138 DW_TAG_common_block entries. Also create a struct common_block
17139 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17140 is used to separate the common blocks name namespace from regular
17141 variable names. */
17142
17143 static void
17144 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17145 {
17146 struct attribute *attr;
17147
17148 attr = dwarf2_attr (die, DW_AT_location, cu);
17149 if (attr != nullptr)
17150 {
17151 /* Support the .debug_loc offsets. */
17152 if (attr->form_is_block ())
17153 {
17154 /* Ok. */
17155 }
17156 else if (attr->form_is_section_offset ())
17157 {
17158 dwarf2_complex_location_expr_complaint ();
17159 attr = NULL;
17160 }
17161 else
17162 {
17163 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17164 "common block member");
17165 attr = NULL;
17166 }
17167 }
17168
17169 if (die->child != NULL)
17170 {
17171 struct objfile *objfile = cu->per_objfile->objfile;
17172 struct die_info *child_die;
17173 size_t n_entries = 0, size;
17174 struct common_block *common_block;
17175 struct symbol *sym;
17176
17177 for (child_die = die->child;
17178 child_die && child_die->tag;
17179 child_die = child_die->sibling)
17180 ++n_entries;
17181
17182 size = (sizeof (struct common_block)
17183 + (n_entries - 1) * sizeof (struct symbol *));
17184 common_block
17185 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17186 size);
17187 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17188 common_block->n_entries = 0;
17189
17190 for (child_die = die->child;
17191 child_die && child_die->tag;
17192 child_die = child_die->sibling)
17193 {
17194 /* Create the symbol in the DW_TAG_common_block block in the current
17195 symbol scope. */
17196 sym = new_symbol (child_die, NULL, cu);
17197 if (sym != NULL)
17198 {
17199 struct attribute *member_loc;
17200
17201 common_block->contents[common_block->n_entries++] = sym;
17202
17203 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17204 cu);
17205 if (member_loc)
17206 {
17207 /* GDB has handled this for a long time, but it is
17208 not specified by DWARF. It seems to have been
17209 emitted by gfortran at least as recently as:
17210 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17211 complaint (_("Variable in common block has "
17212 "DW_AT_data_member_location "
17213 "- DIE at %s [in module %s]"),
17214 sect_offset_str (child_die->sect_off),
17215 objfile_name (objfile));
17216
17217 if (member_loc->form_is_section_offset ())
17218 dwarf2_complex_location_expr_complaint ();
17219 else if (member_loc->form_is_constant ()
17220 || member_loc->form_is_block ())
17221 {
17222 if (attr != nullptr)
17223 mark_common_block_symbol_computed (sym, die, attr,
17224 member_loc, cu);
17225 }
17226 else
17227 dwarf2_complex_location_expr_complaint ();
17228 }
17229 }
17230 }
17231
17232 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17233 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17234 }
17235 }
17236
17237 /* Create a type for a C++ namespace. */
17238
17239 static struct type *
17240 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17241 {
17242 struct objfile *objfile = cu->per_objfile->objfile;
17243 const char *previous_prefix, *name;
17244 int is_anonymous;
17245 struct type *type;
17246
17247 /* For extensions, reuse the type of the original namespace. */
17248 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17249 {
17250 struct die_info *ext_die;
17251 struct dwarf2_cu *ext_cu = cu;
17252
17253 ext_die = dwarf2_extension (die, &ext_cu);
17254 type = read_type_die (ext_die, ext_cu);
17255
17256 /* EXT_CU may not be the same as CU.
17257 Ensure TYPE is recorded with CU in die_type_hash. */
17258 return set_die_type (die, type, cu);
17259 }
17260
17261 name = namespace_name (die, &is_anonymous, cu);
17262
17263 /* Now build the name of the current namespace. */
17264
17265 previous_prefix = determine_prefix (die, cu);
17266 if (previous_prefix[0] != '\0')
17267 name = typename_concat (&objfile->objfile_obstack,
17268 previous_prefix, name, 0, cu);
17269
17270 /* Create the type. */
17271 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17272
17273 return set_die_type (die, type, cu);
17274 }
17275
17276 /* Read a namespace scope. */
17277
17278 static void
17279 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17280 {
17281 struct objfile *objfile = cu->per_objfile->objfile;
17282 int is_anonymous;
17283
17284 /* Add a symbol associated to this if we haven't seen the namespace
17285 before. Also, add a using directive if it's an anonymous
17286 namespace. */
17287
17288 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17289 {
17290 struct type *type;
17291
17292 type = read_type_die (die, cu);
17293 new_symbol (die, type, cu);
17294
17295 namespace_name (die, &is_anonymous, cu);
17296 if (is_anonymous)
17297 {
17298 const char *previous_prefix = determine_prefix (die, cu);
17299
17300 std::vector<const char *> excludes;
17301 add_using_directive (using_directives (cu),
17302 previous_prefix, type->name (), NULL,
17303 NULL, excludes, 0, &objfile->objfile_obstack);
17304 }
17305 }
17306
17307 if (die->child != NULL)
17308 {
17309 struct die_info *child_die = die->child;
17310
17311 while (child_die && child_die->tag)
17312 {
17313 process_die (child_die, cu);
17314 child_die = child_die->sibling;
17315 }
17316 }
17317 }
17318
17319 /* Read a Fortran module as type. This DIE can be only a declaration used for
17320 imported module. Still we need that type as local Fortran "use ... only"
17321 declaration imports depend on the created type in determine_prefix. */
17322
17323 static struct type *
17324 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17325 {
17326 struct objfile *objfile = cu->per_objfile->objfile;
17327 const char *module_name;
17328 struct type *type;
17329
17330 module_name = dwarf2_name (die, cu);
17331 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17332
17333 return set_die_type (die, type, cu);
17334 }
17335
17336 /* Read a Fortran module. */
17337
17338 static void
17339 read_module (struct die_info *die, struct dwarf2_cu *cu)
17340 {
17341 struct die_info *child_die = die->child;
17342 struct type *type;
17343
17344 type = read_type_die (die, cu);
17345 new_symbol (die, type, cu);
17346
17347 while (child_die && child_die->tag)
17348 {
17349 process_die (child_die, cu);
17350 child_die = child_die->sibling;
17351 }
17352 }
17353
17354 /* Return the name of the namespace represented by DIE. Set
17355 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17356 namespace. */
17357
17358 static const char *
17359 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17360 {
17361 struct die_info *current_die;
17362 const char *name = NULL;
17363
17364 /* Loop through the extensions until we find a name. */
17365
17366 for (current_die = die;
17367 current_die != NULL;
17368 current_die = dwarf2_extension (die, &cu))
17369 {
17370 /* We don't use dwarf2_name here so that we can detect the absence
17371 of a name -> anonymous namespace. */
17372 name = dwarf2_string_attr (die, DW_AT_name, cu);
17373
17374 if (name != NULL)
17375 break;
17376 }
17377
17378 /* Is it an anonymous namespace? */
17379
17380 *is_anonymous = (name == NULL);
17381 if (*is_anonymous)
17382 name = CP_ANONYMOUS_NAMESPACE_STR;
17383
17384 return name;
17385 }
17386
17387 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17388 the user defined type vector. */
17389
17390 static struct type *
17391 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17392 {
17393 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17394 struct comp_unit_head *cu_header = &cu->header;
17395 struct type *type;
17396 struct attribute *attr_byte_size;
17397 struct attribute *attr_address_class;
17398 int byte_size, addr_class;
17399 struct type *target_type;
17400
17401 target_type = die_type (die, cu);
17402
17403 /* The die_type call above may have already set the type for this DIE. */
17404 type = get_die_type (die, cu);
17405 if (type)
17406 return type;
17407
17408 type = lookup_pointer_type (target_type);
17409
17410 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17411 if (attr_byte_size)
17412 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17413 else
17414 byte_size = cu_header->addr_size;
17415
17416 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17417 if (attr_address_class)
17418 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17419 else
17420 addr_class = DW_ADDR_none;
17421
17422 ULONGEST alignment = get_alignment (cu, die);
17423
17424 /* If the pointer size, alignment, or address class is different
17425 than the default, create a type variant marked as such and set
17426 the length accordingly. */
17427 if (TYPE_LENGTH (type) != byte_size
17428 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17429 && alignment != TYPE_RAW_ALIGN (type))
17430 || addr_class != DW_ADDR_none)
17431 {
17432 if (gdbarch_address_class_type_flags_p (gdbarch))
17433 {
17434 type_instance_flags type_flags
17435 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17436 addr_class);
17437 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17438 == 0);
17439 type = make_type_with_address_space (type, type_flags);
17440 }
17441 else if (TYPE_LENGTH (type) != byte_size)
17442 {
17443 complaint (_("invalid pointer size %d"), byte_size);
17444 }
17445 else if (TYPE_RAW_ALIGN (type) != alignment)
17446 {
17447 complaint (_("Invalid DW_AT_alignment"
17448 " - DIE at %s [in module %s]"),
17449 sect_offset_str (die->sect_off),
17450 objfile_name (cu->per_objfile->objfile));
17451 }
17452 else
17453 {
17454 /* Should we also complain about unhandled address classes? */
17455 }
17456 }
17457
17458 TYPE_LENGTH (type) = byte_size;
17459 set_type_align (type, alignment);
17460 return set_die_type (die, type, cu);
17461 }
17462
17463 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17464 the user defined type vector. */
17465
17466 static struct type *
17467 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17468 {
17469 struct type *type;
17470 struct type *to_type;
17471 struct type *domain;
17472
17473 to_type = die_type (die, cu);
17474 domain = die_containing_type (die, cu);
17475
17476 /* The calls above may have already set the type for this DIE. */
17477 type = get_die_type (die, cu);
17478 if (type)
17479 return type;
17480
17481 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17482 type = lookup_methodptr_type (to_type);
17483 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17484 {
17485 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17486
17487 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17488 to_type->fields (), to_type->num_fields (),
17489 to_type->has_varargs ());
17490 type = lookup_methodptr_type (new_type);
17491 }
17492 else
17493 type = lookup_memberptr_type (to_type, domain);
17494
17495 return set_die_type (die, type, cu);
17496 }
17497
17498 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17499 the user defined type vector. */
17500
17501 static struct type *
17502 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17503 enum type_code refcode)
17504 {
17505 struct comp_unit_head *cu_header = &cu->header;
17506 struct type *type, *target_type;
17507 struct attribute *attr;
17508
17509 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17510
17511 target_type = die_type (die, cu);
17512
17513 /* The die_type call above may have already set the type for this DIE. */
17514 type = get_die_type (die, cu);
17515 if (type)
17516 return type;
17517
17518 type = lookup_reference_type (target_type, refcode);
17519 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17520 if (attr != nullptr)
17521 {
17522 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17523 }
17524 else
17525 {
17526 TYPE_LENGTH (type) = cu_header->addr_size;
17527 }
17528 maybe_set_alignment (cu, die, type);
17529 return set_die_type (die, type, cu);
17530 }
17531
17532 /* Add the given cv-qualifiers to the element type of the array. GCC
17533 outputs DWARF type qualifiers that apply to an array, not the
17534 element type. But GDB relies on the array element type to carry
17535 the cv-qualifiers. This mimics section 6.7.3 of the C99
17536 specification. */
17537
17538 static struct type *
17539 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17540 struct type *base_type, int cnst, int voltl)
17541 {
17542 struct type *el_type, *inner_array;
17543
17544 base_type = copy_type (base_type);
17545 inner_array = base_type;
17546
17547 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17548 {
17549 TYPE_TARGET_TYPE (inner_array) =
17550 copy_type (TYPE_TARGET_TYPE (inner_array));
17551 inner_array = TYPE_TARGET_TYPE (inner_array);
17552 }
17553
17554 el_type = TYPE_TARGET_TYPE (inner_array);
17555 cnst |= TYPE_CONST (el_type);
17556 voltl |= TYPE_VOLATILE (el_type);
17557 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17558
17559 return set_die_type (die, base_type, cu);
17560 }
17561
17562 static struct type *
17563 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17564 {
17565 struct type *base_type, *cv_type;
17566
17567 base_type = die_type (die, cu);
17568
17569 /* The die_type call above may have already set the type for this DIE. */
17570 cv_type = get_die_type (die, cu);
17571 if (cv_type)
17572 return cv_type;
17573
17574 /* In case the const qualifier is applied to an array type, the element type
17575 is so qualified, not the array type (section 6.7.3 of C99). */
17576 if (base_type->code () == TYPE_CODE_ARRAY)
17577 return add_array_cv_type (die, cu, base_type, 1, 0);
17578
17579 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17580 return set_die_type (die, cv_type, cu);
17581 }
17582
17583 static struct type *
17584 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17585 {
17586 struct type *base_type, *cv_type;
17587
17588 base_type = die_type (die, cu);
17589
17590 /* The die_type call above may have already set the type for this DIE. */
17591 cv_type = get_die_type (die, cu);
17592 if (cv_type)
17593 return cv_type;
17594
17595 /* In case the volatile qualifier is applied to an array type, the
17596 element type is so qualified, not the array type (section 6.7.3
17597 of C99). */
17598 if (base_type->code () == TYPE_CODE_ARRAY)
17599 return add_array_cv_type (die, cu, base_type, 0, 1);
17600
17601 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17602 return set_die_type (die, cv_type, cu);
17603 }
17604
17605 /* Handle DW_TAG_restrict_type. */
17606
17607 static struct type *
17608 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17609 {
17610 struct type *base_type, *cv_type;
17611
17612 base_type = die_type (die, cu);
17613
17614 /* The die_type call above may have already set the type for this DIE. */
17615 cv_type = get_die_type (die, cu);
17616 if (cv_type)
17617 return cv_type;
17618
17619 cv_type = make_restrict_type (base_type);
17620 return set_die_type (die, cv_type, cu);
17621 }
17622
17623 /* Handle DW_TAG_atomic_type. */
17624
17625 static struct type *
17626 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17627 {
17628 struct type *base_type, *cv_type;
17629
17630 base_type = die_type (die, cu);
17631
17632 /* The die_type call above may have already set the type for this DIE. */
17633 cv_type = get_die_type (die, cu);
17634 if (cv_type)
17635 return cv_type;
17636
17637 cv_type = make_atomic_type (base_type);
17638 return set_die_type (die, cv_type, cu);
17639 }
17640
17641 /* Extract all information from a DW_TAG_string_type DIE and add to
17642 the user defined type vector. It isn't really a user defined type,
17643 but it behaves like one, with other DIE's using an AT_user_def_type
17644 attribute to reference it. */
17645
17646 static struct type *
17647 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17648 {
17649 struct objfile *objfile = cu->per_objfile->objfile;
17650 struct gdbarch *gdbarch = objfile->arch ();
17651 struct type *type, *range_type, *index_type, *char_type;
17652 struct attribute *attr;
17653 struct dynamic_prop prop;
17654 bool length_is_constant = true;
17655 LONGEST length;
17656
17657 /* There are a couple of places where bit sizes might be made use of
17658 when parsing a DW_TAG_string_type, however, no producer that we know
17659 of make use of these. Handling bit sizes that are a multiple of the
17660 byte size is easy enough, but what about other bit sizes? Lets deal
17661 with that problem when we have to. Warn about these attributes being
17662 unsupported, then parse the type and ignore them like we always
17663 have. */
17664 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17665 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17666 {
17667 static bool warning_printed = false;
17668 if (!warning_printed)
17669 {
17670 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17671 "currently supported on DW_TAG_string_type."));
17672 warning_printed = true;
17673 }
17674 }
17675
17676 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17677 if (attr != nullptr && !attr->form_is_constant ())
17678 {
17679 /* The string length describes the location at which the length of
17680 the string can be found. The size of the length field can be
17681 specified with one of the attributes below. */
17682 struct type *prop_type;
17683 struct attribute *len
17684 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17685 if (len == nullptr)
17686 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17687 if (len != nullptr && len->form_is_constant ())
17688 {
17689 /* Pass 0 as the default as we know this attribute is constant
17690 and the default value will not be returned. */
17691 LONGEST sz = len->constant_value (0);
17692 prop_type = cu->per_objfile->int_type (sz, true);
17693 }
17694 else
17695 {
17696 /* If the size is not specified then we assume it is the size of
17697 an address on this target. */
17698 prop_type = cu->addr_sized_int_type (true);
17699 }
17700
17701 /* Convert the attribute into a dynamic property. */
17702 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17703 length = 1;
17704 else
17705 length_is_constant = false;
17706 }
17707 else if (attr != nullptr)
17708 {
17709 /* This DW_AT_string_length just contains the length with no
17710 indirection. There's no need to create a dynamic property in this
17711 case. Pass 0 for the default value as we know it will not be
17712 returned in this case. */
17713 length = attr->constant_value (0);
17714 }
17715 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17716 {
17717 /* We don't currently support non-constant byte sizes for strings. */
17718 length = attr->constant_value (1);
17719 }
17720 else
17721 {
17722 /* Use 1 as a fallback length if we have nothing else. */
17723 length = 1;
17724 }
17725
17726 index_type = objfile_type (objfile)->builtin_int;
17727 if (length_is_constant)
17728 range_type = create_static_range_type (NULL, index_type, 1, length);
17729 else
17730 {
17731 struct dynamic_prop low_bound;
17732
17733 low_bound.set_const_val (1);
17734 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17735 }
17736 char_type = language_string_char_type (cu->language_defn, gdbarch);
17737 type = create_string_type (NULL, char_type, range_type);
17738
17739 return set_die_type (die, type, cu);
17740 }
17741
17742 /* Assuming that DIE corresponds to a function, returns nonzero
17743 if the function is prototyped. */
17744
17745 static int
17746 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17747 {
17748 struct attribute *attr;
17749
17750 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17751 if (attr && attr->as_boolean ())
17752 return 1;
17753
17754 /* The DWARF standard implies that the DW_AT_prototyped attribute
17755 is only meaningful for C, but the concept also extends to other
17756 languages that allow unprototyped functions (Eg: Objective C).
17757 For all other languages, assume that functions are always
17758 prototyped. */
17759 if (cu->language != language_c
17760 && cu->language != language_objc
17761 && cu->language != language_opencl)
17762 return 1;
17763
17764 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17765 prototyped and unprototyped functions; default to prototyped,
17766 since that is more common in modern code (and RealView warns
17767 about unprototyped functions). */
17768 if (producer_is_realview (cu->producer))
17769 return 1;
17770
17771 return 0;
17772 }
17773
17774 /* Handle DIES due to C code like:
17775
17776 struct foo
17777 {
17778 int (*funcp)(int a, long l);
17779 int b;
17780 };
17781
17782 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17783
17784 static struct type *
17785 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17786 {
17787 struct objfile *objfile = cu->per_objfile->objfile;
17788 struct type *type; /* Type that this function returns. */
17789 struct type *ftype; /* Function that returns above type. */
17790 struct attribute *attr;
17791
17792 type = die_type (die, cu);
17793
17794 /* The die_type call above may have already set the type for this DIE. */
17795 ftype = get_die_type (die, cu);
17796 if (ftype)
17797 return ftype;
17798
17799 ftype = lookup_function_type (type);
17800
17801 if (prototyped_function_p (die, cu))
17802 ftype->set_is_prototyped (true);
17803
17804 /* Store the calling convention in the type if it's available in
17805 the subroutine die. Otherwise set the calling convention to
17806 the default value DW_CC_normal. */
17807 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17808 if (attr != nullptr
17809 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17810 TYPE_CALLING_CONVENTION (ftype)
17811 = (enum dwarf_calling_convention) attr->constant_value (0);
17812 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17813 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17814 else
17815 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17816
17817 /* Record whether the function returns normally to its caller or not
17818 if the DWARF producer set that information. */
17819 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17820 if (attr && attr->as_boolean ())
17821 TYPE_NO_RETURN (ftype) = 1;
17822
17823 /* We need to add the subroutine type to the die immediately so
17824 we don't infinitely recurse when dealing with parameters
17825 declared as the same subroutine type. */
17826 set_die_type (die, ftype, cu);
17827
17828 if (die->child != NULL)
17829 {
17830 struct type *void_type = objfile_type (objfile)->builtin_void;
17831 struct die_info *child_die;
17832 int nparams, iparams;
17833
17834 /* Count the number of parameters.
17835 FIXME: GDB currently ignores vararg functions, but knows about
17836 vararg member functions. */
17837 nparams = 0;
17838 child_die = die->child;
17839 while (child_die && child_die->tag)
17840 {
17841 if (child_die->tag == DW_TAG_formal_parameter)
17842 nparams++;
17843 else if (child_die->tag == DW_TAG_unspecified_parameters)
17844 ftype->set_has_varargs (true);
17845
17846 child_die = child_die->sibling;
17847 }
17848
17849 /* Allocate storage for parameters and fill them in. */
17850 ftype->set_num_fields (nparams);
17851 ftype->set_fields
17852 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17853
17854 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17855 even if we error out during the parameters reading below. */
17856 for (iparams = 0; iparams < nparams; iparams++)
17857 ftype->field (iparams).set_type (void_type);
17858
17859 iparams = 0;
17860 child_die = die->child;
17861 while (child_die && child_die->tag)
17862 {
17863 if (child_die->tag == DW_TAG_formal_parameter)
17864 {
17865 struct type *arg_type;
17866
17867 /* DWARF version 2 has no clean way to discern C++
17868 static and non-static member functions. G++ helps
17869 GDB by marking the first parameter for non-static
17870 member functions (which is the this pointer) as
17871 artificial. We pass this information to
17872 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17873
17874 DWARF version 3 added DW_AT_object_pointer, which GCC
17875 4.5 does not yet generate. */
17876 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17877 if (attr != nullptr)
17878 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17879 else
17880 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17881 arg_type = die_type (child_die, cu);
17882
17883 /* RealView does not mark THIS as const, which the testsuite
17884 expects. GCC marks THIS as const in method definitions,
17885 but not in the class specifications (GCC PR 43053). */
17886 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17887 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17888 {
17889 int is_this = 0;
17890 struct dwarf2_cu *arg_cu = cu;
17891 const char *name = dwarf2_name (child_die, cu);
17892
17893 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17894 if (attr != nullptr)
17895 {
17896 /* If the compiler emits this, use it. */
17897 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17898 is_this = 1;
17899 }
17900 else if (name && strcmp (name, "this") == 0)
17901 /* Function definitions will have the argument names. */
17902 is_this = 1;
17903 else if (name == NULL && iparams == 0)
17904 /* Declarations may not have the names, so like
17905 elsewhere in GDB, assume an artificial first
17906 argument is "this". */
17907 is_this = 1;
17908
17909 if (is_this)
17910 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17911 arg_type, 0);
17912 }
17913
17914 ftype->field (iparams).set_type (arg_type);
17915 iparams++;
17916 }
17917 child_die = child_die->sibling;
17918 }
17919 }
17920
17921 return ftype;
17922 }
17923
17924 static struct type *
17925 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17926 {
17927 struct objfile *objfile = cu->per_objfile->objfile;
17928 const char *name = NULL;
17929 struct type *this_type, *target_type;
17930
17931 name = dwarf2_full_name (NULL, die, cu);
17932 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17933 this_type->set_target_is_stub (true);
17934 set_die_type (die, this_type, cu);
17935 target_type = die_type (die, cu);
17936 if (target_type != this_type)
17937 TYPE_TARGET_TYPE (this_type) = target_type;
17938 else
17939 {
17940 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17941 spec and cause infinite loops in GDB. */
17942 complaint (_("Self-referential DW_TAG_typedef "
17943 "- DIE at %s [in module %s]"),
17944 sect_offset_str (die->sect_off), objfile_name (objfile));
17945 TYPE_TARGET_TYPE (this_type) = NULL;
17946 }
17947 if (name == NULL)
17948 {
17949 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17950 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17951 Handle these by just returning the target type, rather than
17952 constructing an anonymous typedef type and trying to handle this
17953 elsewhere. */
17954 set_die_type (die, target_type, cu);
17955 return target_type;
17956 }
17957 return this_type;
17958 }
17959
17960 /* Helper for get_dwarf2_rational_constant that computes the value of
17961 a given gmp_mpz given an attribute. */
17962
17963 static void
17964 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17965 {
17966 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17967 location expression that pushes an implicit value. */
17968 if (attr->form == DW_FORM_exprloc)
17969 {
17970 dwarf_block *blk = attr->as_block ();
17971 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17972 {
17973 uint64_t len;
17974 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17975 blk->data + blk->size,
17976 &len);
17977 if (ptr - blk->data + len <= blk->size)
17978 {
17979 mpz_import (value->val, len,
17980 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17981 1, 0, 0, ptr);
17982 return;
17983 }
17984 }
17985
17986 /* On failure set it to 1. */
17987 *value = gdb_mpz (1);
17988 }
17989 else if (attr->form_is_block ())
17990 {
17991 dwarf_block *blk = attr->as_block ();
17992 mpz_import (value->val, blk->size,
17993 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17994 1, 0, 0, blk->data);
17995 }
17996 else
17997 *value = gdb_mpz (attr->constant_value (1));
17998 }
17999
18000 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18001 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18002
18003 If the numerator and/or numerator attribute is missing,
18004 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18005 untouched. */
18006
18007 static void
18008 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18009 gdb_mpz *numerator, gdb_mpz *denominator)
18010 {
18011 struct attribute *num_attr, *denom_attr;
18012
18013 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18014 if (num_attr == nullptr)
18015 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18016 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18017
18018 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18019 if (denom_attr == nullptr)
18020 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18021 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18022
18023 if (num_attr == nullptr || denom_attr == nullptr)
18024 return;
18025
18026 get_mpz (cu, numerator, num_attr);
18027 get_mpz (cu, denominator, denom_attr);
18028 }
18029
18030 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18031 rational constant, rather than a signed one.
18032
18033 If the rational constant has a negative value, a complaint
18034 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18035
18036 static void
18037 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18038 struct dwarf2_cu *cu,
18039 gdb_mpz *numerator,
18040 gdb_mpz *denominator)
18041 {
18042 gdb_mpz num (1);
18043 gdb_mpz denom (1);
18044
18045 get_dwarf2_rational_constant (die, cu, &num, &denom);
18046 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18047 {
18048 mpz_neg (num.val, num.val);
18049 mpz_neg (denom.val, denom.val);
18050 }
18051 else if (mpz_sgn (num.val) == -1)
18052 {
18053 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18054 " in DIE at %s"),
18055 sect_offset_str (die->sect_off));
18056 return;
18057 }
18058 else if (mpz_sgn (denom.val) == -1)
18059 {
18060 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18061 " in DIE at %s"),
18062 sect_offset_str (die->sect_off));
18063 return;
18064 }
18065
18066 *numerator = std::move (num);
18067 *denominator = std::move (denom);
18068 }
18069
18070 /* Assuming that ENCODING is a string whose contents starting at the
18071 K'th character is "_nn" where "nn" is a decimal number, scan that
18072 number and set RESULT to the value. K is updated to point to the
18073 character immediately following the number.
18074
18075 If the string does not conform to the format described above, false
18076 is returned, and K may or may not be changed. */
18077
18078 static bool
18079 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18080 {
18081 /* The next character should be an underscore ('_') followed
18082 by a digit. */
18083 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18084 return false;
18085
18086 /* Skip the underscore. */
18087 k++;
18088 int start = k;
18089
18090 /* Determine the number of digits for our number. */
18091 while (isdigit (encoding[k]))
18092 k++;
18093 if (k == start)
18094 return false;
18095
18096 std::string copy (&encoding[start], k - start);
18097 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18098 return false;
18099
18100 return true;
18101 }
18102
18103 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18104 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18105 DENOM, update OFFSET, and return true on success. Return false on
18106 failure. */
18107
18108 static bool
18109 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18110 gdb_mpz *num, gdb_mpz *denom)
18111 {
18112 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18113 return false;
18114 return ada_get_gnat_encoded_number (encoding, offset, denom);
18115 }
18116
18117 /* Assuming DIE corresponds to a fixed point type, finish the creation
18118 of the corresponding TYPE by setting its type-specific data. CU is
18119 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18120 encodings. It is nullptr if the GNAT encoding should be
18121 ignored. */
18122
18123 static void
18124 finish_fixed_point_type (struct type *type, const char *suffix,
18125 struct die_info *die, struct dwarf2_cu *cu)
18126 {
18127 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18128 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18129
18130 /* If GNAT encodings are preferred, don't examine the
18131 attributes. */
18132 struct attribute *attr = nullptr;
18133 if (suffix == nullptr)
18134 {
18135 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18136 if (attr == nullptr)
18137 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18138 if (attr == nullptr)
18139 attr = dwarf2_attr (die, DW_AT_small, cu);
18140 }
18141
18142 /* Numerator and denominator of our fixed-point type's scaling factor.
18143 The default is a scaling factor of 1, which we use as a fallback
18144 when we are not able to decode it (problem with the debugging info,
18145 unsupported forms, bug in GDB, etc...). Using that as the default
18146 allows us to at least print the unscaled value, which might still
18147 be useful to a user. */
18148 gdb_mpz scale_num (1);
18149 gdb_mpz scale_denom (1);
18150
18151 if (attr == nullptr)
18152 {
18153 int offset = 0;
18154 if (suffix != nullptr
18155 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18156 &scale_denom)
18157 /* The number might be encoded as _nn_dd_nn_dd, where the
18158 second ratio is the 'small value. In this situation, we
18159 want the second value. */
18160 && (suffix[offset] != '_'
18161 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18162 &scale_denom)))
18163 {
18164 /* Found it. */
18165 }
18166 else
18167 {
18168 /* Scaling factor not found. Assume a scaling factor of 1,
18169 and hope for the best. At least the user will be able to
18170 see the encoded value. */
18171 scale_num = 1;
18172 scale_denom = 1;
18173 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18174 sect_offset_str (die->sect_off));
18175 }
18176 }
18177 else if (attr->name == DW_AT_binary_scale)
18178 {
18179 LONGEST scale_exp = attr->constant_value (0);
18180 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18181
18182 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18183 }
18184 else if (attr->name == DW_AT_decimal_scale)
18185 {
18186 LONGEST scale_exp = attr->constant_value (0);
18187 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18188
18189 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18190 }
18191 else if (attr->name == DW_AT_small)
18192 {
18193 struct die_info *scale_die;
18194 struct dwarf2_cu *scale_cu = cu;
18195
18196 scale_die = follow_die_ref (die, attr, &scale_cu);
18197 if (scale_die->tag == DW_TAG_constant)
18198 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18199 &scale_num, &scale_denom);
18200 else
18201 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18202 " (DIE at %s)"),
18203 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18204 }
18205 else
18206 {
18207 complaint (_("unsupported scale attribute %s for fixed-point type"
18208 " (DIE at %s)"),
18209 dwarf_attr_name (attr->name),
18210 sect_offset_str (die->sect_off));
18211 }
18212
18213 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18214 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18215 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18216 mpq_canonicalize (scaling_factor.val);
18217 }
18218
18219 /* The gnat-encoding suffix for fixed point. */
18220
18221 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18222
18223 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18224 "XF" suffix of the name. The text after this is what encodes the
18225 'small and 'delta information. Otherwise, return nullptr. */
18226
18227 static const char *
18228 gnat_encoded_fixed_point_type_info (const char *name)
18229 {
18230 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18231 }
18232
18233 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18234 (which may be different from NAME) to the architecture back-end to allow
18235 it to guess the correct format if necessary. */
18236
18237 static struct type *
18238 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18239 const char *name_hint, enum bfd_endian byte_order)
18240 {
18241 struct gdbarch *gdbarch = objfile->arch ();
18242 const struct floatformat **format;
18243 struct type *type;
18244
18245 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18246 if (format)
18247 type = init_float_type (objfile, bits, name, format, byte_order);
18248 else
18249 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18250
18251 return type;
18252 }
18253
18254 /* Allocate an integer type of size BITS and name NAME. */
18255
18256 static struct type *
18257 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18258 int bits, int unsigned_p, const char *name)
18259 {
18260 struct type *type;
18261
18262 /* Versions of Intel's C Compiler generate an integer type called "void"
18263 instead of using DW_TAG_unspecified_type. This has been seen on
18264 at least versions 14, 17, and 18. */
18265 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18266 && strcmp (name, "void") == 0)
18267 type = objfile_type (objfile)->builtin_void;
18268 else
18269 type = init_integer_type (objfile, bits, unsigned_p, name);
18270
18271 return type;
18272 }
18273
18274 /* Return true if DIE has a DW_AT_small attribute whose value is
18275 a constant rational, where both the numerator and denominator
18276 are equal to zero.
18277
18278 CU is the DIE's Compilation Unit. */
18279
18280 static bool
18281 has_zero_over_zero_small_attribute (struct die_info *die,
18282 struct dwarf2_cu *cu)
18283 {
18284 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18285 if (attr == nullptr)
18286 return false;
18287
18288 struct dwarf2_cu *scale_cu = cu;
18289 struct die_info *scale_die
18290 = follow_die_ref (die, attr, &scale_cu);
18291
18292 if (scale_die->tag != DW_TAG_constant)
18293 return false;
18294
18295 gdb_mpz num (1), denom (1);
18296 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18297 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18298 }
18299
18300 /* Initialise and return a floating point type of size BITS suitable for
18301 use as a component of a complex number. The NAME_HINT is passed through
18302 when initialising the floating point type and is the name of the complex
18303 type.
18304
18305 As DWARF doesn't currently provide an explicit name for the components
18306 of a complex number, but it can be helpful to have these components
18307 named, we try to select a suitable name based on the size of the
18308 component. */
18309 static struct type *
18310 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18311 struct objfile *objfile,
18312 int bits, const char *name_hint,
18313 enum bfd_endian byte_order)
18314 {
18315 gdbarch *gdbarch = objfile->arch ();
18316 struct type *tt = nullptr;
18317
18318 /* Try to find a suitable floating point builtin type of size BITS.
18319 We're going to use the name of this type as the name for the complex
18320 target type that we are about to create. */
18321 switch (cu->language)
18322 {
18323 case language_fortran:
18324 switch (bits)
18325 {
18326 case 32:
18327 tt = builtin_f_type (gdbarch)->builtin_real;
18328 break;
18329 case 64:
18330 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18331 break;
18332 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18333 case 128:
18334 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18335 break;
18336 }
18337 break;
18338 default:
18339 switch (bits)
18340 {
18341 case 32:
18342 tt = builtin_type (gdbarch)->builtin_float;
18343 break;
18344 case 64:
18345 tt = builtin_type (gdbarch)->builtin_double;
18346 break;
18347 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18348 case 128:
18349 tt = builtin_type (gdbarch)->builtin_long_double;
18350 break;
18351 }
18352 break;
18353 }
18354
18355 /* If the type we found doesn't match the size we were looking for, then
18356 pretend we didn't find a type at all, the complex target type we
18357 create will then be nameless. */
18358 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18359 tt = nullptr;
18360
18361 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18362 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18363 }
18364
18365 /* Find a representation of a given base type and install
18366 it in the TYPE field of the die. */
18367
18368 static struct type *
18369 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18370 {
18371 struct objfile *objfile = cu->per_objfile->objfile;
18372 struct type *type;
18373 struct attribute *attr;
18374 int encoding = 0, bits = 0;
18375 const char *name;
18376 gdbarch *arch;
18377
18378 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18379 if (attr != nullptr && attr->form_is_constant ())
18380 encoding = attr->constant_value (0);
18381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18382 if (attr != nullptr)
18383 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18384 name = dwarf2_name (die, cu);
18385 if (!name)
18386 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18387
18388 arch = objfile->arch ();
18389 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18390
18391 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18392 if (attr != nullptr && attr->form_is_constant ())
18393 {
18394 int endianity = attr->constant_value (0);
18395
18396 switch (endianity)
18397 {
18398 case DW_END_big:
18399 byte_order = BFD_ENDIAN_BIG;
18400 break;
18401 case DW_END_little:
18402 byte_order = BFD_ENDIAN_LITTLE;
18403 break;
18404 default:
18405 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18406 break;
18407 }
18408 }
18409
18410 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18411 && cu->language == language_ada
18412 && has_zero_over_zero_small_attribute (die, cu))
18413 {
18414 /* brobecker/2018-02-24: This is a fixed point type for which
18415 the scaling factor is represented as fraction whose value
18416 does not make sense (zero divided by zero), so we should
18417 normally never see these. However, there is a small category
18418 of fixed point types for which GNAT is unable to provide
18419 the scaling factor via the standard DWARF mechanisms, and
18420 for which the info is provided via the GNAT encodings instead.
18421 This is likely what this DIE is about. */
18422 encoding = (encoding == DW_ATE_signed_fixed
18423 ? DW_ATE_signed
18424 : DW_ATE_unsigned);
18425 }
18426
18427 /* With GNAT encodings, fixed-point information will be encoded in
18428 the type name. Note that this can also occur with the above
18429 zero-over-zero case, which is why this is a separate "if" rather
18430 than an "else if". */
18431 const char *gnat_encoding_suffix = nullptr;
18432 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18433 && cu->language == language_ada
18434 && name != nullptr)
18435 {
18436 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18437 if (gnat_encoding_suffix != nullptr)
18438 {
18439 gdb_assert (startswith (gnat_encoding_suffix,
18440 GNAT_FIXED_POINT_SUFFIX));
18441 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18442 name, gnat_encoding_suffix - name);
18443 /* Use -1 here so that SUFFIX points at the "_" after the
18444 "XF". */
18445 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18446
18447 encoding = (encoding == DW_ATE_signed
18448 ? DW_ATE_signed_fixed
18449 : DW_ATE_unsigned_fixed);
18450 }
18451 }
18452
18453 switch (encoding)
18454 {
18455 case DW_ATE_address:
18456 /* Turn DW_ATE_address into a void * pointer. */
18457 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18458 type = init_pointer_type (objfile, bits, name, type);
18459 break;
18460 case DW_ATE_boolean:
18461 type = init_boolean_type (objfile, bits, 1, name);
18462 break;
18463 case DW_ATE_complex_float:
18464 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18465 byte_order);
18466 if (type->code () == TYPE_CODE_ERROR)
18467 {
18468 if (name == nullptr)
18469 {
18470 struct obstack *obstack
18471 = &cu->per_objfile->objfile->objfile_obstack;
18472 name = obconcat (obstack, "_Complex ", type->name (),
18473 nullptr);
18474 }
18475 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18476 }
18477 else
18478 type = init_complex_type (name, type);
18479 break;
18480 case DW_ATE_decimal_float:
18481 type = init_decfloat_type (objfile, bits, name);
18482 break;
18483 case DW_ATE_float:
18484 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18485 break;
18486 case DW_ATE_signed:
18487 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18488 break;
18489 case DW_ATE_unsigned:
18490 if (cu->language == language_fortran
18491 && name
18492 && startswith (name, "character("))
18493 type = init_character_type (objfile, bits, 1, name);
18494 else
18495 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18496 break;
18497 case DW_ATE_signed_char:
18498 if (cu->language == language_ada || cu->language == language_m2
18499 || cu->language == language_pascal
18500 || cu->language == language_fortran)
18501 type = init_character_type (objfile, bits, 0, name);
18502 else
18503 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18504 break;
18505 case DW_ATE_unsigned_char:
18506 if (cu->language == language_ada || cu->language == language_m2
18507 || cu->language == language_pascal
18508 || cu->language == language_fortran
18509 || cu->language == language_rust)
18510 type = init_character_type (objfile, bits, 1, name);
18511 else
18512 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18513 break;
18514 case DW_ATE_UTF:
18515 {
18516 if (bits == 16)
18517 type = builtin_type (arch)->builtin_char16;
18518 else if (bits == 32)
18519 type = builtin_type (arch)->builtin_char32;
18520 else
18521 {
18522 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18523 bits);
18524 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18525 }
18526 return set_die_type (die, type, cu);
18527 }
18528 break;
18529 case DW_ATE_signed_fixed:
18530 type = init_fixed_point_type (objfile, bits, 0, name);
18531 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18532 break;
18533 case DW_ATE_unsigned_fixed:
18534 type = init_fixed_point_type (objfile, bits, 1, name);
18535 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18536 break;
18537
18538 default:
18539 complaint (_("unsupported DW_AT_encoding: '%s'"),
18540 dwarf_type_encoding_name (encoding));
18541 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18542 break;
18543 }
18544
18545 if (name && strcmp (name, "char") == 0)
18546 type->set_has_no_signedness (true);
18547
18548 maybe_set_alignment (cu, die, type);
18549
18550 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18551
18552 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18553 {
18554 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18555 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18556 {
18557 unsigned real_bit_size = attr->as_unsigned ();
18558 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18559 /* Only use the attributes if they make sense together. */
18560 if (attr == nullptr
18561 || (attr->as_unsigned () + real_bit_size
18562 <= 8 * TYPE_LENGTH (type)))
18563 {
18564 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18565 = real_bit_size;
18566 if (attr != nullptr)
18567 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18568 = attr->as_unsigned ();
18569 }
18570 }
18571 }
18572
18573 return set_die_type (die, type, cu);
18574 }
18575
18576 /* Parse dwarf attribute if it's a block, reference or constant and put the
18577 resulting value of the attribute into struct bound_prop.
18578 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18579
18580 static int
18581 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18582 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18583 struct type *default_type)
18584 {
18585 struct dwarf2_property_baton *baton;
18586 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18587 struct objfile *objfile = per_objfile->objfile;
18588 struct obstack *obstack = &objfile->objfile_obstack;
18589
18590 gdb_assert (default_type != NULL);
18591
18592 if (attr == NULL || prop == NULL)
18593 return 0;
18594
18595 if (attr->form_is_block ())
18596 {
18597 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18598 baton->property_type = default_type;
18599 baton->locexpr.per_cu = cu->per_cu;
18600 baton->locexpr.per_objfile = per_objfile;
18601
18602 struct dwarf_block *block = attr->as_block ();
18603 baton->locexpr.size = block->size;
18604 baton->locexpr.data = block->data;
18605 switch (attr->name)
18606 {
18607 case DW_AT_string_length:
18608 baton->locexpr.is_reference = true;
18609 break;
18610 default:
18611 baton->locexpr.is_reference = false;
18612 break;
18613 }
18614
18615 prop->set_locexpr (baton);
18616 gdb_assert (prop->baton () != NULL);
18617 }
18618 else if (attr->form_is_ref ())
18619 {
18620 struct dwarf2_cu *target_cu = cu;
18621 struct die_info *target_die;
18622 struct attribute *target_attr;
18623
18624 target_die = follow_die_ref (die, attr, &target_cu);
18625 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18626 if (target_attr == NULL)
18627 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18628 target_cu);
18629 if (target_attr == NULL)
18630 return 0;
18631
18632 switch (target_attr->name)
18633 {
18634 case DW_AT_location:
18635 if (target_attr->form_is_section_offset ())
18636 {
18637 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18638 baton->property_type = die_type (target_die, target_cu);
18639 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18640 prop->set_loclist (baton);
18641 gdb_assert (prop->baton () != NULL);
18642 }
18643 else if (target_attr->form_is_block ())
18644 {
18645 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18646 baton->property_type = die_type (target_die, target_cu);
18647 baton->locexpr.per_cu = cu->per_cu;
18648 baton->locexpr.per_objfile = per_objfile;
18649 struct dwarf_block *block = target_attr->as_block ();
18650 baton->locexpr.size = block->size;
18651 baton->locexpr.data = block->data;
18652 baton->locexpr.is_reference = true;
18653 prop->set_locexpr (baton);
18654 gdb_assert (prop->baton () != NULL);
18655 }
18656 else
18657 {
18658 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18659 "dynamic property");
18660 return 0;
18661 }
18662 break;
18663 case DW_AT_data_member_location:
18664 {
18665 LONGEST offset;
18666
18667 if (!handle_data_member_location (target_die, target_cu,
18668 &offset))
18669 return 0;
18670
18671 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18672 baton->property_type = read_type_die (target_die->parent,
18673 target_cu);
18674 baton->offset_info.offset = offset;
18675 baton->offset_info.type = die_type (target_die, target_cu);
18676 prop->set_addr_offset (baton);
18677 break;
18678 }
18679 }
18680 }
18681 else if (attr->form_is_constant ())
18682 prop->set_const_val (attr->constant_value (0));
18683 else
18684 {
18685 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18686 dwarf2_name (die, cu));
18687 return 0;
18688 }
18689
18690 return 1;
18691 }
18692
18693 /* See read.h. */
18694
18695 struct type *
18696 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18697 {
18698 struct type *int_type;
18699
18700 /* Helper macro to examine the various builtin types. */
18701 #define TRY_TYPE(F) \
18702 int_type = (unsigned_p \
18703 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18704 : objfile_type (objfile)->builtin_ ## F); \
18705 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18706 return int_type
18707
18708 TRY_TYPE (char);
18709 TRY_TYPE (short);
18710 TRY_TYPE (int);
18711 TRY_TYPE (long);
18712 TRY_TYPE (long_long);
18713
18714 #undef TRY_TYPE
18715
18716 gdb_assert_not_reached ("unable to find suitable integer type");
18717 }
18718
18719 /* See read.h. */
18720
18721 struct type *
18722 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18723 {
18724 int addr_size = this->per_cu->addr_size ();
18725 return this->per_objfile->int_type (addr_size, unsigned_p);
18726 }
18727
18728 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18729 present (which is valid) then compute the default type based on the
18730 compilation units address size. */
18731
18732 static struct type *
18733 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18734 {
18735 struct type *index_type = die_type (die, cu);
18736
18737 /* Dwarf-2 specifications explicitly allows to create subrange types
18738 without specifying a base type.
18739 In that case, the base type must be set to the type of
18740 the lower bound, upper bound or count, in that order, if any of these
18741 three attributes references an object that has a type.
18742 If no base type is found, the Dwarf-2 specifications say that
18743 a signed integer type of size equal to the size of an address should
18744 be used.
18745 For the following C code: `extern char gdb_int [];'
18746 GCC produces an empty range DIE.
18747 FIXME: muller/2010-05-28: Possible references to object for low bound,
18748 high bound or count are not yet handled by this code. */
18749 if (index_type->code () == TYPE_CODE_VOID)
18750 index_type = cu->addr_sized_int_type (false);
18751
18752 return index_type;
18753 }
18754
18755 /* Read the given DW_AT_subrange DIE. */
18756
18757 static struct type *
18758 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18759 {
18760 struct type *base_type, *orig_base_type;
18761 struct type *range_type;
18762 struct attribute *attr;
18763 struct dynamic_prop low, high;
18764 int low_default_is_valid;
18765 int high_bound_is_count = 0;
18766 const char *name;
18767 ULONGEST negative_mask;
18768
18769 orig_base_type = read_subrange_index_type (die, cu);
18770
18771 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18772 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18773 creating the range type, but we use the result of check_typedef
18774 when examining properties of the type. */
18775 base_type = check_typedef (orig_base_type);
18776
18777 /* The die_type call above may have already set the type for this DIE. */
18778 range_type = get_die_type (die, cu);
18779 if (range_type)
18780 return range_type;
18781
18782 high.set_const_val (0);
18783
18784 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18785 omitting DW_AT_lower_bound. */
18786 switch (cu->language)
18787 {
18788 case language_c:
18789 case language_cplus:
18790 low.set_const_val (0);
18791 low_default_is_valid = 1;
18792 break;
18793 case language_fortran:
18794 low.set_const_val (1);
18795 low_default_is_valid = 1;
18796 break;
18797 case language_d:
18798 case language_objc:
18799 case language_rust:
18800 low.set_const_val (0);
18801 low_default_is_valid = (cu->header.version >= 4);
18802 break;
18803 case language_ada:
18804 case language_m2:
18805 case language_pascal:
18806 low.set_const_val (1);
18807 low_default_is_valid = (cu->header.version >= 4);
18808 break;
18809 default:
18810 low.set_const_val (0);
18811 low_default_is_valid = 0;
18812 break;
18813 }
18814
18815 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18816 if (attr != nullptr)
18817 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18818 else if (!low_default_is_valid)
18819 complaint (_("Missing DW_AT_lower_bound "
18820 "- DIE at %s [in module %s]"),
18821 sect_offset_str (die->sect_off),
18822 objfile_name (cu->per_objfile->objfile));
18823
18824 struct attribute *attr_ub, *attr_count;
18825 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18826 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18827 {
18828 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18829 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18830 {
18831 /* If bounds are constant do the final calculation here. */
18832 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18833 high.set_const_val (low.const_val () + high.const_val () - 1);
18834 else
18835 high_bound_is_count = 1;
18836 }
18837 else
18838 {
18839 if (attr_ub != NULL)
18840 complaint (_("Unresolved DW_AT_upper_bound "
18841 "- DIE at %s [in module %s]"),
18842 sect_offset_str (die->sect_off),
18843 objfile_name (cu->per_objfile->objfile));
18844 if (attr_count != NULL)
18845 complaint (_("Unresolved DW_AT_count "
18846 "- DIE at %s [in module %s]"),
18847 sect_offset_str (die->sect_off),
18848 objfile_name (cu->per_objfile->objfile));
18849 }
18850 }
18851
18852 LONGEST bias = 0;
18853 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18854 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18855 bias = bias_attr->constant_value (0);
18856
18857 /* Normally, the DWARF producers are expected to use a signed
18858 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18859 But this is unfortunately not always the case, as witnessed
18860 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18861 is used instead. To work around that ambiguity, we treat
18862 the bounds as signed, and thus sign-extend their values, when
18863 the base type is signed. */
18864 negative_mask =
18865 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18866 if (low.kind () == PROP_CONST
18867 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18868 low.set_const_val (low.const_val () | negative_mask);
18869 if (high.kind () == PROP_CONST
18870 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18871 high.set_const_val (high.const_val () | negative_mask);
18872
18873 /* Check for bit and byte strides. */
18874 struct dynamic_prop byte_stride_prop;
18875 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18876 if (attr_byte_stride != nullptr)
18877 {
18878 struct type *prop_type = cu->addr_sized_int_type (false);
18879 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18880 prop_type);
18881 }
18882
18883 struct dynamic_prop bit_stride_prop;
18884 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18885 if (attr_bit_stride != nullptr)
18886 {
18887 /* It only makes sense to have either a bit or byte stride. */
18888 if (attr_byte_stride != nullptr)
18889 {
18890 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18891 "- DIE at %s [in module %s]"),
18892 sect_offset_str (die->sect_off),
18893 objfile_name (cu->per_objfile->objfile));
18894 attr_bit_stride = nullptr;
18895 }
18896 else
18897 {
18898 struct type *prop_type = cu->addr_sized_int_type (false);
18899 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18900 prop_type);
18901 }
18902 }
18903
18904 if (attr_byte_stride != nullptr
18905 || attr_bit_stride != nullptr)
18906 {
18907 bool byte_stride_p = (attr_byte_stride != nullptr);
18908 struct dynamic_prop *stride
18909 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18910
18911 range_type
18912 = create_range_type_with_stride (NULL, orig_base_type, &low,
18913 &high, bias, stride, byte_stride_p);
18914 }
18915 else
18916 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18917
18918 if (high_bound_is_count)
18919 range_type->bounds ()->flag_upper_bound_is_count = 1;
18920
18921 /* Ada expects an empty array on no boundary attributes. */
18922 if (attr == NULL && cu->language != language_ada)
18923 range_type->bounds ()->high.set_undefined ();
18924
18925 name = dwarf2_name (die, cu);
18926 if (name)
18927 range_type->set_name (name);
18928
18929 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18930 if (attr != nullptr)
18931 TYPE_LENGTH (range_type) = attr->constant_value (0);
18932
18933 maybe_set_alignment (cu, die, range_type);
18934
18935 set_die_type (die, range_type, cu);
18936
18937 /* set_die_type should be already done. */
18938 set_descriptive_type (range_type, die, cu);
18939
18940 return range_type;
18941 }
18942
18943 static struct type *
18944 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18945 {
18946 struct type *type;
18947
18948 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18949 type->set_name (dwarf2_name (die, cu));
18950
18951 /* In Ada, an unspecified type is typically used when the description
18952 of the type is deferred to a different unit. When encountering
18953 such a type, we treat it as a stub, and try to resolve it later on,
18954 when needed. */
18955 if (cu->language == language_ada)
18956 type->set_is_stub (true);
18957
18958 return set_die_type (die, type, cu);
18959 }
18960
18961 /* Read a single die and all its descendents. Set the die's sibling
18962 field to NULL; set other fields in the die correctly, and set all
18963 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18964 location of the info_ptr after reading all of those dies. PARENT
18965 is the parent of the die in question. */
18966
18967 static struct die_info *
18968 read_die_and_children (const struct die_reader_specs *reader,
18969 const gdb_byte *info_ptr,
18970 const gdb_byte **new_info_ptr,
18971 struct die_info *parent)
18972 {
18973 struct die_info *die;
18974 const gdb_byte *cur_ptr;
18975
18976 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18977 if (die == NULL)
18978 {
18979 *new_info_ptr = cur_ptr;
18980 return NULL;
18981 }
18982 store_in_ref_table (die, reader->cu);
18983
18984 if (die->has_children)
18985 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18986 else
18987 {
18988 die->child = NULL;
18989 *new_info_ptr = cur_ptr;
18990 }
18991
18992 die->sibling = NULL;
18993 die->parent = parent;
18994 return die;
18995 }
18996
18997 /* Read a die, all of its descendents, and all of its siblings; set
18998 all of the fields of all of the dies correctly. Arguments are as
18999 in read_die_and_children. */
19000
19001 static struct die_info *
19002 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19003 const gdb_byte *info_ptr,
19004 const gdb_byte **new_info_ptr,
19005 struct die_info *parent)
19006 {
19007 struct die_info *first_die, *last_sibling;
19008 const gdb_byte *cur_ptr;
19009
19010 cur_ptr = info_ptr;
19011 first_die = last_sibling = NULL;
19012
19013 while (1)
19014 {
19015 struct die_info *die
19016 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19017
19018 if (die == NULL)
19019 {
19020 *new_info_ptr = cur_ptr;
19021 return first_die;
19022 }
19023
19024 if (!first_die)
19025 first_die = die;
19026 else
19027 last_sibling->sibling = die;
19028
19029 last_sibling = die;
19030 }
19031 }
19032
19033 /* Read a die, all of its descendents, and all of its siblings; set
19034 all of the fields of all of the dies correctly. Arguments are as
19035 in read_die_and_children.
19036 This the main entry point for reading a DIE and all its children. */
19037
19038 static struct die_info *
19039 read_die_and_siblings (const struct die_reader_specs *reader,
19040 const gdb_byte *info_ptr,
19041 const gdb_byte **new_info_ptr,
19042 struct die_info *parent)
19043 {
19044 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19045 new_info_ptr, parent);
19046
19047 if (dwarf_die_debug)
19048 {
19049 fprintf_unfiltered (gdb_stdlog,
19050 "Read die from %s@0x%x of %s:\n",
19051 reader->die_section->get_name (),
19052 (unsigned) (info_ptr - reader->die_section->buffer),
19053 bfd_get_filename (reader->abfd));
19054 dump_die (die, dwarf_die_debug);
19055 }
19056
19057 return die;
19058 }
19059
19060 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19061 attributes.
19062 The caller is responsible for filling in the extra attributes
19063 and updating (*DIEP)->num_attrs.
19064 Set DIEP to point to a newly allocated die with its information,
19065 except for its child, sibling, and parent fields. */
19066
19067 static const gdb_byte *
19068 read_full_die_1 (const struct die_reader_specs *reader,
19069 struct die_info **diep, const gdb_byte *info_ptr,
19070 int num_extra_attrs)
19071 {
19072 unsigned int abbrev_number, bytes_read, i;
19073 const struct abbrev_info *abbrev;
19074 struct die_info *die;
19075 struct dwarf2_cu *cu = reader->cu;
19076 bfd *abfd = reader->abfd;
19077
19078 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19079 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19080 info_ptr += bytes_read;
19081 if (!abbrev_number)
19082 {
19083 *diep = NULL;
19084 return info_ptr;
19085 }
19086
19087 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19088 if (!abbrev)
19089 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19090 abbrev_number,
19091 bfd_get_filename (abfd));
19092
19093 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19094 die->sect_off = sect_off;
19095 die->tag = abbrev->tag;
19096 die->abbrev = abbrev_number;
19097 die->has_children = abbrev->has_children;
19098
19099 /* Make the result usable.
19100 The caller needs to update num_attrs after adding the extra
19101 attributes. */
19102 die->num_attrs = abbrev->num_attrs;
19103
19104 bool any_need_reprocess = false;
19105 for (i = 0; i < abbrev->num_attrs; ++i)
19106 {
19107 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19108 info_ptr);
19109 if (die->attrs[i].requires_reprocessing_p ())
19110 any_need_reprocess = true;
19111 }
19112
19113 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19114 if (attr != nullptr && attr->form_is_unsigned ())
19115 cu->str_offsets_base = attr->as_unsigned ();
19116
19117 attr = die->attr (DW_AT_loclists_base);
19118 if (attr != nullptr)
19119 cu->loclist_base = attr->as_unsigned ();
19120
19121 auto maybe_addr_base = die->addr_base ();
19122 if (maybe_addr_base.has_value ())
19123 cu->addr_base = *maybe_addr_base;
19124
19125 attr = die->attr (DW_AT_rnglists_base);
19126 if (attr != nullptr)
19127 cu->rnglists_base = attr->as_unsigned ();
19128
19129 if (any_need_reprocess)
19130 {
19131 for (i = 0; i < abbrev->num_attrs; ++i)
19132 {
19133 if (die->attrs[i].requires_reprocessing_p ())
19134 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19135 }
19136 }
19137 *diep = die;
19138 return info_ptr;
19139 }
19140
19141 /* Read a die and all its attributes.
19142 Set DIEP to point to a newly allocated die with its information,
19143 except for its child, sibling, and parent fields. */
19144
19145 static const gdb_byte *
19146 read_full_die (const struct die_reader_specs *reader,
19147 struct die_info **diep, const gdb_byte *info_ptr)
19148 {
19149 const gdb_byte *result;
19150
19151 result = read_full_die_1 (reader, diep, info_ptr, 0);
19152
19153 if (dwarf_die_debug)
19154 {
19155 fprintf_unfiltered (gdb_stdlog,
19156 "Read die from %s@0x%x of %s:\n",
19157 reader->die_section->get_name (),
19158 (unsigned) (info_ptr - reader->die_section->buffer),
19159 bfd_get_filename (reader->abfd));
19160 dump_die (*diep, dwarf_die_debug);
19161 }
19162
19163 return result;
19164 }
19165 \f
19166
19167 /* Returns nonzero if TAG represents a type that we might generate a partial
19168 symbol for. */
19169
19170 static int
19171 is_type_tag_for_partial (int tag, enum language lang)
19172 {
19173 switch (tag)
19174 {
19175 #if 0
19176 /* Some types that would be reasonable to generate partial symbols for,
19177 that we don't at present. Note that normally this does not
19178 matter, mainly because C compilers don't give names to these
19179 types, but instead emit DW_TAG_typedef. */
19180 case DW_TAG_file_type:
19181 case DW_TAG_ptr_to_member_type:
19182 case DW_TAG_set_type:
19183 case DW_TAG_string_type:
19184 case DW_TAG_subroutine_type:
19185 #endif
19186
19187 /* GNAT may emit an array with a name, but no typedef, so we
19188 need to make a symbol in this case. */
19189 case DW_TAG_array_type:
19190 return lang == language_ada;
19191
19192 case DW_TAG_base_type:
19193 case DW_TAG_class_type:
19194 case DW_TAG_interface_type:
19195 case DW_TAG_enumeration_type:
19196 case DW_TAG_structure_type:
19197 case DW_TAG_subrange_type:
19198 case DW_TAG_typedef:
19199 case DW_TAG_union_type:
19200 return 1;
19201 default:
19202 return 0;
19203 }
19204 }
19205
19206 /* Load all DIEs that are interesting for partial symbols into memory. */
19207
19208 static struct partial_die_info *
19209 load_partial_dies (const struct die_reader_specs *reader,
19210 const gdb_byte *info_ptr, int building_psymtab)
19211 {
19212 struct dwarf2_cu *cu = reader->cu;
19213 struct objfile *objfile = cu->per_objfile->objfile;
19214 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19215 unsigned int bytes_read;
19216 unsigned int load_all = 0;
19217 int nesting_level = 1;
19218
19219 parent_die = NULL;
19220 last_die = NULL;
19221
19222 gdb_assert (cu->per_cu != NULL);
19223 if (cu->per_cu->load_all_dies)
19224 load_all = 1;
19225
19226 cu->partial_dies
19227 = htab_create_alloc_ex (cu->header.length / 12,
19228 partial_die_hash,
19229 partial_die_eq,
19230 NULL,
19231 &cu->comp_unit_obstack,
19232 hashtab_obstack_allocate,
19233 dummy_obstack_deallocate);
19234
19235 while (1)
19236 {
19237 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19238 &bytes_read);
19239
19240 /* A NULL abbrev means the end of a series of children. */
19241 if (abbrev == NULL)
19242 {
19243 if (--nesting_level == 0)
19244 return first_die;
19245
19246 info_ptr += bytes_read;
19247 last_die = parent_die;
19248 parent_die = parent_die->die_parent;
19249 continue;
19250 }
19251
19252 /* Check for template arguments. We never save these; if
19253 they're seen, we just mark the parent, and go on our way. */
19254 if (parent_die != NULL
19255 && cu->language == language_cplus
19256 && (abbrev->tag == DW_TAG_template_type_param
19257 || abbrev->tag == DW_TAG_template_value_param))
19258 {
19259 parent_die->has_template_arguments = 1;
19260
19261 if (!load_all)
19262 {
19263 /* We don't need a partial DIE for the template argument. */
19264 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19265 continue;
19266 }
19267 }
19268
19269 /* We only recurse into c++ subprograms looking for template arguments.
19270 Skip their other children. */
19271 if (!load_all
19272 && cu->language == language_cplus
19273 && parent_die != NULL
19274 && parent_die->tag == DW_TAG_subprogram
19275 && abbrev->tag != DW_TAG_inlined_subroutine)
19276 {
19277 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19278 continue;
19279 }
19280
19281 /* Check whether this DIE is interesting enough to save. Normally
19282 we would not be interested in members here, but there may be
19283 later variables referencing them via DW_AT_specification (for
19284 static members). */
19285 if (!load_all
19286 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19287 && abbrev->tag != DW_TAG_constant
19288 && abbrev->tag != DW_TAG_enumerator
19289 && abbrev->tag != DW_TAG_subprogram
19290 && abbrev->tag != DW_TAG_inlined_subroutine
19291 && abbrev->tag != DW_TAG_lexical_block
19292 && abbrev->tag != DW_TAG_variable
19293 && abbrev->tag != DW_TAG_namespace
19294 && abbrev->tag != DW_TAG_module
19295 && abbrev->tag != DW_TAG_member
19296 && abbrev->tag != DW_TAG_imported_unit
19297 && abbrev->tag != DW_TAG_imported_declaration)
19298 {
19299 /* Otherwise we skip to the next sibling, if any. */
19300 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19301 continue;
19302 }
19303
19304 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19305 abbrev);
19306
19307 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19308
19309 /* This two-pass algorithm for processing partial symbols has a
19310 high cost in cache pressure. Thus, handle some simple cases
19311 here which cover the majority of C partial symbols. DIEs
19312 which neither have specification tags in them, nor could have
19313 specification tags elsewhere pointing at them, can simply be
19314 processed and discarded.
19315
19316 This segment is also optional; scan_partial_symbols and
19317 add_partial_symbol will handle these DIEs if we chain
19318 them in normally. When compilers which do not emit large
19319 quantities of duplicate debug information are more common,
19320 this code can probably be removed. */
19321
19322 /* Any complete simple types at the top level (pretty much all
19323 of them, for a language without namespaces), can be processed
19324 directly. */
19325 if (parent_die == NULL
19326 && pdi.has_specification == 0
19327 && pdi.is_declaration == 0
19328 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19329 || pdi.tag == DW_TAG_base_type
19330 || pdi.tag == DW_TAG_array_type
19331 || pdi.tag == DW_TAG_subrange_type))
19332 {
19333 if (building_psymtab && pdi.raw_name != NULL)
19334 add_partial_symbol (&pdi, cu);
19335
19336 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19337 continue;
19338 }
19339
19340 /* The exception for DW_TAG_typedef with has_children above is
19341 a workaround of GCC PR debug/47510. In the case of this complaint
19342 type_name_or_error will error on such types later.
19343
19344 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19345 it could not find the child DIEs referenced later, this is checked
19346 above. In correct DWARF DW_TAG_typedef should have no children. */
19347
19348 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19349 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19350 "- DIE at %s [in module %s]"),
19351 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19352
19353 /* If we're at the second level, and we're an enumerator, and
19354 our parent has no specification (meaning possibly lives in a
19355 namespace elsewhere), then we can add the partial symbol now
19356 instead of queueing it. */
19357 if (pdi.tag == DW_TAG_enumerator
19358 && parent_die != NULL
19359 && parent_die->die_parent == NULL
19360 && parent_die->tag == DW_TAG_enumeration_type
19361 && parent_die->has_specification == 0)
19362 {
19363 if (pdi.raw_name == NULL)
19364 complaint (_("malformed enumerator DIE ignored"));
19365 else if (building_psymtab)
19366 add_partial_symbol (&pdi, cu);
19367
19368 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19369 continue;
19370 }
19371
19372 struct partial_die_info *part_die
19373 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19374
19375 /* We'll save this DIE so link it in. */
19376 part_die->die_parent = parent_die;
19377 part_die->die_sibling = NULL;
19378 part_die->die_child = NULL;
19379
19380 if (last_die && last_die == parent_die)
19381 last_die->die_child = part_die;
19382 else if (last_die)
19383 last_die->die_sibling = part_die;
19384
19385 last_die = part_die;
19386
19387 if (first_die == NULL)
19388 first_die = part_die;
19389
19390 /* Maybe add the DIE to the hash table. Not all DIEs that we
19391 find interesting need to be in the hash table, because we
19392 also have the parent/sibling/child chains; only those that we
19393 might refer to by offset later during partial symbol reading.
19394
19395 For now this means things that might have be the target of a
19396 DW_AT_specification, DW_AT_abstract_origin, or
19397 DW_AT_extension. DW_AT_extension will refer only to
19398 namespaces; DW_AT_abstract_origin refers to functions (and
19399 many things under the function DIE, but we do not recurse
19400 into function DIEs during partial symbol reading) and
19401 possibly variables as well; DW_AT_specification refers to
19402 declarations. Declarations ought to have the DW_AT_declaration
19403 flag. It happens that GCC forgets to put it in sometimes, but
19404 only for functions, not for types.
19405
19406 Adding more things than necessary to the hash table is harmless
19407 except for the performance cost. Adding too few will result in
19408 wasted time in find_partial_die, when we reread the compilation
19409 unit with load_all_dies set. */
19410
19411 if (load_all
19412 || abbrev->tag == DW_TAG_constant
19413 || abbrev->tag == DW_TAG_subprogram
19414 || abbrev->tag == DW_TAG_variable
19415 || abbrev->tag == DW_TAG_namespace
19416 || part_die->is_declaration)
19417 {
19418 void **slot;
19419
19420 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19421 to_underlying (part_die->sect_off),
19422 INSERT);
19423 *slot = part_die;
19424 }
19425
19426 /* For some DIEs we want to follow their children (if any). For C
19427 we have no reason to follow the children of structures; for other
19428 languages we have to, so that we can get at method physnames
19429 to infer fully qualified class names, for DW_AT_specification,
19430 and for C++ template arguments. For C++, we also look one level
19431 inside functions to find template arguments (if the name of the
19432 function does not already contain the template arguments).
19433
19434 For Ada and Fortran, we need to scan the children of subprograms
19435 and lexical blocks as well because these languages allow the
19436 definition of nested entities that could be interesting for the
19437 debugger, such as nested subprograms for instance. */
19438 if (last_die->has_children
19439 && (load_all
19440 || last_die->tag == DW_TAG_namespace
19441 || last_die->tag == DW_TAG_module
19442 || last_die->tag == DW_TAG_enumeration_type
19443 || (cu->language == language_cplus
19444 && last_die->tag == DW_TAG_subprogram
19445 && (last_die->raw_name == NULL
19446 || strchr (last_die->raw_name, '<') == NULL))
19447 || (cu->language != language_c
19448 && (last_die->tag == DW_TAG_class_type
19449 || last_die->tag == DW_TAG_interface_type
19450 || last_die->tag == DW_TAG_structure_type
19451 || last_die->tag == DW_TAG_union_type))
19452 || ((cu->language == language_ada
19453 || cu->language == language_fortran)
19454 && (last_die->tag == DW_TAG_subprogram
19455 || last_die->tag == DW_TAG_lexical_block))))
19456 {
19457 nesting_level++;
19458 parent_die = last_die;
19459 continue;
19460 }
19461
19462 /* Otherwise we skip to the next sibling, if any. */
19463 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19464
19465 /* Back to the top, do it again. */
19466 }
19467 }
19468
19469 partial_die_info::partial_die_info (sect_offset sect_off_,
19470 const struct abbrev_info *abbrev)
19471 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19472 {
19473 }
19474
19475 /* See class definition. */
19476
19477 const char *
19478 partial_die_info::name (dwarf2_cu *cu)
19479 {
19480 if (!canonical_name && raw_name != nullptr)
19481 {
19482 struct objfile *objfile = cu->per_objfile->objfile;
19483 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19484 canonical_name = 1;
19485 }
19486
19487 return raw_name;
19488 }
19489
19490 /* Read a minimal amount of information into the minimal die structure.
19491 INFO_PTR should point just after the initial uleb128 of a DIE. */
19492
19493 const gdb_byte *
19494 partial_die_info::read (const struct die_reader_specs *reader,
19495 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19496 {
19497 struct dwarf2_cu *cu = reader->cu;
19498 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19499 unsigned int i;
19500 int has_low_pc_attr = 0;
19501 int has_high_pc_attr = 0;
19502 int high_pc_relative = 0;
19503
19504 for (i = 0; i < abbrev.num_attrs; ++i)
19505 {
19506 attribute attr;
19507 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19508 /* String and address offsets that need to do the reprocessing have
19509 already been read at this point, so there is no need to wait until
19510 the loop terminates to do the reprocessing. */
19511 if (attr.requires_reprocessing_p ())
19512 read_attribute_reprocess (reader, &attr, tag);
19513 /* Store the data if it is of an attribute we want to keep in a
19514 partial symbol table. */
19515 switch (attr.name)
19516 {
19517 case DW_AT_name:
19518 switch (tag)
19519 {
19520 case DW_TAG_compile_unit:
19521 case DW_TAG_partial_unit:
19522 case DW_TAG_type_unit:
19523 /* Compilation units have a DW_AT_name that is a filename, not
19524 a source language identifier. */
19525 case DW_TAG_enumeration_type:
19526 case DW_TAG_enumerator:
19527 /* These tags always have simple identifiers already; no need
19528 to canonicalize them. */
19529 canonical_name = 1;
19530 raw_name = attr.as_string ();
19531 break;
19532 default:
19533 canonical_name = 0;
19534 raw_name = attr.as_string ();
19535 break;
19536 }
19537 break;
19538 case DW_AT_linkage_name:
19539 case DW_AT_MIPS_linkage_name:
19540 /* Note that both forms of linkage name might appear. We
19541 assume they will be the same, and we only store the last
19542 one we see. */
19543 linkage_name = attr.as_string ();
19544 break;
19545 case DW_AT_low_pc:
19546 has_low_pc_attr = 1;
19547 lowpc = attr.as_address ();
19548 break;
19549 case DW_AT_high_pc:
19550 has_high_pc_attr = 1;
19551 highpc = attr.as_address ();
19552 if (cu->header.version >= 4 && attr.form_is_constant ())
19553 high_pc_relative = 1;
19554 break;
19555 case DW_AT_location:
19556 /* Support the .debug_loc offsets. */
19557 if (attr.form_is_block ())
19558 {
19559 d.locdesc = attr.as_block ();
19560 }
19561 else if (attr.form_is_section_offset ())
19562 {
19563 dwarf2_complex_location_expr_complaint ();
19564 }
19565 else
19566 {
19567 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19568 "partial symbol information");
19569 }
19570 break;
19571 case DW_AT_external:
19572 is_external = attr.as_boolean ();
19573 break;
19574 case DW_AT_declaration:
19575 is_declaration = attr.as_boolean ();
19576 break;
19577 case DW_AT_type:
19578 has_type = 1;
19579 break;
19580 case DW_AT_abstract_origin:
19581 case DW_AT_specification:
19582 case DW_AT_extension:
19583 has_specification = 1;
19584 spec_offset = attr.get_ref_die_offset ();
19585 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19586 || cu->per_cu->is_dwz);
19587 break;
19588 case DW_AT_sibling:
19589 /* Ignore absolute siblings, they might point outside of
19590 the current compile unit. */
19591 if (attr.form == DW_FORM_ref_addr)
19592 complaint (_("ignoring absolute DW_AT_sibling"));
19593 else
19594 {
19595 const gdb_byte *buffer = reader->buffer;
19596 sect_offset off = attr.get_ref_die_offset ();
19597 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19598
19599 if (sibling_ptr < info_ptr)
19600 complaint (_("DW_AT_sibling points backwards"));
19601 else if (sibling_ptr > reader->buffer_end)
19602 reader->die_section->overflow_complaint ();
19603 else
19604 sibling = sibling_ptr;
19605 }
19606 break;
19607 case DW_AT_byte_size:
19608 has_byte_size = 1;
19609 break;
19610 case DW_AT_const_value:
19611 has_const_value = 1;
19612 break;
19613 case DW_AT_calling_convention:
19614 /* DWARF doesn't provide a way to identify a program's source-level
19615 entry point. DW_AT_calling_convention attributes are only meant
19616 to describe functions' calling conventions.
19617
19618 However, because it's a necessary piece of information in
19619 Fortran, and before DWARF 4 DW_CC_program was the only
19620 piece of debugging information whose definition refers to
19621 a 'main program' at all, several compilers marked Fortran
19622 main programs with DW_CC_program --- even when those
19623 functions use the standard calling conventions.
19624
19625 Although DWARF now specifies a way to provide this
19626 information, we support this practice for backward
19627 compatibility. */
19628 if (attr.constant_value (0) == DW_CC_program
19629 && cu->language == language_fortran)
19630 main_subprogram = 1;
19631 break;
19632 case DW_AT_inline:
19633 {
19634 LONGEST value = attr.constant_value (-1);
19635 if (value == DW_INL_inlined
19636 || value == DW_INL_declared_inlined)
19637 may_be_inlined = 1;
19638 }
19639 break;
19640
19641 case DW_AT_import:
19642 if (tag == DW_TAG_imported_unit)
19643 {
19644 d.sect_off = attr.get_ref_die_offset ();
19645 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19646 || cu->per_cu->is_dwz);
19647 }
19648 break;
19649
19650 case DW_AT_main_subprogram:
19651 main_subprogram = attr.as_boolean ();
19652 break;
19653
19654 case DW_AT_ranges:
19655 {
19656 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19657 on DWARF version). */
19658 ULONGEST ranges_offset = attr.as_unsigned ();
19659
19660 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19661 this value. */
19662 if (tag != DW_TAG_compile_unit)
19663 ranges_offset += cu->gnu_ranges_base;
19664
19665 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19666 nullptr, tag))
19667 has_pc_info = 1;
19668 }
19669 break;
19670
19671 default:
19672 break;
19673 }
19674 }
19675
19676 /* For Ada, if both the name and the linkage name appear, we prefer
19677 the latter. This lets "catch exception" work better, regardless
19678 of the order in which the name and linkage name were emitted.
19679 Really, though, this is just a workaround for the fact that gdb
19680 doesn't store both the name and the linkage name. */
19681 if (cu->language == language_ada && linkage_name != nullptr)
19682 raw_name = linkage_name;
19683
19684 if (high_pc_relative)
19685 highpc += lowpc;
19686
19687 if (has_low_pc_attr && has_high_pc_attr)
19688 {
19689 /* When using the GNU linker, .gnu.linkonce. sections are used to
19690 eliminate duplicate copies of functions and vtables and such.
19691 The linker will arbitrarily choose one and discard the others.
19692 The AT_*_pc values for such functions refer to local labels in
19693 these sections. If the section from that file was discarded, the
19694 labels are not in the output, so the relocs get a value of 0.
19695 If this is a discarded function, mark the pc bounds as invalid,
19696 so that GDB will ignore it. */
19697 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19698 {
19699 struct objfile *objfile = per_objfile->objfile;
19700 struct gdbarch *gdbarch = objfile->arch ();
19701
19702 complaint (_("DW_AT_low_pc %s is zero "
19703 "for DIE at %s [in module %s]"),
19704 paddress (gdbarch, lowpc),
19705 sect_offset_str (sect_off),
19706 objfile_name (objfile));
19707 }
19708 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19709 else if (lowpc >= highpc)
19710 {
19711 struct objfile *objfile = per_objfile->objfile;
19712 struct gdbarch *gdbarch = objfile->arch ();
19713
19714 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19715 "for DIE at %s [in module %s]"),
19716 paddress (gdbarch, lowpc),
19717 paddress (gdbarch, highpc),
19718 sect_offset_str (sect_off),
19719 objfile_name (objfile));
19720 }
19721 else
19722 has_pc_info = 1;
19723 }
19724
19725 return info_ptr;
19726 }
19727
19728 /* Find a cached partial DIE at OFFSET in CU. */
19729
19730 struct partial_die_info *
19731 dwarf2_cu::find_partial_die (sect_offset sect_off)
19732 {
19733 struct partial_die_info *lookup_die = NULL;
19734 struct partial_die_info part_die (sect_off);
19735
19736 lookup_die = ((struct partial_die_info *)
19737 htab_find_with_hash (partial_dies, &part_die,
19738 to_underlying (sect_off)));
19739
19740 return lookup_die;
19741 }
19742
19743 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19744 except in the case of .debug_types DIEs which do not reference
19745 outside their CU (they do however referencing other types via
19746 DW_FORM_ref_sig8). */
19747
19748 static const struct cu_partial_die_info
19749 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19750 {
19751 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19752 struct objfile *objfile = per_objfile->objfile;
19753 struct partial_die_info *pd = NULL;
19754
19755 if (offset_in_dwz == cu->per_cu->is_dwz
19756 && cu->header.offset_in_cu_p (sect_off))
19757 {
19758 pd = cu->find_partial_die (sect_off);
19759 if (pd != NULL)
19760 return { cu, pd };
19761 /* We missed recording what we needed.
19762 Load all dies and try again. */
19763 }
19764 else
19765 {
19766 /* TUs don't reference other CUs/TUs (except via type signatures). */
19767 if (cu->per_cu->is_debug_types)
19768 {
19769 error (_("Dwarf Error: Type Unit at offset %s contains"
19770 " external reference to offset %s [in module %s].\n"),
19771 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19772 bfd_get_filename (objfile->obfd));
19773 }
19774 dwarf2_per_cu_data *per_cu
19775 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19776 per_objfile);
19777
19778 cu = per_objfile->get_cu (per_cu);
19779 if (cu == NULL || cu->partial_dies == NULL)
19780 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19781
19782 cu = per_objfile->get_cu (per_cu);
19783
19784 cu->last_used = 0;
19785 pd = cu->find_partial_die (sect_off);
19786 }
19787
19788 /* If we didn't find it, and not all dies have been loaded,
19789 load them all and try again. */
19790
19791 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19792 {
19793 cu->per_cu->load_all_dies = 1;
19794
19795 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19796 THIS_CU->cu may already be in use. So we can't just free it and
19797 replace its DIEs with the ones we read in. Instead, we leave those
19798 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19799 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19800 set. */
19801 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19802
19803 pd = cu->find_partial_die (sect_off);
19804 }
19805
19806 if (pd == NULL)
19807 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19808 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19809 return { cu, pd };
19810 }
19811
19812 /* See if we can figure out if the class lives in a namespace. We do
19813 this by looking for a member function; its demangled name will
19814 contain namespace info, if there is any. */
19815
19816 static void
19817 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19818 struct dwarf2_cu *cu)
19819 {
19820 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19821 what template types look like, because the demangler
19822 frequently doesn't give the same name as the debug info. We
19823 could fix this by only using the demangled name to get the
19824 prefix (but see comment in read_structure_type). */
19825
19826 struct partial_die_info *real_pdi;
19827 struct partial_die_info *child_pdi;
19828
19829 /* If this DIE (this DIE's specification, if any) has a parent, then
19830 we should not do this. We'll prepend the parent's fully qualified
19831 name when we create the partial symbol. */
19832
19833 real_pdi = struct_pdi;
19834 while (real_pdi->has_specification)
19835 {
19836 auto res = find_partial_die (real_pdi->spec_offset,
19837 real_pdi->spec_is_dwz, cu);
19838 real_pdi = res.pdi;
19839 cu = res.cu;
19840 }
19841
19842 if (real_pdi->die_parent != NULL)
19843 return;
19844
19845 for (child_pdi = struct_pdi->die_child;
19846 child_pdi != NULL;
19847 child_pdi = child_pdi->die_sibling)
19848 {
19849 if (child_pdi->tag == DW_TAG_subprogram
19850 && child_pdi->linkage_name != NULL)
19851 {
19852 gdb::unique_xmalloc_ptr<char> actual_class_name
19853 (cu->language_defn->class_name_from_physname
19854 (child_pdi->linkage_name));
19855 if (actual_class_name != NULL)
19856 {
19857 struct objfile *objfile = cu->per_objfile->objfile;
19858 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19859 struct_pdi->canonical_name = 1;
19860 }
19861 break;
19862 }
19863 }
19864 }
19865
19866 /* Return true if a DIE with TAG may have the DW_AT_const_value
19867 attribute. */
19868
19869 static bool
19870 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19871 {
19872 switch (tag)
19873 {
19874 case DW_TAG_constant:
19875 case DW_TAG_enumerator:
19876 case DW_TAG_formal_parameter:
19877 case DW_TAG_template_value_param:
19878 case DW_TAG_variable:
19879 return true;
19880 }
19881
19882 return false;
19883 }
19884
19885 void
19886 partial_die_info::fixup (struct dwarf2_cu *cu)
19887 {
19888 /* Once we've fixed up a die, there's no point in doing so again.
19889 This also avoids a memory leak if we were to call
19890 guess_partial_die_structure_name multiple times. */
19891 if (fixup_called)
19892 return;
19893
19894 /* If we found a reference attribute and the DIE has no name, try
19895 to find a name in the referred to DIE. */
19896
19897 if (raw_name == NULL && has_specification)
19898 {
19899 struct partial_die_info *spec_die;
19900
19901 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19902 spec_die = res.pdi;
19903 cu = res.cu;
19904
19905 spec_die->fixup (cu);
19906
19907 if (spec_die->raw_name)
19908 {
19909 raw_name = spec_die->raw_name;
19910 canonical_name = spec_die->canonical_name;
19911
19912 /* Copy DW_AT_external attribute if it is set. */
19913 if (spec_die->is_external)
19914 is_external = spec_die->is_external;
19915 }
19916 }
19917
19918 if (!has_const_value && has_specification
19919 && can_have_DW_AT_const_value_p (tag))
19920 {
19921 struct partial_die_info *spec_die;
19922
19923 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19924 spec_die = res.pdi;
19925 cu = res.cu;
19926
19927 spec_die->fixup (cu);
19928
19929 if (spec_die->has_const_value)
19930 {
19931 /* Copy DW_AT_const_value attribute if it is set. */
19932 has_const_value = spec_die->has_const_value;
19933 }
19934 }
19935
19936 /* Set default names for some unnamed DIEs. */
19937
19938 if (raw_name == NULL && tag == DW_TAG_namespace)
19939 {
19940 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19941 canonical_name = 1;
19942 }
19943
19944 /* If there is no parent die to provide a namespace, and there are
19945 children, see if we can determine the namespace from their linkage
19946 name. */
19947 if (cu->language == language_cplus
19948 && !cu->per_objfile->per_bfd->types.empty ()
19949 && die_parent == NULL
19950 && has_children
19951 && (tag == DW_TAG_class_type
19952 || tag == DW_TAG_structure_type
19953 || tag == DW_TAG_union_type))
19954 guess_partial_die_structure_name (this, cu);
19955
19956 /* GCC might emit a nameless struct or union that has a linkage
19957 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19958 if (raw_name == NULL
19959 && (tag == DW_TAG_class_type
19960 || tag == DW_TAG_interface_type
19961 || tag == DW_TAG_structure_type
19962 || tag == DW_TAG_union_type)
19963 && linkage_name != NULL)
19964 {
19965 gdb::unique_xmalloc_ptr<char> demangled
19966 (gdb_demangle (linkage_name, DMGL_TYPES));
19967 if (demangled != nullptr)
19968 {
19969 const char *base;
19970
19971 /* Strip any leading namespaces/classes, keep only the base name.
19972 DW_AT_name for named DIEs does not contain the prefixes. */
19973 base = strrchr (demangled.get (), ':');
19974 if (base && base > demangled.get () && base[-1] == ':')
19975 base++;
19976 else
19977 base = demangled.get ();
19978
19979 struct objfile *objfile = cu->per_objfile->objfile;
19980 raw_name = objfile->intern (base);
19981 canonical_name = 1;
19982 }
19983 }
19984
19985 fixup_called = 1;
19986 }
19987
19988 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19989 contents from the given SECTION in the HEADER.
19990
19991 HEADER_OFFSET is the offset of the header in the section. */
19992 static void
19993 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19994 struct dwarf2_section_info *section,
19995 sect_offset header_offset)
19996 {
19997 unsigned int bytes_read;
19998 bfd *abfd = section->get_bfd_owner ();
19999 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20000
20001 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20002 info_ptr += bytes_read;
20003
20004 header->version = read_2_bytes (abfd, info_ptr);
20005 info_ptr += 2;
20006
20007 header->addr_size = read_1_byte (abfd, info_ptr);
20008 info_ptr += 1;
20009
20010 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20011 info_ptr += 1;
20012
20013 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20014 }
20015
20016 /* Return the DW_AT_loclists_base value for the CU. */
20017 static ULONGEST
20018 lookup_loclist_base (struct dwarf2_cu *cu)
20019 {
20020 /* For the .dwo unit, the loclist_base points to the first offset following
20021 the header. The header consists of the following entities-
20022 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20023 bit format)
20024 2. version (2 bytes)
20025 3. address size (1 byte)
20026 4. segment selector size (1 byte)
20027 5. offset entry count (4 bytes)
20028 These sizes are derived as per the DWARFv5 standard. */
20029 if (cu->dwo_unit != nullptr)
20030 {
20031 if (cu->header.initial_length_size == 4)
20032 return LOCLIST_HEADER_SIZE32;
20033 return LOCLIST_HEADER_SIZE64;
20034 }
20035 return cu->loclist_base;
20036 }
20037
20038 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20039 array of offsets in the .debug_loclists section. */
20040
20041 static sect_offset
20042 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20043 {
20044 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20045 struct objfile *objfile = per_objfile->objfile;
20046 bfd *abfd = objfile->obfd;
20047 ULONGEST loclist_header_size =
20048 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20049 : LOCLIST_HEADER_SIZE64);
20050 ULONGEST loclist_base = lookup_loclist_base (cu);
20051
20052 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20053 ULONGEST start_offset =
20054 loclist_base + loclist_index * cu->header.offset_size;
20055
20056 /* Get loclists section. */
20057 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20058
20059 /* Read the loclists section content. */
20060 section->read (objfile);
20061 if (section->buffer == NULL)
20062 error (_("DW_FORM_loclistx used without .debug_loclists "
20063 "section [in module %s]"), objfile_name (objfile));
20064
20065 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20066 so if loclist_base is smaller than the header size, we have a problem. */
20067 if (loclist_base < loclist_header_size)
20068 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20069 objfile_name (objfile));
20070
20071 /* Read the header of the loclists contribution. */
20072 struct loclists_rnglists_header header;
20073 read_loclists_rnglists_header (&header, section,
20074 (sect_offset) (loclist_base - loclist_header_size));
20075
20076 /* Verify the loclist index is valid. */
20077 if (loclist_index >= header.offset_entry_count)
20078 error (_("DW_FORM_loclistx pointing outside of "
20079 ".debug_loclists offset array [in module %s]"),
20080 objfile_name (objfile));
20081
20082 /* Validate that reading won't go beyond the end of the section. */
20083 if (start_offset + cu->header.offset_size > section->size)
20084 error (_("Reading DW_FORM_loclistx index beyond end of"
20085 ".debug_loclists section [in module %s]"),
20086 objfile_name (objfile));
20087
20088 const gdb_byte *info_ptr = section->buffer + start_offset;
20089
20090 if (cu->header.offset_size == 4)
20091 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20092 else
20093 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20094 }
20095
20096 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20097 array of offsets in the .debug_rnglists section. */
20098
20099 static sect_offset
20100 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20101 dwarf_tag tag)
20102 {
20103 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20104 struct objfile *objfile = dwarf2_per_objfile->objfile;
20105 bfd *abfd = objfile->obfd;
20106 ULONGEST rnglist_header_size =
20107 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20108 : RNGLIST_HEADER_SIZE64);
20109
20110 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20111 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20112 doesn't apply. */
20113 ULONGEST rnglist_base =
20114 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20115
20116 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20117 ULONGEST start_offset =
20118 rnglist_base + rnglist_index * cu->header.offset_size;
20119
20120 /* Get rnglists section. */
20121 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20122
20123 /* Read the rnglists section content. */
20124 section->read (objfile);
20125 if (section->buffer == nullptr)
20126 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20127 "[in module %s]"),
20128 objfile_name (objfile));
20129
20130 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20131 so if rnglist_base is smaller than the header size, we have a problem. */
20132 if (rnglist_base < rnglist_header_size)
20133 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20134 objfile_name (objfile));
20135
20136 /* Read the header of the rnglists contribution. */
20137 struct loclists_rnglists_header header;
20138 read_loclists_rnglists_header (&header, section,
20139 (sect_offset) (rnglist_base - rnglist_header_size));
20140
20141 /* Verify the rnglist index is valid. */
20142 if (rnglist_index >= header.offset_entry_count)
20143 error (_("DW_FORM_rnglistx index pointing outside of "
20144 ".debug_rnglists offset array [in module %s]"),
20145 objfile_name (objfile));
20146
20147 /* Validate that reading won't go beyond the end of the section. */
20148 if (start_offset + cu->header.offset_size > section->size)
20149 error (_("Reading DW_FORM_rnglistx index beyond end of"
20150 ".debug_rnglists section [in module %s]"),
20151 objfile_name (objfile));
20152
20153 const gdb_byte *info_ptr = section->buffer + start_offset;
20154
20155 if (cu->header.offset_size == 4)
20156 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20157 else
20158 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20159 }
20160
20161 /* Process the attributes that had to be skipped in the first round. These
20162 attributes are the ones that need str_offsets_base or addr_base attributes.
20163 They could not have been processed in the first round, because at the time
20164 the values of str_offsets_base or addr_base may not have been known. */
20165 static void
20166 read_attribute_reprocess (const struct die_reader_specs *reader,
20167 struct attribute *attr, dwarf_tag tag)
20168 {
20169 struct dwarf2_cu *cu = reader->cu;
20170 switch (attr->form)
20171 {
20172 case DW_FORM_addrx:
20173 case DW_FORM_GNU_addr_index:
20174 attr->set_address (read_addr_index (cu,
20175 attr->as_unsigned_reprocess ()));
20176 break;
20177 case DW_FORM_loclistx:
20178 {
20179 sect_offset loclists_sect_off
20180 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20181
20182 attr->set_unsigned (to_underlying (loclists_sect_off));
20183 }
20184 break;
20185 case DW_FORM_rnglistx:
20186 {
20187 sect_offset rnglists_sect_off
20188 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20189
20190 attr->set_unsigned (to_underlying (rnglists_sect_off));
20191 }
20192 break;
20193 case DW_FORM_strx:
20194 case DW_FORM_strx1:
20195 case DW_FORM_strx2:
20196 case DW_FORM_strx3:
20197 case DW_FORM_strx4:
20198 case DW_FORM_GNU_str_index:
20199 {
20200 unsigned int str_index = attr->as_unsigned_reprocess ();
20201 gdb_assert (!attr->canonical_string_p ());
20202 if (reader->dwo_file != NULL)
20203 attr->set_string_noncanonical (read_dwo_str_index (reader,
20204 str_index));
20205 else
20206 attr->set_string_noncanonical (read_stub_str_index (cu,
20207 str_index));
20208 break;
20209 }
20210 default:
20211 gdb_assert_not_reached (_("Unexpected DWARF form."));
20212 }
20213 }
20214
20215 /* Read an attribute value described by an attribute form. */
20216
20217 static const gdb_byte *
20218 read_attribute_value (const struct die_reader_specs *reader,
20219 struct attribute *attr, unsigned form,
20220 LONGEST implicit_const, const gdb_byte *info_ptr)
20221 {
20222 struct dwarf2_cu *cu = reader->cu;
20223 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20224 struct objfile *objfile = per_objfile->objfile;
20225 bfd *abfd = reader->abfd;
20226 struct comp_unit_head *cu_header = &cu->header;
20227 unsigned int bytes_read;
20228 struct dwarf_block *blk;
20229
20230 attr->form = (enum dwarf_form) form;
20231 switch (form)
20232 {
20233 case DW_FORM_ref_addr:
20234 if (cu_header->version == 2)
20235 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20236 &bytes_read));
20237 else
20238 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20239 &bytes_read));
20240 info_ptr += bytes_read;
20241 break;
20242 case DW_FORM_GNU_ref_alt:
20243 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20244 &bytes_read));
20245 info_ptr += bytes_read;
20246 break;
20247 case DW_FORM_addr:
20248 {
20249 struct gdbarch *gdbarch = objfile->arch ();
20250 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20251 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20252 attr->set_address (addr);
20253 info_ptr += bytes_read;
20254 }
20255 break;
20256 case DW_FORM_block2:
20257 blk = dwarf_alloc_block (cu);
20258 blk->size = read_2_bytes (abfd, info_ptr);
20259 info_ptr += 2;
20260 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20261 info_ptr += blk->size;
20262 attr->set_block (blk);
20263 break;
20264 case DW_FORM_block4:
20265 blk = dwarf_alloc_block (cu);
20266 blk->size = read_4_bytes (abfd, info_ptr);
20267 info_ptr += 4;
20268 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20269 info_ptr += blk->size;
20270 attr->set_block (blk);
20271 break;
20272 case DW_FORM_data2:
20273 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20274 info_ptr += 2;
20275 break;
20276 case DW_FORM_data4:
20277 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20278 info_ptr += 4;
20279 break;
20280 case DW_FORM_data8:
20281 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20282 info_ptr += 8;
20283 break;
20284 case DW_FORM_data16:
20285 blk = dwarf_alloc_block (cu);
20286 blk->size = 16;
20287 blk->data = read_n_bytes (abfd, info_ptr, 16);
20288 info_ptr += 16;
20289 attr->set_block (blk);
20290 break;
20291 case DW_FORM_sec_offset:
20292 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20293 &bytes_read));
20294 info_ptr += bytes_read;
20295 break;
20296 case DW_FORM_loclistx:
20297 {
20298 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20299 &bytes_read));
20300 info_ptr += bytes_read;
20301 }
20302 break;
20303 case DW_FORM_string:
20304 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20305 &bytes_read));
20306 info_ptr += bytes_read;
20307 break;
20308 case DW_FORM_strp:
20309 if (!cu->per_cu->is_dwz)
20310 {
20311 attr->set_string_noncanonical
20312 (read_indirect_string (per_objfile,
20313 abfd, info_ptr, cu_header,
20314 &bytes_read));
20315 info_ptr += bytes_read;
20316 break;
20317 }
20318 /* FALLTHROUGH */
20319 case DW_FORM_line_strp:
20320 if (!cu->per_cu->is_dwz)
20321 {
20322 attr->set_string_noncanonical
20323 (per_objfile->read_line_string (info_ptr, cu_header,
20324 &bytes_read));
20325 info_ptr += bytes_read;
20326 break;
20327 }
20328 /* FALLTHROUGH */
20329 case DW_FORM_GNU_strp_alt:
20330 {
20331 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20332 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20333 &bytes_read);
20334
20335 attr->set_string_noncanonical
20336 (dwz->read_string (objfile, str_offset));
20337 info_ptr += bytes_read;
20338 }
20339 break;
20340 case DW_FORM_exprloc:
20341 case DW_FORM_block:
20342 blk = dwarf_alloc_block (cu);
20343 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20344 info_ptr += bytes_read;
20345 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20346 info_ptr += blk->size;
20347 attr->set_block (blk);
20348 break;
20349 case DW_FORM_block1:
20350 blk = dwarf_alloc_block (cu);
20351 blk->size = read_1_byte (abfd, info_ptr);
20352 info_ptr += 1;
20353 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20354 info_ptr += blk->size;
20355 attr->set_block (blk);
20356 break;
20357 case DW_FORM_data1:
20358 case DW_FORM_flag:
20359 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20360 info_ptr += 1;
20361 break;
20362 case DW_FORM_flag_present:
20363 attr->set_unsigned (1);
20364 break;
20365 case DW_FORM_sdata:
20366 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20367 info_ptr += bytes_read;
20368 break;
20369 case DW_FORM_rnglistx:
20370 {
20371 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20372 &bytes_read));
20373 info_ptr += bytes_read;
20374 }
20375 break;
20376 case DW_FORM_udata:
20377 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20378 info_ptr += bytes_read;
20379 break;
20380 case DW_FORM_ref1:
20381 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20382 + read_1_byte (abfd, info_ptr)));
20383 info_ptr += 1;
20384 break;
20385 case DW_FORM_ref2:
20386 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20387 + read_2_bytes (abfd, info_ptr)));
20388 info_ptr += 2;
20389 break;
20390 case DW_FORM_ref4:
20391 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20392 + read_4_bytes (abfd, info_ptr)));
20393 info_ptr += 4;
20394 break;
20395 case DW_FORM_ref8:
20396 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20397 + read_8_bytes (abfd, info_ptr)));
20398 info_ptr += 8;
20399 break;
20400 case DW_FORM_ref_sig8:
20401 attr->set_signature (read_8_bytes (abfd, info_ptr));
20402 info_ptr += 8;
20403 break;
20404 case DW_FORM_ref_udata:
20405 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20406 + read_unsigned_leb128 (abfd, info_ptr,
20407 &bytes_read)));
20408 info_ptr += bytes_read;
20409 break;
20410 case DW_FORM_indirect:
20411 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20412 info_ptr += bytes_read;
20413 if (form == DW_FORM_implicit_const)
20414 {
20415 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20416 info_ptr += bytes_read;
20417 }
20418 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20419 info_ptr);
20420 break;
20421 case DW_FORM_implicit_const:
20422 attr->set_signed (implicit_const);
20423 break;
20424 case DW_FORM_addrx:
20425 case DW_FORM_GNU_addr_index:
20426 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20427 &bytes_read));
20428 info_ptr += bytes_read;
20429 break;
20430 case DW_FORM_strx:
20431 case DW_FORM_strx1:
20432 case DW_FORM_strx2:
20433 case DW_FORM_strx3:
20434 case DW_FORM_strx4:
20435 case DW_FORM_GNU_str_index:
20436 {
20437 ULONGEST str_index;
20438 if (form == DW_FORM_strx1)
20439 {
20440 str_index = read_1_byte (abfd, info_ptr);
20441 info_ptr += 1;
20442 }
20443 else if (form == DW_FORM_strx2)
20444 {
20445 str_index = read_2_bytes (abfd, info_ptr);
20446 info_ptr += 2;
20447 }
20448 else if (form == DW_FORM_strx3)
20449 {
20450 str_index = read_3_bytes (abfd, info_ptr);
20451 info_ptr += 3;
20452 }
20453 else if (form == DW_FORM_strx4)
20454 {
20455 str_index = read_4_bytes (abfd, info_ptr);
20456 info_ptr += 4;
20457 }
20458 else
20459 {
20460 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20461 info_ptr += bytes_read;
20462 }
20463 attr->set_unsigned_reprocess (str_index);
20464 }
20465 break;
20466 default:
20467 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20468 dwarf_form_name (form),
20469 bfd_get_filename (abfd));
20470 }
20471
20472 /* Super hack. */
20473 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20474 attr->form = DW_FORM_GNU_ref_alt;
20475
20476 /* We have seen instances where the compiler tried to emit a byte
20477 size attribute of -1 which ended up being encoded as an unsigned
20478 0xffffffff. Although 0xffffffff is technically a valid size value,
20479 an object of this size seems pretty unlikely so we can relatively
20480 safely treat these cases as if the size attribute was invalid and
20481 treat them as zero by default. */
20482 if (attr->name == DW_AT_byte_size
20483 && form == DW_FORM_data4
20484 && attr->as_unsigned () >= 0xffffffff)
20485 {
20486 complaint
20487 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20488 hex_string (attr->as_unsigned ()));
20489 attr->set_unsigned (0);
20490 }
20491
20492 return info_ptr;
20493 }
20494
20495 /* Read an attribute described by an abbreviated attribute. */
20496
20497 static const gdb_byte *
20498 read_attribute (const struct die_reader_specs *reader,
20499 struct attribute *attr, const struct attr_abbrev *abbrev,
20500 const gdb_byte *info_ptr)
20501 {
20502 attr->name = abbrev->name;
20503 attr->string_is_canonical = 0;
20504 attr->requires_reprocessing = 0;
20505 return read_attribute_value (reader, attr, abbrev->form,
20506 abbrev->implicit_const, info_ptr);
20507 }
20508
20509 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20510
20511 static const char *
20512 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20513 LONGEST str_offset)
20514 {
20515 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20516 str_offset, "DW_FORM_strp");
20517 }
20518
20519 /* Return pointer to string at .debug_str offset as read from BUF.
20520 BUF is assumed to be in a compilation unit described by CU_HEADER.
20521 Return *BYTES_READ_PTR count of bytes read from BUF. */
20522
20523 static const char *
20524 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20525 const gdb_byte *buf,
20526 const struct comp_unit_head *cu_header,
20527 unsigned int *bytes_read_ptr)
20528 {
20529 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20530
20531 return read_indirect_string_at_offset (per_objfile, str_offset);
20532 }
20533
20534 /* See read.h. */
20535
20536 const char *
20537 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20538 const struct comp_unit_head *cu_header,
20539 unsigned int *bytes_read_ptr)
20540 {
20541 bfd *abfd = objfile->obfd;
20542 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20543
20544 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20545 }
20546
20547 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20548 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20549 ADDR_SIZE is the size of addresses from the CU header. */
20550
20551 static CORE_ADDR
20552 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20553 gdb::optional<ULONGEST> addr_base, int addr_size)
20554 {
20555 struct objfile *objfile = per_objfile->objfile;
20556 bfd *abfd = objfile->obfd;
20557 const gdb_byte *info_ptr;
20558 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20559
20560 per_objfile->per_bfd->addr.read (objfile);
20561 if (per_objfile->per_bfd->addr.buffer == NULL)
20562 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20563 objfile_name (objfile));
20564 if (addr_base_or_zero + addr_index * addr_size
20565 >= per_objfile->per_bfd->addr.size)
20566 error (_("DW_FORM_addr_index pointing outside of "
20567 ".debug_addr section [in module %s]"),
20568 objfile_name (objfile));
20569 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20570 + addr_index * addr_size);
20571 if (addr_size == 4)
20572 return bfd_get_32 (abfd, info_ptr);
20573 else
20574 return bfd_get_64 (abfd, info_ptr);
20575 }
20576
20577 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20578
20579 static CORE_ADDR
20580 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20581 {
20582 return read_addr_index_1 (cu->per_objfile, addr_index,
20583 cu->addr_base, cu->header.addr_size);
20584 }
20585
20586 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20587
20588 static CORE_ADDR
20589 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20590 unsigned int *bytes_read)
20591 {
20592 bfd *abfd = cu->per_objfile->objfile->obfd;
20593 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20594
20595 return read_addr_index (cu, addr_index);
20596 }
20597
20598 /* See read.h. */
20599
20600 CORE_ADDR
20601 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20602 dwarf2_per_objfile *per_objfile,
20603 unsigned int addr_index)
20604 {
20605 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20606 gdb::optional<ULONGEST> addr_base;
20607 int addr_size;
20608
20609 /* We need addr_base and addr_size.
20610 If we don't have PER_CU->cu, we have to get it.
20611 Nasty, but the alternative is storing the needed info in PER_CU,
20612 which at this point doesn't seem justified: it's not clear how frequently
20613 it would get used and it would increase the size of every PER_CU.
20614 Entry points like dwarf2_per_cu_addr_size do a similar thing
20615 so we're not in uncharted territory here.
20616 Alas we need to be a bit more complicated as addr_base is contained
20617 in the DIE.
20618
20619 We don't need to read the entire CU(/TU).
20620 We just need the header and top level die.
20621
20622 IWBN to use the aging mechanism to let us lazily later discard the CU.
20623 For now we skip this optimization. */
20624
20625 if (cu != NULL)
20626 {
20627 addr_base = cu->addr_base;
20628 addr_size = cu->header.addr_size;
20629 }
20630 else
20631 {
20632 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20633 addr_base = reader.cu->addr_base;
20634 addr_size = reader.cu->header.addr_size;
20635 }
20636
20637 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20638 }
20639
20640 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20641 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20642 DWO file. */
20643
20644 static const char *
20645 read_str_index (struct dwarf2_cu *cu,
20646 struct dwarf2_section_info *str_section,
20647 struct dwarf2_section_info *str_offsets_section,
20648 ULONGEST str_offsets_base, ULONGEST str_index)
20649 {
20650 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20651 struct objfile *objfile = per_objfile->objfile;
20652 const char *objf_name = objfile_name (objfile);
20653 bfd *abfd = objfile->obfd;
20654 const gdb_byte *info_ptr;
20655 ULONGEST str_offset;
20656 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20657
20658 str_section->read (objfile);
20659 str_offsets_section->read (objfile);
20660 if (str_section->buffer == NULL)
20661 error (_("%s used without %s section"
20662 " in CU at offset %s [in module %s]"),
20663 form_name, str_section->get_name (),
20664 sect_offset_str (cu->header.sect_off), objf_name);
20665 if (str_offsets_section->buffer == NULL)
20666 error (_("%s used without %s section"
20667 " in CU at offset %s [in module %s]"),
20668 form_name, str_section->get_name (),
20669 sect_offset_str (cu->header.sect_off), objf_name);
20670 info_ptr = (str_offsets_section->buffer
20671 + str_offsets_base
20672 + str_index * cu->header.offset_size);
20673 if (cu->header.offset_size == 4)
20674 str_offset = bfd_get_32 (abfd, info_ptr);
20675 else
20676 str_offset = bfd_get_64 (abfd, info_ptr);
20677 if (str_offset >= str_section->size)
20678 error (_("Offset from %s pointing outside of"
20679 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20680 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20681 return (const char *) (str_section->buffer + str_offset);
20682 }
20683
20684 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20685
20686 static const char *
20687 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20688 {
20689 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20690 ? reader->cu->header.addr_size : 0;
20691 return read_str_index (reader->cu,
20692 &reader->dwo_file->sections.str,
20693 &reader->dwo_file->sections.str_offsets,
20694 str_offsets_base, str_index);
20695 }
20696
20697 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20698
20699 static const char *
20700 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20701 {
20702 struct objfile *objfile = cu->per_objfile->objfile;
20703 const char *objf_name = objfile_name (objfile);
20704 static const char form_name[] = "DW_FORM_GNU_str_index";
20705 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20706
20707 if (!cu->str_offsets_base.has_value ())
20708 error (_("%s used in Fission stub without %s"
20709 " in CU at offset 0x%lx [in module %s]"),
20710 form_name, str_offsets_attr_name,
20711 (long) cu->header.offset_size, objf_name);
20712
20713 return read_str_index (cu,
20714 &cu->per_objfile->per_bfd->str,
20715 &cu->per_objfile->per_bfd->str_offsets,
20716 *cu->str_offsets_base, str_index);
20717 }
20718
20719 /* Return the length of an LEB128 number in BUF. */
20720
20721 static int
20722 leb128_size (const gdb_byte *buf)
20723 {
20724 const gdb_byte *begin = buf;
20725 gdb_byte byte;
20726
20727 while (1)
20728 {
20729 byte = *buf++;
20730 if ((byte & 128) == 0)
20731 return buf - begin;
20732 }
20733 }
20734
20735 static void
20736 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20737 {
20738 switch (lang)
20739 {
20740 case DW_LANG_C89:
20741 case DW_LANG_C99:
20742 case DW_LANG_C11:
20743 case DW_LANG_C:
20744 case DW_LANG_UPC:
20745 cu->language = language_c;
20746 break;
20747 case DW_LANG_Java:
20748 case DW_LANG_C_plus_plus:
20749 case DW_LANG_C_plus_plus_11:
20750 case DW_LANG_C_plus_plus_14:
20751 cu->language = language_cplus;
20752 break;
20753 case DW_LANG_D:
20754 cu->language = language_d;
20755 break;
20756 case DW_LANG_Fortran77:
20757 case DW_LANG_Fortran90:
20758 case DW_LANG_Fortran95:
20759 case DW_LANG_Fortran03:
20760 case DW_LANG_Fortran08:
20761 cu->language = language_fortran;
20762 break;
20763 case DW_LANG_Go:
20764 cu->language = language_go;
20765 break;
20766 case DW_LANG_Mips_Assembler:
20767 cu->language = language_asm;
20768 break;
20769 case DW_LANG_Ada83:
20770 case DW_LANG_Ada95:
20771 cu->language = language_ada;
20772 break;
20773 case DW_LANG_Modula2:
20774 cu->language = language_m2;
20775 break;
20776 case DW_LANG_Pascal83:
20777 cu->language = language_pascal;
20778 break;
20779 case DW_LANG_ObjC:
20780 cu->language = language_objc;
20781 break;
20782 case DW_LANG_Rust:
20783 case DW_LANG_Rust_old:
20784 cu->language = language_rust;
20785 break;
20786 case DW_LANG_Cobol74:
20787 case DW_LANG_Cobol85:
20788 default:
20789 cu->language = language_minimal;
20790 break;
20791 }
20792 cu->language_defn = language_def (cu->language);
20793 }
20794
20795 /* Return the named attribute or NULL if not there. */
20796
20797 static struct attribute *
20798 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20799 {
20800 for (;;)
20801 {
20802 unsigned int i;
20803 struct attribute *spec = NULL;
20804
20805 for (i = 0; i < die->num_attrs; ++i)
20806 {
20807 if (die->attrs[i].name == name)
20808 return &die->attrs[i];
20809 if (die->attrs[i].name == DW_AT_specification
20810 || die->attrs[i].name == DW_AT_abstract_origin)
20811 spec = &die->attrs[i];
20812 }
20813
20814 if (!spec)
20815 break;
20816
20817 die = follow_die_ref (die, spec, &cu);
20818 }
20819
20820 return NULL;
20821 }
20822
20823 /* Return the string associated with a string-typed attribute, or NULL if it
20824 is either not found or is of an incorrect type. */
20825
20826 static const char *
20827 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20828 {
20829 struct attribute *attr;
20830 const char *str = NULL;
20831
20832 attr = dwarf2_attr (die, name, cu);
20833
20834 if (attr != NULL)
20835 {
20836 str = attr->as_string ();
20837 if (str == nullptr)
20838 complaint (_("string type expected for attribute %s for "
20839 "DIE at %s in module %s"),
20840 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20841 objfile_name (cu->per_objfile->objfile));
20842 }
20843
20844 return str;
20845 }
20846
20847 /* Return the dwo name or NULL if not present. If present, it is in either
20848 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20849 static const char *
20850 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20851 {
20852 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20853 if (dwo_name == nullptr)
20854 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20855 return dwo_name;
20856 }
20857
20858 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20859 and holds a non-zero value. This function should only be used for
20860 DW_FORM_flag or DW_FORM_flag_present attributes. */
20861
20862 static int
20863 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20864 {
20865 struct attribute *attr = dwarf2_attr (die, name, cu);
20866
20867 return attr != nullptr && attr->as_boolean ();
20868 }
20869
20870 static int
20871 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20872 {
20873 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20874 which value is non-zero. However, we have to be careful with
20875 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20876 (via dwarf2_flag_true_p) follows this attribute. So we may
20877 end up accidently finding a declaration attribute that belongs
20878 to a different DIE referenced by the specification attribute,
20879 even though the given DIE does not have a declaration attribute. */
20880 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20881 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20882 }
20883
20884 /* Return the die giving the specification for DIE, if there is
20885 one. *SPEC_CU is the CU containing DIE on input, and the CU
20886 containing the return value on output. If there is no
20887 specification, but there is an abstract origin, that is
20888 returned. */
20889
20890 static struct die_info *
20891 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20892 {
20893 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20894 *spec_cu);
20895
20896 if (spec_attr == NULL)
20897 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20898
20899 if (spec_attr == NULL)
20900 return NULL;
20901 else
20902 return follow_die_ref (die, spec_attr, spec_cu);
20903 }
20904
20905 /* Stub for free_line_header to match void * callback types. */
20906
20907 static void
20908 free_line_header_voidp (void *arg)
20909 {
20910 struct line_header *lh = (struct line_header *) arg;
20911
20912 delete lh;
20913 }
20914
20915 /* A convenience function to find the proper .debug_line section for a CU. */
20916
20917 static struct dwarf2_section_info *
20918 get_debug_line_section (struct dwarf2_cu *cu)
20919 {
20920 struct dwarf2_section_info *section;
20921 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20922
20923 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20924 DWO file. */
20925 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20926 section = &cu->dwo_unit->dwo_file->sections.line;
20927 else if (cu->per_cu->is_dwz)
20928 {
20929 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20930
20931 section = &dwz->line;
20932 }
20933 else
20934 section = &per_objfile->per_bfd->line;
20935
20936 return section;
20937 }
20938
20939 /* Read the statement program header starting at OFFSET in
20940 .debug_line, or .debug_line.dwo. Return a pointer
20941 to a struct line_header, allocated using xmalloc.
20942 Returns NULL if there is a problem reading the header, e.g., if it
20943 has a version we don't understand.
20944
20945 NOTE: the strings in the include directory and file name tables of
20946 the returned object point into the dwarf line section buffer,
20947 and must not be freed. */
20948
20949 static line_header_up
20950 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20951 {
20952 struct dwarf2_section_info *section;
20953 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20954
20955 section = get_debug_line_section (cu);
20956 section->read (per_objfile->objfile);
20957 if (section->buffer == NULL)
20958 {
20959 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20960 complaint (_("missing .debug_line.dwo section"));
20961 else
20962 complaint (_("missing .debug_line section"));
20963 return 0;
20964 }
20965
20966 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20967 per_objfile, section, &cu->header);
20968 }
20969
20970 /* Subroutine of dwarf_decode_lines to simplify it.
20971 Return the file name of the psymtab for the given file_entry.
20972 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20973 If space for the result is malloc'd, *NAME_HOLDER will be set.
20974 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20975
20976 static const char *
20977 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20978 const dwarf2_psymtab *pst,
20979 const char *comp_dir,
20980 gdb::unique_xmalloc_ptr<char> *name_holder)
20981 {
20982 const char *include_name = fe.name;
20983 const char *include_name_to_compare = include_name;
20984 const char *pst_filename;
20985 int file_is_pst;
20986
20987 const char *dir_name = fe.include_dir (lh);
20988
20989 gdb::unique_xmalloc_ptr<char> hold_compare;
20990 if (!IS_ABSOLUTE_PATH (include_name)
20991 && (dir_name != NULL || comp_dir != NULL))
20992 {
20993 /* Avoid creating a duplicate psymtab for PST.
20994 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20995 Before we do the comparison, however, we need to account
20996 for DIR_NAME and COMP_DIR.
20997 First prepend dir_name (if non-NULL). If we still don't
20998 have an absolute path prepend comp_dir (if non-NULL).
20999 However, the directory we record in the include-file's
21000 psymtab does not contain COMP_DIR (to match the
21001 corresponding symtab(s)).
21002
21003 Example:
21004
21005 bash$ cd /tmp
21006 bash$ gcc -g ./hello.c
21007 include_name = "hello.c"
21008 dir_name = "."
21009 DW_AT_comp_dir = comp_dir = "/tmp"
21010 DW_AT_name = "./hello.c"
21011
21012 */
21013
21014 if (dir_name != NULL)
21015 {
21016 name_holder->reset (concat (dir_name, SLASH_STRING,
21017 include_name, (char *) NULL));
21018 include_name = name_holder->get ();
21019 include_name_to_compare = include_name;
21020 }
21021 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21022 {
21023 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21024 include_name, (char *) NULL));
21025 include_name_to_compare = hold_compare.get ();
21026 }
21027 }
21028
21029 pst_filename = pst->filename;
21030 gdb::unique_xmalloc_ptr<char> copied_name;
21031 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21032 {
21033 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21034 pst_filename, (char *) NULL));
21035 pst_filename = copied_name.get ();
21036 }
21037
21038 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21039
21040 if (file_is_pst)
21041 return NULL;
21042 return include_name;
21043 }
21044
21045 /* State machine to track the state of the line number program. */
21046
21047 class lnp_state_machine
21048 {
21049 public:
21050 /* Initialize a machine state for the start of a line number
21051 program. */
21052 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21053 bool record_lines_p);
21054
21055 file_entry *current_file ()
21056 {
21057 /* lh->file_names is 0-based, but the file name numbers in the
21058 statement program are 1-based. */
21059 return m_line_header->file_name_at (m_file);
21060 }
21061
21062 /* Record the line in the state machine. END_SEQUENCE is true if
21063 we're processing the end of a sequence. */
21064 void record_line (bool end_sequence);
21065
21066 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21067 nop-out rest of the lines in this sequence. */
21068 void check_line_address (struct dwarf2_cu *cu,
21069 const gdb_byte *line_ptr,
21070 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21071
21072 void handle_set_discriminator (unsigned int discriminator)
21073 {
21074 m_discriminator = discriminator;
21075 m_line_has_non_zero_discriminator |= discriminator != 0;
21076 }
21077
21078 /* Handle DW_LNE_set_address. */
21079 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21080 {
21081 m_op_index = 0;
21082 address += baseaddr;
21083 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21084 }
21085
21086 /* Handle DW_LNS_advance_pc. */
21087 void handle_advance_pc (CORE_ADDR adjust);
21088
21089 /* Handle a special opcode. */
21090 void handle_special_opcode (unsigned char op_code);
21091
21092 /* Handle DW_LNS_advance_line. */
21093 void handle_advance_line (int line_delta)
21094 {
21095 advance_line (line_delta);
21096 }
21097
21098 /* Handle DW_LNS_set_file. */
21099 void handle_set_file (file_name_index file);
21100
21101 /* Handle DW_LNS_negate_stmt. */
21102 void handle_negate_stmt ()
21103 {
21104 m_is_stmt = !m_is_stmt;
21105 }
21106
21107 /* Handle DW_LNS_const_add_pc. */
21108 void handle_const_add_pc ();
21109
21110 /* Handle DW_LNS_fixed_advance_pc. */
21111 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21112 {
21113 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21114 m_op_index = 0;
21115 }
21116
21117 /* Handle DW_LNS_copy. */
21118 void handle_copy ()
21119 {
21120 record_line (false);
21121 m_discriminator = 0;
21122 }
21123
21124 /* Handle DW_LNE_end_sequence. */
21125 void handle_end_sequence ()
21126 {
21127 m_currently_recording_lines = true;
21128 }
21129
21130 private:
21131 /* Advance the line by LINE_DELTA. */
21132 void advance_line (int line_delta)
21133 {
21134 m_line += line_delta;
21135
21136 if (line_delta != 0)
21137 m_line_has_non_zero_discriminator = m_discriminator != 0;
21138 }
21139
21140 struct dwarf2_cu *m_cu;
21141
21142 gdbarch *m_gdbarch;
21143
21144 /* True if we're recording lines.
21145 Otherwise we're building partial symtabs and are just interested in
21146 finding include files mentioned by the line number program. */
21147 bool m_record_lines_p;
21148
21149 /* The line number header. */
21150 line_header *m_line_header;
21151
21152 /* These are part of the standard DWARF line number state machine,
21153 and initialized according to the DWARF spec. */
21154
21155 unsigned char m_op_index = 0;
21156 /* The line table index of the current file. */
21157 file_name_index m_file = 1;
21158 unsigned int m_line = 1;
21159
21160 /* These are initialized in the constructor. */
21161
21162 CORE_ADDR m_address;
21163 bool m_is_stmt;
21164 unsigned int m_discriminator;
21165
21166 /* Additional bits of state we need to track. */
21167
21168 /* The last file that we called dwarf2_start_subfile for.
21169 This is only used for TLLs. */
21170 unsigned int m_last_file = 0;
21171 /* The last file a line number was recorded for. */
21172 struct subfile *m_last_subfile = NULL;
21173
21174 /* The address of the last line entry. */
21175 CORE_ADDR m_last_address;
21176
21177 /* Set to true when a previous line at the same address (using
21178 m_last_address) had m_is_stmt true. This is reset to false when a
21179 line entry at a new address (m_address different to m_last_address) is
21180 processed. */
21181 bool m_stmt_at_address = false;
21182
21183 /* When true, record the lines we decode. */
21184 bool m_currently_recording_lines = false;
21185
21186 /* The last line number that was recorded, used to coalesce
21187 consecutive entries for the same line. This can happen, for
21188 example, when discriminators are present. PR 17276. */
21189 unsigned int m_last_line = 0;
21190 bool m_line_has_non_zero_discriminator = false;
21191 };
21192
21193 void
21194 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21195 {
21196 CORE_ADDR addr_adj = (((m_op_index + adjust)
21197 / m_line_header->maximum_ops_per_instruction)
21198 * m_line_header->minimum_instruction_length);
21199 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21200 m_op_index = ((m_op_index + adjust)
21201 % m_line_header->maximum_ops_per_instruction);
21202 }
21203
21204 void
21205 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21206 {
21207 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21208 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21209 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21210 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21211 / m_line_header->maximum_ops_per_instruction)
21212 * m_line_header->minimum_instruction_length);
21213 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21214 m_op_index = ((m_op_index + adj_opcode_d)
21215 % m_line_header->maximum_ops_per_instruction);
21216
21217 int line_delta = m_line_header->line_base + adj_opcode_r;
21218 advance_line (line_delta);
21219 record_line (false);
21220 m_discriminator = 0;
21221 }
21222
21223 void
21224 lnp_state_machine::handle_set_file (file_name_index file)
21225 {
21226 m_file = file;
21227
21228 const file_entry *fe = current_file ();
21229 if (fe == NULL)
21230 dwarf2_debug_line_missing_file_complaint ();
21231 else if (m_record_lines_p)
21232 {
21233 const char *dir = fe->include_dir (m_line_header);
21234
21235 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21236 m_line_has_non_zero_discriminator = m_discriminator != 0;
21237 dwarf2_start_subfile (m_cu, fe->name, dir);
21238 }
21239 }
21240
21241 void
21242 lnp_state_machine::handle_const_add_pc ()
21243 {
21244 CORE_ADDR adjust
21245 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21246
21247 CORE_ADDR addr_adj
21248 = (((m_op_index + adjust)
21249 / m_line_header->maximum_ops_per_instruction)
21250 * m_line_header->minimum_instruction_length);
21251
21252 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21253 m_op_index = ((m_op_index + adjust)
21254 % m_line_header->maximum_ops_per_instruction);
21255 }
21256
21257 /* Return non-zero if we should add LINE to the line number table.
21258 LINE is the line to add, LAST_LINE is the last line that was added,
21259 LAST_SUBFILE is the subfile for LAST_LINE.
21260 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21261 had a non-zero discriminator.
21262
21263 We have to be careful in the presence of discriminators.
21264 E.g., for this line:
21265
21266 for (i = 0; i < 100000; i++);
21267
21268 clang can emit four line number entries for that one line,
21269 each with a different discriminator.
21270 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21271
21272 However, we want gdb to coalesce all four entries into one.
21273 Otherwise the user could stepi into the middle of the line and
21274 gdb would get confused about whether the pc really was in the
21275 middle of the line.
21276
21277 Things are further complicated by the fact that two consecutive
21278 line number entries for the same line is a heuristic used by gcc
21279 to denote the end of the prologue. So we can't just discard duplicate
21280 entries, we have to be selective about it. The heuristic we use is
21281 that we only collapse consecutive entries for the same line if at least
21282 one of those entries has a non-zero discriminator. PR 17276.
21283
21284 Note: Addresses in the line number state machine can never go backwards
21285 within one sequence, thus this coalescing is ok. */
21286
21287 static int
21288 dwarf_record_line_p (struct dwarf2_cu *cu,
21289 unsigned int line, unsigned int last_line,
21290 int line_has_non_zero_discriminator,
21291 struct subfile *last_subfile)
21292 {
21293 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21294 return 1;
21295 if (line != last_line)
21296 return 1;
21297 /* Same line for the same file that we've seen already.
21298 As a last check, for pr 17276, only record the line if the line
21299 has never had a non-zero discriminator. */
21300 if (!line_has_non_zero_discriminator)
21301 return 1;
21302 return 0;
21303 }
21304
21305 /* Use the CU's builder to record line number LINE beginning at
21306 address ADDRESS in the line table of subfile SUBFILE. */
21307
21308 static void
21309 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21310 unsigned int line, CORE_ADDR address, bool is_stmt,
21311 struct dwarf2_cu *cu)
21312 {
21313 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21314
21315 if (dwarf_line_debug)
21316 {
21317 fprintf_unfiltered (gdb_stdlog,
21318 "Recording line %u, file %s, address %s\n",
21319 line, lbasename (subfile->name),
21320 paddress (gdbarch, address));
21321 }
21322
21323 if (cu != nullptr)
21324 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21325 }
21326
21327 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21328 Mark the end of a set of line number records.
21329 The arguments are the same as for dwarf_record_line_1.
21330 If SUBFILE is NULL the request is ignored. */
21331
21332 static void
21333 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21334 CORE_ADDR address, struct dwarf2_cu *cu)
21335 {
21336 if (subfile == NULL)
21337 return;
21338
21339 if (dwarf_line_debug)
21340 {
21341 fprintf_unfiltered (gdb_stdlog,
21342 "Finishing current line, file %s, address %s\n",
21343 lbasename (subfile->name),
21344 paddress (gdbarch, address));
21345 }
21346
21347 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21348 }
21349
21350 void
21351 lnp_state_machine::record_line (bool end_sequence)
21352 {
21353 if (dwarf_line_debug)
21354 {
21355 fprintf_unfiltered (gdb_stdlog,
21356 "Processing actual line %u: file %u,"
21357 " address %s, is_stmt %u, discrim %u%s\n",
21358 m_line, m_file,
21359 paddress (m_gdbarch, m_address),
21360 m_is_stmt, m_discriminator,
21361 (end_sequence ? "\t(end sequence)" : ""));
21362 }
21363
21364 file_entry *fe = current_file ();
21365
21366 if (fe == NULL)
21367 dwarf2_debug_line_missing_file_complaint ();
21368 /* For now we ignore lines not starting on an instruction boundary.
21369 But not when processing end_sequence for compatibility with the
21370 previous version of the code. */
21371 else if (m_op_index == 0 || end_sequence)
21372 {
21373 fe->included_p = 1;
21374 if (m_record_lines_p)
21375 {
21376 /* When we switch files we insert an end maker in the first file,
21377 switch to the second file and add a new line entry. The
21378 problem is that the end marker inserted in the first file will
21379 discard any previous line entries at the same address. If the
21380 line entries in the first file are marked as is-stmt, while
21381 the new line in the second file is non-stmt, then this means
21382 the end marker will discard is-stmt lines so we can have a
21383 non-stmt line. This means that there are less addresses at
21384 which the user can insert a breakpoint.
21385
21386 To improve this we track the last address in m_last_address,
21387 and whether we have seen an is-stmt at this address. Then
21388 when switching files, if we have seen a stmt at the current
21389 address, and we are switching to create a non-stmt line, then
21390 discard the new line. */
21391 bool file_changed
21392 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21393 bool ignore_this_line
21394 = ((file_changed && !end_sequence && m_last_address == m_address
21395 && !m_is_stmt && m_stmt_at_address)
21396 || (!end_sequence && m_line == 0));
21397
21398 if ((file_changed && !ignore_this_line) || end_sequence)
21399 {
21400 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21401 m_currently_recording_lines ? m_cu : nullptr);
21402 }
21403
21404 if (!end_sequence && !ignore_this_line)
21405 {
21406 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21407
21408 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21409 m_line_has_non_zero_discriminator,
21410 m_last_subfile))
21411 {
21412 buildsym_compunit *builder = m_cu->get_builder ();
21413 dwarf_record_line_1 (m_gdbarch,
21414 builder->get_current_subfile (),
21415 m_line, m_address, is_stmt,
21416 m_currently_recording_lines ? m_cu : nullptr);
21417 }
21418 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21419 m_last_line = m_line;
21420 }
21421 }
21422 }
21423
21424 /* Track whether we have seen any m_is_stmt true at m_address in case we
21425 have multiple line table entries all at m_address. */
21426 if (m_last_address != m_address)
21427 {
21428 m_stmt_at_address = false;
21429 m_last_address = m_address;
21430 }
21431 m_stmt_at_address |= m_is_stmt;
21432 }
21433
21434 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21435 line_header *lh, bool record_lines_p)
21436 {
21437 m_cu = cu;
21438 m_gdbarch = arch;
21439 m_record_lines_p = record_lines_p;
21440 m_line_header = lh;
21441
21442 m_currently_recording_lines = true;
21443
21444 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21445 was a line entry for it so that the backend has a chance to adjust it
21446 and also record it in case it needs it. This is currently used by MIPS
21447 code, cf. `mips_adjust_dwarf2_line'. */
21448 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21449 m_is_stmt = lh->default_is_stmt;
21450 m_discriminator = 0;
21451
21452 m_last_address = m_address;
21453 m_stmt_at_address = false;
21454 }
21455
21456 void
21457 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21458 const gdb_byte *line_ptr,
21459 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21460 {
21461 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21462 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21463 located at 0x0. In this case, additionally check that if
21464 ADDRESS < UNRELOCATED_LOWPC. */
21465
21466 if ((address == 0 && address < unrelocated_lowpc)
21467 || address == (CORE_ADDR) -1)
21468 {
21469 /* This line table is for a function which has been
21470 GCd by the linker. Ignore it. PR gdb/12528 */
21471
21472 struct objfile *objfile = cu->per_objfile->objfile;
21473 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21474
21475 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21476 line_offset, objfile_name (objfile));
21477 m_currently_recording_lines = false;
21478 /* Note: m_currently_recording_lines is left as false until we see
21479 DW_LNE_end_sequence. */
21480 }
21481 }
21482
21483 /* Subroutine of dwarf_decode_lines to simplify it.
21484 Process the line number information in LH.
21485 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21486 program in order to set included_p for every referenced header. */
21487
21488 static void
21489 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21490 const int decode_for_pst_p, CORE_ADDR lowpc)
21491 {
21492 const gdb_byte *line_ptr, *extended_end;
21493 const gdb_byte *line_end;
21494 unsigned int bytes_read, extended_len;
21495 unsigned char op_code, extended_op;
21496 CORE_ADDR baseaddr;
21497 struct objfile *objfile = cu->per_objfile->objfile;
21498 bfd *abfd = objfile->obfd;
21499 struct gdbarch *gdbarch = objfile->arch ();
21500 /* True if we're recording line info (as opposed to building partial
21501 symtabs and just interested in finding include files mentioned by
21502 the line number program). */
21503 bool record_lines_p = !decode_for_pst_p;
21504
21505 baseaddr = objfile->text_section_offset ();
21506
21507 line_ptr = lh->statement_program_start;
21508 line_end = lh->statement_program_end;
21509
21510 /* Read the statement sequences until there's nothing left. */
21511 while (line_ptr < line_end)
21512 {
21513 /* The DWARF line number program state machine. Reset the state
21514 machine at the start of each sequence. */
21515 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21516 bool end_sequence = false;
21517
21518 if (record_lines_p)
21519 {
21520 /* Start a subfile for the current file of the state
21521 machine. */
21522 const file_entry *fe = state_machine.current_file ();
21523
21524 if (fe != NULL)
21525 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21526 }
21527
21528 /* Decode the table. */
21529 while (line_ptr < line_end && !end_sequence)
21530 {
21531 op_code = read_1_byte (abfd, line_ptr);
21532 line_ptr += 1;
21533
21534 if (op_code >= lh->opcode_base)
21535 {
21536 /* Special opcode. */
21537 state_machine.handle_special_opcode (op_code);
21538 }
21539 else switch (op_code)
21540 {
21541 case DW_LNS_extended_op:
21542 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21543 &bytes_read);
21544 line_ptr += bytes_read;
21545 extended_end = line_ptr + extended_len;
21546 extended_op = read_1_byte (abfd, line_ptr);
21547 line_ptr += 1;
21548 if (DW_LNE_lo_user <= extended_op
21549 && extended_op <= DW_LNE_hi_user)
21550 {
21551 /* Vendor extension, ignore. */
21552 line_ptr = extended_end;
21553 break;
21554 }
21555 switch (extended_op)
21556 {
21557 case DW_LNE_end_sequence:
21558 state_machine.handle_end_sequence ();
21559 end_sequence = true;
21560 break;
21561 case DW_LNE_set_address:
21562 {
21563 CORE_ADDR address
21564 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21565 line_ptr += bytes_read;
21566
21567 state_machine.check_line_address (cu, line_ptr,
21568 lowpc - baseaddr, address);
21569 state_machine.handle_set_address (baseaddr, address);
21570 }
21571 break;
21572 case DW_LNE_define_file:
21573 {
21574 const char *cur_file;
21575 unsigned int mod_time, length;
21576 dir_index dindex;
21577
21578 cur_file = read_direct_string (abfd, line_ptr,
21579 &bytes_read);
21580 line_ptr += bytes_read;
21581 dindex = (dir_index)
21582 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21583 line_ptr += bytes_read;
21584 mod_time =
21585 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21586 line_ptr += bytes_read;
21587 length =
21588 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21589 line_ptr += bytes_read;
21590 lh->add_file_name (cur_file, dindex, mod_time, length);
21591 }
21592 break;
21593 case DW_LNE_set_discriminator:
21594 {
21595 /* The discriminator is not interesting to the
21596 debugger; just ignore it. We still need to
21597 check its value though:
21598 if there are consecutive entries for the same
21599 (non-prologue) line we want to coalesce them.
21600 PR 17276. */
21601 unsigned int discr
21602 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21603 line_ptr += bytes_read;
21604
21605 state_machine.handle_set_discriminator (discr);
21606 }
21607 break;
21608 default:
21609 complaint (_("mangled .debug_line section"));
21610 return;
21611 }
21612 /* Make sure that we parsed the extended op correctly. If e.g.
21613 we expected a different address size than the producer used,
21614 we may have read the wrong number of bytes. */
21615 if (line_ptr != extended_end)
21616 {
21617 complaint (_("mangled .debug_line section"));
21618 return;
21619 }
21620 break;
21621 case DW_LNS_copy:
21622 state_machine.handle_copy ();
21623 break;
21624 case DW_LNS_advance_pc:
21625 {
21626 CORE_ADDR adjust
21627 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21628 line_ptr += bytes_read;
21629
21630 state_machine.handle_advance_pc (adjust);
21631 }
21632 break;
21633 case DW_LNS_advance_line:
21634 {
21635 int line_delta
21636 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21637 line_ptr += bytes_read;
21638
21639 state_machine.handle_advance_line (line_delta);
21640 }
21641 break;
21642 case DW_LNS_set_file:
21643 {
21644 file_name_index file
21645 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21646 &bytes_read);
21647 line_ptr += bytes_read;
21648
21649 state_machine.handle_set_file (file);
21650 }
21651 break;
21652 case DW_LNS_set_column:
21653 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21654 line_ptr += bytes_read;
21655 break;
21656 case DW_LNS_negate_stmt:
21657 state_machine.handle_negate_stmt ();
21658 break;
21659 case DW_LNS_set_basic_block:
21660 break;
21661 /* Add to the address register of the state machine the
21662 address increment value corresponding to special opcode
21663 255. I.e., this value is scaled by the minimum
21664 instruction length since special opcode 255 would have
21665 scaled the increment. */
21666 case DW_LNS_const_add_pc:
21667 state_machine.handle_const_add_pc ();
21668 break;
21669 case DW_LNS_fixed_advance_pc:
21670 {
21671 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21672 line_ptr += 2;
21673
21674 state_machine.handle_fixed_advance_pc (addr_adj);
21675 }
21676 break;
21677 default:
21678 {
21679 /* Unknown standard opcode, ignore it. */
21680 int i;
21681
21682 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21683 {
21684 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21685 line_ptr += bytes_read;
21686 }
21687 }
21688 }
21689 }
21690
21691 if (!end_sequence)
21692 dwarf2_debug_line_missing_end_sequence_complaint ();
21693
21694 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21695 in which case we still finish recording the last line). */
21696 state_machine.record_line (true);
21697 }
21698 }
21699
21700 /* Decode the Line Number Program (LNP) for the given line_header
21701 structure and CU. The actual information extracted and the type
21702 of structures created from the LNP depends on the value of PST.
21703
21704 1. If PST is NULL, then this procedure uses the data from the program
21705 to create all necessary symbol tables, and their linetables.
21706
21707 2. If PST is not NULL, this procedure reads the program to determine
21708 the list of files included by the unit represented by PST, and
21709 builds all the associated partial symbol tables.
21710
21711 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21712 It is used for relative paths in the line table.
21713 NOTE: When processing partial symtabs (pst != NULL),
21714 comp_dir == pst->dirname.
21715
21716 NOTE: It is important that psymtabs have the same file name (via strcmp)
21717 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21718 symtab we don't use it in the name of the psymtabs we create.
21719 E.g. expand_line_sal requires this when finding psymtabs to expand.
21720 A good testcase for this is mb-inline.exp.
21721
21722 LOWPC is the lowest address in CU (or 0 if not known).
21723
21724 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21725 for its PC<->lines mapping information. Otherwise only the filename
21726 table is read in. */
21727
21728 static void
21729 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21730 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21731 CORE_ADDR lowpc, int decode_mapping)
21732 {
21733 struct objfile *objfile = cu->per_objfile->objfile;
21734 const int decode_for_pst_p = (pst != NULL);
21735
21736 if (decode_mapping)
21737 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21738
21739 if (decode_for_pst_p)
21740 {
21741 /* Now that we're done scanning the Line Header Program, we can
21742 create the psymtab of each included file. */
21743 for (auto &file_entry : lh->file_names ())
21744 if (file_entry.included_p == 1)
21745 {
21746 gdb::unique_xmalloc_ptr<char> name_holder;
21747 const char *include_name =
21748 psymtab_include_file_name (lh, file_entry, pst,
21749 comp_dir, &name_holder);
21750 if (include_name != NULL)
21751 dwarf2_create_include_psymtab
21752 (cu->per_objfile->per_bfd, include_name, pst,
21753 cu->per_objfile->per_bfd->partial_symtabs.get (),
21754 objfile->per_bfd);
21755 }
21756 }
21757 else
21758 {
21759 /* Make sure a symtab is created for every file, even files
21760 which contain only variables (i.e. no code with associated
21761 line numbers). */
21762 buildsym_compunit *builder = cu->get_builder ();
21763 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21764
21765 for (auto &fe : lh->file_names ())
21766 {
21767 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21768 if (builder->get_current_subfile ()->symtab == NULL)
21769 {
21770 builder->get_current_subfile ()->symtab
21771 = allocate_symtab (cust,
21772 builder->get_current_subfile ()->name);
21773 }
21774 fe.symtab = builder->get_current_subfile ()->symtab;
21775 }
21776 }
21777 }
21778
21779 /* Start a subfile for DWARF. FILENAME is the name of the file and
21780 DIRNAME the name of the source directory which contains FILENAME
21781 or NULL if not known.
21782 This routine tries to keep line numbers from identical absolute and
21783 relative file names in a common subfile.
21784
21785 Using the `list' example from the GDB testsuite, which resides in
21786 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21787 of /srcdir/list0.c yields the following debugging information for list0.c:
21788
21789 DW_AT_name: /srcdir/list0.c
21790 DW_AT_comp_dir: /compdir
21791 files.files[0].name: list0.h
21792 files.files[0].dir: /srcdir
21793 files.files[1].name: list0.c
21794 files.files[1].dir: /srcdir
21795
21796 The line number information for list0.c has to end up in a single
21797 subfile, so that `break /srcdir/list0.c:1' works as expected.
21798 start_subfile will ensure that this happens provided that we pass the
21799 concatenation of files.files[1].dir and files.files[1].name as the
21800 subfile's name. */
21801
21802 static void
21803 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21804 const char *dirname)
21805 {
21806 gdb::unique_xmalloc_ptr<char> copy;
21807
21808 /* In order not to lose the line information directory,
21809 we concatenate it to the filename when it makes sense.
21810 Note that the Dwarf3 standard says (speaking of filenames in line
21811 information): ``The directory index is ignored for file names
21812 that represent full path names''. Thus ignoring dirname in the
21813 `else' branch below isn't an issue. */
21814
21815 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21816 {
21817 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21818 filename = copy.get ();
21819 }
21820
21821 cu->get_builder ()->start_subfile (filename);
21822 }
21823
21824 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21825 buildsym_compunit constructor. */
21826
21827 struct compunit_symtab *
21828 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21829 CORE_ADDR low_pc)
21830 {
21831 gdb_assert (m_builder == nullptr);
21832
21833 m_builder.reset (new struct buildsym_compunit
21834 (this->per_objfile->objfile,
21835 name, comp_dir, language, low_pc));
21836
21837 list_in_scope = get_builder ()->get_file_symbols ();
21838
21839 get_builder ()->record_debugformat ("DWARF 2");
21840 get_builder ()->record_producer (producer);
21841
21842 processing_has_namespace_info = false;
21843
21844 return get_builder ()->get_compunit_symtab ();
21845 }
21846
21847 static void
21848 var_decode_location (struct attribute *attr, struct symbol *sym,
21849 struct dwarf2_cu *cu)
21850 {
21851 struct objfile *objfile = cu->per_objfile->objfile;
21852 struct comp_unit_head *cu_header = &cu->header;
21853
21854 /* NOTE drow/2003-01-30: There used to be a comment and some special
21855 code here to turn a symbol with DW_AT_external and a
21856 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21857 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21858 with some versions of binutils) where shared libraries could have
21859 relocations against symbols in their debug information - the
21860 minimal symbol would have the right address, but the debug info
21861 would not. It's no longer necessary, because we will explicitly
21862 apply relocations when we read in the debug information now. */
21863
21864 /* A DW_AT_location attribute with no contents indicates that a
21865 variable has been optimized away. */
21866 if (attr->form_is_block () && attr->as_block ()->size == 0)
21867 {
21868 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21869 return;
21870 }
21871
21872 /* Handle one degenerate form of location expression specially, to
21873 preserve GDB's previous behavior when section offsets are
21874 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21875 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21876
21877 if (attr->form_is_block ())
21878 {
21879 struct dwarf_block *block = attr->as_block ();
21880
21881 if ((block->data[0] == DW_OP_addr
21882 && block->size == 1 + cu_header->addr_size)
21883 || ((block->data[0] == DW_OP_GNU_addr_index
21884 || block->data[0] == DW_OP_addrx)
21885 && (block->size
21886 == 1 + leb128_size (&block->data[1]))))
21887 {
21888 unsigned int dummy;
21889
21890 if (block->data[0] == DW_OP_addr)
21891 SET_SYMBOL_VALUE_ADDRESS
21892 (sym, cu->header.read_address (objfile->obfd,
21893 block->data + 1,
21894 &dummy));
21895 else
21896 SET_SYMBOL_VALUE_ADDRESS
21897 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21898 &dummy));
21899 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21900 fixup_symbol_section (sym, objfile);
21901 SET_SYMBOL_VALUE_ADDRESS
21902 (sym,
21903 SYMBOL_VALUE_ADDRESS (sym)
21904 + objfile->section_offsets[sym->section_index ()]);
21905 return;
21906 }
21907 }
21908
21909 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21910 expression evaluator, and use LOC_COMPUTED only when necessary
21911 (i.e. when the value of a register or memory location is
21912 referenced, or a thread-local block, etc.). Then again, it might
21913 not be worthwhile. I'm assuming that it isn't unless performance
21914 or memory numbers show me otherwise. */
21915
21916 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21917
21918 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21919 cu->has_loclist = true;
21920 }
21921
21922 /* Given a pointer to a DWARF information entry, figure out if we need
21923 to make a symbol table entry for it, and if so, create a new entry
21924 and return a pointer to it.
21925 If TYPE is NULL, determine symbol type from the die, otherwise
21926 used the passed type.
21927 If SPACE is not NULL, use it to hold the new symbol. If it is
21928 NULL, allocate a new symbol on the objfile's obstack. */
21929
21930 static struct symbol *
21931 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21932 struct symbol *space)
21933 {
21934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21935 struct objfile *objfile = per_objfile->objfile;
21936 struct gdbarch *gdbarch = objfile->arch ();
21937 struct symbol *sym = NULL;
21938 const char *name;
21939 struct attribute *attr = NULL;
21940 struct attribute *attr2 = NULL;
21941 CORE_ADDR baseaddr;
21942 struct pending **list_to_add = NULL;
21943
21944 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21945
21946 baseaddr = objfile->text_section_offset ();
21947
21948 name = dwarf2_name (die, cu);
21949 if (name)
21950 {
21951 int suppress_add = 0;
21952
21953 if (space)
21954 sym = space;
21955 else
21956 sym = new (&objfile->objfile_obstack) symbol;
21957 OBJSTAT (objfile, n_syms++);
21958
21959 /* Cache this symbol's name and the name's demangled form (if any). */
21960 sym->set_language (cu->language, &objfile->objfile_obstack);
21961 /* Fortran does not have mangling standard and the mangling does differ
21962 between gfortran, iFort etc. */
21963 const char *physname
21964 = (cu->language == language_fortran
21965 ? dwarf2_full_name (name, die, cu)
21966 : dwarf2_physname (name, die, cu));
21967 const char *linkagename = dw2_linkage_name (die, cu);
21968
21969 if (linkagename == nullptr || cu->language == language_ada)
21970 sym->set_linkage_name (physname);
21971 else
21972 {
21973 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21974 sym->set_linkage_name (linkagename);
21975 }
21976
21977 /* Default assumptions.
21978 Use the passed type or decode it from the die. */
21979 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21980 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21981 if (type != NULL)
21982 SYMBOL_TYPE (sym) = type;
21983 else
21984 SYMBOL_TYPE (sym) = die_type (die, cu);
21985 attr = dwarf2_attr (die,
21986 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21987 cu);
21988 if (attr != nullptr)
21989 SYMBOL_LINE (sym) = attr->constant_value (0);
21990
21991 attr = dwarf2_attr (die,
21992 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21993 cu);
21994 if (attr != nullptr && attr->is_nonnegative ())
21995 {
21996 file_name_index file_index
21997 = (file_name_index) attr->as_nonnegative ();
21998 struct file_entry *fe;
21999
22000 if (cu->line_header != NULL)
22001 fe = cu->line_header->file_name_at (file_index);
22002 else
22003 fe = NULL;
22004
22005 if (fe == NULL)
22006 complaint (_("file index out of range"));
22007 else
22008 symbol_set_symtab (sym, fe->symtab);
22009 }
22010
22011 switch (die->tag)
22012 {
22013 case DW_TAG_label:
22014 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22015 if (attr != nullptr)
22016 {
22017 CORE_ADDR addr;
22018
22019 addr = attr->as_address ();
22020 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22021 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22022 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22023 }
22024 else
22025 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22026 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22027 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22028 add_symbol_to_list (sym, cu->list_in_scope);
22029 break;
22030 case DW_TAG_subprogram:
22031 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22032 finish_block. */
22033 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22034 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22035 if ((attr2 != nullptr && attr2->as_boolean ())
22036 || cu->language == language_ada
22037 || cu->language == language_fortran)
22038 {
22039 /* Subprograms marked external are stored as a global symbol.
22040 Ada and Fortran subprograms, whether marked external or
22041 not, are always stored as a global symbol, because we want
22042 to be able to access them globally. For instance, we want
22043 to be able to break on a nested subprogram without having
22044 to specify the context. */
22045 list_to_add = cu->get_builder ()->get_global_symbols ();
22046 }
22047 else
22048 {
22049 list_to_add = cu->list_in_scope;
22050 }
22051 break;
22052 case DW_TAG_inlined_subroutine:
22053 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22054 finish_block. */
22055 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22056 SYMBOL_INLINED (sym) = 1;
22057 list_to_add = cu->list_in_scope;
22058 break;
22059 case DW_TAG_template_value_param:
22060 suppress_add = 1;
22061 /* Fall through. */
22062 case DW_TAG_constant:
22063 case DW_TAG_variable:
22064 case DW_TAG_member:
22065 /* Compilation with minimal debug info may result in
22066 variables with missing type entries. Change the
22067 misleading `void' type to something sensible. */
22068 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22069 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22070
22071 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22072 /* In the case of DW_TAG_member, we should only be called for
22073 static const members. */
22074 if (die->tag == DW_TAG_member)
22075 {
22076 /* dwarf2_add_field uses die_is_declaration,
22077 so we do the same. */
22078 gdb_assert (die_is_declaration (die, cu));
22079 gdb_assert (attr);
22080 }
22081 if (attr != nullptr)
22082 {
22083 dwarf2_const_value (attr, sym, cu);
22084 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22085 if (!suppress_add)
22086 {
22087 if (attr2 != nullptr && attr2->as_boolean ())
22088 list_to_add = cu->get_builder ()->get_global_symbols ();
22089 else
22090 list_to_add = cu->list_in_scope;
22091 }
22092 break;
22093 }
22094 attr = dwarf2_attr (die, DW_AT_location, cu);
22095 if (attr != nullptr)
22096 {
22097 var_decode_location (attr, sym, cu);
22098 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22099
22100 /* Fortran explicitly imports any global symbols to the local
22101 scope by DW_TAG_common_block. */
22102 if (cu->language == language_fortran && die->parent
22103 && die->parent->tag == DW_TAG_common_block)
22104 attr2 = NULL;
22105
22106 if (SYMBOL_CLASS (sym) == LOC_STATIC
22107 && SYMBOL_VALUE_ADDRESS (sym) == 0
22108 && !per_objfile->per_bfd->has_section_at_zero)
22109 {
22110 /* When a static variable is eliminated by the linker,
22111 the corresponding debug information is not stripped
22112 out, but the variable address is set to null;
22113 do not add such variables into symbol table. */
22114 }
22115 else if (attr2 != nullptr && attr2->as_boolean ())
22116 {
22117 if (SYMBOL_CLASS (sym) == LOC_STATIC
22118 && (objfile->flags & OBJF_MAINLINE) == 0
22119 && per_objfile->per_bfd->can_copy)
22120 {
22121 /* A global static variable might be subject to
22122 copy relocation. We first check for a local
22123 minsym, though, because maybe the symbol was
22124 marked hidden, in which case this would not
22125 apply. */
22126 bound_minimal_symbol found
22127 = (lookup_minimal_symbol_linkage
22128 (sym->linkage_name (), objfile));
22129 if (found.minsym != nullptr)
22130 sym->maybe_copied = 1;
22131 }
22132
22133 /* A variable with DW_AT_external is never static,
22134 but it may be block-scoped. */
22135 list_to_add
22136 = ((cu->list_in_scope
22137 == cu->get_builder ()->get_file_symbols ())
22138 ? cu->get_builder ()->get_global_symbols ()
22139 : cu->list_in_scope);
22140 }
22141 else
22142 list_to_add = cu->list_in_scope;
22143 }
22144 else
22145 {
22146 /* We do not know the address of this symbol.
22147 If it is an external symbol and we have type information
22148 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22149 The address of the variable will then be determined from
22150 the minimal symbol table whenever the variable is
22151 referenced. */
22152 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22153
22154 /* Fortran explicitly imports any global symbols to the local
22155 scope by DW_TAG_common_block. */
22156 if (cu->language == language_fortran && die->parent
22157 && die->parent->tag == DW_TAG_common_block)
22158 {
22159 /* SYMBOL_CLASS doesn't matter here because
22160 read_common_block is going to reset it. */
22161 if (!suppress_add)
22162 list_to_add = cu->list_in_scope;
22163 }
22164 else if (attr2 != nullptr && attr2->as_boolean ()
22165 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22166 {
22167 /* A variable with DW_AT_external is never static, but it
22168 may be block-scoped. */
22169 list_to_add
22170 = ((cu->list_in_scope
22171 == cu->get_builder ()->get_file_symbols ())
22172 ? cu->get_builder ()->get_global_symbols ()
22173 : cu->list_in_scope);
22174
22175 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22176 }
22177 else if (!die_is_declaration (die, cu))
22178 {
22179 /* Use the default LOC_OPTIMIZED_OUT class. */
22180 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22181 if (!suppress_add)
22182 list_to_add = cu->list_in_scope;
22183 }
22184 }
22185 break;
22186 case DW_TAG_formal_parameter:
22187 {
22188 /* If we are inside a function, mark this as an argument. If
22189 not, we might be looking at an argument to an inlined function
22190 when we do not have enough information to show inlined frames;
22191 pretend it's a local variable in that case so that the user can
22192 still see it. */
22193 struct context_stack *curr
22194 = cu->get_builder ()->get_current_context_stack ();
22195 if (curr != nullptr && curr->name != nullptr)
22196 SYMBOL_IS_ARGUMENT (sym) = 1;
22197 attr = dwarf2_attr (die, DW_AT_location, cu);
22198 if (attr != nullptr)
22199 {
22200 var_decode_location (attr, sym, cu);
22201 }
22202 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22203 if (attr != nullptr)
22204 {
22205 dwarf2_const_value (attr, sym, cu);
22206 }
22207
22208 list_to_add = cu->list_in_scope;
22209 }
22210 break;
22211 case DW_TAG_unspecified_parameters:
22212 /* From varargs functions; gdb doesn't seem to have any
22213 interest in this information, so just ignore it for now.
22214 (FIXME?) */
22215 break;
22216 case DW_TAG_template_type_param:
22217 suppress_add = 1;
22218 /* Fall through. */
22219 case DW_TAG_class_type:
22220 case DW_TAG_interface_type:
22221 case DW_TAG_structure_type:
22222 case DW_TAG_union_type:
22223 case DW_TAG_set_type:
22224 case DW_TAG_enumeration_type:
22225 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22226 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22227
22228 {
22229 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22230 really ever be static objects: otherwise, if you try
22231 to, say, break of a class's method and you're in a file
22232 which doesn't mention that class, it won't work unless
22233 the check for all static symbols in lookup_symbol_aux
22234 saves you. See the OtherFileClass tests in
22235 gdb.c++/namespace.exp. */
22236
22237 if (!suppress_add)
22238 {
22239 buildsym_compunit *builder = cu->get_builder ();
22240 list_to_add
22241 = (cu->list_in_scope == builder->get_file_symbols ()
22242 && cu->language == language_cplus
22243 ? builder->get_global_symbols ()
22244 : cu->list_in_scope);
22245
22246 /* The semantics of C++ state that "struct foo {
22247 ... }" also defines a typedef for "foo". */
22248 if (cu->language == language_cplus
22249 || cu->language == language_ada
22250 || cu->language == language_d
22251 || cu->language == language_rust)
22252 {
22253 /* The symbol's name is already allocated along
22254 with this objfile, so we don't need to
22255 duplicate it for the type. */
22256 if (SYMBOL_TYPE (sym)->name () == 0)
22257 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22258 }
22259 }
22260 }
22261 break;
22262 case DW_TAG_typedef:
22263 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22264 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22265 list_to_add = cu->list_in_scope;
22266 break;
22267 case DW_TAG_array_type:
22268 case DW_TAG_base_type:
22269 case DW_TAG_subrange_type:
22270 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22271 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22272 list_to_add = cu->list_in_scope;
22273 break;
22274 case DW_TAG_enumerator:
22275 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22276 if (attr != nullptr)
22277 {
22278 dwarf2_const_value (attr, sym, cu);
22279 }
22280 {
22281 /* NOTE: carlton/2003-11-10: See comment above in the
22282 DW_TAG_class_type, etc. block. */
22283
22284 list_to_add
22285 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22286 && cu->language == language_cplus
22287 ? cu->get_builder ()->get_global_symbols ()
22288 : cu->list_in_scope);
22289 }
22290 break;
22291 case DW_TAG_imported_declaration:
22292 case DW_TAG_namespace:
22293 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22294 list_to_add = cu->get_builder ()->get_global_symbols ();
22295 break;
22296 case DW_TAG_module:
22297 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22298 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22299 list_to_add = cu->get_builder ()->get_global_symbols ();
22300 break;
22301 case DW_TAG_common_block:
22302 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22303 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22304 add_symbol_to_list (sym, cu->list_in_scope);
22305 break;
22306 default:
22307 /* Not a tag we recognize. Hopefully we aren't processing
22308 trash data, but since we must specifically ignore things
22309 we don't recognize, there is nothing else we should do at
22310 this point. */
22311 complaint (_("unsupported tag: '%s'"),
22312 dwarf_tag_name (die->tag));
22313 break;
22314 }
22315
22316 if (suppress_add)
22317 {
22318 sym->hash_next = objfile->template_symbols;
22319 objfile->template_symbols = sym;
22320 list_to_add = NULL;
22321 }
22322
22323 if (list_to_add != NULL)
22324 add_symbol_to_list (sym, list_to_add);
22325
22326 /* For the benefit of old versions of GCC, check for anonymous
22327 namespaces based on the demangled name. */
22328 if (!cu->processing_has_namespace_info
22329 && cu->language == language_cplus)
22330 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22331 }
22332 return (sym);
22333 }
22334
22335 /* Given an attr with a DW_FORM_dataN value in host byte order,
22336 zero-extend it as appropriate for the symbol's type. The DWARF
22337 standard (v4) is not entirely clear about the meaning of using
22338 DW_FORM_dataN for a constant with a signed type, where the type is
22339 wider than the data. The conclusion of a discussion on the DWARF
22340 list was that this is unspecified. We choose to always zero-extend
22341 because that is the interpretation long in use by GCC. */
22342
22343 static gdb_byte *
22344 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22345 struct dwarf2_cu *cu, LONGEST *value, int bits)
22346 {
22347 struct objfile *objfile = cu->per_objfile->objfile;
22348 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22349 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22350 LONGEST l = attr->constant_value (0);
22351
22352 if (bits < sizeof (*value) * 8)
22353 {
22354 l &= ((LONGEST) 1 << bits) - 1;
22355 *value = l;
22356 }
22357 else if (bits == sizeof (*value) * 8)
22358 *value = l;
22359 else
22360 {
22361 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22362 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22363 return bytes;
22364 }
22365
22366 return NULL;
22367 }
22368
22369 /* Read a constant value from an attribute. Either set *VALUE, or if
22370 the value does not fit in *VALUE, set *BYTES - either already
22371 allocated on the objfile obstack, or newly allocated on OBSTACK,
22372 or, set *BATON, if we translated the constant to a location
22373 expression. */
22374
22375 static void
22376 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22377 const char *name, struct obstack *obstack,
22378 struct dwarf2_cu *cu,
22379 LONGEST *value, const gdb_byte **bytes,
22380 struct dwarf2_locexpr_baton **baton)
22381 {
22382 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22383 struct objfile *objfile = per_objfile->objfile;
22384 struct comp_unit_head *cu_header = &cu->header;
22385 struct dwarf_block *blk;
22386 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22387 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22388
22389 *value = 0;
22390 *bytes = NULL;
22391 *baton = NULL;
22392
22393 switch (attr->form)
22394 {
22395 case DW_FORM_addr:
22396 case DW_FORM_addrx:
22397 case DW_FORM_GNU_addr_index:
22398 {
22399 gdb_byte *data;
22400
22401 if (TYPE_LENGTH (type) != cu_header->addr_size)
22402 dwarf2_const_value_length_mismatch_complaint (name,
22403 cu_header->addr_size,
22404 TYPE_LENGTH (type));
22405 /* Symbols of this form are reasonably rare, so we just
22406 piggyback on the existing location code rather than writing
22407 a new implementation of symbol_computed_ops. */
22408 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22409 (*baton)->per_objfile = per_objfile;
22410 (*baton)->per_cu = cu->per_cu;
22411 gdb_assert ((*baton)->per_cu);
22412
22413 (*baton)->size = 2 + cu_header->addr_size;
22414 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22415 (*baton)->data = data;
22416
22417 data[0] = DW_OP_addr;
22418 store_unsigned_integer (&data[1], cu_header->addr_size,
22419 byte_order, attr->as_address ());
22420 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22421 }
22422 break;
22423 case DW_FORM_string:
22424 case DW_FORM_strp:
22425 case DW_FORM_strx:
22426 case DW_FORM_GNU_str_index:
22427 case DW_FORM_GNU_strp_alt:
22428 /* The string is already allocated on the objfile obstack, point
22429 directly to it. */
22430 *bytes = (const gdb_byte *) attr->as_string ();
22431 break;
22432 case DW_FORM_block1:
22433 case DW_FORM_block2:
22434 case DW_FORM_block4:
22435 case DW_FORM_block:
22436 case DW_FORM_exprloc:
22437 case DW_FORM_data16:
22438 blk = attr->as_block ();
22439 if (TYPE_LENGTH (type) != blk->size)
22440 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22441 TYPE_LENGTH (type));
22442 *bytes = blk->data;
22443 break;
22444
22445 /* The DW_AT_const_value attributes are supposed to carry the
22446 symbol's value "represented as it would be on the target
22447 architecture." By the time we get here, it's already been
22448 converted to host endianness, so we just need to sign- or
22449 zero-extend it as appropriate. */
22450 case DW_FORM_data1:
22451 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22452 break;
22453 case DW_FORM_data2:
22454 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22455 break;
22456 case DW_FORM_data4:
22457 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22458 break;
22459 case DW_FORM_data8:
22460 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22461 break;
22462
22463 case DW_FORM_sdata:
22464 case DW_FORM_implicit_const:
22465 *value = attr->as_signed ();
22466 break;
22467
22468 case DW_FORM_udata:
22469 *value = attr->as_unsigned ();
22470 break;
22471
22472 default:
22473 complaint (_("unsupported const value attribute form: '%s'"),
22474 dwarf_form_name (attr->form));
22475 *value = 0;
22476 break;
22477 }
22478 }
22479
22480
22481 /* Copy constant value from an attribute to a symbol. */
22482
22483 static void
22484 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22485 struct dwarf2_cu *cu)
22486 {
22487 struct objfile *objfile = cu->per_objfile->objfile;
22488 LONGEST value;
22489 const gdb_byte *bytes;
22490 struct dwarf2_locexpr_baton *baton;
22491
22492 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22493 sym->print_name (),
22494 &objfile->objfile_obstack, cu,
22495 &value, &bytes, &baton);
22496
22497 if (baton != NULL)
22498 {
22499 SYMBOL_LOCATION_BATON (sym) = baton;
22500 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22501 }
22502 else if (bytes != NULL)
22503 {
22504 SYMBOL_VALUE_BYTES (sym) = bytes;
22505 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22506 }
22507 else
22508 {
22509 SYMBOL_VALUE (sym) = value;
22510 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22511 }
22512 }
22513
22514 /* Return the type of the die in question using its DW_AT_type attribute. */
22515
22516 static struct type *
22517 die_type (struct die_info *die, struct dwarf2_cu *cu)
22518 {
22519 struct attribute *type_attr;
22520
22521 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22522 if (!type_attr)
22523 {
22524 struct objfile *objfile = cu->per_objfile->objfile;
22525 /* A missing DW_AT_type represents a void type. */
22526 return objfile_type (objfile)->builtin_void;
22527 }
22528
22529 return lookup_die_type (die, type_attr, cu);
22530 }
22531
22532 /* True iff CU's producer generates GNAT Ada auxiliary information
22533 that allows to find parallel types through that information instead
22534 of having to do expensive parallel lookups by type name. */
22535
22536 static int
22537 need_gnat_info (struct dwarf2_cu *cu)
22538 {
22539 /* Assume that the Ada compiler was GNAT, which always produces
22540 the auxiliary information. */
22541 return (cu->language == language_ada);
22542 }
22543
22544 /* Return the auxiliary type of the die in question using its
22545 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22546 attribute is not present. */
22547
22548 static struct type *
22549 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22550 {
22551 struct attribute *type_attr;
22552
22553 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22554 if (!type_attr)
22555 return NULL;
22556
22557 return lookup_die_type (die, type_attr, cu);
22558 }
22559
22560 /* If DIE has a descriptive_type attribute, then set the TYPE's
22561 descriptive type accordingly. */
22562
22563 static void
22564 set_descriptive_type (struct type *type, struct die_info *die,
22565 struct dwarf2_cu *cu)
22566 {
22567 struct type *descriptive_type = die_descriptive_type (die, cu);
22568
22569 if (descriptive_type)
22570 {
22571 ALLOCATE_GNAT_AUX_TYPE (type);
22572 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22573 }
22574 }
22575
22576 /* Return the containing type of the die in question using its
22577 DW_AT_containing_type attribute. */
22578
22579 static struct type *
22580 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22581 {
22582 struct attribute *type_attr;
22583 struct objfile *objfile = cu->per_objfile->objfile;
22584
22585 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22586 if (!type_attr)
22587 error (_("Dwarf Error: Problem turning containing type into gdb type "
22588 "[in module %s]"), objfile_name (objfile));
22589
22590 return lookup_die_type (die, type_attr, cu);
22591 }
22592
22593 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22594
22595 static struct type *
22596 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22597 {
22598 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22599 struct objfile *objfile = per_objfile->objfile;
22600 char *saved;
22601
22602 std::string message
22603 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22604 objfile_name (objfile),
22605 sect_offset_str (cu->header.sect_off),
22606 sect_offset_str (die->sect_off));
22607 saved = obstack_strdup (&objfile->objfile_obstack, message);
22608
22609 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22610 }
22611
22612 /* Look up the type of DIE in CU using its type attribute ATTR.
22613 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22614 DW_AT_containing_type.
22615 If there is no type substitute an error marker. */
22616
22617 static struct type *
22618 lookup_die_type (struct die_info *die, const struct attribute *attr,
22619 struct dwarf2_cu *cu)
22620 {
22621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22622 struct objfile *objfile = per_objfile->objfile;
22623 struct type *this_type;
22624
22625 gdb_assert (attr->name == DW_AT_type
22626 || attr->name == DW_AT_GNAT_descriptive_type
22627 || attr->name == DW_AT_containing_type);
22628
22629 /* First see if we have it cached. */
22630
22631 if (attr->form == DW_FORM_GNU_ref_alt)
22632 {
22633 struct dwarf2_per_cu_data *per_cu;
22634 sect_offset sect_off = attr->get_ref_die_offset ();
22635
22636 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22637 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22638 }
22639 else if (attr->form_is_ref ())
22640 {
22641 sect_offset sect_off = attr->get_ref_die_offset ();
22642
22643 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22644 }
22645 else if (attr->form == DW_FORM_ref_sig8)
22646 {
22647 ULONGEST signature = attr->as_signature ();
22648
22649 return get_signatured_type (die, signature, cu);
22650 }
22651 else
22652 {
22653 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22654 " at %s [in module %s]"),
22655 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22656 objfile_name (objfile));
22657 return build_error_marker_type (cu, die);
22658 }
22659
22660 /* If not cached we need to read it in. */
22661
22662 if (this_type == NULL)
22663 {
22664 struct die_info *type_die = NULL;
22665 struct dwarf2_cu *type_cu = cu;
22666
22667 if (attr->form_is_ref ())
22668 type_die = follow_die_ref (die, attr, &type_cu);
22669 if (type_die == NULL)
22670 return build_error_marker_type (cu, die);
22671 /* If we find the type now, it's probably because the type came
22672 from an inter-CU reference and the type's CU got expanded before
22673 ours. */
22674 this_type = read_type_die (type_die, type_cu);
22675 }
22676
22677 /* If we still don't have a type use an error marker. */
22678
22679 if (this_type == NULL)
22680 return build_error_marker_type (cu, die);
22681
22682 return this_type;
22683 }
22684
22685 /* Return the type in DIE, CU.
22686 Returns NULL for invalid types.
22687
22688 This first does a lookup in die_type_hash,
22689 and only reads the die in if necessary.
22690
22691 NOTE: This can be called when reading in partial or full symbols. */
22692
22693 static struct type *
22694 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22695 {
22696 struct type *this_type;
22697
22698 this_type = get_die_type (die, cu);
22699 if (this_type)
22700 return this_type;
22701
22702 return read_type_die_1 (die, cu);
22703 }
22704
22705 /* Read the type in DIE, CU.
22706 Returns NULL for invalid types. */
22707
22708 static struct type *
22709 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22710 {
22711 struct type *this_type = NULL;
22712
22713 switch (die->tag)
22714 {
22715 case DW_TAG_class_type:
22716 case DW_TAG_interface_type:
22717 case DW_TAG_structure_type:
22718 case DW_TAG_union_type:
22719 this_type = read_structure_type (die, cu);
22720 break;
22721 case DW_TAG_enumeration_type:
22722 this_type = read_enumeration_type (die, cu);
22723 break;
22724 case DW_TAG_subprogram:
22725 case DW_TAG_subroutine_type:
22726 case DW_TAG_inlined_subroutine:
22727 this_type = read_subroutine_type (die, cu);
22728 break;
22729 case DW_TAG_array_type:
22730 this_type = read_array_type (die, cu);
22731 break;
22732 case DW_TAG_set_type:
22733 this_type = read_set_type (die, cu);
22734 break;
22735 case DW_TAG_pointer_type:
22736 this_type = read_tag_pointer_type (die, cu);
22737 break;
22738 case DW_TAG_ptr_to_member_type:
22739 this_type = read_tag_ptr_to_member_type (die, cu);
22740 break;
22741 case DW_TAG_reference_type:
22742 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22743 break;
22744 case DW_TAG_rvalue_reference_type:
22745 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22746 break;
22747 case DW_TAG_const_type:
22748 this_type = read_tag_const_type (die, cu);
22749 break;
22750 case DW_TAG_volatile_type:
22751 this_type = read_tag_volatile_type (die, cu);
22752 break;
22753 case DW_TAG_restrict_type:
22754 this_type = read_tag_restrict_type (die, cu);
22755 break;
22756 case DW_TAG_string_type:
22757 this_type = read_tag_string_type (die, cu);
22758 break;
22759 case DW_TAG_typedef:
22760 this_type = read_typedef (die, cu);
22761 break;
22762 case DW_TAG_subrange_type:
22763 this_type = read_subrange_type (die, cu);
22764 break;
22765 case DW_TAG_base_type:
22766 this_type = read_base_type (die, cu);
22767 break;
22768 case DW_TAG_unspecified_type:
22769 this_type = read_unspecified_type (die, cu);
22770 break;
22771 case DW_TAG_namespace:
22772 this_type = read_namespace_type (die, cu);
22773 break;
22774 case DW_TAG_module:
22775 this_type = read_module_type (die, cu);
22776 break;
22777 case DW_TAG_atomic_type:
22778 this_type = read_tag_atomic_type (die, cu);
22779 break;
22780 default:
22781 complaint (_("unexpected tag in read_type_die: '%s'"),
22782 dwarf_tag_name (die->tag));
22783 break;
22784 }
22785
22786 return this_type;
22787 }
22788
22789 /* See if we can figure out if the class lives in a namespace. We do
22790 this by looking for a member function; its demangled name will
22791 contain namespace info, if there is any.
22792 Return the computed name or NULL.
22793 Space for the result is allocated on the objfile's obstack.
22794 This is the full-die version of guess_partial_die_structure_name.
22795 In this case we know DIE has no useful parent. */
22796
22797 static const char *
22798 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22799 {
22800 struct die_info *spec_die;
22801 struct dwarf2_cu *spec_cu;
22802 struct die_info *child;
22803 struct objfile *objfile = cu->per_objfile->objfile;
22804
22805 spec_cu = cu;
22806 spec_die = die_specification (die, &spec_cu);
22807 if (spec_die != NULL)
22808 {
22809 die = spec_die;
22810 cu = spec_cu;
22811 }
22812
22813 for (child = die->child;
22814 child != NULL;
22815 child = child->sibling)
22816 {
22817 if (child->tag == DW_TAG_subprogram)
22818 {
22819 const char *linkage_name = dw2_linkage_name (child, cu);
22820
22821 if (linkage_name != NULL)
22822 {
22823 gdb::unique_xmalloc_ptr<char> actual_name
22824 (cu->language_defn->class_name_from_physname (linkage_name));
22825 const char *name = NULL;
22826
22827 if (actual_name != NULL)
22828 {
22829 const char *die_name = dwarf2_name (die, cu);
22830
22831 if (die_name != NULL
22832 && strcmp (die_name, actual_name.get ()) != 0)
22833 {
22834 /* Strip off the class name from the full name.
22835 We want the prefix. */
22836 int die_name_len = strlen (die_name);
22837 int actual_name_len = strlen (actual_name.get ());
22838 const char *ptr = actual_name.get ();
22839
22840 /* Test for '::' as a sanity check. */
22841 if (actual_name_len > die_name_len + 2
22842 && ptr[actual_name_len - die_name_len - 1] == ':')
22843 name = obstack_strndup (
22844 &objfile->per_bfd->storage_obstack,
22845 ptr, actual_name_len - die_name_len - 2);
22846 }
22847 }
22848 return name;
22849 }
22850 }
22851 }
22852
22853 return NULL;
22854 }
22855
22856 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22857 prefix part in such case. See
22858 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22859
22860 static const char *
22861 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22862 {
22863 struct attribute *attr;
22864 const char *base;
22865
22866 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22867 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22868 return NULL;
22869
22870 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22871 return NULL;
22872
22873 attr = dw2_linkage_name_attr (die, cu);
22874 const char *attr_name = attr->as_string ();
22875 if (attr == NULL || attr_name == NULL)
22876 return NULL;
22877
22878 /* dwarf2_name had to be already called. */
22879 gdb_assert (attr->canonical_string_p ());
22880
22881 /* Strip the base name, keep any leading namespaces/classes. */
22882 base = strrchr (attr_name, ':');
22883 if (base == NULL || base == attr_name || base[-1] != ':')
22884 return "";
22885
22886 struct objfile *objfile = cu->per_objfile->objfile;
22887 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22888 attr_name,
22889 &base[-1] - attr_name);
22890 }
22891
22892 /* Return the name of the namespace/class that DIE is defined within,
22893 or "" if we can't tell. The caller should not xfree the result.
22894
22895 For example, if we're within the method foo() in the following
22896 code:
22897
22898 namespace N {
22899 class C {
22900 void foo () {
22901 }
22902 };
22903 }
22904
22905 then determine_prefix on foo's die will return "N::C". */
22906
22907 static const char *
22908 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22909 {
22910 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22911 struct die_info *parent, *spec_die;
22912 struct dwarf2_cu *spec_cu;
22913 struct type *parent_type;
22914 const char *retval;
22915
22916 if (cu->language != language_cplus
22917 && cu->language != language_fortran && cu->language != language_d
22918 && cu->language != language_rust)
22919 return "";
22920
22921 retval = anonymous_struct_prefix (die, cu);
22922 if (retval)
22923 return retval;
22924
22925 /* We have to be careful in the presence of DW_AT_specification.
22926 For example, with GCC 3.4, given the code
22927
22928 namespace N {
22929 void foo() {
22930 // Definition of N::foo.
22931 }
22932 }
22933
22934 then we'll have a tree of DIEs like this:
22935
22936 1: DW_TAG_compile_unit
22937 2: DW_TAG_namespace // N
22938 3: DW_TAG_subprogram // declaration of N::foo
22939 4: DW_TAG_subprogram // definition of N::foo
22940 DW_AT_specification // refers to die #3
22941
22942 Thus, when processing die #4, we have to pretend that we're in
22943 the context of its DW_AT_specification, namely the contex of die
22944 #3. */
22945 spec_cu = cu;
22946 spec_die = die_specification (die, &spec_cu);
22947 if (spec_die == NULL)
22948 parent = die->parent;
22949 else
22950 {
22951 parent = spec_die->parent;
22952 cu = spec_cu;
22953 }
22954
22955 if (parent == NULL)
22956 return "";
22957 else if (parent->building_fullname)
22958 {
22959 const char *name;
22960 const char *parent_name;
22961
22962 /* It has been seen on RealView 2.2 built binaries,
22963 DW_TAG_template_type_param types actually _defined_ as
22964 children of the parent class:
22965
22966 enum E {};
22967 template class <class Enum> Class{};
22968 Class<enum E> class_e;
22969
22970 1: DW_TAG_class_type (Class)
22971 2: DW_TAG_enumeration_type (E)
22972 3: DW_TAG_enumerator (enum1:0)
22973 3: DW_TAG_enumerator (enum2:1)
22974 ...
22975 2: DW_TAG_template_type_param
22976 DW_AT_type DW_FORM_ref_udata (E)
22977
22978 Besides being broken debug info, it can put GDB into an
22979 infinite loop. Consider:
22980
22981 When we're building the full name for Class<E>, we'll start
22982 at Class, and go look over its template type parameters,
22983 finding E. We'll then try to build the full name of E, and
22984 reach here. We're now trying to build the full name of E,
22985 and look over the parent DIE for containing scope. In the
22986 broken case, if we followed the parent DIE of E, we'd again
22987 find Class, and once again go look at its template type
22988 arguments, etc., etc. Simply don't consider such parent die
22989 as source-level parent of this die (it can't be, the language
22990 doesn't allow it), and break the loop here. */
22991 name = dwarf2_name (die, cu);
22992 parent_name = dwarf2_name (parent, cu);
22993 complaint (_("template param type '%s' defined within parent '%s'"),
22994 name ? name : "<unknown>",
22995 parent_name ? parent_name : "<unknown>");
22996 return "";
22997 }
22998 else
22999 switch (parent->tag)
23000 {
23001 case DW_TAG_namespace:
23002 parent_type = read_type_die (parent, cu);
23003 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23004 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23005 Work around this problem here. */
23006 if (cu->language == language_cplus
23007 && strcmp (parent_type->name (), "::") == 0)
23008 return "";
23009 /* We give a name to even anonymous namespaces. */
23010 return parent_type->name ();
23011 case DW_TAG_class_type:
23012 case DW_TAG_interface_type:
23013 case DW_TAG_structure_type:
23014 case DW_TAG_union_type:
23015 case DW_TAG_module:
23016 parent_type = read_type_die (parent, cu);
23017 if (parent_type->name () != NULL)
23018 return parent_type->name ();
23019 else
23020 /* An anonymous structure is only allowed non-static data
23021 members; no typedefs, no member functions, et cetera.
23022 So it does not need a prefix. */
23023 return "";
23024 case DW_TAG_compile_unit:
23025 case DW_TAG_partial_unit:
23026 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23027 if (cu->language == language_cplus
23028 && !per_objfile->per_bfd->types.empty ()
23029 && die->child != NULL
23030 && (die->tag == DW_TAG_class_type
23031 || die->tag == DW_TAG_structure_type
23032 || die->tag == DW_TAG_union_type))
23033 {
23034 const char *name = guess_full_die_structure_name (die, cu);
23035 if (name != NULL)
23036 return name;
23037 }
23038 return "";
23039 case DW_TAG_subprogram:
23040 /* Nested subroutines in Fortran get a prefix with the name
23041 of the parent's subroutine. */
23042 if (cu->language == language_fortran)
23043 {
23044 if ((die->tag == DW_TAG_subprogram)
23045 && (dwarf2_name (parent, cu) != NULL))
23046 return dwarf2_name (parent, cu);
23047 }
23048 return determine_prefix (parent, cu);
23049 case DW_TAG_enumeration_type:
23050 parent_type = read_type_die (parent, cu);
23051 if (parent_type->is_declared_class ())
23052 {
23053 if (parent_type->name () != NULL)
23054 return parent_type->name ();
23055 return "";
23056 }
23057 /* Fall through. */
23058 default:
23059 return determine_prefix (parent, cu);
23060 }
23061 }
23062
23063 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23064 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23065 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23066 an obconcat, otherwise allocate storage for the result. The CU argument is
23067 used to determine the language and hence, the appropriate separator. */
23068
23069 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23070
23071 static char *
23072 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23073 int physname, struct dwarf2_cu *cu)
23074 {
23075 const char *lead = "";
23076 const char *sep;
23077
23078 if (suffix == NULL || suffix[0] == '\0'
23079 || prefix == NULL || prefix[0] == '\0')
23080 sep = "";
23081 else if (cu->language == language_d)
23082 {
23083 /* For D, the 'main' function could be defined in any module, but it
23084 should never be prefixed. */
23085 if (strcmp (suffix, "D main") == 0)
23086 {
23087 prefix = "";
23088 sep = "";
23089 }
23090 else
23091 sep = ".";
23092 }
23093 else if (cu->language == language_fortran && physname)
23094 {
23095 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23096 DW_AT_MIPS_linkage_name is preferred and used instead. */
23097
23098 lead = "__";
23099 sep = "_MOD_";
23100 }
23101 else
23102 sep = "::";
23103
23104 if (prefix == NULL)
23105 prefix = "";
23106 if (suffix == NULL)
23107 suffix = "";
23108
23109 if (obs == NULL)
23110 {
23111 char *retval
23112 = ((char *)
23113 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23114
23115 strcpy (retval, lead);
23116 strcat (retval, prefix);
23117 strcat (retval, sep);
23118 strcat (retval, suffix);
23119 return retval;
23120 }
23121 else
23122 {
23123 /* We have an obstack. */
23124 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23125 }
23126 }
23127
23128 /* Get name of a die, return NULL if not found. */
23129
23130 static const char *
23131 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23132 struct objfile *objfile)
23133 {
23134 if (name && cu->language == language_cplus)
23135 {
23136 gdb::unique_xmalloc_ptr<char> canon_name
23137 = cp_canonicalize_string (name);
23138
23139 if (canon_name != nullptr)
23140 name = objfile->intern (canon_name.get ());
23141 }
23142
23143 return name;
23144 }
23145
23146 /* Get name of a die, return NULL if not found.
23147 Anonymous namespaces are converted to their magic string. */
23148
23149 static const char *
23150 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23151 {
23152 struct attribute *attr;
23153 struct objfile *objfile = cu->per_objfile->objfile;
23154
23155 attr = dwarf2_attr (die, DW_AT_name, cu);
23156 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23157 if (attr_name == nullptr
23158 && die->tag != DW_TAG_namespace
23159 && die->tag != DW_TAG_class_type
23160 && die->tag != DW_TAG_interface_type
23161 && die->tag != DW_TAG_structure_type
23162 && die->tag != DW_TAG_union_type)
23163 return NULL;
23164
23165 switch (die->tag)
23166 {
23167 case DW_TAG_compile_unit:
23168 case DW_TAG_partial_unit:
23169 /* Compilation units have a DW_AT_name that is a filename, not
23170 a source language identifier. */
23171 case DW_TAG_enumeration_type:
23172 case DW_TAG_enumerator:
23173 /* These tags always have simple identifiers already; no need
23174 to canonicalize them. */
23175 return attr_name;
23176
23177 case DW_TAG_namespace:
23178 if (attr_name != nullptr)
23179 return attr_name;
23180 return CP_ANONYMOUS_NAMESPACE_STR;
23181
23182 case DW_TAG_class_type:
23183 case DW_TAG_interface_type:
23184 case DW_TAG_structure_type:
23185 case DW_TAG_union_type:
23186 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23187 structures or unions. These were of the form "._%d" in GCC 4.1,
23188 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23189 and GCC 4.4. We work around this problem by ignoring these. */
23190 if (attr_name != nullptr
23191 && (startswith (attr_name, "._")
23192 || startswith (attr_name, "<anonymous")))
23193 return NULL;
23194
23195 /* GCC might emit a nameless typedef that has a linkage name. See
23196 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23197 if (!attr || attr_name == NULL)
23198 {
23199 attr = dw2_linkage_name_attr (die, cu);
23200 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23201 if (attr == NULL || attr_name == NULL)
23202 return NULL;
23203
23204 /* Avoid demangling attr_name the second time on a second
23205 call for the same DIE. */
23206 if (!attr->canonical_string_p ())
23207 {
23208 gdb::unique_xmalloc_ptr<char> demangled
23209 (gdb_demangle (attr_name, DMGL_TYPES));
23210 if (demangled == nullptr)
23211 return nullptr;
23212
23213 attr->set_string_canonical (objfile->intern (demangled.get ()));
23214 attr_name = attr->as_string ();
23215 }
23216
23217 /* Strip any leading namespaces/classes, keep only the
23218 base name. DW_AT_name for named DIEs does not
23219 contain the prefixes. */
23220 const char *base = strrchr (attr_name, ':');
23221 if (base && base > attr_name && base[-1] == ':')
23222 return &base[1];
23223 else
23224 return attr_name;
23225 }
23226 break;
23227
23228 default:
23229 break;
23230 }
23231
23232 if (!attr->canonical_string_p ())
23233 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23234 objfile));
23235 return attr->as_string ();
23236 }
23237
23238 /* Return the die that this die in an extension of, or NULL if there
23239 is none. *EXT_CU is the CU containing DIE on input, and the CU
23240 containing the return value on output. */
23241
23242 static struct die_info *
23243 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23244 {
23245 struct attribute *attr;
23246
23247 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23248 if (attr == NULL)
23249 return NULL;
23250
23251 return follow_die_ref (die, attr, ext_cu);
23252 }
23253
23254 static void
23255 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23256 {
23257 unsigned int i;
23258
23259 print_spaces (indent, f);
23260 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23261 dwarf_tag_name (die->tag), die->abbrev,
23262 sect_offset_str (die->sect_off));
23263
23264 if (die->parent != NULL)
23265 {
23266 print_spaces (indent, f);
23267 fprintf_unfiltered (f, " parent at offset: %s\n",
23268 sect_offset_str (die->parent->sect_off));
23269 }
23270
23271 print_spaces (indent, f);
23272 fprintf_unfiltered (f, " has children: %s\n",
23273 dwarf_bool_name (die->child != NULL));
23274
23275 print_spaces (indent, f);
23276 fprintf_unfiltered (f, " attributes:\n");
23277
23278 for (i = 0; i < die->num_attrs; ++i)
23279 {
23280 print_spaces (indent, f);
23281 fprintf_unfiltered (f, " %s (%s) ",
23282 dwarf_attr_name (die->attrs[i].name),
23283 dwarf_form_name (die->attrs[i].form));
23284
23285 switch (die->attrs[i].form)
23286 {
23287 case DW_FORM_addr:
23288 case DW_FORM_addrx:
23289 case DW_FORM_GNU_addr_index:
23290 fprintf_unfiltered (f, "address: ");
23291 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23292 break;
23293 case DW_FORM_block2:
23294 case DW_FORM_block4:
23295 case DW_FORM_block:
23296 case DW_FORM_block1:
23297 fprintf_unfiltered (f, "block: size %s",
23298 pulongest (die->attrs[i].as_block ()->size));
23299 break;
23300 case DW_FORM_exprloc:
23301 fprintf_unfiltered (f, "expression: size %s",
23302 pulongest (die->attrs[i].as_block ()->size));
23303 break;
23304 case DW_FORM_data16:
23305 fprintf_unfiltered (f, "constant of 16 bytes");
23306 break;
23307 case DW_FORM_ref_addr:
23308 fprintf_unfiltered (f, "ref address: ");
23309 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23310 break;
23311 case DW_FORM_GNU_ref_alt:
23312 fprintf_unfiltered (f, "alt ref address: ");
23313 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23314 break;
23315 case DW_FORM_ref1:
23316 case DW_FORM_ref2:
23317 case DW_FORM_ref4:
23318 case DW_FORM_ref8:
23319 case DW_FORM_ref_udata:
23320 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23321 (long) (die->attrs[i].as_unsigned ()));
23322 break;
23323 case DW_FORM_data1:
23324 case DW_FORM_data2:
23325 case DW_FORM_data4:
23326 case DW_FORM_data8:
23327 case DW_FORM_udata:
23328 fprintf_unfiltered (f, "constant: %s",
23329 pulongest (die->attrs[i].as_unsigned ()));
23330 break;
23331 case DW_FORM_sec_offset:
23332 fprintf_unfiltered (f, "section offset: %s",
23333 pulongest (die->attrs[i].as_unsigned ()));
23334 break;
23335 case DW_FORM_ref_sig8:
23336 fprintf_unfiltered (f, "signature: %s",
23337 hex_string (die->attrs[i].as_signature ()));
23338 break;
23339 case DW_FORM_string:
23340 case DW_FORM_strp:
23341 case DW_FORM_line_strp:
23342 case DW_FORM_strx:
23343 case DW_FORM_GNU_str_index:
23344 case DW_FORM_GNU_strp_alt:
23345 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23346 die->attrs[i].as_string ()
23347 ? die->attrs[i].as_string () : "",
23348 die->attrs[i].canonical_string_p () ? "is" : "not");
23349 break;
23350 case DW_FORM_flag:
23351 if (die->attrs[i].as_boolean ())
23352 fprintf_unfiltered (f, "flag: TRUE");
23353 else
23354 fprintf_unfiltered (f, "flag: FALSE");
23355 break;
23356 case DW_FORM_flag_present:
23357 fprintf_unfiltered (f, "flag: TRUE");
23358 break;
23359 case DW_FORM_indirect:
23360 /* The reader will have reduced the indirect form to
23361 the "base form" so this form should not occur. */
23362 fprintf_unfiltered (f,
23363 "unexpected attribute form: DW_FORM_indirect");
23364 break;
23365 case DW_FORM_sdata:
23366 case DW_FORM_implicit_const:
23367 fprintf_unfiltered (f, "constant: %s",
23368 plongest (die->attrs[i].as_signed ()));
23369 break;
23370 default:
23371 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23372 die->attrs[i].form);
23373 break;
23374 }
23375 fprintf_unfiltered (f, "\n");
23376 }
23377 }
23378
23379 static void
23380 dump_die_for_error (struct die_info *die)
23381 {
23382 dump_die_shallow (gdb_stderr, 0, die);
23383 }
23384
23385 static void
23386 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23387 {
23388 int indent = level * 4;
23389
23390 gdb_assert (die != NULL);
23391
23392 if (level >= max_level)
23393 return;
23394
23395 dump_die_shallow (f, indent, die);
23396
23397 if (die->child != NULL)
23398 {
23399 print_spaces (indent, f);
23400 fprintf_unfiltered (f, " Children:");
23401 if (level + 1 < max_level)
23402 {
23403 fprintf_unfiltered (f, "\n");
23404 dump_die_1 (f, level + 1, max_level, die->child);
23405 }
23406 else
23407 {
23408 fprintf_unfiltered (f,
23409 " [not printed, max nesting level reached]\n");
23410 }
23411 }
23412
23413 if (die->sibling != NULL && level > 0)
23414 {
23415 dump_die_1 (f, level, max_level, die->sibling);
23416 }
23417 }
23418
23419 /* This is called from the pdie macro in gdbinit.in.
23420 It's not static so gcc will keep a copy callable from gdb. */
23421
23422 void
23423 dump_die (struct die_info *die, int max_level)
23424 {
23425 dump_die_1 (gdb_stdlog, 0, max_level, die);
23426 }
23427
23428 static void
23429 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23430 {
23431 void **slot;
23432
23433 slot = htab_find_slot_with_hash (cu->die_hash, die,
23434 to_underlying (die->sect_off),
23435 INSERT);
23436
23437 *slot = die;
23438 }
23439
23440 /* Follow reference or signature attribute ATTR of SRC_DIE.
23441 On entry *REF_CU is the CU of SRC_DIE.
23442 On exit *REF_CU is the CU of the result. */
23443
23444 static struct die_info *
23445 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23446 struct dwarf2_cu **ref_cu)
23447 {
23448 struct die_info *die;
23449
23450 if (attr->form_is_ref ())
23451 die = follow_die_ref (src_die, attr, ref_cu);
23452 else if (attr->form == DW_FORM_ref_sig8)
23453 die = follow_die_sig (src_die, attr, ref_cu);
23454 else
23455 {
23456 dump_die_for_error (src_die);
23457 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23458 objfile_name ((*ref_cu)->per_objfile->objfile));
23459 }
23460
23461 return die;
23462 }
23463
23464 /* Follow reference OFFSET.
23465 On entry *REF_CU is the CU of the source die referencing OFFSET.
23466 On exit *REF_CU is the CU of the result.
23467 Returns NULL if OFFSET is invalid. */
23468
23469 static struct die_info *
23470 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23471 struct dwarf2_cu **ref_cu)
23472 {
23473 struct die_info temp_die;
23474 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23475 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23476
23477 gdb_assert (cu->per_cu != NULL);
23478
23479 target_cu = cu;
23480
23481 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23482 "source CU contains target offset: %d",
23483 sect_offset_str (cu->per_cu->sect_off),
23484 sect_offset_str (sect_off),
23485 cu->header.offset_in_cu_p (sect_off));
23486
23487 if (cu->per_cu->is_debug_types)
23488 {
23489 /* .debug_types CUs cannot reference anything outside their CU.
23490 If they need to, they have to reference a signatured type via
23491 DW_FORM_ref_sig8. */
23492 if (!cu->header.offset_in_cu_p (sect_off))
23493 return NULL;
23494 }
23495 else if (offset_in_dwz != cu->per_cu->is_dwz
23496 || !cu->header.offset_in_cu_p (sect_off))
23497 {
23498 struct dwarf2_per_cu_data *per_cu;
23499
23500 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23501 per_objfile);
23502
23503 dwarf_read_debug_printf_v ("target CU offset: %s, "
23504 "target CU DIEs loaded: %d",
23505 sect_offset_str (per_cu->sect_off),
23506 per_objfile->get_cu (per_cu) != nullptr);
23507
23508 /* If necessary, add it to the queue and load its DIEs.
23509
23510 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23511 it doesn't mean they are currently loaded. Since we require them
23512 to be loaded, we must check for ourselves. */
23513 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23514 || per_objfile->get_cu (per_cu) == nullptr)
23515 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23516 false, cu->language);
23517
23518 target_cu = per_objfile->get_cu (per_cu);
23519 gdb_assert (target_cu != nullptr);
23520 }
23521 else if (cu->dies == NULL)
23522 {
23523 /* We're loading full DIEs during partial symbol reading. */
23524 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23525 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23526 language_minimal);
23527 }
23528
23529 *ref_cu = target_cu;
23530 temp_die.sect_off = sect_off;
23531
23532 if (target_cu != cu)
23533 target_cu->ancestor = cu;
23534
23535 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23536 &temp_die,
23537 to_underlying (sect_off));
23538 }
23539
23540 /* Follow reference attribute ATTR of SRC_DIE.
23541 On entry *REF_CU is the CU of SRC_DIE.
23542 On exit *REF_CU is the CU of the result. */
23543
23544 static struct die_info *
23545 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23546 struct dwarf2_cu **ref_cu)
23547 {
23548 sect_offset sect_off = attr->get_ref_die_offset ();
23549 struct dwarf2_cu *cu = *ref_cu;
23550 struct die_info *die;
23551
23552 die = follow_die_offset (sect_off,
23553 (attr->form == DW_FORM_GNU_ref_alt
23554 || cu->per_cu->is_dwz),
23555 ref_cu);
23556 if (!die)
23557 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23558 "at %s [in module %s]"),
23559 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23560 objfile_name (cu->per_objfile->objfile));
23561
23562 return die;
23563 }
23564
23565 /* See read.h. */
23566
23567 struct dwarf2_locexpr_baton
23568 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23569 dwarf2_per_cu_data *per_cu,
23570 dwarf2_per_objfile *per_objfile,
23571 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23572 bool resolve_abstract_p)
23573 {
23574 struct die_info *die;
23575 struct attribute *attr;
23576 struct dwarf2_locexpr_baton retval;
23577 struct objfile *objfile = per_objfile->objfile;
23578
23579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23580 if (cu == nullptr)
23581 cu = load_cu (per_cu, per_objfile, false);
23582
23583 if (cu == nullptr)
23584 {
23585 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23586 Instead just throw an error, not much else we can do. */
23587 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23588 sect_offset_str (sect_off), objfile_name (objfile));
23589 }
23590
23591 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23592 if (!die)
23593 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23594 sect_offset_str (sect_off), objfile_name (objfile));
23595
23596 attr = dwarf2_attr (die, DW_AT_location, cu);
23597 if (!attr && resolve_abstract_p
23598 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23599 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23600 {
23601 CORE_ADDR pc = get_frame_pc ();
23602 CORE_ADDR baseaddr = objfile->text_section_offset ();
23603 struct gdbarch *gdbarch = objfile->arch ();
23604
23605 for (const auto &cand_off
23606 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23607 {
23608 struct dwarf2_cu *cand_cu = cu;
23609 struct die_info *cand
23610 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23611 if (!cand
23612 || !cand->parent
23613 || cand->parent->tag != DW_TAG_subprogram)
23614 continue;
23615
23616 CORE_ADDR pc_low, pc_high;
23617 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23618 if (pc_low == ((CORE_ADDR) -1))
23619 continue;
23620 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23621 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23622 if (!(pc_low <= pc && pc < pc_high))
23623 continue;
23624
23625 die = cand;
23626 attr = dwarf2_attr (die, DW_AT_location, cu);
23627 break;
23628 }
23629 }
23630
23631 if (!attr)
23632 {
23633 /* DWARF: "If there is no such attribute, then there is no effect.".
23634 DATA is ignored if SIZE is 0. */
23635
23636 retval.data = NULL;
23637 retval.size = 0;
23638 }
23639 else if (attr->form_is_section_offset ())
23640 {
23641 struct dwarf2_loclist_baton loclist_baton;
23642 CORE_ADDR pc = get_frame_pc ();
23643 size_t size;
23644
23645 fill_in_loclist_baton (cu, &loclist_baton, attr);
23646
23647 retval.data = dwarf2_find_location_expression (&loclist_baton,
23648 &size, pc);
23649 retval.size = size;
23650 }
23651 else
23652 {
23653 if (!attr->form_is_block ())
23654 error (_("Dwarf Error: DIE at %s referenced in module %s "
23655 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23656 sect_offset_str (sect_off), objfile_name (objfile));
23657
23658 struct dwarf_block *block = attr->as_block ();
23659 retval.data = block->data;
23660 retval.size = block->size;
23661 }
23662 retval.per_objfile = per_objfile;
23663 retval.per_cu = cu->per_cu;
23664
23665 per_objfile->age_comp_units ();
23666
23667 return retval;
23668 }
23669
23670 /* See read.h. */
23671
23672 struct dwarf2_locexpr_baton
23673 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23674 dwarf2_per_cu_data *per_cu,
23675 dwarf2_per_objfile *per_objfile,
23676 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23677 {
23678 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23679
23680 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23681 get_frame_pc);
23682 }
23683
23684 /* Write a constant of a given type as target-ordered bytes into
23685 OBSTACK. */
23686
23687 static const gdb_byte *
23688 write_constant_as_bytes (struct obstack *obstack,
23689 enum bfd_endian byte_order,
23690 struct type *type,
23691 ULONGEST value,
23692 LONGEST *len)
23693 {
23694 gdb_byte *result;
23695
23696 *len = TYPE_LENGTH (type);
23697 result = (gdb_byte *) obstack_alloc (obstack, *len);
23698 store_unsigned_integer (result, *len, byte_order, value);
23699
23700 return result;
23701 }
23702
23703 /* See read.h. */
23704
23705 const gdb_byte *
23706 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23707 dwarf2_per_cu_data *per_cu,
23708 dwarf2_per_objfile *per_objfile,
23709 obstack *obstack,
23710 LONGEST *len)
23711 {
23712 struct die_info *die;
23713 struct attribute *attr;
23714 const gdb_byte *result = NULL;
23715 struct type *type;
23716 LONGEST value;
23717 enum bfd_endian byte_order;
23718 struct objfile *objfile = per_objfile->objfile;
23719
23720 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23721 if (cu == nullptr)
23722 cu = load_cu (per_cu, per_objfile, false);
23723
23724 if (cu == nullptr)
23725 {
23726 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23727 Instead just throw an error, not much else we can do. */
23728 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23729 sect_offset_str (sect_off), objfile_name (objfile));
23730 }
23731
23732 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23733 if (!die)
23734 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23735 sect_offset_str (sect_off), objfile_name (objfile));
23736
23737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23738 if (attr == NULL)
23739 return NULL;
23740
23741 byte_order = (bfd_big_endian (objfile->obfd)
23742 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23743
23744 switch (attr->form)
23745 {
23746 case DW_FORM_addr:
23747 case DW_FORM_addrx:
23748 case DW_FORM_GNU_addr_index:
23749 {
23750 gdb_byte *tem;
23751
23752 *len = cu->header.addr_size;
23753 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23754 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23755 result = tem;
23756 }
23757 break;
23758 case DW_FORM_string:
23759 case DW_FORM_strp:
23760 case DW_FORM_strx:
23761 case DW_FORM_GNU_str_index:
23762 case DW_FORM_GNU_strp_alt:
23763 /* The string is already allocated on the objfile obstack, point
23764 directly to it. */
23765 {
23766 const char *attr_name = attr->as_string ();
23767 result = (const gdb_byte *) attr_name;
23768 *len = strlen (attr_name);
23769 }
23770 break;
23771 case DW_FORM_block1:
23772 case DW_FORM_block2:
23773 case DW_FORM_block4:
23774 case DW_FORM_block:
23775 case DW_FORM_exprloc:
23776 case DW_FORM_data16:
23777 {
23778 struct dwarf_block *block = attr->as_block ();
23779 result = block->data;
23780 *len = block->size;
23781 }
23782 break;
23783
23784 /* The DW_AT_const_value attributes are supposed to carry the
23785 symbol's value "represented as it would be on the target
23786 architecture." By the time we get here, it's already been
23787 converted to host endianness, so we just need to sign- or
23788 zero-extend it as appropriate. */
23789 case DW_FORM_data1:
23790 type = die_type (die, cu);
23791 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23792 if (result == NULL)
23793 result = write_constant_as_bytes (obstack, byte_order,
23794 type, value, len);
23795 break;
23796 case DW_FORM_data2:
23797 type = die_type (die, cu);
23798 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23799 if (result == NULL)
23800 result = write_constant_as_bytes (obstack, byte_order,
23801 type, value, len);
23802 break;
23803 case DW_FORM_data4:
23804 type = die_type (die, cu);
23805 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23806 if (result == NULL)
23807 result = write_constant_as_bytes (obstack, byte_order,
23808 type, value, len);
23809 break;
23810 case DW_FORM_data8:
23811 type = die_type (die, cu);
23812 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23813 if (result == NULL)
23814 result = write_constant_as_bytes (obstack, byte_order,
23815 type, value, len);
23816 break;
23817
23818 case DW_FORM_sdata:
23819 case DW_FORM_implicit_const:
23820 type = die_type (die, cu);
23821 result = write_constant_as_bytes (obstack, byte_order,
23822 type, attr->as_signed (), len);
23823 break;
23824
23825 case DW_FORM_udata:
23826 type = die_type (die, cu);
23827 result = write_constant_as_bytes (obstack, byte_order,
23828 type, attr->as_unsigned (), len);
23829 break;
23830
23831 default:
23832 complaint (_("unsupported const value attribute form: '%s'"),
23833 dwarf_form_name (attr->form));
23834 break;
23835 }
23836
23837 return result;
23838 }
23839
23840 /* See read.h. */
23841
23842 struct type *
23843 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23844 dwarf2_per_cu_data *per_cu,
23845 dwarf2_per_objfile *per_objfile)
23846 {
23847 struct die_info *die;
23848
23849 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23850 if (cu == nullptr)
23851 cu = load_cu (per_cu, per_objfile, false);
23852
23853 if (cu == nullptr)
23854 return nullptr;
23855
23856 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23857 if (!die)
23858 return NULL;
23859
23860 return die_type (die, cu);
23861 }
23862
23863 /* See read.h. */
23864
23865 struct type *
23866 dwarf2_get_die_type (cu_offset die_offset,
23867 dwarf2_per_cu_data *per_cu,
23868 dwarf2_per_objfile *per_objfile)
23869 {
23870 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23871 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23872 }
23873
23874 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23875 On entry *REF_CU is the CU of SRC_DIE.
23876 On exit *REF_CU is the CU of the result.
23877 Returns NULL if the referenced DIE isn't found. */
23878
23879 static struct die_info *
23880 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23881 struct dwarf2_cu **ref_cu)
23882 {
23883 struct die_info temp_die;
23884 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23885 struct die_info *die;
23886 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23887
23888
23889 /* While it might be nice to assert sig_type->type == NULL here,
23890 we can get here for DW_AT_imported_declaration where we need
23891 the DIE not the type. */
23892
23893 /* If necessary, add it to the queue and load its DIEs.
23894
23895 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23896 it doesn't mean they are currently loaded. Since we require them
23897 to be loaded, we must check for ourselves. */
23898 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23899 language_minimal)
23900 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
23901 read_signatured_type (sig_type, per_objfile);
23902
23903 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23904 gdb_assert (sig_cu != NULL);
23905 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23906 temp_die.sect_off = sig_type->type_offset_in_section;
23907 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23908 to_underlying (temp_die.sect_off));
23909 if (die)
23910 {
23911 /* For .gdb_index version 7 keep track of included TUs.
23912 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23913 if (per_objfile->per_bfd->index_table != NULL
23914 && per_objfile->per_bfd->index_table->version <= 7)
23915 {
23916 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23917 }
23918
23919 *ref_cu = sig_cu;
23920 if (sig_cu != cu)
23921 sig_cu->ancestor = cu;
23922
23923 return die;
23924 }
23925
23926 return NULL;
23927 }
23928
23929 /* Follow signatured type referenced by ATTR in SRC_DIE.
23930 On entry *REF_CU is the CU of SRC_DIE.
23931 On exit *REF_CU is the CU of the result.
23932 The result is the DIE of the type.
23933 If the referenced type cannot be found an error is thrown. */
23934
23935 static struct die_info *
23936 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23937 struct dwarf2_cu **ref_cu)
23938 {
23939 ULONGEST signature = attr->as_signature ();
23940 struct signatured_type *sig_type;
23941 struct die_info *die;
23942
23943 gdb_assert (attr->form == DW_FORM_ref_sig8);
23944
23945 sig_type = lookup_signatured_type (*ref_cu, signature);
23946 /* sig_type will be NULL if the signatured type is missing from
23947 the debug info. */
23948 if (sig_type == NULL)
23949 {
23950 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23951 " from DIE at %s [in module %s]"),
23952 hex_string (signature), sect_offset_str (src_die->sect_off),
23953 objfile_name ((*ref_cu)->per_objfile->objfile));
23954 }
23955
23956 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23957 if (die == NULL)
23958 {
23959 dump_die_for_error (src_die);
23960 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23961 " from DIE at %s [in module %s]"),
23962 hex_string (signature), sect_offset_str (src_die->sect_off),
23963 objfile_name ((*ref_cu)->per_objfile->objfile));
23964 }
23965
23966 return die;
23967 }
23968
23969 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23970 reading in and processing the type unit if necessary. */
23971
23972 static struct type *
23973 get_signatured_type (struct die_info *die, ULONGEST signature,
23974 struct dwarf2_cu *cu)
23975 {
23976 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23977 struct signatured_type *sig_type;
23978 struct dwarf2_cu *type_cu;
23979 struct die_info *type_die;
23980 struct type *type;
23981
23982 sig_type = lookup_signatured_type (cu, signature);
23983 /* sig_type will be NULL if the signatured type is missing from
23984 the debug info. */
23985 if (sig_type == NULL)
23986 {
23987 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23988 " from DIE at %s [in module %s]"),
23989 hex_string (signature), sect_offset_str (die->sect_off),
23990 objfile_name (per_objfile->objfile));
23991 return build_error_marker_type (cu, die);
23992 }
23993
23994 /* If we already know the type we're done. */
23995 type = per_objfile->get_type_for_signatured_type (sig_type);
23996 if (type != nullptr)
23997 return type;
23998
23999 type_cu = cu;
24000 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24001 if (type_die != NULL)
24002 {
24003 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24004 is created. This is important, for example, because for c++ classes
24005 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24006 type = read_type_die (type_die, type_cu);
24007 if (type == NULL)
24008 {
24009 complaint (_("Dwarf Error: Cannot build signatured type %s"
24010 " referenced from DIE at %s [in module %s]"),
24011 hex_string (signature), sect_offset_str (die->sect_off),
24012 objfile_name (per_objfile->objfile));
24013 type = build_error_marker_type (cu, die);
24014 }
24015 }
24016 else
24017 {
24018 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24019 " from DIE at %s [in module %s]"),
24020 hex_string (signature), sect_offset_str (die->sect_off),
24021 objfile_name (per_objfile->objfile));
24022 type = build_error_marker_type (cu, die);
24023 }
24024
24025 per_objfile->set_type_for_signatured_type (sig_type, type);
24026
24027 return type;
24028 }
24029
24030 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24031 reading in and processing the type unit if necessary. */
24032
24033 static struct type *
24034 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24035 struct dwarf2_cu *cu) /* ARI: editCase function */
24036 {
24037 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24038 if (attr->form_is_ref ())
24039 {
24040 struct dwarf2_cu *type_cu = cu;
24041 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24042
24043 return read_type_die (type_die, type_cu);
24044 }
24045 else if (attr->form == DW_FORM_ref_sig8)
24046 {
24047 return get_signatured_type (die, attr->as_signature (), cu);
24048 }
24049 else
24050 {
24051 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24052
24053 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24054 " at %s [in module %s]"),
24055 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24056 objfile_name (per_objfile->objfile));
24057 return build_error_marker_type (cu, die);
24058 }
24059 }
24060
24061 /* Load the DIEs associated with type unit PER_CU into memory. */
24062
24063 static void
24064 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24065 dwarf2_per_objfile *per_objfile)
24066 {
24067 struct signatured_type *sig_type;
24068
24069 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24070 gdb_assert (! per_cu->type_unit_group_p ());
24071
24072 /* We have the per_cu, but we need the signatured_type.
24073 Fortunately this is an easy translation. */
24074 gdb_assert (per_cu->is_debug_types);
24075 sig_type = (struct signatured_type *) per_cu;
24076
24077 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24078
24079 read_signatured_type (sig_type, per_objfile);
24080
24081 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24082 }
24083
24084 /* Read in a signatured type and build its CU and DIEs.
24085 If the type is a stub for the real type in a DWO file,
24086 read in the real type from the DWO file as well. */
24087
24088 static void
24089 read_signatured_type (signatured_type *sig_type,
24090 dwarf2_per_objfile *per_objfile)
24091 {
24092 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24093
24094 gdb_assert (per_cu->is_debug_types);
24095 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24096
24097 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24098
24099 if (!reader.dummy_p)
24100 {
24101 struct dwarf2_cu *cu = reader.cu;
24102 const gdb_byte *info_ptr = reader.info_ptr;
24103
24104 gdb_assert (cu->die_hash == NULL);
24105 cu->die_hash =
24106 htab_create_alloc_ex (cu->header.length / 12,
24107 die_hash,
24108 die_eq,
24109 NULL,
24110 &cu->comp_unit_obstack,
24111 hashtab_obstack_allocate,
24112 dummy_obstack_deallocate);
24113
24114 if (reader.comp_unit_die->has_children)
24115 reader.comp_unit_die->child
24116 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24117 reader.comp_unit_die);
24118 cu->dies = reader.comp_unit_die;
24119 /* comp_unit_die is not stored in die_hash, no need. */
24120
24121 /* We try not to read any attributes in this function, because
24122 not all CUs needed for references have been loaded yet, and
24123 symbol table processing isn't initialized. But we have to
24124 set the CU language, or we won't be able to build types
24125 correctly. Similarly, if we do not read the producer, we can
24126 not apply producer-specific interpretation. */
24127 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24128
24129 reader.keep ();
24130 }
24131
24132 sig_type->per_cu.tu_read = 1;
24133 }
24134
24135 /* Decode simple location descriptions.
24136 Given a pointer to a dwarf block that defines a location, compute
24137 the location and return the value. If COMPUTED is non-null, it is
24138 set to true to indicate that decoding was successful, and false
24139 otherwise. If COMPUTED is null, then this function may emit a
24140 complaint. */
24141
24142 static CORE_ADDR
24143 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24144 {
24145 struct objfile *objfile = cu->per_objfile->objfile;
24146 size_t i;
24147 size_t size = blk->size;
24148 const gdb_byte *data = blk->data;
24149 CORE_ADDR stack[64];
24150 int stacki;
24151 unsigned int bytes_read, unsnd;
24152 gdb_byte op;
24153
24154 if (computed != nullptr)
24155 *computed = false;
24156
24157 i = 0;
24158 stacki = 0;
24159 stack[stacki] = 0;
24160 stack[++stacki] = 0;
24161
24162 while (i < size)
24163 {
24164 op = data[i++];
24165 switch (op)
24166 {
24167 case DW_OP_lit0:
24168 case DW_OP_lit1:
24169 case DW_OP_lit2:
24170 case DW_OP_lit3:
24171 case DW_OP_lit4:
24172 case DW_OP_lit5:
24173 case DW_OP_lit6:
24174 case DW_OP_lit7:
24175 case DW_OP_lit8:
24176 case DW_OP_lit9:
24177 case DW_OP_lit10:
24178 case DW_OP_lit11:
24179 case DW_OP_lit12:
24180 case DW_OP_lit13:
24181 case DW_OP_lit14:
24182 case DW_OP_lit15:
24183 case DW_OP_lit16:
24184 case DW_OP_lit17:
24185 case DW_OP_lit18:
24186 case DW_OP_lit19:
24187 case DW_OP_lit20:
24188 case DW_OP_lit21:
24189 case DW_OP_lit22:
24190 case DW_OP_lit23:
24191 case DW_OP_lit24:
24192 case DW_OP_lit25:
24193 case DW_OP_lit26:
24194 case DW_OP_lit27:
24195 case DW_OP_lit28:
24196 case DW_OP_lit29:
24197 case DW_OP_lit30:
24198 case DW_OP_lit31:
24199 stack[++stacki] = op - DW_OP_lit0;
24200 break;
24201
24202 case DW_OP_reg0:
24203 case DW_OP_reg1:
24204 case DW_OP_reg2:
24205 case DW_OP_reg3:
24206 case DW_OP_reg4:
24207 case DW_OP_reg5:
24208 case DW_OP_reg6:
24209 case DW_OP_reg7:
24210 case DW_OP_reg8:
24211 case DW_OP_reg9:
24212 case DW_OP_reg10:
24213 case DW_OP_reg11:
24214 case DW_OP_reg12:
24215 case DW_OP_reg13:
24216 case DW_OP_reg14:
24217 case DW_OP_reg15:
24218 case DW_OP_reg16:
24219 case DW_OP_reg17:
24220 case DW_OP_reg18:
24221 case DW_OP_reg19:
24222 case DW_OP_reg20:
24223 case DW_OP_reg21:
24224 case DW_OP_reg22:
24225 case DW_OP_reg23:
24226 case DW_OP_reg24:
24227 case DW_OP_reg25:
24228 case DW_OP_reg26:
24229 case DW_OP_reg27:
24230 case DW_OP_reg28:
24231 case DW_OP_reg29:
24232 case DW_OP_reg30:
24233 case DW_OP_reg31:
24234 stack[++stacki] = op - DW_OP_reg0;
24235 if (i < size)
24236 {
24237 if (computed == nullptr)
24238 dwarf2_complex_location_expr_complaint ();
24239 else
24240 return 0;
24241 }
24242 break;
24243
24244 case DW_OP_regx:
24245 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24246 i += bytes_read;
24247 stack[++stacki] = unsnd;
24248 if (i < size)
24249 {
24250 if (computed == nullptr)
24251 dwarf2_complex_location_expr_complaint ();
24252 else
24253 return 0;
24254 }
24255 break;
24256
24257 case DW_OP_addr:
24258 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24259 &bytes_read);
24260 i += bytes_read;
24261 break;
24262
24263 case DW_OP_const1u:
24264 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24265 i += 1;
24266 break;
24267
24268 case DW_OP_const1s:
24269 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24270 i += 1;
24271 break;
24272
24273 case DW_OP_const2u:
24274 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24275 i += 2;
24276 break;
24277
24278 case DW_OP_const2s:
24279 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24280 i += 2;
24281 break;
24282
24283 case DW_OP_const4u:
24284 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24285 i += 4;
24286 break;
24287
24288 case DW_OP_const4s:
24289 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24290 i += 4;
24291 break;
24292
24293 case DW_OP_const8u:
24294 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24295 i += 8;
24296 break;
24297
24298 case DW_OP_constu:
24299 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24300 &bytes_read);
24301 i += bytes_read;
24302 break;
24303
24304 case DW_OP_consts:
24305 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24306 i += bytes_read;
24307 break;
24308
24309 case DW_OP_dup:
24310 stack[stacki + 1] = stack[stacki];
24311 stacki++;
24312 break;
24313
24314 case DW_OP_plus:
24315 stack[stacki - 1] += stack[stacki];
24316 stacki--;
24317 break;
24318
24319 case DW_OP_plus_uconst:
24320 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24321 &bytes_read);
24322 i += bytes_read;
24323 break;
24324
24325 case DW_OP_minus:
24326 stack[stacki - 1] -= stack[stacki];
24327 stacki--;
24328 break;
24329
24330 case DW_OP_deref:
24331 /* If we're not the last op, then we definitely can't encode
24332 this using GDB's address_class enum. This is valid for partial
24333 global symbols, although the variable's address will be bogus
24334 in the psymtab. */
24335 if (i < size)
24336 {
24337 if (computed == nullptr)
24338 dwarf2_complex_location_expr_complaint ();
24339 else
24340 return 0;
24341 }
24342 break;
24343
24344 case DW_OP_GNU_push_tls_address:
24345 case DW_OP_form_tls_address:
24346 /* The top of the stack has the offset from the beginning
24347 of the thread control block at which the variable is located. */
24348 /* Nothing should follow this operator, so the top of stack would
24349 be returned. */
24350 /* This is valid for partial global symbols, but the variable's
24351 address will be bogus in the psymtab. Make it always at least
24352 non-zero to not look as a variable garbage collected by linker
24353 which have DW_OP_addr 0. */
24354 if (i < size)
24355 {
24356 if (computed == nullptr)
24357 dwarf2_complex_location_expr_complaint ();
24358 else
24359 return 0;
24360 }
24361 stack[stacki]++;
24362 break;
24363
24364 case DW_OP_GNU_uninit:
24365 if (computed != nullptr)
24366 return 0;
24367 break;
24368
24369 case DW_OP_addrx:
24370 case DW_OP_GNU_addr_index:
24371 case DW_OP_GNU_const_index:
24372 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24373 &bytes_read);
24374 i += bytes_read;
24375 break;
24376
24377 default:
24378 if (computed == nullptr)
24379 {
24380 const char *name = get_DW_OP_name (op);
24381
24382 if (name)
24383 complaint (_("unsupported stack op: '%s'"),
24384 name);
24385 else
24386 complaint (_("unsupported stack op: '%02x'"),
24387 op);
24388 }
24389
24390 return (stack[stacki]);
24391 }
24392
24393 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24394 outside of the allocated space. Also enforce minimum>0. */
24395 if (stacki >= ARRAY_SIZE (stack) - 1)
24396 {
24397 if (computed == nullptr)
24398 complaint (_("location description stack overflow"));
24399 return 0;
24400 }
24401
24402 if (stacki <= 0)
24403 {
24404 if (computed == nullptr)
24405 complaint (_("location description stack underflow"));
24406 return 0;
24407 }
24408 }
24409
24410 if (computed != nullptr)
24411 *computed = true;
24412 return (stack[stacki]);
24413 }
24414
24415 /* memory allocation interface */
24416
24417 static struct dwarf_block *
24418 dwarf_alloc_block (struct dwarf2_cu *cu)
24419 {
24420 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24421 }
24422
24423 static struct die_info *
24424 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24425 {
24426 struct die_info *die;
24427 size_t size = sizeof (struct die_info);
24428
24429 if (num_attrs > 1)
24430 size += (num_attrs - 1) * sizeof (struct attribute);
24431
24432 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24433 memset (die, 0, sizeof (struct die_info));
24434 return (die);
24435 }
24436
24437 \f
24438
24439 /* Macro support. */
24440
24441 /* An overload of dwarf_decode_macros that finds the correct section
24442 and ensures it is read in before calling the other overload. */
24443
24444 static void
24445 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24446 int section_is_gnu)
24447 {
24448 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24449 struct objfile *objfile = per_objfile->objfile;
24450 const struct line_header *lh = cu->line_header;
24451 unsigned int offset_size = cu->header.offset_size;
24452 struct dwarf2_section_info *section;
24453 const char *section_name;
24454
24455 if (cu->dwo_unit != nullptr)
24456 {
24457 if (section_is_gnu)
24458 {
24459 section = &cu->dwo_unit->dwo_file->sections.macro;
24460 section_name = ".debug_macro.dwo";
24461 }
24462 else
24463 {
24464 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24465 section_name = ".debug_macinfo.dwo";
24466 }
24467 }
24468 else
24469 {
24470 if (section_is_gnu)
24471 {
24472 section = &per_objfile->per_bfd->macro;
24473 section_name = ".debug_macro";
24474 }
24475 else
24476 {
24477 section = &per_objfile->per_bfd->macinfo;
24478 section_name = ".debug_macinfo";
24479 }
24480 }
24481
24482 section->read (objfile);
24483 if (section->buffer == nullptr)
24484 {
24485 complaint (_("missing %s section"), section_name);
24486 return;
24487 }
24488
24489 buildsym_compunit *builder = cu->get_builder ();
24490
24491 struct dwarf2_section_info *str_offsets_section;
24492 struct dwarf2_section_info *str_section;
24493 ULONGEST str_offsets_base;
24494
24495 if (cu->dwo_unit != nullptr)
24496 {
24497 str_offsets_section = &cu->dwo_unit->dwo_file
24498 ->sections.str_offsets;
24499 str_section = &cu->dwo_unit->dwo_file->sections.str;
24500 str_offsets_base = cu->header.addr_size;
24501 }
24502 else
24503 {
24504 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24505 str_section = &per_objfile->per_bfd->str;
24506 str_offsets_base = *cu->str_offsets_base;
24507 }
24508
24509 dwarf_decode_macros (per_objfile, builder, section, lh,
24510 offset_size, offset, str_section, str_offsets_section,
24511 str_offsets_base, section_is_gnu);
24512 }
24513
24514 /* Return the .debug_loc section to use for CU.
24515 For DWO files use .debug_loc.dwo. */
24516
24517 static struct dwarf2_section_info *
24518 cu_debug_loc_section (struct dwarf2_cu *cu)
24519 {
24520 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24521
24522 if (cu->dwo_unit)
24523 {
24524 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24525
24526 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24527 }
24528 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24529 : &per_objfile->per_bfd->loc);
24530 }
24531
24532 /* Return the .debug_rnglists section to use for CU. */
24533 static struct dwarf2_section_info *
24534 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24535 {
24536 if (cu->header.version < 5)
24537 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24538 cu->header.version);
24539 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24540
24541 /* Make sure we read the .debug_rnglists section from the file that
24542 contains the DW_AT_ranges attribute we are reading. Normally that
24543 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24544 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24545 program. */
24546 if (cu->dwo_unit != nullptr
24547 && tag != DW_TAG_compile_unit
24548 && tag != DW_TAG_skeleton_unit)
24549 {
24550 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24551
24552 if (sections->rnglists.size > 0)
24553 return &sections->rnglists;
24554 else
24555 error (_(".debug_rnglists section is missing from .dwo file."));
24556 }
24557 return &dwarf2_per_objfile->per_bfd->rnglists;
24558 }
24559
24560 /* A helper function that fills in a dwarf2_loclist_baton. */
24561
24562 static void
24563 fill_in_loclist_baton (struct dwarf2_cu *cu,
24564 struct dwarf2_loclist_baton *baton,
24565 const struct attribute *attr)
24566 {
24567 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24568 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24569
24570 section->read (per_objfile->objfile);
24571
24572 baton->per_objfile = per_objfile;
24573 baton->per_cu = cu->per_cu;
24574 gdb_assert (baton->per_cu);
24575 /* We don't know how long the location list is, but make sure we
24576 don't run off the edge of the section. */
24577 baton->size = section->size - attr->as_unsigned ();
24578 baton->data = section->buffer + attr->as_unsigned ();
24579 if (cu->base_address.has_value ())
24580 baton->base_address = *cu->base_address;
24581 else
24582 baton->base_address = 0;
24583 baton->from_dwo = cu->dwo_unit != NULL;
24584 }
24585
24586 static void
24587 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24588 struct dwarf2_cu *cu, int is_block)
24589 {
24590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24591 struct objfile *objfile = per_objfile->objfile;
24592 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24593
24594 if (attr->form_is_section_offset ()
24595 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24596 the section. If so, fall through to the complaint in the
24597 other branch. */
24598 && attr->as_unsigned () < section->get_size (objfile))
24599 {
24600 struct dwarf2_loclist_baton *baton;
24601
24602 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24603
24604 fill_in_loclist_baton (cu, baton, attr);
24605
24606 if (!cu->base_address.has_value ())
24607 complaint (_("Location list used without "
24608 "specifying the CU base address."));
24609
24610 SYMBOL_ACLASS_INDEX (sym) = (is_block
24611 ? dwarf2_loclist_block_index
24612 : dwarf2_loclist_index);
24613 SYMBOL_LOCATION_BATON (sym) = baton;
24614 }
24615 else
24616 {
24617 struct dwarf2_locexpr_baton *baton;
24618
24619 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24620 baton->per_objfile = per_objfile;
24621 baton->per_cu = cu->per_cu;
24622 gdb_assert (baton->per_cu);
24623
24624 if (attr->form_is_block ())
24625 {
24626 /* Note that we're just copying the block's data pointer
24627 here, not the actual data. We're still pointing into the
24628 info_buffer for SYM's objfile; right now we never release
24629 that buffer, but when we do clean up properly this may
24630 need to change. */
24631 struct dwarf_block *block = attr->as_block ();
24632 baton->size = block->size;
24633 baton->data = block->data;
24634 }
24635 else
24636 {
24637 dwarf2_invalid_attrib_class_complaint ("location description",
24638 sym->natural_name ());
24639 baton->size = 0;
24640 }
24641
24642 SYMBOL_ACLASS_INDEX (sym) = (is_block
24643 ? dwarf2_locexpr_block_index
24644 : dwarf2_locexpr_index);
24645 SYMBOL_LOCATION_BATON (sym) = baton;
24646 }
24647 }
24648
24649 /* See read.h. */
24650
24651 const comp_unit_head *
24652 dwarf2_per_cu_data::get_header () const
24653 {
24654 if (!m_header_read_in)
24655 {
24656 const gdb_byte *info_ptr
24657 = this->section->buffer + to_underlying (this->sect_off);
24658
24659 memset (&m_header, 0, sizeof (m_header));
24660
24661 read_comp_unit_head (&m_header, info_ptr, this->section,
24662 rcuh_kind::COMPILE);
24663
24664 m_header_read_in = true;
24665 }
24666
24667 return &m_header;
24668 }
24669
24670 /* See read.h. */
24671
24672 int
24673 dwarf2_per_cu_data::addr_size () const
24674 {
24675 return this->get_header ()->addr_size;
24676 }
24677
24678 /* See read.h. */
24679
24680 int
24681 dwarf2_per_cu_data::offset_size () const
24682 {
24683 return this->get_header ()->offset_size;
24684 }
24685
24686 /* See read.h. */
24687
24688 int
24689 dwarf2_per_cu_data::ref_addr_size () const
24690 {
24691 const comp_unit_head *header = this->get_header ();
24692
24693 if (header->version == 2)
24694 return header->addr_size;
24695 else
24696 return header->offset_size;
24697 }
24698
24699 /* See read.h. */
24700
24701 struct type *
24702 dwarf2_cu::addr_type () const
24703 {
24704 struct objfile *objfile = this->per_objfile->objfile;
24705 struct type *void_type = objfile_type (objfile)->builtin_void;
24706 struct type *addr_type = lookup_pointer_type (void_type);
24707 int addr_size = this->per_cu->addr_size ();
24708
24709 if (TYPE_LENGTH (addr_type) == addr_size)
24710 return addr_type;
24711
24712 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24713 return addr_type;
24714 }
24715
24716 /* A helper function for dwarf2_find_containing_comp_unit that returns
24717 the index of the result, and that searches a vector. It will
24718 return a result even if the offset in question does not actually
24719 occur in any CU. This is separate so that it can be unit
24720 tested. */
24721
24722 static int
24723 dwarf2_find_containing_comp_unit
24724 (sect_offset sect_off,
24725 unsigned int offset_in_dwz,
24726 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24727 {
24728 int low, high;
24729
24730 low = 0;
24731 high = all_comp_units.size () - 1;
24732 while (high > low)
24733 {
24734 struct dwarf2_per_cu_data *mid_cu;
24735 int mid = low + (high - low) / 2;
24736
24737 mid_cu = all_comp_units[mid];
24738 if (mid_cu->is_dwz > offset_in_dwz
24739 || (mid_cu->is_dwz == offset_in_dwz
24740 && mid_cu->sect_off + mid_cu->length > sect_off))
24741 high = mid;
24742 else
24743 low = mid + 1;
24744 }
24745 gdb_assert (low == high);
24746 return low;
24747 }
24748
24749 /* Locate the .debug_info compilation unit from CU's objfile which contains
24750 the DIE at OFFSET. Raises an error on failure. */
24751
24752 static struct dwarf2_per_cu_data *
24753 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24754 unsigned int offset_in_dwz,
24755 dwarf2_per_objfile *per_objfile)
24756 {
24757 int low = dwarf2_find_containing_comp_unit
24758 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24759 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24760
24761 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24762 {
24763 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24764 error (_("Dwarf Error: could not find partial DIE containing "
24765 "offset %s [in module %s]"),
24766 sect_offset_str (sect_off),
24767 bfd_get_filename (per_objfile->objfile->obfd));
24768
24769 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24770 <= sect_off);
24771 return per_objfile->per_bfd->all_comp_units[low-1];
24772 }
24773 else
24774 {
24775 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24776 && sect_off >= this_cu->sect_off + this_cu->length)
24777 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24778 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24779 return this_cu;
24780 }
24781 }
24782
24783 #if GDB_SELF_TEST
24784
24785 namespace selftests {
24786 namespace find_containing_comp_unit {
24787
24788 static void
24789 run_test ()
24790 {
24791 struct dwarf2_per_cu_data one {};
24792 struct dwarf2_per_cu_data two {};
24793 struct dwarf2_per_cu_data three {};
24794 struct dwarf2_per_cu_data four {};
24795
24796 one.length = 5;
24797 two.sect_off = sect_offset (one.length);
24798 two.length = 7;
24799
24800 three.length = 5;
24801 three.is_dwz = 1;
24802 four.sect_off = sect_offset (three.length);
24803 four.length = 7;
24804 four.is_dwz = 1;
24805
24806 std::vector<dwarf2_per_cu_data *> units;
24807 units.push_back (&one);
24808 units.push_back (&two);
24809 units.push_back (&three);
24810 units.push_back (&four);
24811
24812 int result;
24813
24814 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24815 SELF_CHECK (units[result] == &one);
24816 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24817 SELF_CHECK (units[result] == &one);
24818 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24819 SELF_CHECK (units[result] == &two);
24820
24821 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24822 SELF_CHECK (units[result] == &three);
24823 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24824 SELF_CHECK (units[result] == &three);
24825 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24826 SELF_CHECK (units[result] == &four);
24827 }
24828
24829 }
24830 }
24831
24832 #endif /* GDB_SELF_TEST */
24833
24834 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24835
24836 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24837 dwarf2_per_objfile *per_objfile)
24838 : per_cu (per_cu),
24839 per_objfile (per_objfile),
24840 mark (false),
24841 has_loclist (false),
24842 checked_producer (false),
24843 producer_is_gxx_lt_4_6 (false),
24844 producer_is_gcc_lt_4_3 (false),
24845 producer_is_icc (false),
24846 producer_is_icc_lt_14 (false),
24847 producer_is_codewarrior (false),
24848 processing_has_namespace_info (false)
24849 {
24850 }
24851
24852 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24853
24854 static void
24855 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24856 enum language pretend_language)
24857 {
24858 struct attribute *attr;
24859
24860 /* Set the language we're debugging. */
24861 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24862 if (attr != nullptr)
24863 set_cu_language (attr->constant_value (0), cu);
24864 else
24865 {
24866 cu->language = pretend_language;
24867 cu->language_defn = language_def (cu->language);
24868 }
24869
24870 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24871 }
24872
24873 /* See read.h. */
24874
24875 dwarf2_cu *
24876 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24877 {
24878 auto it = m_dwarf2_cus.find (per_cu);
24879 if (it == m_dwarf2_cus.end ())
24880 return nullptr;
24881
24882 return it->second;
24883 }
24884
24885 /* See read.h. */
24886
24887 void
24888 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24889 {
24890 gdb_assert (this->get_cu (per_cu) == nullptr);
24891
24892 m_dwarf2_cus[per_cu] = cu;
24893 }
24894
24895 /* See read.h. */
24896
24897 void
24898 dwarf2_per_objfile::age_comp_units ()
24899 {
24900 dwarf_read_debug_printf_v ("running");
24901
24902 /* This is not expected to be called in the middle of CU expansion. There is
24903 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24904 loaded in memory. Calling age_comp_units while the queue is in use could
24905 make us free the DIEs for a CU that is in the queue and therefore break
24906 that invariant. */
24907 gdb_assert (!this->per_bfd->queue.has_value ());
24908
24909 /* Start by clearing all marks. */
24910 for (auto pair : m_dwarf2_cus)
24911 pair.second->mark = false;
24912
24913 /* Traverse all CUs, mark them and their dependencies if used recently
24914 enough. */
24915 for (auto pair : m_dwarf2_cus)
24916 {
24917 dwarf2_cu *cu = pair.second;
24918
24919 cu->last_used++;
24920 if (cu->last_used <= dwarf_max_cache_age)
24921 dwarf2_mark (cu);
24922 }
24923
24924 /* Delete all CUs still not marked. */
24925 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24926 {
24927 dwarf2_cu *cu = it->second;
24928
24929 if (!cu->mark)
24930 {
24931 dwarf_read_debug_printf_v ("deleting old CU %s",
24932 sect_offset_str (cu->per_cu->sect_off));
24933 delete cu;
24934 it = m_dwarf2_cus.erase (it);
24935 }
24936 else
24937 it++;
24938 }
24939 }
24940
24941 /* See read.h. */
24942
24943 void
24944 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24945 {
24946 auto it = m_dwarf2_cus.find (per_cu);
24947 if (it == m_dwarf2_cus.end ())
24948 return;
24949
24950 delete it->second;
24951
24952 m_dwarf2_cus.erase (it);
24953 }
24954
24955 dwarf2_per_objfile::~dwarf2_per_objfile ()
24956 {
24957 remove_all_cus ();
24958 }
24959
24960 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24961 We store these in a hash table separate from the DIEs, and preserve them
24962 when the DIEs are flushed out of cache.
24963
24964 The CU "per_cu" pointer is needed because offset alone is not enough to
24965 uniquely identify the type. A file may have multiple .debug_types sections,
24966 or the type may come from a DWO file. Furthermore, while it's more logical
24967 to use per_cu->section+offset, with Fission the section with the data is in
24968 the DWO file but we don't know that section at the point we need it.
24969 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24970 because we can enter the lookup routine, get_die_type_at_offset, from
24971 outside this file, and thus won't necessarily have PER_CU->cu.
24972 Fortunately, PER_CU is stable for the life of the objfile. */
24973
24974 struct dwarf2_per_cu_offset_and_type
24975 {
24976 const struct dwarf2_per_cu_data *per_cu;
24977 sect_offset sect_off;
24978 struct type *type;
24979 };
24980
24981 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24982
24983 static hashval_t
24984 per_cu_offset_and_type_hash (const void *item)
24985 {
24986 const struct dwarf2_per_cu_offset_and_type *ofs
24987 = (const struct dwarf2_per_cu_offset_and_type *) item;
24988
24989 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24990 }
24991
24992 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24993
24994 static int
24995 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24996 {
24997 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24998 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24999 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25000 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25001
25002 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25003 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25004 }
25005
25006 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25007 table if necessary. For convenience, return TYPE.
25008
25009 The DIEs reading must have careful ordering to:
25010 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25011 reading current DIE.
25012 * Not trying to dereference contents of still incompletely read in types
25013 while reading in other DIEs.
25014 * Enable referencing still incompletely read in types just by a pointer to
25015 the type without accessing its fields.
25016
25017 Therefore caller should follow these rules:
25018 * Try to fetch any prerequisite types we may need to build this DIE type
25019 before building the type and calling set_die_type.
25020 * After building type call set_die_type for current DIE as soon as
25021 possible before fetching more types to complete the current type.
25022 * Make the type as complete as possible before fetching more types. */
25023
25024 static struct type *
25025 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25026 bool skip_data_location)
25027 {
25028 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25029 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25030 struct objfile *objfile = per_objfile->objfile;
25031 struct attribute *attr;
25032 struct dynamic_prop prop;
25033
25034 /* For Ada types, make sure that the gnat-specific data is always
25035 initialized (if not already set). There are a few types where
25036 we should not be doing so, because the type-specific area is
25037 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25038 where the type-specific area is used to store the floatformat).
25039 But this is not a problem, because the gnat-specific information
25040 is actually not needed for these types. */
25041 if (need_gnat_info (cu)
25042 && type->code () != TYPE_CODE_FUNC
25043 && type->code () != TYPE_CODE_FLT
25044 && type->code () != TYPE_CODE_METHODPTR
25045 && type->code () != TYPE_CODE_MEMBERPTR
25046 && type->code () != TYPE_CODE_METHOD
25047 && type->code () != TYPE_CODE_FIXED_POINT
25048 && !HAVE_GNAT_AUX_INFO (type))
25049 INIT_GNAT_SPECIFIC (type);
25050
25051 /* Read DW_AT_allocated and set in type. */
25052 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25053 if (attr != NULL)
25054 {
25055 struct type *prop_type = cu->addr_sized_int_type (false);
25056 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25057 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25058 }
25059
25060 /* Read DW_AT_associated and set in type. */
25061 attr = dwarf2_attr (die, DW_AT_associated, cu);
25062 if (attr != NULL)
25063 {
25064 struct type *prop_type = cu->addr_sized_int_type (false);
25065 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25066 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25067 }
25068
25069 /* Read DW_AT_data_location and set in type. */
25070 if (!skip_data_location)
25071 {
25072 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25073 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25074 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25075 }
25076
25077 if (per_objfile->die_type_hash == NULL)
25078 per_objfile->die_type_hash
25079 = htab_up (htab_create_alloc (127,
25080 per_cu_offset_and_type_hash,
25081 per_cu_offset_and_type_eq,
25082 NULL, xcalloc, xfree));
25083
25084 ofs.per_cu = cu->per_cu;
25085 ofs.sect_off = die->sect_off;
25086 ofs.type = type;
25087 slot = (struct dwarf2_per_cu_offset_and_type **)
25088 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25089 if (*slot)
25090 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25091 sect_offset_str (die->sect_off));
25092 *slot = XOBNEW (&objfile->objfile_obstack,
25093 struct dwarf2_per_cu_offset_and_type);
25094 **slot = ofs;
25095 return type;
25096 }
25097
25098 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25099 or return NULL if the die does not have a saved type. */
25100
25101 static struct type *
25102 get_die_type_at_offset (sect_offset sect_off,
25103 dwarf2_per_cu_data *per_cu,
25104 dwarf2_per_objfile *per_objfile)
25105 {
25106 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25107
25108 if (per_objfile->die_type_hash == NULL)
25109 return NULL;
25110
25111 ofs.per_cu = per_cu;
25112 ofs.sect_off = sect_off;
25113 slot = ((struct dwarf2_per_cu_offset_and_type *)
25114 htab_find (per_objfile->die_type_hash.get (), &ofs));
25115 if (slot)
25116 return slot->type;
25117 else
25118 return NULL;
25119 }
25120
25121 /* Look up the type for DIE in CU in die_type_hash,
25122 or return NULL if DIE does not have a saved type. */
25123
25124 static struct type *
25125 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25126 {
25127 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25128 }
25129
25130 /* Add a dependence relationship from CU to REF_PER_CU. */
25131
25132 static void
25133 dwarf2_add_dependence (struct dwarf2_cu *cu,
25134 struct dwarf2_per_cu_data *ref_per_cu)
25135 {
25136 void **slot;
25137
25138 if (cu->dependencies == NULL)
25139 cu->dependencies
25140 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25141 NULL, &cu->comp_unit_obstack,
25142 hashtab_obstack_allocate,
25143 dummy_obstack_deallocate);
25144
25145 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25146 if (*slot == NULL)
25147 *slot = ref_per_cu;
25148 }
25149
25150 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25151 Set the mark field in every compilation unit in the
25152 cache that we must keep because we are keeping CU.
25153
25154 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25155
25156 static int
25157 dwarf2_mark_helper (void **slot, void *data)
25158 {
25159 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25160 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25161 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25162
25163 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25164 reading of the chain. As such dependencies remain valid it is not much
25165 useful to track and undo them during QUIT cleanups. */
25166 if (cu == nullptr)
25167 return 1;
25168
25169 if (cu->mark)
25170 return 1;
25171
25172 cu->mark = true;
25173
25174 if (cu->dependencies != nullptr)
25175 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25176
25177 return 1;
25178 }
25179
25180 /* Set the mark field in CU and in every other compilation unit in the
25181 cache that we must keep because we are keeping CU. */
25182
25183 static void
25184 dwarf2_mark (struct dwarf2_cu *cu)
25185 {
25186 if (cu->mark)
25187 return;
25188
25189 cu->mark = true;
25190
25191 if (cu->dependencies != nullptr)
25192 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25193 }
25194
25195 /* Trivial hash function for partial_die_info: the hash value of a DIE
25196 is its offset in .debug_info for this objfile. */
25197
25198 static hashval_t
25199 partial_die_hash (const void *item)
25200 {
25201 const struct partial_die_info *part_die
25202 = (const struct partial_die_info *) item;
25203
25204 return to_underlying (part_die->sect_off);
25205 }
25206
25207 /* Trivial comparison function for partial_die_info structures: two DIEs
25208 are equal if they have the same offset. */
25209
25210 static int
25211 partial_die_eq (const void *item_lhs, const void *item_rhs)
25212 {
25213 const struct partial_die_info *part_die_lhs
25214 = (const struct partial_die_info *) item_lhs;
25215 const struct partial_die_info *part_die_rhs
25216 = (const struct partial_die_info *) item_rhs;
25217
25218 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25219 }
25220
25221 struct cmd_list_element *set_dwarf_cmdlist;
25222 struct cmd_list_element *show_dwarf_cmdlist;
25223
25224 static void
25225 show_check_physname (struct ui_file *file, int from_tty,
25226 struct cmd_list_element *c, const char *value)
25227 {
25228 fprintf_filtered (file,
25229 _("Whether to check \"physname\" is %s.\n"),
25230 value);
25231 }
25232
25233 void _initialize_dwarf2_read ();
25234 void
25235 _initialize_dwarf2_read ()
25236 {
25237 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25238 Set DWARF specific variables.\n\
25239 Configure DWARF variables such as the cache size."),
25240 &set_dwarf_cmdlist, "maintenance set dwarf ",
25241 0/*allow-unknown*/, &maintenance_set_cmdlist);
25242
25243 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25244 Show DWARF specific variables.\n\
25245 Show DWARF variables such as the cache size."),
25246 &show_dwarf_cmdlist, "maintenance show dwarf ",
25247 0/*allow-unknown*/, &maintenance_show_cmdlist);
25248
25249 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25250 &dwarf_max_cache_age, _("\
25251 Set the upper bound on the age of cached DWARF compilation units."), _("\
25252 Show the upper bound on the age of cached DWARF compilation units."), _("\
25253 A higher limit means that cached compilation units will be stored\n\
25254 in memory longer, and more total memory will be used. Zero disables\n\
25255 caching, which can slow down startup."),
25256 NULL,
25257 show_dwarf_max_cache_age,
25258 &set_dwarf_cmdlist,
25259 &show_dwarf_cmdlist);
25260
25261 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25262 Set debugging of the DWARF reader."), _("\
25263 Show debugging of the DWARF reader."), _("\
25264 When enabled (non-zero), debugging messages are printed during DWARF\n\
25265 reading and symtab expansion. A value of 1 (one) provides basic\n\
25266 information. A value greater than 1 provides more verbose information."),
25267 NULL,
25268 NULL,
25269 &setdebuglist, &showdebuglist);
25270
25271 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25272 Set debugging of the DWARF DIE reader."), _("\
25273 Show debugging of the DWARF DIE reader."), _("\
25274 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25275 The value is the maximum depth to print."),
25276 NULL,
25277 NULL,
25278 &setdebuglist, &showdebuglist);
25279
25280 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25281 Set debugging of the dwarf line reader."), _("\
25282 Show debugging of the dwarf line reader."), _("\
25283 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25284 A value of 1 (one) provides basic information.\n\
25285 A value greater than 1 provides more verbose information."),
25286 NULL,
25287 NULL,
25288 &setdebuglist, &showdebuglist);
25289
25290 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25291 Set cross-checking of \"physname\" code against demangler."), _("\
25292 Show cross-checking of \"physname\" code against demangler."), _("\
25293 When enabled, GDB's internal \"physname\" code is checked against\n\
25294 the demangler."),
25295 NULL, show_check_physname,
25296 &setdebuglist, &showdebuglist);
25297
25298 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25299 no_class, &use_deprecated_index_sections, _("\
25300 Set whether to use deprecated gdb_index sections."), _("\
25301 Show whether to use deprecated gdb_index sections."), _("\
25302 When enabled, deprecated .gdb_index sections are used anyway.\n\
25303 Normally they are ignored either because of a missing feature or\n\
25304 performance issue.\n\
25305 Warning: This option must be enabled before gdb reads the file."),
25306 NULL,
25307 NULL,
25308 &setlist, &showlist);
25309
25310 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25311 &dwarf2_locexpr_funcs);
25312 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25313 &dwarf2_loclist_funcs);
25314
25315 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25316 &dwarf2_block_frame_base_locexpr_funcs);
25317 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25318 &dwarf2_block_frame_base_loclist_funcs);
25319
25320 #if GDB_SELF_TEST
25321 selftests::register_test ("dw2_expand_symtabs_matching",
25322 selftests::dw2_expand_symtabs_matching::run_test);
25323 selftests::register_test ("dwarf2_find_containing_comp_unit",
25324 selftests::find_containing_comp_unit::run_test);
25325 #endif
25326 }
This page took 0.70836 seconds and 4 git commands to generate.