[gdb/symtab] Fix infinite recursion in dwarf2_cu::get_builder()
[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 #include <unordered_set>
92
93 /* When == 1, print basic high level tracing messages.
94 When > 1, be more verbose.
95 This is in contrast to the low level DIE reading of dwarf_die_debug. */
96 static unsigned int dwarf_read_debug = 0;
97
98 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
99
100 #define dwarf_read_debug_printf(fmt, ...) \
101 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
102 ##__VA_ARGS__)
103
104 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
105
106 #define dwarf_read_debug_printf_v(fmt, ...) \
107 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
108 ##__VA_ARGS__)
109
110 /* When non-zero, dump DIEs after they are read in. */
111 static unsigned int dwarf_die_debug = 0;
112
113 /* When non-zero, dump line number entries as they are read in. */
114 unsigned int dwarf_line_debug = 0;
115
116 /* When true, cross-check physname against demangler. */
117 static bool check_physname = false;
118
119 /* When true, do not reject deprecated .gdb_index sections. */
120 static bool use_deprecated_index_sections = false;
121
122 /* This is used to store the data that is always per objfile. */
123 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
124
125 /* These are used to store the dwarf2_per_bfd objects.
126
127 objfiles having the same BFD, which doesn't require relocations, are going to
128 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
129
130 Other objfiles are not going to share a dwarf2_per_bfd with any other
131 objfiles, so they'll have their own version kept in the _objfile_data_key
132 version. */
133 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
134 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
135
136 /* The "aclass" indices for various kinds of computed DWARF symbols. */
137
138 static int dwarf2_locexpr_index;
139 static int dwarf2_loclist_index;
140 static int dwarf2_locexpr_block_index;
141 static int dwarf2_loclist_block_index;
142
143 /* Size of .debug_loclists section header for 32-bit DWARF format. */
144 #define LOCLIST_HEADER_SIZE32 12
145
146 /* Size of .debug_loclists section header for 64-bit DWARF format. */
147 #define LOCLIST_HEADER_SIZE64 20
148
149 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
150 #define RNGLIST_HEADER_SIZE32 12
151
152 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
153 #define RNGLIST_HEADER_SIZE64 20
154
155 /* An index into a (C++) symbol name component in a symbol name as
156 recorded in the mapped_index's symbol table. For each C++ symbol
157 in the symbol table, we record one entry for the start of each
158 component in the symbol in a table of name components, and then
159 sort the table, in order to be able to binary search symbol names,
160 ignoring leading namespaces, both completion and regular look up.
161 For example, for symbol "A::B::C", we'll have an entry that points
162 to "A::B::C", another that points to "B::C", and another for "C".
163 Note that function symbols in GDB index have no parameter
164 information, just the function/method names. You can convert a
165 name_component to a "const char *" using the
166 'mapped_index::symbol_name_at(offset_type)' method. */
167
168 struct name_component
169 {
170 /* Offset in the symbol name where the component starts. Stored as
171 a (32-bit) offset instead of a pointer to save memory and improve
172 locality on 64-bit architectures. */
173 offset_type name_offset;
174
175 /* The symbol's index in the symbol and constant pool tables of a
176 mapped_index. */
177 offset_type idx;
178 };
179
180 /* Base class containing bits shared by both .gdb_index and
181 .debug_name indexes. */
182
183 struct mapped_index_base
184 {
185 mapped_index_base () = default;
186 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
187
188 /* The name_component table (a sorted vector). See name_component's
189 description above. */
190 std::vector<name_component> name_components;
191
192 /* How NAME_COMPONENTS is sorted. */
193 enum case_sensitivity name_components_casing;
194
195 /* Return the number of names in the symbol table. */
196 virtual size_t symbol_name_count () const = 0;
197
198 /* Get the name of the symbol at IDX in the symbol table. */
199 virtual const char *symbol_name_at
200 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
201
202 /* Return whether the name at IDX in the symbol table should be
203 ignored. */
204 virtual bool symbol_name_slot_invalid (offset_type idx) const
205 {
206 return false;
207 }
208
209 /* Build the symbol name component sorted vector, if we haven't
210 yet. */
211 void build_name_components (dwarf2_per_objfile *per_objfile);
212
213 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
214 possible matches for LN_NO_PARAMS in the name component
215 vector. */
216 std::pair<std::vector<name_component>::const_iterator,
217 std::vector<name_component>::const_iterator>
218 find_name_components_bounds (const lookup_name_info &ln_no_params,
219 enum language lang,
220 dwarf2_per_objfile *per_objfile) const;
221
222 /* Prevent deleting/destroying via a base class pointer. */
223 protected:
224 ~mapped_index_base() = default;
225 };
226
227 /* This is a view into the index that converts from bytes to an
228 offset_type, and allows indexing. Unaligned bytes are specifically
229 allowed here, and handled via unpacking. */
230
231 class offset_view
232 {
233 public:
234 offset_view () = default;
235
236 explicit offset_view (gdb::array_view<const gdb_byte> bytes)
237 : m_bytes (bytes)
238 {
239 }
240
241 /* Extract the INDEXth offset_type from the array. */
242 offset_type operator[] (size_t index) const
243 {
244 const gdb_byte *bytes = &m_bytes[index * sizeof (offset_type)];
245 return (offset_type) extract_unsigned_integer (bytes,
246 sizeof (offset_type),
247 BFD_ENDIAN_LITTLE);
248 }
249
250 /* Return the number of offset_types in this array. */
251 size_t size () const
252 {
253 return m_bytes.size () / sizeof (offset_type);
254 }
255
256 /* Return true if this view is empty. */
257 bool empty () const
258 {
259 return m_bytes.empty ();
260 }
261
262 private:
263 /* The underlying bytes. */
264 gdb::array_view<const gdb_byte> m_bytes;
265 };
266
267 /* A description of the mapped index. The file format is described in
268 a comment by the code that writes the index. */
269 struct mapped_index final : public mapped_index_base
270 {
271 /* Index data format version. */
272 int version = 0;
273
274 /* The address table data. */
275 gdb::array_view<const gdb_byte> address_table;
276
277 /* The symbol table, implemented as a hash table. */
278 offset_view symbol_table;
279
280 /* A pointer to the constant pool. */
281 gdb::array_view<const gdb_byte> constant_pool;
282
283 /* Return the index into the constant pool of the name of the IDXth
284 symbol in the symbol table. */
285 offset_type symbol_name_index (offset_type idx) const
286 {
287 return symbol_table[2 * idx];
288 }
289
290 /* Return the index into the constant pool of the CU vector of the
291 IDXth symbol in the symbol table. */
292 offset_type symbol_vec_index (offset_type idx) const
293 {
294 return symbol_table[2 * idx + 1];
295 }
296
297 bool symbol_name_slot_invalid (offset_type idx) const override
298 {
299 return (symbol_name_index (idx) == 0
300 && symbol_vec_index (idx) == 0);
301 }
302
303 /* Convenience method to get at the name of the symbol at IDX in the
304 symbol table. */
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 {
308 return (const char *) (this->constant_pool.data ()
309 + symbol_name_index (idx));
310 }
311
312 size_t symbol_name_count () const override
313 { return this->symbol_table.size () / 2; }
314 };
315
316 /* A description of the mapped .debug_names.
317 Uninitialized map has CU_COUNT 0. */
318 struct mapped_debug_names final : public mapped_index_base
319 {
320 bfd_endian dwarf5_byte_order;
321 bool dwarf5_is_dwarf64;
322 bool augmentation_is_gdb;
323 uint8_t offset_size;
324 uint32_t cu_count = 0;
325 uint32_t tu_count, bucket_count, name_count;
326 const gdb_byte *cu_table_reordered, *tu_table_reordered;
327 const uint32_t *bucket_table_reordered, *hash_table_reordered;
328 const gdb_byte *name_table_string_offs_reordered;
329 const gdb_byte *name_table_entry_offs_reordered;
330 const gdb_byte *entry_pool;
331
332 struct index_val
333 {
334 ULONGEST dwarf_tag;
335 struct attr
336 {
337 /* Attribute name DW_IDX_*. */
338 ULONGEST dw_idx;
339
340 /* Attribute form DW_FORM_*. */
341 ULONGEST form;
342
343 /* Value if FORM is DW_FORM_implicit_const. */
344 LONGEST implicit_const;
345 };
346 std::vector<attr> attr_vec;
347 };
348
349 std::unordered_map<ULONGEST, index_val> abbrev_map;
350
351 const char *namei_to_name
352 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
353
354 /* Implementation of the mapped_index_base virtual interface, for
355 the name_components cache. */
356
357 const char *symbol_name_at
358 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
359 { return namei_to_name (idx, per_objfile); }
360
361 size_t symbol_name_count () const override
362 { return this->name_count; }
363 };
364
365 /* See dwarf2read.h. */
366
367 dwarf2_per_objfile *
368 get_dwarf2_per_objfile (struct objfile *objfile)
369 {
370 return dwarf2_objfile_data_key.get (objfile);
371 }
372
373 /* Default names of the debugging sections. */
374
375 /* Note that if the debugging section has been compressed, it might
376 have a name like .zdebug_info. */
377
378 const struct dwarf2_debug_sections dwarf2_elf_names =
379 {
380 { ".debug_info", ".zdebug_info" },
381 { ".debug_abbrev", ".zdebug_abbrev" },
382 { ".debug_line", ".zdebug_line" },
383 { ".debug_loc", ".zdebug_loc" },
384 { ".debug_loclists", ".zdebug_loclists" },
385 { ".debug_macinfo", ".zdebug_macinfo" },
386 { ".debug_macro", ".zdebug_macro" },
387 { ".debug_str", ".zdebug_str" },
388 { ".debug_str_offsets", ".zdebug_str_offsets" },
389 { ".debug_line_str", ".zdebug_line_str" },
390 { ".debug_ranges", ".zdebug_ranges" },
391 { ".debug_rnglists", ".zdebug_rnglists" },
392 { ".debug_types", ".zdebug_types" },
393 { ".debug_addr", ".zdebug_addr" },
394 { ".debug_frame", ".zdebug_frame" },
395 { ".eh_frame", NULL },
396 { ".gdb_index", ".zgdb_index" },
397 { ".debug_names", ".zdebug_names" },
398 { ".debug_aranges", ".zdebug_aranges" },
399 23
400 };
401
402 /* List of DWO/DWP sections. */
403
404 static const struct dwop_section_names
405 {
406 struct dwarf2_section_names abbrev_dwo;
407 struct dwarf2_section_names info_dwo;
408 struct dwarf2_section_names line_dwo;
409 struct dwarf2_section_names loc_dwo;
410 struct dwarf2_section_names loclists_dwo;
411 struct dwarf2_section_names macinfo_dwo;
412 struct dwarf2_section_names macro_dwo;
413 struct dwarf2_section_names rnglists_dwo;
414 struct dwarf2_section_names str_dwo;
415 struct dwarf2_section_names str_offsets_dwo;
416 struct dwarf2_section_names types_dwo;
417 struct dwarf2_section_names cu_index;
418 struct dwarf2_section_names tu_index;
419 }
420 dwop_section_names =
421 {
422 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
423 { ".debug_info.dwo", ".zdebug_info.dwo" },
424 { ".debug_line.dwo", ".zdebug_line.dwo" },
425 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
426 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
427 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
428 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
429 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
430 { ".debug_str.dwo", ".zdebug_str.dwo" },
431 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
432 { ".debug_types.dwo", ".zdebug_types.dwo" },
433 { ".debug_cu_index", ".zdebug_cu_index" },
434 { ".debug_tu_index", ".zdebug_tu_index" },
435 };
436
437 /* local data types */
438
439 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
440 begin with a header, which contains the following information. */
441 struct loclists_rnglists_header
442 {
443 /* A 4-byte or 12-byte length containing the length of the
444 set of entries for this compilation unit, not including the
445 length field itself. */
446 unsigned int length;
447
448 /* A 2-byte version identifier. */
449 short version;
450
451 /* A 1-byte unsigned integer containing the size in bytes of an address on
452 the target system. */
453 unsigned char addr_size;
454
455 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
456 on the target system. */
457 unsigned char segment_collector_size;
458
459 /* A 4-byte count of the number of offsets that follow the header. */
460 unsigned int offset_entry_count;
461 };
462
463 /* Type used for delaying computation of method physnames.
464 See comments for compute_delayed_physnames. */
465 struct delayed_method_info
466 {
467 /* The type to which the method is attached, i.e., its parent class. */
468 struct type *type;
469
470 /* The index of the method in the type's function fieldlists. */
471 int fnfield_index;
472
473 /* The index of the method in the fieldlist. */
474 int index;
475
476 /* The name of the DIE. */
477 const char *name;
478
479 /* The DIE associated with this method. */
480 struct die_info *die;
481 };
482
483 /* Internal state when decoding a particular compilation unit. */
484 struct dwarf2_cu
485 {
486 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
487 dwarf2_per_objfile *per_objfile);
488
489 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
490
491 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
492 Create the set of symtabs used by this TU, or if this TU is sharing
493 symtabs with another TU and the symtabs have already been created
494 then restore those symtabs in the line header.
495 We don't need the pc/line-number mapping for type units. */
496 void setup_type_unit_groups (struct die_info *die);
497
498 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
499 buildsym_compunit constructor. */
500 struct compunit_symtab *start_symtab (const char *name,
501 const char *comp_dir,
502 CORE_ADDR low_pc);
503
504 /* Reset the builder. */
505 void reset_builder () { m_builder.reset (); }
506
507 /* Return a type that is a generic pointer type, the size of which
508 matches the address size given in the compilation unit header for
509 this CU. */
510 struct type *addr_type () const;
511
512 /* Find an integer type the same size as the address size given in
513 the compilation unit header for this CU. UNSIGNED_P controls if
514 the integer is unsigned or not. */
515 struct type *addr_sized_int_type (bool unsigned_p) const;
516
517 /* The header of the compilation unit. */
518 struct comp_unit_head header {};
519
520 /* Base address of this compilation unit. */
521 gdb::optional<CORE_ADDR> base_address;
522
523 /* The language we are debugging. */
524 enum language language = language_unknown;
525 const struct language_defn *language_defn = nullptr;
526
527 const char *producer = nullptr;
528
529 private:
530 /* The symtab builder for this CU. This is only non-NULL when full
531 symbols are being read. */
532 std::unique_ptr<buildsym_compunit> m_builder;
533
534 public:
535 /* The generic symbol table building routines have separate lists for
536 file scope symbols and all all other scopes (local scopes). So
537 we need to select the right one to pass to add_symbol_to_list().
538 We do it by keeping a pointer to the correct list in list_in_scope.
539
540 FIXME: The original dwarf code just treated the file scope as the
541 first local scope, and all other local scopes as nested local
542 scopes, and worked fine. Check to see if we really need to
543 distinguish these in buildsym.c. */
544 struct pending **list_in_scope = nullptr;
545
546 /* Hash table holding all the loaded partial DIEs
547 with partial_die->offset.SECT_OFF as hash. */
548 htab_t partial_dies = nullptr;
549
550 /* Storage for things with the same lifetime as this read-in compilation
551 unit, including partial DIEs. */
552 auto_obstack comp_unit_obstack;
553
554 /* Backlink to our per_cu entry. */
555 struct dwarf2_per_cu_data *per_cu;
556
557 /* The dwarf2_per_objfile that owns this. */
558 dwarf2_per_objfile *per_objfile;
559
560 /* How many compilation units ago was this CU last referenced? */
561 int last_used = 0;
562
563 /* A hash table of DIE cu_offset for following references with
564 die_info->offset.sect_off as hash. */
565 htab_t die_hash = nullptr;
566
567 /* Full DIEs if read in. */
568 struct die_info *dies = nullptr;
569
570 /* A set of pointers to dwarf2_per_cu_data objects for compilation
571 units referenced by this one. Only set during full symbol processing;
572 partial symbol tables do not have dependencies. */
573 htab_t dependencies = nullptr;
574
575 /* Header data from the line table, during full symbol processing. */
576 struct line_header *line_header = nullptr;
577 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
578 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
579 this is the DW_TAG_compile_unit die for this CU. We'll hold on
580 to the line header as long as this DIE is being processed. See
581 process_die_scope. */
582 die_info *line_header_die_owner = nullptr;
583
584 /* A list of methods which need to have physnames computed
585 after all type information has been read. */
586 std::vector<delayed_method_info> method_list;
587
588 /* To be copied to symtab->call_site_htab. */
589 htab_t call_site_htab = nullptr;
590
591 /* Non-NULL if this CU came from a DWO file.
592 There is an invariant here that is important to remember:
593 Except for attributes copied from the top level DIE in the "main"
594 (or "stub") file in preparation for reading the DWO file
595 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
596 Either there isn't a DWO file (in which case this is NULL and the point
597 is moot), or there is and either we're not going to read it (in which
598 case this is NULL) or there is and we are reading it (in which case this
599 is non-NULL). */
600 struct dwo_unit *dwo_unit = nullptr;
601
602 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
603 Note this value comes from the Fission stub CU/TU's DIE. */
604 gdb::optional<ULONGEST> addr_base;
605
606 /* The DW_AT_GNU_ranges_base attribute, if present.
607
608 This is only relevant in the context of pre-DWARF 5 split units. In this
609 context, there is a .debug_ranges section in the linked executable,
610 containing all the ranges data for all the compilation units. Each
611 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
612 indicates the base of its contribution to that section. The DW_AT_ranges
613 attributes in the split-unit are of the form DW_FORM_sec_offset and point
614 into the .debug_ranges section of the linked file. However, they are not
615 "true" DW_FORM_sec_offset, because they are relative to the base of their
616 compilation unit's contribution, rather than relative to the beginning of
617 the section. The DW_AT_GNU_ranges_base value must be added to it to make
618 it relative to the beginning of the section.
619
620 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
621 case, so this value can be added without needing to know whether we are in
622 this case or not.
623
624 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
625 skeleton/stub, it must not have the base added, as it already points to the
626 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
627 have a DW_AT_ranges attribute, we can use the
628
629 die->tag != DW_AT_compile_unit
630
631 to determine whether the base should be added or not. */
632 ULONGEST gnu_ranges_base = 0;
633
634 /* The DW_AT_rnglists_base attribute, if present.
635
636 This is used when processing attributes of form DW_FORM_rnglistx in
637 non-split units. Attributes of this form found in a split unit don't
638 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
639 section. */
640 ULONGEST rnglists_base = 0;
641
642 /* The DW_AT_loclists_base attribute if present. */
643 ULONGEST loclist_base = 0;
644
645 /* When reading debug info generated by older versions of rustc, we
646 have to rewrite some union types to be struct types with a
647 variant part. This rewriting must be done after the CU is fully
648 read in, because otherwise at the point of rewriting some struct
649 type might not have been fully processed. So, we keep a list of
650 all such types here and process them after expansion. */
651 std::vector<struct type *> rust_unions;
652
653 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
654 files, the value is implicitly zero. For DWARF 5 version DWO files, the
655 value is often implicit and is the size of the header of
656 .debug_str_offsets section (8 or 4, depending on the address size). */
657 gdb::optional<ULONGEST> str_offsets_base;
658
659 /* Mark used when releasing cached dies. */
660 bool mark : 1;
661
662 /* This CU references .debug_loc. See the symtab->locations_valid field.
663 This test is imperfect as there may exist optimized debug code not using
664 any location list and still facing inlining issues if handled as
665 unoptimized code. For a future better test see GCC PR other/32998. */
666 bool has_loclist : 1;
667
668 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
669 if all the producer_is_* fields are valid. This information is cached
670 because profiling CU expansion showed excessive time spent in
671 producer_is_gxx_lt_4_6. */
672 bool checked_producer : 1;
673 bool producer_is_gxx_lt_4_6 : 1;
674 bool producer_is_gcc_lt_4_3 : 1;
675 bool producer_is_icc : 1;
676 bool producer_is_icc_lt_14 : 1;
677 bool producer_is_codewarrior : 1;
678
679 /* When true, the file that we're processing is known to have
680 debugging info for C++ namespaces. GCC 3.3.x did not produce
681 this information, but later versions do. */
682
683 bool processing_has_namespace_info : 1;
684
685 struct partial_die_info *find_partial_die (sect_offset sect_off);
686
687 /* Get the buildsym_compunit for this CU. */
688 buildsym_compunit *get_builder ()
689 {
690 /* If this CU has a builder associated with it, use that. */
691 if (m_builder != nullptr)
692 return m_builder.get ();
693
694 return nullptr;
695 }
696 };
697
698 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
699 This includes type_unit_group and quick_file_names. */
700
701 struct stmt_list_hash
702 {
703 /* The DWO unit this table is from or NULL if there is none. */
704 struct dwo_unit *dwo_unit;
705
706 /* Offset in .debug_line or .debug_line.dwo. */
707 sect_offset line_sect_off;
708 };
709
710 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
711 an object of this type. This contains elements of type unit groups
712 that can be shared across objfiles. The non-shareable parts are in
713 type_unit_group_unshareable. */
714
715 struct type_unit_group : public dwarf2_per_cu_data
716 {
717 /* The TUs that share this DW_AT_stmt_list entry.
718 This is added to while parsing type units to build partial symtabs,
719 and is deleted afterwards and not used again. */
720 std::vector<signatured_type *> *tus = nullptr;
721
722 /* The data used to construct the hash key. */
723 struct stmt_list_hash hash {};
724 };
725
726 /* These sections are what may appear in a (real or virtual) DWO file. */
727
728 struct dwo_sections
729 {
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info loclists;
734 struct dwarf2_section_info macinfo;
735 struct dwarf2_section_info macro;
736 struct dwarf2_section_info rnglists;
737 struct dwarf2_section_info str;
738 struct dwarf2_section_info str_offsets;
739 /* In the case of a virtual DWO file, these two are unused. */
740 struct dwarf2_section_info info;
741 std::vector<dwarf2_section_info> types;
742 };
743
744 /* CUs/TUs in DWP/DWO files. */
745
746 struct dwo_unit
747 {
748 /* Backlink to the containing struct dwo_file. */
749 struct dwo_file *dwo_file;
750
751 /* The "id" that distinguishes this CU/TU.
752 .debug_info calls this "dwo_id", .debug_types calls this "signature".
753 Since signatures came first, we stick with it for consistency. */
754 ULONGEST signature;
755
756 /* The section this CU/TU lives in, in the DWO file. */
757 struct dwarf2_section_info *section;
758
759 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
760 sect_offset sect_off;
761 unsigned int length;
762
763 /* For types, offset in the type's DIE of the type defined by this TU. */
764 cu_offset type_offset_in_tu;
765 };
766
767 /* include/dwarf2.h defines the DWP section codes.
768 It defines a max value but it doesn't define a min value, which we
769 use for error checking, so provide one. */
770
771 enum dwp_v2_section_ids
772 {
773 DW_SECT_MIN = 1
774 };
775
776 /* Data for one DWO file.
777
778 This includes virtual DWO files (a virtual DWO file is a DWO file as it
779 appears in a DWP file). DWP files don't really have DWO files per se -
780 comdat folding of types "loses" the DWO file they came from, and from
781 a high level view DWP files appear to contain a mass of random types.
782 However, to maintain consistency with the non-DWP case we pretend DWP
783 files contain virtual DWO files, and we assign each TU with one virtual
784 DWO file (generally based on the line and abbrev section offsets -
785 a heuristic that seems to work in practice). */
786
787 struct dwo_file
788 {
789 dwo_file () = default;
790 DISABLE_COPY_AND_ASSIGN (dwo_file);
791
792 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
793 For virtual DWO files the name is constructed from the section offsets
794 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
795 from related CU+TUs. */
796 const char *dwo_name = nullptr;
797
798 /* The DW_AT_comp_dir attribute. */
799 const char *comp_dir = nullptr;
800
801 /* The bfd, when the file is open. Otherwise this is NULL.
802 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
803 gdb_bfd_ref_ptr dbfd;
804
805 /* The sections that make up this DWO file.
806 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
807 sections (for lack of a better name). */
808 struct dwo_sections sections {};
809
810 /* The CUs in the file.
811 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
812 an extension to handle LLVM's Link Time Optimization output (where
813 multiple source files may be compiled into a single object/dwo pair). */
814 htab_up cus;
815
816 /* Table of TUs in the file.
817 Each element is a struct dwo_unit. */
818 htab_up tus;
819 };
820
821 /* These sections are what may appear in a DWP file. */
822
823 struct dwp_sections
824 {
825 /* These are used by all DWP versions (1, 2 and 5). */
826 struct dwarf2_section_info str;
827 struct dwarf2_section_info cu_index;
828 struct dwarf2_section_info tu_index;
829
830 /* These are only used by DWP version 2 and version 5 files.
831 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
832 sections are referenced by section number, and are not recorded here.
833 In DWP version 2 or 5 there is at most one copy of all these sections,
834 each section being (effectively) comprised of the concatenation of all of
835 the individual sections that exist in the version 1 format.
836 To keep the code simple we treat each of these concatenated pieces as a
837 section itself (a virtual section?). */
838 struct dwarf2_section_info abbrev;
839 struct dwarf2_section_info info;
840 struct dwarf2_section_info line;
841 struct dwarf2_section_info loc;
842 struct dwarf2_section_info loclists;
843 struct dwarf2_section_info macinfo;
844 struct dwarf2_section_info macro;
845 struct dwarf2_section_info rnglists;
846 struct dwarf2_section_info str_offsets;
847 struct dwarf2_section_info types;
848 };
849
850 /* These sections are what may appear in a virtual DWO file in DWP version 1.
851 A virtual DWO file is a DWO file as it appears in a DWP file. */
852
853 struct virtual_v1_dwo_sections
854 {
855 struct dwarf2_section_info abbrev;
856 struct dwarf2_section_info line;
857 struct dwarf2_section_info loc;
858 struct dwarf2_section_info macinfo;
859 struct dwarf2_section_info macro;
860 struct dwarf2_section_info str_offsets;
861 /* Each DWP hash table entry records one CU or one TU.
862 That is recorded here, and copied to dwo_unit.section. */
863 struct dwarf2_section_info info_or_types;
864 };
865
866 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
867 In version 2, the sections of the DWO files are concatenated together
868 and stored in one section of that name. Thus each ELF section contains
869 several "virtual" sections. */
870
871 struct virtual_v2_or_v5_dwo_sections
872 {
873 bfd_size_type abbrev_offset;
874 bfd_size_type abbrev_size;
875
876 bfd_size_type line_offset;
877 bfd_size_type line_size;
878
879 bfd_size_type loc_offset;
880 bfd_size_type loc_size;
881
882 bfd_size_type loclists_offset;
883 bfd_size_type loclists_size;
884
885 bfd_size_type macinfo_offset;
886 bfd_size_type macinfo_size;
887
888 bfd_size_type macro_offset;
889 bfd_size_type macro_size;
890
891 bfd_size_type rnglists_offset;
892 bfd_size_type rnglists_size;
893
894 bfd_size_type str_offsets_offset;
895 bfd_size_type str_offsets_size;
896
897 /* Each DWP hash table entry records one CU or one TU.
898 That is recorded here, and copied to dwo_unit.section. */
899 bfd_size_type info_or_types_offset;
900 bfd_size_type info_or_types_size;
901 };
902
903 /* Contents of DWP hash tables. */
904
905 struct dwp_hash_table
906 {
907 uint32_t version, nr_columns;
908 uint32_t nr_units, nr_slots;
909 const gdb_byte *hash_table, *unit_table;
910 union
911 {
912 struct
913 {
914 const gdb_byte *indices;
915 } v1;
916 struct
917 {
918 /* This is indexed by column number and gives the id of the section
919 in that column. */
920 #define MAX_NR_V2_DWO_SECTIONS \
921 (1 /* .debug_info or .debug_types */ \
922 + 1 /* .debug_abbrev */ \
923 + 1 /* .debug_line */ \
924 + 1 /* .debug_loc */ \
925 + 1 /* .debug_str_offsets */ \
926 + 1 /* .debug_macro or .debug_macinfo */)
927 int section_ids[MAX_NR_V2_DWO_SECTIONS];
928 const gdb_byte *offsets;
929 const gdb_byte *sizes;
930 } v2;
931 struct
932 {
933 /* This is indexed by column number and gives the id of the section
934 in that column. */
935 #define MAX_NR_V5_DWO_SECTIONS \
936 (1 /* .debug_info */ \
937 + 1 /* .debug_abbrev */ \
938 + 1 /* .debug_line */ \
939 + 1 /* .debug_loclists */ \
940 + 1 /* .debug_str_offsets */ \
941 + 1 /* .debug_macro */ \
942 + 1 /* .debug_rnglists */)
943 int section_ids[MAX_NR_V5_DWO_SECTIONS];
944 const gdb_byte *offsets;
945 const gdb_byte *sizes;
946 } v5;
947 } section_pool;
948 };
949
950 /* Data for one DWP file. */
951
952 struct dwp_file
953 {
954 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
955 : name (name_),
956 dbfd (std::move (abfd))
957 {
958 }
959
960 /* Name of the file. */
961 const char *name;
962
963 /* File format version. */
964 int version = 0;
965
966 /* The bfd. */
967 gdb_bfd_ref_ptr dbfd;
968
969 /* Section info for this file. */
970 struct dwp_sections sections {};
971
972 /* Table of CUs in the file. */
973 const struct dwp_hash_table *cus = nullptr;
974
975 /* Table of TUs in the file. */
976 const struct dwp_hash_table *tus = nullptr;
977
978 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
979 htab_up loaded_cus;
980 htab_up loaded_tus;
981
982 /* Table to map ELF section numbers to their sections.
983 This is only needed for the DWP V1 file format. */
984 unsigned int num_sections = 0;
985 asection **elf_sections = nullptr;
986 };
987
988 /* Struct used to pass misc. parameters to read_die_and_children, et
989 al. which are used for both .debug_info and .debug_types dies.
990 All parameters here are unchanging for the life of the call. This
991 struct exists to abstract away the constant parameters of die reading. */
992
993 struct die_reader_specs
994 {
995 /* The bfd of die_section. */
996 bfd *abfd;
997
998 /* The CU of the DIE we are parsing. */
999 struct dwarf2_cu *cu;
1000
1001 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
1002 struct dwo_file *dwo_file;
1003
1004 /* The section the die comes from.
1005 This is either .debug_info or .debug_types, or the .dwo variants. */
1006 struct dwarf2_section_info *die_section;
1007
1008 /* die_section->buffer. */
1009 const gdb_byte *buffer;
1010
1011 /* The end of the buffer. */
1012 const gdb_byte *buffer_end;
1013
1014 /* The abbreviation table to use when reading the DIEs. */
1015 struct abbrev_table *abbrev_table;
1016 };
1017
1018 /* A subclass of die_reader_specs that holds storage and has complex
1019 constructor and destructor behavior. */
1020
1021 class cutu_reader : public die_reader_specs
1022 {
1023 public:
1024
1025 cutu_reader (dwarf2_per_cu_data *this_cu,
1026 dwarf2_per_objfile *per_objfile,
1027 struct abbrev_table *abbrev_table,
1028 dwarf2_cu *existing_cu,
1029 bool skip_partial);
1030
1031 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
1032 dwarf2_per_objfile *per_objfile,
1033 struct dwarf2_cu *parent_cu = nullptr,
1034 struct dwo_file *dwo_file = nullptr);
1035
1036 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1037
1038 const gdb_byte *info_ptr = nullptr;
1039 struct die_info *comp_unit_die = nullptr;
1040 bool dummy_p = false;
1041
1042 /* Release the new CU, putting it on the chain. This cannot be done
1043 for dummy CUs. */
1044 void keep ();
1045
1046 private:
1047 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1048 dwarf2_per_objfile *per_objfile,
1049 dwarf2_cu *existing_cu);
1050
1051 struct dwarf2_per_cu_data *m_this_cu;
1052 std::unique_ptr<dwarf2_cu> m_new_cu;
1053
1054 /* The ordinary abbreviation table. */
1055 abbrev_table_up m_abbrev_table_holder;
1056
1057 /* The DWO abbreviation table. */
1058 abbrev_table_up m_dwo_abbrev_table;
1059 };
1060
1061 /* When we construct a partial symbol table entry we only
1062 need this much information. */
1063 struct partial_die_info : public allocate_on_obstack
1064 {
1065 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1066
1067 /* Disable assign but still keep copy ctor, which is needed
1068 load_partial_dies. */
1069 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1070
1071 /* Adjust the partial die before generating a symbol for it. This
1072 function may set the is_external flag or change the DIE's
1073 name. */
1074 void fixup (struct dwarf2_cu *cu);
1075
1076 /* Read a minimal amount of information into the minimal die
1077 structure. */
1078 const gdb_byte *read (const struct die_reader_specs *reader,
1079 const struct abbrev_info &abbrev,
1080 const gdb_byte *info_ptr);
1081
1082 /* Compute the name of this partial DIE. This memoizes the
1083 result, so it is safe to call multiple times. */
1084 const char *name (dwarf2_cu *cu);
1085
1086 /* Offset of this DIE. */
1087 const sect_offset sect_off;
1088
1089 /* DWARF-2 tag for this DIE. */
1090 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1091
1092 /* Assorted flags describing the data found in this DIE. */
1093 const unsigned int has_children : 1;
1094
1095 unsigned int is_external : 1;
1096 unsigned int is_declaration : 1;
1097 unsigned int has_type : 1;
1098 unsigned int has_specification : 1;
1099 unsigned int has_pc_info : 1;
1100 unsigned int may_be_inlined : 1;
1101
1102 /* This DIE has been marked DW_AT_main_subprogram. */
1103 unsigned int main_subprogram : 1;
1104
1105 /* Flag set if the SCOPE field of this structure has been
1106 computed. */
1107 unsigned int scope_set : 1;
1108
1109 /* Flag set if the DIE has a byte_size attribute. */
1110 unsigned int has_byte_size : 1;
1111
1112 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1113 unsigned int has_const_value : 1;
1114
1115 /* Flag set if any of the DIE's children are template arguments. */
1116 unsigned int has_template_arguments : 1;
1117
1118 /* Flag set if fixup has been called on this die. */
1119 unsigned int fixup_called : 1;
1120
1121 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1122 unsigned int is_dwz : 1;
1123
1124 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1125 unsigned int spec_is_dwz : 1;
1126
1127 unsigned int canonical_name : 1;
1128
1129 /* The name of this DIE. Normally the value of DW_AT_name, but
1130 sometimes a default name for unnamed DIEs. */
1131 const char *raw_name = nullptr;
1132
1133 /* The linkage name, if present. */
1134 const char *linkage_name = nullptr;
1135
1136 /* The scope to prepend to our children. This is generally
1137 allocated on the comp_unit_obstack, so will disappear
1138 when this compilation unit leaves the cache. */
1139 const char *scope = nullptr;
1140
1141 /* Some data associated with the partial DIE. The tag determines
1142 which field is live. */
1143 union
1144 {
1145 /* The location description associated with this DIE, if any. */
1146 struct dwarf_block *locdesc;
1147 /* The offset of an import, for DW_TAG_imported_unit. */
1148 sect_offset sect_off;
1149 } d {};
1150
1151 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1152 CORE_ADDR lowpc = 0;
1153 CORE_ADDR highpc = 0;
1154
1155 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1156 DW_AT_sibling, if any. */
1157 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1158 could return DW_AT_sibling values to its caller load_partial_dies. */
1159 const gdb_byte *sibling = nullptr;
1160
1161 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1162 DW_AT_specification (or DW_AT_abstract_origin or
1163 DW_AT_extension). */
1164 sect_offset spec_offset {};
1165
1166 /* Pointers to this DIE's parent, first child, and next sibling,
1167 if any. */
1168 struct partial_die_info *die_parent = nullptr;
1169 struct partial_die_info *die_child = nullptr;
1170 struct partial_die_info *die_sibling = nullptr;
1171
1172 friend struct partial_die_info *
1173 dwarf2_cu::find_partial_die (sect_offset sect_off);
1174
1175 private:
1176 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1177 partial_die_info (sect_offset sect_off)
1178 : partial_die_info (sect_off, DW_TAG_padding, 0)
1179 {
1180 }
1181
1182 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1183 int has_children_)
1184 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1185 {
1186 is_external = 0;
1187 is_declaration = 0;
1188 has_type = 0;
1189 has_specification = 0;
1190 has_pc_info = 0;
1191 may_be_inlined = 0;
1192 main_subprogram = 0;
1193 scope_set = 0;
1194 has_byte_size = 0;
1195 has_const_value = 0;
1196 has_template_arguments = 0;
1197 fixup_called = 0;
1198 is_dwz = 0;
1199 spec_is_dwz = 0;
1200 canonical_name = 0;
1201 }
1202 };
1203
1204 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1205 but this would require a corresponding change in unpack_field_as_long
1206 and friends. */
1207 static int bits_per_byte = 8;
1208
1209 struct variant_part_builder;
1210
1211 /* When reading a variant, we track a bit more information about the
1212 field, and store it in an object of this type. */
1213
1214 struct variant_field
1215 {
1216 int first_field = -1;
1217 int last_field = -1;
1218
1219 /* A variant can contain other variant parts. */
1220 std::vector<variant_part_builder> variant_parts;
1221
1222 /* If we see a DW_TAG_variant, then this will be set if this is the
1223 default branch. */
1224 bool default_branch = false;
1225 /* If we see a DW_AT_discr_value, then this will be the discriminant
1226 value. */
1227 ULONGEST discriminant_value = 0;
1228 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1229 data. */
1230 struct dwarf_block *discr_list_data = nullptr;
1231 };
1232
1233 /* This represents a DW_TAG_variant_part. */
1234
1235 struct variant_part_builder
1236 {
1237 /* The offset of the discriminant field. */
1238 sect_offset discriminant_offset {};
1239
1240 /* Variants that are direct children of this variant part. */
1241 std::vector<variant_field> variants;
1242
1243 /* True if we're currently reading a variant. */
1244 bool processing_variant = false;
1245 };
1246
1247 struct nextfield
1248 {
1249 int accessibility = 0;
1250 int virtuality = 0;
1251 /* Variant parts need to find the discriminant, which is a DIE
1252 reference. We track the section offset of each field to make
1253 this link. */
1254 sect_offset offset;
1255 struct field field {};
1256 };
1257
1258 struct fnfieldlist
1259 {
1260 const char *name = nullptr;
1261 std::vector<struct fn_field> fnfields;
1262 };
1263
1264 /* The routines that read and process dies for a C struct or C++ class
1265 pass lists of data member fields and lists of member function fields
1266 in an instance of a field_info structure, as defined below. */
1267 struct field_info
1268 {
1269 /* List of data member and baseclasses fields. */
1270 std::vector<struct nextfield> fields;
1271 std::vector<struct nextfield> baseclasses;
1272
1273 /* Set if the accessibility of one of the fields is not public. */
1274 bool non_public_fields = false;
1275
1276 /* Member function fieldlist array, contains name of possibly overloaded
1277 member function, number of overloaded member functions and a pointer
1278 to the head of the member function field chain. */
1279 std::vector<struct fnfieldlist> fnfieldlists;
1280
1281 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1282 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1283 std::vector<struct decl_field> typedef_field_list;
1284
1285 /* Nested types defined by this class and the number of elements in this
1286 list. */
1287 std::vector<struct decl_field> nested_types_list;
1288
1289 /* If non-null, this is the variant part we are currently
1290 reading. */
1291 variant_part_builder *current_variant_part = nullptr;
1292 /* This holds all the top-level variant parts attached to the type
1293 we're reading. */
1294 std::vector<variant_part_builder> variant_parts;
1295
1296 /* Return the total number of fields (including baseclasses). */
1297 int nfields () const
1298 {
1299 return fields.size () + baseclasses.size ();
1300 }
1301 };
1302
1303 /* Loaded secondary compilation units are kept in memory until they
1304 have not been referenced for the processing of this many
1305 compilation units. Set this to zero to disable caching. Cache
1306 sizes of up to at least twenty will improve startup time for
1307 typical inter-CU-reference binaries, at an obvious memory cost. */
1308 static int dwarf_max_cache_age = 5;
1309 static void
1310 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1311 struct cmd_list_element *c, const char *value)
1312 {
1313 fprintf_filtered (file, _("The upper bound on the age of cached "
1314 "DWARF compilation units is %s.\n"),
1315 value);
1316 }
1317 \f
1318 /* local function prototypes */
1319
1320 static void dwarf2_find_base_address (struct die_info *die,
1321 struct dwarf2_cu *cu);
1322
1323 static dwarf2_psymtab *create_partial_symtab
1324 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1325 const char *name);
1326
1327 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1328 const gdb_byte *info_ptr,
1329 struct die_info *type_unit_die);
1330
1331 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1332
1333 static void scan_partial_symbols (struct partial_die_info *,
1334 CORE_ADDR *, CORE_ADDR *,
1335 int, struct dwarf2_cu *);
1336
1337 static void add_partial_symbol (struct partial_die_info *,
1338 struct dwarf2_cu *);
1339
1340 static void add_partial_namespace (struct partial_die_info *pdi,
1341 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1342 int set_addrmap, struct dwarf2_cu *cu);
1343
1344 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1345 CORE_ADDR *highpc, int set_addrmap,
1346 struct dwarf2_cu *cu);
1347
1348 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1349 struct dwarf2_cu *cu);
1350
1351 static void add_partial_subprogram (struct partial_die_info *pdi,
1352 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1353 int need_pc, struct dwarf2_cu *cu);
1354
1355 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1356
1357 static struct partial_die_info *load_partial_dies
1358 (const struct die_reader_specs *, const gdb_byte *, int);
1359
1360 /* A pair of partial_die_info and compilation unit. */
1361 struct cu_partial_die_info
1362 {
1363 /* The compilation unit of the partial_die_info. */
1364 struct dwarf2_cu *cu;
1365 /* A partial_die_info. */
1366 struct partial_die_info *pdi;
1367
1368 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1369 : cu (cu),
1370 pdi (pdi)
1371 { /* Nothing. */ }
1372
1373 private:
1374 cu_partial_die_info () = delete;
1375 };
1376
1377 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1378 struct dwarf2_cu *);
1379
1380 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1381 struct attribute *,
1382 const struct attr_abbrev *,
1383 const gdb_byte *);
1384
1385 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1386 struct attribute *attr, dwarf_tag tag);
1387
1388 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1389
1390 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1391 dwarf2_section_info *, sect_offset);
1392
1393 static const char *read_indirect_string
1394 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1395 const struct comp_unit_head *, unsigned int *);
1396
1397 static const char *read_indirect_string_at_offset
1398 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1399
1400 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1401 const gdb_byte *,
1402 unsigned int *);
1403
1404 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1405 ULONGEST str_index);
1406
1407 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1408 ULONGEST str_index);
1409
1410 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1411
1412 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1413 struct dwarf2_cu *);
1414
1415 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1416 struct dwarf2_cu *cu);
1417
1418 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1419
1420 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1421 struct dwarf2_cu *cu);
1422
1423 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1424
1425 static struct die_info *die_specification (struct die_info *die,
1426 struct dwarf2_cu **);
1427
1428 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1429 struct dwarf2_cu *cu);
1430
1431 static void dwarf_decode_lines (struct line_header *, const char *,
1432 struct dwarf2_cu *, dwarf2_psymtab *,
1433 CORE_ADDR, int decode_mapping);
1434
1435 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1436 const char *);
1437
1438 static struct symbol *new_symbol (struct die_info *, struct type *,
1439 struct dwarf2_cu *, struct symbol * = NULL);
1440
1441 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1442 struct dwarf2_cu *);
1443
1444 static void dwarf2_const_value_attr (const struct attribute *attr,
1445 struct type *type,
1446 const char *name,
1447 struct obstack *obstack,
1448 struct dwarf2_cu *cu, LONGEST *value,
1449 const gdb_byte **bytes,
1450 struct dwarf2_locexpr_baton **baton);
1451
1452 static struct type *read_subrange_index_type (struct die_info *die,
1453 struct dwarf2_cu *cu);
1454
1455 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1456
1457 static int need_gnat_info (struct dwarf2_cu *);
1458
1459 static struct type *die_descriptive_type (struct die_info *,
1460 struct dwarf2_cu *);
1461
1462 static void set_descriptive_type (struct type *, struct die_info *,
1463 struct dwarf2_cu *);
1464
1465 static struct type *die_containing_type (struct die_info *,
1466 struct dwarf2_cu *);
1467
1468 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1469 struct dwarf2_cu *);
1470
1471 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1472
1473 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1474
1475 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1476
1477 static char *typename_concat (struct obstack *obs, const char *prefix,
1478 const char *suffix, int physname,
1479 struct dwarf2_cu *cu);
1480
1481 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1482
1483 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1484
1485 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1486
1487 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1488
1489 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1490
1491 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1492
1493 /* Return the .debug_loclists section to use for cu. */
1494 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1495
1496 /* Return the .debug_rnglists section to use for cu. */
1497 static struct dwarf2_section_info *cu_debug_rnglists_section
1498 (struct dwarf2_cu *cu, dwarf_tag tag);
1499
1500 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1501 values. Keep the items ordered with increasing constraints compliance. */
1502 enum pc_bounds_kind
1503 {
1504 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1505 PC_BOUNDS_NOT_PRESENT,
1506
1507 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1508 were present but they do not form a valid range of PC addresses. */
1509 PC_BOUNDS_INVALID,
1510
1511 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1512 PC_BOUNDS_RANGES,
1513
1514 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1515 PC_BOUNDS_HIGH_LOW,
1516 };
1517
1518 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1519 CORE_ADDR *, CORE_ADDR *,
1520 struct dwarf2_cu *,
1521 dwarf2_psymtab *);
1522
1523 static void get_scope_pc_bounds (struct die_info *,
1524 CORE_ADDR *, CORE_ADDR *,
1525 struct dwarf2_cu *);
1526
1527 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1528 CORE_ADDR, struct dwarf2_cu *);
1529
1530 static void dwarf2_add_field (struct field_info *, struct die_info *,
1531 struct dwarf2_cu *);
1532
1533 static void dwarf2_attach_fields_to_type (struct field_info *,
1534 struct type *, struct dwarf2_cu *);
1535
1536 static void dwarf2_add_member_fn (struct field_info *,
1537 struct die_info *, struct type *,
1538 struct dwarf2_cu *);
1539
1540 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1541 struct type *,
1542 struct dwarf2_cu *);
1543
1544 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1545
1546 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1547
1548 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1549
1550 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1551
1552 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1553
1554 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1555
1556 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1557
1558 static struct type *read_module_type (struct die_info *die,
1559 struct dwarf2_cu *cu);
1560
1561 static const char *namespace_name (struct die_info *die,
1562 int *is_anonymous, struct dwarf2_cu *);
1563
1564 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1565
1566 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1567 bool * = nullptr);
1568
1569 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1570 struct dwarf2_cu *);
1571
1572 static struct die_info *read_die_and_siblings_1
1573 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1574 struct die_info *);
1575
1576 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1577 const gdb_byte *info_ptr,
1578 const gdb_byte **new_info_ptr,
1579 struct die_info *parent);
1580
1581 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1582 struct die_info **, const gdb_byte *,
1583 int);
1584
1585 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1586 struct die_info **, const gdb_byte *);
1587
1588 static void process_die (struct die_info *, struct dwarf2_cu *);
1589
1590 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1591 struct objfile *);
1592
1593 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1594
1595 static const char *dwarf2_full_name (const char *name,
1596 struct die_info *die,
1597 struct dwarf2_cu *cu);
1598
1599 static const char *dwarf2_physname (const char *name, struct die_info *die,
1600 struct dwarf2_cu *cu);
1601
1602 static struct die_info *dwarf2_extension (struct die_info *die,
1603 struct dwarf2_cu **);
1604
1605 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1606
1607 static void dump_die_for_error (struct die_info *);
1608
1609 static void dump_die_1 (struct ui_file *, int level, int max_level,
1610 struct die_info *);
1611
1612 /*static*/ void dump_die (struct die_info *, int max_level);
1613
1614 static void store_in_ref_table (struct die_info *,
1615 struct dwarf2_cu *);
1616
1617 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1618 const struct attribute *,
1619 struct dwarf2_cu **);
1620
1621 static struct die_info *follow_die_ref (struct die_info *,
1622 const struct attribute *,
1623 struct dwarf2_cu **);
1624
1625 static struct die_info *follow_die_sig (struct die_info *,
1626 const struct attribute *,
1627 struct dwarf2_cu **);
1628
1629 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1630 struct dwarf2_cu *);
1631
1632 static struct type *get_DW_AT_signature_type (struct die_info *,
1633 const struct attribute *,
1634 struct dwarf2_cu *);
1635
1636 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1637 dwarf2_per_objfile *per_objfile);
1638
1639 static void read_signatured_type (signatured_type *sig_type,
1640 dwarf2_per_objfile *per_objfile);
1641
1642 static int attr_to_dynamic_prop (const struct attribute *attr,
1643 struct die_info *die, struct dwarf2_cu *cu,
1644 struct dynamic_prop *prop, struct type *type);
1645
1646 /* memory allocation interface */
1647
1648 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1649
1650 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1651
1652 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1653
1654 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1655 struct dwarf2_loclist_baton *baton,
1656 const struct attribute *attr);
1657
1658 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1659 struct symbol *sym,
1660 struct dwarf2_cu *cu,
1661 int is_block);
1662
1663 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1664 const gdb_byte *info_ptr,
1665 const struct abbrev_info *abbrev);
1666
1667 static hashval_t partial_die_hash (const void *item);
1668
1669 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1670
1671 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1672 (sect_offset sect_off, unsigned int offset_in_dwz,
1673 dwarf2_per_objfile *per_objfile);
1674
1675 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1676 struct die_info *comp_unit_die,
1677 enum language pretend_language);
1678
1679 static struct type *set_die_type (struct die_info *, struct type *,
1680 struct dwarf2_cu *, bool = false);
1681
1682 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1683
1684 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1685 dwarf2_per_objfile *per_objfile,
1686 dwarf2_cu *existing_cu,
1687 bool skip_partial,
1688 enum language pretend_language);
1689
1690 static void process_full_comp_unit (dwarf2_cu *cu,
1691 enum language pretend_language);
1692
1693 static void process_full_type_unit (dwarf2_cu *cu,
1694 enum language pretend_language);
1695
1696 static void dwarf2_add_dependence (struct dwarf2_cu *,
1697 struct dwarf2_per_cu_data *);
1698
1699 static void dwarf2_mark (struct dwarf2_cu *);
1700
1701 static struct type *get_die_type_at_offset (sect_offset,
1702 dwarf2_per_cu_data *per_cu,
1703 dwarf2_per_objfile *per_objfile);
1704
1705 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1706
1707 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1708 dwarf2_per_objfile *per_objfile,
1709 enum language pretend_language);
1710
1711 static void process_queue (dwarf2_per_objfile *per_objfile);
1712
1713 /* Class, the destructor of which frees all allocated queue entries. This
1714 will only have work to do if an error was thrown while processing the
1715 dwarf. If no error was thrown then the queue entries should have all
1716 been processed, and freed, as we went along. */
1717
1718 class dwarf2_queue_guard
1719 {
1720 public:
1721 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1722 : m_per_objfile (per_objfile)
1723 {
1724 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1725
1726 m_per_objfile->per_bfd->queue.emplace ();
1727 }
1728
1729 /* Free any entries remaining on the queue. There should only be
1730 entries left if we hit an error while processing the dwarf. */
1731 ~dwarf2_queue_guard ()
1732 {
1733 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1734
1735 m_per_objfile->per_bfd->queue.reset ();
1736 }
1737
1738 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1739
1740 private:
1741 dwarf2_per_objfile *m_per_objfile;
1742 };
1743
1744 dwarf2_queue_item::~dwarf2_queue_item ()
1745 {
1746 /* Anything still marked queued is likely to be in an
1747 inconsistent state, so discard it. */
1748 if (per_cu->queued)
1749 {
1750 per_objfile->remove_cu (per_cu);
1751 per_cu->queued = 0;
1752 }
1753 }
1754
1755 /* The return type of find_file_and_directory. Note, the enclosed
1756 string pointers are only valid while this object is valid. */
1757
1758 struct file_and_directory
1759 {
1760 /* The filename. This is never NULL. */
1761 const char *name;
1762
1763 /* The compilation directory. NULL if not known. If we needed to
1764 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1765 points directly to the DW_AT_comp_dir string attribute owned by
1766 the obstack that owns the DIE. */
1767 const char *comp_dir;
1768
1769 /* If we needed to build a new string for comp_dir, this is what
1770 owns the storage. */
1771 std::string comp_dir_storage;
1772 };
1773
1774 static file_and_directory find_file_and_directory (struct die_info *die,
1775 struct dwarf2_cu *cu);
1776
1777 static htab_up allocate_signatured_type_table ();
1778
1779 static htab_up allocate_dwo_unit_table ();
1780
1781 static struct dwo_unit *lookup_dwo_unit_in_dwp
1782 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1783 const char *comp_dir, ULONGEST signature, int is_debug_types);
1784
1785 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1786
1787 static struct dwo_unit *lookup_dwo_comp_unit
1788 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1789 ULONGEST signature);
1790
1791 static struct dwo_unit *lookup_dwo_type_unit
1792 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1793
1794 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1795
1796 /* A unique pointer to a dwo_file. */
1797
1798 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1799
1800 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1801
1802 static void check_producer (struct dwarf2_cu *cu);
1803
1804 static void free_line_header_voidp (void *arg);
1805 \f
1806 /* Various complaints about symbol reading that don't abort the process. */
1807
1808 static void
1809 dwarf2_debug_line_missing_file_complaint (void)
1810 {
1811 complaint (_(".debug_line section has line data without a file"));
1812 }
1813
1814 static void
1815 dwarf2_debug_line_missing_end_sequence_complaint (void)
1816 {
1817 complaint (_(".debug_line section has line "
1818 "program sequence without an end"));
1819 }
1820
1821 static void
1822 dwarf2_complex_location_expr_complaint (void)
1823 {
1824 complaint (_("location expression too complex"));
1825 }
1826
1827 static void
1828 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1829 int arg3)
1830 {
1831 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1832 arg1, arg2, arg3);
1833 }
1834
1835 static void
1836 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1837 {
1838 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1839 arg1, arg2);
1840 }
1841
1842 /* Hash function for line_header_hash. */
1843
1844 static hashval_t
1845 line_header_hash (const struct line_header *ofs)
1846 {
1847 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1848 }
1849
1850 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1851
1852 static hashval_t
1853 line_header_hash_voidp (const void *item)
1854 {
1855 const struct line_header *ofs = (const struct line_header *) item;
1856
1857 return line_header_hash (ofs);
1858 }
1859
1860 /* Equality function for line_header_hash. */
1861
1862 static int
1863 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1864 {
1865 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1866 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1867
1868 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1869 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1870 }
1871
1872 \f
1873
1874 /* See declaration. */
1875
1876 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1877 bool can_copy_)
1878 : obfd (obfd),
1879 can_copy (can_copy_)
1880 {
1881 if (names == NULL)
1882 names = &dwarf2_elf_names;
1883
1884 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1885 locate_sections (obfd, sec, *names);
1886 }
1887
1888 dwarf2_per_bfd::~dwarf2_per_bfd ()
1889 {
1890 for (auto &per_cu : all_comp_units)
1891 per_cu->imported_symtabs_free ();
1892
1893 /* Everything else should be on this->obstack. */
1894 }
1895
1896 /* See read.h. */
1897
1898 void
1899 dwarf2_per_objfile::remove_all_cus ()
1900 {
1901 gdb_assert (!this->per_bfd->queue.has_value ());
1902
1903 for (auto pair : m_dwarf2_cus)
1904 delete pair.second;
1905
1906 m_dwarf2_cus.clear ();
1907 }
1908
1909 /* A helper class that calls free_cached_comp_units on
1910 destruction. */
1911
1912 class free_cached_comp_units
1913 {
1914 public:
1915
1916 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1917 : m_per_objfile (per_objfile)
1918 {
1919 }
1920
1921 ~free_cached_comp_units ()
1922 {
1923 m_per_objfile->remove_all_cus ();
1924 }
1925
1926 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1927
1928 private:
1929
1930 dwarf2_per_objfile *m_per_objfile;
1931 };
1932
1933 /* See read.h. */
1934
1935 bool
1936 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1937 {
1938 gdb_assert (per_cu->index < this->m_symtabs.size ());
1939
1940 return this->m_symtabs[per_cu->index] != nullptr;
1941 }
1942
1943 /* See read.h. */
1944
1945 compunit_symtab *
1946 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1947 {
1948 gdb_assert (per_cu->index < this->m_symtabs.size ());
1949
1950 return this->m_symtabs[per_cu->index];
1951 }
1952
1953 /* See read.h. */
1954
1955 void
1956 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1957 compunit_symtab *symtab)
1958 {
1959 gdb_assert (per_cu->index < this->m_symtabs.size ());
1960 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1961
1962 this->m_symtabs[per_cu->index] = symtab;
1963 }
1964
1965 /* Try to locate the sections we need for DWARF 2 debugging
1966 information and return true if we have enough to do something.
1967 NAMES points to the dwarf2 section names, or is NULL if the standard
1968 ELF names are used. CAN_COPY is true for formats where symbol
1969 interposition is possible and so symbol values must follow copy
1970 relocation rules. */
1971
1972 int
1973 dwarf2_has_info (struct objfile *objfile,
1974 const struct dwarf2_debug_sections *names,
1975 bool can_copy)
1976 {
1977 if (objfile->flags & OBJF_READNEVER)
1978 return 0;
1979
1980 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1981
1982 if (per_objfile == NULL)
1983 {
1984 dwarf2_per_bfd *per_bfd;
1985
1986 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1987 BFD doesn't require relocations.
1988
1989 We don't share with objfiles for which -readnow was requested,
1990 because it would complicate things when loading the same BFD with
1991 -readnow and then without -readnow. */
1992 if (!gdb_bfd_requires_relocations (objfile->obfd)
1993 && (objfile->flags & OBJF_READNOW) == 0)
1994 {
1995 /* See if one has been created for this BFD yet. */
1996 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1997
1998 if (per_bfd == nullptr)
1999 {
2000 /* No, create it now. */
2001 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2002 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
2003 }
2004 }
2005 else
2006 {
2007 /* No sharing possible, create one specifically for this objfile. */
2008 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
2009 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
2010 }
2011
2012 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
2013 }
2014
2015 return (!per_objfile->per_bfd->info.is_virtual
2016 && per_objfile->per_bfd->info.s.section != NULL
2017 && !per_objfile->per_bfd->abbrev.is_virtual
2018 && per_objfile->per_bfd->abbrev.s.section != NULL);
2019 }
2020
2021 /* See declaration. */
2022
2023 void
2024 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
2025 const dwarf2_debug_sections &names)
2026 {
2027 flagword aflag = bfd_section_flags (sectp);
2028
2029 if ((aflag & SEC_HAS_CONTENTS) == 0)
2030 {
2031 }
2032 else if (elf_section_data (sectp)->this_hdr.sh_size
2033 > bfd_get_file_size (abfd))
2034 {
2035 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2036 warning (_("Discarding section %s which has a section size (%s"
2037 ") larger than the file size [in module %s]"),
2038 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2039 bfd_get_filename (abfd));
2040 }
2041 else if (names.info.matches (sectp->name))
2042 {
2043 this->info.s.section = sectp;
2044 this->info.size = bfd_section_size (sectp);
2045 }
2046 else if (names.abbrev.matches (sectp->name))
2047 {
2048 this->abbrev.s.section = sectp;
2049 this->abbrev.size = bfd_section_size (sectp);
2050 }
2051 else if (names.line.matches (sectp->name))
2052 {
2053 this->line.s.section = sectp;
2054 this->line.size = bfd_section_size (sectp);
2055 }
2056 else if (names.loc.matches (sectp->name))
2057 {
2058 this->loc.s.section = sectp;
2059 this->loc.size = bfd_section_size (sectp);
2060 }
2061 else if (names.loclists.matches (sectp->name))
2062 {
2063 this->loclists.s.section = sectp;
2064 this->loclists.size = bfd_section_size (sectp);
2065 }
2066 else if (names.macinfo.matches (sectp->name))
2067 {
2068 this->macinfo.s.section = sectp;
2069 this->macinfo.size = bfd_section_size (sectp);
2070 }
2071 else if (names.macro.matches (sectp->name))
2072 {
2073 this->macro.s.section = sectp;
2074 this->macro.size = bfd_section_size (sectp);
2075 }
2076 else if (names.str.matches (sectp->name))
2077 {
2078 this->str.s.section = sectp;
2079 this->str.size = bfd_section_size (sectp);
2080 }
2081 else if (names.str_offsets.matches (sectp->name))
2082 {
2083 this->str_offsets.s.section = sectp;
2084 this->str_offsets.size = bfd_section_size (sectp);
2085 }
2086 else if (names.line_str.matches (sectp->name))
2087 {
2088 this->line_str.s.section = sectp;
2089 this->line_str.size = bfd_section_size (sectp);
2090 }
2091 else if (names.addr.matches (sectp->name))
2092 {
2093 this->addr.s.section = sectp;
2094 this->addr.size = bfd_section_size (sectp);
2095 }
2096 else if (names.frame.matches (sectp->name))
2097 {
2098 this->frame.s.section = sectp;
2099 this->frame.size = bfd_section_size (sectp);
2100 }
2101 else if (names.eh_frame.matches (sectp->name))
2102 {
2103 this->eh_frame.s.section = sectp;
2104 this->eh_frame.size = bfd_section_size (sectp);
2105 }
2106 else if (names.ranges.matches (sectp->name))
2107 {
2108 this->ranges.s.section = sectp;
2109 this->ranges.size = bfd_section_size (sectp);
2110 }
2111 else if (names.rnglists.matches (sectp->name))
2112 {
2113 this->rnglists.s.section = sectp;
2114 this->rnglists.size = bfd_section_size (sectp);
2115 }
2116 else if (names.types.matches (sectp->name))
2117 {
2118 struct dwarf2_section_info type_section;
2119
2120 memset (&type_section, 0, sizeof (type_section));
2121 type_section.s.section = sectp;
2122 type_section.size = bfd_section_size (sectp);
2123
2124 this->types.push_back (type_section);
2125 }
2126 else if (names.gdb_index.matches (sectp->name))
2127 {
2128 this->gdb_index.s.section = sectp;
2129 this->gdb_index.size = bfd_section_size (sectp);
2130 }
2131 else if (names.debug_names.matches (sectp->name))
2132 {
2133 this->debug_names.s.section = sectp;
2134 this->debug_names.size = bfd_section_size (sectp);
2135 }
2136 else if (names.debug_aranges.matches (sectp->name))
2137 {
2138 this->debug_aranges.s.section = sectp;
2139 this->debug_aranges.size = bfd_section_size (sectp);
2140 }
2141
2142 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2143 && bfd_section_vma (sectp) == 0)
2144 this->has_section_at_zero = true;
2145 }
2146
2147 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2148 SECTION_NAME. */
2149
2150 void
2151 dwarf2_get_section_info (struct objfile *objfile,
2152 enum dwarf2_section_enum sect,
2153 asection **sectp, const gdb_byte **bufp,
2154 bfd_size_type *sizep)
2155 {
2156 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2157 struct dwarf2_section_info *info;
2158
2159 /* We may see an objfile without any DWARF, in which case we just
2160 return nothing. */
2161 if (per_objfile == NULL)
2162 {
2163 *sectp = NULL;
2164 *bufp = NULL;
2165 *sizep = 0;
2166 return;
2167 }
2168 switch (sect)
2169 {
2170 case DWARF2_DEBUG_FRAME:
2171 info = &per_objfile->per_bfd->frame;
2172 break;
2173 case DWARF2_EH_FRAME:
2174 info = &per_objfile->per_bfd->eh_frame;
2175 break;
2176 default:
2177 gdb_assert_not_reached ("unexpected section");
2178 }
2179
2180 info->read (objfile);
2181
2182 *sectp = info->get_bfd_section ();
2183 *bufp = info->buffer;
2184 *sizep = info->size;
2185 }
2186
2187 \f
2188 /* DWARF quick_symbol_functions support. */
2189
2190 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2191 unique line tables, so we maintain a separate table of all .debug_line
2192 derived entries to support the sharing.
2193 All the quick functions need is the list of file names. We discard the
2194 line_header when we're done and don't need to record it here. */
2195 struct quick_file_names
2196 {
2197 /* The data used to construct the hash key. */
2198 struct stmt_list_hash hash;
2199
2200 /* The number of entries in file_names, real_names. */
2201 unsigned int num_file_names;
2202
2203 /* The file names from the line table, after being run through
2204 file_full_name. */
2205 const char **file_names;
2206
2207 /* The file names from the line table after being run through
2208 gdb_realpath. These are computed lazily. */
2209 const char **real_names;
2210 };
2211
2212 /* When using the index (and thus not using psymtabs), each CU has an
2213 object of this type. This is used to hold information needed by
2214 the various "quick" methods. */
2215 struct dwarf2_per_cu_quick_data
2216 {
2217 /* The file table. This can be NULL if there was no file table
2218 or it's currently not read in.
2219 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2220 struct quick_file_names *file_names;
2221
2222 /* A temporary mark bit used when iterating over all CUs in
2223 expand_symtabs_matching. */
2224 unsigned int mark : 1;
2225
2226 /* True if we've tried to read the file table and found there isn't one.
2227 There will be no point in trying to read it again next time. */
2228 unsigned int no_file_data : 1;
2229 };
2230
2231 /* A subclass of psymbol_functions that arranges to read the DWARF
2232 partial symbols when needed. */
2233 struct lazy_dwarf_reader : public psymbol_functions
2234 {
2235 using psymbol_functions::psymbol_functions;
2236
2237 bool can_lazily_read_symbols () override
2238 {
2239 return true;
2240 }
2241
2242 void read_partial_symbols (struct objfile *objfile) override
2243 {
2244 if (dwarf2_has_info (objfile, nullptr))
2245 dwarf2_build_psymtabs (objfile, this);
2246 }
2247 };
2248
2249 static quick_symbol_functions_up
2250 make_lazy_dwarf_reader ()
2251 {
2252 return quick_symbol_functions_up (new lazy_dwarf_reader);
2253 }
2254
2255 struct dwarf2_base_index_functions : public quick_symbol_functions
2256 {
2257 bool has_symbols (struct objfile *objfile) override;
2258
2259 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2260
2261 void forget_cached_source_info (struct objfile *objfile) override;
2262
2263 enum language lookup_global_symbol_language (struct objfile *objfile,
2264 const char *name,
2265 domain_enum domain,
2266 bool *symbol_found_p) override
2267 {
2268 *symbol_found_p = false;
2269 return language_unknown;
2270 }
2271
2272 void print_stats (struct objfile *objfile, bool print_bcache) override;
2273
2274 void expand_all_symtabs (struct objfile *objfile) override;
2275
2276 struct compunit_symtab *find_pc_sect_compunit_symtab
2277 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2278 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2279
2280 struct compunit_symtab *find_compunit_symtab_by_address
2281 (struct objfile *objfile, CORE_ADDR address) override
2282 {
2283 return nullptr;
2284 }
2285
2286 void map_symbol_filenames (struct objfile *objfile,
2287 gdb::function_view<symbol_filename_ftype> fun,
2288 bool need_fullname) override;
2289 };
2290
2291 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2292 {
2293 void dump (struct objfile *objfile) override;
2294
2295 void expand_matching_symbols
2296 (struct objfile *,
2297 const lookup_name_info &lookup_name,
2298 domain_enum domain,
2299 int global,
2300 symbol_compare_ftype *ordered_compare) override;
2301
2302 bool expand_symtabs_matching
2303 (struct objfile *objfile,
2304 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2305 const lookup_name_info *lookup_name,
2306 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2307 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2308 block_search_flags search_flags,
2309 domain_enum domain,
2310 enum search_domain kind) override;
2311 };
2312
2313 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2314 {
2315 void dump (struct objfile *objfile) override;
2316
2317 void expand_matching_symbols
2318 (struct objfile *,
2319 const lookup_name_info &lookup_name,
2320 domain_enum domain,
2321 int global,
2322 symbol_compare_ftype *ordered_compare) override;
2323
2324 bool expand_symtabs_matching
2325 (struct objfile *objfile,
2326 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2327 const lookup_name_info *lookup_name,
2328 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2329 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2330 block_search_flags search_flags,
2331 domain_enum domain,
2332 enum search_domain kind) override;
2333 };
2334
2335 static quick_symbol_functions_up
2336 make_dwarf_gdb_index ()
2337 {
2338 return quick_symbol_functions_up (new dwarf2_gdb_index);
2339 }
2340
2341 static quick_symbol_functions_up
2342 make_dwarf_debug_names ()
2343 {
2344 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2345 }
2346
2347 /* Utility hash function for a stmt_list_hash. */
2348
2349 static hashval_t
2350 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2351 {
2352 hashval_t v = 0;
2353
2354 if (stmt_list_hash->dwo_unit != NULL)
2355 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2356 v += to_underlying (stmt_list_hash->line_sect_off);
2357 return v;
2358 }
2359
2360 /* Utility equality function for a stmt_list_hash. */
2361
2362 static int
2363 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2364 const struct stmt_list_hash *rhs)
2365 {
2366 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2367 return 0;
2368 if (lhs->dwo_unit != NULL
2369 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2370 return 0;
2371
2372 return lhs->line_sect_off == rhs->line_sect_off;
2373 }
2374
2375 /* Hash function for a quick_file_names. */
2376
2377 static hashval_t
2378 hash_file_name_entry (const void *e)
2379 {
2380 const struct quick_file_names *file_data
2381 = (const struct quick_file_names *) e;
2382
2383 return hash_stmt_list_entry (&file_data->hash);
2384 }
2385
2386 /* Equality function for a quick_file_names. */
2387
2388 static int
2389 eq_file_name_entry (const void *a, const void *b)
2390 {
2391 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2392 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2393
2394 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2395 }
2396
2397 /* Delete function for a quick_file_names. */
2398
2399 static void
2400 delete_file_name_entry (void *e)
2401 {
2402 struct quick_file_names *file_data = (struct quick_file_names *) e;
2403 int i;
2404
2405 for (i = 0; i < file_data->num_file_names; ++i)
2406 {
2407 xfree ((void*) file_data->file_names[i]);
2408 if (file_data->real_names)
2409 xfree ((void*) file_data->real_names[i]);
2410 }
2411
2412 /* The space for the struct itself lives on the obstack, so we don't
2413 free it here. */
2414 }
2415
2416 /* Create a quick_file_names hash table. */
2417
2418 static htab_up
2419 create_quick_file_names_table (unsigned int nr_initial_entries)
2420 {
2421 return htab_up (htab_create_alloc (nr_initial_entries,
2422 hash_file_name_entry, eq_file_name_entry,
2423 delete_file_name_entry, xcalloc, xfree));
2424 }
2425
2426 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2427 function is unrelated to symtabs, symtab would have to be created afterwards.
2428 You should call age_cached_comp_units after processing the CU. */
2429
2430 static dwarf2_cu *
2431 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2432 bool skip_partial)
2433 {
2434 if (per_cu->is_debug_types)
2435 load_full_type_unit (per_cu, per_objfile);
2436 else
2437 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2438 skip_partial, language_minimal);
2439
2440 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2441 if (cu == nullptr)
2442 return nullptr; /* Dummy CU. */
2443
2444 dwarf2_find_base_address (cu->dies, cu);
2445
2446 return cu;
2447 }
2448
2449 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2450
2451 static void
2452 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2453 dwarf2_per_objfile *per_objfile, bool skip_partial)
2454 {
2455 /* Skip type_unit_groups, reading the type units they contain
2456 is handled elsewhere. */
2457 if (per_cu->type_unit_group_p ())
2458 return;
2459
2460 {
2461 /* The destructor of dwarf2_queue_guard frees any entries left on
2462 the queue. After this point we're guaranteed to leave this function
2463 with the dwarf queue empty. */
2464 dwarf2_queue_guard q_guard (per_objfile);
2465
2466 if (!per_objfile->symtab_set_p (per_cu))
2467 {
2468 queue_comp_unit (per_cu, per_objfile, language_minimal);
2469 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2470
2471 /* If we just loaded a CU from a DWO, and we're working with an index
2472 that may badly handle TUs, load all the TUs in that DWO as well.
2473 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2474 if (!per_cu->is_debug_types
2475 && cu != NULL
2476 && cu->dwo_unit != NULL
2477 && per_objfile->per_bfd->index_table != NULL
2478 && per_objfile->per_bfd->index_table->version <= 7
2479 /* DWP files aren't supported yet. */
2480 && get_dwp_file (per_objfile) == NULL)
2481 queue_and_load_all_dwo_tus (cu);
2482 }
2483
2484 process_queue (per_objfile);
2485 }
2486
2487 /* Age the cache, releasing compilation units that have not
2488 been used recently. */
2489 per_objfile->age_comp_units ();
2490 }
2491
2492 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2493 the per-objfile for which this symtab is instantiated.
2494
2495 Returns the resulting symbol table. */
2496
2497 static struct compunit_symtab *
2498 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2499 dwarf2_per_objfile *per_objfile,
2500 bool skip_partial)
2501 {
2502 gdb_assert (per_objfile->per_bfd->using_index);
2503
2504 if (!per_objfile->symtab_set_p (per_cu))
2505 {
2506 free_cached_comp_units freer (per_objfile);
2507 scoped_restore decrementer = increment_reading_symtab ();
2508 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2509 process_cu_includes (per_objfile);
2510 }
2511
2512 return per_objfile->get_symtab (per_cu);
2513 }
2514
2515 /* See read.h. */
2516
2517 std::unique_ptr<dwarf2_per_cu_data>
2518 dwarf2_per_bfd::allocate_per_cu ()
2519 {
2520 std::unique_ptr<dwarf2_per_cu_data> result (new dwarf2_per_cu_data);
2521 result->per_bfd = this;
2522 result->index = m_num_psymtabs++;
2523 return result;
2524 }
2525
2526 /* See read.h. */
2527
2528 std::unique_ptr<signatured_type>
2529 dwarf2_per_bfd::allocate_signatured_type ()
2530 {
2531 std::unique_ptr<signatured_type> result (new signatured_type);
2532 result->per_bfd = this;
2533 result->index = m_num_psymtabs++;
2534 tu_stats.nr_tus++;
2535 return result;
2536 }
2537
2538 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2539 obstack, and constructed with the specified field values. */
2540
2541 static std::unique_ptr<dwarf2_per_cu_data>
2542 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2543 struct dwarf2_section_info *section,
2544 int is_dwz,
2545 sect_offset sect_off, ULONGEST length)
2546 {
2547 std::unique_ptr<dwarf2_per_cu_data> the_cu = per_bfd->allocate_per_cu ();
2548 the_cu->sect_off = sect_off;
2549 the_cu->length = length;
2550 the_cu->section = section;
2551 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2552 struct dwarf2_per_cu_quick_data);
2553 the_cu->is_dwz = is_dwz;
2554 return the_cu;
2555 }
2556
2557 /* A helper for create_cus_from_index that handles a given list of
2558 CUs. */
2559
2560 static void
2561 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2562 const gdb_byte *cu_list, offset_type n_elements,
2563 struct dwarf2_section_info *section,
2564 int is_dwz)
2565 {
2566 for (offset_type i = 0; i < n_elements; i += 2)
2567 {
2568 gdb_static_assert (sizeof (ULONGEST) >= 8);
2569
2570 sect_offset sect_off
2571 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2572 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2573 cu_list += 2 * 8;
2574
2575 std::unique_ptr<dwarf2_per_cu_data> per_cu
2576 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2577 length);
2578 per_bfd->all_comp_units.push_back (std::move (per_cu));
2579 }
2580 }
2581
2582 /* Read the CU list from the mapped index, and use it to create all
2583 the CU objects for PER_BFD. */
2584
2585 static void
2586 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2587 const gdb_byte *cu_list, offset_type cu_list_elements,
2588 const gdb_byte *dwz_list, offset_type dwz_elements)
2589 {
2590 gdb_assert (per_bfd->all_comp_units.empty ());
2591 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2592
2593 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2594 &per_bfd->info, 0);
2595
2596 if (dwz_elements == 0)
2597 return;
2598
2599 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2600 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2601 &dwz->info, 1);
2602 }
2603
2604 /* Create the signatured type hash table from the index. */
2605
2606 static void
2607 create_signatured_type_table_from_index
2608 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2609 const gdb_byte *bytes, offset_type elements)
2610 {
2611 htab_up sig_types_hash = allocate_signatured_type_table ();
2612
2613 for (offset_type i = 0; i < elements; i += 3)
2614 {
2615 std::unique_ptr<signatured_type> sig_type;
2616 ULONGEST signature;
2617 void **slot;
2618 cu_offset type_offset_in_tu;
2619
2620 gdb_static_assert (sizeof (ULONGEST) >= 8);
2621 sect_offset sect_off
2622 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2623 type_offset_in_tu
2624 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2625 BFD_ENDIAN_LITTLE);
2626 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2627 bytes += 3 * 8;
2628
2629 sig_type = per_bfd->allocate_signatured_type ();
2630 sig_type->signature = signature;
2631 sig_type->type_offset_in_tu = type_offset_in_tu;
2632 sig_type->is_debug_types = 1;
2633 sig_type->section = section;
2634 sig_type->sect_off = sect_off;
2635 sig_type->v.quick
2636 = OBSTACK_ZALLOC (&per_bfd->obstack,
2637 struct dwarf2_per_cu_quick_data);
2638
2639 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2640 *slot = sig_type.get ();
2641
2642 per_bfd->all_comp_units.push_back (std::move (sig_type));
2643 }
2644
2645 per_bfd->signatured_types = std::move (sig_types_hash);
2646 }
2647
2648 /* Create the signatured type hash table from .debug_names. */
2649
2650 static void
2651 create_signatured_type_table_from_debug_names
2652 (dwarf2_per_objfile *per_objfile,
2653 const mapped_debug_names &map,
2654 struct dwarf2_section_info *section,
2655 struct dwarf2_section_info *abbrev_section)
2656 {
2657 struct objfile *objfile = per_objfile->objfile;
2658
2659 section->read (objfile);
2660 abbrev_section->read (objfile);
2661
2662 htab_up sig_types_hash = allocate_signatured_type_table ();
2663
2664 for (uint32_t i = 0; i < map.tu_count; ++i)
2665 {
2666 std::unique_ptr<signatured_type> sig_type;
2667 void **slot;
2668
2669 sect_offset sect_off
2670 = (sect_offset) (extract_unsigned_integer
2671 (map.tu_table_reordered + i * map.offset_size,
2672 map.offset_size,
2673 map.dwarf5_byte_order));
2674
2675 comp_unit_head cu_header;
2676 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2677 abbrev_section,
2678 section->buffer + to_underlying (sect_off),
2679 rcuh_kind::TYPE);
2680
2681 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2682 sig_type->signature = cu_header.signature;
2683 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2684 sig_type->is_debug_types = 1;
2685 sig_type->section = section;
2686 sig_type->sect_off = sect_off;
2687 sig_type->v.quick
2688 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2689 struct dwarf2_per_cu_quick_data);
2690
2691 slot = htab_find_slot (sig_types_hash.get (), sig_type.get (), INSERT);
2692 *slot = sig_type.get ();
2693
2694 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type));
2695 }
2696
2697 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2698 }
2699
2700 /* Read the address map data from the mapped index, and use it to
2701 populate the psymtabs_addrmap. */
2702
2703 static void
2704 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2705 struct mapped_index *index)
2706 {
2707 struct objfile *objfile = per_objfile->objfile;
2708 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2709 struct gdbarch *gdbarch = objfile->arch ();
2710 const gdb_byte *iter, *end;
2711 struct addrmap *mutable_map;
2712 CORE_ADDR baseaddr;
2713
2714 auto_obstack temp_obstack;
2715
2716 mutable_map = addrmap_create_mutable (&temp_obstack);
2717
2718 iter = index->address_table.data ();
2719 end = iter + index->address_table.size ();
2720
2721 baseaddr = objfile->text_section_offset ();
2722
2723 while (iter < end)
2724 {
2725 ULONGEST hi, lo, cu_index;
2726 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2727 iter += 8;
2728 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2729 iter += 8;
2730 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2731 iter += 4;
2732
2733 if (lo > hi)
2734 {
2735 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2736 hex_string (lo), hex_string (hi));
2737 continue;
2738 }
2739
2740 if (cu_index >= per_bfd->all_comp_units.size ())
2741 {
2742 complaint (_(".gdb_index address table has invalid CU number %u"),
2743 (unsigned) cu_index);
2744 continue;
2745 }
2746
2747 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2748 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2749 addrmap_set_empty (mutable_map, lo, hi - 1,
2750 per_bfd->get_cu (cu_index));
2751 }
2752
2753 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2754 &per_bfd->obstack);
2755 }
2756
2757 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2758 populate the psymtabs_addrmap. */
2759
2760 static void
2761 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2762 struct dwarf2_section_info *section)
2763 {
2764 struct objfile *objfile = per_objfile->objfile;
2765 bfd *abfd = objfile->obfd;
2766 struct gdbarch *gdbarch = objfile->arch ();
2767 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2768 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2769
2770 auto_obstack temp_obstack;
2771 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2772
2773 std::unordered_map<sect_offset,
2774 dwarf2_per_cu_data *,
2775 gdb::hash_enum<sect_offset>>
2776 debug_info_offset_to_per_cu;
2777 for (const auto &per_cu : per_bfd->all_comp_units)
2778 {
2779 const auto insertpair
2780 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off,
2781 per_cu.get ());
2782 if (!insertpair.second)
2783 {
2784 warning (_("Section .debug_aranges in %s has duplicate "
2785 "debug_info_offset %s, ignoring .debug_aranges."),
2786 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2787 return;
2788 }
2789 }
2790
2791 section->read (objfile);
2792
2793 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2794
2795 const gdb_byte *addr = section->buffer;
2796
2797 while (addr < section->buffer + section->size)
2798 {
2799 const gdb_byte *const entry_addr = addr;
2800 unsigned int bytes_read;
2801
2802 const LONGEST entry_length = read_initial_length (abfd, addr,
2803 &bytes_read);
2804 addr += bytes_read;
2805
2806 const gdb_byte *const entry_end = addr + entry_length;
2807 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2808 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2809 if (addr + entry_length > section->buffer + section->size)
2810 {
2811 warning (_("Section .debug_aranges in %s entry at offset %s "
2812 "length %s exceeds section length %s, "
2813 "ignoring .debug_aranges."),
2814 objfile_name (objfile),
2815 plongest (entry_addr - section->buffer),
2816 plongest (bytes_read + entry_length),
2817 pulongest (section->size));
2818 return;
2819 }
2820
2821 /* The version number. */
2822 const uint16_t version = read_2_bytes (abfd, addr);
2823 addr += 2;
2824 if (version != 2)
2825 {
2826 warning (_("Section .debug_aranges in %s entry at offset %s "
2827 "has unsupported version %d, ignoring .debug_aranges."),
2828 objfile_name (objfile),
2829 plongest (entry_addr - section->buffer), version);
2830 return;
2831 }
2832
2833 const uint64_t debug_info_offset
2834 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2835 addr += offset_size;
2836 const auto per_cu_it
2837 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2838 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2839 {
2840 warning (_("Section .debug_aranges in %s entry at offset %s "
2841 "debug_info_offset %s does not exists, "
2842 "ignoring .debug_aranges."),
2843 objfile_name (objfile),
2844 plongest (entry_addr - section->buffer),
2845 pulongest (debug_info_offset));
2846 return;
2847 }
2848 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2849
2850 const uint8_t address_size = *addr++;
2851 if (address_size < 1 || address_size > 8)
2852 {
2853 warning (_("Section .debug_aranges in %s entry at offset %s "
2854 "address_size %u is invalid, ignoring .debug_aranges."),
2855 objfile_name (objfile),
2856 plongest (entry_addr - section->buffer), address_size);
2857 return;
2858 }
2859
2860 const uint8_t segment_selector_size = *addr++;
2861 if (segment_selector_size != 0)
2862 {
2863 warning (_("Section .debug_aranges in %s entry at offset %s "
2864 "segment_selector_size %u is not supported, "
2865 "ignoring .debug_aranges."),
2866 objfile_name (objfile),
2867 plongest (entry_addr - section->buffer),
2868 segment_selector_size);
2869 return;
2870 }
2871
2872 /* Must pad to an alignment boundary that is twice the address
2873 size. It is undocumented by the DWARF standard but GCC does
2874 use it. */
2875 for (size_t padding = ((-(addr - section->buffer))
2876 & (2 * address_size - 1));
2877 padding > 0; padding--)
2878 if (*addr++ != 0)
2879 {
2880 warning (_("Section .debug_aranges in %s entry at offset %s "
2881 "padding is not zero, ignoring .debug_aranges."),
2882 objfile_name (objfile),
2883 plongest (entry_addr - section->buffer));
2884 return;
2885 }
2886
2887 for (;;)
2888 {
2889 if (addr + 2 * address_size > entry_end)
2890 {
2891 warning (_("Section .debug_aranges in %s entry at offset %s "
2892 "address list is not properly terminated, "
2893 "ignoring .debug_aranges."),
2894 objfile_name (objfile),
2895 plongest (entry_addr - section->buffer));
2896 return;
2897 }
2898 ULONGEST start = extract_unsigned_integer (addr, address_size,
2899 dwarf5_byte_order);
2900 addr += address_size;
2901 ULONGEST length = extract_unsigned_integer (addr, address_size,
2902 dwarf5_byte_order);
2903 addr += address_size;
2904 if (start == 0 && length == 0)
2905 break;
2906 if (start == 0 && !per_bfd->has_section_at_zero)
2907 {
2908 /* Symbol was eliminated due to a COMDAT group. */
2909 continue;
2910 }
2911 ULONGEST end = start + length;
2912 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2913 - baseaddr);
2914 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2915 - baseaddr);
2916 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2917 }
2918 }
2919
2920 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2921 &per_bfd->obstack);
2922 }
2923
2924 /* A helper function that reads the .gdb_index from BUFFER and fills
2925 in MAP. FILENAME is the name of the file containing the data;
2926 it is used for error reporting. DEPRECATED_OK is true if it is
2927 ok to use deprecated sections.
2928
2929 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2930 out parameters that are filled in with information about the CU and
2931 TU lists in the section.
2932
2933 Returns true if all went well, false otherwise. */
2934
2935 static bool
2936 read_gdb_index_from_buffer (const char *filename,
2937 bool deprecated_ok,
2938 gdb::array_view<const gdb_byte> buffer,
2939 struct mapped_index *map,
2940 const gdb_byte **cu_list,
2941 offset_type *cu_list_elements,
2942 const gdb_byte **types_list,
2943 offset_type *types_list_elements)
2944 {
2945 const gdb_byte *addr = &buffer[0];
2946 offset_view metadata (buffer);
2947
2948 /* Version check. */
2949 offset_type version = metadata[0];
2950 /* Versions earlier than 3 emitted every copy of a psymbol. This
2951 causes the index to behave very poorly for certain requests. Version 3
2952 contained incomplete addrmap. So, it seems better to just ignore such
2953 indices. */
2954 if (version < 4)
2955 {
2956 static int warning_printed = 0;
2957 if (!warning_printed)
2958 {
2959 warning (_("Skipping obsolete .gdb_index section in %s."),
2960 filename);
2961 warning_printed = 1;
2962 }
2963 return 0;
2964 }
2965 /* Index version 4 uses a different hash function than index version
2966 5 and later.
2967
2968 Versions earlier than 6 did not emit psymbols for inlined
2969 functions. Using these files will cause GDB not to be able to
2970 set breakpoints on inlined functions by name, so we ignore these
2971 indices unless the user has done
2972 "set use-deprecated-index-sections on". */
2973 if (version < 6 && !deprecated_ok)
2974 {
2975 static int warning_printed = 0;
2976 if (!warning_printed)
2977 {
2978 warning (_("\
2979 Skipping deprecated .gdb_index section in %s.\n\
2980 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2981 to use the section anyway."),
2982 filename);
2983 warning_printed = 1;
2984 }
2985 return 0;
2986 }
2987 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2988 of the TU (for symbols coming from TUs),
2989 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2990 Plus gold-generated indices can have duplicate entries for global symbols,
2991 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2992 These are just performance bugs, and we can't distinguish gdb-generated
2993 indices from gold-generated ones, so issue no warning here. */
2994
2995 /* Indexes with higher version than the one supported by GDB may be no
2996 longer backward compatible. */
2997 if (version > 8)
2998 return 0;
2999
3000 map->version = version;
3001
3002 int i = 1;
3003 *cu_list = addr + metadata[i];
3004 *cu_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3005 ++i;
3006
3007 *types_list = addr + metadata[i];
3008 *types_list_elements = (metadata[i + 1] - metadata[i]) / 8;
3009 ++i;
3010
3011 const gdb_byte *address_table = addr + metadata[i];
3012 const gdb_byte *address_table_end = addr + metadata[i + 1];
3013 map->address_table
3014 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3015 ++i;
3016
3017 const gdb_byte *symbol_table = addr + metadata[i];
3018 const gdb_byte *symbol_table_end = addr + metadata[i + 1];
3019 map->symbol_table
3020 = offset_view (gdb::array_view<const gdb_byte> (symbol_table,
3021 symbol_table_end));
3022
3023 ++i;
3024 map->constant_pool = buffer.slice (metadata[i]);
3025
3026 return 1;
3027 }
3028
3029 /* Callback types for dwarf2_read_gdb_index. */
3030
3031 typedef gdb::function_view
3032 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3033 get_gdb_index_contents_ftype;
3034 typedef gdb::function_view
3035 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3036 get_gdb_index_contents_dwz_ftype;
3037
3038 /* Read .gdb_index. If everything went ok, initialize the "quick"
3039 elements of all the CUs and return 1. Otherwise, return 0. */
3040
3041 static int
3042 dwarf2_read_gdb_index
3043 (dwarf2_per_objfile *per_objfile,
3044 get_gdb_index_contents_ftype get_gdb_index_contents,
3045 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3046 {
3047 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3048 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3049 struct dwz_file *dwz;
3050 struct objfile *objfile = per_objfile->objfile;
3051 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3052
3053 gdb::array_view<const gdb_byte> main_index_contents
3054 = get_gdb_index_contents (objfile, per_bfd);
3055
3056 if (main_index_contents.empty ())
3057 return 0;
3058
3059 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3060 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3061 use_deprecated_index_sections,
3062 main_index_contents, map.get (), &cu_list,
3063 &cu_list_elements, &types_list,
3064 &types_list_elements))
3065 return 0;
3066
3067 /* Don't use the index if it's empty. */
3068 if (map->symbol_table.empty ())
3069 return 0;
3070
3071 /* If there is a .dwz file, read it so we can get its CU list as
3072 well. */
3073 dwz = dwarf2_get_dwz_file (per_bfd);
3074 if (dwz != NULL)
3075 {
3076 struct mapped_index dwz_map;
3077 const gdb_byte *dwz_types_ignore;
3078 offset_type dwz_types_elements_ignore;
3079
3080 gdb::array_view<const gdb_byte> dwz_index_content
3081 = get_gdb_index_contents_dwz (objfile, dwz);
3082
3083 if (dwz_index_content.empty ())
3084 return 0;
3085
3086 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3087 1, dwz_index_content, &dwz_map,
3088 &dwz_list, &dwz_list_elements,
3089 &dwz_types_ignore,
3090 &dwz_types_elements_ignore))
3091 {
3092 warning (_("could not read '.gdb_index' section from %s; skipping"),
3093 bfd_get_filename (dwz->dwz_bfd.get ()));
3094 return 0;
3095 }
3096 }
3097
3098 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3099 dwz_list_elements);
3100
3101 if (types_list_elements)
3102 {
3103 /* We can only handle a single .debug_types when we have an
3104 index. */
3105 if (per_bfd->types.size () != 1)
3106 return 0;
3107
3108 dwarf2_section_info *section = &per_bfd->types[0];
3109
3110 create_signatured_type_table_from_index (per_bfd, section, types_list,
3111 types_list_elements);
3112 }
3113
3114 create_addrmap_from_index (per_objfile, map.get ());
3115
3116 per_bfd->index_table = std::move (map);
3117 per_bfd->using_index = 1;
3118 per_bfd->quick_file_names_table =
3119 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3120
3121 return 1;
3122 }
3123
3124 /* die_reader_func for dw2_get_file_names. */
3125
3126 static void
3127 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3128 struct die_info *comp_unit_die)
3129 {
3130 struct dwarf2_cu *cu = reader->cu;
3131 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3132 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3133 struct dwarf2_per_cu_data *lh_cu;
3134 struct attribute *attr;
3135 void **slot;
3136 struct quick_file_names *qfn;
3137
3138 gdb_assert (! this_cu->is_debug_types);
3139
3140 /* Our callers never want to match partial units -- instead they
3141 will match the enclosing full CU. */
3142 if (comp_unit_die->tag == DW_TAG_partial_unit)
3143 {
3144 this_cu->v.quick->no_file_data = 1;
3145 return;
3146 }
3147
3148 lh_cu = this_cu;
3149 slot = NULL;
3150
3151 line_header_up lh;
3152 sect_offset line_offset {};
3153
3154 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3155 if (attr != nullptr && attr->form_is_unsigned ())
3156 {
3157 struct quick_file_names find_entry;
3158
3159 line_offset = (sect_offset) attr->as_unsigned ();
3160
3161 /* We may have already read in this line header (TU line header sharing).
3162 If we have we're done. */
3163 find_entry.hash.dwo_unit = cu->dwo_unit;
3164 find_entry.hash.line_sect_off = line_offset;
3165 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3166 &find_entry, INSERT);
3167 if (*slot != NULL)
3168 {
3169 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3170 return;
3171 }
3172
3173 lh = dwarf_decode_line_header (line_offset, cu);
3174 }
3175 if (lh == NULL)
3176 {
3177 lh_cu->v.quick->no_file_data = 1;
3178 return;
3179 }
3180
3181 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3182 qfn->hash.dwo_unit = cu->dwo_unit;
3183 qfn->hash.line_sect_off = line_offset;
3184 gdb_assert (slot != NULL);
3185 *slot = qfn;
3186
3187 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3188
3189 int offset = 0;
3190 if (strcmp (fnd.name, "<unknown>") != 0)
3191 ++offset;
3192
3193 qfn->num_file_names = offset + lh->file_names_size ();
3194 qfn->file_names =
3195 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3196 qfn->num_file_names);
3197 if (offset != 0)
3198 qfn->file_names[0] = xstrdup (fnd.name);
3199 for (int i = 0; i < lh->file_names_size (); ++i)
3200 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3201 fnd.comp_dir).release ();
3202 qfn->real_names = NULL;
3203
3204 lh_cu->v.quick->file_names = qfn;
3205 }
3206
3207 /* A helper for the "quick" functions which attempts to read the line
3208 table for THIS_CU. */
3209
3210 static struct quick_file_names *
3211 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3212 dwarf2_per_objfile *per_objfile)
3213 {
3214 /* This should never be called for TUs. */
3215 gdb_assert (! this_cu->is_debug_types);
3216 /* Nor type unit groups. */
3217 gdb_assert (! this_cu->type_unit_group_p ());
3218
3219 if (this_cu->v.quick->file_names != NULL)
3220 return this_cu->v.quick->file_names;
3221 /* If we know there is no line data, no point in looking again. */
3222 if (this_cu->v.quick->no_file_data)
3223 return NULL;
3224
3225 cutu_reader reader (this_cu, per_objfile);
3226 if (!reader.dummy_p)
3227 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3228
3229 if (this_cu->v.quick->no_file_data)
3230 return NULL;
3231 return this_cu->v.quick->file_names;
3232 }
3233
3234 /* A helper for the "quick" functions which computes and caches the
3235 real path for a given file name from the line table. */
3236
3237 static const char *
3238 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3239 struct quick_file_names *qfn, int index)
3240 {
3241 if (qfn->real_names == NULL)
3242 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3243 qfn->num_file_names, const char *);
3244
3245 if (qfn->real_names[index] == NULL)
3246 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3247
3248 return qfn->real_names[index];
3249 }
3250
3251 struct symtab *
3252 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3253 {
3254 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3255 dwarf2_per_cu_data *dwarf_cu
3256 = per_objfile->per_bfd->all_comp_units.back ().get ();
3257 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3258
3259 if (cust == NULL)
3260 return NULL;
3261
3262 return compunit_primary_filetab (cust);
3263 }
3264
3265 /* Traversal function for dw2_forget_cached_source_info. */
3266
3267 static int
3268 dw2_free_cached_file_names (void **slot, void *info)
3269 {
3270 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3271
3272 if (file_data->real_names)
3273 {
3274 int i;
3275
3276 for (i = 0; i < file_data->num_file_names; ++i)
3277 {
3278 xfree ((void*) file_data->real_names[i]);
3279 file_data->real_names[i] = NULL;
3280 }
3281 }
3282
3283 return 1;
3284 }
3285
3286 void
3287 dwarf2_base_index_functions::forget_cached_source_info
3288 (struct objfile *objfile)
3289 {
3290 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3291
3292 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3293 dw2_free_cached_file_names, NULL);
3294 }
3295
3296 /* Struct used to manage iterating over all CUs looking for a symbol. */
3297
3298 struct dw2_symtab_iterator
3299 {
3300 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3301 dwarf2_per_objfile *per_objfile;
3302 /* If set, only look for symbols that match that block. Valid values are
3303 GLOBAL_BLOCK and STATIC_BLOCK. */
3304 gdb::optional<block_enum> block_index;
3305 /* The kind of symbol we're looking for. */
3306 domain_enum domain;
3307 /* The list of CUs from the index entry of the symbol,
3308 or NULL if not found. */
3309 offset_view vec;
3310 /* The next element in VEC to look at. */
3311 int next;
3312 /* The number of elements in VEC, or zero if there is no match. */
3313 int length;
3314 /* Have we seen a global version of the symbol?
3315 If so we can ignore all further global instances.
3316 This is to work around gold/15646, inefficient gold-generated
3317 indices. */
3318 int global_seen;
3319 };
3320
3321 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3322
3323 static void
3324 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3325 dwarf2_per_objfile *per_objfile,
3326 gdb::optional<block_enum> block_index,
3327 domain_enum domain, offset_type namei)
3328 {
3329 iter->per_objfile = per_objfile;
3330 iter->block_index = block_index;
3331 iter->domain = domain;
3332 iter->next = 0;
3333 iter->global_seen = 0;
3334 iter->vec = {};
3335 iter->length = 0;
3336
3337 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3338 /* index is NULL if OBJF_READNOW. */
3339 if (index == NULL)
3340 return;
3341
3342 gdb_assert (!index->symbol_name_slot_invalid (namei));
3343 offset_type vec_idx = index->symbol_vec_index (namei);
3344
3345 iter->vec = offset_view (index->constant_pool.slice (vec_idx));
3346 iter->length = iter->vec[0];
3347 }
3348
3349 /* Return the next matching CU or NULL if there are no more. */
3350
3351 static struct dwarf2_per_cu_data *
3352 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3353 {
3354 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3355
3356 for ( ; iter->next < iter->length; ++iter->next)
3357 {
3358 offset_type cu_index_and_attrs = iter->vec[iter->next + 1];
3359 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3360 gdb_index_symbol_kind symbol_kind =
3361 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3362 /* Only check the symbol attributes if they're present.
3363 Indices prior to version 7 don't record them,
3364 and indices >= 7 may elide them for certain symbols
3365 (gold does this). */
3366 int attrs_valid =
3367 (per_objfile->per_bfd->index_table->version >= 7
3368 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3369
3370 /* Don't crash on bad data. */
3371 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
3372 {
3373 complaint (_(".gdb_index entry has bad CU index"
3374 " [in module %s]"), objfile_name (per_objfile->objfile));
3375 continue;
3376 }
3377
3378 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
3379
3380 /* Skip if already read in. */
3381 if (per_objfile->symtab_set_p (per_cu))
3382 continue;
3383
3384 /* Check static vs global. */
3385 if (attrs_valid)
3386 {
3387 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3388
3389 if (iter->block_index.has_value ())
3390 {
3391 bool want_static = *iter->block_index == STATIC_BLOCK;
3392
3393 if (is_static != want_static)
3394 continue;
3395 }
3396
3397 /* Work around gold/15646. */
3398 if (!is_static
3399 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3400 {
3401 if (iter->global_seen)
3402 continue;
3403
3404 iter->global_seen = 1;
3405 }
3406 }
3407
3408 /* Only check the symbol's kind if it has one. */
3409 if (attrs_valid)
3410 {
3411 switch (iter->domain)
3412 {
3413 case VAR_DOMAIN:
3414 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3415 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3416 /* Some types are also in VAR_DOMAIN. */
3417 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3418 continue;
3419 break;
3420 case STRUCT_DOMAIN:
3421 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3422 continue;
3423 break;
3424 case LABEL_DOMAIN:
3425 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3426 continue;
3427 break;
3428 case MODULE_DOMAIN:
3429 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3430 continue;
3431 break;
3432 default:
3433 break;
3434 }
3435 }
3436
3437 ++iter->next;
3438 return per_cu;
3439 }
3440
3441 return NULL;
3442 }
3443
3444 void
3445 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3446 bool print_bcache)
3447 {
3448 if (print_bcache)
3449 return;
3450
3451 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3452 int total = per_objfile->per_bfd->all_comp_units.size ();
3453 int count = 0;
3454
3455 for (int i = 0; i < total; ++i)
3456 {
3457 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3458
3459 if (!per_objfile->symtab_set_p (per_cu))
3460 ++count;
3461 }
3462 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3463 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3464 }
3465
3466 /* This dumps minimal information about the index.
3467 It is called via "mt print objfiles".
3468 One use is to verify .gdb_index has been loaded by the
3469 gdb.dwarf2/gdb-index.exp testcase. */
3470
3471 void
3472 dwarf2_gdb_index::dump (struct objfile *objfile)
3473 {
3474 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3475
3476 gdb_assert (per_objfile->per_bfd->using_index);
3477 printf_filtered (".gdb_index:");
3478 if (per_objfile->per_bfd->index_table != NULL)
3479 {
3480 printf_filtered (" version %d\n",
3481 per_objfile->per_bfd->index_table->version);
3482 }
3483 else
3484 printf_filtered (" faked for \"readnow\"\n");
3485 printf_filtered ("\n");
3486 }
3487
3488 void
3489 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3490 {
3491 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3492 int total_units = per_objfile->per_bfd->all_comp_units.size ();
3493
3494 for (int i = 0; i < total_units; ++i)
3495 {
3496 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (i);
3497
3498 /* We don't want to directly expand a partial CU, because if we
3499 read it with the wrong language, then assertion failures can
3500 be triggered later on. See PR symtab/23010. So, tell
3501 dw2_instantiate_symtab to skip partial CUs -- any important
3502 partial CU will be read via DW_TAG_imported_unit anyway. */
3503 dw2_instantiate_symtab (per_cu, per_objfile, true);
3504 }
3505 }
3506
3507 static bool
3508 dw2_expand_symtabs_matching_symbol
3509 (mapped_index_base &index,
3510 const lookup_name_info &lookup_name_in,
3511 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3512 gdb::function_view<bool (offset_type)> match_callback,
3513 dwarf2_per_objfile *per_objfile);
3514
3515 static bool
3516 dw2_expand_symtabs_matching_one
3517 (dwarf2_per_cu_data *per_cu,
3518 dwarf2_per_objfile *per_objfile,
3519 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3520 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3521
3522 void
3523 dwarf2_gdb_index::expand_matching_symbols
3524 (struct objfile *objfile,
3525 const lookup_name_info &name, domain_enum domain,
3526 int global,
3527 symbol_compare_ftype *ordered_compare)
3528 {
3529 /* Used for Ada. */
3530 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3531
3532 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3533
3534 if (per_objfile->per_bfd->index_table != nullptr)
3535 {
3536 mapped_index &index = *per_objfile->per_bfd->index_table;
3537
3538 const char *match_name = name.ada ().lookup_name ().c_str ();
3539 auto matcher = [&] (const char *symname)
3540 {
3541 if (ordered_compare == nullptr)
3542 return true;
3543 return ordered_compare (symname, match_name) == 0;
3544 };
3545
3546 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3547 [&] (offset_type namei)
3548 {
3549 struct dw2_symtab_iterator iter;
3550 struct dwarf2_per_cu_data *per_cu;
3551
3552 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3553 namei);
3554 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3555 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3556 nullptr);
3557 return true;
3558 }, per_objfile);
3559 }
3560 else
3561 {
3562 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3563 proceed assuming all symtabs have been read in. */
3564 }
3565 }
3566
3567 /* Starting from a search name, return the string that finds the upper
3568 bound of all strings that start with SEARCH_NAME in a sorted name
3569 list. Returns the empty string to indicate that the upper bound is
3570 the end of the list. */
3571
3572 static std::string
3573 make_sort_after_prefix_name (const char *search_name)
3574 {
3575 /* When looking to complete "func", we find the upper bound of all
3576 symbols that start with "func" by looking for where we'd insert
3577 the closest string that would follow "func" in lexicographical
3578 order. Usually, that's "func"-with-last-character-incremented,
3579 i.e. "fund". Mind non-ASCII characters, though. Usually those
3580 will be UTF-8 multi-byte sequences, but we can't be certain.
3581 Especially mind the 0xff character, which is a valid character in
3582 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3583 rule out compilers allowing it in identifiers. Note that
3584 conveniently, strcmp/strcasecmp are specified to compare
3585 characters interpreted as unsigned char. So what we do is treat
3586 the whole string as a base 256 number composed of a sequence of
3587 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3588 to 0, and carries 1 to the following more-significant position.
3589 If the very first character in SEARCH_NAME ends up incremented
3590 and carries/overflows, then the upper bound is the end of the
3591 list. The string after the empty string is also the empty
3592 string.
3593
3594 Some examples of this operation:
3595
3596 SEARCH_NAME => "+1" RESULT
3597
3598 "abc" => "abd"
3599 "ab\xff" => "ac"
3600 "\xff" "a" "\xff" => "\xff" "b"
3601 "\xff" => ""
3602 "\xff\xff" => ""
3603 "" => ""
3604
3605 Then, with these symbols for example:
3606
3607 func
3608 func1
3609 fund
3610
3611 completing "func" looks for symbols between "func" and
3612 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3613 which finds "func" and "func1", but not "fund".
3614
3615 And with:
3616
3617 funcÿ (Latin1 'ÿ' [0xff])
3618 funcÿ1
3619 fund
3620
3621 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3622 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3623
3624 And with:
3625
3626 ÿÿ (Latin1 'ÿ' [0xff])
3627 ÿÿ1
3628
3629 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3630 the end of the list.
3631 */
3632 std::string after = search_name;
3633 while (!after.empty () && (unsigned char) after.back () == 0xff)
3634 after.pop_back ();
3635 if (!after.empty ())
3636 after.back () = (unsigned char) after.back () + 1;
3637 return after;
3638 }
3639
3640 /* See declaration. */
3641
3642 std::pair<std::vector<name_component>::const_iterator,
3643 std::vector<name_component>::const_iterator>
3644 mapped_index_base::find_name_components_bounds
3645 (const lookup_name_info &lookup_name_without_params, language lang,
3646 dwarf2_per_objfile *per_objfile) const
3647 {
3648 auto *name_cmp
3649 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3650
3651 const char *lang_name
3652 = lookup_name_without_params.language_lookup_name (lang);
3653
3654 /* Comparison function object for lower_bound that matches against a
3655 given symbol name. */
3656 auto lookup_compare_lower = [&] (const name_component &elem,
3657 const char *name)
3658 {
3659 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3660 const char *elem_name = elem_qualified + elem.name_offset;
3661 return name_cmp (elem_name, name) < 0;
3662 };
3663
3664 /* Comparison function object for upper_bound that matches against a
3665 given symbol name. */
3666 auto lookup_compare_upper = [&] (const char *name,
3667 const name_component &elem)
3668 {
3669 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3670 const char *elem_name = elem_qualified + elem.name_offset;
3671 return name_cmp (name, elem_name) < 0;
3672 };
3673
3674 auto begin = this->name_components.begin ();
3675 auto end = this->name_components.end ();
3676
3677 /* Find the lower bound. */
3678 auto lower = [&] ()
3679 {
3680 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3681 return begin;
3682 else
3683 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3684 } ();
3685
3686 /* Find the upper bound. */
3687 auto upper = [&] ()
3688 {
3689 if (lookup_name_without_params.completion_mode ())
3690 {
3691 /* In completion mode, we want UPPER to point past all
3692 symbols names that have the same prefix. I.e., with
3693 these symbols, and completing "func":
3694
3695 function << lower bound
3696 function1
3697 other_function << upper bound
3698
3699 We find the upper bound by looking for the insertion
3700 point of "func"-with-last-character-incremented,
3701 i.e. "fund". */
3702 std::string after = make_sort_after_prefix_name (lang_name);
3703 if (after.empty ())
3704 return end;
3705 return std::lower_bound (lower, end, after.c_str (),
3706 lookup_compare_lower);
3707 }
3708 else
3709 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3710 } ();
3711
3712 return {lower, upper};
3713 }
3714
3715 /* See declaration. */
3716
3717 void
3718 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3719 {
3720 if (!this->name_components.empty ())
3721 return;
3722
3723 this->name_components_casing = case_sensitivity;
3724 auto *name_cmp
3725 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3726
3727 /* The code below only knows how to break apart components of C++
3728 symbol names (and other languages that use '::' as
3729 namespace/module separator) and Ada symbol names. */
3730 auto count = this->symbol_name_count ();
3731 for (offset_type idx = 0; idx < count; idx++)
3732 {
3733 if (this->symbol_name_slot_invalid (idx))
3734 continue;
3735
3736 const char *name = this->symbol_name_at (idx, per_objfile);
3737
3738 /* Add each name component to the name component table. */
3739 unsigned int previous_len = 0;
3740
3741 if (strstr (name, "::") != nullptr)
3742 {
3743 for (unsigned int current_len = cp_find_first_component (name);
3744 name[current_len] != '\0';
3745 current_len += cp_find_first_component (name + current_len))
3746 {
3747 gdb_assert (name[current_len] == ':');
3748 this->name_components.push_back ({previous_len, idx});
3749 /* Skip the '::'. */
3750 current_len += 2;
3751 previous_len = current_len;
3752 }
3753 }
3754 else
3755 {
3756 /* Handle the Ada encoded (aka mangled) form here. */
3757 for (const char *iter = strstr (name, "__");
3758 iter != nullptr;
3759 iter = strstr (iter, "__"))
3760 {
3761 this->name_components.push_back ({previous_len, idx});
3762 iter += 2;
3763 previous_len = iter - name;
3764 }
3765 }
3766
3767 this->name_components.push_back ({previous_len, idx});
3768 }
3769
3770 /* Sort name_components elements by name. */
3771 auto name_comp_compare = [&] (const name_component &left,
3772 const name_component &right)
3773 {
3774 const char *left_qualified
3775 = this->symbol_name_at (left.idx, per_objfile);
3776 const char *right_qualified
3777 = this->symbol_name_at (right.idx, per_objfile);
3778
3779 const char *left_name = left_qualified + left.name_offset;
3780 const char *right_name = right_qualified + right.name_offset;
3781
3782 return name_cmp (left_name, right_name) < 0;
3783 };
3784
3785 std::sort (this->name_components.begin (),
3786 this->name_components.end (),
3787 name_comp_compare);
3788 }
3789
3790 /* Helper for dw2_expand_symtabs_matching that works with a
3791 mapped_index_base instead of the containing objfile. This is split
3792 to a separate function in order to be able to unit test the
3793 name_components matching using a mock mapped_index_base. For each
3794 symbol name that matches, calls MATCH_CALLBACK, passing it the
3795 symbol's index in the mapped_index_base symbol table. */
3796
3797 static bool
3798 dw2_expand_symtabs_matching_symbol
3799 (mapped_index_base &index,
3800 const lookup_name_info &lookup_name_in,
3801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3802 gdb::function_view<bool (offset_type)> match_callback,
3803 dwarf2_per_objfile *per_objfile)
3804 {
3805 lookup_name_info lookup_name_without_params
3806 = lookup_name_in.make_ignore_params ();
3807
3808 /* Build the symbol name component sorted vector, if we haven't
3809 yet. */
3810 index.build_name_components (per_objfile);
3811
3812 /* The same symbol may appear more than once in the range though.
3813 E.g., if we're looking for symbols that complete "w", and we have
3814 a symbol named "w1::w2", we'll find the two name components for
3815 that same symbol in the range. To be sure we only call the
3816 callback once per symbol, we first collect the symbol name
3817 indexes that matched in a temporary vector and ignore
3818 duplicates. */
3819 std::vector<offset_type> matches;
3820
3821 struct name_and_matcher
3822 {
3823 symbol_name_matcher_ftype *matcher;
3824 const char *name;
3825
3826 bool operator== (const name_and_matcher &other) const
3827 {
3828 return matcher == other.matcher && strcmp (name, other.name) == 0;
3829 }
3830 };
3831
3832 /* A vector holding all the different symbol name matchers, for all
3833 languages. */
3834 std::vector<name_and_matcher> matchers;
3835
3836 for (int i = 0; i < nr_languages; i++)
3837 {
3838 enum language lang_e = (enum language) i;
3839
3840 const language_defn *lang = language_def (lang_e);
3841 symbol_name_matcher_ftype *name_matcher
3842 = lang->get_symbol_name_matcher (lookup_name_without_params);
3843
3844 name_and_matcher key {
3845 name_matcher,
3846 lookup_name_without_params.language_lookup_name (lang_e)
3847 };
3848
3849 /* Don't insert the same comparison routine more than once.
3850 Note that we do this linear walk. This is not a problem in
3851 practice because the number of supported languages is
3852 low. */
3853 if (std::find (matchers.begin (), matchers.end (), key)
3854 != matchers.end ())
3855 continue;
3856 matchers.push_back (std::move (key));
3857
3858 auto bounds
3859 = index.find_name_components_bounds (lookup_name_without_params,
3860 lang_e, per_objfile);
3861
3862 /* Now for each symbol name in range, check to see if we have a name
3863 match, and if so, call the MATCH_CALLBACK callback. */
3864
3865 for (; bounds.first != bounds.second; ++bounds.first)
3866 {
3867 const char *qualified
3868 = index.symbol_name_at (bounds.first->idx, per_objfile);
3869
3870 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3871 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3872 continue;
3873
3874 matches.push_back (bounds.first->idx);
3875 }
3876 }
3877
3878 std::sort (matches.begin (), matches.end ());
3879
3880 /* Finally call the callback, once per match. */
3881 ULONGEST prev = -1;
3882 bool result = true;
3883 for (offset_type idx : matches)
3884 {
3885 if (prev != idx)
3886 {
3887 if (!match_callback (idx))
3888 {
3889 result = false;
3890 break;
3891 }
3892 prev = idx;
3893 }
3894 }
3895
3896 /* Above we use a type wider than idx's for 'prev', since 0 and
3897 (offset_type)-1 are both possible values. */
3898 static_assert (sizeof (prev) > sizeof (offset_type), "");
3899
3900 return result;
3901 }
3902
3903 #if GDB_SELF_TEST
3904
3905 namespace selftests { namespace dw2_expand_symtabs_matching {
3906
3907 /* A mock .gdb_index/.debug_names-like name index table, enough to
3908 exercise dw2_expand_symtabs_matching_symbol, which works with the
3909 mapped_index_base interface. Builds an index from the symbol list
3910 passed as parameter to the constructor. */
3911 class mock_mapped_index : public mapped_index_base
3912 {
3913 public:
3914 mock_mapped_index (gdb::array_view<const char *> symbols)
3915 : m_symbol_table (symbols)
3916 {}
3917
3918 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3919
3920 /* Return the number of names in the symbol table. */
3921 size_t symbol_name_count () const override
3922 {
3923 return m_symbol_table.size ();
3924 }
3925
3926 /* Get the name of the symbol at IDX in the symbol table. */
3927 const char *symbol_name_at
3928 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3929 {
3930 return m_symbol_table[idx];
3931 }
3932
3933 private:
3934 gdb::array_view<const char *> m_symbol_table;
3935 };
3936
3937 /* Convenience function that converts a NULL pointer to a "<null>"
3938 string, to pass to print routines. */
3939
3940 static const char *
3941 string_or_null (const char *str)
3942 {
3943 return str != NULL ? str : "<null>";
3944 }
3945
3946 /* Check if a lookup_name_info built from
3947 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3948 index. EXPECTED_LIST is the list of expected matches, in expected
3949 matching order. If no match expected, then an empty list is
3950 specified. Returns true on success. On failure prints a warning
3951 indicating the file:line that failed, and returns false. */
3952
3953 static bool
3954 check_match (const char *file, int line,
3955 mock_mapped_index &mock_index,
3956 const char *name, symbol_name_match_type match_type,
3957 bool completion_mode,
3958 std::initializer_list<const char *> expected_list,
3959 dwarf2_per_objfile *per_objfile)
3960 {
3961 lookup_name_info lookup_name (name, match_type, completion_mode);
3962
3963 bool matched = true;
3964
3965 auto mismatch = [&] (const char *expected_str,
3966 const char *got)
3967 {
3968 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
3969 "expected=\"%s\", got=\"%s\"\n"),
3970 file, line,
3971 (match_type == symbol_name_match_type::FULL
3972 ? "FULL" : "WILD"),
3973 name, string_or_null (expected_str), string_or_null (got));
3974 matched = false;
3975 };
3976
3977 auto expected_it = expected_list.begin ();
3978 auto expected_end = expected_list.end ();
3979
3980 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
3981 nullptr,
3982 [&] (offset_type idx)
3983 {
3984 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
3985 const char *expected_str
3986 = expected_it == expected_end ? NULL : *expected_it++;
3987
3988 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
3989 mismatch (expected_str, matched_name);
3990 return true;
3991 }, per_objfile);
3992
3993 const char *expected_str
3994 = expected_it == expected_end ? NULL : *expected_it++;
3995 if (expected_str != NULL)
3996 mismatch (expected_str, NULL);
3997
3998 return matched;
3999 }
4000
4001 /* The symbols added to the mock mapped_index for testing (in
4002 canonical form). */
4003 static const char *test_symbols[] = {
4004 "function",
4005 "std::bar",
4006 "std::zfunction",
4007 "std::zfunction2",
4008 "w1::w2",
4009 "ns::foo<char*>",
4010 "ns::foo<int>",
4011 "ns::foo<long>",
4012 "ns2::tmpl<int>::foo2",
4013 "(anonymous namespace)::A::B::C",
4014
4015 /* These are used to check that the increment-last-char in the
4016 matching algorithm for completion doesn't match "t1_fund" when
4017 completing "t1_func". */
4018 "t1_func",
4019 "t1_func1",
4020 "t1_fund",
4021 "t1_fund1",
4022
4023 /* A UTF-8 name with multi-byte sequences to make sure that
4024 cp-name-parser understands this as a single identifier ("função"
4025 is "function" in PT). */
4026 u8"u8função",
4027
4028 /* \377 (0xff) is Latin1 'ÿ'. */
4029 "yfunc\377",
4030
4031 /* \377 (0xff) is Latin1 'ÿ'. */
4032 "\377",
4033 "\377\377123",
4034
4035 /* A name with all sorts of complications. Starts with "z" to make
4036 it easier for the completion tests below. */
4037 #define Z_SYM_NAME \
4038 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4039 "::tuple<(anonymous namespace)::ui*, " \
4040 "std::default_delete<(anonymous namespace)::ui>, void>"
4041
4042 Z_SYM_NAME
4043 };
4044
4045 /* Returns true if the mapped_index_base::find_name_component_bounds
4046 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4047 in completion mode. */
4048
4049 static bool
4050 check_find_bounds_finds (mapped_index_base &index,
4051 const char *search_name,
4052 gdb::array_view<const char *> expected_syms,
4053 dwarf2_per_objfile *per_objfile)
4054 {
4055 lookup_name_info lookup_name (search_name,
4056 symbol_name_match_type::FULL, true);
4057
4058 auto bounds = index.find_name_components_bounds (lookup_name,
4059 language_cplus,
4060 per_objfile);
4061
4062 size_t distance = std::distance (bounds.first, bounds.second);
4063 if (distance != expected_syms.size ())
4064 return false;
4065
4066 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4067 {
4068 auto nc_elem = bounds.first + exp_elem;
4069 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4070 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4071 return false;
4072 }
4073
4074 return true;
4075 }
4076
4077 /* Test the lower-level mapped_index::find_name_component_bounds
4078 method. */
4079
4080 static void
4081 test_mapped_index_find_name_component_bounds ()
4082 {
4083 mock_mapped_index mock_index (test_symbols);
4084
4085 mock_index.build_name_components (NULL /* per_objfile */);
4086
4087 /* Test the lower-level mapped_index::find_name_component_bounds
4088 method in completion mode. */
4089 {
4090 static const char *expected_syms[] = {
4091 "t1_func",
4092 "t1_func1",
4093 };
4094
4095 SELF_CHECK (check_find_bounds_finds
4096 (mock_index, "t1_func", expected_syms,
4097 NULL /* per_objfile */));
4098 }
4099
4100 /* Check that the increment-last-char in the name matching algorithm
4101 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4102 {
4103 static const char *expected_syms1[] = {
4104 "\377",
4105 "\377\377123",
4106 };
4107 SELF_CHECK (check_find_bounds_finds
4108 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4109
4110 static const char *expected_syms2[] = {
4111 "\377\377123",
4112 };
4113 SELF_CHECK (check_find_bounds_finds
4114 (mock_index, "\377\377", expected_syms2,
4115 NULL /* per_objfile */));
4116 }
4117 }
4118
4119 /* Test dw2_expand_symtabs_matching_symbol. */
4120
4121 static void
4122 test_dw2_expand_symtabs_matching_symbol ()
4123 {
4124 mock_mapped_index mock_index (test_symbols);
4125
4126 /* We let all tests run until the end even if some fails, for debug
4127 convenience. */
4128 bool any_mismatch = false;
4129
4130 /* Create the expected symbols list (an initializer_list). Needed
4131 because lists have commas, and we need to pass them to CHECK,
4132 which is a macro. */
4133 #define EXPECT(...) { __VA_ARGS__ }
4134
4135 /* Wrapper for check_match that passes down the current
4136 __FILE__/__LINE__. */
4137 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4138 any_mismatch |= !check_match (__FILE__, __LINE__, \
4139 mock_index, \
4140 NAME, MATCH_TYPE, COMPLETION_MODE, \
4141 EXPECTED_LIST, NULL)
4142
4143 /* Identity checks. */
4144 for (const char *sym : test_symbols)
4145 {
4146 /* Should be able to match all existing symbols. */
4147 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4148 EXPECT (sym));
4149
4150 /* Should be able to match all existing symbols with
4151 parameters. */
4152 std::string with_params = std::string (sym) + "(int)";
4153 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4154 EXPECT (sym));
4155
4156 /* Should be able to match all existing symbols with
4157 parameters and qualifiers. */
4158 with_params = std::string (sym) + " ( int ) const";
4159 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4160 EXPECT (sym));
4161
4162 /* This should really find sym, but cp-name-parser.y doesn't
4163 know about lvalue/rvalue qualifiers yet. */
4164 with_params = std::string (sym) + " ( int ) &&";
4165 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4166 {});
4167 }
4168
4169 /* Check that the name matching algorithm for completion doesn't get
4170 confused with Latin1 'ÿ' / 0xff. */
4171 {
4172 static const char str[] = "\377";
4173 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4174 EXPECT ("\377", "\377\377123"));
4175 }
4176
4177 /* Check that the increment-last-char in the matching algorithm for
4178 completion doesn't match "t1_fund" when completing "t1_func". */
4179 {
4180 static const char str[] = "t1_func";
4181 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4182 EXPECT ("t1_func", "t1_func1"));
4183 }
4184
4185 /* Check that completion mode works at each prefix of the expected
4186 symbol name. */
4187 {
4188 static const char str[] = "function(int)";
4189 size_t len = strlen (str);
4190 std::string lookup;
4191
4192 for (size_t i = 1; i < len; i++)
4193 {
4194 lookup.assign (str, i);
4195 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4196 EXPECT ("function"));
4197 }
4198 }
4199
4200 /* While "w" is a prefix of both components, the match function
4201 should still only be called once. */
4202 {
4203 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4204 EXPECT ("w1::w2"));
4205 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4206 EXPECT ("w1::w2"));
4207 }
4208
4209 /* Same, with a "complicated" symbol. */
4210 {
4211 static const char str[] = Z_SYM_NAME;
4212 size_t len = strlen (str);
4213 std::string lookup;
4214
4215 for (size_t i = 1; i < len; i++)
4216 {
4217 lookup.assign (str, i);
4218 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4219 EXPECT (Z_SYM_NAME));
4220 }
4221 }
4222
4223 /* In FULL mode, an incomplete symbol doesn't match. */
4224 {
4225 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4226 {});
4227 }
4228
4229 /* A complete symbol with parameters matches any overload, since the
4230 index has no overload info. */
4231 {
4232 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4233 EXPECT ("std::zfunction", "std::zfunction2"));
4234 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4235 EXPECT ("std::zfunction", "std::zfunction2"));
4236 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4237 EXPECT ("std::zfunction", "std::zfunction2"));
4238 }
4239
4240 /* Check that whitespace is ignored appropriately. A symbol with a
4241 template argument list. */
4242 {
4243 static const char expected[] = "ns::foo<int>";
4244 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4245 EXPECT (expected));
4246 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4247 EXPECT (expected));
4248 }
4249
4250 /* Check that whitespace is ignored appropriately. A symbol with a
4251 template argument list that includes a pointer. */
4252 {
4253 static const char expected[] = "ns::foo<char*>";
4254 /* Try both completion and non-completion modes. */
4255 static const bool completion_mode[2] = {false, true};
4256 for (size_t i = 0; i < 2; i++)
4257 {
4258 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4259 completion_mode[i], EXPECT (expected));
4260 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4261 completion_mode[i], EXPECT (expected));
4262
4263 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4264 completion_mode[i], EXPECT (expected));
4265 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4266 completion_mode[i], EXPECT (expected));
4267 }
4268 }
4269
4270 {
4271 /* Check method qualifiers are ignored. */
4272 static const char expected[] = "ns::foo<char*>";
4273 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4274 symbol_name_match_type::FULL, true, EXPECT (expected));
4275 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4276 symbol_name_match_type::FULL, true, EXPECT (expected));
4277 CHECK_MATCH ("foo < char * > ( int ) const",
4278 symbol_name_match_type::WILD, true, EXPECT (expected));
4279 CHECK_MATCH ("foo < char * > ( int ) &&",
4280 symbol_name_match_type::WILD, true, EXPECT (expected));
4281 }
4282
4283 /* Test lookup names that don't match anything. */
4284 {
4285 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4286 {});
4287
4288 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4289 {});
4290 }
4291
4292 /* Some wild matching tests, exercising "(anonymous namespace)",
4293 which should not be confused with a parameter list. */
4294 {
4295 static const char *syms[] = {
4296 "A::B::C",
4297 "B::C",
4298 "C",
4299 "A :: B :: C ( int )",
4300 "B :: C ( int )",
4301 "C ( int )",
4302 };
4303
4304 for (const char *s : syms)
4305 {
4306 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4307 EXPECT ("(anonymous namespace)::A::B::C"));
4308 }
4309 }
4310
4311 {
4312 static const char expected[] = "ns2::tmpl<int>::foo2";
4313 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4314 EXPECT (expected));
4315 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4316 EXPECT (expected));
4317 }
4318
4319 SELF_CHECK (!any_mismatch);
4320
4321 #undef EXPECT
4322 #undef CHECK_MATCH
4323 }
4324
4325 static void
4326 run_test ()
4327 {
4328 test_mapped_index_find_name_component_bounds ();
4329 test_dw2_expand_symtabs_matching_symbol ();
4330 }
4331
4332 }} // namespace selftests::dw2_expand_symtabs_matching
4333
4334 #endif /* GDB_SELF_TEST */
4335
4336 /* If FILE_MATCHER is NULL or if PER_CU has
4337 dwarf2_per_cu_quick_data::MARK set (see
4338 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4339 EXPANSION_NOTIFY on it. */
4340
4341 static bool
4342 dw2_expand_symtabs_matching_one
4343 (dwarf2_per_cu_data *per_cu,
4344 dwarf2_per_objfile *per_objfile,
4345 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4346 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4347 {
4348 if (file_matcher == NULL || per_cu->v.quick->mark)
4349 {
4350 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4351
4352 compunit_symtab *symtab
4353 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4354 gdb_assert (symtab != nullptr);
4355
4356 if (expansion_notify != NULL && symtab_was_null)
4357 return expansion_notify (symtab);
4358 }
4359 return true;
4360 }
4361
4362 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4363 matched, to expand corresponding CUs that were marked. IDX is the
4364 index of the symbol name that matched. */
4365
4366 static bool
4367 dw2_expand_marked_cus
4368 (dwarf2_per_objfile *per_objfile, offset_type idx,
4369 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4370 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4371 block_search_flags search_flags,
4372 search_domain kind)
4373 {
4374 offset_type vec_len, vec_idx;
4375 bool global_seen = false;
4376 mapped_index &index = *per_objfile->per_bfd->index_table;
4377
4378 offset_view vec (index.constant_pool.slice (index.symbol_vec_index (idx)));
4379 vec_len = vec[0];
4380 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4381 {
4382 offset_type cu_index_and_attrs = vec[vec_idx + 1];
4383 /* This value is only valid for index versions >= 7. */
4384 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4385 gdb_index_symbol_kind symbol_kind =
4386 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4387 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4388 /* Only check the symbol attributes if they're present.
4389 Indices prior to version 7 don't record them,
4390 and indices >= 7 may elide them for certain symbols
4391 (gold does this). */
4392 int attrs_valid =
4393 (index.version >= 7
4394 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4395
4396 /* Work around gold/15646. */
4397 if (attrs_valid
4398 && !is_static
4399 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4400 {
4401 if (global_seen)
4402 continue;
4403
4404 global_seen = true;
4405 }
4406
4407 /* Only check the symbol's kind if it has one. */
4408 if (attrs_valid)
4409 {
4410 if (is_static)
4411 {
4412 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4413 continue;
4414 }
4415 else
4416 {
4417 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4418 continue;
4419 }
4420
4421 switch (kind)
4422 {
4423 case VARIABLES_DOMAIN:
4424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4425 continue;
4426 break;
4427 case FUNCTIONS_DOMAIN:
4428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4429 continue;
4430 break;
4431 case TYPES_DOMAIN:
4432 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4433 continue;
4434 break;
4435 case MODULES_DOMAIN:
4436 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4437 continue;
4438 break;
4439 default:
4440 break;
4441 }
4442 }
4443
4444 /* Don't crash on bad data. */
4445 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
4446 {
4447 complaint (_(".gdb_index entry has bad CU index"
4448 " [in module %s]"), objfile_name (per_objfile->objfile));
4449 continue;
4450 }
4451
4452 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cu (cu_index);
4453 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4454 expansion_notify))
4455 return false;
4456 }
4457
4458 return true;
4459 }
4460
4461 /* If FILE_MATCHER is non-NULL, set all the
4462 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4463 that match FILE_MATCHER. */
4464
4465 static void
4466 dw_expand_symtabs_matching_file_matcher
4467 (dwarf2_per_objfile *per_objfile,
4468 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4469 {
4470 if (file_matcher == NULL)
4471 return;
4472
4473 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4474 htab_eq_pointer,
4475 NULL, xcalloc, xfree));
4476 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4477 htab_eq_pointer,
4478 NULL, xcalloc, xfree));
4479
4480 /* The rule is CUs specify all the files, including those used by
4481 any TU, so there's no need to scan TUs here. */
4482
4483 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4484 {
4485 QUIT;
4486
4487 if (per_cu->is_debug_types)
4488 continue;
4489 per_cu->v.quick->mark = 0;
4490
4491 /* We only need to look at symtabs not already expanded. */
4492 if (per_objfile->symtab_set_p (per_cu.get ()))
4493 continue;
4494
4495 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4496 per_objfile);
4497 if (file_data == NULL)
4498 continue;
4499
4500 if (htab_find (visited_not_found.get (), file_data) != NULL)
4501 continue;
4502 else if (htab_find (visited_found.get (), file_data) != NULL)
4503 {
4504 per_cu->v.quick->mark = 1;
4505 continue;
4506 }
4507
4508 for (int j = 0; j < file_data->num_file_names; ++j)
4509 {
4510 const char *this_real_name;
4511
4512 if (file_matcher (file_data->file_names[j], false))
4513 {
4514 per_cu->v.quick->mark = 1;
4515 break;
4516 }
4517
4518 /* Before we invoke realpath, which can get expensive when many
4519 files are involved, do a quick comparison of the basenames. */
4520 if (!basenames_may_differ
4521 && !file_matcher (lbasename (file_data->file_names[j]),
4522 true))
4523 continue;
4524
4525 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4526 if (file_matcher (this_real_name, false))
4527 {
4528 per_cu->v.quick->mark = 1;
4529 break;
4530 }
4531 }
4532
4533 void **slot = htab_find_slot (per_cu->v.quick->mark
4534 ? visited_found.get ()
4535 : visited_not_found.get (),
4536 file_data, INSERT);
4537 *slot = file_data;
4538 }
4539 }
4540
4541 bool
4542 dwarf2_gdb_index::expand_symtabs_matching
4543 (struct objfile *objfile,
4544 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4545 const lookup_name_info *lookup_name,
4546 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4547 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4548 block_search_flags search_flags,
4549 domain_enum domain,
4550 enum search_domain kind)
4551 {
4552 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4553
4554 /* index_table is NULL if OBJF_READNOW. */
4555 if (!per_objfile->per_bfd->index_table)
4556 return true;
4557
4558 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4559
4560 if (symbol_matcher == NULL && lookup_name == NULL)
4561 {
4562 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4563 {
4564 QUIT;
4565
4566 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
4567 file_matcher,
4568 expansion_notify))
4569 return false;
4570 }
4571 return true;
4572 }
4573
4574 mapped_index &index = *per_objfile->per_bfd->index_table;
4575
4576 bool result
4577 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4578 symbol_matcher,
4579 [&] (offset_type idx)
4580 {
4581 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4582 expansion_notify, search_flags, kind))
4583 return false;
4584 return true;
4585 }, per_objfile);
4586
4587 return result;
4588 }
4589
4590 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4591 symtab. */
4592
4593 static struct compunit_symtab *
4594 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4595 CORE_ADDR pc)
4596 {
4597 int i;
4598
4599 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4600 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4601 return cust;
4602
4603 if (cust->includes == NULL)
4604 return NULL;
4605
4606 for (i = 0; cust->includes[i]; ++i)
4607 {
4608 struct compunit_symtab *s = cust->includes[i];
4609
4610 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4611 if (s != NULL)
4612 return s;
4613 }
4614
4615 return NULL;
4616 }
4617
4618 struct compunit_symtab *
4619 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4620 (struct objfile *objfile,
4621 struct bound_minimal_symbol msymbol,
4622 CORE_ADDR pc,
4623 struct obj_section *section,
4624 int warn_if_readin)
4625 {
4626 struct dwarf2_per_cu_data *data;
4627 struct compunit_symtab *result;
4628
4629 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4630 if (per_objfile->per_bfd->index_addrmap == nullptr)
4631 return NULL;
4632
4633 CORE_ADDR baseaddr = objfile->text_section_offset ();
4634 data = ((struct dwarf2_per_cu_data *)
4635 addrmap_find (per_objfile->per_bfd->index_addrmap,
4636 pc - baseaddr));
4637 if (!data)
4638 return NULL;
4639
4640 if (warn_if_readin && per_objfile->symtab_set_p (data))
4641 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4642 paddress (objfile->arch (), pc));
4643
4644 result = recursively_find_pc_sect_compunit_symtab
4645 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4646
4647 gdb_assert (result != NULL);
4648 return result;
4649 }
4650
4651 void
4652 dwarf2_base_index_functions::map_symbol_filenames
4653 (struct objfile *objfile,
4654 gdb::function_view<symbol_filename_ftype> fun,
4655 bool need_fullname)
4656 {
4657 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4658
4659 /* Use caches to ensure we only call FUN once for each filename. */
4660 filename_seen_cache filenames_cache;
4661 std::unordered_set<quick_file_names *> qfn_cache;
4662
4663 /* The rule is CUs specify all the files, including those used by any TU,
4664 so there's no need to scan TUs here. We can ignore file names coming
4665 from already-expanded CUs. It is possible that an expanded CU might
4666 reuse the file names data from a currently unexpanded CU, in this
4667 case we don't want to report the files from the unexpanded CU. */
4668
4669 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4670 {
4671 if (per_objfile->symtab_set_p (per_cu.get ()))
4672 {
4673 if (per_cu->v.quick->file_names != nullptr)
4674 qfn_cache.insert (per_cu->v.quick->file_names);
4675 }
4676 }
4677
4678 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
4679 {
4680 /* We only need to look at symtabs not already expanded. */
4681 if (per_objfile->symtab_set_p (per_cu.get ()))
4682 continue;
4683
4684 quick_file_names *file_data = dw2_get_file_names (per_cu.get (),
4685 per_objfile);
4686 if (file_data == nullptr
4687 || qfn_cache.find (file_data) != qfn_cache.end ())
4688 continue;
4689
4690 for (int j = 0; j < file_data->num_file_names; ++j)
4691 {
4692 const char *filename = file_data->file_names[j];
4693 filenames_cache.seen (filename);
4694 }
4695 }
4696
4697 filenames_cache.traverse ([&] (const char *filename)
4698 {
4699 gdb::unique_xmalloc_ptr<char> this_real_name;
4700
4701 if (need_fullname)
4702 this_real_name = gdb_realpath (filename);
4703 fun (filename, this_real_name.get ());
4704 });
4705 }
4706
4707 bool
4708 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4709 {
4710 return true;
4711 }
4712
4713 /* DWARF-5 debug_names reader. */
4714
4715 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4716 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4717
4718 /* A helper function that reads the .debug_names section in SECTION
4719 and fills in MAP. FILENAME is the name of the file containing the
4720 section; it is used for error reporting.
4721
4722 Returns true if all went well, false otherwise. */
4723
4724 static bool
4725 read_debug_names_from_section (struct objfile *objfile,
4726 const char *filename,
4727 struct dwarf2_section_info *section,
4728 mapped_debug_names &map)
4729 {
4730 if (section->empty ())
4731 return false;
4732
4733 /* Older elfutils strip versions could keep the section in the main
4734 executable while splitting it for the separate debug info file. */
4735 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4736 return false;
4737
4738 section->read (objfile);
4739
4740 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4741
4742 const gdb_byte *addr = section->buffer;
4743
4744 bfd *const abfd = section->get_bfd_owner ();
4745
4746 unsigned int bytes_read;
4747 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4748 addr += bytes_read;
4749
4750 map.dwarf5_is_dwarf64 = bytes_read != 4;
4751 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4752 if (bytes_read + length != section->size)
4753 {
4754 /* There may be multiple per-CU indices. */
4755 warning (_("Section .debug_names in %s length %s does not match "
4756 "section length %s, ignoring .debug_names."),
4757 filename, plongest (bytes_read + length),
4758 pulongest (section->size));
4759 return false;
4760 }
4761
4762 /* The version number. */
4763 uint16_t version = read_2_bytes (abfd, addr);
4764 addr += 2;
4765 if (version != 5)
4766 {
4767 warning (_("Section .debug_names in %s has unsupported version %d, "
4768 "ignoring .debug_names."),
4769 filename, version);
4770 return false;
4771 }
4772
4773 /* Padding. */
4774 uint16_t padding = read_2_bytes (abfd, addr);
4775 addr += 2;
4776 if (padding != 0)
4777 {
4778 warning (_("Section .debug_names in %s has unsupported padding %d, "
4779 "ignoring .debug_names."),
4780 filename, padding);
4781 return false;
4782 }
4783
4784 /* comp_unit_count - The number of CUs in the CU list. */
4785 map.cu_count = read_4_bytes (abfd, addr);
4786 addr += 4;
4787
4788 /* local_type_unit_count - The number of TUs in the local TU
4789 list. */
4790 map.tu_count = read_4_bytes (abfd, addr);
4791 addr += 4;
4792
4793 /* foreign_type_unit_count - The number of TUs in the foreign TU
4794 list. */
4795 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4796 addr += 4;
4797 if (foreign_tu_count != 0)
4798 {
4799 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4800 "ignoring .debug_names."),
4801 filename, static_cast<unsigned long> (foreign_tu_count));
4802 return false;
4803 }
4804
4805 /* bucket_count - The number of hash buckets in the hash lookup
4806 table. */
4807 map.bucket_count = read_4_bytes (abfd, addr);
4808 addr += 4;
4809
4810 /* name_count - The number of unique names in the index. */
4811 map.name_count = read_4_bytes (abfd, addr);
4812 addr += 4;
4813
4814 /* abbrev_table_size - The size in bytes of the abbreviations
4815 table. */
4816 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4817 addr += 4;
4818
4819 /* augmentation_string_size - The size in bytes of the augmentation
4820 string. This value is rounded up to a multiple of 4. */
4821 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4822 addr += 4;
4823 map.augmentation_is_gdb = ((augmentation_string_size
4824 == sizeof (dwarf5_augmentation))
4825 && memcmp (addr, dwarf5_augmentation,
4826 sizeof (dwarf5_augmentation)) == 0);
4827 augmentation_string_size += (-augmentation_string_size) & 3;
4828 addr += augmentation_string_size;
4829
4830 /* List of CUs */
4831 map.cu_table_reordered = addr;
4832 addr += map.cu_count * map.offset_size;
4833
4834 /* List of Local TUs */
4835 map.tu_table_reordered = addr;
4836 addr += map.tu_count * map.offset_size;
4837
4838 /* Hash Lookup Table */
4839 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4840 addr += map.bucket_count * 4;
4841 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4842 addr += map.name_count * 4;
4843
4844 /* Name Table */
4845 map.name_table_string_offs_reordered = addr;
4846 addr += map.name_count * map.offset_size;
4847 map.name_table_entry_offs_reordered = addr;
4848 addr += map.name_count * map.offset_size;
4849
4850 const gdb_byte *abbrev_table_start = addr;
4851 for (;;)
4852 {
4853 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4854 addr += bytes_read;
4855 if (index_num == 0)
4856 break;
4857
4858 const auto insertpair
4859 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4860 if (!insertpair.second)
4861 {
4862 warning (_("Section .debug_names in %s has duplicate index %s, "
4863 "ignoring .debug_names."),
4864 filename, pulongest (index_num));
4865 return false;
4866 }
4867 mapped_debug_names::index_val &indexval = insertpair.first->second;
4868 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4869 addr += bytes_read;
4870
4871 for (;;)
4872 {
4873 mapped_debug_names::index_val::attr attr;
4874 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4875 addr += bytes_read;
4876 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878 if (attr.form == DW_FORM_implicit_const)
4879 {
4880 attr.implicit_const = read_signed_leb128 (abfd, addr,
4881 &bytes_read);
4882 addr += bytes_read;
4883 }
4884 if (attr.dw_idx == 0 && attr.form == 0)
4885 break;
4886 indexval.attr_vec.push_back (std::move (attr));
4887 }
4888 }
4889 if (addr != abbrev_table_start + abbrev_table_size)
4890 {
4891 warning (_("Section .debug_names in %s has abbreviation_table "
4892 "of size %s vs. written as %u, ignoring .debug_names."),
4893 filename, plongest (addr - abbrev_table_start),
4894 abbrev_table_size);
4895 return false;
4896 }
4897 map.entry_pool = addr;
4898
4899 return true;
4900 }
4901
4902 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4903 list. */
4904
4905 static void
4906 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4907 const mapped_debug_names &map,
4908 dwarf2_section_info &section,
4909 bool is_dwz)
4910 {
4911 if (!map.augmentation_is_gdb)
4912 {
4913 for (uint32_t i = 0; i < map.cu_count; ++i)
4914 {
4915 sect_offset sect_off
4916 = (sect_offset) (extract_unsigned_integer
4917 (map.cu_table_reordered + i * map.offset_size,
4918 map.offset_size,
4919 map.dwarf5_byte_order));
4920 /* We don't know the length of the CU, because the CU list in a
4921 .debug_names index can be incomplete, so we can't use the start
4922 of the next CU as end of this CU. We create the CUs here with
4923 length 0, and in cutu_reader::cutu_reader we'll fill in the
4924 actual length. */
4925 std::unique_ptr<dwarf2_per_cu_data> per_cu
4926 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4927 sect_off, 0);
4928 per_bfd->all_comp_units.push_back (std::move (per_cu));
4929 }
4930 return;
4931 }
4932
4933 sect_offset sect_off_prev;
4934 for (uint32_t i = 0; i <= map.cu_count; ++i)
4935 {
4936 sect_offset sect_off_next;
4937 if (i < map.cu_count)
4938 {
4939 sect_off_next
4940 = (sect_offset) (extract_unsigned_integer
4941 (map.cu_table_reordered + i * map.offset_size,
4942 map.offset_size,
4943 map.dwarf5_byte_order));
4944 }
4945 else
4946 sect_off_next = (sect_offset) section.size;
4947 if (i >= 1)
4948 {
4949 const ULONGEST length = sect_off_next - sect_off_prev;
4950 std::unique_ptr<dwarf2_per_cu_data> per_cu
4951 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4952 sect_off_prev, length);
4953 per_bfd->all_comp_units.push_back (std::move (per_cu));
4954 }
4955 sect_off_prev = sect_off_next;
4956 }
4957 }
4958
4959 /* Read the CU list from the mapped index, and use it to create all
4960 the CU objects for this dwarf2_per_objfile. */
4961
4962 static void
4963 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
4964 const mapped_debug_names &map,
4965 const mapped_debug_names &dwz_map)
4966 {
4967 gdb_assert (per_bfd->all_comp_units.empty ());
4968 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4969
4970 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
4971 false /* is_dwz */);
4972
4973 if (dwz_map.cu_count == 0)
4974 return;
4975
4976 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
4977 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
4978 true /* is_dwz */);
4979 }
4980
4981 /* Read .debug_names. If everything went ok, initialize the "quick"
4982 elements of all the CUs and return true. Otherwise, return false. */
4983
4984 static bool
4985 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
4986 {
4987 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
4988 mapped_debug_names dwz_map;
4989 struct objfile *objfile = per_objfile->objfile;
4990 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
4991
4992 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4993 &per_bfd->debug_names, *map))
4994 return false;
4995
4996 /* Don't use the index if it's empty. */
4997 if (map->name_count == 0)
4998 return false;
4999
5000 /* If there is a .dwz file, read it so we can get its CU list as
5001 well. */
5002 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5003 if (dwz != NULL)
5004 {
5005 if (!read_debug_names_from_section (objfile,
5006 bfd_get_filename (dwz->dwz_bfd.get ()),
5007 &dwz->debug_names, dwz_map))
5008 {
5009 warning (_("could not read '.debug_names' section from %s; skipping"),
5010 bfd_get_filename (dwz->dwz_bfd.get ()));
5011 return false;
5012 }
5013 }
5014
5015 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5016
5017 if (map->tu_count != 0)
5018 {
5019 /* We can only handle a single .debug_types when we have an
5020 index. */
5021 if (per_bfd->types.size () != 1)
5022 return false;
5023
5024 dwarf2_section_info *section = &per_bfd->types[0];
5025
5026 create_signatured_type_table_from_debug_names
5027 (per_objfile, *map, section, &per_bfd->abbrev);
5028 }
5029
5030 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5031
5032 per_bfd->debug_names_table = std::move (map);
5033 per_bfd->using_index = 1;
5034 per_bfd->quick_file_names_table =
5035 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5036
5037 return true;
5038 }
5039
5040 /* Type used to manage iterating over all CUs looking for a symbol for
5041 .debug_names. */
5042
5043 class dw2_debug_names_iterator
5044 {
5045 public:
5046 dw2_debug_names_iterator (const mapped_debug_names &map,
5047 block_search_flags block_index,
5048 domain_enum domain,
5049 const char *name, dwarf2_per_objfile *per_objfile)
5050 : m_map (map), m_block_index (block_index), m_domain (domain),
5051 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5052 m_per_objfile (per_objfile)
5053 {}
5054
5055 dw2_debug_names_iterator (const mapped_debug_names &map,
5056 search_domain search, uint32_t namei,
5057 dwarf2_per_objfile *per_objfile,
5058 domain_enum domain = UNDEF_DOMAIN)
5059 : m_map (map),
5060 m_domain (domain),
5061 m_search (search),
5062 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5063 m_per_objfile (per_objfile)
5064 {}
5065
5066 dw2_debug_names_iterator (const mapped_debug_names &map,
5067 block_search_flags block_index, domain_enum domain,
5068 uint32_t namei, dwarf2_per_objfile *per_objfile)
5069 : m_map (map), m_block_index (block_index), m_domain (domain),
5070 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5071 m_per_objfile (per_objfile)
5072 {}
5073
5074 /* Return the next matching CU or NULL if there are no more. */
5075 dwarf2_per_cu_data *next ();
5076
5077 private:
5078 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5079 const char *name,
5080 dwarf2_per_objfile *per_objfile);
5081 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5082 uint32_t namei,
5083 dwarf2_per_objfile *per_objfile);
5084
5085 /* The internalized form of .debug_names. */
5086 const mapped_debug_names &m_map;
5087
5088 /* Restrict the search to these blocks. */
5089 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5090 | SEARCH_STATIC_BLOCK);
5091
5092 /* The kind of symbol we're looking for. */
5093 const domain_enum m_domain = UNDEF_DOMAIN;
5094 const search_domain m_search = ALL_DOMAIN;
5095
5096 /* The list of CUs from the index entry of the symbol, or NULL if
5097 not found. */
5098 const gdb_byte *m_addr;
5099
5100 dwarf2_per_objfile *m_per_objfile;
5101 };
5102
5103 const char *
5104 mapped_debug_names::namei_to_name
5105 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5106 {
5107 const ULONGEST namei_string_offs
5108 = extract_unsigned_integer ((name_table_string_offs_reordered
5109 + namei * offset_size),
5110 offset_size,
5111 dwarf5_byte_order);
5112 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5113 }
5114
5115 /* Find a slot in .debug_names for the object named NAME. If NAME is
5116 found, return pointer to its pool data. If NAME cannot be found,
5117 return NULL. */
5118
5119 const gdb_byte *
5120 dw2_debug_names_iterator::find_vec_in_debug_names
5121 (const mapped_debug_names &map, const char *name,
5122 dwarf2_per_objfile *per_objfile)
5123 {
5124 int (*cmp) (const char *, const char *);
5125
5126 gdb::unique_xmalloc_ptr<char> without_params;
5127 if (current_language->la_language == language_cplus
5128 || current_language->la_language == language_fortran
5129 || current_language->la_language == language_d)
5130 {
5131 /* NAME is already canonical. Drop any qualifiers as
5132 .debug_names does not contain any. */
5133
5134 if (strchr (name, '(') != NULL)
5135 {
5136 without_params = cp_remove_params (name);
5137 if (without_params != NULL)
5138 name = without_params.get ();
5139 }
5140 }
5141
5142 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5143
5144 const uint32_t full_hash = dwarf5_djb_hash (name);
5145 uint32_t namei
5146 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5147 (map.bucket_table_reordered
5148 + (full_hash % map.bucket_count)), 4,
5149 map.dwarf5_byte_order);
5150 if (namei == 0)
5151 return NULL;
5152 --namei;
5153 if (namei >= map.name_count)
5154 {
5155 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5156 "[in module %s]"),
5157 namei, map.name_count,
5158 objfile_name (per_objfile->objfile));
5159 return NULL;
5160 }
5161
5162 for (;;)
5163 {
5164 const uint32_t namei_full_hash
5165 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5166 (map.hash_table_reordered + namei), 4,
5167 map.dwarf5_byte_order);
5168 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5169 return NULL;
5170
5171 if (full_hash == namei_full_hash)
5172 {
5173 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5174
5175 #if 0 /* An expensive sanity check. */
5176 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5177 {
5178 complaint (_("Wrong .debug_names hash for string at index %u "
5179 "[in module %s]"),
5180 namei, objfile_name (dwarf2_per_objfile->objfile));
5181 return NULL;
5182 }
5183 #endif
5184
5185 if (cmp (namei_string, name) == 0)
5186 {
5187 const ULONGEST namei_entry_offs
5188 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5189 + namei * map.offset_size),
5190 map.offset_size, map.dwarf5_byte_order);
5191 return map.entry_pool + namei_entry_offs;
5192 }
5193 }
5194
5195 ++namei;
5196 if (namei >= map.name_count)
5197 return NULL;
5198 }
5199 }
5200
5201 const gdb_byte *
5202 dw2_debug_names_iterator::find_vec_in_debug_names
5203 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5204 {
5205 if (namei >= map.name_count)
5206 {
5207 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5208 "[in module %s]"),
5209 namei, map.name_count,
5210 objfile_name (per_objfile->objfile));
5211 return NULL;
5212 }
5213
5214 const ULONGEST namei_entry_offs
5215 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5216 + namei * map.offset_size),
5217 map.offset_size, map.dwarf5_byte_order);
5218 return map.entry_pool + namei_entry_offs;
5219 }
5220
5221 /* See dw2_debug_names_iterator. */
5222
5223 dwarf2_per_cu_data *
5224 dw2_debug_names_iterator::next ()
5225 {
5226 if (m_addr == NULL)
5227 return NULL;
5228
5229 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5230 struct objfile *objfile = m_per_objfile->objfile;
5231 bfd *const abfd = objfile->obfd;
5232
5233 again:
5234
5235 unsigned int bytes_read;
5236 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5237 m_addr += bytes_read;
5238 if (abbrev == 0)
5239 return NULL;
5240
5241 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5242 if (indexval_it == m_map.abbrev_map.cend ())
5243 {
5244 complaint (_("Wrong .debug_names undefined abbrev code %s "
5245 "[in module %s]"),
5246 pulongest (abbrev), objfile_name (objfile));
5247 return NULL;
5248 }
5249 const mapped_debug_names::index_val &indexval = indexval_it->second;
5250 enum class symbol_linkage {
5251 unknown,
5252 static_,
5253 extern_,
5254 } symbol_linkage_ = symbol_linkage::unknown;
5255 dwarf2_per_cu_data *per_cu = NULL;
5256 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5257 {
5258 ULONGEST ull;
5259 switch (attr.form)
5260 {
5261 case DW_FORM_implicit_const:
5262 ull = attr.implicit_const;
5263 break;
5264 case DW_FORM_flag_present:
5265 ull = 1;
5266 break;
5267 case DW_FORM_udata:
5268 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5269 m_addr += bytes_read;
5270 break;
5271 case DW_FORM_ref4:
5272 ull = read_4_bytes (abfd, m_addr);
5273 m_addr += 4;
5274 break;
5275 case DW_FORM_ref8:
5276 ull = read_8_bytes (abfd, m_addr);
5277 m_addr += 8;
5278 break;
5279 case DW_FORM_ref_sig8:
5280 ull = read_8_bytes (abfd, m_addr);
5281 m_addr += 8;
5282 break;
5283 default:
5284 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5285 dwarf_form_name (attr.form),
5286 objfile_name (objfile));
5287 return NULL;
5288 }
5289 switch (attr.dw_idx)
5290 {
5291 case DW_IDX_compile_unit:
5292 /* Don't crash on bad data. */
5293 if (ull >= per_bfd->all_comp_units.size ())
5294 {
5295 complaint (_(".debug_names entry has bad CU index %s"
5296 " [in module %s]"),
5297 pulongest (ull),
5298 objfile_name (objfile));
5299 continue;
5300 }
5301 per_cu = per_bfd->get_cu (ull);
5302 break;
5303 case DW_IDX_type_unit:
5304 /* Don't crash on bad data. */
5305 if (ull >= per_bfd->tu_stats.nr_tus)
5306 {
5307 complaint (_(".debug_names entry has bad TU index %s"
5308 " [in module %s]"),
5309 pulongest (ull),
5310 objfile_name (objfile));
5311 continue;
5312 }
5313 per_cu = per_bfd->get_cu (ull + per_bfd->tu_stats.nr_tus);
5314 break;
5315 case DW_IDX_die_offset:
5316 /* In a per-CU index (as opposed to a per-module index), index
5317 entries without CU attribute implicitly refer to the single CU. */
5318 if (per_cu == NULL)
5319 per_cu = per_bfd->get_cu (0);
5320 break;
5321 case DW_IDX_GNU_internal:
5322 if (!m_map.augmentation_is_gdb)
5323 break;
5324 symbol_linkage_ = symbol_linkage::static_;
5325 break;
5326 case DW_IDX_GNU_external:
5327 if (!m_map.augmentation_is_gdb)
5328 break;
5329 symbol_linkage_ = symbol_linkage::extern_;
5330 break;
5331 }
5332 }
5333
5334 /* Skip if already read in. */
5335 if (m_per_objfile->symtab_set_p (per_cu))
5336 goto again;
5337
5338 /* Check static vs global. */
5339 if (symbol_linkage_ != symbol_linkage::unknown)
5340 {
5341 if (symbol_linkage_ == symbol_linkage::static_)
5342 {
5343 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5344 goto again;
5345 }
5346 else
5347 {
5348 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5349 goto again;
5350 }
5351 }
5352
5353 /* Match dw2_symtab_iter_next, symbol_kind
5354 and debug_names::psymbol_tag. */
5355 switch (m_domain)
5356 {
5357 case VAR_DOMAIN:
5358 switch (indexval.dwarf_tag)
5359 {
5360 case DW_TAG_variable:
5361 case DW_TAG_subprogram:
5362 /* Some types are also in VAR_DOMAIN. */
5363 case DW_TAG_typedef:
5364 case DW_TAG_structure_type:
5365 break;
5366 default:
5367 goto again;
5368 }
5369 break;
5370 case STRUCT_DOMAIN:
5371 switch (indexval.dwarf_tag)
5372 {
5373 case DW_TAG_typedef:
5374 case DW_TAG_structure_type:
5375 break;
5376 default:
5377 goto again;
5378 }
5379 break;
5380 case LABEL_DOMAIN:
5381 switch (indexval.dwarf_tag)
5382 {
5383 case 0:
5384 case DW_TAG_variable:
5385 break;
5386 default:
5387 goto again;
5388 }
5389 break;
5390 case MODULE_DOMAIN:
5391 switch (indexval.dwarf_tag)
5392 {
5393 case DW_TAG_module:
5394 break;
5395 default:
5396 goto again;
5397 }
5398 break;
5399 default:
5400 break;
5401 }
5402
5403 /* Match dw2_expand_symtabs_matching, symbol_kind and
5404 debug_names::psymbol_tag. */
5405 switch (m_search)
5406 {
5407 case VARIABLES_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case DW_TAG_variable:
5411 break;
5412 default:
5413 goto again;
5414 }
5415 break;
5416 case FUNCTIONS_DOMAIN:
5417 switch (indexval.dwarf_tag)
5418 {
5419 case DW_TAG_subprogram:
5420 break;
5421 default:
5422 goto again;
5423 }
5424 break;
5425 case TYPES_DOMAIN:
5426 switch (indexval.dwarf_tag)
5427 {
5428 case DW_TAG_typedef:
5429 case DW_TAG_structure_type:
5430 break;
5431 default:
5432 goto again;
5433 }
5434 break;
5435 case MODULES_DOMAIN:
5436 switch (indexval.dwarf_tag)
5437 {
5438 case DW_TAG_module:
5439 break;
5440 default:
5441 goto again;
5442 }
5443 default:
5444 break;
5445 }
5446
5447 return per_cu;
5448 }
5449
5450 /* This dumps minimal information about .debug_names. It is called
5451 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5452 uses this to verify that .debug_names has been loaded. */
5453
5454 void
5455 dwarf2_debug_names_index::dump (struct objfile *objfile)
5456 {
5457 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5458
5459 gdb_assert (per_objfile->per_bfd->using_index);
5460 printf_filtered (".debug_names:");
5461 if (per_objfile->per_bfd->debug_names_table)
5462 printf_filtered (" exists\n");
5463 else
5464 printf_filtered (" faked for \"readnow\"\n");
5465 printf_filtered ("\n");
5466 }
5467
5468 void
5469 dwarf2_debug_names_index::expand_matching_symbols
5470 (struct objfile *objfile,
5471 const lookup_name_info &name, domain_enum domain,
5472 int global,
5473 symbol_compare_ftype *ordered_compare)
5474 {
5475 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5476
5477 /* debug_names_table is NULL if OBJF_READNOW. */
5478 if (!per_objfile->per_bfd->debug_names_table)
5479 return;
5480
5481 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5482 const block_search_flags block_flags
5483 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5484
5485 const char *match_name = name.ada ().lookup_name ().c_str ();
5486 auto matcher = [&] (const char *symname)
5487 {
5488 if (ordered_compare == nullptr)
5489 return true;
5490 return ordered_compare (symname, match_name) == 0;
5491 };
5492
5493 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5494 [&] (offset_type namei)
5495 {
5496 /* The name was matched, now expand corresponding CUs that were
5497 marked. */
5498 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5499 per_objfile);
5500
5501 struct dwarf2_per_cu_data *per_cu;
5502 while ((per_cu = iter.next ()) != NULL)
5503 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5504 nullptr);
5505 return true;
5506 }, per_objfile);
5507 }
5508
5509 bool
5510 dwarf2_debug_names_index::expand_symtabs_matching
5511 (struct objfile *objfile,
5512 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5513 const lookup_name_info *lookup_name,
5514 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5515 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5516 block_search_flags search_flags,
5517 domain_enum domain,
5518 enum search_domain kind)
5519 {
5520 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5521
5522 /* debug_names_table is NULL if OBJF_READNOW. */
5523 if (!per_objfile->per_bfd->debug_names_table)
5524 return true;
5525
5526 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5527
5528 if (symbol_matcher == NULL && lookup_name == NULL)
5529 {
5530 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
5531 {
5532 QUIT;
5533
5534 if (!dw2_expand_symtabs_matching_one (per_cu.get (), per_objfile,
5535 file_matcher,
5536 expansion_notify))
5537 return false;
5538 }
5539 return true;
5540 }
5541
5542 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5543
5544 bool result
5545 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5546 symbol_matcher,
5547 [&] (offset_type namei)
5548 {
5549 /* The name was matched, now expand corresponding CUs that were
5550 marked. */
5551 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5552
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
5555 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5556 file_matcher,
5557 expansion_notify))
5558 return false;
5559 return true;
5560 }, per_objfile);
5561
5562 return result;
5563 }
5564
5565 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5566 to either a dwarf2_per_bfd or dwz_file object. */
5567
5568 template <typename T>
5569 static gdb::array_view<const gdb_byte>
5570 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5571 {
5572 dwarf2_section_info *section = &section_owner->gdb_index;
5573
5574 if (section->empty ())
5575 return {};
5576
5577 /* Older elfutils strip versions could keep the section in the main
5578 executable while splitting it for the separate debug info file. */
5579 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5580 return {};
5581
5582 section->read (obj);
5583
5584 /* dwarf2_section_info::size is a bfd_size_type, while
5585 gdb::array_view works with size_t. On 32-bit hosts, with
5586 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5587 is 32-bit. So we need an explicit narrowing conversion here.
5588 This is fine, because it's impossible to allocate or mmap an
5589 array/buffer larger than what size_t can represent. */
5590 return gdb::make_array_view (section->buffer, section->size);
5591 }
5592
5593 /* Lookup the index cache for the contents of the index associated to
5594 DWARF2_OBJ. */
5595
5596 static gdb::array_view<const gdb_byte>
5597 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5598 {
5599 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5600 if (build_id == nullptr)
5601 return {};
5602
5603 return global_index_cache.lookup_gdb_index (build_id,
5604 &dwarf2_per_bfd->index_cache_res);
5605 }
5606
5607 /* Same as the above, but for DWZ. */
5608
5609 static gdb::array_view<const gdb_byte>
5610 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5611 {
5612 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5613 if (build_id == nullptr)
5614 return {};
5615
5616 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5617 }
5618
5619 /* See dwarf2/public.h. */
5620
5621 void
5622 dwarf2_initialize_objfile (struct objfile *objfile)
5623 {
5624 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5625 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5626
5627 dwarf_read_debug_printf ("called");
5628
5629 /* If we're about to read full symbols, don't bother with the
5630 indices. In this case we also don't care if some other debug
5631 format is making psymtabs, because they are all about to be
5632 expanded anyway. */
5633 if ((objfile->flags & OBJF_READNOW))
5634 {
5635 dwarf_read_debug_printf ("readnow requested");
5636
5637 /* When using READNOW, the using_index flag (set below) indicates that
5638 PER_BFD was already initialized, when we loaded some other objfile. */
5639 if (per_bfd->using_index)
5640 {
5641 dwarf_read_debug_printf ("using_index already set");
5642 per_objfile->resize_symtabs ();
5643 objfile->qf.push_front (make_dwarf_gdb_index ());
5644 return;
5645 }
5646
5647 per_bfd->using_index = 1;
5648 create_all_comp_units (per_objfile);
5649 per_bfd->quick_file_names_table
5650 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5651 per_objfile->resize_symtabs ();
5652
5653 for (int i = 0; i < per_bfd->all_comp_units.size (); ++i)
5654 {
5655 dwarf2_per_cu_data *per_cu = per_bfd->get_cu (i);
5656
5657 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5658 struct dwarf2_per_cu_quick_data);
5659 }
5660
5661 /* Arrange for gdb to see the "quick" functions. However, these
5662 functions will be no-ops because we will have expanded all
5663 symtabs. */
5664 objfile->qf.push_front (make_dwarf_gdb_index ());
5665 return;
5666 }
5667
5668 /* Was a debug names index already read when we processed an objfile sharing
5669 PER_BFD? */
5670 if (per_bfd->debug_names_table != nullptr)
5671 {
5672 dwarf_read_debug_printf ("re-using shared debug names table");
5673 per_objfile->resize_symtabs ();
5674 objfile->qf.push_front (make_dwarf_debug_names ());
5675 return;
5676 }
5677
5678 /* Was a GDB index already read when we processed an objfile sharing
5679 PER_BFD? */
5680 if (per_bfd->index_table != nullptr)
5681 {
5682 dwarf_read_debug_printf ("re-using shared index table");
5683 per_objfile->resize_symtabs ();
5684 objfile->qf.push_front (make_dwarf_gdb_index ());
5685 return;
5686 }
5687
5688 /* There might already be partial symtabs built for this BFD. This happens
5689 when loading the same binary twice with the index-cache enabled. If so,
5690 don't try to read an index. The objfile / per_objfile initialization will
5691 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5692 code path. */
5693 if (per_bfd->partial_symtabs != nullptr)
5694 {
5695 dwarf_read_debug_printf ("re-using shared partial symtabs");
5696 objfile->qf.push_front (make_lazy_dwarf_reader ());
5697 return;
5698 }
5699
5700 if (dwarf2_read_debug_names (per_objfile))
5701 {
5702 dwarf_read_debug_printf ("found debug names");
5703 per_objfile->resize_symtabs ();
5704 objfile->qf.push_front (make_dwarf_debug_names ());
5705 return;
5706 }
5707
5708 if (dwarf2_read_gdb_index (per_objfile,
5709 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5710 get_gdb_index_contents_from_section<dwz_file>))
5711 {
5712 dwarf_read_debug_printf ("found gdb index from file");
5713 per_objfile->resize_symtabs ();
5714 objfile->qf.push_front (make_dwarf_gdb_index ());
5715 return;
5716 }
5717
5718 /* ... otherwise, try to find the index in the index cache. */
5719 if (dwarf2_read_gdb_index (per_objfile,
5720 get_gdb_index_contents_from_cache,
5721 get_gdb_index_contents_from_cache_dwz))
5722 {
5723 dwarf_read_debug_printf ("found gdb index from cache");
5724 global_index_cache.hit ();
5725 per_objfile->resize_symtabs ();
5726 objfile->qf.push_front (make_dwarf_gdb_index ());
5727 return;
5728 }
5729
5730 global_index_cache.miss ();
5731 objfile->qf.push_front (make_lazy_dwarf_reader ());
5732 }
5733
5734 \f
5735
5736 /* Build a partial symbol table. */
5737
5738 void
5739 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5740 {
5741 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5742 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5743
5744 if (per_bfd->partial_symtabs != nullptr)
5745 {
5746 /* Partial symbols were already read, so now we can simply
5747 attach them. */
5748 if (psf == nullptr)
5749 {
5750 psf = new psymbol_functions (per_bfd->partial_symtabs);
5751 objfile->qf.emplace_front (psf);
5752 }
5753 else
5754 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5755 per_objfile->resize_symtabs ();
5756 return;
5757 }
5758
5759 if (psf == nullptr)
5760 {
5761 psf = new psymbol_functions;
5762 objfile->qf.emplace_front (psf);
5763 }
5764 const std::shared_ptr<psymtab_storage> &partial_symtabs
5765 = psf->get_partial_symtabs ();
5766
5767 /* Set the local reference to partial symtabs, so that we don't try
5768 to read them again if reading another objfile with the same BFD.
5769 If we can't in fact share, this won't make a difference anyway as
5770 the dwarf2_per_bfd object won't be shared. */
5771 per_bfd->partial_symtabs = partial_symtabs;
5772
5773 try
5774 {
5775 /* This isn't really ideal: all the data we allocate on the
5776 objfile's obstack is still uselessly kept around. However,
5777 freeing it seems unsafe. */
5778 psymtab_discarder psymtabs (partial_symtabs.get ());
5779 dwarf2_build_psymtabs_hard (per_objfile);
5780 psymtabs.keep ();
5781
5782 per_objfile->resize_symtabs ();
5783
5784 /* (maybe) store an index in the cache. */
5785 global_index_cache.store (per_objfile);
5786 }
5787 catch (const gdb_exception_error &except)
5788 {
5789 exception_print (gdb_stderr, except);
5790 }
5791 }
5792
5793 /* Find the base address of the compilation unit for range lists and
5794 location lists. It will normally be specified by DW_AT_low_pc.
5795 In DWARF-3 draft 4, the base address could be overridden by
5796 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5797 compilation units with discontinuous ranges. */
5798
5799 static void
5800 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5801 {
5802 struct attribute *attr;
5803
5804 cu->base_address.reset ();
5805
5806 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5807 if (attr != nullptr)
5808 cu->base_address = attr->as_address ();
5809 else
5810 {
5811 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5812 if (attr != nullptr)
5813 cu->base_address = attr->as_address ();
5814 }
5815 }
5816
5817 /* Helper function that returns the proper abbrev section for
5818 THIS_CU. */
5819
5820 static struct dwarf2_section_info *
5821 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5822 {
5823 struct dwarf2_section_info *abbrev;
5824 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5825
5826 if (this_cu->is_dwz)
5827 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5828 else
5829 abbrev = &per_bfd->abbrev;
5830
5831 return abbrev;
5832 }
5833
5834 /* Fetch the abbreviation table offset from a comp or type unit header. */
5835
5836 static sect_offset
5837 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5838 struct dwarf2_section_info *section,
5839 sect_offset sect_off)
5840 {
5841 bfd *abfd = section->get_bfd_owner ();
5842 const gdb_byte *info_ptr;
5843 unsigned int initial_length_size, offset_size;
5844 uint16_t version;
5845
5846 section->read (per_objfile->objfile);
5847 info_ptr = section->buffer + to_underlying (sect_off);
5848 read_initial_length (abfd, info_ptr, &initial_length_size);
5849 offset_size = initial_length_size == 4 ? 4 : 8;
5850 info_ptr += initial_length_size;
5851
5852 version = read_2_bytes (abfd, info_ptr);
5853 info_ptr += 2;
5854 if (version >= 5)
5855 {
5856 /* Skip unit type and address size. */
5857 info_ptr += 2;
5858 }
5859
5860 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5861 }
5862
5863 /* A partial symtab that is used only for include files. */
5864 struct dwarf2_include_psymtab : public partial_symtab
5865 {
5866 dwarf2_include_psymtab (const char *filename,
5867 psymtab_storage *partial_symtabs,
5868 objfile_per_bfd_storage *objfile_per_bfd)
5869 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5870 {
5871 }
5872
5873 void read_symtab (struct objfile *objfile) override
5874 {
5875 /* It's an include file, no symbols to read for it.
5876 Everything is in the includer symtab. */
5877
5878 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5879 expansion of the includer psymtab. We use the dependencies[0] field to
5880 model the includer. But if we go the regular route of calling
5881 expand_psymtab here, and having expand_psymtab call expand_dependencies
5882 to expand the includer, we'll only use expand_psymtab on the includer
5883 (making it a non-toplevel psymtab), while if we expand the includer via
5884 another path, we'll use read_symtab (making it a toplevel psymtab).
5885 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5886 psymtab, and trigger read_symtab on the includer here directly. */
5887 includer ()->read_symtab (objfile);
5888 }
5889
5890 void expand_psymtab (struct objfile *objfile) override
5891 {
5892 /* This is not called by read_symtab, and should not be called by any
5893 expand_dependencies. */
5894 gdb_assert (false);
5895 }
5896
5897 bool readin_p (struct objfile *objfile) const override
5898 {
5899 return includer ()->readin_p (objfile);
5900 }
5901
5902 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5903 {
5904 return nullptr;
5905 }
5906
5907 private:
5908 partial_symtab *includer () const
5909 {
5910 /* An include psymtab has exactly one dependency: the psymtab that
5911 includes it. */
5912 gdb_assert (this->number_of_dependencies == 1);
5913 return this->dependencies[0];
5914 }
5915 };
5916
5917 /* Allocate a new partial symtab for file named NAME and mark this new
5918 partial symtab as being an include of PST. */
5919
5920 static void
5921 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5922 const char *name,
5923 dwarf2_psymtab *pst,
5924 psymtab_storage *partial_symtabs,
5925 objfile_per_bfd_storage *objfile_per_bfd)
5926 {
5927 dwarf2_include_psymtab *subpst
5928 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5929
5930 if (!IS_ABSOLUTE_PATH (subpst->filename))
5931 subpst->dirname = pst->dirname;
5932
5933 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5934 subpst->dependencies[0] = pst;
5935 subpst->number_of_dependencies = 1;
5936 }
5937
5938 /* Read the Line Number Program data and extract the list of files
5939 included by the source file represented by PST. Build an include
5940 partial symtab for each of these included files. */
5941
5942 static void
5943 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5944 struct die_info *die,
5945 dwarf2_psymtab *pst)
5946 {
5947 line_header_up lh;
5948 struct attribute *attr;
5949
5950 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5951 if (attr != nullptr && attr->form_is_unsigned ())
5952 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
5953 if (lh == NULL)
5954 return; /* No linetable, so no includes. */
5955
5956 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5957 that we pass in the raw text_low here; that is ok because we're
5958 only decoding the line table to make include partial symtabs, and
5959 so the addresses aren't really used. */
5960 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5961 pst->raw_text_low (), 1);
5962 }
5963
5964 static hashval_t
5965 hash_signatured_type (const void *item)
5966 {
5967 const struct signatured_type *sig_type
5968 = (const struct signatured_type *) item;
5969
5970 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5971 return sig_type->signature;
5972 }
5973
5974 static int
5975 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5976 {
5977 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5978 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5979
5980 return lhs->signature == rhs->signature;
5981 }
5982
5983 /* Allocate a hash table for signatured types. */
5984
5985 static htab_up
5986 allocate_signatured_type_table ()
5987 {
5988 return htab_up (htab_create_alloc (41,
5989 hash_signatured_type,
5990 eq_signatured_type,
5991 NULL, xcalloc, xfree));
5992 }
5993
5994 /* A helper for create_debug_types_hash_table. Read types from SECTION
5995 and fill them into TYPES_HTAB. It will process only type units,
5996 therefore DW_UT_type. */
5997
5998 static void
5999 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6000 struct dwo_file *dwo_file,
6001 dwarf2_section_info *section, htab_up &types_htab,
6002 rcuh_kind section_kind)
6003 {
6004 struct objfile *objfile = per_objfile->objfile;
6005 struct dwarf2_section_info *abbrev_section;
6006 bfd *abfd;
6007 const gdb_byte *info_ptr, *end_ptr;
6008
6009 abbrev_section = &dwo_file->sections.abbrev;
6010
6011 dwarf_read_debug_printf ("Reading %s for %s",
6012 section->get_name (),
6013 abbrev_section->get_file_name ());
6014
6015 section->read (objfile);
6016 info_ptr = section->buffer;
6017
6018 if (info_ptr == NULL)
6019 return;
6020
6021 /* We can't set abfd until now because the section may be empty or
6022 not present, in which case the bfd is unknown. */
6023 abfd = section->get_bfd_owner ();
6024
6025 /* We don't use cutu_reader here because we don't need to read
6026 any dies: the signature is in the header. */
6027
6028 end_ptr = info_ptr + section->size;
6029 while (info_ptr < end_ptr)
6030 {
6031 std::unique_ptr<signatured_type> sig_type;
6032 struct dwo_unit *dwo_tu;
6033 void **slot;
6034 const gdb_byte *ptr = info_ptr;
6035 struct comp_unit_head header;
6036 unsigned int length;
6037
6038 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6039
6040 /* Initialize it due to a false compiler warning. */
6041 header.signature = -1;
6042 header.type_cu_offset_in_tu = (cu_offset) -1;
6043
6044 /* We need to read the type's signature in order to build the hash
6045 table, but we don't need anything else just yet. */
6046
6047 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6048 abbrev_section, ptr, section_kind);
6049
6050 length = header.get_length ();
6051
6052 /* Skip dummy type units. */
6053 if (ptr >= info_ptr + length
6054 || peek_abbrev_code (abfd, ptr) == 0
6055 || (header.unit_type != DW_UT_type
6056 && header.unit_type != DW_UT_split_type))
6057 {
6058 info_ptr += length;
6059 continue;
6060 }
6061
6062 if (types_htab == NULL)
6063 types_htab = allocate_dwo_unit_table ();
6064
6065 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6066 dwo_tu->dwo_file = dwo_file;
6067 dwo_tu->signature = header.signature;
6068 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6069 dwo_tu->section = section;
6070 dwo_tu->sect_off = sect_off;
6071 dwo_tu->length = length;
6072
6073 slot = htab_find_slot (types_htab.get (), dwo_tu, INSERT);
6074 gdb_assert (slot != NULL);
6075 if (*slot != NULL)
6076 complaint (_("debug type entry at offset %s is duplicate to"
6077 " the entry at offset %s, signature %s"),
6078 sect_offset_str (sect_off),
6079 sect_offset_str (dwo_tu->sect_off),
6080 hex_string (header.signature));
6081 *slot = dwo_tu;
6082
6083 dwarf_read_debug_printf_v (" offset %s, signature %s",
6084 sect_offset_str (sect_off),
6085 hex_string (header.signature));
6086
6087 info_ptr += length;
6088 }
6089 }
6090
6091 /* Create the hash table of all entries in the .debug_types
6092 (or .debug_types.dwo) section(s).
6093 DWO_FILE is a pointer to the DWO file object.
6094
6095 The result is a pointer to the hash table or NULL if there are no types.
6096
6097 Note: This function processes DWO files only, not DWP files. */
6098
6099 static void
6100 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6101 struct dwo_file *dwo_file,
6102 gdb::array_view<dwarf2_section_info> type_sections,
6103 htab_up &types_htab)
6104 {
6105 for (dwarf2_section_info &section : type_sections)
6106 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6107 rcuh_kind::TYPE);
6108 }
6109
6110 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6111 If SLOT is non-NULL, it is the entry to use in the hash table.
6112 Otherwise we find one. */
6113
6114 static struct signatured_type *
6115 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6116 {
6117 if (per_objfile->per_bfd->all_comp_units.size ()
6118 == per_objfile->per_bfd->all_comp_units.capacity ())
6119 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6120
6121 std::unique_ptr<signatured_type> sig_type_holder
6122 = per_objfile->per_bfd->allocate_signatured_type ();
6123 signatured_type *sig_type = sig_type_holder.get ();
6124
6125 per_objfile->resize_symtabs ();
6126
6127 per_objfile->per_bfd->all_comp_units.push_back (std::move (sig_type_holder));
6128 sig_type->signature = sig;
6129 sig_type->is_debug_types = 1;
6130 if (per_objfile->per_bfd->using_index)
6131 {
6132 sig_type->v.quick =
6133 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6134 struct dwarf2_per_cu_quick_data);
6135 }
6136
6137 if (slot == NULL)
6138 {
6139 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6140 sig_type, INSERT);
6141 }
6142 gdb_assert (*slot == NULL);
6143 *slot = sig_type;
6144 /* The rest of sig_type must be filled in by the caller. */
6145 return sig_type;
6146 }
6147
6148 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6149 Fill in SIG_ENTRY with DWO_ENTRY. */
6150
6151 static void
6152 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6153 struct signatured_type *sig_entry,
6154 struct dwo_unit *dwo_entry)
6155 {
6156 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6157
6158 /* Make sure we're not clobbering something we don't expect to. */
6159 gdb_assert (! sig_entry->queued);
6160 gdb_assert (per_objfile->get_cu (sig_entry) == NULL);
6161 if (per_bfd->using_index)
6162 {
6163 gdb_assert (sig_entry->v.quick != NULL);
6164 gdb_assert (!per_objfile->symtab_set_p (sig_entry));
6165 }
6166 else
6167 gdb_assert (sig_entry->v.psymtab == NULL);
6168 gdb_assert (sig_entry->signature == dwo_entry->signature);
6169 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6170 gdb_assert (sig_entry->type_unit_group == NULL);
6171 gdb_assert (sig_entry->dwo_unit == NULL);
6172
6173 sig_entry->section = dwo_entry->section;
6174 sig_entry->sect_off = dwo_entry->sect_off;
6175 sig_entry->length = dwo_entry->length;
6176 sig_entry->reading_dwo_directly = 1;
6177 sig_entry->per_bfd = per_bfd;
6178 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6179 sig_entry->dwo_unit = dwo_entry;
6180 }
6181
6182 /* Subroutine of lookup_signatured_type.
6183 If we haven't read the TU yet, create the signatured_type data structure
6184 for a TU to be read in directly from a DWO file, bypassing the stub.
6185 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6186 using .gdb_index, then when reading a CU we want to stay in the DWO file
6187 containing that CU. Otherwise we could end up reading several other DWO
6188 files (due to comdat folding) to process the transitive closure of all the
6189 mentioned TUs, and that can be slow. The current DWO file will have every
6190 type signature that it needs.
6191 We only do this for .gdb_index because in the psymtab case we already have
6192 to read all the DWOs to build the type unit groups. */
6193
6194 static struct signatured_type *
6195 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6196 {
6197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6198 struct dwo_file *dwo_file;
6199 struct dwo_unit find_dwo_entry, *dwo_entry;
6200 struct signatured_type find_sig_entry, *sig_entry;
6201 void **slot;
6202
6203 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6204
6205 /* If TU skeletons have been removed then we may not have read in any
6206 TUs yet. */
6207 if (per_objfile->per_bfd->signatured_types == NULL)
6208 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6209
6210 /* We only ever need to read in one copy of a signatured type.
6211 Use the global signatured_types array to do our own comdat-folding
6212 of types. If this is the first time we're reading this TU, and
6213 the TU has an entry in .gdb_index, replace the recorded data from
6214 .gdb_index with this TU. */
6215
6216 find_sig_entry.signature = sig;
6217 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6218 &find_sig_entry, INSERT);
6219 sig_entry = (struct signatured_type *) *slot;
6220
6221 /* We can get here with the TU already read, *or* in the process of being
6222 read. Don't reassign the global entry to point to this DWO if that's
6223 the case. Also note that if the TU is already being read, it may not
6224 have come from a DWO, the program may be a mix of Fission-compiled
6225 code and non-Fission-compiled code. */
6226
6227 /* Have we already tried to read this TU?
6228 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6229 needn't exist in the global table yet). */
6230 if (sig_entry != NULL && sig_entry->tu_read)
6231 return sig_entry;
6232
6233 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6234 dwo_unit of the TU itself. */
6235 dwo_file = cu->dwo_unit->dwo_file;
6236
6237 /* Ok, this is the first time we're reading this TU. */
6238 if (dwo_file->tus == NULL)
6239 return NULL;
6240 find_dwo_entry.signature = sig;
6241 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6242 &find_dwo_entry);
6243 if (dwo_entry == NULL)
6244 return NULL;
6245
6246 /* If the global table doesn't have an entry for this TU, add one. */
6247 if (sig_entry == NULL)
6248 sig_entry = add_type_unit (per_objfile, sig, slot);
6249
6250 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6251 sig_entry->tu_read = 1;
6252 return sig_entry;
6253 }
6254
6255 /* Subroutine of lookup_signatured_type.
6256 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6257 then try the DWP file. If the TU stub (skeleton) has been removed then
6258 it won't be in .gdb_index. */
6259
6260 static struct signatured_type *
6261 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6262 {
6263 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6264 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6265 struct dwo_unit *dwo_entry;
6266 struct signatured_type find_sig_entry, *sig_entry;
6267 void **slot;
6268
6269 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6270 gdb_assert (dwp_file != NULL);
6271
6272 /* If TU skeletons have been removed then we may not have read in any
6273 TUs yet. */
6274 if (per_objfile->per_bfd->signatured_types == NULL)
6275 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6276
6277 find_sig_entry.signature = sig;
6278 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6279 &find_sig_entry, INSERT);
6280 sig_entry = (struct signatured_type *) *slot;
6281
6282 /* Have we already tried to read this TU?
6283 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6284 needn't exist in the global table yet). */
6285 if (sig_entry != NULL)
6286 return sig_entry;
6287
6288 if (dwp_file->tus == NULL)
6289 return NULL;
6290 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6291 1 /* is_debug_types */);
6292 if (dwo_entry == NULL)
6293 return NULL;
6294
6295 sig_entry = add_type_unit (per_objfile, sig, slot);
6296 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6297
6298 return sig_entry;
6299 }
6300
6301 /* Lookup a signature based type for DW_FORM_ref_sig8.
6302 Returns NULL if signature SIG is not present in the table.
6303 It is up to the caller to complain about this. */
6304
6305 static struct signatured_type *
6306 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6307 {
6308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6309
6310 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6311 {
6312 /* We're in a DWO/DWP file, and we're using .gdb_index.
6313 These cases require special processing. */
6314 if (get_dwp_file (per_objfile) == NULL)
6315 return lookup_dwo_signatured_type (cu, sig);
6316 else
6317 return lookup_dwp_signatured_type (cu, sig);
6318 }
6319 else
6320 {
6321 struct signatured_type find_entry, *entry;
6322
6323 if (per_objfile->per_bfd->signatured_types == NULL)
6324 return NULL;
6325 find_entry.signature = sig;
6326 entry = ((struct signatured_type *)
6327 htab_find (per_objfile->per_bfd->signatured_types.get (),
6328 &find_entry));
6329 return entry;
6330 }
6331 }
6332
6333 /* Low level DIE reading support. */
6334
6335 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6336
6337 static void
6338 init_cu_die_reader (struct die_reader_specs *reader,
6339 struct dwarf2_cu *cu,
6340 struct dwarf2_section_info *section,
6341 struct dwo_file *dwo_file,
6342 struct abbrev_table *abbrev_table)
6343 {
6344 gdb_assert (section->readin && section->buffer != NULL);
6345 reader->abfd = section->get_bfd_owner ();
6346 reader->cu = cu;
6347 reader->dwo_file = dwo_file;
6348 reader->die_section = section;
6349 reader->buffer = section->buffer;
6350 reader->buffer_end = section->buffer + section->size;
6351 reader->abbrev_table = abbrev_table;
6352 }
6353
6354 /* Subroutine of cutu_reader to simplify it.
6355 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6356 There's just a lot of work to do, and cutu_reader is big enough
6357 already.
6358
6359 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6360 from it to the DIE in the DWO. If NULL we are skipping the stub.
6361 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6362 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6363 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6364 STUB_COMP_DIR may be non-NULL.
6365 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6366 are filled in with the info of the DIE from the DWO file.
6367 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6368 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6369 kept around for at least as long as *RESULT_READER.
6370
6371 The result is non-zero if a valid (non-dummy) DIE was found. */
6372
6373 static int
6374 read_cutu_die_from_dwo (dwarf2_cu *cu,
6375 struct dwo_unit *dwo_unit,
6376 struct die_info *stub_comp_unit_die,
6377 const char *stub_comp_dir,
6378 struct die_reader_specs *result_reader,
6379 const gdb_byte **result_info_ptr,
6380 struct die_info **result_comp_unit_die,
6381 abbrev_table_up *result_dwo_abbrev_table)
6382 {
6383 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6384 dwarf2_per_cu_data *per_cu = cu->per_cu;
6385 struct objfile *objfile = per_objfile->objfile;
6386 bfd *abfd;
6387 const gdb_byte *begin_info_ptr, *info_ptr;
6388 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6389 int i,num_extra_attrs;
6390 struct dwarf2_section_info *dwo_abbrev_section;
6391 struct die_info *comp_unit_die;
6392
6393 /* At most one of these may be provided. */
6394 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6395
6396 /* These attributes aren't processed until later:
6397 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6398 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6399 referenced later. However, these attributes are found in the stub
6400 which we won't have later. In order to not impose this complication
6401 on the rest of the code, we read them here and copy them to the
6402 DWO CU/TU die. */
6403
6404 stmt_list = NULL;
6405 low_pc = NULL;
6406 high_pc = NULL;
6407 ranges = NULL;
6408 comp_dir = NULL;
6409
6410 if (stub_comp_unit_die != NULL)
6411 {
6412 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6413 DWO file. */
6414 if (!per_cu->is_debug_types)
6415 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6416 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6417 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6418 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6419 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6420
6421 cu->addr_base = stub_comp_unit_die->addr_base ();
6422
6423 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6424 We need the value before we can process DW_AT_ranges values from the
6425 DWO. */
6426 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6427
6428 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6429 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6430 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6431 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6432 section. */
6433 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6434 }
6435 else if (stub_comp_dir != NULL)
6436 {
6437 /* Reconstruct the comp_dir attribute to simplify the code below. */
6438 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6439 comp_dir->name = DW_AT_comp_dir;
6440 comp_dir->form = DW_FORM_string;
6441 comp_dir->set_string_noncanonical (stub_comp_dir);
6442 }
6443
6444 /* Set up for reading the DWO CU/TU. */
6445 cu->dwo_unit = dwo_unit;
6446 dwarf2_section_info *section = dwo_unit->section;
6447 section->read (objfile);
6448 abfd = section->get_bfd_owner ();
6449 begin_info_ptr = info_ptr = (section->buffer
6450 + to_underlying (dwo_unit->sect_off));
6451 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6452
6453 if (per_cu->is_debug_types)
6454 {
6455 signatured_type *sig_type = (struct signatured_type *) per_cu;
6456
6457 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6458 section, dwo_abbrev_section,
6459 info_ptr, rcuh_kind::TYPE);
6460 /* This is not an assert because it can be caused by bad debug info. */
6461 if (sig_type->signature != cu->header.signature)
6462 {
6463 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6464 " TU at offset %s [in module %s]"),
6465 hex_string (sig_type->signature),
6466 hex_string (cu->header.signature),
6467 sect_offset_str (dwo_unit->sect_off),
6468 bfd_get_filename (abfd));
6469 }
6470 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6471 /* For DWOs coming from DWP files, we don't know the CU length
6472 nor the type's offset in the TU until now. */
6473 dwo_unit->length = cu->header.get_length ();
6474 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6475
6476 /* Establish the type offset that can be used to lookup the type.
6477 For DWO files, we don't know it until now. */
6478 sig_type->type_offset_in_section
6479 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6480 }
6481 else
6482 {
6483 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6484 section, dwo_abbrev_section,
6485 info_ptr, rcuh_kind::COMPILE);
6486 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6487 /* For DWOs coming from DWP files, we don't know the CU length
6488 until now. */
6489 dwo_unit->length = cu->header.get_length ();
6490 }
6491
6492 dwo_abbrev_section->read (objfile);
6493 *result_dwo_abbrev_table
6494 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6495 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6496 result_dwo_abbrev_table->get ());
6497
6498 /* Read in the die, but leave space to copy over the attributes
6499 from the stub. This has the benefit of simplifying the rest of
6500 the code - all the work to maintain the illusion of a single
6501 DW_TAG_{compile,type}_unit DIE is done here. */
6502 num_extra_attrs = ((stmt_list != NULL)
6503 + (low_pc != NULL)
6504 + (high_pc != NULL)
6505 + (ranges != NULL)
6506 + (comp_dir != NULL));
6507 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6508 num_extra_attrs);
6509
6510 /* Copy over the attributes from the stub to the DIE we just read in. */
6511 comp_unit_die = *result_comp_unit_die;
6512 i = comp_unit_die->num_attrs;
6513 if (stmt_list != NULL)
6514 comp_unit_die->attrs[i++] = *stmt_list;
6515 if (low_pc != NULL)
6516 comp_unit_die->attrs[i++] = *low_pc;
6517 if (high_pc != NULL)
6518 comp_unit_die->attrs[i++] = *high_pc;
6519 if (ranges != NULL)
6520 comp_unit_die->attrs[i++] = *ranges;
6521 if (comp_dir != NULL)
6522 comp_unit_die->attrs[i++] = *comp_dir;
6523 comp_unit_die->num_attrs += num_extra_attrs;
6524
6525 if (dwarf_die_debug)
6526 {
6527 fprintf_unfiltered (gdb_stdlog,
6528 "Read die from %s@0x%x of %s:\n",
6529 section->get_name (),
6530 (unsigned) (begin_info_ptr - section->buffer),
6531 bfd_get_filename (abfd));
6532 dump_die (comp_unit_die, dwarf_die_debug);
6533 }
6534
6535 /* Skip dummy compilation units. */
6536 if (info_ptr >= begin_info_ptr + dwo_unit->length
6537 || peek_abbrev_code (abfd, info_ptr) == 0)
6538 return 0;
6539
6540 *result_info_ptr = info_ptr;
6541 return 1;
6542 }
6543
6544 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6545 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6546 signature is part of the header. */
6547 static gdb::optional<ULONGEST>
6548 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6549 {
6550 if (cu->header.version >= 5)
6551 return cu->header.signature;
6552 struct attribute *attr;
6553 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6554 if (attr == nullptr || !attr->form_is_unsigned ())
6555 return gdb::optional<ULONGEST> ();
6556 return attr->as_unsigned ();
6557 }
6558
6559 /* Subroutine of cutu_reader to simplify it.
6560 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6561 Returns NULL if the specified DWO unit cannot be found. */
6562
6563 static struct dwo_unit *
6564 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6565 {
6566 dwarf2_per_cu_data *per_cu = cu->per_cu;
6567 struct dwo_unit *dwo_unit;
6568 const char *comp_dir;
6569
6570 gdb_assert (cu != NULL);
6571
6572 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6573 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6574 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6575
6576 if (per_cu->is_debug_types)
6577 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6578 else
6579 {
6580 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6581
6582 if (!signature.has_value ())
6583 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6584 " [in module %s]"),
6585 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6586
6587 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6588 }
6589
6590 return dwo_unit;
6591 }
6592
6593 /* Subroutine of cutu_reader to simplify it.
6594 See it for a description of the parameters.
6595 Read a TU directly from a DWO file, bypassing the stub. */
6596
6597 void
6598 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6599 dwarf2_per_objfile *per_objfile,
6600 dwarf2_cu *existing_cu)
6601 {
6602 struct signatured_type *sig_type;
6603
6604 /* Verify we can do the following downcast, and that we have the
6605 data we need. */
6606 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6607 sig_type = (struct signatured_type *) this_cu;
6608 gdb_assert (sig_type->dwo_unit != NULL);
6609
6610 dwarf2_cu *cu;
6611
6612 if (existing_cu != nullptr)
6613 {
6614 cu = existing_cu;
6615 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6616 /* There's no need to do the rereading_dwo_cu handling that
6617 cutu_reader does since we don't read the stub. */
6618 }
6619 else
6620 {
6621 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6622 in per_objfile yet. */
6623 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6624 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6625 cu = m_new_cu.get ();
6626 }
6627
6628 /* A future optimization, if needed, would be to use an existing
6629 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6630 could share abbrev tables. */
6631
6632 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6633 NULL /* stub_comp_unit_die */,
6634 sig_type->dwo_unit->dwo_file->comp_dir,
6635 this, &info_ptr,
6636 &comp_unit_die,
6637 &m_dwo_abbrev_table) == 0)
6638 {
6639 /* Dummy die. */
6640 dummy_p = true;
6641 }
6642 }
6643
6644 /* Initialize a CU (or TU) and read its DIEs.
6645 If the CU defers to a DWO file, read the DWO file as well.
6646
6647 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6648 Otherwise the table specified in the comp unit header is read in and used.
6649 This is an optimization for when we already have the abbrev table.
6650
6651 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6652 allocated. */
6653
6654 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6655 dwarf2_per_objfile *per_objfile,
6656 struct abbrev_table *abbrev_table,
6657 dwarf2_cu *existing_cu,
6658 bool skip_partial)
6659 : die_reader_specs {},
6660 m_this_cu (this_cu)
6661 {
6662 struct objfile *objfile = per_objfile->objfile;
6663 struct dwarf2_section_info *section = this_cu->section;
6664 bfd *abfd = section->get_bfd_owner ();
6665 const gdb_byte *begin_info_ptr;
6666 struct signatured_type *sig_type = NULL;
6667 struct dwarf2_section_info *abbrev_section;
6668 /* Non-zero if CU currently points to a DWO file and we need to
6669 reread it. When this happens we need to reread the skeleton die
6670 before we can reread the DWO file (this only applies to CUs, not TUs). */
6671 int rereading_dwo_cu = 0;
6672
6673 if (dwarf_die_debug)
6674 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6675 this_cu->is_debug_types ? "type" : "comp",
6676 sect_offset_str (this_cu->sect_off));
6677
6678 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6679 file (instead of going through the stub), short-circuit all of this. */
6680 if (this_cu->reading_dwo_directly)
6681 {
6682 /* Narrow down the scope of possibilities to have to understand. */
6683 gdb_assert (this_cu->is_debug_types);
6684 gdb_assert (abbrev_table == NULL);
6685 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6686 return;
6687 }
6688
6689 /* This is cheap if the section is already read in. */
6690 section->read (objfile);
6691
6692 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6693
6694 abbrev_section = get_abbrev_section_for_cu (this_cu);
6695
6696 dwarf2_cu *cu;
6697
6698 if (existing_cu != nullptr)
6699 {
6700 cu = existing_cu;
6701 /* If this CU is from a DWO file we need to start over, we need to
6702 refetch the attributes from the skeleton CU.
6703 This could be optimized by retrieving those attributes from when we
6704 were here the first time: the previous comp_unit_die was stored in
6705 comp_unit_obstack. But there's no data yet that we need this
6706 optimization. */
6707 if (cu->dwo_unit != NULL)
6708 rereading_dwo_cu = 1;
6709 }
6710 else
6711 {
6712 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6713 in per_objfile yet. */
6714 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6715 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6716 cu = m_new_cu.get ();
6717 }
6718
6719 /* Get the header. */
6720 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6721 {
6722 /* We already have the header, there's no need to read it in again. */
6723 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6724 }
6725 else
6726 {
6727 if (this_cu->is_debug_types)
6728 {
6729 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6730 section, abbrev_section,
6731 info_ptr, rcuh_kind::TYPE);
6732
6733 /* Since per_cu is the first member of struct signatured_type,
6734 we can go from a pointer to one to a pointer to the other. */
6735 sig_type = (struct signatured_type *) this_cu;
6736 gdb_assert (sig_type->signature == cu->header.signature);
6737 gdb_assert (sig_type->type_offset_in_tu
6738 == cu->header.type_cu_offset_in_tu);
6739 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6740
6741 /* LENGTH has not been set yet for type units if we're
6742 using .gdb_index. */
6743 this_cu->length = cu->header.get_length ();
6744
6745 /* Establish the type offset that can be used to lookup the type. */
6746 sig_type->type_offset_in_section =
6747 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6748
6749 this_cu->dwarf_version = cu->header.version;
6750 }
6751 else
6752 {
6753 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6754 section, abbrev_section,
6755 info_ptr,
6756 rcuh_kind::COMPILE);
6757
6758 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6759 if (this_cu->length == 0)
6760 this_cu->length = cu->header.get_length ();
6761 else
6762 gdb_assert (this_cu->length == cu->header.get_length ());
6763 this_cu->dwarf_version = cu->header.version;
6764 }
6765 }
6766
6767 /* Skip dummy compilation units. */
6768 if (info_ptr >= begin_info_ptr + this_cu->length
6769 || peek_abbrev_code (abfd, info_ptr) == 0)
6770 {
6771 dummy_p = true;
6772 return;
6773 }
6774
6775 /* If we don't have them yet, read the abbrevs for this compilation unit.
6776 And if we need to read them now, make sure they're freed when we're
6777 done. */
6778 if (abbrev_table != NULL)
6779 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6780 else
6781 {
6782 abbrev_section->read (objfile);
6783 m_abbrev_table_holder
6784 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6785 abbrev_table = m_abbrev_table_holder.get ();
6786 }
6787
6788 /* Read the top level CU/TU die. */
6789 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6790 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6791
6792 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6793 {
6794 dummy_p = true;
6795 return;
6796 }
6797
6798 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6799 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6800 table from the DWO file and pass the ownership over to us. It will be
6801 referenced from READER, so we must make sure to free it after we're done
6802 with READER.
6803
6804 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6805 DWO CU, that this test will fail (the attribute will not be present). */
6806 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6807 if (dwo_name != nullptr)
6808 {
6809 struct dwo_unit *dwo_unit;
6810 struct die_info *dwo_comp_unit_die;
6811
6812 if (comp_unit_die->has_children)
6813 {
6814 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6815 " has children (offset %s) [in module %s]"),
6816 sect_offset_str (this_cu->sect_off),
6817 bfd_get_filename (abfd));
6818 }
6819 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6820 if (dwo_unit != NULL)
6821 {
6822 if (read_cutu_die_from_dwo (cu, dwo_unit,
6823 comp_unit_die, NULL,
6824 this, &info_ptr,
6825 &dwo_comp_unit_die,
6826 &m_dwo_abbrev_table) == 0)
6827 {
6828 /* Dummy die. */
6829 dummy_p = true;
6830 return;
6831 }
6832 comp_unit_die = dwo_comp_unit_die;
6833 }
6834 else
6835 {
6836 /* Yikes, we couldn't find the rest of the DIE, we only have
6837 the stub. A complaint has already been logged. There's
6838 not much more we can do except pass on the stub DIE to
6839 die_reader_func. We don't want to throw an error on bad
6840 debug info. */
6841 }
6842 }
6843 }
6844
6845 void
6846 cutu_reader::keep ()
6847 {
6848 /* Done, clean up. */
6849 gdb_assert (!dummy_p);
6850 if (m_new_cu != NULL)
6851 {
6852 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
6853 now. */
6854 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
6855 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
6856 }
6857 }
6858
6859 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6860 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6861 assumed to have already done the lookup to find the DWO file).
6862
6863 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6864 THIS_CU->is_debug_types, but nothing else.
6865
6866 We fill in THIS_CU->length.
6867
6868 THIS_CU->cu is always freed when done.
6869 This is done in order to not leave THIS_CU->cu in a state where we have
6870 to care whether it refers to the "main" CU or the DWO CU.
6871
6872 When parent_cu is passed, it is used to provide a default value for
6873 str_offsets_base and addr_base from the parent. */
6874
6875 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6876 dwarf2_per_objfile *per_objfile,
6877 struct dwarf2_cu *parent_cu,
6878 struct dwo_file *dwo_file)
6879 : die_reader_specs {},
6880 m_this_cu (this_cu)
6881 {
6882 struct objfile *objfile = per_objfile->objfile;
6883 struct dwarf2_section_info *section = this_cu->section;
6884 bfd *abfd = section->get_bfd_owner ();
6885 struct dwarf2_section_info *abbrev_section;
6886 const gdb_byte *begin_info_ptr, *info_ptr;
6887
6888 if (dwarf_die_debug)
6889 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6890 this_cu->is_debug_types ? "type" : "comp",
6891 sect_offset_str (this_cu->sect_off));
6892
6893 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6894
6895 abbrev_section = (dwo_file != NULL
6896 ? &dwo_file->sections.abbrev
6897 : get_abbrev_section_for_cu (this_cu));
6898
6899 /* This is cheap if the section is already read in. */
6900 section->read (objfile);
6901
6902 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6903
6904 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6905 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
6906 section, abbrev_section, info_ptr,
6907 (this_cu->is_debug_types
6908 ? rcuh_kind::TYPE
6909 : rcuh_kind::COMPILE));
6910
6911 if (parent_cu != nullptr)
6912 {
6913 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6914 m_new_cu->addr_base = parent_cu->addr_base;
6915 }
6916 this_cu->length = m_new_cu->header.get_length ();
6917
6918 /* Skip dummy compilation units. */
6919 if (info_ptr >= begin_info_ptr + this_cu->length
6920 || peek_abbrev_code (abfd, info_ptr) == 0)
6921 {
6922 dummy_p = true;
6923 return;
6924 }
6925
6926 abbrev_section->read (objfile);
6927 m_abbrev_table_holder
6928 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
6929
6930 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6931 m_abbrev_table_holder.get ());
6932 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6933 }
6934
6935 \f
6936 /* Type Unit Groups.
6937
6938 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6939 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6940 so that all types coming from the same compilation (.o file) are grouped
6941 together. A future step could be to put the types in the same symtab as
6942 the CU the types ultimately came from. */
6943
6944 static hashval_t
6945 hash_type_unit_group (const void *item)
6946 {
6947 const struct type_unit_group *tu_group
6948 = (const struct type_unit_group *) item;
6949
6950 return hash_stmt_list_entry (&tu_group->hash);
6951 }
6952
6953 static int
6954 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
6955 {
6956 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
6957 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
6958
6959 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
6960 }
6961
6962 /* Allocate a hash table for type unit groups. */
6963
6964 static htab_up
6965 allocate_type_unit_groups_table ()
6966 {
6967 return htab_up (htab_create_alloc (3,
6968 hash_type_unit_group,
6969 eq_type_unit_group,
6970 [] (void *arg)
6971 {
6972 type_unit_group *grp
6973 = (type_unit_group *) arg;
6974 delete grp;
6975 },
6976 xcalloc, xfree));
6977 }
6978
6979 /* Type units that don't have DW_AT_stmt_list are grouped into their own
6980 partial symtabs. We combine several TUs per psymtab to not let the size
6981 of any one psymtab grow too big. */
6982 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
6983 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
6984
6985 /* Helper routine for get_type_unit_group.
6986 Create the type_unit_group object used to hold one or more TUs. */
6987
6988 static std::unique_ptr<type_unit_group>
6989 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
6990 {
6991 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6992 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6993
6994 std::unique_ptr<type_unit_group> tu_group (new type_unit_group);
6995 tu_group->per_bfd = per_bfd;
6996
6997 if (per_bfd->using_index)
6998 {
6999 tu_group->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7000 struct dwarf2_per_cu_quick_data);
7001 }
7002 else
7003 {
7004 unsigned int line_offset = to_underlying (line_offset_struct);
7005 dwarf2_psymtab *pst;
7006 std::string name;
7007
7008 /* Give the symtab a useful name for debug purposes. */
7009 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7010 name = string_printf ("<type_units_%d>",
7011 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7012 else
7013 name = string_printf ("<type_units_at_0x%x>", line_offset);
7014
7015 pst = create_partial_symtab (tu_group.get (), per_objfile,
7016 name.c_str ());
7017 pst->anonymous = true;
7018 }
7019
7020 tu_group->hash.dwo_unit = cu->dwo_unit;
7021 tu_group->hash.line_sect_off = line_offset_struct;
7022
7023 return tu_group;
7024 }
7025
7026 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7027 STMT_LIST is a DW_AT_stmt_list attribute. */
7028
7029 static struct type_unit_group *
7030 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7031 {
7032 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7033 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7034 struct type_unit_group *tu_group;
7035 void **slot;
7036 unsigned int line_offset;
7037 struct type_unit_group type_unit_group_for_lookup;
7038
7039 if (per_objfile->per_bfd->type_unit_groups == NULL)
7040 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7041
7042 /* Do we need to create a new group, or can we use an existing one? */
7043
7044 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7045 {
7046 line_offset = stmt_list->as_unsigned ();
7047 ++tu_stats->nr_symtab_sharers;
7048 }
7049 else
7050 {
7051 /* Ugh, no stmt_list. Rare, but we have to handle it.
7052 We can do various things here like create one group per TU or
7053 spread them over multiple groups to split up the expansion work.
7054 To avoid worst case scenarios (too many groups or too large groups)
7055 we, umm, group them in bunches. */
7056 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7057 | (tu_stats->nr_stmt_less_type_units
7058 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7059 ++tu_stats->nr_stmt_less_type_units;
7060 }
7061
7062 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7063 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7064 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7065 &type_unit_group_for_lookup, INSERT);
7066 if (*slot == nullptr)
7067 {
7068 sect_offset line_offset_struct = (sect_offset) line_offset;
7069 std::unique_ptr<type_unit_group> grp
7070 = create_type_unit_group (cu, line_offset_struct);
7071 *slot = grp.release ();
7072 ++tu_stats->nr_symtabs;
7073 }
7074
7075 tu_group = (struct type_unit_group *) *slot;
7076 gdb_assert (tu_group != nullptr);
7077 return tu_group;
7078 }
7079 \f
7080 /* Partial symbol tables. */
7081
7082 /* Create a psymtab named NAME and assign it to PER_CU.
7083
7084 The caller must fill in the following details:
7085 dirname, textlow, texthigh. */
7086
7087 static dwarf2_psymtab *
7088 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7089 dwarf2_per_objfile *per_objfile,
7090 const char *name)
7091 {
7092 dwarf2_psymtab *pst
7093 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7094 per_objfile->objfile->per_bfd, per_cu);
7095
7096 pst->psymtabs_addrmap_supported = true;
7097
7098 /* This is the glue that links PST into GDB's symbol API. */
7099 per_cu->v.psymtab = pst;
7100
7101 return pst;
7102 }
7103
7104 /* DIE reader function for process_psymtab_comp_unit. */
7105
7106 static void
7107 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7108 const gdb_byte *info_ptr,
7109 struct die_info *comp_unit_die,
7110 enum language pretend_language)
7111 {
7112 struct dwarf2_cu *cu = reader->cu;
7113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7114 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7115 struct objfile *objfile = per_objfile->objfile;
7116 struct gdbarch *gdbarch = objfile->arch ();
7117 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7118 CORE_ADDR baseaddr;
7119 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7120 dwarf2_psymtab *pst;
7121 enum pc_bounds_kind cu_bounds_kind;
7122 const char *filename;
7123
7124 gdb_assert (! per_cu->is_debug_types);
7125
7126 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7127
7128 /* Allocate a new partial symbol table structure. */
7129 gdb::unique_xmalloc_ptr<char> debug_filename;
7130 static const char artificial[] = "<artificial>";
7131 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7132 if (filename == NULL)
7133 filename = "";
7134 else if (strcmp (filename, artificial) == 0)
7135 {
7136 debug_filename.reset (concat (artificial, "@",
7137 sect_offset_str (per_cu->sect_off),
7138 (char *) NULL));
7139 filename = debug_filename.get ();
7140 }
7141
7142 pst = create_partial_symtab (per_cu, per_objfile, filename);
7143
7144 /* This must be done before calling dwarf2_build_include_psymtabs. */
7145 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7146
7147 baseaddr = objfile->text_section_offset ();
7148
7149 dwarf2_find_base_address (comp_unit_die, cu);
7150
7151 /* Possibly set the default values of LOWPC and HIGHPC from
7152 `DW_AT_ranges'. */
7153 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7154 &best_highpc, cu, pst);
7155 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7156 {
7157 CORE_ADDR low
7158 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7159 - baseaddr);
7160 CORE_ADDR high
7161 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7162 - baseaddr - 1);
7163 /* Store the contiguous range if it is not empty; it can be
7164 empty for CUs with no code. */
7165 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7166 low, high, pst);
7167 }
7168
7169 /* Check if comp unit has_children.
7170 If so, read the rest of the partial symbols from this comp unit.
7171 If not, there's no more debug_info for this comp unit. */
7172 if (comp_unit_die->has_children)
7173 {
7174 struct partial_die_info *first_die;
7175 CORE_ADDR lowpc, highpc;
7176
7177 lowpc = ((CORE_ADDR) -1);
7178 highpc = ((CORE_ADDR) 0);
7179
7180 first_die = load_partial_dies (reader, info_ptr, 1);
7181
7182 scan_partial_symbols (first_die, &lowpc, &highpc,
7183 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7184
7185 /* If we didn't find a lowpc, set it to highpc to avoid
7186 complaints from `maint check'. */
7187 if (lowpc == ((CORE_ADDR) -1))
7188 lowpc = highpc;
7189
7190 /* If the compilation unit didn't have an explicit address range,
7191 then use the information extracted from its child dies. */
7192 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7193 {
7194 best_lowpc = lowpc;
7195 best_highpc = highpc;
7196 }
7197 }
7198 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7199 best_lowpc + baseaddr)
7200 - baseaddr);
7201 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7202 best_highpc + baseaddr)
7203 - baseaddr);
7204
7205 pst->end ();
7206
7207 if (!cu->per_cu->imported_symtabs_empty ())
7208 {
7209 int i;
7210 int len = cu->per_cu->imported_symtabs_size ();
7211
7212 /* Fill in 'dependencies' here; we fill in 'users' in a
7213 post-pass. */
7214 pst->number_of_dependencies = len;
7215 pst->dependencies
7216 = per_bfd->partial_symtabs->allocate_dependencies (len);
7217 for (i = 0; i < len; ++i)
7218 {
7219 pst->dependencies[i]
7220 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7221 }
7222
7223 cu->per_cu->imported_symtabs_free ();
7224 }
7225
7226 /* Get the list of files included in the current compilation unit,
7227 and build a psymtab for each of them. */
7228 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7229
7230 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7231 ", %d global, %d static syms",
7232 per_cu->is_debug_types ? "type" : "comp",
7233 sect_offset_str (per_cu->sect_off),
7234 paddress (gdbarch, pst->text_low (objfile)),
7235 paddress (gdbarch, pst->text_high (objfile)),
7236 (int) pst->global_psymbols.size (),
7237 (int) pst->static_psymbols.size ());
7238 }
7239
7240 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7241 Process compilation unit THIS_CU for a psymtab. */
7242
7243 static void
7244 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7245 dwarf2_per_objfile *per_objfile,
7246 bool want_partial_unit,
7247 enum language pretend_language)
7248 {
7249 /* If this compilation unit was already read in, free the
7250 cached copy in order to read it in again. This is
7251 necessary because we skipped some symbols when we first
7252 read in the compilation unit (see load_partial_dies).
7253 This problem could be avoided, but the benefit is unclear. */
7254 per_objfile->remove_cu (this_cu);
7255
7256 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7257
7258 switch (reader.comp_unit_die->tag)
7259 {
7260 case DW_TAG_compile_unit:
7261 this_cu->unit_type = DW_UT_compile;
7262 break;
7263 case DW_TAG_partial_unit:
7264 this_cu->unit_type = DW_UT_partial;
7265 break;
7266 case DW_TAG_type_unit:
7267 this_cu->unit_type = DW_UT_type;
7268 break;
7269 default:
7270 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7271 dwarf_tag_name (reader.comp_unit_die->tag),
7272 sect_offset_str (reader.cu->per_cu->sect_off),
7273 objfile_name (per_objfile->objfile));
7274 }
7275
7276 if (reader.dummy_p)
7277 {
7278 /* Nothing. */
7279 }
7280 else if (this_cu->is_debug_types)
7281 build_type_psymtabs_reader (&reader, reader.info_ptr,
7282 reader.comp_unit_die);
7283 else if (want_partial_unit
7284 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7285 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7286 reader.comp_unit_die,
7287 pretend_language);
7288
7289 this_cu->lang = reader.cu->language;
7290
7291 /* Age out any secondary CUs. */
7292 per_objfile->age_comp_units ();
7293 }
7294
7295 /* Reader function for build_type_psymtabs. */
7296
7297 static void
7298 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7299 const gdb_byte *info_ptr,
7300 struct die_info *type_unit_die)
7301 {
7302 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7303 struct dwarf2_cu *cu = reader->cu;
7304 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7305 struct signatured_type *sig_type;
7306 struct type_unit_group *tu_group;
7307 struct attribute *attr;
7308 struct partial_die_info *first_die;
7309 CORE_ADDR lowpc, highpc;
7310 dwarf2_psymtab *pst;
7311
7312 gdb_assert (per_cu->is_debug_types);
7313 sig_type = (struct signatured_type *) per_cu;
7314
7315 if (! type_unit_die->has_children)
7316 return;
7317
7318 attr = type_unit_die->attr (DW_AT_stmt_list);
7319 tu_group = get_type_unit_group (cu, attr);
7320
7321 if (tu_group->tus == nullptr)
7322 tu_group->tus = new std::vector<signatured_type *>;
7323 tu_group->tus->push_back (sig_type);
7324
7325 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7326 pst = create_partial_symtab (per_cu, per_objfile, "");
7327 pst->anonymous = true;
7328
7329 first_die = load_partial_dies (reader, info_ptr, 1);
7330
7331 lowpc = (CORE_ADDR) -1;
7332 highpc = (CORE_ADDR) 0;
7333 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7334
7335 pst->end ();
7336 }
7337
7338 /* Struct used to sort TUs by their abbreviation table offset. */
7339
7340 struct tu_abbrev_offset
7341 {
7342 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7343 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7344 {}
7345
7346 signatured_type *sig_type;
7347 sect_offset abbrev_offset;
7348 };
7349
7350 /* Helper routine for build_type_psymtabs, passed to std::sort. */
7351
7352 static bool
7353 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7354 const struct tu_abbrev_offset &b)
7355 {
7356 return a.abbrev_offset < b.abbrev_offset;
7357 }
7358
7359 /* Efficiently read all the type units.
7360
7361 The efficiency is because we sort TUs by the abbrev table they use and
7362 only read each abbrev table once. In one program there are 200K TUs
7363 sharing 8K abbrev tables.
7364
7365 The main purpose of this function is to support building the
7366 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7367 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7368 can collapse the search space by grouping them by stmt_list.
7369 The savings can be significant, in the same program from above the 200K TUs
7370 share 8K stmt_list tables.
7371
7372 FUNC is expected to call get_type_unit_group, which will create the
7373 struct type_unit_group if necessary and add it to
7374 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7375
7376 static void
7377 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7378 {
7379 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7380 abbrev_table_up abbrev_table;
7381 sect_offset abbrev_offset;
7382
7383 /* It's up to the caller to not call us multiple times. */
7384 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7385
7386 if (per_objfile->per_bfd->tu_stats.nr_tus == 0)
7387 return;
7388
7389 /* TUs typically share abbrev tables, and there can be way more TUs than
7390 abbrev tables. Sort by abbrev table to reduce the number of times we
7391 read each abbrev table in.
7392 Alternatives are to punt or to maintain a cache of abbrev tables.
7393 This is simpler and efficient enough for now.
7394
7395 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7396 symtab to use). Typically TUs with the same abbrev offset have the same
7397 stmt_list value too so in practice this should work well.
7398
7399 The basic algorithm here is:
7400
7401 sort TUs by abbrev table
7402 for each TU with same abbrev table:
7403 read abbrev table if first user
7404 read TU top level DIE
7405 [IWBN if DWO skeletons had DW_AT_stmt_list]
7406 call FUNC */
7407
7408 dwarf_read_debug_printf ("Building type unit groups ...");
7409
7410 /* Sort in a separate table to maintain the order of all_comp_units
7411 for .gdb_index: TU indices directly index all_type_units. */
7412 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7413 sorted_by_abbrev.reserve (per_objfile->per_bfd->tu_stats.nr_tus);
7414
7415 for (const auto &cu : per_objfile->per_bfd->all_comp_units)
7416 {
7417 if (cu->is_debug_types)
7418 {
7419 auto sig_type = static_cast<signatured_type *> (cu.get ());
7420 sorted_by_abbrev.emplace_back
7421 (sig_type, read_abbrev_offset (per_objfile, sig_type->section,
7422 sig_type->sect_off));
7423 }
7424 }
7425
7426 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7427 sort_tu_by_abbrev_offset);
7428
7429 abbrev_offset = (sect_offset) ~(unsigned) 0;
7430
7431 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7432 {
7433 /* Switch to the next abbrev table if necessary. */
7434 if (abbrev_table == NULL
7435 || tu.abbrev_offset != abbrev_offset)
7436 {
7437 abbrev_offset = tu.abbrev_offset;
7438 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7439 abbrev_table =
7440 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7441 ++tu_stats->nr_uniq_abbrev_tables;
7442 }
7443
7444 cutu_reader reader (tu.sig_type, per_objfile,
7445 abbrev_table.get (), nullptr, false);
7446 if (!reader.dummy_p)
7447 build_type_psymtabs_reader (&reader, reader.info_ptr,
7448 reader.comp_unit_die);
7449 }
7450 }
7451
7452 /* Print collected type unit statistics. */
7453
7454 static void
7455 print_tu_stats (dwarf2_per_objfile *per_objfile)
7456 {
7457 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7458
7459 dwarf_read_debug_printf ("Type unit statistics:");
7460 dwarf_read_debug_printf (" %d TUs", tu_stats->nr_tus);
7461 dwarf_read_debug_printf (" %d uniq abbrev tables",
7462 tu_stats->nr_uniq_abbrev_tables);
7463 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7464 tu_stats->nr_symtabs);
7465 dwarf_read_debug_printf (" %d symtab sharers",
7466 tu_stats->nr_symtab_sharers);
7467 dwarf_read_debug_printf (" %d type units without a stmt_list",
7468 tu_stats->nr_stmt_less_type_units);
7469 dwarf_read_debug_printf (" %d all_type_units reallocs",
7470 tu_stats->nr_all_type_units_reallocs);
7471 }
7472
7473 /* Traversal function for build_type_psymtabs. */
7474
7475 static int
7476 build_type_psymtab_dependencies (void **slot, void *info)
7477 {
7478 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7479 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7480 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7481 dwarf2_psymtab *pst = tu_group->v.psymtab;
7482 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7483 int i;
7484
7485 gdb_assert (len > 0);
7486 gdb_assert (tu_group->type_unit_group_p ());
7487
7488 pst->number_of_dependencies = len;
7489 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7490 for (i = 0; i < len; ++i)
7491 {
7492 struct signatured_type *iter = tu_group->tus->at (i);
7493 gdb_assert (iter->is_debug_types);
7494 pst->dependencies[i] = iter->v.psymtab;
7495 iter->type_unit_group = tu_group;
7496 }
7497
7498 delete tu_group->tus;
7499 tu_group->tus = nullptr;
7500
7501 return 1;
7502 }
7503
7504 /* Traversal function for process_skeletonless_type_unit.
7505 Read a TU in a DWO file and build partial symbols for it. */
7506
7507 static int
7508 process_skeletonless_type_unit (void **slot, void *info)
7509 {
7510 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7511 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7512 struct signatured_type find_entry, *entry;
7513
7514 /* If this TU doesn't exist in the global table, add it and read it in. */
7515
7516 if (per_objfile->per_bfd->signatured_types == NULL)
7517 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7518
7519 find_entry.signature = dwo_unit->signature;
7520 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7521 &find_entry, INSERT);
7522 /* If we've already seen this type there's nothing to do. What's happening
7523 is we're doing our own version of comdat-folding here. */
7524 if (*slot != NULL)
7525 return 1;
7526
7527 /* This does the job that create_all_comp_units would have done for
7528 this TU. */
7529 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7530 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7531 *slot = entry;
7532
7533 /* This does the job that build_type_psymtabs would have done. */
7534 cutu_reader reader (entry, per_objfile, nullptr, nullptr, false);
7535 if (!reader.dummy_p)
7536 build_type_psymtabs_reader (&reader, reader.info_ptr,
7537 reader.comp_unit_die);
7538
7539 return 1;
7540 }
7541
7542 /* Traversal function for process_skeletonless_type_units. */
7543
7544 static int
7545 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7546 {
7547 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7548
7549 if (dwo_file->tus != NULL)
7550 htab_traverse_noresize (dwo_file->tus.get (),
7551 process_skeletonless_type_unit, info);
7552
7553 return 1;
7554 }
7555
7556 /* Scan all TUs of DWO files, verifying we've processed them.
7557 This is needed in case a TU was emitted without its skeleton.
7558 Note: This can't be done until we know what all the DWO files are. */
7559
7560 static void
7561 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7562 {
7563 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7564 if (get_dwp_file (per_objfile) == NULL
7565 && per_objfile->per_bfd->dwo_files != NULL)
7566 {
7567 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7568 process_dwo_file_for_skeletonless_type_units,
7569 per_objfile);
7570 }
7571 }
7572
7573 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7574
7575 static void
7576 set_partial_user (dwarf2_per_objfile *per_objfile)
7577 {
7578 for (const auto &per_cu : per_objfile->per_bfd->all_comp_units)
7579 {
7580 dwarf2_psymtab *pst = per_cu->v.psymtab;
7581
7582 if (pst == NULL)
7583 continue;
7584
7585 for (int j = 0; j < pst->number_of_dependencies; ++j)
7586 {
7587 /* Set the 'user' field only if it is not already set. */
7588 if (pst->dependencies[j]->user == NULL)
7589 pst->dependencies[j]->user = pst;
7590 }
7591 }
7592 }
7593
7594 /* Build the partial symbol table by doing a quick pass through the
7595 .debug_info and .debug_abbrev sections. */
7596
7597 static void
7598 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7599 {
7600 struct objfile *objfile = per_objfile->objfile;
7601 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7602
7603 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7604 objfile_name (objfile));
7605
7606 scoped_restore restore_reading_psyms
7607 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7608
7609 per_bfd->info.read (objfile);
7610
7611 /* Any cached compilation units will be linked by the per-objfile
7612 read_in_chain. Make sure to free them when we're done. */
7613 free_cached_comp_units freer (per_objfile);
7614
7615 create_all_comp_units (per_objfile);
7616 build_type_psymtabs (per_objfile);
7617
7618 /* Create a temporary address map on a temporary obstack. We later
7619 copy this to the final obstack. */
7620 auto_obstack temp_obstack;
7621
7622 scoped_restore save_psymtabs_addrmap
7623 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7624 addrmap_create_mutable (&temp_obstack));
7625
7626 for (const auto &per_cu : per_bfd->all_comp_units)
7627 {
7628 if (per_cu->v.psymtab != NULL)
7629 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7630 continue;
7631 process_psymtab_comp_unit (per_cu.get (), per_objfile, false,
7632 language_minimal);
7633 }
7634
7635 /* This has to wait until we read the CUs, we need the list of DWOs. */
7636 process_skeletonless_type_units (per_objfile);
7637
7638 /* Now that all TUs have been processed we can fill in the dependencies. */
7639 if (per_bfd->type_unit_groups != NULL)
7640 {
7641 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7642 build_type_psymtab_dependencies, per_objfile);
7643 }
7644
7645 if (dwarf_read_debug > 0)
7646 print_tu_stats (per_objfile);
7647
7648 set_partial_user (per_objfile);
7649
7650 per_bfd->partial_symtabs->psymtabs_addrmap
7651 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7652 per_bfd->partial_symtabs->obstack ());
7653 /* At this point we want to keep the address map. */
7654 save_psymtabs_addrmap.release ();
7655
7656 dwarf_read_debug_printf ("Done building psymtabs of %s",
7657 objfile_name (objfile));
7658 }
7659
7660 /* Load the partial DIEs for a secondary CU into memory.
7661 This is also used when rereading a primary CU with load_all_dies. */
7662
7663 static void
7664 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7665 dwarf2_per_objfile *per_objfile,
7666 dwarf2_cu *existing_cu)
7667 {
7668 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7669
7670 if (!reader.dummy_p)
7671 {
7672 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7673 language_minimal);
7674
7675 /* Check if comp unit has_children.
7676 If so, read the rest of the partial symbols from this comp unit.
7677 If not, there's no more debug_info for this comp unit. */
7678 if (reader.comp_unit_die->has_children)
7679 load_partial_dies (&reader, reader.info_ptr, 0);
7680
7681 reader.keep ();
7682 }
7683 }
7684
7685 static void
7686 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7687 struct dwarf2_section_info *section,
7688 struct dwarf2_section_info *abbrev_section,
7689 unsigned int is_dwz,
7690 htab_up &types_htab,
7691 rcuh_kind section_kind)
7692 {
7693 const gdb_byte *info_ptr;
7694 struct objfile *objfile = per_objfile->objfile;
7695
7696 dwarf_read_debug_printf ("Reading %s for %s",
7697 section->get_name (),
7698 section->get_file_name ());
7699
7700 section->read (objfile);
7701
7702 info_ptr = section->buffer;
7703
7704 while (info_ptr < section->buffer + section->size)
7705 {
7706 std::unique_ptr<dwarf2_per_cu_data> this_cu;
7707
7708 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7709
7710 comp_unit_head cu_header;
7711 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7712 abbrev_section, info_ptr,
7713 section_kind);
7714
7715 /* Save the compilation unit for later lookup. */
7716 if (cu_header.unit_type != DW_UT_type)
7717 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7718 else
7719 {
7720 if (types_htab == nullptr)
7721 types_htab = allocate_signatured_type_table ();
7722
7723 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7724 signatured_type *sig_ptr = sig_type.get ();
7725 sig_type->signature = cu_header.signature;
7726 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7727 this_cu = std::move (sig_type);
7728
7729 void **slot = htab_find_slot (types_htab.get (), sig_ptr, INSERT);
7730 gdb_assert (slot != nullptr);
7731 if (*slot != nullptr)
7732 complaint (_("debug type entry at offset %s is duplicate to"
7733 " the entry at offset %s, signature %s"),
7734 sect_offset_str (sect_off),
7735 sect_offset_str (sig_ptr->sect_off),
7736 hex_string (sig_ptr->signature));
7737 *slot = sig_ptr;
7738 }
7739 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7740 this_cu->sect_off = sect_off;
7741 this_cu->length = cu_header.length + cu_header.initial_length_size;
7742 this_cu->is_dwz = is_dwz;
7743 this_cu->section = section;
7744
7745 info_ptr = info_ptr + this_cu->length;
7746 per_objfile->per_bfd->all_comp_units.push_back (std::move (this_cu));
7747 }
7748 }
7749
7750 /* Create a list of all compilation units in OBJFILE.
7751 This is only done for -readnow and building partial symtabs. */
7752
7753 static void
7754 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7755 {
7756 htab_up types_htab;
7757
7758 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7759 &per_objfile->per_bfd->abbrev, 0,
7760 types_htab, rcuh_kind::COMPILE);
7761 for (dwarf2_section_info &section : per_objfile->per_bfd->types)
7762 read_comp_units_from_section (per_objfile, &section,
7763 &per_objfile->per_bfd->abbrev, 0,
7764 types_htab, rcuh_kind::TYPE);
7765
7766 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7767 if (dwz != NULL)
7768 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1,
7769 types_htab, rcuh_kind::COMPILE);
7770
7771 per_objfile->per_bfd->signatured_types = std::move (types_htab);
7772 }
7773
7774 /* Process all loaded DIEs for compilation unit CU, starting at
7775 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7776 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7777 DW_AT_ranges). See the comments of add_partial_subprogram on how
7778 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7779
7780 static void
7781 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7782 CORE_ADDR *highpc, int set_addrmap,
7783 struct dwarf2_cu *cu)
7784 {
7785 struct partial_die_info *pdi;
7786
7787 /* Now, march along the PDI's, descending into ones which have
7788 interesting children but skipping the children of the other ones,
7789 until we reach the end of the compilation unit. */
7790
7791 pdi = first_die;
7792
7793 while (pdi != NULL)
7794 {
7795 pdi->fixup (cu);
7796
7797 /* Anonymous namespaces or modules have no name but have interesting
7798 children, so we need to look at them. Ditto for anonymous
7799 enums. */
7800
7801 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7802 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7803 || pdi->tag == DW_TAG_imported_unit
7804 || pdi->tag == DW_TAG_inlined_subroutine)
7805 {
7806 switch (pdi->tag)
7807 {
7808 case DW_TAG_subprogram:
7809 case DW_TAG_inlined_subroutine:
7810 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7811 if (cu->language == language_cplus)
7812 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7813 set_addrmap, cu);
7814 break;
7815 case DW_TAG_constant:
7816 case DW_TAG_variable:
7817 case DW_TAG_typedef:
7818 case DW_TAG_union_type:
7819 if (!pdi->is_declaration
7820 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7821 {
7822 add_partial_symbol (pdi, cu);
7823 }
7824 break;
7825 case DW_TAG_class_type:
7826 case DW_TAG_interface_type:
7827 case DW_TAG_structure_type:
7828 if (!pdi->is_declaration)
7829 {
7830 add_partial_symbol (pdi, cu);
7831 }
7832 if ((cu->language == language_rust
7833 || cu->language == language_cplus) && pdi->has_children)
7834 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7835 set_addrmap, cu);
7836 break;
7837 case DW_TAG_enumeration_type:
7838 if (!pdi->is_declaration)
7839 add_partial_enumeration (pdi, cu);
7840 break;
7841 case DW_TAG_base_type:
7842 case DW_TAG_subrange_type:
7843 /* File scope base type definitions are added to the partial
7844 symbol table. */
7845 add_partial_symbol (pdi, cu);
7846 break;
7847 case DW_TAG_namespace:
7848 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7849 break;
7850 case DW_TAG_module:
7851 if (!pdi->is_declaration)
7852 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7853 break;
7854 case DW_TAG_imported_unit:
7855 {
7856 struct dwarf2_per_cu_data *per_cu;
7857
7858 /* For now we don't handle imported units in type units. */
7859 if (cu->per_cu->is_debug_types)
7860 {
7861 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7862 " supported in type units [in module %s]"),
7863 objfile_name (cu->per_objfile->objfile));
7864 }
7865
7866 per_cu = dwarf2_find_containing_comp_unit
7867 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
7868
7869 /* Go read the partial unit, if needed. */
7870 if (per_cu->v.psymtab == NULL)
7871 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
7872 cu->language);
7873
7874 cu->per_cu->imported_symtabs_push (per_cu);
7875 }
7876 break;
7877 case DW_TAG_imported_declaration:
7878 add_partial_symbol (pdi, cu);
7879 break;
7880 default:
7881 break;
7882 }
7883 }
7884
7885 /* If the die has a sibling, skip to the sibling. */
7886
7887 pdi = pdi->die_sibling;
7888 }
7889 }
7890
7891 /* Functions used to compute the fully scoped name of a partial DIE.
7892
7893 Normally, this is simple. For C++, the parent DIE's fully scoped
7894 name is concatenated with "::" and the partial DIE's name.
7895 Enumerators are an exception; they use the scope of their parent
7896 enumeration type, i.e. the name of the enumeration type is not
7897 prepended to the enumerator.
7898
7899 There are two complexities. One is DW_AT_specification; in this
7900 case "parent" means the parent of the target of the specification,
7901 instead of the direct parent of the DIE. The other is compilers
7902 which do not emit DW_TAG_namespace; in this case we try to guess
7903 the fully qualified name of structure types from their members'
7904 linkage names. This must be done using the DIE's children rather
7905 than the children of any DW_AT_specification target. We only need
7906 to do this for structures at the top level, i.e. if the target of
7907 any DW_AT_specification (if any; otherwise the DIE itself) does not
7908 have a parent. */
7909
7910 /* Compute the scope prefix associated with PDI's parent, in
7911 compilation unit CU. The result will be allocated on CU's
7912 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7913 field. NULL is returned if no prefix is necessary. */
7914 static const char *
7915 partial_die_parent_scope (struct partial_die_info *pdi,
7916 struct dwarf2_cu *cu)
7917 {
7918 const char *grandparent_scope;
7919 struct partial_die_info *parent, *real_pdi;
7920
7921 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7922 then this means the parent of the specification DIE. */
7923
7924 real_pdi = pdi;
7925 while (real_pdi->has_specification)
7926 {
7927 auto res = find_partial_die (real_pdi->spec_offset,
7928 real_pdi->spec_is_dwz, cu);
7929 real_pdi = res.pdi;
7930 cu = res.cu;
7931 }
7932
7933 parent = real_pdi->die_parent;
7934 if (parent == NULL)
7935 return NULL;
7936
7937 if (parent->scope_set)
7938 return parent->scope;
7939
7940 parent->fixup (cu);
7941
7942 grandparent_scope = partial_die_parent_scope (parent, cu);
7943
7944 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7945 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7946 Work around this problem here. */
7947 if (cu->language == language_cplus
7948 && parent->tag == DW_TAG_namespace
7949 && strcmp (parent->name (cu), "::") == 0
7950 && grandparent_scope == NULL)
7951 {
7952 parent->scope = NULL;
7953 parent->scope_set = 1;
7954 return NULL;
7955 }
7956
7957 /* Nested subroutines in Fortran get a prefix. */
7958 if (pdi->tag == DW_TAG_enumerator)
7959 /* Enumerators should not get the name of the enumeration as a prefix. */
7960 parent->scope = grandparent_scope;
7961 else if (parent->tag == DW_TAG_namespace
7962 || parent->tag == DW_TAG_module
7963 || parent->tag == DW_TAG_structure_type
7964 || parent->tag == DW_TAG_class_type
7965 || parent->tag == DW_TAG_interface_type
7966 || parent->tag == DW_TAG_union_type
7967 || parent->tag == DW_TAG_enumeration_type
7968 || (cu->language == language_fortran
7969 && parent->tag == DW_TAG_subprogram
7970 && pdi->tag == DW_TAG_subprogram))
7971 {
7972 if (grandparent_scope == NULL)
7973 parent->scope = parent->name (cu);
7974 else
7975 parent->scope = typename_concat (&cu->comp_unit_obstack,
7976 grandparent_scope,
7977 parent->name (cu), 0, cu);
7978 }
7979 else
7980 {
7981 /* FIXME drow/2004-04-01: What should we be doing with
7982 function-local names? For partial symbols, we should probably be
7983 ignoring them. */
7984 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
7985 dwarf_tag_name (parent->tag),
7986 sect_offset_str (pdi->sect_off));
7987 parent->scope = grandparent_scope;
7988 }
7989
7990 parent->scope_set = 1;
7991 return parent->scope;
7992 }
7993
7994 /* Return the fully scoped name associated with PDI, from compilation unit
7995 CU. The result will be allocated with malloc. */
7996
7997 static gdb::unique_xmalloc_ptr<char>
7998 partial_die_full_name (struct partial_die_info *pdi,
7999 struct dwarf2_cu *cu)
8000 {
8001 const char *parent_scope;
8002
8003 /* If this is a template instantiation, we can not work out the
8004 template arguments from partial DIEs. So, unfortunately, we have
8005 to go through the full DIEs. At least any work we do building
8006 types here will be reused if full symbols are loaded later. */
8007 if (pdi->has_template_arguments)
8008 {
8009 pdi->fixup (cu);
8010
8011 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8012 {
8013 struct die_info *die;
8014 struct attribute attr;
8015 struct dwarf2_cu *ref_cu = cu;
8016
8017 /* DW_FORM_ref_addr is using section offset. */
8018 attr.name = (enum dwarf_attribute) 0;
8019 attr.form = DW_FORM_ref_addr;
8020 attr.u.unsnd = to_underlying (pdi->sect_off);
8021 die = follow_die_ref (NULL, &attr, &ref_cu);
8022
8023 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8024 }
8025 }
8026
8027 parent_scope = partial_die_parent_scope (pdi, cu);
8028 if (parent_scope == NULL)
8029 return NULL;
8030 else
8031 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8032 pdi->name (cu),
8033 0, cu));
8034 }
8035
8036 static void
8037 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8038 {
8039 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8040 struct objfile *objfile = per_objfile->objfile;
8041 struct gdbarch *gdbarch = objfile->arch ();
8042 CORE_ADDR addr = 0;
8043 const char *actual_name = NULL;
8044 CORE_ADDR baseaddr;
8045
8046 baseaddr = objfile->text_section_offset ();
8047
8048 gdb::unique_xmalloc_ptr<char> built_actual_name
8049 = partial_die_full_name (pdi, cu);
8050 if (built_actual_name != NULL)
8051 actual_name = built_actual_name.get ();
8052
8053 if (actual_name == NULL)
8054 actual_name = pdi->name (cu);
8055
8056 partial_symbol psymbol;
8057 memset (&psymbol, 0, sizeof (psymbol));
8058 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8059 psymbol.ginfo.set_section_index (-1);
8060
8061 /* The code below indicates that the psymbol should be installed by
8062 setting this. */
8063 gdb::optional<psymbol_placement> where;
8064
8065 switch (pdi->tag)
8066 {
8067 case DW_TAG_inlined_subroutine:
8068 case DW_TAG_subprogram:
8069 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8070 - baseaddr);
8071 if (pdi->is_external
8072 || cu->language == language_ada
8073 || (cu->language == language_fortran
8074 && pdi->die_parent != NULL
8075 && pdi->die_parent->tag == DW_TAG_subprogram))
8076 {
8077 /* Normally, only "external" DIEs are part of the global scope.
8078 But in Ada and Fortran, we want to be able to access nested
8079 procedures globally. So all Ada and Fortran subprograms are
8080 stored in the global scope. */
8081 where = psymbol_placement::GLOBAL;
8082 }
8083 else
8084 where = psymbol_placement::STATIC;
8085
8086 psymbol.domain = VAR_DOMAIN;
8087 psymbol.aclass = LOC_BLOCK;
8088 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8089 psymbol.ginfo.value.address = addr;
8090
8091 if (pdi->main_subprogram && actual_name != NULL)
8092 set_objfile_main_name (objfile, actual_name, cu->language);
8093 break;
8094 case DW_TAG_constant:
8095 psymbol.domain = VAR_DOMAIN;
8096 psymbol.aclass = LOC_STATIC;
8097 where = (pdi->is_external
8098 ? psymbol_placement::GLOBAL
8099 : psymbol_placement::STATIC);
8100 break;
8101 case DW_TAG_variable:
8102 if (pdi->d.locdesc)
8103 addr = decode_locdesc (pdi->d.locdesc, cu);
8104
8105 if (pdi->d.locdesc
8106 && addr == 0
8107 && !per_objfile->per_bfd->has_section_at_zero)
8108 {
8109 /* A global or static variable may also have been stripped
8110 out by the linker if unused, in which case its address
8111 will be nullified; do not add such variables into partial
8112 symbol table then. */
8113 }
8114 else if (pdi->is_external)
8115 {
8116 /* Global Variable.
8117 Don't enter into the minimal symbol tables as there is
8118 a minimal symbol table entry from the ELF symbols already.
8119 Enter into partial symbol table if it has a location
8120 descriptor or a type.
8121 If the location descriptor is missing, new_symbol will create
8122 a LOC_UNRESOLVED symbol, the address of the variable will then
8123 be determined from the minimal symbol table whenever the variable
8124 is referenced.
8125 The address for the partial symbol table entry is not
8126 used by GDB, but it comes in handy for debugging partial symbol
8127 table building. */
8128
8129 if (pdi->d.locdesc || pdi->has_type)
8130 {
8131 psymbol.domain = VAR_DOMAIN;
8132 psymbol.aclass = LOC_STATIC;
8133 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8134 psymbol.ginfo.value.address = addr;
8135 where = psymbol_placement::GLOBAL;
8136 }
8137 }
8138 else
8139 {
8140 int has_loc = pdi->d.locdesc != NULL;
8141
8142 /* Static Variable. Skip symbols whose value we cannot know (those
8143 without location descriptors or constant values). */
8144 if (!has_loc && !pdi->has_const_value)
8145 return;
8146
8147 psymbol.domain = VAR_DOMAIN;
8148 psymbol.aclass = LOC_STATIC;
8149 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8150 if (has_loc)
8151 psymbol.ginfo.value.address = addr;
8152 where = psymbol_placement::STATIC;
8153 }
8154 break;
8155 case DW_TAG_array_type:
8156 case DW_TAG_typedef:
8157 case DW_TAG_base_type:
8158 case DW_TAG_subrange_type:
8159 psymbol.domain = VAR_DOMAIN;
8160 psymbol.aclass = LOC_TYPEDEF;
8161 where = psymbol_placement::STATIC;
8162 break;
8163 case DW_TAG_imported_declaration:
8164 case DW_TAG_namespace:
8165 psymbol.domain = VAR_DOMAIN;
8166 psymbol.aclass = LOC_TYPEDEF;
8167 where = psymbol_placement::GLOBAL;
8168 break;
8169 case DW_TAG_module:
8170 /* With Fortran 77 there might be a "BLOCK DATA" module
8171 available without any name. If so, we skip the module as it
8172 doesn't bring any value. */
8173 if (actual_name != nullptr)
8174 {
8175 psymbol.domain = MODULE_DOMAIN;
8176 psymbol.aclass = LOC_TYPEDEF;
8177 where = psymbol_placement::GLOBAL;
8178 }
8179 break;
8180 case DW_TAG_class_type:
8181 case DW_TAG_interface_type:
8182 case DW_TAG_structure_type:
8183 case DW_TAG_union_type:
8184 case DW_TAG_enumeration_type:
8185 /* Skip external references. The DWARF standard says in the section
8186 about "Structure, Union, and Class Type Entries": "An incomplete
8187 structure, union or class type is represented by a structure,
8188 union or class entry that does not have a byte size attribute
8189 and that has a DW_AT_declaration attribute." */
8190 if (!pdi->has_byte_size && pdi->is_declaration)
8191 return;
8192
8193 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8194 static vs. global. */
8195 psymbol.domain = STRUCT_DOMAIN;
8196 psymbol.aclass = LOC_TYPEDEF;
8197 where = (cu->language == language_cplus
8198 ? psymbol_placement::GLOBAL
8199 : psymbol_placement::STATIC);
8200 break;
8201 case DW_TAG_enumerator:
8202 psymbol.domain = VAR_DOMAIN;
8203 psymbol.aclass = LOC_CONST;
8204 where = (cu->language == language_cplus
8205 ? psymbol_placement::GLOBAL
8206 : psymbol_placement::STATIC);
8207 break;
8208 default:
8209 break;
8210 }
8211
8212 if (where.has_value ())
8213 {
8214 if (built_actual_name != nullptr)
8215 actual_name = objfile->intern (actual_name);
8216 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8217 psymbol.ginfo.set_linkage_name (actual_name);
8218 else
8219 {
8220 psymbol.ginfo.set_demangled_name (actual_name,
8221 &objfile->objfile_obstack);
8222 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8223 }
8224 cu->per_cu->v.psymtab->add_psymbol
8225 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8226 objfile);
8227 }
8228 }
8229
8230 /* Read a partial die corresponding to a namespace; also, add a symbol
8231 corresponding to that namespace to the symbol table. NAMESPACE is
8232 the name of the enclosing namespace. */
8233
8234 static void
8235 add_partial_namespace (struct partial_die_info *pdi,
8236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8237 int set_addrmap, struct dwarf2_cu *cu)
8238 {
8239 /* Add a symbol for the namespace. */
8240
8241 add_partial_symbol (pdi, cu);
8242
8243 /* Now scan partial symbols in that namespace. */
8244
8245 if (pdi->has_children)
8246 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8247 }
8248
8249 /* Read a partial die corresponding to a Fortran module. */
8250
8251 static void
8252 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8253 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8254 {
8255 /* Add a symbol for the namespace. */
8256
8257 add_partial_symbol (pdi, cu);
8258
8259 /* Now scan partial symbols in that module. */
8260
8261 if (pdi->has_children)
8262 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8263 }
8264
8265 /* Read a partial die corresponding to a subprogram or an inlined
8266 subprogram and create a partial symbol for that subprogram.
8267 When the CU language allows it, this routine also defines a partial
8268 symbol for each nested subprogram that this subprogram contains.
8269 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8270 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8271
8272 PDI may also be a lexical block, in which case we simply search
8273 recursively for subprograms defined inside that lexical block.
8274 Again, this is only performed when the CU language allows this
8275 type of definitions. */
8276
8277 static void
8278 add_partial_subprogram (struct partial_die_info *pdi,
8279 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8280 int set_addrmap, struct dwarf2_cu *cu)
8281 {
8282 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8283 {
8284 if (pdi->has_pc_info)
8285 {
8286 if (pdi->lowpc < *lowpc)
8287 *lowpc = pdi->lowpc;
8288 if (pdi->highpc > *highpc)
8289 *highpc = pdi->highpc;
8290 if (set_addrmap)
8291 {
8292 struct objfile *objfile = cu->per_objfile->objfile;
8293 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8294 struct gdbarch *gdbarch = objfile->arch ();
8295 CORE_ADDR baseaddr;
8296 CORE_ADDR this_highpc;
8297 CORE_ADDR this_lowpc;
8298
8299 baseaddr = objfile->text_section_offset ();
8300 this_lowpc
8301 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8302 pdi->lowpc + baseaddr)
8303 - baseaddr);
8304 this_highpc
8305 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8306 pdi->highpc + baseaddr)
8307 - baseaddr);
8308 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8309 this_lowpc, this_highpc - 1,
8310 cu->per_cu->v.psymtab);
8311 }
8312 }
8313
8314 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8315 {
8316 if (!pdi->is_declaration)
8317 /* Ignore subprogram DIEs that do not have a name, they are
8318 illegal. Do not emit a complaint at this point, we will
8319 do so when we convert this psymtab into a symtab. */
8320 if (pdi->name (cu))
8321 add_partial_symbol (pdi, cu);
8322 }
8323 }
8324
8325 if (! pdi->has_children)
8326 return;
8327
8328 if (cu->language == language_ada || cu->language == language_fortran)
8329 {
8330 pdi = pdi->die_child;
8331 while (pdi != NULL)
8332 {
8333 pdi->fixup (cu);
8334 if (pdi->tag == DW_TAG_subprogram
8335 || pdi->tag == DW_TAG_inlined_subroutine
8336 || pdi->tag == DW_TAG_lexical_block)
8337 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8338 pdi = pdi->die_sibling;
8339 }
8340 }
8341 }
8342
8343 /* Read a partial die corresponding to an enumeration type. */
8344
8345 static void
8346 add_partial_enumeration (struct partial_die_info *enum_pdi,
8347 struct dwarf2_cu *cu)
8348 {
8349 struct partial_die_info *pdi;
8350
8351 if (enum_pdi->name (cu) != NULL)
8352 add_partial_symbol (enum_pdi, cu);
8353
8354 pdi = enum_pdi->die_child;
8355 while (pdi)
8356 {
8357 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8358 complaint (_("malformed enumerator DIE ignored"));
8359 else
8360 add_partial_symbol (pdi, cu);
8361 pdi = pdi->die_sibling;
8362 }
8363 }
8364
8365 /* Return the initial uleb128 in the die at INFO_PTR. */
8366
8367 static unsigned int
8368 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8369 {
8370 unsigned int bytes_read;
8371
8372 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8373 }
8374
8375 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8376 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8377
8378 Return the corresponding abbrev, or NULL if the number is zero (indicating
8379 an empty DIE). In either case *BYTES_READ will be set to the length of
8380 the initial number. */
8381
8382 static const struct abbrev_info *
8383 peek_die_abbrev (const die_reader_specs &reader,
8384 const gdb_byte *info_ptr, unsigned int *bytes_read)
8385 {
8386 dwarf2_cu *cu = reader.cu;
8387 bfd *abfd = reader.abfd;
8388 unsigned int abbrev_number
8389 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8390
8391 if (abbrev_number == 0)
8392 return NULL;
8393
8394 const abbrev_info *abbrev
8395 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8396 if (!abbrev)
8397 {
8398 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8399 " at offset %s [in module %s]"),
8400 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8401 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8402 }
8403
8404 return abbrev;
8405 }
8406
8407 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8408 Returns a pointer to the end of a series of DIEs, terminated by an empty
8409 DIE. Any children of the skipped DIEs will also be skipped. */
8410
8411 static const gdb_byte *
8412 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8413 {
8414 while (1)
8415 {
8416 unsigned int bytes_read;
8417 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8418 &bytes_read);
8419
8420 if (abbrev == NULL)
8421 return info_ptr + bytes_read;
8422 else
8423 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8424 }
8425 }
8426
8427 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8428 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8429 abbrev corresponding to that skipped uleb128 should be passed in
8430 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8431 children. */
8432
8433 static const gdb_byte *
8434 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8435 const struct abbrev_info *abbrev)
8436 {
8437 unsigned int bytes_read;
8438 struct attribute attr;
8439 bfd *abfd = reader->abfd;
8440 struct dwarf2_cu *cu = reader->cu;
8441 const gdb_byte *buffer = reader->buffer;
8442 const gdb_byte *buffer_end = reader->buffer_end;
8443 unsigned int form, i;
8444
8445 for (i = 0; i < abbrev->num_attrs; i++)
8446 {
8447 /* The only abbrev we care about is DW_AT_sibling. */
8448 if (abbrev->attrs[i].name == DW_AT_sibling)
8449 {
8450 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8451 if (attr.form == DW_FORM_ref_addr)
8452 complaint (_("ignoring absolute DW_AT_sibling"));
8453 else
8454 {
8455 sect_offset off = attr.get_ref_die_offset ();
8456 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8457
8458 if (sibling_ptr < info_ptr)
8459 complaint (_("DW_AT_sibling points backwards"));
8460 else if (sibling_ptr > reader->buffer_end)
8461 reader->die_section->overflow_complaint ();
8462 else
8463 return sibling_ptr;
8464 }
8465 }
8466
8467 /* If it isn't DW_AT_sibling, skip this attribute. */
8468 form = abbrev->attrs[i].form;
8469 skip_attribute:
8470 switch (form)
8471 {
8472 case DW_FORM_ref_addr:
8473 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8474 and later it is offset sized. */
8475 if (cu->header.version == 2)
8476 info_ptr += cu->header.addr_size;
8477 else
8478 info_ptr += cu->header.offset_size;
8479 break;
8480 case DW_FORM_GNU_ref_alt:
8481 info_ptr += cu->header.offset_size;
8482 break;
8483 case DW_FORM_addr:
8484 info_ptr += cu->header.addr_size;
8485 break;
8486 case DW_FORM_data1:
8487 case DW_FORM_ref1:
8488 case DW_FORM_flag:
8489 case DW_FORM_strx1:
8490 info_ptr += 1;
8491 break;
8492 case DW_FORM_flag_present:
8493 case DW_FORM_implicit_const:
8494 break;
8495 case DW_FORM_data2:
8496 case DW_FORM_ref2:
8497 case DW_FORM_strx2:
8498 info_ptr += 2;
8499 break;
8500 case DW_FORM_strx3:
8501 info_ptr += 3;
8502 break;
8503 case DW_FORM_data4:
8504 case DW_FORM_ref4:
8505 case DW_FORM_strx4:
8506 info_ptr += 4;
8507 break;
8508 case DW_FORM_data8:
8509 case DW_FORM_ref8:
8510 case DW_FORM_ref_sig8:
8511 info_ptr += 8;
8512 break;
8513 case DW_FORM_data16:
8514 info_ptr += 16;
8515 break;
8516 case DW_FORM_string:
8517 read_direct_string (abfd, info_ptr, &bytes_read);
8518 info_ptr += bytes_read;
8519 break;
8520 case DW_FORM_sec_offset:
8521 case DW_FORM_strp:
8522 case DW_FORM_GNU_strp_alt:
8523 info_ptr += cu->header.offset_size;
8524 break;
8525 case DW_FORM_exprloc:
8526 case DW_FORM_block:
8527 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8528 info_ptr += bytes_read;
8529 break;
8530 case DW_FORM_block1:
8531 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8532 break;
8533 case DW_FORM_block2:
8534 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8535 break;
8536 case DW_FORM_block4:
8537 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8538 break;
8539 case DW_FORM_addrx:
8540 case DW_FORM_strx:
8541 case DW_FORM_sdata:
8542 case DW_FORM_udata:
8543 case DW_FORM_ref_udata:
8544 case DW_FORM_GNU_addr_index:
8545 case DW_FORM_GNU_str_index:
8546 case DW_FORM_rnglistx:
8547 case DW_FORM_loclistx:
8548 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8549 break;
8550 case DW_FORM_indirect:
8551 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8552 info_ptr += bytes_read;
8553 /* We need to continue parsing from here, so just go back to
8554 the top. */
8555 goto skip_attribute;
8556
8557 default:
8558 error (_("Dwarf Error: Cannot handle %s "
8559 "in DWARF reader [in module %s]"),
8560 dwarf_form_name (form),
8561 bfd_get_filename (abfd));
8562 }
8563 }
8564
8565 if (abbrev->has_children)
8566 return skip_children (reader, info_ptr);
8567 else
8568 return info_ptr;
8569 }
8570
8571 /* Locate ORIG_PDI's sibling.
8572 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8573
8574 static const gdb_byte *
8575 locate_pdi_sibling (const struct die_reader_specs *reader,
8576 struct partial_die_info *orig_pdi,
8577 const gdb_byte *info_ptr)
8578 {
8579 /* Do we know the sibling already? */
8580
8581 if (orig_pdi->sibling)
8582 return orig_pdi->sibling;
8583
8584 /* Are there any children to deal with? */
8585
8586 if (!orig_pdi->has_children)
8587 return info_ptr;
8588
8589 /* Skip the children the long way. */
8590
8591 return skip_children (reader, info_ptr);
8592 }
8593
8594 /* Expand this partial symbol table into a full symbol table. SELF is
8595 not NULL. */
8596
8597 void
8598 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8599 {
8600 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8601
8602 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8603
8604 /* If this psymtab is constructed from a debug-only objfile, the
8605 has_section_at_zero flag will not necessarily be correct. We
8606 can get the correct value for this flag by looking at the data
8607 associated with the (presumably stripped) associated objfile. */
8608 if (objfile->separate_debug_objfile_backlink)
8609 {
8610 dwarf2_per_objfile *per_objfile_backlink
8611 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8612
8613 per_objfile->per_bfd->has_section_at_zero
8614 = per_objfile_backlink->per_bfd->has_section_at_zero;
8615 }
8616
8617 expand_psymtab (objfile);
8618
8619 process_cu_includes (per_objfile);
8620 }
8621 \f
8622 /* Reading in full CUs. */
8623
8624 /* Add PER_CU to the queue. */
8625
8626 static void
8627 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8628 dwarf2_per_objfile *per_objfile,
8629 enum language pretend_language)
8630 {
8631 per_cu->queued = 1;
8632
8633 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8634 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8635 }
8636
8637 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8638
8639 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8640 dependency.
8641
8642 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8643 DIEs, false otherwise.
8644
8645 Explanation: there is an invariant that if a CU is queued for expansion
8646 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8647 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8648 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8649 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8650 invariant is respected.
8651
8652 The caller is therefore not required to load the CU's DIEs (we return false)
8653 if:
8654
8655 - the CU is already expanded, and therefore does not get enqueued
8656 - the CU gets enqueued for expansion, but its DIEs are already loaded
8657
8658 Note that the caller should not use this function's return value as an
8659 indicator of whether the CU's DIEs are loaded right now, it should check
8660 that by calling `dwarf2_per_objfile::get_cu` instead. */
8661
8662 static int
8663 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8664 dwarf2_per_cu_data *per_cu,
8665 dwarf2_per_objfile *per_objfile,
8666 enum language pretend_language)
8667 {
8668 /* We may arrive here during partial symbol reading, if we need full
8669 DIEs to process an unusual case (e.g. template arguments). Do
8670 not queue PER_CU, just tell our caller to load its DIEs. */
8671 if (per_cu->per_bfd->reading_partial_symbols)
8672 {
8673 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8674
8675 if (cu == NULL || cu->dies == NULL)
8676 return 1;
8677 return 0;
8678 }
8679
8680 /* Mark the dependence relation so that we don't flush PER_CU
8681 too early. */
8682 if (dependent_cu != NULL)
8683 dwarf2_add_dependence (dependent_cu, per_cu);
8684
8685 /* If it's already on the queue, we have nothing to do. */
8686 if (per_cu->queued)
8687 {
8688 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8689 loaded. */
8690 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8691
8692 /* If the CU is queued for expansion, it should not already be
8693 expanded. */
8694 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8695
8696 /* The DIEs are already loaded, the caller doesn't need to do it. */
8697 return 0;
8698 }
8699
8700 bool queued = false;
8701 if (!per_objfile->symtab_set_p (per_cu))
8702 {
8703 /* Add it to the queue. */
8704 queue_comp_unit (per_cu, per_objfile, pretend_language);
8705 queued = true;
8706 }
8707
8708 /* If the compilation unit is already loaded, just mark it as
8709 used. */
8710 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8711 if (cu != nullptr)
8712 cu->last_used = 0;
8713
8714 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8715 and the DIEs are not already loaded. */
8716 return queued && cu == nullptr;
8717 }
8718
8719 /* Process the queue. */
8720
8721 static void
8722 process_queue (dwarf2_per_objfile *per_objfile)
8723 {
8724 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8725 objfile_name (per_objfile->objfile));
8726
8727 /* The queue starts out with one item, but following a DIE reference
8728 may load a new CU, adding it to the end of the queue. */
8729 while (!per_objfile->per_bfd->queue->empty ())
8730 {
8731 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8732 dwarf2_per_cu_data *per_cu = item.per_cu;
8733
8734 if (!per_objfile->symtab_set_p (per_cu))
8735 {
8736 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8737
8738 /* Skip dummy CUs. */
8739 if (cu != nullptr)
8740 {
8741 unsigned int debug_print_threshold;
8742 char buf[100];
8743
8744 if (per_cu->is_debug_types)
8745 {
8746 struct signatured_type *sig_type =
8747 (struct signatured_type *) per_cu;
8748
8749 sprintf (buf, "TU %s at offset %s",
8750 hex_string (sig_type->signature),
8751 sect_offset_str (per_cu->sect_off));
8752 /* There can be 100s of TUs.
8753 Only print them in verbose mode. */
8754 debug_print_threshold = 2;
8755 }
8756 else
8757 {
8758 sprintf (buf, "CU at offset %s",
8759 sect_offset_str (per_cu->sect_off));
8760 debug_print_threshold = 1;
8761 }
8762
8763 if (dwarf_read_debug >= debug_print_threshold)
8764 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8765
8766 if (per_cu->is_debug_types)
8767 process_full_type_unit (cu, item.pretend_language);
8768 else
8769 process_full_comp_unit (cu, item.pretend_language);
8770
8771 if (dwarf_read_debug >= debug_print_threshold)
8772 dwarf_read_debug_printf ("Done expanding %s", buf);
8773 }
8774 }
8775
8776 per_cu->queued = 0;
8777 per_objfile->per_bfd->queue->pop ();
8778 }
8779
8780 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8781 objfile_name (per_objfile->objfile));
8782 }
8783
8784 /* Read in full symbols for PST, and anything it depends on. */
8785
8786 void
8787 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8788 {
8789 gdb_assert (!readin_p (objfile));
8790
8791 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8792 free_cached_comp_units freer (per_objfile);
8793 expand_dependencies (objfile);
8794
8795 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8796 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8797 }
8798
8799 /* See psympriv.h. */
8800
8801 bool
8802 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8803 {
8804 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8805 return per_objfile->symtab_set_p (per_cu_data);
8806 }
8807
8808 /* See psympriv.h. */
8809
8810 compunit_symtab *
8811 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8812 {
8813 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8814 return per_objfile->get_symtab (per_cu_data);
8815 }
8816
8817 /* Trivial hash function for die_info: the hash value of a DIE
8818 is its offset in .debug_info for this objfile. */
8819
8820 static hashval_t
8821 die_hash (const void *item)
8822 {
8823 const struct die_info *die = (const struct die_info *) item;
8824
8825 return to_underlying (die->sect_off);
8826 }
8827
8828 /* Trivial comparison function for die_info structures: two DIEs
8829 are equal if they have the same offset. */
8830
8831 static int
8832 die_eq (const void *item_lhs, const void *item_rhs)
8833 {
8834 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8835 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8836
8837 return die_lhs->sect_off == die_rhs->sect_off;
8838 }
8839
8840 /* Load the DIEs associated with PER_CU into memory.
8841
8842 In some cases, the caller, while reading partial symbols, will need to load
8843 the full symbols for the CU for some reason. It will already have a
8844 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8845 rather than creating a new one. */
8846
8847 static void
8848 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8849 dwarf2_per_objfile *per_objfile,
8850 dwarf2_cu *existing_cu,
8851 bool skip_partial,
8852 enum language pretend_language)
8853 {
8854 gdb_assert (! this_cu->is_debug_types);
8855
8856 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8857 if (reader.dummy_p)
8858 return;
8859
8860 struct dwarf2_cu *cu = reader.cu;
8861 const gdb_byte *info_ptr = reader.info_ptr;
8862
8863 gdb_assert (cu->die_hash == NULL);
8864 cu->die_hash =
8865 htab_create_alloc_ex (cu->header.length / 12,
8866 die_hash,
8867 die_eq,
8868 NULL,
8869 &cu->comp_unit_obstack,
8870 hashtab_obstack_allocate,
8871 dummy_obstack_deallocate);
8872
8873 if (reader.comp_unit_die->has_children)
8874 reader.comp_unit_die->child
8875 = read_die_and_siblings (&reader, reader.info_ptr,
8876 &info_ptr, reader.comp_unit_die);
8877 cu->dies = reader.comp_unit_die;
8878 /* comp_unit_die is not stored in die_hash, no need. */
8879
8880 /* We try not to read any attributes in this function, because not
8881 all CUs needed for references have been loaded yet, and symbol
8882 table processing isn't initialized. But we have to set the CU language,
8883 or we won't be able to build types correctly.
8884 Similarly, if we do not read the producer, we can not apply
8885 producer-specific interpretation. */
8886 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8887
8888 reader.keep ();
8889 }
8890
8891 /* Add a DIE to the delayed physname list. */
8892
8893 static void
8894 add_to_method_list (struct type *type, int fnfield_index, int index,
8895 const char *name, struct die_info *die,
8896 struct dwarf2_cu *cu)
8897 {
8898 struct delayed_method_info mi;
8899 mi.type = type;
8900 mi.fnfield_index = fnfield_index;
8901 mi.index = index;
8902 mi.name = name;
8903 mi.die = die;
8904 cu->method_list.push_back (mi);
8905 }
8906
8907 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8908 "const" / "volatile". If so, decrements LEN by the length of the
8909 modifier and return true. Otherwise return false. */
8910
8911 template<size_t N>
8912 static bool
8913 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8914 {
8915 size_t mod_len = sizeof (mod) - 1;
8916 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8917 {
8918 len -= mod_len;
8919 return true;
8920 }
8921 return false;
8922 }
8923
8924 /* Compute the physnames of any methods on the CU's method list.
8925
8926 The computation of method physnames is delayed in order to avoid the
8927 (bad) condition that one of the method's formal parameters is of an as yet
8928 incomplete type. */
8929
8930 static void
8931 compute_delayed_physnames (struct dwarf2_cu *cu)
8932 {
8933 /* Only C++ delays computing physnames. */
8934 if (cu->method_list.empty ())
8935 return;
8936 gdb_assert (cu->language == language_cplus);
8937
8938 for (const delayed_method_info &mi : cu->method_list)
8939 {
8940 const char *physname;
8941 struct fn_fieldlist *fn_flp
8942 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8943 physname = dwarf2_physname (mi.name, mi.die, cu);
8944 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8945 = physname ? physname : "";
8946
8947 /* Since there's no tag to indicate whether a method is a
8948 const/volatile overload, extract that information out of the
8949 demangled name. */
8950 if (physname != NULL)
8951 {
8952 size_t len = strlen (physname);
8953
8954 while (1)
8955 {
8956 if (physname[len] == ')') /* shortcut */
8957 break;
8958 else if (check_modifier (physname, len, " const"))
8959 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8960 else if (check_modifier (physname, len, " volatile"))
8961 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8962 else
8963 break;
8964 }
8965 }
8966 }
8967
8968 /* The list is no longer needed. */
8969 cu->method_list.clear ();
8970 }
8971
8972 /* Go objects should be embedded in a DW_TAG_module DIE,
8973 and it's not clear if/how imported objects will appear.
8974 To keep Go support simple until that's worked out,
8975 go back through what we've read and create something usable.
8976 We could do this while processing each DIE, and feels kinda cleaner,
8977 but that way is more invasive.
8978 This is to, for example, allow the user to type "p var" or "b main"
8979 without having to specify the package name, and allow lookups
8980 of module.object to work in contexts that use the expression
8981 parser. */
8982
8983 static void
8984 fixup_go_packaging (struct dwarf2_cu *cu)
8985 {
8986 gdb::unique_xmalloc_ptr<char> package_name;
8987 struct pending *list;
8988 int i;
8989
8990 for (list = *cu->get_builder ()->get_global_symbols ();
8991 list != NULL;
8992 list = list->next)
8993 {
8994 for (i = 0; i < list->nsyms; ++i)
8995 {
8996 struct symbol *sym = list->symbol[i];
8997
8998 if (sym->language () == language_go
8999 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9000 {
9001 gdb::unique_xmalloc_ptr<char> this_package_name
9002 (go_symbol_package_name (sym));
9003
9004 if (this_package_name == NULL)
9005 continue;
9006 if (package_name == NULL)
9007 package_name = std::move (this_package_name);
9008 else
9009 {
9010 struct objfile *objfile = cu->per_objfile->objfile;
9011 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9012 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9013 (symbol_symtab (sym) != NULL
9014 ? symtab_to_filename_for_display
9015 (symbol_symtab (sym))
9016 : objfile_name (objfile)),
9017 this_package_name.get (), package_name.get ());
9018 }
9019 }
9020 }
9021 }
9022
9023 if (package_name != NULL)
9024 {
9025 struct objfile *objfile = cu->per_objfile->objfile;
9026 const char *saved_package_name = objfile->intern (package_name.get ());
9027 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9028 saved_package_name);
9029 struct symbol *sym;
9030
9031 sym = new (&objfile->objfile_obstack) symbol;
9032 sym->set_language (language_go, &objfile->objfile_obstack);
9033 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9034 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9035 e.g., "main" finds the "main" module and not C's main(). */
9036 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9037 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9038 SYMBOL_TYPE (sym) = type;
9039
9040 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9041 }
9042 }
9043
9044 /* Allocate a fully-qualified name consisting of the two parts on the
9045 obstack. */
9046
9047 static const char *
9048 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9049 {
9050 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9051 }
9052
9053 /* A helper that allocates a variant part to attach to a Rust enum
9054 type. OBSTACK is where the results should be allocated. TYPE is
9055 the type we're processing. DISCRIMINANT_INDEX is the index of the
9056 discriminant. It must be the index of one of the fields of TYPE,
9057 or -1 to mean there is no discriminant (univariant enum).
9058 DEFAULT_INDEX is the index of the default field; or -1 if there is
9059 no default. RANGES is indexed by "effective" field number (the
9060 field index, but omitting the discriminant and default fields) and
9061 must hold the discriminant values used by the variants. Note that
9062 RANGES must have a lifetime at least as long as OBSTACK -- either
9063 already allocated on it, or static. */
9064
9065 static void
9066 alloc_rust_variant (struct obstack *obstack, struct type *type,
9067 int discriminant_index, int default_index,
9068 gdb::array_view<discriminant_range> ranges)
9069 {
9070 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9071 gdb_assert (discriminant_index == -1
9072 || (discriminant_index >= 0
9073 && discriminant_index < type->num_fields ()));
9074 gdb_assert (default_index == -1
9075 || (default_index >= 0 && default_index < type->num_fields ()));
9076
9077 /* We have one variant for each non-discriminant field. */
9078 int n_variants = type->num_fields ();
9079 if (discriminant_index != -1)
9080 --n_variants;
9081
9082 variant *variants = new (obstack) variant[n_variants];
9083 int var_idx = 0;
9084 int range_idx = 0;
9085 for (int i = 0; i < type->num_fields (); ++i)
9086 {
9087 if (i == discriminant_index)
9088 continue;
9089
9090 variants[var_idx].first_field = i;
9091 variants[var_idx].last_field = i + 1;
9092
9093 /* The default field does not need a range, but other fields do.
9094 We skipped the discriminant above. */
9095 if (i != default_index)
9096 {
9097 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9098 ++range_idx;
9099 }
9100
9101 ++var_idx;
9102 }
9103
9104 gdb_assert (range_idx == ranges.size ());
9105 gdb_assert (var_idx == n_variants);
9106
9107 variant_part *part = new (obstack) variant_part;
9108 part->discriminant_index = discriminant_index;
9109 /* If there is no discriminant, then whether it is signed is of no
9110 consequence. */
9111 part->is_unsigned
9112 = (discriminant_index == -1
9113 ? false
9114 : type->field (discriminant_index).type ()->is_unsigned ());
9115 part->variants = gdb::array_view<variant> (variants, n_variants);
9116
9117 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9118 gdb::array_view<variant_part> *prop_value
9119 = new (storage) gdb::array_view<variant_part> (part, 1);
9120
9121 struct dynamic_prop prop;
9122 prop.set_variant_parts (prop_value);
9123
9124 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9125 }
9126
9127 /* Some versions of rustc emitted enums in an unusual way.
9128
9129 Ordinary enums were emitted as unions. The first element of each
9130 structure in the union was named "RUST$ENUM$DISR". This element
9131 held the discriminant.
9132
9133 These versions of Rust also implemented the "non-zero"
9134 optimization. When the enum had two values, and one is empty and
9135 the other holds a pointer that cannot be zero, the pointer is used
9136 as the discriminant, with a zero value meaning the empty variant.
9137 Here, the union's first member is of the form
9138 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9139 where the fieldnos are the indices of the fields that should be
9140 traversed in order to find the field (which may be several fields deep)
9141 and the variantname is the name of the variant of the case when the
9142 field is zero.
9143
9144 This function recognizes whether TYPE is of one of these forms,
9145 and, if so, smashes it to be a variant type. */
9146
9147 static void
9148 quirk_rust_enum (struct type *type, struct objfile *objfile)
9149 {
9150 gdb_assert (type->code () == TYPE_CODE_UNION);
9151
9152 /* We don't need to deal with empty enums. */
9153 if (type->num_fields () == 0)
9154 return;
9155
9156 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9157 if (type->num_fields () == 1
9158 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9159 {
9160 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9161
9162 /* Decode the field name to find the offset of the
9163 discriminant. */
9164 ULONGEST bit_offset = 0;
9165 struct type *field_type = type->field (0).type ();
9166 while (name[0] >= '0' && name[0] <= '9')
9167 {
9168 char *tail;
9169 unsigned long index = strtoul (name, &tail, 10);
9170 name = tail;
9171 if (*name != '$'
9172 || index >= field_type->num_fields ()
9173 || (TYPE_FIELD_LOC_KIND (field_type, index)
9174 != FIELD_LOC_KIND_BITPOS))
9175 {
9176 complaint (_("Could not parse Rust enum encoding string \"%s\""
9177 "[in module %s]"),
9178 TYPE_FIELD_NAME (type, 0),
9179 objfile_name (objfile));
9180 return;
9181 }
9182 ++name;
9183
9184 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9185 field_type = field_type->field (index).type ();
9186 }
9187
9188 /* Smash this type to be a structure type. We have to do this
9189 because the type has already been recorded. */
9190 type->set_code (TYPE_CODE_STRUCT);
9191 type->set_num_fields (3);
9192 /* Save the field we care about. */
9193 struct field saved_field = type->field (0);
9194 type->set_fields
9195 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9196
9197 /* Put the discriminant at index 0. */
9198 type->field (0).set_type (field_type);
9199 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9200 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9201 SET_FIELD_BITPOS (type->field (0), bit_offset);
9202
9203 /* The order of fields doesn't really matter, so put the real
9204 field at index 1 and the data-less field at index 2. */
9205 type->field (1) = saved_field;
9206 TYPE_FIELD_NAME (type, 1)
9207 = rust_last_path_segment (type->field (1).type ()->name ());
9208 type->field (1).type ()->set_name
9209 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9210 TYPE_FIELD_NAME (type, 1)));
9211
9212 const char *dataless_name
9213 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9214 name);
9215 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9216 dataless_name);
9217 type->field (2).set_type (dataless_type);
9218 /* NAME points into the original discriminant name, which
9219 already has the correct lifetime. */
9220 TYPE_FIELD_NAME (type, 2) = name;
9221 SET_FIELD_BITPOS (type->field (2), 0);
9222
9223 /* Indicate that this is a variant type. */
9224 static discriminant_range ranges[1] = { { 0, 0 } };
9225 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9226 }
9227 /* A union with a single anonymous field is probably an old-style
9228 univariant enum. */
9229 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9230 {
9231 /* Smash this type to be a structure type. We have to do this
9232 because the type has already been recorded. */
9233 type->set_code (TYPE_CODE_STRUCT);
9234
9235 struct type *field_type = type->field (0).type ();
9236 const char *variant_name
9237 = rust_last_path_segment (field_type->name ());
9238 TYPE_FIELD_NAME (type, 0) = variant_name;
9239 field_type->set_name
9240 (rust_fully_qualify (&objfile->objfile_obstack,
9241 type->name (), variant_name));
9242
9243 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9244 }
9245 else
9246 {
9247 struct type *disr_type = nullptr;
9248 for (int i = 0; i < type->num_fields (); ++i)
9249 {
9250 disr_type = type->field (i).type ();
9251
9252 if (disr_type->code () != TYPE_CODE_STRUCT)
9253 {
9254 /* All fields of a true enum will be structs. */
9255 return;
9256 }
9257 else if (disr_type->num_fields () == 0)
9258 {
9259 /* Could be data-less variant, so keep going. */
9260 disr_type = nullptr;
9261 }
9262 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9263 "RUST$ENUM$DISR") != 0)
9264 {
9265 /* Not a Rust enum. */
9266 return;
9267 }
9268 else
9269 {
9270 /* Found one. */
9271 break;
9272 }
9273 }
9274
9275 /* If we got here without a discriminant, then it's probably
9276 just a union. */
9277 if (disr_type == nullptr)
9278 return;
9279
9280 /* Smash this type to be a structure type. We have to do this
9281 because the type has already been recorded. */
9282 type->set_code (TYPE_CODE_STRUCT);
9283
9284 /* Make space for the discriminant field. */
9285 struct field *disr_field = &disr_type->field (0);
9286 field *new_fields
9287 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9288 * sizeof (struct field)));
9289 memcpy (new_fields + 1, type->fields (),
9290 type->num_fields () * sizeof (struct field));
9291 type->set_fields (new_fields);
9292 type->set_num_fields (type->num_fields () + 1);
9293
9294 /* Install the discriminant at index 0 in the union. */
9295 type->field (0) = *disr_field;
9296 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9297 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9298
9299 /* We need a way to find the correct discriminant given a
9300 variant name. For convenience we build a map here. */
9301 struct type *enum_type = disr_field->type ();
9302 std::unordered_map<std::string, ULONGEST> discriminant_map;
9303 for (int i = 0; i < enum_type->num_fields (); ++i)
9304 {
9305 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9306 {
9307 const char *name
9308 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9309 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9310 }
9311 }
9312
9313 int n_fields = type->num_fields ();
9314 /* We don't need a range entry for the discriminant, but we do
9315 need one for every other field, as there is no default
9316 variant. */
9317 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9318 discriminant_range,
9319 n_fields - 1);
9320 /* Skip the discriminant here. */
9321 for (int i = 1; i < n_fields; ++i)
9322 {
9323 /* Find the final word in the name of this variant's type.
9324 That name can be used to look up the correct
9325 discriminant. */
9326 const char *variant_name
9327 = rust_last_path_segment (type->field (i).type ()->name ());
9328
9329 auto iter = discriminant_map.find (variant_name);
9330 if (iter != discriminant_map.end ())
9331 {
9332 ranges[i - 1].low = iter->second;
9333 ranges[i - 1].high = iter->second;
9334 }
9335
9336 /* In Rust, each element should have the size of the
9337 enclosing enum. */
9338 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9339
9340 /* Remove the discriminant field, if it exists. */
9341 struct type *sub_type = type->field (i).type ();
9342 if (sub_type->num_fields () > 0)
9343 {
9344 sub_type->set_num_fields (sub_type->num_fields () - 1);
9345 sub_type->set_fields (sub_type->fields () + 1);
9346 }
9347 TYPE_FIELD_NAME (type, i) = variant_name;
9348 sub_type->set_name
9349 (rust_fully_qualify (&objfile->objfile_obstack,
9350 type->name (), variant_name));
9351 }
9352
9353 /* Indicate that this is a variant type. */
9354 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9355 gdb::array_view<discriminant_range> (ranges,
9356 n_fields - 1));
9357 }
9358 }
9359
9360 /* Rewrite some Rust unions to be structures with variants parts. */
9361
9362 static void
9363 rust_union_quirks (struct dwarf2_cu *cu)
9364 {
9365 gdb_assert (cu->language == language_rust);
9366 for (type *type_ : cu->rust_unions)
9367 quirk_rust_enum (type_, cu->per_objfile->objfile);
9368 /* We don't need this any more. */
9369 cu->rust_unions.clear ();
9370 }
9371
9372 /* See read.h. */
9373
9374 type_unit_group_unshareable *
9375 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9376 {
9377 auto iter = this->m_type_units.find (tu_group);
9378 if (iter != this->m_type_units.end ())
9379 return iter->second.get ();
9380
9381 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9382 type_unit_group_unshareable *result = uniq.get ();
9383 this->m_type_units[tu_group] = std::move (uniq);
9384 return result;
9385 }
9386
9387 struct type *
9388 dwarf2_per_objfile::get_type_for_signatured_type
9389 (signatured_type *sig_type) const
9390 {
9391 auto iter = this->m_type_map.find (sig_type);
9392 if (iter == this->m_type_map.end ())
9393 return nullptr;
9394
9395 return iter->second;
9396 }
9397
9398 void dwarf2_per_objfile::set_type_for_signatured_type
9399 (signatured_type *sig_type, struct type *type)
9400 {
9401 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9402
9403 this->m_type_map[sig_type] = type;
9404 }
9405
9406 /* A helper function for computing the list of all symbol tables
9407 included by PER_CU. */
9408
9409 static void
9410 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9411 htab_t all_children, htab_t all_type_symtabs,
9412 dwarf2_per_cu_data *per_cu,
9413 dwarf2_per_objfile *per_objfile,
9414 struct compunit_symtab *immediate_parent)
9415 {
9416 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9417 if (*slot != NULL)
9418 {
9419 /* This inclusion and its children have been processed. */
9420 return;
9421 }
9422
9423 *slot = per_cu;
9424
9425 /* Only add a CU if it has a symbol table. */
9426 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9427 if (cust != NULL)
9428 {
9429 /* If this is a type unit only add its symbol table if we haven't
9430 seen it yet (type unit per_cu's can share symtabs). */
9431 if (per_cu->is_debug_types)
9432 {
9433 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9434 if (*slot == NULL)
9435 {
9436 *slot = cust;
9437 result->push_back (cust);
9438 if (cust->user == NULL)
9439 cust->user = immediate_parent;
9440 }
9441 }
9442 else
9443 {
9444 result->push_back (cust);
9445 if (cust->user == NULL)
9446 cust->user = immediate_parent;
9447 }
9448 }
9449
9450 if (!per_cu->imported_symtabs_empty ())
9451 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9452 {
9453 recursively_compute_inclusions (result, all_children,
9454 all_type_symtabs, ptr, per_objfile,
9455 cust);
9456 }
9457 }
9458
9459 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9460 PER_CU. */
9461
9462 static void
9463 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9464 dwarf2_per_objfile *per_objfile)
9465 {
9466 gdb_assert (! per_cu->is_debug_types);
9467
9468 if (!per_cu->imported_symtabs_empty ())
9469 {
9470 int len;
9471 std::vector<compunit_symtab *> result_symtabs;
9472 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9473
9474 /* If we don't have a symtab, we can just skip this case. */
9475 if (cust == NULL)
9476 return;
9477
9478 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9479 htab_eq_pointer,
9480 NULL, xcalloc, xfree));
9481 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9482 htab_eq_pointer,
9483 NULL, xcalloc, xfree));
9484
9485 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9486 {
9487 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9488 all_type_symtabs.get (), ptr,
9489 per_objfile, cust);
9490 }
9491
9492 /* Now we have a transitive closure of all the included symtabs. */
9493 len = result_symtabs.size ();
9494 cust->includes
9495 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9496 struct compunit_symtab *, len + 1);
9497 memcpy (cust->includes, result_symtabs.data (),
9498 len * sizeof (compunit_symtab *));
9499 cust->includes[len] = NULL;
9500 }
9501 }
9502
9503 /* Compute the 'includes' field for the symtabs of all the CUs we just
9504 read. */
9505
9506 static void
9507 process_cu_includes (dwarf2_per_objfile *per_objfile)
9508 {
9509 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9510 {
9511 if (! iter->is_debug_types)
9512 compute_compunit_symtab_includes (iter, per_objfile);
9513 }
9514
9515 per_objfile->per_bfd->just_read_cus.clear ();
9516 }
9517
9518 /* Generate full symbol information for CU, whose DIEs have
9519 already been loaded into memory. */
9520
9521 static void
9522 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9523 {
9524 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9525 struct objfile *objfile = per_objfile->objfile;
9526 struct gdbarch *gdbarch = objfile->arch ();
9527 CORE_ADDR lowpc, highpc;
9528 struct compunit_symtab *cust;
9529 CORE_ADDR baseaddr;
9530 struct block *static_block;
9531 CORE_ADDR addr;
9532
9533 baseaddr = objfile->text_section_offset ();
9534
9535 /* Clear the list here in case something was left over. */
9536 cu->method_list.clear ();
9537
9538 cu->language = pretend_language;
9539 cu->language_defn = language_def (cu->language);
9540
9541 dwarf2_find_base_address (cu->dies, cu);
9542
9543 /* Before we start reading the top-level DIE, ensure it has a valid tag
9544 type. */
9545 switch (cu->dies->tag)
9546 {
9547 case DW_TAG_compile_unit:
9548 case DW_TAG_partial_unit:
9549 case DW_TAG_type_unit:
9550 break;
9551 default:
9552 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9553 dwarf_tag_name (cu->dies->tag),
9554 sect_offset_str (cu->per_cu->sect_off),
9555 objfile_name (per_objfile->objfile));
9556 }
9557
9558 /* Do line number decoding in read_file_scope () */
9559 process_die (cu->dies, cu);
9560
9561 /* For now fudge the Go package. */
9562 if (cu->language == language_go)
9563 fixup_go_packaging (cu);
9564
9565 /* Now that we have processed all the DIEs in the CU, all the types
9566 should be complete, and it should now be safe to compute all of the
9567 physnames. */
9568 compute_delayed_physnames (cu);
9569
9570 if (cu->language == language_rust)
9571 rust_union_quirks (cu);
9572
9573 /* Some compilers don't define a DW_AT_high_pc attribute for the
9574 compilation unit. If the DW_AT_high_pc is missing, synthesize
9575 it, by scanning the DIE's below the compilation unit. */
9576 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9577
9578 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9579 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9580
9581 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9582 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9583 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9584 addrmap to help ensure it has an accurate map of pc values belonging to
9585 this comp unit. */
9586 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9587
9588 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9589 SECT_OFF_TEXT (objfile),
9590 0);
9591
9592 if (cust != NULL)
9593 {
9594 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9595
9596 /* Set symtab language to language from DW_AT_language. If the
9597 compilation is from a C file generated by language preprocessors, do
9598 not set the language if it was already deduced by start_subfile. */
9599 if (!(cu->language == language_c
9600 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9601 COMPUNIT_FILETABS (cust)->language = cu->language;
9602
9603 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9604 produce DW_AT_location with location lists but it can be possibly
9605 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9606 there were bugs in prologue debug info, fixed later in GCC-4.5
9607 by "unwind info for epilogues" patch (which is not directly related).
9608
9609 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9610 needed, it would be wrong due to missing DW_AT_producer there.
9611
9612 Still one can confuse GDB by using non-standard GCC compilation
9613 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9614 */
9615 if (cu->has_loclist && gcc_4_minor >= 5)
9616 cust->locations_valid = 1;
9617
9618 if (gcc_4_minor >= 5)
9619 cust->epilogue_unwind_valid = 1;
9620
9621 cust->call_site_htab = cu->call_site_htab;
9622 }
9623
9624 per_objfile->set_symtab (cu->per_cu, cust);
9625
9626 /* Push it for inclusion processing later. */
9627 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9628
9629 /* Not needed any more. */
9630 cu->reset_builder ();
9631 }
9632
9633 /* Generate full symbol information for type unit CU, whose DIEs have
9634 already been loaded into memory. */
9635
9636 static void
9637 process_full_type_unit (dwarf2_cu *cu,
9638 enum language pretend_language)
9639 {
9640 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9641 struct objfile *objfile = per_objfile->objfile;
9642 struct compunit_symtab *cust;
9643 struct signatured_type *sig_type;
9644
9645 gdb_assert (cu->per_cu->is_debug_types);
9646 sig_type = (struct signatured_type *) cu->per_cu;
9647
9648 /* Clear the list here in case something was left over. */
9649 cu->method_list.clear ();
9650
9651 cu->language = pretend_language;
9652 cu->language_defn = language_def (cu->language);
9653
9654 /* The symbol tables are set up in read_type_unit_scope. */
9655 process_die (cu->dies, cu);
9656
9657 /* For now fudge the Go package. */
9658 if (cu->language == language_go)
9659 fixup_go_packaging (cu);
9660
9661 /* Now that we have processed all the DIEs in the CU, all the types
9662 should be complete, and it should now be safe to compute all of the
9663 physnames. */
9664 compute_delayed_physnames (cu);
9665
9666 if (cu->language == language_rust)
9667 rust_union_quirks (cu);
9668
9669 /* TUs share symbol tables.
9670 If this is the first TU to use this symtab, complete the construction
9671 of it with end_expandable_symtab. Otherwise, complete the addition of
9672 this TU's symbols to the existing symtab. */
9673 type_unit_group_unshareable *tug_unshare =
9674 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9675 if (tug_unshare->compunit_symtab == NULL)
9676 {
9677 buildsym_compunit *builder = cu->get_builder ();
9678 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9679 tug_unshare->compunit_symtab = cust;
9680
9681 if (cust != NULL)
9682 {
9683 /* Set symtab language to language from DW_AT_language. If the
9684 compilation is from a C file generated by language preprocessors,
9685 do not set the language if it was already deduced by
9686 start_subfile. */
9687 if (!(cu->language == language_c
9688 && COMPUNIT_FILETABS (cust)->language != language_c))
9689 COMPUNIT_FILETABS (cust)->language = cu->language;
9690 }
9691 }
9692 else
9693 {
9694 cu->get_builder ()->augment_type_symtab ();
9695 cust = tug_unshare->compunit_symtab;
9696 }
9697
9698 per_objfile->set_symtab (cu->per_cu, cust);
9699
9700 /* Not needed any more. */
9701 cu->reset_builder ();
9702 }
9703
9704 /* Process an imported unit DIE. */
9705
9706 static void
9707 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9708 {
9709 struct attribute *attr;
9710
9711 /* For now we don't handle imported units in type units. */
9712 if (cu->per_cu->is_debug_types)
9713 {
9714 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9715 " supported in type units [in module %s]"),
9716 objfile_name (cu->per_objfile->objfile));
9717 }
9718
9719 attr = dwarf2_attr (die, DW_AT_import, cu);
9720 if (attr != NULL)
9721 {
9722 sect_offset sect_off = attr->get_ref_die_offset ();
9723 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9724 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9725 dwarf2_per_cu_data *per_cu
9726 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9727
9728 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9729 into another compilation unit, at root level. Regard this as a hint,
9730 and ignore it. */
9731 if (die->parent && die->parent->parent == NULL
9732 && per_cu->unit_type == DW_UT_compile
9733 && per_cu->lang == language_cplus)
9734 return;
9735
9736 /* If necessary, add it to the queue and load its DIEs. */
9737 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9738 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9739 false, cu->language);
9740
9741 cu->per_cu->imported_symtabs_push (per_cu);
9742 }
9743 }
9744
9745 /* RAII object that represents a process_die scope: i.e.,
9746 starts/finishes processing a DIE. */
9747 class process_die_scope
9748 {
9749 public:
9750 process_die_scope (die_info *die, dwarf2_cu *cu)
9751 : m_die (die), m_cu (cu)
9752 {
9753 /* We should only be processing DIEs not already in process. */
9754 gdb_assert (!m_die->in_process);
9755 m_die->in_process = true;
9756 }
9757
9758 ~process_die_scope ()
9759 {
9760 m_die->in_process = false;
9761
9762 /* If we're done processing the DIE for the CU that owns the line
9763 header, we don't need the line header anymore. */
9764 if (m_cu->line_header_die_owner == m_die)
9765 {
9766 delete m_cu->line_header;
9767 m_cu->line_header = NULL;
9768 m_cu->line_header_die_owner = NULL;
9769 }
9770 }
9771
9772 private:
9773 die_info *m_die;
9774 dwarf2_cu *m_cu;
9775 };
9776
9777 /* Process a die and its children. */
9778
9779 static void
9780 process_die (struct die_info *die, struct dwarf2_cu *cu)
9781 {
9782 process_die_scope scope (die, cu);
9783
9784 switch (die->tag)
9785 {
9786 case DW_TAG_padding:
9787 break;
9788 case DW_TAG_compile_unit:
9789 case DW_TAG_partial_unit:
9790 read_file_scope (die, cu);
9791 break;
9792 case DW_TAG_type_unit:
9793 read_type_unit_scope (die, cu);
9794 break;
9795 case DW_TAG_subprogram:
9796 /* Nested subprograms in Fortran get a prefix. */
9797 if (cu->language == language_fortran
9798 && die->parent != NULL
9799 && die->parent->tag == DW_TAG_subprogram)
9800 cu->processing_has_namespace_info = true;
9801 /* Fall through. */
9802 case DW_TAG_inlined_subroutine:
9803 read_func_scope (die, cu);
9804 break;
9805 case DW_TAG_lexical_block:
9806 case DW_TAG_try_block:
9807 case DW_TAG_catch_block:
9808 read_lexical_block_scope (die, cu);
9809 break;
9810 case DW_TAG_call_site:
9811 case DW_TAG_GNU_call_site:
9812 read_call_site_scope (die, cu);
9813 break;
9814 case DW_TAG_class_type:
9815 case DW_TAG_interface_type:
9816 case DW_TAG_structure_type:
9817 case DW_TAG_union_type:
9818 process_structure_scope (die, cu);
9819 break;
9820 case DW_TAG_enumeration_type:
9821 process_enumeration_scope (die, cu);
9822 break;
9823
9824 /* These dies have a type, but processing them does not create
9825 a symbol or recurse to process the children. Therefore we can
9826 read them on-demand through read_type_die. */
9827 case DW_TAG_subroutine_type:
9828 case DW_TAG_set_type:
9829 case DW_TAG_pointer_type:
9830 case DW_TAG_ptr_to_member_type:
9831 case DW_TAG_reference_type:
9832 case DW_TAG_rvalue_reference_type:
9833 case DW_TAG_string_type:
9834 break;
9835
9836 case DW_TAG_array_type:
9837 /* We only need to handle this case for Ada -- in other
9838 languages, it's normal for the compiler to emit a typedef
9839 instead. */
9840 if (cu->language != language_ada)
9841 break;
9842 /* FALLTHROUGH */
9843 case DW_TAG_base_type:
9844 case DW_TAG_subrange_type:
9845 case DW_TAG_typedef:
9846 /* Add a typedef symbol for the type definition, if it has a
9847 DW_AT_name. */
9848 new_symbol (die, read_type_die (die, cu), cu);
9849 break;
9850 case DW_TAG_common_block:
9851 read_common_block (die, cu);
9852 break;
9853 case DW_TAG_common_inclusion:
9854 break;
9855 case DW_TAG_namespace:
9856 cu->processing_has_namespace_info = true;
9857 read_namespace (die, cu);
9858 break;
9859 case DW_TAG_module:
9860 cu->processing_has_namespace_info = true;
9861 read_module (die, cu);
9862 break;
9863 case DW_TAG_imported_declaration:
9864 cu->processing_has_namespace_info = true;
9865 if (read_namespace_alias (die, cu))
9866 break;
9867 /* The declaration is not a global namespace alias. */
9868 /* Fall through. */
9869 case DW_TAG_imported_module:
9870 cu->processing_has_namespace_info = true;
9871 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9872 || cu->language != language_fortran))
9873 complaint (_("Tag '%s' has unexpected children"),
9874 dwarf_tag_name (die->tag));
9875 read_import_statement (die, cu);
9876 break;
9877
9878 case DW_TAG_imported_unit:
9879 process_imported_unit_die (die, cu);
9880 break;
9881
9882 case DW_TAG_variable:
9883 read_variable (die, cu);
9884 break;
9885
9886 default:
9887 new_symbol (die, NULL, cu);
9888 break;
9889 }
9890 }
9891 \f
9892 /* DWARF name computation. */
9893
9894 /* A helper function for dwarf2_compute_name which determines whether DIE
9895 needs to have the name of the scope prepended to the name listed in the
9896 die. */
9897
9898 static int
9899 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9900 {
9901 struct attribute *attr;
9902
9903 switch (die->tag)
9904 {
9905 case DW_TAG_namespace:
9906 case DW_TAG_typedef:
9907 case DW_TAG_class_type:
9908 case DW_TAG_interface_type:
9909 case DW_TAG_structure_type:
9910 case DW_TAG_union_type:
9911 case DW_TAG_enumeration_type:
9912 case DW_TAG_enumerator:
9913 case DW_TAG_subprogram:
9914 case DW_TAG_inlined_subroutine:
9915 case DW_TAG_member:
9916 case DW_TAG_imported_declaration:
9917 return 1;
9918
9919 case DW_TAG_variable:
9920 case DW_TAG_constant:
9921 /* We only need to prefix "globally" visible variables. These include
9922 any variable marked with DW_AT_external or any variable that
9923 lives in a namespace. [Variables in anonymous namespaces
9924 require prefixing, but they are not DW_AT_external.] */
9925
9926 if (dwarf2_attr (die, DW_AT_specification, cu))
9927 {
9928 struct dwarf2_cu *spec_cu = cu;
9929
9930 return die_needs_namespace (die_specification (die, &spec_cu),
9931 spec_cu);
9932 }
9933
9934 attr = dwarf2_attr (die, DW_AT_external, cu);
9935 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9936 && die->parent->tag != DW_TAG_module)
9937 return 0;
9938 /* A variable in a lexical block of some kind does not need a
9939 namespace, even though in C++ such variables may be external
9940 and have a mangled name. */
9941 if (die->parent->tag == DW_TAG_lexical_block
9942 || die->parent->tag == DW_TAG_try_block
9943 || die->parent->tag == DW_TAG_catch_block
9944 || die->parent->tag == DW_TAG_subprogram)
9945 return 0;
9946 return 1;
9947
9948 default:
9949 return 0;
9950 }
9951 }
9952
9953 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9954 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9955 defined for the given DIE. */
9956
9957 static struct attribute *
9958 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9959 {
9960 struct attribute *attr;
9961
9962 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9963 if (attr == NULL)
9964 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9965
9966 return attr;
9967 }
9968
9969 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9970 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9971 defined for the given DIE. */
9972
9973 static const char *
9974 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9975 {
9976 const char *linkage_name;
9977
9978 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9979 if (linkage_name == NULL)
9980 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9981
9982 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
9983 See https://github.com/rust-lang/rust/issues/32925. */
9984 if (cu->language == language_rust && linkage_name != NULL
9985 && strchr (linkage_name, '{') != NULL)
9986 linkage_name = NULL;
9987
9988 return linkage_name;
9989 }
9990
9991 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9992 compute the physname for the object, which include a method's:
9993 - formal parameters (C++),
9994 - receiver type (Go),
9995
9996 The term "physname" is a bit confusing.
9997 For C++, for example, it is the demangled name.
9998 For Go, for example, it's the mangled name.
9999
10000 For Ada, return the DIE's linkage name rather than the fully qualified
10001 name. PHYSNAME is ignored..
10002
10003 The result is allocated on the objfile->per_bfd's obstack and
10004 canonicalized. */
10005
10006 static const char *
10007 dwarf2_compute_name (const char *name,
10008 struct die_info *die, struct dwarf2_cu *cu,
10009 int physname)
10010 {
10011 struct objfile *objfile = cu->per_objfile->objfile;
10012
10013 if (name == NULL)
10014 name = dwarf2_name (die, cu);
10015
10016 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10017 but otherwise compute it by typename_concat inside GDB.
10018 FIXME: Actually this is not really true, or at least not always true.
10019 It's all very confusing. compute_and_set_names doesn't try to demangle
10020 Fortran names because there is no mangling standard. So new_symbol
10021 will set the demangled name to the result of dwarf2_full_name, and it is
10022 the demangled name that GDB uses if it exists. */
10023 if (cu->language == language_ada
10024 || (cu->language == language_fortran && physname))
10025 {
10026 /* For Ada unit, we prefer the linkage name over the name, as
10027 the former contains the exported name, which the user expects
10028 to be able to reference. Ideally, we want the user to be able
10029 to reference this entity using either natural or linkage name,
10030 but we haven't started looking at this enhancement yet. */
10031 const char *linkage_name = dw2_linkage_name (die, cu);
10032
10033 if (linkage_name != NULL)
10034 return linkage_name;
10035 }
10036
10037 /* These are the only languages we know how to qualify names in. */
10038 if (name != NULL
10039 && (cu->language == language_cplus
10040 || cu->language == language_fortran || cu->language == language_d
10041 || cu->language == language_rust))
10042 {
10043 if (die_needs_namespace (die, cu))
10044 {
10045 const char *prefix;
10046 const char *canonical_name = NULL;
10047
10048 string_file buf;
10049
10050 prefix = determine_prefix (die, cu);
10051 if (*prefix != '\0')
10052 {
10053 gdb::unique_xmalloc_ptr<char> prefixed_name
10054 (typename_concat (NULL, prefix, name, physname, cu));
10055
10056 buf.puts (prefixed_name.get ());
10057 }
10058 else
10059 buf.puts (name);
10060
10061 /* Template parameters may be specified in the DIE's DW_AT_name, or
10062 as children with DW_TAG_template_type_param or
10063 DW_TAG_value_type_param. If the latter, add them to the name
10064 here. If the name already has template parameters, then
10065 skip this step; some versions of GCC emit both, and
10066 it is more efficient to use the pre-computed name.
10067
10068 Something to keep in mind about this process: it is very
10069 unlikely, or in some cases downright impossible, to produce
10070 something that will match the mangled name of a function.
10071 If the definition of the function has the same debug info,
10072 we should be able to match up with it anyway. But fallbacks
10073 using the minimal symbol, for instance to find a method
10074 implemented in a stripped copy of libstdc++, will not work.
10075 If we do not have debug info for the definition, we will have to
10076 match them up some other way.
10077
10078 When we do name matching there is a related problem with function
10079 templates; two instantiated function templates are allowed to
10080 differ only by their return types, which we do not add here. */
10081
10082 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10083 {
10084 struct attribute *attr;
10085 struct die_info *child;
10086 int first = 1;
10087 const language_defn *cplus_lang = language_def (cu->language);
10088
10089 die->building_fullname = 1;
10090
10091 for (child = die->child; child != NULL; child = child->sibling)
10092 {
10093 struct type *type;
10094 LONGEST value;
10095 const gdb_byte *bytes;
10096 struct dwarf2_locexpr_baton *baton;
10097 struct value *v;
10098
10099 if (child->tag != DW_TAG_template_type_param
10100 && child->tag != DW_TAG_template_value_param)
10101 continue;
10102
10103 if (first)
10104 {
10105 buf.puts ("<");
10106 first = 0;
10107 }
10108 else
10109 buf.puts (", ");
10110
10111 attr = dwarf2_attr (child, DW_AT_type, cu);
10112 if (attr == NULL)
10113 {
10114 complaint (_("template parameter missing DW_AT_type"));
10115 buf.puts ("UNKNOWN_TYPE");
10116 continue;
10117 }
10118 type = die_type (child, cu);
10119
10120 if (child->tag == DW_TAG_template_type_param)
10121 {
10122 cplus_lang->print_type (type, "", &buf, -1, 0,
10123 &type_print_raw_options);
10124 continue;
10125 }
10126
10127 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10128 if (attr == NULL)
10129 {
10130 complaint (_("template parameter missing "
10131 "DW_AT_const_value"));
10132 buf.puts ("UNKNOWN_VALUE");
10133 continue;
10134 }
10135
10136 dwarf2_const_value_attr (attr, type, name,
10137 &cu->comp_unit_obstack, cu,
10138 &value, &bytes, &baton);
10139
10140 if (type->has_no_signedness ())
10141 /* GDB prints characters as NUMBER 'CHAR'. If that's
10142 changed, this can use value_print instead. */
10143 cplus_lang->printchar (value, type, &buf);
10144 else
10145 {
10146 struct value_print_options opts;
10147
10148 if (baton != NULL)
10149 v = dwarf2_evaluate_loc_desc (type, NULL,
10150 baton->data,
10151 baton->size,
10152 baton->per_cu,
10153 baton->per_objfile);
10154 else if (bytes != NULL)
10155 {
10156 v = allocate_value (type);
10157 memcpy (value_contents_writeable (v), bytes,
10158 TYPE_LENGTH (type));
10159 }
10160 else
10161 v = value_from_longest (type, value);
10162
10163 /* Specify decimal so that we do not depend on
10164 the radix. */
10165 get_formatted_print_options (&opts, 'd');
10166 opts.raw = 1;
10167 value_print (v, &buf, &opts);
10168 release_value (v);
10169 }
10170 }
10171
10172 die->building_fullname = 0;
10173
10174 if (!first)
10175 {
10176 /* Close the argument list, with a space if necessary
10177 (nested templates). */
10178 if (!buf.empty () && buf.string ().back () == '>')
10179 buf.puts (" >");
10180 else
10181 buf.puts (">");
10182 }
10183 }
10184
10185 /* For C++ methods, append formal parameter type
10186 information, if PHYSNAME. */
10187
10188 if (physname && die->tag == DW_TAG_subprogram
10189 && cu->language == language_cplus)
10190 {
10191 struct type *type = read_type_die (die, cu);
10192
10193 c_type_print_args (type, &buf, 1, cu->language,
10194 &type_print_raw_options);
10195
10196 if (cu->language == language_cplus)
10197 {
10198 /* Assume that an artificial first parameter is
10199 "this", but do not crash if it is not. RealView
10200 marks unnamed (and thus unused) parameters as
10201 artificial; there is no way to differentiate
10202 the two cases. */
10203 if (type->num_fields () > 0
10204 && TYPE_FIELD_ARTIFICIAL (type, 0)
10205 && type->field (0).type ()->code () == TYPE_CODE_PTR
10206 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10207 buf.puts (" const");
10208 }
10209 }
10210
10211 const std::string &intermediate_name = buf.string ();
10212
10213 if (cu->language == language_cplus)
10214 canonical_name
10215 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10216 objfile);
10217
10218 /* If we only computed INTERMEDIATE_NAME, or if
10219 INTERMEDIATE_NAME is already canonical, then we need to
10220 intern it. */
10221 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10222 name = objfile->intern (intermediate_name);
10223 else
10224 name = canonical_name;
10225 }
10226 }
10227
10228 return name;
10229 }
10230
10231 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10232 If scope qualifiers are appropriate they will be added. The result
10233 will be allocated on the storage_obstack, or NULL if the DIE does
10234 not have a name. NAME may either be from a previous call to
10235 dwarf2_name or NULL.
10236
10237 The output string will be canonicalized (if C++). */
10238
10239 static const char *
10240 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10241 {
10242 return dwarf2_compute_name (name, die, cu, 0);
10243 }
10244
10245 /* Construct a physname for the given DIE in CU. NAME may either be
10246 from a previous call to dwarf2_name or NULL. The result will be
10247 allocated on the objfile_objstack or NULL if the DIE does not have a
10248 name.
10249
10250 The output string will be canonicalized (if C++). */
10251
10252 static const char *
10253 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10254 {
10255 struct objfile *objfile = cu->per_objfile->objfile;
10256 const char *retval, *mangled = NULL, *canon = NULL;
10257 int need_copy = 1;
10258
10259 /* In this case dwarf2_compute_name is just a shortcut not building anything
10260 on its own. */
10261 if (!die_needs_namespace (die, cu))
10262 return dwarf2_compute_name (name, die, cu, 1);
10263
10264 if (cu->language != language_rust)
10265 mangled = dw2_linkage_name (die, cu);
10266
10267 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10268 has computed. */
10269 gdb::unique_xmalloc_ptr<char> demangled;
10270 if (mangled != NULL)
10271 {
10272
10273 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10274 {
10275 /* Do nothing (do not demangle the symbol name). */
10276 }
10277 else
10278 {
10279 /* Use DMGL_RET_DROP for C++ template functions to suppress
10280 their return type. It is easier for GDB users to search
10281 for such functions as `name(params)' than `long name(params)'.
10282 In such case the minimal symbol names do not match the full
10283 symbol names but for template functions there is never a need
10284 to look up their definition from their declaration so
10285 the only disadvantage remains the minimal symbol variant
10286 `long name(params)' does not have the proper inferior type. */
10287 demangled.reset (gdb_demangle (mangled,
10288 (DMGL_PARAMS | DMGL_ANSI
10289 | DMGL_RET_DROP)));
10290 }
10291 if (demangled)
10292 canon = demangled.get ();
10293 else
10294 {
10295 canon = mangled;
10296 need_copy = 0;
10297 }
10298 }
10299
10300 if (canon == NULL || check_physname)
10301 {
10302 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10303
10304 if (canon != NULL && strcmp (physname, canon) != 0)
10305 {
10306 /* It may not mean a bug in GDB. The compiler could also
10307 compute DW_AT_linkage_name incorrectly. But in such case
10308 GDB would need to be bug-to-bug compatible. */
10309
10310 complaint (_("Computed physname <%s> does not match demangled <%s> "
10311 "(from linkage <%s>) - DIE at %s [in module %s]"),
10312 physname, canon, mangled, sect_offset_str (die->sect_off),
10313 objfile_name (objfile));
10314
10315 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10316 is available here - over computed PHYSNAME. It is safer
10317 against both buggy GDB and buggy compilers. */
10318
10319 retval = canon;
10320 }
10321 else
10322 {
10323 retval = physname;
10324 need_copy = 0;
10325 }
10326 }
10327 else
10328 retval = canon;
10329
10330 if (need_copy)
10331 retval = objfile->intern (retval);
10332
10333 return retval;
10334 }
10335
10336 /* Inspect DIE in CU for a namespace alias. If one exists, record
10337 a new symbol for it.
10338
10339 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10340
10341 static int
10342 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10343 {
10344 struct attribute *attr;
10345
10346 /* If the die does not have a name, this is not a namespace
10347 alias. */
10348 attr = dwarf2_attr (die, DW_AT_name, cu);
10349 if (attr != NULL)
10350 {
10351 int num;
10352 struct die_info *d = die;
10353 struct dwarf2_cu *imported_cu = cu;
10354
10355 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10356 keep inspecting DIEs until we hit the underlying import. */
10357 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10358 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10359 {
10360 attr = dwarf2_attr (d, DW_AT_import, cu);
10361 if (attr == NULL)
10362 break;
10363
10364 d = follow_die_ref (d, attr, &imported_cu);
10365 if (d->tag != DW_TAG_imported_declaration)
10366 break;
10367 }
10368
10369 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10370 {
10371 complaint (_("DIE at %s has too many recursively imported "
10372 "declarations"), sect_offset_str (d->sect_off));
10373 return 0;
10374 }
10375
10376 if (attr != NULL)
10377 {
10378 struct type *type;
10379 sect_offset sect_off = attr->get_ref_die_offset ();
10380
10381 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10382 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10383 {
10384 /* This declaration is a global namespace alias. Add
10385 a symbol for it whose type is the aliased namespace. */
10386 new_symbol (die, type, cu);
10387 return 1;
10388 }
10389 }
10390 }
10391
10392 return 0;
10393 }
10394
10395 /* Return the using directives repository (global or local?) to use in the
10396 current context for CU.
10397
10398 For Ada, imported declarations can materialize renamings, which *may* be
10399 global. However it is impossible (for now?) in DWARF to distinguish
10400 "external" imported declarations and "static" ones. As all imported
10401 declarations seem to be static in all other languages, make them all CU-wide
10402 global only in Ada. */
10403
10404 static struct using_direct **
10405 using_directives (struct dwarf2_cu *cu)
10406 {
10407 if (cu->language == language_ada
10408 && cu->get_builder ()->outermost_context_p ())
10409 return cu->get_builder ()->get_global_using_directives ();
10410 else
10411 return cu->get_builder ()->get_local_using_directives ();
10412 }
10413
10414 /* Read the import statement specified by the given die and record it. */
10415
10416 static void
10417 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10418 {
10419 struct objfile *objfile = cu->per_objfile->objfile;
10420 struct attribute *import_attr;
10421 struct die_info *imported_die, *child_die;
10422 struct dwarf2_cu *imported_cu;
10423 const char *imported_name;
10424 const char *imported_name_prefix;
10425 const char *canonical_name;
10426 const char *import_alias;
10427 const char *imported_declaration = NULL;
10428 const char *import_prefix;
10429 std::vector<const char *> excludes;
10430
10431 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10432 if (import_attr == NULL)
10433 {
10434 complaint (_("Tag '%s' has no DW_AT_import"),
10435 dwarf_tag_name (die->tag));
10436 return;
10437 }
10438
10439 imported_cu = cu;
10440 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10441 imported_name = dwarf2_name (imported_die, imported_cu);
10442 if (imported_name == NULL)
10443 {
10444 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10445
10446 The import in the following code:
10447 namespace A
10448 {
10449 typedef int B;
10450 }
10451
10452 int main ()
10453 {
10454 using A::B;
10455 B b;
10456 return b;
10457 }
10458
10459 ...
10460 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10461 <52> DW_AT_decl_file : 1
10462 <53> DW_AT_decl_line : 6
10463 <54> DW_AT_import : <0x75>
10464 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10465 <59> DW_AT_name : B
10466 <5b> DW_AT_decl_file : 1
10467 <5c> DW_AT_decl_line : 2
10468 <5d> DW_AT_type : <0x6e>
10469 ...
10470 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10471 <76> DW_AT_byte_size : 4
10472 <77> DW_AT_encoding : 5 (signed)
10473
10474 imports the wrong die ( 0x75 instead of 0x58 ).
10475 This case will be ignored until the gcc bug is fixed. */
10476 return;
10477 }
10478
10479 /* Figure out the local name after import. */
10480 import_alias = dwarf2_name (die, cu);
10481
10482 /* Figure out where the statement is being imported to. */
10483 import_prefix = determine_prefix (die, cu);
10484
10485 /* Figure out what the scope of the imported die is and prepend it
10486 to the name of the imported die. */
10487 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10488
10489 if (imported_die->tag != DW_TAG_namespace
10490 && imported_die->tag != DW_TAG_module)
10491 {
10492 imported_declaration = imported_name;
10493 canonical_name = imported_name_prefix;
10494 }
10495 else if (strlen (imported_name_prefix) > 0)
10496 canonical_name = obconcat (&objfile->objfile_obstack,
10497 imported_name_prefix,
10498 (cu->language == language_d ? "." : "::"),
10499 imported_name, (char *) NULL);
10500 else
10501 canonical_name = imported_name;
10502
10503 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10504 for (child_die = die->child; child_die && child_die->tag;
10505 child_die = child_die->sibling)
10506 {
10507 /* DWARF-4: A Fortran use statement with a “rename list” may be
10508 represented by an imported module entry with an import attribute
10509 referring to the module and owned entries corresponding to those
10510 entities that are renamed as part of being imported. */
10511
10512 if (child_die->tag != DW_TAG_imported_declaration)
10513 {
10514 complaint (_("child DW_TAG_imported_declaration expected "
10515 "- DIE at %s [in module %s]"),
10516 sect_offset_str (child_die->sect_off),
10517 objfile_name (objfile));
10518 continue;
10519 }
10520
10521 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10522 if (import_attr == NULL)
10523 {
10524 complaint (_("Tag '%s' has no DW_AT_import"),
10525 dwarf_tag_name (child_die->tag));
10526 continue;
10527 }
10528
10529 imported_cu = cu;
10530 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10531 &imported_cu);
10532 imported_name = dwarf2_name (imported_die, imported_cu);
10533 if (imported_name == NULL)
10534 {
10535 complaint (_("child DW_TAG_imported_declaration has unknown "
10536 "imported name - DIE at %s [in module %s]"),
10537 sect_offset_str (child_die->sect_off),
10538 objfile_name (objfile));
10539 continue;
10540 }
10541
10542 excludes.push_back (imported_name);
10543
10544 process_die (child_die, cu);
10545 }
10546
10547 add_using_directive (using_directives (cu),
10548 import_prefix,
10549 canonical_name,
10550 import_alias,
10551 imported_declaration,
10552 excludes,
10553 0,
10554 &objfile->objfile_obstack);
10555 }
10556
10557 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10558 types, but gives them a size of zero. Starting with version 14,
10559 ICC is compatible with GCC. */
10560
10561 static bool
10562 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10563 {
10564 if (!cu->checked_producer)
10565 check_producer (cu);
10566
10567 return cu->producer_is_icc_lt_14;
10568 }
10569
10570 /* ICC generates a DW_AT_type for C void functions. This was observed on
10571 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10572 which says that void functions should not have a DW_AT_type. */
10573
10574 static bool
10575 producer_is_icc (struct dwarf2_cu *cu)
10576 {
10577 if (!cu->checked_producer)
10578 check_producer (cu);
10579
10580 return cu->producer_is_icc;
10581 }
10582
10583 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10584 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10585 this, it was first present in GCC release 4.3.0. */
10586
10587 static bool
10588 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10589 {
10590 if (!cu->checked_producer)
10591 check_producer (cu);
10592
10593 return cu->producer_is_gcc_lt_4_3;
10594 }
10595
10596 static file_and_directory
10597 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10598 {
10599 file_and_directory res;
10600
10601 /* Find the filename. Do not use dwarf2_name here, since the filename
10602 is not a source language identifier. */
10603 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10604 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10605
10606 if (res.comp_dir == NULL
10607 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10608 && IS_ABSOLUTE_PATH (res.name))
10609 {
10610 res.comp_dir_storage = ldirname (res.name);
10611 if (!res.comp_dir_storage.empty ())
10612 res.comp_dir = res.comp_dir_storage.c_str ();
10613 }
10614 if (res.comp_dir != NULL)
10615 {
10616 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10617 directory, get rid of it. */
10618 const char *cp = strchr (res.comp_dir, ':');
10619
10620 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10621 res.comp_dir = cp + 1;
10622 }
10623
10624 if (res.name == NULL)
10625 res.name = "<unknown>";
10626
10627 return res;
10628 }
10629
10630 /* Handle DW_AT_stmt_list for a compilation unit.
10631 DIE is the DW_TAG_compile_unit die for CU.
10632 COMP_DIR is the compilation directory. LOWPC is passed to
10633 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10634
10635 static void
10636 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10637 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10638 {
10639 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10640 struct attribute *attr;
10641 struct line_header line_header_local;
10642 hashval_t line_header_local_hash;
10643 void **slot;
10644 int decode_mapping;
10645
10646 gdb_assert (! cu->per_cu->is_debug_types);
10647
10648 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10649 if (attr == NULL || !attr->form_is_unsigned ())
10650 return;
10651
10652 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10653
10654 /* The line header hash table is only created if needed (it exists to
10655 prevent redundant reading of the line table for partial_units).
10656 If we're given a partial_unit, we'll need it. If we're given a
10657 compile_unit, then use the line header hash table if it's already
10658 created, but don't create one just yet. */
10659
10660 if (per_objfile->line_header_hash == NULL
10661 && die->tag == DW_TAG_partial_unit)
10662 {
10663 per_objfile->line_header_hash
10664 .reset (htab_create_alloc (127, line_header_hash_voidp,
10665 line_header_eq_voidp,
10666 free_line_header_voidp,
10667 xcalloc, xfree));
10668 }
10669
10670 line_header_local.sect_off = line_offset;
10671 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10672 line_header_local_hash = line_header_hash (&line_header_local);
10673 if (per_objfile->line_header_hash != NULL)
10674 {
10675 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10676 &line_header_local,
10677 line_header_local_hash, NO_INSERT);
10678
10679 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10680 is not present in *SLOT (since if there is something in *SLOT then
10681 it will be for a partial_unit). */
10682 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10683 {
10684 gdb_assert (*slot != NULL);
10685 cu->line_header = (struct line_header *) *slot;
10686 return;
10687 }
10688 }
10689
10690 /* dwarf_decode_line_header does not yet provide sufficient information.
10691 We always have to call also dwarf_decode_lines for it. */
10692 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10693 if (lh == NULL)
10694 return;
10695
10696 cu->line_header = lh.release ();
10697 cu->line_header_die_owner = die;
10698
10699 if (per_objfile->line_header_hash == NULL)
10700 slot = NULL;
10701 else
10702 {
10703 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10704 &line_header_local,
10705 line_header_local_hash, INSERT);
10706 gdb_assert (slot != NULL);
10707 }
10708 if (slot != NULL && *slot == NULL)
10709 {
10710 /* This newly decoded line number information unit will be owned
10711 by line_header_hash hash table. */
10712 *slot = cu->line_header;
10713 cu->line_header_die_owner = NULL;
10714 }
10715 else
10716 {
10717 /* We cannot free any current entry in (*slot) as that struct line_header
10718 may be already used by multiple CUs. Create only temporary decoded
10719 line_header for this CU - it may happen at most once for each line
10720 number information unit. And if we're not using line_header_hash
10721 then this is what we want as well. */
10722 gdb_assert (die->tag != DW_TAG_partial_unit);
10723 }
10724 decode_mapping = (die->tag != DW_TAG_partial_unit);
10725 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10726 decode_mapping);
10727
10728 }
10729
10730 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10731
10732 static void
10733 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10734 {
10735 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10736 struct objfile *objfile = per_objfile->objfile;
10737 struct gdbarch *gdbarch = objfile->arch ();
10738 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10739 CORE_ADDR highpc = ((CORE_ADDR) 0);
10740 struct attribute *attr;
10741 struct die_info *child_die;
10742 CORE_ADDR baseaddr;
10743
10744 prepare_one_comp_unit (cu, die, cu->language);
10745 baseaddr = objfile->text_section_offset ();
10746
10747 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10748
10749 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10750 from finish_block. */
10751 if (lowpc == ((CORE_ADDR) -1))
10752 lowpc = highpc;
10753 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10754
10755 file_and_directory fnd = find_file_and_directory (die, cu);
10756
10757 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10758 standardised yet. As a workaround for the language detection we fall
10759 back to the DW_AT_producer string. */
10760 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10761 cu->language = language_opencl;
10762
10763 /* Similar hack for Go. */
10764 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10765 set_cu_language (DW_LANG_Go, cu);
10766
10767 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10768
10769 /* Decode line number information if present. We do this before
10770 processing child DIEs, so that the line header table is available
10771 for DW_AT_decl_file. */
10772 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10773
10774 /* Process all dies in compilation unit. */
10775 if (die->child != NULL)
10776 {
10777 child_die = die->child;
10778 while (child_die && child_die->tag)
10779 {
10780 process_die (child_die, cu);
10781 child_die = child_die->sibling;
10782 }
10783 }
10784
10785 /* Decode macro information, if present. Dwarf 2 macro information
10786 refers to information in the line number info statement program
10787 header, so we can only read it if we've read the header
10788 successfully. */
10789 attr = dwarf2_attr (die, DW_AT_macros, cu);
10790 if (attr == NULL)
10791 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10792 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10793 {
10794 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10795 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10796
10797 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10798 }
10799 else
10800 {
10801 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10802 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10803 {
10804 unsigned int macro_offset = attr->as_unsigned ();
10805
10806 dwarf_decode_macros (cu, macro_offset, 0);
10807 }
10808 }
10809 }
10810
10811 void
10812 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10813 {
10814 struct type_unit_group *tu_group;
10815 int first_time;
10816 struct attribute *attr;
10817 unsigned int i;
10818 struct signatured_type *sig_type;
10819
10820 gdb_assert (per_cu->is_debug_types);
10821 sig_type = (struct signatured_type *) per_cu;
10822
10823 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10824
10825 /* If we're using .gdb_index (includes -readnow) then
10826 per_cu->type_unit_group may not have been set up yet. */
10827 if (sig_type->type_unit_group == NULL)
10828 sig_type->type_unit_group = get_type_unit_group (this, attr);
10829 tu_group = sig_type->type_unit_group;
10830
10831 /* If we've already processed this stmt_list there's no real need to
10832 do it again, we could fake it and just recreate the part we need
10833 (file name,index -> symtab mapping). If data shows this optimization
10834 is useful we can do it then. */
10835 type_unit_group_unshareable *tug_unshare
10836 = per_objfile->get_type_unit_group_unshareable (tu_group);
10837 first_time = tug_unshare->compunit_symtab == NULL;
10838
10839 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10840 debug info. */
10841 line_header_up lh;
10842 if (attr != NULL && attr->form_is_unsigned ())
10843 {
10844 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10845 lh = dwarf_decode_line_header (line_offset, this);
10846 }
10847 if (lh == NULL)
10848 {
10849 if (first_time)
10850 start_symtab ("", NULL, 0);
10851 else
10852 {
10853 gdb_assert (tug_unshare->symtabs == NULL);
10854 gdb_assert (m_builder == nullptr);
10855 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10856 m_builder.reset (new struct buildsym_compunit
10857 (COMPUNIT_OBJFILE (cust), "",
10858 COMPUNIT_DIRNAME (cust),
10859 compunit_language (cust),
10860 0, cust));
10861 list_in_scope = get_builder ()->get_file_symbols ();
10862 }
10863 return;
10864 }
10865
10866 line_header = lh.release ();
10867 line_header_die_owner = die;
10868
10869 if (first_time)
10870 {
10871 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10872
10873 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10874 still initializing it, and our caller (a few levels up)
10875 process_full_type_unit still needs to know if this is the first
10876 time. */
10877
10878 tug_unshare->symtabs
10879 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10880 struct symtab *, line_header->file_names_size ());
10881
10882 auto &file_names = line_header->file_names ();
10883 for (i = 0; i < file_names.size (); ++i)
10884 {
10885 file_entry &fe = file_names[i];
10886 dwarf2_start_subfile (this, fe.name,
10887 fe.include_dir (line_header));
10888 buildsym_compunit *b = get_builder ();
10889 if (b->get_current_subfile ()->symtab == NULL)
10890 {
10891 /* NOTE: start_subfile will recognize when it's been
10892 passed a file it has already seen. So we can't
10893 assume there's a simple mapping from
10894 cu->line_header->file_names to subfiles, plus
10895 cu->line_header->file_names may contain dups. */
10896 b->get_current_subfile ()->symtab
10897 = allocate_symtab (cust, b->get_current_subfile ()->name);
10898 }
10899
10900 fe.symtab = b->get_current_subfile ()->symtab;
10901 tug_unshare->symtabs[i] = fe.symtab;
10902 }
10903 }
10904 else
10905 {
10906 gdb_assert (m_builder == nullptr);
10907 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10908 m_builder.reset (new struct buildsym_compunit
10909 (COMPUNIT_OBJFILE (cust), "",
10910 COMPUNIT_DIRNAME (cust),
10911 compunit_language (cust),
10912 0, cust));
10913 list_in_scope = get_builder ()->get_file_symbols ();
10914
10915 auto &file_names = line_header->file_names ();
10916 for (i = 0; i < file_names.size (); ++i)
10917 {
10918 file_entry &fe = file_names[i];
10919 fe.symtab = tug_unshare->symtabs[i];
10920 }
10921 }
10922
10923 /* The main symtab is allocated last. Type units don't have DW_AT_name
10924 so they don't have a "real" (so to speak) symtab anyway.
10925 There is later code that will assign the main symtab to all symbols
10926 that don't have one. We need to handle the case of a symbol with a
10927 missing symtab (DW_AT_decl_file) anyway. */
10928 }
10929
10930 /* Process DW_TAG_type_unit.
10931 For TUs we want to skip the first top level sibling if it's not the
10932 actual type being defined by this TU. In this case the first top
10933 level sibling is there to provide context only. */
10934
10935 static void
10936 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10937 {
10938 struct die_info *child_die;
10939
10940 prepare_one_comp_unit (cu, die, language_minimal);
10941
10942 /* Initialize (or reinitialize) the machinery for building symtabs.
10943 We do this before processing child DIEs, so that the line header table
10944 is available for DW_AT_decl_file. */
10945 cu->setup_type_unit_groups (die);
10946
10947 if (die->child != NULL)
10948 {
10949 child_die = die->child;
10950 while (child_die && child_die->tag)
10951 {
10952 process_die (child_die, cu);
10953 child_die = child_die->sibling;
10954 }
10955 }
10956 }
10957 \f
10958 /* DWO/DWP files.
10959
10960 http://gcc.gnu.org/wiki/DebugFission
10961 http://gcc.gnu.org/wiki/DebugFissionDWP
10962
10963 To simplify handling of both DWO files ("object" files with the DWARF info)
10964 and DWP files (a file with the DWOs packaged up into one file), we treat
10965 DWP files as having a collection of virtual DWO files. */
10966
10967 static hashval_t
10968 hash_dwo_file (const void *item)
10969 {
10970 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10971 hashval_t hash;
10972
10973 hash = htab_hash_string (dwo_file->dwo_name);
10974 if (dwo_file->comp_dir != NULL)
10975 hash += htab_hash_string (dwo_file->comp_dir);
10976 return hash;
10977 }
10978
10979 static int
10980 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10981 {
10982 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10983 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10984
10985 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10986 return 0;
10987 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10988 return lhs->comp_dir == rhs->comp_dir;
10989 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10990 }
10991
10992 /* Allocate a hash table for DWO files. */
10993
10994 static htab_up
10995 allocate_dwo_file_hash_table ()
10996 {
10997 auto delete_dwo_file = [] (void *item)
10998 {
10999 struct dwo_file *dwo_file = (struct dwo_file *) item;
11000
11001 delete dwo_file;
11002 };
11003
11004 return htab_up (htab_create_alloc (41,
11005 hash_dwo_file,
11006 eq_dwo_file,
11007 delete_dwo_file,
11008 xcalloc, xfree));
11009 }
11010
11011 /* Lookup DWO file DWO_NAME. */
11012
11013 static void **
11014 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11015 const char *dwo_name,
11016 const char *comp_dir)
11017 {
11018 struct dwo_file find_entry;
11019 void **slot;
11020
11021 if (per_objfile->per_bfd->dwo_files == NULL)
11022 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11023
11024 find_entry.dwo_name = dwo_name;
11025 find_entry.comp_dir = comp_dir;
11026 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11027 INSERT);
11028
11029 return slot;
11030 }
11031
11032 static hashval_t
11033 hash_dwo_unit (const void *item)
11034 {
11035 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11036
11037 /* This drops the top 32 bits of the id, but is ok for a hash. */
11038 return dwo_unit->signature;
11039 }
11040
11041 static int
11042 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11043 {
11044 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11045 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11046
11047 /* The signature is assumed to be unique within the DWO file.
11048 So while object file CU dwo_id's always have the value zero,
11049 that's OK, assuming each object file DWO file has only one CU,
11050 and that's the rule for now. */
11051 return lhs->signature == rhs->signature;
11052 }
11053
11054 /* Allocate a hash table for DWO CUs,TUs.
11055 There is one of these tables for each of CUs,TUs for each DWO file. */
11056
11057 static htab_up
11058 allocate_dwo_unit_table ()
11059 {
11060 /* Start out with a pretty small number.
11061 Generally DWO files contain only one CU and maybe some TUs. */
11062 return htab_up (htab_create_alloc (3,
11063 hash_dwo_unit,
11064 eq_dwo_unit,
11065 NULL, xcalloc, xfree));
11066 }
11067
11068 /* die_reader_func for create_dwo_cu. */
11069
11070 static void
11071 create_dwo_cu_reader (const struct die_reader_specs *reader,
11072 const gdb_byte *info_ptr,
11073 struct die_info *comp_unit_die,
11074 struct dwo_file *dwo_file,
11075 struct dwo_unit *dwo_unit)
11076 {
11077 struct dwarf2_cu *cu = reader->cu;
11078 sect_offset sect_off = cu->per_cu->sect_off;
11079 struct dwarf2_section_info *section = cu->per_cu->section;
11080
11081 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11082 if (!signature.has_value ())
11083 {
11084 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11085 " its dwo_id [in module %s]"),
11086 sect_offset_str (sect_off), dwo_file->dwo_name);
11087 return;
11088 }
11089
11090 dwo_unit->dwo_file = dwo_file;
11091 dwo_unit->signature = *signature;
11092 dwo_unit->section = section;
11093 dwo_unit->sect_off = sect_off;
11094 dwo_unit->length = cu->per_cu->length;
11095
11096 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11097 sect_offset_str (sect_off),
11098 hex_string (dwo_unit->signature));
11099 }
11100
11101 /* Create the dwo_units for the CUs in a DWO_FILE.
11102 Note: This function processes DWO files only, not DWP files. */
11103
11104 static void
11105 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11106 dwarf2_cu *cu, struct dwo_file &dwo_file,
11107 dwarf2_section_info &section, htab_up &cus_htab)
11108 {
11109 struct objfile *objfile = per_objfile->objfile;
11110 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11111 const gdb_byte *info_ptr, *end_ptr;
11112
11113 section.read (objfile);
11114 info_ptr = section.buffer;
11115
11116 if (info_ptr == NULL)
11117 return;
11118
11119 dwarf_read_debug_printf ("Reading %s for %s:",
11120 section.get_name (),
11121 section.get_file_name ());
11122
11123 end_ptr = info_ptr + section.size;
11124 while (info_ptr < end_ptr)
11125 {
11126 struct dwarf2_per_cu_data per_cu;
11127 struct dwo_unit read_unit {};
11128 struct dwo_unit *dwo_unit;
11129 void **slot;
11130 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11131
11132 per_cu.per_bfd = per_bfd;
11133 per_cu.is_debug_types = 0;
11134 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11135 per_cu.section = &section;
11136
11137 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11138 if (!reader.dummy_p)
11139 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11140 &dwo_file, &read_unit);
11141 info_ptr += per_cu.length;
11142
11143 // If the unit could not be parsed, skip it.
11144 if (read_unit.dwo_file == NULL)
11145 continue;
11146
11147 if (cus_htab == NULL)
11148 cus_htab = allocate_dwo_unit_table ();
11149
11150 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11151 struct dwo_unit);
11152 *dwo_unit = read_unit;
11153 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11154 gdb_assert (slot != NULL);
11155 if (*slot != NULL)
11156 {
11157 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11158 sect_offset dup_sect_off = dup_cu->sect_off;
11159
11160 complaint (_("debug cu entry at offset %s is duplicate to"
11161 " the entry at offset %s, signature %s"),
11162 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11163 hex_string (dwo_unit->signature));
11164 }
11165 *slot = (void *)dwo_unit;
11166 }
11167 }
11168
11169 /* DWP file .debug_{cu,tu}_index section format:
11170 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11171 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11172
11173 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11174 officially standard DWP format was published with DWARF v5 and is called
11175 Version 5. There are no versions 3 or 4.
11176
11177 DWP Version 1:
11178
11179 Both index sections have the same format, and serve to map a 64-bit
11180 signature to a set of section numbers. Each section begins with a header,
11181 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11182 indexes, and a pool of 32-bit section numbers. The index sections will be
11183 aligned at 8-byte boundaries in the file.
11184
11185 The index section header consists of:
11186
11187 V, 32 bit version number
11188 -, 32 bits unused
11189 N, 32 bit number of compilation units or type units in the index
11190 M, 32 bit number of slots in the hash table
11191
11192 Numbers are recorded using the byte order of the application binary.
11193
11194 The hash table begins at offset 16 in the section, and consists of an array
11195 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11196 order of the application binary). Unused slots in the hash table are 0.
11197 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11198
11199 The parallel table begins immediately after the hash table
11200 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11201 array of 32-bit indexes (using the byte order of the application binary),
11202 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11203 table contains a 32-bit index into the pool of section numbers. For unused
11204 hash table slots, the corresponding entry in the parallel table will be 0.
11205
11206 The pool of section numbers begins immediately following the hash table
11207 (at offset 16 + 12 * M from the beginning of the section). The pool of
11208 section numbers consists of an array of 32-bit words (using the byte order
11209 of the application binary). Each item in the array is indexed starting
11210 from 0. The hash table entry provides the index of the first section
11211 number in the set. Additional section numbers in the set follow, and the
11212 set is terminated by a 0 entry (section number 0 is not used in ELF).
11213
11214 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11215 section must be the first entry in the set, and the .debug_abbrev.dwo must
11216 be the second entry. Other members of the set may follow in any order.
11217
11218 ---
11219
11220 DWP Versions 2 and 5:
11221
11222 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11223 and the entries in the index tables are now offsets into these sections.
11224 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11225 section.
11226
11227 Index Section Contents:
11228 Header
11229 Hash Table of Signatures dwp_hash_table.hash_table
11230 Parallel Table of Indices dwp_hash_table.unit_table
11231 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11232 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11233
11234 The index section header consists of:
11235
11236 V, 32 bit version number
11237 L, 32 bit number of columns in the table of section offsets
11238 N, 32 bit number of compilation units or type units in the index
11239 M, 32 bit number of slots in the hash table
11240
11241 Numbers are recorded using the byte order of the application binary.
11242
11243 The hash table has the same format as version 1.
11244 The parallel table of indices has the same format as version 1,
11245 except that the entries are origin-1 indices into the table of sections
11246 offsets and the table of section sizes.
11247
11248 The table of offsets begins immediately following the parallel table
11249 (at offset 16 + 12 * M from the beginning of the section). The table is
11250 a two-dimensional array of 32-bit words (using the byte order of the
11251 application binary), with L columns and N+1 rows, in row-major order.
11252 Each row in the array is indexed starting from 0. The first row provides
11253 a key to the remaining rows: each column in this row provides an identifier
11254 for a debug section, and the offsets in the same column of subsequent rows
11255 refer to that section. The section identifiers for Version 2 are:
11256
11257 DW_SECT_INFO 1 .debug_info.dwo
11258 DW_SECT_TYPES 2 .debug_types.dwo
11259 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11260 DW_SECT_LINE 4 .debug_line.dwo
11261 DW_SECT_LOC 5 .debug_loc.dwo
11262 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11263 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11264 DW_SECT_MACRO 8 .debug_macro.dwo
11265
11266 The section identifiers for Version 5 are:
11267
11268 DW_SECT_INFO_V5 1 .debug_info.dwo
11269 DW_SECT_RESERVED_V5 2 --
11270 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11271 DW_SECT_LINE_V5 4 .debug_line.dwo
11272 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11273 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11274 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11275 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11276
11277 The offsets provided by the CU and TU index sections are the base offsets
11278 for the contributions made by each CU or TU to the corresponding section
11279 in the package file. Each CU and TU header contains an abbrev_offset
11280 field, used to find the abbreviations table for that CU or TU within the
11281 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11282 be interpreted as relative to the base offset given in the index section.
11283 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11284 should be interpreted as relative to the base offset for .debug_line.dwo,
11285 and offsets into other debug sections obtained from DWARF attributes should
11286 also be interpreted as relative to the corresponding base offset.
11287
11288 The table of sizes begins immediately following the table of offsets.
11289 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11290 with L columns and N rows, in row-major order. Each row in the array is
11291 indexed starting from 1 (row 0 is shared by the two tables).
11292
11293 ---
11294
11295 Hash table lookup is handled the same in version 1 and 2:
11296
11297 We assume that N and M will not exceed 2^32 - 1.
11298 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11299
11300 Given a 64-bit compilation unit signature or a type signature S, an entry
11301 in the hash table is located as follows:
11302
11303 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11304 the low-order k bits all set to 1.
11305
11306 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11307
11308 3) If the hash table entry at index H matches the signature, use that
11309 entry. If the hash table entry at index H is unused (all zeroes),
11310 terminate the search: the signature is not present in the table.
11311
11312 4) Let H = (H + H') modulo M. Repeat at Step 3.
11313
11314 Because M > N and H' and M are relatively prime, the search is guaranteed
11315 to stop at an unused slot or find the match. */
11316
11317 /* Create a hash table to map DWO IDs to their CU/TU entry in
11318 .debug_{info,types}.dwo in DWP_FILE.
11319 Returns NULL if there isn't one.
11320 Note: This function processes DWP files only, not DWO files. */
11321
11322 static struct dwp_hash_table *
11323 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11324 struct dwp_file *dwp_file, int is_debug_types)
11325 {
11326 struct objfile *objfile = per_objfile->objfile;
11327 bfd *dbfd = dwp_file->dbfd.get ();
11328 const gdb_byte *index_ptr, *index_end;
11329 struct dwarf2_section_info *index;
11330 uint32_t version, nr_columns, nr_units, nr_slots;
11331 struct dwp_hash_table *htab;
11332
11333 if (is_debug_types)
11334 index = &dwp_file->sections.tu_index;
11335 else
11336 index = &dwp_file->sections.cu_index;
11337
11338 if (index->empty ())
11339 return NULL;
11340 index->read (objfile);
11341
11342 index_ptr = index->buffer;
11343 index_end = index_ptr + index->size;
11344
11345 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11346 For now it's safe to just read 4 bytes (particularly as it's difficult to
11347 tell if you're dealing with Version 5 before you've read the version). */
11348 version = read_4_bytes (dbfd, index_ptr);
11349 index_ptr += 4;
11350 if (version == 2 || version == 5)
11351 nr_columns = read_4_bytes (dbfd, index_ptr);
11352 else
11353 nr_columns = 0;
11354 index_ptr += 4;
11355 nr_units = read_4_bytes (dbfd, index_ptr);
11356 index_ptr += 4;
11357 nr_slots = read_4_bytes (dbfd, index_ptr);
11358 index_ptr += 4;
11359
11360 if (version != 1 && version != 2 && version != 5)
11361 {
11362 error (_("Dwarf Error: unsupported DWP file version (%s)"
11363 " [in module %s]"),
11364 pulongest (version), dwp_file->name);
11365 }
11366 if (nr_slots != (nr_slots & -nr_slots))
11367 {
11368 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11369 " is not power of 2 [in module %s]"),
11370 pulongest (nr_slots), dwp_file->name);
11371 }
11372
11373 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11374 htab->version = version;
11375 htab->nr_columns = nr_columns;
11376 htab->nr_units = nr_units;
11377 htab->nr_slots = nr_slots;
11378 htab->hash_table = index_ptr;
11379 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11380
11381 /* Exit early if the table is empty. */
11382 if (nr_slots == 0 || nr_units == 0
11383 || (version == 2 && nr_columns == 0)
11384 || (version == 5 && nr_columns == 0))
11385 {
11386 /* All must be zero. */
11387 if (nr_slots != 0 || nr_units != 0
11388 || (version == 2 && nr_columns != 0)
11389 || (version == 5 && nr_columns != 0))
11390 {
11391 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11392 " all zero [in modules %s]"),
11393 dwp_file->name);
11394 }
11395 return htab;
11396 }
11397
11398 if (version == 1)
11399 {
11400 htab->section_pool.v1.indices =
11401 htab->unit_table + sizeof (uint32_t) * nr_slots;
11402 /* It's harder to decide whether the section is too small in v1.
11403 V1 is deprecated anyway so we punt. */
11404 }
11405 else if (version == 2)
11406 {
11407 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11408 int *ids = htab->section_pool.v2.section_ids;
11409 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11410 /* Reverse map for error checking. */
11411 int ids_seen[DW_SECT_MAX + 1];
11412 int i;
11413
11414 if (nr_columns < 2)
11415 {
11416 error (_("Dwarf Error: bad DWP hash table, too few columns"
11417 " in section table [in module %s]"),
11418 dwp_file->name);
11419 }
11420 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11421 {
11422 error (_("Dwarf Error: bad DWP hash table, too many columns"
11423 " in section table [in module %s]"),
11424 dwp_file->name);
11425 }
11426 memset (ids, 255, sizeof_ids);
11427 memset (ids_seen, 255, sizeof (ids_seen));
11428 for (i = 0; i < nr_columns; ++i)
11429 {
11430 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11431
11432 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11433 {
11434 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11435 " in section table [in module %s]"),
11436 id, dwp_file->name);
11437 }
11438 if (ids_seen[id] != -1)
11439 {
11440 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11441 " id %d in section table [in module %s]"),
11442 id, dwp_file->name);
11443 }
11444 ids_seen[id] = i;
11445 ids[i] = id;
11446 }
11447 /* Must have exactly one info or types section. */
11448 if (((ids_seen[DW_SECT_INFO] != -1)
11449 + (ids_seen[DW_SECT_TYPES] != -1))
11450 != 1)
11451 {
11452 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11453 " DWO info/types section [in module %s]"),
11454 dwp_file->name);
11455 }
11456 /* Must have an abbrev section. */
11457 if (ids_seen[DW_SECT_ABBREV] == -1)
11458 {
11459 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11460 " section [in module %s]"),
11461 dwp_file->name);
11462 }
11463 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11464 htab->section_pool.v2.sizes =
11465 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11466 * nr_units * nr_columns);
11467 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11468 * nr_units * nr_columns))
11469 > index_end)
11470 {
11471 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11472 " [in module %s]"),
11473 dwp_file->name);
11474 }
11475 }
11476 else /* version == 5 */
11477 {
11478 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11479 int *ids = htab->section_pool.v5.section_ids;
11480 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11481 /* Reverse map for error checking. */
11482 int ids_seen[DW_SECT_MAX_V5 + 1];
11483
11484 if (nr_columns < 2)
11485 {
11486 error (_("Dwarf Error: bad DWP hash table, too few columns"
11487 " in section table [in module %s]"),
11488 dwp_file->name);
11489 }
11490 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11491 {
11492 error (_("Dwarf Error: bad DWP hash table, too many columns"
11493 " in section table [in module %s]"),
11494 dwp_file->name);
11495 }
11496 memset (ids, 255, sizeof_ids);
11497 memset (ids_seen, 255, sizeof (ids_seen));
11498 for (int i = 0; i < nr_columns; ++i)
11499 {
11500 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11501
11502 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11503 {
11504 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11505 " in section table [in module %s]"),
11506 id, dwp_file->name);
11507 }
11508 if (ids_seen[id] != -1)
11509 {
11510 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11511 " id %d in section table [in module %s]"),
11512 id, dwp_file->name);
11513 }
11514 ids_seen[id] = i;
11515 ids[i] = id;
11516 }
11517 /* Must have seen an info section. */
11518 if (ids_seen[DW_SECT_INFO_V5] == -1)
11519 {
11520 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11521 " DWO info/types section [in module %s]"),
11522 dwp_file->name);
11523 }
11524 /* Must have an abbrev section. */
11525 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11526 {
11527 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11528 " section [in module %s]"),
11529 dwp_file->name);
11530 }
11531 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11532 htab->section_pool.v5.sizes
11533 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11534 * nr_units * nr_columns);
11535 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11536 * nr_units * nr_columns))
11537 > index_end)
11538 {
11539 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11540 " [in module %s]"),
11541 dwp_file->name);
11542 }
11543 }
11544
11545 return htab;
11546 }
11547
11548 /* Update SECTIONS with the data from SECTP.
11549
11550 This function is like the other "locate" section routines, but in
11551 this context the sections to read comes from the DWP V1 hash table,
11552 not the full ELF section table.
11553
11554 The result is non-zero for success, or zero if an error was found. */
11555
11556 static int
11557 locate_v1_virtual_dwo_sections (asection *sectp,
11558 struct virtual_v1_dwo_sections *sections)
11559 {
11560 const struct dwop_section_names *names = &dwop_section_names;
11561
11562 if (names->abbrev_dwo.matches (sectp->name))
11563 {
11564 /* There can be only one. */
11565 if (sections->abbrev.s.section != NULL)
11566 return 0;
11567 sections->abbrev.s.section = sectp;
11568 sections->abbrev.size = bfd_section_size (sectp);
11569 }
11570 else if (names->info_dwo.matches (sectp->name)
11571 || names->types_dwo.matches (sectp->name))
11572 {
11573 /* There can be only one. */
11574 if (sections->info_or_types.s.section != NULL)
11575 return 0;
11576 sections->info_or_types.s.section = sectp;
11577 sections->info_or_types.size = bfd_section_size (sectp);
11578 }
11579 else if (names->line_dwo.matches (sectp->name))
11580 {
11581 /* There can be only one. */
11582 if (sections->line.s.section != NULL)
11583 return 0;
11584 sections->line.s.section = sectp;
11585 sections->line.size = bfd_section_size (sectp);
11586 }
11587 else if (names->loc_dwo.matches (sectp->name))
11588 {
11589 /* There can be only one. */
11590 if (sections->loc.s.section != NULL)
11591 return 0;
11592 sections->loc.s.section = sectp;
11593 sections->loc.size = bfd_section_size (sectp);
11594 }
11595 else if (names->macinfo_dwo.matches (sectp->name))
11596 {
11597 /* There can be only one. */
11598 if (sections->macinfo.s.section != NULL)
11599 return 0;
11600 sections->macinfo.s.section = sectp;
11601 sections->macinfo.size = bfd_section_size (sectp);
11602 }
11603 else if (names->macro_dwo.matches (sectp->name))
11604 {
11605 /* There can be only one. */
11606 if (sections->macro.s.section != NULL)
11607 return 0;
11608 sections->macro.s.section = sectp;
11609 sections->macro.size = bfd_section_size (sectp);
11610 }
11611 else if (names->str_offsets_dwo.matches (sectp->name))
11612 {
11613 /* There can be only one. */
11614 if (sections->str_offsets.s.section != NULL)
11615 return 0;
11616 sections->str_offsets.s.section = sectp;
11617 sections->str_offsets.size = bfd_section_size (sectp);
11618 }
11619 else
11620 {
11621 /* No other kind of section is valid. */
11622 return 0;
11623 }
11624
11625 return 1;
11626 }
11627
11628 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11629 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11630 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11631 This is for DWP version 1 files. */
11632
11633 static struct dwo_unit *
11634 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11635 struct dwp_file *dwp_file,
11636 uint32_t unit_index,
11637 const char *comp_dir,
11638 ULONGEST signature, int is_debug_types)
11639 {
11640 const struct dwp_hash_table *dwp_htab =
11641 is_debug_types ? dwp_file->tus : dwp_file->cus;
11642 bfd *dbfd = dwp_file->dbfd.get ();
11643 const char *kind = is_debug_types ? "TU" : "CU";
11644 struct dwo_file *dwo_file;
11645 struct dwo_unit *dwo_unit;
11646 struct virtual_v1_dwo_sections sections;
11647 void **dwo_file_slot;
11648 int i;
11649
11650 gdb_assert (dwp_file->version == 1);
11651
11652 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11653 kind, pulongest (unit_index), hex_string (signature),
11654 dwp_file->name);
11655
11656 /* Fetch the sections of this DWO unit.
11657 Put a limit on the number of sections we look for so that bad data
11658 doesn't cause us to loop forever. */
11659
11660 #define MAX_NR_V1_DWO_SECTIONS \
11661 (1 /* .debug_info or .debug_types */ \
11662 + 1 /* .debug_abbrev */ \
11663 + 1 /* .debug_line */ \
11664 + 1 /* .debug_loc */ \
11665 + 1 /* .debug_str_offsets */ \
11666 + 1 /* .debug_macro or .debug_macinfo */ \
11667 + 1 /* trailing zero */)
11668
11669 memset (&sections, 0, sizeof (sections));
11670
11671 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11672 {
11673 asection *sectp;
11674 uint32_t section_nr =
11675 read_4_bytes (dbfd,
11676 dwp_htab->section_pool.v1.indices
11677 + (unit_index + i) * sizeof (uint32_t));
11678
11679 if (section_nr == 0)
11680 break;
11681 if (section_nr >= dwp_file->num_sections)
11682 {
11683 error (_("Dwarf Error: bad DWP hash table, section number too large"
11684 " [in module %s]"),
11685 dwp_file->name);
11686 }
11687
11688 sectp = dwp_file->elf_sections[section_nr];
11689 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11690 {
11691 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11692 " [in module %s]"),
11693 dwp_file->name);
11694 }
11695 }
11696
11697 if (i < 2
11698 || sections.info_or_types.empty ()
11699 || sections.abbrev.empty ())
11700 {
11701 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11702 " [in module %s]"),
11703 dwp_file->name);
11704 }
11705 if (i == MAX_NR_V1_DWO_SECTIONS)
11706 {
11707 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11708 " [in module %s]"),
11709 dwp_file->name);
11710 }
11711
11712 /* It's easier for the rest of the code if we fake a struct dwo_file and
11713 have dwo_unit "live" in that. At least for now.
11714
11715 The DWP file can be made up of a random collection of CUs and TUs.
11716 However, for each CU + set of TUs that came from the same original DWO
11717 file, we can combine them back into a virtual DWO file to save space
11718 (fewer struct dwo_file objects to allocate). Remember that for really
11719 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11720
11721 std::string virtual_dwo_name =
11722 string_printf ("virtual-dwo/%d-%d-%d-%d",
11723 sections.abbrev.get_id (),
11724 sections.line.get_id (),
11725 sections.loc.get_id (),
11726 sections.str_offsets.get_id ());
11727 /* Can we use an existing virtual DWO file? */
11728 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11729 comp_dir);
11730 /* Create one if necessary. */
11731 if (*dwo_file_slot == NULL)
11732 {
11733 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11734 virtual_dwo_name.c_str ());
11735
11736 dwo_file = new struct dwo_file;
11737 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11738 dwo_file->comp_dir = comp_dir;
11739 dwo_file->sections.abbrev = sections.abbrev;
11740 dwo_file->sections.line = sections.line;
11741 dwo_file->sections.loc = sections.loc;
11742 dwo_file->sections.macinfo = sections.macinfo;
11743 dwo_file->sections.macro = sections.macro;
11744 dwo_file->sections.str_offsets = sections.str_offsets;
11745 /* The "str" section is global to the entire DWP file. */
11746 dwo_file->sections.str = dwp_file->sections.str;
11747 /* The info or types section is assigned below to dwo_unit,
11748 there's no need to record it in dwo_file.
11749 Also, we can't simply record type sections in dwo_file because
11750 we record a pointer into the vector in dwo_unit. As we collect more
11751 types we'll grow the vector and eventually have to reallocate space
11752 for it, invalidating all copies of pointers into the previous
11753 contents. */
11754 *dwo_file_slot = dwo_file;
11755 }
11756 else
11757 {
11758 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11759 virtual_dwo_name.c_str ());
11760
11761 dwo_file = (struct dwo_file *) *dwo_file_slot;
11762 }
11763
11764 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11765 dwo_unit->dwo_file = dwo_file;
11766 dwo_unit->signature = signature;
11767 dwo_unit->section =
11768 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11769 *dwo_unit->section = sections.info_or_types;
11770 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11771
11772 return dwo_unit;
11773 }
11774
11775 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11776 simplify them. Given a pointer to the containing section SECTION, and
11777 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11778 virtual section of just that piece. */
11779
11780 static struct dwarf2_section_info
11781 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11782 struct dwarf2_section_info *section,
11783 bfd_size_type offset, bfd_size_type size)
11784 {
11785 struct dwarf2_section_info result;
11786 asection *sectp;
11787
11788 gdb_assert (section != NULL);
11789 gdb_assert (!section->is_virtual);
11790
11791 memset (&result, 0, sizeof (result));
11792 result.s.containing_section = section;
11793 result.is_virtual = true;
11794
11795 if (size == 0)
11796 return result;
11797
11798 sectp = section->get_bfd_section ();
11799
11800 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11801 bounds of the real section. This is a pretty-rare event, so just
11802 flag an error (easier) instead of a warning and trying to cope. */
11803 if (sectp == NULL
11804 || offset + size > bfd_section_size (sectp))
11805 {
11806 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11807 " in section %s [in module %s]"),
11808 sectp ? bfd_section_name (sectp) : "<unknown>",
11809 objfile_name (per_objfile->objfile));
11810 }
11811
11812 result.virtual_offset = offset;
11813 result.size = size;
11814 return result;
11815 }
11816
11817 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11818 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11819 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11820 This is for DWP version 2 files. */
11821
11822 static struct dwo_unit *
11823 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11824 struct dwp_file *dwp_file,
11825 uint32_t unit_index,
11826 const char *comp_dir,
11827 ULONGEST signature, int is_debug_types)
11828 {
11829 const struct dwp_hash_table *dwp_htab =
11830 is_debug_types ? dwp_file->tus : dwp_file->cus;
11831 bfd *dbfd = dwp_file->dbfd.get ();
11832 const char *kind = is_debug_types ? "TU" : "CU";
11833 struct dwo_file *dwo_file;
11834 struct dwo_unit *dwo_unit;
11835 struct virtual_v2_or_v5_dwo_sections sections;
11836 void **dwo_file_slot;
11837 int i;
11838
11839 gdb_assert (dwp_file->version == 2);
11840
11841 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11842 kind, pulongest (unit_index), hex_string (signature),
11843 dwp_file->name);
11844
11845 /* Fetch the section offsets of this DWO unit. */
11846
11847 memset (&sections, 0, sizeof (sections));
11848
11849 for (i = 0; i < dwp_htab->nr_columns; ++i)
11850 {
11851 uint32_t offset = read_4_bytes (dbfd,
11852 dwp_htab->section_pool.v2.offsets
11853 + (((unit_index - 1) * dwp_htab->nr_columns
11854 + i)
11855 * sizeof (uint32_t)));
11856 uint32_t size = read_4_bytes (dbfd,
11857 dwp_htab->section_pool.v2.sizes
11858 + (((unit_index - 1) * dwp_htab->nr_columns
11859 + i)
11860 * sizeof (uint32_t)));
11861
11862 switch (dwp_htab->section_pool.v2.section_ids[i])
11863 {
11864 case DW_SECT_INFO:
11865 case DW_SECT_TYPES:
11866 sections.info_or_types_offset = offset;
11867 sections.info_or_types_size = size;
11868 break;
11869 case DW_SECT_ABBREV:
11870 sections.abbrev_offset = offset;
11871 sections.abbrev_size = size;
11872 break;
11873 case DW_SECT_LINE:
11874 sections.line_offset = offset;
11875 sections.line_size = size;
11876 break;
11877 case DW_SECT_LOC:
11878 sections.loc_offset = offset;
11879 sections.loc_size = size;
11880 break;
11881 case DW_SECT_STR_OFFSETS:
11882 sections.str_offsets_offset = offset;
11883 sections.str_offsets_size = size;
11884 break;
11885 case DW_SECT_MACINFO:
11886 sections.macinfo_offset = offset;
11887 sections.macinfo_size = size;
11888 break;
11889 case DW_SECT_MACRO:
11890 sections.macro_offset = offset;
11891 sections.macro_size = size;
11892 break;
11893 }
11894 }
11895
11896 /* It's easier for the rest of the code if we fake a struct dwo_file and
11897 have dwo_unit "live" in that. At least for now.
11898
11899 The DWP file can be made up of a random collection of CUs and TUs.
11900 However, for each CU + set of TUs that came from the same original DWO
11901 file, we can combine them back into a virtual DWO file to save space
11902 (fewer struct dwo_file objects to allocate). Remember that for really
11903 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11904
11905 std::string virtual_dwo_name =
11906 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11907 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11908 (long) (sections.line_size ? sections.line_offset : 0),
11909 (long) (sections.loc_size ? sections.loc_offset : 0),
11910 (long) (sections.str_offsets_size
11911 ? sections.str_offsets_offset : 0));
11912 /* Can we use an existing virtual DWO file? */
11913 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11914 comp_dir);
11915 /* Create one if necessary. */
11916 if (*dwo_file_slot == NULL)
11917 {
11918 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11919 virtual_dwo_name.c_str ());
11920
11921 dwo_file = new struct dwo_file;
11922 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11923 dwo_file->comp_dir = comp_dir;
11924 dwo_file->sections.abbrev =
11925 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
11926 sections.abbrev_offset,
11927 sections.abbrev_size);
11928 dwo_file->sections.line =
11929 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
11930 sections.line_offset,
11931 sections.line_size);
11932 dwo_file->sections.loc =
11933 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
11934 sections.loc_offset, sections.loc_size);
11935 dwo_file->sections.macinfo =
11936 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
11937 sections.macinfo_offset,
11938 sections.macinfo_size);
11939 dwo_file->sections.macro =
11940 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
11941 sections.macro_offset,
11942 sections.macro_size);
11943 dwo_file->sections.str_offsets =
11944 create_dwp_v2_or_v5_section (per_objfile,
11945 &dwp_file->sections.str_offsets,
11946 sections.str_offsets_offset,
11947 sections.str_offsets_size);
11948 /* The "str" section is global to the entire DWP file. */
11949 dwo_file->sections.str = dwp_file->sections.str;
11950 /* The info or types section is assigned below to dwo_unit,
11951 there's no need to record it in dwo_file.
11952 Also, we can't simply record type sections in dwo_file because
11953 we record a pointer into the vector in dwo_unit. As we collect more
11954 types we'll grow the vector and eventually have to reallocate space
11955 for it, invalidating all copies of pointers into the previous
11956 contents. */
11957 *dwo_file_slot = dwo_file;
11958 }
11959 else
11960 {
11961 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11962 virtual_dwo_name.c_str ());
11963
11964 dwo_file = (struct dwo_file *) *dwo_file_slot;
11965 }
11966
11967 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11968 dwo_unit->dwo_file = dwo_file;
11969 dwo_unit->signature = signature;
11970 dwo_unit->section =
11971 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11972 *dwo_unit->section = create_dwp_v2_or_v5_section
11973 (per_objfile,
11974 is_debug_types
11975 ? &dwp_file->sections.types
11976 : &dwp_file->sections.info,
11977 sections.info_or_types_offset,
11978 sections.info_or_types_size);
11979 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11980
11981 return dwo_unit;
11982 }
11983
11984 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11985 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11986 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11987 This is for DWP version 5 files. */
11988
11989 static struct dwo_unit *
11990 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
11991 struct dwp_file *dwp_file,
11992 uint32_t unit_index,
11993 const char *comp_dir,
11994 ULONGEST signature, int is_debug_types)
11995 {
11996 const struct dwp_hash_table *dwp_htab
11997 = is_debug_types ? dwp_file->tus : dwp_file->cus;
11998 bfd *dbfd = dwp_file->dbfd.get ();
11999 const char *kind = is_debug_types ? "TU" : "CU";
12000 struct dwo_file *dwo_file;
12001 struct dwo_unit *dwo_unit;
12002 struct virtual_v2_or_v5_dwo_sections sections {};
12003 void **dwo_file_slot;
12004
12005 gdb_assert (dwp_file->version == 5);
12006
12007 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12008 kind, pulongest (unit_index), hex_string (signature),
12009 dwp_file->name);
12010
12011 /* Fetch the section offsets of this DWO unit. */
12012
12013 /* memset (&sections, 0, sizeof (sections)); */
12014
12015 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12016 {
12017 uint32_t offset = read_4_bytes (dbfd,
12018 dwp_htab->section_pool.v5.offsets
12019 + (((unit_index - 1)
12020 * dwp_htab->nr_columns
12021 + i)
12022 * sizeof (uint32_t)));
12023 uint32_t size = read_4_bytes (dbfd,
12024 dwp_htab->section_pool.v5.sizes
12025 + (((unit_index - 1) * dwp_htab->nr_columns
12026 + i)
12027 * sizeof (uint32_t)));
12028
12029 switch (dwp_htab->section_pool.v5.section_ids[i])
12030 {
12031 case DW_SECT_ABBREV_V5:
12032 sections.abbrev_offset = offset;
12033 sections.abbrev_size = size;
12034 break;
12035 case DW_SECT_INFO_V5:
12036 sections.info_or_types_offset = offset;
12037 sections.info_or_types_size = size;
12038 break;
12039 case DW_SECT_LINE_V5:
12040 sections.line_offset = offset;
12041 sections.line_size = size;
12042 break;
12043 case DW_SECT_LOCLISTS_V5:
12044 sections.loclists_offset = offset;
12045 sections.loclists_size = size;
12046 break;
12047 case DW_SECT_MACRO_V5:
12048 sections.macro_offset = offset;
12049 sections.macro_size = size;
12050 break;
12051 case DW_SECT_RNGLISTS_V5:
12052 sections.rnglists_offset = offset;
12053 sections.rnglists_size = size;
12054 break;
12055 case DW_SECT_STR_OFFSETS_V5:
12056 sections.str_offsets_offset = offset;
12057 sections.str_offsets_size = size;
12058 break;
12059 case DW_SECT_RESERVED_V5:
12060 default:
12061 break;
12062 }
12063 }
12064
12065 /* It's easier for the rest of the code if we fake a struct dwo_file and
12066 have dwo_unit "live" in that. At least for now.
12067
12068 The DWP file can be made up of a random collection of CUs and TUs.
12069 However, for each CU + set of TUs that came from the same original DWO
12070 file, we can combine them back into a virtual DWO file to save space
12071 (fewer struct dwo_file objects to allocate). Remember that for really
12072 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12073
12074 std::string virtual_dwo_name =
12075 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12076 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12077 (long) (sections.line_size ? sections.line_offset : 0),
12078 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12079 (long) (sections.str_offsets_size
12080 ? sections.str_offsets_offset : 0),
12081 (long) (sections.macro_size ? sections.macro_offset : 0),
12082 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12083 /* Can we use an existing virtual DWO file? */
12084 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12085 virtual_dwo_name.c_str (),
12086 comp_dir);
12087 /* Create one if necessary. */
12088 if (*dwo_file_slot == NULL)
12089 {
12090 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12091 virtual_dwo_name.c_str ());
12092
12093 dwo_file = new struct dwo_file;
12094 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12095 dwo_file->comp_dir = comp_dir;
12096 dwo_file->sections.abbrev =
12097 create_dwp_v2_or_v5_section (per_objfile,
12098 &dwp_file->sections.abbrev,
12099 sections.abbrev_offset,
12100 sections.abbrev_size);
12101 dwo_file->sections.line =
12102 create_dwp_v2_or_v5_section (per_objfile,
12103 &dwp_file->sections.line,
12104 sections.line_offset, sections.line_size);
12105 dwo_file->sections.macro =
12106 create_dwp_v2_or_v5_section (per_objfile,
12107 &dwp_file->sections.macro,
12108 sections.macro_offset,
12109 sections.macro_size);
12110 dwo_file->sections.loclists =
12111 create_dwp_v2_or_v5_section (per_objfile,
12112 &dwp_file->sections.loclists,
12113 sections.loclists_offset,
12114 sections.loclists_size);
12115 dwo_file->sections.rnglists =
12116 create_dwp_v2_or_v5_section (per_objfile,
12117 &dwp_file->sections.rnglists,
12118 sections.rnglists_offset,
12119 sections.rnglists_size);
12120 dwo_file->sections.str_offsets =
12121 create_dwp_v2_or_v5_section (per_objfile,
12122 &dwp_file->sections.str_offsets,
12123 sections.str_offsets_offset,
12124 sections.str_offsets_size);
12125 /* The "str" section is global to the entire DWP file. */
12126 dwo_file->sections.str = dwp_file->sections.str;
12127 /* The info or types section is assigned below to dwo_unit,
12128 there's no need to record it in dwo_file.
12129 Also, we can't simply record type sections in dwo_file because
12130 we record a pointer into the vector in dwo_unit. As we collect more
12131 types we'll grow the vector and eventually have to reallocate space
12132 for it, invalidating all copies of pointers into the previous
12133 contents. */
12134 *dwo_file_slot = dwo_file;
12135 }
12136 else
12137 {
12138 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12139 virtual_dwo_name.c_str ());
12140
12141 dwo_file = (struct dwo_file *) *dwo_file_slot;
12142 }
12143
12144 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12145 dwo_unit->dwo_file = dwo_file;
12146 dwo_unit->signature = signature;
12147 dwo_unit->section
12148 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12149 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12150 &dwp_file->sections.info,
12151 sections.info_or_types_offset,
12152 sections.info_or_types_size);
12153 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12154
12155 return dwo_unit;
12156 }
12157
12158 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12159 Returns NULL if the signature isn't found. */
12160
12161 static struct dwo_unit *
12162 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12163 struct dwp_file *dwp_file, const char *comp_dir,
12164 ULONGEST signature, int is_debug_types)
12165 {
12166 const struct dwp_hash_table *dwp_htab =
12167 is_debug_types ? dwp_file->tus : dwp_file->cus;
12168 bfd *dbfd = dwp_file->dbfd.get ();
12169 uint32_t mask = dwp_htab->nr_slots - 1;
12170 uint32_t hash = signature & mask;
12171 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12172 unsigned int i;
12173 void **slot;
12174 struct dwo_unit find_dwo_cu;
12175
12176 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12177 find_dwo_cu.signature = signature;
12178 slot = htab_find_slot (is_debug_types
12179 ? dwp_file->loaded_tus.get ()
12180 : dwp_file->loaded_cus.get (),
12181 &find_dwo_cu, INSERT);
12182
12183 if (*slot != NULL)
12184 return (struct dwo_unit *) *slot;
12185
12186 /* Use a for loop so that we don't loop forever on bad debug info. */
12187 for (i = 0; i < dwp_htab->nr_slots; ++i)
12188 {
12189 ULONGEST signature_in_table;
12190
12191 signature_in_table =
12192 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12193 if (signature_in_table == signature)
12194 {
12195 uint32_t unit_index =
12196 read_4_bytes (dbfd,
12197 dwp_htab->unit_table + hash * sizeof (uint32_t));
12198
12199 if (dwp_file->version == 1)
12200 {
12201 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12202 unit_index, comp_dir,
12203 signature, is_debug_types);
12204 }
12205 else if (dwp_file->version == 2)
12206 {
12207 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12208 unit_index, comp_dir,
12209 signature, is_debug_types);
12210 }
12211 else /* version == 5 */
12212 {
12213 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12214 unit_index, comp_dir,
12215 signature, is_debug_types);
12216 }
12217 return (struct dwo_unit *) *slot;
12218 }
12219 if (signature_in_table == 0)
12220 return NULL;
12221 hash = (hash + hash2) & mask;
12222 }
12223
12224 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12225 " [in module %s]"),
12226 dwp_file->name);
12227 }
12228
12229 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12230 Open the file specified by FILE_NAME and hand it off to BFD for
12231 preliminary analysis. Return a newly initialized bfd *, which
12232 includes a canonicalized copy of FILE_NAME.
12233 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12234 SEARCH_CWD is true if the current directory is to be searched.
12235 It will be searched before debug-file-directory.
12236 If successful, the file is added to the bfd include table of the
12237 objfile's bfd (see gdb_bfd_record_inclusion).
12238 If unable to find/open the file, return NULL.
12239 NOTE: This function is derived from symfile_bfd_open. */
12240
12241 static gdb_bfd_ref_ptr
12242 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12243 const char *file_name, int is_dwp, int search_cwd)
12244 {
12245 int desc;
12246 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12247 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12248 to debug_file_directory. */
12249 const char *search_path;
12250 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12251
12252 gdb::unique_xmalloc_ptr<char> search_path_holder;
12253 if (search_cwd)
12254 {
12255 if (*debug_file_directory != '\0')
12256 {
12257 search_path_holder.reset (concat (".", dirname_separator_string,
12258 debug_file_directory,
12259 (char *) NULL));
12260 search_path = search_path_holder.get ();
12261 }
12262 else
12263 search_path = ".";
12264 }
12265 else
12266 search_path = debug_file_directory;
12267
12268 /* Add the path for the executable binary to the list of search paths. */
12269 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12270 search_path_holder.reset (concat (objfile_dir.c_str (),
12271 dirname_separator_string,
12272 search_path, nullptr));
12273 search_path = search_path_holder.get ();
12274
12275 openp_flags flags = OPF_RETURN_REALPATH;
12276 if (is_dwp)
12277 flags |= OPF_SEARCH_IN_PATH;
12278
12279 gdb::unique_xmalloc_ptr<char> absolute_name;
12280 desc = openp (search_path, flags, file_name,
12281 O_RDONLY | O_BINARY, &absolute_name);
12282 if (desc < 0)
12283 return NULL;
12284
12285 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12286 gnutarget, desc));
12287 if (sym_bfd == NULL)
12288 return NULL;
12289 bfd_set_cacheable (sym_bfd.get (), 1);
12290
12291 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12292 return NULL;
12293
12294 /* Success. Record the bfd as having been included by the objfile's bfd.
12295 This is important because things like demangled_names_hash lives in the
12296 objfile's per_bfd space and may have references to things like symbol
12297 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12298 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12299
12300 return sym_bfd;
12301 }
12302
12303 /* Try to open DWO file FILE_NAME.
12304 COMP_DIR is the DW_AT_comp_dir attribute.
12305 The result is the bfd handle of the file.
12306 If there is a problem finding or opening the file, return NULL.
12307 Upon success, the canonicalized path of the file is stored in the bfd,
12308 same as symfile_bfd_open. */
12309
12310 static gdb_bfd_ref_ptr
12311 open_dwo_file (dwarf2_per_objfile *per_objfile,
12312 const char *file_name, const char *comp_dir)
12313 {
12314 if (IS_ABSOLUTE_PATH (file_name))
12315 return try_open_dwop_file (per_objfile, file_name,
12316 0 /*is_dwp*/, 0 /*search_cwd*/);
12317
12318 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12319
12320 if (comp_dir != NULL)
12321 {
12322 gdb::unique_xmalloc_ptr<char> path_to_try
12323 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12324
12325 /* NOTE: If comp_dir is a relative path, this will also try the
12326 search path, which seems useful. */
12327 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12328 0 /*is_dwp*/,
12329 1 /*search_cwd*/));
12330 if (abfd != NULL)
12331 return abfd;
12332 }
12333
12334 /* That didn't work, try debug-file-directory, which, despite its name,
12335 is a list of paths. */
12336
12337 if (*debug_file_directory == '\0')
12338 return NULL;
12339
12340 return try_open_dwop_file (per_objfile, file_name,
12341 0 /*is_dwp*/, 1 /*search_cwd*/);
12342 }
12343
12344 /* This function is mapped across the sections and remembers the offset and
12345 size of each of the DWO debugging sections we are interested in. */
12346
12347 static void
12348 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12349 dwo_sections *dwo_sections)
12350 {
12351 const struct dwop_section_names *names = &dwop_section_names;
12352
12353 if (names->abbrev_dwo.matches (sectp->name))
12354 {
12355 dwo_sections->abbrev.s.section = sectp;
12356 dwo_sections->abbrev.size = bfd_section_size (sectp);
12357 }
12358 else if (names->info_dwo.matches (sectp->name))
12359 {
12360 dwo_sections->info.s.section = sectp;
12361 dwo_sections->info.size = bfd_section_size (sectp);
12362 }
12363 else if (names->line_dwo.matches (sectp->name))
12364 {
12365 dwo_sections->line.s.section = sectp;
12366 dwo_sections->line.size = bfd_section_size (sectp);
12367 }
12368 else if (names->loc_dwo.matches (sectp->name))
12369 {
12370 dwo_sections->loc.s.section = sectp;
12371 dwo_sections->loc.size = bfd_section_size (sectp);
12372 }
12373 else if (names->loclists_dwo.matches (sectp->name))
12374 {
12375 dwo_sections->loclists.s.section = sectp;
12376 dwo_sections->loclists.size = bfd_section_size (sectp);
12377 }
12378 else if (names->macinfo_dwo.matches (sectp->name))
12379 {
12380 dwo_sections->macinfo.s.section = sectp;
12381 dwo_sections->macinfo.size = bfd_section_size (sectp);
12382 }
12383 else if (names->macro_dwo.matches (sectp->name))
12384 {
12385 dwo_sections->macro.s.section = sectp;
12386 dwo_sections->macro.size = bfd_section_size (sectp);
12387 }
12388 else if (names->rnglists_dwo.matches (sectp->name))
12389 {
12390 dwo_sections->rnglists.s.section = sectp;
12391 dwo_sections->rnglists.size = bfd_section_size (sectp);
12392 }
12393 else if (names->str_dwo.matches (sectp->name))
12394 {
12395 dwo_sections->str.s.section = sectp;
12396 dwo_sections->str.size = bfd_section_size (sectp);
12397 }
12398 else if (names->str_offsets_dwo.matches (sectp->name))
12399 {
12400 dwo_sections->str_offsets.s.section = sectp;
12401 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12402 }
12403 else if (names->types_dwo.matches (sectp->name))
12404 {
12405 struct dwarf2_section_info type_section;
12406
12407 memset (&type_section, 0, sizeof (type_section));
12408 type_section.s.section = sectp;
12409 type_section.size = bfd_section_size (sectp);
12410 dwo_sections->types.push_back (type_section);
12411 }
12412 }
12413
12414 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12415 by PER_CU. This is for the non-DWP case.
12416 The result is NULL if DWO_NAME can't be found. */
12417
12418 static struct dwo_file *
12419 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12420 const char *comp_dir)
12421 {
12422 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12423
12424 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12425 if (dbfd == NULL)
12426 {
12427 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12428
12429 return NULL;
12430 }
12431
12432 dwo_file_up dwo_file (new struct dwo_file);
12433 dwo_file->dwo_name = dwo_name;
12434 dwo_file->comp_dir = comp_dir;
12435 dwo_file->dbfd = std::move (dbfd);
12436
12437 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12438 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12439 &dwo_file->sections);
12440
12441 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12442 dwo_file->cus);
12443
12444 if (cu->per_cu->dwarf_version < 5)
12445 {
12446 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12447 dwo_file->sections.types, dwo_file->tus);
12448 }
12449 else
12450 {
12451 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12452 &dwo_file->sections.info, dwo_file->tus,
12453 rcuh_kind::COMPILE);
12454 }
12455
12456 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12457
12458 return dwo_file.release ();
12459 }
12460
12461 /* This function is mapped across the sections and remembers the offset and
12462 size of each of the DWP debugging sections common to version 1 and 2 that
12463 we are interested in. */
12464
12465 static void
12466 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12467 dwp_file *dwp_file)
12468 {
12469 const struct dwop_section_names *names = &dwop_section_names;
12470 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12471
12472 /* Record the ELF section number for later lookup: this is what the
12473 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12474 gdb_assert (elf_section_nr < dwp_file->num_sections);
12475 dwp_file->elf_sections[elf_section_nr] = sectp;
12476
12477 /* Look for specific sections that we need. */
12478 if (names->str_dwo.matches (sectp->name))
12479 {
12480 dwp_file->sections.str.s.section = sectp;
12481 dwp_file->sections.str.size = bfd_section_size (sectp);
12482 }
12483 else if (names->cu_index.matches (sectp->name))
12484 {
12485 dwp_file->sections.cu_index.s.section = sectp;
12486 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12487 }
12488 else if (names->tu_index.matches (sectp->name))
12489 {
12490 dwp_file->sections.tu_index.s.section = sectp;
12491 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12492 }
12493 }
12494
12495 /* This function is mapped across the sections and remembers the offset and
12496 size of each of the DWP version 2 debugging sections that we are interested
12497 in. This is split into a separate function because we don't know if we
12498 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12499
12500 static void
12501 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12502 {
12503 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12504 const struct dwop_section_names *names = &dwop_section_names;
12505 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12506
12507 /* Record the ELF section number for later lookup: this is what the
12508 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12509 gdb_assert (elf_section_nr < dwp_file->num_sections);
12510 dwp_file->elf_sections[elf_section_nr] = sectp;
12511
12512 /* Look for specific sections that we need. */
12513 if (names->abbrev_dwo.matches (sectp->name))
12514 {
12515 dwp_file->sections.abbrev.s.section = sectp;
12516 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12517 }
12518 else if (names->info_dwo.matches (sectp->name))
12519 {
12520 dwp_file->sections.info.s.section = sectp;
12521 dwp_file->sections.info.size = bfd_section_size (sectp);
12522 }
12523 else if (names->line_dwo.matches (sectp->name))
12524 {
12525 dwp_file->sections.line.s.section = sectp;
12526 dwp_file->sections.line.size = bfd_section_size (sectp);
12527 }
12528 else if (names->loc_dwo.matches (sectp->name))
12529 {
12530 dwp_file->sections.loc.s.section = sectp;
12531 dwp_file->sections.loc.size = bfd_section_size (sectp);
12532 }
12533 else if (names->macinfo_dwo.matches (sectp->name))
12534 {
12535 dwp_file->sections.macinfo.s.section = sectp;
12536 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12537 }
12538 else if (names->macro_dwo.matches (sectp->name))
12539 {
12540 dwp_file->sections.macro.s.section = sectp;
12541 dwp_file->sections.macro.size = bfd_section_size (sectp);
12542 }
12543 else if (names->str_offsets_dwo.matches (sectp->name))
12544 {
12545 dwp_file->sections.str_offsets.s.section = sectp;
12546 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12547 }
12548 else if (names->types_dwo.matches (sectp->name))
12549 {
12550 dwp_file->sections.types.s.section = sectp;
12551 dwp_file->sections.types.size = bfd_section_size (sectp);
12552 }
12553 }
12554
12555 /* This function is mapped across the sections and remembers the offset and
12556 size of each of the DWP version 5 debugging sections that we are interested
12557 in. This is split into a separate function because we don't know if we
12558 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12559
12560 static void
12561 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12562 {
12563 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12564 const struct dwop_section_names *names = &dwop_section_names;
12565 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12566
12567 /* Record the ELF section number for later lookup: this is what the
12568 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12569 gdb_assert (elf_section_nr < dwp_file->num_sections);
12570 dwp_file->elf_sections[elf_section_nr] = sectp;
12571
12572 /* Look for specific sections that we need. */
12573 if (names->abbrev_dwo.matches (sectp->name))
12574 {
12575 dwp_file->sections.abbrev.s.section = sectp;
12576 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12577 }
12578 else if (names->info_dwo.matches (sectp->name))
12579 {
12580 dwp_file->sections.info.s.section = sectp;
12581 dwp_file->sections.info.size = bfd_section_size (sectp);
12582 }
12583 else if (names->line_dwo.matches (sectp->name))
12584 {
12585 dwp_file->sections.line.s.section = sectp;
12586 dwp_file->sections.line.size = bfd_section_size (sectp);
12587 }
12588 else if (names->loclists_dwo.matches (sectp->name))
12589 {
12590 dwp_file->sections.loclists.s.section = sectp;
12591 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12592 }
12593 else if (names->macro_dwo.matches (sectp->name))
12594 {
12595 dwp_file->sections.macro.s.section = sectp;
12596 dwp_file->sections.macro.size = bfd_section_size (sectp);
12597 }
12598 else if (names->rnglists_dwo.matches (sectp->name))
12599 {
12600 dwp_file->sections.rnglists.s.section = sectp;
12601 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12602 }
12603 else if (names->str_offsets_dwo.matches (sectp->name))
12604 {
12605 dwp_file->sections.str_offsets.s.section = sectp;
12606 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12607 }
12608 }
12609
12610 /* Hash function for dwp_file loaded CUs/TUs. */
12611
12612 static hashval_t
12613 hash_dwp_loaded_cutus (const void *item)
12614 {
12615 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12616
12617 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12618 return dwo_unit->signature;
12619 }
12620
12621 /* Equality function for dwp_file loaded CUs/TUs. */
12622
12623 static int
12624 eq_dwp_loaded_cutus (const void *a, const void *b)
12625 {
12626 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12627 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12628
12629 return dua->signature == dub->signature;
12630 }
12631
12632 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12633
12634 static htab_up
12635 allocate_dwp_loaded_cutus_table ()
12636 {
12637 return htab_up (htab_create_alloc (3,
12638 hash_dwp_loaded_cutus,
12639 eq_dwp_loaded_cutus,
12640 NULL, xcalloc, xfree));
12641 }
12642
12643 /* Try to open DWP file FILE_NAME.
12644 The result is the bfd handle of the file.
12645 If there is a problem finding or opening the file, return NULL.
12646 Upon success, the canonicalized path of the file is stored in the bfd,
12647 same as symfile_bfd_open. */
12648
12649 static gdb_bfd_ref_ptr
12650 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12651 {
12652 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12653 1 /*is_dwp*/,
12654 1 /*search_cwd*/));
12655 if (abfd != NULL)
12656 return abfd;
12657
12658 /* Work around upstream bug 15652.
12659 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12660 [Whether that's a "bug" is debatable, but it is getting in our way.]
12661 We have no real idea where the dwp file is, because gdb's realpath-ing
12662 of the executable's path may have discarded the needed info.
12663 [IWBN if the dwp file name was recorded in the executable, akin to
12664 .gnu_debuglink, but that doesn't exist yet.]
12665 Strip the directory from FILE_NAME and search again. */
12666 if (*debug_file_directory != '\0')
12667 {
12668 /* Don't implicitly search the current directory here.
12669 If the user wants to search "." to handle this case,
12670 it must be added to debug-file-directory. */
12671 return try_open_dwop_file (per_objfile, lbasename (file_name),
12672 1 /*is_dwp*/,
12673 0 /*search_cwd*/);
12674 }
12675
12676 return NULL;
12677 }
12678
12679 /* Initialize the use of the DWP file for the current objfile.
12680 By convention the name of the DWP file is ${objfile}.dwp.
12681 The result is NULL if it can't be found. */
12682
12683 static std::unique_ptr<struct dwp_file>
12684 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12685 {
12686 struct objfile *objfile = per_objfile->objfile;
12687
12688 /* Try to find first .dwp for the binary file before any symbolic links
12689 resolving. */
12690
12691 /* If the objfile is a debug file, find the name of the real binary
12692 file and get the name of dwp file from there. */
12693 std::string dwp_name;
12694 if (objfile->separate_debug_objfile_backlink != NULL)
12695 {
12696 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12697 const char *backlink_basename = lbasename (backlink->original_name);
12698
12699 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12700 }
12701 else
12702 dwp_name = objfile->original_name;
12703
12704 dwp_name += ".dwp";
12705
12706 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12707 if (dbfd == NULL
12708 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12709 {
12710 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12711 dwp_name = objfile_name (objfile);
12712 dwp_name += ".dwp";
12713 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12714 }
12715
12716 if (dbfd == NULL)
12717 {
12718 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12719
12720 return std::unique_ptr<dwp_file> ();
12721 }
12722
12723 const char *name = bfd_get_filename (dbfd.get ());
12724 std::unique_ptr<struct dwp_file> dwp_file
12725 (new struct dwp_file (name, std::move (dbfd)));
12726
12727 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12728 dwp_file->elf_sections =
12729 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12730 dwp_file->num_sections, asection *);
12731
12732 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12733 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12734 dwp_file.get ());
12735
12736 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12737
12738 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12739
12740 /* The DWP file version is stored in the hash table. Oh well. */
12741 if (dwp_file->cus && dwp_file->tus
12742 && dwp_file->cus->version != dwp_file->tus->version)
12743 {
12744 /* Technically speaking, we should try to limp along, but this is
12745 pretty bizarre. We use pulongest here because that's the established
12746 portability solution (e.g, we cannot use %u for uint32_t). */
12747 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12748 " TU version %s [in DWP file %s]"),
12749 pulongest (dwp_file->cus->version),
12750 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12751 }
12752
12753 if (dwp_file->cus)
12754 dwp_file->version = dwp_file->cus->version;
12755 else if (dwp_file->tus)
12756 dwp_file->version = dwp_file->tus->version;
12757 else
12758 dwp_file->version = 2;
12759
12760 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12761 {
12762 if (dwp_file->version == 2)
12763 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12764 dwp_file.get ());
12765 else
12766 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12767 dwp_file.get ());
12768 }
12769
12770 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12771 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12772
12773 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12774 dwarf_read_debug_printf (" %s CUs, %s TUs",
12775 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12776 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12777
12778 return dwp_file;
12779 }
12780
12781 /* Wrapper around open_and_init_dwp_file, only open it once. */
12782
12783 static struct dwp_file *
12784 get_dwp_file (dwarf2_per_objfile *per_objfile)
12785 {
12786 if (!per_objfile->per_bfd->dwp_checked)
12787 {
12788 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12789 per_objfile->per_bfd->dwp_checked = 1;
12790 }
12791 return per_objfile->per_bfd->dwp_file.get ();
12792 }
12793
12794 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12795 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12796 or in the DWP file for the objfile, referenced by THIS_UNIT.
12797 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12798 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12799
12800 This is called, for example, when wanting to read a variable with a
12801 complex location. Therefore we don't want to do file i/o for every call.
12802 Therefore we don't want to look for a DWO file on every call.
12803 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12804 then we check if we've already seen DWO_NAME, and only THEN do we check
12805 for a DWO file.
12806
12807 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12808 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12809
12810 static struct dwo_unit *
12811 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12812 ULONGEST signature, int is_debug_types)
12813 {
12814 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12815 struct objfile *objfile = per_objfile->objfile;
12816 const char *kind = is_debug_types ? "TU" : "CU";
12817 void **dwo_file_slot;
12818 struct dwo_file *dwo_file;
12819 struct dwp_file *dwp_file;
12820
12821 /* First see if there's a DWP file.
12822 If we have a DWP file but didn't find the DWO inside it, don't
12823 look for the original DWO file. It makes gdb behave differently
12824 depending on whether one is debugging in the build tree. */
12825
12826 dwp_file = get_dwp_file (per_objfile);
12827 if (dwp_file != NULL)
12828 {
12829 const struct dwp_hash_table *dwp_htab =
12830 is_debug_types ? dwp_file->tus : dwp_file->cus;
12831
12832 if (dwp_htab != NULL)
12833 {
12834 struct dwo_unit *dwo_cutu =
12835 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12836 is_debug_types);
12837
12838 if (dwo_cutu != NULL)
12839 {
12840 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12841 kind, hex_string (signature),
12842 host_address_to_string (dwo_cutu));
12843
12844 return dwo_cutu;
12845 }
12846 }
12847 }
12848 else
12849 {
12850 /* No DWP file, look for the DWO file. */
12851
12852 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12853 if (*dwo_file_slot == NULL)
12854 {
12855 /* Read in the file and build a table of the CUs/TUs it contains. */
12856 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12857 }
12858 /* NOTE: This will be NULL if unable to open the file. */
12859 dwo_file = (struct dwo_file *) *dwo_file_slot;
12860
12861 if (dwo_file != NULL)
12862 {
12863 struct dwo_unit *dwo_cutu = NULL;
12864
12865 if (is_debug_types && dwo_file->tus)
12866 {
12867 struct dwo_unit find_dwo_cutu;
12868
12869 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12870 find_dwo_cutu.signature = signature;
12871 dwo_cutu
12872 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12873 &find_dwo_cutu);
12874 }
12875 else if (!is_debug_types && dwo_file->cus)
12876 {
12877 struct dwo_unit find_dwo_cutu;
12878
12879 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12880 find_dwo_cutu.signature = signature;
12881 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12882 &find_dwo_cutu);
12883 }
12884
12885 if (dwo_cutu != NULL)
12886 {
12887 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
12888 kind, dwo_name, hex_string (signature),
12889 host_address_to_string (dwo_cutu));
12890
12891 return dwo_cutu;
12892 }
12893 }
12894 }
12895
12896 /* We didn't find it. This could mean a dwo_id mismatch, or
12897 someone deleted the DWO/DWP file, or the search path isn't set up
12898 correctly to find the file. */
12899
12900 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
12901 kind, dwo_name, hex_string (signature));
12902
12903 /* This is a warning and not a complaint because it can be caused by
12904 pilot error (e.g., user accidentally deleting the DWO). */
12905 {
12906 /* Print the name of the DWP file if we looked there, helps the user
12907 better diagnose the problem. */
12908 std::string dwp_text;
12909
12910 if (dwp_file != NULL)
12911 dwp_text = string_printf (" [in DWP file %s]",
12912 lbasename (dwp_file->name));
12913
12914 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12915 " [in module %s]"),
12916 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12917 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12918 }
12919 return NULL;
12920 }
12921
12922 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12923 See lookup_dwo_cutu_unit for details. */
12924
12925 static struct dwo_unit *
12926 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12927 ULONGEST signature)
12928 {
12929 gdb_assert (!cu->per_cu->is_debug_types);
12930
12931 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12932 }
12933
12934 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12935 See lookup_dwo_cutu_unit for details. */
12936
12937 static struct dwo_unit *
12938 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12939 {
12940 gdb_assert (cu->per_cu->is_debug_types);
12941
12942 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12943
12944 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12945 }
12946
12947 /* Traversal function for queue_and_load_all_dwo_tus. */
12948
12949 static int
12950 queue_and_load_dwo_tu (void **slot, void *info)
12951 {
12952 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12953 dwarf2_cu *cu = (dwarf2_cu *) info;
12954 ULONGEST signature = dwo_unit->signature;
12955 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12956
12957 if (sig_type != NULL)
12958 {
12959 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12960 a real dependency of PER_CU on SIG_TYPE. That is detected later
12961 while processing PER_CU. */
12962 if (maybe_queue_comp_unit (NULL, sig_type, cu->per_objfile,
12963 cu->language))
12964 load_full_type_unit (sig_type, cu->per_objfile);
12965 cu->per_cu->imported_symtabs_push (sig_type);
12966 }
12967
12968 return 1;
12969 }
12970
12971 /* Queue all TUs contained in the DWO of CU to be read in.
12972 The DWO may have the only definition of the type, though it may not be
12973 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12974 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12975
12976 static void
12977 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12978 {
12979 struct dwo_unit *dwo_unit;
12980 struct dwo_file *dwo_file;
12981
12982 gdb_assert (cu != nullptr);
12983 gdb_assert (!cu->per_cu->is_debug_types);
12984 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12985
12986 dwo_unit = cu->dwo_unit;
12987 gdb_assert (dwo_unit != NULL);
12988
12989 dwo_file = dwo_unit->dwo_file;
12990 if (dwo_file->tus != NULL)
12991 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12992 }
12993
12994 /* Read in various DIEs. */
12995
12996 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12997 Inherit only the children of the DW_AT_abstract_origin DIE not being
12998 already referenced by DW_AT_abstract_origin from the children of the
12999 current DIE. */
13000
13001 static void
13002 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13003 {
13004 struct die_info *child_die;
13005 sect_offset *offsetp;
13006 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13007 struct die_info *origin_die;
13008 /* Iterator of the ORIGIN_DIE children. */
13009 struct die_info *origin_child_die;
13010 struct attribute *attr;
13011 struct dwarf2_cu *origin_cu;
13012 struct pending **origin_previous_list_in_scope;
13013
13014 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13015 if (!attr)
13016 return;
13017
13018 /* Note that following die references may follow to a die in a
13019 different cu. */
13020
13021 origin_cu = cu;
13022 origin_die = follow_die_ref (die, attr, &origin_cu);
13023
13024 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13025 symbols in. */
13026 origin_previous_list_in_scope = origin_cu->list_in_scope;
13027 origin_cu->list_in_scope = cu->list_in_scope;
13028
13029 if (die->tag != origin_die->tag
13030 && !(die->tag == DW_TAG_inlined_subroutine
13031 && origin_die->tag == DW_TAG_subprogram))
13032 complaint (_("DIE %s and its abstract origin %s have different tags"),
13033 sect_offset_str (die->sect_off),
13034 sect_offset_str (origin_die->sect_off));
13035
13036 /* Find if the concrete and abstract trees are structurally the
13037 same. This is a shallow traversal and it is not bullet-proof;
13038 the compiler can trick the debugger into believing that the trees
13039 are isomorphic, whereas they actually are not. However, the
13040 likelyhood of this happening is pretty low, and a full-fledged
13041 check would be an overkill. */
13042 bool are_isomorphic = true;
13043 die_info *concrete_child = die->child;
13044 die_info *abstract_child = origin_die->child;
13045 while (concrete_child != nullptr || abstract_child != nullptr)
13046 {
13047 if (concrete_child == nullptr
13048 || abstract_child == nullptr
13049 || concrete_child->tag != abstract_child->tag)
13050 {
13051 are_isomorphic = false;
13052 break;
13053 }
13054
13055 concrete_child = concrete_child->sibling;
13056 abstract_child = abstract_child->sibling;
13057 }
13058
13059 /* Walk the origin's children in parallel to the concrete children.
13060 This helps match an origin child in case the debug info misses
13061 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13062 origin tree may not have the same tree structure as the concrete
13063 DIE, though. */
13064 die_info *corresponding_abstract_child
13065 = are_isomorphic ? origin_die->child : nullptr;
13066
13067 std::vector<sect_offset> offsets;
13068
13069 for (child_die = die->child;
13070 child_die && child_die->tag;
13071 child_die = child_die->sibling)
13072 {
13073 struct die_info *child_origin_die;
13074 struct dwarf2_cu *child_origin_cu;
13075
13076 /* We are trying to process concrete instance entries:
13077 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13078 it's not relevant to our analysis here. i.e. detecting DIEs that are
13079 present in the abstract instance but not referenced in the concrete
13080 one. */
13081 if (child_die->tag == DW_TAG_call_site
13082 || child_die->tag == DW_TAG_GNU_call_site)
13083 {
13084 if (are_isomorphic)
13085 corresponding_abstract_child
13086 = corresponding_abstract_child->sibling;
13087 continue;
13088 }
13089
13090 /* For each CHILD_DIE, find the corresponding child of
13091 ORIGIN_DIE. If there is more than one layer of
13092 DW_AT_abstract_origin, follow them all; there shouldn't be,
13093 but GCC versions at least through 4.4 generate this (GCC PR
13094 40573). */
13095 child_origin_die = child_die;
13096 child_origin_cu = cu;
13097 while (1)
13098 {
13099 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13100 child_origin_cu);
13101 if (attr == NULL)
13102 break;
13103 child_origin_die = follow_die_ref (child_origin_die, attr,
13104 &child_origin_cu);
13105 }
13106
13107 /* If missing DW_AT_abstract_origin, try the corresponding child
13108 of the origin. Clang emits such lexical scopes. */
13109 if (child_origin_die == child_die
13110 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13111 && are_isomorphic
13112 && child_die->tag == DW_TAG_lexical_block)
13113 child_origin_die = corresponding_abstract_child;
13114
13115 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13116 counterpart may exist. */
13117 if (child_origin_die != child_die)
13118 {
13119 if (child_die->tag != child_origin_die->tag
13120 && !(child_die->tag == DW_TAG_inlined_subroutine
13121 && child_origin_die->tag == DW_TAG_subprogram))
13122 complaint (_("Child DIE %s and its abstract origin %s have "
13123 "different tags"),
13124 sect_offset_str (child_die->sect_off),
13125 sect_offset_str (child_origin_die->sect_off));
13126 if (child_origin_die->parent != origin_die)
13127 complaint (_("Child DIE %s and its abstract origin %s have "
13128 "different parents"),
13129 sect_offset_str (child_die->sect_off),
13130 sect_offset_str (child_origin_die->sect_off));
13131 else
13132 offsets.push_back (child_origin_die->sect_off);
13133 }
13134
13135 if (are_isomorphic)
13136 corresponding_abstract_child = corresponding_abstract_child->sibling;
13137 }
13138 std::sort (offsets.begin (), offsets.end ());
13139 sect_offset *offsets_end = offsets.data () + offsets.size ();
13140 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13141 if (offsetp[-1] == *offsetp)
13142 complaint (_("Multiple children of DIE %s refer "
13143 "to DIE %s as their abstract origin"),
13144 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13145
13146 offsetp = offsets.data ();
13147 origin_child_die = origin_die->child;
13148 while (origin_child_die && origin_child_die->tag)
13149 {
13150 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13151 while (offsetp < offsets_end
13152 && *offsetp < origin_child_die->sect_off)
13153 offsetp++;
13154 if (offsetp >= offsets_end
13155 || *offsetp > origin_child_die->sect_off)
13156 {
13157 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13158 Check whether we're already processing ORIGIN_CHILD_DIE.
13159 This can happen with mutually referenced abstract_origins.
13160 PR 16581. */
13161 if (!origin_child_die->in_process)
13162 process_die (origin_child_die, origin_cu);
13163 }
13164 origin_child_die = origin_child_die->sibling;
13165 }
13166 origin_cu->list_in_scope = origin_previous_list_in_scope;
13167
13168 if (cu != origin_cu)
13169 compute_delayed_physnames (origin_cu);
13170 }
13171
13172 static void
13173 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13174 {
13175 struct objfile *objfile = cu->per_objfile->objfile;
13176 struct gdbarch *gdbarch = objfile->arch ();
13177 struct context_stack *newobj;
13178 CORE_ADDR lowpc;
13179 CORE_ADDR highpc;
13180 struct die_info *child_die;
13181 struct attribute *attr, *call_line, *call_file;
13182 const char *name;
13183 CORE_ADDR baseaddr;
13184 struct block *block;
13185 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13186 std::vector<struct symbol *> template_args;
13187 struct template_symbol *templ_func = NULL;
13188
13189 if (inlined_func)
13190 {
13191 /* If we do not have call site information, we can't show the
13192 caller of this inlined function. That's too confusing, so
13193 only use the scope for local variables. */
13194 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13195 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13196 if (call_line == NULL || call_file == NULL)
13197 {
13198 read_lexical_block_scope (die, cu);
13199 return;
13200 }
13201 }
13202
13203 baseaddr = objfile->text_section_offset ();
13204
13205 name = dwarf2_name (die, cu);
13206
13207 /* Ignore functions with missing or empty names. These are actually
13208 illegal according to the DWARF standard. */
13209 if (name == NULL)
13210 {
13211 complaint (_("missing name for subprogram DIE at %s"),
13212 sect_offset_str (die->sect_off));
13213 return;
13214 }
13215
13216 /* Ignore functions with missing or invalid low and high pc attributes. */
13217 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13218 <= PC_BOUNDS_INVALID)
13219 {
13220 attr = dwarf2_attr (die, DW_AT_external, cu);
13221 if (attr == nullptr || !attr->as_boolean ())
13222 complaint (_("cannot get low and high bounds "
13223 "for subprogram DIE at %s"),
13224 sect_offset_str (die->sect_off));
13225 return;
13226 }
13227
13228 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13229 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13230
13231 /* If we have any template arguments, then we must allocate a
13232 different sort of symbol. */
13233 for (child_die = die->child; child_die; child_die = child_die->sibling)
13234 {
13235 if (child_die->tag == DW_TAG_template_type_param
13236 || child_die->tag == DW_TAG_template_value_param)
13237 {
13238 templ_func = new (&objfile->objfile_obstack) template_symbol;
13239 templ_func->subclass = SYMBOL_TEMPLATE;
13240 break;
13241 }
13242 }
13243
13244 gdb_assert (cu->get_builder () != nullptr);
13245 newobj = cu->get_builder ()->push_context (0, lowpc);
13246 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13247 (struct symbol *) templ_func);
13248
13249 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13250 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13251 cu->language);
13252
13253 /* If there is a location expression for DW_AT_frame_base, record
13254 it. */
13255 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13256 if (attr != nullptr)
13257 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13258
13259 /* If there is a location for the static link, record it. */
13260 newobj->static_link = NULL;
13261 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13262 if (attr != nullptr)
13263 {
13264 newobj->static_link
13265 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13266 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13267 cu->addr_type ());
13268 }
13269
13270 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13271
13272 if (die->child != NULL)
13273 {
13274 child_die = die->child;
13275 while (child_die && child_die->tag)
13276 {
13277 if (child_die->tag == DW_TAG_template_type_param
13278 || child_die->tag == DW_TAG_template_value_param)
13279 {
13280 struct symbol *arg = new_symbol (child_die, NULL, cu);
13281
13282 if (arg != NULL)
13283 template_args.push_back (arg);
13284 }
13285 else
13286 process_die (child_die, cu);
13287 child_die = child_die->sibling;
13288 }
13289 }
13290
13291 inherit_abstract_dies (die, cu);
13292
13293 /* If we have a DW_AT_specification, we might need to import using
13294 directives from the context of the specification DIE. See the
13295 comment in determine_prefix. */
13296 if (cu->language == language_cplus
13297 && dwarf2_attr (die, DW_AT_specification, cu))
13298 {
13299 struct dwarf2_cu *spec_cu = cu;
13300 struct die_info *spec_die = die_specification (die, &spec_cu);
13301
13302 while (spec_die)
13303 {
13304 child_die = spec_die->child;
13305 while (child_die && child_die->tag)
13306 {
13307 if (child_die->tag == DW_TAG_imported_module)
13308 process_die (child_die, spec_cu);
13309 child_die = child_die->sibling;
13310 }
13311
13312 /* In some cases, GCC generates specification DIEs that
13313 themselves contain DW_AT_specification attributes. */
13314 spec_die = die_specification (spec_die, &spec_cu);
13315 }
13316 }
13317
13318 struct context_stack cstk = cu->get_builder ()->pop_context ();
13319 /* Make a block for the local symbols within. */
13320 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13321 cstk.static_link, lowpc, highpc);
13322
13323 /* For C++, set the block's scope. */
13324 if ((cu->language == language_cplus
13325 || cu->language == language_fortran
13326 || cu->language == language_d
13327 || cu->language == language_rust)
13328 && cu->processing_has_namespace_info)
13329 block_set_scope (block, determine_prefix (die, cu),
13330 &objfile->objfile_obstack);
13331
13332 /* If we have address ranges, record them. */
13333 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13334
13335 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13336
13337 /* Attach template arguments to function. */
13338 if (!template_args.empty ())
13339 {
13340 gdb_assert (templ_func != NULL);
13341
13342 templ_func->n_template_arguments = template_args.size ();
13343 templ_func->template_arguments
13344 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13345 templ_func->n_template_arguments);
13346 memcpy (templ_func->template_arguments,
13347 template_args.data (),
13348 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13349
13350 /* Make sure that the symtab is set on the new symbols. Even
13351 though they don't appear in this symtab directly, other parts
13352 of gdb assume that symbols do, and this is reasonably
13353 true. */
13354 for (symbol *sym : template_args)
13355 symbol_set_symtab (sym, symbol_symtab (templ_func));
13356 }
13357
13358 /* In C++, we can have functions nested inside functions (e.g., when
13359 a function declares a class that has methods). This means that
13360 when we finish processing a function scope, we may need to go
13361 back to building a containing block's symbol lists. */
13362 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13363 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13364
13365 /* If we've finished processing a top-level function, subsequent
13366 symbols go in the file symbol list. */
13367 if (cu->get_builder ()->outermost_context_p ())
13368 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13369 }
13370
13371 /* Process all the DIES contained within a lexical block scope. Start
13372 a new scope, process the dies, and then close the scope. */
13373
13374 static void
13375 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13376 {
13377 struct objfile *objfile = cu->per_objfile->objfile;
13378 struct gdbarch *gdbarch = objfile->arch ();
13379 CORE_ADDR lowpc, highpc;
13380 struct die_info *child_die;
13381 CORE_ADDR baseaddr;
13382
13383 baseaddr = objfile->text_section_offset ();
13384
13385 /* Ignore blocks with missing or invalid low and high pc attributes. */
13386 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13387 as multiple lexical blocks? Handling children in a sane way would
13388 be nasty. Might be easier to properly extend generic blocks to
13389 describe ranges. */
13390 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13391 {
13392 case PC_BOUNDS_NOT_PRESENT:
13393 /* DW_TAG_lexical_block has no attributes, process its children as if
13394 there was no wrapping by that DW_TAG_lexical_block.
13395 GCC does no longer produces such DWARF since GCC r224161. */
13396 for (child_die = die->child;
13397 child_die != NULL && child_die->tag;
13398 child_die = child_die->sibling)
13399 {
13400 /* We might already be processing this DIE. This can happen
13401 in an unusual circumstance -- where a subroutine A
13402 appears lexically in another subroutine B, but A actually
13403 inlines B. The recursion is broken here, rather than in
13404 inherit_abstract_dies, because it seems better to simply
13405 drop concrete children here. */
13406 if (!child_die->in_process)
13407 process_die (child_die, cu);
13408 }
13409 return;
13410 case PC_BOUNDS_INVALID:
13411 return;
13412 }
13413 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13414 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13415
13416 cu->get_builder ()->push_context (0, lowpc);
13417 if (die->child != NULL)
13418 {
13419 child_die = die->child;
13420 while (child_die && child_die->tag)
13421 {
13422 process_die (child_die, cu);
13423 child_die = child_die->sibling;
13424 }
13425 }
13426 inherit_abstract_dies (die, cu);
13427 struct context_stack cstk = cu->get_builder ()->pop_context ();
13428
13429 if (*cu->get_builder ()->get_local_symbols () != NULL
13430 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13431 {
13432 struct block *block
13433 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13434 cstk.start_addr, highpc);
13435
13436 /* Note that recording ranges after traversing children, as we
13437 do here, means that recording a parent's ranges entails
13438 walking across all its children's ranges as they appear in
13439 the address map, which is quadratic behavior.
13440
13441 It would be nicer to record the parent's ranges before
13442 traversing its children, simply overriding whatever you find
13443 there. But since we don't even decide whether to create a
13444 block until after we've traversed its children, that's hard
13445 to do. */
13446 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13447 }
13448 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13449 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13450 }
13451
13452 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13453
13454 static void
13455 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13456 {
13457 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13458 struct objfile *objfile = per_objfile->objfile;
13459 struct gdbarch *gdbarch = objfile->arch ();
13460 CORE_ADDR pc, baseaddr;
13461 struct attribute *attr;
13462 struct call_site *call_site, call_site_local;
13463 void **slot;
13464 int nparams;
13465 struct die_info *child_die;
13466
13467 baseaddr = objfile->text_section_offset ();
13468
13469 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13470 if (attr == NULL)
13471 {
13472 /* This was a pre-DWARF-5 GNU extension alias
13473 for DW_AT_call_return_pc. */
13474 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13475 }
13476 if (!attr)
13477 {
13478 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13479 "DIE %s [in module %s]"),
13480 sect_offset_str (die->sect_off), objfile_name (objfile));
13481 return;
13482 }
13483 pc = attr->as_address () + baseaddr;
13484 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13485
13486 if (cu->call_site_htab == NULL)
13487 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13488 NULL, &objfile->objfile_obstack,
13489 hashtab_obstack_allocate, NULL);
13490 call_site_local.pc = pc;
13491 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13492 if (*slot != NULL)
13493 {
13494 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13495 "DIE %s [in module %s]"),
13496 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13497 objfile_name (objfile));
13498 return;
13499 }
13500
13501 /* Count parameters at the caller. */
13502
13503 nparams = 0;
13504 for (child_die = die->child; child_die && child_die->tag;
13505 child_die = child_die->sibling)
13506 {
13507 if (child_die->tag != DW_TAG_call_site_parameter
13508 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13509 {
13510 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13511 "DW_TAG_call_site child DIE %s [in module %s]"),
13512 child_die->tag, sect_offset_str (child_die->sect_off),
13513 objfile_name (objfile));
13514 continue;
13515 }
13516
13517 nparams++;
13518 }
13519
13520 call_site
13521 = ((struct call_site *)
13522 obstack_alloc (&objfile->objfile_obstack,
13523 sizeof (*call_site)
13524 + (sizeof (*call_site->parameter) * (nparams - 1))));
13525 *slot = call_site;
13526 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13527 call_site->pc = pc;
13528
13529 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13530 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13531 {
13532 struct die_info *func_die;
13533
13534 /* Skip also over DW_TAG_inlined_subroutine. */
13535 for (func_die = die->parent;
13536 func_die && func_die->tag != DW_TAG_subprogram
13537 && func_die->tag != DW_TAG_subroutine_type;
13538 func_die = func_die->parent);
13539
13540 /* DW_AT_call_all_calls is a superset
13541 of DW_AT_call_all_tail_calls. */
13542 if (func_die
13543 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13544 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13545 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13546 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13547 {
13548 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13549 not complete. But keep CALL_SITE for look ups via call_site_htab,
13550 both the initial caller containing the real return address PC and
13551 the final callee containing the current PC of a chain of tail
13552 calls do not need to have the tail call list complete. But any
13553 function candidate for a virtual tail call frame searched via
13554 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13555 determined unambiguously. */
13556 }
13557 else
13558 {
13559 struct type *func_type = NULL;
13560
13561 if (func_die)
13562 func_type = get_die_type (func_die, cu);
13563 if (func_type != NULL)
13564 {
13565 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13566
13567 /* Enlist this call site to the function. */
13568 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13569 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13570 }
13571 else
13572 complaint (_("Cannot find function owning DW_TAG_call_site "
13573 "DIE %s [in module %s]"),
13574 sect_offset_str (die->sect_off), objfile_name (objfile));
13575 }
13576 }
13577
13578 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13579 if (attr == NULL)
13580 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13581 if (attr == NULL)
13582 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13583 if (attr == NULL)
13584 {
13585 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13586 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13587 }
13588 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13589 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13590 /* Keep NULL DWARF_BLOCK. */;
13591 else if (attr->form_is_block ())
13592 {
13593 struct dwarf2_locexpr_baton *dlbaton;
13594 struct dwarf_block *block = attr->as_block ();
13595
13596 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13597 dlbaton->data = block->data;
13598 dlbaton->size = block->size;
13599 dlbaton->per_objfile = per_objfile;
13600 dlbaton->per_cu = cu->per_cu;
13601
13602 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13603 }
13604 else if (attr->form_is_ref ())
13605 {
13606 struct dwarf2_cu *target_cu = cu;
13607 struct die_info *target_die;
13608
13609 target_die = follow_die_ref (die, attr, &target_cu);
13610 gdb_assert (target_cu->per_objfile->objfile == objfile);
13611 if (die_is_declaration (target_die, target_cu))
13612 {
13613 const char *target_physname;
13614
13615 /* Prefer the mangled name; otherwise compute the demangled one. */
13616 target_physname = dw2_linkage_name (target_die, target_cu);
13617 if (target_physname == NULL)
13618 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13619 if (target_physname == NULL)
13620 complaint (_("DW_AT_call_target target DIE has invalid "
13621 "physname, for referencing DIE %s [in module %s]"),
13622 sect_offset_str (die->sect_off), objfile_name (objfile));
13623 else
13624 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13625 }
13626 else
13627 {
13628 CORE_ADDR lowpc;
13629
13630 /* DW_AT_entry_pc should be preferred. */
13631 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13632 <= PC_BOUNDS_INVALID)
13633 complaint (_("DW_AT_call_target target DIE has invalid "
13634 "low pc, for referencing DIE %s [in module %s]"),
13635 sect_offset_str (die->sect_off), objfile_name (objfile));
13636 else
13637 {
13638 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13639 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13640 }
13641 }
13642 }
13643 else
13644 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13645 "block nor reference, for DIE %s [in module %s]"),
13646 sect_offset_str (die->sect_off), objfile_name (objfile));
13647
13648 call_site->per_cu = cu->per_cu;
13649 call_site->per_objfile = per_objfile;
13650
13651 for (child_die = die->child;
13652 child_die && child_die->tag;
13653 child_die = child_die->sibling)
13654 {
13655 struct call_site_parameter *parameter;
13656 struct attribute *loc, *origin;
13657
13658 if (child_die->tag != DW_TAG_call_site_parameter
13659 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13660 {
13661 /* Already printed the complaint above. */
13662 continue;
13663 }
13664
13665 gdb_assert (call_site->parameter_count < nparams);
13666 parameter = &call_site->parameter[call_site->parameter_count];
13667
13668 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13669 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13670 register is contained in DW_AT_call_value. */
13671
13672 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13673 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13674 if (origin == NULL)
13675 {
13676 /* This was a pre-DWARF-5 GNU extension alias
13677 for DW_AT_call_parameter. */
13678 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13679 }
13680 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13681 {
13682 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13683
13684 sect_offset sect_off = origin->get_ref_die_offset ();
13685 if (!cu->header.offset_in_cu_p (sect_off))
13686 {
13687 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13688 binding can be done only inside one CU. Such referenced DIE
13689 therefore cannot be even moved to DW_TAG_partial_unit. */
13690 complaint (_("DW_AT_call_parameter offset is not in CU for "
13691 "DW_TAG_call_site child DIE %s [in module %s]"),
13692 sect_offset_str (child_die->sect_off),
13693 objfile_name (objfile));
13694 continue;
13695 }
13696 parameter->u.param_cu_off
13697 = (cu_offset) (sect_off - cu->header.sect_off);
13698 }
13699 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13700 {
13701 complaint (_("No DW_FORM_block* DW_AT_location for "
13702 "DW_TAG_call_site child DIE %s [in module %s]"),
13703 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13704 continue;
13705 }
13706 else
13707 {
13708 struct dwarf_block *block = loc->as_block ();
13709
13710 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13711 (block->data, &block->data[block->size]);
13712 if (parameter->u.dwarf_reg != -1)
13713 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13714 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13715 &block->data[block->size],
13716 &parameter->u.fb_offset))
13717 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13718 else
13719 {
13720 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13721 "for DW_FORM_block* DW_AT_location is supported for "
13722 "DW_TAG_call_site child DIE %s "
13723 "[in module %s]"),
13724 sect_offset_str (child_die->sect_off),
13725 objfile_name (objfile));
13726 continue;
13727 }
13728 }
13729
13730 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13731 if (attr == NULL)
13732 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13733 if (attr == NULL || !attr->form_is_block ())
13734 {
13735 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13736 "DW_TAG_call_site child DIE %s [in module %s]"),
13737 sect_offset_str (child_die->sect_off),
13738 objfile_name (objfile));
13739 continue;
13740 }
13741
13742 struct dwarf_block *block = attr->as_block ();
13743 parameter->value = block->data;
13744 parameter->value_size = block->size;
13745
13746 /* Parameters are not pre-cleared by memset above. */
13747 parameter->data_value = NULL;
13748 parameter->data_value_size = 0;
13749 call_site->parameter_count++;
13750
13751 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13752 if (attr == NULL)
13753 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13754 if (attr != nullptr)
13755 {
13756 if (!attr->form_is_block ())
13757 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13758 "DW_TAG_call_site child DIE %s [in module %s]"),
13759 sect_offset_str (child_die->sect_off),
13760 objfile_name (objfile));
13761 else
13762 {
13763 block = attr->as_block ();
13764 parameter->data_value = block->data;
13765 parameter->data_value_size = block->size;
13766 }
13767 }
13768 }
13769 }
13770
13771 /* Helper function for read_variable. If DIE represents a virtual
13772 table, then return the type of the concrete object that is
13773 associated with the virtual table. Otherwise, return NULL. */
13774
13775 static struct type *
13776 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13777 {
13778 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13779 if (attr == NULL)
13780 return NULL;
13781
13782 /* Find the type DIE. */
13783 struct die_info *type_die = NULL;
13784 struct dwarf2_cu *type_cu = cu;
13785
13786 if (attr->form_is_ref ())
13787 type_die = follow_die_ref (die, attr, &type_cu);
13788 if (type_die == NULL)
13789 return NULL;
13790
13791 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13792 return NULL;
13793 return die_containing_type (type_die, type_cu);
13794 }
13795
13796 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13797
13798 static void
13799 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13800 {
13801 struct rust_vtable_symbol *storage = NULL;
13802
13803 if (cu->language == language_rust)
13804 {
13805 struct type *containing_type = rust_containing_type (die, cu);
13806
13807 if (containing_type != NULL)
13808 {
13809 struct objfile *objfile = cu->per_objfile->objfile;
13810
13811 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13812 storage->concrete_type = containing_type;
13813 storage->subclass = SYMBOL_RUST_VTABLE;
13814 }
13815 }
13816
13817 struct symbol *res = new_symbol (die, NULL, cu, storage);
13818 struct attribute *abstract_origin
13819 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13820 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13821 if (res == NULL && loc && abstract_origin)
13822 {
13823 /* We have a variable without a name, but with a location and an abstract
13824 origin. This may be a concrete instance of an abstract variable
13825 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13826 later. */
13827 struct dwarf2_cu *origin_cu = cu;
13828 struct die_info *origin_die
13829 = follow_die_ref (die, abstract_origin, &origin_cu);
13830 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13831 per_objfile->per_bfd->abstract_to_concrete
13832 [origin_die->sect_off].push_back (die->sect_off);
13833 }
13834 }
13835
13836 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13837 reading .debug_rnglists.
13838 Callback's type should be:
13839 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13840 Return true if the attributes are present and valid, otherwise,
13841 return false. */
13842
13843 template <typename Callback>
13844 static bool
13845 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13846 dwarf_tag tag, Callback &&callback)
13847 {
13848 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13849 struct objfile *objfile = per_objfile->objfile;
13850 bfd *obfd = objfile->obfd;
13851 /* Base address selection entry. */
13852 gdb::optional<CORE_ADDR> base;
13853 const gdb_byte *buffer;
13854 CORE_ADDR baseaddr;
13855 bool overflow = false;
13856 ULONGEST addr_index;
13857 struct dwarf2_section_info *rnglists_section;
13858
13859 base = cu->base_address;
13860 rnglists_section = cu_debug_rnglists_section (cu, tag);
13861 rnglists_section->read (objfile);
13862
13863 if (offset >= rnglists_section->size)
13864 {
13865 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13866 offset);
13867 return false;
13868 }
13869 buffer = rnglists_section->buffer + offset;
13870
13871 baseaddr = objfile->text_section_offset ();
13872
13873 while (1)
13874 {
13875 /* Initialize it due to a false compiler warning. */
13876 CORE_ADDR range_beginning = 0, range_end = 0;
13877 const gdb_byte *buf_end = (rnglists_section->buffer
13878 + rnglists_section->size);
13879 unsigned int bytes_read;
13880
13881 if (buffer == buf_end)
13882 {
13883 overflow = true;
13884 break;
13885 }
13886 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13887 switch (rlet)
13888 {
13889 case DW_RLE_end_of_list:
13890 break;
13891 case DW_RLE_base_address:
13892 if (buffer + cu->header.addr_size > buf_end)
13893 {
13894 overflow = true;
13895 break;
13896 }
13897 base = cu->header.read_address (obfd, buffer, &bytes_read);
13898 buffer += bytes_read;
13899 break;
13900 case DW_RLE_base_addressx:
13901 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13902 buffer += bytes_read;
13903 base = read_addr_index (cu, addr_index);
13904 break;
13905 case DW_RLE_start_length:
13906 if (buffer + cu->header.addr_size > buf_end)
13907 {
13908 overflow = true;
13909 break;
13910 }
13911 range_beginning = cu->header.read_address (obfd, buffer,
13912 &bytes_read);
13913 buffer += bytes_read;
13914 range_end = (range_beginning
13915 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13916 buffer += bytes_read;
13917 if (buffer > buf_end)
13918 {
13919 overflow = true;
13920 break;
13921 }
13922 break;
13923 case DW_RLE_startx_length:
13924 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13925 buffer += bytes_read;
13926 range_beginning = read_addr_index (cu, addr_index);
13927 if (buffer > buf_end)
13928 {
13929 overflow = true;
13930 break;
13931 }
13932 range_end = (range_beginning
13933 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13934 buffer += bytes_read;
13935 break;
13936 case DW_RLE_offset_pair:
13937 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13938 buffer += bytes_read;
13939 if (buffer > buf_end)
13940 {
13941 overflow = true;
13942 break;
13943 }
13944 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13945 buffer += bytes_read;
13946 if (buffer > buf_end)
13947 {
13948 overflow = true;
13949 break;
13950 }
13951 break;
13952 case DW_RLE_start_end:
13953 if (buffer + 2 * cu->header.addr_size > buf_end)
13954 {
13955 overflow = true;
13956 break;
13957 }
13958 range_beginning = cu->header.read_address (obfd, buffer,
13959 &bytes_read);
13960 buffer += bytes_read;
13961 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13962 buffer += bytes_read;
13963 break;
13964 case DW_RLE_startx_endx:
13965 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13966 buffer += bytes_read;
13967 range_beginning = read_addr_index (cu, addr_index);
13968 if (buffer > buf_end)
13969 {
13970 overflow = true;
13971 break;
13972 }
13973 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13974 buffer += bytes_read;
13975 range_end = read_addr_index (cu, addr_index);
13976 break;
13977 default:
13978 complaint (_("Invalid .debug_rnglists data (no base address)"));
13979 return false;
13980 }
13981 if (rlet == DW_RLE_end_of_list || overflow)
13982 break;
13983 if (rlet == DW_RLE_base_address)
13984 continue;
13985
13986 if (range_beginning > range_end)
13987 {
13988 /* Inverted range entries are invalid. */
13989 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13990 return false;
13991 }
13992
13993 /* Empty range entries have no effect. */
13994 if (range_beginning == range_end)
13995 continue;
13996
13997 /* Only DW_RLE_offset_pair needs the base address added. */
13998 if (rlet == DW_RLE_offset_pair)
13999 {
14000 if (!base.has_value ())
14001 {
14002 /* We have no valid base address for the DW_RLE_offset_pair. */
14003 complaint (_("Invalid .debug_rnglists data (no base address for "
14004 "DW_RLE_offset_pair)"));
14005 return false;
14006 }
14007
14008 range_beginning += *base;
14009 range_end += *base;
14010 }
14011
14012 /* A not-uncommon case of bad debug info.
14013 Don't pollute the addrmap with bad data. */
14014 if (range_beginning + baseaddr == 0
14015 && !per_objfile->per_bfd->has_section_at_zero)
14016 {
14017 complaint (_(".debug_rnglists entry has start address of zero"
14018 " [in module %s]"), objfile_name (objfile));
14019 continue;
14020 }
14021
14022 callback (range_beginning, range_end);
14023 }
14024
14025 if (overflow)
14026 {
14027 complaint (_("Offset %d is not terminated "
14028 "for DW_AT_ranges attribute"),
14029 offset);
14030 return false;
14031 }
14032
14033 return true;
14034 }
14035
14036 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14037 Callback's type should be:
14038 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14039 Return 1 if the attributes are present and valid, otherwise, return 0. */
14040
14041 template <typename Callback>
14042 static int
14043 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14044 Callback &&callback)
14045 {
14046 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14047 struct objfile *objfile = per_objfile->objfile;
14048 struct comp_unit_head *cu_header = &cu->header;
14049 bfd *obfd = objfile->obfd;
14050 unsigned int addr_size = cu_header->addr_size;
14051 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14052 /* Base address selection entry. */
14053 gdb::optional<CORE_ADDR> base;
14054 unsigned int dummy;
14055 const gdb_byte *buffer;
14056 CORE_ADDR baseaddr;
14057
14058 if (cu_header->version >= 5)
14059 return dwarf2_rnglists_process (offset, cu, tag, callback);
14060
14061 base = cu->base_address;
14062
14063 per_objfile->per_bfd->ranges.read (objfile);
14064 if (offset >= per_objfile->per_bfd->ranges.size)
14065 {
14066 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14067 offset);
14068 return 0;
14069 }
14070 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14071
14072 baseaddr = objfile->text_section_offset ();
14073
14074 while (1)
14075 {
14076 CORE_ADDR range_beginning, range_end;
14077
14078 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14079 buffer += addr_size;
14080 range_end = cu->header.read_address (obfd, buffer, &dummy);
14081 buffer += addr_size;
14082 offset += 2 * addr_size;
14083
14084 /* An end of list marker is a pair of zero addresses. */
14085 if (range_beginning == 0 && range_end == 0)
14086 /* Found the end of list entry. */
14087 break;
14088
14089 /* Each base address selection entry is a pair of 2 values.
14090 The first is the largest possible address, the second is
14091 the base address. Check for a base address here. */
14092 if ((range_beginning & mask) == mask)
14093 {
14094 /* If we found the largest possible address, then we already
14095 have the base address in range_end. */
14096 base = range_end;
14097 continue;
14098 }
14099
14100 if (!base.has_value ())
14101 {
14102 /* We have no valid base address for the ranges
14103 data. */
14104 complaint (_("Invalid .debug_ranges data (no base address)"));
14105 return 0;
14106 }
14107
14108 if (range_beginning > range_end)
14109 {
14110 /* Inverted range entries are invalid. */
14111 complaint (_("Invalid .debug_ranges data (inverted range)"));
14112 return 0;
14113 }
14114
14115 /* Empty range entries have no effect. */
14116 if (range_beginning == range_end)
14117 continue;
14118
14119 range_beginning += *base;
14120 range_end += *base;
14121
14122 /* A not-uncommon case of bad debug info.
14123 Don't pollute the addrmap with bad data. */
14124 if (range_beginning + baseaddr == 0
14125 && !per_objfile->per_bfd->has_section_at_zero)
14126 {
14127 complaint (_(".debug_ranges entry has start address of zero"
14128 " [in module %s]"), objfile_name (objfile));
14129 continue;
14130 }
14131
14132 callback (range_beginning, range_end);
14133 }
14134
14135 return 1;
14136 }
14137
14138 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14139 Return 1 if the attributes are present and valid, otherwise, return 0.
14140 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14141
14142 static int
14143 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14144 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14145 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14146 {
14147 struct objfile *objfile = cu->per_objfile->objfile;
14148 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14149 struct gdbarch *gdbarch = objfile->arch ();
14150 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14151 int low_set = 0;
14152 CORE_ADDR low = 0;
14153 CORE_ADDR high = 0;
14154 int retval;
14155
14156 retval = dwarf2_ranges_process (offset, cu, tag,
14157 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14158 {
14159 if (ranges_pst != NULL)
14160 {
14161 CORE_ADDR lowpc;
14162 CORE_ADDR highpc;
14163
14164 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14165 range_beginning + baseaddr)
14166 - baseaddr);
14167 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14168 range_end + baseaddr)
14169 - baseaddr);
14170 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14171 lowpc, highpc - 1, ranges_pst);
14172 }
14173
14174 /* FIXME: This is recording everything as a low-high
14175 segment of consecutive addresses. We should have a
14176 data structure for discontiguous block ranges
14177 instead. */
14178 if (! low_set)
14179 {
14180 low = range_beginning;
14181 high = range_end;
14182 low_set = 1;
14183 }
14184 else
14185 {
14186 if (range_beginning < low)
14187 low = range_beginning;
14188 if (range_end > high)
14189 high = range_end;
14190 }
14191 });
14192 if (!retval)
14193 return 0;
14194
14195 if (! low_set)
14196 /* If the first entry is an end-of-list marker, the range
14197 describes an empty scope, i.e. no instructions. */
14198 return 0;
14199
14200 if (low_return)
14201 *low_return = low;
14202 if (high_return)
14203 *high_return = high;
14204 return 1;
14205 }
14206
14207 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14208 definition for the return value. *LOWPC and *HIGHPC are set iff
14209 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14210
14211 static enum pc_bounds_kind
14212 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14213 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14214 dwarf2_psymtab *pst)
14215 {
14216 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14217 struct attribute *attr;
14218 struct attribute *attr_high;
14219 CORE_ADDR low = 0;
14220 CORE_ADDR high = 0;
14221 enum pc_bounds_kind ret;
14222
14223 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14224 if (attr_high)
14225 {
14226 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14227 if (attr != nullptr)
14228 {
14229 low = attr->as_address ();
14230 high = attr_high->as_address ();
14231 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14232 high += low;
14233 }
14234 else
14235 /* Found high w/o low attribute. */
14236 return PC_BOUNDS_INVALID;
14237
14238 /* Found consecutive range of addresses. */
14239 ret = PC_BOUNDS_HIGH_LOW;
14240 }
14241 else
14242 {
14243 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14244 if (attr != nullptr && attr->form_is_unsigned ())
14245 {
14246 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14247 on DWARF version). */
14248 ULONGEST ranges_offset = attr->as_unsigned ();
14249
14250 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14251 this value. */
14252 if (die->tag != DW_TAG_compile_unit)
14253 ranges_offset += cu->gnu_ranges_base;
14254
14255 /* Value of the DW_AT_ranges attribute is the offset in the
14256 .debug_ranges section. */
14257 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14258 die->tag))
14259 return PC_BOUNDS_INVALID;
14260 /* Found discontinuous range of addresses. */
14261 ret = PC_BOUNDS_RANGES;
14262 }
14263 else
14264 return PC_BOUNDS_NOT_PRESENT;
14265 }
14266
14267 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14268 if (high <= low)
14269 return PC_BOUNDS_INVALID;
14270
14271 /* When using the GNU linker, .gnu.linkonce. sections are used to
14272 eliminate duplicate copies of functions and vtables and such.
14273 The linker will arbitrarily choose one and discard the others.
14274 The AT_*_pc values for such functions refer to local labels in
14275 these sections. If the section from that file was discarded, the
14276 labels are not in the output, so the relocs get a value of 0.
14277 If this is a discarded function, mark the pc bounds as invalid,
14278 so that GDB will ignore it. */
14279 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14280 return PC_BOUNDS_INVALID;
14281
14282 *lowpc = low;
14283 if (highpc)
14284 *highpc = high;
14285 return ret;
14286 }
14287
14288 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14289 its low and high PC addresses. Do nothing if these addresses could not
14290 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14291 and HIGHPC to the high address if greater than HIGHPC. */
14292
14293 static void
14294 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14295 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14296 struct dwarf2_cu *cu)
14297 {
14298 CORE_ADDR low, high;
14299 struct die_info *child = die->child;
14300
14301 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14302 {
14303 *lowpc = std::min (*lowpc, low);
14304 *highpc = std::max (*highpc, high);
14305 }
14306
14307 /* If the language does not allow nested subprograms (either inside
14308 subprograms or lexical blocks), we're done. */
14309 if (cu->language != language_ada)
14310 return;
14311
14312 /* Check all the children of the given DIE. If it contains nested
14313 subprograms, then check their pc bounds. Likewise, we need to
14314 check lexical blocks as well, as they may also contain subprogram
14315 definitions. */
14316 while (child && child->tag)
14317 {
14318 if (child->tag == DW_TAG_subprogram
14319 || child->tag == DW_TAG_lexical_block)
14320 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14321 child = child->sibling;
14322 }
14323 }
14324
14325 /* Get the low and high pc's represented by the scope DIE, and store
14326 them in *LOWPC and *HIGHPC. If the correct values can't be
14327 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14328
14329 static void
14330 get_scope_pc_bounds (struct die_info *die,
14331 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14332 struct dwarf2_cu *cu)
14333 {
14334 CORE_ADDR best_low = (CORE_ADDR) -1;
14335 CORE_ADDR best_high = (CORE_ADDR) 0;
14336 CORE_ADDR current_low, current_high;
14337
14338 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14339 >= PC_BOUNDS_RANGES)
14340 {
14341 best_low = current_low;
14342 best_high = current_high;
14343 }
14344 else
14345 {
14346 struct die_info *child = die->child;
14347
14348 while (child && child->tag)
14349 {
14350 switch (child->tag) {
14351 case DW_TAG_subprogram:
14352 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14353 break;
14354 case DW_TAG_namespace:
14355 case DW_TAG_module:
14356 /* FIXME: carlton/2004-01-16: Should we do this for
14357 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14358 that current GCC's always emit the DIEs corresponding
14359 to definitions of methods of classes as children of a
14360 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14361 the DIEs giving the declarations, which could be
14362 anywhere). But I don't see any reason why the
14363 standards says that they have to be there. */
14364 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14365
14366 if (current_low != ((CORE_ADDR) -1))
14367 {
14368 best_low = std::min (best_low, current_low);
14369 best_high = std::max (best_high, current_high);
14370 }
14371 break;
14372 default:
14373 /* Ignore. */
14374 break;
14375 }
14376
14377 child = child->sibling;
14378 }
14379 }
14380
14381 *lowpc = best_low;
14382 *highpc = best_high;
14383 }
14384
14385 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14386 in DIE. */
14387
14388 static void
14389 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14390 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14391 {
14392 struct objfile *objfile = cu->per_objfile->objfile;
14393 struct gdbarch *gdbarch = objfile->arch ();
14394 struct attribute *attr;
14395 struct attribute *attr_high;
14396
14397 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14398 if (attr_high)
14399 {
14400 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14401 if (attr != nullptr)
14402 {
14403 CORE_ADDR low = attr->as_address ();
14404 CORE_ADDR high = attr_high->as_address ();
14405
14406 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14407 high += low;
14408
14409 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14410 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14411 cu->get_builder ()->record_block_range (block, low, high - 1);
14412 }
14413 }
14414
14415 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14416 if (attr != nullptr && attr->form_is_unsigned ())
14417 {
14418 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14419 on DWARF version). */
14420 ULONGEST ranges_offset = attr->as_unsigned ();
14421
14422 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14423 this value. */
14424 if (die->tag != DW_TAG_compile_unit)
14425 ranges_offset += cu->gnu_ranges_base;
14426
14427 std::vector<blockrange> blockvec;
14428 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14429 [&] (CORE_ADDR start, CORE_ADDR end)
14430 {
14431 start += baseaddr;
14432 end += baseaddr;
14433 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14434 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14435 cu->get_builder ()->record_block_range (block, start, end - 1);
14436 blockvec.emplace_back (start, end);
14437 });
14438
14439 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14440 }
14441 }
14442
14443 /* Check whether the producer field indicates either of GCC < 4.6, or the
14444 Intel C/C++ compiler, and cache the result in CU. */
14445
14446 static void
14447 check_producer (struct dwarf2_cu *cu)
14448 {
14449 int major, minor;
14450
14451 if (cu->producer == NULL)
14452 {
14453 /* For unknown compilers expect their behavior is DWARF version
14454 compliant.
14455
14456 GCC started to support .debug_types sections by -gdwarf-4 since
14457 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14458 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14459 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14460 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14461 }
14462 else if (producer_is_gcc (cu->producer, &major, &minor))
14463 {
14464 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14465 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14466 }
14467 else if (producer_is_icc (cu->producer, &major, &minor))
14468 {
14469 cu->producer_is_icc = true;
14470 cu->producer_is_icc_lt_14 = major < 14;
14471 }
14472 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14473 cu->producer_is_codewarrior = true;
14474 else
14475 {
14476 /* For other non-GCC compilers, expect their behavior is DWARF version
14477 compliant. */
14478 }
14479
14480 cu->checked_producer = true;
14481 }
14482
14483 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14484 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14485 during 4.6.0 experimental. */
14486
14487 static bool
14488 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14489 {
14490 if (!cu->checked_producer)
14491 check_producer (cu);
14492
14493 return cu->producer_is_gxx_lt_4_6;
14494 }
14495
14496
14497 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14498 with incorrect is_stmt attributes. */
14499
14500 static bool
14501 producer_is_codewarrior (struct dwarf2_cu *cu)
14502 {
14503 if (!cu->checked_producer)
14504 check_producer (cu);
14505
14506 return cu->producer_is_codewarrior;
14507 }
14508
14509 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14510 If that attribute is not available, return the appropriate
14511 default. */
14512
14513 static enum dwarf_access_attribute
14514 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14515 {
14516 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14517 if (attr != nullptr)
14518 {
14519 LONGEST value = attr->constant_value (-1);
14520 if (value == DW_ACCESS_public
14521 || value == DW_ACCESS_protected
14522 || value == DW_ACCESS_private)
14523 return (dwarf_access_attribute) value;
14524 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14525 plongest (value));
14526 }
14527
14528 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14529 {
14530 /* The default DWARF 2 accessibility for members is public, the default
14531 accessibility for inheritance is private. */
14532
14533 if (die->tag != DW_TAG_inheritance)
14534 return DW_ACCESS_public;
14535 else
14536 return DW_ACCESS_private;
14537 }
14538 else
14539 {
14540 /* DWARF 3+ defines the default accessibility a different way. The same
14541 rules apply now for DW_TAG_inheritance as for the members and it only
14542 depends on the container kind. */
14543
14544 if (die->parent->tag == DW_TAG_class_type)
14545 return DW_ACCESS_private;
14546 else
14547 return DW_ACCESS_public;
14548 }
14549 }
14550
14551 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14552 offset. If the attribute was not found return 0, otherwise return
14553 1. If it was found but could not properly be handled, set *OFFSET
14554 to 0. */
14555
14556 static int
14557 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14558 LONGEST *offset)
14559 {
14560 struct attribute *attr;
14561
14562 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14563 if (attr != NULL)
14564 {
14565 *offset = 0;
14566
14567 /* Note that we do not check for a section offset first here.
14568 This is because DW_AT_data_member_location is new in DWARF 4,
14569 so if we see it, we can assume that a constant form is really
14570 a constant and not a section offset. */
14571 if (attr->form_is_constant ())
14572 *offset = attr->constant_value (0);
14573 else if (attr->form_is_section_offset ())
14574 dwarf2_complex_location_expr_complaint ();
14575 else if (attr->form_is_block ())
14576 *offset = decode_locdesc (attr->as_block (), cu);
14577 else
14578 dwarf2_complex_location_expr_complaint ();
14579
14580 return 1;
14581 }
14582
14583 return 0;
14584 }
14585
14586 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14587
14588 static void
14589 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14590 struct field *field)
14591 {
14592 struct attribute *attr;
14593
14594 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14595 if (attr != NULL)
14596 {
14597 if (attr->form_is_constant ())
14598 {
14599 LONGEST offset = attr->constant_value (0);
14600 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14601 }
14602 else if (attr->form_is_section_offset ())
14603 dwarf2_complex_location_expr_complaint ();
14604 else if (attr->form_is_block ())
14605 {
14606 bool handled;
14607 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14608 if (handled)
14609 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14610 else
14611 {
14612 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14613 struct objfile *objfile = per_objfile->objfile;
14614 struct dwarf2_locexpr_baton *dlbaton
14615 = XOBNEW (&objfile->objfile_obstack,
14616 struct dwarf2_locexpr_baton);
14617 dlbaton->data = attr->as_block ()->data;
14618 dlbaton->size = attr->as_block ()->size;
14619 /* When using this baton, we want to compute the address
14620 of the field, not the value. This is why
14621 is_reference is set to false here. */
14622 dlbaton->is_reference = false;
14623 dlbaton->per_objfile = per_objfile;
14624 dlbaton->per_cu = cu->per_cu;
14625
14626 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14627 }
14628 }
14629 else
14630 dwarf2_complex_location_expr_complaint ();
14631 }
14632 }
14633
14634 /* Add an aggregate field to the field list. */
14635
14636 static void
14637 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14638 struct dwarf2_cu *cu)
14639 {
14640 struct objfile *objfile = cu->per_objfile->objfile;
14641 struct gdbarch *gdbarch = objfile->arch ();
14642 struct nextfield *new_field;
14643 struct attribute *attr;
14644 struct field *fp;
14645 const char *fieldname = "";
14646
14647 if (die->tag == DW_TAG_inheritance)
14648 {
14649 fip->baseclasses.emplace_back ();
14650 new_field = &fip->baseclasses.back ();
14651 }
14652 else
14653 {
14654 fip->fields.emplace_back ();
14655 new_field = &fip->fields.back ();
14656 }
14657
14658 new_field->offset = die->sect_off;
14659
14660 new_field->accessibility = dwarf2_access_attribute (die, cu);
14661 if (new_field->accessibility != DW_ACCESS_public)
14662 fip->non_public_fields = true;
14663
14664 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14665 if (attr != nullptr)
14666 new_field->virtuality = attr->as_virtuality ();
14667 else
14668 new_field->virtuality = DW_VIRTUALITY_none;
14669
14670 fp = &new_field->field;
14671
14672 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14673 {
14674 /* Data member other than a C++ static data member. */
14675
14676 /* Get type of field. */
14677 fp->set_type (die_type (die, cu));
14678
14679 SET_FIELD_BITPOS (*fp, 0);
14680
14681 /* Get bit size of field (zero if none). */
14682 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14683 if (attr != nullptr)
14684 {
14685 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14686 }
14687 else
14688 {
14689 FIELD_BITSIZE (*fp) = 0;
14690 }
14691
14692 /* Get bit offset of field. */
14693 handle_data_member_location (die, cu, fp);
14694 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14695 if (attr != nullptr && attr->form_is_constant ())
14696 {
14697 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14698 {
14699 /* For big endian bits, the DW_AT_bit_offset gives the
14700 additional bit offset from the MSB of the containing
14701 anonymous object to the MSB of the field. We don't
14702 have to do anything special since we don't need to
14703 know the size of the anonymous object. */
14704 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14705 + attr->constant_value (0)));
14706 }
14707 else
14708 {
14709 /* For little endian bits, compute the bit offset to the
14710 MSB of the anonymous object, subtract off the number of
14711 bits from the MSB of the field to the MSB of the
14712 object, and then subtract off the number of bits of
14713 the field itself. The result is the bit offset of
14714 the LSB of the field. */
14715 int anonymous_size;
14716 int bit_offset = attr->constant_value (0);
14717
14718 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14719 if (attr != nullptr && attr->form_is_constant ())
14720 {
14721 /* The size of the anonymous object containing
14722 the bit field is explicit, so use the
14723 indicated size (in bytes). */
14724 anonymous_size = attr->constant_value (0);
14725 }
14726 else
14727 {
14728 /* The size of the anonymous object containing
14729 the bit field must be inferred from the type
14730 attribute of the data member containing the
14731 bit field. */
14732 anonymous_size = TYPE_LENGTH (fp->type ());
14733 }
14734 SET_FIELD_BITPOS (*fp,
14735 (FIELD_BITPOS (*fp)
14736 + anonymous_size * bits_per_byte
14737 - bit_offset - FIELD_BITSIZE (*fp)));
14738 }
14739 }
14740 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14741 if (attr != NULL)
14742 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14743 + attr->constant_value (0)));
14744
14745 /* Get name of field. */
14746 fieldname = dwarf2_name (die, cu);
14747 if (fieldname == NULL)
14748 fieldname = "";
14749
14750 /* The name is already allocated along with this objfile, so we don't
14751 need to duplicate it for the type. */
14752 fp->name = fieldname;
14753
14754 /* Change accessibility for artificial fields (e.g. virtual table
14755 pointer or virtual base class pointer) to private. */
14756 if (dwarf2_attr (die, DW_AT_artificial, cu))
14757 {
14758 FIELD_ARTIFICIAL (*fp) = 1;
14759 new_field->accessibility = DW_ACCESS_private;
14760 fip->non_public_fields = true;
14761 }
14762 }
14763 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14764 {
14765 /* C++ static member. */
14766
14767 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14768 is a declaration, but all versions of G++ as of this writing
14769 (so through at least 3.2.1) incorrectly generate
14770 DW_TAG_variable tags. */
14771
14772 const char *physname;
14773
14774 /* Get name of field. */
14775 fieldname = dwarf2_name (die, cu);
14776 if (fieldname == NULL)
14777 return;
14778
14779 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14780 if (attr
14781 /* Only create a symbol if this is an external value.
14782 new_symbol checks this and puts the value in the global symbol
14783 table, which we want. If it is not external, new_symbol
14784 will try to put the value in cu->list_in_scope which is wrong. */
14785 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14786 {
14787 /* A static const member, not much different than an enum as far as
14788 we're concerned, except that we can support more types. */
14789 new_symbol (die, NULL, cu);
14790 }
14791
14792 /* Get physical name. */
14793 physname = dwarf2_physname (fieldname, die, cu);
14794
14795 /* The name is already allocated along with this objfile, so we don't
14796 need to duplicate it for the type. */
14797 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14798 fp->set_type (die_type (die, cu));
14799 FIELD_NAME (*fp) = fieldname;
14800 }
14801 else if (die->tag == DW_TAG_inheritance)
14802 {
14803 /* C++ base class field. */
14804 handle_data_member_location (die, cu, fp);
14805 FIELD_BITSIZE (*fp) = 0;
14806 fp->set_type (die_type (die, cu));
14807 FIELD_NAME (*fp) = fp->type ()->name ();
14808 }
14809 else
14810 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14811 }
14812
14813 /* Can the type given by DIE define another type? */
14814
14815 static bool
14816 type_can_define_types (const struct die_info *die)
14817 {
14818 switch (die->tag)
14819 {
14820 case DW_TAG_typedef:
14821 case DW_TAG_class_type:
14822 case DW_TAG_structure_type:
14823 case DW_TAG_union_type:
14824 case DW_TAG_enumeration_type:
14825 return true;
14826
14827 default:
14828 return false;
14829 }
14830 }
14831
14832 /* Add a type definition defined in the scope of the FIP's class. */
14833
14834 static void
14835 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14836 struct dwarf2_cu *cu)
14837 {
14838 struct decl_field fp;
14839 memset (&fp, 0, sizeof (fp));
14840
14841 gdb_assert (type_can_define_types (die));
14842
14843 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14844 fp.name = dwarf2_name (die, cu);
14845 fp.type = read_type_die (die, cu);
14846
14847 /* Save accessibility. */
14848 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14849 switch (accessibility)
14850 {
14851 case DW_ACCESS_public:
14852 /* The assumed value if neither private nor protected. */
14853 break;
14854 case DW_ACCESS_private:
14855 fp.is_private = 1;
14856 break;
14857 case DW_ACCESS_protected:
14858 fp.is_protected = 1;
14859 break;
14860 }
14861
14862 if (die->tag == DW_TAG_typedef)
14863 fip->typedef_field_list.push_back (fp);
14864 else
14865 fip->nested_types_list.push_back (fp);
14866 }
14867
14868 /* A convenience typedef that's used when finding the discriminant
14869 field for a variant part. */
14870 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14871 offset_map_type;
14872
14873 /* Compute the discriminant range for a given variant. OBSTACK is
14874 where the results will be stored. VARIANT is the variant to
14875 process. IS_UNSIGNED indicates whether the discriminant is signed
14876 or unsigned. */
14877
14878 static const gdb::array_view<discriminant_range>
14879 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14880 bool is_unsigned)
14881 {
14882 std::vector<discriminant_range> ranges;
14883
14884 if (variant.default_branch)
14885 return {};
14886
14887 if (variant.discr_list_data == nullptr)
14888 {
14889 discriminant_range r
14890 = {variant.discriminant_value, variant.discriminant_value};
14891 ranges.push_back (r);
14892 }
14893 else
14894 {
14895 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14896 variant.discr_list_data->size);
14897 while (!data.empty ())
14898 {
14899 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14900 {
14901 complaint (_("invalid discriminant marker: %d"), data[0]);
14902 break;
14903 }
14904 bool is_range = data[0] == DW_DSC_range;
14905 data = data.slice (1);
14906
14907 ULONGEST low, high;
14908 unsigned int bytes_read;
14909
14910 if (data.empty ())
14911 {
14912 complaint (_("DW_AT_discr_list missing low value"));
14913 break;
14914 }
14915 if (is_unsigned)
14916 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14917 else
14918 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14919 &bytes_read);
14920 data = data.slice (bytes_read);
14921
14922 if (is_range)
14923 {
14924 if (data.empty ())
14925 {
14926 complaint (_("DW_AT_discr_list missing high value"));
14927 break;
14928 }
14929 if (is_unsigned)
14930 high = read_unsigned_leb128 (nullptr, data.data (),
14931 &bytes_read);
14932 else
14933 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14934 &bytes_read);
14935 data = data.slice (bytes_read);
14936 }
14937 else
14938 high = low;
14939
14940 ranges.push_back ({ low, high });
14941 }
14942 }
14943
14944 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14945 ranges.size ());
14946 std::copy (ranges.begin (), ranges.end (), result);
14947 return gdb::array_view<discriminant_range> (result, ranges.size ());
14948 }
14949
14950 static const gdb::array_view<variant_part> create_variant_parts
14951 (struct obstack *obstack,
14952 const offset_map_type &offset_map,
14953 struct field_info *fi,
14954 const std::vector<variant_part_builder> &variant_parts);
14955
14956 /* Fill in a "struct variant" for a given variant field. RESULT is
14957 the variant to fill in. OBSTACK is where any needed allocations
14958 will be done. OFFSET_MAP holds the mapping from section offsets to
14959 fields for the type. FI describes the fields of the type we're
14960 processing. FIELD is the variant field we're converting. */
14961
14962 static void
14963 create_one_variant (variant &result, struct obstack *obstack,
14964 const offset_map_type &offset_map,
14965 struct field_info *fi, const variant_field &field)
14966 {
14967 result.discriminants = convert_variant_range (obstack, field, false);
14968 result.first_field = field.first_field + fi->baseclasses.size ();
14969 result.last_field = field.last_field + fi->baseclasses.size ();
14970 result.parts = create_variant_parts (obstack, offset_map, fi,
14971 field.variant_parts);
14972 }
14973
14974 /* Fill in a "struct variant_part" for a given variant part. RESULT
14975 is the variant part to fill in. OBSTACK is where any needed
14976 allocations will be done. OFFSET_MAP holds the mapping from
14977 section offsets to fields for the type. FI describes the fields of
14978 the type we're processing. BUILDER is the variant part to be
14979 converted. */
14980
14981 static void
14982 create_one_variant_part (variant_part &result,
14983 struct obstack *obstack,
14984 const offset_map_type &offset_map,
14985 struct field_info *fi,
14986 const variant_part_builder &builder)
14987 {
14988 auto iter = offset_map.find (builder.discriminant_offset);
14989 if (iter == offset_map.end ())
14990 {
14991 result.discriminant_index = -1;
14992 /* Doesn't matter. */
14993 result.is_unsigned = false;
14994 }
14995 else
14996 {
14997 result.discriminant_index = iter->second;
14998 result.is_unsigned
14999 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15000 }
15001
15002 size_t n = builder.variants.size ();
15003 variant *output = new (obstack) variant[n];
15004 for (size_t i = 0; i < n; ++i)
15005 create_one_variant (output[i], obstack, offset_map, fi,
15006 builder.variants[i]);
15007
15008 result.variants = gdb::array_view<variant> (output, n);
15009 }
15010
15011 /* Create a vector of variant parts that can be attached to a type.
15012 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15013 holds the mapping from section offsets to fields for the type. FI
15014 describes the fields of the type we're processing. VARIANT_PARTS
15015 is the vector to convert. */
15016
15017 static const gdb::array_view<variant_part>
15018 create_variant_parts (struct obstack *obstack,
15019 const offset_map_type &offset_map,
15020 struct field_info *fi,
15021 const std::vector<variant_part_builder> &variant_parts)
15022 {
15023 if (variant_parts.empty ())
15024 return {};
15025
15026 size_t n = variant_parts.size ();
15027 variant_part *result = new (obstack) variant_part[n];
15028 for (size_t i = 0; i < n; ++i)
15029 create_one_variant_part (result[i], obstack, offset_map, fi,
15030 variant_parts[i]);
15031
15032 return gdb::array_view<variant_part> (result, n);
15033 }
15034
15035 /* Compute the variant part vector for FIP, attaching it to TYPE when
15036 done. */
15037
15038 static void
15039 add_variant_property (struct field_info *fip, struct type *type,
15040 struct dwarf2_cu *cu)
15041 {
15042 /* Map section offsets of fields to their field index. Note the
15043 field index here does not take the number of baseclasses into
15044 account. */
15045 offset_map_type offset_map;
15046 for (int i = 0; i < fip->fields.size (); ++i)
15047 offset_map[fip->fields[i].offset] = i;
15048
15049 struct objfile *objfile = cu->per_objfile->objfile;
15050 gdb::array_view<variant_part> parts
15051 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15052 fip->variant_parts);
15053
15054 struct dynamic_prop prop;
15055 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15056 obstack_copy (&objfile->objfile_obstack, &parts,
15057 sizeof (parts)));
15058
15059 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15060 }
15061
15062 /* Create the vector of fields, and attach it to the type. */
15063
15064 static void
15065 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15066 struct dwarf2_cu *cu)
15067 {
15068 int nfields = fip->nfields ();
15069
15070 /* Record the field count, allocate space for the array of fields,
15071 and create blank accessibility bitfields if necessary. */
15072 type->set_num_fields (nfields);
15073 type->set_fields
15074 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15075
15076 if (fip->non_public_fields && cu->language != language_ada)
15077 {
15078 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15079
15080 TYPE_FIELD_PRIVATE_BITS (type) =
15081 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15082 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15083
15084 TYPE_FIELD_PROTECTED_BITS (type) =
15085 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15086 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15087
15088 TYPE_FIELD_IGNORE_BITS (type) =
15089 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15090 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15091 }
15092
15093 /* If the type has baseclasses, allocate and clear a bit vector for
15094 TYPE_FIELD_VIRTUAL_BITS. */
15095 if (!fip->baseclasses.empty () && cu->language != language_ada)
15096 {
15097 int num_bytes = B_BYTES (fip->baseclasses.size ());
15098 unsigned char *pointer;
15099
15100 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15101 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15102 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15103 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15104 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15105 }
15106
15107 if (!fip->variant_parts.empty ())
15108 add_variant_property (fip, type, cu);
15109
15110 /* Copy the saved-up fields into the field vector. */
15111 for (int i = 0; i < nfields; ++i)
15112 {
15113 struct nextfield &field
15114 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15115 : fip->fields[i - fip->baseclasses.size ()]);
15116
15117 type->field (i) = field.field;
15118 switch (field.accessibility)
15119 {
15120 case DW_ACCESS_private:
15121 if (cu->language != language_ada)
15122 SET_TYPE_FIELD_PRIVATE (type, i);
15123 break;
15124
15125 case DW_ACCESS_protected:
15126 if (cu->language != language_ada)
15127 SET_TYPE_FIELD_PROTECTED (type, i);
15128 break;
15129
15130 case DW_ACCESS_public:
15131 break;
15132
15133 default:
15134 /* Unknown accessibility. Complain and treat it as public. */
15135 {
15136 complaint (_("unsupported accessibility %d"),
15137 field.accessibility);
15138 }
15139 break;
15140 }
15141 if (i < fip->baseclasses.size ())
15142 {
15143 switch (field.virtuality)
15144 {
15145 case DW_VIRTUALITY_virtual:
15146 case DW_VIRTUALITY_pure_virtual:
15147 if (cu->language == language_ada)
15148 error (_("unexpected virtuality in component of Ada type"));
15149 SET_TYPE_FIELD_VIRTUAL (type, i);
15150 break;
15151 }
15152 }
15153 }
15154 }
15155
15156 /* Return true if this member function is a constructor, false
15157 otherwise. */
15158
15159 static int
15160 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15161 {
15162 const char *fieldname;
15163 const char *type_name;
15164 int len;
15165
15166 if (die->parent == NULL)
15167 return 0;
15168
15169 if (die->parent->tag != DW_TAG_structure_type
15170 && die->parent->tag != DW_TAG_union_type
15171 && die->parent->tag != DW_TAG_class_type)
15172 return 0;
15173
15174 fieldname = dwarf2_name (die, cu);
15175 type_name = dwarf2_name (die->parent, cu);
15176 if (fieldname == NULL || type_name == NULL)
15177 return 0;
15178
15179 len = strlen (fieldname);
15180 return (strncmp (fieldname, type_name, len) == 0
15181 && (type_name[len] == '\0' || type_name[len] == '<'));
15182 }
15183
15184 /* Add a member function to the proper fieldlist. */
15185
15186 static void
15187 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15188 struct type *type, struct dwarf2_cu *cu)
15189 {
15190 struct objfile *objfile = cu->per_objfile->objfile;
15191 struct attribute *attr;
15192 int i;
15193 struct fnfieldlist *flp = nullptr;
15194 struct fn_field *fnp;
15195 const char *fieldname;
15196 struct type *this_type;
15197
15198 if (cu->language == language_ada)
15199 error (_("unexpected member function in Ada type"));
15200
15201 /* Get name of member function. */
15202 fieldname = dwarf2_name (die, cu);
15203 if (fieldname == NULL)
15204 return;
15205
15206 /* Look up member function name in fieldlist. */
15207 for (i = 0; i < fip->fnfieldlists.size (); i++)
15208 {
15209 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15210 {
15211 flp = &fip->fnfieldlists[i];
15212 break;
15213 }
15214 }
15215
15216 /* Create a new fnfieldlist if necessary. */
15217 if (flp == nullptr)
15218 {
15219 fip->fnfieldlists.emplace_back ();
15220 flp = &fip->fnfieldlists.back ();
15221 flp->name = fieldname;
15222 i = fip->fnfieldlists.size () - 1;
15223 }
15224
15225 /* Create a new member function field and add it to the vector of
15226 fnfieldlists. */
15227 flp->fnfields.emplace_back ();
15228 fnp = &flp->fnfields.back ();
15229
15230 /* Delay processing of the physname until later. */
15231 if (cu->language == language_cplus)
15232 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15233 die, cu);
15234 else
15235 {
15236 const char *physname = dwarf2_physname (fieldname, die, cu);
15237 fnp->physname = physname ? physname : "";
15238 }
15239
15240 fnp->type = alloc_type (objfile);
15241 this_type = read_type_die (die, cu);
15242 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15243 {
15244 int nparams = this_type->num_fields ();
15245
15246 /* TYPE is the domain of this method, and THIS_TYPE is the type
15247 of the method itself (TYPE_CODE_METHOD). */
15248 smash_to_method_type (fnp->type, type,
15249 TYPE_TARGET_TYPE (this_type),
15250 this_type->fields (),
15251 this_type->num_fields (),
15252 this_type->has_varargs ());
15253
15254 /* Handle static member functions.
15255 Dwarf2 has no clean way to discern C++ static and non-static
15256 member functions. G++ helps GDB by marking the first
15257 parameter for non-static member functions (which is the this
15258 pointer) as artificial. We obtain this information from
15259 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15260 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15261 fnp->voffset = VOFFSET_STATIC;
15262 }
15263 else
15264 complaint (_("member function type missing for '%s'"),
15265 dwarf2_full_name (fieldname, die, cu));
15266
15267 /* Get fcontext from DW_AT_containing_type if present. */
15268 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15269 fnp->fcontext = die_containing_type (die, cu);
15270
15271 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15272 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15273
15274 /* Get accessibility. */
15275 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15276 switch (accessibility)
15277 {
15278 case DW_ACCESS_private:
15279 fnp->is_private = 1;
15280 break;
15281 case DW_ACCESS_protected:
15282 fnp->is_protected = 1;
15283 break;
15284 }
15285
15286 /* Check for artificial methods. */
15287 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15288 if (attr && attr->as_boolean ())
15289 fnp->is_artificial = 1;
15290
15291 /* Check for defaulted methods. */
15292 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15293 if (attr != nullptr)
15294 fnp->defaulted = attr->defaulted ();
15295
15296 /* Check for deleted methods. */
15297 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15298 if (attr != nullptr && attr->as_boolean ())
15299 fnp->is_deleted = 1;
15300
15301 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15302
15303 /* Get index in virtual function table if it is a virtual member
15304 function. For older versions of GCC, this is an offset in the
15305 appropriate virtual table, as specified by DW_AT_containing_type.
15306 For everyone else, it is an expression to be evaluated relative
15307 to the object address. */
15308
15309 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15310 if (attr != nullptr)
15311 {
15312 if (attr->form_is_block () && attr->as_block ()->size > 0)
15313 {
15314 struct dwarf_block *block = attr->as_block ();
15315
15316 if (block->data[0] == DW_OP_constu)
15317 {
15318 /* Old-style GCC. */
15319 fnp->voffset = decode_locdesc (block, cu) + 2;
15320 }
15321 else if (block->data[0] == DW_OP_deref
15322 || (block->size > 1
15323 && block->data[0] == DW_OP_deref_size
15324 && block->data[1] == cu->header.addr_size))
15325 {
15326 fnp->voffset = decode_locdesc (block, cu);
15327 if ((fnp->voffset % cu->header.addr_size) != 0)
15328 dwarf2_complex_location_expr_complaint ();
15329 else
15330 fnp->voffset /= cu->header.addr_size;
15331 fnp->voffset += 2;
15332 }
15333 else
15334 dwarf2_complex_location_expr_complaint ();
15335
15336 if (!fnp->fcontext)
15337 {
15338 /* If there is no `this' field and no DW_AT_containing_type,
15339 we cannot actually find a base class context for the
15340 vtable! */
15341 if (this_type->num_fields () == 0
15342 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15343 {
15344 complaint (_("cannot determine context for virtual member "
15345 "function \"%s\" (offset %s)"),
15346 fieldname, sect_offset_str (die->sect_off));
15347 }
15348 else
15349 {
15350 fnp->fcontext
15351 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15352 }
15353 }
15354 }
15355 else if (attr->form_is_section_offset ())
15356 {
15357 dwarf2_complex_location_expr_complaint ();
15358 }
15359 else
15360 {
15361 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15362 fieldname);
15363 }
15364 }
15365 else
15366 {
15367 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15368 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15369 {
15370 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15371 complaint (_("Member function \"%s\" (offset %s) is virtual "
15372 "but the vtable offset is not specified"),
15373 fieldname, sect_offset_str (die->sect_off));
15374 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15375 TYPE_CPLUS_DYNAMIC (type) = 1;
15376 }
15377 }
15378 }
15379
15380 /* Create the vector of member function fields, and attach it to the type. */
15381
15382 static void
15383 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15384 struct dwarf2_cu *cu)
15385 {
15386 if (cu->language == language_ada)
15387 error (_("unexpected member functions in Ada type"));
15388
15389 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15390 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15391 TYPE_ALLOC (type,
15392 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15393
15394 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15395 {
15396 struct fnfieldlist &nf = fip->fnfieldlists[i];
15397 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15398
15399 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15400 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15401 fn_flp->fn_fields = (struct fn_field *)
15402 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15403
15404 for (int k = 0; k < nf.fnfields.size (); ++k)
15405 fn_flp->fn_fields[k] = nf.fnfields[k];
15406 }
15407
15408 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15409 }
15410
15411 /* Returns non-zero if NAME is the name of a vtable member in CU's
15412 language, zero otherwise. */
15413 static int
15414 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15415 {
15416 static const char vptr[] = "_vptr";
15417
15418 /* Look for the C++ form of the vtable. */
15419 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15420 return 1;
15421
15422 return 0;
15423 }
15424
15425 /* GCC outputs unnamed structures that are really pointers to member
15426 functions, with the ABI-specified layout. If TYPE describes
15427 such a structure, smash it into a member function type.
15428
15429 GCC shouldn't do this; it should just output pointer to member DIEs.
15430 This is GCC PR debug/28767. */
15431
15432 static void
15433 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15434 {
15435 struct type *pfn_type, *self_type, *new_type;
15436
15437 /* Check for a structure with no name and two children. */
15438 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15439 return;
15440
15441 /* Check for __pfn and __delta members. */
15442 if (TYPE_FIELD_NAME (type, 0) == NULL
15443 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15444 || TYPE_FIELD_NAME (type, 1) == NULL
15445 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15446 return;
15447
15448 /* Find the type of the method. */
15449 pfn_type = type->field (0).type ();
15450 if (pfn_type == NULL
15451 || pfn_type->code () != TYPE_CODE_PTR
15452 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15453 return;
15454
15455 /* Look for the "this" argument. */
15456 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15457 if (pfn_type->num_fields () == 0
15458 /* || pfn_type->field (0).type () == NULL */
15459 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15460 return;
15461
15462 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15463 new_type = alloc_type (objfile);
15464 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15465 pfn_type->fields (), pfn_type->num_fields (),
15466 pfn_type->has_varargs ());
15467 smash_to_methodptr_type (type, new_type);
15468 }
15469
15470 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15471 requires rewriting, then copy it and return the updated copy.
15472 Otherwise return nullptr. */
15473
15474 static struct type *
15475 rewrite_array_type (struct type *type)
15476 {
15477 if (type->code () != TYPE_CODE_ARRAY)
15478 return nullptr;
15479
15480 struct type *index_type = type->index_type ();
15481 range_bounds *current_bounds = index_type->bounds ();
15482
15483 /* Handle multi-dimensional arrays. */
15484 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15485 if (new_target == nullptr)
15486 {
15487 /* Maybe we don't need to rewrite this array. */
15488 if (current_bounds->low.kind () == PROP_CONST
15489 && current_bounds->high.kind () == PROP_CONST)
15490 return nullptr;
15491 }
15492
15493 /* Either the target type was rewritten, or the bounds have to be
15494 updated. Either way we want to copy the type and update
15495 everything. */
15496 struct type *copy = copy_type (type);
15497 int nfields = copy->num_fields ();
15498 field *new_fields
15499 = ((struct field *) TYPE_ZALLOC (copy,
15500 nfields * sizeof (struct field)));
15501 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15502 copy->set_fields (new_fields);
15503 if (new_target != nullptr)
15504 TYPE_TARGET_TYPE (copy) = new_target;
15505
15506 struct type *index_copy = copy_type (index_type);
15507 range_bounds *bounds
15508 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15509 sizeof (range_bounds));
15510 *bounds = *current_bounds;
15511 bounds->low.set_const_val (1);
15512 bounds->high.set_const_val (0);
15513 index_copy->set_bounds (bounds);
15514 copy->set_index_type (index_copy);
15515
15516 return copy;
15517 }
15518
15519 /* While some versions of GCC will generate complicated DWARF for an
15520 array (see quirk_ada_thick_pointer), more recent versions were
15521 modified to emit an explicit thick pointer structure. However, in
15522 this case, the array still has DWARF expressions for its ranges,
15523 and these must be ignored. */
15524
15525 static void
15526 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15527 struct type *type)
15528 {
15529 gdb_assert (cu->language == language_ada);
15530
15531 /* Check for a structure with two children. */
15532 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15533 return;
15534
15535 /* Check for P_ARRAY and P_BOUNDS members. */
15536 if (TYPE_FIELD_NAME (type, 0) == NULL
15537 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15538 || TYPE_FIELD_NAME (type, 1) == NULL
15539 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15540 return;
15541
15542 /* Make sure we're looking at a pointer to an array. */
15543 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15544 return;
15545
15546 /* The Ada code already knows how to handle these types, so all that
15547 we need to do is turn the bounds into static bounds. However, we
15548 don't want to rewrite existing array or index types in-place,
15549 because those may be referenced in other contexts where this
15550 rewriting is undesirable. */
15551 struct type *new_ary_type
15552 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15553 if (new_ary_type != nullptr)
15554 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15555 }
15556
15557 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15558 appropriate error checking and issuing complaints if there is a
15559 problem. */
15560
15561 static ULONGEST
15562 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15563 {
15564 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15565
15566 if (attr == nullptr)
15567 return 0;
15568
15569 if (!attr->form_is_constant ())
15570 {
15571 complaint (_("DW_AT_alignment must have constant form"
15572 " - DIE at %s [in module %s]"),
15573 sect_offset_str (die->sect_off),
15574 objfile_name (cu->per_objfile->objfile));
15575 return 0;
15576 }
15577
15578 LONGEST val = attr->constant_value (0);
15579 if (val < 0)
15580 {
15581 complaint (_("DW_AT_alignment value must not be negative"
15582 " - DIE at %s [in module %s]"),
15583 sect_offset_str (die->sect_off),
15584 objfile_name (cu->per_objfile->objfile));
15585 return 0;
15586 }
15587 ULONGEST align = val;
15588
15589 if (align == 0)
15590 {
15591 complaint (_("DW_AT_alignment value must not be zero"
15592 " - DIE at %s [in module %s]"),
15593 sect_offset_str (die->sect_off),
15594 objfile_name (cu->per_objfile->objfile));
15595 return 0;
15596 }
15597 if ((align & (align - 1)) != 0)
15598 {
15599 complaint (_("DW_AT_alignment value must be a power of 2"
15600 " - DIE at %s [in module %s]"),
15601 sect_offset_str (die->sect_off),
15602 objfile_name (cu->per_objfile->objfile));
15603 return 0;
15604 }
15605
15606 return align;
15607 }
15608
15609 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15610 the alignment for TYPE. */
15611
15612 static void
15613 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15614 struct type *type)
15615 {
15616 if (!set_type_align (type, get_alignment (cu, die)))
15617 complaint (_("DW_AT_alignment value too large"
15618 " - DIE at %s [in module %s]"),
15619 sect_offset_str (die->sect_off),
15620 objfile_name (cu->per_objfile->objfile));
15621 }
15622
15623 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15624 constant for a type, according to DWARF5 spec, Table 5.5. */
15625
15626 static bool
15627 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15628 {
15629 switch (value)
15630 {
15631 case DW_CC_normal:
15632 case DW_CC_pass_by_reference:
15633 case DW_CC_pass_by_value:
15634 return true;
15635
15636 default:
15637 complaint (_("unrecognized DW_AT_calling_convention value "
15638 "(%s) for a type"), pulongest (value));
15639 return false;
15640 }
15641 }
15642
15643 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15644 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15645 also according to GNU-specific values (see include/dwarf2.h). */
15646
15647 static bool
15648 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15649 {
15650 switch (value)
15651 {
15652 case DW_CC_normal:
15653 case DW_CC_program:
15654 case DW_CC_nocall:
15655 return true;
15656
15657 case DW_CC_GNU_renesas_sh:
15658 case DW_CC_GNU_borland_fastcall_i386:
15659 case DW_CC_GDB_IBM_OpenCL:
15660 return true;
15661
15662 default:
15663 complaint (_("unrecognized DW_AT_calling_convention value "
15664 "(%s) for a subroutine"), pulongest (value));
15665 return false;
15666 }
15667 }
15668
15669 /* Called when we find the DIE that starts a structure or union scope
15670 (definition) to create a type for the structure or union. Fill in
15671 the type's name and general properties; the members will not be
15672 processed until process_structure_scope. A symbol table entry for
15673 the type will also not be done until process_structure_scope (assuming
15674 the type has a name).
15675
15676 NOTE: we need to call these functions regardless of whether or not the
15677 DIE has a DW_AT_name attribute, since it might be an anonymous
15678 structure or union. This gets the type entered into our set of
15679 user defined types. */
15680
15681 static struct type *
15682 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15683 {
15684 struct objfile *objfile = cu->per_objfile->objfile;
15685 struct type *type;
15686 struct attribute *attr;
15687 const char *name;
15688
15689 /* If the definition of this type lives in .debug_types, read that type.
15690 Don't follow DW_AT_specification though, that will take us back up
15691 the chain and we want to go down. */
15692 attr = die->attr (DW_AT_signature);
15693 if (attr != nullptr)
15694 {
15695 type = get_DW_AT_signature_type (die, attr, cu);
15696
15697 /* The type's CU may not be the same as CU.
15698 Ensure TYPE is recorded with CU in die_type_hash. */
15699 return set_die_type (die, type, cu);
15700 }
15701
15702 type = alloc_type (objfile);
15703 INIT_CPLUS_SPECIFIC (type);
15704
15705 name = dwarf2_name (die, cu);
15706 if (name != NULL)
15707 {
15708 if (cu->language == language_cplus
15709 || cu->language == language_d
15710 || cu->language == language_rust)
15711 {
15712 const char *full_name = dwarf2_full_name (name, die, cu);
15713
15714 /* dwarf2_full_name might have already finished building the DIE's
15715 type. If so, there is no need to continue. */
15716 if (get_die_type (die, cu) != NULL)
15717 return get_die_type (die, cu);
15718
15719 type->set_name (full_name);
15720 }
15721 else
15722 {
15723 /* The name is already allocated along with this objfile, so
15724 we don't need to duplicate it for the type. */
15725 type->set_name (name);
15726 }
15727 }
15728
15729 if (die->tag == DW_TAG_structure_type)
15730 {
15731 type->set_code (TYPE_CODE_STRUCT);
15732 }
15733 else if (die->tag == DW_TAG_union_type)
15734 {
15735 type->set_code (TYPE_CODE_UNION);
15736 }
15737 else
15738 {
15739 type->set_code (TYPE_CODE_STRUCT);
15740 }
15741
15742 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15743 type->set_is_declared_class (true);
15744
15745 /* Store the calling convention in the type if it's available in
15746 the die. Otherwise the calling convention remains set to
15747 the default value DW_CC_normal. */
15748 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15749 if (attr != nullptr
15750 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15751 {
15752 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15753 TYPE_CPLUS_CALLING_CONVENTION (type)
15754 = (enum dwarf_calling_convention) (attr->constant_value (0));
15755 }
15756
15757 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15758 if (attr != nullptr)
15759 {
15760 if (attr->form_is_constant ())
15761 TYPE_LENGTH (type) = attr->constant_value (0);
15762 else
15763 {
15764 struct dynamic_prop prop;
15765 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15766 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15767 TYPE_LENGTH (type) = 0;
15768 }
15769 }
15770 else
15771 {
15772 TYPE_LENGTH (type) = 0;
15773 }
15774
15775 maybe_set_alignment (cu, die, type);
15776
15777 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15778 {
15779 /* ICC<14 does not output the required DW_AT_declaration on
15780 incomplete types, but gives them a size of zero. */
15781 type->set_is_stub (true);
15782 }
15783 else
15784 type->set_stub_is_supported (true);
15785
15786 if (die_is_declaration (die, cu))
15787 type->set_is_stub (true);
15788 else if (attr == NULL && die->child == NULL
15789 && producer_is_realview (cu->producer))
15790 /* RealView does not output the required DW_AT_declaration
15791 on incomplete types. */
15792 type->set_is_stub (true);
15793
15794 /* We need to add the type field to the die immediately so we don't
15795 infinitely recurse when dealing with pointers to the structure
15796 type within the structure itself. */
15797 set_die_type (die, type, cu);
15798
15799 /* set_die_type should be already done. */
15800 set_descriptive_type (type, die, cu);
15801
15802 return type;
15803 }
15804
15805 static void handle_struct_member_die
15806 (struct die_info *child_die,
15807 struct type *type,
15808 struct field_info *fi,
15809 std::vector<struct symbol *> *template_args,
15810 struct dwarf2_cu *cu);
15811
15812 /* A helper for handle_struct_member_die that handles
15813 DW_TAG_variant_part. */
15814
15815 static void
15816 handle_variant_part (struct die_info *die, struct type *type,
15817 struct field_info *fi,
15818 std::vector<struct symbol *> *template_args,
15819 struct dwarf2_cu *cu)
15820 {
15821 variant_part_builder *new_part;
15822 if (fi->current_variant_part == nullptr)
15823 {
15824 fi->variant_parts.emplace_back ();
15825 new_part = &fi->variant_parts.back ();
15826 }
15827 else if (!fi->current_variant_part->processing_variant)
15828 {
15829 complaint (_("nested DW_TAG_variant_part seen "
15830 "- DIE at %s [in module %s]"),
15831 sect_offset_str (die->sect_off),
15832 objfile_name (cu->per_objfile->objfile));
15833 return;
15834 }
15835 else
15836 {
15837 variant_field &current = fi->current_variant_part->variants.back ();
15838 current.variant_parts.emplace_back ();
15839 new_part = &current.variant_parts.back ();
15840 }
15841
15842 /* When we recurse, we want callees to add to this new variant
15843 part. */
15844 scoped_restore save_current_variant_part
15845 = make_scoped_restore (&fi->current_variant_part, new_part);
15846
15847 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15848 if (discr == NULL)
15849 {
15850 /* It's a univariant form, an extension we support. */
15851 }
15852 else if (discr->form_is_ref ())
15853 {
15854 struct dwarf2_cu *target_cu = cu;
15855 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15856
15857 new_part->discriminant_offset = target_die->sect_off;
15858 }
15859 else
15860 {
15861 complaint (_("DW_AT_discr does not have DIE reference form"
15862 " - DIE at %s [in module %s]"),
15863 sect_offset_str (die->sect_off),
15864 objfile_name (cu->per_objfile->objfile));
15865 }
15866
15867 for (die_info *child_die = die->child;
15868 child_die != NULL;
15869 child_die = child_die->sibling)
15870 handle_struct_member_die (child_die, type, fi, template_args, cu);
15871 }
15872
15873 /* A helper for handle_struct_member_die that handles
15874 DW_TAG_variant. */
15875
15876 static void
15877 handle_variant (struct die_info *die, struct type *type,
15878 struct field_info *fi,
15879 std::vector<struct symbol *> *template_args,
15880 struct dwarf2_cu *cu)
15881 {
15882 if (fi->current_variant_part == nullptr)
15883 {
15884 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15885 "- DIE at %s [in module %s]"),
15886 sect_offset_str (die->sect_off),
15887 objfile_name (cu->per_objfile->objfile));
15888 return;
15889 }
15890 if (fi->current_variant_part->processing_variant)
15891 {
15892 complaint (_("nested DW_TAG_variant seen "
15893 "- DIE at %s [in module %s]"),
15894 sect_offset_str (die->sect_off),
15895 objfile_name (cu->per_objfile->objfile));
15896 return;
15897 }
15898
15899 scoped_restore save_processing_variant
15900 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15901 true);
15902
15903 fi->current_variant_part->variants.emplace_back ();
15904 variant_field &variant = fi->current_variant_part->variants.back ();
15905 variant.first_field = fi->fields.size ();
15906
15907 /* In a variant we want to get the discriminant and also add a
15908 field for our sole member child. */
15909 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15910 if (discr == nullptr || !discr->form_is_constant ())
15911 {
15912 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15913 if (discr == nullptr || discr->as_block ()->size == 0)
15914 variant.default_branch = true;
15915 else
15916 variant.discr_list_data = discr->as_block ();
15917 }
15918 else
15919 variant.discriminant_value = discr->constant_value (0);
15920
15921 for (die_info *variant_child = die->child;
15922 variant_child != NULL;
15923 variant_child = variant_child->sibling)
15924 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15925
15926 variant.last_field = fi->fields.size ();
15927 }
15928
15929 /* A helper for process_structure_scope that handles a single member
15930 DIE. */
15931
15932 static void
15933 handle_struct_member_die (struct die_info *child_die, struct type *type,
15934 struct field_info *fi,
15935 std::vector<struct symbol *> *template_args,
15936 struct dwarf2_cu *cu)
15937 {
15938 if (child_die->tag == DW_TAG_member
15939 || child_die->tag == DW_TAG_variable)
15940 {
15941 /* NOTE: carlton/2002-11-05: A C++ static data member
15942 should be a DW_TAG_member that is a declaration, but
15943 all versions of G++ as of this writing (so through at
15944 least 3.2.1) incorrectly generate DW_TAG_variable
15945 tags for them instead. */
15946 dwarf2_add_field (fi, child_die, cu);
15947 }
15948 else if (child_die->tag == DW_TAG_subprogram)
15949 {
15950 /* Rust doesn't have member functions in the C++ sense.
15951 However, it does emit ordinary functions as children
15952 of a struct DIE. */
15953 if (cu->language == language_rust)
15954 read_func_scope (child_die, cu);
15955 else
15956 {
15957 /* C++ member function. */
15958 dwarf2_add_member_fn (fi, child_die, type, cu);
15959 }
15960 }
15961 else if (child_die->tag == DW_TAG_inheritance)
15962 {
15963 /* C++ base class field. */
15964 dwarf2_add_field (fi, child_die, cu);
15965 }
15966 else if (type_can_define_types (child_die))
15967 dwarf2_add_type_defn (fi, child_die, cu);
15968 else if (child_die->tag == DW_TAG_template_type_param
15969 || child_die->tag == DW_TAG_template_value_param)
15970 {
15971 struct symbol *arg = new_symbol (child_die, NULL, cu);
15972
15973 if (arg != NULL)
15974 template_args->push_back (arg);
15975 }
15976 else if (child_die->tag == DW_TAG_variant_part)
15977 handle_variant_part (child_die, type, fi, template_args, cu);
15978 else if (child_die->tag == DW_TAG_variant)
15979 handle_variant (child_die, type, fi, template_args, cu);
15980 }
15981
15982 /* Finish creating a structure or union type, including filling in
15983 its members and creating a symbol for it. */
15984
15985 static void
15986 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15987 {
15988 struct objfile *objfile = cu->per_objfile->objfile;
15989 struct die_info *child_die;
15990 struct type *type;
15991
15992 type = get_die_type (die, cu);
15993 if (type == NULL)
15994 type = read_structure_type (die, cu);
15995
15996 bool has_template_parameters = false;
15997 if (die->child != NULL && ! die_is_declaration (die, cu))
15998 {
15999 struct field_info fi;
16000 std::vector<struct symbol *> template_args;
16001
16002 child_die = die->child;
16003
16004 while (child_die && child_die->tag)
16005 {
16006 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16007 child_die = child_die->sibling;
16008 }
16009
16010 /* Attach template arguments to type. */
16011 if (!template_args.empty ())
16012 {
16013 has_template_parameters = true;
16014 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16015 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16016 TYPE_TEMPLATE_ARGUMENTS (type)
16017 = XOBNEWVEC (&objfile->objfile_obstack,
16018 struct symbol *,
16019 TYPE_N_TEMPLATE_ARGUMENTS (type));
16020 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16021 template_args.data (),
16022 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16023 * sizeof (struct symbol *)));
16024 }
16025
16026 /* Attach fields and member functions to the type. */
16027 if (fi.nfields () > 0)
16028 dwarf2_attach_fields_to_type (&fi, type, cu);
16029 if (!fi.fnfieldlists.empty ())
16030 {
16031 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16032
16033 /* Get the type which refers to the base class (possibly this
16034 class itself) which contains the vtable pointer for the current
16035 class from the DW_AT_containing_type attribute. This use of
16036 DW_AT_containing_type is a GNU extension. */
16037
16038 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16039 {
16040 struct type *t = die_containing_type (die, cu);
16041
16042 set_type_vptr_basetype (type, t);
16043 if (type == t)
16044 {
16045 int i;
16046
16047 /* Our own class provides vtbl ptr. */
16048 for (i = t->num_fields () - 1;
16049 i >= TYPE_N_BASECLASSES (t);
16050 --i)
16051 {
16052 const char *fieldname = TYPE_FIELD_NAME (t, i);
16053
16054 if (is_vtable_name (fieldname, cu))
16055 {
16056 set_type_vptr_fieldno (type, i);
16057 break;
16058 }
16059 }
16060
16061 /* Complain if virtual function table field not found. */
16062 if (i < TYPE_N_BASECLASSES (t))
16063 complaint (_("virtual function table pointer "
16064 "not found when defining class '%s'"),
16065 type->name () ? type->name () : "");
16066 }
16067 else
16068 {
16069 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16070 }
16071 }
16072 else if (cu->producer
16073 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16074 {
16075 /* The IBM XLC compiler does not provide direct indication
16076 of the containing type, but the vtable pointer is
16077 always named __vfp. */
16078
16079 int i;
16080
16081 for (i = type->num_fields () - 1;
16082 i >= TYPE_N_BASECLASSES (type);
16083 --i)
16084 {
16085 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16086 {
16087 set_type_vptr_fieldno (type, i);
16088 set_type_vptr_basetype (type, type);
16089 break;
16090 }
16091 }
16092 }
16093 }
16094
16095 /* Copy fi.typedef_field_list linked list elements content into the
16096 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16097 if (!fi.typedef_field_list.empty ())
16098 {
16099 int count = fi.typedef_field_list.size ();
16100
16101 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16102 TYPE_TYPEDEF_FIELD_ARRAY (type)
16103 = ((struct decl_field *)
16104 TYPE_ALLOC (type,
16105 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16106 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16107
16108 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16109 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16110 }
16111
16112 /* Copy fi.nested_types_list linked list elements content into the
16113 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16114 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16115 {
16116 int count = fi.nested_types_list.size ();
16117
16118 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16119 TYPE_NESTED_TYPES_ARRAY (type)
16120 = ((struct decl_field *)
16121 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16122 TYPE_NESTED_TYPES_COUNT (type) = count;
16123
16124 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16125 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16126 }
16127 }
16128
16129 quirk_gcc_member_function_pointer (type, objfile);
16130 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16131 cu->rust_unions.push_back (type);
16132 else if (cu->language == language_ada)
16133 quirk_ada_thick_pointer_struct (die, cu, type);
16134
16135 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16136 snapshots) has been known to create a die giving a declaration
16137 for a class that has, as a child, a die giving a definition for a
16138 nested class. So we have to process our children even if the
16139 current die is a declaration. Normally, of course, a declaration
16140 won't have any children at all. */
16141
16142 child_die = die->child;
16143
16144 while (child_die != NULL && child_die->tag)
16145 {
16146 if (child_die->tag == DW_TAG_member
16147 || child_die->tag == DW_TAG_variable
16148 || child_die->tag == DW_TAG_inheritance
16149 || child_die->tag == DW_TAG_template_value_param
16150 || child_die->tag == DW_TAG_template_type_param)
16151 {
16152 /* Do nothing. */
16153 }
16154 else
16155 process_die (child_die, cu);
16156
16157 child_die = child_die->sibling;
16158 }
16159
16160 /* Do not consider external references. According to the DWARF standard,
16161 these DIEs are identified by the fact that they have no byte_size
16162 attribute, and a declaration attribute. */
16163 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16164 || !die_is_declaration (die, cu)
16165 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16166 {
16167 struct symbol *sym = new_symbol (die, type, cu);
16168
16169 if (has_template_parameters)
16170 {
16171 struct symtab *symtab;
16172 if (sym != nullptr)
16173 symtab = symbol_symtab (sym);
16174 else if (cu->line_header != nullptr)
16175 {
16176 /* Any related symtab will do. */
16177 symtab
16178 = cu->line_header->file_names ()[0].symtab;
16179 }
16180 else
16181 {
16182 symtab = nullptr;
16183 complaint (_("could not find suitable "
16184 "symtab for template parameter"
16185 " - DIE at %s [in module %s]"),
16186 sect_offset_str (die->sect_off),
16187 objfile_name (objfile));
16188 }
16189
16190 if (symtab != nullptr)
16191 {
16192 /* Make sure that the symtab is set on the new symbols.
16193 Even though they don't appear in this symtab directly,
16194 other parts of gdb assume that symbols do, and this is
16195 reasonably true. */
16196 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16197 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16198 }
16199 }
16200 }
16201 }
16202
16203 /* Assuming DIE is an enumeration type, and TYPE is its associated
16204 type, update TYPE using some information only available in DIE's
16205 children. In particular, the fields are computed. */
16206
16207 static void
16208 update_enumeration_type_from_children (struct die_info *die,
16209 struct type *type,
16210 struct dwarf2_cu *cu)
16211 {
16212 struct die_info *child_die;
16213 int unsigned_enum = 1;
16214 int flag_enum = 1;
16215
16216 auto_obstack obstack;
16217 std::vector<struct field> fields;
16218
16219 for (child_die = die->child;
16220 child_die != NULL && child_die->tag;
16221 child_die = child_die->sibling)
16222 {
16223 struct attribute *attr;
16224 LONGEST value;
16225 const gdb_byte *bytes;
16226 struct dwarf2_locexpr_baton *baton;
16227 const char *name;
16228
16229 if (child_die->tag != DW_TAG_enumerator)
16230 continue;
16231
16232 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16233 if (attr == NULL)
16234 continue;
16235
16236 name = dwarf2_name (child_die, cu);
16237 if (name == NULL)
16238 name = "<anonymous enumerator>";
16239
16240 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16241 &value, &bytes, &baton);
16242 if (value < 0)
16243 {
16244 unsigned_enum = 0;
16245 flag_enum = 0;
16246 }
16247 else
16248 {
16249 if (count_one_bits_ll (value) >= 2)
16250 flag_enum = 0;
16251 }
16252
16253 fields.emplace_back ();
16254 struct field &field = fields.back ();
16255 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16256 SET_FIELD_ENUMVAL (field, value);
16257 }
16258
16259 if (!fields.empty ())
16260 {
16261 type->set_num_fields (fields.size ());
16262 type->set_fields
16263 ((struct field *)
16264 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16265 memcpy (type->fields (), fields.data (),
16266 sizeof (struct field) * fields.size ());
16267 }
16268
16269 if (unsigned_enum)
16270 type->set_is_unsigned (true);
16271
16272 if (flag_enum)
16273 type->set_is_flag_enum (true);
16274 }
16275
16276 /* Given a DW_AT_enumeration_type die, set its type. We do not
16277 complete the type's fields yet, or create any symbols. */
16278
16279 static struct type *
16280 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16281 {
16282 struct objfile *objfile = cu->per_objfile->objfile;
16283 struct type *type;
16284 struct attribute *attr;
16285 const char *name;
16286
16287 /* If the definition of this type lives in .debug_types, read that type.
16288 Don't follow DW_AT_specification though, that will take us back up
16289 the chain and we want to go down. */
16290 attr = die->attr (DW_AT_signature);
16291 if (attr != nullptr)
16292 {
16293 type = get_DW_AT_signature_type (die, attr, cu);
16294
16295 /* The type's CU may not be the same as CU.
16296 Ensure TYPE is recorded with CU in die_type_hash. */
16297 return set_die_type (die, type, cu);
16298 }
16299
16300 type = alloc_type (objfile);
16301
16302 type->set_code (TYPE_CODE_ENUM);
16303 name = dwarf2_full_name (NULL, die, cu);
16304 if (name != NULL)
16305 type->set_name (name);
16306
16307 attr = dwarf2_attr (die, DW_AT_type, cu);
16308 if (attr != NULL)
16309 {
16310 struct type *underlying_type = die_type (die, cu);
16311
16312 TYPE_TARGET_TYPE (type) = underlying_type;
16313 }
16314
16315 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16316 if (attr != nullptr)
16317 {
16318 TYPE_LENGTH (type) = attr->constant_value (0);
16319 }
16320 else
16321 {
16322 TYPE_LENGTH (type) = 0;
16323 }
16324
16325 maybe_set_alignment (cu, die, type);
16326
16327 /* The enumeration DIE can be incomplete. In Ada, any type can be
16328 declared as private in the package spec, and then defined only
16329 inside the package body. Such types are known as Taft Amendment
16330 Types. When another package uses such a type, an incomplete DIE
16331 may be generated by the compiler. */
16332 if (die_is_declaration (die, cu))
16333 type->set_is_stub (true);
16334
16335 /* If this type has an underlying type that is not a stub, then we
16336 may use its attributes. We always use the "unsigned" attribute
16337 in this situation, because ordinarily we guess whether the type
16338 is unsigned -- but the guess can be wrong and the underlying type
16339 can tell us the reality. However, we defer to a local size
16340 attribute if one exists, because this lets the compiler override
16341 the underlying type if needed. */
16342 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16343 {
16344 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16345 underlying_type = check_typedef (underlying_type);
16346
16347 type->set_is_unsigned (underlying_type->is_unsigned ());
16348
16349 if (TYPE_LENGTH (type) == 0)
16350 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16351
16352 if (TYPE_RAW_ALIGN (type) == 0
16353 && TYPE_RAW_ALIGN (underlying_type) != 0)
16354 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16355 }
16356
16357 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16358
16359 set_die_type (die, type, cu);
16360
16361 /* Finish the creation of this type by using the enum's children.
16362 Note that, as usual, this must come after set_die_type to avoid
16363 infinite recursion when trying to compute the names of the
16364 enumerators. */
16365 update_enumeration_type_from_children (die, type, cu);
16366
16367 return type;
16368 }
16369
16370 /* Given a pointer to a die which begins an enumeration, process all
16371 the dies that define the members of the enumeration, and create the
16372 symbol for the enumeration type.
16373
16374 NOTE: We reverse the order of the element list. */
16375
16376 static void
16377 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16378 {
16379 struct type *this_type;
16380
16381 this_type = get_die_type (die, cu);
16382 if (this_type == NULL)
16383 this_type = read_enumeration_type (die, cu);
16384
16385 if (die->child != NULL)
16386 {
16387 struct die_info *child_die;
16388 const char *name;
16389
16390 child_die = die->child;
16391 while (child_die && child_die->tag)
16392 {
16393 if (child_die->tag != DW_TAG_enumerator)
16394 {
16395 process_die (child_die, cu);
16396 }
16397 else
16398 {
16399 name = dwarf2_name (child_die, cu);
16400 if (name)
16401 new_symbol (child_die, this_type, cu);
16402 }
16403
16404 child_die = child_die->sibling;
16405 }
16406 }
16407
16408 /* If we are reading an enum from a .debug_types unit, and the enum
16409 is a declaration, and the enum is not the signatured type in the
16410 unit, then we do not want to add a symbol for it. Adding a
16411 symbol would in some cases obscure the true definition of the
16412 enum, giving users an incomplete type when the definition is
16413 actually available. Note that we do not want to do this for all
16414 enums which are just declarations, because C++0x allows forward
16415 enum declarations. */
16416 if (cu->per_cu->is_debug_types
16417 && die_is_declaration (die, cu))
16418 {
16419 struct signatured_type *sig_type;
16420
16421 sig_type = (struct signatured_type *) cu->per_cu;
16422 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16423 if (sig_type->type_offset_in_section != die->sect_off)
16424 return;
16425 }
16426
16427 new_symbol (die, this_type, cu);
16428 }
16429
16430 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16431 expression for an index type and finds the corresponding field
16432 offset in the hidden "P_BOUNDS" structure. Returns true on success
16433 and updates *FIELD, false if it fails to recognize an
16434 expression. */
16435
16436 static bool
16437 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16438 int *bounds_offset, struct field *field,
16439 struct dwarf2_cu *cu)
16440 {
16441 struct attribute *attr = dwarf2_attr (die, name, cu);
16442 if (attr == nullptr || !attr->form_is_block ())
16443 return false;
16444
16445 const struct dwarf_block *block = attr->as_block ();
16446 const gdb_byte *start = block->data;
16447 const gdb_byte *end = block->data + block->size;
16448
16449 /* The expression to recognize generally looks like:
16450
16451 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16452 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16453
16454 However, the second "plus_uconst" may be missing:
16455
16456 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16457 DW_OP_deref_size: 4)
16458
16459 This happens when the field is at the start of the structure.
16460
16461 Also, the final deref may not be sized:
16462
16463 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16464 DW_OP_deref)
16465
16466 This happens when the size of the index type happens to be the
16467 same as the architecture's word size. This can occur with or
16468 without the second plus_uconst. */
16469
16470 if (end - start < 2)
16471 return false;
16472 if (*start++ != DW_OP_push_object_address)
16473 return false;
16474 if (*start++ != DW_OP_plus_uconst)
16475 return false;
16476
16477 uint64_t this_bound_off;
16478 start = gdb_read_uleb128 (start, end, &this_bound_off);
16479 if (start == nullptr || (int) this_bound_off != this_bound_off)
16480 return false;
16481 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16482 is consistent among all bounds. */
16483 if (*bounds_offset == -1)
16484 *bounds_offset = this_bound_off;
16485 else if (*bounds_offset != this_bound_off)
16486 return false;
16487
16488 if (start == end || *start++ != DW_OP_deref)
16489 return false;
16490
16491 int offset = 0;
16492 if (start ==end)
16493 return false;
16494 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16495 {
16496 /* This means an offset of 0. */
16497 }
16498 else if (*start++ != DW_OP_plus_uconst)
16499 return false;
16500 else
16501 {
16502 /* The size is the parameter to DW_OP_plus_uconst. */
16503 uint64_t val;
16504 start = gdb_read_uleb128 (start, end, &val);
16505 if (start == nullptr)
16506 return false;
16507 if ((int) val != val)
16508 return false;
16509 offset = val;
16510 }
16511
16512 if (start == end)
16513 return false;
16514
16515 uint64_t size;
16516 if (*start == DW_OP_deref_size)
16517 {
16518 start = gdb_read_uleb128 (start + 1, end, &size);
16519 if (start == nullptr)
16520 return false;
16521 }
16522 else if (*start == DW_OP_deref)
16523 {
16524 size = cu->header.addr_size;
16525 ++start;
16526 }
16527 else
16528 return false;
16529
16530 SET_FIELD_BITPOS (*field, 8 * offset);
16531 if (size != TYPE_LENGTH (field->type ()))
16532 FIELD_BITSIZE (*field) = 8 * size;
16533
16534 return true;
16535 }
16536
16537 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16538 some kinds of Ada arrays:
16539
16540 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16541 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16542 <11e0> DW_AT_data_location: 2 byte block: 97 6
16543 (DW_OP_push_object_address; DW_OP_deref)
16544 <11e3> DW_AT_type : <0x1173>
16545 <11e7> DW_AT_sibling : <0x1201>
16546 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16547 <11ec> DW_AT_type : <0x1206>
16548 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16549 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16550 DW_OP_deref_size: 4)
16551 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16552 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16553 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16554
16555 This actually represents a "thick pointer", which is a structure
16556 with two elements: one that is a pointer to the array data, and one
16557 that is a pointer to another structure; this second structure holds
16558 the array bounds.
16559
16560 This returns a new type on success, or nullptr if this didn't
16561 recognize the type. */
16562
16563 static struct type *
16564 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16565 struct type *type)
16566 {
16567 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16568 /* So far we've only seen this with block form. */
16569 if (attr == nullptr || !attr->form_is_block ())
16570 return nullptr;
16571
16572 /* Note that this will fail if the structure layout is changed by
16573 the compiler. However, we have no good way to recognize some
16574 other layout, because we don't know what expression the compiler
16575 might choose to emit should this happen. */
16576 struct dwarf_block *blk = attr->as_block ();
16577 if (blk->size != 2
16578 || blk->data[0] != DW_OP_push_object_address
16579 || blk->data[1] != DW_OP_deref)
16580 return nullptr;
16581
16582 int bounds_offset = -1;
16583 int max_align = -1;
16584 std::vector<struct field> range_fields;
16585 for (struct die_info *child_die = die->child;
16586 child_die;
16587 child_die = child_die->sibling)
16588 {
16589 if (child_die->tag == DW_TAG_subrange_type)
16590 {
16591 struct type *underlying = read_subrange_index_type (child_die, cu);
16592
16593 int this_align = type_align (underlying);
16594 if (this_align > max_align)
16595 max_align = this_align;
16596
16597 range_fields.emplace_back ();
16598 range_fields.emplace_back ();
16599
16600 struct field &lower = range_fields[range_fields.size () - 2];
16601 struct field &upper = range_fields[range_fields.size () - 1];
16602
16603 lower.set_type (underlying);
16604 FIELD_ARTIFICIAL (lower) = 1;
16605
16606 upper.set_type (underlying);
16607 FIELD_ARTIFICIAL (upper) = 1;
16608
16609 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16610 &bounds_offset, &lower, cu)
16611 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16612 &bounds_offset, &upper, cu))
16613 return nullptr;
16614 }
16615 }
16616
16617 /* This shouldn't really happen, but double-check that we found
16618 where the bounds are stored. */
16619 if (bounds_offset == -1)
16620 return nullptr;
16621
16622 struct objfile *objfile = cu->per_objfile->objfile;
16623 for (int i = 0; i < range_fields.size (); i += 2)
16624 {
16625 char name[20];
16626
16627 /* Set the name of each field in the bounds. */
16628 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16629 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16630 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16631 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16632 }
16633
16634 struct type *bounds = alloc_type (objfile);
16635 bounds->set_code (TYPE_CODE_STRUCT);
16636
16637 bounds->set_num_fields (range_fields.size ());
16638 bounds->set_fields
16639 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16640 * sizeof (struct field))));
16641 memcpy (bounds->fields (), range_fields.data (),
16642 bounds->num_fields () * sizeof (struct field));
16643
16644 int last_fieldno = range_fields.size () - 1;
16645 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16646 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16647 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16648
16649 /* Rewrite the existing array type in place. Specifically, we
16650 remove any dynamic properties we might have read, and we replace
16651 the index types. */
16652 struct type *iter = type;
16653 for (int i = 0; i < range_fields.size (); i += 2)
16654 {
16655 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16656 iter->main_type->dyn_prop_list = nullptr;
16657 iter->set_index_type
16658 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16659 iter = TYPE_TARGET_TYPE (iter);
16660 }
16661
16662 struct type *result = alloc_type (objfile);
16663 result->set_code (TYPE_CODE_STRUCT);
16664
16665 result->set_num_fields (2);
16666 result->set_fields
16667 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16668 * sizeof (struct field))));
16669
16670 /* The names are chosen to coincide with what the compiler does with
16671 -fgnat-encodings=all, which the Ada code in gdb already
16672 understands. */
16673 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16674 result->field (0).set_type (lookup_pointer_type (type));
16675
16676 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16677 result->field (1).set_type (lookup_pointer_type (bounds));
16678 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16679
16680 result->set_name (type->name ());
16681 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16682 + TYPE_LENGTH (result->field (1).type ()));
16683
16684 return result;
16685 }
16686
16687 /* Extract all information from a DW_TAG_array_type DIE and put it in
16688 the DIE's type field. For now, this only handles one dimensional
16689 arrays. */
16690
16691 static struct type *
16692 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16693 {
16694 struct objfile *objfile = cu->per_objfile->objfile;
16695 struct die_info *child_die;
16696 struct type *type;
16697 struct type *element_type, *range_type, *index_type;
16698 struct attribute *attr;
16699 const char *name;
16700 struct dynamic_prop *byte_stride_prop = NULL;
16701 unsigned int bit_stride = 0;
16702
16703 element_type = die_type (die, cu);
16704
16705 /* The die_type call above may have already set the type for this DIE. */
16706 type = get_die_type (die, cu);
16707 if (type)
16708 return type;
16709
16710 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16711 if (attr != NULL)
16712 {
16713 int stride_ok;
16714 struct type *prop_type = cu->addr_sized_int_type (false);
16715
16716 byte_stride_prop
16717 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16718 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16719 prop_type);
16720 if (!stride_ok)
16721 {
16722 complaint (_("unable to read array DW_AT_byte_stride "
16723 " - DIE at %s [in module %s]"),
16724 sect_offset_str (die->sect_off),
16725 objfile_name (cu->per_objfile->objfile));
16726 /* Ignore this attribute. We will likely not be able to print
16727 arrays of this type correctly, but there is little we can do
16728 to help if we cannot read the attribute's value. */
16729 byte_stride_prop = NULL;
16730 }
16731 }
16732
16733 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16734 if (attr != NULL)
16735 bit_stride = attr->constant_value (0);
16736
16737 /* Irix 6.2 native cc creates array types without children for
16738 arrays with unspecified length. */
16739 if (die->child == NULL)
16740 {
16741 index_type = objfile_type (objfile)->builtin_int;
16742 range_type = create_static_range_type (NULL, index_type, 0, -1);
16743 type = create_array_type_with_stride (NULL, element_type, range_type,
16744 byte_stride_prop, bit_stride);
16745 return set_die_type (die, type, cu);
16746 }
16747
16748 std::vector<struct type *> range_types;
16749 child_die = die->child;
16750 while (child_die && child_die->tag)
16751 {
16752 if (child_die->tag == DW_TAG_subrange_type)
16753 {
16754 struct type *child_type = read_type_die (child_die, cu);
16755
16756 if (child_type != NULL)
16757 {
16758 /* The range type was succesfully read. Save it for the
16759 array type creation. */
16760 range_types.push_back (child_type);
16761 }
16762 }
16763 child_die = child_die->sibling;
16764 }
16765
16766 if (range_types.empty ())
16767 {
16768 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16769 sect_offset_str (die->sect_off),
16770 objfile_name (cu->per_objfile->objfile));
16771 return NULL;
16772 }
16773
16774 /* Dwarf2 dimensions are output from left to right, create the
16775 necessary array types in backwards order. */
16776
16777 type = element_type;
16778
16779 if (read_array_order (die, cu) == DW_ORD_col_major)
16780 {
16781 int i = 0;
16782
16783 while (i < range_types.size ())
16784 {
16785 type = create_array_type_with_stride (NULL, type, range_types[i++],
16786 byte_stride_prop, bit_stride);
16787 bit_stride = 0;
16788 byte_stride_prop = nullptr;
16789 }
16790 }
16791 else
16792 {
16793 size_t ndim = range_types.size ();
16794 while (ndim-- > 0)
16795 {
16796 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16797 byte_stride_prop, bit_stride);
16798 bit_stride = 0;
16799 byte_stride_prop = nullptr;
16800 }
16801 }
16802
16803 gdb_assert (type != element_type);
16804
16805 /* Understand Dwarf2 support for vector types (like they occur on
16806 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16807 array type. This is not part of the Dwarf2/3 standard yet, but a
16808 custom vendor extension. The main difference between a regular
16809 array and the vector variant is that vectors are passed by value
16810 to functions. */
16811 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16812 if (attr != nullptr)
16813 make_vector_type (type);
16814
16815 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16816 implementation may choose to implement triple vectors using this
16817 attribute. */
16818 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16819 if (attr != nullptr && attr->form_is_unsigned ())
16820 {
16821 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16822 TYPE_LENGTH (type) = attr->as_unsigned ();
16823 else
16824 complaint (_("DW_AT_byte_size for array type smaller "
16825 "than the total size of elements"));
16826 }
16827
16828 name = dwarf2_name (die, cu);
16829 if (name)
16830 type->set_name (name);
16831
16832 maybe_set_alignment (cu, die, type);
16833
16834 struct type *replacement_type = nullptr;
16835 if (cu->language == language_ada)
16836 {
16837 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16838 if (replacement_type != nullptr)
16839 type = replacement_type;
16840 }
16841
16842 /* Install the type in the die. */
16843 set_die_type (die, type, cu, replacement_type != nullptr);
16844
16845 /* set_die_type should be already done. */
16846 set_descriptive_type (type, die, cu);
16847
16848 return type;
16849 }
16850
16851 static enum dwarf_array_dim_ordering
16852 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16853 {
16854 struct attribute *attr;
16855
16856 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16857
16858 if (attr != nullptr)
16859 {
16860 LONGEST val = attr->constant_value (-1);
16861 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
16862 return (enum dwarf_array_dim_ordering) val;
16863 }
16864
16865 /* GNU F77 is a special case, as at 08/2004 array type info is the
16866 opposite order to the dwarf2 specification, but data is still
16867 laid out as per normal fortran.
16868
16869 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16870 version checking. */
16871
16872 if (cu->language == language_fortran
16873 && cu->producer && strstr (cu->producer, "GNU F77"))
16874 {
16875 return DW_ORD_row_major;
16876 }
16877
16878 switch (cu->language_defn->array_ordering ())
16879 {
16880 case array_column_major:
16881 return DW_ORD_col_major;
16882 case array_row_major:
16883 default:
16884 return DW_ORD_row_major;
16885 };
16886 }
16887
16888 /* Extract all information from a DW_TAG_set_type DIE and put it in
16889 the DIE's type field. */
16890
16891 static struct type *
16892 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16893 {
16894 struct type *domain_type, *set_type;
16895 struct attribute *attr;
16896
16897 domain_type = die_type (die, cu);
16898
16899 /* The die_type call above may have already set the type for this DIE. */
16900 set_type = get_die_type (die, cu);
16901 if (set_type)
16902 return set_type;
16903
16904 set_type = create_set_type (NULL, domain_type);
16905
16906 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16907 if (attr != nullptr && attr->form_is_unsigned ())
16908 TYPE_LENGTH (set_type) = attr->as_unsigned ();
16909
16910 maybe_set_alignment (cu, die, set_type);
16911
16912 return set_die_type (die, set_type, cu);
16913 }
16914
16915 /* A helper for read_common_block that creates a locexpr baton.
16916 SYM is the symbol which we are marking as computed.
16917 COMMON_DIE is the DIE for the common block.
16918 COMMON_LOC is the location expression attribute for the common
16919 block itself.
16920 MEMBER_LOC is the location expression attribute for the particular
16921 member of the common block that we are processing.
16922 CU is the CU from which the above come. */
16923
16924 static void
16925 mark_common_block_symbol_computed (struct symbol *sym,
16926 struct die_info *common_die,
16927 struct attribute *common_loc,
16928 struct attribute *member_loc,
16929 struct dwarf2_cu *cu)
16930 {
16931 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16932 struct objfile *objfile = per_objfile->objfile;
16933 struct dwarf2_locexpr_baton *baton;
16934 gdb_byte *ptr;
16935 unsigned int cu_off;
16936 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16937 LONGEST offset = 0;
16938
16939 gdb_assert (common_loc && member_loc);
16940 gdb_assert (common_loc->form_is_block ());
16941 gdb_assert (member_loc->form_is_block ()
16942 || member_loc->form_is_constant ());
16943
16944 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16945 baton->per_objfile = per_objfile;
16946 baton->per_cu = cu->per_cu;
16947 gdb_assert (baton->per_cu);
16948
16949 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16950
16951 if (member_loc->form_is_constant ())
16952 {
16953 offset = member_loc->constant_value (0);
16954 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16955 }
16956 else
16957 baton->size += member_loc->as_block ()->size;
16958
16959 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16960 baton->data = ptr;
16961
16962 *ptr++ = DW_OP_call4;
16963 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16964 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16965 ptr += 4;
16966
16967 if (member_loc->form_is_constant ())
16968 {
16969 *ptr++ = DW_OP_addr;
16970 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16971 ptr += cu->header.addr_size;
16972 }
16973 else
16974 {
16975 /* We have to copy the data here, because DW_OP_call4 will only
16976 use a DW_AT_location attribute. */
16977 struct dwarf_block *block = member_loc->as_block ();
16978 memcpy (ptr, block->data, block->size);
16979 ptr += block->size;
16980 }
16981
16982 *ptr++ = DW_OP_plus;
16983 gdb_assert (ptr - baton->data == baton->size);
16984
16985 SYMBOL_LOCATION_BATON (sym) = baton;
16986 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16987 }
16988
16989 /* Create appropriate locally-scoped variables for all the
16990 DW_TAG_common_block entries. Also create a struct common_block
16991 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16992 is used to separate the common blocks name namespace from regular
16993 variable names. */
16994
16995 static void
16996 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16997 {
16998 struct attribute *attr;
16999
17000 attr = dwarf2_attr (die, DW_AT_location, cu);
17001 if (attr != nullptr)
17002 {
17003 /* Support the .debug_loc offsets. */
17004 if (attr->form_is_block ())
17005 {
17006 /* Ok. */
17007 }
17008 else if (attr->form_is_section_offset ())
17009 {
17010 dwarf2_complex_location_expr_complaint ();
17011 attr = NULL;
17012 }
17013 else
17014 {
17015 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17016 "common block member");
17017 attr = NULL;
17018 }
17019 }
17020
17021 if (die->child != NULL)
17022 {
17023 struct objfile *objfile = cu->per_objfile->objfile;
17024 struct die_info *child_die;
17025 size_t n_entries = 0, size;
17026 struct common_block *common_block;
17027 struct symbol *sym;
17028
17029 for (child_die = die->child;
17030 child_die && child_die->tag;
17031 child_die = child_die->sibling)
17032 ++n_entries;
17033
17034 size = (sizeof (struct common_block)
17035 + (n_entries - 1) * sizeof (struct symbol *));
17036 common_block
17037 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17038 size);
17039 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17040 common_block->n_entries = 0;
17041
17042 for (child_die = die->child;
17043 child_die && child_die->tag;
17044 child_die = child_die->sibling)
17045 {
17046 /* Create the symbol in the DW_TAG_common_block block in the current
17047 symbol scope. */
17048 sym = new_symbol (child_die, NULL, cu);
17049 if (sym != NULL)
17050 {
17051 struct attribute *member_loc;
17052
17053 common_block->contents[common_block->n_entries++] = sym;
17054
17055 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17056 cu);
17057 if (member_loc)
17058 {
17059 /* GDB has handled this for a long time, but it is
17060 not specified by DWARF. It seems to have been
17061 emitted by gfortran at least as recently as:
17062 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17063 complaint (_("Variable in common block has "
17064 "DW_AT_data_member_location "
17065 "- DIE at %s [in module %s]"),
17066 sect_offset_str (child_die->sect_off),
17067 objfile_name (objfile));
17068
17069 if (member_loc->form_is_section_offset ())
17070 dwarf2_complex_location_expr_complaint ();
17071 else if (member_loc->form_is_constant ()
17072 || member_loc->form_is_block ())
17073 {
17074 if (attr != nullptr)
17075 mark_common_block_symbol_computed (sym, die, attr,
17076 member_loc, cu);
17077 }
17078 else
17079 dwarf2_complex_location_expr_complaint ();
17080 }
17081 }
17082 }
17083
17084 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17085 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17086 }
17087 }
17088
17089 /* Create a type for a C++ namespace. */
17090
17091 static struct type *
17092 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17093 {
17094 struct objfile *objfile = cu->per_objfile->objfile;
17095 const char *previous_prefix, *name;
17096 int is_anonymous;
17097 struct type *type;
17098
17099 /* For extensions, reuse the type of the original namespace. */
17100 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17101 {
17102 struct die_info *ext_die;
17103 struct dwarf2_cu *ext_cu = cu;
17104
17105 ext_die = dwarf2_extension (die, &ext_cu);
17106 type = read_type_die (ext_die, ext_cu);
17107
17108 /* EXT_CU may not be the same as CU.
17109 Ensure TYPE is recorded with CU in die_type_hash. */
17110 return set_die_type (die, type, cu);
17111 }
17112
17113 name = namespace_name (die, &is_anonymous, cu);
17114
17115 /* Now build the name of the current namespace. */
17116
17117 previous_prefix = determine_prefix (die, cu);
17118 if (previous_prefix[0] != '\0')
17119 name = typename_concat (&objfile->objfile_obstack,
17120 previous_prefix, name, 0, cu);
17121
17122 /* Create the type. */
17123 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17124
17125 return set_die_type (die, type, cu);
17126 }
17127
17128 /* Read a namespace scope. */
17129
17130 static void
17131 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17132 {
17133 struct objfile *objfile = cu->per_objfile->objfile;
17134 int is_anonymous;
17135
17136 /* Add a symbol associated to this if we haven't seen the namespace
17137 before. Also, add a using directive if it's an anonymous
17138 namespace. */
17139
17140 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17141 {
17142 struct type *type;
17143
17144 type = read_type_die (die, cu);
17145 new_symbol (die, type, cu);
17146
17147 namespace_name (die, &is_anonymous, cu);
17148 if (is_anonymous)
17149 {
17150 const char *previous_prefix = determine_prefix (die, cu);
17151
17152 std::vector<const char *> excludes;
17153 add_using_directive (using_directives (cu),
17154 previous_prefix, type->name (), NULL,
17155 NULL, excludes, 0, &objfile->objfile_obstack);
17156 }
17157 }
17158
17159 if (die->child != NULL)
17160 {
17161 struct die_info *child_die = die->child;
17162
17163 while (child_die && child_die->tag)
17164 {
17165 process_die (child_die, cu);
17166 child_die = child_die->sibling;
17167 }
17168 }
17169 }
17170
17171 /* Read a Fortran module as type. This DIE can be only a declaration used for
17172 imported module. Still we need that type as local Fortran "use ... only"
17173 declaration imports depend on the created type in determine_prefix. */
17174
17175 static struct type *
17176 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17177 {
17178 struct objfile *objfile = cu->per_objfile->objfile;
17179 const char *module_name;
17180 struct type *type;
17181
17182 module_name = dwarf2_name (die, cu);
17183 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17184
17185 return set_die_type (die, type, cu);
17186 }
17187
17188 /* Read a Fortran module. */
17189
17190 static void
17191 read_module (struct die_info *die, struct dwarf2_cu *cu)
17192 {
17193 struct die_info *child_die = die->child;
17194 struct type *type;
17195
17196 type = read_type_die (die, cu);
17197 new_symbol (die, type, cu);
17198
17199 while (child_die && child_die->tag)
17200 {
17201 process_die (child_die, cu);
17202 child_die = child_die->sibling;
17203 }
17204 }
17205
17206 /* Return the name of the namespace represented by DIE. Set
17207 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17208 namespace. */
17209
17210 static const char *
17211 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17212 {
17213 struct die_info *current_die;
17214 const char *name = NULL;
17215
17216 /* Loop through the extensions until we find a name. */
17217
17218 for (current_die = die;
17219 current_die != NULL;
17220 current_die = dwarf2_extension (die, &cu))
17221 {
17222 /* We don't use dwarf2_name here so that we can detect the absence
17223 of a name -> anonymous namespace. */
17224 name = dwarf2_string_attr (die, DW_AT_name, cu);
17225
17226 if (name != NULL)
17227 break;
17228 }
17229
17230 /* Is it an anonymous namespace? */
17231
17232 *is_anonymous = (name == NULL);
17233 if (*is_anonymous)
17234 name = CP_ANONYMOUS_NAMESPACE_STR;
17235
17236 return name;
17237 }
17238
17239 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17240 the user defined type vector. */
17241
17242 static struct type *
17243 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17244 {
17245 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17246 struct comp_unit_head *cu_header = &cu->header;
17247 struct type *type;
17248 struct attribute *attr_byte_size;
17249 struct attribute *attr_address_class;
17250 int byte_size, addr_class;
17251 struct type *target_type;
17252
17253 target_type = die_type (die, cu);
17254
17255 /* The die_type call above may have already set the type for this DIE. */
17256 type = get_die_type (die, cu);
17257 if (type)
17258 return type;
17259
17260 type = lookup_pointer_type (target_type);
17261
17262 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17263 if (attr_byte_size)
17264 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17265 else
17266 byte_size = cu_header->addr_size;
17267
17268 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17269 if (attr_address_class)
17270 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17271 else
17272 addr_class = DW_ADDR_none;
17273
17274 ULONGEST alignment = get_alignment (cu, die);
17275
17276 /* If the pointer size, alignment, or address class is different
17277 than the default, create a type variant marked as such and set
17278 the length accordingly. */
17279 if (TYPE_LENGTH (type) != byte_size
17280 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17281 && alignment != TYPE_RAW_ALIGN (type))
17282 || addr_class != DW_ADDR_none)
17283 {
17284 if (gdbarch_address_class_type_flags_p (gdbarch))
17285 {
17286 type_instance_flags type_flags
17287 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17288 addr_class);
17289 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17290 == 0);
17291 type = make_type_with_address_space (type, type_flags);
17292 }
17293 else if (TYPE_LENGTH (type) != byte_size)
17294 {
17295 complaint (_("invalid pointer size %d"), byte_size);
17296 }
17297 else if (TYPE_RAW_ALIGN (type) != alignment)
17298 {
17299 complaint (_("Invalid DW_AT_alignment"
17300 " - DIE at %s [in module %s]"),
17301 sect_offset_str (die->sect_off),
17302 objfile_name (cu->per_objfile->objfile));
17303 }
17304 else
17305 {
17306 /* Should we also complain about unhandled address classes? */
17307 }
17308 }
17309
17310 TYPE_LENGTH (type) = byte_size;
17311 set_type_align (type, alignment);
17312 return set_die_type (die, type, cu);
17313 }
17314
17315 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17316 the user defined type vector. */
17317
17318 static struct type *
17319 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17320 {
17321 struct type *type;
17322 struct type *to_type;
17323 struct type *domain;
17324
17325 to_type = die_type (die, cu);
17326 domain = die_containing_type (die, cu);
17327
17328 /* The calls above may have already set the type for this DIE. */
17329 type = get_die_type (die, cu);
17330 if (type)
17331 return type;
17332
17333 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17334 type = lookup_methodptr_type (to_type);
17335 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17336 {
17337 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17338
17339 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17340 to_type->fields (), to_type->num_fields (),
17341 to_type->has_varargs ());
17342 type = lookup_methodptr_type (new_type);
17343 }
17344 else
17345 type = lookup_memberptr_type (to_type, domain);
17346
17347 return set_die_type (die, type, cu);
17348 }
17349
17350 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17351 the user defined type vector. */
17352
17353 static struct type *
17354 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17355 enum type_code refcode)
17356 {
17357 struct comp_unit_head *cu_header = &cu->header;
17358 struct type *type, *target_type;
17359 struct attribute *attr;
17360
17361 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17362
17363 target_type = die_type (die, cu);
17364
17365 /* The die_type call above may have already set the type for this DIE. */
17366 type = get_die_type (die, cu);
17367 if (type)
17368 return type;
17369
17370 type = lookup_reference_type (target_type, refcode);
17371 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17372 if (attr != nullptr)
17373 {
17374 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17375 }
17376 else
17377 {
17378 TYPE_LENGTH (type) = cu_header->addr_size;
17379 }
17380 maybe_set_alignment (cu, die, type);
17381 return set_die_type (die, type, cu);
17382 }
17383
17384 /* Add the given cv-qualifiers to the element type of the array. GCC
17385 outputs DWARF type qualifiers that apply to an array, not the
17386 element type. But GDB relies on the array element type to carry
17387 the cv-qualifiers. This mimics section 6.7.3 of the C99
17388 specification. */
17389
17390 static struct type *
17391 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17392 struct type *base_type, int cnst, int voltl)
17393 {
17394 struct type *el_type, *inner_array;
17395
17396 base_type = copy_type (base_type);
17397 inner_array = base_type;
17398
17399 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17400 {
17401 TYPE_TARGET_TYPE (inner_array) =
17402 copy_type (TYPE_TARGET_TYPE (inner_array));
17403 inner_array = TYPE_TARGET_TYPE (inner_array);
17404 }
17405
17406 el_type = TYPE_TARGET_TYPE (inner_array);
17407 cnst |= TYPE_CONST (el_type);
17408 voltl |= TYPE_VOLATILE (el_type);
17409 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17410
17411 return set_die_type (die, base_type, cu);
17412 }
17413
17414 static struct type *
17415 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17416 {
17417 struct type *base_type, *cv_type;
17418
17419 base_type = die_type (die, cu);
17420
17421 /* The die_type call above may have already set the type for this DIE. */
17422 cv_type = get_die_type (die, cu);
17423 if (cv_type)
17424 return cv_type;
17425
17426 /* In case the const qualifier is applied to an array type, the element type
17427 is so qualified, not the array type (section 6.7.3 of C99). */
17428 if (base_type->code () == TYPE_CODE_ARRAY)
17429 return add_array_cv_type (die, cu, base_type, 1, 0);
17430
17431 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17432 return set_die_type (die, cv_type, cu);
17433 }
17434
17435 static struct type *
17436 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17437 {
17438 struct type *base_type, *cv_type;
17439
17440 base_type = die_type (die, cu);
17441
17442 /* The die_type call above may have already set the type for this DIE. */
17443 cv_type = get_die_type (die, cu);
17444 if (cv_type)
17445 return cv_type;
17446
17447 /* In case the volatile qualifier is applied to an array type, the
17448 element type is so qualified, not the array type (section 6.7.3
17449 of C99). */
17450 if (base_type->code () == TYPE_CODE_ARRAY)
17451 return add_array_cv_type (die, cu, base_type, 0, 1);
17452
17453 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17454 return set_die_type (die, cv_type, cu);
17455 }
17456
17457 /* Handle DW_TAG_restrict_type. */
17458
17459 static struct type *
17460 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17461 {
17462 struct type *base_type, *cv_type;
17463
17464 base_type = die_type (die, cu);
17465
17466 /* The die_type call above may have already set the type for this DIE. */
17467 cv_type = get_die_type (die, cu);
17468 if (cv_type)
17469 return cv_type;
17470
17471 cv_type = make_restrict_type (base_type);
17472 return set_die_type (die, cv_type, cu);
17473 }
17474
17475 /* Handle DW_TAG_atomic_type. */
17476
17477 static struct type *
17478 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17479 {
17480 struct type *base_type, *cv_type;
17481
17482 base_type = die_type (die, cu);
17483
17484 /* The die_type call above may have already set the type for this DIE. */
17485 cv_type = get_die_type (die, cu);
17486 if (cv_type)
17487 return cv_type;
17488
17489 cv_type = make_atomic_type (base_type);
17490 return set_die_type (die, cv_type, cu);
17491 }
17492
17493 /* Extract all information from a DW_TAG_string_type DIE and add to
17494 the user defined type vector. It isn't really a user defined type,
17495 but it behaves like one, with other DIE's using an AT_user_def_type
17496 attribute to reference it. */
17497
17498 static struct type *
17499 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17500 {
17501 struct objfile *objfile = cu->per_objfile->objfile;
17502 struct gdbarch *gdbarch = objfile->arch ();
17503 struct type *type, *range_type, *index_type, *char_type;
17504 struct attribute *attr;
17505 struct dynamic_prop prop;
17506 bool length_is_constant = true;
17507 LONGEST length;
17508
17509 /* There are a couple of places where bit sizes might be made use of
17510 when parsing a DW_TAG_string_type, however, no producer that we know
17511 of make use of these. Handling bit sizes that are a multiple of the
17512 byte size is easy enough, but what about other bit sizes? Lets deal
17513 with that problem when we have to. Warn about these attributes being
17514 unsupported, then parse the type and ignore them like we always
17515 have. */
17516 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17517 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17518 {
17519 static bool warning_printed = false;
17520 if (!warning_printed)
17521 {
17522 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17523 "currently supported on DW_TAG_string_type."));
17524 warning_printed = true;
17525 }
17526 }
17527
17528 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17529 if (attr != nullptr && !attr->form_is_constant ())
17530 {
17531 /* The string length describes the location at which the length of
17532 the string can be found. The size of the length field can be
17533 specified with one of the attributes below. */
17534 struct type *prop_type;
17535 struct attribute *len
17536 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17537 if (len == nullptr)
17538 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17539 if (len != nullptr && len->form_is_constant ())
17540 {
17541 /* Pass 0 as the default as we know this attribute is constant
17542 and the default value will not be returned. */
17543 LONGEST sz = len->constant_value (0);
17544 prop_type = cu->per_objfile->int_type (sz, true);
17545 }
17546 else
17547 {
17548 /* If the size is not specified then we assume it is the size of
17549 an address on this target. */
17550 prop_type = cu->addr_sized_int_type (true);
17551 }
17552
17553 /* Convert the attribute into a dynamic property. */
17554 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17555 length = 1;
17556 else
17557 length_is_constant = false;
17558 }
17559 else if (attr != nullptr)
17560 {
17561 /* This DW_AT_string_length just contains the length with no
17562 indirection. There's no need to create a dynamic property in this
17563 case. Pass 0 for the default value as we know it will not be
17564 returned in this case. */
17565 length = attr->constant_value (0);
17566 }
17567 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17568 {
17569 /* We don't currently support non-constant byte sizes for strings. */
17570 length = attr->constant_value (1);
17571 }
17572 else
17573 {
17574 /* Use 1 as a fallback length if we have nothing else. */
17575 length = 1;
17576 }
17577
17578 index_type = objfile_type (objfile)->builtin_int;
17579 if (length_is_constant)
17580 range_type = create_static_range_type (NULL, index_type, 1, length);
17581 else
17582 {
17583 struct dynamic_prop low_bound;
17584
17585 low_bound.set_const_val (1);
17586 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17587 }
17588 char_type = language_string_char_type (cu->language_defn, gdbarch);
17589 type = create_string_type (NULL, char_type, range_type);
17590
17591 return set_die_type (die, type, cu);
17592 }
17593
17594 /* Assuming that DIE corresponds to a function, returns nonzero
17595 if the function is prototyped. */
17596
17597 static int
17598 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17599 {
17600 struct attribute *attr;
17601
17602 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17603 if (attr && attr->as_boolean ())
17604 return 1;
17605
17606 /* The DWARF standard implies that the DW_AT_prototyped attribute
17607 is only meaningful for C, but the concept also extends to other
17608 languages that allow unprototyped functions (Eg: Objective C).
17609 For all other languages, assume that functions are always
17610 prototyped. */
17611 if (cu->language != language_c
17612 && cu->language != language_objc
17613 && cu->language != language_opencl)
17614 return 1;
17615
17616 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17617 prototyped and unprototyped functions; default to prototyped,
17618 since that is more common in modern code (and RealView warns
17619 about unprototyped functions). */
17620 if (producer_is_realview (cu->producer))
17621 return 1;
17622
17623 return 0;
17624 }
17625
17626 /* Handle DIES due to C code like:
17627
17628 struct foo
17629 {
17630 int (*funcp)(int a, long l);
17631 int b;
17632 };
17633
17634 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17635
17636 static struct type *
17637 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17638 {
17639 struct objfile *objfile = cu->per_objfile->objfile;
17640 struct type *type; /* Type that this function returns. */
17641 struct type *ftype; /* Function that returns above type. */
17642 struct attribute *attr;
17643
17644 type = die_type (die, cu);
17645
17646 /* The die_type call above may have already set the type for this DIE. */
17647 ftype = get_die_type (die, cu);
17648 if (ftype)
17649 return ftype;
17650
17651 ftype = lookup_function_type (type);
17652
17653 if (prototyped_function_p (die, cu))
17654 ftype->set_is_prototyped (true);
17655
17656 /* Store the calling convention in the type if it's available in
17657 the subroutine die. Otherwise set the calling convention to
17658 the default value DW_CC_normal. */
17659 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17660 if (attr != nullptr
17661 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17662 TYPE_CALLING_CONVENTION (ftype)
17663 = (enum dwarf_calling_convention) attr->constant_value (0);
17664 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17665 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17666 else
17667 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17668
17669 /* Record whether the function returns normally to its caller or not
17670 if the DWARF producer set that information. */
17671 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17672 if (attr && attr->as_boolean ())
17673 TYPE_NO_RETURN (ftype) = 1;
17674
17675 /* We need to add the subroutine type to the die immediately so
17676 we don't infinitely recurse when dealing with parameters
17677 declared as the same subroutine type. */
17678 set_die_type (die, ftype, cu);
17679
17680 if (die->child != NULL)
17681 {
17682 struct type *void_type = objfile_type (objfile)->builtin_void;
17683 struct die_info *child_die;
17684 int nparams, iparams;
17685
17686 /* Count the number of parameters.
17687 FIXME: GDB currently ignores vararg functions, but knows about
17688 vararg member functions. */
17689 nparams = 0;
17690 child_die = die->child;
17691 while (child_die && child_die->tag)
17692 {
17693 if (child_die->tag == DW_TAG_formal_parameter)
17694 nparams++;
17695 else if (child_die->tag == DW_TAG_unspecified_parameters)
17696 ftype->set_has_varargs (true);
17697
17698 child_die = child_die->sibling;
17699 }
17700
17701 /* Allocate storage for parameters and fill them in. */
17702 ftype->set_num_fields (nparams);
17703 ftype->set_fields
17704 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17705
17706 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17707 even if we error out during the parameters reading below. */
17708 for (iparams = 0; iparams < nparams; iparams++)
17709 ftype->field (iparams).set_type (void_type);
17710
17711 iparams = 0;
17712 child_die = die->child;
17713 while (child_die && child_die->tag)
17714 {
17715 if (child_die->tag == DW_TAG_formal_parameter)
17716 {
17717 struct type *arg_type;
17718
17719 /* DWARF version 2 has no clean way to discern C++
17720 static and non-static member functions. G++ helps
17721 GDB by marking the first parameter for non-static
17722 member functions (which is the this pointer) as
17723 artificial. We pass this information to
17724 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17725
17726 DWARF version 3 added DW_AT_object_pointer, which GCC
17727 4.5 does not yet generate. */
17728 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17729 if (attr != nullptr)
17730 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17731 else
17732 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17733 arg_type = die_type (child_die, cu);
17734
17735 /* RealView does not mark THIS as const, which the testsuite
17736 expects. GCC marks THIS as const in method definitions,
17737 but not in the class specifications (GCC PR 43053). */
17738 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17739 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17740 {
17741 int is_this = 0;
17742 struct dwarf2_cu *arg_cu = cu;
17743 const char *name = dwarf2_name (child_die, cu);
17744
17745 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17746 if (attr != nullptr)
17747 {
17748 /* If the compiler emits this, use it. */
17749 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17750 is_this = 1;
17751 }
17752 else if (name && strcmp (name, "this") == 0)
17753 /* Function definitions will have the argument names. */
17754 is_this = 1;
17755 else if (name == NULL && iparams == 0)
17756 /* Declarations may not have the names, so like
17757 elsewhere in GDB, assume an artificial first
17758 argument is "this". */
17759 is_this = 1;
17760
17761 if (is_this)
17762 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17763 arg_type, 0);
17764 }
17765
17766 ftype->field (iparams).set_type (arg_type);
17767 iparams++;
17768 }
17769 child_die = child_die->sibling;
17770 }
17771 }
17772
17773 return ftype;
17774 }
17775
17776 static struct type *
17777 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17778 {
17779 struct objfile *objfile = cu->per_objfile->objfile;
17780 const char *name = NULL;
17781 struct type *this_type, *target_type;
17782
17783 name = dwarf2_full_name (NULL, die, cu);
17784 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17785 this_type->set_target_is_stub (true);
17786 set_die_type (die, this_type, cu);
17787 target_type = die_type (die, cu);
17788 if (target_type != this_type)
17789 TYPE_TARGET_TYPE (this_type) = target_type;
17790 else
17791 {
17792 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17793 spec and cause infinite loops in GDB. */
17794 complaint (_("Self-referential DW_TAG_typedef "
17795 "- DIE at %s [in module %s]"),
17796 sect_offset_str (die->sect_off), objfile_name (objfile));
17797 TYPE_TARGET_TYPE (this_type) = NULL;
17798 }
17799 if (name == NULL)
17800 {
17801 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17802 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17803 Handle these by just returning the target type, rather than
17804 constructing an anonymous typedef type and trying to handle this
17805 elsewhere. */
17806 set_die_type (die, target_type, cu);
17807 return target_type;
17808 }
17809 return this_type;
17810 }
17811
17812 /* Helper for get_dwarf2_rational_constant that computes the value of
17813 a given gmp_mpz given an attribute. */
17814
17815 static void
17816 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17817 {
17818 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17819 location expression that pushes an implicit value. */
17820 if (attr->form == DW_FORM_exprloc)
17821 {
17822 dwarf_block *blk = attr->as_block ();
17823 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17824 {
17825 uint64_t len;
17826 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17827 blk->data + blk->size,
17828 &len);
17829 if (ptr - blk->data + len <= blk->size)
17830 {
17831 mpz_import (value->val, len,
17832 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17833 1, 0, 0, ptr);
17834 return;
17835 }
17836 }
17837
17838 /* On failure set it to 1. */
17839 *value = gdb_mpz (1);
17840 }
17841 else if (attr->form_is_block ())
17842 {
17843 dwarf_block *blk = attr->as_block ();
17844 mpz_import (value->val, blk->size,
17845 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17846 1, 0, 0, blk->data);
17847 }
17848 else
17849 *value = gdb_mpz (attr->constant_value (1));
17850 }
17851
17852 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17853 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17854
17855 If the numerator and/or numerator attribute is missing,
17856 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17857 untouched. */
17858
17859 static void
17860 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
17861 gdb_mpz *numerator, gdb_mpz *denominator)
17862 {
17863 struct attribute *num_attr, *denom_attr;
17864
17865 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
17866 if (num_attr == nullptr)
17867 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
17868 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17869
17870 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
17871 if (denom_attr == nullptr)
17872 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
17873 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
17874
17875 if (num_attr == nullptr || denom_attr == nullptr)
17876 return;
17877
17878 get_mpz (cu, numerator, num_attr);
17879 get_mpz (cu, denominator, denom_attr);
17880 }
17881
17882 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
17883 rational constant, rather than a signed one.
17884
17885 If the rational constant has a negative value, a complaint
17886 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
17887
17888 static void
17889 get_dwarf2_unsigned_rational_constant (struct die_info *die,
17890 struct dwarf2_cu *cu,
17891 gdb_mpz *numerator,
17892 gdb_mpz *denominator)
17893 {
17894 gdb_mpz num (1);
17895 gdb_mpz denom (1);
17896
17897 get_dwarf2_rational_constant (die, cu, &num, &denom);
17898 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
17899 {
17900 mpz_neg (num.val, num.val);
17901 mpz_neg (denom.val, denom.val);
17902 }
17903 else if (mpz_sgn (num.val) == -1)
17904 {
17905 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
17906 " in DIE at %s"),
17907 sect_offset_str (die->sect_off));
17908 return;
17909 }
17910 else if (mpz_sgn (denom.val) == -1)
17911 {
17912 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
17913 " in DIE at %s"),
17914 sect_offset_str (die->sect_off));
17915 return;
17916 }
17917
17918 *numerator = std::move (num);
17919 *denominator = std::move (denom);
17920 }
17921
17922 /* Assuming that ENCODING is a string whose contents starting at the
17923 K'th character is "_nn" where "nn" is a decimal number, scan that
17924 number and set RESULT to the value. K is updated to point to the
17925 character immediately following the number.
17926
17927 If the string does not conform to the format described above, false
17928 is returned, and K may or may not be changed. */
17929
17930 static bool
17931 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
17932 {
17933 /* The next character should be an underscore ('_') followed
17934 by a digit. */
17935 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
17936 return false;
17937
17938 /* Skip the underscore. */
17939 k++;
17940 int start = k;
17941
17942 /* Determine the number of digits for our number. */
17943 while (isdigit (encoding[k]))
17944 k++;
17945 if (k == start)
17946 return false;
17947
17948 std::string copy (&encoding[start], k - start);
17949 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
17950 return false;
17951
17952 return true;
17953 }
17954
17955 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
17956 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
17957 DENOM, update OFFSET, and return true on success. Return false on
17958 failure. */
17959
17960 static bool
17961 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
17962 gdb_mpz *num, gdb_mpz *denom)
17963 {
17964 if (!ada_get_gnat_encoded_number (encoding, offset, num))
17965 return false;
17966 return ada_get_gnat_encoded_number (encoding, offset, denom);
17967 }
17968
17969 /* Assuming DIE corresponds to a fixed point type, finish the creation
17970 of the corresponding TYPE by setting its type-specific data. CU is
17971 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
17972 encodings. It is nullptr if the GNAT encoding should be
17973 ignored. */
17974
17975 static void
17976 finish_fixed_point_type (struct type *type, const char *suffix,
17977 struct die_info *die, struct dwarf2_cu *cu)
17978 {
17979 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
17980 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
17981
17982 /* If GNAT encodings are preferred, don't examine the
17983 attributes. */
17984 struct attribute *attr = nullptr;
17985 if (suffix == nullptr)
17986 {
17987 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
17988 if (attr == nullptr)
17989 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
17990 if (attr == nullptr)
17991 attr = dwarf2_attr (die, DW_AT_small, cu);
17992 }
17993
17994 /* Numerator and denominator of our fixed-point type's scaling factor.
17995 The default is a scaling factor of 1, which we use as a fallback
17996 when we are not able to decode it (problem with the debugging info,
17997 unsupported forms, bug in GDB, etc...). Using that as the default
17998 allows us to at least print the unscaled value, which might still
17999 be useful to a user. */
18000 gdb_mpz scale_num (1);
18001 gdb_mpz scale_denom (1);
18002
18003 if (attr == nullptr)
18004 {
18005 int offset = 0;
18006 if (suffix != nullptr
18007 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18008 &scale_denom)
18009 /* The number might be encoded as _nn_dd_nn_dd, where the
18010 second ratio is the 'small value. In this situation, we
18011 want the second value. */
18012 && (suffix[offset] != '_'
18013 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18014 &scale_denom)))
18015 {
18016 /* Found it. */
18017 }
18018 else
18019 {
18020 /* Scaling factor not found. Assume a scaling factor of 1,
18021 and hope for the best. At least the user will be able to
18022 see the encoded value. */
18023 scale_num = 1;
18024 scale_denom = 1;
18025 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18026 sect_offset_str (die->sect_off));
18027 }
18028 }
18029 else if (attr->name == DW_AT_binary_scale)
18030 {
18031 LONGEST scale_exp = attr->constant_value (0);
18032 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18033
18034 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18035 }
18036 else if (attr->name == DW_AT_decimal_scale)
18037 {
18038 LONGEST scale_exp = attr->constant_value (0);
18039 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18040
18041 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18042 }
18043 else if (attr->name == DW_AT_small)
18044 {
18045 struct die_info *scale_die;
18046 struct dwarf2_cu *scale_cu = cu;
18047
18048 scale_die = follow_die_ref (die, attr, &scale_cu);
18049 if (scale_die->tag == DW_TAG_constant)
18050 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18051 &scale_num, &scale_denom);
18052 else
18053 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18054 " (DIE at %s)"),
18055 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18056 }
18057 else
18058 {
18059 complaint (_("unsupported scale attribute %s for fixed-point type"
18060 " (DIE at %s)"),
18061 dwarf_attr_name (attr->name),
18062 sect_offset_str (die->sect_off));
18063 }
18064
18065 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18066 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18067 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18068 mpq_canonicalize (scaling_factor.val);
18069 }
18070
18071 /* The gnat-encoding suffix for fixed point. */
18072
18073 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18074
18075 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18076 "XF" suffix of the name. The text after this is what encodes the
18077 'small and 'delta information. Otherwise, return nullptr. */
18078
18079 static const char *
18080 gnat_encoded_fixed_point_type_info (const char *name)
18081 {
18082 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18083 }
18084
18085 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18086 (which may be different from NAME) to the architecture back-end to allow
18087 it to guess the correct format if necessary. */
18088
18089 static struct type *
18090 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18091 const char *name_hint, enum bfd_endian byte_order)
18092 {
18093 struct gdbarch *gdbarch = objfile->arch ();
18094 const struct floatformat **format;
18095 struct type *type;
18096
18097 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18098 if (format)
18099 type = init_float_type (objfile, bits, name, format, byte_order);
18100 else
18101 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18102
18103 return type;
18104 }
18105
18106 /* Allocate an integer type of size BITS and name NAME. */
18107
18108 static struct type *
18109 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18110 int bits, int unsigned_p, const char *name)
18111 {
18112 struct type *type;
18113
18114 /* Versions of Intel's C Compiler generate an integer type called "void"
18115 instead of using DW_TAG_unspecified_type. This has been seen on
18116 at least versions 14, 17, and 18. */
18117 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18118 && strcmp (name, "void") == 0)
18119 type = objfile_type (objfile)->builtin_void;
18120 else
18121 type = init_integer_type (objfile, bits, unsigned_p, name);
18122
18123 return type;
18124 }
18125
18126 /* Return true if DIE has a DW_AT_small attribute whose value is
18127 a constant rational, where both the numerator and denominator
18128 are equal to zero.
18129
18130 CU is the DIE's Compilation Unit. */
18131
18132 static bool
18133 has_zero_over_zero_small_attribute (struct die_info *die,
18134 struct dwarf2_cu *cu)
18135 {
18136 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18137 if (attr == nullptr)
18138 return false;
18139
18140 struct dwarf2_cu *scale_cu = cu;
18141 struct die_info *scale_die
18142 = follow_die_ref (die, attr, &scale_cu);
18143
18144 if (scale_die->tag != DW_TAG_constant)
18145 return false;
18146
18147 gdb_mpz num (1), denom (1);
18148 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18149 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18150 }
18151
18152 /* Initialise and return a floating point type of size BITS suitable for
18153 use as a component of a complex number. The NAME_HINT is passed through
18154 when initialising the floating point type and is the name of the complex
18155 type.
18156
18157 As DWARF doesn't currently provide an explicit name for the components
18158 of a complex number, but it can be helpful to have these components
18159 named, we try to select a suitable name based on the size of the
18160 component. */
18161 static struct type *
18162 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18163 struct objfile *objfile,
18164 int bits, const char *name_hint,
18165 enum bfd_endian byte_order)
18166 {
18167 gdbarch *gdbarch = objfile->arch ();
18168 struct type *tt = nullptr;
18169
18170 /* Try to find a suitable floating point builtin type of size BITS.
18171 We're going to use the name of this type as the name for the complex
18172 target type that we are about to create. */
18173 switch (cu->language)
18174 {
18175 case language_fortran:
18176 switch (bits)
18177 {
18178 case 32:
18179 tt = builtin_f_type (gdbarch)->builtin_real;
18180 break;
18181 case 64:
18182 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18183 break;
18184 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18185 case 128:
18186 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18187 break;
18188 }
18189 break;
18190 default:
18191 switch (bits)
18192 {
18193 case 32:
18194 tt = builtin_type (gdbarch)->builtin_float;
18195 break;
18196 case 64:
18197 tt = builtin_type (gdbarch)->builtin_double;
18198 break;
18199 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18200 case 128:
18201 tt = builtin_type (gdbarch)->builtin_long_double;
18202 break;
18203 }
18204 break;
18205 }
18206
18207 /* If the type we found doesn't match the size we were looking for, then
18208 pretend we didn't find a type at all, the complex target type we
18209 create will then be nameless. */
18210 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18211 tt = nullptr;
18212
18213 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18214 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18215 }
18216
18217 /* Find a representation of a given base type and install
18218 it in the TYPE field of the die. */
18219
18220 static struct type *
18221 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18222 {
18223 struct objfile *objfile = cu->per_objfile->objfile;
18224 struct type *type;
18225 struct attribute *attr;
18226 int encoding = 0, bits = 0;
18227 const char *name;
18228 gdbarch *arch;
18229
18230 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18231 if (attr != nullptr && attr->form_is_constant ())
18232 encoding = attr->constant_value (0);
18233 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18234 if (attr != nullptr)
18235 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18236 name = dwarf2_name (die, cu);
18237 if (!name)
18238 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18239
18240 arch = objfile->arch ();
18241 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18242
18243 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18244 if (attr != nullptr && attr->form_is_constant ())
18245 {
18246 int endianity = attr->constant_value (0);
18247
18248 switch (endianity)
18249 {
18250 case DW_END_big:
18251 byte_order = BFD_ENDIAN_BIG;
18252 break;
18253 case DW_END_little:
18254 byte_order = BFD_ENDIAN_LITTLE;
18255 break;
18256 default:
18257 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18258 break;
18259 }
18260 }
18261
18262 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18263 && cu->language == language_ada
18264 && has_zero_over_zero_small_attribute (die, cu))
18265 {
18266 /* brobecker/2018-02-24: This is a fixed point type for which
18267 the scaling factor is represented as fraction whose value
18268 does not make sense (zero divided by zero), so we should
18269 normally never see these. However, there is a small category
18270 of fixed point types for which GNAT is unable to provide
18271 the scaling factor via the standard DWARF mechanisms, and
18272 for which the info is provided via the GNAT encodings instead.
18273 This is likely what this DIE is about. */
18274 encoding = (encoding == DW_ATE_signed_fixed
18275 ? DW_ATE_signed
18276 : DW_ATE_unsigned);
18277 }
18278
18279 /* With GNAT encodings, fixed-point information will be encoded in
18280 the type name. Note that this can also occur with the above
18281 zero-over-zero case, which is why this is a separate "if" rather
18282 than an "else if". */
18283 const char *gnat_encoding_suffix = nullptr;
18284 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18285 && cu->language == language_ada
18286 && name != nullptr)
18287 {
18288 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18289 if (gnat_encoding_suffix != nullptr)
18290 {
18291 gdb_assert (startswith (gnat_encoding_suffix,
18292 GNAT_FIXED_POINT_SUFFIX));
18293 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18294 name, gnat_encoding_suffix - name);
18295 /* Use -1 here so that SUFFIX points at the "_" after the
18296 "XF". */
18297 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18298
18299 encoding = (encoding == DW_ATE_signed
18300 ? DW_ATE_signed_fixed
18301 : DW_ATE_unsigned_fixed);
18302 }
18303 }
18304
18305 switch (encoding)
18306 {
18307 case DW_ATE_address:
18308 /* Turn DW_ATE_address into a void * pointer. */
18309 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18310 type = init_pointer_type (objfile, bits, name, type);
18311 break;
18312 case DW_ATE_boolean:
18313 type = init_boolean_type (objfile, bits, 1, name);
18314 break;
18315 case DW_ATE_complex_float:
18316 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18317 byte_order);
18318 if (type->code () == TYPE_CODE_ERROR)
18319 {
18320 if (name == nullptr)
18321 {
18322 struct obstack *obstack
18323 = &cu->per_objfile->objfile->objfile_obstack;
18324 name = obconcat (obstack, "_Complex ", type->name (),
18325 nullptr);
18326 }
18327 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18328 }
18329 else
18330 type = init_complex_type (name, type);
18331 break;
18332 case DW_ATE_decimal_float:
18333 type = init_decfloat_type (objfile, bits, name);
18334 break;
18335 case DW_ATE_float:
18336 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18337 break;
18338 case DW_ATE_signed:
18339 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18340 break;
18341 case DW_ATE_unsigned:
18342 if (cu->language == language_fortran
18343 && name
18344 && startswith (name, "character("))
18345 type = init_character_type (objfile, bits, 1, name);
18346 else
18347 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18348 break;
18349 case DW_ATE_signed_char:
18350 if (cu->language == language_ada || cu->language == language_m2
18351 || cu->language == language_pascal
18352 || cu->language == language_fortran)
18353 type = init_character_type (objfile, bits, 0, name);
18354 else
18355 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18356 break;
18357 case DW_ATE_unsigned_char:
18358 if (cu->language == language_ada || cu->language == language_m2
18359 || cu->language == language_pascal
18360 || cu->language == language_fortran
18361 || cu->language == language_rust)
18362 type = init_character_type (objfile, bits, 1, name);
18363 else
18364 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18365 break;
18366 case DW_ATE_UTF:
18367 {
18368 if (bits == 16)
18369 type = builtin_type (arch)->builtin_char16;
18370 else if (bits == 32)
18371 type = builtin_type (arch)->builtin_char32;
18372 else
18373 {
18374 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18375 bits);
18376 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18377 }
18378 return set_die_type (die, type, cu);
18379 }
18380 break;
18381 case DW_ATE_signed_fixed:
18382 type = init_fixed_point_type (objfile, bits, 0, name);
18383 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18384 break;
18385 case DW_ATE_unsigned_fixed:
18386 type = init_fixed_point_type (objfile, bits, 1, name);
18387 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18388 break;
18389
18390 default:
18391 complaint (_("unsupported DW_AT_encoding: '%s'"),
18392 dwarf_type_encoding_name (encoding));
18393 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18394 break;
18395 }
18396
18397 if (name && strcmp (name, "char") == 0)
18398 type->set_has_no_signedness (true);
18399
18400 maybe_set_alignment (cu, die, type);
18401
18402 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18403
18404 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18405 {
18406 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18407 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18408 {
18409 unsigned real_bit_size = attr->as_unsigned ();
18410 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18411 /* Only use the attributes if they make sense together. */
18412 if (attr == nullptr
18413 || (attr->as_unsigned () + real_bit_size
18414 <= 8 * TYPE_LENGTH (type)))
18415 {
18416 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18417 = real_bit_size;
18418 if (attr != nullptr)
18419 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18420 = attr->as_unsigned ();
18421 }
18422 }
18423 }
18424
18425 return set_die_type (die, type, cu);
18426 }
18427
18428 /* Parse dwarf attribute if it's a block, reference or constant and put the
18429 resulting value of the attribute into struct bound_prop.
18430 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18431
18432 static int
18433 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18434 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18435 struct type *default_type)
18436 {
18437 struct dwarf2_property_baton *baton;
18438 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18439 struct objfile *objfile = per_objfile->objfile;
18440 struct obstack *obstack = &objfile->objfile_obstack;
18441
18442 gdb_assert (default_type != NULL);
18443
18444 if (attr == NULL || prop == NULL)
18445 return 0;
18446
18447 if (attr->form_is_block ())
18448 {
18449 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18450 baton->property_type = default_type;
18451 baton->locexpr.per_cu = cu->per_cu;
18452 baton->locexpr.per_objfile = per_objfile;
18453
18454 struct dwarf_block *block = attr->as_block ();
18455 baton->locexpr.size = block->size;
18456 baton->locexpr.data = block->data;
18457 switch (attr->name)
18458 {
18459 case DW_AT_string_length:
18460 baton->locexpr.is_reference = true;
18461 break;
18462 default:
18463 baton->locexpr.is_reference = false;
18464 break;
18465 }
18466
18467 prop->set_locexpr (baton);
18468 gdb_assert (prop->baton () != NULL);
18469 }
18470 else if (attr->form_is_ref ())
18471 {
18472 struct dwarf2_cu *target_cu = cu;
18473 struct die_info *target_die;
18474 struct attribute *target_attr;
18475
18476 target_die = follow_die_ref (die, attr, &target_cu);
18477 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18478 if (target_attr == NULL)
18479 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18480 target_cu);
18481 if (target_attr == NULL)
18482 return 0;
18483
18484 switch (target_attr->name)
18485 {
18486 case DW_AT_location:
18487 if (target_attr->form_is_section_offset ())
18488 {
18489 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18490 baton->property_type = die_type (target_die, target_cu);
18491 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18492 prop->set_loclist (baton);
18493 gdb_assert (prop->baton () != NULL);
18494 }
18495 else if (target_attr->form_is_block ())
18496 {
18497 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18498 baton->property_type = die_type (target_die, target_cu);
18499 baton->locexpr.per_cu = cu->per_cu;
18500 baton->locexpr.per_objfile = per_objfile;
18501 struct dwarf_block *block = target_attr->as_block ();
18502 baton->locexpr.size = block->size;
18503 baton->locexpr.data = block->data;
18504 baton->locexpr.is_reference = true;
18505 prop->set_locexpr (baton);
18506 gdb_assert (prop->baton () != NULL);
18507 }
18508 else
18509 {
18510 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18511 "dynamic property");
18512 return 0;
18513 }
18514 break;
18515 case DW_AT_data_member_location:
18516 {
18517 LONGEST offset;
18518
18519 if (!handle_data_member_location (target_die, target_cu,
18520 &offset))
18521 return 0;
18522
18523 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18524 baton->property_type = read_type_die (target_die->parent,
18525 target_cu);
18526 baton->offset_info.offset = offset;
18527 baton->offset_info.type = die_type (target_die, target_cu);
18528 prop->set_addr_offset (baton);
18529 break;
18530 }
18531 }
18532 }
18533 else if (attr->form_is_constant ())
18534 prop->set_const_val (attr->constant_value (0));
18535 else
18536 {
18537 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18538 dwarf2_name (die, cu));
18539 return 0;
18540 }
18541
18542 return 1;
18543 }
18544
18545 /* See read.h. */
18546
18547 struct type *
18548 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18549 {
18550 struct type *int_type;
18551
18552 /* Helper macro to examine the various builtin types. */
18553 #define TRY_TYPE(F) \
18554 int_type = (unsigned_p \
18555 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18556 : objfile_type (objfile)->builtin_ ## F); \
18557 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18558 return int_type
18559
18560 TRY_TYPE (char);
18561 TRY_TYPE (short);
18562 TRY_TYPE (int);
18563 TRY_TYPE (long);
18564 TRY_TYPE (long_long);
18565
18566 #undef TRY_TYPE
18567
18568 gdb_assert_not_reached ("unable to find suitable integer type");
18569 }
18570
18571 /* See read.h. */
18572
18573 struct type *
18574 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18575 {
18576 int addr_size = this->per_cu->addr_size ();
18577 return this->per_objfile->int_type (addr_size, unsigned_p);
18578 }
18579
18580 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18581 present (which is valid) then compute the default type based on the
18582 compilation units address size. */
18583
18584 static struct type *
18585 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18586 {
18587 struct type *index_type = die_type (die, cu);
18588
18589 /* Dwarf-2 specifications explicitly allows to create subrange types
18590 without specifying a base type.
18591 In that case, the base type must be set to the type of
18592 the lower bound, upper bound or count, in that order, if any of these
18593 three attributes references an object that has a type.
18594 If no base type is found, the Dwarf-2 specifications say that
18595 a signed integer type of size equal to the size of an address should
18596 be used.
18597 For the following C code: `extern char gdb_int [];'
18598 GCC produces an empty range DIE.
18599 FIXME: muller/2010-05-28: Possible references to object for low bound,
18600 high bound or count are not yet handled by this code. */
18601 if (index_type->code () == TYPE_CODE_VOID)
18602 index_type = cu->addr_sized_int_type (false);
18603
18604 return index_type;
18605 }
18606
18607 /* Read the given DW_AT_subrange DIE. */
18608
18609 static struct type *
18610 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18611 {
18612 struct type *base_type, *orig_base_type;
18613 struct type *range_type;
18614 struct attribute *attr;
18615 struct dynamic_prop low, high;
18616 int low_default_is_valid;
18617 int high_bound_is_count = 0;
18618 const char *name;
18619 ULONGEST negative_mask;
18620
18621 orig_base_type = read_subrange_index_type (die, cu);
18622
18623 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18624 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18625 creating the range type, but we use the result of check_typedef
18626 when examining properties of the type. */
18627 base_type = check_typedef (orig_base_type);
18628
18629 /* The die_type call above may have already set the type for this DIE. */
18630 range_type = get_die_type (die, cu);
18631 if (range_type)
18632 return range_type;
18633
18634 high.set_const_val (0);
18635
18636 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18637 omitting DW_AT_lower_bound. */
18638 switch (cu->language)
18639 {
18640 case language_c:
18641 case language_cplus:
18642 low.set_const_val (0);
18643 low_default_is_valid = 1;
18644 break;
18645 case language_fortran:
18646 low.set_const_val (1);
18647 low_default_is_valid = 1;
18648 break;
18649 case language_d:
18650 case language_objc:
18651 case language_rust:
18652 low.set_const_val (0);
18653 low_default_is_valid = (cu->header.version >= 4);
18654 break;
18655 case language_ada:
18656 case language_m2:
18657 case language_pascal:
18658 low.set_const_val (1);
18659 low_default_is_valid = (cu->header.version >= 4);
18660 break;
18661 default:
18662 low.set_const_val (0);
18663 low_default_is_valid = 0;
18664 break;
18665 }
18666
18667 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18668 if (attr != nullptr)
18669 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18670 else if (!low_default_is_valid)
18671 complaint (_("Missing DW_AT_lower_bound "
18672 "- DIE at %s [in module %s]"),
18673 sect_offset_str (die->sect_off),
18674 objfile_name (cu->per_objfile->objfile));
18675
18676 struct attribute *attr_ub, *attr_count;
18677 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18678 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18679 {
18680 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18681 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18682 {
18683 /* If bounds are constant do the final calculation here. */
18684 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18685 high.set_const_val (low.const_val () + high.const_val () - 1);
18686 else
18687 high_bound_is_count = 1;
18688 }
18689 else
18690 {
18691 if (attr_ub != NULL)
18692 complaint (_("Unresolved DW_AT_upper_bound "
18693 "- DIE at %s [in module %s]"),
18694 sect_offset_str (die->sect_off),
18695 objfile_name (cu->per_objfile->objfile));
18696 if (attr_count != NULL)
18697 complaint (_("Unresolved DW_AT_count "
18698 "- DIE at %s [in module %s]"),
18699 sect_offset_str (die->sect_off),
18700 objfile_name (cu->per_objfile->objfile));
18701 }
18702 }
18703
18704 LONGEST bias = 0;
18705 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18706 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18707 bias = bias_attr->constant_value (0);
18708
18709 /* Normally, the DWARF producers are expected to use a signed
18710 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18711 But this is unfortunately not always the case, as witnessed
18712 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18713 is used instead. To work around that ambiguity, we treat
18714 the bounds as signed, and thus sign-extend their values, when
18715 the base type is signed. */
18716 negative_mask =
18717 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18718 if (low.kind () == PROP_CONST
18719 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18720 low.set_const_val (low.const_val () | negative_mask);
18721 if (high.kind () == PROP_CONST
18722 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18723 high.set_const_val (high.const_val () | negative_mask);
18724
18725 /* Check for bit and byte strides. */
18726 struct dynamic_prop byte_stride_prop;
18727 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18728 if (attr_byte_stride != nullptr)
18729 {
18730 struct type *prop_type = cu->addr_sized_int_type (false);
18731 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18732 prop_type);
18733 }
18734
18735 struct dynamic_prop bit_stride_prop;
18736 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18737 if (attr_bit_stride != nullptr)
18738 {
18739 /* It only makes sense to have either a bit or byte stride. */
18740 if (attr_byte_stride != nullptr)
18741 {
18742 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18743 "- DIE at %s [in module %s]"),
18744 sect_offset_str (die->sect_off),
18745 objfile_name (cu->per_objfile->objfile));
18746 attr_bit_stride = nullptr;
18747 }
18748 else
18749 {
18750 struct type *prop_type = cu->addr_sized_int_type (false);
18751 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18752 prop_type);
18753 }
18754 }
18755
18756 if (attr_byte_stride != nullptr
18757 || attr_bit_stride != nullptr)
18758 {
18759 bool byte_stride_p = (attr_byte_stride != nullptr);
18760 struct dynamic_prop *stride
18761 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18762
18763 range_type
18764 = create_range_type_with_stride (NULL, orig_base_type, &low,
18765 &high, bias, stride, byte_stride_p);
18766 }
18767 else
18768 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18769
18770 if (high_bound_is_count)
18771 range_type->bounds ()->flag_upper_bound_is_count = 1;
18772
18773 /* Ada expects an empty array on no boundary attributes. */
18774 if (attr == NULL && cu->language != language_ada)
18775 range_type->bounds ()->high.set_undefined ();
18776
18777 name = dwarf2_name (die, cu);
18778 if (name)
18779 range_type->set_name (name);
18780
18781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18782 if (attr != nullptr)
18783 TYPE_LENGTH (range_type) = attr->constant_value (0);
18784
18785 maybe_set_alignment (cu, die, range_type);
18786
18787 set_die_type (die, range_type, cu);
18788
18789 /* set_die_type should be already done. */
18790 set_descriptive_type (range_type, die, cu);
18791
18792 return range_type;
18793 }
18794
18795 static struct type *
18796 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18797 {
18798 struct type *type;
18799
18800 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18801 type->set_name (dwarf2_name (die, cu));
18802
18803 /* In Ada, an unspecified type is typically used when the description
18804 of the type is deferred to a different unit. When encountering
18805 such a type, we treat it as a stub, and try to resolve it later on,
18806 when needed. */
18807 if (cu->language == language_ada)
18808 type->set_is_stub (true);
18809
18810 return set_die_type (die, type, cu);
18811 }
18812
18813 /* Read a single die and all its descendents. Set the die's sibling
18814 field to NULL; set other fields in the die correctly, and set all
18815 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18816 location of the info_ptr after reading all of those dies. PARENT
18817 is the parent of the die in question. */
18818
18819 static struct die_info *
18820 read_die_and_children (const struct die_reader_specs *reader,
18821 const gdb_byte *info_ptr,
18822 const gdb_byte **new_info_ptr,
18823 struct die_info *parent)
18824 {
18825 struct die_info *die;
18826 const gdb_byte *cur_ptr;
18827
18828 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18829 if (die == NULL)
18830 {
18831 *new_info_ptr = cur_ptr;
18832 return NULL;
18833 }
18834 store_in_ref_table (die, reader->cu);
18835
18836 if (die->has_children)
18837 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18838 else
18839 {
18840 die->child = NULL;
18841 *new_info_ptr = cur_ptr;
18842 }
18843
18844 die->sibling = NULL;
18845 die->parent = parent;
18846 return die;
18847 }
18848
18849 /* Read a die, all of its descendents, and all of its siblings; set
18850 all of the fields of all of the dies correctly. Arguments are as
18851 in read_die_and_children. */
18852
18853 static struct die_info *
18854 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18855 const gdb_byte *info_ptr,
18856 const gdb_byte **new_info_ptr,
18857 struct die_info *parent)
18858 {
18859 struct die_info *first_die, *last_sibling;
18860 const gdb_byte *cur_ptr;
18861
18862 cur_ptr = info_ptr;
18863 first_die = last_sibling = NULL;
18864
18865 while (1)
18866 {
18867 struct die_info *die
18868 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
18869
18870 if (die == NULL)
18871 {
18872 *new_info_ptr = cur_ptr;
18873 return first_die;
18874 }
18875
18876 if (!first_die)
18877 first_die = die;
18878 else
18879 last_sibling->sibling = die;
18880
18881 last_sibling = die;
18882 }
18883 }
18884
18885 /* Read a die, all of its descendents, and all of its siblings; set
18886 all of the fields of all of the dies correctly. Arguments are as
18887 in read_die_and_children.
18888 This the main entry point for reading a DIE and all its children. */
18889
18890 static struct die_info *
18891 read_die_and_siblings (const struct die_reader_specs *reader,
18892 const gdb_byte *info_ptr,
18893 const gdb_byte **new_info_ptr,
18894 struct die_info *parent)
18895 {
18896 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18897 new_info_ptr, parent);
18898
18899 if (dwarf_die_debug)
18900 {
18901 fprintf_unfiltered (gdb_stdlog,
18902 "Read die from %s@0x%x of %s:\n",
18903 reader->die_section->get_name (),
18904 (unsigned) (info_ptr - reader->die_section->buffer),
18905 bfd_get_filename (reader->abfd));
18906 dump_die (die, dwarf_die_debug);
18907 }
18908
18909 return die;
18910 }
18911
18912 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18913 attributes.
18914 The caller is responsible for filling in the extra attributes
18915 and updating (*DIEP)->num_attrs.
18916 Set DIEP to point to a newly allocated die with its information,
18917 except for its child, sibling, and parent fields. */
18918
18919 static const gdb_byte *
18920 read_full_die_1 (const struct die_reader_specs *reader,
18921 struct die_info **diep, const gdb_byte *info_ptr,
18922 int num_extra_attrs)
18923 {
18924 unsigned int abbrev_number, bytes_read, i;
18925 const struct abbrev_info *abbrev;
18926 struct die_info *die;
18927 struct dwarf2_cu *cu = reader->cu;
18928 bfd *abfd = reader->abfd;
18929
18930 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18931 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18932 info_ptr += bytes_read;
18933 if (!abbrev_number)
18934 {
18935 *diep = NULL;
18936 return info_ptr;
18937 }
18938
18939 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18940 if (!abbrev)
18941 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18942 abbrev_number,
18943 bfd_get_filename (abfd));
18944
18945 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18946 die->sect_off = sect_off;
18947 die->tag = abbrev->tag;
18948 die->abbrev = abbrev_number;
18949 die->has_children = abbrev->has_children;
18950
18951 /* Make the result usable.
18952 The caller needs to update num_attrs after adding the extra
18953 attributes. */
18954 die->num_attrs = abbrev->num_attrs;
18955
18956 bool any_need_reprocess = false;
18957 for (i = 0; i < abbrev->num_attrs; ++i)
18958 {
18959 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18960 info_ptr);
18961 if (die->attrs[i].requires_reprocessing_p ())
18962 any_need_reprocess = true;
18963 }
18964
18965 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18966 if (attr != nullptr && attr->form_is_unsigned ())
18967 cu->str_offsets_base = attr->as_unsigned ();
18968
18969 attr = die->attr (DW_AT_loclists_base);
18970 if (attr != nullptr)
18971 cu->loclist_base = attr->as_unsigned ();
18972
18973 auto maybe_addr_base = die->addr_base ();
18974 if (maybe_addr_base.has_value ())
18975 cu->addr_base = *maybe_addr_base;
18976
18977 attr = die->attr (DW_AT_rnglists_base);
18978 if (attr != nullptr)
18979 cu->rnglists_base = attr->as_unsigned ();
18980
18981 if (any_need_reprocess)
18982 {
18983 for (i = 0; i < abbrev->num_attrs; ++i)
18984 {
18985 if (die->attrs[i].requires_reprocessing_p ())
18986 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
18987 }
18988 }
18989 *diep = die;
18990 return info_ptr;
18991 }
18992
18993 /* Read a die and all its attributes.
18994 Set DIEP to point to a newly allocated die with its information,
18995 except for its child, sibling, and parent fields. */
18996
18997 static const gdb_byte *
18998 read_full_die (const struct die_reader_specs *reader,
18999 struct die_info **diep, const gdb_byte *info_ptr)
19000 {
19001 const gdb_byte *result;
19002
19003 result = read_full_die_1 (reader, diep, info_ptr, 0);
19004
19005 if (dwarf_die_debug)
19006 {
19007 fprintf_unfiltered (gdb_stdlog,
19008 "Read die from %s@0x%x of %s:\n",
19009 reader->die_section->get_name (),
19010 (unsigned) (info_ptr - reader->die_section->buffer),
19011 bfd_get_filename (reader->abfd));
19012 dump_die (*diep, dwarf_die_debug);
19013 }
19014
19015 return result;
19016 }
19017 \f
19018
19019 /* Returns nonzero if TAG represents a type that we might generate a partial
19020 symbol for. */
19021
19022 static int
19023 is_type_tag_for_partial (int tag, enum language lang)
19024 {
19025 switch (tag)
19026 {
19027 #if 0
19028 /* Some types that would be reasonable to generate partial symbols for,
19029 that we don't at present. Note that normally this does not
19030 matter, mainly because C compilers don't give names to these
19031 types, but instead emit DW_TAG_typedef. */
19032 case DW_TAG_file_type:
19033 case DW_TAG_ptr_to_member_type:
19034 case DW_TAG_set_type:
19035 case DW_TAG_string_type:
19036 case DW_TAG_subroutine_type:
19037 #endif
19038
19039 /* GNAT may emit an array with a name, but no typedef, so we
19040 need to make a symbol in this case. */
19041 case DW_TAG_array_type:
19042 return lang == language_ada;
19043
19044 case DW_TAG_base_type:
19045 case DW_TAG_class_type:
19046 case DW_TAG_interface_type:
19047 case DW_TAG_enumeration_type:
19048 case DW_TAG_structure_type:
19049 case DW_TAG_subrange_type:
19050 case DW_TAG_typedef:
19051 case DW_TAG_union_type:
19052 return 1;
19053 default:
19054 return 0;
19055 }
19056 }
19057
19058 /* Load all DIEs that are interesting for partial symbols into memory. */
19059
19060 static struct partial_die_info *
19061 load_partial_dies (const struct die_reader_specs *reader,
19062 const gdb_byte *info_ptr, int building_psymtab)
19063 {
19064 struct dwarf2_cu *cu = reader->cu;
19065 struct objfile *objfile = cu->per_objfile->objfile;
19066 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19067 unsigned int bytes_read;
19068 unsigned int load_all = 0;
19069 int nesting_level = 1;
19070
19071 parent_die = NULL;
19072 last_die = NULL;
19073
19074 gdb_assert (cu->per_cu != NULL);
19075 if (cu->per_cu->load_all_dies)
19076 load_all = 1;
19077
19078 cu->partial_dies
19079 = htab_create_alloc_ex (cu->header.length / 12,
19080 partial_die_hash,
19081 partial_die_eq,
19082 NULL,
19083 &cu->comp_unit_obstack,
19084 hashtab_obstack_allocate,
19085 dummy_obstack_deallocate);
19086
19087 while (1)
19088 {
19089 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19090 &bytes_read);
19091
19092 /* A NULL abbrev means the end of a series of children. */
19093 if (abbrev == NULL)
19094 {
19095 if (--nesting_level == 0)
19096 return first_die;
19097
19098 info_ptr += bytes_read;
19099 last_die = parent_die;
19100 parent_die = parent_die->die_parent;
19101 continue;
19102 }
19103
19104 /* Check for template arguments. We never save these; if
19105 they're seen, we just mark the parent, and go on our way. */
19106 if (parent_die != NULL
19107 && cu->language == language_cplus
19108 && (abbrev->tag == DW_TAG_template_type_param
19109 || abbrev->tag == DW_TAG_template_value_param))
19110 {
19111 parent_die->has_template_arguments = 1;
19112
19113 if (!load_all)
19114 {
19115 /* We don't need a partial DIE for the template argument. */
19116 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19117 continue;
19118 }
19119 }
19120
19121 /* We only recurse into c++ subprograms looking for template arguments.
19122 Skip their other children. */
19123 if (!load_all
19124 && cu->language == language_cplus
19125 && parent_die != NULL
19126 && parent_die->tag == DW_TAG_subprogram
19127 && abbrev->tag != DW_TAG_inlined_subroutine)
19128 {
19129 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19130 continue;
19131 }
19132
19133 /* Check whether this DIE is interesting enough to save. Normally
19134 we would not be interested in members here, but there may be
19135 later variables referencing them via DW_AT_specification (for
19136 static members). */
19137 if (!load_all
19138 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19139 && abbrev->tag != DW_TAG_constant
19140 && abbrev->tag != DW_TAG_enumerator
19141 && abbrev->tag != DW_TAG_subprogram
19142 && abbrev->tag != DW_TAG_inlined_subroutine
19143 && abbrev->tag != DW_TAG_lexical_block
19144 && abbrev->tag != DW_TAG_variable
19145 && abbrev->tag != DW_TAG_namespace
19146 && abbrev->tag != DW_TAG_module
19147 && abbrev->tag != DW_TAG_member
19148 && abbrev->tag != DW_TAG_imported_unit
19149 && abbrev->tag != DW_TAG_imported_declaration)
19150 {
19151 /* Otherwise we skip to the next sibling, if any. */
19152 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19153 continue;
19154 }
19155
19156 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19157 abbrev);
19158
19159 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19160
19161 /* This two-pass algorithm for processing partial symbols has a
19162 high cost in cache pressure. Thus, handle some simple cases
19163 here which cover the majority of C partial symbols. DIEs
19164 which neither have specification tags in them, nor could have
19165 specification tags elsewhere pointing at them, can simply be
19166 processed and discarded.
19167
19168 This segment is also optional; scan_partial_symbols and
19169 add_partial_symbol will handle these DIEs if we chain
19170 them in normally. When compilers which do not emit large
19171 quantities of duplicate debug information are more common,
19172 this code can probably be removed. */
19173
19174 /* Any complete simple types at the top level (pretty much all
19175 of them, for a language without namespaces), can be processed
19176 directly. */
19177 if (parent_die == NULL
19178 && pdi.has_specification == 0
19179 && pdi.is_declaration == 0
19180 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19181 || pdi.tag == DW_TAG_base_type
19182 || pdi.tag == DW_TAG_array_type
19183 || pdi.tag == DW_TAG_subrange_type))
19184 {
19185 if (building_psymtab && pdi.raw_name != NULL)
19186 add_partial_symbol (&pdi, cu);
19187
19188 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19189 continue;
19190 }
19191
19192 /* The exception for DW_TAG_typedef with has_children above is
19193 a workaround of GCC PR debug/47510. In the case of this complaint
19194 type_name_or_error will error on such types later.
19195
19196 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19197 it could not find the child DIEs referenced later, this is checked
19198 above. In correct DWARF DW_TAG_typedef should have no children. */
19199
19200 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19201 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19202 "- DIE at %s [in module %s]"),
19203 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19204
19205 /* If we're at the second level, and we're an enumerator, and
19206 our parent has no specification (meaning possibly lives in a
19207 namespace elsewhere), then we can add the partial symbol now
19208 instead of queueing it. */
19209 if (pdi.tag == DW_TAG_enumerator
19210 && parent_die != NULL
19211 && parent_die->die_parent == NULL
19212 && parent_die->tag == DW_TAG_enumeration_type
19213 && parent_die->has_specification == 0)
19214 {
19215 if (pdi.raw_name == NULL)
19216 complaint (_("malformed enumerator DIE ignored"));
19217 else if (building_psymtab)
19218 add_partial_symbol (&pdi, cu);
19219
19220 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19221 continue;
19222 }
19223
19224 struct partial_die_info *part_die
19225 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19226
19227 /* We'll save this DIE so link it in. */
19228 part_die->die_parent = parent_die;
19229 part_die->die_sibling = NULL;
19230 part_die->die_child = NULL;
19231
19232 if (last_die && last_die == parent_die)
19233 last_die->die_child = part_die;
19234 else if (last_die)
19235 last_die->die_sibling = part_die;
19236
19237 last_die = part_die;
19238
19239 if (first_die == NULL)
19240 first_die = part_die;
19241
19242 /* Maybe add the DIE to the hash table. Not all DIEs that we
19243 find interesting need to be in the hash table, because we
19244 also have the parent/sibling/child chains; only those that we
19245 might refer to by offset later during partial symbol reading.
19246
19247 For now this means things that might have be the target of a
19248 DW_AT_specification, DW_AT_abstract_origin, or
19249 DW_AT_extension. DW_AT_extension will refer only to
19250 namespaces; DW_AT_abstract_origin refers to functions (and
19251 many things under the function DIE, but we do not recurse
19252 into function DIEs during partial symbol reading) and
19253 possibly variables as well; DW_AT_specification refers to
19254 declarations. Declarations ought to have the DW_AT_declaration
19255 flag. It happens that GCC forgets to put it in sometimes, but
19256 only for functions, not for types.
19257
19258 Adding more things than necessary to the hash table is harmless
19259 except for the performance cost. Adding too few will result in
19260 wasted time in find_partial_die, when we reread the compilation
19261 unit with load_all_dies set. */
19262
19263 if (load_all
19264 || abbrev->tag == DW_TAG_constant
19265 || abbrev->tag == DW_TAG_subprogram
19266 || abbrev->tag == DW_TAG_variable
19267 || abbrev->tag == DW_TAG_namespace
19268 || part_die->is_declaration)
19269 {
19270 void **slot;
19271
19272 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19273 to_underlying (part_die->sect_off),
19274 INSERT);
19275 *slot = part_die;
19276 }
19277
19278 /* For some DIEs we want to follow their children (if any). For C
19279 we have no reason to follow the children of structures; for other
19280 languages we have to, so that we can get at method physnames
19281 to infer fully qualified class names, for DW_AT_specification,
19282 and for C++ template arguments. For C++, we also look one level
19283 inside functions to find template arguments (if the name of the
19284 function does not already contain the template arguments).
19285
19286 For Ada and Fortran, we need to scan the children of subprograms
19287 and lexical blocks as well because these languages allow the
19288 definition of nested entities that could be interesting for the
19289 debugger, such as nested subprograms for instance. */
19290 if (last_die->has_children
19291 && (load_all
19292 || last_die->tag == DW_TAG_namespace
19293 || last_die->tag == DW_TAG_module
19294 || last_die->tag == DW_TAG_enumeration_type
19295 || (cu->language == language_cplus
19296 && last_die->tag == DW_TAG_subprogram
19297 && (last_die->raw_name == NULL
19298 || strchr (last_die->raw_name, '<') == NULL))
19299 || (cu->language != language_c
19300 && (last_die->tag == DW_TAG_class_type
19301 || last_die->tag == DW_TAG_interface_type
19302 || last_die->tag == DW_TAG_structure_type
19303 || last_die->tag == DW_TAG_union_type))
19304 || ((cu->language == language_ada
19305 || cu->language == language_fortran)
19306 && (last_die->tag == DW_TAG_subprogram
19307 || last_die->tag == DW_TAG_lexical_block))))
19308 {
19309 nesting_level++;
19310 parent_die = last_die;
19311 continue;
19312 }
19313
19314 /* Otherwise we skip to the next sibling, if any. */
19315 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19316
19317 /* Back to the top, do it again. */
19318 }
19319 }
19320
19321 partial_die_info::partial_die_info (sect_offset sect_off_,
19322 const struct abbrev_info *abbrev)
19323 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19324 {
19325 }
19326
19327 /* See class definition. */
19328
19329 const char *
19330 partial_die_info::name (dwarf2_cu *cu)
19331 {
19332 if (!canonical_name && raw_name != nullptr)
19333 {
19334 struct objfile *objfile = cu->per_objfile->objfile;
19335 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19336 canonical_name = 1;
19337 }
19338
19339 return raw_name;
19340 }
19341
19342 /* Read a minimal amount of information into the minimal die structure.
19343 INFO_PTR should point just after the initial uleb128 of a DIE. */
19344
19345 const gdb_byte *
19346 partial_die_info::read (const struct die_reader_specs *reader,
19347 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19348 {
19349 struct dwarf2_cu *cu = reader->cu;
19350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19351 unsigned int i;
19352 int has_low_pc_attr = 0;
19353 int has_high_pc_attr = 0;
19354 int high_pc_relative = 0;
19355
19356 for (i = 0; i < abbrev.num_attrs; ++i)
19357 {
19358 attribute attr;
19359 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19360 /* String and address offsets that need to do the reprocessing have
19361 already been read at this point, so there is no need to wait until
19362 the loop terminates to do the reprocessing. */
19363 if (attr.requires_reprocessing_p ())
19364 read_attribute_reprocess (reader, &attr, tag);
19365 /* Store the data if it is of an attribute we want to keep in a
19366 partial symbol table. */
19367 switch (attr.name)
19368 {
19369 case DW_AT_name:
19370 switch (tag)
19371 {
19372 case DW_TAG_compile_unit:
19373 case DW_TAG_partial_unit:
19374 case DW_TAG_type_unit:
19375 /* Compilation units have a DW_AT_name that is a filename, not
19376 a source language identifier. */
19377 case DW_TAG_enumeration_type:
19378 case DW_TAG_enumerator:
19379 /* These tags always have simple identifiers already; no need
19380 to canonicalize them. */
19381 canonical_name = 1;
19382 raw_name = attr.as_string ();
19383 break;
19384 default:
19385 canonical_name = 0;
19386 raw_name = attr.as_string ();
19387 break;
19388 }
19389 break;
19390 case DW_AT_linkage_name:
19391 case DW_AT_MIPS_linkage_name:
19392 /* Note that both forms of linkage name might appear. We
19393 assume they will be the same, and we only store the last
19394 one we see. */
19395 linkage_name = attr.as_string ();
19396 break;
19397 case DW_AT_low_pc:
19398 has_low_pc_attr = 1;
19399 lowpc = attr.as_address ();
19400 break;
19401 case DW_AT_high_pc:
19402 has_high_pc_attr = 1;
19403 highpc = attr.as_address ();
19404 if (cu->header.version >= 4 && attr.form_is_constant ())
19405 high_pc_relative = 1;
19406 break;
19407 case DW_AT_location:
19408 /* Support the .debug_loc offsets. */
19409 if (attr.form_is_block ())
19410 {
19411 d.locdesc = attr.as_block ();
19412 }
19413 else if (attr.form_is_section_offset ())
19414 {
19415 dwarf2_complex_location_expr_complaint ();
19416 }
19417 else
19418 {
19419 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19420 "partial symbol information");
19421 }
19422 break;
19423 case DW_AT_external:
19424 is_external = attr.as_boolean ();
19425 break;
19426 case DW_AT_declaration:
19427 is_declaration = attr.as_boolean ();
19428 break;
19429 case DW_AT_type:
19430 has_type = 1;
19431 break;
19432 case DW_AT_abstract_origin:
19433 case DW_AT_specification:
19434 case DW_AT_extension:
19435 has_specification = 1;
19436 spec_offset = attr.get_ref_die_offset ();
19437 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19438 || cu->per_cu->is_dwz);
19439 break;
19440 case DW_AT_sibling:
19441 /* Ignore absolute siblings, they might point outside of
19442 the current compile unit. */
19443 if (attr.form == DW_FORM_ref_addr)
19444 complaint (_("ignoring absolute DW_AT_sibling"));
19445 else
19446 {
19447 const gdb_byte *buffer = reader->buffer;
19448 sect_offset off = attr.get_ref_die_offset ();
19449 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19450
19451 if (sibling_ptr < info_ptr)
19452 complaint (_("DW_AT_sibling points backwards"));
19453 else if (sibling_ptr > reader->buffer_end)
19454 reader->die_section->overflow_complaint ();
19455 else
19456 sibling = sibling_ptr;
19457 }
19458 break;
19459 case DW_AT_byte_size:
19460 has_byte_size = 1;
19461 break;
19462 case DW_AT_const_value:
19463 has_const_value = 1;
19464 break;
19465 case DW_AT_calling_convention:
19466 /* DWARF doesn't provide a way to identify a program's source-level
19467 entry point. DW_AT_calling_convention attributes are only meant
19468 to describe functions' calling conventions.
19469
19470 However, because it's a necessary piece of information in
19471 Fortran, and before DWARF 4 DW_CC_program was the only
19472 piece of debugging information whose definition refers to
19473 a 'main program' at all, several compilers marked Fortran
19474 main programs with DW_CC_program --- even when those
19475 functions use the standard calling conventions.
19476
19477 Although DWARF now specifies a way to provide this
19478 information, we support this practice for backward
19479 compatibility. */
19480 if (attr.constant_value (0) == DW_CC_program
19481 && cu->language == language_fortran)
19482 main_subprogram = 1;
19483 break;
19484 case DW_AT_inline:
19485 {
19486 LONGEST value = attr.constant_value (-1);
19487 if (value == DW_INL_inlined
19488 || value == DW_INL_declared_inlined)
19489 may_be_inlined = 1;
19490 }
19491 break;
19492
19493 case DW_AT_import:
19494 if (tag == DW_TAG_imported_unit)
19495 {
19496 d.sect_off = attr.get_ref_die_offset ();
19497 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19498 || cu->per_cu->is_dwz);
19499 }
19500 break;
19501
19502 case DW_AT_main_subprogram:
19503 main_subprogram = attr.as_boolean ();
19504 break;
19505
19506 case DW_AT_ranges:
19507 {
19508 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19509 on DWARF version). */
19510 ULONGEST ranges_offset = attr.as_unsigned ();
19511
19512 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19513 this value. */
19514 if (tag != DW_TAG_compile_unit)
19515 ranges_offset += cu->gnu_ranges_base;
19516
19517 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19518 nullptr, tag))
19519 has_pc_info = 1;
19520 }
19521 break;
19522
19523 default:
19524 break;
19525 }
19526 }
19527
19528 /* For Ada, if both the name and the linkage name appear, we prefer
19529 the latter. This lets "catch exception" work better, regardless
19530 of the order in which the name and linkage name were emitted.
19531 Really, though, this is just a workaround for the fact that gdb
19532 doesn't store both the name and the linkage name. */
19533 if (cu->language == language_ada && linkage_name != nullptr)
19534 raw_name = linkage_name;
19535
19536 if (high_pc_relative)
19537 highpc += lowpc;
19538
19539 if (has_low_pc_attr && has_high_pc_attr)
19540 {
19541 /* When using the GNU linker, .gnu.linkonce. sections are used to
19542 eliminate duplicate copies of functions and vtables and such.
19543 The linker will arbitrarily choose one and discard the others.
19544 The AT_*_pc values for such functions refer to local labels in
19545 these sections. If the section from that file was discarded, the
19546 labels are not in the output, so the relocs get a value of 0.
19547 If this is a discarded function, mark the pc bounds as invalid,
19548 so that GDB will ignore it. */
19549 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19550 {
19551 struct objfile *objfile = per_objfile->objfile;
19552 struct gdbarch *gdbarch = objfile->arch ();
19553
19554 complaint (_("DW_AT_low_pc %s is zero "
19555 "for DIE at %s [in module %s]"),
19556 paddress (gdbarch, lowpc),
19557 sect_offset_str (sect_off),
19558 objfile_name (objfile));
19559 }
19560 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19561 else if (lowpc >= highpc)
19562 {
19563 struct objfile *objfile = per_objfile->objfile;
19564 struct gdbarch *gdbarch = objfile->arch ();
19565
19566 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19567 "for DIE at %s [in module %s]"),
19568 paddress (gdbarch, lowpc),
19569 paddress (gdbarch, highpc),
19570 sect_offset_str (sect_off),
19571 objfile_name (objfile));
19572 }
19573 else
19574 has_pc_info = 1;
19575 }
19576
19577 return info_ptr;
19578 }
19579
19580 /* Find a cached partial DIE at OFFSET in CU. */
19581
19582 struct partial_die_info *
19583 dwarf2_cu::find_partial_die (sect_offset sect_off)
19584 {
19585 struct partial_die_info *lookup_die = NULL;
19586 struct partial_die_info part_die (sect_off);
19587
19588 lookup_die = ((struct partial_die_info *)
19589 htab_find_with_hash (partial_dies, &part_die,
19590 to_underlying (sect_off)));
19591
19592 return lookup_die;
19593 }
19594
19595 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19596 except in the case of .debug_types DIEs which do not reference
19597 outside their CU (they do however referencing other types via
19598 DW_FORM_ref_sig8). */
19599
19600 static const struct cu_partial_die_info
19601 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19602 {
19603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19604 struct objfile *objfile = per_objfile->objfile;
19605 struct partial_die_info *pd = NULL;
19606
19607 if (offset_in_dwz == cu->per_cu->is_dwz
19608 && cu->header.offset_in_cu_p (sect_off))
19609 {
19610 pd = cu->find_partial_die (sect_off);
19611 if (pd != NULL)
19612 return { cu, pd };
19613 /* We missed recording what we needed.
19614 Load all dies and try again. */
19615 }
19616 else
19617 {
19618 /* TUs don't reference other CUs/TUs (except via type signatures). */
19619 if (cu->per_cu->is_debug_types)
19620 {
19621 error (_("Dwarf Error: Type Unit at offset %s contains"
19622 " external reference to offset %s [in module %s].\n"),
19623 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19624 bfd_get_filename (objfile->obfd));
19625 }
19626 dwarf2_per_cu_data *per_cu
19627 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19628 per_objfile);
19629
19630 cu = per_objfile->get_cu (per_cu);
19631 if (cu == NULL || cu->partial_dies == NULL)
19632 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19633
19634 cu = per_objfile->get_cu (per_cu);
19635
19636 cu->last_used = 0;
19637 pd = cu->find_partial_die (sect_off);
19638 }
19639
19640 /* If we didn't find it, and not all dies have been loaded,
19641 load them all and try again. */
19642
19643 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19644 {
19645 cu->per_cu->load_all_dies = 1;
19646
19647 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19648 THIS_CU->cu may already be in use. So we can't just free it and
19649 replace its DIEs with the ones we read in. Instead, we leave those
19650 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19651 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19652 set. */
19653 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19654
19655 pd = cu->find_partial_die (sect_off);
19656 }
19657
19658 if (pd == NULL)
19659 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19660 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19661 return { cu, pd };
19662 }
19663
19664 /* See if we can figure out if the class lives in a namespace. We do
19665 this by looking for a member function; its demangled name will
19666 contain namespace info, if there is any. */
19667
19668 static void
19669 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19670 struct dwarf2_cu *cu)
19671 {
19672 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19673 what template types look like, because the demangler
19674 frequently doesn't give the same name as the debug info. We
19675 could fix this by only using the demangled name to get the
19676 prefix (but see comment in read_structure_type). */
19677
19678 struct partial_die_info *real_pdi;
19679 struct partial_die_info *child_pdi;
19680
19681 /* If this DIE (this DIE's specification, if any) has a parent, then
19682 we should not do this. We'll prepend the parent's fully qualified
19683 name when we create the partial symbol. */
19684
19685 real_pdi = struct_pdi;
19686 while (real_pdi->has_specification)
19687 {
19688 auto res = find_partial_die (real_pdi->spec_offset,
19689 real_pdi->spec_is_dwz, cu);
19690 real_pdi = res.pdi;
19691 cu = res.cu;
19692 }
19693
19694 if (real_pdi->die_parent != NULL)
19695 return;
19696
19697 for (child_pdi = struct_pdi->die_child;
19698 child_pdi != NULL;
19699 child_pdi = child_pdi->die_sibling)
19700 {
19701 if (child_pdi->tag == DW_TAG_subprogram
19702 && child_pdi->linkage_name != NULL)
19703 {
19704 gdb::unique_xmalloc_ptr<char> actual_class_name
19705 (cu->language_defn->class_name_from_physname
19706 (child_pdi->linkage_name));
19707 if (actual_class_name != NULL)
19708 {
19709 struct objfile *objfile = cu->per_objfile->objfile;
19710 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19711 struct_pdi->canonical_name = 1;
19712 }
19713 break;
19714 }
19715 }
19716 }
19717
19718 /* Return true if a DIE with TAG may have the DW_AT_const_value
19719 attribute. */
19720
19721 static bool
19722 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19723 {
19724 switch (tag)
19725 {
19726 case DW_TAG_constant:
19727 case DW_TAG_enumerator:
19728 case DW_TAG_formal_parameter:
19729 case DW_TAG_template_value_param:
19730 case DW_TAG_variable:
19731 return true;
19732 }
19733
19734 return false;
19735 }
19736
19737 void
19738 partial_die_info::fixup (struct dwarf2_cu *cu)
19739 {
19740 /* Once we've fixed up a die, there's no point in doing so again.
19741 This also avoids a memory leak if we were to call
19742 guess_partial_die_structure_name multiple times. */
19743 if (fixup_called)
19744 return;
19745
19746 /* If we found a reference attribute and the DIE has no name, try
19747 to find a name in the referred to DIE. */
19748
19749 if (raw_name == NULL && has_specification)
19750 {
19751 struct partial_die_info *spec_die;
19752
19753 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19754 spec_die = res.pdi;
19755 cu = res.cu;
19756
19757 spec_die->fixup (cu);
19758
19759 if (spec_die->raw_name)
19760 {
19761 raw_name = spec_die->raw_name;
19762 canonical_name = spec_die->canonical_name;
19763
19764 /* Copy DW_AT_external attribute if it is set. */
19765 if (spec_die->is_external)
19766 is_external = spec_die->is_external;
19767 }
19768 }
19769
19770 if (!has_const_value && has_specification
19771 && can_have_DW_AT_const_value_p (tag))
19772 {
19773 struct partial_die_info *spec_die;
19774
19775 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19776 spec_die = res.pdi;
19777 cu = res.cu;
19778
19779 spec_die->fixup (cu);
19780
19781 if (spec_die->has_const_value)
19782 {
19783 /* Copy DW_AT_const_value attribute if it is set. */
19784 has_const_value = spec_die->has_const_value;
19785 }
19786 }
19787
19788 /* Set default names for some unnamed DIEs. */
19789
19790 if (raw_name == NULL && tag == DW_TAG_namespace)
19791 {
19792 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19793 canonical_name = 1;
19794 }
19795
19796 /* If there is no parent die to provide a namespace, and there are
19797 children, see if we can determine the namespace from their linkage
19798 name. */
19799 if (cu->language == language_cplus
19800 && !cu->per_objfile->per_bfd->types.empty ()
19801 && die_parent == NULL
19802 && has_children
19803 && (tag == DW_TAG_class_type
19804 || tag == DW_TAG_structure_type
19805 || tag == DW_TAG_union_type))
19806 guess_partial_die_structure_name (this, cu);
19807
19808 /* GCC might emit a nameless struct or union that has a linkage
19809 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19810 if (raw_name == NULL
19811 && (tag == DW_TAG_class_type
19812 || tag == DW_TAG_interface_type
19813 || tag == DW_TAG_structure_type
19814 || tag == DW_TAG_union_type)
19815 && linkage_name != NULL)
19816 {
19817 gdb::unique_xmalloc_ptr<char> demangled
19818 (gdb_demangle (linkage_name, DMGL_TYPES));
19819 if (demangled != nullptr)
19820 {
19821 const char *base;
19822
19823 /* Strip any leading namespaces/classes, keep only the base name.
19824 DW_AT_name for named DIEs does not contain the prefixes. */
19825 base = strrchr (demangled.get (), ':');
19826 if (base && base > demangled.get () && base[-1] == ':')
19827 base++;
19828 else
19829 base = demangled.get ();
19830
19831 struct objfile *objfile = cu->per_objfile->objfile;
19832 raw_name = objfile->intern (base);
19833 canonical_name = 1;
19834 }
19835 }
19836
19837 fixup_called = 1;
19838 }
19839
19840 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19841 contents from the given SECTION in the HEADER.
19842
19843 HEADER_OFFSET is the offset of the header in the section. */
19844 static void
19845 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19846 struct dwarf2_section_info *section,
19847 sect_offset header_offset)
19848 {
19849 unsigned int bytes_read;
19850 bfd *abfd = section->get_bfd_owner ();
19851 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19852
19853 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19854 info_ptr += bytes_read;
19855
19856 header->version = read_2_bytes (abfd, info_ptr);
19857 info_ptr += 2;
19858
19859 header->addr_size = read_1_byte (abfd, info_ptr);
19860 info_ptr += 1;
19861
19862 header->segment_collector_size = read_1_byte (abfd, info_ptr);
19863 info_ptr += 1;
19864
19865 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
19866 }
19867
19868 /* Return the DW_AT_loclists_base value for the CU. */
19869 static ULONGEST
19870 lookup_loclist_base (struct dwarf2_cu *cu)
19871 {
19872 /* For the .dwo unit, the loclist_base points to the first offset following
19873 the header. The header consists of the following entities-
19874 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
19875 bit format)
19876 2. version (2 bytes)
19877 3. address size (1 byte)
19878 4. segment selector size (1 byte)
19879 5. offset entry count (4 bytes)
19880 These sizes are derived as per the DWARFv5 standard. */
19881 if (cu->dwo_unit != nullptr)
19882 {
19883 if (cu->header.initial_length_size == 4)
19884 return LOCLIST_HEADER_SIZE32;
19885 return LOCLIST_HEADER_SIZE64;
19886 }
19887 return cu->loclist_base;
19888 }
19889
19890 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
19891 array of offsets in the .debug_loclists section. */
19892
19893 static sect_offset
19894 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
19895 {
19896 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19897 struct objfile *objfile = per_objfile->objfile;
19898 bfd *abfd = objfile->obfd;
19899 ULONGEST loclist_header_size =
19900 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
19901 : LOCLIST_HEADER_SIZE64);
19902 ULONGEST loclist_base = lookup_loclist_base (cu);
19903
19904 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
19905 ULONGEST start_offset =
19906 loclist_base + loclist_index * cu->header.offset_size;
19907
19908 /* Get loclists section. */
19909 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
19910
19911 /* Read the loclists section content. */
19912 section->read (objfile);
19913 if (section->buffer == NULL)
19914 error (_("DW_FORM_loclistx used without .debug_loclists "
19915 "section [in module %s]"), objfile_name (objfile));
19916
19917 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
19918 so if loclist_base is smaller than the header size, we have a problem. */
19919 if (loclist_base < loclist_header_size)
19920 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
19921 objfile_name (objfile));
19922
19923 /* Read the header of the loclists contribution. */
19924 struct loclists_rnglists_header header;
19925 read_loclists_rnglists_header (&header, section,
19926 (sect_offset) (loclist_base - loclist_header_size));
19927
19928 /* Verify the loclist index is valid. */
19929 if (loclist_index >= header.offset_entry_count)
19930 error (_("DW_FORM_loclistx pointing outside of "
19931 ".debug_loclists offset array [in module %s]"),
19932 objfile_name (objfile));
19933
19934 /* Validate that reading won't go beyond the end of the section. */
19935 if (start_offset + cu->header.offset_size > section->size)
19936 error (_("Reading DW_FORM_loclistx index beyond end of"
19937 ".debug_loclists section [in module %s]"),
19938 objfile_name (objfile));
19939
19940 const gdb_byte *info_ptr = section->buffer + start_offset;
19941
19942 if (cu->header.offset_size == 4)
19943 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
19944 else
19945 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
19946 }
19947
19948 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
19949 array of offsets in the .debug_rnglists section. */
19950
19951 static sect_offset
19952 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
19953 dwarf_tag tag)
19954 {
19955 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19956 struct objfile *objfile = dwarf2_per_objfile->objfile;
19957 bfd *abfd = objfile->obfd;
19958 ULONGEST rnglist_header_size =
19959 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
19960 : RNGLIST_HEADER_SIZE64);
19961
19962 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
19963 .debug_rnglists.dwo section. The rnglists base given in the skeleton
19964 doesn't apply. */
19965 ULONGEST rnglist_base =
19966 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
19967
19968 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
19969 ULONGEST start_offset =
19970 rnglist_base + rnglist_index * cu->header.offset_size;
19971
19972 /* Get rnglists section. */
19973 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
19974
19975 /* Read the rnglists section content. */
19976 section->read (objfile);
19977 if (section->buffer == nullptr)
19978 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
19979 "[in module %s]"),
19980 objfile_name (objfile));
19981
19982 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
19983 so if rnglist_base is smaller than the header size, we have a problem. */
19984 if (rnglist_base < rnglist_header_size)
19985 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
19986 objfile_name (objfile));
19987
19988 /* Read the header of the rnglists contribution. */
19989 struct loclists_rnglists_header header;
19990 read_loclists_rnglists_header (&header, section,
19991 (sect_offset) (rnglist_base - rnglist_header_size));
19992
19993 /* Verify the rnglist index is valid. */
19994 if (rnglist_index >= header.offset_entry_count)
19995 error (_("DW_FORM_rnglistx index pointing outside of "
19996 ".debug_rnglists offset array [in module %s]"),
19997 objfile_name (objfile));
19998
19999 /* Validate that reading won't go beyond the end of the section. */
20000 if (start_offset + cu->header.offset_size > section->size)
20001 error (_("Reading DW_FORM_rnglistx index beyond end of"
20002 ".debug_rnglists section [in module %s]"),
20003 objfile_name (objfile));
20004
20005 const gdb_byte *info_ptr = section->buffer + start_offset;
20006
20007 if (cu->header.offset_size == 4)
20008 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20009 else
20010 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20011 }
20012
20013 /* Process the attributes that had to be skipped in the first round. These
20014 attributes are the ones that need str_offsets_base or addr_base attributes.
20015 They could not have been processed in the first round, because at the time
20016 the values of str_offsets_base or addr_base may not have been known. */
20017 static void
20018 read_attribute_reprocess (const struct die_reader_specs *reader,
20019 struct attribute *attr, dwarf_tag tag)
20020 {
20021 struct dwarf2_cu *cu = reader->cu;
20022 switch (attr->form)
20023 {
20024 case DW_FORM_addrx:
20025 case DW_FORM_GNU_addr_index:
20026 attr->set_address (read_addr_index (cu,
20027 attr->as_unsigned_reprocess ()));
20028 break;
20029 case DW_FORM_loclistx:
20030 {
20031 sect_offset loclists_sect_off
20032 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20033
20034 attr->set_unsigned (to_underlying (loclists_sect_off));
20035 }
20036 break;
20037 case DW_FORM_rnglistx:
20038 {
20039 sect_offset rnglists_sect_off
20040 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20041
20042 attr->set_unsigned (to_underlying (rnglists_sect_off));
20043 }
20044 break;
20045 case DW_FORM_strx:
20046 case DW_FORM_strx1:
20047 case DW_FORM_strx2:
20048 case DW_FORM_strx3:
20049 case DW_FORM_strx4:
20050 case DW_FORM_GNU_str_index:
20051 {
20052 unsigned int str_index = attr->as_unsigned_reprocess ();
20053 gdb_assert (!attr->canonical_string_p ());
20054 if (reader->dwo_file != NULL)
20055 attr->set_string_noncanonical (read_dwo_str_index (reader,
20056 str_index));
20057 else
20058 attr->set_string_noncanonical (read_stub_str_index (cu,
20059 str_index));
20060 break;
20061 }
20062 default:
20063 gdb_assert_not_reached (_("Unexpected DWARF form."));
20064 }
20065 }
20066
20067 /* Read an attribute value described by an attribute form. */
20068
20069 static const gdb_byte *
20070 read_attribute_value (const struct die_reader_specs *reader,
20071 struct attribute *attr, unsigned form,
20072 LONGEST implicit_const, const gdb_byte *info_ptr)
20073 {
20074 struct dwarf2_cu *cu = reader->cu;
20075 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20076 struct objfile *objfile = per_objfile->objfile;
20077 bfd *abfd = reader->abfd;
20078 struct comp_unit_head *cu_header = &cu->header;
20079 unsigned int bytes_read;
20080 struct dwarf_block *blk;
20081
20082 attr->form = (enum dwarf_form) form;
20083 switch (form)
20084 {
20085 case DW_FORM_ref_addr:
20086 if (cu_header->version == 2)
20087 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20088 &bytes_read));
20089 else
20090 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20091 &bytes_read));
20092 info_ptr += bytes_read;
20093 break;
20094 case DW_FORM_GNU_ref_alt:
20095 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20096 &bytes_read));
20097 info_ptr += bytes_read;
20098 break;
20099 case DW_FORM_addr:
20100 {
20101 struct gdbarch *gdbarch = objfile->arch ();
20102 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20103 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20104 attr->set_address (addr);
20105 info_ptr += bytes_read;
20106 }
20107 break;
20108 case DW_FORM_block2:
20109 blk = dwarf_alloc_block (cu);
20110 blk->size = read_2_bytes (abfd, info_ptr);
20111 info_ptr += 2;
20112 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20113 info_ptr += blk->size;
20114 attr->set_block (blk);
20115 break;
20116 case DW_FORM_block4:
20117 blk = dwarf_alloc_block (cu);
20118 blk->size = read_4_bytes (abfd, info_ptr);
20119 info_ptr += 4;
20120 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20121 info_ptr += blk->size;
20122 attr->set_block (blk);
20123 break;
20124 case DW_FORM_data2:
20125 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20126 info_ptr += 2;
20127 break;
20128 case DW_FORM_data4:
20129 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20130 info_ptr += 4;
20131 break;
20132 case DW_FORM_data8:
20133 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20134 info_ptr += 8;
20135 break;
20136 case DW_FORM_data16:
20137 blk = dwarf_alloc_block (cu);
20138 blk->size = 16;
20139 blk->data = read_n_bytes (abfd, info_ptr, 16);
20140 info_ptr += 16;
20141 attr->set_block (blk);
20142 break;
20143 case DW_FORM_sec_offset:
20144 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20145 &bytes_read));
20146 info_ptr += bytes_read;
20147 break;
20148 case DW_FORM_loclistx:
20149 {
20150 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20151 &bytes_read));
20152 info_ptr += bytes_read;
20153 }
20154 break;
20155 case DW_FORM_string:
20156 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20157 &bytes_read));
20158 info_ptr += bytes_read;
20159 break;
20160 case DW_FORM_strp:
20161 if (!cu->per_cu->is_dwz)
20162 {
20163 attr->set_string_noncanonical
20164 (read_indirect_string (per_objfile,
20165 abfd, info_ptr, cu_header,
20166 &bytes_read));
20167 info_ptr += bytes_read;
20168 break;
20169 }
20170 /* FALLTHROUGH */
20171 case DW_FORM_line_strp:
20172 if (!cu->per_cu->is_dwz)
20173 {
20174 attr->set_string_noncanonical
20175 (per_objfile->read_line_string (info_ptr, cu_header,
20176 &bytes_read));
20177 info_ptr += bytes_read;
20178 break;
20179 }
20180 /* FALLTHROUGH */
20181 case DW_FORM_GNU_strp_alt:
20182 {
20183 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20184 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20185 &bytes_read);
20186
20187 attr->set_string_noncanonical
20188 (dwz->read_string (objfile, str_offset));
20189 info_ptr += bytes_read;
20190 }
20191 break;
20192 case DW_FORM_exprloc:
20193 case DW_FORM_block:
20194 blk = dwarf_alloc_block (cu);
20195 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20196 info_ptr += bytes_read;
20197 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20198 info_ptr += blk->size;
20199 attr->set_block (blk);
20200 break;
20201 case DW_FORM_block1:
20202 blk = dwarf_alloc_block (cu);
20203 blk->size = read_1_byte (abfd, info_ptr);
20204 info_ptr += 1;
20205 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20206 info_ptr += blk->size;
20207 attr->set_block (blk);
20208 break;
20209 case DW_FORM_data1:
20210 case DW_FORM_flag:
20211 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20212 info_ptr += 1;
20213 break;
20214 case DW_FORM_flag_present:
20215 attr->set_unsigned (1);
20216 break;
20217 case DW_FORM_sdata:
20218 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20219 info_ptr += bytes_read;
20220 break;
20221 case DW_FORM_rnglistx:
20222 {
20223 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20224 &bytes_read));
20225 info_ptr += bytes_read;
20226 }
20227 break;
20228 case DW_FORM_udata:
20229 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20230 info_ptr += bytes_read;
20231 break;
20232 case DW_FORM_ref1:
20233 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20234 + read_1_byte (abfd, info_ptr)));
20235 info_ptr += 1;
20236 break;
20237 case DW_FORM_ref2:
20238 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20239 + read_2_bytes (abfd, info_ptr)));
20240 info_ptr += 2;
20241 break;
20242 case DW_FORM_ref4:
20243 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20244 + read_4_bytes (abfd, info_ptr)));
20245 info_ptr += 4;
20246 break;
20247 case DW_FORM_ref8:
20248 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20249 + read_8_bytes (abfd, info_ptr)));
20250 info_ptr += 8;
20251 break;
20252 case DW_FORM_ref_sig8:
20253 attr->set_signature (read_8_bytes (abfd, info_ptr));
20254 info_ptr += 8;
20255 break;
20256 case DW_FORM_ref_udata:
20257 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20258 + read_unsigned_leb128 (abfd, info_ptr,
20259 &bytes_read)));
20260 info_ptr += bytes_read;
20261 break;
20262 case DW_FORM_indirect:
20263 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20264 info_ptr += bytes_read;
20265 if (form == DW_FORM_implicit_const)
20266 {
20267 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20268 info_ptr += bytes_read;
20269 }
20270 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20271 info_ptr);
20272 break;
20273 case DW_FORM_implicit_const:
20274 attr->set_signed (implicit_const);
20275 break;
20276 case DW_FORM_addrx:
20277 case DW_FORM_GNU_addr_index:
20278 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20279 &bytes_read));
20280 info_ptr += bytes_read;
20281 break;
20282 case DW_FORM_strx:
20283 case DW_FORM_strx1:
20284 case DW_FORM_strx2:
20285 case DW_FORM_strx3:
20286 case DW_FORM_strx4:
20287 case DW_FORM_GNU_str_index:
20288 {
20289 ULONGEST str_index;
20290 if (form == DW_FORM_strx1)
20291 {
20292 str_index = read_1_byte (abfd, info_ptr);
20293 info_ptr += 1;
20294 }
20295 else if (form == DW_FORM_strx2)
20296 {
20297 str_index = read_2_bytes (abfd, info_ptr);
20298 info_ptr += 2;
20299 }
20300 else if (form == DW_FORM_strx3)
20301 {
20302 str_index = read_3_bytes (abfd, info_ptr);
20303 info_ptr += 3;
20304 }
20305 else if (form == DW_FORM_strx4)
20306 {
20307 str_index = read_4_bytes (abfd, info_ptr);
20308 info_ptr += 4;
20309 }
20310 else
20311 {
20312 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20313 info_ptr += bytes_read;
20314 }
20315 attr->set_unsigned_reprocess (str_index);
20316 }
20317 break;
20318 default:
20319 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20320 dwarf_form_name (form),
20321 bfd_get_filename (abfd));
20322 }
20323
20324 /* Super hack. */
20325 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20326 attr->form = DW_FORM_GNU_ref_alt;
20327
20328 /* We have seen instances where the compiler tried to emit a byte
20329 size attribute of -1 which ended up being encoded as an unsigned
20330 0xffffffff. Although 0xffffffff is technically a valid size value,
20331 an object of this size seems pretty unlikely so we can relatively
20332 safely treat these cases as if the size attribute was invalid and
20333 treat them as zero by default. */
20334 if (attr->name == DW_AT_byte_size
20335 && form == DW_FORM_data4
20336 && attr->as_unsigned () >= 0xffffffff)
20337 {
20338 complaint
20339 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20340 hex_string (attr->as_unsigned ()));
20341 attr->set_unsigned (0);
20342 }
20343
20344 return info_ptr;
20345 }
20346
20347 /* Read an attribute described by an abbreviated attribute. */
20348
20349 static const gdb_byte *
20350 read_attribute (const struct die_reader_specs *reader,
20351 struct attribute *attr, const struct attr_abbrev *abbrev,
20352 const gdb_byte *info_ptr)
20353 {
20354 attr->name = abbrev->name;
20355 attr->string_is_canonical = 0;
20356 attr->requires_reprocessing = 0;
20357 return read_attribute_value (reader, attr, abbrev->form,
20358 abbrev->implicit_const, info_ptr);
20359 }
20360
20361 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20362
20363 static const char *
20364 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20365 LONGEST str_offset)
20366 {
20367 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20368 str_offset, "DW_FORM_strp");
20369 }
20370
20371 /* Return pointer to string at .debug_str offset as read from BUF.
20372 BUF is assumed to be in a compilation unit described by CU_HEADER.
20373 Return *BYTES_READ_PTR count of bytes read from BUF. */
20374
20375 static const char *
20376 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20377 const gdb_byte *buf,
20378 const struct comp_unit_head *cu_header,
20379 unsigned int *bytes_read_ptr)
20380 {
20381 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20382
20383 return read_indirect_string_at_offset (per_objfile, str_offset);
20384 }
20385
20386 /* See read.h. */
20387
20388 const char *
20389 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20390 const struct comp_unit_head *cu_header,
20391 unsigned int *bytes_read_ptr)
20392 {
20393 bfd *abfd = objfile->obfd;
20394 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20395
20396 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20397 }
20398
20399 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20400 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20401 ADDR_SIZE is the size of addresses from the CU header. */
20402
20403 static CORE_ADDR
20404 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20405 gdb::optional<ULONGEST> addr_base, int addr_size)
20406 {
20407 struct objfile *objfile = per_objfile->objfile;
20408 bfd *abfd = objfile->obfd;
20409 const gdb_byte *info_ptr;
20410 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20411
20412 per_objfile->per_bfd->addr.read (objfile);
20413 if (per_objfile->per_bfd->addr.buffer == NULL)
20414 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20415 objfile_name (objfile));
20416 if (addr_base_or_zero + addr_index * addr_size
20417 >= per_objfile->per_bfd->addr.size)
20418 error (_("DW_FORM_addr_index pointing outside of "
20419 ".debug_addr section [in module %s]"),
20420 objfile_name (objfile));
20421 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20422 + addr_index * addr_size);
20423 if (addr_size == 4)
20424 return bfd_get_32 (abfd, info_ptr);
20425 else
20426 return bfd_get_64 (abfd, info_ptr);
20427 }
20428
20429 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20430
20431 static CORE_ADDR
20432 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20433 {
20434 return read_addr_index_1 (cu->per_objfile, addr_index,
20435 cu->addr_base, cu->header.addr_size);
20436 }
20437
20438 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20439
20440 static CORE_ADDR
20441 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20442 unsigned int *bytes_read)
20443 {
20444 bfd *abfd = cu->per_objfile->objfile->obfd;
20445 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20446
20447 return read_addr_index (cu, addr_index);
20448 }
20449
20450 /* See read.h. */
20451
20452 CORE_ADDR
20453 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20454 dwarf2_per_objfile *per_objfile,
20455 unsigned int addr_index)
20456 {
20457 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20458 gdb::optional<ULONGEST> addr_base;
20459 int addr_size;
20460
20461 /* We need addr_base and addr_size.
20462 If we don't have PER_CU->cu, we have to get it.
20463 Nasty, but the alternative is storing the needed info in PER_CU,
20464 which at this point doesn't seem justified: it's not clear how frequently
20465 it would get used and it would increase the size of every PER_CU.
20466 Entry points like dwarf2_per_cu_addr_size do a similar thing
20467 so we're not in uncharted territory here.
20468 Alas we need to be a bit more complicated as addr_base is contained
20469 in the DIE.
20470
20471 We don't need to read the entire CU(/TU).
20472 We just need the header and top level die.
20473
20474 IWBN to use the aging mechanism to let us lazily later discard the CU.
20475 For now we skip this optimization. */
20476
20477 if (cu != NULL)
20478 {
20479 addr_base = cu->addr_base;
20480 addr_size = cu->header.addr_size;
20481 }
20482 else
20483 {
20484 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20485 addr_base = reader.cu->addr_base;
20486 addr_size = reader.cu->header.addr_size;
20487 }
20488
20489 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20490 }
20491
20492 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20493 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20494 DWO file. */
20495
20496 static const char *
20497 read_str_index (struct dwarf2_cu *cu,
20498 struct dwarf2_section_info *str_section,
20499 struct dwarf2_section_info *str_offsets_section,
20500 ULONGEST str_offsets_base, ULONGEST str_index)
20501 {
20502 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20503 struct objfile *objfile = per_objfile->objfile;
20504 const char *objf_name = objfile_name (objfile);
20505 bfd *abfd = objfile->obfd;
20506 const gdb_byte *info_ptr;
20507 ULONGEST str_offset;
20508 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20509
20510 str_section->read (objfile);
20511 str_offsets_section->read (objfile);
20512 if (str_section->buffer == NULL)
20513 error (_("%s used without %s section"
20514 " in CU at offset %s [in module %s]"),
20515 form_name, str_section->get_name (),
20516 sect_offset_str (cu->header.sect_off), objf_name);
20517 if (str_offsets_section->buffer == NULL)
20518 error (_("%s used without %s section"
20519 " in CU at offset %s [in module %s]"),
20520 form_name, str_section->get_name (),
20521 sect_offset_str (cu->header.sect_off), objf_name);
20522 info_ptr = (str_offsets_section->buffer
20523 + str_offsets_base
20524 + str_index * cu->header.offset_size);
20525 if (cu->header.offset_size == 4)
20526 str_offset = bfd_get_32 (abfd, info_ptr);
20527 else
20528 str_offset = bfd_get_64 (abfd, info_ptr);
20529 if (str_offset >= str_section->size)
20530 error (_("Offset from %s pointing outside of"
20531 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20532 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20533 return (const char *) (str_section->buffer + str_offset);
20534 }
20535
20536 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20537
20538 static const char *
20539 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20540 {
20541 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20542 ? reader->cu->header.addr_size : 0;
20543 return read_str_index (reader->cu,
20544 &reader->dwo_file->sections.str,
20545 &reader->dwo_file->sections.str_offsets,
20546 str_offsets_base, str_index);
20547 }
20548
20549 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20550
20551 static const char *
20552 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20553 {
20554 struct objfile *objfile = cu->per_objfile->objfile;
20555 const char *objf_name = objfile_name (objfile);
20556 static const char form_name[] = "DW_FORM_GNU_str_index";
20557 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20558
20559 if (!cu->str_offsets_base.has_value ())
20560 error (_("%s used in Fission stub without %s"
20561 " in CU at offset 0x%lx [in module %s]"),
20562 form_name, str_offsets_attr_name,
20563 (long) cu->header.offset_size, objf_name);
20564
20565 return read_str_index (cu,
20566 &cu->per_objfile->per_bfd->str,
20567 &cu->per_objfile->per_bfd->str_offsets,
20568 *cu->str_offsets_base, str_index);
20569 }
20570
20571 /* Return the length of an LEB128 number in BUF. */
20572
20573 static int
20574 leb128_size (const gdb_byte *buf)
20575 {
20576 const gdb_byte *begin = buf;
20577 gdb_byte byte;
20578
20579 while (1)
20580 {
20581 byte = *buf++;
20582 if ((byte & 128) == 0)
20583 return buf - begin;
20584 }
20585 }
20586
20587 static void
20588 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20589 {
20590 switch (lang)
20591 {
20592 case DW_LANG_C89:
20593 case DW_LANG_C99:
20594 case DW_LANG_C11:
20595 case DW_LANG_C:
20596 case DW_LANG_UPC:
20597 cu->language = language_c;
20598 break;
20599 case DW_LANG_Java:
20600 case DW_LANG_C_plus_plus:
20601 case DW_LANG_C_plus_plus_11:
20602 case DW_LANG_C_plus_plus_14:
20603 cu->language = language_cplus;
20604 break;
20605 case DW_LANG_D:
20606 cu->language = language_d;
20607 break;
20608 case DW_LANG_Fortran77:
20609 case DW_LANG_Fortran90:
20610 case DW_LANG_Fortran95:
20611 case DW_LANG_Fortran03:
20612 case DW_LANG_Fortran08:
20613 cu->language = language_fortran;
20614 break;
20615 case DW_LANG_Go:
20616 cu->language = language_go;
20617 break;
20618 case DW_LANG_Mips_Assembler:
20619 cu->language = language_asm;
20620 break;
20621 case DW_LANG_Ada83:
20622 case DW_LANG_Ada95:
20623 cu->language = language_ada;
20624 break;
20625 case DW_LANG_Modula2:
20626 cu->language = language_m2;
20627 break;
20628 case DW_LANG_Pascal83:
20629 cu->language = language_pascal;
20630 break;
20631 case DW_LANG_ObjC:
20632 cu->language = language_objc;
20633 break;
20634 case DW_LANG_Rust:
20635 case DW_LANG_Rust_old:
20636 cu->language = language_rust;
20637 break;
20638 case DW_LANG_Cobol74:
20639 case DW_LANG_Cobol85:
20640 default:
20641 cu->language = language_minimal;
20642 break;
20643 }
20644 cu->language_defn = language_def (cu->language);
20645 }
20646
20647 /* Return the named attribute or NULL if not there. */
20648
20649 static struct attribute *
20650 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20651 {
20652 for (;;)
20653 {
20654 unsigned int i;
20655 struct attribute *spec = NULL;
20656
20657 for (i = 0; i < die->num_attrs; ++i)
20658 {
20659 if (die->attrs[i].name == name)
20660 return &die->attrs[i];
20661 if (die->attrs[i].name == DW_AT_specification
20662 || die->attrs[i].name == DW_AT_abstract_origin)
20663 spec = &die->attrs[i];
20664 }
20665
20666 if (!spec)
20667 break;
20668
20669 die = follow_die_ref (die, spec, &cu);
20670 }
20671
20672 return NULL;
20673 }
20674
20675 /* Return the string associated with a string-typed attribute, or NULL if it
20676 is either not found or is of an incorrect type. */
20677
20678 static const char *
20679 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20680 {
20681 struct attribute *attr;
20682 const char *str = NULL;
20683
20684 attr = dwarf2_attr (die, name, cu);
20685
20686 if (attr != NULL)
20687 {
20688 str = attr->as_string ();
20689 if (str == nullptr)
20690 complaint (_("string type expected for attribute %s for "
20691 "DIE at %s in module %s"),
20692 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20693 objfile_name (cu->per_objfile->objfile));
20694 }
20695
20696 return str;
20697 }
20698
20699 /* Return the dwo name or NULL if not present. If present, it is in either
20700 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20701 static const char *
20702 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20703 {
20704 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20705 if (dwo_name == nullptr)
20706 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20707 return dwo_name;
20708 }
20709
20710 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20711 and holds a non-zero value. This function should only be used for
20712 DW_FORM_flag or DW_FORM_flag_present attributes. */
20713
20714 static int
20715 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20716 {
20717 struct attribute *attr = dwarf2_attr (die, name, cu);
20718
20719 return attr != nullptr && attr->as_boolean ();
20720 }
20721
20722 static int
20723 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20724 {
20725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20726 which value is non-zero. However, we have to be careful with
20727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20728 (via dwarf2_flag_true_p) follows this attribute. So we may
20729 end up accidently finding a declaration attribute that belongs
20730 to a different DIE referenced by the specification attribute,
20731 even though the given DIE does not have a declaration attribute. */
20732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20734 }
20735
20736 /* Return the die giving the specification for DIE, if there is
20737 one. *SPEC_CU is the CU containing DIE on input, and the CU
20738 containing the return value on output. If there is no
20739 specification, but there is an abstract origin, that is
20740 returned. */
20741
20742 static struct die_info *
20743 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20744 {
20745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20746 *spec_cu);
20747
20748 if (spec_attr == NULL)
20749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20750
20751 if (spec_attr == NULL)
20752 return NULL;
20753 else
20754 return follow_die_ref (die, spec_attr, spec_cu);
20755 }
20756
20757 /* Stub for free_line_header to match void * callback types. */
20758
20759 static void
20760 free_line_header_voidp (void *arg)
20761 {
20762 struct line_header *lh = (struct line_header *) arg;
20763
20764 delete lh;
20765 }
20766
20767 /* A convenience function to find the proper .debug_line section for a CU. */
20768
20769 static struct dwarf2_section_info *
20770 get_debug_line_section (struct dwarf2_cu *cu)
20771 {
20772 struct dwarf2_section_info *section;
20773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20774
20775 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20776 DWO file. */
20777 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20778 section = &cu->dwo_unit->dwo_file->sections.line;
20779 else if (cu->per_cu->is_dwz)
20780 {
20781 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20782
20783 section = &dwz->line;
20784 }
20785 else
20786 section = &per_objfile->per_bfd->line;
20787
20788 return section;
20789 }
20790
20791 /* Read the statement program header starting at OFFSET in
20792 .debug_line, or .debug_line.dwo. Return a pointer
20793 to a struct line_header, allocated using xmalloc.
20794 Returns NULL if there is a problem reading the header, e.g., if it
20795 has a version we don't understand.
20796
20797 NOTE: the strings in the include directory and file name tables of
20798 the returned object point into the dwarf line section buffer,
20799 and must not be freed. */
20800
20801 static line_header_up
20802 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20803 {
20804 struct dwarf2_section_info *section;
20805 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20806
20807 section = get_debug_line_section (cu);
20808 section->read (per_objfile->objfile);
20809 if (section->buffer == NULL)
20810 {
20811 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20812 complaint (_("missing .debug_line.dwo section"));
20813 else
20814 complaint (_("missing .debug_line section"));
20815 return 0;
20816 }
20817
20818 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20819 per_objfile, section, &cu->header);
20820 }
20821
20822 /* Subroutine of dwarf_decode_lines to simplify it.
20823 Return the file name of the psymtab for the given file_entry.
20824 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20825 If space for the result is malloc'd, *NAME_HOLDER will be set.
20826 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20827
20828 static const char *
20829 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20830 const dwarf2_psymtab *pst,
20831 const char *comp_dir,
20832 gdb::unique_xmalloc_ptr<char> *name_holder)
20833 {
20834 const char *include_name = fe.name;
20835 const char *include_name_to_compare = include_name;
20836 const char *pst_filename;
20837 int file_is_pst;
20838
20839 const char *dir_name = fe.include_dir (lh);
20840
20841 gdb::unique_xmalloc_ptr<char> hold_compare;
20842 if (!IS_ABSOLUTE_PATH (include_name)
20843 && (dir_name != NULL || comp_dir != NULL))
20844 {
20845 /* Avoid creating a duplicate psymtab for PST.
20846 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20847 Before we do the comparison, however, we need to account
20848 for DIR_NAME and COMP_DIR.
20849 First prepend dir_name (if non-NULL). If we still don't
20850 have an absolute path prepend comp_dir (if non-NULL).
20851 However, the directory we record in the include-file's
20852 psymtab does not contain COMP_DIR (to match the
20853 corresponding symtab(s)).
20854
20855 Example:
20856
20857 bash$ cd /tmp
20858 bash$ gcc -g ./hello.c
20859 include_name = "hello.c"
20860 dir_name = "."
20861 DW_AT_comp_dir = comp_dir = "/tmp"
20862 DW_AT_name = "./hello.c"
20863
20864 */
20865
20866 if (dir_name != NULL)
20867 {
20868 name_holder->reset (concat (dir_name, SLASH_STRING,
20869 include_name, (char *) NULL));
20870 include_name = name_holder->get ();
20871 include_name_to_compare = include_name;
20872 }
20873 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20874 {
20875 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20876 include_name, (char *) NULL));
20877 include_name_to_compare = hold_compare.get ();
20878 }
20879 }
20880
20881 pst_filename = pst->filename;
20882 gdb::unique_xmalloc_ptr<char> copied_name;
20883 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20884 {
20885 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20886 pst_filename, (char *) NULL));
20887 pst_filename = copied_name.get ();
20888 }
20889
20890 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
20891
20892 if (file_is_pst)
20893 return NULL;
20894 return include_name;
20895 }
20896
20897 /* State machine to track the state of the line number program. */
20898
20899 class lnp_state_machine
20900 {
20901 public:
20902 /* Initialize a machine state for the start of a line number
20903 program. */
20904 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20905 bool record_lines_p);
20906
20907 file_entry *current_file ()
20908 {
20909 /* lh->file_names is 0-based, but the file name numbers in the
20910 statement program are 1-based. */
20911 return m_line_header->file_name_at (m_file);
20912 }
20913
20914 /* Record the line in the state machine. END_SEQUENCE is true if
20915 we're processing the end of a sequence. */
20916 void record_line (bool end_sequence);
20917
20918 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
20919 nop-out rest of the lines in this sequence. */
20920 void check_line_address (struct dwarf2_cu *cu,
20921 const gdb_byte *line_ptr,
20922 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
20923
20924 void handle_set_discriminator (unsigned int discriminator)
20925 {
20926 m_discriminator = discriminator;
20927 m_line_has_non_zero_discriminator |= discriminator != 0;
20928 }
20929
20930 /* Handle DW_LNE_set_address. */
20931 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20932 {
20933 m_op_index = 0;
20934 address += baseaddr;
20935 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20936 }
20937
20938 /* Handle DW_LNS_advance_pc. */
20939 void handle_advance_pc (CORE_ADDR adjust);
20940
20941 /* Handle a special opcode. */
20942 void handle_special_opcode (unsigned char op_code);
20943
20944 /* Handle DW_LNS_advance_line. */
20945 void handle_advance_line (int line_delta)
20946 {
20947 advance_line (line_delta);
20948 }
20949
20950 /* Handle DW_LNS_set_file. */
20951 void handle_set_file (file_name_index file);
20952
20953 /* Handle DW_LNS_negate_stmt. */
20954 void handle_negate_stmt ()
20955 {
20956 m_is_stmt = !m_is_stmt;
20957 }
20958
20959 /* Handle DW_LNS_const_add_pc. */
20960 void handle_const_add_pc ();
20961
20962 /* Handle DW_LNS_fixed_advance_pc. */
20963 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20964 {
20965 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20966 m_op_index = 0;
20967 }
20968
20969 /* Handle DW_LNS_copy. */
20970 void handle_copy ()
20971 {
20972 record_line (false);
20973 m_discriminator = 0;
20974 }
20975
20976 /* Handle DW_LNE_end_sequence. */
20977 void handle_end_sequence ()
20978 {
20979 m_currently_recording_lines = true;
20980 }
20981
20982 private:
20983 /* Advance the line by LINE_DELTA. */
20984 void advance_line (int line_delta)
20985 {
20986 m_line += line_delta;
20987
20988 if (line_delta != 0)
20989 m_line_has_non_zero_discriminator = m_discriminator != 0;
20990 }
20991
20992 struct dwarf2_cu *m_cu;
20993
20994 gdbarch *m_gdbarch;
20995
20996 /* True if we're recording lines.
20997 Otherwise we're building partial symtabs and are just interested in
20998 finding include files mentioned by the line number program. */
20999 bool m_record_lines_p;
21000
21001 /* The line number header. */
21002 line_header *m_line_header;
21003
21004 /* These are part of the standard DWARF line number state machine,
21005 and initialized according to the DWARF spec. */
21006
21007 unsigned char m_op_index = 0;
21008 /* The line table index of the current file. */
21009 file_name_index m_file = 1;
21010 unsigned int m_line = 1;
21011
21012 /* These are initialized in the constructor. */
21013
21014 CORE_ADDR m_address;
21015 bool m_is_stmt;
21016 unsigned int m_discriminator;
21017
21018 /* Additional bits of state we need to track. */
21019
21020 /* The last file that we called dwarf2_start_subfile for.
21021 This is only used for TLLs. */
21022 unsigned int m_last_file = 0;
21023 /* The last file a line number was recorded for. */
21024 struct subfile *m_last_subfile = NULL;
21025
21026 /* The address of the last line entry. */
21027 CORE_ADDR m_last_address;
21028
21029 /* Set to true when a previous line at the same address (using
21030 m_last_address) had m_is_stmt true. This is reset to false when a
21031 line entry at a new address (m_address different to m_last_address) is
21032 processed. */
21033 bool m_stmt_at_address = false;
21034
21035 /* When true, record the lines we decode. */
21036 bool m_currently_recording_lines = false;
21037
21038 /* The last line number that was recorded, used to coalesce
21039 consecutive entries for the same line. This can happen, for
21040 example, when discriminators are present. PR 17276. */
21041 unsigned int m_last_line = 0;
21042 bool m_line_has_non_zero_discriminator = false;
21043 };
21044
21045 void
21046 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21047 {
21048 CORE_ADDR addr_adj = (((m_op_index + adjust)
21049 / m_line_header->maximum_ops_per_instruction)
21050 * m_line_header->minimum_instruction_length);
21051 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21052 m_op_index = ((m_op_index + adjust)
21053 % m_line_header->maximum_ops_per_instruction);
21054 }
21055
21056 void
21057 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21058 {
21059 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21060 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21061 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21062 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21063 / m_line_header->maximum_ops_per_instruction)
21064 * m_line_header->minimum_instruction_length);
21065 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21066 m_op_index = ((m_op_index + adj_opcode_d)
21067 % m_line_header->maximum_ops_per_instruction);
21068
21069 int line_delta = m_line_header->line_base + adj_opcode_r;
21070 advance_line (line_delta);
21071 record_line (false);
21072 m_discriminator = 0;
21073 }
21074
21075 void
21076 lnp_state_machine::handle_set_file (file_name_index file)
21077 {
21078 m_file = file;
21079
21080 const file_entry *fe = current_file ();
21081 if (fe == NULL)
21082 dwarf2_debug_line_missing_file_complaint ();
21083 else if (m_record_lines_p)
21084 {
21085 const char *dir = fe->include_dir (m_line_header);
21086
21087 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21088 m_line_has_non_zero_discriminator = m_discriminator != 0;
21089 dwarf2_start_subfile (m_cu, fe->name, dir);
21090 }
21091 }
21092
21093 void
21094 lnp_state_machine::handle_const_add_pc ()
21095 {
21096 CORE_ADDR adjust
21097 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21098
21099 CORE_ADDR addr_adj
21100 = (((m_op_index + adjust)
21101 / m_line_header->maximum_ops_per_instruction)
21102 * m_line_header->minimum_instruction_length);
21103
21104 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21105 m_op_index = ((m_op_index + adjust)
21106 % m_line_header->maximum_ops_per_instruction);
21107 }
21108
21109 /* Return non-zero if we should add LINE to the line number table.
21110 LINE is the line to add, LAST_LINE is the last line that was added,
21111 LAST_SUBFILE is the subfile for LAST_LINE.
21112 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21113 had a non-zero discriminator.
21114
21115 We have to be careful in the presence of discriminators.
21116 E.g., for this line:
21117
21118 for (i = 0; i < 100000; i++);
21119
21120 clang can emit four line number entries for that one line,
21121 each with a different discriminator.
21122 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21123
21124 However, we want gdb to coalesce all four entries into one.
21125 Otherwise the user could stepi into the middle of the line and
21126 gdb would get confused about whether the pc really was in the
21127 middle of the line.
21128
21129 Things are further complicated by the fact that two consecutive
21130 line number entries for the same line is a heuristic used by gcc
21131 to denote the end of the prologue. So we can't just discard duplicate
21132 entries, we have to be selective about it. The heuristic we use is
21133 that we only collapse consecutive entries for the same line if at least
21134 one of those entries has a non-zero discriminator. PR 17276.
21135
21136 Note: Addresses in the line number state machine can never go backwards
21137 within one sequence, thus this coalescing is ok. */
21138
21139 static int
21140 dwarf_record_line_p (struct dwarf2_cu *cu,
21141 unsigned int line, unsigned int last_line,
21142 int line_has_non_zero_discriminator,
21143 struct subfile *last_subfile)
21144 {
21145 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21146 return 1;
21147 if (line != last_line)
21148 return 1;
21149 /* Same line for the same file that we've seen already.
21150 As a last check, for pr 17276, only record the line if the line
21151 has never had a non-zero discriminator. */
21152 if (!line_has_non_zero_discriminator)
21153 return 1;
21154 return 0;
21155 }
21156
21157 /* Use the CU's builder to record line number LINE beginning at
21158 address ADDRESS in the line table of subfile SUBFILE. */
21159
21160 static void
21161 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21162 unsigned int line, CORE_ADDR address, bool is_stmt,
21163 struct dwarf2_cu *cu)
21164 {
21165 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21166
21167 if (dwarf_line_debug)
21168 {
21169 fprintf_unfiltered (gdb_stdlog,
21170 "Recording line %u, file %s, address %s\n",
21171 line, lbasename (subfile->name),
21172 paddress (gdbarch, address));
21173 }
21174
21175 if (cu != nullptr)
21176 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21177 }
21178
21179 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21180 Mark the end of a set of line number records.
21181 The arguments are the same as for dwarf_record_line_1.
21182 If SUBFILE is NULL the request is ignored. */
21183
21184 static void
21185 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21186 CORE_ADDR address, struct dwarf2_cu *cu)
21187 {
21188 if (subfile == NULL)
21189 return;
21190
21191 if (dwarf_line_debug)
21192 {
21193 fprintf_unfiltered (gdb_stdlog,
21194 "Finishing current line, file %s, address %s\n",
21195 lbasename (subfile->name),
21196 paddress (gdbarch, address));
21197 }
21198
21199 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21200 }
21201
21202 void
21203 lnp_state_machine::record_line (bool end_sequence)
21204 {
21205 if (dwarf_line_debug)
21206 {
21207 fprintf_unfiltered (gdb_stdlog,
21208 "Processing actual line %u: file %u,"
21209 " address %s, is_stmt %u, discrim %u%s\n",
21210 m_line, m_file,
21211 paddress (m_gdbarch, m_address),
21212 m_is_stmt, m_discriminator,
21213 (end_sequence ? "\t(end sequence)" : ""));
21214 }
21215
21216 file_entry *fe = current_file ();
21217
21218 if (fe == NULL)
21219 dwarf2_debug_line_missing_file_complaint ();
21220 /* For now we ignore lines not starting on an instruction boundary.
21221 But not when processing end_sequence for compatibility with the
21222 previous version of the code. */
21223 else if (m_op_index == 0 || end_sequence)
21224 {
21225 fe->included_p = 1;
21226 if (m_record_lines_p)
21227 {
21228 /* When we switch files we insert an end maker in the first file,
21229 switch to the second file and add a new line entry. The
21230 problem is that the end marker inserted in the first file will
21231 discard any previous line entries at the same address. If the
21232 line entries in the first file are marked as is-stmt, while
21233 the new line in the second file is non-stmt, then this means
21234 the end marker will discard is-stmt lines so we can have a
21235 non-stmt line. This means that there are less addresses at
21236 which the user can insert a breakpoint.
21237
21238 To improve this we track the last address in m_last_address,
21239 and whether we have seen an is-stmt at this address. Then
21240 when switching files, if we have seen a stmt at the current
21241 address, and we are switching to create a non-stmt line, then
21242 discard the new line. */
21243 bool file_changed
21244 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21245 bool ignore_this_line
21246 = ((file_changed && !end_sequence && m_last_address == m_address
21247 && !m_is_stmt && m_stmt_at_address)
21248 || (!end_sequence && m_line == 0));
21249
21250 if ((file_changed && !ignore_this_line) || end_sequence)
21251 {
21252 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21253 m_currently_recording_lines ? m_cu : nullptr);
21254 }
21255
21256 if (!end_sequence && !ignore_this_line)
21257 {
21258 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21259
21260 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21261 m_line_has_non_zero_discriminator,
21262 m_last_subfile))
21263 {
21264 buildsym_compunit *builder = m_cu->get_builder ();
21265 dwarf_record_line_1 (m_gdbarch,
21266 builder->get_current_subfile (),
21267 m_line, m_address, is_stmt,
21268 m_currently_recording_lines ? m_cu : nullptr);
21269 }
21270 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21271 m_last_line = m_line;
21272 }
21273 }
21274 }
21275
21276 /* Track whether we have seen any m_is_stmt true at m_address in case we
21277 have multiple line table entries all at m_address. */
21278 if (m_last_address != m_address)
21279 {
21280 m_stmt_at_address = false;
21281 m_last_address = m_address;
21282 }
21283 m_stmt_at_address |= m_is_stmt;
21284 }
21285
21286 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21287 line_header *lh, bool record_lines_p)
21288 {
21289 m_cu = cu;
21290 m_gdbarch = arch;
21291 m_record_lines_p = record_lines_p;
21292 m_line_header = lh;
21293
21294 m_currently_recording_lines = true;
21295
21296 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21297 was a line entry for it so that the backend has a chance to adjust it
21298 and also record it in case it needs it. This is currently used by MIPS
21299 code, cf. `mips_adjust_dwarf2_line'. */
21300 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21301 m_is_stmt = lh->default_is_stmt;
21302 m_discriminator = 0;
21303
21304 m_last_address = m_address;
21305 m_stmt_at_address = false;
21306 }
21307
21308 void
21309 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21310 const gdb_byte *line_ptr,
21311 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21312 {
21313 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21314 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21315 located at 0x0. In this case, additionally check that if
21316 ADDRESS < UNRELOCATED_LOWPC. */
21317
21318 if ((address == 0 && address < unrelocated_lowpc)
21319 || address == (CORE_ADDR) -1)
21320 {
21321 /* This line table is for a function which has been
21322 GCd by the linker. Ignore it. PR gdb/12528 */
21323
21324 struct objfile *objfile = cu->per_objfile->objfile;
21325 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21326
21327 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21328 line_offset, objfile_name (objfile));
21329 m_currently_recording_lines = false;
21330 /* Note: m_currently_recording_lines is left as false until we see
21331 DW_LNE_end_sequence. */
21332 }
21333 }
21334
21335 /* Subroutine of dwarf_decode_lines to simplify it.
21336 Process the line number information in LH.
21337 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21338 program in order to set included_p for every referenced header. */
21339
21340 static void
21341 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21342 const int decode_for_pst_p, CORE_ADDR lowpc)
21343 {
21344 const gdb_byte *line_ptr, *extended_end;
21345 const gdb_byte *line_end;
21346 unsigned int bytes_read, extended_len;
21347 unsigned char op_code, extended_op;
21348 CORE_ADDR baseaddr;
21349 struct objfile *objfile = cu->per_objfile->objfile;
21350 bfd *abfd = objfile->obfd;
21351 struct gdbarch *gdbarch = objfile->arch ();
21352 /* True if we're recording line info (as opposed to building partial
21353 symtabs and just interested in finding include files mentioned by
21354 the line number program). */
21355 bool record_lines_p = !decode_for_pst_p;
21356
21357 baseaddr = objfile->text_section_offset ();
21358
21359 line_ptr = lh->statement_program_start;
21360 line_end = lh->statement_program_end;
21361
21362 /* Read the statement sequences until there's nothing left. */
21363 while (line_ptr < line_end)
21364 {
21365 /* The DWARF line number program state machine. Reset the state
21366 machine at the start of each sequence. */
21367 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21368 bool end_sequence = false;
21369
21370 if (record_lines_p)
21371 {
21372 /* Start a subfile for the current file of the state
21373 machine. */
21374 const file_entry *fe = state_machine.current_file ();
21375
21376 if (fe != NULL)
21377 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21378 }
21379
21380 /* Decode the table. */
21381 while (line_ptr < line_end && !end_sequence)
21382 {
21383 op_code = read_1_byte (abfd, line_ptr);
21384 line_ptr += 1;
21385
21386 if (op_code >= lh->opcode_base)
21387 {
21388 /* Special opcode. */
21389 state_machine.handle_special_opcode (op_code);
21390 }
21391 else switch (op_code)
21392 {
21393 case DW_LNS_extended_op:
21394 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21395 &bytes_read);
21396 line_ptr += bytes_read;
21397 extended_end = line_ptr + extended_len;
21398 extended_op = read_1_byte (abfd, line_ptr);
21399 line_ptr += 1;
21400 if (DW_LNE_lo_user <= extended_op
21401 && extended_op <= DW_LNE_hi_user)
21402 {
21403 /* Vendor extension, ignore. */
21404 line_ptr = extended_end;
21405 break;
21406 }
21407 switch (extended_op)
21408 {
21409 case DW_LNE_end_sequence:
21410 state_machine.handle_end_sequence ();
21411 end_sequence = true;
21412 break;
21413 case DW_LNE_set_address:
21414 {
21415 CORE_ADDR address
21416 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21417 line_ptr += bytes_read;
21418
21419 state_machine.check_line_address (cu, line_ptr,
21420 lowpc - baseaddr, address);
21421 state_machine.handle_set_address (baseaddr, address);
21422 }
21423 break;
21424 case DW_LNE_define_file:
21425 {
21426 const char *cur_file;
21427 unsigned int mod_time, length;
21428 dir_index dindex;
21429
21430 cur_file = read_direct_string (abfd, line_ptr,
21431 &bytes_read);
21432 line_ptr += bytes_read;
21433 dindex = (dir_index)
21434 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21435 line_ptr += bytes_read;
21436 mod_time =
21437 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21438 line_ptr += bytes_read;
21439 length =
21440 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21441 line_ptr += bytes_read;
21442 lh->add_file_name (cur_file, dindex, mod_time, length);
21443 }
21444 break;
21445 case DW_LNE_set_discriminator:
21446 {
21447 /* The discriminator is not interesting to the
21448 debugger; just ignore it. We still need to
21449 check its value though:
21450 if there are consecutive entries for the same
21451 (non-prologue) line we want to coalesce them.
21452 PR 17276. */
21453 unsigned int discr
21454 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21455 line_ptr += bytes_read;
21456
21457 state_machine.handle_set_discriminator (discr);
21458 }
21459 break;
21460 default:
21461 complaint (_("mangled .debug_line section"));
21462 return;
21463 }
21464 /* Make sure that we parsed the extended op correctly. If e.g.
21465 we expected a different address size than the producer used,
21466 we may have read the wrong number of bytes. */
21467 if (line_ptr != extended_end)
21468 {
21469 complaint (_("mangled .debug_line section"));
21470 return;
21471 }
21472 break;
21473 case DW_LNS_copy:
21474 state_machine.handle_copy ();
21475 break;
21476 case DW_LNS_advance_pc:
21477 {
21478 CORE_ADDR adjust
21479 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21480 line_ptr += bytes_read;
21481
21482 state_machine.handle_advance_pc (adjust);
21483 }
21484 break;
21485 case DW_LNS_advance_line:
21486 {
21487 int line_delta
21488 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21489 line_ptr += bytes_read;
21490
21491 state_machine.handle_advance_line (line_delta);
21492 }
21493 break;
21494 case DW_LNS_set_file:
21495 {
21496 file_name_index file
21497 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21498 &bytes_read);
21499 line_ptr += bytes_read;
21500
21501 state_machine.handle_set_file (file);
21502 }
21503 break;
21504 case DW_LNS_set_column:
21505 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21506 line_ptr += bytes_read;
21507 break;
21508 case DW_LNS_negate_stmt:
21509 state_machine.handle_negate_stmt ();
21510 break;
21511 case DW_LNS_set_basic_block:
21512 break;
21513 /* Add to the address register of the state machine the
21514 address increment value corresponding to special opcode
21515 255. I.e., this value is scaled by the minimum
21516 instruction length since special opcode 255 would have
21517 scaled the increment. */
21518 case DW_LNS_const_add_pc:
21519 state_machine.handle_const_add_pc ();
21520 break;
21521 case DW_LNS_fixed_advance_pc:
21522 {
21523 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21524 line_ptr += 2;
21525
21526 state_machine.handle_fixed_advance_pc (addr_adj);
21527 }
21528 break;
21529 default:
21530 {
21531 /* Unknown standard opcode, ignore it. */
21532 int i;
21533
21534 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21535 {
21536 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21537 line_ptr += bytes_read;
21538 }
21539 }
21540 }
21541 }
21542
21543 if (!end_sequence)
21544 dwarf2_debug_line_missing_end_sequence_complaint ();
21545
21546 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21547 in which case we still finish recording the last line). */
21548 state_machine.record_line (true);
21549 }
21550 }
21551
21552 /* Decode the Line Number Program (LNP) for the given line_header
21553 structure and CU. The actual information extracted and the type
21554 of structures created from the LNP depends on the value of PST.
21555
21556 1. If PST is NULL, then this procedure uses the data from the program
21557 to create all necessary symbol tables, and their linetables.
21558
21559 2. If PST is not NULL, this procedure reads the program to determine
21560 the list of files included by the unit represented by PST, and
21561 builds all the associated partial symbol tables.
21562
21563 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21564 It is used for relative paths in the line table.
21565 NOTE: When processing partial symtabs (pst != NULL),
21566 comp_dir == pst->dirname.
21567
21568 NOTE: It is important that psymtabs have the same file name (via strcmp)
21569 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21570 symtab we don't use it in the name of the psymtabs we create.
21571 E.g. expand_line_sal requires this when finding psymtabs to expand.
21572 A good testcase for this is mb-inline.exp.
21573
21574 LOWPC is the lowest address in CU (or 0 if not known).
21575
21576 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21577 for its PC<->lines mapping information. Otherwise only the filename
21578 table is read in. */
21579
21580 static void
21581 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21582 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21583 CORE_ADDR lowpc, int decode_mapping)
21584 {
21585 struct objfile *objfile = cu->per_objfile->objfile;
21586 const int decode_for_pst_p = (pst != NULL);
21587
21588 if (decode_mapping)
21589 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21590
21591 if (decode_for_pst_p)
21592 {
21593 /* Now that we're done scanning the Line Header Program, we can
21594 create the psymtab of each included file. */
21595 for (auto &file_entry : lh->file_names ())
21596 if (file_entry.included_p == 1)
21597 {
21598 gdb::unique_xmalloc_ptr<char> name_holder;
21599 const char *include_name =
21600 psymtab_include_file_name (lh, file_entry, pst,
21601 comp_dir, &name_holder);
21602 if (include_name != NULL)
21603 dwarf2_create_include_psymtab
21604 (cu->per_objfile->per_bfd, include_name, pst,
21605 cu->per_objfile->per_bfd->partial_symtabs.get (),
21606 objfile->per_bfd);
21607 }
21608 }
21609 else
21610 {
21611 /* Make sure a symtab is created for every file, even files
21612 which contain only variables (i.e. no code with associated
21613 line numbers). */
21614 buildsym_compunit *builder = cu->get_builder ();
21615 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21616
21617 for (auto &fe : lh->file_names ())
21618 {
21619 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21620 if (builder->get_current_subfile ()->symtab == NULL)
21621 {
21622 builder->get_current_subfile ()->symtab
21623 = allocate_symtab (cust,
21624 builder->get_current_subfile ()->name);
21625 }
21626 fe.symtab = builder->get_current_subfile ()->symtab;
21627 }
21628 }
21629 }
21630
21631 /* Start a subfile for DWARF. FILENAME is the name of the file and
21632 DIRNAME the name of the source directory which contains FILENAME
21633 or NULL if not known.
21634 This routine tries to keep line numbers from identical absolute and
21635 relative file names in a common subfile.
21636
21637 Using the `list' example from the GDB testsuite, which resides in
21638 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21639 of /srcdir/list0.c yields the following debugging information for list0.c:
21640
21641 DW_AT_name: /srcdir/list0.c
21642 DW_AT_comp_dir: /compdir
21643 files.files[0].name: list0.h
21644 files.files[0].dir: /srcdir
21645 files.files[1].name: list0.c
21646 files.files[1].dir: /srcdir
21647
21648 The line number information for list0.c has to end up in a single
21649 subfile, so that `break /srcdir/list0.c:1' works as expected.
21650 start_subfile will ensure that this happens provided that we pass the
21651 concatenation of files.files[1].dir and files.files[1].name as the
21652 subfile's name. */
21653
21654 static void
21655 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21656 const char *dirname)
21657 {
21658 gdb::unique_xmalloc_ptr<char> copy;
21659
21660 /* In order not to lose the line information directory,
21661 we concatenate it to the filename when it makes sense.
21662 Note that the Dwarf3 standard says (speaking of filenames in line
21663 information): ``The directory index is ignored for file names
21664 that represent full path names''. Thus ignoring dirname in the
21665 `else' branch below isn't an issue. */
21666
21667 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21668 {
21669 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21670 filename = copy.get ();
21671 }
21672
21673 cu->get_builder ()->start_subfile (filename);
21674 }
21675
21676 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21677 buildsym_compunit constructor. */
21678
21679 struct compunit_symtab *
21680 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21681 CORE_ADDR low_pc)
21682 {
21683 gdb_assert (m_builder == nullptr);
21684
21685 m_builder.reset (new struct buildsym_compunit
21686 (this->per_objfile->objfile,
21687 name, comp_dir, language, low_pc));
21688
21689 list_in_scope = get_builder ()->get_file_symbols ();
21690
21691 get_builder ()->record_debugformat ("DWARF 2");
21692 get_builder ()->record_producer (producer);
21693
21694 processing_has_namespace_info = false;
21695
21696 return get_builder ()->get_compunit_symtab ();
21697 }
21698
21699 static void
21700 var_decode_location (struct attribute *attr, struct symbol *sym,
21701 struct dwarf2_cu *cu)
21702 {
21703 struct objfile *objfile = cu->per_objfile->objfile;
21704 struct comp_unit_head *cu_header = &cu->header;
21705
21706 /* NOTE drow/2003-01-30: There used to be a comment and some special
21707 code here to turn a symbol with DW_AT_external and a
21708 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21709 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21710 with some versions of binutils) where shared libraries could have
21711 relocations against symbols in their debug information - the
21712 minimal symbol would have the right address, but the debug info
21713 would not. It's no longer necessary, because we will explicitly
21714 apply relocations when we read in the debug information now. */
21715
21716 /* A DW_AT_location attribute with no contents indicates that a
21717 variable has been optimized away. */
21718 if (attr->form_is_block () && attr->as_block ()->size == 0)
21719 {
21720 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21721 return;
21722 }
21723
21724 /* Handle one degenerate form of location expression specially, to
21725 preserve GDB's previous behavior when section offsets are
21726 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21727 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21728
21729 if (attr->form_is_block ())
21730 {
21731 struct dwarf_block *block = attr->as_block ();
21732
21733 if ((block->data[0] == DW_OP_addr
21734 && block->size == 1 + cu_header->addr_size)
21735 || ((block->data[0] == DW_OP_GNU_addr_index
21736 || block->data[0] == DW_OP_addrx)
21737 && (block->size
21738 == 1 + leb128_size (&block->data[1]))))
21739 {
21740 unsigned int dummy;
21741
21742 if (block->data[0] == DW_OP_addr)
21743 SET_SYMBOL_VALUE_ADDRESS
21744 (sym, cu->header.read_address (objfile->obfd,
21745 block->data + 1,
21746 &dummy));
21747 else
21748 SET_SYMBOL_VALUE_ADDRESS
21749 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21750 &dummy));
21751 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21752 fixup_symbol_section (sym, objfile);
21753 SET_SYMBOL_VALUE_ADDRESS
21754 (sym,
21755 SYMBOL_VALUE_ADDRESS (sym)
21756 + objfile->section_offsets[sym->section_index ()]);
21757 return;
21758 }
21759 }
21760
21761 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21762 expression evaluator, and use LOC_COMPUTED only when necessary
21763 (i.e. when the value of a register or memory location is
21764 referenced, or a thread-local block, etc.). Then again, it might
21765 not be worthwhile. I'm assuming that it isn't unless performance
21766 or memory numbers show me otherwise. */
21767
21768 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21769
21770 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21771 cu->has_loclist = true;
21772 }
21773
21774 /* Given a pointer to a DWARF information entry, figure out if we need
21775 to make a symbol table entry for it, and if so, create a new entry
21776 and return a pointer to it.
21777 If TYPE is NULL, determine symbol type from the die, otherwise
21778 used the passed type.
21779 If SPACE is not NULL, use it to hold the new symbol. If it is
21780 NULL, allocate a new symbol on the objfile's obstack. */
21781
21782 static struct symbol *
21783 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21784 struct symbol *space)
21785 {
21786 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21787 struct objfile *objfile = per_objfile->objfile;
21788 struct gdbarch *gdbarch = objfile->arch ();
21789 struct symbol *sym = NULL;
21790 const char *name;
21791 struct attribute *attr = NULL;
21792 struct attribute *attr2 = NULL;
21793 CORE_ADDR baseaddr;
21794 struct pending **list_to_add = NULL;
21795
21796 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21797
21798 baseaddr = objfile->text_section_offset ();
21799
21800 name = dwarf2_name (die, cu);
21801 if (name)
21802 {
21803 int suppress_add = 0;
21804
21805 if (space)
21806 sym = space;
21807 else
21808 sym = new (&objfile->objfile_obstack) symbol;
21809 OBJSTAT (objfile, n_syms++);
21810
21811 /* Cache this symbol's name and the name's demangled form (if any). */
21812 sym->set_language (cu->language, &objfile->objfile_obstack);
21813 /* Fortran does not have mangling standard and the mangling does differ
21814 between gfortran, iFort etc. */
21815 const char *physname
21816 = (cu->language == language_fortran
21817 ? dwarf2_full_name (name, die, cu)
21818 : dwarf2_physname (name, die, cu));
21819 const char *linkagename = dw2_linkage_name (die, cu);
21820
21821 if (linkagename == nullptr || cu->language == language_ada)
21822 sym->set_linkage_name (physname);
21823 else
21824 {
21825 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21826 sym->set_linkage_name (linkagename);
21827 }
21828
21829 /* Default assumptions.
21830 Use the passed type or decode it from the die. */
21831 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21832 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21833 if (type != NULL)
21834 SYMBOL_TYPE (sym) = type;
21835 else
21836 SYMBOL_TYPE (sym) = die_type (die, cu);
21837 attr = dwarf2_attr (die,
21838 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21839 cu);
21840 if (attr != nullptr)
21841 SYMBOL_LINE (sym) = attr->constant_value (0);
21842
21843 attr = dwarf2_attr (die,
21844 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21845 cu);
21846 if (attr != nullptr && attr->is_nonnegative ())
21847 {
21848 file_name_index file_index
21849 = (file_name_index) attr->as_nonnegative ();
21850 struct file_entry *fe;
21851
21852 if (cu->line_header != NULL)
21853 fe = cu->line_header->file_name_at (file_index);
21854 else
21855 fe = NULL;
21856
21857 if (fe == NULL)
21858 complaint (_("file index out of range"));
21859 else
21860 symbol_set_symtab (sym, fe->symtab);
21861 }
21862
21863 switch (die->tag)
21864 {
21865 case DW_TAG_label:
21866 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
21867 if (attr != nullptr)
21868 {
21869 CORE_ADDR addr;
21870
21871 addr = attr->as_address ();
21872 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21873 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
21874 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
21875 }
21876 else
21877 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21878 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21879 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
21880 add_symbol_to_list (sym, cu->list_in_scope);
21881 break;
21882 case DW_TAG_subprogram:
21883 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21884 finish_block. */
21885 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21886 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21887 if ((attr2 != nullptr && attr2->as_boolean ())
21888 || cu->language == language_ada
21889 || cu->language == language_fortran)
21890 {
21891 /* Subprograms marked external are stored as a global symbol.
21892 Ada and Fortran subprograms, whether marked external or
21893 not, are always stored as a global symbol, because we want
21894 to be able to access them globally. For instance, we want
21895 to be able to break on a nested subprogram without having
21896 to specify the context. */
21897 list_to_add = cu->get_builder ()->get_global_symbols ();
21898 }
21899 else
21900 {
21901 list_to_add = cu->list_in_scope;
21902 }
21903 break;
21904 case DW_TAG_inlined_subroutine:
21905 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21906 finish_block. */
21907 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
21908 SYMBOL_INLINED (sym) = 1;
21909 list_to_add = cu->list_in_scope;
21910 break;
21911 case DW_TAG_template_value_param:
21912 suppress_add = 1;
21913 /* Fall through. */
21914 case DW_TAG_constant:
21915 case DW_TAG_variable:
21916 case DW_TAG_member:
21917 /* Compilation with minimal debug info may result in
21918 variables with missing type entries. Change the
21919 misleading `void' type to something sensible. */
21920 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
21921 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
21922
21923 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21924 /* In the case of DW_TAG_member, we should only be called for
21925 static const members. */
21926 if (die->tag == DW_TAG_member)
21927 {
21928 /* dwarf2_add_field uses die_is_declaration,
21929 so we do the same. */
21930 gdb_assert (die_is_declaration (die, cu));
21931 gdb_assert (attr);
21932 }
21933 if (attr != nullptr)
21934 {
21935 dwarf2_const_value (attr, sym, cu);
21936 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21937 if (!suppress_add)
21938 {
21939 if (attr2 != nullptr && attr2->as_boolean ())
21940 list_to_add = cu->get_builder ()->get_global_symbols ();
21941 else
21942 list_to_add = cu->list_in_scope;
21943 }
21944 break;
21945 }
21946 attr = dwarf2_attr (die, DW_AT_location, cu);
21947 if (attr != nullptr)
21948 {
21949 var_decode_location (attr, sym, cu);
21950 attr2 = dwarf2_attr (die, DW_AT_external, cu);
21951
21952 /* Fortran explicitly imports any global symbols to the local
21953 scope by DW_TAG_common_block. */
21954 if (cu->language == language_fortran && die->parent
21955 && die->parent->tag == DW_TAG_common_block)
21956 attr2 = NULL;
21957
21958 if (SYMBOL_CLASS (sym) == LOC_STATIC
21959 && SYMBOL_VALUE_ADDRESS (sym) == 0
21960 && !per_objfile->per_bfd->has_section_at_zero)
21961 {
21962 /* When a static variable is eliminated by the linker,
21963 the corresponding debug information is not stripped
21964 out, but the variable address is set to null;
21965 do not add such variables into symbol table. */
21966 }
21967 else if (attr2 != nullptr && attr2->as_boolean ())
21968 {
21969 if (SYMBOL_CLASS (sym) == LOC_STATIC
21970 && (objfile->flags & OBJF_MAINLINE) == 0
21971 && per_objfile->per_bfd->can_copy)
21972 {
21973 /* A global static variable might be subject to
21974 copy relocation. We first check for a local
21975 minsym, though, because maybe the symbol was
21976 marked hidden, in which case this would not
21977 apply. */
21978 bound_minimal_symbol found
21979 = (lookup_minimal_symbol_linkage
21980 (sym->linkage_name (), objfile));
21981 if (found.minsym != nullptr)
21982 sym->maybe_copied = 1;
21983 }
21984
21985 /* A variable with DW_AT_external is never static,
21986 but it may be block-scoped. */
21987 list_to_add
21988 = ((cu->list_in_scope
21989 == cu->get_builder ()->get_file_symbols ())
21990 ? cu->get_builder ()->get_global_symbols ()
21991 : cu->list_in_scope);
21992 }
21993 else
21994 list_to_add = cu->list_in_scope;
21995 }
21996 else
21997 {
21998 /* We do not know the address of this symbol.
21999 If it is an external symbol and we have type information
22000 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22001 The address of the variable will then be determined from
22002 the minimal symbol table whenever the variable is
22003 referenced. */
22004 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22005
22006 /* Fortran explicitly imports any global symbols to the local
22007 scope by DW_TAG_common_block. */
22008 if (cu->language == language_fortran && die->parent
22009 && die->parent->tag == DW_TAG_common_block)
22010 {
22011 /* SYMBOL_CLASS doesn't matter here because
22012 read_common_block is going to reset it. */
22013 if (!suppress_add)
22014 list_to_add = cu->list_in_scope;
22015 }
22016 else if (attr2 != nullptr && attr2->as_boolean ()
22017 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22018 {
22019 /* A variable with DW_AT_external is never static, but it
22020 may be block-scoped. */
22021 list_to_add
22022 = ((cu->list_in_scope
22023 == cu->get_builder ()->get_file_symbols ())
22024 ? cu->get_builder ()->get_global_symbols ()
22025 : cu->list_in_scope);
22026
22027 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22028 }
22029 else if (!die_is_declaration (die, cu))
22030 {
22031 /* Use the default LOC_OPTIMIZED_OUT class. */
22032 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22033 if (!suppress_add)
22034 list_to_add = cu->list_in_scope;
22035 }
22036 }
22037 break;
22038 case DW_TAG_formal_parameter:
22039 {
22040 SYMBOL_IS_ARGUMENT (sym) = 1;
22041 attr = dwarf2_attr (die, DW_AT_location, cu);
22042 if (attr != nullptr)
22043 {
22044 var_decode_location (attr, sym, cu);
22045 }
22046 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22047 if (attr != nullptr)
22048 {
22049 dwarf2_const_value (attr, sym, cu);
22050 }
22051
22052 list_to_add = cu->list_in_scope;
22053 }
22054 break;
22055 case DW_TAG_unspecified_parameters:
22056 /* From varargs functions; gdb doesn't seem to have any
22057 interest in this information, so just ignore it for now.
22058 (FIXME?) */
22059 break;
22060 case DW_TAG_template_type_param:
22061 suppress_add = 1;
22062 /* Fall through. */
22063 case DW_TAG_class_type:
22064 case DW_TAG_interface_type:
22065 case DW_TAG_structure_type:
22066 case DW_TAG_union_type:
22067 case DW_TAG_set_type:
22068 case DW_TAG_enumeration_type:
22069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22070 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22071
22072 {
22073 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22074 really ever be static objects: otherwise, if you try
22075 to, say, break of a class's method and you're in a file
22076 which doesn't mention that class, it won't work unless
22077 the check for all static symbols in lookup_symbol_aux
22078 saves you. See the OtherFileClass tests in
22079 gdb.c++/namespace.exp. */
22080
22081 if (!suppress_add)
22082 {
22083 buildsym_compunit *builder = cu->get_builder ();
22084 list_to_add
22085 = (cu->list_in_scope == builder->get_file_symbols ()
22086 && cu->language == language_cplus
22087 ? builder->get_global_symbols ()
22088 : cu->list_in_scope);
22089
22090 /* The semantics of C++ state that "struct foo {
22091 ... }" also defines a typedef for "foo". */
22092 if (cu->language == language_cplus
22093 || cu->language == language_ada
22094 || cu->language == language_d
22095 || cu->language == language_rust)
22096 {
22097 /* The symbol's name is already allocated along
22098 with this objfile, so we don't need to
22099 duplicate it for the type. */
22100 if (SYMBOL_TYPE (sym)->name () == 0)
22101 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22102 }
22103 }
22104 }
22105 break;
22106 case DW_TAG_typedef:
22107 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22108 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22109 list_to_add = cu->list_in_scope;
22110 break;
22111 case DW_TAG_array_type:
22112 case DW_TAG_base_type:
22113 case DW_TAG_subrange_type:
22114 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22115 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22116 list_to_add = cu->list_in_scope;
22117 break;
22118 case DW_TAG_enumerator:
22119 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22120 if (attr != nullptr)
22121 {
22122 dwarf2_const_value (attr, sym, cu);
22123 }
22124 {
22125 /* NOTE: carlton/2003-11-10: See comment above in the
22126 DW_TAG_class_type, etc. block. */
22127
22128 list_to_add
22129 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22130 && cu->language == language_cplus
22131 ? cu->get_builder ()->get_global_symbols ()
22132 : cu->list_in_scope);
22133 }
22134 break;
22135 case DW_TAG_imported_declaration:
22136 case DW_TAG_namespace:
22137 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22138 list_to_add = cu->get_builder ()->get_global_symbols ();
22139 break;
22140 case DW_TAG_module:
22141 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22142 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22143 list_to_add = cu->get_builder ()->get_global_symbols ();
22144 break;
22145 case DW_TAG_common_block:
22146 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22147 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22148 add_symbol_to_list (sym, cu->list_in_scope);
22149 break;
22150 default:
22151 /* Not a tag we recognize. Hopefully we aren't processing
22152 trash data, but since we must specifically ignore things
22153 we don't recognize, there is nothing else we should do at
22154 this point. */
22155 complaint (_("unsupported tag: '%s'"),
22156 dwarf_tag_name (die->tag));
22157 break;
22158 }
22159
22160 if (suppress_add)
22161 {
22162 sym->hash_next = objfile->template_symbols;
22163 objfile->template_symbols = sym;
22164 list_to_add = NULL;
22165 }
22166
22167 if (list_to_add != NULL)
22168 add_symbol_to_list (sym, list_to_add);
22169
22170 /* For the benefit of old versions of GCC, check for anonymous
22171 namespaces based on the demangled name. */
22172 if (!cu->processing_has_namespace_info
22173 && cu->language == language_cplus)
22174 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22175 }
22176 return (sym);
22177 }
22178
22179 /* Given an attr with a DW_FORM_dataN value in host byte order,
22180 zero-extend it as appropriate for the symbol's type. The DWARF
22181 standard (v4) is not entirely clear about the meaning of using
22182 DW_FORM_dataN for a constant with a signed type, where the type is
22183 wider than the data. The conclusion of a discussion on the DWARF
22184 list was that this is unspecified. We choose to always zero-extend
22185 because that is the interpretation long in use by GCC. */
22186
22187 static gdb_byte *
22188 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22189 struct dwarf2_cu *cu, LONGEST *value, int bits)
22190 {
22191 struct objfile *objfile = cu->per_objfile->objfile;
22192 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22193 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22194 LONGEST l = attr->constant_value (0);
22195
22196 if (bits < sizeof (*value) * 8)
22197 {
22198 l &= ((LONGEST) 1 << bits) - 1;
22199 *value = l;
22200 }
22201 else if (bits == sizeof (*value) * 8)
22202 *value = l;
22203 else
22204 {
22205 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22206 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22207 return bytes;
22208 }
22209
22210 return NULL;
22211 }
22212
22213 /* Read a constant value from an attribute. Either set *VALUE, or if
22214 the value does not fit in *VALUE, set *BYTES - either already
22215 allocated on the objfile obstack, or newly allocated on OBSTACK,
22216 or, set *BATON, if we translated the constant to a location
22217 expression. */
22218
22219 static void
22220 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22221 const char *name, struct obstack *obstack,
22222 struct dwarf2_cu *cu,
22223 LONGEST *value, const gdb_byte **bytes,
22224 struct dwarf2_locexpr_baton **baton)
22225 {
22226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22227 struct objfile *objfile = per_objfile->objfile;
22228 struct comp_unit_head *cu_header = &cu->header;
22229 struct dwarf_block *blk;
22230 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22231 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22232
22233 *value = 0;
22234 *bytes = NULL;
22235 *baton = NULL;
22236
22237 switch (attr->form)
22238 {
22239 case DW_FORM_addr:
22240 case DW_FORM_addrx:
22241 case DW_FORM_GNU_addr_index:
22242 {
22243 gdb_byte *data;
22244
22245 if (TYPE_LENGTH (type) != cu_header->addr_size)
22246 dwarf2_const_value_length_mismatch_complaint (name,
22247 cu_header->addr_size,
22248 TYPE_LENGTH (type));
22249 /* Symbols of this form are reasonably rare, so we just
22250 piggyback on the existing location code rather than writing
22251 a new implementation of symbol_computed_ops. */
22252 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22253 (*baton)->per_objfile = per_objfile;
22254 (*baton)->per_cu = cu->per_cu;
22255 gdb_assert ((*baton)->per_cu);
22256
22257 (*baton)->size = 2 + cu_header->addr_size;
22258 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22259 (*baton)->data = data;
22260
22261 data[0] = DW_OP_addr;
22262 store_unsigned_integer (&data[1], cu_header->addr_size,
22263 byte_order, attr->as_address ());
22264 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22265 }
22266 break;
22267 case DW_FORM_string:
22268 case DW_FORM_strp:
22269 case DW_FORM_strx:
22270 case DW_FORM_GNU_str_index:
22271 case DW_FORM_GNU_strp_alt:
22272 /* The string is already allocated on the objfile obstack, point
22273 directly to it. */
22274 *bytes = (const gdb_byte *) attr->as_string ();
22275 break;
22276 case DW_FORM_block1:
22277 case DW_FORM_block2:
22278 case DW_FORM_block4:
22279 case DW_FORM_block:
22280 case DW_FORM_exprloc:
22281 case DW_FORM_data16:
22282 blk = attr->as_block ();
22283 if (TYPE_LENGTH (type) != blk->size)
22284 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22285 TYPE_LENGTH (type));
22286 *bytes = blk->data;
22287 break;
22288
22289 /* The DW_AT_const_value attributes are supposed to carry the
22290 symbol's value "represented as it would be on the target
22291 architecture." By the time we get here, it's already been
22292 converted to host endianness, so we just need to sign- or
22293 zero-extend it as appropriate. */
22294 case DW_FORM_data1:
22295 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22296 break;
22297 case DW_FORM_data2:
22298 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22299 break;
22300 case DW_FORM_data4:
22301 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22302 break;
22303 case DW_FORM_data8:
22304 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22305 break;
22306
22307 case DW_FORM_sdata:
22308 case DW_FORM_implicit_const:
22309 *value = attr->as_signed ();
22310 break;
22311
22312 case DW_FORM_udata:
22313 *value = attr->as_unsigned ();
22314 break;
22315
22316 default:
22317 complaint (_("unsupported const value attribute form: '%s'"),
22318 dwarf_form_name (attr->form));
22319 *value = 0;
22320 break;
22321 }
22322 }
22323
22324
22325 /* Copy constant value from an attribute to a symbol. */
22326
22327 static void
22328 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22329 struct dwarf2_cu *cu)
22330 {
22331 struct objfile *objfile = cu->per_objfile->objfile;
22332 LONGEST value;
22333 const gdb_byte *bytes;
22334 struct dwarf2_locexpr_baton *baton;
22335
22336 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22337 sym->print_name (),
22338 &objfile->objfile_obstack, cu,
22339 &value, &bytes, &baton);
22340
22341 if (baton != NULL)
22342 {
22343 SYMBOL_LOCATION_BATON (sym) = baton;
22344 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22345 }
22346 else if (bytes != NULL)
22347 {
22348 SYMBOL_VALUE_BYTES (sym) = bytes;
22349 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22350 }
22351 else
22352 {
22353 SYMBOL_VALUE (sym) = value;
22354 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22355 }
22356 }
22357
22358 /* Return the type of the die in question using its DW_AT_type attribute. */
22359
22360 static struct type *
22361 die_type (struct die_info *die, struct dwarf2_cu *cu)
22362 {
22363 struct attribute *type_attr;
22364
22365 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22366 if (!type_attr)
22367 {
22368 struct objfile *objfile = cu->per_objfile->objfile;
22369 /* A missing DW_AT_type represents a void type. */
22370 return objfile_type (objfile)->builtin_void;
22371 }
22372
22373 return lookup_die_type (die, type_attr, cu);
22374 }
22375
22376 /* True iff CU's producer generates GNAT Ada auxiliary information
22377 that allows to find parallel types through that information instead
22378 of having to do expensive parallel lookups by type name. */
22379
22380 static int
22381 need_gnat_info (struct dwarf2_cu *cu)
22382 {
22383 /* Assume that the Ada compiler was GNAT, which always produces
22384 the auxiliary information. */
22385 return (cu->language == language_ada);
22386 }
22387
22388 /* Return the auxiliary type of the die in question using its
22389 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22390 attribute is not present. */
22391
22392 static struct type *
22393 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22394 {
22395 struct attribute *type_attr;
22396
22397 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22398 if (!type_attr)
22399 return NULL;
22400
22401 return lookup_die_type (die, type_attr, cu);
22402 }
22403
22404 /* If DIE has a descriptive_type attribute, then set the TYPE's
22405 descriptive type accordingly. */
22406
22407 static void
22408 set_descriptive_type (struct type *type, struct die_info *die,
22409 struct dwarf2_cu *cu)
22410 {
22411 struct type *descriptive_type = die_descriptive_type (die, cu);
22412
22413 if (descriptive_type)
22414 {
22415 ALLOCATE_GNAT_AUX_TYPE (type);
22416 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22417 }
22418 }
22419
22420 /* Return the containing type of the die in question using its
22421 DW_AT_containing_type attribute. */
22422
22423 static struct type *
22424 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22425 {
22426 struct attribute *type_attr;
22427 struct objfile *objfile = cu->per_objfile->objfile;
22428
22429 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22430 if (!type_attr)
22431 error (_("Dwarf Error: Problem turning containing type into gdb type "
22432 "[in module %s]"), objfile_name (objfile));
22433
22434 return lookup_die_type (die, type_attr, cu);
22435 }
22436
22437 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22438
22439 static struct type *
22440 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22441 {
22442 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22443 struct objfile *objfile = per_objfile->objfile;
22444 char *saved;
22445
22446 std::string message
22447 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22448 objfile_name (objfile),
22449 sect_offset_str (cu->header.sect_off),
22450 sect_offset_str (die->sect_off));
22451 saved = obstack_strdup (&objfile->objfile_obstack, message);
22452
22453 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22454 }
22455
22456 /* Look up the type of DIE in CU using its type attribute ATTR.
22457 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22458 DW_AT_containing_type.
22459 If there is no type substitute an error marker. */
22460
22461 static struct type *
22462 lookup_die_type (struct die_info *die, const struct attribute *attr,
22463 struct dwarf2_cu *cu)
22464 {
22465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22466 struct objfile *objfile = per_objfile->objfile;
22467 struct type *this_type;
22468
22469 gdb_assert (attr->name == DW_AT_type
22470 || attr->name == DW_AT_GNAT_descriptive_type
22471 || attr->name == DW_AT_containing_type);
22472
22473 /* First see if we have it cached. */
22474
22475 if (attr->form == DW_FORM_GNU_ref_alt)
22476 {
22477 struct dwarf2_per_cu_data *per_cu;
22478 sect_offset sect_off = attr->get_ref_die_offset ();
22479
22480 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22481 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22482 }
22483 else if (attr->form_is_ref ())
22484 {
22485 sect_offset sect_off = attr->get_ref_die_offset ();
22486
22487 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22488 }
22489 else if (attr->form == DW_FORM_ref_sig8)
22490 {
22491 ULONGEST signature = attr->as_signature ();
22492
22493 return get_signatured_type (die, signature, cu);
22494 }
22495 else
22496 {
22497 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22498 " at %s [in module %s]"),
22499 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22500 objfile_name (objfile));
22501 return build_error_marker_type (cu, die);
22502 }
22503
22504 /* If not cached we need to read it in. */
22505
22506 if (this_type == NULL)
22507 {
22508 struct die_info *type_die = NULL;
22509 struct dwarf2_cu *type_cu = cu;
22510
22511 if (attr->form_is_ref ())
22512 type_die = follow_die_ref (die, attr, &type_cu);
22513 if (type_die == NULL)
22514 return build_error_marker_type (cu, die);
22515 /* If we find the type now, it's probably because the type came
22516 from an inter-CU reference and the type's CU got expanded before
22517 ours. */
22518 this_type = read_type_die (type_die, type_cu);
22519 }
22520
22521 /* If we still don't have a type use an error marker. */
22522
22523 if (this_type == NULL)
22524 return build_error_marker_type (cu, die);
22525
22526 return this_type;
22527 }
22528
22529 /* Return the type in DIE, CU.
22530 Returns NULL for invalid types.
22531
22532 This first does a lookup in die_type_hash,
22533 and only reads the die in if necessary.
22534
22535 NOTE: This can be called when reading in partial or full symbols. */
22536
22537 static struct type *
22538 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22539 {
22540 struct type *this_type;
22541
22542 this_type = get_die_type (die, cu);
22543 if (this_type)
22544 return this_type;
22545
22546 return read_type_die_1 (die, cu);
22547 }
22548
22549 /* Read the type in DIE, CU.
22550 Returns NULL for invalid types. */
22551
22552 static struct type *
22553 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22554 {
22555 struct type *this_type = NULL;
22556
22557 switch (die->tag)
22558 {
22559 case DW_TAG_class_type:
22560 case DW_TAG_interface_type:
22561 case DW_TAG_structure_type:
22562 case DW_TAG_union_type:
22563 this_type = read_structure_type (die, cu);
22564 break;
22565 case DW_TAG_enumeration_type:
22566 this_type = read_enumeration_type (die, cu);
22567 break;
22568 case DW_TAG_subprogram:
22569 case DW_TAG_subroutine_type:
22570 case DW_TAG_inlined_subroutine:
22571 this_type = read_subroutine_type (die, cu);
22572 break;
22573 case DW_TAG_array_type:
22574 this_type = read_array_type (die, cu);
22575 break;
22576 case DW_TAG_set_type:
22577 this_type = read_set_type (die, cu);
22578 break;
22579 case DW_TAG_pointer_type:
22580 this_type = read_tag_pointer_type (die, cu);
22581 break;
22582 case DW_TAG_ptr_to_member_type:
22583 this_type = read_tag_ptr_to_member_type (die, cu);
22584 break;
22585 case DW_TAG_reference_type:
22586 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22587 break;
22588 case DW_TAG_rvalue_reference_type:
22589 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22590 break;
22591 case DW_TAG_const_type:
22592 this_type = read_tag_const_type (die, cu);
22593 break;
22594 case DW_TAG_volatile_type:
22595 this_type = read_tag_volatile_type (die, cu);
22596 break;
22597 case DW_TAG_restrict_type:
22598 this_type = read_tag_restrict_type (die, cu);
22599 break;
22600 case DW_TAG_string_type:
22601 this_type = read_tag_string_type (die, cu);
22602 break;
22603 case DW_TAG_typedef:
22604 this_type = read_typedef (die, cu);
22605 break;
22606 case DW_TAG_subrange_type:
22607 this_type = read_subrange_type (die, cu);
22608 break;
22609 case DW_TAG_base_type:
22610 this_type = read_base_type (die, cu);
22611 break;
22612 case DW_TAG_unspecified_type:
22613 this_type = read_unspecified_type (die, cu);
22614 break;
22615 case DW_TAG_namespace:
22616 this_type = read_namespace_type (die, cu);
22617 break;
22618 case DW_TAG_module:
22619 this_type = read_module_type (die, cu);
22620 break;
22621 case DW_TAG_atomic_type:
22622 this_type = read_tag_atomic_type (die, cu);
22623 break;
22624 default:
22625 complaint (_("unexpected tag in read_type_die: '%s'"),
22626 dwarf_tag_name (die->tag));
22627 break;
22628 }
22629
22630 return this_type;
22631 }
22632
22633 /* See if we can figure out if the class lives in a namespace. We do
22634 this by looking for a member function; its demangled name will
22635 contain namespace info, if there is any.
22636 Return the computed name or NULL.
22637 Space for the result is allocated on the objfile's obstack.
22638 This is the full-die version of guess_partial_die_structure_name.
22639 In this case we know DIE has no useful parent. */
22640
22641 static const char *
22642 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22643 {
22644 struct die_info *spec_die;
22645 struct dwarf2_cu *spec_cu;
22646 struct die_info *child;
22647 struct objfile *objfile = cu->per_objfile->objfile;
22648
22649 spec_cu = cu;
22650 spec_die = die_specification (die, &spec_cu);
22651 if (spec_die != NULL)
22652 {
22653 die = spec_die;
22654 cu = spec_cu;
22655 }
22656
22657 for (child = die->child;
22658 child != NULL;
22659 child = child->sibling)
22660 {
22661 if (child->tag == DW_TAG_subprogram)
22662 {
22663 const char *linkage_name = dw2_linkage_name (child, cu);
22664
22665 if (linkage_name != NULL)
22666 {
22667 gdb::unique_xmalloc_ptr<char> actual_name
22668 (cu->language_defn->class_name_from_physname (linkage_name));
22669 const char *name = NULL;
22670
22671 if (actual_name != NULL)
22672 {
22673 const char *die_name = dwarf2_name (die, cu);
22674
22675 if (die_name != NULL
22676 && strcmp (die_name, actual_name.get ()) != 0)
22677 {
22678 /* Strip off the class name from the full name.
22679 We want the prefix. */
22680 int die_name_len = strlen (die_name);
22681 int actual_name_len = strlen (actual_name.get ());
22682 const char *ptr = actual_name.get ();
22683
22684 /* Test for '::' as a sanity check. */
22685 if (actual_name_len > die_name_len + 2
22686 && ptr[actual_name_len - die_name_len - 1] == ':')
22687 name = obstack_strndup (
22688 &objfile->per_bfd->storage_obstack,
22689 ptr, actual_name_len - die_name_len - 2);
22690 }
22691 }
22692 return name;
22693 }
22694 }
22695 }
22696
22697 return NULL;
22698 }
22699
22700 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22701 prefix part in such case. See
22702 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22703
22704 static const char *
22705 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22706 {
22707 struct attribute *attr;
22708 const char *base;
22709
22710 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22711 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22712 return NULL;
22713
22714 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22715 return NULL;
22716
22717 attr = dw2_linkage_name_attr (die, cu);
22718 const char *attr_name = attr->as_string ();
22719 if (attr == NULL || attr_name == NULL)
22720 return NULL;
22721
22722 /* dwarf2_name had to be already called. */
22723 gdb_assert (attr->canonical_string_p ());
22724
22725 /* Strip the base name, keep any leading namespaces/classes. */
22726 base = strrchr (attr_name, ':');
22727 if (base == NULL || base == attr_name || base[-1] != ':')
22728 return "";
22729
22730 struct objfile *objfile = cu->per_objfile->objfile;
22731 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22732 attr_name,
22733 &base[-1] - attr_name);
22734 }
22735
22736 /* Return the name of the namespace/class that DIE is defined within,
22737 or "" if we can't tell. The caller should not xfree the result.
22738
22739 For example, if we're within the method foo() in the following
22740 code:
22741
22742 namespace N {
22743 class C {
22744 void foo () {
22745 }
22746 };
22747 }
22748
22749 then determine_prefix on foo's die will return "N::C". */
22750
22751 static const char *
22752 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22753 {
22754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22755 struct die_info *parent, *spec_die;
22756 struct dwarf2_cu *spec_cu;
22757 struct type *parent_type;
22758 const char *retval;
22759
22760 if (cu->language != language_cplus
22761 && cu->language != language_fortran && cu->language != language_d
22762 && cu->language != language_rust)
22763 return "";
22764
22765 retval = anonymous_struct_prefix (die, cu);
22766 if (retval)
22767 return retval;
22768
22769 /* We have to be careful in the presence of DW_AT_specification.
22770 For example, with GCC 3.4, given the code
22771
22772 namespace N {
22773 void foo() {
22774 // Definition of N::foo.
22775 }
22776 }
22777
22778 then we'll have a tree of DIEs like this:
22779
22780 1: DW_TAG_compile_unit
22781 2: DW_TAG_namespace // N
22782 3: DW_TAG_subprogram // declaration of N::foo
22783 4: DW_TAG_subprogram // definition of N::foo
22784 DW_AT_specification // refers to die #3
22785
22786 Thus, when processing die #4, we have to pretend that we're in
22787 the context of its DW_AT_specification, namely the contex of die
22788 #3. */
22789 spec_cu = cu;
22790 spec_die = die_specification (die, &spec_cu);
22791 if (spec_die == NULL)
22792 parent = die->parent;
22793 else
22794 {
22795 parent = spec_die->parent;
22796 cu = spec_cu;
22797 }
22798
22799 if (parent == NULL)
22800 return "";
22801 else if (parent->building_fullname)
22802 {
22803 const char *name;
22804 const char *parent_name;
22805
22806 /* It has been seen on RealView 2.2 built binaries,
22807 DW_TAG_template_type_param types actually _defined_ as
22808 children of the parent class:
22809
22810 enum E {};
22811 template class <class Enum> Class{};
22812 Class<enum E> class_e;
22813
22814 1: DW_TAG_class_type (Class)
22815 2: DW_TAG_enumeration_type (E)
22816 3: DW_TAG_enumerator (enum1:0)
22817 3: DW_TAG_enumerator (enum2:1)
22818 ...
22819 2: DW_TAG_template_type_param
22820 DW_AT_type DW_FORM_ref_udata (E)
22821
22822 Besides being broken debug info, it can put GDB into an
22823 infinite loop. Consider:
22824
22825 When we're building the full name for Class<E>, we'll start
22826 at Class, and go look over its template type parameters,
22827 finding E. We'll then try to build the full name of E, and
22828 reach here. We're now trying to build the full name of E,
22829 and look over the parent DIE for containing scope. In the
22830 broken case, if we followed the parent DIE of E, we'd again
22831 find Class, and once again go look at its template type
22832 arguments, etc., etc. Simply don't consider such parent die
22833 as source-level parent of this die (it can't be, the language
22834 doesn't allow it), and break the loop here. */
22835 name = dwarf2_name (die, cu);
22836 parent_name = dwarf2_name (parent, cu);
22837 complaint (_("template param type '%s' defined within parent '%s'"),
22838 name ? name : "<unknown>",
22839 parent_name ? parent_name : "<unknown>");
22840 return "";
22841 }
22842 else
22843 switch (parent->tag)
22844 {
22845 case DW_TAG_namespace:
22846 parent_type = read_type_die (parent, cu);
22847 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22848 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22849 Work around this problem here. */
22850 if (cu->language == language_cplus
22851 && strcmp (parent_type->name (), "::") == 0)
22852 return "";
22853 /* We give a name to even anonymous namespaces. */
22854 return parent_type->name ();
22855 case DW_TAG_class_type:
22856 case DW_TAG_interface_type:
22857 case DW_TAG_structure_type:
22858 case DW_TAG_union_type:
22859 case DW_TAG_module:
22860 parent_type = read_type_die (parent, cu);
22861 if (parent_type->name () != NULL)
22862 return parent_type->name ();
22863 else
22864 /* An anonymous structure is only allowed non-static data
22865 members; no typedefs, no member functions, et cetera.
22866 So it does not need a prefix. */
22867 return "";
22868 case DW_TAG_compile_unit:
22869 case DW_TAG_partial_unit:
22870 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22871 if (cu->language == language_cplus
22872 && !per_objfile->per_bfd->types.empty ()
22873 && die->child != NULL
22874 && (die->tag == DW_TAG_class_type
22875 || die->tag == DW_TAG_structure_type
22876 || die->tag == DW_TAG_union_type))
22877 {
22878 const char *name = guess_full_die_structure_name (die, cu);
22879 if (name != NULL)
22880 return name;
22881 }
22882 return "";
22883 case DW_TAG_subprogram:
22884 /* Nested subroutines in Fortran get a prefix with the name
22885 of the parent's subroutine. */
22886 if (cu->language == language_fortran)
22887 {
22888 if ((die->tag == DW_TAG_subprogram)
22889 && (dwarf2_name (parent, cu) != NULL))
22890 return dwarf2_name (parent, cu);
22891 }
22892 return determine_prefix (parent, cu);
22893 case DW_TAG_enumeration_type:
22894 parent_type = read_type_die (parent, cu);
22895 if (parent_type->is_declared_class ())
22896 {
22897 if (parent_type->name () != NULL)
22898 return parent_type->name ();
22899 return "";
22900 }
22901 /* Fall through. */
22902 default:
22903 return determine_prefix (parent, cu);
22904 }
22905 }
22906
22907 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22908 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22909 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22910 an obconcat, otherwise allocate storage for the result. The CU argument is
22911 used to determine the language and hence, the appropriate separator. */
22912
22913 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
22914
22915 static char *
22916 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22917 int physname, struct dwarf2_cu *cu)
22918 {
22919 const char *lead = "";
22920 const char *sep;
22921
22922 if (suffix == NULL || suffix[0] == '\0'
22923 || prefix == NULL || prefix[0] == '\0')
22924 sep = "";
22925 else if (cu->language == language_d)
22926 {
22927 /* For D, the 'main' function could be defined in any module, but it
22928 should never be prefixed. */
22929 if (strcmp (suffix, "D main") == 0)
22930 {
22931 prefix = "";
22932 sep = "";
22933 }
22934 else
22935 sep = ".";
22936 }
22937 else if (cu->language == language_fortran && physname)
22938 {
22939 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22940 DW_AT_MIPS_linkage_name is preferred and used instead. */
22941
22942 lead = "__";
22943 sep = "_MOD_";
22944 }
22945 else
22946 sep = "::";
22947
22948 if (prefix == NULL)
22949 prefix = "";
22950 if (suffix == NULL)
22951 suffix = "";
22952
22953 if (obs == NULL)
22954 {
22955 char *retval
22956 = ((char *)
22957 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
22958
22959 strcpy (retval, lead);
22960 strcat (retval, prefix);
22961 strcat (retval, sep);
22962 strcat (retval, suffix);
22963 return retval;
22964 }
22965 else
22966 {
22967 /* We have an obstack. */
22968 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
22969 }
22970 }
22971
22972 /* Get name of a die, return NULL if not found. */
22973
22974 static const char *
22975 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
22976 struct objfile *objfile)
22977 {
22978 if (name && cu->language == language_cplus)
22979 {
22980 gdb::unique_xmalloc_ptr<char> canon_name
22981 = cp_canonicalize_string (name);
22982
22983 if (canon_name != nullptr)
22984 name = objfile->intern (canon_name.get ());
22985 }
22986
22987 return name;
22988 }
22989
22990 /* Get name of a die, return NULL if not found.
22991 Anonymous namespaces are converted to their magic string. */
22992
22993 static const char *
22994 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
22995 {
22996 struct attribute *attr;
22997 struct objfile *objfile = cu->per_objfile->objfile;
22998
22999 attr = dwarf2_attr (die, DW_AT_name, cu);
23000 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23001 if (attr_name == nullptr
23002 && die->tag != DW_TAG_namespace
23003 && die->tag != DW_TAG_class_type
23004 && die->tag != DW_TAG_interface_type
23005 && die->tag != DW_TAG_structure_type
23006 && die->tag != DW_TAG_union_type)
23007 return NULL;
23008
23009 switch (die->tag)
23010 {
23011 case DW_TAG_compile_unit:
23012 case DW_TAG_partial_unit:
23013 /* Compilation units have a DW_AT_name that is a filename, not
23014 a source language identifier. */
23015 case DW_TAG_enumeration_type:
23016 case DW_TAG_enumerator:
23017 /* These tags always have simple identifiers already; no need
23018 to canonicalize them. */
23019 return attr_name;
23020
23021 case DW_TAG_namespace:
23022 if (attr_name != nullptr)
23023 return attr_name;
23024 return CP_ANONYMOUS_NAMESPACE_STR;
23025
23026 case DW_TAG_class_type:
23027 case DW_TAG_interface_type:
23028 case DW_TAG_structure_type:
23029 case DW_TAG_union_type:
23030 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23031 structures or unions. These were of the form "._%d" in GCC 4.1,
23032 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23033 and GCC 4.4. We work around this problem by ignoring these. */
23034 if (attr_name != nullptr
23035 && (startswith (attr_name, "._")
23036 || startswith (attr_name, "<anonymous")))
23037 return NULL;
23038
23039 /* GCC might emit a nameless typedef that has a linkage name. See
23040 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23041 if (!attr || attr_name == NULL)
23042 {
23043 attr = dw2_linkage_name_attr (die, cu);
23044 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23045 if (attr == NULL || attr_name == NULL)
23046 return NULL;
23047
23048 /* Avoid demangling attr_name the second time on a second
23049 call for the same DIE. */
23050 if (!attr->canonical_string_p ())
23051 {
23052 gdb::unique_xmalloc_ptr<char> demangled
23053 (gdb_demangle (attr_name, DMGL_TYPES));
23054 if (demangled == nullptr)
23055 return nullptr;
23056
23057 attr->set_string_canonical (objfile->intern (demangled.get ()));
23058 attr_name = attr->as_string ();
23059 }
23060
23061 /* Strip any leading namespaces/classes, keep only the
23062 base name. DW_AT_name for named DIEs does not
23063 contain the prefixes. */
23064 const char *base = strrchr (attr_name, ':');
23065 if (base && base > attr_name && base[-1] == ':')
23066 return &base[1];
23067 else
23068 return attr_name;
23069 }
23070 break;
23071
23072 default:
23073 break;
23074 }
23075
23076 if (!attr->canonical_string_p ())
23077 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23078 objfile));
23079 return attr->as_string ();
23080 }
23081
23082 /* Return the die that this die in an extension of, or NULL if there
23083 is none. *EXT_CU is the CU containing DIE on input, and the CU
23084 containing the return value on output. */
23085
23086 static struct die_info *
23087 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23088 {
23089 struct attribute *attr;
23090
23091 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23092 if (attr == NULL)
23093 return NULL;
23094
23095 return follow_die_ref (die, attr, ext_cu);
23096 }
23097
23098 static void
23099 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23100 {
23101 unsigned int i;
23102
23103 print_spaces (indent, f);
23104 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23105 dwarf_tag_name (die->tag), die->abbrev,
23106 sect_offset_str (die->sect_off));
23107
23108 if (die->parent != NULL)
23109 {
23110 print_spaces (indent, f);
23111 fprintf_unfiltered (f, " parent at offset: %s\n",
23112 sect_offset_str (die->parent->sect_off));
23113 }
23114
23115 print_spaces (indent, f);
23116 fprintf_unfiltered (f, " has children: %s\n",
23117 dwarf_bool_name (die->child != NULL));
23118
23119 print_spaces (indent, f);
23120 fprintf_unfiltered (f, " attributes:\n");
23121
23122 for (i = 0; i < die->num_attrs; ++i)
23123 {
23124 print_spaces (indent, f);
23125 fprintf_unfiltered (f, " %s (%s) ",
23126 dwarf_attr_name (die->attrs[i].name),
23127 dwarf_form_name (die->attrs[i].form));
23128
23129 switch (die->attrs[i].form)
23130 {
23131 case DW_FORM_addr:
23132 case DW_FORM_addrx:
23133 case DW_FORM_GNU_addr_index:
23134 fprintf_unfiltered (f, "address: ");
23135 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23136 break;
23137 case DW_FORM_block2:
23138 case DW_FORM_block4:
23139 case DW_FORM_block:
23140 case DW_FORM_block1:
23141 fprintf_unfiltered (f, "block: size %s",
23142 pulongest (die->attrs[i].as_block ()->size));
23143 break;
23144 case DW_FORM_exprloc:
23145 fprintf_unfiltered (f, "expression: size %s",
23146 pulongest (die->attrs[i].as_block ()->size));
23147 break;
23148 case DW_FORM_data16:
23149 fprintf_unfiltered (f, "constant of 16 bytes");
23150 break;
23151 case DW_FORM_ref_addr:
23152 fprintf_unfiltered (f, "ref address: ");
23153 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23154 break;
23155 case DW_FORM_GNU_ref_alt:
23156 fprintf_unfiltered (f, "alt ref address: ");
23157 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23158 break;
23159 case DW_FORM_ref1:
23160 case DW_FORM_ref2:
23161 case DW_FORM_ref4:
23162 case DW_FORM_ref8:
23163 case DW_FORM_ref_udata:
23164 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23165 (long) (die->attrs[i].as_unsigned ()));
23166 break;
23167 case DW_FORM_data1:
23168 case DW_FORM_data2:
23169 case DW_FORM_data4:
23170 case DW_FORM_data8:
23171 case DW_FORM_udata:
23172 fprintf_unfiltered (f, "constant: %s",
23173 pulongest (die->attrs[i].as_unsigned ()));
23174 break;
23175 case DW_FORM_sec_offset:
23176 fprintf_unfiltered (f, "section offset: %s",
23177 pulongest (die->attrs[i].as_unsigned ()));
23178 break;
23179 case DW_FORM_ref_sig8:
23180 fprintf_unfiltered (f, "signature: %s",
23181 hex_string (die->attrs[i].as_signature ()));
23182 break;
23183 case DW_FORM_string:
23184 case DW_FORM_strp:
23185 case DW_FORM_line_strp:
23186 case DW_FORM_strx:
23187 case DW_FORM_GNU_str_index:
23188 case DW_FORM_GNU_strp_alt:
23189 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23190 die->attrs[i].as_string ()
23191 ? die->attrs[i].as_string () : "",
23192 die->attrs[i].canonical_string_p () ? "is" : "not");
23193 break;
23194 case DW_FORM_flag:
23195 if (die->attrs[i].as_boolean ())
23196 fprintf_unfiltered (f, "flag: TRUE");
23197 else
23198 fprintf_unfiltered (f, "flag: FALSE");
23199 break;
23200 case DW_FORM_flag_present:
23201 fprintf_unfiltered (f, "flag: TRUE");
23202 break;
23203 case DW_FORM_indirect:
23204 /* The reader will have reduced the indirect form to
23205 the "base form" so this form should not occur. */
23206 fprintf_unfiltered (f,
23207 "unexpected attribute form: DW_FORM_indirect");
23208 break;
23209 case DW_FORM_sdata:
23210 case DW_FORM_implicit_const:
23211 fprintf_unfiltered (f, "constant: %s",
23212 plongest (die->attrs[i].as_signed ()));
23213 break;
23214 default:
23215 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23216 die->attrs[i].form);
23217 break;
23218 }
23219 fprintf_unfiltered (f, "\n");
23220 }
23221 }
23222
23223 static void
23224 dump_die_for_error (struct die_info *die)
23225 {
23226 dump_die_shallow (gdb_stderr, 0, die);
23227 }
23228
23229 static void
23230 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23231 {
23232 int indent = level * 4;
23233
23234 gdb_assert (die != NULL);
23235
23236 if (level >= max_level)
23237 return;
23238
23239 dump_die_shallow (f, indent, die);
23240
23241 if (die->child != NULL)
23242 {
23243 print_spaces (indent, f);
23244 fprintf_unfiltered (f, " Children:");
23245 if (level + 1 < max_level)
23246 {
23247 fprintf_unfiltered (f, "\n");
23248 dump_die_1 (f, level + 1, max_level, die->child);
23249 }
23250 else
23251 {
23252 fprintf_unfiltered (f,
23253 " [not printed, max nesting level reached]\n");
23254 }
23255 }
23256
23257 if (die->sibling != NULL && level > 0)
23258 {
23259 dump_die_1 (f, level, max_level, die->sibling);
23260 }
23261 }
23262
23263 /* This is called from the pdie macro in gdbinit.in.
23264 It's not static so gcc will keep a copy callable from gdb. */
23265
23266 void
23267 dump_die (struct die_info *die, int max_level)
23268 {
23269 dump_die_1 (gdb_stdlog, 0, max_level, die);
23270 }
23271
23272 static void
23273 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23274 {
23275 void **slot;
23276
23277 slot = htab_find_slot_with_hash (cu->die_hash, die,
23278 to_underlying (die->sect_off),
23279 INSERT);
23280
23281 *slot = die;
23282 }
23283
23284 /* Follow reference or signature attribute ATTR of SRC_DIE.
23285 On entry *REF_CU is the CU of SRC_DIE.
23286 On exit *REF_CU is the CU of the result. */
23287
23288 static struct die_info *
23289 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23290 struct dwarf2_cu **ref_cu)
23291 {
23292 struct die_info *die;
23293
23294 if (attr->form_is_ref ())
23295 die = follow_die_ref (src_die, attr, ref_cu);
23296 else if (attr->form == DW_FORM_ref_sig8)
23297 die = follow_die_sig (src_die, attr, ref_cu);
23298 else
23299 {
23300 dump_die_for_error (src_die);
23301 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23302 objfile_name ((*ref_cu)->per_objfile->objfile));
23303 }
23304
23305 return die;
23306 }
23307
23308 /* Follow reference OFFSET.
23309 On entry *REF_CU is the CU of the source die referencing OFFSET.
23310 On exit *REF_CU is the CU of the result.
23311 Returns NULL if OFFSET is invalid. */
23312
23313 static struct die_info *
23314 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23315 struct dwarf2_cu **ref_cu)
23316 {
23317 struct die_info temp_die;
23318 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23319 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23320
23321 gdb_assert (cu->per_cu != NULL);
23322
23323 target_cu = cu;
23324
23325 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23326 "source CU contains target offset: %d",
23327 sect_offset_str (cu->per_cu->sect_off),
23328 sect_offset_str (sect_off),
23329 cu->header.offset_in_cu_p (sect_off));
23330
23331 if (cu->per_cu->is_debug_types)
23332 {
23333 /* .debug_types CUs cannot reference anything outside their CU.
23334 If they need to, they have to reference a signatured type via
23335 DW_FORM_ref_sig8. */
23336 if (!cu->header.offset_in_cu_p (sect_off))
23337 return NULL;
23338 }
23339 else if (offset_in_dwz != cu->per_cu->is_dwz
23340 || !cu->header.offset_in_cu_p (sect_off))
23341 {
23342 struct dwarf2_per_cu_data *per_cu;
23343
23344 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23345 per_objfile);
23346
23347 dwarf_read_debug_printf_v ("target CU offset: %s, "
23348 "target CU DIEs loaded: %d",
23349 sect_offset_str (per_cu->sect_off),
23350 per_objfile->get_cu (per_cu) != nullptr);
23351
23352 /* If necessary, add it to the queue and load its DIEs.
23353
23354 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23355 it doesn't mean they are currently loaded. Since we require them
23356 to be loaded, we must check for ourselves. */
23357 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23358 || per_objfile->get_cu (per_cu) == nullptr)
23359 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23360 false, cu->language);
23361
23362 target_cu = per_objfile->get_cu (per_cu);
23363 gdb_assert (target_cu != nullptr);
23364 }
23365 else if (cu->dies == NULL)
23366 {
23367 /* We're loading full DIEs during partial symbol reading. */
23368 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23369 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23370 language_minimal);
23371 }
23372
23373 *ref_cu = target_cu;
23374 temp_die.sect_off = sect_off;
23375
23376 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23377 &temp_die,
23378 to_underlying (sect_off));
23379 }
23380
23381 /* Follow reference attribute ATTR of SRC_DIE.
23382 On entry *REF_CU is the CU of SRC_DIE.
23383 On exit *REF_CU is the CU of the result. */
23384
23385 static struct die_info *
23386 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23387 struct dwarf2_cu **ref_cu)
23388 {
23389 sect_offset sect_off = attr->get_ref_die_offset ();
23390 struct dwarf2_cu *cu = *ref_cu;
23391 struct die_info *die;
23392
23393 die = follow_die_offset (sect_off,
23394 (attr->form == DW_FORM_GNU_ref_alt
23395 || cu->per_cu->is_dwz),
23396 ref_cu);
23397 if (!die)
23398 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23399 "at %s [in module %s]"),
23400 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23401 objfile_name (cu->per_objfile->objfile));
23402
23403 return die;
23404 }
23405
23406 /* See read.h. */
23407
23408 struct dwarf2_locexpr_baton
23409 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23410 dwarf2_per_cu_data *per_cu,
23411 dwarf2_per_objfile *per_objfile,
23412 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23413 bool resolve_abstract_p)
23414 {
23415 struct die_info *die;
23416 struct attribute *attr;
23417 struct dwarf2_locexpr_baton retval;
23418 struct objfile *objfile = per_objfile->objfile;
23419
23420 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23421 if (cu == nullptr)
23422 cu = load_cu (per_cu, per_objfile, false);
23423
23424 if (cu == nullptr)
23425 {
23426 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23427 Instead just throw an error, not much else we can do. */
23428 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23429 sect_offset_str (sect_off), objfile_name (objfile));
23430 }
23431
23432 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23433 if (!die)
23434 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23435 sect_offset_str (sect_off), objfile_name (objfile));
23436
23437 attr = dwarf2_attr (die, DW_AT_location, cu);
23438 if (!attr && resolve_abstract_p
23439 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23440 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23441 {
23442 CORE_ADDR pc = get_frame_pc ();
23443 CORE_ADDR baseaddr = objfile->text_section_offset ();
23444 struct gdbarch *gdbarch = objfile->arch ();
23445
23446 for (const auto &cand_off
23447 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23448 {
23449 struct dwarf2_cu *cand_cu = cu;
23450 struct die_info *cand
23451 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23452 if (!cand
23453 || !cand->parent
23454 || cand->parent->tag != DW_TAG_subprogram)
23455 continue;
23456
23457 CORE_ADDR pc_low, pc_high;
23458 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23459 if (pc_low == ((CORE_ADDR) -1))
23460 continue;
23461 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23462 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23463 if (!(pc_low <= pc && pc < pc_high))
23464 continue;
23465
23466 die = cand;
23467 attr = dwarf2_attr (die, DW_AT_location, cu);
23468 break;
23469 }
23470 }
23471
23472 if (!attr)
23473 {
23474 /* DWARF: "If there is no such attribute, then there is no effect.".
23475 DATA is ignored if SIZE is 0. */
23476
23477 retval.data = NULL;
23478 retval.size = 0;
23479 }
23480 else if (attr->form_is_section_offset ())
23481 {
23482 struct dwarf2_loclist_baton loclist_baton;
23483 CORE_ADDR pc = get_frame_pc ();
23484 size_t size;
23485
23486 fill_in_loclist_baton (cu, &loclist_baton, attr);
23487
23488 retval.data = dwarf2_find_location_expression (&loclist_baton,
23489 &size, pc);
23490 retval.size = size;
23491 }
23492 else
23493 {
23494 if (!attr->form_is_block ())
23495 error (_("Dwarf Error: DIE at %s referenced in module %s "
23496 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23497 sect_offset_str (sect_off), objfile_name (objfile));
23498
23499 struct dwarf_block *block = attr->as_block ();
23500 retval.data = block->data;
23501 retval.size = block->size;
23502 }
23503 retval.per_objfile = per_objfile;
23504 retval.per_cu = cu->per_cu;
23505
23506 per_objfile->age_comp_units ();
23507
23508 return retval;
23509 }
23510
23511 /* See read.h. */
23512
23513 struct dwarf2_locexpr_baton
23514 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23515 dwarf2_per_cu_data *per_cu,
23516 dwarf2_per_objfile *per_objfile,
23517 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23518 {
23519 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23520
23521 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23522 get_frame_pc);
23523 }
23524
23525 /* Write a constant of a given type as target-ordered bytes into
23526 OBSTACK. */
23527
23528 static const gdb_byte *
23529 write_constant_as_bytes (struct obstack *obstack,
23530 enum bfd_endian byte_order,
23531 struct type *type,
23532 ULONGEST value,
23533 LONGEST *len)
23534 {
23535 gdb_byte *result;
23536
23537 *len = TYPE_LENGTH (type);
23538 result = (gdb_byte *) obstack_alloc (obstack, *len);
23539 store_unsigned_integer (result, *len, byte_order, value);
23540
23541 return result;
23542 }
23543
23544 /* See read.h. */
23545
23546 const gdb_byte *
23547 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23548 dwarf2_per_cu_data *per_cu,
23549 dwarf2_per_objfile *per_objfile,
23550 obstack *obstack,
23551 LONGEST *len)
23552 {
23553 struct die_info *die;
23554 struct attribute *attr;
23555 const gdb_byte *result = NULL;
23556 struct type *type;
23557 LONGEST value;
23558 enum bfd_endian byte_order;
23559 struct objfile *objfile = per_objfile->objfile;
23560
23561 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23562 if (cu == nullptr)
23563 cu = load_cu (per_cu, per_objfile, false);
23564
23565 if (cu == nullptr)
23566 {
23567 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23568 Instead just throw an error, not much else we can do. */
23569 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23570 sect_offset_str (sect_off), objfile_name (objfile));
23571 }
23572
23573 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23574 if (!die)
23575 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23576 sect_offset_str (sect_off), objfile_name (objfile));
23577
23578 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23579 if (attr == NULL)
23580 return NULL;
23581
23582 byte_order = (bfd_big_endian (objfile->obfd)
23583 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23584
23585 switch (attr->form)
23586 {
23587 case DW_FORM_addr:
23588 case DW_FORM_addrx:
23589 case DW_FORM_GNU_addr_index:
23590 {
23591 gdb_byte *tem;
23592
23593 *len = cu->header.addr_size;
23594 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23595 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23596 result = tem;
23597 }
23598 break;
23599 case DW_FORM_string:
23600 case DW_FORM_strp:
23601 case DW_FORM_strx:
23602 case DW_FORM_GNU_str_index:
23603 case DW_FORM_GNU_strp_alt:
23604 /* The string is already allocated on the objfile obstack, point
23605 directly to it. */
23606 {
23607 const char *attr_name = attr->as_string ();
23608 result = (const gdb_byte *) attr_name;
23609 *len = strlen (attr_name);
23610 }
23611 break;
23612 case DW_FORM_block1:
23613 case DW_FORM_block2:
23614 case DW_FORM_block4:
23615 case DW_FORM_block:
23616 case DW_FORM_exprloc:
23617 case DW_FORM_data16:
23618 {
23619 struct dwarf_block *block = attr->as_block ();
23620 result = block->data;
23621 *len = block->size;
23622 }
23623 break;
23624
23625 /* The DW_AT_const_value attributes are supposed to carry the
23626 symbol's value "represented as it would be on the target
23627 architecture." By the time we get here, it's already been
23628 converted to host endianness, so we just need to sign- or
23629 zero-extend it as appropriate. */
23630 case DW_FORM_data1:
23631 type = die_type (die, cu);
23632 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23633 if (result == NULL)
23634 result = write_constant_as_bytes (obstack, byte_order,
23635 type, value, len);
23636 break;
23637 case DW_FORM_data2:
23638 type = die_type (die, cu);
23639 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23640 if (result == NULL)
23641 result = write_constant_as_bytes (obstack, byte_order,
23642 type, value, len);
23643 break;
23644 case DW_FORM_data4:
23645 type = die_type (die, cu);
23646 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23647 if (result == NULL)
23648 result = write_constant_as_bytes (obstack, byte_order,
23649 type, value, len);
23650 break;
23651 case DW_FORM_data8:
23652 type = die_type (die, cu);
23653 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23654 if (result == NULL)
23655 result = write_constant_as_bytes (obstack, byte_order,
23656 type, value, len);
23657 break;
23658
23659 case DW_FORM_sdata:
23660 case DW_FORM_implicit_const:
23661 type = die_type (die, cu);
23662 result = write_constant_as_bytes (obstack, byte_order,
23663 type, attr->as_signed (), len);
23664 break;
23665
23666 case DW_FORM_udata:
23667 type = die_type (die, cu);
23668 result = write_constant_as_bytes (obstack, byte_order,
23669 type, attr->as_unsigned (), len);
23670 break;
23671
23672 default:
23673 complaint (_("unsupported const value attribute form: '%s'"),
23674 dwarf_form_name (attr->form));
23675 break;
23676 }
23677
23678 return result;
23679 }
23680
23681 /* See read.h. */
23682
23683 struct type *
23684 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23685 dwarf2_per_cu_data *per_cu,
23686 dwarf2_per_objfile *per_objfile)
23687 {
23688 struct die_info *die;
23689
23690 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23691 if (cu == nullptr)
23692 cu = load_cu (per_cu, per_objfile, false);
23693
23694 if (cu == nullptr)
23695 return nullptr;
23696
23697 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23698 if (!die)
23699 return NULL;
23700
23701 return die_type (die, cu);
23702 }
23703
23704 /* See read.h. */
23705
23706 struct type *
23707 dwarf2_get_die_type (cu_offset die_offset,
23708 dwarf2_per_cu_data *per_cu,
23709 dwarf2_per_objfile *per_objfile)
23710 {
23711 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23712 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23713 }
23714
23715 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23716 On entry *REF_CU is the CU of SRC_DIE.
23717 On exit *REF_CU is the CU of the result.
23718 Returns NULL if the referenced DIE isn't found. */
23719
23720 static struct die_info *
23721 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23722 struct dwarf2_cu **ref_cu)
23723 {
23724 struct die_info temp_die;
23725 struct dwarf2_cu *sig_cu;
23726 struct die_info *die;
23727 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23728
23729
23730 /* While it might be nice to assert sig_type->type == NULL here,
23731 we can get here for DW_AT_imported_declaration where we need
23732 the DIE not the type. */
23733
23734 /* If necessary, add it to the queue and load its DIEs.
23735
23736 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23737 it doesn't mean they are currently loaded. Since we require them
23738 to be loaded, we must check for ourselves. */
23739 if (maybe_queue_comp_unit (*ref_cu, sig_type, per_objfile,
23740 language_minimal)
23741 || per_objfile->get_cu (sig_type) == nullptr)
23742 read_signatured_type (sig_type, per_objfile);
23743
23744 sig_cu = per_objfile->get_cu (sig_type);
23745 gdb_assert (sig_cu != NULL);
23746 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23747 temp_die.sect_off = sig_type->type_offset_in_section;
23748 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23749 to_underlying (temp_die.sect_off));
23750 if (die)
23751 {
23752 /* For .gdb_index version 7 keep track of included TUs.
23753 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23754 if (per_objfile->per_bfd->index_table != NULL
23755 && per_objfile->per_bfd->index_table->version <= 7)
23756 {
23757 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23758 }
23759
23760 *ref_cu = sig_cu;
23761
23762 return die;
23763 }
23764
23765 return NULL;
23766 }
23767
23768 /* Follow signatured type referenced by ATTR in SRC_DIE.
23769 On entry *REF_CU is the CU of SRC_DIE.
23770 On exit *REF_CU is the CU of the result.
23771 The result is the DIE of the type.
23772 If the referenced type cannot be found an error is thrown. */
23773
23774 static struct die_info *
23775 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23776 struct dwarf2_cu **ref_cu)
23777 {
23778 ULONGEST signature = attr->as_signature ();
23779 struct signatured_type *sig_type;
23780 struct die_info *die;
23781
23782 gdb_assert (attr->form == DW_FORM_ref_sig8);
23783
23784 sig_type = lookup_signatured_type (*ref_cu, signature);
23785 /* sig_type will be NULL if the signatured type is missing from
23786 the debug info. */
23787 if (sig_type == NULL)
23788 {
23789 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23790 " from DIE at %s [in module %s]"),
23791 hex_string (signature), sect_offset_str (src_die->sect_off),
23792 objfile_name ((*ref_cu)->per_objfile->objfile));
23793 }
23794
23795 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23796 if (die == NULL)
23797 {
23798 dump_die_for_error (src_die);
23799 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23800 " from DIE at %s [in module %s]"),
23801 hex_string (signature), sect_offset_str (src_die->sect_off),
23802 objfile_name ((*ref_cu)->per_objfile->objfile));
23803 }
23804
23805 return die;
23806 }
23807
23808 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23809 reading in and processing the type unit if necessary. */
23810
23811 static struct type *
23812 get_signatured_type (struct die_info *die, ULONGEST signature,
23813 struct dwarf2_cu *cu)
23814 {
23815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23816 struct signatured_type *sig_type;
23817 struct dwarf2_cu *type_cu;
23818 struct die_info *type_die;
23819 struct type *type;
23820
23821 sig_type = lookup_signatured_type (cu, signature);
23822 /* sig_type will be NULL if the signatured type is missing from
23823 the debug info. */
23824 if (sig_type == NULL)
23825 {
23826 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23827 " from DIE at %s [in module %s]"),
23828 hex_string (signature), sect_offset_str (die->sect_off),
23829 objfile_name (per_objfile->objfile));
23830 return build_error_marker_type (cu, die);
23831 }
23832
23833 /* If we already know the type we're done. */
23834 type = per_objfile->get_type_for_signatured_type (sig_type);
23835 if (type != nullptr)
23836 return type;
23837
23838 type_cu = cu;
23839 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23840 if (type_die != NULL)
23841 {
23842 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23843 is created. This is important, for example, because for c++ classes
23844 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23845 type = read_type_die (type_die, type_cu);
23846 if (type == NULL)
23847 {
23848 complaint (_("Dwarf Error: Cannot build signatured type %s"
23849 " referenced from DIE at %s [in module %s]"),
23850 hex_string (signature), sect_offset_str (die->sect_off),
23851 objfile_name (per_objfile->objfile));
23852 type = build_error_marker_type (cu, die);
23853 }
23854 }
23855 else
23856 {
23857 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23858 " from DIE at %s [in module %s]"),
23859 hex_string (signature), sect_offset_str (die->sect_off),
23860 objfile_name (per_objfile->objfile));
23861 type = build_error_marker_type (cu, die);
23862 }
23863
23864 per_objfile->set_type_for_signatured_type (sig_type, type);
23865
23866 return type;
23867 }
23868
23869 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23870 reading in and processing the type unit if necessary. */
23871
23872 static struct type *
23873 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
23874 struct dwarf2_cu *cu) /* ARI: editCase function */
23875 {
23876 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
23877 if (attr->form_is_ref ())
23878 {
23879 struct dwarf2_cu *type_cu = cu;
23880 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23881
23882 return read_type_die (type_die, type_cu);
23883 }
23884 else if (attr->form == DW_FORM_ref_sig8)
23885 {
23886 return get_signatured_type (die, attr->as_signature (), cu);
23887 }
23888 else
23889 {
23890 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23891
23892 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
23893 " at %s [in module %s]"),
23894 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
23895 objfile_name (per_objfile->objfile));
23896 return build_error_marker_type (cu, die);
23897 }
23898 }
23899
23900 /* Load the DIEs associated with type unit PER_CU into memory. */
23901
23902 static void
23903 load_full_type_unit (dwarf2_per_cu_data *per_cu,
23904 dwarf2_per_objfile *per_objfile)
23905 {
23906 struct signatured_type *sig_type;
23907
23908 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23909 gdb_assert (! per_cu->type_unit_group_p ());
23910
23911 /* We have the per_cu, but we need the signatured_type.
23912 Fortunately this is an easy translation. */
23913 gdb_assert (per_cu->is_debug_types);
23914 sig_type = (struct signatured_type *) per_cu;
23915
23916 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
23917
23918 read_signatured_type (sig_type, per_objfile);
23919
23920 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
23921 }
23922
23923 /* Read in a signatured type and build its CU and DIEs.
23924 If the type is a stub for the real type in a DWO file,
23925 read in the real type from the DWO file as well. */
23926
23927 static void
23928 read_signatured_type (signatured_type *sig_type,
23929 dwarf2_per_objfile *per_objfile)
23930 {
23931 gdb_assert (sig_type->is_debug_types);
23932 gdb_assert (per_objfile->get_cu (sig_type) == nullptr);
23933
23934 cutu_reader reader (sig_type, per_objfile, nullptr, nullptr, false);
23935
23936 if (!reader.dummy_p)
23937 {
23938 struct dwarf2_cu *cu = reader.cu;
23939 const gdb_byte *info_ptr = reader.info_ptr;
23940
23941 gdb_assert (cu->die_hash == NULL);
23942 cu->die_hash =
23943 htab_create_alloc_ex (cu->header.length / 12,
23944 die_hash,
23945 die_eq,
23946 NULL,
23947 &cu->comp_unit_obstack,
23948 hashtab_obstack_allocate,
23949 dummy_obstack_deallocate);
23950
23951 if (reader.comp_unit_die->has_children)
23952 reader.comp_unit_die->child
23953 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
23954 reader.comp_unit_die);
23955 cu->dies = reader.comp_unit_die;
23956 /* comp_unit_die is not stored in die_hash, no need. */
23957
23958 /* We try not to read any attributes in this function, because
23959 not all CUs needed for references have been loaded yet, and
23960 symbol table processing isn't initialized. But we have to
23961 set the CU language, or we won't be able to build types
23962 correctly. Similarly, if we do not read the producer, we can
23963 not apply producer-specific interpretation. */
23964 prepare_one_comp_unit (cu, cu->dies, language_minimal);
23965
23966 reader.keep ();
23967 }
23968
23969 sig_type->tu_read = 1;
23970 }
23971
23972 /* Decode simple location descriptions.
23973 Given a pointer to a dwarf block that defines a location, compute
23974 the location and return the value. If COMPUTED is non-null, it is
23975 set to true to indicate that decoding was successful, and false
23976 otherwise. If COMPUTED is null, then this function may emit a
23977 complaint. */
23978
23979 static CORE_ADDR
23980 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
23981 {
23982 struct objfile *objfile = cu->per_objfile->objfile;
23983 size_t i;
23984 size_t size = blk->size;
23985 const gdb_byte *data = blk->data;
23986 CORE_ADDR stack[64];
23987 int stacki;
23988 unsigned int bytes_read, unsnd;
23989 gdb_byte op;
23990
23991 if (computed != nullptr)
23992 *computed = false;
23993
23994 i = 0;
23995 stacki = 0;
23996 stack[stacki] = 0;
23997 stack[++stacki] = 0;
23998
23999 while (i < size)
24000 {
24001 op = data[i++];
24002 switch (op)
24003 {
24004 case DW_OP_lit0:
24005 case DW_OP_lit1:
24006 case DW_OP_lit2:
24007 case DW_OP_lit3:
24008 case DW_OP_lit4:
24009 case DW_OP_lit5:
24010 case DW_OP_lit6:
24011 case DW_OP_lit7:
24012 case DW_OP_lit8:
24013 case DW_OP_lit9:
24014 case DW_OP_lit10:
24015 case DW_OP_lit11:
24016 case DW_OP_lit12:
24017 case DW_OP_lit13:
24018 case DW_OP_lit14:
24019 case DW_OP_lit15:
24020 case DW_OP_lit16:
24021 case DW_OP_lit17:
24022 case DW_OP_lit18:
24023 case DW_OP_lit19:
24024 case DW_OP_lit20:
24025 case DW_OP_lit21:
24026 case DW_OP_lit22:
24027 case DW_OP_lit23:
24028 case DW_OP_lit24:
24029 case DW_OP_lit25:
24030 case DW_OP_lit26:
24031 case DW_OP_lit27:
24032 case DW_OP_lit28:
24033 case DW_OP_lit29:
24034 case DW_OP_lit30:
24035 case DW_OP_lit31:
24036 stack[++stacki] = op - DW_OP_lit0;
24037 break;
24038
24039 case DW_OP_reg0:
24040 case DW_OP_reg1:
24041 case DW_OP_reg2:
24042 case DW_OP_reg3:
24043 case DW_OP_reg4:
24044 case DW_OP_reg5:
24045 case DW_OP_reg6:
24046 case DW_OP_reg7:
24047 case DW_OP_reg8:
24048 case DW_OP_reg9:
24049 case DW_OP_reg10:
24050 case DW_OP_reg11:
24051 case DW_OP_reg12:
24052 case DW_OP_reg13:
24053 case DW_OP_reg14:
24054 case DW_OP_reg15:
24055 case DW_OP_reg16:
24056 case DW_OP_reg17:
24057 case DW_OP_reg18:
24058 case DW_OP_reg19:
24059 case DW_OP_reg20:
24060 case DW_OP_reg21:
24061 case DW_OP_reg22:
24062 case DW_OP_reg23:
24063 case DW_OP_reg24:
24064 case DW_OP_reg25:
24065 case DW_OP_reg26:
24066 case DW_OP_reg27:
24067 case DW_OP_reg28:
24068 case DW_OP_reg29:
24069 case DW_OP_reg30:
24070 case DW_OP_reg31:
24071 stack[++stacki] = op - DW_OP_reg0;
24072 if (i < size)
24073 {
24074 if (computed == nullptr)
24075 dwarf2_complex_location_expr_complaint ();
24076 else
24077 return 0;
24078 }
24079 break;
24080
24081 case DW_OP_regx:
24082 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24083 i += bytes_read;
24084 stack[++stacki] = unsnd;
24085 if (i < size)
24086 {
24087 if (computed == nullptr)
24088 dwarf2_complex_location_expr_complaint ();
24089 else
24090 return 0;
24091 }
24092 break;
24093
24094 case DW_OP_addr:
24095 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24096 &bytes_read);
24097 i += bytes_read;
24098 break;
24099
24100 case DW_OP_const1u:
24101 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24102 i += 1;
24103 break;
24104
24105 case DW_OP_const1s:
24106 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24107 i += 1;
24108 break;
24109
24110 case DW_OP_const2u:
24111 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24112 i += 2;
24113 break;
24114
24115 case DW_OP_const2s:
24116 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24117 i += 2;
24118 break;
24119
24120 case DW_OP_const4u:
24121 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24122 i += 4;
24123 break;
24124
24125 case DW_OP_const4s:
24126 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24127 i += 4;
24128 break;
24129
24130 case DW_OP_const8u:
24131 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24132 i += 8;
24133 break;
24134
24135 case DW_OP_constu:
24136 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24137 &bytes_read);
24138 i += bytes_read;
24139 break;
24140
24141 case DW_OP_consts:
24142 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24143 i += bytes_read;
24144 break;
24145
24146 case DW_OP_dup:
24147 stack[stacki + 1] = stack[stacki];
24148 stacki++;
24149 break;
24150
24151 case DW_OP_plus:
24152 stack[stacki - 1] += stack[stacki];
24153 stacki--;
24154 break;
24155
24156 case DW_OP_plus_uconst:
24157 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24158 &bytes_read);
24159 i += bytes_read;
24160 break;
24161
24162 case DW_OP_minus:
24163 stack[stacki - 1] -= stack[stacki];
24164 stacki--;
24165 break;
24166
24167 case DW_OP_deref:
24168 /* If we're not the last op, then we definitely can't encode
24169 this using GDB's address_class enum. This is valid for partial
24170 global symbols, although the variable's address will be bogus
24171 in the psymtab. */
24172 if (i < size)
24173 {
24174 if (computed == nullptr)
24175 dwarf2_complex_location_expr_complaint ();
24176 else
24177 return 0;
24178 }
24179 break;
24180
24181 case DW_OP_GNU_push_tls_address:
24182 case DW_OP_form_tls_address:
24183 /* The top of the stack has the offset from the beginning
24184 of the thread control block at which the variable is located. */
24185 /* Nothing should follow this operator, so the top of stack would
24186 be returned. */
24187 /* This is valid for partial global symbols, but the variable's
24188 address will be bogus in the psymtab. Make it always at least
24189 non-zero to not look as a variable garbage collected by linker
24190 which have DW_OP_addr 0. */
24191 if (i < size)
24192 {
24193 if (computed == nullptr)
24194 dwarf2_complex_location_expr_complaint ();
24195 else
24196 return 0;
24197 }
24198 stack[stacki]++;
24199 break;
24200
24201 case DW_OP_GNU_uninit:
24202 if (computed != nullptr)
24203 return 0;
24204 break;
24205
24206 case DW_OP_addrx:
24207 case DW_OP_GNU_addr_index:
24208 case DW_OP_GNU_const_index:
24209 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24210 &bytes_read);
24211 i += bytes_read;
24212 break;
24213
24214 default:
24215 if (computed == nullptr)
24216 {
24217 const char *name = get_DW_OP_name (op);
24218
24219 if (name)
24220 complaint (_("unsupported stack op: '%s'"),
24221 name);
24222 else
24223 complaint (_("unsupported stack op: '%02x'"),
24224 op);
24225 }
24226
24227 return (stack[stacki]);
24228 }
24229
24230 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24231 outside of the allocated space. Also enforce minimum>0. */
24232 if (stacki >= ARRAY_SIZE (stack) - 1)
24233 {
24234 if (computed == nullptr)
24235 complaint (_("location description stack overflow"));
24236 return 0;
24237 }
24238
24239 if (stacki <= 0)
24240 {
24241 if (computed == nullptr)
24242 complaint (_("location description stack underflow"));
24243 return 0;
24244 }
24245 }
24246
24247 if (computed != nullptr)
24248 *computed = true;
24249 return (stack[stacki]);
24250 }
24251
24252 /* memory allocation interface */
24253
24254 static struct dwarf_block *
24255 dwarf_alloc_block (struct dwarf2_cu *cu)
24256 {
24257 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24258 }
24259
24260 static struct die_info *
24261 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24262 {
24263 struct die_info *die;
24264 size_t size = sizeof (struct die_info);
24265
24266 if (num_attrs > 1)
24267 size += (num_attrs - 1) * sizeof (struct attribute);
24268
24269 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24270 memset (die, 0, sizeof (struct die_info));
24271 return (die);
24272 }
24273
24274 \f
24275
24276 /* Macro support. */
24277
24278 /* An overload of dwarf_decode_macros that finds the correct section
24279 and ensures it is read in before calling the other overload. */
24280
24281 static void
24282 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24283 int section_is_gnu)
24284 {
24285 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24286 struct objfile *objfile = per_objfile->objfile;
24287 const struct line_header *lh = cu->line_header;
24288 unsigned int offset_size = cu->header.offset_size;
24289 struct dwarf2_section_info *section;
24290 const char *section_name;
24291
24292 if (cu->dwo_unit != nullptr)
24293 {
24294 if (section_is_gnu)
24295 {
24296 section = &cu->dwo_unit->dwo_file->sections.macro;
24297 section_name = ".debug_macro.dwo";
24298 }
24299 else
24300 {
24301 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24302 section_name = ".debug_macinfo.dwo";
24303 }
24304 }
24305 else
24306 {
24307 if (section_is_gnu)
24308 {
24309 section = &per_objfile->per_bfd->macro;
24310 section_name = ".debug_macro";
24311 }
24312 else
24313 {
24314 section = &per_objfile->per_bfd->macinfo;
24315 section_name = ".debug_macinfo";
24316 }
24317 }
24318
24319 section->read (objfile);
24320 if (section->buffer == nullptr)
24321 {
24322 complaint (_("missing %s section"), section_name);
24323 return;
24324 }
24325
24326 buildsym_compunit *builder = cu->get_builder ();
24327
24328 struct dwarf2_section_info *str_offsets_section;
24329 struct dwarf2_section_info *str_section;
24330 ULONGEST str_offsets_base;
24331
24332 if (cu->dwo_unit != nullptr)
24333 {
24334 str_offsets_section = &cu->dwo_unit->dwo_file
24335 ->sections.str_offsets;
24336 str_section = &cu->dwo_unit->dwo_file->sections.str;
24337 str_offsets_base = cu->header.addr_size;
24338 }
24339 else
24340 {
24341 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24342 str_section = &per_objfile->per_bfd->str;
24343 str_offsets_base = *cu->str_offsets_base;
24344 }
24345
24346 dwarf_decode_macros (per_objfile, builder, section, lh,
24347 offset_size, offset, str_section, str_offsets_section,
24348 str_offsets_base, section_is_gnu);
24349 }
24350
24351 /* Return the .debug_loc section to use for CU.
24352 For DWO files use .debug_loc.dwo. */
24353
24354 static struct dwarf2_section_info *
24355 cu_debug_loc_section (struct dwarf2_cu *cu)
24356 {
24357 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24358
24359 if (cu->dwo_unit)
24360 {
24361 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24362
24363 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24364 }
24365 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24366 : &per_objfile->per_bfd->loc);
24367 }
24368
24369 /* Return the .debug_rnglists section to use for CU. */
24370 static struct dwarf2_section_info *
24371 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24372 {
24373 if (cu->header.version < 5)
24374 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24375 cu->header.version);
24376 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24377
24378 /* Make sure we read the .debug_rnglists section from the file that
24379 contains the DW_AT_ranges attribute we are reading. Normally that
24380 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24381 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24382 program. */
24383 if (cu->dwo_unit != nullptr
24384 && tag != DW_TAG_compile_unit
24385 && tag != DW_TAG_skeleton_unit)
24386 {
24387 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24388
24389 if (sections->rnglists.size > 0)
24390 return &sections->rnglists;
24391 else
24392 error (_(".debug_rnglists section is missing from .dwo file."));
24393 }
24394 return &dwarf2_per_objfile->per_bfd->rnglists;
24395 }
24396
24397 /* A helper function that fills in a dwarf2_loclist_baton. */
24398
24399 static void
24400 fill_in_loclist_baton (struct dwarf2_cu *cu,
24401 struct dwarf2_loclist_baton *baton,
24402 const struct attribute *attr)
24403 {
24404 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24405 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24406
24407 section->read (per_objfile->objfile);
24408
24409 baton->per_objfile = per_objfile;
24410 baton->per_cu = cu->per_cu;
24411 gdb_assert (baton->per_cu);
24412 /* We don't know how long the location list is, but make sure we
24413 don't run off the edge of the section. */
24414 baton->size = section->size - attr->as_unsigned ();
24415 baton->data = section->buffer + attr->as_unsigned ();
24416 if (cu->base_address.has_value ())
24417 baton->base_address = *cu->base_address;
24418 else
24419 baton->base_address = 0;
24420 baton->from_dwo = cu->dwo_unit != NULL;
24421 }
24422
24423 static void
24424 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24425 struct dwarf2_cu *cu, int is_block)
24426 {
24427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24428 struct objfile *objfile = per_objfile->objfile;
24429 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24430
24431 if (attr->form_is_section_offset ()
24432 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24433 the section. If so, fall through to the complaint in the
24434 other branch. */
24435 && attr->as_unsigned () < section->get_size (objfile))
24436 {
24437 struct dwarf2_loclist_baton *baton;
24438
24439 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24440
24441 fill_in_loclist_baton (cu, baton, attr);
24442
24443 if (!cu->base_address.has_value ())
24444 complaint (_("Location list used without "
24445 "specifying the CU base address."));
24446
24447 SYMBOL_ACLASS_INDEX (sym) = (is_block
24448 ? dwarf2_loclist_block_index
24449 : dwarf2_loclist_index);
24450 SYMBOL_LOCATION_BATON (sym) = baton;
24451 }
24452 else
24453 {
24454 struct dwarf2_locexpr_baton *baton;
24455
24456 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24457 baton->per_objfile = per_objfile;
24458 baton->per_cu = cu->per_cu;
24459 gdb_assert (baton->per_cu);
24460
24461 if (attr->form_is_block ())
24462 {
24463 /* Note that we're just copying the block's data pointer
24464 here, not the actual data. We're still pointing into the
24465 info_buffer for SYM's objfile; right now we never release
24466 that buffer, but when we do clean up properly this may
24467 need to change. */
24468 struct dwarf_block *block = attr->as_block ();
24469 baton->size = block->size;
24470 baton->data = block->data;
24471 }
24472 else
24473 {
24474 dwarf2_invalid_attrib_class_complaint ("location description",
24475 sym->natural_name ());
24476 baton->size = 0;
24477 }
24478
24479 SYMBOL_ACLASS_INDEX (sym) = (is_block
24480 ? dwarf2_locexpr_block_index
24481 : dwarf2_locexpr_index);
24482 SYMBOL_LOCATION_BATON (sym) = baton;
24483 }
24484 }
24485
24486 /* See read.h. */
24487
24488 const comp_unit_head *
24489 dwarf2_per_cu_data::get_header () const
24490 {
24491 if (!m_header_read_in)
24492 {
24493 const gdb_byte *info_ptr
24494 = this->section->buffer + to_underlying (this->sect_off);
24495
24496 memset (&m_header, 0, sizeof (m_header));
24497
24498 read_comp_unit_head (&m_header, info_ptr, this->section,
24499 rcuh_kind::COMPILE);
24500
24501 m_header_read_in = true;
24502 }
24503
24504 return &m_header;
24505 }
24506
24507 /* See read.h. */
24508
24509 int
24510 dwarf2_per_cu_data::addr_size () const
24511 {
24512 return this->get_header ()->addr_size;
24513 }
24514
24515 /* See read.h. */
24516
24517 int
24518 dwarf2_per_cu_data::offset_size () const
24519 {
24520 return this->get_header ()->offset_size;
24521 }
24522
24523 /* See read.h. */
24524
24525 int
24526 dwarf2_per_cu_data::ref_addr_size () const
24527 {
24528 const comp_unit_head *header = this->get_header ();
24529
24530 if (header->version == 2)
24531 return header->addr_size;
24532 else
24533 return header->offset_size;
24534 }
24535
24536 /* See read.h. */
24537
24538 struct type *
24539 dwarf2_cu::addr_type () const
24540 {
24541 struct objfile *objfile = this->per_objfile->objfile;
24542 struct type *void_type = objfile_type (objfile)->builtin_void;
24543 struct type *addr_type = lookup_pointer_type (void_type);
24544 int addr_size = this->per_cu->addr_size ();
24545
24546 if (TYPE_LENGTH (addr_type) == addr_size)
24547 return addr_type;
24548
24549 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24550 return addr_type;
24551 }
24552
24553 /* A helper function for dwarf2_find_containing_comp_unit that returns
24554 the index of the result, and that searches a vector. It will
24555 return a result even if the offset in question does not actually
24556 occur in any CU. This is separate so that it can be unit
24557 tested. */
24558
24559 static int
24560 dwarf2_find_containing_comp_unit
24561 (sect_offset sect_off,
24562 unsigned int offset_in_dwz,
24563 const std::vector<std::unique_ptr<dwarf2_per_cu_data>> &all_comp_units)
24564 {
24565 int low, high;
24566
24567 low = 0;
24568 high = all_comp_units.size () - 1;
24569 while (high > low)
24570 {
24571 struct dwarf2_per_cu_data *mid_cu;
24572 int mid = low + (high - low) / 2;
24573
24574 mid_cu = all_comp_units[mid].get ();
24575 if (mid_cu->is_dwz > offset_in_dwz
24576 || (mid_cu->is_dwz == offset_in_dwz
24577 && mid_cu->sect_off + mid_cu->length > sect_off))
24578 high = mid;
24579 else
24580 low = mid + 1;
24581 }
24582 gdb_assert (low == high);
24583 return low;
24584 }
24585
24586 /* Locate the .debug_info compilation unit from CU's objfile which contains
24587 the DIE at OFFSET. Raises an error on failure. */
24588
24589 static struct dwarf2_per_cu_data *
24590 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24591 unsigned int offset_in_dwz,
24592 dwarf2_per_objfile *per_objfile)
24593 {
24594 int low = dwarf2_find_containing_comp_unit
24595 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24596 dwarf2_per_cu_data *this_cu
24597 = per_objfile->per_bfd->all_comp_units[low].get ();
24598
24599 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24600 {
24601 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24602 error (_("Dwarf Error: could not find partial DIE containing "
24603 "offset %s [in module %s]"),
24604 sect_offset_str (sect_off),
24605 bfd_get_filename (per_objfile->objfile->obfd));
24606
24607 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24608 <= sect_off);
24609 return per_objfile->per_bfd->all_comp_units[low - 1].get ();
24610 }
24611 else
24612 {
24613 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24614 && sect_off >= this_cu->sect_off + this_cu->length)
24615 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24616 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24617 return this_cu;
24618 }
24619 }
24620
24621 #if GDB_SELF_TEST
24622
24623 namespace selftests {
24624 namespace find_containing_comp_unit {
24625
24626 static void
24627 run_test ()
24628 {
24629 std::unique_ptr<dwarf2_per_cu_data> one (new dwarf2_per_cu_data);
24630 dwarf2_per_cu_data *one_ptr = one.get ();
24631 std::unique_ptr<dwarf2_per_cu_data> two (new dwarf2_per_cu_data);
24632 dwarf2_per_cu_data *two_ptr = two.get ();
24633 std::unique_ptr<dwarf2_per_cu_data> three (new dwarf2_per_cu_data);
24634 dwarf2_per_cu_data *three_ptr = three.get ();
24635 std::unique_ptr<dwarf2_per_cu_data> four (new dwarf2_per_cu_data);
24636 dwarf2_per_cu_data *four_ptr = four.get ();
24637
24638 one->length = 5;
24639 two->sect_off = sect_offset (one->length);
24640 two->length = 7;
24641
24642 three->length = 5;
24643 three->is_dwz = 1;
24644 four->sect_off = sect_offset (three->length);
24645 four->length = 7;
24646 four->is_dwz = 1;
24647
24648 std::vector<std::unique_ptr<dwarf2_per_cu_data>> units;
24649 units.push_back (std::move (one));
24650 units.push_back (std::move (two));
24651 units.push_back (std::move (three));
24652 units.push_back (std::move (four));
24653
24654 int result;
24655
24656 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24657 SELF_CHECK (units[result].get () == one_ptr);
24658 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24659 SELF_CHECK (units[result].get () == one_ptr);
24660 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24661 SELF_CHECK (units[result].get () == two_ptr);
24662
24663 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24664 SELF_CHECK (units[result].get () == three_ptr);
24665 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24666 SELF_CHECK (units[result].get () == three_ptr);
24667 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24668 SELF_CHECK (units[result].get () == four_ptr);
24669 }
24670
24671 }
24672 }
24673
24674 #endif /* GDB_SELF_TEST */
24675
24676 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24677
24678 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24679 dwarf2_per_objfile *per_objfile)
24680 : per_cu (per_cu),
24681 per_objfile (per_objfile),
24682 mark (false),
24683 has_loclist (false),
24684 checked_producer (false),
24685 producer_is_gxx_lt_4_6 (false),
24686 producer_is_gcc_lt_4_3 (false),
24687 producer_is_icc (false),
24688 producer_is_icc_lt_14 (false),
24689 producer_is_codewarrior (false),
24690 processing_has_namespace_info (false)
24691 {
24692 }
24693
24694 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24695
24696 static void
24697 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24698 enum language pretend_language)
24699 {
24700 struct attribute *attr;
24701
24702 /* Set the language we're debugging. */
24703 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24704 if (attr != nullptr)
24705 set_cu_language (attr->constant_value (0), cu);
24706 else
24707 {
24708 cu->language = pretend_language;
24709 cu->language_defn = language_def (cu->language);
24710 }
24711
24712 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24713 }
24714
24715 /* See read.h. */
24716
24717 dwarf2_cu *
24718 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24719 {
24720 auto it = m_dwarf2_cus.find (per_cu);
24721 if (it == m_dwarf2_cus.end ())
24722 return nullptr;
24723
24724 return it->second;
24725 }
24726
24727 /* See read.h. */
24728
24729 void
24730 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24731 {
24732 gdb_assert (this->get_cu (per_cu) == nullptr);
24733
24734 m_dwarf2_cus[per_cu] = cu;
24735 }
24736
24737 /* See read.h. */
24738
24739 void
24740 dwarf2_per_objfile::age_comp_units ()
24741 {
24742 dwarf_read_debug_printf_v ("running");
24743
24744 /* This is not expected to be called in the middle of CU expansion. There is
24745 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24746 loaded in memory. Calling age_comp_units while the queue is in use could
24747 make us free the DIEs for a CU that is in the queue and therefore break
24748 that invariant. */
24749 gdb_assert (!this->per_bfd->queue.has_value ());
24750
24751 /* Start by clearing all marks. */
24752 for (auto pair : m_dwarf2_cus)
24753 pair.second->mark = false;
24754
24755 /* Traverse all CUs, mark them and their dependencies if used recently
24756 enough. */
24757 for (auto pair : m_dwarf2_cus)
24758 {
24759 dwarf2_cu *cu = pair.second;
24760
24761 cu->last_used++;
24762 if (cu->last_used <= dwarf_max_cache_age)
24763 dwarf2_mark (cu);
24764 }
24765
24766 /* Delete all CUs still not marked. */
24767 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24768 {
24769 dwarf2_cu *cu = it->second;
24770
24771 if (!cu->mark)
24772 {
24773 dwarf_read_debug_printf_v ("deleting old CU %s",
24774 sect_offset_str (cu->per_cu->sect_off));
24775 delete cu;
24776 it = m_dwarf2_cus.erase (it);
24777 }
24778 else
24779 it++;
24780 }
24781 }
24782
24783 /* See read.h. */
24784
24785 void
24786 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24787 {
24788 auto it = m_dwarf2_cus.find (per_cu);
24789 if (it == m_dwarf2_cus.end ())
24790 return;
24791
24792 delete it->second;
24793
24794 m_dwarf2_cus.erase (it);
24795 }
24796
24797 dwarf2_per_objfile::~dwarf2_per_objfile ()
24798 {
24799 remove_all_cus ();
24800 }
24801
24802 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24803 We store these in a hash table separate from the DIEs, and preserve them
24804 when the DIEs are flushed out of cache.
24805
24806 The CU "per_cu" pointer is needed because offset alone is not enough to
24807 uniquely identify the type. A file may have multiple .debug_types sections,
24808 or the type may come from a DWO file. Furthermore, while it's more logical
24809 to use per_cu->section+offset, with Fission the section with the data is in
24810 the DWO file but we don't know that section at the point we need it.
24811 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24812 because we can enter the lookup routine, get_die_type_at_offset, from
24813 outside this file, and thus won't necessarily have PER_CU->cu.
24814 Fortunately, PER_CU is stable for the life of the objfile. */
24815
24816 struct dwarf2_per_cu_offset_and_type
24817 {
24818 const struct dwarf2_per_cu_data *per_cu;
24819 sect_offset sect_off;
24820 struct type *type;
24821 };
24822
24823 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24824
24825 static hashval_t
24826 per_cu_offset_and_type_hash (const void *item)
24827 {
24828 const struct dwarf2_per_cu_offset_and_type *ofs
24829 = (const struct dwarf2_per_cu_offset_and_type *) item;
24830
24831 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24832 }
24833
24834 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24835
24836 static int
24837 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24838 {
24839 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24840 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24841 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24842 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24843
24844 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24845 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24846 }
24847
24848 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24849 table if necessary. For convenience, return TYPE.
24850
24851 The DIEs reading must have careful ordering to:
24852 * Not cause infinite loops trying to read in DIEs as a prerequisite for
24853 reading current DIE.
24854 * Not trying to dereference contents of still incompletely read in types
24855 while reading in other DIEs.
24856 * Enable referencing still incompletely read in types just by a pointer to
24857 the type without accessing its fields.
24858
24859 Therefore caller should follow these rules:
24860 * Try to fetch any prerequisite types we may need to build this DIE type
24861 before building the type and calling set_die_type.
24862 * After building type call set_die_type for current DIE as soon as
24863 possible before fetching more types to complete the current type.
24864 * Make the type as complete as possible before fetching more types. */
24865
24866 static struct type *
24867 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
24868 bool skip_data_location)
24869 {
24870 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24871 struct dwarf2_per_cu_offset_and_type **slot, ofs;
24872 struct objfile *objfile = per_objfile->objfile;
24873 struct attribute *attr;
24874 struct dynamic_prop prop;
24875
24876 /* For Ada types, make sure that the gnat-specific data is always
24877 initialized (if not already set). There are a few types where
24878 we should not be doing so, because the type-specific area is
24879 already used to hold some other piece of info (eg: TYPE_CODE_FLT
24880 where the type-specific area is used to store the floatformat).
24881 But this is not a problem, because the gnat-specific information
24882 is actually not needed for these types. */
24883 if (need_gnat_info (cu)
24884 && type->code () != TYPE_CODE_FUNC
24885 && type->code () != TYPE_CODE_FLT
24886 && type->code () != TYPE_CODE_METHODPTR
24887 && type->code () != TYPE_CODE_MEMBERPTR
24888 && type->code () != TYPE_CODE_METHOD
24889 && type->code () != TYPE_CODE_FIXED_POINT
24890 && !HAVE_GNAT_AUX_INFO (type))
24891 INIT_GNAT_SPECIFIC (type);
24892
24893 /* Read DW_AT_allocated and set in type. */
24894 attr = dwarf2_attr (die, DW_AT_allocated, cu);
24895 if (attr != NULL)
24896 {
24897 struct type *prop_type = cu->addr_sized_int_type (false);
24898 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24899 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
24900 }
24901
24902 /* Read DW_AT_associated and set in type. */
24903 attr = dwarf2_attr (die, DW_AT_associated, cu);
24904 if (attr != NULL)
24905 {
24906 struct type *prop_type = cu->addr_sized_int_type (false);
24907 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
24908 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
24909 }
24910
24911 /* Read DW_AT_data_location and set in type. */
24912 if (!skip_data_location)
24913 {
24914 attr = dwarf2_attr (die, DW_AT_data_location, cu);
24915 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
24916 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
24917 }
24918
24919 if (per_objfile->die_type_hash == NULL)
24920 per_objfile->die_type_hash
24921 = htab_up (htab_create_alloc (127,
24922 per_cu_offset_and_type_hash,
24923 per_cu_offset_and_type_eq,
24924 NULL, xcalloc, xfree));
24925
24926 ofs.per_cu = cu->per_cu;
24927 ofs.sect_off = die->sect_off;
24928 ofs.type = type;
24929 slot = (struct dwarf2_per_cu_offset_and_type **)
24930 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
24931 if (*slot)
24932 complaint (_("A problem internal to GDB: DIE %s has type already set"),
24933 sect_offset_str (die->sect_off));
24934 *slot = XOBNEW (&objfile->objfile_obstack,
24935 struct dwarf2_per_cu_offset_and_type);
24936 **slot = ofs;
24937 return type;
24938 }
24939
24940 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
24941 or return NULL if the die does not have a saved type. */
24942
24943 static struct type *
24944 get_die_type_at_offset (sect_offset sect_off,
24945 dwarf2_per_cu_data *per_cu,
24946 dwarf2_per_objfile *per_objfile)
24947 {
24948 struct dwarf2_per_cu_offset_and_type *slot, ofs;
24949
24950 if (per_objfile->die_type_hash == NULL)
24951 return NULL;
24952
24953 ofs.per_cu = per_cu;
24954 ofs.sect_off = sect_off;
24955 slot = ((struct dwarf2_per_cu_offset_and_type *)
24956 htab_find (per_objfile->die_type_hash.get (), &ofs));
24957 if (slot)
24958 return slot->type;
24959 else
24960 return NULL;
24961 }
24962
24963 /* Look up the type for DIE in CU in die_type_hash,
24964 or return NULL if DIE does not have a saved type. */
24965
24966 static struct type *
24967 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
24968 {
24969 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
24970 }
24971
24972 /* Add a dependence relationship from CU to REF_PER_CU. */
24973
24974 static void
24975 dwarf2_add_dependence (struct dwarf2_cu *cu,
24976 struct dwarf2_per_cu_data *ref_per_cu)
24977 {
24978 void **slot;
24979
24980 if (cu->dependencies == NULL)
24981 cu->dependencies
24982 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
24983 NULL, &cu->comp_unit_obstack,
24984 hashtab_obstack_allocate,
24985 dummy_obstack_deallocate);
24986
24987 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
24988 if (*slot == NULL)
24989 *slot = ref_per_cu;
24990 }
24991
24992 /* Subroutine of dwarf2_mark to pass to htab_traverse.
24993 Set the mark field in every compilation unit in the
24994 cache that we must keep because we are keeping CU.
24995
24996 DATA is the dwarf2_per_objfile object in which to look up CUs. */
24997
24998 static int
24999 dwarf2_mark_helper (void **slot, void *data)
25000 {
25001 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25002 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25003 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25004
25005 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25006 reading of the chain. As such dependencies remain valid it is not much
25007 useful to track and undo them during QUIT cleanups. */
25008 if (cu == nullptr)
25009 return 1;
25010
25011 if (cu->mark)
25012 return 1;
25013
25014 cu->mark = true;
25015
25016 if (cu->dependencies != nullptr)
25017 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25018
25019 return 1;
25020 }
25021
25022 /* Set the mark field in CU and in every other compilation unit in the
25023 cache that we must keep because we are keeping CU. */
25024
25025 static void
25026 dwarf2_mark (struct dwarf2_cu *cu)
25027 {
25028 if (cu->mark)
25029 return;
25030
25031 cu->mark = true;
25032
25033 if (cu->dependencies != nullptr)
25034 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25035 }
25036
25037 /* Trivial hash function for partial_die_info: the hash value of a DIE
25038 is its offset in .debug_info for this objfile. */
25039
25040 static hashval_t
25041 partial_die_hash (const void *item)
25042 {
25043 const struct partial_die_info *part_die
25044 = (const struct partial_die_info *) item;
25045
25046 return to_underlying (part_die->sect_off);
25047 }
25048
25049 /* Trivial comparison function for partial_die_info structures: two DIEs
25050 are equal if they have the same offset. */
25051
25052 static int
25053 partial_die_eq (const void *item_lhs, const void *item_rhs)
25054 {
25055 const struct partial_die_info *part_die_lhs
25056 = (const struct partial_die_info *) item_lhs;
25057 const struct partial_die_info *part_die_rhs
25058 = (const struct partial_die_info *) item_rhs;
25059
25060 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25061 }
25062
25063 struct cmd_list_element *set_dwarf_cmdlist;
25064 struct cmd_list_element *show_dwarf_cmdlist;
25065
25066 static void
25067 show_check_physname (struct ui_file *file, int from_tty,
25068 struct cmd_list_element *c, const char *value)
25069 {
25070 fprintf_filtered (file,
25071 _("Whether to check \"physname\" is %s.\n"),
25072 value);
25073 }
25074
25075 void _initialize_dwarf2_read ();
25076 void
25077 _initialize_dwarf2_read ()
25078 {
25079 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25080 Set DWARF specific variables.\n\
25081 Configure DWARF variables such as the cache size."),
25082 &set_dwarf_cmdlist, "maintenance set dwarf ",
25083 0/*allow-unknown*/, &maintenance_set_cmdlist);
25084
25085 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25086 Show DWARF specific variables.\n\
25087 Show DWARF variables such as the cache size."),
25088 &show_dwarf_cmdlist, "maintenance show dwarf ",
25089 0/*allow-unknown*/, &maintenance_show_cmdlist);
25090
25091 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25092 &dwarf_max_cache_age, _("\
25093 Set the upper bound on the age of cached DWARF compilation units."), _("\
25094 Show the upper bound on the age of cached DWARF compilation units."), _("\
25095 A higher limit means that cached compilation units will be stored\n\
25096 in memory longer, and more total memory will be used. Zero disables\n\
25097 caching, which can slow down startup."),
25098 NULL,
25099 show_dwarf_max_cache_age,
25100 &set_dwarf_cmdlist,
25101 &show_dwarf_cmdlist);
25102
25103 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25104 Set debugging of the DWARF reader."), _("\
25105 Show debugging of the DWARF reader."), _("\
25106 When enabled (non-zero), debugging messages are printed during DWARF\n\
25107 reading and symtab expansion. A value of 1 (one) provides basic\n\
25108 information. A value greater than 1 provides more verbose information."),
25109 NULL,
25110 NULL,
25111 &setdebuglist, &showdebuglist);
25112
25113 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25114 Set debugging of the DWARF DIE reader."), _("\
25115 Show debugging of the DWARF DIE reader."), _("\
25116 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25117 The value is the maximum depth to print."),
25118 NULL,
25119 NULL,
25120 &setdebuglist, &showdebuglist);
25121
25122 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25123 Set debugging of the dwarf line reader."), _("\
25124 Show debugging of the dwarf line reader."), _("\
25125 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25126 A value of 1 (one) provides basic information.\n\
25127 A value greater than 1 provides more verbose information."),
25128 NULL,
25129 NULL,
25130 &setdebuglist, &showdebuglist);
25131
25132 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25133 Set cross-checking of \"physname\" code against demangler."), _("\
25134 Show cross-checking of \"physname\" code against demangler."), _("\
25135 When enabled, GDB's internal \"physname\" code is checked against\n\
25136 the demangler."),
25137 NULL, show_check_physname,
25138 &setdebuglist, &showdebuglist);
25139
25140 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25141 no_class, &use_deprecated_index_sections, _("\
25142 Set whether to use deprecated gdb_index sections."), _("\
25143 Show whether to use deprecated gdb_index sections."), _("\
25144 When enabled, deprecated .gdb_index sections are used anyway.\n\
25145 Normally they are ignored either because of a missing feature or\n\
25146 performance issue.\n\
25147 Warning: This option must be enabled before gdb reads the file."),
25148 NULL,
25149 NULL,
25150 &setlist, &showlist);
25151
25152 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25153 &dwarf2_locexpr_funcs);
25154 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25155 &dwarf2_loclist_funcs);
25156
25157 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25158 &dwarf2_block_frame_base_locexpr_funcs);
25159 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25160 &dwarf2_block_frame_base_loclist_funcs);
25161
25162 #if GDB_SELF_TEST
25163 selftests::register_test ("dw2_expand_symtabs_matching",
25164 selftests::dw2_expand_symtabs_matching::run_test);
25165 selftests::register_test ("dwarf2_find_containing_comp_unit",
25166 selftests::find_containing_comp_unit::run_test);
25167 #endif
25168 }
This page took 0.859993 seconds and 4 git commands to generate.